@coda-console/claude 1.10.2 → 1.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,34 @@
1
1
  # Claude
2
2
 
3
3
  Claude SDK Agent:Coda 基于 Claude Agent SDK 写的 ACP 入口,一个进程一个 SDK 会话;资源页的「Claude」显示会话报告的额度,可从 Coda 登录。
4
+
5
+ ## 认不出的东西
6
+
7
+ Claude Code 会自己长出新东西:SDK 升级带来新的消息类型,服务端让模型返回新的内容块,模型多了新的原生工具。
8
+ 入口不认识时若悄悄丢掉,用户就看不到本该看到的。入口为此有三层防范:
9
+
10
+ 1. **登记**(`agent/registry.mjs`):锁定的 SDK 的每种消息(`SDKMessage` 的类型与子类型,如 `system/init`)、助手消息与用户消息里的每种内容块,
11
+ 各注明转给 Coda 还是明确不显示,附一句原因。SDK 没有声明、却照样转来的几种内部消息(`system/post_turn_summary` 等)也登记为不显示。
12
+ 2. **升级检查**(`test/plugins/claude/sdk-registry.mjs`,`npm test` 里跑):用 TypeScript 编译器的类型检查器(仓库的 devDependency `typescript`)
13
+ 读锁定 SDK 入口导出的 `SDKMessage`,展开联合取每个成员 `type`、`subtype` 的字面量,再顺着助手、用户消息 `message.content` 的元素类型
14
+ 展开内容块的联合取 `type`,有没登记的就失败并列出来。别名、交叉类型、再导出与泛型都由检查器解析;`type`、`subtype` 不全是字符串字面量的
15
+ (比如 `string`)判失败并说出是哪个成员,不会悄悄放过。
16
+ 3. **运行时报告**:Coda 在 `initialize` 的 `clientCapabilities._meta.coda.unrecognized` 里表示收,入口才发 `_coda/unrecognized`
17
+ (协议 5.4.0,`docs/agent-protocol.md`「没适配的东西」),同一会话同一 `name` 只报一次;没表示就什么都不发,下面的后备显示照旧。
18
+ - 没登记的消息报 `sdk:<type>/<subtype>`(没有子类型的报 `sdk:<type>`),没登记的内容块报 `block:<type>`,都是 `kind: output`。
19
+ 消息和助手消息里的内容块带文字的(`text`,字符串或文字块的 `content`,消息的 `message.content`)照常显示成 Claude 的回复,报 `lost: false`;
20
+ 没有文字的报 `lost: true` 并附一句说明。结果、助手与用户消息、流式事件、额度事件不论子类型都按类型处理,子类型没登记也报 `lost: false`。
21
+ - 用户消息是给模型的输入(工具结果、Claude Code 补的说明等),不是 Claude 说的话:里面没登记的内容块只报不显示,`lost: true`,
22
+ 会话里留一条「没有显示」的说明。
23
+ - 每轮开始的 `system/init` 列出模型能用的工具,和已知清单 `agent/known-tools.json` 对照,清单里没有的报 `tool:<名字>`(`kind: tool`、`lost: false`)。
24
+ MCP 工具(`mcp__` 开头)是用户配的,不比。清单是锁定 SDK 在本入口的启动选项下 `system/init` 列出的工具,加上入口另外认识的
25
+ (`agent/tools.mjs` 里的、旧版 Claude Code 的)。
26
+
27
+ ### 升级 SDK 时
28
+
29
+ 1. 升 `@anthropic-ai/claude-agent-sdk` 的 PR(Dependabot 开的,或手动改版本并更新 `package-lock.json`)先要让 `npm test` 里的升级检查通过:
30
+ `node test/plugins/claude/sdk-registry.mjs` 列出新 SDK 有、登记表没有的消息类型与内容块。
31
+ 2. 逐个看新 SDK 的 `sdk.d.ts` 与它带的 `@anthropic-ai/sdk`:要显示的在 `agent/session.mjs` 里处理,再登记为转给 Coda;不显示的登记并写明原因。
32
+ 检查读不出声明时,按报错补读法或改登记,不要跳过。
33
+ 3. 用新 SDK 跑一轮真实会话,把新出现的原生工具加进 `agent/known-tools.json`(会话里没登记的工具会报成 `tool:<名字>`,遥测按 Agent 版本计数);
34
+ 要按读写、命令等显示的,同时在 `agent/tools.mjs` 里归类。
@@ -0,0 +1,48 @@
1
+ [
2
+ "Agent",
3
+ "Artifact",
4
+ "AskUserQuestion",
5
+ "Bash",
6
+ "BashOutput",
7
+ "CronCreate",
8
+ "CronDelete",
9
+ "CronList",
10
+ "Edit",
11
+ "EnterPlanMode",
12
+ "EnterWorktree",
13
+ "ExitPlanMode",
14
+ "ExitWorktree",
15
+ "Glob",
16
+ "Grep",
17
+ "KillBash",
18
+ "KillShell",
19
+ "LS",
20
+ "ListAgents",
21
+ "ListMcpResourcesTool",
22
+ "Monitor",
23
+ "MultiEdit",
24
+ "NotebookEdit",
25
+ "NotebookRead",
26
+ "ProposeGoal",
27
+ "PushNotification",
28
+ "Read",
29
+ "ReadMcpResourceTool",
30
+ "RemoteTrigger",
31
+ "ReportFindings",
32
+ "ScheduleWakeup",
33
+ "SendMessage",
34
+ "Skill",
35
+ "Task",
36
+ "TaskCreate",
37
+ "TaskGet",
38
+ "TaskList",
39
+ "TaskOutput",
40
+ "TaskStop",
41
+ "TaskUpdate",
42
+ "TodoWrite",
43
+ "ToolSearch",
44
+ "WebFetch",
45
+ "WebSearch",
46
+ "Workflow",
47
+ "Write"
48
+ ]
@@ -0,0 +1,132 @@
1
+ // What the locked Claude Agent SDK sends, and what this entry does with each (README「认不出的东西」): every message type and
2
+ // subtype of its SDKMessage, and every content block type of an assistant's and a user's message, shown in Coda or not, with
3
+ // why. An SDK upgrade registers what it brings: the check over the SDK's declarations (test/plugins/claude/sdk-registry.mjs)
4
+ // fails while any is missing. What comes that is not here all the same, and a native tool not in known-tools.json, is told to
5
+ // Coda (`_coda/unrecognized`, docs/agent-protocol.md「没适配的东西」).
6
+ import { readFileSync } from 'node:fs';
7
+
8
+ const shown = why => ({ shown: true, why }), hidden = why => ({ shown: false, why });
9
+ const failed = shown('一轮失败结束:报执行失败与用量');
10
+ const tasks = shown('后台任务与子 Agent,报成等待项(waits.mjs)');
11
+ const hooks = hidden('钩子的运行过程与输出,是运行机配置的事,不是 Claude 说的话');
12
+ // Neither in SDKMessage nor kept back: the SDK passes these on as they come (its readMessages).
13
+ const internal = hidden('SDK 没有声明、却照样转来的内部消息,不是 Claude 说的话');
14
+
15
+ // By type, and subtype where the type has them (messageName).
16
+ export const MESSAGES = {
17
+ assistant: shown('Claude 说的话、思考与工具调用,按内容块(ASSISTANT_BLOCKS)'),
18
+ user: shown('工具的结果转成工具调用的结果,其余按内容块(USER_BLOCKS)'),
19
+ stream_event: shown('文字与思考边生成边显示;其余内容块在完整的助手消息里处理'),
20
+ 'result/success': shown('一轮的结束:答复这一轮的 prompt,报用量'),
21
+ 'result/error_during_execution': failed,
22
+ 'result/error_max_turns': failed,
23
+ 'result/error_max_budget_usd': failed,
24
+ 'result/error_max_structured_output_retries': failed,
25
+ rate_limit_event: shown('额度窗口写进资源「Claude」读的 limits.json,额度耗尽时随执行失败报'),
26
+ 'system/init': shown('每轮开始:设置项、可用命令与标题,并对照已知的原生工具'),
27
+ 'system/commands_changed': shown('可用命令'),
28
+ 'system/status': shown('审批模式的变化与压缩的进度'),
29
+ 'system/compact_boundary': shown('压缩完成,及压缩前后的上下文'),
30
+ 'system/local_command_output': shown('斜杠命令的输出,作为 Claude 的回复显示'),
31
+ 'system/session_state_changed': shown('会话空闲时了结等待项'),
32
+ 'system/task_started': tasks,
33
+ 'system/task_updated': tasks,
34
+ 'system/task_notification': tasks,
35
+ 'system/background_tasks_changed': tasks,
36
+ 'system/task_progress': hidden('后台任务的进度摘要;等待项只报开始与结束'),
37
+ 'system/api_retry': hidden('请求在重试,还没有结果;最后失败随这一轮的结果报'),
38
+ 'system/control_request_progress': hidden('只给 side_question 控制请求报进度,入口不发这种请求'),
39
+ 'system/model_refusal_fallback': hidden('拒答后换模型重试的提示;重试的回答照常显示'),
40
+ 'system/model_refusal_no_fallback': hidden('拒答的提示;拒答由这一轮的助手消息与结果报'),
41
+ 'system/hook_started': hooks,
42
+ 'system/hook_progress': hooks,
43
+ 'system/hook_response': hooks,
44
+ 'system/plugin_install': hidden('Claude Code 插件的安装进度'),
45
+ 'system/thinking_tokens': hidden('思考用量的估计,只给进度指示用;思考的文字另外转'),
46
+ 'system/worker_shutting_down': hidden('远程控制的 worker 停下的原因,入口不用远程控制'),
47
+ 'system/notification': hidden('Claude Code 终端里的通知,不是 Claude 说的话'),
48
+ 'system/files_persisted': hidden('文件存到云端的结果,入口不用这个功能'),
49
+ 'system/memory_recall': hidden('召回的记忆,已作为上下文交给模型'),
50
+ 'system/elicitation_complete': hidden('链接表单完成的确认;表单本身经 onElicitation 转给 Coda'),
51
+ 'system/permission_denied': hidden('被拒的工具调用;拒绝的原因在工具结果里,随工具调用显示'),
52
+ 'system/mirror_error': hidden('会话记录镜像(SessionStore)写入失败,入口不用镜像'),
53
+ 'system/informational': hidden('终端里的提示行(状态、钩子的反馈),不是 Claude 说的话'),
54
+ tool_progress: hidden('工具运行中的心跳;工具调用本身照常显示'),
55
+ auth_status: hidden('登录过程的输出,入口不在会话里登录'),
56
+ tool_use_summary: hidden('几个工具调用的摘要;工具调用本身已逐个显示'),
57
+ prompt_suggestion: hidden('猜用户下一句的建议,入口没有打开 promptSuggestions'),
58
+ conversation_reset: hidden('/clear 之类开了新的对话;Coda 的会话记录照旧'),
59
+ 'system/post_turn_summary': internal,
60
+ 'system/task_summary': internal,
61
+ 'system/session_metadata': internal,
62
+ active_goal: internal,
63
+ autocompact_state: internal,
64
+ };
65
+
66
+ const results = hidden('服务端工具的结果,给模型读的');
67
+ const assistants = hidden('助手一方的内容块,不出现在用户消息里');
68
+ const upload = hidden('交给代码执行容器的文件');
69
+
70
+ export const ASSISTANT_BLOCKS = {
71
+ text: shown('Claude 说的话'),
72
+ thinking: shown('思考'),
73
+ tool_use: shown('工具调用'),
74
+ redacted_thinking: hidden('加密的思考,没有可读的文字'),
75
+ server_tool_use: hidden('服务端自己执行的工具的调用;Claude Code 的工具走 tool_use'),
76
+ web_search_tool_result: results,
77
+ web_fetch_tool_result: results,
78
+ advisor_tool_result: results,
79
+ code_execution_tool_result: results,
80
+ bash_code_execution_tool_result: results,
81
+ text_editor_code_execution_tool_result: results,
82
+ tool_search_tool_result: results,
83
+ mcp_tool_use: hidden('API 的 MCP 连接器;Claude Code 的 MCP 工具走 tool_use'),
84
+ mcp_tool_result: hidden('API 的 MCP 连接器的结果;Claude Code 的 MCP 工具走 tool_result'),
85
+ container_upload: upload,
86
+ compaction: hidden('服务端压缩出的摘要,给模型接着用;压缩由 status 与 compact_boundary 报'),
87
+ fallback: hidden('拒答后换模型的标记,之后的内容照常显示'),
88
+ };
89
+
90
+ export const USER_BLOCKS = {
91
+ tool_result: shown('工具调用的结果'),
92
+ text: hidden('用户的输入(Coda 自己有),或 Claude Code 给模型补的说明;唤醒一轮的开场文字交给等待项认领'),
93
+ image: hidden('给模型看的图片'),
94
+ document: hidden('给模型读的文档'),
95
+ search_result: hidden('给模型的检索结果'),
96
+ container_upload: upload,
97
+ thinking: assistants,
98
+ redacted_thinking: assistants,
99
+ tool_use: assistants,
100
+ server_tool_use: assistants,
101
+ web_search_tool_result: assistants,
102
+ web_fetch_tool_result: assistants,
103
+ code_execution_tool_result: assistants,
104
+ bash_code_execution_tool_result: assistants,
105
+ text_editor_code_execution_tool_result: assistants,
106
+ tool_search_tool_result: assistants,
107
+ };
108
+
109
+ // The native tools known here: what the locked SDK's init lists as this entry starts it, and the names it knows otherwise
110
+ // (tools.mjs, older Claude Code). An MCP server's (`mcp__…`) are the user's, never compared.
111
+ export const KNOWN_TOOLS = new Set(JSON.parse(readFileSync(new URL('./known-tools.json', import.meta.url), 'utf8')));
112
+
113
+ // A message's name here: its type, and its subtype where it has one.
114
+ export const messageName = m => typeof m.subtype === 'string' ? `${m.type}/${m.subtype}` : m.type;
115
+
116
+ // What is told to Coda: once a name in the session (a process has one), and only when Coda takes it (its initialize's
117
+ // `_meta.coda.unrecognized`, `taken`). What was lost says so, for the note Coda keeps in the session.
118
+ export class Unrecognized {
119
+ taken = false; told = new Set();
120
+ // `tell`: the notification's params but its session.
121
+ constructor(tell) { this.tell = tell; }
122
+ report(kind, name, lost) {
123
+ name = name.slice(0, 200);
124
+ if (!this.taken || this.told.has(name)) return;
125
+ this.told.add(name);
126
+ this.tell({ kind, name, lost, ...(lost ? { detail: `Claude Code 发来了入口还不认识的内容(${name}),没有显示出来` } : {}) });
127
+ }
128
+ // The native tools a turn's init lists that are not known here.
129
+ tools(names) {
130
+ for (const name of names) if (!name.startsWith('mcp__') && !KNOWN_TOOLS.has(name)) this.report('tool', `tool:${name}`, false);
131
+ }
132
+ }
package/agent/session.mjs CHANGED
@@ -5,6 +5,7 @@
5
5
  // withdraws them. A turn Claude begins on its own is told by what it handles. The plan windows it reports go to the file
