@coda-console/plugin-check 0.8.26 → 0.8.28

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.
@@ -2,7 +2,7 @@
2
2
  // may plug into a point one of them declares for it (`contributes.extensionPoints` there, `contributes.extensions` here).
3
3
  // Coda checks the names and hands each plugin what is plugged into its points, as declared: what passes between the two
4
4
  // is the extended plugin's own contract, which Coda neither reads nor shapes. A point may instead take what any plugin
5
- // writes under a key of its `contributes` (`contributes` there, as the resources plugin's `panels`): of the schema the point
5
+ // writes under a key of its `contributes` (`contributes` there): of the schema the point
6
6
  // declares, kept as written, for the declaring plugin to read, only the commands in it listed where installing confirms them.
7
7
  import { CORE_PROTOCOLS } from '@coda-console/protocol/protocol';
8
8
  import { pointProblem, valueProblem } from '@coda-console/protocol/validate';
@@ -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 = 5;
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).
@@ -109,7 +109,7 @@ export function wantedLevel(engines) {
109
109
  return typeof value === 'string' && /^[1-9][0-9]{0,8}$/.test(value) ? Number(value) : null;
110
110
  }
111
111
  // The keys of `contributes` Coda once read and no longer does: a manifest still writing one was made for what Coda took away.
112
- const RETIRED_CONTRIBUTES = ['endpoints', 'groups', 'events'];
112
+ const RETIRED_CONTRIBUTES = ['endpoints', 'groups', 'events', 'resources', 'panels'];
113
113
  // Those of them `contributes` writes, a manifest's or a version's registry metadata: what either is read with as its `retired`.
114
114
  export const retiredContributes = contributes => RETIRED_CONTRIBUTES.filter(key => Object.hasOwn(contributes ?? {}, key));
115
115
  // Whether a plugin runs on this machine (docs/plugins.md「兼容」): the one judgment reading a source, installing, the store and
@@ -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
- export function usesRefusal(plugin, kind, need, definition) {
191
- if (ownsProtocol(plugin, kind)) return null;
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,18 +20,15 @@ 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, COMMIT, INTEGRITY, NPM_VERSION, npmPluginId, repoAddress, sourcePath } from './plugin-source-rules.mjs';
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
- // contribution point's (contribution-points.mjs), or for a point another plugin declares to take it (the resources
34
- // plugin's `panels`), refused when neither does.
30
+ // contribution point's (contribution-points.mjs), or for a point another plugin declares to take it, refused when
31
+ // neither does.
35
32
  export const CONTRIBUTES = TYPES;
36
33
  // The shortest a kind's records are given again by its command on their own (`contributes.records`' `every`).
37
34
  const EVERY_LEAST = 60_000;
@@ -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, and its npm dependencies that are plugins
216
- // (`dependencies`), each with where it comes from, which a version from that same place may stand in for (docs/plugins.md
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) {
@@ -270,7 +232,7 @@ export function publishedFrom(repository) {
270
232
  }
271
233
 
272
234
  // ---- Its processes ----
273
- // What Coda gives each process it runs for a plugin (docs/plugins.md「插件进程的环境」) — its agents', its panels', the steps
235
+ // What Coda gives each process it runs for a plugin (docs/plugins.md「插件进程的环境」) — its agents', the steps
274
236
  // of its workflows, the commands it puts on sessions' PATH —, beside the environment the process runs in: Coda's own Node,
275
237
  // the plugin's directory and its state on this backend (`stateDir(id)`, none without one), its settings as they apply
276
238
  // (`settings`), what installed plugins plug into its points and where the plugins it depends on are, whose files (an npm
@@ -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 built-in plugin and a version of it from where it comes from. A built-in package's copy names both, the
76
- // directory being the one its package says it is published from (components/plugins/plugin-rules.mjs dependencyPlugins).
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
- // An installed built-in plugin (including an update from its own source) reserves its ID. Uninstalling frees the name;
252
- // an installed replacement from another source keeps it across refreshes and updates.
253
- export function builtinProblem(id, source, builtin, installed) {
254
- const copy = builtin.get(id), row = installed.get(id);
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: an installed built-in one's ID from another source (builtinProblem), what a component's
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, installed) {
273
- const named = builtinProblem(plugin.id, plugin.source, builtin, installed);
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);
@@ -350,7 +352,7 @@ export function contributions(plugin, catalog = new Map(), defined = () => null)
350
352
  ...(v.checkout ? { checkout: v.checkout } : {}), ...(v.provides ? { provides: v.provides } : {}) })) } : {}),
