@aiti-ai/ext-shared 0.0.0-stage → 0.1.8
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/LICENSE +21 -0
- package/agent-accounts.test.mjs +227 -0
- package/agent-accounts.ts +324 -0
- package/agent-capability-contract.test.mjs +129 -0
- package/agent-interaction.test.mjs +194 -0
- package/agent-interaction.ts +340 -0
- package/agent-launch.test.mjs +262 -0
- package/agent-spawn-env.test.mjs +169 -0
- package/agent-spawn-env.ts +548 -0
- package/aiti-editor-inject.mjs +49 -0
- package/backend-queue.test.mjs +336 -0
- package/backend-queue.ts +469 -0
- package/blob-save-route.ts +128 -0
- package/cbc-model-catalog.ts +86 -0
- package/check-client.mjs +273 -0
- package/check-ext-globals.mjs +144 -0
- package/controlled-login-dialog.mjs +270 -0
- package/controlled-login.mjs +398 -0
- package/controlled-login.test.mjs +466 -0
- package/convert-saveas.test.mjs +39 -0
- package/convert-saveas.ts +170 -0
- package/copy-client-bundle.mjs +39 -0
- package/cursor-cli-store.ts +377 -0
- package/dedupe-fetch.test.mjs +71 -0
- package/dedupe-fetch.ts +58 -0
- package/deploy-commit.mjs +52 -0
- package/dev-pack.mjs +246 -0
- package/dock-icons.ts +127 -0
- package/esbuild-bin.test.mjs +53 -0
- package/esbuild-bin.ts +66 -0
- package/exec-async.test.mjs +86 -0
- package/exec-async.ts +179 -0
- package/ext-display-names.ts +91 -0
- package/ext-entry-contracts.test.mjs +131 -0
- package/ext-float-dock-core.test.mjs +348 -0
- package/ext-float-dock-core.ts +783 -0
- package/ext-float-dock-lite.ts +52 -0
- package/ext-float-dock.test.mjs +286 -0
- package/ext-float-dock.ts +740 -0
- package/ext-icons-deploy.mjs +24 -0
- package/ext-lazy.test.mjs +186 -0
- package/ext-lazy.tsx +183 -0
- package/ext-model-state.test.mjs +272 -0
- package/ext-model-state.ts +324 -0
- package/ext-protocol.mjs +113 -0
- package/ext-protocol.test.mjs +88 -0
- package/ext-session-usage.test.mjs +219 -0
- package/ext-session-usage.ts +507 -0
- package/external-cli-governance.ts +153 -0
- package/git-ops-client/api.mjs +56 -0
- package/git-ops-client/branch-panel.tsx +987 -0
- package/git-ops-client/i18n.mjs +328 -0
- package/host-tools-allow.test.mjs +46 -0
- package/host-tools-allow.ts +84 -0
- package/icons/alert.svg +1 -0
- package/icons/bookmark.svg +1 -0
- package/icons/bot.svg +1 -0
- package/icons/chart-column.svg +1 -0
- package/icons/check-circle.svg +1 -0
- package/icons/clipboard-list.svg +1 -0
- package/icons/clock.svg +1 -0
- package/icons/coffee.svg +1 -0
- package/icons/edit.svg +1 -0
- package/icons/eye.svg +1 -0
- package/icons/git-branch.svg +1 -0
- package/icons/git-commit-horizontal.svg +1 -0
- package/icons/globe.svg +1 -0
- package/icons/history.svg +1 -0
- package/icons/import.svg +1 -0
- package/icons/inbox.svg +1 -0
- package/icons/key-round.svg +1 -0
- package/icons/layout-grid.svg +1 -0
- package/icons/mail.svg +1 -0
- package/icons/message-circle.svg +1 -0
- package/icons/message-square.svg +1 -0
- package/icons/meter-vertical.svg +1 -0
- package/icons/mic.svg +1 -0
- package/icons/monitor.svg +1 -0
- package/icons/pen-line.svg +1 -0
- package/icons/plug.svg +1 -0
- package/icons/radio-dot.svg +1 -0
- package/icons/radio.svg +1 -0
- package/icons/refresh.svg +1 -0
- package/icons/rocket.svg +1 -0
- package/icons/search.svg +1 -0
- package/icons/shield.svg +1 -0
- package/icons/sparkles.svg +1 -0
- package/icons/square-kanban.svg +1 -0
- package/icons/terminal.svg +1 -0
- package/icons/users.svg +1 -0
- package/icons/volume-2.svg +1 -0
- package/icons/wand.svg +1 -0
- package/icons/wrench.svg +1 -0
- package/icons/x-circle.svg +1 -0
- package/icons/x.svg +1 -0
- package/ignore-semantics.ts +76 -0
- package/lazy-loading-metrics.mjs +61 -0
- package/new-session-cwd.test.mjs +389 -0
- package/new-session-cwd.ts +253 -0
- package/open-viewer-window.ts +65 -0
- package/overlay-back-client.test.mjs +123 -0
- package/overlay-back-client.ts +87 -0
- package/package.json +65 -4
- package/pi-fork-rpc/README.md +12 -0
- package/pi-fork-rpc/cli-resolve.test.mjs +42 -0
- package/pi-fork-rpc/cli-resolve.ts +79 -0
- package/pi-fork-rpc/remote-queue.test.mjs +100 -0
- package/pi-fork-rpc/remote-queue.ts +140 -0
- package/pi-fork-rpc/spec.ts +106 -0
- package/pi-fork-rpc/wire.test.mjs +260 -0
- package/pi-fork-rpc/wire.ts +367 -0
- package/popup-anchor.ts +45 -0
- package/provider-priority-contract.test.mjs +55 -0
- package/reconcile-deps.mjs +41 -0
- package/reload-instances.mjs +60 -0
- package/seed-tool.test.mjs +95 -0
- package/seed-tool.ts +88 -0
- package/session-recycle.ts +109 -0
- package/shared-copy-sync.test.mjs +115 -0
- package/soffice-locate.ts +171 -0
- package/soffice-panel.ts +239 -0
- package/sync-dir.mjs +91 -0
- package/sync-dir.test.mjs +101 -0
- package/sync-gate.test.mjs +93 -0
- package/sync-mirror.test.mjs +85 -0
- package/task-list.test.mjs +91 -0
- package/task-list.ts +118 -0
- package/test-rm-rf.mjs +33 -0
- package/ui-dialogs.test.mjs +198 -0
- package/ui-dialogs.ts +310 -0
- package/README.md +0 -3
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* provider-priority-contract —— 同 agentKey 认领顺序约定断言(债务 D9 防回归)。
|
|
3
|
+
*
|
|
4
|
+
* pi-web `findProvidersForAgent`(lib/session/providers.ts)按 `priority ?? 0` 降序排——
|
|
5
|
+
* 同 agentKey 有多家 provider 时,数值决定谁被认领。约定(规范 §1):
|
|
6
|
+
* - 专属扩展高于通用驱动:qoder 20(aiti-qoder-agent,credits/listModels/区域路由)
|
|
7
|
+
* > CC 族原生 10(claude-code / codebuddy / cursor)
|
|
8
|
+
* > 通用 ACP 驱动 0(aiti-acp-agent,缺省不声明);
|
|
9
|
+
* - 本测试静态扫描各扩展 index.ts 的 priority 声明钉死数值(改动须有意识 + 同步规范)。
|
|
10
|
+
*
|
|
11
|
+
* Run: node --experimental-strip-types --test pi-web-ext/_shared/provider-priority-contract.test.mjs
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import assert from "node:assert/strict";
|
|
15
|
+
import test from "node:test";
|
|
16
|
+
import { readFileSync } from "node:fs";
|
|
17
|
+
import { join, dirname } from "node:path";
|
|
18
|
+
import { fileURLToPath } from "node:url";
|
|
19
|
+
|
|
20
|
+
const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
21
|
+
|
|
22
|
+
/** 约定表:扩展 → 期望 priority(number = 必须显式声明;null = 不得声明(吃缺省 0))。 */
|
|
23
|
+
const EXPECTED = {
|
|
24
|
+
"aiti-qoder-agent": 20,
|
|
25
|
+
"aiti-claude-code-agent": 10,
|
|
26
|
+
"aiti-codebuddy-agent": 10,
|
|
27
|
+
"aiti-cursor-agent": 10,
|
|
28
|
+
"aiti-acp-agent": null, // 通用驱动:永远让位(缺省 0)
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function declaredPriorities(text) {
|
|
32
|
+
const out = [];
|
|
33
|
+
for (const [i, line] of text.split(/\r?\n/).entries()) {
|
|
34
|
+
const t = line.trim();
|
|
35
|
+
if (t.startsWith("//") || t.startsWith("*")) continue;
|
|
36
|
+
const m = t.match(/priority:\s*(\d+)/);
|
|
37
|
+
if (m) out.push({ value: Number(m[1]), line: i + 1 });
|
|
38
|
+
}
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for (const [ext, expected] of Object.entries(EXPECTED)) {
|
|
43
|
+
test(`[${ext}] provider priority 约定(D9)`, () => {
|
|
44
|
+
const text = readFileSync(join(ROOT, ext, "index.ts"), "utf8");
|
|
45
|
+
const declared = declaredPriorities(text);
|
|
46
|
+
if (expected === null) {
|
|
47
|
+
assert.equal(declared.length, 0, `${ext}: 通用驱动不声明 priority(吃缺省 0 让位专属扩展);发现 ${JSON.stringify(declared)}`);
|
|
48
|
+
} else {
|
|
49
|
+
assert.ok(declared.length >= 1, `${ext}: 应声明 priority: ${expected}`);
|
|
50
|
+
for (const d of declared) {
|
|
51
|
+
assert.equal(d.value, expected, `${ext}:${d.line} priority=${d.value} ≠ 约定 ${expected}(改动须同步规范 §1 与 findProvidersForAgent 认领语义)`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* _shared/reconcile-deps.mjs · 通知 pi-web 双实例对齐扩展共享依赖(各扩展 deploy.mjs 共用)
|
|
3
|
+
*
|
|
4
|
+
* 背景:扩展运行时依赖统一装进共享根 <webRoot>/node_modules(jiti 从扩展目录向上解析
|
|
5
|
+
* 自动命中;机制仿 pi 本体 npm: 包安装)。共享根的唯一写入方是 aiti-extman 的
|
|
6
|
+
* shared-deps reconcile——deploy.mjs 拷完文件后 POST 本通知即可,不在部署脚本里跑 npm。
|
|
7
|
+
*
|
|
8
|
+
* 用法(与 reload-instances 同款:允许失败,永不 throw、不影响 deploy 退出码):
|
|
9
|
+
* import { notifyReconcile } from "../../_shared/reconcile-deps.mjs";
|
|
10
|
+
* await notifyReconcile(); // 默认 "[deploy] " 前缀;建议在 reloadInstances() 之前调
|
|
11
|
+
*
|
|
12
|
+
* 失败无害:extman 在下一次 install/remove/reconcile-deps 时会自愈对齐;双实例各自
|
|
13
|
+
* reconcile 各自的 webRoot(~/.pi/web 与 ~/.pi/web-dev),互不相干。
|
|
14
|
+
*/
|
|
15
|
+
import { buildInstances } from "./reload-instances.mjs";
|
|
16
|
+
|
|
17
|
+
export async function notifyReconcile(prefix = "[deploy] ") {
|
|
18
|
+
for (const inst of buildInstances()) {
|
|
19
|
+
if (inst.base === null) {
|
|
20
|
+
console.log(`${prefix}reconcile ${inst.name} 跳过:basePath 非法`);
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
const r = await fetch(inst.base + "/api/extensions/aiti-extman/ops", {
|
|
25
|
+
method: "POST",
|
|
26
|
+
headers: { "content-type": "application/json" },
|
|
27
|
+
body: JSON.stringify({ action: "reconcile-deps", source: "deploy" }),
|
|
28
|
+
});
|
|
29
|
+
const body = await r.json().catch(() => null);
|
|
30
|
+
const err = body && body.deps && body.deps.error;
|
|
31
|
+
if (r.ok && body && body.ok) {
|
|
32
|
+
console.log(`${prefix}reconcile ${inst.name} → ✓(${Object.keys(body.deps?.deps || {}).length} 个共享依赖)`);
|
|
33
|
+
} else {
|
|
34
|
+
console.log(`${prefix}reconcile ${inst.name} → HTTP ${r.status} ⚠${err ? " " + err : "(extman 未就绪?下次安装时自动对齐)"}`);
|
|
35
|
+
}
|
|
36
|
+
} catch (e) {
|
|
37
|
+
const cause = e && e.cause ? String(e.cause.code || e.cause.message || e.cause) : String((e && e.message) || e);
|
|
38
|
+
console.log(`${prefix}reconcile ${inst.name} 不可达(${cause})— 下次安装时自动对齐`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* _shared/reload-instances.mjs · pi-web 双实例热重载(各扩展 deploy.mjs 共用)
|
|
3
|
+
*
|
|
4
|
+
* dev 3001 / 生产 3002;端口可经 AITI_DEV_PORT / AITI_PROD_PORT 覆盖;
|
|
5
|
+
* basePath 双 env(AITI_DEV_BASE_PATH / AITI_PROD_BASE_PATH)按机器设值,
|
|
6
|
+
* 推荐值不带斜杠(48:dev 与 空;155:系统级 wdev 与 win)——git-bash 启动
|
|
7
|
+
* 原生程序时会把 / 开头的 env 值做 MSYS 路径转换(/wdev → D:/apps/Git/wdev)
|
|
8
|
+
* 致 URL 非法;不带斜杠则不触发转换。此处自动补 /(旧 /xxx 格式仍兼容),
|
|
9
|
+
* 兼容不了时提示 MSYS_NO_PATHCONV=1。
|
|
10
|
+
* 避免「server 端代码改了、实例没 reload 导致跑旧逻辑」。
|
|
11
|
+
*
|
|
12
|
+
* 用法(静态 import 或顶层 await import 均可):
|
|
13
|
+
* import { reloadInstances } from "../../_shared/reload-instances.mjs";
|
|
14
|
+
* await reloadInstances(); // 默认 "[deploy] " 前缀
|
|
15
|
+
*/
|
|
16
|
+
const BASE_PATH_RE = /^\/[A-Za-z0-9._-]*$/;
|
|
17
|
+
|
|
18
|
+
function buildInstance(name, portEnv, portDefault, pathEnv, pathDefault) {
|
|
19
|
+
const origin = `http://127.0.0.1:${process.env[portEnv] || portDefault}`;
|
|
20
|
+
const raw = process.env[pathEnv];
|
|
21
|
+
if (raw === undefined || raw === null || String(raw).trim() === "") return { name, base: origin + pathDefault };
|
|
22
|
+
// 自动补 /:wdev → /wdev(推荐无斜杠格式,MSYS 不转换);/wdev → /wdev(旧格式兼容)
|
|
23
|
+
const p = "/" + String(raw).trim().replace(/^\/+|\/+$/g, "");
|
|
24
|
+
if (BASE_PATH_RE.test(p)) return { name, base: origin + p };
|
|
25
|
+
return { name, base: null, raw };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** 双实例清单(dev/prod 各 {name, base});reload-instances 与 reconcile-deps 等共用。 */
|
|
29
|
+
export function buildInstances() {
|
|
30
|
+
return [
|
|
31
|
+
buildInstance("dev", "AITI_DEV_PORT", 3001, "AITI_DEV_BASE_PATH", "/dev"),
|
|
32
|
+
buildInstance("prod", "AITI_PROD_PORT", 3002, "AITI_PROD_BASE_PATH", ""),
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function reloadInstances(prefix = "[deploy] ") {
|
|
37
|
+
// 批次合并(deploy-all 场景):子条目部署脚本被注入 PI_WEB_DEFER_RELOAD=1 时不各自 reload,
|
|
38
|
+
// 由 deploy-all 在全部条目完成后统一执行一轮(N 条目 × 2 实例 → 2 次 POST)。
|
|
39
|
+
// 避免批量部署时的 reload 风暴(2026-09-15 实测:65 条目逐个 reload 把 dev 实例打满)。
|
|
40
|
+
// 单独跑某个扩展 deploy.mjs 不带该 env → 行为不变(部署完立即 reload)。
|
|
41
|
+
if (process.env.PI_WEB_DEFER_RELOAD === "1") {
|
|
42
|
+
console.log(`${prefix}reload 已 defer(PI_WEB_DEFER_RELOAD=1,由 deploy-all 收尾统一执行)`);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
// 允许失败:网络异常/非 2xx/服务未启动/env 非法一律只打提示,永不 throw——不影响 deploy 退出码
|
|
46
|
+
for (const inst of buildInstances()) {
|
|
47
|
+
if (inst.base === null) {
|
|
48
|
+
const msysHint = /[\\:]/.test(inst.raw) ? "——疑似 git-bash MSYS 路径转换,用 MSYS_NO_PATHCONV=1 前缀重跑" : "";
|
|
49
|
+
console.log(`${prefix}reload ${inst.name} 跳过:AITI_${inst.name.toUpperCase()}_BASE_PATH="${inst.raw}" 非法(须形如 /xxx 或留空)${msysHint}`);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
const r = await fetch(inst.base + "/api/extensions/reload", { method: "POST" });
|
|
54
|
+
console.log(`${prefix}reload ${inst.name} → HTTP ${r.status} ${r.ok ? "✓" : "⚠ 失败(请手动 POST /api/extensions/reload)"}`);
|
|
55
|
+
} catch (e) {
|
|
56
|
+
const cause = e && e.cause ? String(e.cause.code || e.cause.message || e.cause) : String((e && e.message) || e);
|
|
57
|
+
console.log(`${prefix}reload ${inst.name} 不可达(${cause})— 需手动 POST /api/extensions/reload`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* _shared/seed-tool — 索引工具行 → pi 原生工具条目的共享口径单测(D18 归一)。
|
|
3
|
+
*
|
|
4
|
+
* 钉死四家(zcode/opencode/qoder/codebuddy)seedHistory 收敛后的统一行为:
|
|
5
|
+
* coerce 严格形状(自 zcode-core 搬来)、空结果占位 vs 结果缺席不造条目、
|
|
6
|
+
* callID 缺席时合成配对 id 的递增语义、model/provider 的「缺席省略 / 空串保留」。
|
|
7
|
+
*
|
|
8
|
+
* Run: node --experimental-strip-types --test pi-web-ext/_shared/seed-tool.test.mjs
|
|
9
|
+
*/
|
|
10
|
+
import assert from "node:assert/strict";
|
|
11
|
+
import test from "node:test";
|
|
12
|
+
|
|
13
|
+
const { EMPTY_TOOL_ERROR, EMPTY_TOOL_OUTPUT, createSeedToolReplay, normalizeSeedTool, seedToolResultText } =
|
|
14
|
+
await import("@aiti-ai/ext-shared/seed-tool");
|
|
15
|
+
|
|
16
|
+
test("normalizeSeedTool:严格形状归一 + 非法回退 null", () => {
|
|
17
|
+
assert.deepEqual(
|
|
18
|
+
normalizeSeedTool({ callID: "call_1", name: "Bash", input: { command: "pwd" }, output: "/home" }),
|
|
19
|
+
{ callID: "call_1", name: "Bash", input: { command: "pwd" }, output: "/home" },
|
|
20
|
+
);
|
|
21
|
+
assert.deepEqual(normalizeSeedTool({ name: "Read", input: null, isError: true }), { name: "Read", input: {}, isError: true });
|
|
22
|
+
assert.equal(normalizeSeedTool({ input: {} }), null, "缺 name");
|
|
23
|
+
assert.equal(normalizeSeedTool("text"), null);
|
|
24
|
+
assert.equal(normalizeSeedTool(null), null);
|
|
25
|
+
assert.equal(normalizeSeedTool([{ name: "Bash" }]), null, "数组不是工具行");
|
|
26
|
+
assert.deepEqual(normalizeSeedTool({ name: "B", input: "raw" })?.input, {}, "input 非对象 → 空对象兜底");
|
|
27
|
+
assert.equal(normalizeSeedTool({ name: "B", output: 123 }).output, undefined, "非字符串 output 丢");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("空 output 与缺席 output 语义分开", () => {
|
|
31
|
+
assert.equal(normalizeSeedTool({ name: "B" }).output, undefined);
|
|
32
|
+
assert.equal(seedToolResultText({ name: "B", input: {}, output: "ok" }), "ok");
|
|
33
|
+
assert.equal(seedToolResultText({ name: "B", input: {}, output: "" }), EMPTY_TOOL_OUTPUT);
|
|
34
|
+
assert.equal(seedToolResultText({ name: "B", input: {}, output: " \n", isError: true }), EMPTY_TOOL_ERROR);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("回放:按 callID 配对出 toolCall + toolResult 两条", () => {
|
|
38
|
+
const replay = createSeedToolReplay("x-tool");
|
|
39
|
+
const out = replay.entries({ callID: "call_1", name: "Bash", input: { command: "ls" }, output: "dist" });
|
|
40
|
+
assert.equal(out.length, 2);
|
|
41
|
+
assert.deepEqual(out[0], {
|
|
42
|
+
role: "assistant",
|
|
43
|
+
content: [{ type: "toolCall", toolCallId: "call_1", toolName: "Bash", input: { command: "ls" } }],
|
|
44
|
+
});
|
|
45
|
+
assert.deepEqual(out[1], {
|
|
46
|
+
role: "toolResult",
|
|
47
|
+
toolCallId: "call_1",
|
|
48
|
+
toolName: "Bash",
|
|
49
|
+
content: [{ type: "text", text: "dist" }],
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("回放:isError 透传;空结果仍配对(占位文案)", () => {
|
|
54
|
+
const replay = createSeedToolReplay("x-tool");
|
|
55
|
+
const err = replay.entries({ callID: "c1", name: "Read", output: "boom", isError: true });
|
|
56
|
+
assert.equal(err[1].isError, true);
|
|
57
|
+
const empty = replay.entries({ callID: "c2", name: "Touch", output: "" });
|
|
58
|
+
assert.deepEqual(empty[1].content, [{ type: "text", text: EMPTY_TOOL_OUTPUT }]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("回放:结果缺席只出调用卡(不凭空造 toolResult)", () => {
|
|
62
|
+
const replay = createSeedToolReplay("x-tool");
|
|
63
|
+
const out = replay.entries({ callID: "c3", name: "Read", input: { path: "/y" } });
|
|
64
|
+
assert.equal(out.length, 1);
|
|
65
|
+
assert.equal(out[0].content[0].toolCallId, "c3");
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("回放:callID 缺席按前缀合成,跨行递增;新回放器重新计数", () => {
|
|
69
|
+
const replay = createSeedToolReplay("qoder-tool");
|
|
70
|
+
const a = replay.entries({ name: "Bash", output: "1" });
|
|
71
|
+
const b = replay.entries({ name: "Bash", output: "2" });
|
|
72
|
+
assert.equal(a[0].content[0].toolCallId, "qoder-tool-1");
|
|
73
|
+
assert.equal(b[0].content[0].toolCallId, "qoder-tool-2");
|
|
74
|
+
assert.equal(b[1].toolCallId, "qoder-tool-2", "结果条目与合成 id 配对");
|
|
75
|
+
assert.equal(createSeedToolReplay("zcode-tool").entries({ name: "Bash" })[0].content[0].toolCallId, "zcode-tool-1");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("回放:model/provider 缺席省略、空串保留(各家既有写法)", () => {
|
|
79
|
+
const replay = createSeedToolReplay("x-tool");
|
|
80
|
+
assert.equal("model" in replay.entries({ name: "B" })[0], false);
|
|
81
|
+
assert.equal("provider" in replay.entries({ name: "B" })[0], false);
|
|
82
|
+
const withOpts = replay.entries({ name: "B" }, { model: "glm-4.7", provider: undefined })[0];
|
|
83
|
+
assert.equal(withOpts.model, "glm-4.7");
|
|
84
|
+
assert.equal("provider" in withOpts, false);
|
|
85
|
+
const qoderish = replay.entries({ name: "B" }, { model: "", provider: "aiti-qoder-agent" })[0];
|
|
86
|
+
assert.equal(qoderish.model, "", "qoder 直播同形状:空 model 也照写");
|
|
87
|
+
assert.equal(qoderish.provider, "aiti-qoder-agent");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("回放:非法/非工具行返回 null,交调用方走文本兜底", () => {
|
|
91
|
+
const replay = createSeedToolReplay("x-tool");
|
|
92
|
+
assert.equal(replay.entries(undefined), null);
|
|
93
|
+
assert.equal(replay.entries('🛠 Bash {"command":"ls"}'), null);
|
|
94
|
+
assert.equal(replay.entries({ name: "" }), null);
|
|
95
|
+
});
|
package/seed-tool.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* seed-tool — 索引工具行(session-manager `IndexedMessage.tool`)→ pi 原生工具条目的共享口径。
|
|
3
|
+
*
|
|
4
|
+
* ⚠ 权威源:本文件。zcode / opencode / qoder / codebuddy 四家的 `seedHistory` 曾各存一份
|
|
5
|
+
* coerce + 回放实现且策略互相漂移(D18:空结果丢/留、input 缺省、model/provider 写法),
|
|
6
|
+
* codex 更晚(2026-09-22)补上 tool 分支——现五家统一走此处,调用方只保留各自的 id 前缀
|
|
7
|
+
* 与 model/provider 取值。新增/改名导出必须发布新版 ext-shared 并 reconcile,否则运行期
|
|
8
|
+
* 拿到 undefined(同 task-list 教训)。
|
|
9
|
+
*
|
|
10
|
+
* 语义(与 claude-like / opencode-family adapter 的生产端不变量对齐):
|
|
11
|
+
* - 带 tool 的行,text 只可能是 🛠 兜底行 → 结构化命中即整体接管该条(调用方 continue,不再出文本);
|
|
12
|
+
* - output 缺席 = adapter 没见到 tool_result(轮中途中断/孤儿调用)→ 只回放调用卡,不凭空造结果;
|
|
13
|
+
* - output 空串 = 工具确实跑完但无文本 → 结果条目给占位文案(与直播路径同口径,卡片不裸奔)。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** 归一后的工具行(input 恒为对象;output 缺席与空串语义不同,见上)。 */
|
|
17
|
+
export interface SeedTool {
|
|
18
|
+
callID?: string;
|
|
19
|
+
name: string;
|
|
20
|
+
input: Record<string, unknown>;
|
|
21
|
+
output?: string;
|
|
22
|
+
isError?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 无文本结果占位(aiti-qoder-agent / aiti-zcode-agent 直播路径既有文案)。 */
|
|
26
|
+
export const EMPTY_TOOL_OUTPUT = "(工具执行完成,无文本输出)";
|
|
27
|
+
export const EMPTY_TOOL_ERROR = "(工具执行失败)";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* IndexedMessage.tool → 严格形状。非法(非对象/数组/缺 name)返回 null —— 调用方回退
|
|
31
|
+
* 🛠 文本行展示(老索引、未知格式不丢内容)。
|
|
32
|
+
*/
|
|
33
|
+
export function normalizeSeedTool(v: unknown): SeedTool | null {
|
|
34
|
+
if (!v || typeof v !== "object" || Array.isArray(v)) return null;
|
|
35
|
+
const t = v as Record<string, unknown>;
|
|
36
|
+
if (typeof t.name !== "string" || !t.name) return null;
|
|
37
|
+
const input = t.input && typeof t.input === "object" && !Array.isArray(t.input)
|
|
38
|
+
? { ...(t.input as Record<string, unknown>) }
|
|
39
|
+
: {};
|
|
40
|
+
return {
|
|
41
|
+
...(typeof t.callID === "string" && t.callID ? { callID: t.callID } : {}),
|
|
42
|
+
name: t.name,
|
|
43
|
+
input,
|
|
44
|
+
...(typeof t.output === "string" ? { output: t.output } : {}),
|
|
45
|
+
...(t.isError === true ? { isError: true } : {}),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 工具结果条目的文本:空白结果按成败/失败给占位,避免宿主渲出无内容的空卡。 */
|
|
50
|
+
export function seedToolResultText(tool: SeedTool): string {
|
|
51
|
+
const out = tool.output ?? "";
|
|
52
|
+
return out.trim() ? out : (tool.isError ? EMPTY_TOOL_ERROR : EMPTY_TOOL_OUTPUT);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 一次 `seedHistory` 的回放器(callID 缺席时按前缀合成配对 id,故计数须随调用周期新建)。
|
|
57
|
+
* `entries()` 返回 null 表示该行不是结构化工具行,调用方自行走文本兜底。
|
|
58
|
+
*/
|
|
59
|
+
export function createSeedToolReplay(idPrefix: string): {
|
|
60
|
+
entries(tool: unknown, opts?: { model?: string; provider?: string }): Array<Record<string, unknown>> | null;
|
|
61
|
+
} {
|
|
62
|
+
let seq = 0;
|
|
63
|
+
return {
|
|
64
|
+
entries(tool: unknown, opts?: { model?: string; provider?: string }): Array<Record<string, unknown>> | null {
|
|
65
|
+
const t = normalizeSeedTool(tool);
|
|
66
|
+
if (!t) return null;
|
|
67
|
+
const callId = t.callID || `${idPrefix}-${++seq}`;
|
|
68
|
+
const items: Array<Record<string, unknown>> = [
|
|
69
|
+
{
|
|
70
|
+
role: "assistant",
|
|
71
|
+
content: [{ type: "toolCall", toolCallId: callId, toolName: t.name, input: t.input }],
|
|
72
|
+
...(opts?.model !== undefined ? { model: opts.model } : {}),
|
|
73
|
+
...(opts?.provider !== undefined ? { provider: opts.provider } : {}),
|
|
74
|
+
},
|
|
75
|
+
];
|
|
76
|
+
if (t.output !== undefined) {
|
|
77
|
+
items.push({
|
|
78
|
+
role: "toolResult",
|
|
79
|
+
toolCallId: callId,
|
|
80
|
+
toolName: t.name,
|
|
81
|
+
content: [{ type: "text", text: seedToolResultText(t) }],
|
|
82
|
+
...(t.isError ? { isError: true } : {}),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return items;
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* session-recycle —— 会话 recycle API 共享实现(债务 D8)。
|
|
3
|
+
*
|
|
4
|
+
* 把存量 handle 升级到当前扩展代码,无需重启 pi-web 进程(扩展热重载只换模块,
|
|
5
|
+
* core handle store(globalThis.__sessionHandles)里已存在的 handle 实例继续跑老代码)。
|
|
6
|
+
* 样板:aiti-zcode-agent / aiti-codebuddy-agent 的本地实现(先例);本层抽公共骨架,
|
|
7
|
+
* 各扩展以「isOwn + parseId」两个闭包接入。
|
|
8
|
+
*
|
|
9
|
+
* GET /session/recycle → 存量会话清单(id/running/alive)
|
|
10
|
+
* POST /session/recycle {id, force?} → 空闲校验(运行中需 force:true,会打断当前轮)
|
|
11
|
+
* → dispose 旧 handle → 自 fetch POST /api/session-backends(loopback 恒过门禁)
|
|
12
|
+
* → core 复活分支建新 handle(当前代码)→ putHandle 覆盖。
|
|
13
|
+
* 占位 id(未发过消息的空会话)仅 dispose——前端下次访问按需重建。
|
|
14
|
+
*
|
|
15
|
+
* ⚠ 权威源:pi-web-ext/_shared/session-recycle.ts。部署时复制进各扩展目录
|
|
16
|
+
* (同 backend-queue 先例)——改动须同步全部副本,头部保留本注释。
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export interface RecycleHandleLike {
|
|
20
|
+
id: string;
|
|
21
|
+
isRunning(): boolean;
|
|
22
|
+
alive(): boolean;
|
|
23
|
+
dispose(): unknown;
|
|
24
|
+
cwd?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface SessionRecycleOpts {
|
|
28
|
+
/** GET 清单过滤 + POST id 归属校验(扩展自认领:id 前缀或 handle 特征)。 */
|
|
29
|
+
isOwn(handle: RecycleHandleLike): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* id → 重建参数。返回 null = id 不合法(400);
|
|
32
|
+
* sessionId 为空串 = 占位会话(仅 dispose,不重建);providerType = 扩展注册 type 全名。
|
|
33
|
+
* handle 参数 = 该 id 当前对应的旧 handle(providerType 可按 handle 特征细分,如 pi 双 fork)。
|
|
34
|
+
*/
|
|
35
|
+
parseId(id: string, handle: RecycleHandleLike): { sessionId: string; providerType: string } | null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 服务端自基址(loopback;127.0.0.1 直连不走 nginx/Authelia,经域名来的回退 argv 解析)。 */
|
|
39
|
+
export function selfBaseFromRequest(req: Request): string {
|
|
40
|
+
try {
|
|
41
|
+
const u = new URL(req.url);
|
|
42
|
+
if (u.hostname === "127.0.0.1" || u.hostname === "localhost" || /^\d+\.\d+\.\d+\.\d+$/.test(u.hostname)) {
|
|
43
|
+
return u.origin;
|
|
44
|
+
}
|
|
45
|
+
} catch { /* fallthrough */ }
|
|
46
|
+
const i = process.argv.indexOf("--port");
|
|
47
|
+
const port = i >= 0 ? Number(process.argv[i + 1]) : NaN;
|
|
48
|
+
return `http://127.0.0.1:${Number.isFinite(port) ? port : 3000}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function createSessionRecycleApi(opts: SessionRecycleOpts): (
|
|
52
|
+
req: Request,
|
|
53
|
+
ctx: { path: string[]; method: string },
|
|
54
|
+
) => Promise<Response | null> {
|
|
55
|
+
return async (req, ctx) => {
|
|
56
|
+
if (ctx.path[0] !== "session" || ctx.path[1] !== "recycle") return null;
|
|
57
|
+
const store = (globalThis as { __sessionHandles?: Map<string, RecycleHandleLike> }).__sessionHandles;
|
|
58
|
+
if (ctx.method === "GET") {
|
|
59
|
+
const sessions = [...(store?.values() ?? [])]
|
|
60
|
+
.filter((h) => opts.isOwn(h))
|
|
61
|
+
.map((h) => ({ id: h.id, running: h.isRunning(), alive: h.alive() }));
|
|
62
|
+
return Response.json({ sessions });
|
|
63
|
+
}
|
|
64
|
+
if (req.method !== "POST") return Response.json({ error: "method not allowed" }, { status: 405 });
|
|
65
|
+
let body: { id?: unknown; force?: unknown } = {};
|
|
66
|
+
try { body = await req.json() as typeof body; } catch { /* 空体按缺省 */ }
|
|
67
|
+
const id = typeof body.id === "string" ? body.id : "";
|
|
68
|
+
const old = store?.get(id);
|
|
69
|
+
if (!old || !opts.isOwn(old)) {
|
|
70
|
+
return Response.json({ error: `无存活的 handle:${id}` }, { status: 404 });
|
|
71
|
+
}
|
|
72
|
+
const parsed = opts.parseId(id, old);
|
|
73
|
+
if (!parsed) return Response.json({ error: `id 不属于本扩展(或无法解析):${id}` }, { status: 400 });
|
|
74
|
+
if (old.isRunning() && !body.force) {
|
|
75
|
+
return Response.json(
|
|
76
|
+
{ error: "会话正在运行中——等本轮收口后重试;确认要打断请带 force:true(当前轮作废,已排队消息随之清空)", running: true },
|
|
77
|
+
{ status: 409 },
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
const cwd = typeof old.cwd === "string" ? old.cwd : "";
|
|
81
|
+
try { await old.dispose(); } catch { /* 已死容错 */ }
|
|
82
|
+
if (!parsed.sessionId) {
|
|
83
|
+
return Response.json({ recycled: true, recreated: false, id, note: "占位会话已释放(未发过消息的空会话),前端下次访问按需重建" });
|
|
84
|
+
}
|
|
85
|
+
const base = selfBaseFromRequest(req);
|
|
86
|
+
try {
|
|
87
|
+
const res = await fetch(`${base}/api/session-backends`, {
|
|
88
|
+
method: "POST",
|
|
89
|
+
headers: { "content-type": "application/json" },
|
|
90
|
+
body: JSON.stringify({ providerType: parsed.providerType, sessionId: parsed.sessionId, ...(cwd ? { cwd } : {}) }),
|
|
91
|
+
});
|
|
92
|
+
const data = await res.json().catch(() => ({}));
|
|
93
|
+
if (!res.ok) {
|
|
94
|
+
return Response.json({
|
|
95
|
+
recycled: true, recreated: false, id,
|
|
96
|
+
error: `重建请求 HTTP ${res.status}: ${JSON.stringify(data).slice(0, 200)}`,
|
|
97
|
+
note: "旧 handle 已释放——前端刷新该会话即走复活路径用当前代码重建",
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return Response.json({ recycled: true, recreated: true, id, session: data });
|
|
101
|
+
} catch (e) {
|
|
102
|
+
return Response.json({
|
|
103
|
+
recycled: true, recreated: false, id,
|
|
104
|
+
error: `重建请求失败: ${e instanceof Error ? e.message : String(e)}`,
|
|
105
|
+
note: "旧 handle 已释放——前端刷新该会话即走复活路径用当前代码重建",
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* shared-copy-sync —— 共享层「权威源 ↔ 扩展副本」一致性校验(债务清单 D7)。
|
|
3
|
+
*
|
|
4
|
+
* 背景:backend-queue.ts / agent-spawn-env.ts 等共享实现层以手工副本方式分发到各
|
|
5
|
+
* agent 扩展目录(部署随扩展拷贝),无同步脚本——副本侧改了不回写权威源(或反向)
|
|
6
|
+
* 即静默漂移,D1 的捕获缺失曾同时存在于全部副本。
|
|
7
|
+
*
|
|
8
|
+
* 校验策略:
|
|
9
|
+
* 1. 整文件副本:凡全仓出现的同名副本必须与 _shared 权威源逐字节一致;
|
|
10
|
+
* 2. external-governance.ts(副本文件名)↔ external-cli-governance.ts(权威源):
|
|
11
|
+
* 副本是按扩展裁剪的子集(各只保留自家 CLI 白名单段),校验「副本非注释行 ⊆
|
|
12
|
+
* 权威源行集合」——抓逻辑改动不回写,允许裁剪。
|
|
13
|
+
*
|
|
14
|
+
* Run: node --experimental-strip-types --test pi-web-ext/_shared/shared-copy-sync.test.mjs
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import assert from "node:assert/strict";
|
|
18
|
+
import test from "node:test";
|
|
19
|
+
import { readFileSync, readdirSync, statSync } from "node:fs";
|
|
20
|
+
import { join, dirname, relative } from "node:path";
|
|
21
|
+
import { fileURLToPath } from "node:url";
|
|
22
|
+
|
|
23
|
+
const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
24
|
+
const SEP = process.platform === "win32" ? "\\" : "/";
|
|
25
|
+
|
|
26
|
+
/** 整文件副本(文件名相同,权威源 _shared/<name>)。 */
|
|
27
|
+
const EXACT_COPIES = [
|
|
28
|
+
"backend-queue.ts",
|
|
29
|
+
"agent-spawn-env.ts",
|
|
30
|
+
"ext-model-state.ts",
|
|
31
|
+
"controlled-login.mjs",
|
|
32
|
+
"cbc-model-catalog.ts",
|
|
33
|
+
"cursor-cli-store.ts", // 副本另含 session-manager/lib/agents/(find 全仓覆盖)
|
|
34
|
+
"session-recycle.ts", // D8:acp/qoder/pi/attach 四家副本
|
|
35
|
+
"exec-async.ts", // execSync→async 改造(2026-09-12):git-trace/git-review/channels/editor/agents/cmd/session-manager 副本
|
|
36
|
+
"agent-accounts.ts", // 账号档引擎(2026-09-13):aiti-agent-manager 副本
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
/** 裁剪子集副本:副本文件名 → 权威源文件名。 */
|
|
40
|
+
const SUBSET_COPIES = {
|
|
41
|
+
"external-governance.ts": "external-cli-governance.ts",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function walkFiles(dir, out = []) {
|
|
45
|
+
let entries;
|
|
46
|
+
try {
|
|
47
|
+
entries = readdirSync(dir);
|
|
48
|
+
} catch {
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
for (const name of entries) {
|
|
52
|
+
const p = join(dir, name);
|
|
53
|
+
const st = statSync(p);
|
|
54
|
+
if (st.isDirectory()) {
|
|
55
|
+
if (name === "node_modules" || name === "dist" || name === ".git" || name === ".next") continue;
|
|
56
|
+
walkFiles(p, out);
|
|
57
|
+
} else {
|
|
58
|
+
out.push(p);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const allFiles = walkFiles(ROOT);
|
|
65
|
+
|
|
66
|
+
/** 非注释/非空行(trim 后),用于子集比较。 */
|
|
67
|
+
function codeLines(text) {
|
|
68
|
+
return text.split(/\r?\n/)
|
|
69
|
+
.map((l) => l.trim())
|
|
70
|
+
.filter((l) => l && !l.startsWith("//") && !l.startsWith("*") && !l.startsWith("/*"));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
test("整文件共享副本与权威源逐字节一致(D7)", () => {
|
|
74
|
+
const problems = [];
|
|
75
|
+
for (const name of EXACT_COPIES) {
|
|
76
|
+
const authority = join(ROOT, "_shared", name);
|
|
77
|
+
let authBuf;
|
|
78
|
+
try {
|
|
79
|
+
authBuf = readFileSync(authority);
|
|
80
|
+
} catch {
|
|
81
|
+
problems.push(`权威源缺失: _shared/${name}(副本校验无从谈起,请确认权威源在位)`);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const copies = allFiles.filter((p) => p.split(SEP).pop() === name && relative(ROOT, p).split(SEP)[0] !== "_shared");
|
|
85
|
+
// M2a(2026-09-16)后副本已退役为 @aiti-ai/ext-shared 包导入:副本可以为零;
|
|
86
|
+
// 若再出现同名副本(新扩展手拷),仍强制与权威源逐字节一致——防漂移守卫保留。
|
|
87
|
+
for (const copy of copies) {
|
|
88
|
+
const same = readFileSync(copy).equals(authBuf);
|
|
89
|
+
if (!same) {
|
|
90
|
+
problems.push(`副本漂移: ${relative(ROOT, copy)} ≠ _shared/${name} —— 以权威源为准同步(副本侧改动须先回写 _shared)`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
assert.deepEqual(problems, [], `\n${problems.join("\n")}`);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("external-governance 副本是权威源(external-cli-governance)的行级子集(D7)", () => {
|
|
98
|
+
const problems = [];
|
|
99
|
+
for (const [copyName, authName] of Object.entries(SUBSET_COPIES)) {
|
|
100
|
+
const authority = new Set(codeLines(readFileSync(join(ROOT, "_shared", authName), "utf8")));
|
|
101
|
+
const copies = allFiles.filter((p) => p.split(SEP).pop() === copyName);
|
|
102
|
+
assert.ok(copies.length, `${copyName}: 全仓未发现任何副本`);
|
|
103
|
+
for (const copy of copies) {
|
|
104
|
+
const missing = codeLines(readFileSync(copy, "utf8")).filter((l) => !authority.has(l));
|
|
105
|
+
if (missing.length) {
|
|
106
|
+
problems.push(
|
|
107
|
+
`副本越权/漂移: ${relative(ROOT, copy)} 有 ${missing.length} 行不在权威源 _shared/${authName} 中` +
|
|
108
|
+
`(前 3 行: ${missing.slice(0, 3).map((s) => JSON.stringify(s.slice(0, 60))).join(" | ")})——` +
|
|
109
|
+
`逻辑改动必须先回写权威源再裁剪`,
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
assert.deepEqual(problems, [], `\n${problems.join("\n")}`);
|
|
115
|
+
});
|