@armadra/agent 0.18.0 → 0.20.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 +26 -0
- package/CHANGELOG.zh-CN.md +26 -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
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* trust.json 的校验(从 schema.ts 拆出,#301 加了 `extensions` 段)。
|
|
3
|
+
*/
|
|
4
|
+
import { msg } from "../i18n/index.js";
|
|
5
|
+
import { Checker, join } from "./checker.js";
|
|
6
|
+
export function validateTrustFile(value) {
|
|
7
|
+
const c = new Checker();
|
|
8
|
+
if (!c.object(value, ""))
|
|
9
|
+
return c.diagnostics;
|
|
10
|
+
c.version(value, "");
|
|
11
|
+
const entries = value["entries"];
|
|
12
|
+
if (!Array.isArray(entries)) {
|
|
13
|
+
c.error("entries", msg().config.schema.array);
|
|
14
|
+
return c.diagnostics;
|
|
15
|
+
}
|
|
16
|
+
entries.forEach((entry, index) => {
|
|
17
|
+
const p = join("entries", index);
|
|
18
|
+
if (!c.object(entry, p))
|
|
19
|
+
return;
|
|
20
|
+
c.string(entry, "path", p, true);
|
|
21
|
+
c.string(entry, "at", p, true);
|
|
22
|
+
if (typeof entry["trusted"] !== "boolean")
|
|
23
|
+
c.error(join(p, "trusted"), msg().config.schema.boolean);
|
|
24
|
+
});
|
|
25
|
+
const extensions = value["extensions"];
|
|
26
|
+
if (extensions !== undefined) {
|
|
27
|
+
if (!Array.isArray(extensions)) {
|
|
28
|
+
c.error("extensions", msg().config.schema.array);
|
|
29
|
+
return c.diagnostics;
|
|
30
|
+
}
|
|
31
|
+
extensions.forEach((entry, index) => {
|
|
32
|
+
const p = join("extensions", index);
|
|
33
|
+
if (!c.object(entry, p))
|
|
34
|
+
return;
|
|
35
|
+
c.string(entry, "path", p, true);
|
|
36
|
+
c.string(entry, "sha256", p, true);
|
|
37
|
+
c.string(entry, "at", p, true);
|
|
38
|
+
if (typeof entry["trusted"] !== "boolean")
|
|
39
|
+
c.error(join(p, "trusted"), msg().config.schema.boolean);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return c.diagnostics;
|
|
43
|
+
}
|
|
44
|
+
export function asTrustFile(value) {
|
|
45
|
+
return value;
|
|
46
|
+
}
|
package/dist/config/schema.d.ts
CHANGED
|
@@ -5,26 +5,25 @@
|
|
|
5
5
|
* 校验结果是诊断列表:`severity: "error"` 拒绝该文件(启动退出码 3),`"warning"` 只提示
|
|
6
6
|
* (未知字段等)。`path` 是 JSON 指针风格的字段路径(`permission.mode`、`hooks.PreToolUse[0]`)。
|
|
7
7
|
*/
|
|
8
|
-
import { type AmaConfig, type AuthFile, type ProfileFile
|
|
8
|
+
import { type AmaConfig, type AuthFile, type ProfileFile } from "./types.js";
|
|
9
9
|
import { type Diagnostic } from "./checker.js";
|
|
10
10
|
import type { HookConfig } from "../hooks/types.js";
|
|
11
|
-
export type { AmaConfig, AuthFile, ProfileFile, TrustFile, TrustEntry, ProviderConfig, PermissionConfig, CompactionConfig, RetryConfig, ToolsConfig, ToolsPreset, ToolsPresetInput, CodemodeConfig, CodemodeMode, HooksSettings, UiConfig, SkillsConfig, CacheConfig, CheckpointsConfig, ModelConfig, ModelOverride, ChannelConfig, } from "./types.js";
|
|
11
|
+
export type { AmaConfig, AuthFile, ProfileFile, TrustFile, TrustEntry, ExtensionTrustEntry, ProviderConfig, PermissionConfig, CompactionConfig, RetryConfig, ToolsConfig, ToolsPreset, ToolsPresetInput, CodemodeConfig, CodemodeMode, HooksSettings, UiConfig, SkillsConfig, CacheConfig, CheckpointsConfig, ModelConfig, ModelOverride, ChannelConfig, } from "./types.js";
|
|
12
12
|
export { CONFIG_FILE_VERSION } from "./types.js";
|
|
13
13
|
export type { HookConfig } from "../hooks/types.js";
|
|
14
14
|
export type { Diagnostic } from "./checker.js";
|
|
15
15
|
export { THINKING_LEVELS } from "./checker.js";
|
|
16
|
+
export { asTrustFile, validateTrustFile } from "./schema-trust.js";
|
|
16
17
|
export declare const HOOK_TIMEOUT_MAX_MS = 600000;
|
|
17
18
|
export declare function hasErrors(diagnostics: readonly Diagnostic[]): boolean;
|
|
18
19
|
export declare function validateConfig(value: unknown): Diagnostic[];
|
|
19
20
|
export declare function validateAuthFile(value: unknown): Diagnostic[];
|
|
20
21
|
export declare const PROFILE_PATH_FIELDS: readonly ["host", "hooksFile", "authFile", "sessionDir", "config"];
|
|
21
|
-
export declare const PROFILE_PATH_LIST_FIELDS: readonly ["instructions", "skillDirs", "promptDirs", "agentDirs"];
|
|
22
|
+
export declare const PROFILE_PATH_LIST_FIELDS: readonly ["instructions", "skillDirs", "promptDirs", "agentDirs", "extensions"];
|
|
22
23
|
export declare function validateProfile(value: unknown): Diagnostic[];
|
|
23
|
-
export declare function validateTrustFile(value: unknown): Diagnostic[];
|
|
24
24
|
export declare function validateHookConfig(value: unknown): Diagnostic[];
|
|
25
25
|
/** 校验通过后的类型断言(调用方保证 `hasErrors()` 为 false)。 */
|
|
26
26
|
export declare function asConfig(value: unknown): AmaConfig;
|
|
27
27
|
export declare function asAuthFile(value: unknown): AuthFile;
|
|
28
28
|
export declare function asProfile(value: unknown): ProfileFile;
|
|
29
|
-
export declare function asTrustFile(value: unknown): TrustFile;
|
|
30
29
|
export declare function asHookConfig(value: unknown): HookConfig;
|
package/dist/config/schema.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import { isAbsolute } from "node:path";
|
|
9
9
|
import { msg } from "../i18n/index.js";
|
|
10
10
|
import { MAX_TIMER_MS } from "../tools/timers.js";
|
|
11
|
-
import { MIN_CONTEXT_BUDGET
|
|
11
|
+
import { MIN_CONTEXT_BUDGET } from "./types.js";
|
|
12
12
|
import { Checker, THINKING_LEVELS, checkSection, join, } from "./checker.js";
|
|
13
13
|
import { W5_COMPACTION_KEYS, W5_CONFIG_KEYS, W5_UI_KEYS, checkCompactionW5, checkUiW5, validateConfigW5, } from "./schema-w5.js";
|
|
14
14
|
import { W6_CONFIG_KEYS, W6_UI_KEYS, checkOAuthEntry, checkProfileMemory, checkUiW6, validateConfigW6, } from "./schema-w6.js";
|
|
@@ -19,6 +19,7 @@ import { BUILTIN_PROVIDERS } from "../ai/providers/builtin.js";
|
|
|
19
19
|
import { CACHE_RETENTIONS, CHANNEL_NAME_PATTERN, CHECKPOINT_MODES, SANDBOX_ENABLED_MODES, SANDBOX_NETWORK_MODES, CODEMODE_ENGINES, CODEMODE_MODES, TOOLS_PRESET_INPUTS, } from "./types.js";
|
|
20
20
|
export { CONFIG_FILE_VERSION } from "./types.js";
|
|
21
21
|
export { THINKING_LEVELS } from "./checker.js";
|
|
22
|
+
export { asTrustFile, validateTrustFile } from "./schema-trust.js";
|
|
22
23
|
export const HOOK_TIMEOUT_MAX_MS = 600_000;
|
|
23
24
|
export function hasErrors(diagnostics) {
|
|
24
25
|
return diagnostics.some((d) => d.severity === "error");
|
|
@@ -384,6 +385,7 @@ export const PROFILE_PATH_LIST_FIELDS = [
|
|
|
384
385
|
"skillDirs",
|
|
385
386
|
"promptDirs",
|
|
386
387
|
"agentDirs",
|
|
388
|
+
"extensions",
|
|
387
389
|
];
|
|
388
390
|
export function validateProfile(value) {
|
|
389
391
|
const c = new Checker();
|
|
@@ -410,27 +412,6 @@ export function validateProfile(value) {
|
|
|
410
412
|
checkProfileMemory(c, value);
|
|
411
413
|
return c.diagnostics;
|
|
412
414
|
}
|
|
413
|
-
export function validateTrustFile(value) {
|
|
414
|
-
const c = new Checker();
|
|
415
|
-
if (!c.object(value, ""))
|
|
416
|
-
return c.diagnostics;
|
|
417
|
-
c.version(value, "");
|
|
418
|
-
const entries = value["entries"];
|
|
419
|
-
if (!Array.isArray(entries)) {
|
|
420
|
-
c.error("entries", msg().config.schema.array);
|
|
421
|
-
return c.diagnostics;
|
|
422
|
-
}
|
|
423
|
-
entries.forEach((entry, index) => {
|
|
424
|
-
const p = join("entries", index);
|
|
425
|
-
if (!c.object(entry, p))
|
|
426
|
-
return;
|
|
427
|
-
c.string(entry, "path", p, true);
|
|
428
|
-
c.string(entry, "at", p, true);
|
|
429
|
-
if (typeof entry["trusted"] !== "boolean")
|
|
430
|
-
c.error(join(p, "trusted"), msg().config.schema.boolean);
|
|
431
|
-
});
|
|
432
|
-
return c.diagnostics;
|
|
433
|
-
}
|
|
434
415
|
export function validateHookConfig(value) {
|
|
435
416
|
const c = new Checker();
|
|
436
417
|
if (!c.object(value, ""))
|
|
@@ -490,9 +471,6 @@ export function asAuthFile(value) {
|
|
|
490
471
|
export function asProfile(value) {
|
|
491
472
|
return value;
|
|
492
473
|
}
|
|
493
|
-
export function asTrustFile(value) {
|
|
494
|
-
return value;
|
|
495
|
-
}
|
|
496
474
|
export function asHookConfig(value) {
|
|
497
475
|
return value;
|
|
498
476
|
}
|
package/dist/config/trust.d.ts
CHANGED
|
@@ -19,6 +19,8 @@ export declare function isWithin(dir: string, ancestor: string, platform?: NodeJ
|
|
|
19
19
|
export declare function findTrustEntry(entries: readonly TrustEntry[], cwd: string, platform?: NodeJS.Platform): TrustEntry | undefined;
|
|
20
20
|
/** 记录信任决策(同路径覆盖)。 */
|
|
21
21
|
export declare function recordTrust(configDir: string, path: string, trusted: boolean, now?: Date): void;
|
|
22
|
+
/** 原子写 trust.json(保留 `extensions` 段)。 */
|
|
23
|
+
export declare function writeTrustFile(configDir: string, file: TrustFile): void;
|
|
22
24
|
/** cwd 下需要信任才能加载的资源(为空则无需询问)。 */
|
|
23
25
|
export declare function trustGatedResources(cwd: string): string[];
|
|
24
26
|
/** cwd 自身与全部祖先(由内向外)。 */
|
package/dist/config/trust.js
CHANGED
|
@@ -53,10 +53,14 @@ export function recordTrust(configDir, path, trusted, now = new Date()) {
|
|
|
53
53
|
const abs = resolve(path);
|
|
54
54
|
const entries = file.entries.filter((e) => normalize(e.path, process.platform) !== normalize(abs, process.platform));
|
|
55
55
|
entries.push({ path: abs, trusted, at: now.toISOString() });
|
|
56
|
+
writeTrustFile(configDir, { ...file, entries });
|
|
57
|
+
}
|
|
58
|
+
/** 原子写 trust.json(保留 `extensions` 段)。 */
|
|
59
|
+
export function writeTrustFile(configDir, file) {
|
|
56
60
|
const target = trustFilePath(configDir);
|
|
57
61
|
mkdirSync(dirname(target), { recursive: true, mode: 0o700 });
|
|
58
62
|
const tmp = `${target}.${process.pid}.tmp`;
|
|
59
|
-
writeFileSync(tmp, `${JSON.stringify({ version: CONFIG_FILE_VERSION
|
|
63
|
+
writeFileSync(tmp, `${JSON.stringify({ ...file, version: CONFIG_FILE_VERSION }, null, 2)}\n`, {
|
|
60
64
|
mode: 0o600,
|
|
61
65
|
});
|
|
62
66
|
renameSync(tmp, target);
|
package/dist/config/types.d.ts
CHANGED
|
@@ -286,6 +286,8 @@ export interface ProfileFile {
|
|
|
286
286
|
promptDirs?: string[];
|
|
287
287
|
/** [W5-C0] 子 Agent 定义目录(发现顺序在 `--agent-dir` 之后、用户级之前,W5-G)。 */
|
|
288
288
|
agentDirs?: string[];
|
|
289
|
+
/** [#301] 本地扩展文件(绝对路径),排在用户级 `extensions/` 之后。 */
|
|
290
|
+
extensions?: string[];
|
|
289
291
|
hooksFile?: string;
|
|
290
292
|
authFile?: string;
|
|
291
293
|
/** false:key 只来自 authFile,不读环境变量(§11.2)。 */
|
|
@@ -304,8 +306,19 @@ export interface TrustEntry {
|
|
|
304
306
|
/** ISO 8601。 */
|
|
305
307
|
at: string;
|
|
306
308
|
}
|
|
309
|
+
/** [#301] 本地扩展的信任记录:按文件内容 sha256;内容变了记录不再匹配,重新询问。 */
|
|
310
|
+
export interface ExtensionTrustEntry {
|
|
311
|
+
path: string;
|
|
312
|
+
/** 小写十六进制,64 位。 */
|
|
313
|
+
sha256: string;
|
|
314
|
+
trusted: boolean;
|
|
315
|
+
/** ISO 8601。 */
|
|
316
|
+
at: string;
|
|
317
|
+
}
|
|
307
318
|
/** trust.json(只在用户级目录)。 */
|
|
308
319
|
export interface TrustFile {
|
|
309
320
|
version: typeof CONFIG_FILE_VERSION;
|
|
310
321
|
entries: TrustEntry[];
|
|
322
|
+
/** [#301] 本地扩展的信任记录;旧文件没有这一段。 */
|
|
323
|
+
extensions?: ExtensionTrustEntry[];
|
|
311
324
|
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 宿主注入的 runner(`HostApi.runners.provide` 的底座,docs/history/wave5-plan.md §5.5,D17)。[W5-E → W5-EG]
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* [#306] 一个 HostContext 一份(宿主适配器与本地扩展共用,经 {@link ExternalAgents} 的 `hostRunners`
|
|
5
|
+
* 选项交给每个主会话):模块可以在 `create()` 里或之后任何时候 `provide`,跨模块同 id 先到先得(后来者
|
|
6
|
+
* `runner_exists`);返回的函数注销。`onDone` 带 {@link RunnerScope} 时只回调该模块提供的 runner。
|
|
6
7
|
* `onChange` 供 `/agents` / RPC `get_agents` 的缓存刷新。
|
|
7
8
|
*
|
|
8
9
|
* [#226] `onDone`(`HostApi.runners.onDone`):任务入口经 {@link HostRunnerRegistry.resolve} 拿到的是
|
|
@@ -11,6 +12,11 @@
|
|
|
11
12
|
* 同一轮里多次 `wait()` 只发一次,落定后没有新一轮的 `wait()` 不再发。
|
|
12
13
|
*/
|
|
13
14
|
import type { HostRunner, RunnerDoneEvent } from "../host/types.js";
|
|
15
|
+
/** [#306] 提供方范围:onDone 只回调同一 owner 提供的 runner;log 缺省走注册表的 log。 */
|
|
16
|
+
export interface RunnerScope {
|
|
17
|
+
owner: symbol;
|
|
18
|
+
log?: (message: string, error: unknown) => void;
|
|
19
|
+
}
|
|
14
20
|
export type RunnerDoneHandler = (event: RunnerDoneEvent) => void | Promise<void>;
|
|
15
21
|
export interface HostRunnerRegistryOptions {
|
|
16
22
|
/** 毫秒时钟(测试注入)。 */
|
|
@@ -22,11 +28,16 @@ export declare class HostRunnerRegistry {
|
|
|
22
28
|
private readonly runners;
|
|
23
29
|
private readonly listeners;
|
|
24
30
|
private readonly doneHandlers;
|
|
31
|
+
private readonly owners;
|
|
25
32
|
private readonly observed;
|
|
26
33
|
private readonly now;
|
|
27
34
|
private readonly log;
|
|
28
35
|
constructor(options?: HostRunnerRegistryOptions);
|
|
29
|
-
|
|
36
|
+
/**
|
|
37
|
+
* [#306] 跨模块同 id 先到先得,后来者抛 `runner_exists`;同一模块再次提供同 id 照旧替换(旧的注销函数
|
|
38
|
+
* 随之失效)。`owner` 供 onDone 按模块过滤。
|
|
39
|
+
*/
|
|
40
|
+
provide(runner: HostRunner, owner?: symbol): () => void;
|
|
30
41
|
get(id: string): HostRunner | undefined;
|
|
31
42
|
/** [#226] 任务入口用:同 {@link get},但返回发 `onDone` 的观察包装(同一 runner 包装复用)。 */
|
|
32
43
|
resolve(id: string): HostRunner | undefined;
|
|
@@ -34,7 +45,7 @@ export declare class HostRunnerRegistry {
|
|
|
34
45
|
/** 注入 / 注销时回调;返回取消订阅。 */
|
|
35
46
|
onChange(listener: () => void): () => void;
|
|
36
47
|
/** [#226] 每轮结束时回调(`HostApi.runners.onDone`);返回注销函数。 */
|
|
37
|
-
onDone(handler: RunnerDoneHandler): () => void;
|
|
48
|
+
onDone(handler: RunnerDoneHandler, scope?: RunnerScope): () => void;
|
|
38
49
|
private changed;
|
|
39
50
|
private emitDone;
|
|
40
51
|
private fail;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 宿主注入的 runner(`HostApi.runners.provide` 的底座,docs/history/wave5-plan.md §5.5,D17)。[W5-E → W5-EG]
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* [#306] 一个 HostContext 一份(宿主适配器与本地扩展共用,经 {@link ExternalAgents} 的 `hostRunners`
|
|
5
|
+
* 选项交给每个主会话):模块可以在 `create()` 里或之后任何时候 `provide`,跨模块同 id 先到先得(后来者
|
|
6
|
+
* `runner_exists`);返回的函数注销。`onDone` 带 {@link RunnerScope} 时只回调该模块提供的 runner。
|
|
6
7
|
* `onChange` 供 `/agents` / RPC `get_agents` 的缓存刷新。
|
|
7
8
|
*
|
|
8
9
|
* [#226] `onDone`(`HostApi.runners.onDone`):任务入口经 {@link HostRunnerRegistry.resolve} 拿到的是
|
|
@@ -31,7 +32,8 @@ function statusOf(result, aborted) {
|
|
|
31
32
|
export class HostRunnerRegistry {
|
|
32
33
|
runners = new Map();
|
|
33
34
|
listeners = new Set();
|
|
34
|
-
doneHandlers = new
|
|
35
|
+
doneHandlers = new Map();
|
|
36
|
+
owners = new Map();
|
|
35
37
|
observed = new WeakMap();
|
|
36
38
|
now;
|
|
37
39
|
log;
|
|
@@ -39,15 +41,23 @@ export class HostRunnerRegistry {
|
|
|
39
41
|
this.now = options.now ?? Date.now;
|
|
40
42
|
this.log = options.log ?? (() => undefined);
|
|
41
43
|
}
|
|
42
|
-
|
|
44
|
+
/**
|
|
45
|
+
* [#306] 跨模块同 id 先到先得,后来者抛 `runner_exists`;同一模块再次提供同 id 照旧替换(旧的注销函数
|
|
46
|
+
* 随之失效)。`owner` 供 onDone 按模块过滤。
|
|
47
|
+
*/
|
|
48
|
+
provide(runner, owner) {
|
|
43
49
|
if (typeof runner?.id !== "string" || runner.id === "" || typeof runner.start !== "function")
|
|
44
50
|
throw new AmaError("invalid_arguments", msg().drivers.host.runnerNeedsIdStart);
|
|
51
|
+
if (this.runners.has(runner.id) && this.owners.get(runner.id) !== owner)
|
|
52
|
+
throw new AmaError("runner_exists", msg().drivers.host.runnerExists(runner.id));
|
|
45
53
|
this.runners.set(runner.id, runner);
|
|
54
|
+
this.owners.set(runner.id, owner);
|
|
46
55
|
this.changed();
|
|
47
56
|
return () => {
|
|
48
57
|
if (this.runners.get(runner.id) !== runner)
|
|
49
58
|
return;
|
|
50
59
|
this.runners.delete(runner.id);
|
|
60
|
+
this.owners.delete(runner.id);
|
|
51
61
|
this.changed();
|
|
52
62
|
};
|
|
53
63
|
}
|
|
@@ -61,7 +71,7 @@ export class HostRunnerRegistry {
|
|
|
61
71
|
return undefined;
|
|
62
72
|
let wrapped = this.observed.get(runner);
|
|
63
73
|
if (wrapped === undefined) {
|
|
64
|
-
wrapped = this.observe(runner);
|
|
74
|
+
wrapped = this.observe(runner, this.owners.get(runner.id));
|
|
65
75
|
this.observed.set(runner, wrapped);
|
|
66
76
|
}
|
|
67
77
|
return wrapped;
|
|
@@ -75,10 +85,10 @@ export class HostRunnerRegistry {
|
|
|
75
85
|
return () => void this.listeners.delete(listener);
|
|
76
86
|
}
|
|
77
87
|
/** [#226] 每轮结束时回调(`HostApi.runners.onDone`);返回注销函数。 */
|
|
78
|
-
onDone(handler) {
|
|
88
|
+
onDone(handler, scope) {
|
|
79
89
|
if (typeof handler !== "function")
|
|
80
90
|
throw new AmaError("invalid_arguments", msg().drivers.host.runnerDoneNeedsFunction);
|
|
81
|
-
this.doneHandlers.
|
|
91
|
+
this.doneHandlers.set(handler, scope);
|
|
82
92
|
return () => void this.doneHandlers.delete(handler);
|
|
83
93
|
}
|
|
84
94
|
changed() {
|
|
@@ -91,31 +101,35 @@ export class HostRunnerRegistry {
|
|
|
91
101
|
}
|
|
92
102
|
}
|
|
93
103
|
}
|
|
94
|
-
emitDone(event) {
|
|
95
|
-
for (const handler of [...this.doneHandlers]) {
|
|
104
|
+
emitDone(event, owner) {
|
|
105
|
+
for (const [handler, scope] of [...this.doneHandlers]) {
|
|
106
|
+
// 带 scope 的处理器只收本模块 runner 的事件
|
|
107
|
+
if (scope !== undefined && scope.owner !== owner)
|
|
108
|
+
continue;
|
|
109
|
+
const fail = (error) => this.fail(error, scope);
|
|
96
110
|
try {
|
|
97
111
|
const out = handler(event);
|
|
98
112
|
if (out instanceof Promise)
|
|
99
|
-
out.catch(
|
|
113
|
+
out.catch(fail);
|
|
100
114
|
}
|
|
101
115
|
catch (error) {
|
|
102
|
-
|
|
116
|
+
fail(error);
|
|
103
117
|
}
|
|
104
118
|
}
|
|
105
119
|
}
|
|
106
|
-
fail(error) {
|
|
107
|
-
this.log(msg().drivers.host.handlerFailed("runners.onDone"), error);
|
|
120
|
+
fail(error, scope) {
|
|
121
|
+
(scope?.log ?? this.log)(msg().drivers.host.handlerFailed("runners.onDone"), error);
|
|
108
122
|
}
|
|
109
|
-
observe(runner) {
|
|
123
|
+
observe(runner, owner) {
|
|
110
124
|
return new Proxy(runner, {
|
|
111
125
|
get: (target, prop) => {
|
|
112
126
|
if (prop !== "start")
|
|
113
127
|
return forward(target, prop);
|
|
114
|
-
return async (request) => this.observeHandle(target.id, request, this.now(), await target.start(request));
|
|
128
|
+
return async (request) => this.observeHandle(target.id, owner, request, this.now(), await target.start(request));
|
|
115
129
|
},
|
|
116
130
|
});
|
|
117
131
|
}
|
|
118
|
-
observeHandle(runner, request, startedAt, handle) {
|
|
132
|
+
observeHandle(runner, owner, request, startedAt, handle) {
|
|
119
133
|
let round = 0;
|
|
120
134
|
let fresh = true;
|
|
121
135
|
let pending = false;
|
|
@@ -146,7 +160,7 @@ export class HostRunnerRegistry {
|
|
|
146
160
|
event.text = done.text;
|
|
147
161
|
if (done.error !== undefined)
|
|
148
162
|
event.error = done.error;
|
|
149
|
-
this.emitDone(event);
|
|
163
|
+
this.emitDone(event, owner);
|
|
150
164
|
};
|
|
151
165
|
const wait = () => {
|
|
152
166
|
const result = handle.wait();
|
package/dist/errors.d.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* `exitCode` 用数字而不是 `ExitCode` 类型,避免底层模块反向依赖 cli/。
|
|
8
8
|
*/
|
|
9
9
|
/** 已知错误码;实现可以使用其它字符串,但下列码的语义固定。 */
|
|
10
|
-
export type KnownErrorCode = "no_api_key" | "busy" | "aborted" | "invalid_arguments" | "schema_violation" | "model_not_found" | "provider_not_found" | "tool_not_found" | "tool_exists" | "config_invalid" | "profile_invalid" | "session_not_found" | "session_corrupt" | "host_load_failed" | "host_version_mismatch" | "hook_failed" | "not_implemented";
|
|
10
|
+
export type KnownErrorCode = "no_api_key" | "busy" | "aborted" | "invalid_arguments" | "schema_violation" | "model_not_found" | "provider_not_found" | "tool_not_found" | "tool_exists" | "runner_exists" | "config_invalid" | "profile_invalid" | "session_not_found" | "session_corrupt" | "host_load_failed" | "host_version_mismatch" | "hook_failed" | "not_implemented";
|
|
11
11
|
export type ErrorCode = KnownErrorCode | (string & {});
|
|
12
12
|
export interface AmaErrorOptions {
|
|
13
13
|
exitCode?: number;
|
package/dist/host/api-impl.d.ts
CHANGED
|
@@ -13,11 +13,18 @@
|
|
|
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
25
|
import { HostRunnerRegistry } from "../drivers/host-runners.js";
|
|
20
26
|
import type { AgentEventName, AgentEvents, ApprovalBroker, HostAdapter, HostAdapterHandle, HostApi, HostMode, InstructionSource } from "./types.js";
|
|
27
|
+
import { HostCommandRegistry } from "./commands.js";
|
|
21
28
|
import { HostResourceRegistry } from "./resources.js";
|
|
22
29
|
import type { ToolRegistryApi } from "../tools/types.js";
|
|
23
30
|
import type { WarmingDecisionHandler } from "../ai/cache/types.js";
|
|
@@ -63,12 +70,46 @@ export interface HostApiDeps {
|
|
|
63
70
|
/** [#270] 宿主资源注册表(与 read 工具共用同一实例);缺省新建。 */
|
|
64
71
|
resources?: HostResourceRegistry;
|
|
65
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* [#301] 多个模块(宿主适配器 + 本地扩展)共享的状态:工具注册表、事件总线、资源注册表经 deps 共享,
|
|
75
|
+
* 其余——指令列表、broker 槽、保温处理器、通知函数、各模块的状态表——放在这里。
|
|
76
|
+
* 单个宿主适配器时自己建一个,行为与此前一致。
|
|
77
|
+
*/
|
|
78
|
+
export interface HostContext {
|
|
79
|
+
/** 全部模块追加的指令,按加载顺序(宿主适配器在前)。 */
|
|
80
|
+
readonly instructions: InstructionSource[];
|
|
81
|
+
/** broker 槽:第一个设置的模块占有,其余模块的 setBroker 被忽略。 */
|
|
82
|
+
readonly broker: {
|
|
83
|
+
owner?: symbol;
|
|
84
|
+
value?: ApprovalBroker;
|
|
85
|
+
};
|
|
86
|
+
/** 全部模块经 `cache.onWarmingDecision` 注册的处理器,按注册顺序;最后一个生效。 */
|
|
87
|
+
readonly warming: WarmingDecisionHandler[];
|
|
88
|
+
/** 各模块的状态表(merged 后给状态栏)。 */
|
|
89
|
+
readonly statuses: Set<Map<string, string>>;
|
|
90
|
+
/** 晚绑定的通知函数(所有模块共用)。 */
|
|
91
|
+
notify: HostNotify | undefined;
|
|
92
|
+
/** 全部模块经 `commands.register` 注册的斜杠命令。 */
|
|
93
|
+
readonly commands: HostCommandRegistry;
|
|
94
|
+
/** [#306] 全部模块经 `runners.provide` 注入的 runner(同 id 先到先得)。 */
|
|
95
|
+
readonly runners: HostRunnerRegistry;
|
|
96
|
+
/** [#304] 由模块禁用的工具名 → 持有它的模块;最后一个持有者撤回时才解除禁用。 */
|
|
97
|
+
readonly disabledBy: Map<string, Set<symbol>>;
|
|
98
|
+
}
|
|
99
|
+
export interface HostContextOptions {
|
|
100
|
+
notify?: HostNotify;
|
|
101
|
+
/** 保留的命令名(内置命令、别名、交互界面命令);由装配方注入。 */
|
|
102
|
+
reservedCommand?: (name: string) => boolean;
|
|
103
|
+
}
|
|
104
|
+
export declare function createHostContext(options?: HostContextOptions): HostContext;
|
|
105
|
+
/** 合并全部模块的状态(同键后加载者覆盖先加载者)。 */
|
|
106
|
+
export declare function mergedHostStatus(context: HostContext): ReadonlyMap<string, string>;
|
|
66
107
|
/** createHostApi 的产物:api 交给适配器,其余给 bootstrap / AgentSession。 */
|
|
67
108
|
export interface HostApiBinding {
|
|
68
109
|
readonly api: HostApi;
|
|
69
|
-
/**
|
|
110
|
+
/** 共享上下文中全部模块追加的指令(系统提示 host 节,按加载与追加顺序)。 */
|
|
70
111
|
readonly instructions: readonly InstructionSource[];
|
|
71
|
-
/**
|
|
112
|
+
/** 共享的 broker 槽(第一个设置的模块占有;占有者可再次设置)。 */
|
|
72
113
|
broker(): ApprovalBroker | undefined;
|
|
73
114
|
/** 适配器注册的工具名。 */
|
|
74
115
|
readonly registeredTools: readonly string[];
|
|
@@ -86,8 +127,10 @@ export interface HostApiBinding {
|
|
|
86
127
|
warmingDecider(): WarmingDecisionHandler | undefined;
|
|
87
128
|
/** [#270] 宿主资源注册表(系统提示资源行与 read 工具都读它)。 */
|
|
88
129
|
readonly resources: HostResourceRegistry;
|
|
130
|
+
/** [#304] 撤回本 binding 的全部注册(幂等);之后注册类调用被忽略并记 warning。 */
|
|
131
|
+
revoke(): void;
|
|
89
132
|
}
|
|
90
|
-
/**
|
|
133
|
+
/** 共享的 runner 注册表(HostApi 不是本模块创建的返回 undefined;[#306] 全部模块共用同一张)。 */
|
|
91
134
|
export declare function hostRunnersOf(api: HostApi): HostRunnerRegistry | undefined;
|
|
92
|
-
export declare function createHostApi(deps: HostApiDeps): HostApiBinding;
|
|
135
|
+
export declare function createHostApi(deps: HostApiDeps, context?: HostContext): HostApiBinding;
|
|
93
136
|
export {};
|