@coda-console/plugin-check 0.8.20 → 0.8.22
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 +3 -3
- package/check/agent.mjs +2 -1
- package/check/coda/adapters/call-limit.mjs +56 -0
- package/check/coda/adapters/clock.mjs +48 -0
- package/check/coda/adapters/plugin-command.mjs +171 -0
- package/check/coda/adapters/plugin-modules.mjs +505 -0
- package/check/coda/adapters/plugin-stream-services.mjs +53 -0
- package/check/coda/adapters/plugin-test-host.d.mts +316 -0
- package/check/coda/adapters/plugin-test-host.mjs +404 -0
- package/check/coda/adapters/process-relay.mjs +74 -0
- package/check/coda/adapters/provider-api.mjs +147 -40
- package/check/coda/application/contributions.mjs +1 -0
- package/check/coda/builtin.json +66 -67
- package/check/coda/components/composers/schemas.mjs +4 -2
- package/check/coda/components/concurrency-groups/schemas.mjs +102 -0
- package/check/coda/components/plugins/plugin-extensions.mjs +104 -10
- package/check/coda/components/plugins/plugin-rules.mjs +19 -1
- package/check/coda/components/plugins/plugin-source-rules.mjs +34 -14
- package/check/coda/components/plugins/process-contributions.mjs +9 -0
- package/check/coda/components/prompt-recovery/prompt-recovery.mjs +2 -49
- package/check/coda/components/protocols/record-rules.mjs +70 -0
- package/check/coda/components/protocols/types.mjs +22 -4
- package/check/coda/components/workflows/contributions.mjs +2 -6
- package/check/coda/contract.json +29 -21
- package/check/coda/domain/errors.mjs +5 -1
- package/check/coda/domain/resume-prompt.mjs +93 -0
- package/check/coda/domain/session-env.mjs +1 -1
- package/check/coda/resources/plugins/resources/panels/panels.mjs +26 -17
- package/check/coda/resources/plugins/resources/panels/rules.mjs +0 -4
- package/check/coda/resources/plugins/resources/panels/sign-in.mjs +32 -41
- package/check/manifest.mjs +0 -1
- package/check/panel.mjs +6 -5
- package/package.json +2 -2
- package/check/coda/adapters/admission-api.mjs +0 -22
- package/check/coda/components/protocols/creation.mjs +0 -19
- package/check/coda/domain/resource-admission.mjs +0 -15
- package/check/coda/resources/plugins/resources/panels/command.mjs +0 -46
- package/check/command.mjs +0 -48
|
@@ -1,50 +1,157 @@
|
|
|
1
|
-
// The plugin host's local API.
|
|
2
|
-
// its own actions. `handle` keeps a plugin's operation handlers in `operations`, by kind and operation, for the protocols
|
|
1
|
+
// The plugin host's local API. `handle` keeps a plugin's operation handlers in `operations`, by kind and operation, for the protocols
|
|
3
2
|
// its manifest implements.
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import { ContractError } from '../domain/errors.mjs';
|
|
4
|
+
import { abortError, bounded, checkResult, EFFECTS, timeoutError } from './call-limit.mjs';
|
|
5
|
+
export { abortError };
|
|
6
|
+
|
|
7
|
+
const CODE = /^[a-z][a-z0-9_]{0,63}$/;
|
|
8
|
+
const sayable = ({ code, effects, retryAfterMs }) => typeof code === 'string' && CODE.test(code)
|
|
9
|
+
&& (effects === undefined || EFFECTS.includes(effects)) && (retryAfterMs === undefined || (Number.isSafeInteger(retryAfterMs) && retryAfterMs >= 0));
|
|
10
|
+
// `coda.error`: a failure a handler throws, with its code, how much it did (`effects`, unsaid `unknown`) and how long its
|
|
11
|
+
// caller had better wait before asking again.
|
|
12
|
+
export function codaError(code, message, { effects, retryAfterMs } = {}) {
|
|
13
|
+
if (!sayable({ code, effects, retryAfterMs })) throw new TypeError('coda.error(code, message, { effects?, retryAfterMs? }):code 是小写字母、数字和「_」,effects 是 none、some 或 unknown,retryAfterMs 是非负整数毫秒');
|
|
14
|
+
return new ContractError(code, String(message), { effects, retryAfterMs });
|
|
15
|
+
}
|
|
16
|
+
// A failure as it crosses the plugin host's channel. One that says no code, or not as `coda.error` would, is the handler's
|
|
17
|
+
// own, of which nothing is known (`handler_error`, `unknown`), whatever `effects` it says. What goes out is made first, and
|
|
18
|
+
// its envelope is what 1 MiB is of (`bounded`, adapters/call-limit.mjs). `effects`, given: what it says instead, whatever
|
|
19
|
+
// the handler says — a query's, `none`, as it only reads.
|
|
20
|
+
export function failureOf(error, effects = null) {
|
|
21
|
+
const message = String(error?.message ?? error);
|
|
22
|
+
if (!error || typeof error !== 'object' || !sayable(error)) return bounded({ code: 'handler_error', message, effects: effects ?? 'unknown' });
|
|
23
|
+
return bounded({ code: error.code, message, effects: effects ?? error.effects ?? 'unknown', ...(error.retryAfterMs === undefined ? {} : { retryAfterMs: error.retryAfterMs }) });
|
|
24
|
+
}
|
|
25
|
+
// What crossed the channel as an error, an error again: a failure as `failureOf` says it, or a message alone.
|
|
26
|
+
export function failed(said) {
|
|
27
|
+
if (!said || typeof said !== 'object') return new Error(String(said));
|
|
28
|
+
const { code, message, effects, retryAfterMs } = failureOf(said);
|
|
29
|
+
return new ContractError(code, message, { effects, retryAfterMs });
|
|
30
|
+
}
|
|
31
|
+
// Why a call was called off, as it crosses the channel, and as its handler's `ctx.signal` goes off with it again: its caller
|
|
32
|
+
// gave up (`AbortError`), or its deadline passed (a query's `timeout`; a doing call's `TimeoutError`, coda#2749 3.3).
|
|
33
|
+
export const cancelOf = reason => ['AbortError', 'TimeoutError'].includes(reason?.name) ? { name: reason.name } : failureOf(reason);
|
|
34
|
+
export const canceled = said => said?.name === 'AbortError' ? abortError() : said?.name === 'TimeoutError' ? timeoutError() : failed(said);
|
|
35
|
+
// A call its caller gave up on with `signal`: failing with the reason it went off with itself — of a handler's `ctx.signal`,
|
|
36
|
+
// the reason the call it is made under was called off with (`AbortError`, `TimeoutError`), which the handler, throwing it,
|
|
37
|
+
// ends that call by — whatever it is (`abort` takes any value), and, an object that can take it, saying how much was done
|
|
38
|
+
// (`effects`) unless it says so already.
|
|
39
|
+
export function givenUp(signal, effects) {
|
|
40
|
+
const reason = signal.reason;
|
|
41
|
+
if (Object.isExtensible(reason) && reason.effects === undefined) reason.effects = effects;
|
|
42
|
+
return reason;
|
|
43
|
+
}
|
|
44
|
+
// What a module's handler of a doing call ended with (adapters/plugin-modules.mjs `run`), as the call's outcome (coda#2749
|
|
45
|
+
// 3.2): called off, as it said; failed, as `failureOf` says; completed, with a result as the operation (`found`) has it and
|
|
46
|
+
// within 1 MiB, or else a `handler_error` of unknown effect.
|
|
47
|
+
export function outcomeOf(found, name, said) {
|
|
48
|
+
if (said?.outcome === 'canceled') return { outcome: 'canceled' };
|
|
49
|
+
if (said?.outcome === 'failed') return { outcome: 'failed', error: failureOf(said.error) };
|
|
50
|
+
try {
|
|
51
|
+
checkResult(found, name, said?.result);
|
|
52
|
+
return { outcome: 'completed', result: said.result };
|
|
53
|
+
} catch (error) { return { outcome: 'failed', error: { code: 'handler_error', message: error.message, effects: 'unknown' } }; }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// A handler of an operation of a protocol the plugin implements, kept in `operations` as `{ run, resume }`: a function, or
|
|
57
|
+
// (`coda.handle`, coda#2749 3.1) `{ run, resume? }`, `resume` kept for what a later host does with it. `self` is no protocol
|
|
58
|
+
// (`invalid_input`): a query only the plugin's own processes make, which no manifest lists (`coda self`, docs/plugins.md
|
|
59
|
+
// 「只给自己用的查询」), is registered by its name with `coda.self.handle` and kept under `self`, which no protocol's name is.
|
|
60
|
+
function registrar(plugin, operations) {
|
|
61
|
+
const register = (kind, operation, entry) => {
|
|
62
|
+
const id = JSON.stringify([kind, operation]);
|
|
63
|
+
if (operations.has(id)) throw new TypeError(`已登记:${kind}/${operation}`);
|
|
64
|
+
operations.set(id, entry);
|
|
65
|
+
return { dispose: () => { if (operations.get(id) === entry) operations.delete(id); } };
|
|
66
|
+
};
|
|
67
|
+
return {
|
|
68
|
+
handle(kind, operation, handler) {
|
|
69
|
+
const { run, resume } = typeof handler === 'function' ? { run: handler } : handler ?? {};
|
|
70
|
+
if (typeof kind !== 'string' || !kind || typeof operation !== 'string' || !operation || typeof run !== 'function'
|
|
71
|
+
|| (resume !== undefined && typeof resume !== 'function')) throw new TypeError('handle(协议名, 操作, handler 或 { run, resume? })');
|
|
72
|
+
if (kind === 'self') throw new ContractError('invalid_input', 'self 不是协议:只给自己用的查询用 coda.self.handle(名字, handler) 登记', { effects: 'none' });
|
|
73
|
+
if (!(plugin.protocolImplementations ?? []).includes(kind)) throw new TypeError(`插件 ${plugin.id} 没有实现 ${kind}`);
|
|
74
|
+
return register(kind, operation, { run, resume });
|
|
75
|
+
},
|
|
76
|
+
self(name, handler) {
|
|
77
|
+
if (typeof name !== 'string' || !name || typeof handler !== 'function') throw new TypeError('self.handle(名字, handler)');
|
|
78
|
+
return register('self', name, { run: handler });
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// What a module queries and answers by (coda#2749 3.1–3.5, 6): `handle` an operation of a protocol it implements, a query
|
|
84
|
+
// or a doing call, its handler's return value the result and what it throws the failure; `self.handle` a query of its own
|
|
85
|
+
// processes'; `query` a query of any record's, through the backend's unified call
|
|
86
|
+
// (`send(request, signal)`), failing as one that did nothing (`none`), as it only reads, or, given up on with its `signal`,
|
|
87
|
+
// with that signal's reason itself, `none` added unless it says its own `effects` (`givenUp`) — a handler's `ctx.signal`'s
|
|
88
|
+
// `TimeoutError`, of its own doing call, `unknown`, thrown as it is; `error`, a failure to throw. `within(signal, deadline)`: `query` as
|
|
89
|
+
// a handler's `ctx.query` is, given up on as the call it is made under is, and given at most the time left of it. Doing an
|
|
90
|
+
// operation (one not `safe`, an `idempotent` one too) is no query: it is a call (`callingAPI`).
|
|
91
|
+
export function queryingAPI({ send, plugin, operations }) {
|
|
92
|
+
const register = registrar(plugin, operations);
|
|
93
|
+
async function query(record, operation, input = {}, options = {}, within = null) {
|
|
94
|
+
if (!record || typeof record.kind !== 'string' || typeof record.id !== 'string' || typeof operation !== 'string' || !operation)
|
|
95
|
+
throw new TypeError('query({ kind, id }, 操作, 输入?, { signal?, timeout? })');
|
|
96
|
+
const { signal = null, timeout = null } = options ?? {};
|
|
97
|
+
if (options?.id !== undefined) throw new TypeError('查询两边都不留,没有 options.id');
|
|
98
|
+
if (timeout !== null && !(Number.isSafeInteger(timeout) && timeout > 0)) throw new TypeError('timeout 须为正整数毫秒');
|
|
99
|
+
const given = within ? (signal ? AbortSignal.any([signal, within.signal]) : within.signal) : signal;
|
|
100
|
+
if (given?.aborted) throw givenUp(given, 'none');
|
|
101
|
+
try { return await send({ record: { kind: record.kind, id: record.id }, operation, input, timeout, deadline: within?.deadline ?? null }, given); }
|
|
102
|
+
catch (error) {
|
|
103
|
+
if (!(given?.aborted && error === given.reason) && Object.isExtensible(error)) error.effects = 'none';
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
handle: register.handle,
|
|
109
|
+
self: Object.freeze({ handle: register.self }),
|
|
110
|
+
query: (record, operation, input, options) => query(record, operation, input, options),
|
|
111
|
+
error: codaError,
|
|
112
|
+
within: (signal, deadline) => (record, operation, input, options) => query(record, operation, input, options, { signal, deadline }),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// What a module does by (docs/plugins.md「做事的调用」): `call` a doing operation of any record's (one not `safe`, an
|
|
117
|
+
// `idempotent` one too), through the call kernel from the plugin's address (`send(request, signal, onProgress)`): its result, or a
|
|
118
|
+
// failure as `failed` says, one cancelled an `AbortError`, one past its time a `TimeoutError`, each with its `code` and
|
|
119
|
+
// `effects`. `options.id`, its idempotency key: the same call again, or refused (`idempotency_conflict`) for other content.
|
|
120
|
+
// Given up on with its `signal`, it fails as an `AbortError` at once, the call cancelled. `within(signal, deadline, under)`:
|
|
121
|
+
// `call` as a doing call's handler's `ctx.call` is, made by that call (the backend's request `under` which it came), given
|
|
122
|
+
// up on as it is, and given at most the time it has left.
|
|
123
|
+
export function callingAPI(send) {
|
|
124
|
+
const named = error => error?.code === 'canceled' ? Object.assign(error, { name: 'AbortError' }) : error?.code === 'timeout' ? Object.assign(error, { name: 'TimeoutError' }) : error;
|
|
125
|
+
async function call(record, operation, input = {}, options = {}, within = null) {
|
|
126
|
+
if (!record || typeof record.kind !== 'string' || typeof record.id !== 'string' || typeof operation !== 'string' || !operation)
|
|
127
|
+
throw new TypeError('call({ kind, id }, 操作, 输入?, { id?, signal?, timeout?, onProgress? })');
|
|
128
|
+
const { id = null, signal = null, timeout = null, onProgress = null } = options ?? {};
|
|
129
|
+
if (id !== null && !(typeof id === 'string' && id && id.length <= 2048)) throw new TypeError('id 须为不超过 2048 字的字符串');
|
|
130
|
+
if (timeout !== null && !(Number.isSafeInteger(timeout) && timeout > 0)) throw new TypeError('timeout 须为正整数毫秒');
|
|
131
|
+
if (onProgress !== null && typeof onProgress !== 'function') throw new TypeError('onProgress 须为函数');
|
|
132
|
+
const given = within ? (signal ? AbortSignal.any([signal, within.signal]) : within.signal) : signal;
|
|
133
|
+
if (given?.aborted) throw givenUp(given, 'none');
|
|
134
|
+
try {
|
|
135
|
+
return await send({ record: { kind: record.kind, id: record.id }, operation, input, id, timeout, deadline: within?.deadline ?? null,
|
|
136
|
+
under: within?.under ?? null, progress: onProgress !== null }, given, onProgress);
|
|
137
|
+
} catch (error) { throw named(error); }
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
call: (record, operation, input, options) => call(record, operation, input, options),
|
|
141
|
+
within: (signal, deadline, under) => (record, operation, input, options) => call(record, operation, input, options, { signal, deadline, under }),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// What is left of `coda.protocols` beside `register` (adapters/plugin-modules.mjs): `resolve`, a record's service as this
|
|
146
|
+
// machine reaches it, and `upload`, an upload of this machine a post to a session names.
|
|
147
|
+
export function providerAPI({ call, connection }) {
|
|
7
148
|
return {
|
|
8
149
|
async resolve(ref, service) {
|
|
9
150
|
const { resource, path } = await call('resolve', [ref, service]);
|
|
10
151
|
const api = connection();
|
|
11
152
|
return { resource, url: `${api.url}${path}`, headers: { authorization: `Bearer ${api.token}` } };
|
|
12
153
|
},
|
|
13
|
-
|
|
14
|
-
update: (kind, id, spec, revision) => call('update', [kind, id, spec, revision]),
|
|
15
|
-
setStatus: (kind, id, status, revision) => call('setStatus', [kind, id, status, revision]),
|
|
16
|
-
delete: (kind, id) => call('delete', [kind, id]),
|
|
17
|
-
publish: (kind, id, enabled = true) => call('publish', [kind, id, enabled]),
|
|
18
|
-
// An upload of this machine a post to a session names (`upload`): `{ id, name, size, mime?, duration?, …, path }`.
|
|
154
|
+
// `{ id, name, size, mime?, duration?, …, path }`.
|
|
19
155
|
upload: id => call('upload', [id]),
|
|
20
|
-
observe,
|
|
21
|
-
handle(kind, operation, handler) {
|
|
22
|
-
if (typeof kind !== 'string' || !kind || typeof operation !== 'string' || !operation || typeof handler !== 'function')
|
|
23
|
-
throw new TypeError('protocols.handle(协议名, 操作, handler)');
|
|
24
|
-
if (!(plugin.protocolImplementations ?? []).includes(kind)) throw new TypeError(`插件 ${plugin.id} 没有实现 ${kind}`);
|
|
25
|
-
const id = JSON.stringify([kind, operation]);
|
|
26
|
-
if (operations.has(id)) throw new TypeError(`协议操作已登记:${kind}/${operation}`);
|
|
27
|
-
operations.set(id, handler);
|
|
28
|
-
return { dispose: () => { if (operations.get(id) === handler) operations.delete(id); } };
|
|
29
|
-
},
|
|
30
|
-
async control(kind, handler, scope = 'local') {
|
|
31
|
-
if (typeof handler !== 'function') throw new TypeError('controller 须为函数');
|
|
32
|
-
const pending = new Map(), running = new Set();
|
|
33
|
-
let disposed = false;
|
|
34
|
-
const subscription = await observe(kind, event => {
|
|
35
|
-
pending.set(event.id, event);
|
|
36
|
-
if (running.has(event.id)) return;
|
|
37
|
-
running.add(event.id);
|
|
38
|
-
void (async () => {
|
|
39
|
-
try {
|
|
40
|
-
while (!disposed && pending.has(event.id)) {
|
|
41
|
-
const next = pending.get(event.id); pending.delete(event.id);
|
|
42
|
-
try { await handler(next); } catch (error) { report('[protocol controller]', error); }
|
|
43
|
-
}
|
|
44
|
-
} finally { running.delete(event.id); }
|
|
45
|
-
})();
|
|
46
|
-
}, scope);
|
|
47
|
-
return { dispose() { disposed = true; pending.clear(); subscription.dispose(); } };
|
|
48
|
-
},
|
|
49
156
|
};
|
|
50
157
|
}
|
|
@@ -7,3 +7,4 @@ import '../components/workflows/contributions.mjs';
|
|
|
7
7
|
import '../components/plugins/model-contributions.mjs';
|
|
8
8
|
import '../components/plugins/git-contributions.mjs';
|
|
9
9
|
import '../components/composers/contributions.mjs';
|
|
10
|
+
import '../components/plugins/process-contributions.mjs';
|