@coda-console/protocol 4.2.0 → 5.0.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,17 @@
2
2
 
3
3
  按 semver。协议自己的大版本写在协议名里(如 `coda/run@1`),与 npm 包版本、插件清单的 v2 独立;更早的版本见 git 历史。
4
4
 
5
+ ## 5.0.0 — 2026-11-09
6
+
7
+ 不兼容变更:
8
+
9
+ - `coda/session@1` 的 `post` 从立即结果改为开运行(`run: true`),输入不变:发一条消息可能要十几秒到几十秒(排队、按真人节奏输入、发出后确认)。发送者固定是 `self`,重发同一个 `requestId` 得到同一个运行、只发一次;`completed` 的 `result` 是 `{ message: { id, cursor } }`,即以后 `read` 读到的那条;`failed` 的 `result` 是 `{ code, message }`,不确定发没发出去时 `code` 为 `unknown`,调用方显示「结果未知」、不自动重发;取消沿用 `coda/run@1`。协议名仍是 `coda/session@1`,不升大版本:目前还没有第三方实现 `post`。
10
+
11
+ 新增:
12
+
13
+ - 操作定义的 `idempotent: true`:可以改状态、但重发做的是同一件事的立即结果操作;调用双方和 `safe` 一样,都不按 `requestId` 留请求和回答。只能与 `result` 一起写,不能与 `run` 同时出现;`safe` 已含 `idempotent`,两者不同时写。
14
+ - `coda/session@1` 的操作 `draft`(`idempotent`):设定 `self` 还没发出去的草稿,结果 `{}`。输入 `{ text, version, steps? }`:`text` 是全文(空串为清空),`version` 是非负整数、比大小,Provider 丢掉不大于已见最大版本的,建议用毫秒时间;`steps` 是从上一版到这一版的中间状态 `{ text, delayMs }`,可照节奏回放,最后一步等于 `text`。用户发出时仍调 `post`,全文在 `post` 的输入里,没写过草稿的 `post` 也照常发。
15
+
5
16
  ## 4.2.0 — 2026-11-09
6
17
 
