@coda-console/plugin-check 0.8.28 → 0.8.30

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.
Files changed (32) hide show
  1. package/README.md +13 -23
  2. package/check/coda/adapters/plugin-events.mjs +3 -10
  3. package/check/coda/adapters/plugin-modules.mjs +19 -23
  4. package/check/coda/adapters/plugin-setup-api.mjs +5 -0
  5. package/check/coda/adapters/plugin-test-host.d.mts +0 -13
  6. package/check/coda/adapters/plugin-test-host.mjs +6 -25
  7. package/check/coda/api-surface.txt +3 -6
  8. package/check/coda/application/contributions.mjs +1 -0
  9. package/check/coda/builtin.json +15 -49
  10. package/check/coda/components/plugins/contribution-points.mjs +1 -2
  11. package/check/coda/components/plugins/git-contributions.mjs +10 -6
  12. package/check/coda/components/plugins/plugin-extensions.mjs +9 -72
  13. package/check/coda/components/plugins/plugin-rules.mjs +16 -20
  14. package/check/coda/components/plugins/plugin-source-rules.mjs +13 -29
  15. package/check/coda/components/plugins/self-contributions.mjs +13 -0
  16. package/check/coda/components/protocols/record-rules.mjs +8 -0
  17. package/check/coda/contract.json +18 -26
  18. package/check/manifest.mjs +10 -8
  19. package/package.json +2 -2
  20. package/templates/module/main.mjs +6 -13
  21. package/templates/module/package.json +7 -7
  22. package/templates/module/test/main.test.mjs +7 -23
  23. package/check/coda/application/host-protocols.mjs +0 -19
  24. package/check/coda/components/workflows/workflow-protocol-definitions.mjs +0 -362
  25. package/check/coda/domain/accord.mjs +0 -144
  26. package/check/coda/domain/jcs.mjs +0 -58
  27. package/check/coda/domain/usage.mjs +0 -56
  28. package/check/coda/kernel/accord-protocol.mjs +0 -168
  29. package/check/coda/kernel/plugin-protocol.mjs +0 -56
  30. package/check/coda/kernel/project-protocol.mjs +0 -52
  31. package/check/coda/kernel/thread-protocol.mjs +0 -167
  32. package/check/coda/kernel/usage-protocol.mjs +0 -97
