@coda-console/plugin-check 0.8.25 → 0.8.27
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 +25 -23
- package/check/coda/api-surface.txt +22 -1
- package/check/coda/application/host-protocols.mjs +3 -1
- package/check/coda/builtin.json +43 -1136
- package/check/coda/components/plugins/plugin-extensions.mjs +5 -4
- package/check/coda/components/plugins/plugin-rules.mjs +3 -41
- package/check/coda/components/plugins/plugin-source-rules.mjs +18 -14
- package/check/coda/components/protocols/record-rules.mjs +2 -2
- package/check/coda/contract.json +15 -10
- package/check/coda/domain/accord.mjs +144 -0
- package/check/coda/domain/jcs.mjs +58 -0
- package/check/coda/domain/usage.mjs +56 -0
- package/check/coda/kernel/accord-protocol.mjs +168 -0
- package/check/coda/kernel/usage-protocol.mjs +97 -0
- package/check/index.mjs +4 -1
- package/check/module.mjs +5 -4
- package/check/resources.mjs +99 -0
- package/package.json +2 -2
|
@@ -98,7 +98,7 @@ export function implementsMissing(plugin, plugins, defined) {
|
|
|
98
98
|
}
|
|
99
99
|
// This Coda's API Level (docs/plugins.md「兼容」): what it offers plugins, counted. A manifest's `engines.coda` names the least
|
|
100
100
|
// a plugin needs; one naming none needs 0, which any Coda has.
|
|
101
|
-
export const API_LEVEL =
|
|
101
|
+
export const API_LEVEL = 6;
|
|
102
102
|
// The API Level a package's registry metadata asks for in its `engines`: a positive integer, or its digits as text (what
|
|
103
103
|
// npm's registry may make of one); undefined when it asks for none, null when it is anything else. A manifest read from
|
|
104
104
|
// its directory has the schema's shape (plugin-rules.mjs readPlugin).
|
|
@@ -186,9 +186,10 @@ export function subscribed(plugin, plugins, defined) {
|
|
|
186
186
|
// a stream of its records (`{ stream }`), or reading its records (`'read'`; writing them is the implementer's alone) —
|
|
187
187
|
// `definition` its protocol's (`protocolDefinition`), or null (docs/plugins.md「用到的权限」). The plugin that implements or
|
|
188
188
|
// defines the kind may; any other, a built-in one too, only by a group of the kind's it uses that has the operation or the
|
|
189
|
-
// stream, or reads its records. An operation or a stream in no group is open to no plugin.
|
|
190
|
-
|
|
191
|
-
|
|
189
|
+
// stream, or reads its records. An operation or a stream in no group is open to no plugin. `owned` false: by its uses
|
|
190
|
+
// alone, implementing or defining the kind not counted.
|
|
191
|
+
export function usesRefusal(plugin, kind, need, definition, owned = true) {
|
|
192
|
+
if (owned && ownsProtocol(plugin, kind)) return null;
|
|
192
193
|
const groups = Object.entries(definition?.permissions ?? {}), used = new Set(plugin.uses?.[kind] ?? []);
|
|
193
194
|
const covers = ([, group]) => typeof need === 'string' ? group[need] === true
|
|
194
195
|
: need.stream !== undefined ? (group.streams ?? []).includes(need.stream) : (group.operations ?? []).includes(need.operation);
|
|
@@ -20,15 +20,12 @@ import { iconFiles } from './plugin-icons.mjs';
|
|
|
20
20
|
import { checkCatalog, extensionsFor, retiredContributes } from './plugin-extensions.mjs';
|
|
21
21
|
import { contributionProblem, manifestProblem, TYPES } from '@coda-console/protocol/validate';
|
|
22
22
|
import { ownedProtocolsProblem } from '@coda-console/protocol/protocol';
|
|
23
|
-
import { claims,
|
|
23
|
+
import { claims, npmPluginId, repoAddress, sourcePath } from './plugin-source-rules.mjs';
|
|
24
24
|
import { CONTRIBUTION_POINTS } from './contribution-points.mjs';
|
|
25
25
|
|
|
26
26
|
const ID = /^[a-z][a-z0-9-]{0,39}$/;
|
|
27
27
|
// Plugins that ship with Coda, as VS Code's built-in extensions ship with it: updated with Coda itself.
|
|
28
28
|
export const BUILTIN_DIR = fileURLToPath(new URL('../../resources/plugins/', import.meta.url));
|
|
29
|
-
// The package the backend runs from (the runtime clone's, or the release's), whose npm dependencies may be built-in plugins
|
|
30
|
-
// too (dependencyPlugins).
|
|
31
|
-
const PACKAGE_ROOT = fileURLToPath(new URL('../../../', import.meta.url));
|
|
32
29
|
// What the plugins' base reads of `contributes` itself, the general types schemas/plugin.json names; any other key is a
|
|
33
30
|
// contribution point's (contribution-points.mjs), or for a point another plugin declares to take it (the resources
|
|
34
31
|
// plugin's `panels`), refused when neither does.
|
|
@@ -212,18 +209,12 @@ export function mcpValues(plugin, settings, state, base) {
|
|
|
212
209
|
return [{ name: server.name, command, args, env: Object.entries(env).map(([name, value]) => ({ name, value })) }];
|
|
213
210
|
});
|
|
214
211
|
}
|
|
215
|
-
// The plugins Coda ships: those in `dir`, which come from nowhere else
|
|
216
|
-
|
|
217
|
-
// 「内置插件的来源」).
|
|
218
|
-
export function builtinPlugins(dir = BUILTIN_DIR, dependencies = dir === BUILTIN_DIR ? dependencyPlugins() : new Map()) {
|
|
212
|
+
// The plugins Coda ships: those in `dir`, which come from nowhere else.
|
|
213
|
+
export function builtinPlugins(dir = BUILTIN_DIR) {
|
|
219
214
|
const plugins = new Map((existsSync(dir) ? readdirSync(dir, { withFileTypes: true }) : []).filter(d => d.isDirectory()).map(d => {
|
|
220
215
|
const plugin = readPlugin(join(dir, d.name), { kind: 'builtin' });
|
|
221
216
|
return [plugin.id, plugin];
|
|
222
217
|
}));
|
|
223
|
-
for (const [id, plugin] of dependencies) {
|
|
224
|
-
check(!plugins.has(id), 'invalid_plugin', `插件 ${id} 同时存在于资源目录与 npm 依赖`);
|
|
225
|
-
plugins.set(id, plugin);
|
|
226
|
-
}
|
|
227
218
|
// An agent, a group and Git's credentials for an address are one plugin's (components/plugins/plugin-source-rules.mjs claims).
|
|
228
219
|
const owner = new Map();
|
|
229
220
|
for (const plugin of plugins.values()) for (const claim of claims(plugin)) {
|
|
@@ -232,35 +223,6 @@ export function builtinPlugins(dir = BUILTIN_DIR, dependencies = dir === BUILTIN
|
|
|
232
223
|
}
|
|
233
224
|
return checkCatalog(plugins);
|
|
234
225
|
}
|
|
235
|
-
// A direct npm dependency declaring `contributes` is a bundled plugin (docs/plugins.md「npm 依赖中的插件」). npm installs its
|
|
236
|
-
// files and dependencies; where it comes from is what the dependency pins: a version of a package from the registry, with
|
|
237
|
-
// the integrity package-lock.json has of its tarball and the repository directory the package says it is published from,
|
|
238
|
-
// or a commit of a Git repository. Installed from the start, unless `coda.optionalPlugins` of the backend's package.json
|
|
239
|
-
// names it.
|
|
240
|
-
export function dependencyPlugins(root = PACKAGE_ROOT) {
|
|
241
|
-
const manifest = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')), plugins = new Map();
|
|
242
|
-
const optional = new Set(manifest.coda?.optionalPlugins ?? []);
|
|
243
|
-
let lock;
|
|
244
|
-
for (const [name, spec] of Object.entries(manifest.dependencies ?? {})) {
|
|
245
|
-
const dir = join(root, 'node_modules', name), pkg = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
|
|
246
|
-
if (!Object.hasOwn(pkg, 'contributes')) continue;
|
|
247
|
-
let source;
|
|
248
|
-
if (typeof spec === 'string' && NPM_VERSION.test(spec)) {
|
|
249
|
-
lock ??= JSON.parse(readFileSync(join(root, 'package-lock.json'), 'utf8')).packages ?? {};
|
|
250
|
-
const integrity = lock[`node_modules/${name}`]?.integrity;
|
|
251
|
-
check(typeof integrity === 'string' && INTEGRITY.test(integrity), 'invalid_plugin', `package-lock.json 里没有插件依赖 ${name} 的 sha512 完整性摘要`);
|
|
252
|
-
source = { kind: 'builtin', package: name, tag: 'latest', version: spec, integrity, ...publishedFrom(pkg.repository) };
|
|
253
|
-
} else {
|
|
254
|
-
const git = typeof spec === 'string' && /^git\+(.+)#([^#]+)$/.exec(spec);
|
|
255
|
-
check(git && COMMIT.test(git[2]), 'invalid_plugin', `插件依赖 ${name} 须写一个版本号,或用 Git 仓库地址与完整提交号固定版本`);
|
|
256
|
-
source = { kind: 'builtin', repo: repoAddress(git[1]), path: '', commit: git[2] };
|
|
257
|
-
}
|
|
258
|
-
const plugin = readPlugin(dir, source);
|
|
259
|
-
check(!plugins.has(plugin.id), 'invalid_plugin', `多个 npm 依赖声明了插件 ${plugin.id}`);
|
|
260
|
-
plugins.set(plugin.id, { ...plugin, defaultInstalled: !optional.has(name) });
|
|
261
|
-
}
|
|
262
|
-
return plugins;
|
|
263
|
-
}
|
|
264
226
|
// The repository directory a package says it is published from (`repository`: its address, and `directory` in it), as a
|
|
265
227
|
// source names one; nothing where it says none Coda reads.
|
|
266
228
|
export function publishedFrom(repository) {
|
|
@@ -72,8 +72,8 @@ export function repoIdentity(repo) {
|
|
|
72
72
|
return github ? `github.com/${github[1].toLowerCase()}` : repo.replace(/\.git$/, '');
|
|
73
73
|
}
|
|
74
74
|
// Whether two sources name the same package, at whatever version, or the same directory of the same repository, at whatever
|
|
75
|
-
// commit: a
|
|
76
|
-
// directory being the one its package says it is published from (components/plugins/plugin-rules.mjs
|
|
75
|
+
// commit: a package of the store and the plugin installed from it or from where it is published. A package read from npm
|
|
76
|
+
// names both, the directory being the one its package says it is published from (components/plugins/plugin-rules.mjs publishedFrom).
|
|
77
77
|
export const sameSource = (a, b) => !!a?.package && a.package === b?.package
|
|
78
78
|
|| !!(a?.repo && b?.repo) && repoIdentity(a.repo) === repoIdentity(b.repo) && (a.path ?? '') === (b.path ?? '');
|
|
79
79
|
// Installed again from the same directory, another commit keeps the one it replaces as `previous`, so that going back is
|
|
@@ -97,6 +97,11 @@ export const fromElsewhere = source => source?.kind === 'git' || source?.kind ==
|
|
|
97
97
|
// the integrity of its tarball, which names that tarball and no other. The one it had before likewise (`previousKey`).
|
|
98
98
|
export const versionKey = source => source.kind === 'npm' ? createHash('sha256').update(source.integrity).digest('hex').slice(0, 40) : source.commit;
|
|
99
99
|
export const previousKey = source => !source.previous ? null : source.kind === 'npm' ? versionKey({ kind: 'npm', integrity: source.previous.integrity }) : source.previous;
|
|
100
|
+
// Whether `source` is going back from the one `installed`: its `previous`, from the same place (as nextSource keeps it) —
|
|
101
|
+
// the same package at that version and integrity, or the same repository and directory at that commit.
|
|
102
|
+
export const goesBack = (installed, source) => installed?.kind === source.kind && !!installed.previous && (source.kind === 'npm'
|
|
103
|
+
? installed.package === source.package && installed.previous.version === source.version && installed.previous.integrity === source.integrity
|
|
104
|
+
: installed.repo === source.repo && installed.path === source.path && installed.previous === source.commit);
|
|
100
105
|
|
|
101
106
|
// ---- npm ----
|
|
102
107
|
// A package as npm names it (`name`, `@scope/name`) and the version of it wanted: one version, a dist-tag, or (empty) the
|
|
@@ -248,13 +253,10 @@ export function npmPackages(manifest, lock) {
|
|
|
248
253
|
return { name, version: typeof version === 'string' ? version : String(range), ...(optional ? { optional: true } : {}) };
|
|
249
254
|
});
|
|
250
255
|
}
|
|
251
|
-
//
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
if (!copy || !row || sameSource(copy.source, source)) return null;
|
|
256
|
-
if (fromElsewhere(row.source) && !sameSource(copy.source, row.source)) return null;
|
|
257
|
-
return `与内置插件 ${id} 同名${copy.source?.repo || copy.source?.package ? ',来源不同' : ''},请先卸载内置插件再安装`;
|
|
256
|
+
// A plugin from a repository or npm never takes the ID of one Coda ships (`builtin`), installed or not, nor Coda's own.
|
|
257
|
+
export function builtinProblem(id, builtin) {
|
|
258
|
+
if (id === 'coda') return 'coda 是 Coda 自己的 ID';
|
|
259
|
+
return builtin.has(id) ? `${id} 是 Coda 自带插件的 ID` : null;
|
|
258
260
|
}
|
|
259
261
|
// What only one plugin may declare, as said to the user: what a component's contribution point says (its `claim`: an
|
|
260
262
|
// agent, which sessions name by its ID alone; a concurrency group, which any process takes by its name; Git's credentials
|
|
@@ -265,12 +267,12 @@ export const claims = plugin => [...[...CONTRIBUTION_POINTS.values()].flatMap(po
|
|
|
265
267
|
...(plugin.views ?? []).flatMap(v => (v.handles ?? []).map(kind => `打开 ${kind} 引用`)),
|
|
266
268
|
...Object.values(plugin.extensionPoints ?? {}).filter(p => p.contributes).map(p => `扩展点 contributes.${p.contributes}`)];
|
|
267
269
|
// Why a plugin cannot sit beside those this machine has (`catalog`: built-in and installed, by ID; `builtin`: the plugins
|
|
268
|
-
// Coda ships, by ID), or null:
|
|
270
|
+
// Coda ships, by ID), or null: a built-in one's ID or Coda's own (builtinProblem), what a component's
|
|
269
271
|
// contribution point lets only a built-in plugin bring (its `builtinOnly`, of the plugin read: why, none when it brings none
|
|
270
272
|
// of it), or what only one plugin may declare (claims), a protocol or an event of one, another plugin declares. What it needs of the others — the plugins it depends on,
|
|
271
273
|
// the points it plugs into or writes for (plugin-extensions.mjs offerProblem) — is checked once all of them are there.
|
|
272
|
-
export function catalogProblem(plugin, catalog, builtin
|
|
273
|
-
const named = builtinProblem(plugin.id,
|
|
274
|
+
export function catalogProblem(plugin, catalog, builtin) {
|
|
275
|
+
const named = builtinProblem(plugin.id, builtin);
|
|
274
276
|
if (named) return named;
|
|
275
277
|
if (plugin.source?.kind !== 'builtin') for (const point of CONTRIBUTION_POINTS.values()) {
|
|
276
278
|
const only = point.builtinOnly?.(plugin);
|
|
@@ -385,13 +387,15 @@ export function candidate({ path, manifest, plugin, problem, fallback }, { catal
|
|
|
385
387
|
const id = plugin?.id ?? (typeof manifestId(manifest) === 'string' ? manifestId(manifest) : fallback);
|
|
386
388
|
const row = installed.get(id), along = plugin ? together(plugin, { catalog, installed, siblings, defined }) : [];
|
|
387
389
|
const missing = along.filter(each => each.from === 'missing').map(each => each.id);
|
|
390
|
+
// One coming along from the source is installed as it is (components/plugins/plugins.mjs installFrom): judged by its ID the same.
|
|
391
|
+
const reserved = along.filter(each => each.from === 'source').map(each => [each.id, builtinProblem(each.id, builtin)]).find(([, why]) => why);
|
|
388
392
|
// What this machine has once it is installed, those installed with it among them: the points it may write for.
|
|
389
393
|
const after = plugin ? catalogAfter(plugin, { catalog, siblings }) : catalog;
|
|
390
394
|
const incompatible = plugin ? installingIncompatibility(after, installed, id, { channel, level, defined }) : null;
|
|
391
395
|
return { path, id, displayName: plugin?.displayName ?? (typeof manifest?.displayName === 'string' ? manifest.displayName : id),
|
|
392
396
|
description: plugin?.description ?? (typeof manifest?.description === 'string' ? manifest.description : ''), version: plugin?.version ?? '',
|
|
393
|
-
installed: row ? row.source ??
|
|
394
|
-
problem: problem ?? incompatible?.error ?? (plugin ? catalogProblem(plugin, catalog, builtin
|
|
397
|
+
installed: row ? row.source ?? { kind: 'builtin' } : null,
|
|
398
|
+
problem: problem ?? incompatible?.error ?? (plugin ? catalogProblem(plugin, catalog, builtin) ?? offerProblem(plugin, after) : null) ?? (reserved ? reserved.join(':') : null)
|
|
395
399
|
?? (missing.length ? `要一起装的 ${missing.join('、')} 不在这台机器上${plugin.source?.kind === 'npm' ? '' : ',也不在这个提交里'}` : null),
|
|
396
400
|
...(!problem && incompatible ? { incompatible } : {}), together: along.filter(each => each.from !== 'missing'),
|
|
397
401
|
...(plugin ? { contributes: contributions(plugin, after, defined) } : {}) };
|
|
@@ -6,13 +6,13 @@ import { derivedUUID } from '../../domain/values.mjs';
|
|
|
6
6
|
|
|
7
7
|
// Where a kind's records are, said once for every kind (Protocols `register` reads it). The kinds only this machine keeps:
|
|
8
8
|
// Coda's composers and what is submitted through them, the runs of its posts to sessions, and the kinds it serves whole
|
|
9
|
-
// from its own tables: `coda/thread@1`, `coda/project@1`, `coda/card@1`, `coda/plugin@1` and the workflows' `coda/workflow@1`
|
|
9
|
+
// from its own tables: `coda/thread@1`, `coda/project@1`, `coda/card@1`, `coda/plugin@1`, `coda/usage@1`, `coda/accord@1` (this machine's friends and its key, each machine its own) and the workflows' `coda/workflow@1`
|
|
10
10
|
// and `coda/workflow-run@1` (one of another backend is found by asking the space: mirror.mjs `place`). `coda/session@1` is shared (Coda's sessions are served beside
|
|
11
11
|
// it, a plugin's external sessions are written in the record directory). They are never in the record directory, and no
|
|
12
12
|
// plugin writes one. Every other kind, every one a plugin defines among them, is shared: its records are this machine's rows of
|
|
13
13
|
// the record directory, which the other backends of a space copy.
|
|
14
14
|
export const LOCAL_KINDS = new Set(['coda/composer@1', 'coda/submission@1', 'coda/run@1', 'coda/thread@1', 'coda/project@1', 'coda/card@1',
|
|
15
|
-
'coda/plugin@1', 'coda/workflow@1', 'coda/workflow-run@1']);
|
|
15
|
+
'coda/plugin@1', 'coda/usage@1', 'coda/accord@1', 'coda/workflow@1', 'coda/workflow-run@1']);
|
|
16
16
|
// `list(kind, { scope?, id?, labels? })`: this machine's records (`local`) or this machine's and the others' (`available`,
|
|
17
17
|
// unsaid), all of them, those of an ID, or those whose labels have each of these values (`labels`, as Kubernetes' equality
|
|
18
18
|
// selector: nothing but equality).
|
package/check/coda/contract.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
|
-
"commit": "
|
|
3
|
-
"apiLevel":
|
|
2
|
+
"commit": "54365d425202b30d8f400338a0ee2a3504b12522",
|
|
3
|
+
"apiLevel": 6,
|
|
4
4
|
"packages": {
|
|
5
5
|
"@0dep/piso": "5.2.0",
|
|
6
6
|
"@agentclientprotocol/sdk": "2.6.0",
|
|
7
|
-
"@coda-console/protocol": "6.
|
|
7
|
+
"@coda-console/protocol": "6.17.0",
|
|
8
8
|
"ajv": "8.20.0",
|
|
9
9
|
"yaml": "2.9.1"
|
|
10
10
|
},
|
|
11
11
|
"files": {
|
|
12
12
|
"check/coda/application/contributions.mjs": "861c363d06cdabfc4ce15435126fbc7ad7dafd3c93ca2ad9e9534a6f5a237942",
|
|
13
|
-
"check/coda/application/host-protocols.mjs": "
|
|
13
|
+
"check/coda/application/host-protocols.mjs": "1153b5e35f59ab04bef50dc4cecbca4fb2326096eb556978dd7d51736611e970",
|
|
14
14
|
"check/coda/components/protocols/protocol-events.mjs": "076f335287d0398c602ad6db286ab0001dc00ac42d32a23918a66ee37c3e9a2f",
|
|
15
|
-
"check/coda/components/protocols/record-rules.mjs": "
|
|
15
|
+
"check/coda/components/protocols/record-rules.mjs": "c6bbafdaa869543ea4759be526ca8cf1ff5a03dad999f6ad724a602703c92a44",
|
|
16
16
|
"check/coda/components/protocols/types.mjs": "283cd0f4516f10637d6fe257fe645ead87912219d471c06408d56617f244e1d1",
|
|
17
17
|
"check/coda/adapters/provider-api.mjs": "70441ea2a417ede13e04113ec254496883be3186f291ef5ee65834f35009eb9e",
|
|
18
18
|
"check/coda/adapters/call-limit.mjs": "7e86d95ff7b0a88955015904ec91ae55e78fad816d06f710d6e62af53ccbdeab",
|
|
@@ -30,10 +30,10 @@
|
|
|
30
30
|
"check/coda/components/plugins/contribution-points.mjs": "2dade9fe4f3fa2b4e89f49a1ac4378ab2621e5ac542eb175967091f04568b45c",
|
|
31
31
|
"check/coda/components/plugins/git-contributions.mjs": "532917cd3ec45e12535e9e6c206ae14fa598bebc789ab0c8f15609d1699fa1cd",
|
|
32
32
|
"check/coda/components/plugins/model-contributions.mjs": "018a7157954b2326887ba1a780b37d3beb33338e41a28936b313562c2939d563",
|
|
33
|
-
"check/coda/components/plugins/plugin-extensions.mjs": "
|
|
33
|
+
"check/coda/components/plugins/plugin-extensions.mjs": "49b6dc6203246f1fd4e9faf8dea33654c601a4d10145713bc3d84099df5f5f07",
|
|
34
34
|
"check/coda/components/plugins/plugin-icons.mjs": "ec29cadce4948ecacabfe911a2ceb45414b96e28971358acbc4e952bedc78e1a",
|
|
35
|
-
"check/coda/components/plugins/plugin-rules.mjs": "
|
|
36
|
-
"check/coda/components/plugins/plugin-source-rules.mjs": "
|
|
35
|
+
"check/coda/components/plugins/plugin-rules.mjs": "c1a8d7180b95585654a838cea8bf7fbc124c5e798ce1875f8a58d0f7fc3b21cd",
|
|
36
|
+
"check/coda/components/plugins/plugin-source-rules.mjs": "cbe26ff38c68f89958eb16e825d4b63de580f61b16e717404af195a5709f1f6d",
|
|
37
37
|
"check/coda/components/plugins/process-contributions.mjs": "6d39a904f52956b87c04351398f79a4fc34981fa01453195bd7a3b9f2c5d237b",
|
|
38
38
|
"check/coda/components/plugins/skill-document.mjs": "153ad3c096c7c9b7774bcf7dcc0ba603c962ee1ac8697c8a71e3f2c9ba9c92e6",
|
|
39
39
|
"check/coda/components/prompt-recovery/prompt-recovery.mjs": "25477742af8a9134ae343af6ab702926692bdd8dd07844f3cba47e8b702c0e49",
|
|
@@ -41,21 +41,26 @@
|
|
|
41
41
|
"check/coda/components/session-config/contributions.mjs": "74d52d65dc7b6f13ef301a211bdf50cc91ae468c462cbe201f5dc34f7437b97b",
|
|
42
42
|
"check/coda/components/workflows/contributions.mjs": "f16e36e5e591d7ab6c6e63f252e780717ba17bb4642aedd2797c5ff017df9d95",
|
|
43
43
|
"check/coda/components/workflows/workflow-protocol-definitions.mjs": "094ab1b2676f05fa88eb7ccd558b4eb4dce609fdf412f7663f84906ac2732186",
|
|
44
|
+
"check/coda/domain/accord.mjs": "35cb6c2f918433b72aa44996efe2212d9b9c4ea807858d80947c8aa089b85fa2",
|
|
44
45
|
"check/coda/domain/agent-protocol.mjs": "f1d7c939c240664b773c04d9a1c1e9457cc8396fd8f99f1d5b6b6964150843c0",
|
|
45
46
|
"check/coda/domain/duration.mjs": "3f07411812362adcacfc288b1513f10e7d0ea1addcdc1b47ae553561f1d3430b",
|
|
46
47
|
"check/coda/domain/errors.mjs": "15ad3348ea0e173a1a8602ec72766592cb41c0221420d9e5884bd68090ecc84b",
|
|
48
|
+
"check/coda/domain/jcs.mjs": "2f1074ac2dd23d397567c01c7a7107ad3607bad6995a8a5c9115a6705a015afe",
|
|
47
49
|
"check/coda/domain/resume-prompt.mjs": "9895ccf235808b9bf3f4836999c65ddd6d764fcaa6e8a9a9647612faf0c9629a",
|
|
48
50
|
"check/coda/domain/plugin-services.mjs": "940a72a14da980188a32c790c7f341d3ec0062a6e87ef1ebc9ef051798ea0a75",
|
|
49
51
|
"check/coda/domain/session-env.mjs": "283bbc2e8ae9ea22b53747865a651a4dac9c6cfc242f27612e0c82e6e9cc151a",
|
|
52
|
+
"check/coda/domain/usage.mjs": "9077cfb738016fed72e4fb43f415aab258bdfa213e466740f2c0535dbba45ed3",
|
|
50
53
|
"check/coda/domain/values.mjs": "afb8bf30e374bed27fe41ee4b6d8c3c0b4dbe1dcfb6b62dad9d3d53b966706a1",
|
|
54
|
+
"check/coda/kernel/accord-protocol.mjs": "f9a5c59f9b278f0ceebb9c1c5f6c2b49be0300ce3bde04e46dcf1c10acf2f0ac",
|
|
51
55
|
"check/coda/kernel/agent-failures.mjs": "cbc215e2403d86f0acc765c2077197e0b529fc01e213db465c388361238ea8ce",
|
|
52
56
|
"check/coda/kernel/plugin-protocol.mjs": "c514f9b6237404f8877dea568e4d425f1e380c19f3cb87764edf56f6c92ebd69",
|
|
53
57
|
"check/coda/kernel/project-protocol.mjs": "69874b2b226f111b156724454c968e829018f1caac0a02883f13e1f980482739",
|
|
54
58
|
"check/coda/kernel/thread-protocol.mjs": "58bbd66c0058552f26c1b4ed08231e9afd142f2b7a9f50ed6bd66cbfe8f579e1",
|
|
59
|
+
"check/coda/kernel/usage-protocol.mjs": "4bd07a4d4c63c75e5328ef7e907bfa4e83af430f7f04a08cf8eb883a23ddc51f",
|
|
55
60
|
"check/coda/resources/plugins/resources/panels/panels.mjs": "37ffc6a4ede3e4612cd13cf1a973a90892f8ce7e8a0f055e7cfe86313aae5224",
|
|
56
61
|
"check/coda/resources/plugins/resources/panels/rules.mjs": "a433d4e84138937297ec31a8912aab7bd48bba4b66da3a10978edb1367613c96",
|
|
57
62
|
"check/coda/resources/plugins/resources/panels/sign-in.mjs": "6eb016ef9a7f8aa03e0f4b2fa35ce139b8b1ff0deb4ec6e6a34139aed4f2841a",
|
|
58
|
-
"check/coda/builtin.json": "
|
|
59
|
-
"check/coda/api-surface.txt": "
|
|
63
|
+
"check/coda/builtin.json": "d42d66c67aa3a9dee8239146ef797617d6502b45f48e0557a7db899e12a5737c",
|
|
64
|
+
"check/coda/api-surface.txt": "babddd7c7fb1b897874d86deed1d42f07ded67b2fff60e75a9c0b8c4749581ea"
|
|
60
65
|
}
|
|
61
66
|
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { createPrivateKey, createPublicKey, generateKeyPairSync, sign, verify } from 'node:crypto';
|
|
2
|
+
import { jcs } from './jcs.mjs';
|
|
3
|
+
|
|
4
|
+
// Accord records (AIP-01) and bindings (AIP-02): formats, signing and verification. Pure: keys are node:crypto KeyObjects.
|
|
5
|
+
|
|
6
|
+
export const MAX_RECORD_BYTES = 65536;
|
|
7
|
+
const KEY = /^ed25519:[0-9a-f]{64}$/;
|
|
8
|
+
const SIGNATURE = /^[0-9a-f]{128}$/;
|
|
9
|
+
const KIND = /^[a-z0-9-]{1,64}(\/[a-z0-9-]{1,64})?$/;
|
|
10
|
+
// RFC 8410: SubjectPublicKeyInfo of an Ed25519 key is this prefix followed by the 32 raw bytes.
|
|
11
|
+
const SPKI_PREFIX = Buffer.from('302a300506032b6570032100', 'hex');
|
|
12
|
+
const RECORD_DOMAIN = 'accord:record:1\n';
|
|
13
|
+
const CERT_DOMAIN = 'accord:cert:1\n';
|
|
14
|
+
const CERT_FIELDS = ['key', 'from', 'until', 'sig'];
|
|
15
|
+
|
|
16
|
+
const isObject = value => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
17
|
+
const isTime = value => Number.isSafeInteger(value) && value >= 0;
|
|
18
|
+
const bytes = text => Buffer.byteLength(text, 'utf8');
|
|
19
|
+
|
|
20
|
+
export const isAccordKind = kind => typeof kind === 'string' && KIND.test(kind);
|
|
21
|
+
|
|
22
|
+
export function generateIdentity() {
|
|
23
|
+
const { publicKey, privateKey } = generateKeyPairSync('ed25519');
|
|
24
|
+
return { author: publicKeyToAuthor(publicKey), publicKey, privateKey };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function publicKeyToAuthor(publicKey) {
|
|
28
|
+
const der = publicKey.export({ format: 'der', type: 'spki' });
|
|
29
|
+
if (der.length !== SPKI_PREFIX.length + 32 || !der.subarray(0, SPKI_PREFIX.length).equals(SPKI_PREFIX)) {
|
|
30
|
+
throw new TypeError('不是 Ed25519 公钥');
|
|
31
|
+
}
|
|
32
|
+
return `ed25519:${der.subarray(SPKI_PREFIX.length).toString('hex')}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function authorToPublicKey(author) {
|
|
36
|
+
if (typeof author !== 'string' || !KEY.test(author)) throw new TypeError('钥匙格式不合法');
|
|
37
|
+
return createPublicKey({ key: Buffer.concat([SPKI_PREFIX, Buffer.from(author.slice(8), 'hex')]), format: 'der', type: 'spki' });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const isEd25519 = privateKey => privateKey?.type === 'private' && privateKey.asymmetricKeyType === 'ed25519';
|
|
41
|
+
const signed = (domain, value, privateKey) => {
|
|
42
|
+
if (!isEd25519(privateKey)) throw new TypeError('签名须用 Ed25519 私钥');
|
|
43
|
+
return sign(null, Buffer.from(domain + jcs(value), 'utf8'), privateKey).toString('hex');
|
|
44
|
+
};
|
|
45
|
+
const verified = (domain, value, signature, key) => verify(null, Buffer.from(domain + jcs(value), 'utf8'), authorToPublicKey(key), Buffer.from(signature, 'hex'));
|
|
46
|
+
|
|
47
|
+
function certBodyProblem({ key, from, until }) {
|
|
48
|
+
if (typeof key !== 'string' || !KEY.test(key)) return '证书钥匙格式不合法';
|
|
49
|
+
if (!isTime(from) || !isTime(until) || from > until) return '证书有效期不合法';
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function certProblem(cert) {
|
|
54
|
+
if (!isObject(cert)) return '证书不是对象';
|
|
55
|
+
if (Object.keys(cert).length !== CERT_FIELDS.length || !CERT_FIELDS.every(name => Object.hasOwn(cert, name))) return '证书字段不符';
|
|
56
|
+
if (typeof cert.sig !== 'string' || !SIGNATURE.test(cert.sig)) return '证书签名格式不合法';
|
|
57
|
+
return certBodyProblem(cert);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/// A certificate: the author's signature over the key that may sign for them during [from, until].
|
|
61
|
+
export function signCert({ key, from, until }, authorPrivateKey) {
|
|
62
|
+
const body = { key, from, until };
|
|
63
|
+
const problem = certBodyProblem(body);
|
|
64
|
+
if (problem) throw new TypeError(problem);
|
|
65
|
+
return { ...body, sig: signed(CERT_DOMAIN, body, authorPrivateKey) };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function verifyCert(cert, author) {
|
|
69
|
+
const problem = certProblem(cert);
|
|
70
|
+
if (problem) return { ok: false, reason: problem };
|
|
71
|
+
if (typeof author !== 'string' || !KEY.test(author)) return { ok: false, reason: '作者钥匙格式不合法' };
|
|
72
|
+
const { key, from, until, sig } = cert;
|
|
73
|
+
return verified(CERT_DOMAIN, { key, from, until }, sig, author) ? { ok: true } : { ok: false, reason: '证书签名不符' };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function recordProblem(record) {
|
|
77
|
+
if (!isObject(record)) return '记录不是对象';
|
|
78
|
+
if (Object.hasOwn(record, 'gone')) return '记录顶层不能有 gone';
|
|
79
|
+
if (typeof record.author !== 'string' || !KEY.test(record.author)) return '作者钥匙格式不合法';
|
|
80
|
+
if (typeof record.id !== 'string' || bytes(record.id) < 1 || bytes(record.id) > 256) return 'id 须为 1 到 256 字节';
|
|
81
|
+
if (!isAccordKind(record.kind)) return '种类不合法';
|
|
82
|
+
if (!isTime(record.version)) return 'version 须为安全非负整数';
|
|
83
|
+
if (Object.hasOwn(record, 'body') && !isObject(record.body)) return 'body 须为对象';
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function signRecord(record, signerPrivateKey) {
|
|
88
|
+
if (isObject(record) && Object.hasOwn(record, 'sig')) throw new TypeError('待签记录不能已有 sig');
|
|
89
|
+
const problem = recordProblem(record);
|
|
90
|
+
if (problem) throw new TypeError(problem);
|
|
91
|
+
const full = { ...record, sig: signed(RECORD_DOMAIN, record, signerPrivateKey) };
|
|
92
|
+
const checked = verifyRecord(full);
|
|
93
|
+
if (!checked.ok) throw new TypeError(checked.reason);
|
|
94
|
+
return full;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function verifyRecord(record) {
|
|
98
|
+
const problem = recordProblem(record);
|
|
99
|
+
if (problem) return { ok: false, reason: problem };
|
|
100
|
+
if (typeof record.sig !== 'string' || !SIGNATURE.test(record.sig)) return { ok: false, reason: '签名格式不合法' };
|
|
101
|
+
const { sig, ...rest } = record;
|
|
102
|
+
let whole;
|
|
103
|
+
try { whole = jcs(record); } catch (error) { return { ok: false, reason: `记录不能规范化:${error.message}` }; }
|
|
104
|
+
if (bytes(whole) > MAX_RECORD_BYTES) return { ok: false, reason: '记录超过 64 KiB' };
|
|
105
|
+
let signer = record.author;
|
|
106
|
+
if (Object.hasOwn(record, 'cert')) {
|
|
107
|
+
const certified = verifyCert(record.cert, record.author);
|
|
108
|
+
if (!certified.ok) return certified;
|
|
109
|
+
if (record.version < record.cert.from || record.version > record.cert.until) return { ok: false, reason: 'version 不在证书有效期内' };
|
|
110
|
+
signer = record.cert.key;
|
|
111
|
+
}
|
|
112
|
+
return verified(RECORD_DOMAIN, rest, sig, signer) ? { ok: true } : { ok: false, reason: '签名不符' };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// AIP-02: the record by which an author says that `endpoint` speaks for them in `scope`.
|
|
116
|
+
const bindingProblem = (scope, endpoint) => {
|
|
117
|
+
if (typeof scope !== 'string' || scope === '') return 'scope 须为非空字符串';
|
|
118
|
+
if (typeof endpoint !== 'string' || endpoint === '') return 'endpoint 须为非空字符串';
|
|
119
|
+
return null;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export function signBinding({ author, id, version, scope, endpoint, cert }, signerPrivateKey) {
|
|
123
|
+
const problem = bindingProblem(scope, endpoint);
|
|
124
|
+
if (problem) throw new TypeError(problem);
|
|
125
|
+
const record = { author, id, kind: 'binding', version, body: { scope, endpoint } };
|
|
126
|
+
if (cert !== undefined) record.cert = cert;
|
|
127
|
+
return signRecord(record, signerPrivateKey);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function verifyBinding(record, { scope, endpoint, now = Date.now(), clockSkewMs = 5 * 60_000 }) {
|
|
131
|
+
const expected = bindingProblem(scope, endpoint);
|
|
132
|
+
if (expected) return { ok: false, reason: `期望的${expected}` };
|
|
133
|
+
const checked = verifyRecord(record);
|
|
134
|
+
if (!checked.ok) return checked;
|
|
135
|
+
if (record.kind !== 'binding') return { ok: false, reason: '不是绑定记录' };
|
|
136
|
+
const stated = bindingProblem(record.body?.scope, record.body?.endpoint);
|
|
137
|
+
if (stated) return { ok: false, reason: `记录的${stated}` };
|
|
138
|
+
if (record.body?.scope !== scope) return { ok: false, reason: 'scope 不符' };
|
|
139
|
+
if (record.body?.endpoint !== endpoint) return { ok: false, reason: 'endpoint 不符' };
|
|
140
|
+
if (record.cert && !(now >= record.cert.from - clockSkewMs && now <= record.cert.until + clockSkewMs)) {
|
|
141
|
+
return { ok: false, reason: '证书不在有效期内' };
|
|
142
|
+
}
|
|
143
|
+
return { ok: true, author: record.author };
|
|
144
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// RFC 8785 (JCS): the one byte-exact serialization a signature covers. Input is JSON data only — anything that has no JSON
|
|
2
|
+
// form is refused rather than dropped, since a silently dropped field would sign something other than what was meant.
|
|
3
|
+
|
|
4
|
+
const plain = value => { const proto = Object.getPrototypeOf(value); return proto === Object.prototype || proto === null; };
|
|
5
|
+
|
|
6
|
+
function text(value) {
|
|
7
|
+
if (!value.isWellFormed()) throw new TypeError('JCS 不接受孤立的 UTF-16 代理项');
|
|
8
|
+
return JSON.stringify(value);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function scalar(value) {
|
|
12
|
+
if (value === null) return 'null';
|
|
13
|
+
switch (typeof value) {
|
|
14
|
+
case 'boolean': return String(value);
|
|
15
|
+
case 'number':
|
|
16
|
+
if (!Number.isFinite(value)) throw new TypeError('JCS 不接受 NaN 与 Infinity');
|
|
17
|
+
return String(value); // ECMAScript Number::toString: shortest round-trip form; -0 is "0"
|
|
18
|
+
case 'string': return text(value);
|
|
19
|
+
default: throw new TypeError(`JCS 不接受 ${typeof value}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Walks with an explicit stack, so nesting depth is bounded by memory and not by the call stack. Each work item is a
|
|
24
|
+
// value still to write, a piece of punctuation (`{ text }`) to write as it is, or the end of a container (`{ leave }`);
|
|
25
|
+
// items are pushed in reverse. `active` holds the containers being written, so a cycle is refused where a shared
|
|
26
|
+
// child, reached again after its first visit is done, is not.
|
|
27
|
+
export function jcs(root) {
|
|
28
|
+
const out = [], work = [{ value: root }], active = new Set();
|
|
29
|
+
while (work.length) {
|
|
30
|
+
const item = work.pop();
|
|
31
|
+
if (Object.hasOwn(item, 'text')) { out.push(item.text); continue; }
|
|
32
|
+
if (Object.hasOwn(item, 'leave')) { active.delete(item.leave); continue; }
|
|
33
|
+
const { value } = item;
|
|
34
|
+
if (value === null || typeof value !== 'object') { out.push(scalar(value)); continue; }
|
|
35
|
+
if (active.has(value)) throw new TypeError('JCS 不接受循环引用');
|
|
36
|
+
if (Array.isArray(value)) {
|
|
37
|
+
active.add(value);
|
|
38
|
+
out.push('[');
|
|
39
|
+
work.push({ leave: value }, { text: ']' });
|
|
40
|
+
for (let i = value.length - 1; i >= 0; i--) {
|
|
41
|
+
work.push({ value: value[i] });
|
|
42
|
+
if (i > 0) work.push({ text: ',' });
|
|
43
|
+
}
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (!plain(value)) throw new TypeError('JCS 只接受普通对象与数组');
|
|
47
|
+
// Default string comparison orders by UTF-16 code units, as RFC 8785 requires.
|
|
48
|
+
const keys = Object.keys(value).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
49
|
+
active.add(value);
|
|
50
|
+
out.push('{');
|
|
51
|
+
work.push({ leave: value }, { text: '}' });
|
|
52
|
+
for (let i = keys.length - 1; i >= 0; i--) {
|
|
53
|
+
work.push({ value: value[keys[i]] }, { text: `${text(keys[i])}:` });
|
|
54
|
+
if (i > 0) work.push({ text: ',' });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return out.join('');
|
|
58
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// What a turn the agent reported using, as a Coda turn's `usage` (the agent's own count of this turn, not a running total),
|
|
2
|
+
// brought to the counts usage_buckets keeps. Only this standard shape is read: nothing here knows an agent's own fields.
|
|
3
|
+
export const USAGE_BUCKET_MS = 600_000;
|
|
4
|
+
|
|
5
|
+
export function bucketStart(timeMs) {
|
|
6
|
+
if (!Number.isSafeInteger(timeMs) || timeMs < 0) throw new TypeError('时间必须是非负整数毫秒');
|
|
7
|
+
return Math.floor(timeMs / USAGE_BUCKET_MS) * USAGE_BUCKET_MS;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// A model's name as a session keeps it (domain/values.mjs text), as it is: not blank, at most `max` long, no NUL; null for one that is not.
|
|
11
|
+
export const modelName = (value, max = 200) => typeof value === 'string' && value.trim().length > 0 && value.length <= max && !value.includes('\0') ? value : null;
|
|
12
|
+
|
|
13
|
+
const FIELDS = [['inputTokens', 'input'], ['outputTokens', 'output'], ['cachedReadTokens', 'cacheRead'], ['cachedWriteTokens', 'cacheWrite']];
|
|
14
|
+
const isObject = value => value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
15
|
+
&& [Object.prototype, null].includes(Object.getPrototypeOf(value));
|
|
16
|
+
|
|
17
|
+
// The counts of one object, `{ ok, counts }` or `{ ok: false, reason }`: each given field (not undefined or null) a non-negative safe integer, at least one given.
|
|
18
|
+
function counts(raw, what) {
|
|
19
|
+
const result = {};
|
|
20
|
+
let given = 0;
|
|
21
|
+
for (const [field, name] of FIELDS) {
|
|
22
|
+
const value = raw[field];
|
|
23
|
+
if (value === undefined || value === null) { result[name] = 0; continue; }
|
|
24
|
+
if (!Number.isSafeInteger(value) || value < 0) return { ok: false, reason: `${what}的 ${field} 不是非负整数` };
|
|
25
|
+
result[name] = value; given++;
|
|
26
|
+
}
|
|
27
|
+
return given ? { ok: true, counts: result } : { ok: false, reason: `${what}没有任何 token 字段` };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// `{ ok: true, usage: null }` for none reported; otherwise `{ ok: true, usage: { input, output, cacheRead, cacheWrite } }`, or
|
|
31
|
+
// `{ usage: { breakdown: [{ model, ...counts }] } }` of a turn that used several models (the only source then, the top
|
|
32
|
+
// level not counted beside it); `{ ok: false, reason }` for anything else, never thrown.
|
|
33
|
+
export function normalizeTurnUsage(raw) {
|
|
34
|
+
if (raw === undefined || raw === null) return { ok: true, usage: null };
|
|
35
|
+
if (!isObject(raw)) return { ok: false, reason: 'usage 不是对象' };
|
|
36
|
+
if (!('breakdown' in raw)) {
|
|
37
|
+
const top = counts(raw, 'usage');
|
|
38
|
+
return top.ok ? { ok: true, usage: top.counts } : top;
|
|
39
|
+
}
|
|
40
|
+
const { breakdown } = raw;
|
|
41
|
+
if (!Array.isArray(breakdown) || !breakdown.length) return { ok: false, reason: 'breakdown 不是非空数组' };
|
|
42
|
+
const byModel = new Map();
|
|
43
|
+
for (const item of breakdown) {
|
|
44
|
+
if (!isObject(item)) return { ok: false, reason: 'breakdown 的项不是对象' };
|
|
45
|
+
const model = modelName(item.model);
|
|
46
|
+
if (!model) return { ok: false, reason: 'breakdown 的 model 不是 1–200 字、不含 NUL 的文本' };
|
|
47
|
+
const each = counts(item, `模型 ${model}`);
|
|
48
|
+
if (!each.ok) return each;
|
|
49
|
+
const sum = byModel.get(model) ?? { model, input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
|
|
50
|
+
for (const [, name] of FIELDS) sum[name] += each.counts[name];
|
|
51
|
+
if (!Number.isSafeInteger(sum.input) || !Number.isSafeInteger(sum.output) || !Number.isSafeInteger(sum.cacheRead) || !Number.isSafeInteger(sum.cacheWrite))
|
|
52
|
+
return { ok: false, reason: `模型 ${model} 的合计超出安全整数` };
|
|
53
|
+
byModel.set(model, sum);
|
|
54
|
+
}
|
|
55
|
+
return { ok: true, usage: { breakdown: [...byModel.values()] } };
|
|
56
|
+
}
|