7
18
  - `coda/session@1` 的媒体 part(`read` 的消息与 `post` 的输入)加可选的 `width`、`height`(像素,已按拍摄方向转正)、`duration`(秒)、`thumb`(内嵌 JPEG 的 base64,长边不超过 64 像素,至多 8192 字符)与 `path`(服务内路径,须和 `service` 一起出现);文档写清原文件在记录持有方的 `/v1/plugins/<provider 插件>/services/<service>/<path>`,Range 与流中断沿用本机流式服务,以及缺尺寸、缩略图、`service` 时怎么显示(Coda #2342 第 1 步)。
package/README.md CHANGED
@@ -127,7 +127,7 @@ agentValueProblem('review.json', review, '工具的审核');
127
127
  - **`coda/run@1`**:被调用方自己的运行记录。spec 记录调用的记录、操作、输入、请求 ID、父引用和是否被等待;status 是 `working`、`input-required`、`completed`、`failed`、`canceled`,后三个是终态。`completed`/`failed` 必带 `result`,`failed` 的 `result` 约定为 `{ code, message }`(`code: "unknown"` 表示结局不明),`canceled` 可带取消前的话和留下的东西;另有可选的 `progress` 和等输入时的 `awaiting`。操作是 `cancel`。
128
128
  - **`coda/executor@1`**:执行器。操作 `execute` 接一份执行工作并返回运行引用,运行记录在执行器那边;没有 `target`、`executor` 或 `assignmentId` 绑定,也不回写调用方的记录。
129
129
  - **`coda/agent@1`**:Agent 资源,复数名 `agents`;spec 照 Coda 的 Agent 资源(`agent`、`title`、`plugin?`、`defaultModel`、`models`、`modes` 等),`plugin` 是提供 Agent 程序的插件、不是记录的 provider;status 为空对象;操作 `spawn`(`run: true`)开一个子会话。
130
- - **`coda/session@1`**:插件提供的外部会话。spec 是标题、容器和可选的环境引用;status 是 `head` 游标与参与者;操作 `read`、`post` 都是立即结果,撤回、编辑、表情是用 `about` 指向旧消息的新消息。
130
+ - **`coda/session@1`**:插件提供的外部会话。spec 是标题、容器和可选的环境引用;status 是 `head` 游标与参与者;操作 `read`(`safe`)与 `draft`(`idempotent`)是立即结果,`post` 开运行;撤回、编辑、表情是用 `about` 指向旧消息的新消息。
131
131
  - **`coda/environment@1`**:插件提供的只读外部环境。spec 带 `actions`;操作 `list` 列目录、`read` 读文件(一次至多 1 MiB)。
132
132
 
133
133
  统一调用是 `{ record: {kind,id}, operation, input, requestId, parent: {kind,id}, awaited }`,回答恰为 `{ result }` 或 `{ run: {kind,id} }` 之一。同一 `requestId` 重发只做一次;慢操作返回运行引用,调用方 watch 运行记录取结果,没有推送或回写。
package/docs/protocol.md CHANGED
@@ -73,7 +73,7 @@ protocolShortName('coda/run@1'); // 'coda-1run-21'
73
73
  | `plural` | 复数名(见「复数名」),可选;不写就是短名 |
74
74
  | `spec` | 记录的 spec 的 JSON Schema,必填 |
75
75
  | `status` | 记录的 status 的 JSON Schema,必填 |
76
- | `operations` | 操作名 → `{ description?, input, result, safe? }` 或 `{ description?, input, run: true }`,必填;不定义操作写 `{}` |
76
+ | `operations` | 操作名 → `{ description?, input, result, safe?, idempotent? }` 或 `{ description?, input, run: true }`,必填;不定义操作写 `{}` |
77
77
 
78
78
  `spec`、`status`、`input`、`result` 都是 JSON Schema draft 2020-12,只引用自己文档里的 `#/$defs/...`,不引用别的文件。`input` 必有。操作的输出二选一:
79
79
 
@@ -82,6 +82,8 @@ protocolShortName('coda/run@1'); // 'coda-1run-21'
82
82
 
83
83
  `safe: true` 只能和 `result` 一起写,不能和 `run` 一起写,表示只读、没有副作用、立即给结果的操作;调用方和被调用方都不按 `requestId` 留它的请求和回答(见「调用信封」)。
84
84
 
85
+ `idempotent: true` 也只能和 `result` 一起写,不能和 `run` 一起写,表示可以改状态、但重发做的是同一件事的立即结果操作,比如把草稿设成某一段:调用双方和 `safe` 一样,都不按 `requestId` 留请求和回答。两者是 HTTP 的 safe 与 idempotent 之分:`safe` 只读,`idempotent` 可以改状态。`safe` 已含 `idempotent`,两个不同时写。
86
+
85
87
  ```json
86
88
  {
87
89
  "name": "my-plugin/task@1",
@@ -158,7 +160,7 @@ definitionProblem(definition, { namespace: 'my-plugin' }); // 同时核对命名
158
160
  {"run": {"kind": "coda/run@1", "id": "run-1"}}
159
161
  ```
160
162
 
161
- `requestId` 由调用方给,重发同一个请求只做一次。`safe` 的操作例外:调用双方都不按 `requestId` 留请求和回答,重发就再读一次。读回来的是对方的数据,比如群消息、环境里的文件内容,留在调用双方的库里就成了镜像。立即结果用 `result`;慢的操作用 `run` 指向被调用方自己的 `coda/run@1` 记录,调用方 watch 这条记录取结果,被调用方不推送。取消也是一次调用(`coda/run@1` 的 `cancel` 操作),不是写别人的记录。
163
+ `requestId` 由调用方给,重发同一个请求只做一次。`safe` 与 `idempotent` 的操作例外:调用双方都不按 `requestId` 留请求和回答,重发就再做一次。`safe` 读回来的是对方的数据,比如群消息、环境里的文件内容,留在调用双方的库里就成了镜像;`idempotent` 重做一遍结果相同,留着也没用。立即结果用 `result`;慢的操作用 `run` 指向被调用方自己的 `coda/run@1` 记录,调用方 watch 这条记录取结果,被调用方不推送。取消也是一次调用(`coda/run@1` 的 `cancel` 操作),不是写别人的记录。
162
164
 
163
165
  ```js
164
166
  import { invocationRequestProblem, invocationResponseProblem } from '@coda-console/protocol/protocol';
@@ -283,7 +285,7 @@ status:
283
285
 
284
286
  ## coda/session@1
285
287
 
286
- 插件提供的外部会话,比如一个微信群:参与者、消息和发消息。
288
+ 插件提供的外部会话,比如一个微信群:参与者、消息、草稿和发消息。
287
289
 
288
290
  spec:
289
291
 
@@ -302,14 +304,24 @@ status:
302
304
  | `state` | 可选,`active` 或 `unreachable` |
303
305
  | `reason` | 可选,状态说明 |
304
306
 
305
- 观察方 watch 这条记录,`head` 变了就用 `read` 读游标之后的消息。两个操作都是立即结果,不开运行;`read` 是 `safe` 的:
307
+ 观察方 watch 这条记录,`head` 变了就用 `read` 读游标之后的消息。`read` 与 `draft` 是立即结果,`read` 是 `safe` 的,`draft` 是 `idempotent` 的;`post` 开运行:
306
308
 
307
309
  - `read`:输入 `{ after?, before?, limit? }`,`after`、`before` 是游标,`limit` 为 1 到 100;结果 `{ messages, cursor, more }`,没有消息时 `cursor` 为 `null`。`messages` 总按先后排,旧的在前:
308
310
  - 都不带游标:最新的 `limit` 条,`more` 表示更早还有没有;客户端打开会话时先读这一页;
309
311
  - 只带 `after`:这个游标之后的,`more` 表示之后还有没有;
310
312
  - 只带 `before`:这个游标之前最新的 `limit` 条,`more` 表示更早还有没有;往前翻页就拿这一页第一条的 `cursor` 作下一次的 `before`;
311
313
  - `after` 与 `before` 不能同时带。
312
- - `post`:输入 `{ text, parts?, replyTo?, mentions? }`;结果 `{ message: { id, cursor } }`。发送者固定是 `self`;Provider 按调用的 `requestId` 只发一次,发送失败就报错。
314
+ - `post`(`run: true`):输入 `{ text, parts?, replyTo?, mentions? }`,以 `self` 发一条消息。发一条可能要十几秒到几十秒(排队、按真人节奏输入、发出后确认),所以开运行:
315
+ - 发送者固定是 `self`;重发同一个 `requestId`,得到的是同一个运行,只发一次;
316
+ - 运行 `completed` 时,`result` 是 `{ message: { id, cursor } }`,就是以后 `read` 会读到的那条消息;
317
+ - 发送失败时运行为 `failed`,`result` 是 `{ code, message }`;不确定发没发出去时(例如输入到一半进程没了)`code` 为 `unknown`,调用方显示「结果未知」,不自动重发;
318
+ - 取消沿用 `coda/run@1` 的 `cancel`:还没开始发的停下,得到 `canceled`;已经开始发的,Provider 可以不停,是否停下看 status;
319
+ - 全文总在 `post` 的输入里;没有先写过草稿的 `post` 也照常发出去。
320
+ - `draft`(`idempotent`):设定 `self` 在这个会话里还没发出去的文字,结果 `{}`。输入 `{ text, version, steps? }`:
321
+ - `text` 是当前草稿的全文,至多 65536 个字符;空串表示清空或放弃;
322
+ - `version` 是调用方给的非负整数版本号,比大小:Provider 收到不大于已见最大版本的直接丢掉,多台设备都在写时后写的为准;建议用毫秒时间;
323
+ - `steps` 可选,至多 1000 项,是从上一版到这一版之间按先后排的中间状态 `{ text, delayMs }`:每一步的全文,以及距上一步的间隔(毫秒,0 到 600000);Provider 可以照这个节奏回放,最后一步等于 `text`;
324
+ - Provider 可以把草稿同步到对方平台的输入框,用户发出时那边基本已经打完;用不上草稿的 Provider 直接答 `{}`。
313
325
 
314
326
  消息是 `{ id, cursor, sender, senderName?, text, parts?, replyTo?, mentions?, about?, createdAt? }`:
315
327
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coda-console/protocol",
3
- "version": "4.2.0",
3
+ "version": "5.0.0",
4
4
  "description": "Coda 的协议:插件清单的类型(JSON Schema)、通用校验器、协议定义与调用信封、流程方言与 Agent 接入协议",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -2,7 +2,7 @@
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
3
  "$id": "https://coda.pub/schemas/protocol/v1/definition.json",
4
4
  "title": "协议定义",
5
- "description": "一个协议的名字、记录格式与操作。名字是「命名空间/名字@大版本」;spec、status 与每个操作的输入、结果都是 JSON Schema(draft 2020-12),只引用定义自身的 $defs。操作要么立刻返回结果(result),要么返回运行引用(run: true);只读的立即结果可标 safe: true。",
5
+ "description": "一个协议的名字、记录格式与操作。名字是「命名空间/名字@大版本」;spec、status 与每个操作的输入、结果都是 JSON Schema(draft 2020-12),只引用定义自身的 $defs。操作要么立刻返回结果(result),要么返回运行引用(run: true);只读的立即结果可标 safe: true,可改状态但重发做同一件事的立即结果可标 idempotent: true。",
6
6
  "type": "object",
7
7
  "properties": {
8
8
  "name": {"$ref": "#/$defs/name"},
@@ -45,19 +45,21 @@
45
45
  "title": {"description": "给人看的名字", "type": "string", "maxLength": 100, "pattern": "^(?=.*\\S)[^\\u0000\\r\\n]*$"},
46
46
  "description": {"description": "一句话说明", "type": "string", "maxLength": 1000, "pattern": "^(?=.*\\S)[^\\u0000\\r\\n]*$"},
47
47
  "operation": {
48
- "description": "操作:input 必有;result 是立即结果的 schema,或写 run: true 表示返回运行引用。两者恰写一个。safe: true 只能与 result 一起写。",
48
+ "description": "操作:input 必有;result 是立即结果的 schema,或写 run: true 表示返回运行引用。两者恰写一个。safe: true、idempotent: true 只能与 result 一起写,两者不同时写(safe 已含 idempotent)。",
49
49
  "type": "object",
50
50
  "properties": {
51
51
  "description": {"$ref": "#/$defs/description"},
52
52
  "input": {"$ref": "#/$defs/schema"},
53
53
  "result": {"$ref": "#/$defs/schema"},
54
54
  "run": {"const": true, "description": "返回运行引用,而不是立即结果"},
55
- "safe": {"const": true, "description": "只读、没有副作用:调用双方都不按 requestId 留请求和回答,重发就再做一次"}
55
+ "safe": {"const": true, "description": "只读、没有副作用:调用双方都不按 requestId 留请求和回答,重发就再做一次"},
56
+ "idempotent": {"const": true, "description": "可改状态,但重发做的是同一件事:调用双方都不按 requestId 留请求和回答,重发就再做一次"}
56
57
  },
57
58
  "required": ["input"],
58
59
  "additionalProperties": false,
59
60
  "oneOf": [{"required": ["result"]}, {"required": ["run"]}],
60
- "dependentRequired": {"safe": ["result"]}
61
+ "dependentRequired": {"safe": ["result"], "idempotent": ["result"]},
62
+ "not": {"required": ["safe", "idempotent"]}
61
63
  }
62
64
  }
63
65
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "coda/session@1",
3
3
  "title": "会话",
4
- "description": "插件提供的外部会话:参与者、消息与发消息。Provider 每追加一条消息就推进 status.head,观察方 watch 记录后用 read 读游标之后的消息;撤回、编辑、表情回应都是一条用 about 指向旧消息的新消息。",
4
+ "description": "插件提供的外部会话:参与者、消息、草稿与发消息。Provider 每追加一条消息就推进 status.head,观察方 watch 记录后用 read 读游标之后的消息;撤回、编辑、表情回应都是一条用 about 指向旧消息的新消息。",
5
5
  "spec": {
6
6
  "$schema": "https://json-schema.org/draft/2020-12/schema",
7
7
  "type": "object",
@@ -148,7 +148,7 @@
148
148
  }
149
149
  },
150
150
  "post": {
151
- "description": "以 self 发一条消息;Provider 按 requestId 只发一次,立即结果。",
151
+ "description": "以 self 发一条消息,开运行;重发同一个 requestId 得到同一个运行,只发一次。completed 的 result 是 { message: { id, cursor } },即以后 read 读到的那条;failed 的 result 是 { code, message },不确定发没发出去时 code 为 unknown,调用方显示结果未知、不自动重发。还没开始发时取消得到 canceled,已开始的看 status。没先写草稿也照常发。",
152
152
  "input": {
153
153
  "$schema": "https://json-schema.org/draft/2020-12/schema",
154
154
  "type": "object",
@@ -198,22 +198,39 @@
198
198
  "required": ["text"],
199
199
  "additionalProperties": false
200
200
  },
201
- "result": {
201
+ "run": true
202
+ },
203
+ "draft": {
204
+ "description": "设定 self 在这个会话里还没发出去的草稿,立即结果。text 是全文,空串为清空;version 只增,Provider 丢掉不大于已见最大版本的;steps 是从上一版到这一版的中间状态,可照 delayMs 回放,最后一步等于 text。用不上草稿的 Provider 直接答 {}。",
205
+ "idempotent": true,
206
+ "input": {
202
207
  "$schema": "https://json-schema.org/draft/2020-12/schema",
203
208
  "type": "object",
204
209
  "properties": {
205
- "message": {
206
- "type": "object",
207
- "properties": {
208
- "id": {"type": "string", "minLength": 1, "maxLength": 2048},
209
- "cursor": {"type": "string", "minLength": 1, "maxLength": 2048}
210
- },
211
- "required": ["id", "cursor"],
212
- "additionalProperties": false
210
+ "text": {"type": "string", "maxLength": 65536, "description": "草稿全文;空串表示清空或放弃"},
211
+ "version": {"type": "integer", "minimum": 0, "maximum": 9007199254740991, "description": "调用方给的版本号,比大小,后写的为准;建议用毫秒时间"},
212
+ "steps": {
213
+ "type": "array",
214
+ "maxItems": 1000,
215
+ "description": "从上一版到这一版的中间状态,按先后排;最后一步等于 text",
216
+ "items": {
217
+ "type": "object",
218
+ "properties": {
219
+ "text": {"type": "string", "maxLength": 65536, "description": "这一步的全文"},
220
+ "delayMs": {"type": "integer", "minimum": 0, "maximum": 600000, "description": "距上一步的间隔(毫秒)"}
221
+ },
222
+ "required": ["text", "delayMs"],
223
+ "additionalProperties": false
224
+ }
213
225
  }
214
226
  },
215
- "required": ["message"],
227
+ "required": ["text", "version"],
216
228
  "additionalProperties": false
229
+ },
230
+ "result": {
231
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
232
+ "type": "object",
233
+ "maxProperties": 0
217
234
  }
218
235
  }
219
236
  }