@coda-console/protocol 6.23.0 → 6.25.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,20 @@
2
2
 
3
3
  版本规矩见 README 的[「版本」一节](README.md#版本)。协议自己的大版本写在协议名里(如 `coda/run@1`),与 npm 包版本、插件清单的 v2 独立;更早的版本见 git 历史。
4
4
 
5
+ ## 6.25.0 — 2026-11-24
6
+
7
+ - 这是在 6.24.0 之上的次版本。依据 README「版本」一节:协议定义是内部面,只加可选的键和类型字段,升次版本;`stable/` 不动,清单 schema 没改。三处都是只增:6.24.0 下合法的定义仍合法。
8
+ - 协议定义顶层加可选键 `resource`:`{ columns, symbol? }`,展示声明。写了它的种类成为可列出、可引用的资源;`columns` 非空、不重复、至多 8 项,每项是 `spec` 的顶层字段名,列表里按此顺序显示,第一列是名字,所在机器那一列由宿主自己加;`symbol` 是客户端画的 SF Symbol 名。`definitionProblem` 另查:每一列须是 `spec.properties` 里声明的标量字段(`type` 为 string、number、integer、boolean 之一,数组形式去掉 `"null"` 后只剩一种标量也算;有 `enum` 而无 `type` 不算);没写 `spec` 的定义不能写 `resource`。
9
+ - 权限组加布尔键 `sessions`:为 true 表示这组里的操作与流开放给会话(会话经工具调用),不只是开放给插件。`definitionProblem` 另查两条:有 `events` 的组不能出现 `sessions`(写成 `false` 也不行);写了 `sessions` 的组(`true`、`false` 都算)须至少有一个操作或一条流,只有 `read` 不够。
10
+ - `./types`:`Caller` 与 `StreamCaller` 的各分支都可再带可选的 `machine`(发起调用的那台后端的 ID)、`plugin`(以哪个插件的身份发起)、`parent`(`{ kind, id }`,直接的发起方,不透明引用),共用新类型 `CallerOrigin`;已有字段与分支的形状不变。`machine` 由接收方按信封来源自己核验;`plugin`、`parent` 与页面打开者(`device`、`name`、`local`)是来源后端转述的,接收方无法独立核验;只给直接的调用方,不给整条父链。`Context.caller` 的注释同步:Coda 填写时,查询与插件、会话发起的调用也带。
11
+ - 需要你知道:`Caller`、`StreamCaller` 的收窄是类型层面的变化(运行时的形状只增不改)。`machine`、`plugin` 现在可能出现在任何分支上,用 `in` 判断分支的 TypeScript 代码要改成按值判断,`in` 收窄后这两个字段的类型是 `string | undefined`。改写示例分两件事。一,字段有没有值:`caller.machine !== undefined`、`s.plugin !== undefined`,这只是说有没有值,不等于原来的哪个分支。二,判断跨机来源:原来的 `'machine' in caller` 写成 `caller.machine !== undefined && caller.machine !== selfId`(`selfId` 是本机自己的 ID),它只说明「这次调用是别台后端发来的」,不说明是不是页面发起的:查询、插件与会话发起的调用也带 `machine`。现有调用方字段不能在所有情况下判定是不是页面发起的:别台本机客户端打开页面转来的调用只带 `machine`(不带 `local`),与别台没有 `plugin`、`parent` 的其他调用形状相同;要靠「是不是页面」做判断的插件不要只依赖这几个字段。有 `device`(或 `local: true`)时才说明调用前面有个页面打开者;原来的 `'plugin' in s`(`StreamCaller` 的插件分支:本机上的插件来问,本机按令牌核验过)写成 `s.plugin !== undefined && !('device' in s) && !('local' in s)`,因为别台转来的 `{ device, role: 'backend', plugin }` 的 `plugin` 是转述的,只看有没有值会把它当成本机核验过的(类型上联合没有公共的 `device`,所以写 `'device' in s`,不能写 `s.device === undefined`)。`'local' in caller`、`'device' in caller`、`'role' in caller` 照旧可用。两个字段在各分支上含义一致:`machine` 一律是发起调用的那台后端的 ID(接收方按信封核验);`StreamCaller` 的 `plugin` 是以哪个插件的身份来问,最后一跳是本机时由本机核验,最后一跳是别台后端(`role: 'backend'`)时是转述的、本机无法核验。
12
+
13
+ ## 6.24.0 — 2026-11-23
14
+
15
+ - 这是在 6.23.0 之上的次版本。依据 README「版本」一节:协议定义是内部面,放宽限制和新增路径种类,升次版本;`stable/` 不动,清单 schema 没改。`coda/session@1` 只加不删,协议名不变:6.23.0 里有效的记录和 `read` 结果照旧有效。
16
+ - 指针 `blob` 的 `path` 新增第三类 `content/<sha256>`:内容寻址,`<sha256>` 是 64 位小写十六进制,后面不带别的段;大写、63 或 65 位、非十六进制字符、百分号编码、多余的斜杠或段都不收。供后端内容寻址缓存、头像与跨尾网小图复用同一套 `BlobPointer`。媒体 part 的 `blob`、`preview` 与头像的指针都照此放宽。
17
+ - `./blob` 的 `BLOB_PATHS` 追加第 3 行 `content`,`parseBlobPath` 对这类路径给 `{ kind: 'content', sha256 }`;schema 的 `pattern` 与它判得一样,`test/blob.test.mjs` 守着。已有两类路径的判断与返回不变。
18
+
5
19
  ## 6.23.0 — 2026-11-23
6
20
 
7
21
  - 这是在 6.22.0 之上的次版本。依据 README「版本」一节:新增导出是内部面,升次版本;`stable/` 不动,清单与协议的 schema 没改。这一版把 `@coda-console/plugin-check` 里不依赖 Coda 宿主运行时的两样迁进本包,供 Coda 与各插件仓库直接依赖。
package/README.md CHANGED
@@ -90,6 +90,7 @@ import { parseBlobPath } from '@coda-console/protocol/blob';
90
90
 
91
91
  // 指针的 path 是哪一类、各部分是什么;不认得给 null。不依赖 Node,浏览器里也能跑。
92
92
  parseBlobPath('plugins/wechat/services/media/a.jpg'); // { kind: 'service', plugin: 'wechat', service: 'media', path: 'a.jpg' }
93
+ parseBlobPath('content/' + 'f'.repeat(64)); // { kind: 'content', sha256: 'ffff…' },内容寻址:64 位小写十六进制 SHA-256
93
94
  ```
94
95
 
95
96
  ```js
package/blob.mjs CHANGED
@@ -5,6 +5,7 @@
5
5
  const MAX_LENGTH = 2048;
6
6
  const UUID = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
7
7
  const ID = '[a-z][a-z0-9-]{0,39}';
8
+ const SHA256 = '[0-9a-f]{64}';
8
9
  // What RFC 3986 lets a segment hold, less what a URL parser or a server would take for another path: `.` and `..`, written
9
10
  // plainly or encoded, and an encoded slash or backslash.
10
11
  const SEGMENT = "(?!(?:[.]|%2[eE]){1,2}(?:/|$))(?:[A-Za-z0-9._~!$&'()*+,;=:@-]|%(?!2[fF]|5[cC])[0-9A-Fa-f]{2})+";
@@ -12,11 +13,12 @@ const SEGMENT = "(?!(?:[.]|%2[eE]){1,2}(?:/|$))(?:[A-Za-z0-9._~!$&'()*+,;=:@-]|%
12
13
  export const BLOB_PATHS = Object.freeze([
13
14
  { kind: 'upload', pattern: `uploads/(?<upload>${UUID})(?<preview>/preview)?`, parts: ({ upload, preview }) => ({ upload, preview: preview !== undefined }) },
14
15
  { kind: 'service', pattern: `plugins/(?<plugin>${ID})/services/(?<service>${ID})/(?<path>${SEGMENT}(?:/${SEGMENT})*)`, parts: groups => ({ ...groups }) },
16
+ { kind: 'content', pattern: `content/(?<sha256>${SHA256})`, parts: ({ sha256 }) => ({ sha256 }) },
15
17
  ].map(row => Object.freeze(row)));
16
18
 
17
19
  const rows = BLOB_PATHS.map(({ kind, pattern, parts }) => ({ kind, parts, regexp: new RegExp(`^(?:${pattern})$`, 'u') }));
18
20
 
19
- // `{ kind: 'upload', upload, preview }`, `{ kind: 'service', plugin, service, path }`, or null for what is no such path.
21
+ // `{ kind: 'upload', upload, preview }`, `{ kind: 'service', plugin, service, path }`, `{ kind: 'content', sha256 }`, or null for what is no such path.
20
22
  export function parseBlobPath(path) {
21
23
  if (typeof path !== 'string' || path.length > MAX_LENGTH) return null;
22
24
  for (const { kind, parts, regexp } of rows) {
package/docs/protocol.md CHANGED
@@ -76,7 +76,8 @@ protocolShortName('coda/run@1'); // 'coda-1run-21'
76
76
  | `operations` | 操作名 → `{ description?, input, result, safe?, idempotent?, timeout? }` 或 `{ description?, input, run: true, timeout? }`;没写 `events` 时必填,不定义操作写 `{}` |
77
77
  | `streams` | 流名 → `{ description?, input, shape?, frames?, subprotocol? }`,可选(见「记录流」) |
78
78
  | `events` | 事件名 → `{ description?, payload, starts?, ends? }`,可选(见「事件」) |
79
- | `permissions` | 权限组名 → `{ title, trial?, operations?, streams?, events?, read? }`,可选(见「权限组」) |
79
+ | `resource` | 展示声明 `{ columns, symbol? }`,可选(见「资源展示」) |
80
+ | `permissions` | 权限组名 → `{ title, trial?, operations?, streams?, events?, read?, sessions? }`,可选(见「权限组」) |
80
81
 
81
82
  `spec`、`status`、`input`、`result` 都是 JSON Schema draft 2020-12,只引用自己文档里的 `#/$defs/...`,不引用别的文件。`input` 必有。操作的输出二选一:
82
83
 
@@ -273,6 +274,23 @@ const body = writeFrames(frames, { heartbeatMs: 15000 });
273
274
  }
274
275
  ```
275
276
 
277
+ ### 资源展示
278
+
279
+ 定义可以写 `resource`,声明这个种类在资源列表里怎么展示:
280
+
281
+ ```json
282
+ "resource": {"columns": ["name", "region", "port"], "symbol": "server.rack"}
283
+ ```
284
+
285
+ | 字段 | 含义 |
286
+ | --- | --- |
287
+ | `columns` | 必填,非空、不重复、至多 8 项的字符串数组,每项是 `spec` 的顶层字段名(1–64 个字符) |
288
+ | `symbol` | 可选,客户端画的 SF Symbol 名,1–64 个字符 |
289
+
290
+ 不许有别的键。写了 `resource` 的种类成为可列出、可引用的资源:列表里每条按 `columns` 的顺序显示,第一列是名字;所在机器那一列由宿主自己加,不用写;列名取该字段 schema 的 `title`,没有就用字段名;只展示这些列与 `spec` 里带 `description` 的字段。不写就和没有这个声明一样。
291
+
292
+ `definitionProblem` 还查:每一列都必须是 `spec.properties` 里声明的字段,且该字段是标量:它的 schema 的 `type` 是 `string`、`number`、`integer` 或 `boolean` 之一(`type` 是数组时,去掉 `"null"` 后只剩一种标量也算;有 `enum` 而无 `type` 不算)。没写 `spec` 的定义(只有 `events` 的协议)不能写 `resource`。报错写明 `<位置>.resource.columns[i]` 与原因。
293
+
276
294
  ### 权限组
277
295
 
278
296
  一切对插件开放的能力都以协议提供,权限挂在协议上:定义用 `permissions` 把自己的操作、流、事件与记录的读取分成组,插件在清单的 `uses`(必需)或 `optionalUses`(可选)里声明用到哪些组,没声明的调用被宿主拒绝。组名的写法同操作名(小写字母开头,只用小写字母、数字和「-」,至多 64 个字符)。每组:
@@ -285,8 +303,9 @@ const body = writeFrames(frames, { heartbeatMs: 15000 });
285
303
  | `streams` | 可选,不重复的流名数组,归这组管的流 |
286
304
  | `events` | 可选,至少一项、不重复的事件名数组,归这组管的事件;插件在 `uses` 或 `optionalUses` 里写了这组,就是订了组里全部事件 |
287
305
  | `read` | 可选,布尔,管不管读这个协议的记录(get、list、watch)。写记录只许实现方,不分给插件 |
306
+ | `sessions` | 可选,布尔,为 true 表示这组里的操作与流开放给会话(会话经工具调用),不只是开放给插件 |
288
307
 
289
- 不许有别的键。写了 `permissions` 的定义还要满足(`definitionProblem` 查):每个操作、每条流、每个事件最多属于一组;组里列的操作都在 `operations` 里,列的流都在 `streams` 里,列的事件都在 `events` 里;每组至少管一样东西(有操作、有流、有事件,或 `read` 为 true);有 `events` 的组只放事件:`events` 至少一项,且不能再出现 `operations`、`streams`、`read` 这几个键,写成 `[]` 或 `false` 也不行。**不在任何组里的操作、流和事件不对插件开放**,只有 Coda 自己和它的客户端调;不写 `permissions` 的定义照旧合法,那时整个协议都不对插件开放。
308
+ 不许有别的键。写了 `permissions` 的定义还要满足(`definitionProblem` 查):每个操作、每条流、每个事件最多属于一组;组里列的操作都在 `operations` 里,列的流都在 `streams` 里,列的事件都在 `events` 里;每组至少管一样东西(有操作、有流、有事件,或 `read` 为 true);有 `events` 的组只放事件:`events` 至少一项,且不能再出现 `operations`、`streams`、`read`、`sessions` 这几个键,写成 `[]` 或 `false` 也不行;写了 `sessions` 的组(不论 true 还是 false)必须至少有一个操作或一条流,只有 `read` 不够。**不在任何组里的操作、流和事件不对插件开放**,只有 Coda 自己和它的客户端调;不写 `permissions` 的定义照旧合法,那时整个协议都不对插件开放。
290
309
 
291
310
  ```json
292
311
  "permissions": {
@@ -575,18 +594,19 @@ status:
575
594
  | `name`、`size`、`mime` | 文件名、字节数、MIME 类型,约束同媒体 part 的同名字段 |
576
595
  | `sha256` | 内容的 SHA-256,64 位小写十六进制;是写的一方的声明 |
577
596
 
578
- `path` 是 URL 路径的写法,已按段百分号编码,不带 `/v1/`、不带开头的 `/`、不带 `?` 和 `#`,至多 2048 个字符。只收两类:
597
+ `path` 是 URL 路径的写法,已按段百分号编码,不带 `/v1/`、不带开头的 `/`、不带 `?` 和 `#`,至多 2048 个字符。只收三类:
579
598
 
580
599
  | 写法 | 是什么 |
581
600
  | --- | --- |
582
601
  | `uploads/<上传 ID>`、`uploads/<上传 ID>/preview` | 那台上的一个上传,和它的预览图;上传 ID 是小写 UUID |
583
602
  | `plugins/<插件 ID>/services/<服务 ID>/<路径>` | 那台上一个插件登记的本机流式服务里的内容;两个 ID 都是小写字母、数字和「-」,以字母开头,至多 40 个字符,路径至少一段 |
603
+ | `content/<sha256>` | 内容寻址:按内容的 SHA-256 取,`<sha256>` 是 64 位小写十六进制,后面没有别的段;供后端内容寻址缓存、头像与跨尾网小图等复用同一套指针 |
584
604
 
585
605
  每一段非空,只用 RFC 3986 的路径字符(字母、数字、`-._~!$&'()*+,;=:@`)和百分号编码,百分号后面必须是两位十六进制;不是 `.`、`..`,也不是它们的百分号写法(URL 解析器会把 `%2e%2e` 当成 `..`);不许有编码过的斜杠和反斜杠(`%2f`、`%5c`,大小写都算)。
586
606
 
587
607
  **谁来取**:读的一方拿着指针找 Coda 取,不自己拼插件服务的地址;`owner` 不是它连着的那台时由 Coda 转。schema 只查写法,不核对 `owner`、插件和写的一方是不是同一个,取的时候由 Coda 判。
588
608
 
589
- 两类路径列在 `@coda-console/protocol/blob` 的表里,schema 的 `pattern` 和它判得一样:
609
+ 三类路径列在 `@coda-console/protocol/blob` 的表里,schema 的 `pattern` 和它判得一样:
590
610
 
591
611
  ```js
592
612
  import { parseBlobPath } from '@coda-console/protocol/blob';
@@ -595,6 +615,8 @@ parseBlobPath('uploads/1c79fe8d-d534-4cb8-a53a-64c03a117027/preview');
595
615
  // { kind: 'upload', upload: '1c79fe8d-d534-4cb8-a53a-64c03a117027', preview: true }
596
616
  parseBlobPath('plugins/wechat/services/media/msg/m7/IMG_0412.jpg');
597
617
  // { kind: 'service', plugin: 'wechat', service: 'media', path: 'msg/m7/IMG_0412.jpg' },path 是原样的,没有解码
618
+ parseBlobPath('content/0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef');
619
+ // { kind: 'content', sha256: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef' }
598
620
  parseBlobPath('plugins/wechat/services/media/../secret'); // null
599
621
  ```
600
622
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coda-console/protocol",
3
- "version": "6.23.0",
3
+ "version": "6.25.0",
4
4
  "description": "Coda 的协议:插件清单的类型(JSON Schema)、通用校验器、协议定义与调用信封、流程方言与 Agent 接入协议",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/plugin.d.mts CHANGED
@@ -61,11 +61,38 @@ export interface CallOptions {
61
61
  }
62
62
 
63
63
  /**
64
- * Who is before the page that made a doing call: Coda's own client on this machine, or a device, with its name if known.
65
- * A call forwarded to the backend that holds the record has no `local` but the `machine` it was made from (that one's ID),
66
- * a device kept as it is: so `{ machine }` or `{ machine, device, name? }`.
64
+ * What a call says of where it came from, beside who is before it. All optional, filled by Coda when it can; only the direct
65
+ * caller is given, never the whole chain of parents.
66
+ * - `machine`: the ID of the backend the call was made from. The receiver checks it itself against where the envelope came from.
67
+ * - `plugin`: the plugin the call is made as. Relayed by the originating backend: the receiver cannot check it on its own.
68
+ * - `parent`: whoever made the call directly, e.g. the session that did, an opaque reference. Relayed too, not checkable.
69
+ * The page's opener (`device`, `name`, `local`) is relayed by the originating backend as well.
70
+ *
71
+ * Read where a call came from by value, not by whether a property exists. Since 6.25.0 `machine` and `plugin` may be on any
72
+ * branch of `Caller` and `StreamCaller`, so `'machine' in caller` and `'plugin' in caller` no longer tell the branches apart,
73
+ * and TypeScript types the field as `string | undefined` after such a check. Tell this machine's own client (`local`), a
74
+ * device (`device`, and `role` of a `StreamCaller`) and a plugin by those fields and the values of `machine` and `plugin`.
75
+ * The two mean the same on every branch: `machine` is always the ID of the backend the call was made from, which the
76
+ * receiver checks against the envelope; a `StreamCaller`'s `plugin` is the plugin the call is made as, checked by this machine
77
+ * (by its token, or by Coda's own entry) when the last hop is this machine, relayed and not checkable by this machine when
78
+ * the last hop is another backend (`role: 'backend'`). So `s.plugin !== undefined` and `caller.machine !== undefined` say only
79
+ * that there is a value; the old branch of a plugin checked by this machine is `s.plugin !== undefined && !('device' in s) && !('local' in s)`,
80
+ * and a call relayed from another backend is `caller.machine !== undefined && caller.machine !== selfId`.
67
81
  */
68
- export type Caller = { local: true } | { device: string; name?: string } | { machine: string; device?: string; name?: string };
82
+ export interface CallerOrigin {
83
+ machine?: string;
84
+ plugin?: string;
85
+ parent?: { kind: string; id: string };
86
+ }
87
+
88
+ /**
89
+ * Who made a doing call or a query. When a page's opener is before it, `local: true` (Coda's own client on this machine) or
90
+ * `device` (with its `name` if known). A call forwarded from another backend has no `local` but the `machine` it was made from
91
+ * (that one's ID), a device kept as it is: so `{ machine }` or `{ machine, device, name? }`. A bare `{ machine }` may be a page
92
+ * opened by that machine's own client or any other call from that machine, so these fields alone do not tell whether a page
93
+ * made the call. Each form may also carry a `CallerOrigin`.
94
+ */
95
+ export type Caller = ({ local: true } | { device: string; name?: string } | { machine: string; device?: string; name?: string }) & CallerOrigin;
69
96
 
70
97
  /** What a handler of a call is given beside its input. A handler of a query, and `resume`, have no `progress` nor `ask`. */
71
98
  export interface Context {
@@ -98,8 +125,10 @@ export interface Context {
98
125
  */
99
126
  call?: Coda['call'];
100
127
  /**
101
- * A doing call's, made from a plugin's page: who is before that page, as Coda knows it, never as the page says. None
102
- * for a call a module (`coda.call`), a command (`coda call`) or a flow makes.
128
+ * Who made the call, as Coda knows it when it fills this in, never as the page says: who is before the page that made
129
+ * it, and for a query or a call a plugin or a session makes, where it came from (see `CallerOrigin`). `machine` is
130
+ * checked by the receiver against the envelope; `plugin`, `parent` and the page's opener are relayed by the originating
131
+ * backend and cannot be checked by the receiver. Only the direct caller, not the chain of parents.
103
132
  */
104
133
  caller?: Caller;
105
134
  }
@@ -110,7 +139,7 @@ export type Handler = (input: Json, ctx: Context) => Json | Promise<Json>;
110
139
  * Who asks a service or a stream: this machine's own client or tools, a device, another backend of the space (the last hop,
111
140
  * of one sent on), or a plugin (by its token, or from its page; a stream's only).
112
141
  */
113
- export type StreamCaller = { local: true } | { device: string; role: 'client' | 'backend'; name?: string } | { plugin: string };
142
+ export type StreamCaller = ({ local: true } | { device: string; role: 'client' | 'backend'; name?: string } | { plugin: string }) & CallerOrigin;
114
143
  /** What a handler of a service (`coda.serve`) is given beside its request. */
115
144
  export interface ServeContext {
116
145
  caller: StreamCaller;
package/protocol.mjs CHANGED
@@ -78,7 +78,7 @@ function schemaProblem(schema, where) {
78
78
 
79
79
  // Permission groups, when written: every group names real operations, streams and events and covers something, every
80
80
  // 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
81
- // holds only events: subscribing to the group is subscribing to all of them.
81
+ // holds only events: subscribing to the group is subscribing to all of them. A group that writes sessions holds an operation or a stream.
82
82
  function permissionsProblem({ operations = {}, streams = {}, events = {}, permissions }, where) {
83
83
  if (permissions === undefined) return null;
84
84
  const owner = { operations: new Map(), streams: new Map(), events: new Map() };
@@ -92,8 +92,10 @@ function permissionsProblem({ operations = {}, streams = {}, events = {}, permis
92
92
  owner[key].set(name, group);
93
93
  }
94
94
  }
95
- if (entry.events !== undefined && ['operations', 'streams', 'read'].some(key => Object.hasOwn(entry, key)))
96
- return `${where}.permissions.${group}:有 events 的组只放事件,不能再写 operations、streams、read`;
95
+ if (entry.events !== undefined && ['operations', 'streams', 'read', 'sessions'].some(key => Object.hasOwn(entry, key)))
96
+ return `${where}.permissions.${group}:有 events 的组只放事件,不能再写 operations、streams、read、sessions`;
97
+ if (Object.hasOwn(entry, 'sessions') && !entry.operations?.length && !entry.streams?.length)
98
+ return `${where}.permissions.${group}.sessions:写了 sessions 的组至少要有一个操作或一条流,只有 read 不够`;
97
99
  if (!entry.operations?.length && !entry.streams?.length && !entry.events?.length && entry.read !== true)
98
100
  return `${where}.permissions.${group}:至少管一样东西:写 operations、streams 或 events,或 read 为 true`;
99
101
  }
@@ -128,6 +130,25 @@ function recordsProblem(definition, where) {
128
130
  return reading ? `${where}.permissions.${reading[0]}.read:${without}读记录的权限,要它须同时写 spec 和 status` : null;
129
131
  }
130
132
 
133
+ // `resource.columns`: each is a scalar top-level field of `spec` (a string, number, integer or boolean; `null` beside one is fine).
134
+ const SCALAR_TYPES = ['string', 'number', 'integer', 'boolean'];
135
+ function isScalarSchema(schema) {
136
+ if (schema === null || typeof schema !== 'object') return false;
137
+ const types = (Array.isArray(schema.type) ? schema.type : [schema.type]).filter(type => type !== 'null');
138
+ return types.length === 1 && SCALAR_TYPES.includes(types[0]);
139
+ }
140
+
141
+ function resourceProblem({ spec, resource }, where) {
142
+ if (resource === undefined) return null;
143
+ if (spec === undefined) return `${where}.resource:没有记录的协议(没写 spec)不能写 resource`;
144
+ const properties = spec?.properties ?? {};
145
+ for (const [index, column] of resource.columns.entries()) {
146
+ if (!Object.hasOwn(properties, column)) return `${where}.resource.columns[${index}]:spec 里没有字段「${column}」`;
147
+ if (!isScalarSchema(properties[column])) return `${where}.resource.columns[${index}]:字段「${column}」不是标量,须是 string、number、integer 或 boolean`;
148
+ }
149
+ return null;
150
+ }
151
+
131
152
  // `namespace`: the plugin ID a manifest owns definitions under; omitted for core definitions and standalone checks.
132
153
  export function definitionProblem(definition, { namespace = null, where = '协议定义' } = {}) {
133
154
  if (!definitionCheck(definition))
@@ -143,6 +164,8 @@ export function definitionProblem(definition, { namespace = null, where = '协
143
164
  if (recording) return recording;
144
165
  const grouping = permissionsProblem(definition, where);
145
166
  if (grouping) return grouping;
167
+ const resource = resourceProblem(definition, where);
168
+ if (resource) return resource;
146
169
  const parts = protocolHasRecords(definition) ? [['spec', definition.spec], ['status', definition.status]] : [];
147
170
  for (const [operation, value] of Object.entries(definition.operations ?? {})) {
148
171
  parts.push([`operations.${operation}.input`, value.input]);
@@ -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 声明记录流;可写 events 声明协议的事件;可写 permissions 把操作、流、事件与记录的读取分成权限组,供插件在清单里按组申请。写了 events 的定义可以不写 spec、status、operations(没有记录的协议;它不能有非空的 operations、streams,也不能有 read 为真的组);没写 events 时三者必填。",
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 时三者必填。可写 resource 声明种类在资源列表里的展示(见 resource)。",
6
6
  "type": "object",
7
7
  "properties": {
8
8
  "name": {"$ref": "#/$defs/name"},
@@ -33,8 +33,9 @@
33
33
  "propertyNames": {"$ref": "#/$defs/eventName"},
34
34
  "additionalProperties": {"$ref": "#/$defs/event"}
35
35
  },
36
+ "resource": {"$ref": "#/$defs/resource"},
36
37
  "permissions": {
37
- "description": "权限组:组名 → 一组对插件开放的能力。插件在清单的 uses(必需)或 optionalUses(可选)里按组申请,没申请的调用被宿主拒绝;可选权限缺席时不阻止插件运行,可用性判定与鉴权由 Coda 宿主负责。每个操作、每条流、每个事件最多属于一组,组里列的操作、流与事件都存在,每组至少管一样东西(有操作、有流、有事件,或 read 为真),有 events 的组不能再有 operations、streams、read 这几个键;没写 spec、status 的协议不能有非空的 operations、streams,也不能有 read 为真的组;这些由 definitionProblem 查。不在任何组里的操作、流和事件不对插件开放,只有 Coda 自己和它的客户端调;不写 permissions 就是整个协议都不分组",
38
+ "description": "权限组:组名 → 一组对插件开放的能力。插件在清单的 uses(必需)或 optionalUses(可选)里按组申请,没申请的调用被宿主拒绝;可选权限缺席时不阻止插件运行,可用性判定与鉴权由 Coda 宿主负责。每个操作、每条流、每个事件最多属于一组,组里列的操作、流与事件都存在,每组至少管一样东西(有操作、有流、有事件,或 read 为真),有 events 的组不能再有 operations、streams、read、sessions 这几个键;写了 sessions 的组必须至少有一个操作或一条流(只有 read 不够);没写 spec、status 的协议不能有非空的 operations、streams,也不能有 read 为真的组;这些由 definitionProblem 查。不在任何组里的操作、流和事件不对插件开放,只有 Coda 自己和它的客户端调;不写 permissions 就是整个协议都不分组",
38
39
  "type": "object",
39
40
  "maxProperties": 50,
40
41
  "propertyNames": {"$ref": "#/$defs/operationName"},
@@ -79,16 +80,27 @@
79
80
  },
80
81
  "title": {"description": "给人看的名字", "type": "string", "maxLength": 100, "pattern": "^(?=.*\\S)[^\\u0000\\r\\n]*$"},
81
82
  "description": {"description": "一句话说明", "type": "string", "maxLength": 1000, "pattern": "^(?=.*\\S)[^\\u0000\\r\\n]*$"},
83
+ "resource": {
84
+ "description": "展示声明:写了它的种类成为可列出、可引用的资源。列表里每条按 columns 的顺序显示,第一列是名字;所在机器那一列由宿主自己加,不用写;列名取该字段 schema 的 title,没有就用字段名;只展示这些列与 spec 里带 description 的字段。columns 的每一项都必须是 spec.properties 里声明的字段,且是标量(schema 的 type 是 string、number、integer 或 boolean 之一;type 是数组时去掉 \"null\" 后只剩一种标量也算;有 enum 而无 type 不算);没写 spec 的定义不能写 resource,这些由 definitionProblem 查。不写就和没有这个声明一样",
85
+ "type": "object",
86
+ "properties": {
87
+ "columns": {"type": "array", "minItems": 1, "maxItems": 8, "uniqueItems": true, "items": {"type": "string", "minLength": 1, "maxLength": 64}, "description": "列:spec 的顶层字段名,按显示顺序,第一列是名字;不重复"},
88
+ "symbol": {"type": "string", "minLength": 1, "maxLength": 64, "description": "客户端画的 SF Symbol 名"}
89
+ },
90
+ "required": ["columns"],
91
+ "additionalProperties": false
92
+ },
82
93
  "permissionGroup": {
83
- "description": "一个权限组:title 是给用户看的一句话,说这组让插件能做什么;trial 标这组是试行的;operations 是归这组管的操作;streams 是归这组管的流;events 是归这组管的事件,插件在 uses 或 optionalUses 里写了这组就是订了组里全部事件,有 events 的组只放事件(不能再写 operations、streams、read 这几个键);read 管不管读这个协议的记录(get、list、watch);写记录只许实现方,不分给插件",
94
+ "description": "一个权限组:title 是给用户看的一句话,说这组让插件能做什么;trial 标这组是试行的;operations 是归这组管的操作;streams 是归这组管的流;events 是归这组管的事件,插件在 uses 或 optionalUses 里写了这组就是订了组里全部事件,有 events 的组只放事件(不能再写 operations、streams、read、sessions 这几个键);read 管不管读这个协议的记录(get、list、watch);sessions 为 true 表示这组里的操作与流开放给会话(会话经工具调用),不只是开放给插件,写了 sessions 的组须至少有一个操作或一条流;写记录只许实现方,不分给插件",
84
95
  "type": "object",
85
96
  "properties": {
86
97
  "title": {"$ref": "#/$defs/title"},
87
98
  "trial": {"type": "boolean", "description": "是否试行:只影响展示,不影响能否调用"},
88
99
  "operations": {"type": "array", "maxItems": 50, "uniqueItems": true, "items": {"$ref": "#/$defs/operationName"}, "description": "归这组管的操作名,不重复"},
89
100
  "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 这几个键"},
91
- "read": {"type": "boolean", "description": "管不管读这个协议的记录(get、list、watch)"}
101
+ "events": {"type": "array", "minItems": 1, "maxItems": 50, "uniqueItems": true, "items": {"$ref": "#/$defs/eventName"}, "description": "归这组管的事件名,不重复;有 events 的组不能再有 operations、streams、read、sessions 这几个键"},
102
+ "read": {"type": "boolean", "description": "管不管读这个协议的记录(get、list、watch)"},
103
+ "sessions": {"type": "boolean", "description": "为 true 表示这组里的操作与流开放给会话(会话经工具调用),不只是开放给插件;写了这个键的组须至少有一个操作或一条流(只有 read 不够),有 events 的组不能写这个键"}
92
104
  },
93
105
  "required": ["title"],
94
106
  "additionalProperties": false
@@ -43,8 +43,8 @@
43
43
  "path": {
44
44
  "type": "string",
45
45
  "maxLength": 2048,
46
- "pattern": "^(?:uploads/(?:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(?:/preview)?|plugins/(?:[a-z][a-z0-9-]{0,39})/services/(?:[a-z][a-z0-9-]{0,39})/(?:(?!(?:[.]|%2[eE]){1,2}(?:/|$))(?:[A-Za-z0-9._~!$&'()*+,;=:@-]|%(?!2[fF]|5[cC])[0-9A-Fa-f]{2})+(?:/(?!(?:[.]|%2[eE]){1,2}(?:/|$))(?:[A-Za-z0-9._~!$&'()*+,;=:@-]|%(?!2[fF]|5[cC])[0-9A-Fa-f]{2})+)*))$",
47
- "description": "那台上的地址:URL 路径,已按段百分号编码,不带 /v1/ 和开头的 /。只收 uploads/<上传 ID>、uploads/<上传 ID>/preview 与 plugins/<插件 ID>/services/<服务 ID>/<路径>,同 @coda-console/protocol/blob 的 parseBlobPath"
46
+ "pattern": "^(?:uploads/(?:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(?:/preview)?|plugins/(?:[a-z][a-z0-9-]{0,39})/services/(?:[a-z][a-z0-9-]{0,39})/(?:(?!(?:[.]|%2[eE]){1,2}(?:/|$))(?:[A-Za-z0-9._~!$&'()*+,;=:@-]|%(?!2[fF]|5[cC])[0-9A-Fa-f]{2})+(?:/(?!(?:[.]|%2[eE]){1,2}(?:/|$))(?:[A-Za-z0-9._~!$&'()*+,;=:@-]|%(?!2[fF]|5[cC])[0-9A-Fa-f]{2})+)*)|content/(?:[0-9a-f]{64}))$",
47
+ "description": "那台上的地址:URL 路径,已按段百分号编码,不带 /v1/ 和开头的 /。只收 uploads/<上传 ID>、uploads/<上传 ID>/preview、plugins/<插件 ID>/services/<服务 ID>/<路径> 与 content/<sha256>(64 位小写十六进制 SHA-256),同 @coda-console/protocol/blob 的 parseBlobPath"
48
48
  },
49
49
  "name": {"type": "string", "maxLength": 1024},
50
50
  "size": {"type": "integer", "minimum": 0},
@@ -145,8 +145,8 @@
145
145
  "path": {
146
146
  "type": "string",
147
147
  "maxLength": 2048,
148
- "pattern": "^(?:uploads/(?:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(?:/preview)?|plugins/(?:[a-z][a-z0-9-]{0,39})/services/(?:[a-z][a-z0-9-]{0,39})/(?:(?!(?:[.]|%2[eE]){1,2}(?:/|$))(?:[A-Za-z0-9._~!$&'()*+,;=:@-]|%(?!2[fF]|5[cC])[0-9A-Fa-f]{2})+(?:/(?!(?:[.]|%2[eE]){1,2}(?:/|$))(?:[A-Za-z0-9._~!$&'()*+,;=:@-]|%(?!2[fF]|5[cC])[0-9A-Fa-f]{2})+)*))$",
149
- "description": "那台上的地址:URL 路径,已按段百分号编码,不带 /v1/ 和开头的 /。只收 uploads/<上传 ID>、uploads/<上传 ID>/preview 与 plugins/<插件 ID>/services/<服务 ID>/<路径>,同 @coda-console/protocol/blob 的 parseBlobPath"
148
+ "pattern": "^(?:uploads/(?:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(?:/preview)?|plugins/(?:[a-z][a-z0-9-]{0,39})/services/(?:[a-z][a-z0-9-]{0,39})/(?:(?!(?:[.]|%2[eE]){1,2}(?:/|$))(?:[A-Za-z0-9._~!$&'()*+,;=:@-]|%(?!2[fF]|5[cC])[0-9A-Fa-f]{2})+(?:/(?!(?:[.]|%2[eE]){1,2}(?:/|$))(?:[A-Za-z0-9._~!$&'()*+,;=:@-]|%(?!2[fF]|5[cC])[0-9A-Fa-f]{2})+)*)|content/(?:[0-9a-f]{64}))$",
149
+ "description": "那台上的地址:URL 路径,已按段百分号编码,不带 /v1/ 和开头的 /。只收 uploads/<上传 ID>、uploads/<上传 ID>/preview、plugins/<插件 ID>/services/<服务 ID>/<路径> 与 content/<sha256>(64 位小写十六进制 SHA-256),同 @coda-console/protocol/blob 的 parseBlobPath"
150
150
  },
151
151
  "name": {"type": "string", "maxLength": 1024},
152
152
  "size": {"type": "integer", "minimum": 0},