@armadra/agent 0.18.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/CHANGELOG.zh-CN.md +20 -0
- package/README.md +4 -0
- package/README.zh-CN.md +4 -0
- package/dist/bundle/ama.cjs +25956 -25161
- package/dist/cli/bootstrap.js +32 -6
- package/dist/cli/compose-agents.js +2 -1
- package/dist/cli/compose-extensions-load.d.ts +20 -0
- package/dist/cli/compose-extensions-load.js +38 -0
- package/dist/cli/compose.js +1 -0
- package/dist/cli/deps.d.ts +11 -0
- package/dist/cli/runtime.d.ts +7 -0
- package/dist/cli/subcommands/doctor.js +33 -0
- package/dist/config/extension-tree-hash.d.ts +31 -0
- package/dist/config/extension-tree-hash.js +88 -0
- package/dist/config/extension-trust.d.ts +70 -0
- package/dist/config/extension-trust.js +112 -0
- package/dist/config/profile.d.ts +2 -0
- package/dist/config/profile.js +1 -0
- package/dist/config/schema-trust.d.ts +7 -0
- package/dist/config/schema-trust.js +46 -0
- package/dist/config/schema.d.ts +4 -5
- package/dist/config/schema.js +3 -25
- package/dist/config/trust.d.ts +2 -0
- package/dist/config/trust.js +5 -1
- package/dist/config/types.d.ts +13 -0
- package/dist/drivers/host-runners.d.ts +15 -4
- package/dist/drivers/host-runners.js +31 -17
- package/dist/errors.d.ts +1 -1
- package/dist/host/api-impl.d.ts +47 -4
- package/dist/host/api-impl.js +181 -20
- package/dist/host/commands.d.ts +23 -0
- package/dist/host/commands.js +76 -0
- package/dist/host/extensions.d.ts +43 -0
- package/dist/host/extensions.js +117 -0
- package/dist/host/resources.d.ts +2 -0
- package/dist/host/resources.js +5 -0
- package/dist/host/types.d.ts +41 -2
- package/dist/host/types.js +4 -2
- package/dist/i18n/catalog.d.ts +31 -0
- package/dist/i18n/messages/cli.d.ts +24 -0
- package/dist/i18n/messages/cli.js +24 -0
- package/dist/i18n/messages/drivers.d.ts +22 -0
- package/dist/i18n/messages/drivers.js +22 -0
- package/dist/i18n/messages/errors.d.ts +1 -1
- package/dist/i18n/messages/interactive-startup.d.ts +4 -0
- package/dist/i18n/messages/interactive-startup.js +4 -0
- package/dist/i18n/messages/interactive.d.ts +4 -0
- package/dist/i18n/messages/report.d.ts +12 -0
- package/dist/i18n/messages/report.js +12 -0
- package/dist/modes/acp/acp-config.d.ts +4 -2
- package/dist/modes/acp/acp-config.js +17 -3
- package/dist/modes/acp/acp-events.d.ts +2 -0
- package/dist/modes/acp/acp-events.js +4 -0
- package/dist/modes/acp/acp-server.js +6 -2
- package/dist/modes/acp/acp-sessions.d.ts +8 -1
- package/dist/modes/acp/acp-sessions.js +18 -0
- package/dist/modes/commands-core.d.ts +14 -1
- package/dist/modes/commands-core.js +34 -2
- package/dist/modes/interactive/startup-ui.js +21 -0
- package/dist/modes/interactive/status-area.js +1 -1
- package/dist/modes/print/print-mode.d.ts +3 -0
- package/dist/modes/print/print-mode.js +35 -8
- package/dist/modes/rpc/commands.d.ts +2 -0
- package/dist/modes/rpc/commands.js +22 -7
- package/dist/modes/rpc/rpc-mode.js +3 -0
- package/dist/modes/startup-ui-text.js +11 -0
- package/dist/rpc.d.ts +2 -1
- package/dist/tools/registry.d.ts +6 -0
- package/dist/tools/registry.js +13 -0
- package/dist/tools/types.d.ts +7 -0
- package/docs/README.md +1 -1
- package/docs/en/README.md +3 -3
- package/docs/en/guides/extensions.md +141 -0
- package/docs/en/reference/acp.md +1 -1
- package/docs/en/reference/host-api.md +55 -27
- package/docs/en/reference/rpc.md +9 -7
- package/docs/guides/extensions.md +136 -0
- package/docs/reference/acp.md +1 -1
- package/docs/reference/host-api.md +55 -27
- package/docs/reference/rpc.md +9 -7
- package/package.json +4 -2
package/dist/host/api-impl.js
CHANGED
|
@@ -13,13 +13,20 @@
|
|
|
13
13
|
* [#226] `runners.onDone` 与 `minor`(host-api 1.1):每轮结束的事件由 registry 的观察包装发出,
|
|
14
14
|
* 处理器异常走本适配器的 `log`。
|
|
15
15
|
*
|
|
16
|
+
* [#304] 每个 binding 记下自己的全部注册,`revoke()` 一次撤回(扩展 create 抛错 / 超时 / 未返回适配器时
|
|
17
|
+
* 调用);撤回后注册类方法一律忽略并记 warning(防住超时后迟到的注册)。
|
|
18
|
+
*
|
|
19
|
+
* [#306] runner 注册表放在 {@link HostContext}(宿主适配器与扩展共用,同 id 先到先得,`runner_exists`),
|
|
20
|
+
* `onDone` 只回调本模块自己提供的 runner。
|
|
21
|
+
*
|
|
16
22
|
* [#270] `resources`(host-api 1.2):注册进 bootstrap 注入的 {@link HostResourceRegistry}(read 工具
|
|
17
23
|
* 持同一个实例);未注入时本地新建一个(SDK / 测试),宿主激活后由 bootstrap 冻结。
|
|
18
24
|
*/
|
|
19
|
-
import { HostRunnerRegistry } from "../drivers/host-runners.js";
|
|
25
|
+
import { HostRunnerRegistry, } from "../drivers/host-runners.js";
|
|
20
26
|
import { AmaError } from "../errors.js";
|
|
21
27
|
import { AMA_VERSION } from "../version.js";
|
|
22
28
|
import { HOST_API_MINOR, HOST_API_VERSION } from "./types.js";
|
|
29
|
+
import { HostCommandRegistry } from "./commands.js";
|
|
23
30
|
import { HostResourceRegistry } from "./resources.js";
|
|
24
31
|
import { msg } from "../i18n/index.js";
|
|
25
32
|
/** 进程内事件总线(宿主观察 AgentEvents)。 */
|
|
@@ -55,9 +62,29 @@ export class AgentEventBus {
|
|
|
55
62
|
}
|
|
56
63
|
}
|
|
57
64
|
}
|
|
65
|
+
export function createHostContext(options = {}) {
|
|
66
|
+
return {
|
|
67
|
+
instructions: [],
|
|
68
|
+
broker: {},
|
|
69
|
+
warming: [],
|
|
70
|
+
statuses: new Set(),
|
|
71
|
+
notify: options.notify,
|
|
72
|
+
commands: new HostCommandRegistry(options.reservedCommand),
|
|
73
|
+
runners: new HostRunnerRegistry(),
|
|
74
|
+
disabledBy: new Map(),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** 合并全部模块的状态(同键后加载者覆盖先加载者)。 */
|
|
78
|
+
export function mergedHostStatus(context) {
|
|
79
|
+
const merged = new Map();
|
|
80
|
+
for (const map of context.statuses)
|
|
81
|
+
for (const [k, v] of map)
|
|
82
|
+
merged.set(k, v);
|
|
83
|
+
return merged;
|
|
84
|
+
}
|
|
58
85
|
const TOOL_NAME = /^[a-z][a-z0-9_]{1,63}$/;
|
|
59
86
|
const hostRunnerRegistries = new WeakMap();
|
|
60
|
-
/**
|
|
87
|
+
/** 共享的 runner 注册表(HostApi 不是本模块创建的返回 undefined;[#306] 全部模块共用同一张)。 */
|
|
61
88
|
export function hostRunnersOf(api) {
|
|
62
89
|
return hostRunnerRegistries.get(api);
|
|
63
90
|
}
|
|
@@ -75,22 +102,38 @@ function validateTool(tool) {
|
|
|
75
102
|
throw new AmaError("invalid_arguments", msg().drivers.host.toolBadPermission(tool.name));
|
|
76
103
|
}
|
|
77
104
|
}
|
|
78
|
-
export function createHostApi(deps
|
|
79
|
-
|
|
105
|
+
export function createHostApi(deps, context = createHostContext({
|
|
106
|
+
...(deps.notify !== undefined ? { notify: deps.notify } : {}),
|
|
107
|
+
})) {
|
|
108
|
+
const instructions = context.instructions;
|
|
109
|
+
const token = Symbol("host-binding");
|
|
80
110
|
const registeredTools = [];
|
|
81
111
|
const disabledTools = [];
|
|
82
112
|
const status = new Map();
|
|
83
|
-
|
|
84
|
-
const warmingHandlers =
|
|
85
|
-
let notify = deps.notify;
|
|
113
|
+
context.statuses.add(status);
|
|
114
|
+
const warmingHandlers = context.warming;
|
|
86
115
|
const writeErr = deps.stderr ?? ((text) => void process.stderr.write(text));
|
|
87
116
|
const log = deps.log ??
|
|
88
117
|
((level, message) => {
|
|
89
118
|
if (level === "warn" || level === "error")
|
|
90
119
|
writeErr(`ama: [host] ${message}\n`);
|
|
91
120
|
});
|
|
92
|
-
const runners =
|
|
121
|
+
const runners = context.runners;
|
|
122
|
+
const scope = {
|
|
123
|
+
owner: token,
|
|
124
|
+
log: (message, error) => log("error", message, error),
|
|
125
|
+
};
|
|
93
126
|
const resources = deps.resources ?? new HostResourceRegistry();
|
|
127
|
+
// [#304] 撤回材料:按注册顺序逆序执行;revoked 之后的注册类调用一律忽略
|
|
128
|
+
const undo = [];
|
|
129
|
+
let revoked = false;
|
|
130
|
+
const ignored = (what) => {
|
|
131
|
+
if (revoked)
|
|
132
|
+
log("warn", msg().drivers.host.revokedIgnored(what));
|
|
133
|
+
return revoked;
|
|
134
|
+
};
|
|
135
|
+
const noop = () => undefined;
|
|
136
|
+
const ownInstructions = [];
|
|
94
137
|
const api = Object.freeze({
|
|
95
138
|
version: HOST_API_VERSION,
|
|
96
139
|
minor: HOST_API_MINOR,
|
|
@@ -105,14 +148,44 @@ export function createHostApi(deps) {
|
|
|
105
148
|
}),
|
|
106
149
|
tools: Object.freeze({
|
|
107
150
|
register(tool) {
|
|
151
|
+
if (ignored("tools.register"))
|
|
152
|
+
return;
|
|
108
153
|
validateTool(tool);
|
|
109
154
|
if (deps.tools.get(tool.name) !== undefined) {
|
|
110
155
|
throw new AmaError("tool_exists", msg().drivers.host.toolExists(tool.name));
|
|
111
156
|
}
|
|
112
157
|
deps.tools.register(tool, "host");
|
|
113
158
|
registeredTools.push(tool.name);
|
|
159
|
+
undo.push(() => deps.tools.unregister(tool.name));
|
|
114
160
|
},
|
|
115
161
|
disable(name) {
|
|
162
|
+
if (ignored("tools.disable"))
|
|
163
|
+
return;
|
|
164
|
+
// 按名记持有者:只有由模块禁用(禁用前是启用的)的名字才记;全部持有者撤回后才解除。
|
|
165
|
+
// 配置或启动前就禁用的名字不记,永远不会被扩展的撤回放开(伴随工具同理逐个判断)。
|
|
166
|
+
const targets = deps.tools.disableTargets?.(name) ?? [name];
|
|
167
|
+
const held = [];
|
|
168
|
+
for (const target of targets) {
|
|
169
|
+
const holders = context.disabledBy.get(target);
|
|
170
|
+
if (holders !== undefined)
|
|
171
|
+
holders.add(token);
|
|
172
|
+
else if (!deps.tools.isDisabled(target))
|
|
173
|
+
context.disabledBy.set(target, new Set([token]));
|
|
174
|
+
else
|
|
175
|
+
continue;
|
|
176
|
+
held.push(target);
|
|
177
|
+
}
|
|
178
|
+
if (held.length > 0) {
|
|
179
|
+
undo.push(() => {
|
|
180
|
+
for (const target of held) {
|
|
181
|
+
const holders = context.disabledBy.get(target);
|
|
182
|
+
if (holders === undefined || !holders.delete(token) || holders.size > 0)
|
|
183
|
+
continue;
|
|
184
|
+
context.disabledBy.delete(target);
|
|
185
|
+
deps.tools.enable(target);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
116
189
|
deps.tools.disable(name);
|
|
117
190
|
if (!disabledTools.includes(name))
|
|
118
191
|
disabledTools.push(name);
|
|
@@ -121,39 +194,63 @@ export function createHostApi(deps) {
|
|
|
121
194
|
}),
|
|
122
195
|
instructions: Object.freeze({
|
|
123
196
|
add(source) {
|
|
197
|
+
if (ignored("instructions.add"))
|
|
198
|
+
return;
|
|
124
199
|
if (source.kind === "file" && typeof source.path !== "string") {
|
|
125
200
|
throw new AmaError("invalid_arguments", msg().drivers.host.instructionNeedsPath);
|
|
126
201
|
}
|
|
127
202
|
if (source.kind === "text" && typeof source.text !== "string") {
|
|
128
203
|
throw new AmaError("invalid_arguments", msg().drivers.host.instructionNeedsText);
|
|
129
204
|
}
|
|
130
|
-
|
|
205
|
+
const copy = { ...source };
|
|
206
|
+
instructions.push(copy);
|
|
207
|
+
ownInstructions.push(copy);
|
|
131
208
|
},
|
|
132
209
|
}),
|
|
133
210
|
events: Object.freeze({
|
|
134
|
-
on
|
|
211
|
+
on(name, handler) {
|
|
212
|
+
if (ignored("events.on"))
|
|
213
|
+
return noop;
|
|
214
|
+
const off = deps.bus.on(name, handler);
|
|
215
|
+
undo.push(off);
|
|
216
|
+
return off;
|
|
217
|
+
},
|
|
135
218
|
}),
|
|
136
219
|
approvals: Object.freeze({
|
|
137
220
|
setBroker(next) {
|
|
221
|
+
if (ignored("approvals.setBroker"))
|
|
222
|
+
return;
|
|
138
223
|
if (typeof next?.ask !== "function") {
|
|
139
224
|
throw new AmaError("invalid_arguments", msg().drivers.host.brokerNeedsAsk);
|
|
140
225
|
}
|
|
141
|
-
broker
|
|
226
|
+
if (context.broker.owner !== undefined && context.broker.owner !== token) {
|
|
227
|
+
log("warn", msg().drivers.host.brokerIgnored);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
context.broker.owner = token;
|
|
231
|
+
context.broker.value = next;
|
|
142
232
|
},
|
|
143
233
|
}),
|
|
144
234
|
messages: Object.freeze({
|
|
145
|
-
|
|
235
|
+
// [#304] 撤回后忽略(只记 warning);不 reject,免得没 await 的扩展触发未处理的拒绝
|
|
236
|
+
sendUser: (text, origin) => ignored("messages.sendUser")
|
|
237
|
+
? Promise.resolve("queued")
|
|
238
|
+
: deps.sendUser(text, origin ?? "host"),
|
|
146
239
|
}),
|
|
147
240
|
ui: Object.freeze({
|
|
148
241
|
notify(message, level = "info") {
|
|
242
|
+
if (ignored("ui.notify"))
|
|
243
|
+
return;
|
|
149
244
|
// 交互 / line 由 UI 显示;rpc 由模式转成事件;print(或未注入)写 stderr。
|
|
150
|
-
if (notify !== undefined && deps.mode !== "print") {
|
|
151
|
-
notify(message, level);
|
|
245
|
+
if (context.notify !== undefined && deps.mode !== "print") {
|
|
246
|
+
context.notify(message, level);
|
|
152
247
|
return;
|
|
153
248
|
}
|
|
154
249
|
writeErr(`ama: [host${level === "info" ? "" : ` ${level}`}] ${message}\n`);
|
|
155
250
|
},
|
|
156
251
|
setStatus(key, text) {
|
|
252
|
+
if (ignored("ui.setStatus"))
|
|
253
|
+
return;
|
|
157
254
|
if (text === undefined || text === "")
|
|
158
255
|
status.delete(key);
|
|
159
256
|
else
|
|
@@ -164,31 +261,94 @@ export function createHostApi(deps) {
|
|
|
164
261
|
log,
|
|
165
262
|
cache: Object.freeze({
|
|
166
263
|
onWarmingDecision(handler) {
|
|
264
|
+
if (ignored("cache.onWarmingDecision"))
|
|
265
|
+
return noop;
|
|
167
266
|
if (typeof handler !== "function") {
|
|
168
267
|
throw new AmaError("invalid_arguments", msg().drivers.host.warmingNeedsFunction);
|
|
169
268
|
}
|
|
170
269
|
warmingHandlers.push(handler);
|
|
171
|
-
|
|
270
|
+
const off = () => {
|
|
172
271
|
const index = warmingHandlers.lastIndexOf(handler);
|
|
173
272
|
if (index >= 0)
|
|
174
273
|
warmingHandlers.splice(index, 1);
|
|
175
274
|
};
|
|
275
|
+
undo.push(off);
|
|
276
|
+
return off;
|
|
176
277
|
},
|
|
177
278
|
}),
|
|
178
279
|
runners: Object.freeze({
|
|
179
|
-
provide
|
|
180
|
-
|
|
280
|
+
provide(runner) {
|
|
281
|
+
if (ignored("runners.provide"))
|
|
282
|
+
return noop;
|
|
283
|
+
const off = runners.provide(runner, token);
|
|
284
|
+
undo.push(off);
|
|
285
|
+
return off;
|
|
286
|
+
},
|
|
287
|
+
onDone(handler) {
|
|
288
|
+
if (ignored("runners.onDone"))
|
|
289
|
+
return noop;
|
|
290
|
+
const off = runners.onDone(handler, scope);
|
|
291
|
+
undo.push(off);
|
|
292
|
+
return off;
|
|
293
|
+
},
|
|
294
|
+
}),
|
|
295
|
+
commands: Object.freeze({
|
|
296
|
+
register(command) {
|
|
297
|
+
if (ignored("commands.register"))
|
|
298
|
+
return noop;
|
|
299
|
+
const off = context.commands.register(command);
|
|
300
|
+
undo.push(off);
|
|
301
|
+
return off;
|
|
302
|
+
},
|
|
303
|
+
list: () => context.commands.list().map((c) => c.name),
|
|
181
304
|
}),
|
|
182
305
|
resources: Object.freeze({
|
|
183
|
-
register
|
|
306
|
+
register(resource) {
|
|
307
|
+
if (ignored("resources.register"))
|
|
308
|
+
return;
|
|
309
|
+
resources.register(resource);
|
|
310
|
+
undo.push(() => resources.remove(resource.scheme, resource));
|
|
311
|
+
},
|
|
184
312
|
list: () => resources.schemes(),
|
|
185
313
|
}),
|
|
186
314
|
});
|
|
187
315
|
hostRunnerRegistries.set(api, runners);
|
|
316
|
+
const revoke = () => {
|
|
317
|
+
if (revoked)
|
|
318
|
+
return;
|
|
319
|
+
revoked = true;
|
|
320
|
+
for (const fn of undo.reverse()) {
|
|
321
|
+
try {
|
|
322
|
+
fn();
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
log("warn", String(error));
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
undo.length = 0;
|
|
329
|
+
for (const entry of ownInstructions) {
|
|
330
|
+
const index = instructions.indexOf(entry);
|
|
331
|
+
if (index >= 0)
|
|
332
|
+
instructions.splice(index, 1);
|
|
333
|
+
}
|
|
334
|
+
registeredTools.length = 0;
|
|
335
|
+
disabledTools.length = 0;
|
|
336
|
+
if (context.broker.owner === token) {
|
|
337
|
+
delete context.broker.owner;
|
|
338
|
+
delete context.broker.value;
|
|
339
|
+
}
|
|
340
|
+
const keys = [...status.keys()];
|
|
341
|
+
status.clear();
|
|
342
|
+
context.statuses.delete(status);
|
|
343
|
+
// 其它模块可能也设了同名键:按合并后的剩余值重新通知,而不是一律清掉
|
|
344
|
+
const merged = mergedHostStatus(context);
|
|
345
|
+
for (const key of keys)
|
|
346
|
+
deps.onStatus?.(key, merged.get(key));
|
|
347
|
+
};
|
|
188
348
|
return {
|
|
189
349
|
api,
|
|
190
350
|
instructions,
|
|
191
|
-
broker: () => broker,
|
|
351
|
+
broker: () => context.broker.value,
|
|
192
352
|
registeredTools,
|
|
193
353
|
disabledTools,
|
|
194
354
|
status: () => status,
|
|
@@ -196,9 +356,10 @@ export function createHostApi(deps) {
|
|
|
196
356
|
return { adapter, api, source, status: () => status };
|
|
197
357
|
},
|
|
198
358
|
setNotify(fn) {
|
|
199
|
-
notify = fn ?? deps.notify;
|
|
359
|
+
context.notify = fn ?? deps.notify;
|
|
200
360
|
},
|
|
201
361
|
warmingDecider: () => warmingHandlers.at(-1),
|
|
202
362
|
resources,
|
|
363
|
+
revoke,
|
|
203
364
|
};
|
|
204
365
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 斜杠命令注册表(#301,host-api 1.4 `commands.register`)。
|
|
3
|
+
*
|
|
4
|
+
* 宿主适配器与本地扩展共用一张表(HostContext 持有);`commands-core` 在内置命令之后、提示模板之前
|
|
5
|
+
* 查它。保留名(内置命令、别名、交互界面命令)由装配方经 `isReserved` 注入,host 层不依赖模式层。
|
|
6
|
+
*/
|
|
7
|
+
import type { HostCommand, HostCommandContext, HostCommandResult } from "./types.js";
|
|
8
|
+
export declare const COMMAND_NAME: RegExp;
|
|
9
|
+
/** 只读视图(Runtime / 命令分派用)。 */
|
|
10
|
+
export interface HostCommandSource {
|
|
11
|
+
get(name: string): HostCommand | undefined;
|
|
12
|
+
list(): readonly HostCommand[];
|
|
13
|
+
}
|
|
14
|
+
export declare class HostCommandRegistry implements HostCommandSource {
|
|
15
|
+
private readonly isReserved;
|
|
16
|
+
private readonly commands;
|
|
17
|
+
constructor(isReserved?: (name: string) => boolean);
|
|
18
|
+
register(command: HostCommand): () => void;
|
|
19
|
+
get(name: string): HostCommand | undefined;
|
|
20
|
+
list(): readonly HostCommand[];
|
|
21
|
+
}
|
|
22
|
+
/** 调命令并校验结果形状;不合法抛 AmaError。命令自己抛的错原样上抛。 */
|
|
23
|
+
export declare function runHostCommand(command: HostCommand, args: string, ctx: HostCommandContext): Promise<Exclude<HostCommandResult, undefined>>;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 斜杠命令注册表(#301,host-api 1.4 `commands.register`)。
|
|
3
|
+
*
|
|
4
|
+
* 宿主适配器与本地扩展共用一张表(HostContext 持有);`commands-core` 在内置命令之后、提示模板之前
|
|
5
|
+
* 查它。保留名(内置命令、别名、交互界面命令)由装配方经 `isReserved` 注入,host 层不依赖模式层。
|
|
6
|
+
*/
|
|
7
|
+
import { AmaError } from "../errors.js";
|
|
8
|
+
import { msg } from "../i18n/index.js";
|
|
9
|
+
export const COMMAND_NAME = /^[a-z][a-z0-9-]{0,63}$/;
|
|
10
|
+
export class HostCommandRegistry {
|
|
11
|
+
isReserved;
|
|
12
|
+
commands = new Map();
|
|
13
|
+
constructor(isReserved = () => false) {
|
|
14
|
+
this.isReserved = isReserved;
|
|
15
|
+
}
|
|
16
|
+
register(command) {
|
|
17
|
+
const h = msg().drivers.host;
|
|
18
|
+
if (typeof command !== "object" || command === null) {
|
|
19
|
+
throw new AmaError("invalid_arguments", h.commandNotObject);
|
|
20
|
+
}
|
|
21
|
+
const name = command.name;
|
|
22
|
+
if (typeof name !== "string" || !COMMAND_NAME.test(name)) {
|
|
23
|
+
throw new AmaError("invalid_arguments", h.commandBadName(String(name)));
|
|
24
|
+
}
|
|
25
|
+
if (typeof command.description !== "string" || command.description.trim() === "") {
|
|
26
|
+
throw new AmaError("invalid_arguments", h.commandNoDescription(name));
|
|
27
|
+
}
|
|
28
|
+
if (typeof command.run !== "function") {
|
|
29
|
+
throw new AmaError("invalid_arguments", h.commandNoRun(name));
|
|
30
|
+
}
|
|
31
|
+
if (command.args !== undefined && typeof command.args !== "string") {
|
|
32
|
+
throw new AmaError("invalid_arguments", h.commandBadArgs(name));
|
|
33
|
+
}
|
|
34
|
+
if (this.isReserved(name))
|
|
35
|
+
throw new AmaError("command_exists", h.commandReserved(name));
|
|
36
|
+
if (this.commands.has(name))
|
|
37
|
+
throw new AmaError("command_exists", h.commandExists(name));
|
|
38
|
+
const stored = {
|
|
39
|
+
name,
|
|
40
|
+
description: command.description,
|
|
41
|
+
...(command.args !== undefined ? { args: command.args } : {}),
|
|
42
|
+
run: command.run,
|
|
43
|
+
};
|
|
44
|
+
this.commands.set(name, stored);
|
|
45
|
+
return () => {
|
|
46
|
+
if (this.commands.get(name) === stored)
|
|
47
|
+
this.commands.delete(name);
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
get(name) {
|
|
51
|
+
return this.commands.get(name);
|
|
52
|
+
}
|
|
53
|
+
list() {
|
|
54
|
+
return [...this.commands.values()];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** 调命令并校验结果形状;不合法抛 AmaError。命令自己抛的错原样上抛。 */
|
|
58
|
+
export async function runHostCommand(command, args, ctx) {
|
|
59
|
+
const result = await command.run(args, ctx);
|
|
60
|
+
if (result === undefined)
|
|
61
|
+
return { kind: "handled" };
|
|
62
|
+
const bad = () => new AmaError("invalid_result", msg().drivers.host.commandBadResult(command.name));
|
|
63
|
+
if (typeof result !== "object" || result === null)
|
|
64
|
+
throw bad();
|
|
65
|
+
if (result.kind === "handled") {
|
|
66
|
+
if (result.message !== undefined && typeof result.message !== "string")
|
|
67
|
+
throw bad();
|
|
68
|
+
return result.message === undefined
|
|
69
|
+
? { kind: "handled" }
|
|
70
|
+
: { kind: "handled", message: result.message };
|
|
71
|
+
}
|
|
72
|
+
if (result.kind === "prompt" && typeof result.text === "string" && result.text !== "") {
|
|
73
|
+
return { kind: "prompt", text: result.text };
|
|
74
|
+
}
|
|
75
|
+
throw bad();
|
|
76
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本地扩展(#301,docs/guides/extensions.md):用户自己安装、可以有多个的宿主适配器。
|
|
3
|
+
*
|
|
4
|
+
* - 发现:`<configDir>/extensions/` 下的 `*.{mjs,cjs}` 与带 `index.{mjs,cjs}` 的子目录(#305)按入口名排序,
|
|
5
|
+
* 其后是 profile `extensions`(绝对路径,可以是目录);
|
|
6
|
+
* 不加载项目级。
|
|
7
|
+
* - 激活:每个模块一个独立的 {@link HostApiBinding},共享工具注册表、事件总线、broker 槽、资源注册表与
|
|
8
|
+
* 保温处理器(共享状态见 api-impl 的 `HostContext`);任何失败(加载、版本不等、create 抛错或超时、
|
|
9
|
+
* 没有返回适配器)撤回该模块已注册的全部内容(#304)、只产生 warning 并跳过,不阻止启动(宿主适配器的失败规则不变)。
|
|
10
|
+
* - 释放:按加载的逆序 dispose,失败只回调 onError。
|
|
11
|
+
*/
|
|
12
|
+
import type { HostApiBinding } from "./api-impl.js";
|
|
13
|
+
import type { HostAdapterHandle } from "./types.js";
|
|
14
|
+
export declare const EXTENSIONS_DIR = "extensions";
|
|
15
|
+
/** 目录扩展的入口:`index.mjs`,其次 `index.cjs`;都没有时返回 `index.mjs`(加载时报「找不到」)。 */
|
|
16
|
+
export declare function resolveExtensionEntry(path: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* 用户级目录里的扩展(#305 起含目录):单文件 `*.{mjs,cjs}` 与带 `index.{mjs,cjs}` 的子目录,
|
|
19
|
+
* 按入口名(文件名或目录名)一起排序。目录不存在 → 空。
|
|
20
|
+
*/
|
|
21
|
+
export declare function discoverExtensionDir(configDir: string): string[];
|
|
22
|
+
/** 加载顺序:用户级目录(按文件名),其后 profile `extensions`;同一路径只算一次。 */
|
|
23
|
+
export declare function discoverExtensions(configDir: string, profileExtensions?: readonly string[]): string[];
|
|
24
|
+
export interface ExtensionSkip {
|
|
25
|
+
path: string;
|
|
26
|
+
reason: string;
|
|
27
|
+
}
|
|
28
|
+
export interface ActivateExtensionsInput {
|
|
29
|
+
/** 要加载的文件(已经过信任决定)。 */
|
|
30
|
+
paths: readonly string[];
|
|
31
|
+
/** 每个模块一个新 binding(共享同一个 HostContext)。 */
|
|
32
|
+
createBinding(): HostApiBinding;
|
|
33
|
+
warn(message: string): void;
|
|
34
|
+
timeoutMs?: number;
|
|
35
|
+
}
|
|
36
|
+
export interface ActivatedExtensions {
|
|
37
|
+
handles: HostAdapterHandle[];
|
|
38
|
+
skipped: ExtensionSkip[];
|
|
39
|
+
}
|
|
40
|
+
/** 逐个激活;失败的只记 warning。 */
|
|
41
|
+
export declare function activateExtensions(input: ActivateExtensionsInput): Promise<ActivatedExtensions>;
|
|
42
|
+
/** 逆序 dispose(每个失败只回调 onError,不影响其余)。 */
|
|
43
|
+
export declare function disposeExtensions(handles: readonly HostAdapterHandle[], onError?: (handle: HostAdapterHandle, error: unknown) => void): Promise<void>;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本地扩展(#301,docs/guides/extensions.md):用户自己安装、可以有多个的宿主适配器。
|
|
3
|
+
*
|
|
4
|
+
* - 发现:`<configDir>/extensions/` 下的 `*.{mjs,cjs}` 与带 `index.{mjs,cjs}` 的子目录(#305)按入口名排序,
|
|
5
|
+
* 其后是 profile `extensions`(绝对路径,可以是目录);
|
|
6
|
+
* 不加载项目级。
|
|
7
|
+
* - 激活:每个模块一个独立的 {@link HostApiBinding},共享工具注册表、事件总线、broker 槽、资源注册表与
|
|
8
|
+
* 保温处理器(共享状态见 api-impl 的 `HostContext`);任何失败(加载、版本不等、create 抛错或超时、
|
|
9
|
+
* 没有返回适配器)撤回该模块已注册的全部内容(#304)、只产生 warning 并跳过,不阻止启动(宿主适配器的失败规则不变)。
|
|
10
|
+
* - 释放:按加载的逆序 dispose,失败只回调 onError。
|
|
11
|
+
*/
|
|
12
|
+
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
13
|
+
import { join, resolve } from "node:path";
|
|
14
|
+
import { StartupError } from "../errors.js";
|
|
15
|
+
import { msg } from "../i18n/index.js";
|
|
16
|
+
import { createAdapter, disposeHost, loadHostModule } from "./loader.js";
|
|
17
|
+
export const EXTENSIONS_DIR = "extensions";
|
|
18
|
+
const ENTRY_NAMES = ["index.mjs", "index.cjs"];
|
|
19
|
+
function isDir(path) {
|
|
20
|
+
try {
|
|
21
|
+
return statSync(path).isDirectory();
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** 目录扩展的入口:`index.mjs`,其次 `index.cjs`;都没有时返回 `index.mjs`(加载时报「找不到」)。 */
|
|
28
|
+
export function resolveExtensionEntry(path) {
|
|
29
|
+
if (!isDir(path))
|
|
30
|
+
return path;
|
|
31
|
+
return join(path, ENTRY_NAMES.find((n) => existsSync(join(path, n))) ?? "index.mjs");
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 用户级目录里的扩展(#305 起含目录):单文件 `*.{mjs,cjs}` 与带 `index.{mjs,cjs}` 的子目录,
|
|
35
|
+
* 按入口名(文件名或目录名)一起排序。目录不存在 → 空。
|
|
36
|
+
*/
|
|
37
|
+
export function discoverExtensionDir(configDir) {
|
|
38
|
+
const dir = join(configDir, EXTENSIONS_DIR);
|
|
39
|
+
if (!existsSync(dir))
|
|
40
|
+
return [];
|
|
41
|
+
let entries;
|
|
42
|
+
try {
|
|
43
|
+
entries = readdirSync(dir, { withFileTypes: true }).map((e) => ({
|
|
44
|
+
name: e.name,
|
|
45
|
+
// 常规文件与符号链接都先当文件看(链接指向目录时下面再按目录处理)
|
|
46
|
+
file: e.isFile() || e.isSymbolicLink(),
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return [];
|
|
51
|
+
}
|
|
52
|
+
const found = [];
|
|
53
|
+
for (const { name, file } of entries) {
|
|
54
|
+
const full = join(dir, name);
|
|
55
|
+
if (isDir(full)) {
|
|
56
|
+
if (ENTRY_NAMES.some((n) => existsSync(join(full, n))))
|
|
57
|
+
found.push(name);
|
|
58
|
+
}
|
|
59
|
+
else if (file && /\.(mjs|cjs)$/i.test(name)) {
|
|
60
|
+
found.push(name);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return found.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)).map((name) => join(dir, name));
|
|
64
|
+
}
|
|
65
|
+
/** 加载顺序:用户级目录(按文件名),其后 profile `extensions`;同一路径只算一次。 */
|
|
66
|
+
export function discoverExtensions(configDir, profileExtensions = []) {
|
|
67
|
+
const seen = new Set();
|
|
68
|
+
const result = [];
|
|
69
|
+
for (const path of [
|
|
70
|
+
...discoverExtensionDir(configDir),
|
|
71
|
+
...profileExtensions.map((p) => resolve(p)),
|
|
72
|
+
]) {
|
|
73
|
+
if (seen.has(path))
|
|
74
|
+
continue;
|
|
75
|
+
seen.add(path);
|
|
76
|
+
result.push(path);
|
|
77
|
+
}
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
function errorText(error) {
|
|
81
|
+
return error instanceof Error ? error.message : String(error);
|
|
82
|
+
}
|
|
83
|
+
/** 逐个激活;失败的只记 warning。 */
|
|
84
|
+
export async function activateExtensions(input) {
|
|
85
|
+
const handles = [];
|
|
86
|
+
const skipped = [];
|
|
87
|
+
const skip = (path, reason) => {
|
|
88
|
+
skipped.push({ path, reason });
|
|
89
|
+
input.warn(msg().cli.bootstrap.extensionSkipped(path, reason));
|
|
90
|
+
};
|
|
91
|
+
for (const path of input.paths) {
|
|
92
|
+
// [#304] 失败(抛错 / 超时 / 未返回适配器)一律撤回它已注册的内容;之后迟到的注册被忽略
|
|
93
|
+
let binding;
|
|
94
|
+
try {
|
|
95
|
+
const module = await loadHostModule(resolveExtensionEntry(path));
|
|
96
|
+
binding = input.createBinding();
|
|
97
|
+
const adapter = await createAdapter(module, binding.api, path, input.timeoutMs);
|
|
98
|
+
if (adapter === undefined) {
|
|
99
|
+
binding.revoke();
|
|
100
|
+
skip(path, msg().cli.bootstrap.extensionNotActivated);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
handles.push(binding.handle(adapter, path));
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
binding?.revoke();
|
|
107
|
+
skip(path, error instanceof StartupError ? error.message : errorText(error));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return { handles, skipped };
|
|
111
|
+
}
|
|
112
|
+
/** 逆序 dispose(每个失败只回调 onError,不影响其余)。 */
|
|
113
|
+
export async function disposeExtensions(handles, onError = () => undefined) {
|
|
114
|
+
for (const handle of [...handles].reverse()) {
|
|
115
|
+
await disposeHost(handle, (error) => onError(handle, error));
|
|
116
|
+
}
|
|
117
|
+
}
|
package/dist/host/resources.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export declare class HostResourceRegistry implements HostResourceLookup {
|
|
|
12
12
|
private readonly entries;
|
|
13
13
|
private frozen;
|
|
14
14
|
register(resource: HostResource): void;
|
|
15
|
+
/** [#304] 撤回:移除 scheme(激活失败的扩展用,不受冻结限制)。 */
|
|
16
|
+
remove(scheme: string, resource: HostResource): void;
|
|
15
17
|
/** 宿主激活后调用:之后不再接受注册。 */
|
|
16
18
|
freeze(): void;
|
|
17
19
|
get isFrozen(): boolean;
|
package/dist/host/resources.js
CHANGED
|
@@ -40,6 +40,11 @@ export class HostResourceRegistry {
|
|
|
40
40
|
}
|
|
41
41
|
this.entries.set(scheme, resource);
|
|
42
42
|
}
|
|
43
|
+
/** [#304] 撤回:移除 scheme(激活失败的扩展用,不受冻结限制)。 */
|
|
44
|
+
remove(scheme, resource) {
|
|
45
|
+
if (this.entries.get(scheme) === resource)
|
|
46
|
+
this.entries.delete(scheme);
|
|
47
|
+
}
|
|
43
48
|
/** 宿主激活后调用:之后不再接受注册。 */
|
|
44
49
|
freeze() {
|
|
45
50
|
this.frozen = true;
|