mirror of
https://github.com/open-webui/computer.git
synced 2026-11-20 21:41:54 +03:00
refac
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
|
||||
`cptr` (short for "computer") runs on your machine and serves your whole computer (files, terminal, editor, git) to any browser. It literally is your computer.
|
||||
|
||||
Use it from your phone, tablet, laptop, another computer, or the machine it's running on. Designed to feel native on every screen. Plug in an AI that can actually read, write, and run things on your machine, or bring your favourite terminal agent. Terminal multiplexer, parallel AI agents, full workstation, one tool, your computer, any device.
|
||||
Use it from your phone, tablet, laptop, another computer, or the machine it's running on. Designed to feel native on every screen. Connect your own AI via API key, or plug in a coding agent you already subscribe to and skip the key entirely. Terminal multiplexer, parallel AI agents, full workstation, one tool, your computer, any device.
|
||||
|
||||
## Install
|
||||
|
||||
@@ -69,7 +69,7 @@ Or skip networking entirely and connect a [messaging bot](#messaging-bots) inste
|
||||
|
||||
## AI agent
|
||||
|
||||
Bring your own API key. Works with OpenAI, Anthropic, Ollama, or any OpenAI-compatible endpoint.
|
||||
Bring your own API key (OpenAI, Anthropic, Ollama, or any OpenAI-compatible endpoint), or connect a coding agent you already subscribe to.
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
@@ -91,7 +91,15 @@ Bring your own API key. Works with OpenAI, Anthropic, Ollama, or any OpenAI-comp
|
||||
| 🔌 **Tool servers** | Connect external tools via MCP or OpenAPI. |
|
||||
| 🧠 **Context compaction** | Long conversations are automatically summarised to stay fast. |
|
||||
|
||||
Already have a favourite terminal agent? Claude Code, Codex, Gemini CLI, Cursor, Grok, OpenCode, Kilo Code, and Pi all plug straight in. Use the subscription you already pay for.
|
||||
## Coding agents
|
||||
|
||||
Connect a coding agent as a native backend and use the subscription you already pay for. No separate API key needed.
|
||||
|
||||
**Codex** · **Claude Code** · **Cursor** · **Grok** · **OpenCode**
|
||||
|
||||
Add an agent profile from Settings, pick your models, and it shows up in the model selector like any other provider. Conversations run inside your workspace with full tool access and resume where you left off.
|
||||
|
||||
Prefer to run agents yourself? Any terminal agent (Gemini CLI, Kilo Code, Pi, and others) works in the terminal tab the way it always has.
|
||||
|
||||
## Messaging bots
|
||||
|
||||
|
||||
@@ -193,6 +193,8 @@
|
||||
const label = args.background ? 'Background sub-agent' : 'Sub-agent';
|
||||
return `${label}: "${t.length > 60 ? t.slice(0, 60) + '…' : t}"`;
|
||||
}
|
||||
case 'agent_tool':
|
||||
return args.title || 'Agent tool';
|
||||
default: {
|
||||
// External tool: {server_id}_{tool_name} → "tool_name (server_id)"
|
||||
const idx = name.indexOf('_');
|
||||
@@ -270,11 +272,26 @@
|
||||
for (const url of structuredImageUrls) {
|
||||
const escapedUrl = escapeRegExp(url);
|
||||
cleaned = cleaned.replace(new RegExp(`!\\[[^\\]]*\\]\\(${escapedUrl}\\)`, 'g'), '');
|
||||
cleaned = cleaned.replace(new RegExp(`(^|\\n)[ \\t]*${escapedUrl}[ \\t]*(?=\\n|$)`, 'g'), '$1');
|
||||
cleaned = cleaned.replace(
|
||||
new RegExp(`(^|\\n)[ \\t]*${escapedUrl}[ \\t]*(?=\\n|$)`, 'g'),
|
||||
'$1'
|
||||
);
|
||||
}
|
||||
return cleaned.replace(/\n{3,}/g, '\n\n').trim();
|
||||
}
|
||||
|
||||
function isPseudoAgentMessageTool(item: any): boolean {
|
||||
const title = String(item?.arguments?.title || item?.arguments?.item?.type || '').toLowerCase();
|
||||
return (
|
||||
item?.name === 'agent_tool' &&
|
||||
(title === 'usermessage' ||
|
||||
title === 'agentmessage' ||
|
||||
title === 'assistantmessage' ||
|
||||
title === 'assistant message' ||
|
||||
title === 'user message')
|
||||
);
|
||||
}
|
||||
|
||||
const displayItems = $derived.by((): DisplayItem[] => {
|
||||
if (!output?.length) return [];
|
||||
|
||||
@@ -310,6 +327,7 @@
|
||||
|
||||
for (const item of output) {
|
||||
if (item.type === 'function_call') {
|
||||
if (isPseudoAgentMessageTool(item)) continue;
|
||||
ensureGroup();
|
||||
currentGroup!.entries.push(item);
|
||||
currentGroup!.calls.push(item);
|
||||
|
||||
+106
-8
@@ -101,12 +101,23 @@ class AcpClient:
|
||||
async def notify(self, method: str, params: dict[str, Any]) -> None:
|
||||
await self._send({"jsonrpc": "2.0", "method": method, "params": params})
|
||||
|
||||
async def prompt(self, text: str) -> dict[str, Any]:
|
||||
async def prompt(self, text: str, images: list[dict[str, str]] | None = None) -> dict[str, Any]:
|
||||
if not self.session_id:
|
||||
raise RuntimeError("ACP session has not started")
|
||||
prompt: list[dict[str, str]] = []
|
||||
if text.strip():
|
||||
prompt.append({"type": "text", "text": text})
|
||||
for image in images or []:
|
||||
prompt.append(
|
||||
{
|
||||
"type": "image",
|
||||
"data": image["data"],
|
||||
"mimeType": image["mimeType"],
|
||||
}
|
||||
)
|
||||
return await self.request(
|
||||
"session/prompt",
|
||||
{"sessionId": self.session_id, "prompt": [{"type": "text", "text": text}]},
|
||||
{"sessionId": self.session_id, "prompt": prompt},
|
||||
)
|
||||
|
||||
async def cancel(self) -> None:
|
||||
@@ -121,11 +132,17 @@ class AcpClient:
|
||||
with suppress(Exception):
|
||||
await self.request(
|
||||
"session/set_config_option",
|
||||
{"sessionId": self.session_id, "configId": self.model_config_id, "value": model},
|
||||
{
|
||||
"sessionId": self.session_id,
|
||||
"configId": self.model_config_id,
|
||||
"value": model,
|
||||
},
|
||||
)
|
||||
return
|
||||
with suppress(Exception):
|
||||
await self.request("session/set_model", {"sessionId": self.session_id, "modelId": model})
|
||||
await self.request(
|
||||
"session/set_model", {"sessionId": self.session_id, "modelId": model}
|
||||
)
|
||||
|
||||
async def _open_session(self) -> None:
|
||||
setup: dict[str, Any] | None = None
|
||||
@@ -173,7 +190,11 @@ class AcpClient:
|
||||
pass
|
||||
|
||||
async def _handle_message(self, message: dict[str, Any]) -> None:
|
||||
if "id" in message and ("result" in message or "error" in message) and "method" not in message:
|
||||
if (
|
||||
"id" in message
|
||||
and ("result" in message or "error" in message)
|
||||
and "method" not in message
|
||||
):
|
||||
future = self.pending.pop(int(message["id"]), None)
|
||||
if future and not future.done():
|
||||
future.set_result(message)
|
||||
@@ -189,9 +210,9 @@ class AcpClient:
|
||||
async def _reply_permission(self, request_id: int, params: dict[str, Any]) -> None:
|
||||
option_id = None
|
||||
if self.auto_approve_permissions:
|
||||
option_id = _select_permission_option(params, "allow_always") or _select_permission_option(
|
||||
params, "allow_once"
|
||||
)
|
||||
option_id = _select_permission_option(
|
||||
params, "allow_always"
|
||||
) or _select_permission_option(params, "allow_once")
|
||||
outcome = (
|
||||
{"outcome": {"outcome": "selected", "optionId": option_id}}
|
||||
if option_id
|
||||
@@ -275,6 +296,83 @@ def acp_text_from_update(params: dict[str, Any]) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
def acp_tool_from_update(params: dict[str, Any]) -> dict[str, Any] | None:
|
||||
update = params.get("update")
|
||||
if not isinstance(update, dict):
|
||||
return None
|
||||
if update.get("sessionUpdate") not in {"tool_call", "tool_call_update"}:
|
||||
return None
|
||||
call_id = update.get("toolCallId")
|
||||
if not isinstance(call_id, str) or not call_id.strip():
|
||||
return None
|
||||
|
||||
raw_input = update.get("rawInput")
|
||||
command = _command_from_raw_input(raw_input)
|
||||
title = str(update.get("title") or update.get("kind") or "Agent tool").strip()
|
||||
status = _tool_status(update.get("status"))
|
||||
args: dict[str, Any] = {}
|
||||
if command:
|
||||
name = "run_command"
|
||||
args["command"] = command
|
||||
else:
|
||||
name = "agent_tool"
|
||||
args["title"] = title
|
||||
if raw_input is not None:
|
||||
args["input"] = raw_input
|
||||
output = _tool_output(update)
|
||||
return {
|
||||
"call_id": call_id.strip(),
|
||||
"name": name,
|
||||
"status": status,
|
||||
"arguments": args,
|
||||
"output": output,
|
||||
}
|
||||
|
||||
|
||||
def _tool_status(value: Any) -> str:
|
||||
normalized = str(value or "").strip().lower()
|
||||
if normalized in {"completed", "success"}:
|
||||
return "completed"
|
||||
if normalized in {"failed", "error"}:
|
||||
return "failed"
|
||||
if normalized in {"pending"}:
|
||||
return "pending"
|
||||
return "in_progress"
|
||||
|
||||
|
||||
def _command_from_raw_input(raw_input: Any) -> str | None:
|
||||
if not isinstance(raw_input, dict):
|
||||
return None
|
||||
command = raw_input.get("command")
|
||||
if isinstance(command, str) and command.strip():
|
||||
return command.strip()
|
||||
executable = raw_input.get("executable")
|
||||
args = raw_input.get("args")
|
||||
if isinstance(executable, str) and isinstance(args, list):
|
||||
parts = [executable, *(str(arg) for arg in args)]
|
||||
return " ".join(part for part in parts if part.strip())
|
||||
return None
|
||||
|
||||
|
||||
def _tool_output(update: dict[str, Any]) -> str | None:
|
||||
raw_output = update.get("rawOutput")
|
||||
if raw_output is not None:
|
||||
return raw_output if isinstance(raw_output, str) else json.dumps(raw_output, indent=2)
|
||||
content = update.get("content")
|
||||
if not isinstance(content, list):
|
||||
return None
|
||||
text: list[str] = []
|
||||
for item in content:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
nested = item.get("content")
|
||||
if isinstance(nested, dict) and nested.get("type") == "text":
|
||||
value = nested.get("text")
|
||||
if isinstance(value, str) and value.strip():
|
||||
text.append(value.strip())
|
||||
return "\n".join(text) or None
|
||||
|
||||
|
||||
def acp_models_from_setup(setup: dict[str, Any]) -> list[str]:
|
||||
models = setup.get("models")
|
||||
if not isinstance(models, dict):
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
"""Attachment preparation for coding agent turns."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from cptr.utils.storage import get_storage
|
||||
|
||||
|
||||
@dataclass
|
||||
class AgentAttachment:
|
||||
type: str
|
||||
name: str
|
||||
mime_type: str
|
||||
path: str
|
||||
base64: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class PreparedAgentAttachments:
|
||||
images: list[AgentAttachment]
|
||||
files: list[AgentAttachment]
|
||||
prompt_suffix: str
|
||||
|
||||
|
||||
def _safe_segment(value: str) -> str:
|
||||
safe = re.sub(r"[^a-zA-Z0-9_.-]+", "_", value).strip("._")
|
||||
return safe or "attachment"
|
||||
|
||||
|
||||
def _is_image(file_meta: dict[str, Any]) -> bool:
|
||||
content_type = str(file_meta.get("content_type") or file_meta.get("mime_type") or "")
|
||||
return file_meta.get("type") == "image" or content_type.startswith("image/")
|
||||
|
||||
|
||||
async def prepare_agent_attachments(
|
||||
*,
|
||||
workspace: str,
|
||||
chat_id: str,
|
||||
message_id: str,
|
||||
files: list[dict[str, Any]] | None,
|
||||
) -> PreparedAgentAttachments:
|
||||
if not files:
|
||||
return PreparedAgentAttachments(images=[], files=[], prompt_suffix="")
|
||||
|
||||
root = (
|
||||
Path(workspace)
|
||||
/ ".cptr"
|
||||
/ "attachments"
|
||||
/ _safe_segment(chat_id)
|
||||
/ _safe_segment(message_id)
|
||||
)
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
images: list[AgentAttachment] = []
|
||||
staged_files: list[AgentAttachment] = []
|
||||
storage = get_storage()
|
||||
for file_meta in files:
|
||||
if not isinstance(file_meta, dict):
|
||||
continue
|
||||
file_id = file_meta.get("id")
|
||||
if not isinstance(file_id, str) or not file_id:
|
||||
continue
|
||||
data = await storage.get(file_id)
|
||||
if data is None:
|
||||
raise RuntimeError(
|
||||
f"Uploaded file is no longer available: {file_meta.get('name') or file_id}"
|
||||
)
|
||||
|
||||
name = _safe_segment(str(file_meta.get("name") or file_id))
|
||||
path = root / f"{_safe_segment(file_id)}-{name}"
|
||||
path.write_bytes(data)
|
||||
mime_type = str(
|
||||
file_meta.get("content_type")
|
||||
or file_meta.get("mime_type")
|
||||
or ("image/png" if _is_image(file_meta) else "application/octet-stream")
|
||||
)
|
||||
attachment = AgentAttachment(
|
||||
type="image" if _is_image(file_meta) else "file",
|
||||
name=name,
|
||||
mime_type=mime_type,
|
||||
path=str(path),
|
||||
base64=base64.b64encode(data).decode(),
|
||||
)
|
||||
if attachment.type == "image":
|
||||
images.append(attachment)
|
||||
else:
|
||||
staged_files.append(attachment)
|
||||
|
||||
suffix = ""
|
||||
if staged_files:
|
||||
lines = "\n".join(f"- {item.path}" for item in staged_files)
|
||||
suffix = f"\n\nAttached files staged in the workspace:\n{lines}"
|
||||
return PreparedAgentAttachments(images=images, files=staged_files, prompt_suffix=suffix)
|
||||
@@ -7,7 +7,15 @@ import os
|
||||
import shlex
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
from cptr.utils.agents.events import AgentDone, AgentError, AgentEvent, AgentReasoningDelta, AgentTextDelta
|
||||
from cptr.utils.agents.attachments import PreparedAgentAttachments
|
||||
from cptr.utils.agents.events import (
|
||||
AgentDone,
|
||||
AgentError,
|
||||
AgentEvent,
|
||||
AgentReasoningDelta,
|
||||
AgentTextDelta,
|
||||
AgentToolUpdate,
|
||||
)
|
||||
|
||||
|
||||
def _prompt_from_messages(messages: list[dict[str, Any]]) -> str:
|
||||
@@ -26,6 +34,26 @@ def _prompt_from_messages(messages: list[dict[str, Any]]) -> str:
|
||||
return "\n\n".join(parts)
|
||||
|
||||
|
||||
def _claude_query_input(prompt: str, attachments: PreparedAgentAttachments) -> Any:
|
||||
if not attachments.images:
|
||||
return prompt
|
||||
content: list[dict[str, Any]] = []
|
||||
if prompt.strip():
|
||||
content.append({"type": "text", "text": prompt})
|
||||
for image in attachments.images:
|
||||
content.append(
|
||||
{
|
||||
"type": "image",
|
||||
"source": {
|
||||
"type": "base64",
|
||||
"media_type": image.mime_type,
|
||||
"data": image.base64,
|
||||
},
|
||||
}
|
||||
)
|
||||
return content
|
||||
|
||||
|
||||
def _chat_approval_mode(chat_params: dict[str, Any]) -> str:
|
||||
if "tool_approval_mode" in chat_params:
|
||||
return str(chat_params.get("tool_approval_mode") or "auto")
|
||||
@@ -42,6 +70,32 @@ def _permission_mode(chat_approval_mode: str) -> str:
|
||||
return "default"
|
||||
|
||||
|
||||
def _tool_update_from_claude_start(
|
||||
event: dict[str, Any],
|
||||
) -> tuple[int | None, AgentToolUpdate | None]:
|
||||
block = event.get("content_block")
|
||||
if not isinstance(block, dict):
|
||||
return None, None
|
||||
if block.get("type") not in {"tool_use", "server_tool_use", "mcp_tool_use"}:
|
||||
return None, None
|
||||
call_id = block.get("id")
|
||||
if not isinstance(call_id, str) or not call_id.strip():
|
||||
return None, None
|
||||
index = event.get("index")
|
||||
name = str(block.get("name") or block.get("type") or "Claude action").strip()
|
||||
raw_input = block.get("input")
|
||||
arguments = raw_input if isinstance(raw_input, dict) else {}
|
||||
return (
|
||||
index if isinstance(index, int) else None,
|
||||
AgentToolUpdate(
|
||||
call_id=call_id.strip(),
|
||||
name="agent_tool",
|
||||
status="in_progress",
|
||||
arguments={"title": name, **arguments},
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def run_claude_code_agent(
|
||||
*,
|
||||
profile: dict[str, Any],
|
||||
@@ -51,6 +105,7 @@ async def run_claude_code_agent(
|
||||
system_prompt: str,
|
||||
chat_params: dict[str, Any],
|
||||
resume_state: dict[str, Any] | None,
|
||||
attachments: PreparedAgentAttachments,
|
||||
) -> AsyncIterator[AgentEvent]:
|
||||
try:
|
||||
import claude_agent_sdk as sdk
|
||||
@@ -92,11 +147,17 @@ async def run_claude_code_agent(
|
||||
session_id = value if isinstance(value, str) and value else None
|
||||
|
||||
try:
|
||||
query = client.query(prompt, session_id=session_id) if session_id else client.query(prompt)
|
||||
query_input = _claude_query_input(prompt, attachments)
|
||||
query = (
|
||||
client.query(query_input, session_id=session_id)
|
||||
if session_id
|
||||
else client.query(query_input)
|
||||
)
|
||||
await asyncio.wait_for(query, timeout=20)
|
||||
stream = client.receive_response()
|
||||
usage: dict[str, Any] | None = None
|
||||
observed_session_id = session_id
|
||||
tool_calls: dict[int, AgentToolUpdate] = {}
|
||||
|
||||
async for message in stream:
|
||||
event = getattr(message, "event", None)
|
||||
@@ -105,12 +166,31 @@ async def run_claude_code_agent(
|
||||
if event_type == "content_block_delta":
|
||||
delta = event.get("delta", {})
|
||||
if isinstance(delta, dict):
|
||||
if delta.get("type") == "text_delta" and isinstance(delta.get("text"), str):
|
||||
if delta.get("type") == "text_delta" and isinstance(
|
||||
delta.get("text"), str
|
||||
):
|
||||
yield AgentTextDelta(delta["text"])
|
||||
elif delta.get("type") == "thinking_delta" and isinstance(
|
||||
delta.get("thinking"), str
|
||||
):
|
||||
yield AgentReasoningDelta(delta["thinking"])
|
||||
elif event_type == "content_block_start":
|
||||
index, tool = _tool_update_from_claude_start(event)
|
||||
if tool:
|
||||
if index is not None:
|
||||
tool_calls[index] = tool
|
||||
yield tool
|
||||
elif event_type == "content_block_stop":
|
||||
index = event.get("index")
|
||||
tool = tool_calls.get(index) if isinstance(index, int) else None
|
||||
if tool:
|
||||
yield AgentToolUpdate(
|
||||
call_id=tool.call_id,
|
||||
name=tool.name,
|
||||
status="completed",
|
||||
arguments=tool.arguments,
|
||||
output="",
|
||||
)
|
||||
continue
|
||||
|
||||
class_name = message.__class__.__name__
|
||||
@@ -125,7 +205,9 @@ async def run_claude_code_agent(
|
||||
if text:
|
||||
yield AgentReasoningDelta(text)
|
||||
elif class_name == "ResultMessage":
|
||||
observed_session_id = getattr(message, "session_id", None) or observed_session_id
|
||||
observed_session_id = (
|
||||
getattr(message, "session_id", None) or observed_session_id
|
||||
)
|
||||
raw_usage = getattr(message, "usage", None)
|
||||
if isinstance(raw_usage, dict):
|
||||
usage = {
|
||||
|
||||
@@ -5,15 +5,18 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
from contextlib import suppress
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
from cptr.utils.agents.attachments import PreparedAgentAttachments
|
||||
from cptr.utils.agents.events import (
|
||||
AgentDone,
|
||||
AgentError,
|
||||
AgentEvent,
|
||||
AgentReasoningDelta,
|
||||
AgentTextDelta,
|
||||
AgentToolUpdate,
|
||||
)
|
||||
|
||||
|
||||
@@ -101,8 +104,10 @@ class CodexAppServer:
|
||||
message = json.loads(line.decode())
|
||||
except Exception:
|
||||
continue
|
||||
if "id" in message and "method" not in message and (
|
||||
"result" in message or "error" in message
|
||||
if (
|
||||
"id" in message
|
||||
and "method" not in message
|
||||
and ("result" in message or "error" in message)
|
||||
):
|
||||
future = self.pending.pop(int(message["id"]), None)
|
||||
if future and not future.done():
|
||||
@@ -150,6 +155,59 @@ def _sandbox_for_approval(value: str) -> str:
|
||||
return "workspace-write"
|
||||
|
||||
|
||||
def _tool_from_item_event(method: str, params: dict[str, Any]) -> AgentToolUpdate | None:
|
||||
if method not in {"item/started", "item/completed"}:
|
||||
return None
|
||||
item = params.get("item") if isinstance(params.get("item"), dict) else {}
|
||||
item_type = str(item.get("type") or "").strip()
|
||||
normalized_type = re.sub(r"[^a-z0-9]+", " ", item_type.lower()).strip()
|
||||
if not _is_tool_item_type(normalized_type):
|
||||
return None
|
||||
call_id = params.get("itemId") or item.get("id") or item.get("callId")
|
||||
if not isinstance(call_id, str) or not call_id.strip():
|
||||
return None
|
||||
title = str(item.get("title") or item.get("name") or item_type or "Codex action").strip()
|
||||
detail = _item_detail(item)
|
||||
return AgentToolUpdate(
|
||||
call_id=call_id.strip(),
|
||||
name="run_command" if "command" in normalized_type and detail else "agent_tool",
|
||||
status="completed" if method == "item/completed" else "in_progress",
|
||||
arguments={"command": detail}
|
||||
if "command" in normalized_type and detail
|
||||
else {"title": title},
|
||||
output=detail if method == "item/completed" else None,
|
||||
)
|
||||
|
||||
|
||||
def _is_tool_item_type(normalized_type: str) -> bool:
|
||||
if not normalized_type:
|
||||
return False
|
||||
blocked = ("user", "agent message", "assistant", "reasoning", "thought", "plan", "todo")
|
||||
if any(value in normalized_type for value in blocked):
|
||||
return False
|
||||
allowed = (
|
||||
"command",
|
||||
"file change",
|
||||
"patch",
|
||||
"edit",
|
||||
"mcp",
|
||||
"dynamic tool",
|
||||
"collab",
|
||||
"web search",
|
||||
"image",
|
||||
"error",
|
||||
)
|
||||
return any(value in normalized_type for value in allowed)
|
||||
|
||||
|
||||
def _item_detail(item: dict[str, Any]) -> str | None:
|
||||
for key in ("command", "title", "summary", "text", "path", "prompt"):
|
||||
value = item.get(key)
|
||||
if isinstance(value, str) and value.strip():
|
||||
return value.strip()
|
||||
return None
|
||||
|
||||
|
||||
async def run_codex_agent(
|
||||
*,
|
||||
profile: dict[str, Any],
|
||||
@@ -159,6 +217,7 @@ async def run_codex_agent(
|
||||
system_prompt: str,
|
||||
chat_params: dict[str, Any],
|
||||
resume_state: dict[str, Any] | None,
|
||||
attachments: PreparedAgentAttachments,
|
||||
) -> AsyncIterator[AgentEvent]:
|
||||
env = os.environ.copy()
|
||||
if profile.get("home"):
|
||||
@@ -188,11 +247,21 @@ async def run_codex_agent(
|
||||
return
|
||||
|
||||
prompt = _messages_to_prompt(messages)
|
||||
turn_input: list[dict[str, Any]] = []
|
||||
if prompt:
|
||||
turn_input.append({"type": "text", "text": prompt})
|
||||
for image in attachments.images:
|
||||
turn_input.append(
|
||||
{
|
||||
"type": "image",
|
||||
"url": f"data:{image.mime_type};base64,{image.base64}",
|
||||
}
|
||||
)
|
||||
turn_response = await client.request(
|
||||
"turn/start",
|
||||
{
|
||||
"threadId": thread_id,
|
||||
"input": [{"type": "text", "text": prompt}],
|
||||
"input": turn_input,
|
||||
},
|
||||
)
|
||||
turn = turn_response.get("result", {}).get("turn", {})
|
||||
@@ -213,7 +282,11 @@ async def run_codex_agent(
|
||||
delta = params.get("delta")
|
||||
if isinstance(delta, str) and delta:
|
||||
yield AgentReasoningDelta(delta)
|
||||
elif method == "turn/completed":
|
||||
else:
|
||||
tool = _tool_from_item_event(str(method or ""), params)
|
||||
if tool:
|
||||
yield tool
|
||||
if method == "turn/completed":
|
||||
yield AgentDone(
|
||||
resume_state={
|
||||
"profile_id": profile["id"],
|
||||
|
||||
@@ -7,8 +7,20 @@ import os
|
||||
from contextlib import suppress
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
from cptr.utils.agents.acp import AcpClient, acp_event_stream, acp_text_from_update
|
||||
from cptr.utils.agents.events import AgentDone, AgentError, AgentEvent, AgentTextDelta
|
||||
from cptr.utils.agents.attachments import PreparedAgentAttachments
|
||||
from cptr.utils.agents.acp import (
|
||||
AcpClient,
|
||||
acp_event_stream,
|
||||
acp_text_from_update,
|
||||
acp_tool_from_update,
|
||||
)
|
||||
from cptr.utils.agents.events import (
|
||||
AgentDone,
|
||||
AgentError,
|
||||
AgentEvent,
|
||||
AgentTextDelta,
|
||||
AgentToolUpdate,
|
||||
)
|
||||
|
||||
|
||||
CURSOR_CAPABILITIES = {"_meta": {"parameterizedModelPicker": True}}
|
||||
@@ -45,6 +57,7 @@ async def run_cursor_agent(
|
||||
system_prompt: str,
|
||||
chat_params: dict[str, Any],
|
||||
resume_state: dict[str, Any] | None,
|
||||
attachments: PreparedAgentAttachments,
|
||||
) -> AsyncIterator[AgentEvent]:
|
||||
env = os.environ.copy()
|
||||
if profile.get("home"):
|
||||
@@ -79,24 +92,35 @@ async def run_cursor_agent(
|
||||
if system_prompt:
|
||||
prompt = f"{system_prompt}\n\n{prompt}" if prompt else system_prompt
|
||||
|
||||
prompt_task = asyncio.create_task(client.prompt(prompt))
|
||||
images = [
|
||||
{"data": image.base64, "mimeType": image.mime_type} for image in attachments.images
|
||||
]
|
||||
prompt_task = asyncio.create_task(client.prompt(prompt, images=images))
|
||||
try:
|
||||
async for event in acp_event_stream(client):
|
||||
params = event.get("params") if isinstance(event.get("params"), dict) else {}
|
||||
text = acp_text_from_update(params)
|
||||
if text:
|
||||
yield AgentTextDelta(text)
|
||||
tool = acp_tool_from_update(params)
|
||||
if tool:
|
||||
yield AgentToolUpdate(**tool)
|
||||
if prompt_task.done():
|
||||
try:
|
||||
next_event = await asyncio.wait_for(client.events.get(), timeout=0.25)
|
||||
except asyncio.TimeoutError:
|
||||
break
|
||||
next_params = (
|
||||
next_event.get("params") if isinstance(next_event.get("params"), dict) else {}
|
||||
next_event.get("params")
|
||||
if isinstance(next_event.get("params"), dict)
|
||||
else {}
|
||||
)
|
||||
next_text = acp_text_from_update(next_params)
|
||||
if next_text:
|
||||
yield AgentTextDelta(next_text)
|
||||
next_tool = acp_tool_from_update(next_params)
|
||||
if next_tool:
|
||||
yield AgentToolUpdate(**next_tool)
|
||||
await prompt_task
|
||||
finally:
|
||||
if not prompt_task.done():
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -15,6 +16,15 @@ class AgentReasoningDelta:
|
||||
text: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class AgentToolUpdate:
|
||||
call_id: str
|
||||
status: str
|
||||
name: str | None = None
|
||||
arguments: dict[str, Any] | None = None
|
||||
output: str | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class AgentDone:
|
||||
usage: dict[str, Any] | None = None
|
||||
@@ -26,4 +36,4 @@ class AgentError:
|
||||
message: str
|
||||
|
||||
|
||||
AgentEvent = AgentTextDelta | AgentReasoningDelta | AgentDone | AgentError
|
||||
AgentEvent = AgentTextDelta | AgentReasoningDelta | AgentToolUpdate | AgentDone | AgentError
|
||||
|
||||
@@ -7,8 +7,20 @@ import os
|
||||
from contextlib import suppress
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
from cptr.utils.agents.acp import AcpClient, acp_event_stream, acp_text_from_update
|
||||
from cptr.utils.agents.events import AgentDone, AgentError, AgentEvent, AgentTextDelta
|
||||
from cptr.utils.agents.attachments import PreparedAgentAttachments
|
||||
from cptr.utils.agents.acp import (
|
||||
AcpClient,
|
||||
acp_event_stream,
|
||||
acp_text_from_update,
|
||||
acp_tool_from_update,
|
||||
)
|
||||
from cptr.utils.agents.events import (
|
||||
AgentDone,
|
||||
AgentError,
|
||||
AgentEvent,
|
||||
AgentTextDelta,
|
||||
AgentToolUpdate,
|
||||
)
|
||||
|
||||
|
||||
def _prompt_from_messages(messages: list[dict[str, Any]]) -> str:
|
||||
@@ -46,6 +58,7 @@ async def run_grok_agent(
|
||||
system_prompt: str,
|
||||
chat_params: dict[str, Any],
|
||||
resume_state: dict[str, Any] | None,
|
||||
attachments: PreparedAgentAttachments,
|
||||
) -> AsyncIterator[AgentEvent]:
|
||||
env = os.environ.copy()
|
||||
if profile.get("home"):
|
||||
@@ -74,24 +87,35 @@ async def run_grok_agent(
|
||||
if system_prompt:
|
||||
prompt = f"{system_prompt}\n\n{prompt}" if prompt else system_prompt
|
||||
|
||||
prompt_task = asyncio.create_task(client.prompt(prompt))
|
||||
images = [
|
||||
{"data": image.base64, "mimeType": image.mime_type} for image in attachments.images
|
||||
]
|
||||
prompt_task = asyncio.create_task(client.prompt(prompt, images=images))
|
||||
try:
|
||||
async for event in acp_event_stream(client):
|
||||
params = event.get("params") if isinstance(event.get("params"), dict) else {}
|
||||
text = acp_text_from_update(params)
|
||||
if text:
|
||||
yield AgentTextDelta(text)
|
||||
tool = acp_tool_from_update(params)
|
||||
if tool:
|
||||
yield AgentToolUpdate(**tool)
|
||||
if prompt_task.done():
|
||||
try:
|
||||
next_event = await asyncio.wait_for(client.events.get(), timeout=0.25)
|
||||
except asyncio.TimeoutError:
|
||||
break
|
||||
next_params = (
|
||||
next_event.get("params") if isinstance(next_event.get("params"), dict) else {}
|
||||
next_event.get("params")
|
||||
if isinstance(next_event.get("params"), dict)
|
||||
else {}
|
||||
)
|
||||
next_text = acp_text_from_update(next_params)
|
||||
if next_text:
|
||||
yield AgentTextDelta(next_text)
|
||||
next_tool = acp_tool_from_update(next_params)
|
||||
if next_tool:
|
||||
yield AgentToolUpdate(**next_tool)
|
||||
await prompt_task
|
||||
finally:
|
||||
if not prompt_task.done():
|
||||
|
||||
@@ -9,11 +9,19 @@ import os
|
||||
import re
|
||||
import socket
|
||||
from contextlib import asynccontextmanager, suppress
|
||||
from pathlib import Path
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
import httpx
|
||||
|
||||
from cptr.utils.agents.events import AgentDone, AgentError, AgentEvent, AgentTextDelta
|
||||
from cptr.utils.agents.attachments import PreparedAgentAttachments
|
||||
from cptr.utils.agents.events import (
|
||||
AgentDone,
|
||||
AgentError,
|
||||
AgentEvent,
|
||||
AgentTextDelta,
|
||||
AgentToolUpdate,
|
||||
)
|
||||
|
||||
|
||||
def _prompt_from_messages(messages: list[dict[str, Any]]) -> str:
|
||||
@@ -137,6 +145,22 @@ def _parse_model(model: str) -> dict[str, str]:
|
||||
return {"providerID": provider_id, "modelID": model_id}
|
||||
|
||||
|
||||
def _opencode_parts(prompt: str, attachments: PreparedAgentAttachments) -> list[dict[str, Any]]:
|
||||
parts: list[dict[str, Any]] = []
|
||||
if prompt.strip():
|
||||
parts.append({"type": "text", "text": prompt})
|
||||
for item in [*attachments.images, *attachments.files]:
|
||||
parts.append(
|
||||
{
|
||||
"type": "file",
|
||||
"mime": item.mime_type,
|
||||
"filename": item.name,
|
||||
"url": Path(item.path).resolve().as_uri(),
|
||||
}
|
||||
)
|
||||
return parts
|
||||
|
||||
|
||||
def _text_from_event(event: dict[str, Any], emitted: dict[str, str]) -> str | None:
|
||||
event_type = event.get("type")
|
||||
props = event.get("properties") if isinstance(event.get("properties"), dict) else {}
|
||||
@@ -161,6 +185,49 @@ def _text_from_event(event: dict[str, Any], emitted: dict[str, str]) -> str | No
|
||||
return None
|
||||
|
||||
|
||||
def _tool_from_event(event: dict[str, Any]) -> AgentToolUpdate | None:
|
||||
if event.get("type") != "message.part.updated":
|
||||
return None
|
||||
props = event.get("properties") if isinstance(event.get("properties"), dict) else {}
|
||||
part = props.get("part") if isinstance(props.get("part"), dict) else {}
|
||||
if part.get("type") != "tool":
|
||||
return None
|
||||
call_id = part.get("callID") or part.get("id")
|
||||
if not isinstance(call_id, str) or not call_id.strip():
|
||||
return None
|
||||
state = part.get("state") if isinstance(part.get("state"), dict) else {}
|
||||
tool = str(part.get("tool") or state.get("title") or "Agent tool").strip()
|
||||
status = _opencode_tool_status(state.get("status"))
|
||||
output = _opencode_tool_output(state)
|
||||
return AgentToolUpdate(
|
||||
call_id=call_id.strip(),
|
||||
name="agent_tool",
|
||||
status=status,
|
||||
arguments={"title": tool, **({"state": state} if state else {})},
|
||||
output=output,
|
||||
)
|
||||
|
||||
|
||||
def _opencode_tool_status(value: Any) -> str:
|
||||
normalized = str(value or "").strip().lower()
|
||||
if normalized == "completed":
|
||||
return "completed"
|
||||
if normalized == "error":
|
||||
return "failed"
|
||||
if normalized == "pending":
|
||||
return "pending"
|
||||
return "in_progress"
|
||||
|
||||
|
||||
def _opencode_tool_output(state: dict[str, Any]) -> str | None:
|
||||
for key in ("output", "result", "message", "error"):
|
||||
value = state.get(key)
|
||||
if value is None:
|
||||
continue
|
||||
return value if isinstance(value, str) else json.dumps(value, indent=2)
|
||||
return None
|
||||
|
||||
|
||||
async def run_opencode_agent(
|
||||
*,
|
||||
profile: dict[str, Any],
|
||||
@@ -170,6 +237,7 @@ async def run_opencode_agent(
|
||||
system_prompt: str,
|
||||
chat_params: dict[str, Any],
|
||||
resume_state: dict[str, Any] | None,
|
||||
attachments: PreparedAgentAttachments,
|
||||
) -> AsyncIterator[AgentEvent]:
|
||||
del chat_params, resume_state
|
||||
try:
|
||||
@@ -190,7 +258,7 @@ async def run_opencode_agent(
|
||||
raise RuntimeError("OpenCode did not return a session id")
|
||||
|
||||
emitted: dict[str, str] = {}
|
||||
event_queue: asyncio.Queue[str | None] = asyncio.Queue()
|
||||
event_queue: asyncio.Queue[AgentEvent | None] = asyncio.Queue()
|
||||
event_task = asyncio.create_task(
|
||||
_collect_opencode_events(client, headers, session_id, emitted, event_queue)
|
||||
)
|
||||
@@ -199,6 +267,7 @@ async def run_opencode_agent(
|
||||
if system_prompt:
|
||||
prompt = f"{system_prompt}\n\n{prompt}" if prompt else system_prompt
|
||||
parsed_model = _parse_model(model)
|
||||
parts = _opencode_parts(prompt, attachments)
|
||||
try:
|
||||
await _request(
|
||||
client,
|
||||
@@ -208,7 +277,7 @@ async def run_opencode_agent(
|
||||
json_body={
|
||||
"sessionID": session_id,
|
||||
"model": parsed_model,
|
||||
"parts": [{"type": "text", "text": prompt}],
|
||||
"parts": parts,
|
||||
},
|
||||
)
|
||||
|
||||
@@ -216,7 +285,7 @@ async def run_opencode_agent(
|
||||
item = await event_queue.get()
|
||||
if item is None:
|
||||
break
|
||||
yield AgentTextDelta(item)
|
||||
yield item
|
||||
except asyncio.CancelledError:
|
||||
with suppress(Exception):
|
||||
await _request(
|
||||
@@ -251,7 +320,7 @@ async def _collect_opencode_events(
|
||||
headers: dict[str, str],
|
||||
session_id: str,
|
||||
emitted: dict[str, str],
|
||||
queue: asyncio.Queue[str | None],
|
||||
queue: asyncio.Queue[AgentEvent | None],
|
||||
) -> None:
|
||||
try:
|
||||
for path in ("event.subscribe", "event/subscribe", "event"):
|
||||
@@ -276,11 +345,17 @@ async def _collect_opencode_events(
|
||||
continue
|
||||
text = _text_from_event(event, emitted)
|
||||
if text:
|
||||
await queue.put(text)
|
||||
await queue.put(AgentTextDelta(text))
|
||||
tool = _tool_from_event(event)
|
||||
if tool:
|
||||
await queue.put(tool)
|
||||
status = (
|
||||
props.get("status") if isinstance(props.get("status"), dict) else {}
|
||||
)
|
||||
if event.get("type") == "session.status" and status.get("type") == "idle":
|
||||
if (
|
||||
event.get("type") == "session.status"
|
||||
and status.get("type") == "idle"
|
||||
):
|
||||
await queue.put(None)
|
||||
return
|
||||
except Exception:
|
||||
|
||||
+102
-2
@@ -35,7 +35,9 @@ from cptr.utils.agents.events import (
|
||||
AgentError,
|
||||
AgentReasoningDelta,
|
||||
AgentTextDelta,
|
||||
AgentToolUpdate,
|
||||
)
|
||||
from cptr.utils.agents.attachments import prepare_agent_attachments
|
||||
from cptr.utils.model_targets import AgentModelTarget, ApiModelTarget, ModelTarget
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -567,11 +569,21 @@ async def _load_message_history(chat_id: str, message_id: str) -> tuple[list[dic
|
||||
# reasoning_items) followed by the corresponding tool-result
|
||||
# messages.
|
||||
if m.output:
|
||||
native_agent_call_ids = {
|
||||
item["call_id"]
|
||||
for item in m.output
|
||||
if item.get("type") == "function_call"
|
||||
and item.get("call_id")
|
||||
and _is_native_agent_tool_item(item)
|
||||
}
|
||||
# ── Collect the set of call_ids that have outputs ──
|
||||
# This is needed to filter out orphaned function_calls
|
||||
# (e.g. from crashes, partial persistence, or data corruption).
|
||||
output_call_ids = {
|
||||
item["call_id"] for item in m.output if item.get("type") == "function_call_output"
|
||||
item["call_id"]
|
||||
for item in m.output
|
||||
if item.get("type") == "function_call_output"
|
||||
and item.get("call_id") not in native_agent_call_ids
|
||||
}
|
||||
|
||||
# ── Group output items into per-iteration turns ──
|
||||
@@ -588,6 +600,8 @@ async def _load_message_history(chat_id: str, message_id: str) -> tuple[list[dic
|
||||
current_turn = {"reasoning": [], "calls": [], "outputs": []}
|
||||
current_turn["reasoning"].append(item)
|
||||
elif itype == "function_call" and item.get("status") == "completed":
|
||||
if item.get("call_id") in native_agent_call_ids:
|
||||
continue
|
||||
# Only include calls that have a matching output
|
||||
if item["call_id"] not in output_call_ids:
|
||||
logger.warning(
|
||||
@@ -613,7 +627,10 @@ async def _load_message_history(chat_id: str, message_id: str) -> tuple[list[dic
|
||||
if item.get("fc_id"):
|
||||
tc["fc_id"] = item["fc_id"]
|
||||
current_turn["calls"].append(tc)
|
||||
elif itype == "function_call_output":
|
||||
elif (
|
||||
itype == "function_call_output"
|
||||
and item.get("call_id") not in native_agent_call_ids
|
||||
):
|
||||
current_turn["outputs"].append(item)
|
||||
# Skip other types (message, artifact, pending calls, etc.)
|
||||
|
||||
@@ -983,6 +1000,38 @@ def _upsert_output_item(items: list[dict], item: dict) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def _safe_tool_name(name: str | None) -> str:
|
||||
if not name:
|
||||
return "agent_tool"
|
||||
value = re.sub(r"[^a-zA-Z0-9_-]+", "_", name.strip()).strip("_")
|
||||
return value or "agent_tool"
|
||||
|
||||
|
||||
def _is_native_agent_tool_item(item: dict) -> bool:
|
||||
return bool(
|
||||
item.get("native_agent")
|
||||
or (isinstance(item.get("id"), str) and item["id"].startswith("agent-"))
|
||||
)
|
||||
|
||||
|
||||
def _append_prompt_suffix(messages: list[dict], suffix: str) -> None:
|
||||
if not suffix:
|
||||
return
|
||||
for message in reversed(messages):
|
||||
if message.get("role") != "user":
|
||||
continue
|
||||
content = message.get("content", "")
|
||||
if isinstance(content, list):
|
||||
for block in content:
|
||||
if isinstance(block, dict) and block.get("type") == "text":
|
||||
block["text"] = f"{block.get('text') or ''}{suffix}"
|
||||
return
|
||||
content.append({"type": "text", "text": suffix.strip()})
|
||||
else:
|
||||
message["content"] = f"{content or ''}{suffix}"
|
||||
return
|
||||
|
||||
|
||||
def _find_safe_split(messages: list[dict], target_keep: int) -> int:
|
||||
"""Find a safe split index that doesn't break tool call pairs.
|
||||
|
||||
@@ -1226,6 +1275,19 @@ async def run_chat_task(
|
||||
)
|
||||
if loaded_summary:
|
||||
system += f"\n\n[CONVERSATION SUMMARY]\n{loaded_summary}"
|
||||
current_user_files = []
|
||||
if msg and msg.parent_id:
|
||||
parent_msg = await ChatMessage.get_by_id(msg.parent_id)
|
||||
meta_files = (parent_msg.meta or {}).get("files") if parent_msg else None
|
||||
if isinstance(meta_files, list):
|
||||
current_user_files = meta_files
|
||||
agent_attachments = await prepare_agent_attachments(
|
||||
workspace=workspace,
|
||||
chat_id=chat_id,
|
||||
message_id=(msg.parent_id if msg and msg.parent_id else message_id),
|
||||
files=current_user_files,
|
||||
)
|
||||
_append_prompt_suffix(messages, agent_attachments.prompt_suffix)
|
||||
if regeneration_prompt:
|
||||
messages.append({"role": "user", "content": regeneration_prompt})
|
||||
if chat_params.get("plan_mode", False):
|
||||
@@ -1259,6 +1321,7 @@ async def run_chat_task(
|
||||
system_prompt=system,
|
||||
chat_params=chat_params,
|
||||
resume_state=resume_state,
|
||||
attachments=agent_attachments,
|
||||
):
|
||||
if isinstance(event, AgentTextDelta):
|
||||
content += event.text
|
||||
@@ -1276,6 +1339,43 @@ async def run_chat_task(
|
||||
_upsert_output_item(output_items, item)
|
||||
await emit(output=item)
|
||||
_sync_state()
|
||||
elif isinstance(event, AgentToolUpdate):
|
||||
flushed_item = _flush_text()
|
||||
if flushed_item:
|
||||
await emit(output=flushed_item)
|
||||
existing = next(
|
||||
(
|
||||
item
|
||||
for item in output_items
|
||||
if item.get("type") == "function_call"
|
||||
and item.get("call_id") == event.call_id
|
||||
),
|
||||
{},
|
||||
)
|
||||
call_item = {
|
||||
"type": "function_call",
|
||||
"id": existing.get("id") or f"agent-{event.call_id}",
|
||||
"call_id": event.call_id,
|
||||
"name": _safe_tool_name(event.name or existing.get("name")),
|
||||
"native_agent": True,
|
||||
"arguments": {
|
||||
**(existing.get("arguments") or {}),
|
||||
**(event.arguments or {}),
|
||||
},
|
||||
"status": event.status,
|
||||
}
|
||||
_upsert_output_item(output_items, call_item)
|
||||
await emit(output=call_item)
|
||||
if event.output is not None or event.status == "completed":
|
||||
output_item = {
|
||||
"type": "function_call_output",
|
||||
"call_id": event.call_id,
|
||||
"native_agent": True,
|
||||
"output": event.output or "",
|
||||
}
|
||||
_upsert_output_item(output_items, output_item)
|
||||
await emit(output=output_item)
|
||||
_sync_state()
|
||||
elif isinstance(event, AgentError):
|
||||
raise RuntimeError(event.message)
|
||||
elif isinstance(event, AgentDone):
|
||||
|
||||
Reference in New Issue
Block a user