6
6
  // the plugin's「Claude」resource reads (limits.json in CODA_PLUGIN_STATE, limits.mjs), not to Coda's session. Chosen, a session
7
7
  // that is to go on later is compacted before its cache expires, as the plugin's lifecycle process asks (compaction.mjs).
8
+ // What the SDK sends that is not registered, and native tools not known, are told to Coda (registry.mjs).
8
9
  import { randomUUID } from 'node:crypto';
9
10
  import { readFileSync } from 'node:fs';
10
11
  import { realpath, stat } from 'node:fs/promises';
@@ -21,6 +22,7 @@ import { toolCallFields, ToolCallCache } from './tools.mjs';
21
22
  import { elicitationForm, questionForm } from './questions.mjs';
22
23
  import { ClaudeWaits } from './waits.mjs';
23
24
  import { CacheUse } from './compaction.mjs';
25
+ import { ASSISTANT_BLOCKS, MESSAGES, USER_BLOCKS, Unrecognized, messageName } from './registry.mjs';
24
26
 
25
27
  // The Claude Agent SDK this entry runs, as the handshake's `agentInfo` names it (Coda's protocol「Agent 版本」).
26
28
  const SDK = (() => {
@@ -56,6 +58,8 @@ const CHUNKS = new Set(['agent_message_chunk', 'agent_thought_chunk']);
56
58
  const SAYING = new Set([...CHUNKS, 'tool_call', 'tool_call_update', 'plan']);
57
59
  // How many messages sent into a prompt (steer) may be waiting to be taken at once.
58
60
  const STEERING_LIMIT = 32;
61
+ // The message types handled whatever their subtype (receive); a system message is handled by its subtype.
62
+ const WHOLE = new Set(['assistant', 'user', 'stream_event', 'result', 'rate_limit_event']);
59
63
 
60
64
  class InputStream {
61
65
  queue = []; wake = null; ended = false;
@@ -89,12 +93,16 @@ export class ClaudeSession {
89
93
  // what the native turn going said of compacting (its boundary, its failure, its command's output), the compaction's once
90
94
  // that turn's result names its input.
91
95
  this.cacheUse = new CacheUse({ now }); this.compacting = null; this.turnCompaction = {};
96
+ this.unrecognized = new Unrecognized(params => this.notify('_coda/unrecognized', { sessionId: this.sessionId, ...params }));
92
97
  }
93
98
  connect(stream) {
94
99
  this.connection = agent({ name: 'coda-claude' })
95
- .onRequest('initialize', () => ({ protocolVersion: PROTOCOL_VERSION, agentInfo: SDK ? { name: 'claude-agent-sdk', version: SDK } : { name: 'coda-claude', version: '1' },
96
- agentCapabilities: { loadSession: true, sessionCapabilities: { additionalDirectories: {}, systemPrompt: {} }, _meta: { coda: { waits: {}, compact: {} } } },
97
- _meta: { steering: { supported: true } } }))
100
+ .onRequest('initialize', params => params ?? {}, ({ params }) => {
101
+ this.unrecognized.taken = !!params.clientCapabilities?._meta?.coda?.unrecognized;
102
+ return { protocolVersion: PROTOCOL_VERSION, agentInfo: SDK ? { name: 'claude-agent-sdk', version: SDK } : { name: 'coda-claude', version: '1' },
103
+ agentCapabilities: { loadSession: true, sessionCapabilities: { additionalDirectories: {}, systemPrompt: {} }, _meta: { coda: { waits: {}, compact: {} } } },
104
+ _meta: { steering: { supported: true } } };
105
+ })
98
106
  // The SDK's own reading of these drops what its schema does not name, `systemPrompt` among them: they are read as sent.
99
107
  .onRequest('session/new', params => params ?? {}, ({ params }) => this.open(params))
100
108
  .onRequest('session/load', params => params ?? {}, ({ params }) => this.open(params, params.sessionId))
@@ -436,6 +444,8 @@ export class ClaudeSession {
436
444
  // result not the compaction's; the compaction's once its result names the compaction's input (result).
437
445
  const compacting = this.compacting && (m.type === 'result' ? takenIn(m).includes(this.compacting.id) : !conversing(m));
438
446
  if (!compacting) this.begin(m.type === 'user' && !parent && m.origin ? { origin: m.origin, text: text(m.message?.content) } : {});
447
+ const name = messageName(m);
448
+ if (!Object.hasOwn(MESSAGES, name)) this.unlisted(`sdk:${name}`, m, parent ?? this.sessionId, m.uuid, WHOLE.has(m.type));
439
449
  switch (m.type) {
440
450
  case 'system': return this.system(m);
441
451
  case 'stream_event': return this.stream(m.event, parent);
@@ -454,6 +464,7 @@ export class ClaudeSession {
454
464
  this.turning = true;
455
465
  this.turnError = null; this.turnLimits = null;
456
466
  this.terminal = m.terminal_slash_commands ?? [];
467
+ this.unrecognized.tools(m.tools ?? []);
457
468
  // Its effort is the one applied, the model's own included: what was chosen stays what it was. A query on the
458
469
  // service's model reports it by that service's name for it.
459
470
  this.current = { ...this.current, model: this.serviceModel ?? reportedModel(this.models, this.current.model, m.model), mode: m.permissionMode ?? this.current.mode };
@@ -512,6 +523,14 @@ export class ClaudeSession {
512
523
  const said = m.content ?? m.stdout;
513
524
  if (typeof said === 'string') this.update(this.sessionId, { sessionUpdate: 'agent_message_chunk', messageId: m.uuid, content: { type: 'text', text: said } });
514
525
  }
526
+ // A message, or a content block of an assistant's, not registered (registry.mjs) is told to Coda, and the text it carries (as
527
+ // the SDK's own carry it: `text`, or `content` as a string or text blocks, or its message's) is said as Claude's; with none
528
+ // the user saw nothing of it. One `handled` as its type is (a result, whatever its subtype) loses nothing.
529
+ unlisted(name, item, sessionId, messageId, handled = false) {
530
+ const said = handled ? '' : typeof item.text === 'string' ? item.text : text(item.content) || text(item.message?.content);
531
+ this.chunk(sessionId, 'text', messageId, said);
532
+ this.unrecognized.report('output', name, !handled && !said);
533
+ }
515
534
  // A turn Claude began on its own is told by what it handles, before anything said or asked in it; its opening message, when
516
535
  // that comes first, may say what that is.
517
536
  begin(opening) {
@@ -582,12 +601,16 @@ export class ClaudeSession {
582
601
  if (streamed) streamed.confirmed = true;
583
602
  this.chunk(sessionId, block.type, apiId, said.slice(streamed?.sent ?? 0));
584
603
  if (!parent && block.type === 'text') this.title?.append(said);
585
- }
604
+ } else if (!Object.hasOwn(ASSISTANT_BLOCKS, block.type)) this.unlisted(`block:${block.type}`, block, sessionId, apiId);
586
605
  }
587
606
  }
588
607
  toolResults(m, parent) {
589
608
  for (const block of Array.isArray(m.message?.content) ? m.message.content : []) {
590
- if (block.type !== 'tool_result') continue;
609
+ // A user's message is input to the model, never Claude's words: a block not registered is told to Coda as not shown.
610
+ if (block.type !== 'tool_result') {
611
+ if (!Object.hasOwn(USER_BLOCKS, block.type)) this.unrecognized.report('output', `block:${block.type}`, true);
612
+ continue;
613
+ }
591
614
  const content = this.tools.finish(block.tool_use_id, m.tool_use_result, block.is_error);
592
615
  if (!content) continue;
593
616
  const said = text(block.content);
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "id": "claude",
4
4
  "displayName": "Claude",
5
5
  "description": "用 Claude Agent SDK 跑会话",
6
- "version": "1.10.2",
6
+ "version": "1.11.0",
7
7
  "categories": [
8
8
  "Agent"
9
9
  ],