@coda-console/protocol 6.13.0 → 6.15.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,21 @@
2
2
 
3
3
  版本规矩见 README 的[「版本」一节](README.md#版本)。协议自己的大版本写在协议名里(如 `coda/run@1`),与 npm 包版本、插件清单的 v2 独立;更早的版本见 git 历史。
4
4
 
5
+ ## 6.15.0 — 2026-11-19
6
+
7
+ - 这是在 6.14.0 之上的次版本(页面绑定需求二,协议包成员)。依据 README「版本」一节:`contributes.endpoints`、`records`、`self` 都不在 `stable/manifest.json`,是内部面,新增可选键、删内部键升次版本;`stable-grows` 与 `test/stable.test.mjs` 不受影响。H6 的协议成员(6.14.0)先合,本成员顺延为 6.15.0。
8
+ - 清单 `contributes` 加可选的 `records`:数组,每种记录一项 `{ kind, command, every? }`,不许别的键,至多 20 项,按 `kind` 去重。`kind` 是协议名(复用 `definition.json` 的 `name`);`command` 是插件目录里的文件(`x-file`,写法同 `protocolImplementations` 的 `commands`);`every` 可选,写法与 Coda 资源面板的 `maxAge` 同一套(ISO 8601 风格:周 `W`、天 `D`、时 `H`、分 `M`、秒 `S`,只有秒能带小数点,不收年、月,如 `PT5M`、`PT90S`、`P1W1D`),以后资源面板迁成记录时 `maxAge` 就对应 `every`。`every` 不短于 `PT1M` 的下限 schema 表达不了,由 Coda 判,schema 只查时长的写法。`kind` 是不是这个插件 `protocolImplementations` 里的种类,也由 Coda 装载时查。
9
+ - 清单 `contributes` 加可选的 `self`:对象,名字(复用 `operationName`)→ 插件目录里的文件(`x-file`),至多 50 个。
10
+ - 不兼容:删掉 `contributes.endpoints`:删去 `schemas/plugin/endpoints.json` 与 `plugin.json` 里的引用。`extension-points.json` 里保留键名单的 `endpoints` 留着,第三方仍不能把它当扩展点的键;保留名单另加 `records`、`self`。清单里再写 `contributes.endpoints` 的结果与别的不认识的键一样:不查形状,`manifestProblem` 通过;旧清单判不判兼容由 Coda 定。
11
+
12
+ ## 6.14.0 — 2026-11-19
13
+
14
+ - 这是在 6.13.0 之上的次版本。依据 README「版本」一节:协议定义是内部面,新增可选字段升次版本;`contributes.events` 是试行面,不在 `stable/` 里,删它不动稳定面(先例见 6.12.0 删 `contributes.groups`),`stable-grows` 与 `test/stable.test.mjs` 不受影响(Coda 宿主设计 coda#2749 H6)。
15
+ - 协议定义加可选的顶层 `events`:事件名 → `{ description?, payload, starts?, ends? }`,不许别的键,至少一条、至多 50 条。事件名是点分的全名(沿用原 `contributes.events` 的写法:「域.名」,小写字母、数字和「_」,以字母开头,至少两段,至多 100 个字符),`coda/` 以外的协议不能用 `workflow`、`session`、`user` 做域。`payload` 是载荷的 JSON Schema,顶层不能定义 `coda`(留给宿主);`starts: false` 表示不开新流程、只交给已在跟这个主体的运行,`ends: true` 表示这个事件结束它的主体。事件的版本就是协议的大版本。`definitionProblem` 还查每个事件的 `payload` 能编译。
16
+ - 权限组加可选的 `events: [事件名…]`(不重复)。`definitionProblem`:组里列的事件必须在 `events` 里;每个事件最多属于一组;有 `events` 的组只放事件:`events` 至少一项,`operations`、`streams`、`read` 这几个键不能出现,按键在不在判断,写成 `[]` 或 `false` 也不行;「每组至少管一样东西」把 `events` 算进去。不在任何组里的事件不对插件开放。
17
+ - 只有事件的协议:写了 `events` 的定义可以不写 `spec`、`status`、`operations`;`spec` 与 `status` 要么都写要么都不写。没写 `events` 的定义照旧三者必填。没有记录(没写 `spec`、`status`)的协议不能有非空的 `operations`、非空的 `streams`,也不能有 `read: true` 的组,这些都按记录寻址;要就须同时写 `spec` 和 `status`。`protocol.mjs` 导出 `protocolHasRecords(definition)`:有 `spec` 才有记录,Coda 据此不把只有事件的协议列成记录种类。
18
+ - 不兼容:删掉 `contributes.events`:删去 `schemas/plugin/events.json`。事件改写在协议定义里。`extension-points.json` 里保留键名单的 `events` 留着,第三方仍不能把它当扩展点的键。清单里再写 `contributes.events` 的结果与别的不认识的键一样:不查形状,`manifestProblem` 通过。
19
+
5
20
  ## 6.13.0 — 2026-11-19
6
21
 
7
22
  - 这是在 6.12.0 之上的次版本(6.12.0 由 H9-B 在途,版本号暂排其后,合并时跟上 main 再顺延)。依据 README「版本」一节:清单新增可选根键,旧清单照旧通过;这一键进稳定面,`stable/manifest.json` 加 `engines.coda`,`revision` 加到 3(Coda 宿主设计 H7-A,第 10 题)。
package/README.md CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  Coda 与插件之间的约定,Coda 和插件都引用它,独立演进:
4
4
 
5
- - **插件清单的类型**(`schemas/plugin/`):插件目录 `package.json` 里 `contributes` 的每个键是一种类型,各有一份 JSON Schema(draft 2020-12,`$id` 为 `https://coda.pub/schemas/plugin/v2/<文件>`)。`plugin.json` 是整份清单,只列通用键(`configuration`、`extensionPoints`、`extensions`、`services`、`setup`、`environment`、`path`、`endpoints`、`views`、`protocols`、`protocolImplementations`);`setting.json` 是一项设置的声明。其余键由 Coda 的贡献点登记表或某个插件的扩展点声明:Coda 自己的(`agents`、`composerModes`、`modelProviders` 等)的 schema 仍放在这里作公开数据(如 `agents.json`、`composer-modes.json`),由登记它的 provider 引用;扩展点可以带自己的 schema(`extensionPoints.<名>.schema`,声明它的插件目录里的一个文件),写给它的东西合乎那份 schema 才对。
5
+ - **插件清单的类型**(`schemas/plugin/`):插件目录 `package.json` 里 `contributes` 的每个键是一种类型,各有一份 JSON Schema(draft 2020-12,`$id` 为 `https://coda.pub/schemas/plugin/v2/<文件>`)。`plugin.json` 是整份清单,只列通用键(`configuration`、`extensionPoints`、`extensions`、`services`、`setup`、`environment`、`path`、`views`、`protocols`、`protocolImplementations`、`records`、`self`);`setting.json` 是一项设置的声明。其余键由 Coda 的贡献点登记表或某个插件的扩展点声明:Coda 自己的(`agents`、`composerModes`、`modelProviders` 等)的 schema 仍放在这里作公开数据(如 `agents.json`、`composer-modes.json`),由登记它的 provider 引用;扩展点可以带自己的 schema(`extensionPoints.<名>.schema`,声明它的插件目录里的一个文件),写给它的东西合乎那份 schema 才对。
6
6
  - **通用校验器**(`validate.mjs`):用 Ajv 对清单做 schema 校验。JSON Schema 说不了的只用五个通用关键字(`x-unique-by`、`x-max-contains`、`x-ref`、`x-placeholders`、`x-file`,照 Kubernetes 的 `x-kubernetes-*`),每个在哪里写都是同一个意思,写法见文件开头。校验器不随功能变长:类型加字段就是改 schema。
7
- - **协议定义与调用**(`schemas/protocol/`、`protocol.mjs`、`docs/protocol.md`):协议是 `命名空间/名字@大版本` + spec/status 的 JSON Schema + 操作;插件在 `contributes.protocols` 里定义自己命名空间下的协议,在 `contributes.protocolImplementations` 里声明实现哪些协议;定义可写 `streams` 声明只读记录流,可写 `permissions` 把操作、流与记录的读取分成权限组,插件在清单根的 `uses` 里按组申请。调用是 `{ record, operation, input, requestId, parent, awaited }`,回答是 `{ result }` 或 `{ run }`;`coda/run@1`、`coda/agent@1`、`coda/session@1`、`coda/environment@1`、`coda/card@1` 是五个核心协议。
7
+ - **协议定义与调用**(`schemas/protocol/`、`protocol.mjs`、`docs/protocol.md`):协议是 `命名空间/名字@大版本` + spec/status 的 JSON Schema + 操作;插件在 `contributes.protocols` 里定义自己命名空间下的协议,在 `contributes.protocolImplementations` 里声明实现哪些协议;定义可写 `streams` 声明只读记录流,可写 `events` 声明事件(载荷格式与 `starts`、`ends`;只有事件的协议可以不写 `spec`、`status`、`operations`,`protocolHasRecords` 判断一份定义有没有记录),可写 `permissions` 把操作、流、事件与记录的读取分成权限组,插件在清单根的 `uses` 里按组申请。调用是 `{ record, operation, input, requestId, parent, awaited }`,回答是 `{ result }` 或 `{ run }`;`coda/run@1`、`coda/agent@1`、`coda/session@1`、`coda/environment@1`、`coda/card@1` 是五个核心协议。
8
8
  - **流程方言**(`workflow/`):项目和插件的流程(`.coda/workflows/*.bpmn`,BPMN 2.0)在 Coda 的命名空间 `https://coda.pub/schema/bpmn/1.0` 里能写什么。`moddle.json` 是 `coda:` 属性与元素(bpmn-moddle 的描述格式),`values.json` 是每个值能写什么(每个 `coda:` 类型的属性组成的对象的 JSON Schema,一个元素里同种 `coda:` 元素成组的 schema),`actions.json` 是 `coda:action` 的各个动作和它们接受的参数,`dialect.json` 是 Coda 自己发的事件、步骤失败的错误码、表达式能读的运行变量和运行的输入能是哪些类型。`workflow.mjs` 用它们读一份流程:方言里没有的、值不合 schema 的都拒绝,说法是「位置:schema 对那里的说明」。值的 schema 用清单的那几个关键字,另加三个:`x-feel`(是 FEEL:「=」开头是表达式,否则文字里的 `{{ … }}` 是,按这份流程里的名字读得通)、`x-duration`(ISO 8601 时长,不为负)、`x-number`(文字换算成数字是个有限的数)。`structure.json` 声明流程结构规则,`structure.mjs` 负责跨节点检查。
9
9
  - **Agent 接入扩展**(`schemas/agent/`、`agent.mjs`、`docs/agent-protocol.md`):Coda 怎样用 ACP 接 Agent,以及 Coda 加的扩展(会话指令、插话、`_coda/wait`、执行失败、工具调用的审核、配置探测)。
10
10
 
package/docs/protocol.md CHANGED
@@ -7,7 +7,7 @@
7
7
  - 协议定义文档的 JSON Schema 与校验:`schemas/protocol/v1/definition.json`、`@coda-console/protocol/protocol`;
8
8
  - 统一调用信封:`schemas/protocol/v1/request.json`、`response.json`;
9
9
  - 五个核心协议:`coda/run@1`、`coda/agent@1`、`coda/session@1`、`coda/environment@1`、`coda/card@1`;
10
- - 插件清单里的 `contributes.protocols` 与 `contributes.protocolImplementations`。
10
+ - 插件清单里的 `contributes.protocols`、`contributes.protocolImplementations`、`contributes.records` 与 `contributes.self`。
11
11
 
12
12
  `executor` 与 `execution` 两个旧对象协议和 `@coda-console/protocol/executor` 在 3.0.0 删除。
13
13
 
@@ -71,11 +71,12 @@ protocolShortName('coda/run@1'); // 'coda-1run-21'
71
71
  | `title` | 给人看的名字,可选 |
72
72
  | `description` | 一句话说明,可选 |
73
73
  | `plural` | 复数名(见「复数名」),可选;不写就是短名 |
74
- | `spec` | 记录的 spec 的 JSON Schema,必填 |
75
- | `status` | 记录的 status 的 JSON Schema,必填 |
76
- | `operations` | 操作名 → `{ description?, input, result, safe?, idempotent?, timeout? }` 或 `{ description?, input, run: true, timeout? }`,必填;不定义操作写 `{}` |
74
+ | `spec` | 记录的 spec 的 JSON Schema;没写 `events` 时必填,与 `status` 一起写 |
75
+ | `status` | 记录的 status 的 JSON Schema;没写 `events` 时必填,与 `spec` 一起写 |
76
+ | `operations` | 操作名 → `{ description?, input, result, safe?, idempotent?, timeout? }` 或 `{ description?, input, run: true, timeout? }`;没写 `events` 时必填,不定义操作写 `{}` |
77
77
  | `streams` | 流名 → `{ description?, input }`,可选(见「只读记录流」) |
78
- | `permissions` | 权限组名 → `{ title, trial?, operations?, streams?, read? }`,可选(见「权限组」) |
78
+ | `events` | 事件名 → `{ description?, payload, starts?, ends? }`,可选(见「事件」) |
79
+ | `permissions` | 权限组名 → `{ title, trial?, operations?, streams?, events?, read? }`,可选(见「权限组」) |
79
80
 
80
81
  `spec`、`status`、`input`、`result` 都是 JSON Schema draft 2020-12,只引用自己文档里的 `#/$defs/...`,不引用别的文件。`input` 必有。操作的输出二选一:
81
82
 
@@ -200,9 +201,37 @@ invocationResponseProblem(response); // 恰好有 result 或 run 之一
200
201
  }
201
202
  ```
202
203
 
204
+ ### 事件
205
+
206
+ 定义用顶层 `events` 声明协议的事件,键是事件名,值是:
207
+
208
+ | 字段 | 含义 |
209
+ | --- | --- |
210
+ | `description` | 可选,一句话说明 |
211
+ | `payload` | 必填,载荷的 JSON Schema(draft 2020-12,`definitionProblem` 查它能编译);顶层不能定义 `coda`,那是留给宿主的 |
212
+ | `starts` | 可选,布尔,默认 `true`;`false` 表示这个事件不开新流程,只交给已在跟这个主体的运行 |
213
+ | `ends` | 可选,布尔;`true` 表示这个事件结束它的主体(如 PR 关闭) |
214
+
215
+ 事件名是点分的全名:「域.名」,小写字母、数字和「_」,以字母开头,至多 100 个字符,至少两段,如 `pull_request.closed`;至多 50 条。`coda/` 以外的协议不能用 `workflow`、`session`、`user` 做域,那是 Coda 自己的。事件的版本就是所在协议的大版本,没有另外的版本号。事件名在本机是否唯一、谁能发、怎样投递是宿主的事,本包只管声明的形状。
216
+
217
+ 只有事件的协议:写了 `events` 的定义可以不写 `spec`、`status`、`operations`,那样的协议没有记录;`spec` 与 `status` 要么都写,要么都不写。判断一份定义有没有记录用 `protocolHasRecords(definition)`(有 `spec` 就有记录),宿主据此不把只有事件的协议列成记录种类。操作、流、读记录都按记录寻址,所以没有记录的协议不能有非空的 `operations`、非空的 `streams`,也不能有 `read: true` 的组(`definitionProblem` 查);要这些就必须同时写 `spec` 和 `status`。没写 `events` 的定义,`spec`、`status`、`operations` 仍必填。
218
+
219
+ ```json
220
+ {
221
+ "name": "my-plugin/pulls@1",
222
+ "events": {
223
+ "pull_request.opened": {"payload": {"type": "object", "properties": {"number": {"type": "integer"}}, "required": ["number"]}},
224
+ "pull_request.closed": {"payload": {"type": "object"}, "starts": false, "ends": true}
225
+ },
226
+ "permissions": {
227
+ "notify": {"title": "收到 PR 的打开与关闭通知", "trial": true, "events": ["pull_request.opened", "pull_request.closed"]}
228
+ }
229
+ }
230
+ ```
231
+
203
232
  ### 权限组
204
233
 
205
- 一切对插件开放的能力都以协议提供,权限挂在协议上:定义用 `permissions` 把自己的操作、流与记录的读取分成组,插件在清单的 `uses` 里声明用到哪些组,没声明的调用被宿主拒绝。组名的写法同操作名(小写字母开头,只用小写字母、数字和「-」,至多 64 个字符)。每组:
234
+ 一切对插件开放的能力都以协议提供,权限挂在协议上:定义用 `permissions` 把自己的操作、流、事件与记录的读取分成组,插件在清单的 `uses` 里声明用到哪些组,没声明的调用被宿主拒绝。组名的写法同操作名(小写字母开头,只用小写字母、数字和「-」,至多 64 个字符)。每组:
206
235
 
207
236
  | 字段 | 含义 |
208
237
  | --- | --- |
@@ -210,9 +239,10 @@ invocationResponseProblem(response); // 恰好有 result 或 run 之一
210
239
  | `trial` | 可选,布尔,这组是否试行;只影响展示,不影响能否调用 |
211
240
  | `operations` | 可选,不重复的操作名数组,归这组管的操作 |
212
241
  | `streams` | 可选,不重复的流名数组,归这组管的流 |
242
+ | `events` | 可选,至少一项、不重复的事件名数组,归这组管的事件;插件在 `uses` 里写了这组,就是订了组里全部事件 |
213
243
  | `read` | 可选,布尔,管不管读这个协议的记录(get、list、watch)。写记录只许实现方,不分给插件 |
214
244
 
215
- 不许有别的键。写了 `permissions` 的定义还要满足(`definitionProblem` 查):每个操作、每条流最多属于一组;组里列的操作都在 `operations` 里,列的流都在 `streams` 里;每组至少管一样东西(有操作、有流,或 `read` 为 true)。**不在任何组里的操作和流不对插件开放**,只有 Coda 自己和它的客户端调;不写 `permissions` 的定义照旧合法,那时整个协议都不对插件开放。
245
+ 不许有别的键。写了 `permissions` 的定义还要满足(`definitionProblem` 查):每个操作、每条流、每个事件最多属于一组;组里列的操作都在 `operations` 里,列的流都在 `streams` 里,列的事件都在 `events` 里;每组至少管一样东西(有操作、有流、有事件,或 `read` 为 true);有 `events` 的组只放事件:`events` 至少一项,且不能再出现 `operations`、`streams`、`read` 这几个键,写成 `[]` 或 `false` 也不行。**不在任何组里的操作、流和事件不对插件开放**,只有 Coda 自己和它的客户端调;不写 `permissions` 的定义照旧合法,那时整个协议都不对插件开放。
216
246
 
217
247
  ```json
218
248
  "permissions": {
@@ -263,6 +293,25 @@ invocationResponseProblem(response); // 恰好有 result 或 run 之一
263
293
 
264
294
  同一个协议在数组里只能出现一次,写成文本还是对象都算。
265
295
 
296
+ `contributes.records` 声明哪些种类的记录由命令给出,每种一项,至多 20 项:
297
+
298
+ ```json
299
+ "records": [
300
+ {"kind": "my-plugin/task@1", "command": "bin/tasks.sh", "every": "PT5M"}
301
+ ]
302
+ ```
303
+
304
+ - `kind`:协议名;同一种类只能出现一次。须是这个插件 `protocolImplementations` 里的种类,由 Coda 装载时查。
305
+ - `command`:插件目录里的文件(相对路径,不离开插件目录),写法同 `commands`。
306
+ - `every`:可选,定时重跑的间隔,ISO 8601 风格:周 `W`、天 `D`、时 `H`、分 `M`、秒 `S`(如 `PT5M`、`PT90S`、`P1W1D`),只有秒能带小数点,不收年、月;不短于 `PT1M`;不写就不定时。下限 schema 表达不了,由 Coda 判。
307
+ - 不许别的键。命令怎么跑、打印什么由 Coda 的文档讲,这里不重述。
308
+
309
+ `contributes.self` 声明由命令回答的「只给自己用的查询」:名字(格式同操作名)→ 插件目录里的文件(相对路径,不离开插件目录),至多 50 个。
310
+
311
+ ```json
312
+ "self": {"operation": "bin/self-operation.sh"}
313
+ ```
314
+
266
315
  插件用到别的协议的权限,写在清单根的 `uses`,不放进 `contributes`(Coda 把不认识的 `contributes` 键当扩展点,旧版本会因此拒装)。键是带大版本的协议名,值是权限组名的数组,至少一项、不重复:
267
316
 
268
317
  ```json
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coda-console/protocol",
3
- "version": "6.13.0",
3
+ "version": "6.15.0",
4
4
  "description": "Coda 的协议:插件清单的类型(JSON Schema)、通用校验器、协议定义与调用信封、流程方言与 Agent 接入协议",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/protocol.mjs CHANGED
@@ -74,14 +74,15 @@ function schemaProblem(schema, where) {
74
74
  }
75
75
  }
76
76
 
77
- // Permission groups, when written: every group names real operations and streams and covers something, every operation and
78
- // stream sits in at most one group. An operation or stream in no group is not open to plugins.
79
- function permissionsProblem({ operations, streams = {}, permissions }, where) {
77
+ // Permission groups, when written: every group names real operations, streams and events and covers something, every
78
+ // operation, stream and event sits in at most one group. One that is in no group is not open to plugins. A group with events
79
+ // holds only events: subscribing to the group is subscribing to all of them.
80
+ function permissionsProblem({ operations = {}, streams = {}, events = {}, permissions }, where) {
80
81
  if (permissions === undefined) return null;
81
- const owner = { operations: new Map(), streams: new Map() };
82
- const things = { operations: ['操作', operations], streams: ['流', streams] };
82
+ const owner = { operations: new Map(), streams: new Map(), events: new Map() };
83
+ const things = { operations: ['操作', operations], streams: ['流', streams], events: ['事件', events] };
83
84
  for (const [group, entry] of Object.entries(permissions)) {
84
- for (const key of ['operations', 'streams']) {
85
+ for (const key of ['operations', 'streams', 'events']) {
85
86
  const [label, defined] = things[key];
86
87
  for (const name of entry[key] ?? []) {
87
88
  if (!Object.hasOwn(defined, name)) return `${where}.permissions.${group}.${key}:没有${label}「${name}」`;
@@ -89,11 +90,42 @@ function permissionsProblem({ operations, streams = {}, permissions }, where) {
89
90
  owner[key].set(name, group);
90
91
  }
91
92
  }
92
- if (!entry.operations?.length && !entry.streams?.length && entry.read !== true) return `${where}.permissions.${group}:至少管一样东西:写 operations 或 streams,或 read 为 true`;
93
+ if (entry.events !== undefined && ['operations', 'streams', 'read'].some(key => Object.hasOwn(entry, key)))
94
+ return `${where}.permissions.${group}:有 events 的组只放事件,不能再写 operations、streams、read`;
95
+ if (!entry.operations?.length && !entry.streams?.length && !entry.events?.length && entry.read !== true)
96
+ return `${where}.permissions.${group}:至少管一样东西:写 operations、streams 或 events,或 read 为 true`;
93
97
  }
94
98
  return null;
95
99
  }
96
100
 
101
+ // Events: a protocol outside `coda/` may not use Coda's own event domains, and a payload leaves the top-level `coda` to the host.
102
+ const CODA_EVENT_DOMAINS = ['workflow', 'session', 'user'];
103
+ function eventsProblem({ events = {} }, namespace, where) {
104
+ for (const [name, { payload }] of Object.entries(events)) {
105
+ if (namespace !== 'coda' && CODA_EVENT_DOMAINS.includes(name.slice(0, name.indexOf('.'))))
106
+ return `${where}.events.${name}:事件名的域不能是 ${CODA_EVENT_DOMAINS.join('、')},那是 Coda 自己的`;
107
+ if (payload !== null && typeof payload === 'object' && Object.hasOwn(payload.properties ?? {}, 'coda'))
108
+ return `${where}.events.${name}.payload:顶层不能定义 coda,那是留给宿主的`;
109
+ }
110
+ return null;
111
+ }
112
+
113
+ // A definition with no `spec` has no records (a protocol of events only): it is no record kind. Operations, streams and
114
+ // reads are addressed by record, so such a protocol has none.
115
+ export function protocolHasRecords(definition) {
116
+ return definition.spec !== undefined;
117
+ }
118
+
119
+ function recordsProblem(definition, where) {
120
+ if (protocolHasRecords(definition)) return null;
121
+ const { operations = {}, streams = {}, permissions = {} } = definition;
122
+ const without = '没有记录的协议(没写 spec、status)不能有';
123
+ if (Object.keys(operations).length) return `${where}.operations:${without}操作,要定义操作须同时写 spec 和 status`;
124
+ if (Object.keys(streams).length) return `${where}.streams:${without}流,要定义流须同时写 spec 和 status`;
125
+ const reading = Object.entries(permissions).find(([, group]) => group.read === true);
126
+ return reading ? `${where}.permissions.${reading[0]}.read:${without}读记录的权限,要它须同时写 spec 和 status` : null;
127
+ }
128
+
97
129
  // `namespace`: the plugin ID a manifest owns definitions under; omitted for core definitions and standalone checks.
98
130
  export function definitionProblem(definition, { namespace = null, where = '协议定义' } = {}) {
99
131
  if (!definitionCheck(definition))
@@ -103,14 +135,19 @@ export function definitionProblem(definition, { namespace = null, where = '协
103
135
  if (!parsed) return `${where}.name:${NAME_PROBLEM}`;
104
136
  if (namespace !== null && parsed.namespace !== namespace)
105
137
  return `${where}.name:命名空间须为「${namespace}」,不能在自己的命名空间之外定义协议`;
138
+ const eventing = eventsProblem(definition, parsed.namespace, where);
139
+ if (eventing) return eventing;
140
+ const recording = recordsProblem(definition, where);
141
+ if (recording) return recording;
106
142
  const grouping = permissionsProblem(definition, where);
107
143
  if (grouping) return grouping;
108
- const parts = [['spec', definition.spec], ['status', definition.status]];
109
- for (const [operation, value] of Object.entries(definition.operations)) {
144
+ const parts = protocolHasRecords(definition) ? [['spec', definition.spec], ['status', definition.status]] : [];
145
+ for (const [operation, value] of Object.entries(definition.operations ?? {})) {
110
146
  parts.push([`operations.${operation}.input`, value.input]);
111
147
  if (value.result !== undefined) parts.push([`operations.${operation}.result`, value.result]);
112
148
  }
113
149
  for (const [stream, value] of Object.entries(definition.streams ?? {})) parts.push([`streams.${stream}.input`, value.input]);
150
+ for (const [event, value] of Object.entries(definition.events ?? {})) parts.push([`events.${event}.payload`, value.payload]);
114
151
  for (const [key, schema] of parts) {
115
152
  const problem = schemaProblem(schema, `${where}.${key}`);
116
153
  if (problem) return problem;
@@ -38,7 +38,9 @@
38
38
  "executors",
39
39
  "protocols",
40
40
  "protocolImplementations",
41
- "composerModes"
41
+ "composerModes",
42
+ "records",
43
+ "self"
42
44
  ]
43
45
  }
44
46
  },
@@ -59,10 +59,11 @@
59
59
  "setup": {"$ref": "setup.json"},
60
60
  "environment": {"$ref": "environment.json"},
61
61
  "path": {"$ref": "path.json"},
62
- "endpoints": {"$ref": "endpoints.json"},
63
62
  "views": {"$ref": "views.json"},
64
63
  "protocols": {"$ref": "protocols.json"},
65
- "protocolImplementations": {"$ref": "protocol-implementations.json"}
64
+ "protocolImplementations": {"$ref": "protocol-implementations.json"},
65
+ "records": {"$ref": "records.json"},
66
+ "self": {"$ref": "self.json"}
66
67
  },
67
68
  "propertyNames": {"description": "contributes 的键:字母和数字,以小写字母开头", "pattern": "^[a-z][A-Za-z0-9]{0,39}$"}
68
69
  }
@@ -0,0 +1,23 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://coda.pub/schemas/plugin/v2/records.json",
4
+ "title": "contributes.records:由命令给出的记录",
5
+ "description": "每种记录一项 { kind, command, every? }:kind 是协议名,须是这个插件 protocolImplementations 里的种类(装载时由 Coda 查),同一种类只能出现一次;command 是插件目录里的文件(写法同 protocolImplementations 的 commands),Coda 以插件身份运行它,打印的全集是这种记录的全部记录;every 是定时重跑的间隔,写法与资源面板的 maxAge 相同(ISO 8601 风格:周 W、天 D、时 H、分 M、秒 S,只有秒能带小数点,不收年、月),不短于 PT1M(下限 schema 表达不了,由 Coda 判),不写就不定时。",
6
+ "type": "array",
7
+ "maxItems": 20,
8
+ "x-unique-by": ["kind"],
9
+ "items": {
10
+ "type": "object",
11
+ "properties": {
12
+ "kind": {"$ref": "https://coda.pub/schemas/protocol/v1/definition.json#/$defs/name"},
13
+ "command": {"description": "插件目录里的文件(相对路径,不离开插件目录)", "type": "string", "x-file": {"type": "file"}},
14
+ "every": {
15
+ "description": "ISO 8601 风格的时长,如 PT5M、P1W1D:周 W、天 D、时 H、分 M、秒 S,只有秒能带小数点,不收年、月;不短于 PT1M,下限由 Coda 判",
16
+ "type": "string",
17
+ "pattern": "^P(?!$)(?:\\d+W)?(?:\\d+D)?(?:T(?=\\d)(?:\\d+H)?(?:\\d+M)?(?:\\d+(?:\\.\\d+)?S)?)?$"
18
+ }
19
+ },
20
+ "required": ["kind", "command"],
21
+ "additionalProperties": false
22
+ }
23
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://coda.pub/schemas/plugin/v2/self.json",
4
+ "title": "contributes.self:只给自己用的查询",
5
+ "description": "名字 → 插件目录里的文件(相对路径,不离开插件目录),由命令回答插件自己的页面与进程的 self 查询;同一个名字写了命令就交给命令,没有再找模块。至多 50 个。",
6
+ "type": "object",
7
+ "maxProperties": 50,
8
+ "propertyNames": {"$ref": "https://coda.pub/schemas/protocol/v1/definition.json#/$defs/operationName"},
9
+ "additionalProperties": {"type": "string", "x-file": {"type": "file"}}
10
+ }
@@ -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,可改状态但重发做同一件事的立即结果可标 idempotent: true。操作可写 timeout 给出默认期限。可写 streams 声明只读的记录流;可写 permissions 把操作、流与记录的读取分成权限组,供插件在清单里按组申请。",
5
+ "description": "一个协议的名字、记录格式与操作。名字是「命名空间/名字@大版本」;spec、status 与每个操作的输入、结果都是 JSON Schema(draft 2020-12),只引用定义自身的 $defs。操作要么立刻返回结果(result),要么返回运行引用(run: true);只读的立即结果可标 safe: true,可改状态但重发做同一件事的立即结果可标 idempotent: true。操作可写 timeout 给出默认期限。可写 streams 声明只读的记录流;可写 events 声明协议的事件;可写 permissions 把操作、流、事件与记录的读取分成权限组,供插件在清单里按组申请。写了 events 的定义可以不写 spec、status、operations(没有记录的协议;它不能有非空的 operations、streams,也不能有 read 为真的组);没写 events 时三者必填。",
6
6
  "type": "object",
7
7
  "properties": {
8
8
  "name": {"$ref": "#/$defs/name"},
@@ -25,15 +25,31 @@
25
25
  "propertyNames": {"$ref": "#/$defs/operationName"},
26
26
  "additionalProperties": {"$ref": "#/$defs/stream"}
27
27
  },
28
+ "events": {
29
+ "description": "事件:事件名 → 载荷格式与流程里的意思。事件名在本机唯一;事件的版本就是这个协议的大版本。载荷顶层不能定义 coda(留给宿主)、事件名的域不能是 workflow、session、user(coda 命名空间除外)、载荷须是可用的 JSON Schema,这些由 definitionProblem 查",
30
+ "type": "object",
31
+ "minProperties": 1,
32
+ "maxProperties": 50,
33
+ "propertyNames": {"$ref": "#/$defs/eventName"},
34
+ "additionalProperties": {"$ref": "#/$defs/event"}
35
+ },
28
36
  "permissions": {
29
- "description": "权限组:组名 → 一组对插件开放的能力。插件在清单的 uses 里按组申请,没申请的调用被宿主拒绝。每个操作、每条流最多属于一组,组里列的操作与流都存在,每组至少管一样东西(有操作、有流,或 read 为真);这些由 definitionProblem 查。不在任何组里的操作和流不对插件开放,只有 Coda 自己和它的客户端调;不写 permissions 就是整个协议都不分组",
37
+ "description": "权限组:组名 → 一组对插件开放的能力。插件在清单的 uses 里按组申请,没申请的调用被宿主拒绝。每个操作、每条流、每个事件最多属于一组,组里列的操作、流与事件都存在,每组至少管一样东西(有操作、有流、有事件,或 read 为真),有 events 的组不能再有 operations、streams、read 这几个键;没写 spec、status 的协议不能有非空的 operations、streams,也不能有 read 为真的组;这些由 definitionProblem 查。不在任何组里的操作、流和事件不对插件开放,只有 Coda 自己和它的客户端调;不写 permissions 就是整个协议都不分组",
30
38
  "type": "object",
31
39
  "maxProperties": 50,
32
40
  "propertyNames": {"$ref": "#/$defs/operationName"},
33
41
  "additionalProperties": {"$ref": "#/$defs/permissionGroup"}
34
42
  }
35
43
  },
36
- "required": ["name", "spec", "status", "operations"],
44
+ "required": ["name"],
45
+ "allOf": [
46
+ {
47
+ "if": {"not": {"required": ["events"]}},
48
+ "then": {"required": ["spec", "status", "operations"], "properties": {"spec": {"$ref": "#/properties/spec"}, "status": {"$ref": "#/properties/status"}, "operations": {"$ref": "#/properties/operations"}}}
49
+ },
50
+ {"if": {"required": ["spec"]}, "then": {"required": ["status"], "properties": {"status": {"$ref": "#/properties/status"}}}},
51
+ {"if": {"required": ["status"]}, "then": {"required": ["spec"], "properties": {"spec": {"$ref": "#/properties/spec"}}}}
52
+ ],
37
53
  "additionalProperties": false,
38
54
  "$defs": {
39
55
  "name": {
@@ -52,6 +68,11 @@
52
68
  "type": "string",
53
69
  "pattern": "^[a-z][a-z0-9-]{0,63}$"
54
70
  },
71
+ "eventName": {
72
+ "description": "事件名是「<域>.<名>」:小写字母、数字和「_」,以字母开头,不超过 100 个字符,如 pull_request.closed",
73
+ "type": "string",
74
+ "pattern": "^(?=.{3,100}$)[a-z][a-z0-9_]*(\\.[a-z][a-z0-9_]*)+$"
75
+ },
55
76
  "schema": {
56
77
  "description": "JSON Schema(draft 2020-12)",
57
78
  "type": ["object", "boolean"]
@@ -59,18 +80,31 @@
59
80
  "title": {"description": "给人看的名字", "type": "string", "maxLength": 100, "pattern": "^(?=.*\\S)[^\\u0000\\r\\n]*$"},
60
81
  "description": {"description": "一句话说明", "type": "string", "maxLength": 1000, "pattern": "^(?=.*\\S)[^\\u0000\\r\\n]*$"},
61
82
  "permissionGroup": {
62
- "description": "一个权限组:title 是给用户看的一句话,说这组让插件能做什么;trial 标这组是试行的;operations 是归这组管的操作;streams 是归这组管的流;read 管不管读这个协议的记录(get、list、watch);写记录只许实现方,不分给插件",
83
+ "description": "一个权限组:title 是给用户看的一句话,说这组让插件能做什么;trial 标这组是试行的;operations 是归这组管的操作;streams 是归这组管的流;events 是归这组管的事件,插件在 uses 里写了这组就是订了组里全部事件,有 events 的组只放事件(不能再写 operations、streams、read 这几个键);read 管不管读这个协议的记录(get、list、watch);写记录只许实现方,不分给插件",
63
84
  "type": "object",
64
85
  "properties": {
65
86
  "title": {"$ref": "#/$defs/title"},
66
87
  "trial": {"type": "boolean", "description": "是否试行:只影响展示,不影响能否调用"},
67
88
  "operations": {"type": "array", "maxItems": 50, "uniqueItems": true, "items": {"$ref": "#/$defs/operationName"}, "description": "归这组管的操作名,不重复"},
68
89
  "streams": {"type": "array", "maxItems": 50, "uniqueItems": true, "items": {"$ref": "#/$defs/operationName"}, "description": "归这组管的流名,不重复"},
90
+ "events": {"type": "array", "minItems": 1, "maxItems": 50, "uniqueItems": true, "items": {"$ref": "#/$defs/eventName"}, "description": "归这组管的事件名,不重复;有 events 的组不能再有 operations、streams、read 这几个键"},
69
91
  "read": {"type": "boolean", "description": "管不管读这个协议的记录(get、list、watch)"}
70
92
  },
71
93
  "required": ["title"],
72
94
  "additionalProperties": false
73
95
  },
96
+ "event": {
97
+ "description": "事件:payload 是载荷的 JSON Schema;starts 默认 true,写 false 表示这个事件不开新流程,只交给已在跟这个主体的运行;ends 为 true 表示这个事件结束它的主体(如 PR 关闭)",
98
+ "type": "object",
99
+ "properties": {
100
+ "description": {"$ref": "#/$defs/description"},
101
+ "payload": {"$ref": "#/$defs/schema"},
102
+ "starts": {"type": "boolean"},
103
+ "ends": {"type": "boolean"}
104
+ },
105
+ "required": ["payload"],
106
+ "additionalProperties": false
107
+ },
74
108
  "stream": {
75
109
  "description": "只读记录流:input 是参数的 JSON Schema,参数都来自查询串,取值都是字符串;description 是一句话说明",
76
110
  "type": "object",
@@ -1,18 +0,0 @@
1
- {
2
- "$schema": "https://json-schema.org/draft/2020-12/schema",
3
- "$id": "https://coda.pub/schemas/plugin/v2/endpoints.json",
4
- "title": "contributes.endpoints:客户端发给插件的请求",
5
- "description": "id → 回答它的一行命令(请求从标准输入给)。",
6
- "type": "array",
7
- "maxItems": 20,
8
- "items": {
9
- "type": "object",
10
- "properties": {
11
- "id": {"$ref": "common.json#/$defs/id"},
12
- "command": {"$ref": "common.json#/$defs/command"}
13
- },
14
- "required": ["id", "command"],
15
- "additionalProperties": false
16
- },
17
- "x-unique-by": ["id"]
18
- }
@@ -1,23 +0,0 @@
1
- {
2
- "$schema": "https://json-schema.org/draft/2020-12/schema",
3
- "$id": "https://coda.pub/schemas/plugin/v2/events.json",
4
- "title": "contributes.events:插件发布的事件",
5
- "description": "流程订阅的事件名;starts(默认开)是否开始订阅它的流程,否则只送到在跑的运行;ends 是否结束它的主体(如 PR 关闭)。",
6
- "type": "array",
7
- "maxItems": 50,
8
- "items": {
9
- "type": "object",
10
- "properties": {
11
- "name": {
12
- "description": "事件名是「<域>.<名>」:小写字母、数字和「_」,以字母开头,不超过 100 个字符;域不能是 Coda 自己的 workflow、session、user",
13
- "type": "string",
14
- "pattern": "^(?!(workflow|session|user)\\.)(?=.{3,100}$)[a-z][a-z0-9_]*(\\.[a-z][a-z0-9_]*)+$"
15
- },
16
- "starts": {"type": "boolean"},
17
- "ends": {"type": "boolean"}
18
- },
19
- "required": ["name"],
20
- "additionalProperties": false
21
- },
22
- "x-unique-by": ["name"]
23
- }