351
353
  ...Object.assign({}, ...[...CONTRIBUTION_POINTS.values()].map(point => point.confirm?.(plugin))),
352
354
  extensions: plugin.extensions.map(e => ({ plugin: e.plugin, point: e.point, command: e.command })),
353
- // What it writes for points under keys of `contributes` (the resources plugin's panels): by point, how many and the
355
+ // What it writes for points under keys of `contributes`: by point, how many and the
354
356
  // commands in them, each with what it is for.
355
357
  ...(offers.length ? { offers } : {}),
356
358
  ...(plugin.setup ? { setup: { command: plugin.setup.command } } : {}),
@@ -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 ?? builtin.get(id)?.source ?? { kind: 'builtin' } : null,
394
- problem: problem ?? incompatible?.error ?? (plugin ? catalogProblem(plugin, catalog, builtin, installed) ?? offerProblem(plugin, after) : null)
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) } : {}) };
@@ -1,10 +1,10 @@
1
1
  {
2
- "commit": "6152218e2afb04e9afba3fad4c312e5f155a1d56",
3
- "apiLevel": 5,
2
+ "commit": "b5d62fd390b27f300bb43d6c5b6bb6bb133a8515",
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.15.0",
7
+ "@coda-console/protocol": "6.17.0",
8
8
  "ajv": "8.20.0",
9
9
  "yaml": "2.9.1"
10
10
  },
@@ -19,9 +19,9 @@
19
19
  "check/coda/adapters/clock.mjs": "79733b9ac3807b3da147fe8b782318a1cb42d1d51b9c8b81a507c13c7a8025fd",
20
20
  "check/coda/adapters/plugin-command.mjs": "47bd580f7142780933315f64d4562d23a61f996b3f411cfb583333261b590ee8",
21
21
  "check/coda/adapters/plugin-events.mjs": "5b6b98063a2a2f3d112908ad9fdc08119ec1bd577ca765811799ef6d1030dfe4",
22
- "check/coda/adapters/plugin-modules.mjs": "dd916da2c3411087fc09ed20e2c1c5f10ce450ed096b9054db7d9c47000a2857",
22
+ "check/coda/adapters/plugin-modules.mjs": "9a86b356897864a9f0dabc5ec73deaf0c54e4913e538cd5f759f4a1959213cc9",
23
23
  "check/coda/adapters/plugin-stream-services.mjs": "1e92eb3dd9d844d4f2773a60bc2941aae3c9cda10d9e96e31de9f09f51e672e5",
24
- "check/coda/adapters/plugin-test-host.d.mts": "d0592b00e898da7abae8d48b0ee6f72cd807ac9b7a7e8d185148904082850575",
24
+ "check/coda/adapters/plugin-test-host.d.mts": "c86b3244f3f5d58fe5f25b50c2e7bd00983d72b841ca4a978f10d4dfa6ceae87",
25
25
  "check/coda/adapters/plugin-test-host.mjs": "7c8e5ee46792181bfd3b2f4d67d1ac6844c64c2af678655308cfdeb5ecf84c42",
26
26
  "check/coda/adapters/process-relay.mjs": "5d379bd7ad07138d69c7accbe769d71294843399539bdf1fab8b6a1ddf13b9d8",
27
27
  "check/coda/components/composers/contributions.mjs": "43d3e15fb1f63b7b5c20716030d9537613809ed5cc2fb466a3678bfa308f9f92",
@@ -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": "54b2fbc9db34ae0a18fe6193714287b82fff3f796069318c9a1e5f92d48475aa",
33
+ "check/coda/components/plugins/plugin-extensions.mjs": "31ce03ceec14ef36cb5db5a197e444567286de43a2162eec3af7d005697daa63",
34
34
  "check/coda/components/plugins/plugin-icons.mjs": "ec29cadce4948ecacabfe911a2ceb45414b96e28971358acbc4e952bedc78e1a",
