pi-web-ui 0.87.2 → 0.89.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 +66 -1
- package/bin/pi-web-ui.mjs +463 -0
- package/dist/server/agent-service.js +419 -45
- package/dist/server/attachments.js +39 -0
- package/dist/server/client-state.js +22 -0
- package/dist/server/conversation-read-tool.js +325 -0
- package/dist/server/dsh/dsh-agent-service.js +32 -3
- package/dist/server/files-service.js +103 -0
- package/dist/server/host-metrics.js +59 -0
- package/dist/server/index.js +381 -8
- package/dist/server/model-admin.js +285 -36
- 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 +1005 -68
- package/dist/server/scheduler-tasks.js +405 -0
- package/dist/server/settings-service.js +8 -1
- package/dist/server/terminals.js +14 -1
- package/dist/server/tool-manager.js +6 -1
- package/dist/server/update-check.js +249 -19
- package/extensions/webui.ts +9 -2
- package/package.json +4 -2
- package/plugin-sdk/README.md +127 -0
- package/plugin-sdk/index.d.ts +454 -0
- package/plugin-sdk/index.mjs +545 -0
- package/plugin-sdk/package.json +9 -0
- package/plugins/catalog.json +11 -2
- package/web/dist/assets/TerminalPanel-BOUmN3uu.js +6 -0
- package/web/dist/assets/TerminalPanel-DOrYoP_4.css +32 -0
- package/web/dist/assets/index-3XU67qMh.js +376 -0
- package/web/dist/assets/index-B-gdvWtW.css +10 -0
- package/web/dist/index.html +2 -3
- package/web/dist/assets/index-CYuoen3I.js +0 -374
- package/web/dist/assets/index-F86qWlJy.css +0 -41
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
function calculateMemoryPercent(snapshot) {
|
|
3
|
+
const memoryUsed = Math.max(0, snapshot.memoryTotal - snapshot.memoryFree);
|
|
4
|
+
return snapshot.memoryTotal > 0 ? Math.max(0, Math.min(100, (memoryUsed / snapshot.memoryTotal) * 100)) : 0;
|
|
5
|
+
}
|
|
6
|
+
export function defaultReadSnapshot() {
|
|
7
|
+
const cpus = os.cpus();
|
|
8
|
+
let cpuIdle = 0;
|
|
9
|
+
let cpuTotal = 0;
|
|
10
|
+
for (const cpu of cpus) {
|
|
11
|
+
const times = cpu.times;
|
|
12
|
+
const total = (times.user ?? 0) + (times.nice ?? 0) + (times.sys ?? 0) + (times.idle ?? 0) + (times.irq ?? 0);
|
|
13
|
+
cpuIdle += times.idle ?? 0;
|
|
14
|
+
cpuTotal += total;
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
cpuIdle,
|
|
18
|
+
cpuTotal,
|
|
19
|
+
memoryFree: os.freemem(),
|
|
20
|
+
memoryTotal: os.totalmem(),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export function calculateHostMetrics(previous, current) {
|
|
24
|
+
const totalDelta = current.cpuTotal - previous.cpuTotal;
|
|
25
|
+
const idleDelta = current.cpuIdle - previous.cpuIdle;
|
|
26
|
+
let cpuPercent = null;
|
|
27
|
+
if (totalDelta > 0) {
|
|
28
|
+
const rawPercent = (1 - idleDelta / totalDelta) * 100;
|
|
29
|
+
cpuPercent = Math.max(0, Math.min(100, rawPercent));
|
|
30
|
+
}
|
|
31
|
+
const memoryPercent = calculateMemoryPercent(current);
|
|
32
|
+
return {
|
|
33
|
+
cpuPercent,
|
|
34
|
+
memoryPercent,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export function createHostMetricsSampler(readSnapshot = defaultReadSnapshot) {
|
|
38
|
+
let previous = null;
|
|
39
|
+
try {
|
|
40
|
+
previous = readSnapshot();
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
// 初始基线读取异常时不击穿服务启动,待后续心跳重试
|
|
44
|
+
previous = null;
|
|
45
|
+
}
|
|
46
|
+
return () => {
|
|
47
|
+
const current = readSnapshot();
|
|
48
|
+
if (!previous) {
|
|
49
|
+
previous = current;
|
|
50
|
+
return {
|
|
51
|
+
cpuPercent: null,
|
|
52
|
+
memoryPercent: calculateMemoryPercent(current),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const metrics = calculateHostMetrics(previous, current);
|
|
56
|
+
previous = current;
|
|
57
|
+
return metrics;
|
|
58
|
+
};
|
|
59
|
+
}
|
package/dist/server/index.js
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
*/
|
|
19
19
|
import { existsSync, readFileSync } from "node:fs";
|
|
20
20
|
import { stat } from "node:fs/promises";
|
|
21
|
-
import { createServer } from "node:http";
|
|
21
|
+
import { createServer, request as proxyRequest } from "node:http";
|
|
22
22
|
import { createConnection } from "node:net";
|
|
23
23
|
import { basename, delimiter, dirname, join, resolve, sep } from "node:path";
|
|
24
24
|
import { homedir } from "node:os";
|
|
@@ -45,6 +45,8 @@ import { PluginInstaller } from "./plugin-installer.js";
|
|
|
45
45
|
import { syncPluginCatalog } from "./plugin-catalog-sync.js";
|
|
46
46
|
import { McpBridge } from "./mcp-bridge.js";
|
|
47
47
|
import { createMcpHotReload } from "./mcp-hot-reload.js";
|
|
48
|
+
import { createHostMetricsSampler } from "./host-metrics.js";
|
|
49
|
+
import { SchedulerStore } from "./scheduler-tasks.js";
|
|
48
50
|
/** Strip npm-injected env (`npm start` exports `npm_config_*` / `npm_package_*` /
|
|
49
51
|
* `npm_lifecycle_*` into every child). Anything this server spawns — shells,
|
|
50
52
|
* `pi update` — would otherwise inherit `npm_config_allow_scripts`, which npm
|
|
@@ -502,6 +504,75 @@ app.all(["/plugins-api/:id/*", "/plugins-api/:id"], (req, res) => {
|
|
|
502
504
|
const rest = String(req.params[0] ?? "");
|
|
503
505
|
pluginMgr.handleHttp(String(req.params.id ?? ""), req.method, rest, req, res);
|
|
504
506
|
});
|
|
507
|
+
function proxyForwardPath(hit, url) {
|
|
508
|
+
let fwd = String(url ?? "/").slice(hit.prefix.length);
|
|
509
|
+
if (!fwd.startsWith("/"))
|
|
510
|
+
fwd = `/${fwd}`;
|
|
511
|
+
return fwd || "/";
|
|
512
|
+
}
|
|
513
|
+
function proxyHttp(hit, req, res) {
|
|
514
|
+
const fwdPath = proxyForwardPath(hit, req.url ?? "/");
|
|
515
|
+
const headers = {};
|
|
516
|
+
for (const [k, v] of Object.entries(req.headers)) {
|
|
517
|
+
if (v === undefined)
|
|
518
|
+
continue;
|
|
519
|
+
if (k.toLowerCase() === "host")
|
|
520
|
+
continue;
|
|
521
|
+
if (k.toLowerCase() === "content-length" && req.method !== "GET" && req.method !== "HEAD")
|
|
522
|
+
continue;
|
|
523
|
+
headers[k] = v;
|
|
524
|
+
}
|
|
525
|
+
// 内页拼 SSE/资源绝对地址用(子路径反代下 import.meta 推导不到前缀,靠这个头)。
|
|
526
|
+
headers["x-pi-proxy-prefix"] = hit.prefix;
|
|
527
|
+
let body;
|
|
528
|
+
if (req.method !== "GET" && req.method !== "HEAD" && req.body !== undefined) {
|
|
529
|
+
const b = req.body;
|
|
530
|
+
if (Buffer.isBuffer(b))
|
|
531
|
+
body = b;
|
|
532
|
+
else if (typeof b === "string")
|
|
533
|
+
body = Buffer.from(b);
|
|
534
|
+
else if (b !== undefined) {
|
|
535
|
+
body = Buffer.from(JSON.stringify(b));
|
|
536
|
+
if (!headers["content-type"])
|
|
537
|
+
headers["content-type"] = "application/json";
|
|
538
|
+
}
|
|
539
|
+
if (body)
|
|
540
|
+
headers["content-length"] = String(body.length);
|
|
541
|
+
}
|
|
542
|
+
const up = proxyRequest({ host: hit.host, port: hit.port, method: req.method, path: fwdPath, headers, timeout: 30000 }, (upRes) => {
|
|
543
|
+
const out = {};
|
|
544
|
+
for (const [k, v] of Object.entries(upRes.headers)) {
|
|
545
|
+
if (v === undefined)
|
|
546
|
+
continue;
|
|
547
|
+
const lk = k.toLowerCase();
|
|
548
|
+
if (lk === "connection" || lk === "keep-alive" || lk === "transfer-encoding" || lk === "upgrade")
|
|
549
|
+
continue;
|
|
550
|
+
out[k] = v;
|
|
551
|
+
}
|
|
552
|
+
res.writeHead(upRes.statusCode ?? 502, out);
|
|
553
|
+
upRes.pipe(res);
|
|
554
|
+
});
|
|
555
|
+
up.on("timeout", () => up.destroy(new Error("proxy timeout")));
|
|
556
|
+
up.on("error", (err) => {
|
|
557
|
+
console.error(`[proxy:${hit.prefix}] → 127.0.0.1:${hit.port}${fwdPath} failed:`, err);
|
|
558
|
+
if (!res.headersSent)
|
|
559
|
+
res.status(502).end("proxy target unreachable");
|
|
560
|
+
else
|
|
561
|
+
res.end();
|
|
562
|
+
});
|
|
563
|
+
if (body)
|
|
564
|
+
up.end(body);
|
|
565
|
+
else
|
|
566
|
+
req.pipe(up);
|
|
567
|
+
}
|
|
568
|
+
app.use((req, res, next) => {
|
|
569
|
+
const hit = pluginMgr.findProxy(req.path);
|
|
570
|
+
if (!hit) {
|
|
571
|
+
next();
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
proxyHttp(hit, req, res);
|
|
575
|
+
});
|
|
505
576
|
app.get("/plugins/:id/client/*", (req, res) => {
|
|
506
577
|
// express 4 的通配参数在运行时落在 params[0],但类型声明里没有 —— 显式取
|
|
507
578
|
const rest = String(req.params[0] ?? "");
|
|
@@ -658,6 +729,67 @@ httpServer.on("upgrade", (req, socket, head) => {
|
|
|
658
729
|
catch {
|
|
659
730
|
/* fall through to the path check below */
|
|
660
731
|
}
|
|
732
|
+
// 通用插件代理的 websocket 透传(live-reload 这类 socket 走这条;与 proxyHttp 同前缀表)。
|
|
733
|
+
const proxyHit = pluginMgr.findProxy(pathname);
|
|
734
|
+
if (proxyHit) {
|
|
735
|
+
if (!originAllowed(req)) {
|
|
736
|
+
socket.write("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
|
|
737
|
+
socket.destroy();
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
if (AUTH_TOKEN && !tokenOk(req)) {
|
|
741
|
+
socket.write("HTTP/1.1 401 Unauthorized\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
|
|
742
|
+
socket.destroy();
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
const target = createConnection(proxyHit.port, proxyHit.host);
|
|
746
|
+
const tearDown = () => {
|
|
747
|
+
try {
|
|
748
|
+
socket.destroy();
|
|
749
|
+
}
|
|
750
|
+
catch {
|
|
751
|
+
/* already gone */
|
|
752
|
+
}
|
|
753
|
+
try {
|
|
754
|
+
target.destroy();
|
|
755
|
+
}
|
|
756
|
+
catch {
|
|
757
|
+
/* already gone */
|
|
758
|
+
}
|
|
759
|
+
};
|
|
760
|
+
target.on("error", tearDown);
|
|
761
|
+
socket.on("error", tearDown);
|
|
762
|
+
target.setTimeout(10000, tearDown);
|
|
763
|
+
target.on("connect", () => {
|
|
764
|
+
target.setTimeout(0);
|
|
765
|
+
let fwdPath = String(req.url ?? "/").slice(proxyHit.prefix.length) || "/";
|
|
766
|
+
if (!fwdPath.startsWith("/"))
|
|
767
|
+
fwdPath = `/${fwdPath}`;
|
|
768
|
+
const lines = [`${req.method} ${fwdPath} HTTP/${req.httpVersion}`];
|
|
769
|
+
for (const [k, v] of Object.entries(req.headers)) {
|
|
770
|
+
if (k.toLowerCase() === "host") {
|
|
771
|
+
lines.push(`host: 127.0.0.1:${proxyHit.port}`);
|
|
772
|
+
continue;
|
|
773
|
+
}
|
|
774
|
+
if (Array.isArray(v))
|
|
775
|
+
for (const x of v)
|
|
776
|
+
lines.push(`${k}: ${x}`);
|
|
777
|
+
else if (v !== undefined)
|
|
778
|
+
lines.push(`${k}: ${v}`);
|
|
779
|
+
}
|
|
780
|
+
lines.push("", "");
|
|
781
|
+
try {
|
|
782
|
+
target.write(lines.join("\r\n"));
|
|
783
|
+
if (head?.length)
|
|
784
|
+
target.write(head);
|
|
785
|
+
socket.pipe(target).pipe(socket);
|
|
786
|
+
}
|
|
787
|
+
catch {
|
|
788
|
+
tearDown();
|
|
789
|
+
}
|
|
790
|
+
});
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
661
793
|
if (pathname !== "/ws") {
|
|
662
794
|
socket.destroy();
|
|
663
795
|
return;
|
|
@@ -679,15 +811,27 @@ httpServer.on("upgrade", (req, socket, head) => {
|
|
|
679
811
|
});
|
|
680
812
|
});
|
|
681
813
|
// Heartbeat: lets clients detect half-open connections (server killed without
|
|
682
|
-
// closing sockets, sleep/wake, network partitions).
|
|
683
|
-
//
|
|
814
|
+
// closing sockets, sleep/wake, network partitions). Also broadcasts lightweight
|
|
815
|
+
// server host metrics (CPU and memory usage) sampled every ~2s.
|
|
816
|
+
const HEARTBEAT_INTERVAL_MS = 2_000;
|
|
817
|
+
const sampleHostMetrics = createHostMetricsSampler();
|
|
684
818
|
const heartbeatTimer = setInterval(() => {
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
819
|
+
let message = { type: "heartbeat" };
|
|
820
|
+
try {
|
|
821
|
+
message = { type: "heartbeat", hostMetrics: sampleHostMetrics() };
|
|
822
|
+
}
|
|
823
|
+
catch {
|
|
824
|
+
// 继续发送普通心跳,下一轮重试采样。
|
|
825
|
+
}
|
|
826
|
+
if (wss.clients.size > 0) {
|
|
827
|
+
const payload = JSON.stringify(message);
|
|
828
|
+
for (const ws of wss.clients) {
|
|
829
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
830
|
+
ws.send(payload);
|
|
831
|
+
}
|
|
688
832
|
}
|
|
689
833
|
}
|
|
690
|
-
},
|
|
834
|
+
}, HEARTBEAT_INTERVAL_MS);
|
|
691
835
|
// 引擎分发:PI_WEB_ENGINE=dsh 时使用 DeepSeek Harness 引擎(server/dsh/),
|
|
692
836
|
// 默认 pi 引擎。同一 wire 协议,前端无感知(ready/health 携带 engine 字段)。
|
|
693
837
|
import { DshAgentService } from "./dsh/dsh-agent-service.js";
|
|
@@ -754,6 +898,104 @@ pluginMgr.pathAccessRequester = (pluginId, dir, reason) => new Promise((resolve)
|
|
|
754
898
|
});
|
|
755
899
|
// 用户点了「允许」→ 授权表变了 → 立刻重推给所有在线客户端(设置面板「已授权目录」即时可见)。
|
|
756
900
|
pluginMgr.onGrantsChanged = () => pushPluginGrants();
|
|
901
|
+
const pendingPermissionRequests = new Map();
|
|
902
|
+
/** 把能力授权表推给所有在线客户端(设置面板展示 + 撤销后刷新)。 */
|
|
903
|
+
function pushPluginPermissions() {
|
|
904
|
+
const payload = JSON.stringify({ type: "plugin_permissions", grants: pluginMgr.permGrants.list() });
|
|
905
|
+
for (const client of wss.clients) {
|
|
906
|
+
if (client.readyState === WebSocket.OPEN) {
|
|
907
|
+
try {
|
|
908
|
+
client.send(payload);
|
|
909
|
+
}
|
|
910
|
+
catch {
|
|
911
|
+
/* 死连接 */
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
pluginMgr.permissionRequester = (pluginId, req) => new Promise((resolve) => {
|
|
917
|
+
const id = randomUUID();
|
|
918
|
+
const resolved = JSON.stringify({ type: "plugin_permission_resolved", id });
|
|
919
|
+
const timer = setTimeout(() => {
|
|
920
|
+
pendingPermissionRequests.delete(id);
|
|
921
|
+
for (const client of wss.clients) {
|
|
922
|
+
if (client.readyState === WebSocket.OPEN) {
|
|
923
|
+
try {
|
|
924
|
+
client.send(resolved);
|
|
925
|
+
}
|
|
926
|
+
catch {
|
|
927
|
+
/* 死连接 */
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
resolve({ ok: false, remember: false });
|
|
932
|
+
}, 120_000);
|
|
933
|
+
pendingPermissionRequests.set(id, { resolve, timer });
|
|
934
|
+
const ask = JSON.stringify({
|
|
935
|
+
type: "plugin_permission_request",
|
|
936
|
+
id,
|
|
937
|
+
pluginId,
|
|
938
|
+
family: req.family,
|
|
939
|
+
...(req.hosts ? { hosts: req.hosts } : {}),
|
|
940
|
+
...(req.models ? { models: req.models } : {}),
|
|
941
|
+
...(req.reason ? { reason: req.reason } : {}),
|
|
942
|
+
});
|
|
943
|
+
for (const client of wss.clients) {
|
|
944
|
+
if (client.readyState === WebSocket.OPEN) {
|
|
945
|
+
try {
|
|
946
|
+
client.send(ask);
|
|
947
|
+
}
|
|
948
|
+
catch {
|
|
949
|
+
/* 死连接 */
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
});
|
|
954
|
+
pluginMgr.onPermGrantsChanged = () => pushPluginPermissions();
|
|
955
|
+
// 内置定时任务(issue #184):全局 <dataDir>/scheduler-tasks.json,TTL 与
|
|
956
|
+
// client-state 同级;执行走标准 pi 引擎的无头伪客户端(chatFromScheduler),
|
|
957
|
+
// DSH 引擎无该方法时 executor 回 not-supported(历史里记失败,不炸进程)。
|
|
958
|
+
const scheduler = new SchedulerStore(DATA_DIR, {
|
|
959
|
+
executor: async (task) => {
|
|
960
|
+
try {
|
|
961
|
+
const svc = service;
|
|
962
|
+
if (typeof svc.chatFromScheduler !== "function")
|
|
963
|
+
return { ok: false, error: "当前引擎不支持定时任务(仅标准 pi 引擎)" };
|
|
964
|
+
return await svc.chatFromScheduler({
|
|
965
|
+
id: task.id,
|
|
966
|
+
cwd: task.cwd,
|
|
967
|
+
prompt: task.prompt,
|
|
968
|
+
model: task.model,
|
|
969
|
+
thinkingLevel: task.thinkingLevel,
|
|
970
|
+
});
|
|
971
|
+
}
|
|
972
|
+
catch (err) {
|
|
973
|
+
return { ok: false, error: err.message };
|
|
974
|
+
}
|
|
975
|
+
},
|
|
976
|
+
onChange: () => pushSchedulerTasks(),
|
|
977
|
+
notify: (level, text, textEn) => pushNoticeToAll(level, text, textEn ?? text),
|
|
978
|
+
});
|
|
979
|
+
scheduler.start();
|
|
980
|
+
/** 把调度器任务列表推给所有在线客户端(设置面板展示 + 变更后刷新)。 */
|
|
981
|
+
function pushSchedulerTasks() {
|
|
982
|
+
try {
|
|
983
|
+
const payload = JSON.stringify({ type: "scheduler_tasks", tasks: scheduler.list() });
|
|
984
|
+
for (const client of wss.clients) {
|
|
985
|
+
if (client.readyState !== WebSocket.OPEN)
|
|
986
|
+
continue;
|
|
987
|
+
try {
|
|
988
|
+
client.send(payload);
|
|
989
|
+
}
|
|
990
|
+
catch {
|
|
991
|
+
/* 死连接:index.ts 自己会清理 */
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
catch {
|
|
996
|
+
/* 序列化失败不影响调度 */
|
|
997
|
+
}
|
|
998
|
+
}
|
|
757
999
|
/** 广播通知条给全部在线客户端(全局事件,不属于某个 ClientSession —— 如 mcp.json 坏)。 */
|
|
758
1000
|
function pushNoticeToAll(level, text, textEn) {
|
|
759
1001
|
const payload = JSON.stringify({ type: "notice", level, text, textEn });
|
|
@@ -799,7 +1041,10 @@ pluginMgr.chatProvider = (pluginId, req) => service.chatFromPlugin?.(pluginId, r
|
|
|
799
1041
|
// 插件扩展点:插件注册的 AI 工具(registerAgentTool)+ MCP 桥工具 → 会话创建时
|
|
800
1042
|
// 带上 + 变化时动态注入/移除已有会话。
|
|
801
1043
|
service.pluginToolsProvider = () => [...pluginMgr.getAgentTools(), ...mcpBridge.getTools()];
|
|
802
|
-
pluginMgr.onAgentToolsChanged = () =>
|
|
1044
|
+
pluginMgr.onAgentToolsChanged = () => {
|
|
1045
|
+
service.applyPluginAgentTools();
|
|
1046
|
+
void pluginMgr.pushToAll().catch(() => { });
|
|
1047
|
+
};
|
|
803
1048
|
// 插件扩展点:插件斜杠命令(registerCommand)→ 命令选择器目录 + prompt 拦截执行。
|
|
804
1049
|
pluginMgr.onCommandsChanged = () => service.applyPluginCommandCatalog();
|
|
805
1050
|
service.pluginCommandsProvider = () => pluginMgr.listCommands();
|
|
@@ -907,6 +1152,19 @@ service.pluginStopBgTask = (taskId) => pluginMgr.stopPluginBgTask(taskId);
|
|
|
907
1152
|
return { ok: false, error: err.message };
|
|
908
1153
|
}
|
|
909
1154
|
};
|
|
1155
|
+
// llmProvider:插件直调模型(孤立无工具的一次性补全,不建对话)。
|
|
1156
|
+
// 标准 pi 引擎走 service.completeForPlugins;DSH/未知引擎回 {ok:false},绝不抛错。
|
|
1157
|
+
pm.llmProvider = async (pluginId, req) => {
|
|
1158
|
+
try {
|
|
1159
|
+
const svc = service;
|
|
1160
|
+
if (typeof svc.completeForPlugins !== "function")
|
|
1161
|
+
return { ok: false, error: "当前引擎不支持 LLM 直调(仅标准 pi 引擎)" };
|
|
1162
|
+
return await svc.completeForPlugins(pluginId, (req ?? {}));
|
|
1163
|
+
}
|
|
1164
|
+
catch (err) {
|
|
1165
|
+
return { ok: false, error: err.message };
|
|
1166
|
+
}
|
|
1167
|
+
};
|
|
910
1168
|
}
|
|
911
1169
|
// 插件宿主工作区实时跟随当前项目:任意客户端 set_cwd 成功后同步给
|
|
912
1170
|
// PluginManager,编辑器等工作区跟随型插件随即切根(详见 plugins.ts notifyCwd)。
|
|
@@ -1172,6 +1430,12 @@ wss.on("connection", (ws) => {
|
|
|
1172
1430
|
case "file_copy":
|
|
1173
1431
|
void cs.copyEntry(msg.src, msg.destDir, msg.move);
|
|
1174
1432
|
break;
|
|
1433
|
+
case "file_reveal":
|
|
1434
|
+
void cs.revealEntry(msg.path);
|
|
1435
|
+
break;
|
|
1436
|
+
case "file_open_default":
|
|
1437
|
+
void cs.openDefaultEntry(msg.path);
|
|
1438
|
+
break;
|
|
1175
1439
|
case "list_models":
|
|
1176
1440
|
void cs.listModels();
|
|
1177
1441
|
break;
|
|
@@ -1287,6 +1551,12 @@ wss.on("connection", (ws) => {
|
|
|
1287
1551
|
case "refresh_provider_models":
|
|
1288
1552
|
void cs.refreshProviderModels(msg.providerId, msg.reqId);
|
|
1289
1553
|
break;
|
|
1554
|
+
case "refresh_builtin_models":
|
|
1555
|
+
void cs.refreshBuiltinModels(msg.reqId);
|
|
1556
|
+
break;
|
|
1557
|
+
case "append_builtin_model":
|
|
1558
|
+
void cs.appendBuiltinModel(msg.providerId, msg.model, msg.reqId);
|
|
1559
|
+
break;
|
|
1290
1560
|
case "clone_provider":
|
|
1291
1561
|
void cs.cloneProvider(msg.provider, msg.reqId);
|
|
1292
1562
|
break;
|
|
@@ -1371,6 +1641,7 @@ wss.on("connection", (ws) => {
|
|
|
1371
1641
|
disabledSkills: msg.disabledSkills,
|
|
1372
1642
|
disabledExtensions: msg.disabledExtensions,
|
|
1373
1643
|
disabledAgentTools: msg.disabledAgentTools,
|
|
1644
|
+
disabledPluginTools: msg.disabledPluginTools,
|
|
1374
1645
|
disabledPlugins: msg.disabledPlugins,
|
|
1375
1646
|
terminalToolsEnabled: msg.terminalToolsEnabled,
|
|
1376
1647
|
terminalBash: msg.terminalBash,
|
|
@@ -1492,6 +1763,29 @@ wss.on("connection", (ws) => {
|
|
|
1492
1763
|
}
|
|
1493
1764
|
break;
|
|
1494
1765
|
}
|
|
1766
|
+
// -- 插件能力授权(host.requestPermission)-------------------------------
|
|
1767
|
+
case "plugin_permission_response": {
|
|
1768
|
+
const id = String(msg.id ?? "");
|
|
1769
|
+
const pending = pendingPermissionRequests.get(id);
|
|
1770
|
+
if (pending) {
|
|
1771
|
+
clearTimeout(pending.timer);
|
|
1772
|
+
pendingPermissionRequests.delete(id);
|
|
1773
|
+
// 先答复者胜:通知其它在线端收起同一条请求(与目录授权不同,这里要显式 resolved)。
|
|
1774
|
+
const resolved = JSON.stringify({ type: "plugin_permission_resolved", id });
|
|
1775
|
+
for (const client of wss.clients) {
|
|
1776
|
+
if (client.readyState === WebSocket.OPEN) {
|
|
1777
|
+
try {
|
|
1778
|
+
client.send(resolved);
|
|
1779
|
+
}
|
|
1780
|
+
catch {
|
|
1781
|
+
/* 死连接 */
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
pending.resolve({ ok: msg.ok === true, remember: msg.remember === true });
|
|
1786
|
+
}
|
|
1787
|
+
break;
|
|
1788
|
+
}
|
|
1495
1789
|
case "plugin_dom_consent": {
|
|
1496
1790
|
void pluginMgr
|
|
1497
1791
|
.setDomConsent(msg.pluginId, msg.granted === true)
|
|
@@ -1514,6 +1808,18 @@ wss.on("connection", (ws) => {
|
|
|
1514
1808
|
pushPluginGrants();
|
|
1515
1809
|
break;
|
|
1516
1810
|
}
|
|
1811
|
+
case "plugin_permission_revoke": {
|
|
1812
|
+
const family = msg.family === "net" || msg.family === "llm" ? msg.family : undefined;
|
|
1813
|
+
const removed = pluginMgr.permGrants.revoke(typeof msg.pluginId === "string" ? msg.pluginId : undefined, family, typeof msg.host === "string" || typeof msg.model === "string"
|
|
1814
|
+
? {
|
|
1815
|
+
...(typeof msg.host === "string" ? { host: msg.host } : {}),
|
|
1816
|
+
...(typeof msg.model === "string" ? { model: msg.model } : {}),
|
|
1817
|
+
}
|
|
1818
|
+
: undefined);
|
|
1819
|
+
cs?.emitNotice("info", `已撤销 ${removed} 条能力授权`, `Revoked ${removed} permission grant(s)`);
|
|
1820
|
+
pushPluginPermissions();
|
|
1821
|
+
break;
|
|
1822
|
+
}
|
|
1517
1823
|
// -- 插件市场目录同步(issue #148)--------------------------------------
|
|
1518
1824
|
case "plugin_catalog_sync": {
|
|
1519
1825
|
const syncLang = () => cs?.getLang() ?? "en";
|
|
@@ -1580,6 +1886,63 @@ wss.on("connection", (ws) => {
|
|
|
1580
1886
|
case "delete_preset":
|
|
1581
1887
|
void cs.deletePreset(msg.name);
|
|
1582
1888
|
break;
|
|
1889
|
+
case "schedule_list":
|
|
1890
|
+
try {
|
|
1891
|
+
send({ type: "scheduler_tasks", tasks: scheduler.list() });
|
|
1892
|
+
}
|
|
1893
|
+
catch (err) {
|
|
1894
|
+
send({
|
|
1895
|
+
type: "notice",
|
|
1896
|
+
level: "error",
|
|
1897
|
+
text: `读取定时任务失败:${err.message}`,
|
|
1898
|
+
textEn: `Failed to list scheduled tasks: ${err.message}`,
|
|
1899
|
+
});
|
|
1900
|
+
}
|
|
1901
|
+
break;
|
|
1902
|
+
case "schedule_save":
|
|
1903
|
+
try {
|
|
1904
|
+
scheduler.upsert(msg.task);
|
|
1905
|
+
}
|
|
1906
|
+
catch (err) {
|
|
1907
|
+
send({
|
|
1908
|
+
type: "notice",
|
|
1909
|
+
level: "error",
|
|
1910
|
+
text: `保存定时任务失败:${err.message}`,
|
|
1911
|
+
textEn: `Failed to save scheduled task: ${err.message}`,
|
|
1912
|
+
});
|
|
1913
|
+
}
|
|
1914
|
+
break;
|
|
1915
|
+
case "schedule_delete":
|
|
1916
|
+
if (!scheduler.remove(msg.id)) {
|
|
1917
|
+
send({
|
|
1918
|
+
type: "notice",
|
|
1919
|
+
level: "warning",
|
|
1920
|
+
text: `定时任务不存在:${msg.id}`,
|
|
1921
|
+
textEn: `No such scheduled task: ${msg.id}`,
|
|
1922
|
+
});
|
|
1923
|
+
}
|
|
1924
|
+
break;
|
|
1925
|
+
case "schedule_run":
|
|
1926
|
+
void scheduler.runNow(msg.id).then((r) => {
|
|
1927
|
+
if (!r.ok)
|
|
1928
|
+
send({
|
|
1929
|
+
type: "notice",
|
|
1930
|
+
level: "warning",
|
|
1931
|
+
text: `定时任务手动触发失败:${r.error ?? "未知错误"}`,
|
|
1932
|
+
textEn: `Manual scheduled-task run failed: ${r.error ?? "unknown error"}`,
|
|
1933
|
+
});
|
|
1934
|
+
});
|
|
1935
|
+
break;
|
|
1936
|
+
case "schedule_toggle":
|
|
1937
|
+
if (!scheduler.setEnabled(msg.id, msg.enabled === true)) {
|
|
1938
|
+
send({
|
|
1939
|
+
type: "notice",
|
|
1940
|
+
level: "warning",
|
|
1941
|
+
text: `定时任务不存在:${msg.id}`,
|
|
1942
|
+
textEn: `No such scheduled task: ${msg.id}`,
|
|
1943
|
+
});
|
|
1944
|
+
}
|
|
1945
|
+
break;
|
|
1583
1946
|
default:
|
|
1584
1947
|
break;
|
|
1585
1948
|
}
|
|
@@ -1634,6 +1997,8 @@ wss.on("connection", (ws) => {
|
|
|
1634
1997
|
pluginMgr.notifyAttach(cid);
|
|
1635
1998
|
// 插件目录授权表(设置面板展示 + 可撤销)
|
|
1636
1999
|
send({ type: "plugin_grants", grants: pluginMgr.grants.list() });
|
|
2000
|
+
// 插件能力授权表(设置面板展示 + 可撤销;session 授权带标记)
|
|
2001
|
+
send({ type: "plugin_permissions", grants: pluginMgr.permGrants.list() });
|
|
1637
2002
|
// 插件命令可能在本客户端 attach 过程中才注册(首载竞态)——
|
|
1638
2003
|
// 重推一次目录,保证选择器完整。
|
|
1639
2004
|
service.applyPluginCommandCatalog();
|
|
@@ -1641,6 +2006,13 @@ wss.on("connection", (ws) => {
|
|
|
1641
2006
|
// 注册表(plugin-fence.ts),`` ```lang `` 围栏才能立即命中插件;
|
|
1642
2007
|
// 否则消息先落成普通代码块,清单后到也不会重渲。
|
|
1643
2008
|
cs.flushSnapshot();
|
|
2009
|
+
// 内置定时任务列表随附推一次(后续变更经 pushSchedulerTasks 广播)。
|
|
2010
|
+
try {
|
|
2011
|
+
send({ type: "scheduler_tasks", tasks: scheduler.list() });
|
|
2012
|
+
}
|
|
2013
|
+
catch {
|
|
2014
|
+
/* 推送失败不挡快照 */
|
|
2015
|
+
}
|
|
1644
2016
|
})
|
|
1645
2017
|
.catch(() => {
|
|
1646
2018
|
if (closed)
|
|
@@ -1786,6 +2158,7 @@ async function shutdown(signal = "SIGINT") {
|
|
|
1786
2158
|
try {
|
|
1787
2159
|
clearInterval(heartbeatTimer);
|
|
1788
2160
|
stopControl();
|
|
2161
|
+
scheduler.stop();
|
|
1789
2162
|
pluginMgr.dispose();
|
|
1790
2163
|
pluginInstaller.dispose();
|
|
1791
2164
|
mcpHotReload.dispose();
|