pi-web-ui 0.87.1 → 0.88.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 +33 -1
- package/bin/pi-web-ui.mjs +463 -0
- package/dist/server/agent-service.js +71 -4
- package/dist/server/dsh/dsh-agent-service.js +17 -8
- package/dist/server/host-metrics.js +59 -0
- package/dist/server/index.js +182 -21
- package/dist/server/plugin-facilities.js +115 -2
- package/dist/server/plugin-llm.js +129 -0
- package/dist/server/plugin-permissions.js +176 -0
- package/dist/server/plugin-schedule.js +211 -0
- package/dist/server/plugins.js +865 -67
- package/dist/server/terminals.js +23 -0
- package/package.json +2 -1
- package/plugin-sdk/README.md +127 -0
- package/plugin-sdk/index.d.ts +450 -0
- package/plugin-sdk/index.mjs +536 -0
- package/plugin-sdk/package.json +9 -0
- package/web/dist/assets/TerminalPanel-CDH27dNP.js +6 -0
- package/web/dist/assets/TerminalPanel-DOrYoP_4.css +32 -0
- package/web/dist/assets/index-CQg5A74_.js +375 -0
- package/web/dist/assets/index-DTEcxHWs.css +10 -0
- package/web/dist/index.html +2 -3
- package/web/dist/assets/index-8xCnPMZP.js +0 -374
- package/web/dist/assets/index-F86qWlJy.css +0 -41
package/dist/server/terminals.js
CHANGED
|
@@ -863,6 +863,10 @@ export class TerminalManager {
|
|
|
863
863
|
idleTimer: null,
|
|
864
864
|
watches: [],
|
|
865
865
|
agentBash,
|
|
866
|
+
// 有命令的终端(runCommand)与 ai-bash 天生就是“用过”的;裸 shell
|
|
867
|
+
// 从 pristine 开始,第一次输入/agent 触碰时才置位(见 inputChecked /
|
|
868
|
+
// noteAgentActivity)。
|
|
869
|
+
used: agentBash || command !== undefined,
|
|
866
870
|
locale,
|
|
867
871
|
};
|
|
868
872
|
this.terms.set(id, entry);
|
|
@@ -901,6 +905,7 @@ export class TerminalManager {
|
|
|
901
905
|
if (!entry || entry.exited)
|
|
902
906
|
return;
|
|
903
907
|
entry.agentTouched = true;
|
|
908
|
+
entry.used = true;
|
|
904
909
|
entry.lastActivityAt = Date.now();
|
|
905
910
|
this.armIdleWatch(entry);
|
|
906
911
|
}
|
|
@@ -1065,6 +1070,21 @@ export class TerminalManager {
|
|
|
1065
1070
|
n++;
|
|
1066
1071
|
return n;
|
|
1067
1072
|
}
|
|
1073
|
+
/** Count of LIVE terminals that were actually used (typed into / ran a
|
|
1074
|
+
* command / touched by the agent / ai-bash). Pristine shells — e.g. the
|
|
1075
|
+
* one auto-created when the user opens the terminal tab but never types
|
|
1076
|
+
* anything — do NOT count: they hold no foreground work and no shell
|
|
1077
|
+
* state worth protecting, so they neither retain the conversation nor
|
|
1078
|
+
* block its dismissal (they are killed together with the conversation).
|
|
1079
|
+
* Retained-output history never counts either (same as countLive).
|
|
1080
|
+
* 存活且“用过”的终端数——对话保留/关闭拦截只看这个口径。 */
|
|
1081
|
+
countBlockingLive() {
|
|
1082
|
+
let n = 0;
|
|
1083
|
+
for (const entry of this.terms.values())
|
|
1084
|
+
if (!entry.exited && entry.used)
|
|
1085
|
+
n++;
|
|
1086
|
+
return n;
|
|
1087
|
+
}
|
|
1068
1088
|
emitList() {
|
|
1069
1089
|
this.emit({ type: "terminal_list", terminals: this.list() });
|
|
1070
1090
|
}
|
|
@@ -1116,10 +1136,13 @@ export class TerminalManager {
|
|
|
1116
1136
|
if (!entry || entry.exited)
|
|
1117
1137
|
return pick(this.lang?.() ?? "en", "终端不存在或进程已退出", "Terminal not found or its process has exited", "terminals.not.found.exited");
|
|
1118
1138
|
// 已武装的纪元里任何人(含用户手动敲键盘)写了输入都算新活动,重置倒计时。
|
|
1139
|
+
// 成功的输入同时把终端标为“用过”(见 used):动过的 shell 才参与对话保留/
|
|
1140
|
+
// 关闭拦截,没动过的空 shell 不算。
|
|
1119
1141
|
entry.lastActivityAt = Date.now();
|
|
1120
1142
|
if (entry.idleTimer)
|
|
1121
1143
|
this.armIdleWatch(entry);
|
|
1122
1144
|
entry.pty.write(data);
|
|
1145
|
+
entry.used = true;
|
|
1123
1146
|
return null;
|
|
1124
1147
|
}
|
|
1125
1148
|
key(id, key, modifiers = {}) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-web-ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.88.0",
|
|
4
4
|
"description": "Web chat interface for the pi coding agent, powered by the pi SDK (@earendil-works/pi-coding-agent) — one-command run, Docker/systemd/launchd deployable",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"themes/",
|
|
22
22
|
"deploy/",
|
|
23
23
|
"extensions/",
|
|
24
|
+
"plugin-sdk/",
|
|
24
25
|
"plugins/catalog.json",
|
|
25
26
|
"CHANGELOG.md",
|
|
26
27
|
"README.md",
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# 插件 SDK(starter)
|
|
2
|
+
|
|
3
|
+
## 脚手架:`pi-web-ui plugin create`
|
|
4
|
+
|
|
5
|
+
不用手拷文件,一条命令生成最小可跑骨架(`manifest.json` + `index.mjs` + `client/entry.mjs` + `README.md`,SDK 自动拷进 `sdk/`):
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pi-web-ui plugin create my-plugin --template minimal # 零权限,可直接激活
|
|
9
|
+
pi-web-ui plugin create my-plugin --template ui-slot # composer.actions 按钮 + onUiAction 示例
|
|
10
|
+
pi-web-ui plugin create my-plugin --template agent-tool # registerAgentTool 示例
|
|
11
|
+
pi-web-ui plugin create my-plugin --template renderer # view:false + renderers 示例
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
常用选项:`--dir <插件父目录>`(默认 `<dataDir>/plugins`,也可用 `--data-dir` 指定数据目录)、`--force`(目标已存在时覆盖)。生成后会自动做一次 manifest 基础校验 + 生成文件的 `node --check`,有 warning 会直接打印;生效只需刷新浏览器(或发 `plugins_reload`),未运行则下次启动生效。
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
`@pi-web-ui/plugin-sdk` 是写插件的**起手包**:`index.mjs`(零依赖纯 ESM,可直接拷进插件目录)+ `index.d.ts`(宿主接口精简类型,编辑器补全用)。
|
|
19
|
+
|
|
20
|
+
> 和 `server/plugins.ts` 的全量 `PluginHost` 是**同语义的精简版**:运行时以宿主实际注入为准,宿主版本见 `PLUGIN_API_VERSION`。SDK 只求“写得顺”,不做运行时 polyfill。
|
|
21
|
+
|
|
22
|
+
## 怎么用
|
|
23
|
+
|
|
24
|
+
1. 把 `index.mjs`(和要补全就把 `index.d.ts`)拷进你的插件目录(如 `my-plugin/sdk/`);
|
|
25
|
+
2. 服务端入口:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import { definePlugin, selectOptions } from "./sdk/index.mjs";
|
|
29
|
+
|
|
30
|
+
export default definePlugin({
|
|
31
|
+
async activate(host) {
|
|
32
|
+
const tone = String(host.getSettings().tone ?? "short");
|
|
33
|
+
const off = host.ui.register({
|
|
34
|
+
slot: "composer.actions",
|
|
35
|
+
id: "tone",
|
|
36
|
+
label: "语气",
|
|
37
|
+
kind: "select",
|
|
38
|
+
action: "my-plugin:tone",
|
|
39
|
+
value: tone,
|
|
40
|
+
options: selectOptions([
|
|
41
|
+
{ value: "short", label: "简短" },
|
|
42
|
+
{ value: "full", label: "详细" },
|
|
43
|
+
]),
|
|
44
|
+
});
|
|
45
|
+
// 反激活时注销(可选,宿主也会统一清理)
|
|
46
|
+
return;
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
3. 客户端视图(`client/entry.mjs`):
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
import { defineView, onUiAction } from "./sdk/index.mjs";
|
|
55
|
+
|
|
56
|
+
export default defineView({
|
|
57
|
+
mount(el, ctx) {
|
|
58
|
+
const status = document.createElement("div");
|
|
59
|
+
status.textContent = "就绪";
|
|
60
|
+
el.appendChild(status);
|
|
61
|
+
// 动作回调走宿主桥(client bundle 与宿主同页,window 直达),
|
|
62
|
+
// 要落盘/跨端再经 ctx.send 发给服务端入口。
|
|
63
|
+
return onUiAction("my-plugin:tone", (itemId, value) => {
|
|
64
|
+
status.textContent = `语气:${value ?? ""}`;
|
|
65
|
+
ctx.send({ action: "my-plugin:tone", value });
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
注意 `select` 的切换值是第二个参数:`onUiAction("my-plugin:tone", (itemId, value) => …)`。
|
|
72
|
+
|
|
73
|
+
## 本地单测(createMockHost)
|
|
74
|
+
|
|
75
|
+
插件作者不用起服务就能测 `activate`:SDK 自带 `createMockHost(overrides?)`(零依赖纯 ESM,`node --test` / vitest 均可):
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
// my-plugin/index.test.mjs —— 跑法:node --test index.test.mjs
|
|
79
|
+
import { describe, it } from "node:test";
|
|
80
|
+
import assert from "node:assert/strict";
|
|
81
|
+
import plugin from "./index.mjs";
|
|
82
|
+
import { createMockHost } from "./sdk/index.mjs";
|
|
83
|
+
|
|
84
|
+
describe("my-plugin", () => {
|
|
85
|
+
it("activate 注册 UI + 读注入的设置", async () => {
|
|
86
|
+
const host = createMockHost({ settings: { tone: "full" } });
|
|
87
|
+
await plugin.activate(host);
|
|
88
|
+
const reg = host.calls.find((c) => c.method === "ui.register");
|
|
89
|
+
assert.ok(reg, "应该调过 host.ui.register");
|
|
90
|
+
assert.equal(reg.args[0].value, "full"); // 读到的是注入的预设
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("reset() 清空记录;emit 模拟客户端消息", async () => {
|
|
94
|
+
const host = createMockHost();
|
|
95
|
+
await plugin.activate(host);
|
|
96
|
+
host.reset();
|
|
97
|
+
assert.equal(host.calls.length, 0);
|
|
98
|
+
assert.equal(host.logs.length, 0);
|
|
99
|
+
host.mock.emit("onMessage", { action: "my-plugin:tone" }, "client-1");
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
要点:
|
|
105
|
+
|
|
106
|
+
- `host.calls`:全量调用记录 `{ method, args, seq }`(嵌套按点分路径,如 `"ui.register"`、`"fs.readText"`;`log`/`storage` 也记;`seq` 为本次窗口内序号)。`host.mock.calls(method?)` 按方法名过滤;`host.logs`:分级条目 `{ level, text }`;`host.reset()` 只清空两者(settings/handlers/注册表这些 fake 状态保留,要全新状态就重新 `createMockHost()`,零成本)。
|
|
107
|
+
- `overrides.settings` 是 `getSettings()` 返回的预设对象(同一引用);`host.mock.setSettings(next)` 合并预设,`host.mock.emitSettings(next)` 合并 + 触发 `onSettingsChanged`(模拟用户在设置面板保存)。其余 `overrides` 键整体替换对应方法(`fs`/`ui`/`llm` 等命名空间传对象 = 按子键合并覆盖),被覆盖的方法仍进 `calls` 记录。
|
|
108
|
+
- 无注入回退(与宿主无 provider 时同语义):`conversations.list/search` 回 `[]`、`get` 回 `null`;`prompt`/`steer`/`abortRun`/`chatWait`/`llm.complete`/`net.fetch`/`bash`/`scm` 回 `{ ok:false, … }`;`requestPermission` 回 `false`;`dialogs.select/input` 回 `{ ok:false }`、`confirm` 回 `false`;`notifyAction` resolve `null`;`schedule()` 只记调用不设真定时器(单测不泄漏句柄)。两处刻意差异:`chat()` 未注入时 resolve `{ ok:false }` 而不是 reject(真宿主 reject,由 `chatWait` 包成 `{ ok:false }`);`fs.read/readPath` 给空 `Uint8Array`(浏览器侧 import 本文件也安全)。
|
|
109
|
+
- 注册/订阅类返回注销函数,传进来的 handler 存进 `host.mock.handlers[method]`,用 `host.mock.emit(method, ...args)` 同步触发(异步 handler 用 `await host.mock.emitAsync(...)` 按序 await,返回各返回值)。工具/命令/路由/定时任务的定义体另存进 `host.mock.agentTools/commands/routes/schedules`(活数组,注销即摘除;工具的 `execute` 可直接调);`schedule()` 不设真定时器,用 `await host.mock.fireSchedules()` 手动触发。
|
|
110
|
+
- `ui` 是内存注册表(同 id 覆盖、单次上限 32 条):`list()` 真反映 register/update/remove/arrange。
|
|
111
|
+
- 脚手架可直接生成这个最小单测:`pi-web-ui plugin create my-plugin --with-test`(生成 `index.test.mjs`,现有模板默认不变)。
|
|
112
|
+
- `sdk/index.mjs` 是静态拷贝:`SDK_VERSION` 与 `plugin-sdk/package.json` 的 version 保持一致;已装插件用 `pi-web-ui plugin upgrade-sdk [id]` 一键刷新拷贝(版本号对不上才拷,无拷贝的插件跳过)。
|
|
113
|
+
- mock 只在 SDK 层:不断言、不碰 `server/plugins.ts` 运行时。类型见 `index.d.ts` 的 `MockHost` / `MockHostOverrides` / `createMockHost`。
|
|
114
|
+
|
|
115
|
+
## 本次 P0 新增速览
|
|
116
|
+
|
|
117
|
+
| 能力 | 服务端 | 视图 |
|
|
118
|
+
| ------------------------------------------------------------------------------------------------ | --------------------- | ------------------------------- |
|
|
119
|
+
| `host.fs.stat/mkdir/append/glob`(+ `*Path` 跨目录版) | ✅ | — |
|
|
120
|
+
| `kind: "select"` + `options` + `value`,`host.ui.update(id, { value })` 刷新 | ✅ | 顶栏/输入框/消息工具条已渲染 |
|
|
121
|
+
| `when` 肯定形条件(`file.isDir` / `file.isFile` / `session.isRunning` / `message.hasSelection`) | — | 右键菜单现场求值,置灰 |
|
|
122
|
+
| `settings` 新增 `secret` 类型(加密存,浏览器只见有无) | ✅ | 设置表单掩码 + 留空不改 |
|
|
123
|
+
| `host.llm.complete`(孤立无工具补全,不建对话) | ✅(要 `llm` 能力族) | — |
|
|
124
|
+
| `schedule` 持久版(`{id, persistent, catchUp, label}`,落盘+补跑+进后台面板) | ✅ | — |
|
|
125
|
+
| `host.requestPermission`(动态授权:net 补主机 / llm 限模型,用户逐条确认可记住) | ✅(基础族须已声明) | 确认框 + 设置面板「已授权能力」 |
|
|
126
|
+
|
|
127
|
+
完整契约见 `docs/architecture-plugins.md`。
|
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi-web-ui 插件 SDK 类型(starter,自包含,不引用 server/ 目录)。
|
|
3
|
+
* 与 server/plugins.ts 的 PluginHost 同语义的精简版:写插件时抄 autocomplete 用,
|
|
4
|
+
* 运行时以宿主实际注入为准(宿主 API 版本见 PLUGIN_API_VERSION)。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export type PluginPermissionFamily =
|
|
8
|
+
| "fs"
|
|
9
|
+
| "fs:read"
|
|
10
|
+
| "fs:write"
|
|
11
|
+
| "ui"
|
|
12
|
+
| "tools"
|
|
13
|
+
| "http"
|
|
14
|
+
| "chat"
|
|
15
|
+
| "net"
|
|
16
|
+
| "dom"
|
|
17
|
+
| "dom:anchor";
|
|
18
|
+
|
|
19
|
+
export interface WsEntry {
|
|
20
|
+
name: string;
|
|
21
|
+
type: "file" | "dir";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface WsStat extends WsEntry {
|
|
25
|
+
/** 字节数(目录为 0)。 */
|
|
26
|
+
size: number;
|
|
27
|
+
/** 修改时间毫秒时间戳。 */
|
|
28
|
+
mtime: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface UiSelectOption {
|
|
32
|
+
value: string;
|
|
33
|
+
label?: string;
|
|
34
|
+
labelEn?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type UiItemKind =
|
|
38
|
+
| "view"
|
|
39
|
+
| "action"
|
|
40
|
+
| "badge"
|
|
41
|
+
| "menu"
|
|
42
|
+
| "page"
|
|
43
|
+
| "organizer"
|
|
44
|
+
| "divider"
|
|
45
|
+
| "toggle"
|
|
46
|
+
| "input"
|
|
47
|
+
| "progress"
|
|
48
|
+
| "select";
|
|
49
|
+
|
|
50
|
+
export interface UiContribution {
|
|
51
|
+
id: string;
|
|
52
|
+
slot?: string;
|
|
53
|
+
label: string;
|
|
54
|
+
labelEn?: string;
|
|
55
|
+
icon?: string;
|
|
56
|
+
hint?: string;
|
|
57
|
+
hintEn?: string;
|
|
58
|
+
kind?: UiItemKind;
|
|
59
|
+
children?: UiContribution[];
|
|
60
|
+
order?: number;
|
|
61
|
+
align?: "start" | "center" | "end";
|
|
62
|
+
group?: string;
|
|
63
|
+
hidden?: boolean;
|
|
64
|
+
action?: string;
|
|
65
|
+
view?: string;
|
|
66
|
+
when?: string[];
|
|
67
|
+
badge?: string;
|
|
68
|
+
checked?: boolean;
|
|
69
|
+
value?: string;
|
|
70
|
+
progress?: number;
|
|
71
|
+
options?: UiSelectOption[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface PluginAgentTool {
|
|
75
|
+
name: string;
|
|
76
|
+
label?: string;
|
|
77
|
+
description: string;
|
|
78
|
+
promptSnippet?: string;
|
|
79
|
+
promptGuidelines?: string[];
|
|
80
|
+
parameters?: Record<string, unknown>;
|
|
81
|
+
execute(
|
|
82
|
+
toolCallId: string,
|
|
83
|
+
params: Record<string, unknown>,
|
|
84
|
+
signal?: AbortSignal,
|
|
85
|
+
onUpdate?: (partial: unknown) => void,
|
|
86
|
+
): Promise<unknown>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface PluginCommandDef {
|
|
90
|
+
name: string;
|
|
91
|
+
description?: string;
|
|
92
|
+
descriptionEn?: string;
|
|
93
|
+
argumentHint?: string;
|
|
94
|
+
argumentHintEn?: string;
|
|
95
|
+
run(args: string, ctx: { clientId?: string }): unknown | Promise<unknown>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** host.requestPermission:运行时申请能力范围(net 补主机,llm 限模型作用域)。
|
|
99
|
+
* 基础族必须已声明;用户逐条确认(可记住),无浏览器/拒绝一律 false。 */
|
|
100
|
+
export interface PluginPermissionRequest {
|
|
101
|
+
family: "net" | "llm";
|
|
102
|
+
hosts?: string[];
|
|
103
|
+
models?: string[];
|
|
104
|
+
reason?: string;
|
|
105
|
+
}
|
|
106
|
+
/** host.schedule:定时任务(毫秒间隔或 5 字段 cron)。persistent 落盘,重启后重调即重建。 */
|
|
107
|
+
export interface PluginHostScheduleOptions {
|
|
108
|
+
/** 持久任务的稳定 id(必填且合法,重启后靠它重建;每次 activate 都要重调)。 */
|
|
109
|
+
id?: string;
|
|
110
|
+
/** 重启不丢(声明 + lastRun 落盘;停止=删声明)。持久任务毫秒底线 60s。 */
|
|
111
|
+
persistent?: boolean;
|
|
112
|
+
/** 重启发现漏跑:"skip"(缺省)跳过,"once" 补跑一次(15s 缓冲)。 */
|
|
113
|
+
catchUp?: "skip" | "once";
|
|
114
|
+
/** 后台面板显示名(缺省 id)。 */
|
|
115
|
+
label?: string;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** host.fs:工作区相对方法 + 跨目录 *Path 方法(后者需用户授权)。 */
|
|
119
|
+
export interface PluginHostFs {
|
|
120
|
+
list(relDir?: string): Promise<WsEntry[]>;
|
|
121
|
+
read(relPath: string): Promise<Buffer>;
|
|
122
|
+
readText(relPath: string, maxBytes?: number): Promise<string>;
|
|
123
|
+
write(relPath: string, data: string | Uint8Array): Promise<void>;
|
|
124
|
+
remove(relPath: string): Promise<void>;
|
|
125
|
+
stat(relPath: string): Promise<WsStat>;
|
|
126
|
+
mkdir(relDir: string): Promise<void>;
|
|
127
|
+
append(relPath: string, data: string | Uint8Array): Promise<void>;
|
|
128
|
+
glob(pattern: string, relDir?: string): Promise<string[]>;
|
|
129
|
+
requestAccess(dir: string, reason?: string): Promise<boolean>;
|
|
130
|
+
authorizedDirs(): string[];
|
|
131
|
+
listPath(absDir: string): Promise<WsEntry[]>;
|
|
132
|
+
readPath(absPath: string): Promise<Buffer>;
|
|
133
|
+
readTextPath(absPath: string, maxBytes?: number): Promise<string>;
|
|
134
|
+
writePath(absPath: string, data: string | Uint8Array): Promise<void>;
|
|
135
|
+
removePath(absPath: string): Promise<void>;
|
|
136
|
+
statPath(absPath: string): Promise<WsStat>;
|
|
137
|
+
mkdirPath(absDir: string): Promise<void>;
|
|
138
|
+
appendPath(absPath: string, data: string | Uint8Array): Promise<void>;
|
|
139
|
+
globPath(absDir: string, pattern: string): Promise<string[]>;
|
|
140
|
+
watch(relPath: string, handler: (ev: { type: string; path: string }) => void): () => void;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** host.ui:运行时 UI 条目管理(manifest "ui" 是声明式基线)。 */
|
|
144
|
+
export interface PluginHostUi {
|
|
145
|
+
register(items: unknown[] | unknown): () => void;
|
|
146
|
+
update(id: string, patch: Record<string, unknown>): void;
|
|
147
|
+
remove(id: string): void;
|
|
148
|
+
arrange(ops: unknown[] | unknown): void;
|
|
149
|
+
list(): { items: UiContribution[]; arrange: unknown[] };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** host.llm.complete:孤立无工具的一次性补全(不建对话、不进历史)。
|
|
153
|
+
* 需要 manifest.permissions 含 "llm"(花用户自己的模型额度)。 */
|
|
154
|
+
export interface PluginHostLlm {
|
|
155
|
+
complete(req: {
|
|
156
|
+
prompt: string;
|
|
157
|
+
system?: string;
|
|
158
|
+
model?: string;
|
|
159
|
+
maxChars?: number;
|
|
160
|
+
timeoutMs?: number;
|
|
161
|
+
}): Promise<{
|
|
162
|
+
ok: boolean;
|
|
163
|
+
text?: string;
|
|
164
|
+
model?: string;
|
|
165
|
+
usage?: { input: number; output: number };
|
|
166
|
+
error?: string;
|
|
167
|
+
}>;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** 插件服务端入口拿到的宿主接口(精简:全量见 server/plugins.ts PluginHost)。 */
|
|
171
|
+
export interface PluginHost {
|
|
172
|
+
broadcast(payload: unknown): void;
|
|
173
|
+
notify(level: "info" | "warning" | "error", text: string, textEn?: string): void;
|
|
174
|
+
onMessage(handler: (payload: unknown, from?: string) => void): () => void;
|
|
175
|
+
sendTo(clientId: string, payload: unknown): void;
|
|
176
|
+
onAttach(handler: (clientId: string) => void): () => void;
|
|
177
|
+
onToolEvent(handler: (ev: unknown) => void): () => void;
|
|
178
|
+
onRunEvent(handler: (ev: unknown) => void): () => void;
|
|
179
|
+
getActiveConversation(): unknown;
|
|
180
|
+
onConversationChanged(handler: () => void): () => void;
|
|
181
|
+
registerAgentTool(tool: PluginAgentTool): () => void;
|
|
182
|
+
dir: string;
|
|
183
|
+
dataDir: string;
|
|
184
|
+
readonly cwd: string;
|
|
185
|
+
onCwdChange(handler: (cwd: string) => void): () => void;
|
|
186
|
+
registerCommand(cmd: PluginCommandDef): () => void;
|
|
187
|
+
/** 挂载 HTTP 路由(实际暴露为 /plugins-api/<id><path>;要 "http" 能力;返回注销函数)。 */
|
|
188
|
+
route(
|
|
189
|
+
method: "GET" | "POST" | "PUT" | "DELETE",
|
|
190
|
+
path: string,
|
|
191
|
+
handler: (req: unknown, res: unknown) => void,
|
|
192
|
+
): () => void;
|
|
193
|
+
ui: PluginHostUi;
|
|
194
|
+
llm: PluginHostLlm;
|
|
195
|
+
storage: {
|
|
196
|
+
get<T>(key: string, fallback?: T): T | undefined;
|
|
197
|
+
set(key: string, value: unknown): void;
|
|
198
|
+
delete(key: string): void;
|
|
199
|
+
all(): Record<string, unknown>;
|
|
200
|
+
};
|
|
201
|
+
secrets: {
|
|
202
|
+
set(name: string, value: string): void;
|
|
203
|
+
get(name: string): string | undefined;
|
|
204
|
+
has(name: string): boolean;
|
|
205
|
+
delete(name: string): void;
|
|
206
|
+
list(): string[];
|
|
207
|
+
};
|
|
208
|
+
ensureDeps(specs: string[], opts?: { onProgress?: (msg: string) => void }): Promise<boolean>;
|
|
209
|
+
fs: PluginHostFs;
|
|
210
|
+
schedule(spec: string | number, fn: () => void, opts?: PluginHostScheduleOptions): () => void;
|
|
211
|
+
requestPermission(req: PluginPermissionRequest): Promise<boolean>;
|
|
212
|
+
getSettings(): Record<string, unknown>;
|
|
213
|
+
onSettingsChanged(handler: (values: Record<string, unknown>) => void): () => void;
|
|
214
|
+
/** 分级运行时日志:host.log(level?, ...args)(level 缺省 "info")。
|
|
215
|
+
* 首参是 "debug"|"info"|"warn"|"error" 之一即当级别,老写法的
|
|
216
|
+
* host.log(...args) 照旧按 info 走。全部进宿主内存环形缓冲(每插件最近
|
|
217
|
+
* 200 条,单条截断 500 字符,不落盘);error 级同时走 console.error。
|
|
218
|
+
* 用户在设置面板“界面插件”页点某插件的“日志”按需查看(级别过滤 + 清空)。 */
|
|
219
|
+
log(level?: "debug" | "info" | "warn" | "error", ...args: unknown[]): void;
|
|
220
|
+
/** 无头调用:把外部通道文本投给 agent(要 "chat" 能力;宿主未接 chatProvider
|
|
221
|
+
* 时 reject(由 chatWait 包成 {ok:false},插件侧用 chatWait 更省心)。 */
|
|
222
|
+
chat(req: { text: string; accountId?: string }): Promise<{
|
|
223
|
+
ok: boolean;
|
|
224
|
+
conversationId?: string;
|
|
225
|
+
error?: string;
|
|
226
|
+
}>;
|
|
227
|
+
/** 等待无头调用的运行结束(默认 120s 超时;无注入/超时/失败一律回 {ok:false},绝不抛错)。 */
|
|
228
|
+
chatWait(
|
|
229
|
+
req: { text: string; accountId?: string },
|
|
230
|
+
opts?: { timeoutMs?: number },
|
|
231
|
+
): Promise<{ ok: boolean; conversationId?: string; error?: string }>;
|
|
232
|
+
/** 对话读写:无注入时 list/search 回 []、get 回 null,绝不抛错。 */
|
|
233
|
+
conversations: {
|
|
234
|
+
list(): Array<{ id: string; title: string }> | Promise<Array<{ id: string; title: string }>>;
|
|
235
|
+
get(id: string): unknown;
|
|
236
|
+
search(
|
|
237
|
+
query: string,
|
|
238
|
+
limit?: number,
|
|
239
|
+
): Array<{ id: string; title: string }> | Promise<Array<{ id: string; title: string }>>;
|
|
240
|
+
};
|
|
241
|
+
/** 向指定对话发一条用户消息(无注入回 {ok:false},绝不抛错)。 */
|
|
242
|
+
prompt(
|
|
243
|
+
conversationId: string,
|
|
244
|
+
req: { text: string; attachments?: Array<{ path: string; mode?: string }> },
|
|
245
|
+
): Promise<{ ok: boolean; error?: string }>;
|
|
246
|
+
/** 插队指定对话的当前运行(无注入回 {ok:false},绝不抛错)。 */
|
|
247
|
+
steer(conversationId: string, text: string): Promise<{ ok: boolean; error?: string }>;
|
|
248
|
+
/** 中止指定对话的运行(无注入回 {ok:false},绝不抛错)。 */
|
|
249
|
+
abortRun(conversationId: string): Promise<{ ok: boolean; error?: string }>;
|
|
250
|
+
/** 项目组装:在已授权目录里建目录/clone 仓库/写文件(失败回 {ok:false,error},不留半成品)。 */
|
|
251
|
+
project: {
|
|
252
|
+
create(spec: { dir: string; [key: string]: unknown }): Promise<{
|
|
253
|
+
ok: boolean;
|
|
254
|
+
dir: string;
|
|
255
|
+
log: string[];
|
|
256
|
+
error?: string;
|
|
257
|
+
}>;
|
|
258
|
+
};
|
|
259
|
+
/** 常驻后台任务(顶栏「后台任务」面板;返回 update/unregister)。 */
|
|
260
|
+
registerBackgroundTask(task: {
|
|
261
|
+
id: string;
|
|
262
|
+
label: string;
|
|
263
|
+
stop?: () => void;
|
|
264
|
+
status?: string;
|
|
265
|
+
}): {
|
|
266
|
+
update(next: Partial<{ label: string; status: string; stop: () => void }>): void;
|
|
267
|
+
unregister(): void;
|
|
268
|
+
};
|
|
269
|
+
/** 插件可见的模型列表(无注入回 [])。 */
|
|
270
|
+
models: {
|
|
271
|
+
list(): Array<{ id: string; [key: string]: unknown }> | Promise<Array<{ id: string; [key: string]: unknown }>>;
|
|
272
|
+
};
|
|
273
|
+
/** 会话统计 / 流式增量订阅(返回注销函数)。 */
|
|
274
|
+
onStats(handler: (s: unknown) => void): () => void;
|
|
275
|
+
onStreaming(handler: (ev: { conversationId?: string; delta: string }) => void): () => void;
|
|
276
|
+
/** 出站网络(要 "net" 能力 + 主机白名单;失败一律 {ok:false,error},绝不抛错)。 */
|
|
277
|
+
net: {
|
|
278
|
+
fetch(
|
|
279
|
+
url: string,
|
|
280
|
+
init?: { method?: string; body?: string; headers?: Record<string, string> },
|
|
281
|
+
): Promise<{ ok: boolean; status?: number; text?: string; error?: string }>;
|
|
282
|
+
};
|
|
283
|
+
/** 插件间事件总线(emit 回填 from;on 返回取消函数)。 */
|
|
284
|
+
events: {
|
|
285
|
+
emit(topic: string, payload?: unknown): void;
|
|
286
|
+
on(topic: string, handler: (ev: unknown) => void): () => void;
|
|
287
|
+
};
|
|
288
|
+
/** 只读 git 查询(失败回 {ok:false,error} 对象而非抛错)。 */
|
|
289
|
+
scm: {
|
|
290
|
+
status(): Promise<unknown>;
|
|
291
|
+
log(path?: string, limit?: number): Promise<unknown>;
|
|
292
|
+
};
|
|
293
|
+
/** 受限 shell(cwd 缺省当前工作区;默认超时 60s;要 "tools" 能力)。 */
|
|
294
|
+
bash(
|
|
295
|
+
cmd: string,
|
|
296
|
+
opts?: { cwd?: string; timeoutMs?: number },
|
|
297
|
+
): Promise<{
|
|
298
|
+
ok: boolean;
|
|
299
|
+
output: string;
|
|
300
|
+
exitCode?: number;
|
|
301
|
+
error?: string;
|
|
302
|
+
}>;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** 插件服务端入口形状(activate 必填,deactivate 可选)。 */
|
|
306
|
+
export interface PluginModule {
|
|
307
|
+
activate(host: PluginHost): void | Promise<void>;
|
|
308
|
+
deactivate?: () => void;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** 插件宿主动作桥(window.__piWebUiHost,client bundle 用)。版本号见 PLUGIN_HOST_API_VERSION。 */
|
|
312
|
+
export interface PluginHostBridge {
|
|
313
|
+
version: number;
|
|
314
|
+
setView(view: string): void;
|
|
315
|
+
/** 打开一个 `modal.dialog` 槽位的条目(全局 id `<pluginId>:<itemId>`;
|
|
316
|
+
* 不存在/被隐藏返回 false,同一时刻只开一个)。 */
|
|
317
|
+
openModal(id: string): boolean;
|
|
318
|
+
/** 关掉当前弹窗(幂等)。 */
|
|
319
|
+
closeModal(): boolean;
|
|
320
|
+
onUiAction(action: string, handler: (itemId: string, value?: string) => void): () => void;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** 插件视图入口形状(client/entry.mjs 默认导出)。 */
|
|
324
|
+
export interface PluginViewModule {
|
|
325
|
+
mount(el: HTMLElement, ctx: PluginViewContext): void | (() => void);
|
|
326
|
+
cleanup?: () => void;
|
|
327
|
+
renderers?: Record<string, (code: string, ctx: PluginViewContext) => HTMLElement | null>;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** 视图 ctx(窄通道:只依赖 send/onData;动作回调走 window.__piWebUiHost.onUiAction,见 sdk onUiAction)。 */
|
|
331
|
+
export interface PluginViewContext {
|
|
332
|
+
send(payload: unknown): void;
|
|
333
|
+
onData(handler: (payload: unknown) => void): () => void;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export declare function definePlugin(def: PluginModule): PluginModule;
|
|
337
|
+
export declare function defineView(view: PluginViewModule): PluginViewModule;
|
|
338
|
+
export declare function defineRenderer(
|
|
339
|
+
renderers: Record<string, (code: string, ctx: PluginViewContext) => HTMLElement | null>,
|
|
340
|
+
): Pick<PluginViewModule, "renderers">;
|
|
341
|
+
export declare function actionHandler(map: Record<string, (value?: string) => void>): (
|
|
342
|
+
itemId: string,
|
|
343
|
+
value?: string,
|
|
344
|
+
) => void;
|
|
345
|
+
export declare function onUiAction(action: string, handler: (itemId: string, value?: string) => void): () => void;
|
|
346
|
+
export declare function getSetting<T>(host: PluginHost, key: string, fallback: T): T;
|
|
347
|
+
export declare function selectOptions(list: Array<string | UiSelectOption>): UiSelectOption[];
|
|
348
|
+
|
|
349
|
+
/** SDK 版本(与 plugin-sdk/package.json 的 version 保持一致,单测锁定)。 */
|
|
350
|
+
export declare const SDK_VERSION: string;
|
|
351
|
+
|
|
352
|
+
/** createMockHost 的一条调用记录(method 如 "ui.register"、"fs.readText";log 也记)。 */
|
|
353
|
+
export interface MockHostCall {
|
|
354
|
+
method: string;
|
|
355
|
+
args: unknown[];
|
|
356
|
+
/** 本次记录窗口内的序号(0 起;reset() 后新记录重新从 0 开始)。 */
|
|
357
|
+
seq: number;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** createMockHost 的一条分级日志(log 被 overrides 整体替换时不再写这里)。 */
|
|
361
|
+
export interface MockHostLogEntry {
|
|
362
|
+
level: "debug" | "info" | "warn" | "error";
|
|
363
|
+
text: string;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** createMockHost(overrides?):settings/cwd/dir/dataDir 是专用键;命名空间键
|
|
367
|
+
* (fs/ui/llm/conversations/scm/net/events/models/storage/secrets/project/dialogs/
|
|
368
|
+
* shortcuts/searchProviders/composerProviders)传对象 = 按子键合并覆盖;其余键整体
|
|
369
|
+
* 替换对应方法(仍进 calls 记录)。`calls`/`logs`/`mock`/`reset` 不可覆盖。 */
|
|
370
|
+
export interface MockHostOverrides {
|
|
371
|
+
settings?: Record<string, unknown>;
|
|
372
|
+
cwd?: string;
|
|
373
|
+
dir?: string;
|
|
374
|
+
dataDir?: string;
|
|
375
|
+
[key: string]: unknown;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** mock 命名空间:handlers 活视图 + emit/emitAsync/setSettings/emitSettings 驱动器
|
|
379
|
+
* + calls 过滤 + 注册表活视图(agentTools/commands/routes/schedules)+ fireSchedules。 */
|
|
380
|
+
export interface MockHostControls {
|
|
381
|
+
/** 方法名 → 已注册 handler 数组(活引用,与注销函数联动)。 */
|
|
382
|
+
handlers: Record<string, Array<(...args: any[]) => unknown>>;
|
|
383
|
+
/** 主动触发某订阅方法的 handlers(错误直接抛给测试,不吞)。 */
|
|
384
|
+
emit(method: string, ...args: unknown[]): void;
|
|
385
|
+
/** 异步版:按注册顺序依次 await,返回各 handler 返回值(抛错即 reject,不吞)。 */
|
|
386
|
+
emitAsync(method: string, ...args: unknown[]): Promise<unknown[]>;
|
|
387
|
+
/** 按方法名过滤调用记录(不传即全部拷贝)。 */
|
|
388
|
+
calls(method?: string): MockHostCall[];
|
|
389
|
+
/** 合并 settings 预设(返回同一引用,getSettings() 读到的就是它)。 */
|
|
390
|
+
setSettings(next: Record<string, unknown>): Record<string, unknown>;
|
|
391
|
+
/** 合并预设 + 触发 onSettingsChanged(模拟用户在设置面板保存)。 */
|
|
392
|
+
emitSettings(next?: Record<string, unknown>): Record<string, unknown>;
|
|
393
|
+
/** 已注册的 Agent 工具定义(活数组,注销即摘除;工具的 execute 可直接调)。 */
|
|
394
|
+
agentTools: Array<PluginAgentTool>;
|
|
395
|
+
/** 已注册的斜杠命令定义(活数组,注销即摘除)。 */
|
|
396
|
+
commands: Array<PluginCommandDef>;
|
|
397
|
+
/** 已挂载的路由表(活数组,注销即摘除)。 */
|
|
398
|
+
routes: Array<{ method: string; path: string; handler: (req: unknown, res: unknown) => void }>;
|
|
399
|
+
/** 已登记的定时任务(活数组 {spec, fn, opts};schedule() 不设真定时器,注销即摘除)。 */
|
|
400
|
+
schedules: Array<{ spec: string | number; fn: () => void; opts?: PluginHostScheduleOptions }>;
|
|
401
|
+
/** 依次触发全部已登记的定时回调(返回各回调返回值;抛错即 reject)。 */
|
|
402
|
+
fireSchedules(): Promise<unknown[]>;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** 浏览器桥兼容(client 侧逻辑单测也能用同一个 mock):无注入回退语义。 */
|
|
406
|
+
export interface MockHostDialogs {
|
|
407
|
+
select(opts: { title?: string; options?: unknown; [key: string]: unknown }): Promise<{
|
|
408
|
+
ok: boolean;
|
|
409
|
+
selected?: string[];
|
|
410
|
+
error?: string;
|
|
411
|
+
}>;
|
|
412
|
+
confirm(opts: { title?: string; text?: string; [key: string]: unknown }): Promise<boolean>;
|
|
413
|
+
input(opts: { title?: string; [key: string]: unknown }): Promise<{ ok: boolean; value?: string }>;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** createMockHost 返回的 mock host(PluginHost 全量 + harness 字段 + 桥兼容)。 */
|
|
417
|
+
export interface MockHost extends PluginHost {
|
|
418
|
+
/** 全量调用记录({method, args},含 log/storage 等)。 */
|
|
419
|
+
calls: MockHostCall[];
|
|
420
|
+
/** 分级日志条目。 */
|
|
421
|
+
logs: MockHostLogEntry[];
|
|
422
|
+
/** 测试驱动器(handlers/emit/setSettings/emitSettings)。 */
|
|
423
|
+
mock: MockHostControls;
|
|
424
|
+
/** 清空 calls + logs(原地;settings/handlers/注册表这些 fake 状态保留,
|
|
425
|
+
* 要全新状态就重新 createMockHost())。 */
|
|
426
|
+
reset(): void;
|
|
427
|
+
dialogs: MockHostDialogs;
|
|
428
|
+
notifyAction(opts: { text: string; actions: Array<{ id: string; label: string }> }): Promise<string | null>;
|
|
429
|
+
shortcuts: {
|
|
430
|
+
register(shortcut: string, handler: () => void): () => void;
|
|
431
|
+
};
|
|
432
|
+
searchProviders: {
|
|
433
|
+
register(provider: {
|
|
434
|
+
id: string;
|
|
435
|
+
label: string;
|
|
436
|
+
search: (q: string) => Promise<Array<{ title: string; hint?: string; action: string }>>;
|
|
437
|
+
}): () => void;
|
|
438
|
+
list(): Array<{ id: string; label: string }>;
|
|
439
|
+
};
|
|
440
|
+
composerProviders: {
|
|
441
|
+
register(provider: {
|
|
442
|
+
id: string;
|
|
443
|
+
label: string;
|
|
444
|
+
search: (q: string) => Promise<Array<{ title: string; hint?: string; text?: string }>>;
|
|
445
|
+
}): () => void;
|
|
446
|
+
list(): Array<{ id: string; label: string }>;
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
export declare function createMockHost(overrides?: MockHostOverrides): MockHost;
|