diff --git a/CHANGELOG.md b/CHANGELOG.md index d057b9b..092981b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ + [修复] 修复 Agent 发送后的运行提示闪退及回复需要切换标签页才显示的问题。 + [优化] Agent 历史记录支持多选批量删除,点击记录可直接进入对话。 + [优化] Canvas Agent 改为从独立指令文件初始化当前工作目录的 AGENTS.md,不再为每轮消息重复拼接前置提示词。 ++ [优化] 重构 Canvas Agent 源码目录,按 Agent、画布、服务与通用工具拆分模块职责,提升代码可维护性。 ## v0.11.0 - 2026-07-28 diff --git a/canvas-agent/codex-server.md b/canvas-agent/codex-server.md new file mode 100644 index 0000000..caba0ee --- /dev/null +++ b/canvas-agent/codex-server.md @@ -0,0 +1,2175 @@ +# Codex App Server + +> For the complete documentation index, see [llms.txt](https://learn.chatgpt.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL. + +Codex app-server is the interface Codex uses to power rich clients (for example, the Codex VS Code extension). Use it when you want a deep integration inside your own product: authentication, conversation history, approvals, and streamed agent events. The app-server implementation is open source in the Codex GitHub repository ([openai/codex/codex-rs/app-server](https://github.com/openai/codex/tree/main/codex-rs/app-server)). See the [Open Source](https://learn.chatgpt.com/docs/open-source) page for the full list of open-source Codex components. + +If you are automating jobs or running Codex in CI, use the +[Codex SDK](https://learn.chatgpt.com/docs/codex-sdk) instead. + +## Connect the CLI terminal UI + +Remote terminal UI mode lets you run app-server on one machine and connect the +Codex CLI terminal interface from another. Start a WebSocket listener: + +```bash +codex app-server --listen ws://127.0.0.1:4500 +``` + +Then connect the terminal UI: + +```bash +codex --remote ws://127.0.0.1:4500 +``` + +For a non-local connection, configure WebSocket authentication and put the +connection behind TLS. Store the bearer token in an environment variable and +pass its name instead of putting the token on the command line: + +```bash +export CODEX_REMOTE_TOKEN="$(cat "$HOME/.codex/app-server-token")" +codex --remote wss://remote-host:4500 \ + --remote-auth-token-env CODEX_REMOTE_TOKEN +``` + +The `--remote` option accepts `ws://`, `wss://`, `unix://`, and +`unix://PATH` endpoints. Use plain WebSockets only for localhost or an SSH +port-forwarded connection. + +## Protocol + +Like [MCP](https://modelcontextprotocol.io/), `codex app-server` supports bidirectional communication using JSON-RPC 2.0 messages (with the `"jsonrpc":"2.0"` header omitted on the wire). + +Supported transports: + +- `stdio` (`--listen stdio://`, default): newline-delimited JSON (JSONL). +- `websocket` (`--listen ws://IP:PORT`, experimental and unsupported): one + JSON-RPC message per WebSocket text frame. +- Unix socket (`--listen unix://` or `--listen unix://PATH`): WebSocket + connections over Codex's default app-server control socket or a custom Unix + socket path, using the standard HTTP Upgrade handshake. +- `off` (`--listen off`): don't expose a local transport. + +When you run with `--listen ws://IP:PORT`, the same listener also serves basic +HTTP health probes: + +- `GET /readyz` returns `200 OK` once the listener accepts new connections. +- `GET /healthz` returns `200 OK` when the request doesn't include an `Origin` + header. +- Requests with an `Origin` header are rejected with `403 Forbidden`. + +WebSocket transport is experimental and unsupported. Local listeners such as +`ws://127.0.0.1:PORT` are appropriate for localhost and SSH port-forwarding +workflows. Non-loopback WebSocket listeners currently allow unauthenticated +connections by default during rollout, so configure WebSocket auth before +exposing one remotely. + +Supported WebSocket auth flags: + +- `--ws-auth capability-token --ws-token-file /absolute/path` +- `--ws-auth capability-token --ws-token-sha256 HEX` +- `--ws-auth signed-bearer-token --ws-shared-secret-file /absolute/path` + +For signed bearer tokens, you can also set `--ws-issuer`, `--ws-audience`, and +`--ws-max-clock-skew-seconds`. Clients present the credential as +`Authorization: Bearer ` during the WebSocket handshake, and app-server +enforces auth before JSON-RPC `initialize`. + +Prefer `--ws-token-file` over passing raw bearer tokens on the command line. Use +`--ws-token-sha256` only when the client keeps the raw high-entropy token in a +separate local secret store; the hash is only a verifier, and clients still need +the original token. + +In WebSocket mode, app-server uses bounded queues. When request ingress is full, +the server rejects new requests with JSON-RPC error code `-32001` and message +`"Server overloaded; retry later."` Clients should retry with an exponentially +increasing delay and jitter. + +## Message schema + +Requests include `method`, `params`, and `id`: + +```json +{ "method": "thread/start", "id": 10, "params": { "model": "gpt-5.4" } } +``` + +Responses echo the `id` with either `result` or `error`: + +```json +{ "id": 10, "result": { "thread": { "id": "thr_123" } } } +``` + +```json +{ "id": 10, "error": { "code": 123, "message": "Something went wrong" } } +``` + +Notifications omit `id` and use only `method` and `params`: + +```json +{ "method": "turn/started", "params": { "turn": { "id": "turn_456" } } } +``` + +You can generate a TypeScript schema or a JSON Schema bundle from the CLI. Each output is specific to the Codex version you ran, so the generated artifacts match that version exactly: + +```bash +codex app-server generate-ts --out ./schemas +codex app-server generate-json-schema --out ./schemas +``` + +## Getting started + +1. Start the server with `codex app-server` (default stdio transport), + `codex app-server --listen ws://127.0.0.1:4500` (TCP WebSocket), or + `codex app-server --listen unix://` (default Unix socket). +2. Connect a client over the selected transport, then send `initialize` followed by the `initialized` notification. +3. Start a thread and a turn, then keep reading notifications from the active transport stream. + +Example (Node.js / TypeScript): + +```ts + + + +const proc = spawn("codex", ["app-server"], { + stdio: ["pipe", "pipe", "inherit"], +}); +const rl = readline.createInterface({ input: proc.stdout }); + +const send = (message: unknown) => { + proc.stdin.write(`${JSON.stringify(message)}\n`); +}; + +let threadId: string | null = null; + +rl.on("line", (line) => { + const msg = JSON.parse(line) as any; + console.log("server:", msg); + + if (msg.id === 1 && msg.result?.thread?.id && !threadId) { + threadId = msg.result.thread.id; + send({ + method: "turn/start", + id: 2, + params: { + threadId, + input: [{ type: "text", text: "Summarize this repo." }], + }, + }); + } +}); + +send({ + method: "initialize", + id: 0, + params: { + clientInfo: { + name: "my_product", + title: "My Product", + version: "0.1.0", + }, + }, +}); +send({ method: "initialized", params: {} }); +send({ method: "thread/start", id: 1, params: { model: "gpt-5.4" } }); +``` + +## Core primitives + +- **Thread**: A conversation between a user and the Codex agent. Threads contain turns. +- **Turn**: A single user request and the agent work that follows. Turns contain items and stream incremental updates. +- **Item**: A unit of input or output (user message, agent message, command runs, file change, tool call, and more). + +Use the thread APIs to create, list, or archive conversations. Drive a conversation with turn APIs and stream progress via turn notifications. + +## Lifecycle overview + +- **Initialize once per connection**: Immediately after opening a transport connection, send an `initialize` request with your client metadata, then emit `initialized`. The server rejects any request on that connection before this handshake. +- **Start (or resume) a thread**: Call `thread/start` for a new conversation, `thread/resume` to continue an existing one, or `thread/fork` to branch history into a new thread id. +- **Begin a turn**: Call `turn/start` with the target `threadId` and user input. Optional fields override model, personality, `cwd`, sandbox policy, and more. +- **Steer an active turn**: Call `turn/steer` to append user input to the currently in-flight turn without creating a new turn. +- **Stream events**: After `turn/start`, keep reading notifications on stdout: `thread/archived`, `thread/unarchived`, `item/started`, `item/completed`, `item/agentMessage/delta`, tool progress, and other updates. +- **Finish the turn**: The server emits `turn/completed` with final status when the model finishes or after a `turn/interrupt` cancellation. + +## Initialization + +Clients must send a single `initialize` request per transport connection before invoking any other method on that connection, then acknowledge with an `initialized` notification. Requests sent before initialization receive a `Not initialized` error, and repeated `initialize` calls on the same connection return `Already initialized`. + +The server returns the user agent string it will present to upstream services plus `platformFamily` and `platformOs` values that describe the runtime target. Set `clientInfo` to identify your integration. + +`initialize.params.capabilities` also supports these client capabilities: + +- `optOutNotificationMethods` - exact notification method names to suppress for + this connection. Matching is exact (no wildcards or prefixes); unknown names + are accepted and ignored. +- `requestAttestation` - opt into the server-initiated `attestation/generate` + request. Desktop hosts that provide upstream attestation respond with an + opaque `{ "token": "..." }` value. +- `mcpServerOpenaiFormElicitation` - allow downstream MCP servers to send the + OpenAI extended-form variant of `mcpServer/elicitation/request`. + +**Important**: Use `clientInfo.name` to identify your client for the OpenAI Compliance Logs Platform. If you are developing a new Codex integration intended for enterprise use, please contact OpenAI to get it added to a known clients list. For more context, see the [Codex logs reference](https://chatgpt.com/admin/api-reference#tag/Logs:-Codex). + +Example (from the Codex VS Code extension): + +```json +{ + "method": "initialize", + "id": 0, + "params": { + "clientInfo": { + "name": "codex_vscode", + "title": "Codex VS Code Extension", + "version": "0.1.0" + } + } +} +``` + +Example with notification opt-out: + +```json +{ + "method": "initialize", + "id": 1, + "params": { + "clientInfo": { + "name": "my_client", + "title": "My Client", + "version": "0.1.0" + }, + "capabilities": { + "experimentalApi": true, + "optOutNotificationMethods": ["thread/started", "item/agentMessage/delta"] + } + } +} +``` + +## Experimental API opt-in + +Some app-server methods and fields are intentionally gated behind `experimentalApi` capability. + +- Omit `capabilities` (or set `experimentalApi` to `false`) to stay on the stable API surface, and the server rejects experimental methods/fields. +- Set `capabilities.experimentalApi` to `true` to enable experimental methods and fields. + +```json +{ + "method": "initialize", + "id": 1, + "params": { + "clientInfo": { + "name": "my_client", + "title": "My Client", + "version": "0.1.0" + }, + "capabilities": { + "experimentalApi": true + } + } +} +``` + +If a client sends an experimental method or field without opting in, app-server rejects it with: + +` requires experimentalApi capability` + +## API overview + +- `thread/start` - create a new thread; emits `thread/started` and automatically subscribes you to turn/item events for that thread. +- `thread/resume` - reopen an existing thread by id so later `turn/start` calls append to it. +- `thread/fork` - fork a thread into a new thread id by copying stored history. Pass `lastTurnId` to copy history through that turn and omit later turns. Emits `thread/started` for the new thread; returned threads include `forkedFromId` when available. +- `thread/read` - read a stored thread by id without resuming it; set `includeTurns` to return full turn history. Returned `thread` objects include runtime `status`. +- `thread/list` - page through stored thread logs; supports cursor-based pagination plus `modelProviders`, `sourceKinds`, `archived`, `cwd`, `useStateDbOnly`, `searchTerm`, and experimental `parentThreadId` or `ancestorThreadId` filters. Returned `thread` objects include runtime `status`. +- `thread/turns/list` - experimental; page through a stored thread's turn history without resuming it. `itemsView` controls whether turn items are omitted, summarized, or fully loaded. +- `thread/items/list` - experimental; page through persisted thread items, optionally restricted to one `turnId`. The active thread store must support item pagination. +- `thread/loaded/list` - list the thread ids currently loaded in memory. +- `thread/name/set` - set or update a thread's user-facing name for a loaded thread or a persisted rollout; emits `thread/name/updated`. +- `thread/goal/set` - set the goal for a thread; emits `thread/goal/updated`. +- `thread/goal/get` - read the current goal for a thread. +- `thread/goal/clear` - clear the goal for a thread; emits `thread/goal/cleared`. +- `thread/metadata/update` - patch SQLite-backed stored thread metadata; currently supports persisted `gitInfo`. +- `thread/archive` - move a thread's log file into the archived directory and attempt to archive spawned descendant thread logs that aren't already archived; returns `{}` on success and emits `thread/archived` for each archived thread. +- `thread/delete` - permanently delete a persisted active or archived thread and any spawned descendant threads; returns `{}` on success and emits `thread/deleted` for each deleted thread. +- `thread/unsubscribe` - unsubscribe this connection from thread turn/item events. If this was the last subscriber, the server unloads the thread after a no-subscriber inactivity grace period and emits `thread/closed`. +- `thread/unarchive` - restore an archived thread rollout back into the active sessions directory; returns the restored `thread` and emits `thread/unarchived`. +- `thread/status/changed` - notification emitted when a loaded thread's runtime `status` changes. +- `thread/compact/start` - trigger conversation history compaction for a thread; returns `{}` immediately while progress streams via `turn/*` and `item/*` notifications. +- `thread/shellCommand` - run a user-initiated shell command against a thread. This runs outside the sandbox with full access and doesn't inherit the thread sandbox policy. +- `thread/backgroundTerminals/clean` - stop all running background terminals for a thread (experimental; requires `capabilities.experimentalApi`). +- `thread/backgroundTerminals/list` - list running background terminals for a loaded thread (experimental; requires `capabilities.experimentalApi`). +- `thread/backgroundTerminals/terminate` - terminate one running background terminal by app-server `processId` (experimental; requires `capabilities.experimentalApi`). +- `thread/rollback` - deprecated; drop the last N turns from the in-memory context and persist a rollback marker; returns the updated `thread`. +- `turn/start` - add user input to a thread and begin Codex generation; responds with the initial `turn` and streams events. For `collaborationMode`, `settings.developer_instructions: null` means "use built-in instructions for the selected mode." +- `thread/inject_items` - append raw Responses API items to a loaded thread's model-visible history without starting a user turn. +- `turn/steer` - append user input to the active in-flight turn for a thread; returns the accepted `turnId`. +- `turn/interrupt` - request cancellation of an in-flight turn; success is `{}` and the turn ends with `status: "interrupted"`. +- `review/start` - kick off the Codex reviewer for a thread; emits `enteredReviewMode` and `exitedReviewMode` items. +- `command/exec` - run a single command under the server sandbox without starting a thread/turn. +- `command/exec/write` - write `stdin` bytes to a running `command/exec` session or close `stdin`. +- `command/exec/resize` - resize a running PTY-backed `command/exec` session. +- `command/exec/terminate` - stop a running `command/exec` session. +- `command/exec/outputDelta` (notify) - emitted for base64-encoded stdout/stderr chunks from a streaming `command/exec` session. +- `process/spawn` - start an explicit process session outside Codex's sandbox (experimental; requires `capabilities.experimentalApi`). +- `process/writeStdin` - write stdin bytes to a running `process/spawn` session or close stdin (experimental). +- `process/resizePty` - resize a running PTY-backed process session (experimental). +- `process/kill` - terminate a running process session (experimental). +- `process/outputDelta` and `process/exited` (notify) - emitted for streaming process output and process exit status (experimental). +- `model/list` - list available models (set `includeHidden: true` to include entries with `hidden: true`) with effort options, optional `upgrade`, and `inputModalities`. +- `modelProvider/capabilities/read` - read provider capability bounds for model/provider combinations. +- `experimentalFeature/list` - list feature flags with lifecycle stage metadata and cursor pagination. +- `experimentalFeature/enablement/set` - patch in-memory runtime settings for supported feature keys such as `apps` and `plugins`. +- `environment/info` - experimental; connect to a configured execution environment and return its shell plus default working directory. +- `permissionProfile/list` - list beta permission profiles and whether effective requirements allow them, with cursor pagination. +- `collaborationMode/list` - list collaboration mode presets (experimental, no pagination). +- `skills/list` - list skills for one or more `cwd` values (supports `forceReload` and optional `perCwdExtraUserRoots`). +- `skills/extraRoots/set` - replace the process-level extra roots used to discover standalone skills without persisting them. +- `skills/changed` (notify) - emitted when watched local skill files change. +- `hooks/list` - list discovered lifecycle hooks for one or more `cwd` values. +- `marketplace/add` - add a remote plugin marketplace and persist it into the user's marketplace config. +- `marketplace/remove` - remove a configured marketplace and its installed marketplace root when present. +- `marketplace/upgrade` - refresh a configured Git marketplace, or all configured Git marketplaces when you omit the marketplace name. +- `plugin/list` - under development; list discovered plugin marketplaces and plugin state, including install/auth policy metadata, marketplace load errors, featured plugin ids, and local, Git, package-registry, or remote plugin source metadata. Summaries can include remote `version`, local `localVersion`, structured light/dark icons, and `installPolicySource`, which can be `null`, `WORKSPACE_SETTING`, or `IMPLICIT_CANONICAL_APP` for current remote rows. Don't call this method from production clients yet. +- `plugin/read` - under development; read one plugin by marketplace path or remote marketplace name and plugin name, including bundled skills, apps, MCP server names, and a remote plugin `shareUrl` when the remote catalog provides one. Don't call this method from production clients yet. +- `plugin/install` - under development; install a plugin from a marketplace path or remote marketplace name. Don't call this method from production clients yet. +- `plugin/uninstall` - under development; uninstall an installed plugin. Don't call this method from production clients yet. +- `plugin/skill/read` - read remote plugin skill Markdown on demand by remote marketplace, plugin id, and skill name. +- `app/list` - list available apps (connectors) with pagination plus accessibility/enabled metadata. +- `skills/config/write` - enable or disable skills by path. +- `mcpServer/oauth/login` - start an OAuth login for a configured MCP server; returns an authorization URL and emits `mcpServer/oauthLogin/completed` on completion. +- `tool/requestUserInput` - prompt the user with 1-3 short questions for a tool call (experimental); questions can set `isOther` for a free-form option. +- `mcpServer/elicitation/request` (server request) - ask the client for structured form input or confirmation of a URL flow requested by an MCP server. +- `item/permissions/requestApproval` (server request) - ask the client to grant a subset of network or filesystem permissions requested by the built-in `request_permissions` tool. +- `config/mcpServer/reload` - reload MCP server configuration from disk and queue a refresh for loaded threads. +- `mcpServerStatus/list` - list MCP servers, tools, resources, and auth status (cursor + limit pagination). Use `detail: "full"` for full data or `detail: "toolsAndAuthOnly"` to omit resources. +- `mcpServer/resource/read` - read a single MCP resource through an initialized MCP server. +- `mcpServer/tool/call` - call a tool on a thread's configured MCP server. +- `mcpServer/startupStatus/updated` (notify) - emitted when a configured MCP server's startup status changes for a loaded thread. +- `windowsSandbox/setupStart` - start Windows sandbox setup for `elevated` or `unelevated` mode; returns quickly and later emits `windowsSandbox/setupCompleted`. +- `feedback/upload` - submit a feedback report (classification + optional reason/logs + conversation id, plus optional `extraLogFiles` attachments). +- `config/read` - fetch the effective configuration on disk after resolving configuration layering. +- `externalAgentConfig/detect` - detect external-agent artifacts that can be migrated with `includeHome` and optional `cwds`; each detected item includes `cwd` (`null` for home). +- `externalAgentConfig/import` - apply selected external-agent migration items by passing explicit `migrationItems` with `cwd` (`null` for home). Supported item types include config, skills, `AGENTS.md`, plugins, MCP server config, subagents, hooks, commands, and sessions; non-empty imports emit `externalAgentConfig/import/progress` and `externalAgentConfig/import/completed` as work finishes. Plugin and session imports can complete asynchronously. +- `config/value/write` - write a single configuration key/value to the user's `config.toml` on disk. +- `config/batchWrite` - apply configuration edits atomically to the user's `config.toml` on disk. +- `configRequirements/read` - fetch requirements from `requirements.toml` and/or MDM, including allow-lists, pinned `featureRequirements`, and residency/network requirements (or `null` if you haven't set any up). +- `fs/readFile`, `fs/writeFile`, `fs/createDirectory`, `fs/getMetadata`, `fs/readDirectory`, `fs/remove`, `fs/copy`, `fs/watch`, `fs/unwatch`, and `fs/changed` (notify) - operate on absolute filesystem paths through the app-server v2 filesystem API. + +Plugin summaries include a `source` union. Local plugins return +`{ "type": "local", "path": ... }`, Git-backed marketplace entries return +`{ "type": "git", "url": ..., "path": ..., "refName": ..., "sha": ... }`, +package-registry entries return +`{ "type": "npm", "package": ..., "version": ..., "registry": ... }`, and +remote catalog entries return `{ "type": "remote" }`. For remote-only catalog +entries, `PluginMarketplaceEntry.path` can be `null`; pass +`remoteMarketplaceName` instead of `marketplacePath` when reading or installing +those plugins. + +## Models + +### List models (`model/list`) + +Call `model/list` to discover available models and their capabilities before rendering model or personality selectors. + +```json +{ "method": "model/list", "id": 6, "params": { "limit": 20, "includeHidden": false } } +{ "id": 6, "result": { + "data": [{ + "id": "gpt-5.4", + "model": "gpt-5.4", + "displayName": "GPT-5.4", + "hidden": false, + "defaultReasoningEffort": "medium", + "supportedReasoningEfforts": [{ + "reasoningEffort": "low", + "description": "Lower latency" + }], + "inputModalities": ["text", "image"], + "supportsPersonality": true, + "isDefault": true + }], + "nextCursor": null +} } +``` + +Each model entry can include: + +- `supportedReasoningEfforts` - supported effort options for the model. +- `defaultReasoningEffort` - suggested default effort for clients. +- `upgrade` - optional recommended upgrade model id for migration prompts in clients. +- `upgradeInfo` - optional upgrade metadata for migration prompts in clients. +- `hidden` - whether the model is hidden from the default picker list. +- `inputModalities` - supported input types for the model (for example `text`, `image`). +- `supportsPersonality` - whether the model supports personality-specific instructions such as `/personality`. +- `isDefault` - whether the model is the recommended default. + +By default, `model/list` returns picker-visible models only. Set `includeHidden: true` if you need the full list and want to filter on the client side using `hidden`. + +When `inputModalities` is missing (older model catalogs), treat it as `["text", "image"]` for backward compatibility. + +### List experimental features (`experimentalFeature/list`) + +Use this endpoint to discover feature flags with metadata and lifecycle stage: + +```json +{ "method": "experimentalFeature/list", "id": 7, "params": { "limit": 20 } } +{ "id": 7, "result": { + "data": [{ + "name": "unified_exec", + "stage": "beta", + "displayName": "Unified exec", + "description": "Use the unified PTY-backed execution tool.", + "announcement": "Beta rollout for improved command execution reliability.", + "enabled": false, + "defaultEnabled": false + }], + "nextCursor": null +} } +``` + +`stage` can be `beta`, `underDevelopment`, `stable`, `deprecated`, or `removed`. For non-beta flags, `displayName`, `description`, and `announcement` may be `null`. + +### Inspect an execution environment (experimental) + +Use `environment/info` to inspect a configured remote environment before +starting work there. The method requires `capabilities.experimentalApi = true`. + +```json +{ "method": "environment/info", "id": 8, "params": { "environmentId": "devbox" } } +{ "id": 8, "result": { + "shell": { "name": "zsh", "path": "/bin/zsh" }, + "cwd": "file:///workspace/project" +} } +``` + +`cwd` can be `null`. When present, it's a canonical `file:` URI that uses the +environment's native path syntax. Unknown environment IDs and connection or +protocol failures return request errors. + +## Threads + +- `thread/read` reads a stored thread without subscribing to it; set `includeTurns` to include turns. +- `thread/turns/list` is experimental and pages through a stored thread's turn history without + resuming it. Use `itemsView` to choose whether turn items are omitted, + summarized, or fully loaded. +- `thread/items/list` is experimental and pages through persisted thread items, optionally restricted to one turn. +- `thread/list` supports cursor pagination plus `modelProviders`, `sourceKinds`, `archived`, `cwd`, `useStateDbOnly`, `searchTerm`, and experimental `parentThreadId` or `ancestorThreadId` filtering. +- `thread/loaded/list` returns the thread IDs currently in memory. +- `thread/archive` moves the thread's persisted JSONL log into the archived directory and attempts to archive spawned descendant thread logs that aren't already archived. +- `thread/delete` permanently deletes a persisted active or archived thread and its spawned descendant threads. +- `thread/metadata/update` patches stored thread metadata, currently including persisted `gitInfo`. +- `thread/unsubscribe` unsubscribes the current connection from a loaded thread and can trigger `thread/closed` after an inactivity grace period. +- `thread/unarchive` restores an archived thread rollout back into the active sessions directory. +- `thread/compact/start` triggers compaction and returns `{}` immediately. +- `thread/rollback` is deprecated. It drops the last N turns from the in-memory context and records a rollback marker in the thread's persisted JSONL log. +- `thread/inject_items` appends raw Responses API items to a loaded thread's model-visible history without starting a user turn. + +### Start or resume a thread + +Start a fresh thread when you need a new Codex conversation. + +```json +{ "method": "thread/start", "id": 10, "params": { + "model": "gpt-5.4", + "cwd": "/Users/me/project", + "approvalPolicy": "never", + "sandbox": "workspaceWrite", + "personality": "friendly", + "serviceName": "my_app_server_client" +} } +{ "id": 10, "result": { + "thread": { + "id": "thr_123", + "sessionId": "thr_123", + "preview": "", + "ephemeral": false, + "modelProvider": "openai", + "createdAt": 1730910000 + } +} } +{ "method": "thread/started", "params": { "thread": { "id": "thr_123" } } } +``` + +`serviceName` is optional. Set it when you want app-server to tag thread-level metrics with your integration's service name. + +`thread/start`, `thread/resume`, and `thread/fork` return +`instructionSources`, an array of loaded instruction-file paths. Each path uses +its source environment's native absolute syntax, including for remote +environments. + +Experimental clients can set `historyMode` on `thread/start` to `"legacy"` +(the default) or `"paginated"`. Paginated thread creation isn't supported yet +and returns JSON-RPC error `-32601`. App-server can list and read summaries for +existing paginated records, but full-history reads, turn pagination, and resume +fail closed until paginated history is supported. + +Beta clients that opt into `capabilities.experimentalApi` can pass a named +permission-profile id in `permissions` instead of the legacy `sandbox` field. +Don't send `permissions` and `sandbox` together. Use +`permissionProfile/list` with the project `cwd` to discover available profiles +and whether managed requirements allow each one. + +`thread.sessionId` identifies the current live session tree root. Root threads +use their own thread id as the session id; forked threads keep the session id +of the root they came from. Clients should read the session id from +`thread.sessionId` instead of deriving it from the thread id. + +To continue a stored session, call `thread/resume` with the `thread.id` you recorded earlier. The response shape matches `thread/start`. You can also pass the same configuration overrides supported by `thread/start`, such as `personality`: + +```json +{ "method": "thread/resume", "id": 11, "params": { + "threadId": "thr_123", + "personality": "friendly" +} } +{ "id": 11, "result": { "thread": { "id": "thr_123", "name": "Bug bash notes", "ephemeral": false } } } +``` + +Resuming a thread doesn't update `thread.updatedAt` (or the rollout file's modified time) by itself. The timestamp updates when you start a turn. + +If you mark an enabled MCP server as `required` in config and that server fails to initialize, `thread/start` and `thread/resume` fail instead of continuing without it. + +`dynamicTools` on `thread/start` is an experimental field (requires `capabilities.experimentalApi = true`). Codex persists these dynamic tools in the thread rollout metadata and restores them on `thread/resume` when you don't supply new dynamic tools. + +If you resume with a different model than the one recorded in the rollout, Codex emits a warning and applies a one-time model-switch instruction on the next turn. + +### Manage a thread goal + +Use `thread/goal/set`, `thread/goal/get`, and `thread/goal/clear` to manage the +same persisted goal state surfaced by `/goal` in the TUI. + +```json +{ "method": "thread/goal/set", "id": 13, "params": { + "threadId": "thr_123", + "objective": "Finish the migration and keep tests green", + "status": "active", + "tokenBudget": 40000 +} } +{ "id": 13, "result": { "goal": { + "threadId": "thr_123", + "objective": "Finish the migration and keep tests green", + "status": "active", + "tokenBudget": 40000, + "tokensUsed": 0, + "timeUsedSeconds": 0 +} } } +{ "method": "thread/goal/updated", "params": { + "threadId": "thr_123", + "goal": { + "threadId": "thr_123", + "objective": "Finish the migration and keep tests green", + "status": "active", + "tokenBudget": 40000, + "tokensUsed": 0, + "timeUsedSeconds": 0 + } +} } +``` + +Goal objectives must be non-empty and at most 4,000 characters. Supplying a new +objective replaces the goal and resets usage accounting. Supplying the current +non-terminal objective, or omitting `objective`, updates status or token budget +while preserving usage history. + +To branch from a stored session, call `thread/fork` with the `thread.id`. This creates a new thread id and emits a `thread/started` notification for it. Pass +`lastTurnId` to copy history through that turn, inclusive, and omit later +turns: + +```json +{ "method": "thread/fork", "id": 12, "params": { "threadId": "thr_123", "lastTurnId": "turn_456" } } +{ "id": 12, "result": { "thread": { "id": "thr_456", "sessionId": "thr_123", "forkedFromId": "thr_123" } } } +{ "method": "thread/started", "params": { "thread": { "id": "thr_456" } } } +``` + +App-server rejects an in-progress `lastTurnId`. If you omit the field while the +source thread is mid-turn, the fork records an interruption marker instead of +retaining an unmarked partial turn. + +When a user-facing thread title has been set, app-server hydrates `thread.name` on `thread/list`, `thread/read`, `thread/resume`, `thread/unarchive`, and `thread/rollback` responses. `thread/start` and `thread/fork` may omit `name` (or return `null`) until a title is set later. + +### Read a stored thread (without resuming) + +Use `thread/read` when you want stored thread data but don't want to resume the thread or subscribe to its events. + +- `includeTurns` - when `true`, the response includes the thread's turns; when `false` or omitted, you get the thread summary only. +- Returned `thread` objects include runtime `status` (`notLoaded`, `idle`, `systemError`, or `active` with `activeFlags`). + +```json +{ "method": "thread/read", "id": 19, "params": { "threadId": "thr_123", "includeTurns": true } } +{ "id": 19, "result": { "thread": { "id": "thr_123", "name": "Bug bash notes", "ephemeral": false, "status": { "type": "notLoaded" }, "turns": [] } } } +``` + +Unlike `thread/resume`, `thread/read` doesn't load the thread into memory or emit `thread/started`. + +### List thread turns + +`thread/turns/list` is experimental. Use it to page a stored thread's turn history without resuming it. Results default to newest-first so clients can fetch older turns with `nextCursor`. The response also includes `backwardsCursor`; pass it as `cursor` with `sortDirection: "asc"` to fetch turns newer than the first item from the earlier page. + +`itemsView` controls how much turn-item data the response includes: + +- `notLoaded` omits items. +- `summary` returns summarized item data and is the default when omitted. +- `full` returns full item data. + +```json +{ "method": "thread/turns/list", "id": 20, "params": { + "threadId": "thr_123", + "limit": 50, + "sortDirection": "desc", + "itemsView": "summary" +} } +{ "id": 20, "result": { + "data": [], + "nextCursor": "older-turns-cursor-or-null", + "backwardsCursor": "newer-turns-cursor-or-null" +} } +``` + +`thread/items/list` is also experimental. It pages persisted items without +resuming the thread. Pass `turnId` to restrict results to one turn, or omit it +to page items across the thread. The active thread store must support item +pagination; otherwise, the server returns an unsupported-method error. + +### List threads (with pagination & filters) + +`thread/list` lets you render a history UI. Results default to newest-first by `createdAt`. Filters apply before pagination. Pass any combination of: + +- `cursor` - opaque string from a prior response; omit for the first page. +- `limit` - server defaults to a reasonable page size if unset. +- `sortKey` - `created_at` (default), `updated_at`, or `recency_at`. +- `sortDirection` - `desc` (default) or `asc`. +- `modelProviders` - restrict results to specific providers; unset, null, or an empty array includes all providers. +- `sourceKinds` - restrict results to specific thread sources. When omitted or `[]`, the server defaults to interactive sources only: `cli` and `vscode`. +- `archived` - when `true`, list archived threads only. When `false` or omitted, list non-archived threads (default). +- `cwd` - restrict results to threads whose session current working directory exactly matches this path, or one of the paths in an array. Relative paths resolve from the app-server process working directory. +- `useStateDbOnly` - when `true`, return state database results without scanning JSONL thread logs to repair metadata. Omit it or pass `false` for the default scan-and-repair behavior. +- `searchTerm` - restrict results to threads whose extracted title contains this case-sensitive text fragment. +- `parentThreadId` - restrict results to direct child threads of the given parent thread. This filter is experimental and requires `capabilities.experimentalApi = true`. +- `ancestorThreadId` - restrict results to spawned descendants of the given thread at any depth. This filter is experimental and requires `capabilities.experimentalApi = true`; don't combine it with `parentThreadId`. + +`sourceKinds` accepts the following values: + +- `cli` +- `vscode` +- `exec` +- `appServer` +- `subAgent` +- `subAgentReview` +- `subAgentCompact` +- `subAgentThreadSpawn` +- `subAgentOther` +- `unknown` + +Example: + +```json +{ "method": "thread/list", "id": 20, "params": { + "cursor": null, + "limit": 25, + "sortKey": "created_at" +} } +{ "id": 20, "result": { + "data": [ + { "id": "thr_a", "preview": "Create a TUI", "ephemeral": false, "modelProvider": "openai", "createdAt": 1730831111, "updatedAt": 1730831111, "name": "TUI prototype", "status": { "type": "notLoaded" } }, + { "id": "thr_b", "preview": "Fix tests", "ephemeral": true, "modelProvider": "openai", "createdAt": 1730750000, "updatedAt": 1730750000, "status": { "type": "notLoaded" } } + ], + "nextCursor": "opaque-token-or-null" +} } +``` + +When `nextCursor` is `null`, you have reached the final page. + +### Update stored thread metadata + +Use `thread/metadata/update` to patch stored thread metadata without resuming the thread. Today this supports persisted `gitInfo`; omitted fields are left unchanged, and explicit `null` clears a stored value. + +```json +{ "method": "thread/metadata/update", "id": 21, "params": { + "threadId": "thr_123", + "gitInfo": { "branch": "feature/sidebar-pr" } +} } +{ "id": 21, "result": { + "thread": { + "id": "thr_123", + "gitInfo": { "sha": null, "branch": "feature/sidebar-pr", "originUrl": null } + } +} } +``` + +### Track thread status changes + +`thread/status/changed` is emitted whenever a loaded thread's runtime status changes. The payload includes `threadId` and the new `status`. + +```json +{ + "method": "thread/status/changed", + "params": { + "threadId": "thr_123", + "status": { "type": "active", "activeFlags": ["waitingOnApproval"] } + } +} +``` + +### List loaded threads + +`thread/loaded/list` returns thread IDs currently loaded in memory. + +```json +{ "method": "thread/loaded/list", "id": 21 } +{ "id": 21, "result": { "data": ["thr_123", "thr_456"] } } +``` + +### Unsubscribe from a loaded thread + +`thread/unsubscribe` removes the current connection's subscription to a thread. The response status is one of: + +- `unsubscribed` when the connection was subscribed and is now removed. +- `notSubscribed` when the connection wasn't subscribed to that thread. +- `notLoaded` when the thread isn't loaded. + +If this was the last subscriber, the server keeps the thread loaded until it has no subscribers and no thread activity for 30 minutes. When the grace period expires, app-server unloads the thread and emits a `thread/status/changed` transition to `notLoaded` plus `thread/closed`. + +```json +{ "method": "thread/unsubscribe", "id": 22, "params": { "threadId": "thr_123" } } +{ "id": 22, "result": { "status": "unsubscribed" } } +``` + +If the thread later expires: + +```json +{ "method": "thread/status/changed", "params": { + "threadId": "thr_123", + "status": { "type": "notLoaded" } +} } +{ "method": "thread/closed", "params": { "threadId": "thr_123" } } +``` + +### Archive a thread + +Use `thread/archive` to move the persisted thread log (stored as a JSONL file on disk) into the archived sessions directory. Archiving a thread also attempts to archive spawned descendant threads that aren't already archived. + +```json +{ "method": "thread/archive", "id": 22, "params": { "threadId": "thr_b" } } +{ "id": 22, "result": {} } +{ "method": "thread/archived", "params": { "threadId": "thr_b" } } +{ "method": "thread/archived", "params": { "threadId": "thr_child" } } +``` + +Archived threads won't appear in future calls to `thread/list` unless you pass `archived: true`. The server emits one `thread/archived` notification for each thread it actually archives; if a spawned descendant can't be archived, the request can still succeed without an archived notification for that descendant. + +### Delete a thread + +Use `thread/delete` to permanently delete a persisted active or archived thread +and its spawned descendant threads. The server removes existing rollout files and +associated metadata before returning success; missing rollout files are treated +as already deleted. Ephemeral root threads can't be deleted. + +```json +{ "method": "thread/delete", "id": 23, "params": { "threadId": "thr_b" } } +{ "id": 23, "result": {} } +{ "method": "thread/deleted", "params": { "threadId": "thr_b" } } +{ "method": "thread/deleted", "params": { "threadId": "thr_child" } } +``` + +### Unarchive a thread + +Use `thread/unarchive` to move an archived thread rollout back into the active sessions directory. + +```json +{ "method": "thread/unarchive", "id": 24, "params": { "threadId": "thr_b" } } +{ "id": 24, "result": { "thread": { "id": "thr_b", "name": "Bug bash notes" } } } +{ "method": "thread/unarchived", "params": { "threadId": "thr_b" } } +``` + +### Trigger thread compaction + +Use `thread/compact/start` to trigger manual history compaction for a thread. The request returns immediately with `{}`. + +App-server emits progress as standard `turn/*` and `item/*` notifications on the same `threadId`, including a `contextCompaction` item lifecycle (`item/started` then `item/completed`). + +```json +{ "method": "thread/compact/start", "id": 25, "params": { "threadId": "thr_b" } } +{ "id": 25, "result": {} } +``` + +### Run a thread shell command + +Use `thread/shellCommand` for user-initiated shell commands that belong to a thread. The request returns immediately with `{}` while progress streams through standard `turn/*` and `item/*` notifications. + +This API runs outside the sandbox with full access and doesn't inherit the thread sandbox policy. Clients should expose it only for explicit user-initiated commands. + +If the thread already has an active turn, the command runs as an auxiliary action on that turn and its formatted output is injected into the turn's message stream. If the thread is idle, app-server starts a standalone turn for the shell command. + +```json +{ "method": "thread/shellCommand", "id": 26, "params": { "threadId": "thr_b", "command": "git status --short" } } +{ "id": 26, "result": {} } +``` + +### Clean background terminals + +Use `thread/backgroundTerminals/clean` to stop all running background terminals associated with a thread. This method is experimental and requires `capabilities.experimentalApi = true`. + +```json +{ "method": "thread/backgroundTerminals/clean", "id": 27, "params": { "threadId": "thr_b" } } +{ "id": 27, "result": {} } +``` + +Use `thread/backgroundTerminals/list` to inspect running background terminals +for a loaded thread. The request supports standard `cursor` and `limit` +pagination, and the returned `processId` is the app-server process id. This +method is experimental and requires `capabilities.experimentalApi = true`: + +```json +{ "method": "thread/backgroundTerminals/list", "id": 28, "params": { "threadId": "thr_b" } } +{ "id": 28, "result": { "data": [ + { + "itemId": "item_456", + "processId": "42", + "command": "python3 -m http.server", + "cwd": "/workspace", + "osPid": null, + "cpuPercent": null, + "rssKb": null + } +], "nextCursor": null } } +``` + +Use `thread/backgroundTerminals/terminate` with that `processId` to stop one +background terminal. This method is experimental and requires +`capabilities.experimentalApi = true`: + +```json +{ "method": "thread/backgroundTerminals/terminate", "id": 29, "params": { "threadId": "thr_b", "processId": "42" } } +{ "id": 29, "result": { "terminated": true } } +``` + +### Roll back recent turns + +`thread/rollback` is deprecated and will be removed. It removes the last +`numTurns` entries from the in-memory context and persists a rollback marker in +the rollout log. The returned `thread` includes `turns` populated after the +rollback. + +```json +{ "method": "thread/rollback", "id": 30, "params": { "threadId": "thr_b", "numTurns": 1 } } +{ "id": 30, "result": { "thread": { "id": "thr_b", "name": "Bug bash notes", "ephemeral": false } } } +``` + +## Turns + +The `input` field accepts a list of items: + +- `{ "type": "text", "text": "Explain this diff" }` +- `{ "type": "image", "url": "https://.../design.png" }` +- `{ "type": "localImage", "path": "/tmp/screenshot.png" }` + +You can override configuration settings per turn (model, effort, personality, `cwd`, sandbox policy, summary). When specified, these settings become the defaults for later turns on the same thread. `outputSchema` applies only to the current turn. For `sandboxPolicy.type = "externalSandbox"`, set `networkAccess` to `restricted` or `enabled`; for `workspaceWrite`, `networkAccess` remains a boolean. + +For `turn/start.collaborationMode`, `settings.developer_instructions: null` means "use built-in instructions for the selected mode" rather than clearing mode instructions. + +### Sandbox read access (`ReadOnlyAccess`) + +`sandboxPolicy` supports explicit read-access controls: + +- `readOnly`: optional `access` (`{ "type": "fullAccess" }` by default, or restricted roots). +- `workspaceWrite`: optional `readOnlyAccess` (`{ "type": "fullAccess" }` by default, or restricted roots). + +Restricted read access shape: + +```json +{ + "type": "restricted", + "includePlatformDefaults": true, + "readableRoots": ["/Users/me/shared-read-only"] +} +``` + +On macOS, `includePlatformDefaults: true` appends a curated platform-default Seatbelt policy for restricted-read sessions. This improves tool compatibility without broadly allowing all of `/System`. + +Examples: + +```json +{ "type": "readOnly", "access": { "type": "fullAccess" } } +``` + +```json +{ + "type": "workspaceWrite", + "writableRoots": ["/Users/me/project"], + "readOnlyAccess": { + "type": "restricted", + "includePlatformDefaults": true, + "readableRoots": ["/Users/me/shared-read-only"] + }, + "networkAccess": false +} +``` + +### Start a turn + +```json +{ "method": "turn/start", "id": 30, "params": { + "threadId": "thr_123", + "input": [ { "type": "text", "text": "Run tests" } ], + "cwd": "/Users/me/project", + "approvalPolicy": "unlessTrusted", + "sandboxPolicy": { + "type": "workspaceWrite", + "writableRoots": ["/Users/me/project"], + "networkAccess": true + }, + "model": "gpt-5.4", + "effort": "medium", + "summary": "concise", + "personality": "friendly", + "outputSchema": { + "type": "object", + "properties": { "answer": { "type": "string" } }, + "required": ["answer"], + "additionalProperties": false + } +} } +{ "id": 30, "result": { "turn": { "id": "turn_456", "status": "inProgress", "items": [], "error": null } } } +``` + +### Inject items into a thread + +Use `thread/inject_items` to append prebuilt Responses API items to a loaded thread's prompt history without starting a user turn. These items are persisted to the rollout and included in subsequent model requests. + +```json +{ "method": "thread/inject_items", "id": 31, "params": { + "threadId": "thr_123", + "items": [ + { + "type": "message", + "role": "assistant", + "content": [{ "type": "output_text", "text": "Previously computed context." }] + } + ] +} } +{ "id": 31, "result": {} } +``` + +### Steer an active turn + +Use `turn/steer` to append more user input to the active in-flight turn. + +- Include `expectedTurnId`; it must match the active turn id. +- The request fails if there is no active turn on the thread. +- `turn/steer` doesn't emit a new `turn/started` notification. +- `turn/steer` doesn't accept turn-level overrides (`model`, `cwd`, `sandboxPolicy`, or `outputSchema`). + +```json +{ "method": "turn/steer", "id": 32, "params": { + "threadId": "thr_123", + "input": [ { "type": "text", "text": "Actually focus on failing tests first." } ], + "expectedTurnId": "turn_456" +} } +{ "id": 32, "result": { "turnId": "turn_456" } } +``` + +### Start a turn (invoke a skill) + +Invoke a skill explicitly by including `$` in the text input and adding a `skill` input item alongside it. + +```json +{ "method": "turn/start", "id": 33, "params": { + "threadId": "thr_123", + "input": [ + { "type": "text", "text": "$skill-creator Add a new skill for triaging flaky CI and include step-by-step usage." }, + { "type": "skill", "name": "skill-creator", "path": "/Users/me/.codex/skills/skill-creator/SKILL.md" } + ] +} } +{ "id": 33, "result": { "turn": { "id": "turn_457", "status": "inProgress", "items": [], "error": null } } } +``` + +### Interrupt a turn + +```json +{ "method": "turn/interrupt", "id": 31, "params": { "threadId": "thr_123", "turnId": "turn_456" } } +{ "id": 31, "result": {} } +``` + +On success, the turn finishes with `status: "interrupted"`. + +## Review + +`review/start` runs the Codex reviewer for a thread and streams review items. Targets include: + +- `uncommittedChanges` +- `baseBranch` (diff against a branch) +- `commit` (review a specific commit) +- `custom` (free-form instructions) + +Use `delivery: "inline"` (default) to run the review on the existing thread, or `delivery: "detached"` to fork a new review thread. + +Example request/response: + +```json +{ "method": "review/start", "id": 40, "params": { + "threadId": "thr_123", + "delivery": "inline", + "target": { "type": "commit", "sha": "1234567deadbeef", "title": "Polish tui colors" } +} } +{ "id": 40, "result": { + "turn": { + "id": "turn_900", + "status": "inProgress", + "items": [ + { "type": "userMessage", "id": "turn_900", "content": [ { "type": "text", "text": "Review commit 1234567: Polish tui colors" } ] } + ], + "error": null + }, + "reviewThreadId": "thr_123" +} } +``` + +For a detached review, use `"delivery": "detached"`. The response is the same shape, but `reviewThreadId` will be the id of the new review thread (different from the original `threadId`). The server also emits a `thread/started` notification for that new thread before streaming the review turn. + +Codex streams the usual `turn/started` notification followed by an `item/started` with an `enteredReviewMode` item: + +```json +{ + "method": "item/started", + "params": { + "item": { + "type": "enteredReviewMode", + "id": "turn_900", + "review": "current changes" + } + } +} +``` + +When the reviewer finishes, the server emits `item/started` and `item/completed` containing an `exitedReviewMode` item with the final review text: + +```json +{ + "method": "item/completed", + "params": { + "item": { + "type": "exitedReviewMode", + "id": "turn_900", + "review": "Looks solid overall..." + } + } +} +``` + +Use this notification to render the reviewer output in your client. + +## Process execution + +`process/*` is an experimental, explicit process-control API. It requires +`capabilities.experimentalApi = true` and runs outside Codex's sandbox. Use it +only when your client intentionally exposes local process control without a +sandbox. + +Start a process with `process/spawn` and provide a `processHandle`, then use +that handle for stdin, resize, and kill requests. Output streams through +`process/outputDelta` notifications and completion streams through +`process/exited`. + +```json +{ "method": "process/spawn", "id": 48, "params": { + "command": ["python3", "-m", "pytest", "-q"], + "processHandle": "pytest-1", + "cwd": "/Users/me/project", + "tty": true +} } +{ "id": 48, "result": {} } +{ "method": "process/outputDelta", "params": { + "processHandle": "pytest-1", + "stream": "stdout", + "deltaBase64": "Li4u" +} } +{ "method": "process/exited", "params": { + "processHandle": "pytest-1", + "exitCode": 0 +} } +``` + +Use `process/writeStdin` with `deltaBase64`, `closeStdin`, or both to send +input. Use `process/resizePty` for PTY resize events and `process/kill` to +terminate a running process. + +## Command execution + +`command/exec` runs a single command (`argv` array) under the server sandbox without creating a thread. + +```json +{ "method": "command/exec", "id": 50, "params": { + "command": ["ls", "-la"], + "cwd": "/Users/me/project", + "sandboxPolicy": { "type": "workspaceWrite" }, + "timeoutMs": 10000 +} } +{ "id": 50, "result": { "exitCode": 0, "stdout": "...", "stderr": "" } } +``` + +Use `sandboxPolicy.type = "externalSandbox"` if you already sandbox the server process and want Codex to skip its own sandbox enforcement. For external sandbox mode, set `networkAccess` to `restricted` (default) or `enabled`. For `readOnly` and `workspaceWrite`, use the same optional `access` / `readOnlyAccess` structure shown above. + +Notes: + +- The server rejects empty `command` arrays. +- `sandboxPolicy` accepts the same shape used by `turn/start` (for example, `dangerFullAccess`, `readOnly`, `workspaceWrite`, `externalSandbox`). +- When omitted, `timeoutMs` falls back to the server default. +- Set `tty: true` for PTY-backed sessions, and use `processId` when you plan to follow up with `command/exec/write`, `command/exec/resize`, or `command/exec/terminate`. +- Set `streamStdoutStderr: true` to receive `command/exec/outputDelta` notifications while the command is running. + +### Read admin requirements (`configRequirements/read`) + +Use `configRequirements/read` to inspect the effective admin requirements loaded from `requirements.toml` and/or MDM. + +```json +{ "method": "configRequirements/read", "id": 52, "params": {} } +{ "id": 52, "result": { + "requirements": { + "allowedApprovalPolicies": ["onRequest", "unlessTrusted"], + "allowedSandboxModes": ["readOnly", "workspaceWrite"], + "featureRequirements": { + "personality": true, + "unified_exec": false + }, + "network": { + "enabled": true, + "allowedDomains": ["api.openai.com"], + "allowUnixSockets": ["/tmp/example.sock"], + "dangerouslyAllowAllUnixSockets": false + } + } +} } +``` + +`result.requirements` is `null` when no requirements are configured. See the docs on [`requirements.toml`](https://learn.chatgpt.com/docs/config-file/config-reference#requirementstoml) for details on supported keys and values. + +### Windows sandbox setup (`windowsSandbox/setupStart`) + +Custom Windows clients can trigger sandbox setup asynchronously instead of blocking on startup checks. + +```json +{ "method": "windowsSandbox/setupStart", "id": 53, "params": { "mode": "elevated" } } +{ "id": 53, "result": { "started": true } } +``` + +App-server starts setup in the background and later emits a completion notification: + +```json +{ + "method": "windowsSandbox/setupCompleted", + "params": { "mode": "elevated", "success": true, "error": null } +} +``` + +Modes: + +- `elevated` - run the elevated Windows sandbox setup path. +- `unelevated` - run the legacy setup/preflight path. + +## Filesystem + +The v2 filesystem APIs operate on absolute paths. Use `fs/watch` when a client needs to invalidate UI state after a file or directory changes. + +```json +{ "method": "fs/watch", "id": 54, "params": { + "watchId": "0195ec6b-1d6f-7c2e-8c7a-56f2c4a8b9d1", + "path": "/Users/me/project/.git/HEAD" +} } +{ "id": 54, "result": { "path": "/Users/me/project/.git/HEAD" } } +{ "method": "fs/changed", "params": { + "watchId": "0195ec6b-1d6f-7c2e-8c7a-56f2c4a8b9d1", + "changedPaths": ["/Users/me/project/.git/HEAD"] +} } +{ "method": "fs/unwatch", "id": 55, "params": { + "watchId": "0195ec6b-1d6f-7c2e-8c7a-56f2c4a8b9d1" +} } +{ "id": 55, "result": {} } +``` + +Watching a file emits `fs/changed` for that file path, including updates delivered by replace or rename operations. + +## Events + +Event notifications are the server-initiated stream for thread lifecycles, turn lifecycles, and the items within them. After you start or resume a thread, keep reading the active transport stream for `thread/started`, `thread/archived`, `thread/unarchived`, `thread/closed`, `thread/status/changed`, `turn/*`, `item/*`, and `serverRequest/resolved` notifications. + +### Notification opt-out + +Clients can suppress specific notifications per connection by sending exact method names in `initialize.params.capabilities.optOutNotificationMethods`. + +- Exact-match only: `item/agentMessage/delta` suppresses only that method. +- Unknown method names are ignored. +- Applies to the current `thread/*`, `turn/*`, `item/*`, and related v2 notifications. +- Doesn't apply to requests, responses, or errors. + +### Fuzzy file search events (experimental) + +The fuzzy file search session API emits per-query notifications: + +- `fuzzyFileSearch/sessionUpdated` - `{ sessionId, query, files }` with the current matches for the active query. +- `fuzzyFileSearch/sessionCompleted` - `{ sessionId }` once indexing and matching for that query completes. + +### Warning events + +- `configWarning` - `{ summary, details?, path?, range? }` for recoverable + configuration or initialization problems. +- `warning` - `{ threadId?, message }` for non-fatal runtime warnings. + +### Windows sandbox setup events + +- `windowsSandbox/setupCompleted` - `{ mode, success, error }` emitted after a `windowsSandbox/setupStart` request finishes. + +### Turn events + +- `turn/started` - `{ turn }` with the turn id, empty `items`, and `status: "inProgress"`. +- `turn/completed` - `{ turn }` where `turn.status` is `completed`, `interrupted`, or `failed`; failures carry `{ error: { message, codexErrorInfo?, additionalDetails? } }`. +- `turn/diff/updated` - `{ threadId, turnId, diff }` with the latest aggregated unified diff across every file change in the turn. +- `turn/plan/updated` - `{ turnId, explanation?, plan }` whenever the agent shares or changes its plan; each `plan` entry is `{ step, status }` with `status` in `pending`, `inProgress`, or `completed`. +- `hook/started` and `hook/completed` - `{ threadId, turnId?, run }` when a lifecycle hook starts and when its final run summary is available. +- `model/safetyBuffering/updated` - `{ threadId, turnId, model, useCases, reasons, showBufferingUi, fasterModel }` when a response enters transient safety buffering. +- `model/rerouted` - `{ threadId, turnId, fromModel, toModel, reason }` when the service routes a request to another model. +- `model/verification` - `{ threadId, turnId, verifications }` when the service requires additional account verification. +- `thread/tokenUsage/updated` - usage updates for the active thread. + +`turn/diff/updated` and `turn/plan/updated` currently include empty `items` arrays even when item events stream. Use `item/*` notifications as the source of truth for turn items. + +### Items + +`ThreadItem` is the tagged union carried in turn responses and `item/*` notifications. Common item types include: + +- `userMessage` - `{id, content}` where `content` is a list of user inputs (`text`, `image`, or `localImage`). +- `agentMessage` - `{id, text, phase?}` containing the accumulated agent reply. When present, `phase` uses Responses API wire values (`commentary`, `final_answer`). +- `plan` - `{id, text}` containing proposed plan text in plan mode. Treat the final `plan` item from `item/completed` as authoritative. +- `reasoning` - `{id, summary, content}` where `summary` holds streamed reasoning summaries and `content` holds raw reasoning blocks. +- `commandExecution` - `{id, command, cwd, status, commandActions, aggregatedOutput?, exitCode?, durationMs?}`. +- `fileChange` - `{id, changes, status}` describing proposed edits; `changes` list `{path, kind, diff}`. +- `mcpToolCall` - `{id, server, tool, status, arguments, appContext?, pluginId?, result?, error?}`. For trusted MCP apps, `appContext` can include `connectorId`, `linkId`, `resourceUri`, `appName`, `templateId`, and the stable connector `actionName`. Older persisted items can omit newer metadata. Use `appContext.resourceUri` instead of the deprecated top-level `mcpAppResourceUri`. +- `dynamicToolCall` - `{id, tool, arguments, status, contentItems?, success?, durationMs?}` for client-executed dynamic tool invocations. +- `collabToolCall` - `{id, tool, status, senderThreadId, receiverThreadId?, newThreadId?, prompt?, agentStatus?}`. +- `webSearch` - `{id, query, action?}` for web search requests issued by the agent. +- `imageView` - `{id, path}` emitted when the agent invokes the image viewer tool. +- `enteredReviewMode` - `{id, review}` sent when the reviewer starts. +- `exitedReviewMode` - `{id, review}` emitted when the reviewer finishes. +- `contextCompaction` - `{id}` emitted when Codex compacts the conversation history. + +For `webSearch.action`, the action `type` can be `search` (`query?`, `queries?`), `openPage` (`url?`), or `findInPage` (`url?`, `pattern?`). + +The app server deprecates the legacy `thread/compacted` notification; use the `contextCompaction` item instead. + +All items emit two shared lifecycle events: + +- `item/started` - emits the full `item` when a new unit of work begins; the `item.id` matches the `itemId` used by deltas. +- `item/completed` - sends the final `item` once work finishes; treat this as the authoritative state. + +### Item deltas + +- `item/agentMessage/delta` - appends streamed text for the agent message. +- `item/plan/delta` - streams proposed plan text. The final `plan` item may not exactly equal the concatenated deltas. +- `item/reasoning/summaryTextDelta` - streams readable reasoning summaries; `summaryIndex` increments when a new summary section opens. +- `item/reasoning/summaryPartAdded` - marks a boundary between reasoning summary sections. +- `item/reasoning/textDelta` - streams raw reasoning text (when supported by the model). +- `item/commandExecution/outputDelta` - streams stdout/stderr for a command; append deltas in order. +- `item/fileChange/outputDelta` - deprecated compatibility notification for legacy `apply_patch` text output. Current app-server versions no longer emit it; use `fileChange` items and `turn/diff/updated` instead. + +## Errors + +If a turn fails, the server emits an `error` event with `{ error: { message, codexErrorInfo?, additionalDetails? } }` and then finishes the turn with `status: "failed"`. When an upstream HTTP status is available, it appears in `codexErrorInfo.httpStatusCode`. + +Common `codexErrorInfo` values include: + +- `ContextWindowExceeded` +- `UsageLimitExceeded` +- `HttpConnectionFailed` (4xx/5xx upstream errors) +- `ResponseStreamConnectionFailed` +- `ResponseStreamDisconnected` +- `ResponseTooManyFailedAttempts` +- `BadRequest`, `Unauthorized`, `SandboxError`, `InternalServerError`, `Other` + +When an upstream HTTP status is available, the server forwards it in `httpStatusCode` on the relevant `codexErrorInfo` variant. + +## Approvals + +Depending on a user's Codex settings, command execution and file changes may require approval. The app-server sends a server-initiated JSON-RPC request to the client, and the client responds with a decision payload. + +- Command execution decisions: `accept`, `acceptForSession`, `decline`, `cancel`, or `{ "acceptWithExecpolicyAmendment": { "execpolicy_amendment": ["cmd", "..."] } }`. +- File change decisions: `accept`, `acceptForSession`, `decline`, `cancel`. + +- Requests include `threadId` and `turnId` - use them to scope UI state to the active conversation. +- The server resumes or declines the work and ends the item with `item/completed`. + +### Command execution approvals + +Order of messages: + +1. `item/started` shows the pending `commandExecution` item with `command`, `cwd`, and other fields. +2. `item/commandExecution/requestApproval` includes `itemId`, `threadId`, `turnId`, optional `reason`, optional `command`, optional `cwd`, optional `commandActions`, optional `proposedExecpolicyAmendment`, optional `networkApprovalContext`, and optional `availableDecisions`. When `initialize.params.capabilities.experimentalApi = true`, the payload can also include experimental `additionalPermissions` describing requested per-command sandbox access. Any filesystem paths inside `additionalPermissions` are absolute on the wire. +3. Client responds with one of the command execution approval decisions above. +4. `serverRequest/resolved` confirms that the pending request has been answered or cleared. +5. `item/completed` returns the final `commandExecution` item with `status: completed | failed | declined`. + +When `networkApprovalContext` is present, the prompt is for managed network access (not a general shell-command approval). The current v2 schema exposes the target `host` and `protocol`; clients should render a network-specific prompt and not rely on `command` being a user-meaningful shell command preview. + +Codex groups concurrent network approval prompts by destination (`host`, protocol, and port). The app-server may therefore send one prompt that unblocks multiple queued requests to the same destination, while different ports on the same host are treated separately. + +### File change approvals + +Order of messages: + +1. `item/started` emits a `fileChange` item with proposed `changes` and `status: "inProgress"`. +2. `item/fileChange/requestApproval` includes `itemId`, `threadId`, `turnId`, optional `reason`, and optional `grantRoot`. +3. Client responds with one of the file change approval decisions above. +4. `serverRequest/resolved` confirms that the pending request has been answered or cleared. +5. `item/completed` returns the final `fileChange` item with `status: completed | failed | declined`. + +### `tool/requestUserInput` + +When the client responds to `item/tool/requestUserInput`, app-server emits `serverRequest/resolved` with `{ threadId, requestId }`. If the pending request is cleared by turn start, turn completion, or turn interruption before the client answers, the server emits the same notification for that cleanup. + +Request params include `autoResolutionMs` as an integer millisecond timeout or +`null`. When present, host clients can resolve the prompt automatically after that +interval if the user doesn't answer. + +### Permission requests + +The built-in `request_permissions` tool sends +`item/permissions/requestApproval` with the `threadId`, `turnId`, `itemId`, +`environmentId`, `cwd`, optional `reason`, and requested network or filesystem +permissions. Respond with `permissions` containing only the granted subset. +Set `scope` to `"session"` to persist the grant for later turns in the same +session; omit it or use `"turn"` for a turn-scoped grant. Permissions that +weren't requested are ignored. + +### MCP server elicitation requests + +An MCP server can interrupt a turn with `mcpServer/elicitation/request`. The +request includes `threadId`, an optional `turnId`, `serverName`, and one of +these request shapes: + +- `mode: "form"` or `mode: "openai/form"`, with `message` and + `requestedSchema`. +- `mode: "url"`, with `message`, `url`, and `elicitationId`. + +Respond with `action: "accept"` and the requested `content`, or with +`action: "decline"` or `"cancel"` and `content: null`. App-server then emits +`serverRequest/resolved`. To receive the `openai/form` variant, opt in with +`initialize.params.capabilities.mcpServerOpenaiFormElicitation`. + +### Dynamic tool calls (experimental) + +`dynamicTools` on `thread/start` and the corresponding `item/tool/call` request or response flow are experimental APIs. + +Dynamic tool names and namespace names must follow Responses API naming +constraints. Avoid reserved namespace names used by built-in Codex tools. + +When a dynamic tool is invoked during a turn, app-server emits: + +1. `item/started` with `item.type = "dynamicToolCall"`, `status = "inProgress"`, plus `tool` and `arguments`. +2. `item/tool/call` as a server request to the client. +3. The client response payload with returned content items. +4. `item/completed` with `item.type = "dynamicToolCall"`, the final `status`, and any returned `contentItems` or `success` value. + +### MCP tool-call approvals (apps) + +App (connector) tool calls can also require approval. When an app tool call has side effects, the server may elicit approval with `tool/requestUserInput` and options such as **Accept**, **Decline**, and **Cancel**. Destructive tool annotations always trigger approval even when the tool also advertises less-privileged hints. If the user declines or cancels, the related `mcpToolCall` item completes with an error instead of running the tool. + +## Skills + +Invoke a skill by including `$` in the user text input. Add a `skill` input item (recommended) so the server injects full skill instructions instead of relying on the model to resolve the name. + +```json +{ + "method": "turn/start", + "id": 101, + "params": { + "threadId": "thread-1", + "input": [ + { + "type": "text", + "text": "$skill-creator Add a new skill for triaging flaky CI." + }, + { + "type": "skill", + "name": "skill-creator", + "path": "/Users/me/.codex/skills/skill-creator/SKILL.md" + } + ] + } +} +``` + +If you omit the `skill` item, the model will still parse the `$` marker and try to locate the skill, which can add latency. + +Example: + +``` +$skill-creator Add a new skill for triaging flaky CI and include step-by-step usage. +``` + +Use `skills/list` to fetch available skills (optionally scoped by `cwds`, with `forceReload`). You can also include `perCwdExtraUserRoots` to scan extra absolute paths as `user` scope for specific `cwd` values. App-server ignores entries whose `cwd` isn't present in `cwds`. `skills/list` may reuse a cached result per `cwd`; set `forceReload: true` to refresh from disk. When present, the server reads `interface` and `dependencies` from `SKILL.json`. + +```json +{ "method": "skills/list", "id": 25, "params": { + "cwds": ["/Users/me/project", "/Users/me/other-project"], + "forceReload": true, + "perCwdExtraUserRoots": [ + { + "cwd": "/Users/me/project", + "extraUserRoots": ["/Users/me/shared-skills"] + } + ] +} } +{ "id": 25, "result": { + "data": [{ + "cwd": "/Users/me/project", + "skills": [ + { + "name": "skill-creator", + "description": "Create or update a Codex skill", + "enabled": true, + "interface": { + "displayName": "Skill Creator", + "shortDescription": "Create or update a Codex skill" + }, + "dependencies": { + "tools": [ + { + "type": "env_var", + "value": "GITHUB_TOKEN", + "description": "GitHub API token" + }, + { + "type": "mcp", + "value": "github", + "transport": "streamable_http", + "url": "https://example.com/mcp" + } + ] + } + } + ], + "errors": [] + }] +} } +``` + +The server also emits `skills/changed` notifications when watched local skill files change. Treat this as an invalidation signal and rerun `skills/list` with your current params when needed. + +To enable or disable a skill by path: + +```json +{ + "method": "skills/config/write", + "id": 26, + "params": { + "path": "/Users/me/.codex/skills/skill-creator/SKILL.md", + "enabled": false + } +} +``` + +## Apps (connectors) + +Use `app/list` to fetch available apps. In the CLI/TUI, `/apps` is the user-facing picker; in custom clients, call `app/list` directly. Each entry includes both `isAccessible` (available to the user) and `isEnabled` (enabled in `config.toml`) so clients can distinguish install/access from local enabled state. App entries can also include optional `branding`, `appMetadata`, and `labels` fields. + +```json +{ "method": "app/list", "id": 50, "params": { + "cursor": null, + "limit": 50, + "threadId": "thread-1", + "forceRefetch": false +} } +{ "id": 50, "result": { + "data": [ + { + "id": "demo-app", + "name": "Demo App", + "description": "Example connector for documentation.", + "logoUrl": "https://example.com/demo-app.png", + "logoUrlDark": null, + "distributionChannel": null, + "branding": null, + "appMetadata": null, + "labels": null, + "installUrl": "https://chatgpt.com/apps/demo-app/demo-app", + "isAccessible": true, + "isEnabled": true + } + ], + "nextCursor": null +} } +``` + +If you provide `threadId`, app feature gating (`features.apps`) uses that thread's config snapshot. When omitted, app-server uses the latest global config. + +`app/list` returns after both accessible apps and directory apps load. Set `forceRefetch: true` to bypass app caches and fetch fresh data. Cache entries are only replaced when refreshes succeed. + +The server also emits `app/list/updated` notifications whenever either source (accessible apps or directory apps) finishes loading. Each notification includes the latest merged app list. + +```json +{ + "method": "app/list/updated", + "params": { + "data": [ + { + "id": "demo-app", + "name": "Demo App", + "description": "Example connector for documentation.", + "logoUrl": "https://example.com/demo-app.png", + "logoUrlDark": null, + "distributionChannel": null, + "branding": null, + "appMetadata": null, + "labels": null, + "installUrl": "https://chatgpt.com/apps/demo-app/demo-app", + "isAccessible": true, + "isEnabled": true + } + ] + } +} +``` + +Invoke an app by inserting `$` in the text input and adding a `mention` input item with the `app://` path (recommended). + +```json +{ + "method": "turn/start", + "id": 51, + "params": { + "threadId": "thread-1", + "input": [ + { + "type": "text", + "text": "$demo-app Pull the latest updates from the team." + }, + { + "type": "mention", + "name": "Demo App", + "path": "app://demo-app" + } + ] + } +} +``` + +### Config RPC examples for app settings + +Use `config/read`, `config/value/write`, and `config/batchWrite` to inspect or update app controls in `config.toml`. + +Read the effective app config shape (including `_default` and per-tool overrides): + +```json +{ "method": "config/read", "id": 60, "params": { "includeLayers": false } } +{ "id": 60, "result": { + "config": { + "apps": { + "_default": { + "enabled": true, + "destructive_enabled": true, + "open_world_enabled": true, + "approvals_reviewer": "user", + "default_tools_approval_mode": "auto" + }, + "google_drive": { + "enabled": true, + "destructive_enabled": false, + "approvals_reviewer": "auto_review", + "default_tools_approval_mode": "prompt", + "tools": { + "files/delete": { "enabled": false, "approval_mode": "approve" } + } + } + } + } +} } +``` + +`apps._default.approvals_reviewer` sets the reviewer for all apps unless a +per-app value overrides it. When both are omitted, the app inherits the +top-level `approvals_reviewer` value. `apps._default.default_tools_approval_mode` +sets the fallback approval mode for tools without a per-app or per-tool +override. Managed approval-mode requirements override tool approval-mode +settings. + +Update a single app setting: + +```json +{ + "method": "config/value/write", + "id": 61, + "params": { + "keyPath": "apps.google_drive.default_tools_approval_mode", + "value": "prompt", + "mergeStrategy": "replace" + } +} +``` + +Apply multiple app edits atomically: + +```json +{ + "method": "config/batchWrite", + "id": 62, + "params": { + "edits": [ + { + "keyPath": "apps._default.destructive_enabled", + "value": false, + "mergeStrategy": "upsert" + }, + { + "keyPath": "apps.google_drive.tools.files/delete.approval_mode", + "value": "approve", + "mergeStrategy": "upsert" + } + ] + } +} +``` + +### Detect and import external agent config + +Use `externalAgentConfig/detect` to discover external-agent artifacts that can be migrated, then pass the selected entries to `externalAgentConfig/import`. + +Detection example: + +```json +{ "method": "externalAgentConfig/detect", "id": 63, "params": { + "includeHome": true, + "cwds": ["/Users/me/project"] +} } +{ "id": 63, "result": { + "items": [ + { + "itemType": "AGENTS_MD", + "description": "Import /Users/me/project/CLAUDE.md to /Users/me/project/AGENTS.md.", + "cwd": "/Users/me/project" + }, + { + "itemType": "SKILLS", + "description": "Copy skill folders from /Users/me/.claude/skills to /Users/me/.agents/skills.", + "cwd": null + } + ] +} } +``` + +Import example: + +```json +{ "method": "externalAgentConfig/import", "id": 64, "params": { + "migrationItems": [ + { + "itemType": "AGENTS_MD", + "description": "Import /Users/me/project/CLAUDE.md to /Users/me/project/AGENTS.md.", + "cwd": "/Users/me/project" + } + ], + "source": "claude-code" +} } +{ "id": 64, "result": { "importId": "8ae96ff3-3425-4f4c-8772-b6fd61502868" } } +``` + +The optional top-level `source` import parameter labels the product that +produced the selected migration items. + +The server emits `externalAgentConfig/import/progress` as item types complete, +and `externalAgentConfig/import/completed` after all synchronous and background +imports finish. These notifications include the same `importId` from the +response and `itemTypeResults` with per-type `successes` and `failures`. +Completion may arrive immediately after the response or after background remote +imports complete. + +```json +{ "method": "externalAgentConfig/import/progress", "params": { + "importId": "8ae96ff3-3425-4f4c-8772-b6fd61502868", + "itemTypeResults": [ + { + "itemType": "AGENTS_MD", + "successes": [ + { "itemType": "AGENTS_MD", "cwd": "/Users/me/project", "source": null, "target": "/Users/me/project/AGENTS.md" } + ], + "failures": [] + } + ] +} } +{ "method": "externalAgentConfig/import/completed", "params": { + "importId": "8ae96ff3-3425-4f4c-8772-b6fd61502868", + "itemTypeResults": [ + { + "itemType": "AGENTS_MD", + "successes": [ + { "itemType": "AGENTS_MD", "cwd": "/Users/me/project", "source": null, "target": "/Users/me/project/AGENTS.md" } + ], + "failures": [] + } + ] +} } +``` + +Read prior completed imports: + +```json +{ "method": "externalAgentConfig/import/readHistories", "id": 65 } +{ "id": 65, "result": { "data": [ + { + "importId": "8ae96ff3-3425-4f4c-8772-b6fd61502868", + "completedAtMs": 1781784000000, + "successes": [ + { "itemType": "AGENTS_MD", "cwd": "/Users/me/project", "source": null, "target": "/Users/me/project/AGENTS.md" } + ], + "failures": [] + } +] } } +``` + +Supported `itemType` values are `AGENTS_MD`, `CONFIG`, `SKILLS`, `PLUGINS`, +`MCP_SERVER_CONFIG`, `SUBAGENTS`, `HOOKS`, `COMMANDS`, and `SESSIONS`. For +`PLUGINS` items, `details.plugins` lists each `marketplaceName` and the +`pluginNames` Codex can try to migrate. Detection returns only items that still +have work to do. For example, Codex skips AGENTS migration when `AGENTS.md` +already exists and is non-empty, and skill imports don't overwrite existing +skill directories. + +When detecting plugins from `.claude/settings.json`, Codex reads configured +marketplace sources from `extraKnownMarketplaces`. If `enabledPlugins` contains +plugins from `claude-plugins-official` but the marketplace source is missing, +Codex infers `anthropics/claude-plugins-official` as the source. + +## Auth endpoints + +The JSON-RPC auth/account surface exposes request/response methods plus server-initiated notifications (no `id`). Use these to determine auth state, start or cancel logins, logout, inspect ChatGPT rate limits, and notify workspace owners about depleted credits or usage limits. + +### Authentication modes + +Codex supports these authentication modes. `account/updated.authMode` shows the active mode and includes the current ChatGPT `planType` when available. `account/read` also reports account and plan details. + +- **API key (`apikey`)** - the caller supplies an OpenAI API key with `type: "apiKey"`, and Codex stores it for API requests. +- **ChatGPT managed (`chatgpt`)** - Codex owns the ChatGPT OAuth flow, persists tokens, and refreshes them automatically. Start with `type: "chatgpt"` for the browser flow or `type: "chatgptDeviceCode"` for the device-code flow. +- **ChatGPT external tokens (`chatgptAuthTokens`)** - experimental and intended for host apps that already own the user's ChatGPT auth lifecycle. The host app supplies an `accessToken`, `chatgptAccountId`, and optional `chatgptPlanType` directly, and must refresh the token when asked. +- **Amazon Bedrock** - `account/read` reports Bedrock accounts as `type: "amazonBedrock"` and indicates whether credentials come from a Codex-managed Bedrock API key (`credentialSource: "codexManaged"`) or the external AWS credential chain (`credentialSource: "awsManaged"`). `account/updated.authMode` uses `bedrockApiKey` for Codex-managed Bedrock API keys. + +### API overview + +- `account/read` - fetch current account info; optionally refresh tokens. +- `account/login/start` - begin login (`apiKey`, `chatgpt`, `chatgptDeviceCode`, or experimental `chatgptAuthTokens`). +- `account/login/completed` (notify) - emitted when a login attempt finishes (success or error). +- `account/login/cancel` - cancel a pending managed ChatGPT login by `loginId`. +- `account/logout` - sign out; triggers `account/updated`. +- `account/updated` (notify) - emitted whenever auth mode changes (`authMode`: `apikey`, `chatgpt`, `chatgptAuthTokens`, `agentIdentity`, `personalAccessToken`, `bedrockApiKey`, or `null`) and includes `planType` when available. +- `account/chatgptAuthTokens/refresh` (server request) - request fresh externally managed ChatGPT tokens after an authorization error. +- `account/rateLimits/read` - fetch ChatGPT rate limits. +- `account/rateLimits/updated` (notify) - emitted whenever a user's ChatGPT rate limits change. +- `account/sendAddCreditsNudgeEmail` - ask ChatGPT to email a workspace owner about depleted credits or a reached usage limit. +- `account/rateLimitResetCredit/consume` - consume one earned rate-limit reset using a caller-provided `idempotencyKey` value. +- `account/usage/read` - fetch ChatGPT account token-activity summaries and daily buckets. +- `account/workspaceMessages/read` - fetch active workspace messages, including notification headlines when available. +- `mcpServer/oauthLogin/completed` (notify) - emitted after a `mcpServer/oauth/login` flow finishes; payload includes `{ name, threadId, success, error? }`. `threadId` can be `null` for app-scoped or plugin OAuth flows. +- `mcpServer/startupStatus/updated` (notify) - emitted when a configured MCP server's startup status changes; payload includes `{ threadId, name, status, error, failureReason }`. `threadId` is `null` for app-scoped startup. On failed startup, `failureReason: "reauthenticationRequired"` means stored OAuth credentials expired and couldn't be refreshed, so the client should offer to reconnect the server. + +### 1) Check auth state + +Request: + +```json +{ "method": "account/read", "id": 1, "params": { "refreshToken": false } } +``` + +Response examples: + +```json +{ "id": 1, "result": { "account": null, "requiresOpenaiAuth": false } } +``` + +```json +{ "id": 1, "result": { "account": null, "requiresOpenaiAuth": true } } +``` + +```json +{ + "id": 1, + "result": { "account": { "type": "apiKey" }, "requiresOpenaiAuth": true } +} +``` + +```json +{ + "id": 1, + "result": { + "account": { + "type": "amazonBedrock", + "credentialSource": "codexManaged" + }, + "requiresOpenaiAuth": false + } +} +``` + +```json +{ + "id": 1, + "result": { + "account": { + "type": "amazonBedrock", + "credentialSource": "awsManaged" + }, + "requiresOpenaiAuth": false + } +} +``` + +```json +{ + "id": 1, + "result": { + "account": { + "type": "chatgpt", + "email": "user@example.com", + "planType": "pro" + }, + "requiresOpenaiAuth": true + } +} +``` + +Field notes: + +- `refreshToken` (boolean): set `true` to force a token refresh in managed ChatGPT mode. In external token mode (`chatgptAuthTokens`), app-server ignores this flag. +- `email` is `null` when the ChatGPT account doesn't have an email address. +- `requiresOpenaiAuth` reflects the active provider; when `false`, Codex can run without OpenAI credentials. +- Amazon Bedrock reports `credentialSource: "codexManaged"` when it uses a + Bedrock API key managed by Codex. It reports `credentialSource: "awsManaged"` + for the external AWS credential path. This identifies the selected credential + source; it doesn't validate that the AWS credential chain can resolve + credentials. + +### 2) Log in with an API key + +1. Send: + +```json + { + "method": "account/login/start", + "id": 2, + "params": { "type": "apiKey", "apiKey": "sk-..." } + } +``` + +2. Expect: + +```json + { "id": 2, "result": { "type": "apiKey" } } +``` + +3. Notifications: + +```json + { + "method": "account/login/completed", + "params": { "loginId": null, "success": true, "error": null } + } +``` + +```json + { + "method": "account/updated", + "params": { "authMode": "apikey", "planType": null } + } +``` + +### 3) Log in with ChatGPT (browser flow) + +1. Start: + +```json + { + "method": "account/login/start", + "id": 3, + "params": { + "type": "chatgpt", + "useHostedLoginSuccessPage": true, + "appBrand": "chatgpt" + } + } +``` + +By default, a successful browser callback redirects to a local success page. +Set `useHostedLoginSuccessPage: true` to use the hosted success page when +organization setup isn't required. With hosted success enabled, `appBrand` +can be `"codex"` or `"chatgpt"`; omitted or `null` values default to +`"codex"`. + +```json + { + "id": 3, + "result": { + "type": "chatgpt", + "loginId": "", + "authUrl": "https://chatgpt.com/...&redirect_uri=http%3A%2F%2Flocalhost%3A%2Fauth%2Fcallback" + } + } +``` + +2. Open `authUrl` in a browser; the app-server hosts the local callback. +3. Wait for notifications: + +```json + { + "method": "account/login/completed", + "params": { "loginId": "", "success": true, "error": null } + } +``` + +```json + { + "method": "account/updated", + "params": { "authMode": "chatgpt", "planType": "plus" } + } +``` + +### 3b) Log in with ChatGPT (device-code flow) + +Use this flow when your client owns the sign-in ceremony or when a browser callback is brittle. + +1. Start: + +```json + { + "method": "account/login/start", + "id": 4, + "params": { "type": "chatgptDeviceCode" } + } +``` + +```json + { + "id": 4, + "result": { + "type": "chatgptDeviceCode", + "loginId": "", + "verificationUrl": "https://auth.openai.com/codex/device", + "userCode": "ABCD-1234" + } + } +``` + +2. Show `verificationUrl` and `userCode` to the user; the frontend owns the UX. +3. Wait for notifications: + +```json + { + "method": "account/login/completed", + "params": { "loginId": "", "success": true, "error": null } + } +``` + +```json + { + "method": "account/updated", + "params": { "authMode": "chatgpt", "planType": "plus" } + } +``` + +### 3c) Log in with externally managed ChatGPT tokens (`chatgptAuthTokens`) + +Use this experimental mode only when a host application owns the user's ChatGPT auth lifecycle and supplies tokens directly. Clients must set `capabilities.experimentalApi = true` during `initialize` before using this login type. + +1. Send: + +```json + { + "method": "account/login/start", + "id": 7, + "params": { + "type": "chatgptAuthTokens", + "accessToken": "", + "chatgptAccountId": "org-123", + "chatgptPlanType": "business" + } + } +``` + +2. Expect: + +```json + { "id": 7, "result": { "type": "chatgptAuthTokens" } } +``` + +3. Notifications: + +```json + { + "method": "account/login/completed", + "params": { "loginId": null, "success": true, "error": null } + } +``` + +```json + { + "method": "account/updated", + "params": { "authMode": "chatgptAuthTokens", "planType": "business" } + } +``` + +When the server receives a `401 Unauthorized`, it may request refreshed tokens from the host app: + +```json +{ + "method": "account/chatgptAuthTokens/refresh", + "id": 8, + "params": { "reason": "unauthorized", "previousAccountId": "org-123" } +} +{ "id": 8, "result": { "accessToken": "", "chatgptAccountId": "org-123", "chatgptPlanType": "business" } } +``` + +The server retries the original request after a successful refresh response. Requests time out after about 10 seconds. + +### 4) Cancel a ChatGPT login + +```json +{ "method": "account/login/cancel", "id": 4, "params": { "loginId": "" } } +{ "method": "account/login/completed", "params": { "loginId": "", "success": false, "error": "..." } } +``` + +### 5) Logout + +```json +{ "method": "account/logout", "id": 5 } +{ "id": 5, "result": {} } +{ "method": "account/updated", "params": { "authMode": null, "planType": null } } +``` + +### 6) Rate limits (ChatGPT) + +```json +{ "method": "account/rateLimits/read", "id": 6 } +{ "id": 6, "result": { + "rateLimits": { + "limitId": "codex", + "limitName": null, + "primary": { "usedPercent": 25, "windowDurationMins": 15, "resetsAt": 1730947200 }, + "secondary": null, + "rateLimitReachedType": null + }, + "rateLimitsByLimitId": { + "codex": { + "limitId": "codex", + "limitName": null, + "primary": { "usedPercent": 25, "windowDurationMins": 15, "resetsAt": 1730947200 }, + "secondary": null, + "rateLimitReachedType": null + }, + "codex_other": { + "limitId": "codex_other", + "limitName": "codex_other", + "primary": { "usedPercent": 42, "windowDurationMins": 60, "resetsAt": 1730950800 }, + "secondary": null, + "rateLimitReachedType": null + } + }, + "rateLimitResetCredits": { + "availableCount": 2, + "credits": [{ + "id": "RateLimitResetCredit_1", + "resetType": "codexRateLimits", + "status": "available", + "grantedAt": 1781654400, + "expiresAt": 1784246400, + "title": "Rate-limit reset", + "description": "Reset an eligible Codex rate-limit window." + }] + } +} } +{ "method": "account/rateLimits/updated", "params": { + "rateLimits": { + "limitId": "codex", + "primary": { "usedPercent": 31, "windowDurationMins": 15, "resetsAt": 1730948100 } + } +} } +``` + +Field notes: + +- `rateLimits` is the backward-compatible single-bucket view. +- `rateLimitsByLimitId` (when present) is the multi-bucket view keyed by metered `limit_id` (for example `codex`). +- `limitId` is the metered bucket identifier. +- `limitName` is an optional user-facing label for the bucket. +- `usedPercent` is current usage within the quota window. +- `windowDurationMins` is the quota window length. +- `resetsAt` is a Unix timestamp (seconds) for the next reset. +- `planType` is included when the server returns the ChatGPT plan associated with a bucket. +- `credits` is included when the server returns remaining workspace credit details. +- `rateLimitReachedType` identifies the server-classified limit state when one has been reached. +- `rateLimitResetCredits` contains the available earned-reset count when the service provides it; otherwise it's `null`. +- `rateLimitResetCredits.credits` is `null` when only the count is known. An empty array means the service fetched details and returned no available credits. The service can cap the detail rows, so `availableCount` is authoritative. +- Each detail row includes an opaque `id`, `resetType`, `status`, `grantedAt`, `expiresAt` (which can be `null`), `title` (which can be `null`), and `description` (which can be `null`). +- Fetch `account/rateLimits/read` after consuming a reset. + +### 7) Token usage (ChatGPT) + +Use `account/usage/read` to fetch ChatGPT token-activity summary fields and +optional daily buckets. + +```json +{ "method": "account/usage/read", "id": 7 } +{ "id": 7, "result": { + "summary": { + "lifetimeTokens": 1234567, + "peakDailyTokens": 45678, + "longestRunningTurnSec": 540, + "currentStreakDays": 8, + "longestStreakDays": 14 + }, + "dailyUsageBuckets": [ + { "startDate": "2026-06-18", "tokens": 12345 } + ] +} } +``` + +Field notes: + +- `summary` values may be `null` when the service hasn't returned that metric. +- `dailyUsageBuckets` may be `null`; when present, each bucket includes `startDate` and `tokens`. +- The endpoint requires authentication backed by Codex services. ChatGPT, + external ChatGPT tokens, agent identity, and personal access token auth work; + API-key-only and Bedrock auth don't. + +### 8) Earned rate-limit resets (ChatGPT) + +Use `account/rateLimitResetCredit/consume` to consume one earned reset. + +```json +{ "method": "account/rateLimitResetCredit/consume", "id": 8, "params": { "idempotencyKey": "8ae96ff3-3425-4f4c-8772-b6fd61502868", "creditId": "RateLimitResetCredit_1" } } +{ "id": 8, "result": { "outcome": "reset" } } +``` + +Field notes: + +- `idempotencyKey` must be non-empty. Use a UUID for each logical redemption attempt and reuse the same value when retrying that attempt. +- `creditId` is optional. When provided, it must be a non-empty opaque ID from `account/rateLimits/read`. When omitted, the service selects the next available credit. +- `reset` means a credit was consumed. +- `alreadyRedeemed` means the same redemption completed previously. Treat it as an idempotent success and refresh account limits. +- `nothingToReset` means there is no eligible rate-limit window to reset. +- `noCredit` means the account has no earned reset credits available. +- Fetch `account/rateLimits/read` after consuming a reset instead of inferring updated windows from this response. + +### 9) Notify a workspace owner about a limit + +Use `account/sendAddCreditsNudgeEmail` to ask ChatGPT to email a workspace owner when credits are depleted or a usage limit has been reached. + +```json +{ "method": "account/sendAddCreditsNudgeEmail", "id": 9, "params": { "creditType": "credits" } } +{ "id": 9, "result": { "status": "sent" } } +``` + +Use `creditType: "credits"` when workspace credits are depleted, or `creditType: "usage_limit"` when the workspace usage limit has been reached. If the owner was already notified recently, the response status is `cooldown_active`. + +### 10) Workspace messages (ChatGPT) + +Use `account/workspaceMessages/read` to fetch active messages for the current +workspace, including notification headlines when available. + +```json +{ "method": "account/workspaceMessages/read", "id": 10 } +{ "id": 10, "result": { "featureEnabled": true, "messages": [ + { "messageId": "msg_123", "messageType": "headline", "messageBody": "Workspace maintenance starts at 5pm.", "createdAt": 1781395200, "archivedAt": null } +] } } +``` \ No newline at end of file diff --git a/canvas-agent/package.json b/canvas-agent/package.json index 1121730..0d5ef30 100644 --- a/canvas-agent/package.json +++ b/canvas-agent/package.json @@ -15,7 +15,7 @@ "scripts": { "dev": "tsx src/index.ts", "debug": "tsx src/index.ts --debug", - "test": "tsx --test src/canvas-session.test.ts", + "test": "tsx --test src/canvas/session.test.ts", "build": "tsc -p tsconfig.json", "start": "node dist/index.js", "prepack": "npm run build" diff --git a/canvas-agent/src/agent/claude.ts b/canvas-agent/src/agent/claude.ts new file mode 100644 index 0000000..511093b --- /dev/null +++ b/canvas-agent/src/agent/claude.ts @@ -0,0 +1,48 @@ +import { spawn } from "node:child_process"; + +import { AGENT_PROMPT } from "../config.js"; +import { errorMessage } from "../utils/value.js"; +import type { AgentEmit } from "./types.js"; + +/** 使用 Claude CLI 执行一次带 Canvas Agent 工具的任务。 */ +export function runClaudeTurn(prompt: string, emit: AgentEmit) { + const fullPrompt = withAgentPrompt(prompt); + if (!fullPrompt) return; + const child = spawnAgent("claude", ["-p", "--output-format", "stream-json", "--verbose", "--include-partial-messages", "--allowedTools", "mcp__infinite-canvas__*", fullPrompt], emit); + if (child) pipeJsonLines(child, emit, "claude"); +} + +/** 为 Claude CLI 请求拼接 Canvas Agent 指令。 */ +function withAgentPrompt(prompt: string) { + return prompt.trim() ? `${AGENT_PROMPT}\n\n用户请求:${prompt}` : ""; +} + +/** 将 Claude CLI 的 JSON Lines 输出转换为 Agent 事件。 */ +function pipeJsonLines(child: ReturnType, emit: AgentEmit, agent: string) { + let out = ""; + child.stdout?.on("data", (chunk) => { + out += chunk.toString(); + const lines = out.split(/\r?\n/); + out = lines.pop() || ""; + lines.filter(Boolean).forEach((line) => { + try { + emit("agent_event", { agent, ...JSON.parse(line) }); + } catch { + emit("agent_event", { agent, type: "raw", text: line }); + } + }); + }); + child.stderr?.on("data", (chunk) => emit("agent_log", { text: chunk.toString() })); + child.on("error", (error) => emit("agent_error", { message: error.message })); + child.on("close", (code) => emit("agent_done", { agent, code })); +} + +/** 启动外部 Agent CLI,并将同步启动异常转换为事件。 */ +function spawnAgent(name: string, args: string[], emit: AgentEmit) { + try { + return spawn(name, args, { stdio: ["ignore", "pipe", "pipe"], shell: process.platform === "win32", windowsHide: true }); + } catch (error) { + emit("agent_error", { message: errorMessage(error) }); + return null; + } +} diff --git a/canvas-agent/src/agent/codex-client.ts b/canvas-agent/src/agent/codex-client.ts new file mode 100644 index 0000000..d91b34e --- /dev/null +++ b/canvas-agent/src/agent/codex-client.ts @@ -0,0 +1,308 @@ +import { spawn, type ChildProcess } from "node:child_process"; +import { createRequire } from "node:module"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +import { VERSION } from "../config.js"; +import { logger } from "../utils/logger.js"; +import { field, type JsonRecord } from "../utils/value.js"; +import type { AgentEmit } from "./types.js"; + +type AgentEvent = JsonRecord & { type: string; usage?: unknown }; +type PendingRequest = { resolve: (value: unknown) => void; reject: (error: Error) => void }; + +const canvasAgentMcp = canvasAgentMcpCommand(); +const require = createRequire(import.meta.url); + +/** 封装 Codex app-server 的 JSON-RPC 通信与事件转换。 */ +export class CodexAppClient { + private nextId = 1; + private buffer = ""; + private currentThreadId = ""; + private textByItem = new Map(); + private lastUsage: unknown = null; + private pending = new Map(); + private activeTurns = new Map(); + private completedTurns = new Map(); + + /** 保存 app-server 子进程和事件出口。 */ + private constructor(private child: ChildProcess, private emit: AgentEmit) {} + + /** 启动并初始化 Codex app-server。 */ + static async start(emit: AgentEmit, onExit: () => void) { + logger.info("Starting Codex app-server", { executable: process.execPath, codex: codexBin() }); + const child = spawn(process.execPath, [codexBin(), "app-server", "--stdio"], { stdio: ["pipe", "pipe", "pipe"], windowsHide: true }); + const client = new CodexAppClient(child, emit); + child.stdout?.on("data", (chunk) => client.read(chunk.toString())); + child.stderr?.on("data", (chunk) => { + const text = chunk.toString(); + logger.warn("Codex app-server stderr", { text }); + emit("agent_log", { text }); + }); + child.on("error", (error) => { + logger.error("Codex app-server process error", error); + emit("agent_error", { message: error.message }); + }); + child.on("exit", (code) => { + logger.warn("Codex app-server exited", { code }); + client.failAll(`Codex app-server exited: ${code ?? 0}`); + onExit(); + emit("agent_log", { text: `Codex app-server exited: ${code ?? 0}` }); + }); + await client.request("initialize", { clientInfo: { name: "canvas-agent", title: "Infinite Canvas Agent", version: VERSION }, capabilities: { experimentalApi: true, requestAttestation: false } }); + client.notify("initialized"); + return client; + } + + /** 创建新的 Codex 线程。 */ + async startThread(cwd?: string) { + const result = await this.request("thread/start", { approvalPolicy: "never", sandbox: "workspace-write", config: codexConfig(), ...(cwd ? { cwd } : {}), threadSource: "user" }); + const thread = field(result, "thread") as JsonRecord | undefined; + const id = String(field(thread, "id") || ""); + if (!id) throw new Error("Codex app-server 没有返回 thread id"); + return thread || {}; + } + + /** 恢复已有 Codex 线程。 */ + async resumeThread(threadId: string, cwd?: string) { + const result = await this.request("thread/resume", { threadId, approvalPolicy: "never", sandbox: "workspace-write", config: codexConfig(), ...(cwd ? { cwd } : {}) }); + const thread = field(result, "thread") as JsonRecord | undefined; + const id = String(field(thread, "id") || ""); + if (!id) throw new Error("Codex app-server 没有返回 thread id"); + return thread || {}; + } + + /** 查询 Codex 线程列表。 */ + listThreads(params: JsonRecord) { + return this.request("thread/list", params); + } + + /** 读取指定 Codex 线程。 */ + readThread(threadId: string, includeTurns = true) { + return this.request("thread/read", { threadId, includeTurns }); + } + + /** 归档指定 Codex 线程。 */ + archiveThread(threadId: string) { + return this.request("thread/archive", { threadId }); + } + + /** 启动一个 Codex turn 并等待完成通知。 */ + async startTurn(threadId: string, prompt: string, images: string[], onTurn?: (turnId: string) => void) { + const result = await this.request("turn/start", { threadId, input: codexInput(prompt, images), approvalPolicy: "never" }); + const turnId = String(field(field(result, "turn"), "id") || ""); + if (!turnId) throw new Error("Codex app-server 没有返回 turn id"); + this.currentThreadId = threadId; + onTurn?.(turnId); + const completed = this.completedTurns.get(turnId); + if (this.completedTurns.has(turnId)) { + this.completedTurns.delete(turnId); + if (completed) throw completed; + return; + } + await new Promise((resolve, reject) => this.activeTurns.set(turnId, { resolve, reject })); + } + + /** 中断当前正在运行的 Codex turn。 */ + interruptCurrentTurn() { + if (this.activeTurns.size === 0) return false; + try { + logger.warn("Interrupting active Codex turn", { threadId: this.currentThreadId, activeTurns: this.activeTurns.size }); + this.child.kill("SIGINT"); + return true; + } catch { + return false; + } + } + + /** 发送 JSON-RPC 请求并保存待处理 Promise。 */ + private request(method: string, params: unknown) { + const id = this.nextId++; + this.write({ id, method, params }); + return new Promise((resolve, reject) => this.pending.set(id, { resolve, reject })); + } + + /** 发送无需响应的 JSON-RPC 通知。 */ + private notify(method: string, params?: unknown) { + this.write(params === undefined ? { method } : { method, params }); + } + + /** 将 JSON-RPC 消息写入 app-server 标准输入。 */ + private write(value: unknown) { + const method = String(field(value, "method") || ""); + const params = field(value, "params"); + if (method) logger.debug(`Codex ${method}`, { id: field(value, "id"), threadId: field(params, "threadId") }); + this.child.stdin?.write(`${JSON.stringify(value)}\n`); + } + + /** 按行解析 app-server 标准输出。 */ + private read(chunk: string) { + this.buffer += chunk; + const lines = this.buffer.split(/\r?\n/); + this.buffer = lines.pop() || ""; + lines.filter(Boolean).forEach((line) => { + try { + this.handle(JSON.parse(line) as JsonRecord); + } catch (error) { + logger.warn("Invalid Codex app-server output", { error, line }); + this.emit("agent_log", { text: line }); + } + }); + } + + /** 分派单条 JSON-RPC 响应、请求或通知。 */ + private handle(message: JsonRecord) { + const id = Number(message.id); + if (message.error && this.pending.has(id)) { + const error = String(field(message.error, "message") || "Codex request failed"); + logger.warn("Codex request failed", { id, error }); + return this.reject(id, error); + } + if (this.pending.has(id)) return this.resolve(id, message.result); + if (typeof message.method === "string" && "id" in message) return this.answerServerRequest(message); + if (typeof message.method === "string") this.handleNotification(message.method, (message.params || {}) as JsonRecord); + } + + /** 转换并广播 app-server 通知。 */ + private handleNotification(method: string, params: JsonRecord) { + if (method === "item/agentMessage/delta") return this.emitDelta(params); + if (method === "thread/tokenUsage/updated") { + this.lastUsage = normalizeUsage(params); + this.emit("agent_event", { agent: "codex", type: "usage.updated", usage: this.lastUsage, ...codexEventScope(params) }); + return; + } + const event = normalizeCodexNotification(method, params); + if (!event) return; + if (event.type === "item.completed") { + const item = field(event, "item") as JsonRecord | undefined; + const id = String(field(item, "id") || ""); + const streamedText = this.textByItem.get(id); + if (item?.type === "agent_message" && streamedText && !item.text) item.text = streamedText; + if (id) this.textByItem.delete(id); + } + if (event.type === "turn.completed") event.usage = this.lastUsage; + this.emit("agent_event", { agent: "codex", ...event }); + if (event.type === "turn.completed") { + const turnId = String(field(params, "turnId") || field(field(params, "turn"), "id") || ""); + const pending = this.activeTurns.get(turnId); + const error = field(field(params, "turn"), "error"); + if (pending) { + this.activeTurns.delete(turnId); + error ? pending.reject(new Error(String(field(error, "message") || "Codex turn failed"))) : pending.resolve(event); + } else if (turnId) { + this.completedTurns.set(turnId, error ? new Error(String(field(error, "message") || "Codex turn failed")) : null); + } + if (this.activeTurns.size === 0) this.currentThreadId = ""; + this.emit("agent_done", { agent: "codex", usage: event.usage, ...codexEventScope(params) }); + } + } + + /** 合并并广播 Agent 文本增量。 */ + private emitDelta(params: JsonRecord) { + const id = String(field(params, "itemId") || ""); + const text = `${this.textByItem.get(id) || ""}${String(field(params, "delta") || "")}`; + this.textByItem.set(id, text); + this.emit("agent_event", { agent: "codex", type: "item.updated", item: { id, type: "agent_message", text }, ...codexEventScope(params) }); + } + + /** 自动回复 app-server 发起的授权或交互请求。 */ + private answerServerRequest(message: JsonRecord) { + const method = String(message.method); + const result = method === "mcpServer/elicitation/request" ? { action: "accept", content: {}, _meta: null } : { decision: "decline" }; + this.write({ id: message.id, result }); + this.emit("agent_event", { agent: "codex", type: "server.request", method, params: message.params, result }); + } + + /** 完成指定 JSON-RPC 请求。 */ + private resolve(id: number, result: unknown) { + const pending = this.pending.get(id); + if (pending) (this.pending.delete(id), pending.resolve(result)); + } + + /** 拒绝指定 JSON-RPC 请求。 */ + private reject(id: number, message: string) { + const pending = this.pending.get(id); + if (pending) (this.pending.delete(id), pending.reject(new Error(message))); + } + + /** 拒绝进程退出时仍未完成的请求与 turn。 */ + private failAll(message: string) { + [...this.pending.values(), ...this.activeTurns.values()].forEach((item) => item.reject(new Error(message))); + this.pending.clear(); + this.activeTurns.clear(); + this.currentThreadId = ""; + } +} + +/** 生成 Codex 调用 Canvas Agent MCP 的启动命令。 */ +function canvasAgentMcpCommand() { + const current = process.argv.find((arg) => /index\.(t|j)s$/.test(arg)) || ""; + const entry = path.resolve(current || fileURLToPath(new URL("../index.js", import.meta.url))); + const tsx = path.join(path.dirname(entry), "..", "node_modules", "tsx", "dist", "cli.mjs"); + return entry.endsWith(".ts") ? { command: process.execPath, args: [tsx, entry, "mcp"] } : { command: process.execPath, args: [entry, "mcp"] }; +} + +/** 生成 Codex app-server 使用的 MCP 配置。 */ +function codexConfig() { + return { mcp_servers: { "infinite-canvas": { command: canvasAgentMcp.command, args: canvasAgentMcp.args, default_tools_approval_mode: "approve", startup_timeout_sec: 20, tool_timeout_sec: 90 } } }; +} + +/** 将文本和本地图片转换为 Codex turn 输入。 */ +function codexInput(prompt: string, images: string[]) { + return [{ type: "text", text: prompt, text_elements: [] }, ...images.map((file) => ({ type: "localImage", path: file }))]; +} + +/** 将 app-server 通知转换为前端使用的 Agent 事件。 */ +function normalizeCodexNotification(method: string, params: JsonRecord): AgentEvent | null { + const scope = codexEventScope(params); + if (method === "thread/started") return { type: "thread.started", ...scope }; + if (method === "turn/started") return { type: "turn.started", ...scope }; + if (method === "turn/completed") return { type: "turn.completed", usage: null, duration_ms: field(field(params, "turn"), "durationMs"), ...scope }; + if (method === "item/started") return { type: "item.started", item: normalizeItem(field(params, "item")), ...scope }; + if (method === "item/completed") return { type: "item.completed", item: normalizeItem(field(params, "item")), ...scope }; + if (method === "error") return { type: "error", message: field(params, "message"), ...scope }; + return null; +} + +/** 提取 Codex 事件所属的线程和 turn。 */ +function codexEventScope(params: JsonRecord) { + const threadId = String(field(params, "threadId") || field(field(params, "thread"), "id") || ""); + const turnId = String(field(params, "turnId") || field(field(params, "turn"), "id") || ""); + return { ...(threadId ? { thread_id: threadId } : {}), ...(turnId ? { turn_id: turnId } : {}) }; +} + +/** 统一 app-server item 的类型和参数格式。 */ +function normalizeItem(item: unknown) { + const value = item && typeof item === "object" ? { ...(item as JsonRecord) } : {}; + if (value.type === "agentMessage") value.type = "agent_message"; + if (value.type === "mcpToolCall") value.type = "mcp_tool_call"; + if (value.type === "agent_message" && typeof value.id === "string") value.text = String(value.text || ""); + if ("arguments" in value) value.arguments = parseMaybeJson(value.arguments); + return value; +} + +/** 将 Codex token usage 转换为前端字段。 */ +function normalizeUsage(params: JsonRecord) { + const last = field(field(params, "tokenUsage"), "last") as JsonRecord | undefined; + return { + input_tokens: field(last, "inputTokens"), + cached_input_tokens: field(last, "cachedInputTokens"), + output_tokens: field(last, "outputTokens"), + reasoning_output_tokens: field(last, "reasoningOutputTokens"), + }; +} + +/** 尝试将字符串解析为 JSON,失败时保留原值。 */ +function parseMaybeJson(value: unknown) { + if (typeof value !== "string") return value; + try { + return JSON.parse(value); + } catch { + return value; + } +} + +/** 定位当前依赖中 Codex CLI 的执行文件。 */ +function codexBin() { + return path.join(path.dirname(require.resolve("@openai/codex/package.json")), "bin", "codex.js"); +} diff --git a/canvas-agent/src/agent/codex-history.ts b/canvas-agent/src/agent/codex-history.ts new file mode 100644 index 0000000..70d0486 --- /dev/null +++ b/canvas-agent/src/agent/codex-history.ts @@ -0,0 +1,100 @@ +import { field } from "../utils/value.js"; + +type AgentHistoryMessage = { id: string; role: "user" | "assistant" | "tool" | "error"; title?: string; text: string; detail?: unknown; streamId?: string }; + +/** 将 Codex 线程转换为列表展示所需的摘要。 */ +export function summarizeCodexThread(thread: unknown) { + return { + id: String(field(thread, "id") || ""), + sessionId: String(field(thread, "sessionId") || ""), + preview: displayUserText(String(field(thread, "preview") || "")), + name: stringOrNull(field(thread, "name")), + cwd: String(field(thread, "cwd") || ""), + status: String(field(thread, "status") || ""), + source: field(thread, "source"), + threadSource: field(thread, "threadSource"), + createdAt: Number(field(thread, "createdAt") || 0), + updatedAt: Number(field(thread, "updatedAt") || 0), + }; +} + +/** 将 Codex turn items 转换为网页聊天历史。 */ +export function threadMessages(thread: unknown): AgentHistoryMessage[] { + const turns = arrayValue(field(thread, "turns")); + const messages: AgentHistoryMessage[] = []; + turns.forEach((turn, turnIndex) => { + arrayValue(field(turn, "items")).forEach((item, itemIndex) => { + const type = String(field(item, "type") || ""); + const id = String(field(item, "id") || `${turnIndex}-${itemIndex}`); + if (type === "userMessage") { + const text = displayUserText(userInputText(field(item, "content"))); + if (text) messages.push({ id, role: "user", text }); + } + if (type === "agentMessage") { + const text = String(field(item, "text") || "").trim(); + if (text) messages.push({ id, role: "assistant", title: "Codex", text }); + } + if (type === "mcpToolCall") { + const tool = String(field(item, "tool") || "工具调用"); + const error = field(field(item, "error"), "message"); + messages.push({ id, role: error ? "error" : "tool", title: toolName(tool), text: error ? String(error) : `${toolName(tool)} ${String(field(item, "status") || "完成")}`, detail: item }); + } + if (type === "commandExecution") { + const command = String(field(item, "command") || "").trim(); + if (command) messages.push({ id, role: "tool", title: "命令", text: command, detail: { cwd: field(item, "cwd"), status: field(item, "status"), exitCode: field(item, "exitCode") } }); + } + if (type === "fileChange") messages.push({ id, role: "tool", title: "文件变更", text: "Codex 修改了文件", detail: item }); + }); + }); + return messages.filter((item) => item.text).slice(-120); +} + +/** 提取用户输入条目中的文本与附件占位信息。 */ +function userInputText(content: unknown) { + return arrayValue(content) + .map((item) => { + const type = String(field(item, "type") || ""); + if (type === "text") return String(field(item, "text") || ""); + if (type === "image" || type === "localImage") return "图片附件"; + if (type === "mention") return `@${String(field(item, "name") || "文件")}`; + return ""; + }) + .filter(Boolean) + .join("\n"); +} + +/** 移除用户消息中由旧流程拼接的 Agent 前置提示词。 */ +function displayUserText(text: string) { + const value = text.trim(); + const marker = "用户请求:"; + const index = value.lastIndexOf(marker); + return (index >= 0 ? value.slice(index + marker.length) : value).trim(); +} + +/** 将未知值转换为数组。 */ +function arrayValue(value: unknown) { + return Array.isArray(value) ? value : []; +} + +/** 将非空字符串保留为字符串,否则返回 null。 */ +function stringOrNull(value: unknown) { + return typeof value === "string" && value.trim() ? value : null; +} + +/** 将 MCP 工具名称转换为聊天记录中的中文标题。 */ +function toolName(name: string) { + if (name === "canvas_apply_ops") return "画布操作"; + if (name === "canvas_get_state") return "读取画布"; + if (name === "canvas_get_selection") return "读取选区"; + if (name === "canvas_export_snapshot") return "导出快照"; + if (name === "canvas_create_attachment_nodes") return "添加附件图片"; + if (name === "canvas_create_text_node") return "创建文本"; + if (name === "canvas_create_image_prompt_flow") return "创建生图流程"; + if (name === "canvas_create_generation_flow") return "创建生成流程"; + if (name === "canvas_generate_text") return "生成文本"; + if (name === "canvas_generate_image") return "生成图片"; + if (name === "canvas_generate_video") return "生成视频"; + if (name === "canvas_generate_audio") return "生成音频"; + if (name === "canvas_run_generation") return "触发生成"; + return name; +} diff --git a/canvas-agent/src/agent/codex.ts b/canvas-agent/src/agent/codex.ts new file mode 100644 index 0000000..c8b19a7 --- /dev/null +++ b/canvas-agent/src/agent/codex.ts @@ -0,0 +1,195 @@ +import fs from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; + +import { logger } from "../utils/logger.js"; +import { errorMessage, field } from "../utils/value.js"; +import { CodexAppClient } from "./codex-client.js"; +import { summarizeCodexThread, threadMessages } from "./codex-history.js"; +import type { AgentAttachment, AgentEmit } from "./types.js"; + +type CodexRunOptions = { threadId?: string; cwd?: string; appEmit?: AgentEmit; onStart?: () => void; onThread?: (threadId: string) => void; onTurn?: (turnId: string) => void; onFinish?: () => void }; + +let codexQueue: Promise = Promise.resolve(); +let codexApp: CodexAppClient | null = null; +let codexAppStart: Promise | null = null; +let codexThreadId = ""; + +export { summarizeCodexThread } from "./codex-history.js"; + +/** 将 Codex turn 加入串行队列并等待执行完成。 */ +export async function runCodexTurn(prompt: string, emit: AgentEmit, attachments: AgentAttachment[] = [], options: CodexRunOptions = {}) { + if (!prompt.trim()) return; + codexQueue = codexQueue.catch(() => undefined).then(() => runCodexTurnNow(prompt, emit, attachments, options)); + await codexQueue; +} + +/** 中断当前线程正在执行的 Codex turn。 */ +export function interruptCodexTurn(threadId?: string) { + if (!codexApp || (threadId && threadId !== codexThreadId)) return false; + return codexApp.interruptCurrentTurn(); +} + +/** 创建新的 Codex 线程并记录当前线程 ID。 */ +export async function startCodexThread(emit: AgentEmit, cwd?: string) { + const app = await getCodexApp(emit); + const thread = await app.startThread(cwd); + codexThreadId = String(field(thread, "id") || ""); + return thread; +} + +/** 恢复指定 Codex 线程并返回聊天历史。 */ +export async function resumeCodexThread(emit: AgentEmit, threadId: string, cwd?: string) { + const app = await getCodexApp(emit); + await loadCodexThread(emit, threadId, cwd, false); + const thread = await app.resumeThread(threadId, cwd); + assertThreadWorkspace(thread, cwd); + codexThreadId = String(field(thread, "id") || threadId); + return { thread, messages: threadMessages(thread) }; +} + +/** 查询当前工作空间中的 Codex 线程。 */ +export async function listCodexThreads(emit: AgentEmit, options: { cwd: string; searchTerm?: string; limit?: number }) { + const app = await getCodexApp(emit); + const result = await app.listThreads({ + limit: options.limit || 40, + sortKey: "updated_at", + sortDirection: "desc", + sourceKinds: ["cli", "vscode", "appServer", "exec"], + cwd: options.cwd, + ...(options.searchTerm ? { searchTerm: options.searchTerm } : {}), + }); + const data = Array.isArray(field(result, "data")) ? (field(result, "data") as unknown[]).map(summarizeCodexThread).filter((thread) => threadInWorkspace(thread, options.cwd)) : []; + return { data, nextCursor: field(result, "nextCursor") || null, backwardsCursor: field(result, "backwardsCursor") || null }; +} + +/** 读取指定 Codex 线程及其聊天历史。 */ +export async function readCodexThread(emit: AgentEmit, threadId: string, cwd?: string) { + const thread = await loadCodexThread(emit, threadId, cwd, true); + return { thread: summarizeCodexThread(thread), messages: threadMessages(thread) }; +} + +/** 确认指定 Codex 线程属于当前工作空间。 */ +export async function verifyCodexThreadWorkspace(emit: AgentEmit, threadId: string, cwd: string) { + await loadCodexThread(emit, threadId, cwd, false); +} + +/** 归档指定 Codex 线程。 */ +export async function archiveCodexThread(emit: AgentEmit, threadId: string, cwd?: string) { + const app = await getCodexApp(emit); + await loadCodexThread(emit, threadId, cwd, false); + await app.archiveThread(threadId); +} + +/** 判断线程异常是否允许自动新建线程后重试。 */ +export function isRecoverableThreadError(error: unknown) { + return /thread not loaded|no rollout found/i.test(errorMessage(error)); +} + +/** 执行一次 Codex turn,并负责附件临时文件和线程恢复。 */ +async function runCodexTurnNow(prompt: string, emit: AgentEmit, attachments: AgentAttachment[], options: CodexRunOptions) { + let files: string[] = []; + try { + options.onStart?.(); + files = await writeAttachmentFiles(attachments); + const app = await getCodexApp(options.appEmit || emit); + let threadId = await ensureCodexThread(app, options, emit); + options.onThread?.(threadId); + try { + await app.startTurn(threadId, prompt, files, options.onTurn); + } catch (error) { + if (!isRecoverableThreadError(error)) throw error; + emit("agent_log", { text: `Codex thread unavailable, starting a new thread: ${errorMessage(error)}` }); + codexThreadId = ""; + threadId = await ensureCodexThread(app, { cwd: options.cwd }, emit); + options.onThread?.(threadId); + await app.startTurn(threadId, prompt, files, options.onTurn); + } + } catch (error) { + logger.error("Codex turn failed", error); + emit("agent_error", { message: errorMessage(error) }); + } finally { + options.onFinish?.(); + await Promise.all(files.map((file) => fs.unlink(file).catch(() => undefined))); + } +} + +/** 恢复请求线程或创建新的 Codex 线程。 */ +async function ensureCodexThread(app: CodexAppClient, options: CodexRunOptions, emit: AgentEmit) { + if (options.threadId) { + if (options.threadId === codexThreadId) return codexThreadId; + try { + const result = await app.readThread(options.threadId, false); + assertThreadWorkspace(field(result, "thread") || {}, options.cwd); + const thread = await app.resumeThread(options.threadId, options.cwd); + assertThreadWorkspace(thread, options.cwd); + codexThreadId = String(field(thread, "id") || options.threadId); + return codexThreadId; + } catch (error) { + if (!isRecoverableThreadError(error)) throw error; + emit("agent_log", { text: `Codex thread unavailable, starting a new thread: ${errorMessage(error)}` }); + } + } + if (!codexThreadId) { + const thread = await app.startThread(options.cwd); + codexThreadId = String(field(thread, "id") || ""); + } + return codexThreadId; +} + +/** 从 app-server 读取线程并校验工作空间。 */ +async function loadCodexThread(emit: AgentEmit, threadId: string, cwd: string | undefined, includeTurns: boolean) { + const app = await getCodexApp(emit); + const result = await app.readThread(threadId, includeTurns); + const thread = field(result, "thread") || {}; + assertThreadWorkspace(thread, cwd); + return thread; +} + +/** 获取已启动的 Codex app-server 客户端。 */ +async function getCodexApp(emit: AgentEmit) { + if (codexApp) return codexApp; + codexAppStart ||= CodexAppClient.start(emit, () => { + codexApp = null; + codexThreadId = ""; + }); + try { + codexApp = await codexAppStart; + return codexApp; + } finally { + codexAppStart = null; + } +} + +/** 校验线程是否属于指定工作空间。 */ +function assertThreadWorkspace(thread: unknown, cwd?: string) { + if (!cwd || threadInWorkspace(thread, cwd)) return; + throw new Error("该 Codex 会话不属于当前画布工作空间"); +} + +/** 判断线程工作目录是否与当前工作空间一致。 */ +function threadInWorkspace(thread: unknown, cwd: string) { + const threadCwd = String(field(thread, "cwd") || ""); + return Boolean(threadCwd && path.resolve(threadCwd) === path.resolve(cwd)); +} + +/** 将图片附件写入临时文件供 Codex 读取。 */ +async function writeAttachmentFiles(attachments: AgentAttachment[]) { + return await Promise.all(attachments.filter((item) => item.dataUrl?.startsWith("data:image/")).map(writeAttachmentFile)); +} + +/** 将单个 Data URL 图片附件写入临时文件。 */ +async function writeAttachmentFile(item: AgentAttachment) { + const [, meta = "", data = ""] = item.dataUrl?.match(/^data:([^;]+);base64,(.+)$/) || []; + if (!data) throw new Error(`图片附件无效:${item.name || "未命名图片"}`); + const file = path.join(os.tmpdir(), `infinite-canvas-${Date.now()}-${Math.random().toString(16).slice(2)}.${imageExt(meta || item.type)}`); + await fs.writeFile(file, Buffer.from(data, "base64")); + return file; +} + +/** 根据图片 MIME 类型返回临时文件扩展名。 */ +function imageExt(type = "") { + if (type.includes("png")) return "png"; + if (type.includes("webp")) return "webp"; + return "jpg"; +} diff --git a/canvas-agent/src/agent/types.ts b/canvas-agent/src/agent/types.ts new file mode 100644 index 0000000..4febde1 --- /dev/null +++ b/canvas-agent/src/agent/types.ts @@ -0,0 +1,5 @@ +/** Agent 向网页广播事件的函数类型。 */ +export type AgentEmit = (type: string, payload: unknown) => void; + +/** 用户随当前 Agent 消息上传的附件。 */ +export type AgentAttachment = { id?: string; name?: string; type?: string; size?: number; width?: number; height?: number; dataUrl?: string }; diff --git a/canvas-agent/src/agents.ts b/canvas-agent/src/agents.ts deleted file mode 100644 index ee3380d..0000000 --- a/canvas-agent/src/agents.ts +++ /dev/null @@ -1,576 +0,0 @@ -import { spawn, type ChildProcess, type StdioOptions } from "node:child_process"; -import fs from "node:fs/promises"; -import os from "node:os"; -import path from "node:path"; -import { createRequire } from "node:module"; -import { fileURLToPath } from "node:url"; - -import { AGENT_PROMPT, VERSION } from "./config.js"; -import { logger } from "./utils/logger.js"; -import type { AgentAttachment, AgentEmit } from "./types.js"; - -type Json = Record; -type AgentEvent = Json & { type: string; usage?: unknown }; -type PendingRequest = { resolve: (value: unknown) => void; reject: (error: Error) => void }; -type CodexRunOptions = { threadId?: string; cwd?: string; appEmit?: AgentEmit; onStart?: () => void; onThread?: (threadId: string) => void; onTurn?: (turnId: string) => void; onFinish?: () => void }; -type AgentHistoryMessage = { id: string; role: "user" | "assistant" | "tool" | "error"; title?: string; text: string; detail?: unknown; streamId?: string }; - -let codexQueue: Promise = Promise.resolve(); -let codexApp: CodexAppClient | null = null; -let codexAppStart: Promise | null = null; -let codexThreadId = ""; -const canvasAgentMcp = canvasAgentMcpCommand(); -const require = createRequire(import.meta.url); - -export function withAgentPrompt(prompt: string) { - return prompt.trim() ? `${AGENT_PROMPT}\n\n用户请求:${prompt}` : ""; -} - -export async function runCodexTurn(prompt: string, emit: AgentEmit, attachments: AgentAttachment[] = [], options: CodexRunOptions = {}) { - if (!prompt.trim()) return; - codexQueue = codexQueue.catch(() => undefined).then(() => runCodexTurnNow(prompt, emit, attachments, options)); - await codexQueue; -} - -export function interruptCodexTurn(threadId?: string) { - if (!codexApp || (threadId && threadId !== codexThreadId)) return false; - return codexApp.interruptCurrentTurn(); -} - -async function runCodexTurnNow(prompt: string, emit: AgentEmit, attachments: AgentAttachment[], options: CodexRunOptions) { - let files: string[] = []; - try { - options.onStart?.(); - files = await writeAttachmentFiles(attachments); - const app = await getCodexApp(options.appEmit || emit); - let threadId = await ensureCodexThread(app, options, emit); - options.onThread?.(threadId); - try { - await app.startTurn(threadId, prompt, files, options.onTurn); - } catch (error) { - if (!isRecoverableThreadError(error)) throw error; - emit("agent_log", { text: `Codex thread unavailable, starting a new thread: ${errorMessage(error)}` }); - codexThreadId = ""; - threadId = await ensureCodexThread(app, { cwd: options.cwd }, emit); - options.onThread?.(threadId); - await app.startTurn(threadId, prompt, files, options.onTurn); - } - } catch (error) { - logger.error("Codex turn failed", error); - emit("agent_error", { message: errorMessage(error) }); - } finally { - options.onFinish?.(); - await Promise.all(files.map((file) => fs.unlink(file).catch(() => undefined))); - } -} - -export async function startCodexThread(emit: AgentEmit, cwd?: string) { - const app = await getCodexApp(emit); - const thread = await app.startThread(cwd); - codexThreadId = String(field(thread, "id") || ""); - return thread; -} - -export async function resumeCodexThread(emit: AgentEmit, threadId: string, cwd?: string) { - const app = await getCodexApp(emit); - await loadCodexThread(emit, threadId, cwd, false); - const thread = await app.resumeThread(threadId, cwd); - assertThreadWorkspace(thread, cwd); - codexThreadId = String(field(thread, "id") || threadId); - return { thread, messages: threadMessages(thread) }; -} - -export async function listCodexThreads(emit: AgentEmit, options: { cwd: string; searchTerm?: string; limit?: number }) { - const app = await getCodexApp(emit); - const result = await app.listThreads({ - limit: options.limit || 40, - sortKey: "updated_at", - sortDirection: "desc", - sourceKinds: ["cli", "vscode", "appServer", "exec"], - cwd: options.cwd, - ...(options.searchTerm ? { searchTerm: options.searchTerm } : {}), - }); - const data = Array.isArray(field(result, "data")) ? (field(result, "data") as unknown[]).map(summarizeCodexThread).filter((thread) => threadInWorkspace(thread, options.cwd)) : []; - return { data, nextCursor: field(result, "nextCursor") || null, backwardsCursor: field(result, "backwardsCursor") || null }; -} - -export async function readCodexThread(emit: AgentEmit, threadId: string, cwd?: string) { - const thread = await loadCodexThread(emit, threadId, cwd, true); - return { thread: summarizeCodexThread(thread), messages: threadMessages(thread) }; -} - -export async function verifyCodexThreadWorkspace(emit: AgentEmit, threadId: string, cwd: string) { - await loadCodexThread(emit, threadId, cwd, false); -} - -export async function archiveCodexThread(emit: AgentEmit, threadId: string, cwd?: string) { - const app = await getCodexApp(emit); - await loadCodexThread(emit, threadId, cwd, false); - await app.archiveThread(threadId); -} - -export function runClaudeTurn(prompt: string, emit: AgentEmit) { - if (!prompt.trim()) return; - const child = spawnAgent("claude", ["-p", "--output-format", "stream-json", "--verbose", "--include-partial-messages", "--allowedTools", "mcp__infinite-canvas__*", prompt], ["ignore", "pipe", "pipe"], emit); - if (!child) return; - pipeJsonLines(child, emit, "claude"); -} - -async function ensureCodexThread(app: CodexAppClient, options: CodexRunOptions, emit: AgentEmit) { - if (options.threadId) { - if (options.threadId === codexThreadId) return codexThreadId; - try { - const result = await app.readThread(options.threadId, false); - assertThreadWorkspace(field(result, "thread") || {}, options.cwd); - const thread = await app.resumeThread(options.threadId, options.cwd); - assertThreadWorkspace(thread, options.cwd); - codexThreadId = String(field(thread, "id") || options.threadId); - return codexThreadId; - } catch (error) { - if (!isRecoverableThreadError(error)) throw error; - emit("agent_log", { text: `Codex thread unavailable, starting a new thread: ${errorMessage(error)}` }); - } - } - if (!codexThreadId) { - const thread = await app.startThread(options.cwd); - codexThreadId = String(field(thread, "id") || ""); - } - return codexThreadId; -} - -export function isRecoverableThreadError(error: unknown) { - return /thread not loaded|no rollout found/i.test(errorMessage(error)); -} - -class CodexAppClient { - private nextId = 1; - private buffer = ""; - private textByItem = new Map(); - private lastUsage: unknown = null; - private pending = new Map(); - private activeTurns = new Map(); - private completedTurns = new Map(); - - private constructor(private child: ChildProcess, private emit: AgentEmit) {} - - static async start(emit: AgentEmit) { - logger.info("Starting Codex app-server", { executable: process.execPath, codex: codexBin() }); - const child = spawn(process.execPath, [codexBin(), "app-server", "--stdio"], { stdio: ["pipe", "pipe", "pipe"], windowsHide: true }); - const client = new CodexAppClient(child, emit); - child.stdout?.on("data", (chunk) => client.read(chunk.toString())); - child.stderr?.on("data", (chunk) => { - const text = chunk.toString(); - logger.warn("Codex app-server stderr", { text }); - emit("agent_log", { text }); - }); - child.on("error", (error) => { - logger.error("Codex app-server process error", error); - emit("agent_error", { message: error.message }); - }); - child.on("exit", (code) => { - logger.warn("Codex app-server exited", { code }); - client.failAll(`Codex app-server exited: ${code ?? 0}`); - codexApp = null; - codexThreadId = ""; - emit("agent_log", { text: `Codex app-server exited: ${code ?? 0}` }); - }); - await client.request("initialize", { clientInfo: { name: "canvas-agent", title: "Infinite Canvas Agent", version: VERSION }, capabilities: { experimentalApi: true, requestAttestation: false } }); - client.notify("initialized"); - return client; - } - - async startThread(cwd?: string) { - const result = await this.request("thread/start", { approvalPolicy: "never", sandbox: "workspace-write", config: codexConfig(), ...(cwd ? { cwd } : {}), threadSource: "user" }); - const thread = field(result, "thread") as Json | undefined; - const id = String(field(thread, "id") || ""); - if (!id) throw new Error("Codex app-server 没有返回 thread id"); - return thread || {}; - } - - async resumeThread(threadId: string, cwd?: string) { - const result = await this.request("thread/resume", { threadId, approvalPolicy: "never", sandbox: "workspace-write", config: codexConfig(), ...(cwd ? { cwd } : {}) }); - const thread = field(result, "thread") as Json | undefined; - const id = String(field(thread, "id") || ""); - if (!id) throw new Error("Codex app-server 没有返回 thread id"); - return thread || {}; - } - - listThreads(params: Json) { - return this.request("thread/list", params); - } - - readThread(threadId: string, includeTurns = true) { - return this.request("thread/read", { threadId, includeTurns }); - } - - archiveThread(threadId: string) { - return this.request("thread/archive", { threadId }); - } - - async startTurn(threadId: string, prompt: string, images: string[], onTurn?: (turnId: string) => void) { - const result = await this.request("turn/start", { threadId, input: codexInput(prompt, images), approvalPolicy: "never" }); - const turnId = String(field(field(result, "turn"), "id") || ""); - if (!turnId) throw new Error("Codex app-server 没有返回 turn id"); - onTurn?.(turnId); - const completed = this.completedTurns.get(turnId); - if (this.completedTurns.has(turnId)) { - this.completedTurns.delete(turnId); - if (completed) throw completed; - return; - } - await new Promise((resolve, reject) => this.activeTurns.set(turnId, { resolve, reject })); - } - - interruptCurrentTurn() { - if (this.activeTurns.size === 0) return false; - try { - logger.warn("Interrupting active Codex turn", { threadId: codexThreadId, activeTurns: this.activeTurns.size }); - this.child.kill("SIGINT"); - return true; - } catch { - return false; - } - } - - private request(method: string, params: unknown) { - const id = this.nextId++; - this.write({ id, method, params }); - return new Promise((resolve, reject) => this.pending.set(id, { resolve, reject })); - } - - private notify(method: string, params?: unknown) { - this.write(params === undefined ? { method } : { method, params }); - } - - private write(value: unknown) { - const method = String(field(value, "method") || ""); - const params = field(value, "params"); - if (method) logger.debug(`Codex ${method}`, { id: field(value, "id"), threadId: field(params, "threadId") }); - this.child.stdin?.write(`${JSON.stringify(value)}\n`); - } - - private read(chunk: string) { - this.buffer += chunk; - const lines = this.buffer.split(/\r?\n/); - this.buffer = lines.pop() || ""; - lines.filter(Boolean).forEach((line) => { - try { - this.handle(JSON.parse(line) as Json); - } catch (error) { - logger.warn("Invalid Codex app-server output", { error, line }); - this.emit("agent_log", { text: line }); - } - }); - } - - private handle(message: Json) { - const id = Number(message.id); - if (message.error && this.pending.has(id)) { - const error = String(field(message.error, "message") || "Codex request failed"); - logger.warn("Codex request failed", { id, error }); - return this.reject(id, error); - } - if (this.pending.has(id)) return this.resolve(id, message.result); - if (typeof message.method === "string" && "id" in message) return this.answerServerRequest(message); - if (typeof message.method === "string") this.handleNotification(message.method, (message.params || {}) as Json); - } - - private handleNotification(method: string, params: Json) { - if (method === "item/agentMessage/delta") return this.emitDelta(params); - if (method === "thread/tokenUsage/updated") { - this.lastUsage = normalizeUsage(params); - this.emit("agent_event", { agent: "codex", type: "usage.updated", usage: this.lastUsage, ...codexEventScope(params) }); - return; - } - const event = normalizeCodexNotification(method, params); - if (!event) return; - if (event.type === "item.completed") { - const item = field(event, "item") as Json | undefined; - const id = String(field(item, "id") || ""); - const streamedText = this.textByItem.get(id); - if (item?.type === "agent_message" && streamedText && !item.text) item.text = streamedText; - if (id) this.textByItem.delete(id); - } - if (event.type === "turn.completed") event.usage = this.lastUsage; - this.emit("agent_event", { agent: "codex", ...event }); - if (event.type === "turn.completed") { - const turnId = String(field(params, "turnId") || field(field(params, "turn"), "id") || ""); - const pending = this.activeTurns.get(turnId); - const error = field(field(params, "turn"), "error"); - if (pending) { - this.activeTurns.delete(turnId); - error ? pending.reject(new Error(String(field(error, "message") || "Codex turn failed"))) : pending.resolve(event); - } else if (turnId) { - this.completedTurns.set(turnId, error ? new Error(String(field(error, "message") || "Codex turn failed")) : null); - } - this.emit("agent_done", { agent: "codex", usage: event.usage, ...codexEventScope(params) }); - } - } - - private emitDelta(params: Json) { - const id = String(field(params, "itemId") || ""); - const text = `${this.textByItem.get(id) || ""}${String(field(params, "delta") || "")}`; - this.textByItem.set(id, text); - this.emit("agent_event", { agent: "codex", type: "item.updated", item: { id, type: "agent_message", text }, ...codexEventScope(params) }); - } - - private answerServerRequest(message: Json) { - const method = String(message.method); - const result = method === "mcpServer/elicitation/request" ? { action: "accept", content: {}, _meta: null } : { decision: "decline" }; - this.write({ id: message.id, result }); - this.emit("agent_event", { agent: "codex", type: "server.request", method, params: message.params, result }); - } - - private resolve(id: number, result: unknown) { - const pending = this.pending.get(id); - if (pending) (this.pending.delete(id), pending.resolve(result)); - } - - private reject(id: number, message: string) { - const pending = this.pending.get(id); - if (pending) (this.pending.delete(id), pending.reject(new Error(message))); - } - - failAll(message: string) { - [...this.pending.values(), ...this.activeTurns.values()].forEach((item) => item.reject(new Error(message))); - this.pending.clear(); - this.activeTurns.clear(); - } -} - -function canvasAgentMcpCommand() { - const current = process.argv.find((arg) => /index\.(t|j)s$/.test(arg)) || ""; - const entry = path.resolve(current || fileURLToPath(new URL("./index.js", import.meta.url))); - const tsx = path.join(path.dirname(entry), "..", "node_modules", "tsx", "dist", "cli.mjs"); - return entry.endsWith(".ts") ? { command: process.execPath, args: [tsx, entry, "mcp"] } : { command: process.execPath, args: [entry, "mcp"] }; -} - -function codexConfig() { - return { mcp_servers: { "infinite-canvas": { command: canvasAgentMcp.command, args: canvasAgentMcp.args, default_tools_approval_mode: "approve", startup_timeout_sec: 20, tool_timeout_sec: 90 } } }; -} - -function codexInput(prompt: string, images: string[]) { - return [{ type: "text", text: prompt, text_elements: [] }, ...images.map((file) => ({ type: "localImage", path: file }))]; -} - -function normalizeCodexNotification(method: string, params: Json): AgentEvent | null { - const scope = codexEventScope(params); - if (method === "thread/started") return { type: "thread.started", ...scope }; - if (method === "turn/started") return { type: "turn.started", ...scope }; - if (method === "turn/completed") return { type: "turn.completed", usage: null, duration_ms: field(field(params, "turn"), "durationMs"), ...scope }; - if (method === "item/started") return { type: "item.started", item: normalizeItem(field(params, "item")), ...scope }; - if (method === "item/completed") return { type: "item.completed", item: normalizeItem(field(params, "item")), ...scope }; - if (method === "error") return { type: "error", message: field(params, "message"), ...scope }; - return null; -} - -function codexEventScope(params: Json) { - const threadId = String(field(params, "threadId") || field(field(params, "thread"), "id") || ""); - const turnId = String(field(params, "turnId") || field(field(params, "turn"), "id") || ""); - return { ...(threadId ? { thread_id: threadId } : {}), ...(turnId ? { turn_id: turnId } : {}) }; -} - -async function loadCodexThread(emit: AgentEmit, threadId: string, cwd: string | undefined, includeTurns: boolean) { - const app = await getCodexApp(emit); - const result = await app.readThread(threadId, includeTurns); - const thread = field(result, "thread") || {}; - assertThreadWorkspace(thread, cwd); - return thread; -} - -async function getCodexApp(emit: AgentEmit) { - if (codexApp) return codexApp; - codexAppStart ||= CodexAppClient.start(emit); - try { - codexApp = await codexAppStart; - return codexApp; - } finally { - codexAppStart = null; - } -} - -function assertThreadWorkspace(thread: unknown, cwd?: string) { - if (!cwd || threadInWorkspace(thread, cwd)) return; - throw new Error("该 Codex 会话不属于当前画布工作空间"); -} - -function threadInWorkspace(thread: unknown, cwd: string) { - const threadCwd = String(field(thread, "cwd") || ""); - return Boolean(threadCwd && path.resolve(threadCwd) === path.resolve(cwd)); -} - -function normalizeItem(item: unknown) { - const value = item && typeof item === "object" ? { ...(item as Json) } : {}; - if (value.type === "agentMessage") value.type = "agent_message"; - if (value.type === "mcpToolCall") value.type = "mcp_tool_call"; - if (value.type === "agent_message" && typeof value.id === "string") value.text = String(value.text || ""); - if ("arguments" in value) value.arguments = parseMaybeJson(value.arguments); - return value; -} - -function normalizeUsage(params: Json) { - const last = field(field(params, "tokenUsage"), "last") as Json | undefined; - return { - input_tokens: field(last, "inputTokens"), - cached_input_tokens: field(last, "cachedInputTokens"), - output_tokens: field(last, "outputTokens"), - reasoning_output_tokens: field(last, "reasoningOutputTokens"), - }; -} - -function parseMaybeJson(value: unknown) { - if (typeof value !== "string") return value; - try { - return JSON.parse(value); - } catch { - return value; - } -} - -function field(value: unknown, key: string) { - return value && typeof value === "object" ? (value as Json)[key] : undefined; -} - -export function summarizeCodexThread(thread: unknown) { - return { - id: String(field(thread, "id") || ""), - sessionId: String(field(thread, "sessionId") || ""), - preview: displayUserText(String(field(thread, "preview") || "")), - name: stringOrNull(field(thread, "name")), - cwd: String(field(thread, "cwd") || ""), - status: String(field(thread, "status") || ""), - source: field(thread, "source"), - threadSource: field(thread, "threadSource"), - createdAt: Number(field(thread, "createdAt") || 0), - updatedAt: Number(field(thread, "updatedAt") || 0), - }; -} - -function threadMessages(thread: unknown): AgentHistoryMessage[] { - const turns = arrayValue(field(thread, "turns")); - const messages: AgentHistoryMessage[] = []; - turns.forEach((turn, turnIndex) => { - arrayValue(field(turn, "items")).forEach((item, itemIndex) => { - const type = String(field(item, "type") || ""); - const id = String(field(item, "id") || `${turnIndex}-${itemIndex}`); - if (type === "userMessage") { - const text = displayUserText(userInputText(field(item, "content"))); - if (text) messages.push({ id, role: "user", text }); - } - if (type === "agentMessage") { - const text = String(field(item, "text") || "").trim(); - if (text) messages.push({ id, role: "assistant", title: "Codex", text }); - } - if (type === "mcpToolCall") { - const tool = String(field(item, "tool") || "工具调用"); - const error = field(field(item, "error"), "message"); - messages.push({ id, role: error ? "error" : "tool", title: toolName(tool), text: error ? String(error) : `${toolName(tool)} ${String(field(item, "status") || "完成")}`, detail: item }); - } - if (type === "commandExecution") { - const command = String(field(item, "command") || "").trim(); - if (command) messages.push({ id, role: "tool", title: "命令", text: command, detail: { cwd: field(item, "cwd"), status: field(item, "status"), exitCode: field(item, "exitCode") } }); - } - if (type === "fileChange") messages.push({ id, role: "tool", title: "文件变更", text: "Codex 修改了文件", detail: item }); - }); - }); - return messages.filter((item) => item.text).slice(-120); -} - -function userInputText(content: unknown) { - return arrayValue(content) - .map((item) => { - const type = String(field(item, "type") || ""); - if (type === "text") return String(field(item, "text") || ""); - if (type === "image" || type === "localImage") return "图片附件"; - if (type === "mention") return `@${String(field(item, "name") || "文件")}`; - return ""; - }) - .filter(Boolean) - .join("\n"); -} - -function displayUserText(text: string) { - const value = text.trim(); - const marker = "用户请求:"; - const index = value.lastIndexOf(marker); - return (index >= 0 ? value.slice(index + marker.length) : value).trim(); -} - -function arrayValue(value: unknown) { - return Array.isArray(value) ? value : []; -} - -function stringOrNull(value: unknown) { - return typeof value === "string" && value.trim() ? value : null; -} - -function toolName(name: string) { - if (name === "canvas_apply_ops") return "画布操作"; - if (name === "canvas_get_state") return "读取画布"; - if (name === "canvas_get_selection") return "读取选区"; - if (name === "canvas_export_snapshot") return "导出快照"; - if (name === "canvas_create_attachment_nodes") return "添加附件图片"; - if (name === "canvas_create_text_node") return "创建文本"; - if (name === "canvas_create_image_prompt_flow") return "创建生图流程"; - if (name === "canvas_create_generation_flow") return "创建生成流程"; - if (name === "canvas_generate_text") return "生成文本"; - if (name === "canvas_generate_image") return "生成图片"; - if (name === "canvas_generate_video") return "生成视频"; - if (name === "canvas_generate_audio") return "生成音频"; - if (name === "canvas_run_generation") return "触发生成"; - return name; -} - -async function writeAttachmentFiles(attachments: AgentAttachment[]) { - return await Promise.all(attachments.filter((item) => item.dataUrl?.startsWith("data:image/")).map(writeAttachmentFile)); -} - -async function writeAttachmentFile(item: AgentAttachment) { - const [, meta = "", data = ""] = item.dataUrl?.match(/^data:([^;]+);base64,(.+)$/) || []; - if (!data) throw new Error(`图片附件无效:${item.name || "未命名图片"}`); - const file = path.join(os.tmpdir(), `infinite-canvas-${Date.now()}-${Math.random().toString(16).slice(2)}.${imageExt(meta || item.type)}`); - await fs.writeFile(file, Buffer.from(data, "base64")); - return file; -} - -function imageExt(type = "") { - if (type.includes("png")) return "png"; - if (type.includes("webp")) return "webp"; - return "jpg"; -} - -function codexBin() { - return path.join(path.dirname(require.resolve("@openai/codex/package.json")), "bin", "codex.js"); -} - -function pipeJsonLines(child: ReturnType, emit: AgentEmit, agent: string) { - let out = ""; - child.stdout?.on("data", (chunk) => { - out += chunk.toString(); - const lines = out.split(/\r?\n/); - out = lines.pop() || ""; - lines.filter(Boolean).forEach((line) => { - try { - emit("agent_event", { agent, ...JSON.parse(line) }); - } catch { - emit("agent_event", { agent, type: "raw", text: line }); - } - }); - }); - child.stderr?.on("data", (chunk) => emit("agent_log", { text: chunk.toString() })); - child.on("error", (error) => emit("agent_error", { message: error.message })); - child.on("close", (code) => emit("agent_done", { agent, code })); -} - -function spawnAgent(name: string, args: string[], stdio: StdioOptions, emit: AgentEmit) { - try { - return spawn(name, args, { stdio, shell: process.platform === "win32", windowsHide: true }); - } catch (error) { - emit("agent_error", { message: errorMessage(error) }); - return null; - } -} - -function errorMessage(error: unknown) { - return error instanceof Error ? error.message : String(error); -} diff --git a/canvas-agent/src/canvas/operations.ts b/canvas-agent/src/canvas/operations.ts new file mode 100644 index 0000000..10a1aab --- /dev/null +++ b/canvas-agent/src/canvas/operations.ts @@ -0,0 +1,168 @@ +import crypto from "node:crypto"; + +import type { ToolName } from "./schemas.js"; +import { nextCanvasX } from "./tools.js"; +import type { CanvasNode, CanvasNodeType, CanvasSnapshot } from "./types.js"; + +export type CanvasToolRequest = { name: "canvas_apply_ops"; input: Record }; + +/** 将上层画布工具调用转换为前端可执行的批量操作。 */ +export function buildCanvasToolRequest(name: ToolName, input: Record, state: CanvasSnapshot | null): CanvasToolRequest { + if (name === "canvas_apply_ops") return { name, input }; + if (name === "canvas_create_node") { + const data = input as { nodeType: CanvasNodeType; title?: string; x?: number; y?: number; width?: number; height?: number; metadata?: Record }; + return applyOps([{ type: "add_node", nodeType: data.nodeType, title: data.title, position: { x: data.x ?? nextCanvasX(state), y: data.y ?? 0 }, width: data.width, height: data.height, metadata: data.metadata }]); + } + if (name === "canvas_create_text_node") { + const data = input as { text?: string; x?: number; y?: number; title?: string; width?: number; height?: number }; + return applyOps([textNodeOp(data, data.x ?? nextCanvasX(state), data.y ?? 0)]); + } + if (name === "canvas_create_text_nodes") { + const data = input as { items: Array<{ text: string; title?: string; x?: number; y?: number; width?: number; height?: number }>; x?: number; y?: number; gap?: number; direction?: "row" | "column" }; + const x = Number(data.x ?? nextCanvasX(state)); + const y = Number(data.y ?? 0); + const gap = Number(data.gap ?? 40); + return applyOps(data.items.map((item, index) => textNodeOp(item, item.x ?? (data.direction === "row" ? x + index * (340 + gap) : x), item.y ?? (data.direction === "row" ? y : y + index * (240 + gap))))); + } + if (name === "canvas_create_image_prompt_flow") return applyOps(generationFlowOps({ ...input, mode: "image" }, state)); + if (name === "canvas_create_config_node") { + const x = Number(input.x ?? nextCanvasX(state)); + const y = Number(input.y ?? 0); + const configId = `config-${crypto.randomUUID()}`; + const mode = generationMode(input.mode); + const prompt = String(input.prompt || ""); + return applyOps([configNodeOp(configId, input, x, y), ...(input.autoRun ? [runGenerationOp(configId, mode, prompt)] : [])]); + } + if (name === "canvas_create_generation_flow") return applyOps(generationFlowOps(input, state)); + if (name === "canvas_generate_text" || name === "canvas_generate_image" || name === "canvas_generate_video" || name === "canvas_generate_audio") { + return applyOps(generationFlowOps({ ...input, mode: name.replace("canvas_generate_", ""), autoRun: true }, state)); + } + if (name === "canvas_update_node") { + const data = input as { id: string; patch?: Record; metadata?: Record }; + return applyOps([{ type: "update_node", id: data.id, patch: data.patch, metadata: data.metadata }]); + } + if (name === "canvas_update_node_text") { + const data = input as { id: string; text: string; title?: string }; + return applyOps([{ type: "update_node", id: data.id, patch: { ...(data.title ? { title: data.title } : {}) }, metadata: { content: data.text, status: "success" } }]); + } + if (name === "canvas_move_nodes") { + const data = input as { items: Array<{ id: string; x?: number; y?: number; dx?: number; dy?: number }> }; + return applyOps(data.items.map((item) => { + const current = findNode(state, item.id); + return { type: "update_node", id: item.id, patch: { position: { x: item.x ?? ((current?.position.x || 0) + (item.dx || 0)), y: item.y ?? ((current?.position.y || 0) + (item.dy || 0)) } } }; + })); + } + if (name === "canvas_resize_node") { + const data = input as { id: string; width: number; height: number; freeResize?: boolean }; + return applyOps([{ type: "update_node", id: data.id, patch: { width: data.width, height: data.height }, metadata: data.freeResize === undefined ? undefined : { freeResize: data.freeResize } }]); + } + if (name === "canvas_delete_nodes") return applyOps([{ type: "delete_node", ids: (input as { ids: string[] }).ids }]); + if (name === "canvas_connect_nodes") { + const data = input as { connections: Array<{ fromNodeId: string; toNodeId: string }> }; + return applyOps(data.connections.map((connection) => ({ type: "connect_nodes", ...connection }))); + } + if (name === "canvas_select_nodes") return applyOps([{ type: "select_nodes", ids: (input as { ids: string[] }).ids }]); + if (name === "canvas_set_viewport") return applyOps([{ type: "set_viewport", viewport: (input as { viewport: unknown }).viewport }]); + if (name === "canvas_run_generation") { + const data = input as { nodeId: string; mode?: string; prompt?: string }; + return applyOps([runGenerationOp(data.nodeId, generationMode(data.mode), data.prompt)]); + } + throw new Error(`未知工具:${name}`); +} + +/** 按最大边限制计算附件图片节点尺寸,并保持原始比例。 */ +export function fitAttachmentNodeSize(width: number, height: number) { + const scale = Math.min(1, 640 / width, 640 / height); + return { width: width * scale, height: height * scale }; +} + +/** 创建统一的批量画布操作请求。 */ +function applyOps(ops: unknown[]): CanvasToolRequest { + return { name: "canvas_apply_ops", input: { ops } }; +} + +/** 创建文本节点操作。 */ +function textNodeOp(input: { id?: string; text?: string; title?: string; width?: number; height?: number }, x: number, y: number) { + return { type: "add_node", id: input.id, nodeType: "text", title: input.title, position: { x, y }, width: input.width, height: input.height, metadata: { content: input.text || "", status: "success", fontSize: 14 } }; +} + +/** 创建生成配置节点操作。 */ +function configNodeOp(id: string, input: Record, x: number, y: number) { + const mode = generationMode(input.mode); + const prompt = String(input.prompt || ""); + return { + type: "add_node", + id, + nodeType: "config", + title: String(input.title || generationTitle(mode)), + position: { x, y }, + width: typeof input.width === "number" ? input.width : undefined, + height: typeof input.height === "number" ? input.height : undefined, + metadata: cleanRecord({ + generationMode: mode, + composerContent: prompt, + prompt, + status: "idle", + model: input.model, + size: input.size, + quality: input.quality, + count: input.count, + seconds: input.seconds, + vquality: input.vquality, + generateAudio: input.generateAudio, + watermark: input.watermark, + audioVoice: input.audioVoice, + audioFormat: input.audioFormat, + audioSpeed: input.audioSpeed, + audioInstructions: input.audioInstructions, + }), + }; +} + +/** 创建包含提示词、配置节点和引用连线的生成流程。 */ +function generationFlowOps(input: Record, state: CanvasSnapshot | null) { + const mode = generationMode(input.mode); + const prompt = String(input.prompt || ""); + const x = Number(input.x ?? nextCanvasX(state)); + const y = Number(input.y ?? 0); + const textId = `text-${crypto.randomUUID()}`; + const configId = `config-${crypto.randomUUID()}`; + const referenceNodeIds = Array.isArray(input.referenceNodeIds) ? input.referenceNodeIds.filter((id): id is string => typeof id === "string") : []; + const tokens = [`@[node:${textId}]`, ...referenceNodeIds.map((id) => `@[node:${id}]`)]; + return [ + textNodeOp({ id: textId, text: prompt, title: String(input.title || "提示词") }, x, y), + configNodeOp(configId, { ...input, prompt: tokens.join("\n") }, x + 420, y), + { type: "connect_nodes", fromNodeId: textId, toNodeId: configId }, + ...referenceNodeIds.map((fromNodeId) => ({ type: "connect_nodes", fromNodeId, toNodeId: configId })), + { type: "select_nodes", ids: [configId] }, + ...(input.autoRun ? [runGenerationOp(configId, mode, tokens.join("\n"))] : []), + ]; +} + +/** 创建触发节点生成的画布操作。 */ +function runGenerationOp(nodeId: string, mode: "text" | "image" | "video" | "audio", prompt?: string) { + return { type: "run_generation", nodeId, mode, prompt }; +} + +/** 将未知生成模式归一为画布支持的模式。 */ +function generationMode(value: unknown): "text" | "image" | "video" | "audio" { + return value === "text" || value === "video" || value === "audio" ? value : "image"; +} + +/** 获取生成模式对应的默认节点标题。 */ +function generationTitle(mode: "text" | "image" | "video" | "audio") { + if (mode === "text") return "文本生成"; + if (mode === "video") return "视频生成"; + if (mode === "audio") return "音频生成"; + return "图片生成"; +} + +/** 按节点 ID 查找当前画布节点。 */ +function findNode(state: CanvasSnapshot | null, id: string): CanvasNode | undefined { + return (state?.nodes || []).find((node) => node.id === id); +} + +/** 移除对象中未设置的生成参数。 */ +function cleanRecord(value: Record) { + return Object.fromEntries(Object.entries(value).filter(([, item]) => item !== undefined && item !== "")); +} diff --git a/canvas-agent/src/schemas.ts b/canvas-agent/src/canvas/schemas.ts similarity index 99% rename from canvas-agent/src/schemas.ts rename to canvas-agent/src/canvas/schemas.ts index b155598..6267968 100644 --- a/canvas-agent/src/schemas.ts +++ b/canvas-agent/src/canvas/schemas.ts @@ -6,6 +6,7 @@ const viewportSchema = z.object({ x: z.number(), y: z.number(), k: z.number() }) const nodeTypeSchema = z.enum(["image", "text", "config", "video", "audio"]); const generationModeSchema = z.enum(["text", "image", "video", "audio"]); +/** Canvas Agent 对外提供的工具名称。 */ export const toolNames = [ "site_navigate", "canvas_list_projects", diff --git a/canvas-agent/src/canvas-session.test.ts b/canvas-agent/src/canvas/session.test.ts similarity index 96% rename from canvas-agent/src/canvas-session.test.ts rename to canvas-agent/src/canvas/session.test.ts index a6742f1..ff0792c 100644 --- a/canvas-agent/src/canvas-session.test.ts +++ b/canvas-agent/src/canvas/session.test.ts @@ -3,7 +3,7 @@ import type { ServerResponse } from "node:http"; import assert from "node:assert/strict"; import test from "node:test"; -import { CanvasSession } from "./canvas-session.js"; +import { CanvasSession } from "./session.js"; test("MCP 读取当前激活网页的画布", async (t) => { const session = new CanvasSession(); @@ -251,38 +251,46 @@ test("closing the bound client falls back to the active client", async (t) => { assert.deepEqual(await result, { ok: true }); }); +/** 创建用于测试的画布 SSE 连接。 */ function connect(session: CanvasSession, clientId: string) { const response = new FakeSseResponse(); session.openEvents(new URL(`http://127.0.0.1/events?clientId=${clientId}`), response as unknown as ServerResponse); return response; } +/** 创建最小画布快照。 */ function snapshot(projectId: string) { return { projectId, title: projectId, nodes: [], connections: [], selectedNodeIds: [], viewport: { x: 0, y: 0, k: 1 } }; } +/** 安全读取测试对象字段。 */ function field(value: unknown, key: string) { return value && typeof value === "object" ? (value as Record)[key] : undefined; } +/** 模拟 Node SSE 响应并提供事件读取能力。 */ class FakeSseResponse extends EventEmitter { private chunks: string[] = []; + /** 模拟写入响应头。 */ writeHead() { return this; } + /** 保存写入的 SSE 文本块。 */ write(chunk: string) { this.chunks.push(chunk); return true; } + /** 读取指定类型的首个 SSE 事件数据。 */ event(type: string) { const chunk = this.chunks.find((item) => item.startsWith(`event: ${type}\n`)); const data = chunk?.split("\n").find((line) => line.startsWith("data: "))?.slice(6); return data ? (JSON.parse(data) as unknown) : undefined; } + /** 触发连接关闭事件。 */ close() { this.emit("close"); } diff --git a/canvas-agent/src/canvas-session.ts b/canvas-agent/src/canvas/session.ts similarity index 52% rename from canvas-agent/src/canvas-session.ts rename to canvas-agent/src/canvas/session.ts index f537d62..aecbf39 100644 --- a/canvas-agent/src/canvas-session.ts +++ b/canvas-agent/src/canvas/session.ts @@ -1,10 +1,12 @@ import crypto from "node:crypto"; import type { ServerResponse } from "node:http"; -import { logger } from "./utils/logger.js"; -import { type ToolName } from "./schemas.js"; +import type { AgentAttachment } from "../agent/types.js"; +import { logger } from "../utils/logger.js"; +import { buildCanvasToolRequest, fitAttachmentNodeSize } from "./operations.js"; +import type { ToolName } from "./schemas.js"; import { compactCanvasState, compactNode, isToolName, nextCanvasX, parseToolInput } from "./tools.js"; -import type { AgentAttachment, CanvasNode, CanvasNodeType, CanvasSnapshot } from "./types.js"; +import type { CanvasSnapshot } from "./types.js"; type PendingRequest = { clientId: string; resolve: (value: unknown) => void; reject: (error: Error) => void }; type TurnAttachment = { clientId: string; id: string; name: string; type: string; size: number; width: number; height: number; dataUrl: string }; @@ -23,6 +25,7 @@ const SITE_TOOLS = new Set([ "generation_get_status", ]); +/** 管理网页画布连接、状态、附件和工具请求。 */ export class CanvasSession { private clients = new Map(); private clientFocusOrder = new Map(); @@ -34,28 +37,34 @@ export class CanvasSession { private focusSequence = 0; private codexState: CodexState = { busy: false, threadId: "", turnId: "" }; + /** 获取当前目标网页的画布状态。 */ private get canvasState() { return this.canvasStates.get(this.targetClientId) || null; } + /** 获取当前 turn 绑定或最近激活的网页客户端。 */ private get targetClientId() { return this.boundClientId || this.activeClientId; } + /** 返回 Canvas Agent 当前连接状态。 */ health() { return { ok: true, hasCanvas: Boolean(this.canvasState), clients: this.clients.size, codexBusy: this.codexState.busy }; } + /** 返回 Codex 是否正在执行任务。 */ get codexBusy() { return this.codexState.busy; } + /** 更新并广播 Codex 运行状态。 */ setCodexState(patch: Partial) { this.codexState = { ...this.codexState, ...patch }; logger.debug("Codex state changed", this.codexState); this.emitAll("codex_state", this.codexState); } + /** 建立网页与 Canvas Agent 之间的 SSE 连接。 */ openEvents(url: URL, res: ServerResponse) { const clientId = url.searchParams.get("clientId") || crypto.randomUUID(); const statusOnly = url.searchParams.get("role") === "status"; @@ -88,6 +97,7 @@ export class CanvasSession { }); } + /** 保存指定网页上报的最新画布快照。 */ updateState(body: unknown, clientId?: string) { const targetClientId = clientId || this.activeClientId; if (!targetClientId) return; @@ -95,6 +105,7 @@ export class CanvasSession { logger.debug("Canvas state updated", { clientId: targetClientId, nodes: Array.isArray((body as CanvasSnapshot | null)?.nodes) ? (body as CanvasSnapshot).nodes.length : 0, connections: Array.isArray((body as CanvasSnapshot | null)?.connections) ? (body as CanvasSnapshot).connections.length : 0 }); } + /** 将指定网页设为最近激活的工具目标。 */ activateClient(clientId: string) { if (!this.clients.has(clientId)) throw new Error("当前网页未连接"); this.activeClientId = clientId; @@ -102,17 +113,20 @@ export class CanvasSession { logger.debug("Canvas client activated", { clientId }); } + /** 将当前 Agent turn 固定绑定到指定网页。 */ bindClient(clientId: string) { if (!this.clients.has(clientId)) throw new Error("当前网页未连接"); this.boundClientId = clientId; logger.debug("Canvas client bound to turn", { clientId }); } + /** 解除当前 Agent turn 的网页绑定。 */ releaseClient(clientId: string) { if (this.boundClientId === clientId) this.boundClientId = ""; logger.debug("Canvas client released from turn", { clientId }); } + /** 保存当前 turn 可用的图片附件并返回安全引用。 */ setTurnAttachments(clientId: string, attachments: AgentAttachment[]) { this.turnAttachments.clear(); return attachments.flatMap((item, index) => { @@ -133,12 +147,14 @@ export class CanvasSession { }); } + /** 清理指定网页或全部 turn 附件。 */ clearTurnAttachments(clientId?: string) { this.turnAttachments.forEach((item, id) => { if (!clientId || item.clientId === clientId) this.turnAttachments.delete(id); }); } + /** 获取属于指定网页 turn 的图片附件。 */ getTurnAttachment(clientId: string, attachmentId: string) { const attachment = this.turnAttachments.get(attachmentId); if (!attachment) throw new Error(`找不到本轮图片附件:${attachmentId}`); @@ -146,6 +162,7 @@ export class CanvasSession { return attachment; } + /** 接收网页返回的工具调用结果。 */ resolveResult(clientId: string, body: { requestId?: string; error?: string; result?: unknown }) { const item = body.requestId ? this.pending.get(body.requestId) : null; if (!item || !body.requestId || item.clientId !== clientId) return false; @@ -155,125 +172,39 @@ export class CanvasSession { return true; } + /** 向全部已连接网页广播事件。 */ emitAll(type: string, payload: unknown) { this.clients.forEach((client) => sendEvent(client, type, payload)); } + /** 向全部网页广播带线程归属的事件。 */ emitThread(type: string, threadId: string, payload: Record = {}) { this.emitAll(type, { ...payload, threadId }); } + /** 校验工具参数并将调用分派到当前目标网页。 */ async callTool(name: unknown, rawInput: unknown) { if (!isToolName(name)) throw new Error(`未知工具:${String(name)}`); logger.info("MCP tool called", { name, input: rawInput, targetClientId: this.targetClientId }); - let tool: ToolName = name; - let input = parseToolInput(tool, rawInput) as Record; - if (SITE_TOOLS.has(tool)) { + const input = parseToolInput(name, rawInput) as Record; + if (SITE_TOOLS.has(name)) { if (!this.clients.size) throw new Error("当前没有已连接网页"); - return await this.requestCanvasTool(tool, input); + return await this.requestCanvasTool(name, input); } - const readTool = ["canvas_get_state", "canvas_get_selection", "canvas_export_snapshot"].includes(tool); + const readTool = ["canvas_get_state", "canvas_get_selection", "canvas_export_snapshot"].includes(name); if (readTool && (!this.clients.size || !this.canvasState)) throw new Error("当前没有已连接画布"); - if (tool === "canvas_get_state" || tool === "canvas_export_snapshot") return compactCanvasState(this.canvasState); - if (tool === "canvas_get_selection") { + if (name === "canvas_get_state" || name === "canvas_export_snapshot") return compactCanvasState(this.canvasState); + if (name === "canvas_get_selection") { const ids = new Set(this.canvasState?.selectedNodeIds || []); return { nodes: (this.canvasState?.nodes || []).filter((node) => ids.has(node.id)).map(compactNode) }; } - if (tool === "canvas_create_attachment_nodes") return await this.createAttachmentNodes(input as { attachmentIds: string[]; x?: number; y?: number; gap?: number; direction?: "row" | "column" }); - if (tool === "canvas_create_node") { - const data = input as { nodeType: CanvasNodeType; title?: string; x?: number; y?: number; width?: number; height?: number; metadata?: Record }; - input = { ops: [{ type: "add_node", nodeType: data.nodeType, title: data.title, position: { x: data.x ?? nextCanvasX(this.canvasState), y: data.y ?? 0 }, width: data.width, height: data.height, metadata: data.metadata }] }; - tool = "canvas_apply_ops"; - } - if (tool === "canvas_create_text_node") { - const text = input as { text?: string; x?: number; y?: number; title?: string; width?: number; height?: number }; - input = { ops: [textNodeOp(text, text.x ?? nextCanvasX(this.canvasState), text.y ?? 0)] }; - tool = "canvas_apply_ops"; - } - if (tool === "canvas_create_text_nodes") { - const data = input as { items: Array<{ text: string; title?: string; x?: number; y?: number; width?: number; height?: number }>; x?: number; y?: number; gap?: number; direction?: "row" | "column" }; - const x = Number(data.x ?? nextCanvasX(this.canvasState)); - const y = Number(data.y ?? 0); - const gap = Number(data.gap ?? 40); - input = { - ops: data.items.map((item, index) => textNodeOp(item, item.x ?? (data.direction === "row" ? x + index * (340 + gap) : x), item.y ?? (data.direction === "row" ? y : y + index * (240 + gap)))), - }; - tool = "canvas_apply_ops"; - } - if (tool === "canvas_create_image_prompt_flow") { - input = { ops: generationFlowOps({ ...(input as Record), mode: "image" }, this.canvasState) }; - tool = "canvas_apply_ops"; - } - if (tool === "canvas_create_config_node") { - const data = input as Record; - const x = Number(data.x ?? nextCanvasX(this.canvasState)); - const y = Number(data.y ?? 0); - const configId = `config-${crypto.randomUUID()}`; - const mode = generationMode(data.mode); - const prompt = String(data.prompt || ""); - input = { ops: [configNodeOp(configId, data, x, y), ...(data.autoRun ? [runGenerationOp(configId, mode, prompt)] : [])] }; - tool = "canvas_apply_ops"; - } - if (tool === "canvas_create_generation_flow") { - input = { ops: generationFlowOps(input as Record, this.canvasState) }; - tool = "canvas_apply_ops"; - } - if (tool === "canvas_generate_text" || tool === "canvas_generate_image" || tool === "canvas_generate_video" || tool === "canvas_generate_audio") { - input = { ops: generationFlowOps({ ...(input as Record), mode: tool.replace("canvas_generate_", ""), autoRun: true }, this.canvasState) }; - tool = "canvas_apply_ops"; - } - if (tool === "canvas_update_node") { - const data = input as { id: string; patch?: Record; metadata?: Record }; - input = { ops: [{ type: "update_node", id: data.id, patch: data.patch, metadata: data.metadata }] }; - tool = "canvas_apply_ops"; - } - if (tool === "canvas_update_node_text") { - const data = input as { id: string; text: string; title?: string }; - input = { ops: [{ type: "update_node", id: data.id, patch: { ...(data.title ? { title: data.title } : {}) }, metadata: { content: data.text, status: "success" } }] }; - tool = "canvas_apply_ops"; - } - if (tool === "canvas_move_nodes") { - const data = input as { items: Array<{ id: string; x?: number; y?: number; dx?: number; dy?: number }> }; - input = { - ops: data.items.map((item) => { - const current = findNode(this.canvasState, item.id); - return { type: "update_node", id: item.id, patch: { position: { x: item.x ?? ((current?.position.x || 0) + (item.dx || 0)), y: item.y ?? ((current?.position.y || 0) + (item.dy || 0)) } } }; - }), - }; - tool = "canvas_apply_ops"; - } - if (tool === "canvas_resize_node") { - const data = input as { id: string; width: number; height: number; freeResize?: boolean }; - input = { ops: [{ type: "update_node", id: data.id, patch: { width: data.width, height: data.height }, metadata: data.freeResize === undefined ? undefined : { freeResize: data.freeResize } }] }; - tool = "canvas_apply_ops"; - } - if (tool === "canvas_delete_nodes") { - input = { ops: [{ type: "delete_node", ids: (input as { ids: string[] }).ids }] }; - tool = "canvas_apply_ops"; - } - if (tool === "canvas_connect_nodes") { - const data = input as { connections: Array<{ fromNodeId: string; toNodeId: string }> }; - input = { ops: data.connections.map((connection) => ({ type: "connect_nodes", ...connection })) }; - tool = "canvas_apply_ops"; - } - if (tool === "canvas_select_nodes") { - input = { ops: [{ type: "select_nodes", ids: (input as { ids: string[] }).ids }] }; - tool = "canvas_apply_ops"; - } - if (tool === "canvas_set_viewport") { - input = { ops: [{ type: "set_viewport", viewport: (input as { viewport: unknown }).viewport }] }; - tool = "canvas_apply_ops"; - } - if (tool === "canvas_run_generation") { - const data = input as { nodeId: string; mode?: string; prompt?: string }; - input = { ops: [runGenerationOp(data.nodeId, generationMode(data.mode), data.prompt)] }; - tool = "canvas_apply_ops"; - } - if (tool !== "canvas_apply_ops") throw new Error(`未知工具:${tool}`); + if (name === "canvas_create_attachment_nodes") return await this.createAttachmentNodes(input as { attachmentIds: string[]; x?: number; y?: number; gap?: number; direction?: "row" | "column" }); if (!this.clients.size) throw new Error("当前没有已连接画布"); - return await this.requestCanvasTool(tool, input); + const request = buildCanvasToolRequest(name, input, this.canvasState); + return await this.requestCanvasTool(request.name, request.input); } + /** 将当前 turn 的附件转换为画布图片节点。 */ private async createAttachmentNodes(input: { attachmentIds: string[]; x?: number; y?: number; gap?: number; direction?: "row" | "column" }) { const clientId = this.targetClientId; if (!this.clients.has(clientId)) throw new Error("当前没有已连接画布"); @@ -300,6 +231,7 @@ export class CanvasSession { return { nodes: nodes.map(({ id, attachmentId, title }) => ({ id, attachmentId, title })) }; } + /** 向目标网页发送工具请求并等待调用结果。 */ private async requestCanvasTool(name: ToolName, input: Record) { const requestId = crypto.randomUUID(); const clientId = this.targetClientId; @@ -318,95 +250,12 @@ export class CanvasSession { } } +/** 向 SSE 连接写入一个事件。 */ function sendEvent(res: ServerResponse, type: string, payload: unknown) { res.write(`event: ${type}\ndata: ${JSON.stringify(payload)}\n\n`); } - -function textNodeOp(input: { id?: string; text?: string; title?: string; width?: number; height?: number }, x: number, y: number) { - return { type: "add_node", id: input.id, nodeType: "text", title: input.title, position: { x, y }, width: input.width, height: input.height, metadata: { content: input.text || "", status: "success", fontSize: 14 } }; -} - -function configNodeOp(id: string, input: Record, x: number, y: number) { - const mode = generationMode(input.mode); - const prompt = String(input.prompt || ""); - return { - type: "add_node", - id, - nodeType: "config", - title: String(input.title || generationTitle(mode)), - position: { x, y }, - width: typeof input.width === "number" ? input.width : undefined, - height: typeof input.height === "number" ? input.height : undefined, - metadata: cleanRecord({ - generationMode: mode, - composerContent: prompt, - prompt, - status: "idle", - model: input.model, - size: input.size, - quality: input.quality, - count: input.count, - seconds: input.seconds, - vquality: input.vquality, - generateAudio: input.generateAudio, - watermark: input.watermark, - audioVoice: input.audioVoice, - audioFormat: input.audioFormat, - audioSpeed: input.audioSpeed, - audioInstructions: input.audioInstructions, - }), - }; -} - -function generationFlowOps(input: Record, state: CanvasSnapshot | null) { - const mode = generationMode(input.mode); - const prompt = String(input.prompt || ""); - const x = Number(input.x ?? nextCanvasX(state)); - const y = Number(input.y ?? 0); - const textId = `text-${crypto.randomUUID()}`; - const configId = `config-${crypto.randomUUID()}`; - const referenceNodeIds = Array.isArray(input.referenceNodeIds) ? input.referenceNodeIds.filter((id): id is string => typeof id === "string") : []; - const tokens = [`@[node:${textId}]`, ...referenceNodeIds.map((id) => `@[node:${id}]`)]; - const configInput = { ...input, prompt: tokens.join("\n") }; - return [ - textNodeOp({ id: textId, text: prompt, title: String(input.title || "提示词") }, x, y), - configNodeOp(configId, configInput, x + 420, y), - { type: "connect_nodes", fromNodeId: textId, toNodeId: configId }, - ...referenceNodeIds.map((fromNodeId) => ({ type: "connect_nodes", fromNodeId, toNodeId: configId })), - { type: "select_nodes", ids: [configId] }, - ...(input.autoRun ? [runGenerationOp(configId, mode, tokens.join("\n"))] : []), - ]; -} - -function runGenerationOp(nodeId: string, mode: "text" | "image" | "video" | "audio", prompt?: string) { - return { type: "run_generation", nodeId, mode, prompt }; -} - -function generationMode(value: unknown): "text" | "image" | "video" | "audio" { - return value === "text" || value === "video" || value === "audio" ? value : "image"; -} - -function generationTitle(mode: "text" | "image" | "video" | "audio") { - if (mode === "text") return "文本生成"; - if (mode === "video") return "视频生成"; - if (mode === "audio") return "音频生成"; - return "图片生成"; -} - -function findNode(state: CanvasSnapshot | null, id: string): CanvasNode | undefined { - return (state?.nodes || []).find((node) => node.id === id); -} - -function cleanRecord(value: Record) { - return Object.fromEntries(Object.entries(value).filter(([, item]) => item !== undefined && item !== "")); -} - +/** 将未知数值转换为正数,否则使用默认值。 */ function positiveNumber(value: unknown, fallback: number) { const number = Number(value); return Number.isFinite(number) && number > 0 ? number : fallback; } - -function fitAttachmentNodeSize(width: number, height: number) { - const scale = Math.min(1, 640 / width, 640 / height); - return { width: width * scale, height: height * scale }; -} diff --git a/canvas-agent/src/tools.ts b/canvas-agent/src/canvas/tools.ts similarity index 79% rename from canvas-agent/src/tools.ts rename to canvas-agent/src/canvas/tools.ts index 1f5539d..212cc8e 100644 --- a/canvas-agent/src/tools.ts +++ b/canvas-agent/src/canvas/tools.ts @@ -1,25 +1,30 @@ import { toolInputSchemas, toolNames, type ToolName } from "./schemas.js"; import type { CanvasNode, CanvasSnapshot } from "./types.js"; +/** 判断传入名称是否为已注册的画布工具。 */ export function isToolName(name: unknown): name is ToolName { return typeof name === "string" && toolNames.includes(name as ToolName); } +/** 按工具名称校验并解析调用参数。 */ export function parseToolInput(name: ToolName, input: unknown) { return toolInputSchemas[name].parse(input ?? {}); } +/** 压缩画布快照,避免向 Agent 返回过长的节点内容。 */ export function compactCanvasState(state: CanvasSnapshot | null) { if (!state) throw new Error("当前没有已连接画布"); return { ...state, nodes: (state.nodes || []).map(compactNode) }; } +/** 压缩单个画布节点的元数据内容。 */ export function compactNode(node: CanvasNode) { const metadata = { ...(node.metadata || {}) }; if (typeof metadata.content === "string" && metadata.content.length > 240) metadata.content = `${metadata.content.slice(0, 120)}...`; return { id: node.id, type: node.type, title: node.title, position: node.position, width: node.width, height: node.height, metadata }; } +/** 计算新节点在当前画布右侧的默认横坐标。 */ export function nextCanvasX(state: CanvasSnapshot | null) { const nodes = state?.nodes || []; return nodes.length ? Math.max(...nodes.map((node) => node.position.x + node.width)) + 80 : 0; diff --git a/canvas-agent/src/types.ts b/canvas-agent/src/canvas/types.ts similarity index 75% rename from canvas-agent/src/types.ts rename to canvas-agent/src/canvas/types.ts index 6c6f458..f252152 100644 --- a/canvas-agent/src/types.ts +++ b/canvas-agent/src/canvas/types.ts @@ -1,8 +1,7 @@ +/** 画布坐标。 */ export type Position = { x: number; y: number }; export type Viewport = { x: number; y: number; k: number }; export type CanvasNodeType = "image" | "text" | "config" | "video" | "audio"; export type CanvasNode = { id: string; type: CanvasNodeType; title?: string; position: Position; width: number; height: number; metadata?: Record }; export type CanvasConnection = { id: string; fromNodeId: string; toNodeId: string }; export type CanvasSnapshot = { projectId?: string; title?: string; nodes?: CanvasNode[]; connections?: CanvasConnection[]; selectedNodeIds?: string[]; viewport?: Viewport; clientId?: string }; -export type AgentEmit = (type: string, payload: unknown) => void; -export type AgentAttachment = { id?: string; name?: string; type?: string; size?: number; width?: number; height?: number; dataUrl?: string }; diff --git a/canvas-agent/src/config.ts b/canvas-agent/src/config.ts index 4fcaa87..532b02d 100644 --- a/canvas-agent/src/config.ts +++ b/canvas-agent/src/config.ts @@ -13,6 +13,7 @@ const initializedWorkspaces = new Set(); export type SiteWorkspaceConfig = { workspacePath: string; activeThreadId?: string; pinnedThreadIds?: string[] }; export type CanvasAgentConfig = { url: string; token: string; origins?: string[]; workspace?: SiteWorkspaceConfig }; +/** 读取本地 Canvas Agent 配置,不存在时生成默认配置。 */ export function loadConfig(create = false): CanvasAgentConfig { try { return JSON.parse(fs.readFileSync(CONFIG_FILE, "utf8")) as CanvasAgentConfig; @@ -23,11 +24,13 @@ export function loadConfig(create = false): CanvasAgentConfig { } } +/** 将 Canvas Agent 配置写入用户配置目录。 */ export function saveConfig(config: CanvasAgentConfig) { fs.mkdirSync(CONFIG_DIR, { recursive: true }); fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2)); } +/** 确保站点级 Codex 工作空间存在并已初始化。 */ export function ensureSiteWorkspace(config: CanvasAgentConfig) { const current = config.workspace; if (current?.workspacePath) { @@ -42,6 +45,7 @@ export function ensureSiteWorkspace(config: CanvasAgentConfig) { return { workspacePath }; } +/** 更新站点级 Codex 工作空间配置。 */ export function updateSiteWorkspace(config: CanvasAgentConfig, patch: Partial) { const current = ensureSiteWorkspace(config); const workspacePath = patch.workspacePath ? resolveWorkspacePath(patch.workspacePath) : current.workspacePath; @@ -52,6 +56,7 @@ export function updateSiteWorkspace(config: CanvasAgentConfig, patch: Partial { const data = payload && typeof payload === "object" && !Array.isArray(payload) ? payload as Record : { value: payload }; const threadId = String(data.threadId || data.thread_id || ensureSiteWorkspace(config).activeThreadId || ""); threadId ? session.emitThread(type, threadId, data) : session.emitAll(type, data); }; + /** 保存并广播当前站点工作空间的活跃线程。 */ const setActiveThread = (activeThreadId: string, payload: Record = {}) => { const workspace = updateSiteWorkspace(config, { activeThreadId: activeThreadId || undefined }); session.emitThread("workspace_changed", activeThreadId, { ...payload, activeThreadId }); @@ -138,6 +142,7 @@ export function startHttpServer() { message: { id: String(req.body?.messageId || Date.now()), role: "user", text: String(req.body?.messageText || prompt || `发送了 ${attachments.length} 张图片`) }, }; let chatThreadId = ""; + /** 将当前 turn 事件固定广播到实际线程。 */ const turnEmit = (type: string, payload: unknown) => { const data = payload && typeof payload === "object" && !Array.isArray(payload) ? payload as Record : { value: payload }; session.emitThread(type, threadId, data); @@ -181,7 +186,7 @@ export function startHttpServer() { res.json({ ok }); }); app.post("/agent/claude/turn", (req, res) => { - runClaudeTurn(withAgentPrompt(String(req.body?.prompt || "")), emit); + runClaudeTurn(String(req.body?.prompt || ""), emit); res.json({ ok: true }); }); app.use((_req, res) => res.status(404).json({ ok: false, error: "not found" })); @@ -202,18 +207,22 @@ export function startHttpServer() { }); } +/** 将异步 Express 路由异常交给统一错误处理中间件。 */ function route(handler: (req: Request, res: Response) => Promise) { return (req: Request, res: Response, next: NextFunction) => void handler(req, res).catch(next); } +/** 从 Express 路由参数中读取单个字符串。 */ function routeParam(value: string | string[]) { return Array.isArray(value) ? value[0] || "" : value; } +/** 结合服务配置解析当前请求 URL。 */ function requestUrl(req: Request, config: CanvasAgentConfig) { return new URL(req.originalUrl || req.url || "/", config.url); } +/** 设置跨域响应头并记录通过 token 授权的来源。 */ function setCors(req: Request, res: Response, url: URL, config: CanvasAgentConfig) { const origin = req.headers.origin; res.setHeader("Access-Control-Allow-Origin", origin || "*"); @@ -230,11 +239,13 @@ function setCors(req: Request, res: Response, url: URL, config: CanvasAgentConfi return config.origins.includes(origin); } +/** 校验请求查询参数或请求头中的连接 token。 */ function validToken(req: Request, url: URL, token: string) { const header = req.headers["x-canvas-agent-token"]; return url.searchParams.get("token") === token || header === token || (Array.isArray(header) && header.includes(token)); } +/** 向 Agent 提示词追加本轮图片附件引用说明。 */ function withAttachmentContext(prompt: string, attachments: Array<{ id: string; name: string }>) { if (!attachments.length) return prompt; const list = attachments.map((item, index) => `${index + 1}. attachmentId=${item.id}, name=${JSON.stringify(item.name)}`).join("\n"); diff --git a/canvas-agent/src/mcp-server.ts b/canvas-agent/src/server/mcp.ts similarity index 86% rename from canvas-agent/src/mcp-server.ts rename to canvas-agent/src/server/mcp.ts index 3e7aaf7..3aee16f 100644 --- a/canvas-agent/src/mcp-server.ts +++ b/canvas-agent/src/server/mcp.ts @@ -1,11 +1,12 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; -import { AGENT_PROMPT, loadConfig, type CanvasAgentConfig, VERSION } from "./config.js"; -import { toolDescriptions, toolInputSchemas, toolNames, type ToolName } from "./schemas.js"; +import { toolDescriptions, toolInputSchemas, toolNames, type ToolName } from "../canvas/schemas.js"; +import { AGENT_PROMPT, loadConfig, type CanvasAgentConfig, VERSION } from "../config.js"; type CanvasAgentToolResponse = { ok?: boolean; result?: unknown; error?: string }; +/** 启动通过标准输入输出通信的 MCP 服务。 */ export async function startMcpServer() { const config = loadConfig(true); const server = new McpServer({ name: "canvas-agent", version: VERSION }, { instructions: AGENT_PROMPT }); @@ -13,6 +14,7 @@ export async function startMcpServer() { await server.connect(new StdioServerTransport()); } +/** 向 MCP Server 注册单个 Canvas Agent 工具。 */ function registerCanvasTool(server: McpServer, config: CanvasAgentConfig, name: ToolName) { const schema = toolInputSchemas[name]; server.registerTool(name, { description: toolDescriptions[name], inputSchema: schema.shape }, async (input: unknown) => { @@ -21,6 +23,7 @@ function registerCanvasTool(server: McpServer, config: CanvasAgentConfig, name: }); } +/** 将 MCP 工具调用转发到本地 Canvas Agent HTTP 服务。 */ async function postCanvasAgentTool(config: CanvasAgentConfig, name: ToolName, input: unknown) { const res = await fetch(`${config.url}/api/tools`, { method: "POST", headers: { "content-type": "application/json", "x-canvas-agent-token": config.token }, body: JSON.stringify({ name, input }) }); const body = (await res.json()) as CanvasAgentToolResponse; diff --git a/canvas-agent/src/utils/value.ts b/canvas-agent/src/utils/value.ts new file mode 100644 index 0000000..31ab724 --- /dev/null +++ b/canvas-agent/src/utils/value.ts @@ -0,0 +1,11 @@ +export type JsonRecord = Record; + +/** 安全读取未知对象中的指定字段。 */ +export function field(value: unknown, key: string) { + return value && typeof value === "object" ? (value as JsonRecord)[key] : undefined; +} + +/** 将未知异常转换为可展示的错误信息。 */ +export function errorMessage(error: unknown) { + return error instanceof Error ? error.message : String(error); +}