pi-web-ui 0.88.0 → 0.90.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 +80 -1
- package/dist/server/agent-service.js +464 -68
- package/dist/server/attachments.js +39 -0
- package/dist/server/auth-cookie.js +23 -0
- package/dist/server/client-state.js +35 -0
- package/dist/server/composer-drafts.js +20 -2
- package/dist/server/conversation-read-tool.js +325 -0
- package/dist/server/dsh/dsh-agent-service.js +46 -11
- package/dist/server/file-archives.js +258 -0
- package/dist/server/file-transfer-routes.js +113 -0
- package/dist/server/files-service.js +123 -1
- package/dist/server/index.js +361 -14
- package/dist/server/marker-service.js +3 -1
- package/dist/server/model-admin.js +285 -36
- package/dist/server/plugins.js +152 -5
- package/dist/server/queue-utils.js +16 -0
- package/dist/server/schedule-agent-tool.js +240 -0
- package/dist/server/scheduler-tasks.js +414 -0
- package/dist/server/settings-service.js +76 -2
- package/dist/server/terminals.js +14 -1
- package/dist/server/tool-manager.js +16 -1
- package/dist/server/update-check.js +253 -19
- package/dist/server/webui-context.js +4 -0
- package/dist/server/ws-unknown-types.js +44 -0
- package/extensions/webui.ts +9 -2
- package/package.json +7 -3
- package/plugin-sdk/index.d.ts +4 -0
- package/plugin-sdk/index.mjs +9 -0
- package/plugins/catalog.json +11 -2
- package/web/dist/assets/TerminalPanel-BDbdLhpT.js +6 -0
- package/web/dist/assets/index-1f_DP8S3.js +380 -0
- package/web/dist/assets/index-DeUwDA-C.css +10 -0
- package/web/dist/index.html +2 -2
- package/web/dist/assets/TerminalPanel-CDH27dNP.js +0 -6
- package/web/dist/assets/index-CQg5A74_.js +0 -375
- package/web/dist/assets/index-DTEcxHWs.css +0 -10
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";
|
|
@@ -30,7 +30,8 @@ import { WebSocket, WebSocketServer } from "ws";
|
|
|
30
30
|
import { VERSION, getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
31
31
|
import { PROTOCOL_VERSION } from "./protocol-version.js";
|
|
32
32
|
import { AgentService, workspacePath, QuiesceRejectedError } from "./agent-service.js";
|
|
33
|
-
import { isAbsoluteWirePath, wireToAbs } from "./files-service.js";
|
|
33
|
+
import { WS_MAX_PAYLOAD_BYTES, isAbsoluteWirePath, wireToAbs } from "./files-service.js";
|
|
34
|
+
import { registerFileTransferRoutes } from "./file-transfer-routes.js";
|
|
34
35
|
import { previewKind } from "./text-sniff.js";
|
|
35
36
|
import { startControlServer } from "./control-socket.js";
|
|
36
37
|
import { scheduleUploadCleanup } from "./uploads.js";
|
|
@@ -39,6 +40,7 @@ import { listThemes, resolveThemeFile } from "./themes.js";
|
|
|
39
40
|
import { isManaged, managedRefusal } from "./managed.js";
|
|
40
41
|
import { launchOrigin, toServiceInfo } from "./launch-origin.js";
|
|
41
42
|
import { parseTabs, tabsRefusal } from "./tabs.js";
|
|
43
|
+
import { recordUnknownWsType } from "./ws-unknown-types.js";
|
|
42
44
|
import { installPack, isKnownPack, listPacks, loadServerStrings, readPackFile, removePack, unloadServerStrings, } from "./locales.js";
|
|
43
45
|
import { PluginManager, resolvePluginClientFile, } from "./plugins.js";
|
|
44
46
|
import { PluginInstaller } from "./plugin-installer.js";
|
|
@@ -46,6 +48,8 @@ import { syncPluginCatalog } from "./plugin-catalog-sync.js";
|
|
|
46
48
|
import { McpBridge } from "./mcp-bridge.js";
|
|
47
49
|
import { createMcpHotReload } from "./mcp-hot-reload.js";
|
|
48
50
|
import { createHostMetricsSampler } from "./host-metrics.js";
|
|
51
|
+
import { SchedulerStore } from "./scheduler-tasks.js";
|
|
52
|
+
import { buildPiWebTokenCookie, isTlsRequest } from "./auth-cookie.js";
|
|
49
53
|
/** Strip npm-injected env (`npm start` exports `npm_config_*` / `npm_package_*` /
|
|
50
54
|
* `npm_lifecycle_*` into every child). Anything this server spawns — shells,
|
|
51
55
|
* `pi update` — would otherwise inherit `npm_config_allow_scripts`, which npm
|
|
@@ -218,15 +222,19 @@ if (AUTH_TOKEN) {
|
|
|
218
222
|
// 重要:只要请求携带着有效 token(query/header/cookie 任一匹配)就把 cookie 刷新为
|
|
219
223
|
// 当前 AUTH_TOKEN——服务端重启改了 PI_WEB_TOKEN 后,旧 cookie 经一次正确的
|
|
220
224
|
// ?token= 进入即被重新同步,无需用户清缓存(issue #71)。
|
|
225
|
+
// Secure 只在 TLS 连接上加:常加会让明文 HTTP(默认 loopback)收不到 cookie。
|
|
226
|
+
const secure = isTlsRequest(req);
|
|
221
227
|
if (ok) {
|
|
222
228
|
if (cookie !== encodeURIComponent(AUTH_TOKEN)) {
|
|
223
|
-
res.setHeader("Set-Cookie",
|
|
229
|
+
res.setHeader("Set-Cookie", buildPiWebTokenCookie(encodeURIComponent(AUTH_TOKEN), 31536000, secure));
|
|
224
230
|
}
|
|
225
231
|
}
|
|
226
232
|
else if (cookie) {
|
|
227
233
|
// 请求带的 cookie 已是失效旧值(服务端口令已更换)——立即让其过期,
|
|
228
234
|
// 避免浏览器被残留 cookie 卡死一年(本来也不该再信任它鉴权)。
|
|
229
|
-
|
|
235
|
+
// Secure 与非 Secure 在浏览器里是两个独立 cookie:只清一种会因种植时的
|
|
236
|
+
// 协议不同而残留,所以两种属性组合各发一遍(HTTP 下 Secure 那条被忽略,无害)。
|
|
237
|
+
res.setHeader("Set-Cookie", [buildPiWebTokenCookie("", 0, false), buildPiWebTokenCookie("", 0, true)]);
|
|
230
238
|
}
|
|
231
239
|
if (req.path === "/api/health" || ok) {
|
|
232
240
|
next();
|
|
@@ -252,6 +260,7 @@ const ORIGIN = launchOrigin();
|
|
|
252
260
|
const SERVICE_INFO = toServiceInfo(ORIGIN);
|
|
253
261
|
/** PI_WEB_TABS: the tabs this instance offers. null = all of them, as before. */
|
|
254
262
|
const TABS = parseTabs();
|
|
263
|
+
registerFileTransferRoutes(app, (clientId) => service.get(clientId)?.cwd);
|
|
255
264
|
app.get("/api/health", (_req, res) => {
|
|
256
265
|
res.json({ ok: true, piVersion: VERSION, cwd: CWD, pid: process.pid, engine: ENGINE });
|
|
257
266
|
});
|
|
@@ -351,6 +360,7 @@ app.get("/api/file", async (req, res) => {
|
|
|
351
360
|
*/
|
|
352
361
|
app.get("/api/preview/*", async (req, res) => {
|
|
353
362
|
try {
|
|
363
|
+
// SAFETY: Express wildcard captures are indexed string route parameters.
|
|
354
364
|
const captured = String(req.params[0] ?? "");
|
|
355
365
|
const ABS_MARKER = "__abs__/";
|
|
356
366
|
const cid = typeof req.query.clientId === "string" ? req.query.clientId : "";
|
|
@@ -500,11 +510,81 @@ const PLUGINS_DIR = join(DATA_DIR, "plugins");
|
|
|
500
510
|
// /plugins-api/<id>/inbox。PI_WEB_TOKEN 鉴权(上方 app.use)自动覆盖;
|
|
501
511
|
// 响应已在前面过了 express.json。注意不要在此 catch-all 里消费 body。
|
|
502
512
|
app.all(["/plugins-api/:id/*", "/plugins-api/:id"], (req, res) => {
|
|
513
|
+
// SAFETY: Express wildcard captures are strings indexed by 0.
|
|
503
514
|
const rest = String(req.params[0] ?? "");
|
|
504
515
|
pluginMgr.handleHttp(String(req.params.id ?? ""), req.method, rest, req, res);
|
|
505
516
|
});
|
|
517
|
+
function proxyForwardPath(hit, url) {
|
|
518
|
+
let fwd = String(url ?? "/").slice(hit.prefix.length);
|
|
519
|
+
if (!fwd.startsWith("/"))
|
|
520
|
+
fwd = `/${fwd}`;
|
|
521
|
+
return fwd || "/";
|
|
522
|
+
}
|
|
523
|
+
function proxyHttp(hit, req, res) {
|
|
524
|
+
const fwdPath = proxyForwardPath(hit, req.url ?? "/");
|
|
525
|
+
const headers = {};
|
|
526
|
+
for (const [k, v] of Object.entries(req.headers)) {
|
|
527
|
+
if (v === undefined)
|
|
528
|
+
continue;
|
|
529
|
+
if (k.toLowerCase() === "host")
|
|
530
|
+
continue;
|
|
531
|
+
if (k.toLowerCase() === "content-length" && req.method !== "GET" && req.method !== "HEAD")
|
|
532
|
+
continue;
|
|
533
|
+
headers[k] = v;
|
|
534
|
+
}
|
|
535
|
+
// 内页拼 SSE/资源绝对地址用(子路径反代下 import.meta 推导不到前缀,靠这个头)。
|
|
536
|
+
headers["x-pi-proxy-prefix"] = hit.prefix;
|
|
537
|
+
let body;
|
|
538
|
+
if (req.method !== "GET" && req.method !== "HEAD" && req.body !== undefined) {
|
|
539
|
+
const b = req.body;
|
|
540
|
+
if (Buffer.isBuffer(b))
|
|
541
|
+
body = b;
|
|
542
|
+
else if (typeof b === "string")
|
|
543
|
+
body = Buffer.from(b);
|
|
544
|
+
else if (b !== undefined) {
|
|
545
|
+
body = Buffer.from(JSON.stringify(b));
|
|
546
|
+
if (!headers["content-type"])
|
|
547
|
+
headers["content-type"] = "application/json";
|
|
548
|
+
}
|
|
549
|
+
if (body)
|
|
550
|
+
headers["content-length"] = String(body.length);
|
|
551
|
+
}
|
|
552
|
+
const up = proxyRequest({ host: hit.host, port: hit.port, method: req.method, path: fwdPath, headers, timeout: 30000 }, (upRes) => {
|
|
553
|
+
const out = {};
|
|
554
|
+
for (const [k, v] of Object.entries(upRes.headers)) {
|
|
555
|
+
if (v === undefined)
|
|
556
|
+
continue;
|
|
557
|
+
const lk = k.toLowerCase();
|
|
558
|
+
if (lk === "connection" || lk === "keep-alive" || lk === "transfer-encoding" || lk === "upgrade")
|
|
559
|
+
continue;
|
|
560
|
+
out[k] = v;
|
|
561
|
+
}
|
|
562
|
+
res.writeHead(upRes.statusCode ?? 502, out);
|
|
563
|
+
upRes.pipe(res);
|
|
564
|
+
});
|
|
565
|
+
up.on("timeout", () => up.destroy(new Error("proxy timeout")));
|
|
566
|
+
up.on("error", (err) => {
|
|
567
|
+
console.error(`[proxy:${hit.prefix}] → 127.0.0.1:${hit.port}${fwdPath} failed:`, err);
|
|
568
|
+
if (!res.headersSent)
|
|
569
|
+
res.status(502).end("proxy target unreachable");
|
|
570
|
+
else
|
|
571
|
+
res.end();
|
|
572
|
+
});
|
|
573
|
+
if (body)
|
|
574
|
+
up.end(body);
|
|
575
|
+
else
|
|
576
|
+
req.pipe(up);
|
|
577
|
+
}
|
|
578
|
+
app.use((req, res, next) => {
|
|
579
|
+
const hit = pluginMgr.findProxy(req.path);
|
|
580
|
+
if (!hit) {
|
|
581
|
+
next();
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
proxyHttp(hit, req, res);
|
|
585
|
+
});
|
|
506
586
|
app.get("/plugins/:id/client/*", (req, res) => {
|
|
507
|
-
// express 4 的通配参数在运行时落在 params[0]
|
|
587
|
+
// SAFETY: express 4 的通配参数在运行时落在 params[0],但类型声明里没有 -- 显式取
|
|
508
588
|
const rest = String(req.params[0] ?? "");
|
|
509
589
|
// 特权 DOM 门禁:声明了 dom 能力的插件,其 bundle 需用户逐个授权后才下发
|
|
510
590
|
// (同源 bundle 技术上拦不住 DOM 访问,门只能放在这里;见 server/plugin-dom.ts)。
|
|
@@ -597,9 +677,10 @@ else if (process.env[RESTART_CHILD_ENV]) {
|
|
|
597
677
|
const httpServer = createServer(app);
|
|
598
678
|
const wss = new WebSocketServer({
|
|
599
679
|
noServer: true,
|
|
600
|
-
//
|
|
601
|
-
//
|
|
602
|
-
|
|
680
|
+
// 右键上传走单帧 base64(100MB 文件 → ~133MB 文本):上限与
|
|
681
|
+
// files-service 的上传 cap 对齐(base64 上限 + 1MB 包络余量),
|
|
682
|
+
// 超限帧由 ws 层直接拒收,不进 handler 再分配 Buffer。
|
|
683
|
+
maxPayload: WS_MAX_PAYLOAD_BYTES,
|
|
603
684
|
// Per-message deflate: big-session snapshots serialize to multi-MB JSON
|
|
604
685
|
// strings; wire-level compression cuts that several-fold. threshold keeps
|
|
605
686
|
// tiny messages (notices/heartbeats) uncompressed to save CPU.
|
|
@@ -659,6 +740,67 @@ httpServer.on("upgrade", (req, socket, head) => {
|
|
|
659
740
|
catch {
|
|
660
741
|
/* fall through to the path check below */
|
|
661
742
|
}
|
|
743
|
+
// 通用插件代理的 websocket 透传(live-reload 这类 socket 走这条;与 proxyHttp 同前缀表)。
|
|
744
|
+
const proxyHit = pluginMgr.findProxy(pathname);
|
|
745
|
+
if (proxyHit) {
|
|
746
|
+
if (!originAllowed(req)) {
|
|
747
|
+
socket.write("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
|
|
748
|
+
socket.destroy();
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
if (AUTH_TOKEN && !tokenOk(req)) {
|
|
752
|
+
socket.write("HTTP/1.1 401 Unauthorized\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
|
|
753
|
+
socket.destroy();
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
const target = createConnection(proxyHit.port, proxyHit.host);
|
|
757
|
+
const tearDown = () => {
|
|
758
|
+
try {
|
|
759
|
+
socket.destroy();
|
|
760
|
+
}
|
|
761
|
+
catch {
|
|
762
|
+
/* already gone */
|
|
763
|
+
}
|
|
764
|
+
try {
|
|
765
|
+
target.destroy();
|
|
766
|
+
}
|
|
767
|
+
catch {
|
|
768
|
+
/* already gone */
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
target.on("error", tearDown);
|
|
772
|
+
socket.on("error", tearDown);
|
|
773
|
+
target.setTimeout(10000, tearDown);
|
|
774
|
+
target.on("connect", () => {
|
|
775
|
+
target.setTimeout(0);
|
|
776
|
+
let fwdPath = String(req.url ?? "/").slice(proxyHit.prefix.length) || "/";
|
|
777
|
+
if (!fwdPath.startsWith("/"))
|
|
778
|
+
fwdPath = `/${fwdPath}`;
|
|
779
|
+
const lines = [`${req.method} ${fwdPath} HTTP/${req.httpVersion}`];
|
|
780
|
+
for (const [k, v] of Object.entries(req.headers)) {
|
|
781
|
+
if (k.toLowerCase() === "host") {
|
|
782
|
+
lines.push(`host: 127.0.0.1:${proxyHit.port}`);
|
|
783
|
+
continue;
|
|
784
|
+
}
|
|
785
|
+
if (Array.isArray(v))
|
|
786
|
+
for (const x of v)
|
|
787
|
+
lines.push(`${k}: ${x}`);
|
|
788
|
+
else if (v !== undefined)
|
|
789
|
+
lines.push(`${k}: ${v}`);
|
|
790
|
+
}
|
|
791
|
+
lines.push("", "");
|
|
792
|
+
try {
|
|
793
|
+
target.write(lines.join("\r\n"));
|
|
794
|
+
if (head?.length)
|
|
795
|
+
target.write(head);
|
|
796
|
+
socket.pipe(target).pipe(socket);
|
|
797
|
+
}
|
|
798
|
+
catch {
|
|
799
|
+
tearDown();
|
|
800
|
+
}
|
|
801
|
+
});
|
|
802
|
+
return;
|
|
803
|
+
}
|
|
662
804
|
if (pathname !== "/ws") {
|
|
663
805
|
socket.destroy();
|
|
664
806
|
return;
|
|
@@ -701,6 +843,8 @@ const heartbeatTimer = setInterval(() => {
|
|
|
701
843
|
}
|
|
702
844
|
}
|
|
703
845
|
}, HEARTBEAT_INTERVAL_MS);
|
|
846
|
+
// Unref'd so it never blocks exit: heartbeat alone must not hold the event loop open.
|
|
847
|
+
heartbeatTimer.unref?.();
|
|
704
848
|
// 引擎分发:PI_WEB_ENGINE=dsh 时使用 DeepSeek Harness 引擎(server/dsh/),
|
|
705
849
|
// 默认 pi 引擎。同一 wire 协议,前端无感知(ready/health 携带 engine 字段)。
|
|
706
850
|
import { DshAgentService } from "./dsh/dsh-agent-service.js";
|
|
@@ -821,6 +965,85 @@ pluginMgr.permissionRequester = (pluginId, req) => new Promise((resolve) => {
|
|
|
821
965
|
}
|
|
822
966
|
});
|
|
823
967
|
pluginMgr.onPermGrantsChanged = () => pushPluginPermissions();
|
|
968
|
+
// 内置定时任务(issue #184):全局 <dataDir>/scheduler-tasks.json,TTL 与
|
|
969
|
+
// client-state 同级;Agent 工具建的任务优先唤醒发起对话(issue #193:
|
|
970
|
+
// wakeConversation steer 投递,不切用户当前对话),对话不在了再回落无头伪
|
|
971
|
+
// 客户端(chatFromScheduler);单次任务触发后自动删除。DSH 引擎无这俩方法时
|
|
972
|
+
// executor 回 not-supported(历史里记失败,不炸进程)。
|
|
973
|
+
const scheduler = new SchedulerStore(DATA_DIR, {
|
|
974
|
+
executor: async (task) => {
|
|
975
|
+
let result;
|
|
976
|
+
try {
|
|
977
|
+
const svc = service;
|
|
978
|
+
if (typeof svc.chatFromScheduler !== "function" && typeof svc.wakeConversation !== "function") {
|
|
979
|
+
result = { ok: false, error: "当前引擎不支持定时任务(仅标准 pi 引擎)" };
|
|
980
|
+
}
|
|
981
|
+
else {
|
|
982
|
+
// 发起对话还在 → steer 唤醒它(报告直接落原对话);不在了 → 无头执行。
|
|
983
|
+
const target = String(task.conversationId ?? "").trim();
|
|
984
|
+
if (target && typeof svc.wakeConversation === "function") {
|
|
985
|
+
const w = await svc.wakeConversation(target, `[定时任务 ${task.name}] ${task.prompt}`);
|
|
986
|
+
if (w.ok) {
|
|
987
|
+
result = { ok: true, conversationId: w.conversationId };
|
|
988
|
+
}
|
|
989
|
+
else {
|
|
990
|
+
result = await svc.chatFromScheduler({
|
|
991
|
+
id: task.id,
|
|
992
|
+
cwd: task.cwd,
|
|
993
|
+
prompt: task.prompt,
|
|
994
|
+
model: task.model,
|
|
995
|
+
thinkingLevel: task.thinkingLevel,
|
|
996
|
+
});
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
else {
|
|
1000
|
+
result = await svc.chatFromScheduler({
|
|
1001
|
+
id: task.id,
|
|
1002
|
+
cwd: task.cwd,
|
|
1003
|
+
prompt: task.prompt,
|
|
1004
|
+
model: task.model,
|
|
1005
|
+
thinkingLevel: task.thinkingLevel,
|
|
1006
|
+
});
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
catch (err) {
|
|
1011
|
+
result = { ok: false, error: err.message };
|
|
1012
|
+
}
|
|
1013
|
+
// 单次任务:触发一次后自动删除(历史随任务一起走,成败已由上面的 notify 播报)。
|
|
1014
|
+
if (task.oneShot) {
|
|
1015
|
+
try {
|
|
1016
|
+
scheduler.remove(task.id);
|
|
1017
|
+
}
|
|
1018
|
+
catch {
|
|
1019
|
+
// 删除失败不影响已记录的执行结果
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
return result;
|
|
1023
|
+
},
|
|
1024
|
+
onChange: () => pushSchedulerTasks(),
|
|
1025
|
+
notify: (level, text, textEn) => pushNoticeToAll(level, text, textEn ?? text),
|
|
1026
|
+
});
|
|
1027
|
+
scheduler.start();
|
|
1028
|
+
/** 把调度器任务列表推给所有在线客户端(设置面板展示 + 变更后刷新)。 */
|
|
1029
|
+
function pushSchedulerTasks() {
|
|
1030
|
+
try {
|
|
1031
|
+
const payload = JSON.stringify({ type: "scheduler_tasks", tasks: scheduler.list() });
|
|
1032
|
+
for (const client of wss.clients) {
|
|
1033
|
+
if (client.readyState !== WebSocket.OPEN)
|
|
1034
|
+
continue;
|
|
1035
|
+
try {
|
|
1036
|
+
client.send(payload);
|
|
1037
|
+
}
|
|
1038
|
+
catch {
|
|
1039
|
+
/* 死连接:index.ts 自己会清理 */
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
catch {
|
|
1044
|
+
/* 序列化失败不影响调度 */
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
824
1047
|
/** 广播通知条给全部在线客户端(全局事件,不属于某个 ClientSession —— 如 mcp.json 坏)。 */
|
|
825
1048
|
function pushNoticeToAll(level, text, textEn) {
|
|
826
1049
|
const payload = JSON.stringify({ type: "notice", level, text, textEn });
|
|
@@ -866,7 +1089,10 @@ pluginMgr.chatProvider = (pluginId, req) => service.chatFromPlugin?.(pluginId, r
|
|
|
866
1089
|
// 插件扩展点:插件注册的 AI 工具(registerAgentTool)+ MCP 桥工具 → 会话创建时
|
|
867
1090
|
// 带上 + 变化时动态注入/移除已有会话。
|
|
868
1091
|
service.pluginToolsProvider = () => [...pluginMgr.getAgentTools(), ...mcpBridge.getTools()];
|
|
869
|
-
pluginMgr.onAgentToolsChanged = () =>
|
|
1092
|
+
pluginMgr.onAgentToolsChanged = () => {
|
|
1093
|
+
service.applyPluginAgentTools();
|
|
1094
|
+
void pluginMgr.pushToAll().catch(() => { });
|
|
1095
|
+
};
|
|
870
1096
|
// 插件扩展点:插件斜杠命令(registerCommand)→ 命令选择器目录 + prompt 拦截执行。
|
|
871
1097
|
pluginMgr.onCommandsChanged = () => service.applyPluginCommandCatalog();
|
|
872
1098
|
service.pluginCommandsProvider = () => pluginMgr.listCommands();
|
|
@@ -874,6 +1100,11 @@ service.pluginCommandsProvider = () => pluginMgr.listCommands();
|
|
|
874
1100
|
pluginMgr.onBgTasksChanged = () => service.refreshBackgroundServers();
|
|
875
1101
|
service.pluginBgTasksProvider = () => pluginMgr.bgTasks();
|
|
876
1102
|
service.pluginStopBgTask = (taskId) => pluginMgr.stopPluginBgTask(taskId);
|
|
1103
|
+
// 定时任务 Agent 工具的数据源(schedule_task/list/cancel):标准 pi 引擎的
|
|
1104
|
+
// AgentService 才有 schedulerStore 字段,DSH service 没有 —— 有才设。
|
|
1105
|
+
if ("schedulerStore" in service) {
|
|
1106
|
+
service.schedulerStore = scheduler;
|
|
1107
|
+
}
|
|
877
1108
|
// ---------------------------------------------------------------------------
|
|
878
1109
|
// 插件扩展点 v2(并行任务在 server/plugins.ts 加 host.conversations/prompt/
|
|
879
1110
|
// steer/abortRun/chatWait/fs.watch/scm/bash/schedule/models/onStats/onStreaming/
|
|
@@ -887,12 +1118,12 @@ service.pluginStopBgTask = (taskId) => pluginMgr.stopPluginBgTask(taskId);
|
|
|
887
1118
|
// {ok:false},绝不抛错炸进程。
|
|
888
1119
|
// ---------------------------------------------------------------------------
|
|
889
1120
|
{
|
|
1121
|
+
// SAFETY: This compatibility bridge only assigns optional plugin hooks; consumers test their presence.
|
|
890
1122
|
const pm = pluginMgr;
|
|
891
1123
|
/** 挑一个客户端会话:标准 pi 引擎走 service.pluginClient(),DSH/未知引擎无此方法即 undefined。 */
|
|
892
1124
|
const pickClient = () => {
|
|
893
1125
|
try {
|
|
894
|
-
|
|
895
|
-
return typeof svc.pluginClient === "function" ? svc.pluginClient() : undefined;
|
|
1126
|
+
return service instanceof AgentService ? service.pluginClient() : undefined;
|
|
896
1127
|
}
|
|
897
1128
|
catch {
|
|
898
1129
|
return undefined;
|
|
@@ -978,6 +1209,7 @@ service.pluginStopBgTask = (taskId) => pluginMgr.stopPluginBgTask(taskId);
|
|
|
978
1209
|
// 标准 pi 引擎走 service.completeForPlugins;DSH/未知引擎回 {ok:false},绝不抛错。
|
|
979
1210
|
pm.llmProvider = async (pluginId, req) => {
|
|
980
1211
|
try {
|
|
1212
|
+
// SAFETY: Only invoked after the optional method is checked; request fields are narrowed below.
|
|
981
1213
|
const svc = service;
|
|
982
1214
|
if (typeof svc.completeForPlugins !== "function")
|
|
983
1215
|
return { ok: false, error: "当前引擎不支持 LLM 直调(仅标准 pi 引擎)" };
|
|
@@ -1137,7 +1369,7 @@ wss.on("connection", (ws) => {
|
|
|
1137
1369
|
void cs.prompt(msg.text, msg.attachments, msg.queue);
|
|
1138
1370
|
break;
|
|
1139
1371
|
case "queue_remove":
|
|
1140
|
-
cs.removeQueued(msg.kind, msg.text);
|
|
1372
|
+
cs.removeQueued(msg.kind, msg.text, msg.index);
|
|
1141
1373
|
break;
|
|
1142
1374
|
case "draft_update":
|
|
1143
1375
|
cs.saveDraft?.(msg.sessionId, msg.text, msg.ts);
|
|
@@ -1252,6 +1484,12 @@ wss.on("connection", (ws) => {
|
|
|
1252
1484
|
case "file_copy":
|
|
1253
1485
|
void cs.copyEntry(msg.src, msg.destDir, msg.move);
|
|
1254
1486
|
break;
|
|
1487
|
+
case "file_reveal":
|
|
1488
|
+
void cs.revealEntry(msg.path);
|
|
1489
|
+
break;
|
|
1490
|
+
case "file_open_default":
|
|
1491
|
+
void cs.openDefaultEntry(msg.path);
|
|
1492
|
+
break;
|
|
1255
1493
|
case "list_models":
|
|
1256
1494
|
void cs.listModels();
|
|
1257
1495
|
break;
|
|
@@ -1278,7 +1516,7 @@ wss.on("connection", (ws) => {
|
|
|
1278
1516
|
void cs.completePath(msg.path);
|
|
1279
1517
|
break;
|
|
1280
1518
|
case "make_dir":
|
|
1281
|
-
void cs.makeDir(msg.path);
|
|
1519
|
+
void cs.makeDir(msg.path, msg.setAsCwd === true);
|
|
1282
1520
|
break;
|
|
1283
1521
|
case "check_update":
|
|
1284
1522
|
void cs.checkUpdate();
|
|
@@ -1367,6 +1605,12 @@ wss.on("connection", (ws) => {
|
|
|
1367
1605
|
case "refresh_provider_models":
|
|
1368
1606
|
void cs.refreshProviderModels(msg.providerId, msg.reqId);
|
|
1369
1607
|
break;
|
|
1608
|
+
case "refresh_builtin_models":
|
|
1609
|
+
void cs.refreshBuiltinModels(msg.reqId);
|
|
1610
|
+
break;
|
|
1611
|
+
case "append_builtin_model":
|
|
1612
|
+
void cs.appendBuiltinModel(msg.providerId, msg.model, msg.reqId);
|
|
1613
|
+
break;
|
|
1370
1614
|
case "clone_provider":
|
|
1371
1615
|
void cs.cloneProvider(msg.provider, msg.reqId);
|
|
1372
1616
|
break;
|
|
@@ -1443,6 +1687,7 @@ wss.on("connection", (ws) => {
|
|
|
1443
1687
|
cs.pushSettings();
|
|
1444
1688
|
break;
|
|
1445
1689
|
case "set_settings":
|
|
1690
|
+
// SAFETY: Both dispatch engines validate the explicitly enumerated settings fields below.
|
|
1446
1691
|
void cs.setSettings({
|
|
1447
1692
|
promptMode: msg.promptMode,
|
|
1448
1693
|
customSystemPrompt: msg.customSystemPrompt,
|
|
@@ -1451,6 +1696,7 @@ wss.on("connection", (ws) => {
|
|
|
1451
1696
|
disabledSkills: msg.disabledSkills,
|
|
1452
1697
|
disabledExtensions: msg.disabledExtensions,
|
|
1453
1698
|
disabledAgentTools: msg.disabledAgentTools,
|
|
1699
|
+
disabledPluginTools: msg.disabledPluginTools,
|
|
1454
1700
|
disabledPlugins: msg.disabledPlugins,
|
|
1455
1701
|
terminalToolsEnabled: msg.terminalToolsEnabled,
|
|
1456
1702
|
terminalBash: msg.terminalBash,
|
|
@@ -1695,8 +1941,73 @@ wss.on("connection", (ws) => {
|
|
|
1695
1941
|
case "delete_preset":
|
|
1696
1942
|
void cs.deletePreset(msg.name);
|
|
1697
1943
|
break;
|
|
1698
|
-
|
|
1944
|
+
case "schedule_list":
|
|
1945
|
+
try {
|
|
1946
|
+
send({ type: "scheduler_tasks", tasks: scheduler.list() });
|
|
1947
|
+
}
|
|
1948
|
+
catch (err) {
|
|
1949
|
+
send({
|
|
1950
|
+
type: "notice",
|
|
1951
|
+
level: "error",
|
|
1952
|
+
text: `读取定时任务失败:${err.message}`,
|
|
1953
|
+
textEn: `Failed to list scheduled tasks: ${err.message}`,
|
|
1954
|
+
});
|
|
1955
|
+
}
|
|
1956
|
+
break;
|
|
1957
|
+
case "schedule_save":
|
|
1958
|
+
try {
|
|
1959
|
+
scheduler.upsert(msg.task);
|
|
1960
|
+
}
|
|
1961
|
+
catch (err) {
|
|
1962
|
+
send({
|
|
1963
|
+
type: "notice",
|
|
1964
|
+
level: "error",
|
|
1965
|
+
text: `保存定时任务失败:${err.message}`,
|
|
1966
|
+
textEn: `Failed to save scheduled task: ${err.message}`,
|
|
1967
|
+
});
|
|
1968
|
+
}
|
|
1969
|
+
break;
|
|
1970
|
+
case "schedule_delete":
|
|
1971
|
+
if (!scheduler.remove(msg.id)) {
|
|
1972
|
+
send({
|
|
1973
|
+
type: "notice",
|
|
1974
|
+
level: "warning",
|
|
1975
|
+
text: `定时任务不存在:${msg.id}`,
|
|
1976
|
+
textEn: `No such scheduled task: ${msg.id}`,
|
|
1977
|
+
});
|
|
1978
|
+
}
|
|
1979
|
+
break;
|
|
1980
|
+
case "schedule_run":
|
|
1981
|
+
void scheduler.runNow(msg.id).then((r) => {
|
|
1982
|
+
if (!r.ok)
|
|
1983
|
+
send({
|
|
1984
|
+
type: "notice",
|
|
1985
|
+
level: "warning",
|
|
1986
|
+
text: `定时任务手动触发失败:${r.error ?? "未知错误"}`,
|
|
1987
|
+
textEn: `Manual scheduled-task run failed: ${r.error ?? "unknown error"}`,
|
|
1988
|
+
});
|
|
1989
|
+
});
|
|
1699
1990
|
break;
|
|
1991
|
+
case "schedule_toggle":
|
|
1992
|
+
if (!scheduler.setEnabled(msg.id, msg.enabled === true)) {
|
|
1993
|
+
send({
|
|
1994
|
+
type: "notice",
|
|
1995
|
+
level: "warning",
|
|
1996
|
+
text: `定时任务不存在:${msg.id}`,
|
|
1997
|
+
textEn: `No such scheduled task: ${msg.id}`,
|
|
1998
|
+
});
|
|
1999
|
+
}
|
|
2000
|
+
break;
|
|
2001
|
+
default: {
|
|
2002
|
+
// 未知 type 只计数 + 节流 warn,不改变已知类型行为。
|
|
2003
|
+
// default 分支里 msg 已收窄成 never,type 需从宽化后取。
|
|
2004
|
+
const raw = msg.type;
|
|
2005
|
+
const name = typeof raw === "string" ? raw : String(raw);
|
|
2006
|
+
const r = recordUnknownWsType(name);
|
|
2007
|
+
if (r.warn)
|
|
2008
|
+
console.warn(`[ws] unknown message type "${name}" (x${r.count})`);
|
|
2009
|
+
break;
|
|
2010
|
+
}
|
|
1700
2011
|
}
|
|
1701
2012
|
};
|
|
1702
2013
|
ws.on("message", (data) => {
|
|
@@ -1758,6 +2069,13 @@ wss.on("connection", (ws) => {
|
|
|
1758
2069
|
// 注册表(plugin-fence.ts),`` ```lang `` 围栏才能立即命中插件;
|
|
1759
2070
|
// 否则消息先落成普通代码块,清单后到也不会重渲。
|
|
1760
2071
|
cs.flushSnapshot();
|
|
2072
|
+
// 内置定时任务列表随附推一次(后续变更经 pushSchedulerTasks 广播)。
|
|
2073
|
+
try {
|
|
2074
|
+
send({ type: "scheduler_tasks", tasks: scheduler.list() });
|
|
2075
|
+
}
|
|
2076
|
+
catch {
|
|
2077
|
+
/* 推送失败不挡快照 */
|
|
2078
|
+
}
|
|
1761
2079
|
})
|
|
1762
2080
|
.catch(() => {
|
|
1763
2081
|
if (closed)
|
|
@@ -1894,6 +2212,33 @@ async function shutdown(signal = "SIGINT") {
|
|
|
1894
2212
|
}
|
|
1895
2213
|
shuttingDown = true;
|
|
1896
2214
|
console.log("\nshutting down…");
|
|
2215
|
+
// Windows ConPTY 兜底看门狗(PR #204 带入):主线程若死锁在 native
|
|
2216
|
+
// ClosePseudoConsole 里,事件循环冻结,上面的 forceExitTimer 永远触发不了 ——
|
|
2217
|
+
// 只能靠外部进程收尾。超时取 forceExit + 余量(只在进程内兜底失效时才开火);
|
|
2218
|
+
// 正常退出在 finally 里取消,避免误杀 + PID 复用竞态。
|
|
2219
|
+
// cmd.exe 是无 GUI 的控制台宿主,windowsHide 藏的是它一闪而过的黑窗口。
|
|
2220
|
+
let disarmKiller = null;
|
|
2221
|
+
if (process.platform === "win32") {
|
|
2222
|
+
try {
|
|
2223
|
+
const { spawn } = await import("node:child_process");
|
|
2224
|
+
const killer = spawn("cmd.exe", [
|
|
2225
|
+
"/c",
|
|
2226
|
+
`timeout /t ${Math.ceil((SHUTDOWN_FORCE_EXIT_MS + 5000) / 1000)} /nobreak >nul && taskkill /F /PID ${process.pid}`,
|
|
2227
|
+
], { detached: true, stdio: "ignore", windowsHide: true });
|
|
2228
|
+
killer.unref();
|
|
2229
|
+
disarmKiller = () => {
|
|
2230
|
+
try {
|
|
2231
|
+
killer.kill();
|
|
2232
|
+
}
|
|
2233
|
+
catch {
|
|
2234
|
+
// 已退出/杀不掉:看门狗使命本来就是收尾,无需上报
|
|
2235
|
+
}
|
|
2236
|
+
};
|
|
2237
|
+
}
|
|
2238
|
+
catch {
|
|
2239
|
+
// 看门狗起不来:回落进程内 forceExitTimer,不阻断关机
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
1897
2242
|
const forceExitTimer = setTimeout(() => {
|
|
1898
2243
|
console.error("shutdown 超时仍未完成,强制退出…");
|
|
1899
2244
|
process.exit(1);
|
|
@@ -1903,6 +2248,7 @@ async function shutdown(signal = "SIGINT") {
|
|
|
1903
2248
|
try {
|
|
1904
2249
|
clearInterval(heartbeatTimer);
|
|
1905
2250
|
stopControl();
|
|
2251
|
+
scheduler.stop();
|
|
1906
2252
|
pluginMgr.dispose();
|
|
1907
2253
|
pluginInstaller.dispose();
|
|
1908
2254
|
mcpHotReload.dispose();
|
|
@@ -1930,6 +2276,7 @@ async function shutdown(signal = "SIGINT") {
|
|
|
1930
2276
|
}
|
|
1931
2277
|
finally {
|
|
1932
2278
|
clearTimeout(forceExitTimer);
|
|
2279
|
+
disarmKiller?.();
|
|
1933
2280
|
process.exit(code);
|
|
1934
2281
|
}
|
|
1935
2282
|
}
|