@coda-console/protocol 6.15.0 → 6.16.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/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  版本规矩见 README 的[「版本」一节](README.md#版本)。协议自己的大版本写在协议名里(如 `coda/run@1`),与 npm 包版本、插件清单的 v2 独立;更早的版本见 git 历史。
4
4
 
5
+ ## 6.16.0 — 2026-11-20
6
+
7
+ - 这是在 6.15.0 之上的次版本。依据 README「版本」一节:Agent 接入协议是内部面,新增 schema 与文档小节升次版本;`stable/` 不动,`stable-grows` 与 `test/stable.test.mjs` 不受影响(每轮 Token 用量采集,coda#1824)。
8
+ - 新增 `schemas/agent/prompt-usage.json`:`session/prompt` 成功答复顶层 `result.usage` 的形状。`inputTokens`(未缓存输入)、`outputTokens`、`cachedReadTokens`、`cachedWriteTokens`、`totalTokens`、`thoughtTokens` 都是 0 到 9007199254740991 的整数,缺失或 `null` 即未报告;顶层须至少有一个非 `null` 的前四项计数,或带 `breakdown`;只有 `totalTokens`、`thoughtTokens` 不算。可选 `breakdown` 非空,每项须有 `model`(1 到 200 个 UTF-16 字符,用 `x-length` 按 `string.length` 计,不含 NUL,不全是空白)和至少一个非 `null` 的前四项计数;同一 `model` 的多项按四个核心计数(`inputTokens`、`outputTokens`、`cachedReadTokens`、`cachedWriteTokens`)分别相加,各自合计不得超过 9007199254740991,`totalTokens`、`thoughtTokens` 与其他键不参与(`agent.mjs` 的 `x-sum-max` 关键字检查)。未知键照旧允许。`agentValueProblem('prompt-usage.json', …)` 即可校验,`AGENT_SCHEMAS` 自动收录。
9
+ - `docs/agent-protocol.md` 加「每轮的用量」一节,写明位置(`result.usage`,不放 `_meta`)、`inputTokens` 不含缓存、只报本轮增量、`breakdown` 为分模型的唯一来源,同一模型分项的合并规则与安全整数上限,顶层汇总只供支持本约定、不处理 `breakdown` 的读取方使用(与 ACP 实验版按累计口径的 `usage` 不互通),以及不许带提示词、路径、代码和费用。
10
+ - `session/prompt` 仍由 ACP SDK 校验,不进 `AGENT_METHODS`;`usage.json`(`usage_update._meta.coda`)、`messages.json`、清单 schema 与 `stable/` 都没动。
11
+
5
12
  ## 6.15.0 — 2026-11-19
6
13
 
7
14
  - 这是在 6.14.0 之上的次版本(页面绑定需求二,协议包成员)。依据 README「版本」一节:`contributes.endpoints`、`records`、`self` 都不在 `stable/manifest.json`,是内部面,新增可选键、删内部键升次版本;`stable-grows` 与 `test/stable.test.mjs` 不受影响。H6 的协议成员(6.14.0)先合,本成员顺延为 6.15.0。
package/agent.mjs CHANGED
@@ -13,6 +13,27 @@ export const AGENT_SCHEMAS = Object.fromEntries(readdirSync(dir).filter(file =>
13
13
  const schemas = Object.values(AGENT_SCHEMAS), ajv = newAjv({ ownProperties: true });
14
14
  valueKeywords(ajv, {});
15
15
  ajv.addFormat('date-parse', value => Number.isFinite(Date.parse(value)));
16
+ // Entries of one model are merged by adding the four core counts; each sum must stay a safe integer.
17
+ const CORE_COUNTS = ['inputTokens', 'outputTokens', 'cachedReadTokens', 'cachedWriteTokens'];
18
+ ajv.addKeyword({
19
+ keyword: 'x-sum-max', type: 'array', schemaType: 'number', errors: true,
20
+ compile(limit, parentSchema) {
21
+ return function sum(entries) {
22
+ const totals = new Map();
23
+ for (const entry of entries) {
24
+ if (typeof entry?.model !== 'string') continue;
25
+ const counts = totals.get(entry.model) ?? totals.set(entry.model, {}).get(entry.model);
26
+ for (const name of CORE_COUNTS) {
27
+ const count = entry[name];
28
+ if (!Number.isInteger(count)) continue;
29
+ counts[name] = (counts[name] ?? 0) + count;
30
+ if (counts[name] > limit) { sum.errors = [{ keyword: 'x-sum-max', params: { limit }, parentSchema, message: '合计过大' }]; return false; }
31
+ }
32
+ }
33
+ return true;
34
+ };
35
+ },
36
+ });
16
37
  for (const schema of schemas) ajv.addSchema(schema);
17
38
 
18
39
  // A reference is relative to schemas/agent/, optionally with a JSON Pointer fragment.
@@ -1,7 +1,7 @@
1
1
  # Agent 接入协议
2
2
 
3
3
  Coda 用 [ACP](https://agentclientprotocol.com)(Agent Client Protocol)接 Agent,把[会话指令](#会话指令)交给 Agent(ACP 的 `systemPrompt`;不收它的由 Coda 在第一轮送入),
4
- 可选支持[插话](#插话)(`_session/steering`),另收 Agent 活动报告(`_coda/wait`)、入口[没适配的东西](#没适配的东西)(`_coda/unrecognized`)、[执行失败事实](#执行失败)(ACP 错误数据里的 `_meta.coda.failure`)和[工具调用的审核](#工具调用的审核)(`_meta.coda.review`)。本文是这份协议的唯一正文,写给要让一个 Agent(比如 Cursor 的)接入 Coda 的人:照它实现,
4
+ 可选支持[插话](#插话)(`_session/steering`),另收 Agent 活动报告(`_coda/wait`)、入口[没适配的东西](#没适配的东西)(`_coda/unrecognized`)、[执行失败事实](#执行失败)(ACP 错误数据里的 `_meta.coda.failure`)、[工具调用的审核](#工具调用的审核)(`_meta.coda.review`)和[每轮的用量](#每轮的用量)(`session/prompt` 答复的 `usage`)。本文是这份协议的唯一正文,写给要让一个 Agent(比如 Cursor 的)接入 Coda 的人:照它实现,
5
5
  Coda 就知道这个 Agent 在一轮之外还在做什么,如实显示、不回收它的进程,把它处理这些东西时的输出、审批和提问放进正确的一轮,
6
6
  并在这次调用结清时把结果只交回一次。产品上这些规则的来由见会话行为契约(Coda 的 docs/session-contracts.md)与完成唤醒契约(Coda 的 docs/subagent-design.md「完成唤醒契约」)。
7
7
 
@@ -28,6 +28,7 @@ ACP 的消息、内容块、会话指令和能力由 ACP SDK 校验,这里只
28
28
  | `error.data._meta.coda.failure` | [failure.json](../schemas/agent/failure.json) |
29
29
  | 工具更新的 `_meta.coda.review` | [review.json](../schemas/agent/review.json);已知风险值见 `$defs/risk` |
30
30
  | 用量更新的 `_meta.coda` | [usage.json](../schemas/agent/usage.json) |
31
+ | `session/prompt` 答复的 `usage` | [prompt-usage.json](../schemas/agent/prompt-usage.json) |
31
32
  | 配置选项的 `_meta.coda.default` | [default.json](../schemas/agent/default.json)(传入 `_meta.coda` 对象) |
32
33
  | 模型、模式、配置项及其选项的 `_meta.coda.shortName` | [display.json](../schemas/agent/display.json)(传入 `_meta.coda` 对象) |
33
34
 
@@ -60,7 +61,7 @@ Coda 是 ACP 的客户端。每个会话的 Agent 进程由一个会话托管进
60
61
  | --- | --- | --- |
61
62
  | `initialize` | Coda → Agent | 握手。`clientCapabilities` 带 `elicitation.form`、`elicitation.url`、`session.compaction`、`session.configOptions.boolean`、`_meta.coda.waits`(见[能力协商](#能力协商))、`_meta.coda.unrecognized`(见[没适配的东西](#没适配的东西))与 `_meta.coda.remote`(见[远端入口](#远端入口)) |
62
63
  | `session/new`、`session/load` | Coda → Agent | 新建或恢复原生会话,会话 ID 由 Agent 给;Agent 声明 `loadSession` 才会被要求恢复。Agent 声明了才带 `systemPrompt`([会话指令](#会话指令))。带 `_meta.coda.probe: true` 时改为连接内虚拟会话,见[配置探测](#配置探测)。结果里要报出当前模型(见下)。恢复期间重放的历史 Coda 不收(它有自己的),只收命令与标题 |
63
- | `session/prompt` | Coda → Agent | 一轮:Coda 送出用户的消息或它自己合成的输入(子会话结果等),Agent 回复 `stopReason` 即本轮结束 |
64
+ | `session/prompt` | Coda → Agent | 一轮:Coda 送出用户的消息或它自己合成的输入(子会话结果等),Agent 回复 `stopReason` 即本轮结束;答复可带本轮的 `usage`,见[每轮的用量](#每轮的用量) |
64
65
  | `_session/steering` | Coda → Agent | [插话](#插话):把用户的消息加进正在进行的一轮;只对声明支持的 Agent 发 |
65
66
  | `session/cancel` | Coda → Agent | 停止:本轮在进行时取消本轮;Agent 在一轮之外处理活动(`handling`)时,停止处理 |
66
67
  | `_coda/session/compact` | Coda → Agent | [压缩上下文](#压缩上下文):请 Agent 压缩这个会话的上下文;只对声明支持的 Agent 发,两轮之间也发 |
@@ -297,6 +298,24 @@ Agent 在 `session/update` 的 `usage_update` 里可以带 `_meta.coda`:一个
297
298
  `lastRequestAt`(主线程最后一次请求模型的时刻,提示词缓存从这一刻起算;比一轮结束早一段输出的时间)、`contextTokens`(那次请求带的上下文 token 数)、
298
299
  `cacheTtl`(缓存时长,`PT1H` 或 `PT5M`)、`compactedAt`(最近一次压缩的时刻)。
299
300
 
301
+ ## 每轮的用量
302
+
303
+ `session/prompt` 答复里 `usage` 的形状见 [prompt-usage.json](../schemas/agent/prompt-usage.json)。
304
+
305
+ Agent 在 `session/prompt` 成功答复的 `result.usage` 里报这一轮新用掉的 token(增量),Coda 据此按轮、按模型统计用量。`usage` 在 `result` 顶层,不放 `_meta`:`_meta` 留给各客户端的私有扩展,Coda 不解析 `_meta.usage`。
306
+
307
+ ```json
308
+ {"jsonrpc":"2.0","id":7,"result":{"stopReason":"end_turn","usage":{"inputTokens":1200,"outputTokens":480,"cachedReadTokens":53000,"cachedWriteTokens":null,"totalTokens":54680,"thoughtTokens":150,"breakdown":[{"model":"claude-sonnet-5-5","inputTokens":1000,"outputTokens":400,"cachedReadTokens":50000},{"model":"claude-haiku-5-5","inputTokens":200,"outputTokens":80,"cachedReadTokens":3000}]}}}
309
+ ```
310
+
311
+ - 计数都是 0 到 9007199254740991(安全整数上限)的整数;缺失或 `null` 表示没报告,不代表 0。顶层须至少有一个非 `null` 的 `inputTokens`、`outputTokens`、`cachedReadTokens`、`cachedWriteTokens`,或带 `breakdown`;只有 `totalTokens`、`thoughtTokens` 不算。
312
+ - `inputTokens` 是未缓存的输入,不含 `cachedReadTokens`、`cachedWriteTokens`;`outputTokens` 若来源已含思考 token,不再另加 `thoughtTokens`。`totalTokens` 与 `thoughtTokens` 只作同一轮的展示,Coda 不拿 `totalTokens` 求和。
313
+ - 只报本轮增量,绝不传会话累计的总量。失败或没有 `result` 的轮次不计入。
314
+ - `breakdown` 可选,非空数组,每项是 `model`(模型 ID,1 到 200 个 UTF-16 字符,即 JS 的 `string.length`,不含 NUL,不全是空白)加上述计数,且至少有一个非 `null` 的 `inputTokens`、`outputTokens`、`cachedReadTokens`、`cachedWriteTokens`。有 `breakdown` 时它是分模型的唯一来源,Coda 绝不把它与顶层相加。
315
+ - 同一 `model` 可以出现在多项里,Coda 把它们的 `inputTokens`、`outputTokens`、`cachedReadTokens`、`cachedWriteTokens` 分别相加;这四个核心计数同模型的合计各自不得超过 9007199254740991,超过即整份 `usage` 不合法。`totalTokens`、`thoughtTokens` 与其他键不参与合并,也不检查合计。
316
+ - 顶层汇总写不写都行:它供支持本节每轮增量约定、但不处理 `breakdown` 的读取方使用,建议填各模型之和。它不与 ACP 实验版的 `usage`(要求三项同时存在,按累计口径)互通,不要当成通用 ACP 客户端能读的字段。
317
+ - 不许带提示词、文件路径、项目名、代码或费用,只许模型 ID。未知键照旧允许、不改写,Coda 只读本节列出的字段。
318
+
300
319
  ## 工具调用的审核
301
320
 
302
321
  审核的形状与已知风险值见 [review.json](../schemas/agent/review.json)。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coda-console/protocol",
3
- "version": "6.15.0",
3
+ "version": "6.16.0",
4
4
  "description": "Coda 的协议:插件清单的类型(JSON Schema)、通用校验器、协议定义与调用信封、流程方言与 Agent 接入协议",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -0,0 +1,183 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://coda.pub/schemas/agent/v1/prompt-usage.json",
4
+ "type": "object",
5
+ "description": "usage 须为对象,并至少带一个非 null 的 inputTokens、outputTokens、cachedReadTokens、cachedWriteTokens 或一份 breakdown;只记本轮增量",
6
+ "$defs": {
7
+ "count": {
8
+ "type": [
9
+ "integer",
10
+ "null"
11
+ ],
12
+ "minimum": 0,
13
+ "maximum": 9007199254740991,
14
+ "description": "token 计数须为 0 到 9007199254740991 的整数,或省略、null 表示未报告"
15
+ },
16
+ "model": {
17
+ "type": "string",
18
+ "pattern": "^(?!\\s+$)[^\\u0000]+$",
19
+ "x-length": 200,
20
+ "description": "model 须为 1 到 200 个 UTF-16 字符、不含 NUL、不全是空白的模型 ID"
21
+ },
22
+ "counts": {
23
+ "properties": {
24
+ "inputTokens": {
25
+ "$ref": "#/$defs/count"
26
+ },
27
+ "outputTokens": {
28
+ "$ref": "#/$defs/count"
29
+ },
30
+ "cachedReadTokens": {
31
+ "$ref": "#/$defs/count"
32
+ },
33
+ "cachedWriteTokens": {
34
+ "$ref": "#/$defs/count"
35
+ },
36
+ "totalTokens": {
37
+ "$ref": "#/$defs/count"
38
+ },
39
+ "thoughtTokens": {
40
+ "$ref": "#/$defs/count"
41
+ }
42
+ }
43
+ },
44
+ "entry": {
45
+ "type": "object",
46
+ "description": "breakdown 的每一项须为对象,带 model 和至少一个非 null 的 inputTokens、outputTokens、cachedReadTokens 或 cachedWriteTokens",
47
+ "allOf": [
48
+ {
49
+ "$ref": "#/$defs/counts"
50
+ }
51
+ ],
52
+ "properties": {
53
+ "model": {
54
+ "$ref": "#/$defs/model"
55
+ }
56
+ },
57
+ "required": [
58
+ "model"
59
+ ],
60
+ "anyOf": [
61
+ {
62
+ "properties": {
63
+ "inputTokens": {
64
+ "not": {
65
+ "type": "null"
66
+ }
67
+ }
68
+ },
69
+ "required": [
70
+ "inputTokens"
71
+ ]
72
+ },
73
+ {
74
+ "properties": {
75
+ "outputTokens": {
76
+ "not": {
77
+ "type": "null"
78
+ }
79
+ }
80
+ },
81
+ "required": [
82
+ "outputTokens"
83
+ ]
84
+ },
85
+ {
86
+ "properties": {
87
+ "cachedReadTokens": {
88
+ "not": {
89
+ "type": "null"
90
+ }
91
+ }
92
+ },
93
+ "required": [
94
+ "cachedReadTokens"
95
+ ]
96
+ },
97
+ {
98
+ "properties": {
99
+ "cachedWriteTokens": {
100
+ "not": {
101
+ "type": "null"
102
+ }
103
+ }
104
+ },
105
+ "required": [
106
+ "cachedWriteTokens"
107
+ ]
108
+ }
109
+ ]
110
+ }
111
+ },
112
+ "allOf": [
113
+ {
114
+ "$ref": "#/$defs/counts"
115
+ }
116
+ ],
117
+ "properties": {
118
+ "breakdown": {
119
+ "type": "array",
120
+ "minItems": 1,
121
+ "description": "breakdown 须为至少一项的数组,同一 model 的 inputTokens、outputTokens、cachedReadTokens、cachedWriteTokens 各自合计不超过 9007199254740991",
122
+ "items": {
123
+ "$ref": "#/$defs/entry"
124
+ },
125
+ "x-sum-max": 9007199254740991
126
+ }
127
+ },
128
+ "anyOf": [
129
+ {
130
+ "required": [
131
+ "breakdown"
132
+ ]
133
+ },
134
+ {
135
+ "properties": {
136
+ "inputTokens": {
137
+ "not": {
138
+ "type": "null"
139
+ }
140
+ }
141
+ },
142
+ "required": [
143
+ "inputTokens"
144
+ ]
145
+ },
146
+ {
147
+ "properties": {
148
+ "outputTokens": {
149
+ "not": {
150
+ "type": "null"
151
+ }
152
+ }
153
+ },
154
+ "required": [
155
+ "outputTokens"
156
+ ]
157
+ },
158
+ {
159
+ "properties": {
160
+ "cachedReadTokens": {
161
+ "not": {
162
+ "type": "null"
163
+ }
164
+ }
165
+ },
166
+ "required": [
167
+ "cachedReadTokens"
168
+ ]
169
+ },
170
+ {
171
+ "properties": {
172
+ "cachedWriteTokens": {
173
+ "not": {
174
+ "type": "null"
175
+ }
176
+ }
177
+ },
178
+ "required": [
179
+ "cachedWriteTokens"
180
+ ]
181
+ }
182
+ ]
183
+ }