35
- "check/coda/components/plugins/plugin-rules.mjs": "32cf0a3956a646e0c18cea62da4c0969af9f4c6f60ab1a4970937163a13a878a",
36
- "check/coda/components/plugins/plugin-source-rules.mjs": "d3bf278c4fb35e99f602ea81419dff0ad0c2753ee20e6e3b648d2fecf82da44f",
35
+ "check/coda/components/plugins/plugin-rules.mjs": "546785b38813791ea6cd423952178d880524184cc782003bba7f3387787c1fd5",
36
+ "check/coda/components/plugins/plugin-source-rules.mjs": "2ed213f001929a54b05237e7bce4bf561f20068f552234ef74648c1ec53e3d09",
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",
@@ -57,10 +57,7 @@
57
57
  "check/coda/kernel/project-protocol.mjs": "69874b2b226f111b156724454c968e829018f1caac0a02883f13e1f980482739",
58
58
  "check/coda/kernel/thread-protocol.mjs": "58bbd66c0058552f26c1b4ed08231e9afd142f2b7a9f50ed6bd66cbfe8f579e1",
59
59
  "check/coda/kernel/usage-protocol.mjs": "4bd07a4d4c63c75e5328ef7e907bfa4e83af430f7f04a08cf8eb883a23ddc51f",
60
- "check/coda/resources/plugins/resources/panels/panels.mjs": "37ffc6a4ede3e4612cd13cf1a973a90892f8ce7e8a0f055e7cfe86313aae5224",
61
- "check/coda/resources/plugins/resources/panels/rules.mjs": "a433d4e84138937297ec31a8912aab7bd48bba4b66da3a10978edb1367613c96",
62
- "check/coda/resources/plugins/resources/panels/sign-in.mjs": "6eb016ef9a7f8aa03e0f4b2fa35ce139b8b1ff0deb4ec6e6a34139aed4f2841a",
63
- "check/coda/builtin.json": "0eb63a693c912f7e339cda0b76b85110f881784b92c7788c8ea6d0a1c7468ade",
64
- "check/coda/api-surface.txt": "47fe85e101b2315d6e5783e1e74050f493c2045ca6ea4577ad841d6643d8b238"
60
+ "check/coda/builtin.json": "a69fb8a1978b7cdb602f76cfae487e9b683026cf5c35a92c83fdd1b23b4eec83",
61
+ "check/coda/api-surface.txt": "7b21d96b9ccb36cf6dbbc04904ceb39299acc4e05488815297660b1e1c5caca3"
65
62
  }
66
63
  }
package/check/index.mjs CHANGED
@@ -1,12 +1,12 @@
1
1
  // The contract check of one plugin directory: the manifest and its workflows, its module if it has one, then each agent's handshake (and with `live`, a
2
- // turn) and each resource type's (or panel's) reading. The manifest is checked against Coda's schemas of its types and its workflows against Coda's
2
+ // turn) and the reading of its resource records. The manifest is checked against Coda's schemas of its types and its workflows against Coda's
3
3
  // dialect of BPMN (both the protocol's), the rest by Coda's own code (check/coda/), run where a plugin is written.
4
4
  import { resolve } from 'node:path';
5
5
  import { checkAgent } from './agent.mjs';
6
6
  import { checkLevel } from './level.mjs';
7
7
  import { checkManifest } from './manifest.mjs';
8
8
  import { checkModule } from './module.mjs';
9
- import { checkPanel } from './panel.mjs';
9
+ import { checkResources, RESOURCE } from './resources.mjs';
10
10
  import { checkWorkflows } from './workflows.mjs';
11
11
  import { Report } from './report.mjs';
12
12
 