@@ -1,97 +0,0 @@
1
- // Coda's own protocol of the tokens the turns hosted here used, `coda/usage@1`: one record, this machine's (`local`), and its
2
- // query `buckets`, the counts kept per UTC ten minutes, agent and model (domain/usage.mjs, usage_buckets). Nothing but the counts
3
- // is said: no session, directory, text or cost. The record is read through (Protocols.serve), nothing is kept of it, it is
4
- // no section and not in the record directory: another machine's is not read.
5
- import { createHash } from 'node:crypto';
6
- import { requireCondition as check } from '../domain/errors.mjs';
7
- import { USAGE_BUCKET_MS } from '../domain/usage.mjs';
8
- import { canonical } from '../domain/values.mjs';
9
-
10
- export const USAGE = 'coda/usage@1';
11
- export const USAGE_LOCAL_ID = 'local';
12
- export const MAX_USAGE_SPAN_MS = 90 * 24 * 60 * 60 * 1000;
13
- // About a fortnight of active buckets at a read, and the envelope of a call is far under 1 MiB even with 200-character
14
- // four-byte model names.
15
- export const MAX_USAGE_LIMIT = 500;
16
-
17
- const time = { type: 'integer', minimum: 0, maximum: Number.MAX_SAFE_INTEGER };
18
-
19
- export const USAGE_PROTOCOL = {
20
- kind: USAGE,
21
- title: '用量',
22
- spec: { type: 'object', properties: { bucketMs: { type: 'integer', minimum: 1 } }, required: ['bucketMs'], additionalProperties: false },
23
- status: { type: 'object', additionalProperties: false },
24
- operations: {
25
- // The buckets starting in `[from, to)`, newest first, of an agent and a model if given; `truncated` when more than `limit` are.
26
- buckets: { safe: true,
27
- input: {
28
- type: 'object',
29
- properties: {
30
- from: time,
31
- to: time,
32
- agent: { type: 'string', minLength: 1, maxLength: 100 },
33
- model: { type: 'string', minLength: 1, maxLength: 200 },
34
- limit: { type: 'integer', minimum: 1, maximum: MAX_USAGE_LIMIT },
35
- },
36
- required: ['from', 'to'],
37
- additionalProperties: false,
38
- },
39
- result: {
40
- type: 'object',
41
- properties: {
42
- buckets: {
43
- type: 'array',
44
- items: {
45
- type: 'object',
46
- properties: {
47
- start: time,
48
- agent: { type: 'string', minLength: 1 },
49
- model: { type: 'string', minLength: 1 },
50
- input: time,
51
- output: time,
52
- cacheRead: time,
53
- cacheWrite: time,
54
- turns: time,
55
- reportedTurns: time,
56
- },
57
- required: ['start', 'agent', 'model', 'input', 'output', 'cacheRead', 'cacheWrite', 'turns', 'reportedTurns'],
58
- additionalProperties: false,
59
- },
60
- },
61
- truncated: { type: 'boolean' },
62
- },
63
- required: ['buckets', 'truncated'],
64
- additionalProperties: false,
65
- } },
66
- },
67
- // What a plugin may ask for in its `uses` (docs/plugins.md「用到的权限」), on trial while the protocol is not stable.
68
- permissions: {
69
- read: { title: '查看本机的 Token 用量统计', trial: true, read: true, operations: ['buckets'] },
70
- },
71
- };
72
-
73
- const record = () => {
74
- const spec = { bucketMs: USAGE_BUCKET_MS }, status = {};
75
- return { kind: USAGE, id: USAGE_LOCAL_ID, provider: 'coda', createdAt: new Date(0).toISOString(), creationSequence: 0, spec, status,
76
- revision: `local:${createHash('sha256').update(canonical([spec, status])).digest('hex').slice(0, 16)}` };
77
- };
78
- export const usageRecords = () => ({
79
- get: id => id === USAGE_LOCAL_ID ? record() : null,
80
- list: () => [record()],
81
- });
82
-
83
- // Its query through the unified call (components/protocols/calls.mjs `handle`): `buckets`, read from the store as it keeps them,
84
- // for this machine's callers alone: the record's ID is the same on every machine, so a call another backend sends is refused.
85
- export function usageQueries(calls, { store }) {
86
- calls.handle(USAGE, 'buckets', ({ input }) => {
87
- check(input.from <= input.to, 'invalid_input', 'from 不能大于 to');
88
- check(input.to - input.from <= MAX_USAGE_SPAN_MS, 'invalid_input', '单次查询时间跨度不能超过 90 天');
89
- for (const [name, value] of [['agent', input.agent], ['model', input.model]])
90
- check(value === undefined || (!value.includes('\0') && value.trim().length > 0), 'invalid_input', `${name} 不能为空或含 NUL`);
91
- const limit = input.limit ?? MAX_USAGE_LIMIT;
92
- if (input.from === input.to) return { buckets: [], truncated: false };
93
- const rows = store.usageBuckets({ from: input.from, to: input.to, agent: input.agent, model: input.model, limit: limit + 1 });
94
- return { buckets: rows.slice(0, limit).map(row => ({ start: row.bucketStart, agent: row.agent, model: row.model, input: row.input, output: row.output,
95
- cacheRead: row.cacheRead, cacheWrite: row.cacheWrite, turns: row.turns, reportedTurns: row.reportedTurns })), truncated: rows.length > limit };
96
- }, { admit: ({ caller }) => check(caller === calls.self(), 'forbidden', '用量统计只在本机读取') });
97
- }