@@ -19,12 +19,13 @@ export async function checkPlugin(dir, { agent: only = null, manifestOnly = fals
19
19
  checkLevel({ checked: plugin, say: report.scope('manifest'), note: report.scope('note') });
20
20
  const agents = only ? plugin.agents.filter(a => a.id === only) : plugin.agents;
21
21
  if (only && !agents.length) report.scope('manifest').fail('options.agent', `清单里没有能检查的 Agent ${only}`);
22
+ const resources = plugin.plugin.records?.some(one => one.kind === RESOURCE);
22
23
  if (manifestOnly) {
23
- if (agents.length || plugin.panels.length) report.scope('handshake').skip('options.manifest', '只查清单(--manifest):不启动 Agent、不读资源');
24
+ if (agents.length || resources) report.scope('handshake').skip('options.manifest', '只查清单(--manifest):不启动 Agent、不读资源');
24
25
  return { report, plugin };
25
26
  }
26
27
  if (plugin.plugin.main) await checkModule({ checked: plugin, say: report.scope('module'), note: report.scope('note'), limit: limits?.activate });
27
28
  for (const agent of agents) await checkAgent({ agent, checked: plugin, live, prompt, limits, say: level => report.scope(level, `Agent ${agent.id}`) });
28
- if (!only) for (const panel of plugin.panels) await checkPanel({ panel, checked: plugin, say: report.scope('handshake', `${panel.format === 'resources' ? '资源类型' : '面板'} ${panel.id}`) });
29
+ if (!only && resources) await checkResources({ checked: plugin, say: report.scope('handshake', `资源记录 ${RESOURCE}`) });
29
30
  return { report, plugin };
30
31
  }
@@ -1,4 +1,4 @@
1
- // What the checker starts and makes goes however it ends: the process groups of agents and panels it started (detached,
1
+ // What the checker starts and makes goes however it ends: the process groups of agents and record commands it started (detached,
2
2
  // so a Ctrl-C at the terminal does not reach them), what stops those it had Coda's code start, and its temporary
3
3
  // directories. bin/check.mjs turns SIGINT and SIGTERM into an exit, which runs this.
4
4
  import { mkdtempSync, rmSync } from 'node:fs';
@@ -13,9 +13,8 @@ import { HOST_PROTOCOLS } from './coda/application/host-protocols.mjs';
13
13
  import shipped from './coda/builtin.json' with { type: 'json' };
14
14
  import { CORE_PROTOCOLS } from '@coda-console/protocol/protocol';
15
15
  import { manifestProblem } from '@coda-console/protocol/validate';
16
- import { offerProblem, protocolDefinition, usesMissing } from './coda/components/plugins/plugin-extensions.mjs';
16
+ import { offerProblem, ownIncompatibility, protocolDefinition, usesMissing } from './coda/components/plugins/plugin-extensions.mjs';
17
17
  import { choosesModelProviders, pluginEnv, readPlugin, resolveSettings } from './coda/components/plugins/plugin-rules.mjs';
18
- import { panelDeclarations } from './coda/resources/plugins/resources/panels/rules.mjs';
19
18
 
20
19
  // The plugins Coda ships, by ID. Their files are Coda's, not here: none has a directory on this machine.
21
20
  const builtin = new Map(shipped.map(plugin => [plugin.id, { ...plugin, dir: undefined }]));
@@ -63,7 +62,7 @@ function pageSize(data) {
63
62
  }
64
63
 
65
64
  // Checks the manifest in `dir` and reports under `say` (report.mjs). Returns what the later levels need — the plugin as Coda
66
- // reads it, its agents and the panels the resources plugin can read, its settings for every project, the directories of the
65
+ // reads it, its agents, its settings for every project, the directories of the
67
66
  // plugins it depends on that are here, `definitionOf(kind)`, a protocol's definition as far as it is here (its own, Coda's
68
67
  // or a plugin's beside it; undefined if not), `definitions`, all of those that are here, and `env(stateDir)`, what Coda gives each of its processes —; null when it is no
69
68
  // plugin, or not of the types it names.
@@ -132,11 +131,10 @@ export function checkManifest(dir, say) {
132
131
  else say.pass('manifest.events', `用到 ${kind} 的权限组 ${group}:不含事件`);
133
132
  }
134
133
  }
135
- // Its resource types, or the panels before them, as the resources plugin reads what is written for its point once that is
136
- // of the point's schema: one declaring types has those alone.
137
- const format = plugin.offers?.resources ? 'resources' : 'panels', what = format === 'resources' ? '资源类型' : '面板';
138
- const panels = offered ? [] : panelDeclarations(plugin.offers?.[format]).map(panel => ({ ...panel, format }));
139
- for (const panel of panels) say.pass('manifest.panels', `${what} ${panel.id}「${panel.title}」:动作 ${Object.keys(panel.actions).join('、') || '无'}${panel.signIn ? ',可从 Coda 登录' : ''}`);
134
+ // The keys of `contributes` Coda retired (`resources`, `panels`): a manifest still writing one is one Coda calls for an update
135
+ // (ownIncompatibility, of the retired keys alone: the Level and the channel are another level's and the installing machine's).
136
+ const retired = ownIncompatibility({ retired: plugin.retired }, {});
137
+ if (retired) say.fail('manifest.retired', retired.error);
140
138
 
141
139
  // What its processes are given (pluginEnv): the plugins it depends on, those beside it here; one Coda ships has its
142
140
  // directory in Coda, not here, even with one of its ID beside it (Coda picks its own), and one found nowhere is not named.
@@ -152,6 +150,6 @@ export function checkManifest(dir, say) {
152
150
  // The model services of a machine with none, for one that chooses them; the PATH has no directories of Coda's own here.
153
151
  const env = stateDir => pluginEnv({ catalog: given, installed, id: plugin.id, stateDir: id => id === plugin.id ? stateDir : null, settings,
154
152
  providers: choosesModelProviders(plugin) ? {} : null, userPath: process.env.PATH ?? '' });
155
- return { id: plugin.id, manifest, plugin, agents: plugin.agents, panels, settings, dependencies, env, definitionOf: kind => protocolDefinition(kind, protocols, hosted),
153
+ return { id: plugin.id, manifest, plugin, agents: plugin.agents, settings, dependencies, env, definitionOf: kind => protocolDefinition(kind, protocols, hosted),
156
154
  definitions: [...Object.values(CORE_PROTOCOLS), ...HOST_PROTOCOLS, ...[...protocols.values()].flatMap(each => each.protocols ?? [])] };
157
155
  }
package/check/module.mjs CHANGED
@@ -3,7 +3,7 @@
3
3
  // handshake, for a plugin with a `main`.
4
4
  import { join } from 'node:path';
5
5
  import { pathToFileURL } from 'node:url';
6
- import { protocolHasRecords } from '@coda-console/protocol/protocol';
6
+ import { protocolHasRecords, RESOURCE_PROTOCOL } from '@coda-console/protocol/protocol';
7
7
  import { usesRefusal } from './coda/components/plugins/plugin-extensions.mjs';
8
8
  import { createTestHost } from './coda/adapters/plugin-test-host.mjs';
9
9
  import { closing, temporary } from './leftovers.mjs';
@@ -20,7 +20,6 @@ export const NOT_PROVIDED = {
20
20
  'services.register': { message: '测试宿主没有后端,不答 serviceRegister' },
21
21
  tell: { message: '测试宿主没有后端,不答 tell' },
22
22
  api: { message: 'Coda 还在启动,稍后再试' },
23
- environment: { message: '测试宿主没有后端,不答 environment' },
24
23
  'protocols.resolve': { message: '测试宿主没有后端,不答 provider' },
25
24
  'protocols.upload': { message: '测试宿主没有后端,不答 provider' },
26
25
  'pluginSetup.retry': { code: 'unsupported', message: '测试宿主不提供 coda.pluginSetup' },
@@ -113,8 +112,9 @@ function unfinished({ plugin, definitions, definitionOf }, e, from, absent, used
113
112
  }
114
113
 
115
114
  // What the manifest says it implements against what the module registered, both ways: an operation not implemented by a command
116
- // is the module's to register (but for those only Coda answers); one registered that the definition lacks is no operation of the
117
- // protocol. (A protocol the manifest does not implement is refused by the test host as the module registers: it fails `activate`.)
115
+ // is the module's to register (but for those only Coda answers, and for `coda/resource@1`, whose operations a plugin offers one by
116
+ // one: the manifest writes only those it has); one registered that the definition lacks is no operation of the protocol.
117
+ // (A protocol the manifest does not implement is refused by the test host as the module registers: it fails `activate`.)
118
118
  function compare({ plugin, definitionOf }, { operations }, say) {
119
119
  const registered = new Set(operations.map(({ kind, operation }) => `${kind} ${operation}`));
120
120
  let problems = 0, incomplete = false;
@@ -123,7 +123,7 @@ function compare({ plugin, definitionOf }, { operations }, say) {
123
123
  if (!definition) { incomplete = true; say.warn('module.registered', `未完成核对:协议 ${kind} 的定义不在本地,查不了模块登记了它的哪些操作`); continue; }
124
124
  // `codaOnly`: Coda's own protocol's operation that Coda alone answers.
125
125
  for (const [operation, { codaOnly }] of Object.entries(definition.operations ?? {}))
126
- if (!codaOnly && !Object.hasOwn(commands, operation) && !registered.has(`${kind} ${operation}`)) { problems++; say.fail('module.unregistered', `清单写了实现 ${kind} 的操作 ${operation},模块没登记(coda.handle)`); }
126
+ if (!codaOnly && kind !== RESOURCE_PROTOCOL.name && !Object.hasOwn(commands, operation) && !registered.has(`${kind} ${operation}`)) { problems++; say.fail('module.unregistered', `清单写了实现 ${kind} 的操作 ${operation},模块没登记(coda.handle)`); }
127
127
  }
128
128
  for (const { kind, operation } of operations) {
129
129
  const definition = definitionOf(kind);
package/check/report.mjs CHANGED
@@ -1,4 +1,4 @@
1
- // What a check found, in the order found: each line a level (清单, 模块, 握手, 一轮, 说明), what it is about (the plugin, an agent, a panel),
1
+ // What a check found, in the order found: each line a level (清单, 模块, 握手, 一轮, 说明), what it is about (the plugin, an agent, a resource record),
2
2
  // whether it passed, failed, was skipped or is worth a look (warn, info), a rule ID the tests name, and a line of Chinese.
3
3
  export const LEVELS = { manifest: '清单', module: '模块', handshake: '握手', live: '一轮', note: '说明' };
4
4
  const MARKS = { pass: '✓', fail: '✗', warn: '!', skip: '-', info: '·' };
@@ -0,0 +1,99 @@
1
+ // A plugin's resource records (`coda/resource@1`, written by the command its `contributes.records` names for the kind) at the
2
+ // handshake level: the command run as Coda's host runs a record command (cwd the plugin's directory, its own environment,
3
+ // `{}` on standard input, `CODA_RECORD_KIND`, 30 seconds), and what it printed held to what the host holds it to before it
4
+ // writes the plugin's whole set of the kind (backend components/plugins/record-commands.mjs, components/protocols/providers.mjs):
5
+ // an array of `{ key, spec }`, each at most 64 KiB, each key once, the spec the protocol's schema, `spec.type` the key; and a
6
+ // button's `args` at most 4096 bytes, which no schema can say. Its operations (`act`, `sign-in`, `install`) are not run: a button
7
+ // may change things, and only a person presses it.
8
+ //
9
+ // For the tests of a plugin's resources: `resourceRecords(dir, { env })` runs the command and gives the records it printed, and
10
+ // throws what is wrong; `resourceRecordsProblem(output)` judges what is already at hand.
11
+ import { spawn } from 'node:child_process';
12
+ import { readFileSync } from 'node:fs';
13
+ import { dirname, join } from 'node:path';
14
+ import { RESOURCE_PROTOCOL } from '@coda-console/protocol/protocol';
15
+ import { pointProblem } from '@coda-console/protocol/validate';
16
+ import { fileCommand, runPluginCommand, settle } from './coda/adapters/plugin-command.mjs';
17
+ import { checkLabels, checkReplace, checkWrite } from './coda/components/protocols/record-rules.mjs';
18
+ import { cleared } from './coda/components/plugins/plugin-rules.mjs';
19
+ import { group, temporary } from './leftovers.mjs';
20
+
21
+ export const RESOURCE = RESOURCE_PROTOCOL.name;
22
+ // A record as the host takes it (at most, as JSON), and a button's `args` (as JSON, in bytes).
23
+ export const ENTRY = 64 * 1024, ARGS = 4096;
24
+ // How long the host gives a record command.
25
+ const TIMEOUT = 30_000;
26
+ const bytes = value => Buffer.byteLength(JSON.stringify(value));
27
+
28
+ // What is wrong with what a resource record command printed (`output`, parsed), as the host takes it, or null: the first thing
29
+ // found, naming the record by its place in the array.
30
+ export function resourceRecordsProblem(output) {
31
+ if (!Array.isArray(output)) return '命令的输出不是数组';
32
+ const big = output.findIndex(one => bytes(one) > ENTRY);
33
+ if (big >= 0) return `第 ${big + 1} 条记录超过 ${ENTRY / 1024} KiB`;
34
+ try { checkReplace(output); } catch (error) { return error.message; }
35
+ for (const [i, { key, ...value }] of output.entries()) {
36
+ const at = `第 ${i + 1} 条记录(${key})`;
37
+ try { checkWrite(value); checkLabels(value.labels ?? null); } catch (error) { return `${at}:${error.message}`; }
38
+ const problem = pointProblem(RESOURCE_PROTOCOL.spec, value.spec, `${at} spec`)
39
+ ?? (Object.hasOwn(value, 'status') ? pointProblem(RESOURCE_PROTOCOL.status, value.status, `${at} status`) : null);
40
+ if (problem) return problem;
41
+ if (value.spec.type !== key) return `${at}:spec.type(${value.spec.type})须与 key 相同`;
42
+ for (const [r, resource] of value.spec.resources.entries()) for (const [b, { args }] of resource.buttons.entries())
43
+ if (args !== undefined && bytes(args) > ARGS) return `${at}:spec.resources[${r}].buttons[${b}].args 序列化后 ${bytes(args)} 字节,超过 ${ARGS} 字节`;
44
+ }
45
+ return null;
46
+ }
47
+
48
+ // What starts a command: a child of the checker's, in a group of its own, which goes however the checker ends.
49
+ const ends = new Map();
50
+ const start = {
51
+ spawn: (owner, _entry, file, args, options) => {
52
+ const child = spawn(file, args, { ...options, detached: true });
53
+ ends.set(owner, group(child.pid));
54
+ return child;
55
+ },
56
+ remove: owner => { ends.get(owner)?.(); ends.delete(owner); },
57
+ };
58
+
59
+ // The plugin's resource record command `command` (a file in `dir`) run, as `{ result }`, the array it printed, or `{ error }`,
60
+ // the message of why it gave none. `env`: the environment of the plugin's processes, as the host gives it.
61
+ export async function runResourceCommand(dir, command, env) {
62
+ const ran = await runPluginCommand(fileCommand(command), { start, cwd: dir, env: { ...env, CODA_RECORD_KIND: RESOURCE }, input: '{}', timeoutMs: TIMEOUT });
63
+ const { result, error } = settle(ran, { what: '记录命令' });
64
+ return error ? { error: error.message } : { result };
65
+ }
66
+
67
+ // The records the plugin in `dir` gives of `coda/resource@1`: its manifest's command for the kind run (`env`: the environment of
68
+ // its processes, for a test that puts its own in place of the machine's, as the host gives it; by default this process's, with
69
+ // `CODA_PLUGIN_PATH` the plugin's directory and this Node first on the PATH), and what it printed, `[{ key, spec }]`, held to the
70
+ // host's limits. Throws what is wrong: the command's failure, or the first problem of its output (`resourceRecordsProblem`).
71
+ export async function resourceRecords(dir, { env = { ...process.env, CODA_PLUGIN_PATH: dir, PATH: [dirname(process.execPath), process.env.PATH].filter(Boolean).join(':') } } = {}) {
72
+ const { contributes } = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')), found = contributes?.records?.find(one => one.kind === RESOURCE);
73
+ if (!found) throw new Error(`清单的 contributes.records 里没有 ${RESOURCE}`);
74
+ const { error, result } = await runResourceCommand(dir, found.command, env);
75
+ if (error) throw new Error(error);
76
+ const problem = resourceRecordsProblem(result);
77
+ if (problem) throw new Error(problem);
78
+ return result;
79
+ }
80
+
81
+ // The check of the plugin `checked` (the manifest level's reading, manifest.mjs checkManifest) that writes `coda/resource@1`
82
+ // records: its command run once in the environment Coda gives its processes, what it printed judged as the tests judge it.
83
+ export async function checkResources({ checked, say }) {
84
+ const { plugin } = checked, { command } = plugin.records.find(one => one.kind === RESOURCE);
85
+ // The plugin's state directory, which Coda keeps for it; here a temporary one, gone however the checker ends.
86
+ const state = temporary('coda-plugin-check-state-');
87
+ try {
88
+ const env = { ...cleared(process.env), ...checked.env(state.dir) };
89
+ delete env.CODA_THREAD_ID;
90
+ env.PATH = [dirname(process.execPath), env.PATH].filter(Boolean).join(':');
91
+ env.CODA_GROUPS_STATE = JSON.stringify({ now: Date.now(), groups: [] });
92
+ const started = Date.now(), { error, result } = await runResourceCommand(plugin.dir, command, env), took = ((Date.now() - started) / 1000).toFixed(1);
93
+ if (error) return say.fail('resource.read', `读取没成功:${error}`);
94
+ const problem = resourceRecordsProblem(result);
95
+ if (problem) return say.fail('resource.output', `输出不合 ${RESOURCE}:${problem}`);
96
+ const resources = result.flatMap(record => record.spec.resources), count = key => resources.reduce((n, resource) => n + resource[key].length, 0);
97
+ say.pass('resource.read', `读取 ${took} 秒,输出合 ${RESOURCE}:${result.length} 条记录,${resources.length} 个资源,读数 ${count('values')} 个,按钮 ${count('buttons')} 个`);
98
+ } finally { state.remove(); }
99
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coda-console/plugin-check",
3
- "version": "0.8.26",
4
- "description": "Coda 插件的契约检查:扮演 Coda 读清单、启动 Agent 讲 ACP 与 _coda/wait、读资源类型(与旧的面板)。",
3
+ "version": "0.8.28",
4
+ "description": "Coda 插件的契约检查:扮演 Coda 读清单、启动 Agent 讲 ACP 与 _coda/wait、读资源记录。",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -13,7 +13,8 @@
13
13
  },
14
14
  "exports": {
15
15
  "./types": "./check/coda/adapters/plugin-test-host.d.mts",
16
- "./test-host": "./check/coda/adapters/plugin-test-host.mjs"
16
+ "./test-host": "./check/coda/adapters/plugin-test-host.mjs",
17
+ "./resources": "./check/resources.mjs"
17
18
  },
18
19
  "bin": {
19
20
  "coda-plugin-check": "bin/check.mjs"
@@ -29,7 +30,7 @@
29
30
  "dependencies": {
30
31
  "@0dep/piso": "5.2.0",
31
32
  "@agentclientprotocol/sdk": "2.6.0",
32
- "@coda-console/protocol": "6.15.0",
33
+ "@coda-console/protocol": "6.17.0",
33
34
  "ajv": "8.20.0",
34
35
  "yaml": "2.9.1"
35
36
  },
@@ -3,20 +3,23 @@
3
3
  "displayName": "运行时间(模板)",
4
4
  "description": "最小的资源插件模板",
5
5
  "version": "1.0.0",
6
+ "engines": {
7
+ "coda": 6
8
+ },
6
9
  "contributes": {
7
- "resources": [
10
+ "protocolImplementations": [
8
11
  {
9
- "id": "uptime",
10
- "title": "运行时间",
11
- "command": "node \"$CODA_PLUGIN_PATH/scripts/resources.mjs\"",
12
- "maxAge": "PT1M",
13
- "actions": {
14
- "mark": {
15
- "command": "node \"$CODA_PLUGIN_PATH/scripts/mark.mjs\"",
16
- "confirm": "记下此刻?\n之后显示距这次记下过了多久。"
17
- }
12
+ "kind": "coda/resource@1",
13
+ "commands": {
14
+ "act": "scripts/act.mjs"
18
15
  }
19
16
  }
17
+ ],
18
+ "records": [
19
+ {
20
+ "kind": "coda/resource@1",
21
+ "command": "scripts/resource.mjs"
22
+ }
20
23
  ]
21
24
  }
22
25
  }
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env node
2
+ // 资源上按钮的动作(`coda/resource@1` 的 `act` 操作,contributes.protocolImplementations 里的 commands.act):
3
+ // Coda 在点按钮(并确认)后运行它,`{ action, args }` 在标准输入里(args 是按钮带的参数,这个按钮没带,是 {})。
4
+ // 退出码 0 且标准输出是 `{}` 就算成功;失败就退出 1,标准错误的最后一行是按钮旁显示的原因。
5
+ // 不认识的动作要让调用失败,不要悄悄成功。
6
+ import { readFileSync, writeFileSync } from 'node:fs';
7
+ import { join } from 'node:path';
8
+
9
+ // 「记下此刻」:存在插件的状态目录里(CODA_PLUGIN_STATE,跨读取、跨版本保留),记录命令下次运行时读它。
10
+ function mark() {
11
+ writeFileSync(join(process.env.CODA_PLUGIN_STATE, 'mark.json'), JSON.stringify({ at: new Date().toISOString() }));
12
+ }
13
+
14
+ const actions = { mark };
15
+ const { action } = JSON.parse(readFileSync(0, 'utf8'));
16
+ try {
17
+ if (!Object.hasOwn(actions, action)) throw new Error(`不认识的动作:${action}`);
18
+ actions[action]();
19
+ console.log('{}');
20
+ } catch (error) { console.error(error.message); process.exitCode = 1; }