pi-web-ui 0.114.3 → 0.116.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 +62 -1
- package/bin/pi-web-ui.mjs +90 -8
- package/dist/gh-comment-781.md +5 -0
- package/dist/server/agent-service-daemon.js +522 -32
- package/dist/server/agent-service.js +954 -402
- package/dist/server/canvas/canvas-dispatch.js +13 -1
- package/dist/server/client-state.js +42 -2
- package/dist/server/communicate-tool.js +2 -0
- package/dist/server/conversation-communication.js +14 -0
- package/dist/server/conversation-identity.js +27 -0
- package/dist/server/device-center/approval-summary.js +31 -0
- package/dist/server/device-center/bot-catalog.js +21 -7
- package/dist/server/device-center/bot-execution.js +100 -9
- package/dist/server/device-center/bot-presets.js +46 -0
- package/dist/server/device-center/collab-service.js +362 -13
- package/dist/server/device-center/collab-tool.js +824 -13
- package/dist/server/device-center/connector.js +359 -23
- package/dist/server/device-center/desktop-page.js +22 -16
- package/dist/server/device-center/device-integration.js +595 -0
- package/dist/server/device-center/execution-guides.js +32 -0
- package/dist/server/device-center/execution-instructions.js +20 -0
- package/dist/server/device-center/execution-settings.js +213 -0
- package/dist/server/device-center/execution-system-prompt.js +38 -0
- package/dist/server/device-center/execution-workspace.js +20 -0
- package/dist/server/device-center/executor.js +392 -40
- package/dist/server/device-center/file-sync-paths.js +171 -0
- package/dist/server/device-center/file-sync.js +186 -96
- package/dist/server/device-center/group-context-delta.js +37 -0
- package/dist/server/device-center/group-context-message.js +210 -0
- package/dist/server/device-center/group-context.js +179 -0
- package/dist/server/device-center/group-events.js +136 -0
- package/dist/server/device-center/group-plan-guard.js +138 -0
- package/dist/server/device-center/hub-conversations.js +233 -0
- package/dist/server/device-center/hub-deliveries.js +277 -0
- package/dist/server/device-center/hub-integration.js +451 -0
- package/dist/server/device-center/hub-locks.js +320 -0
- package/dist/server/device-center/hub-page.js +13 -33
- package/dist/server/device-center/hub-webhooks.js +165 -0
- package/dist/server/device-center/hub.js +2379 -107
- package/dist/server/device-center/human-interaction.js +232 -0
- package/dist/server/device-center/integration-errors.js +25 -0
- package/dist/server/device-center/local-hub.js +20 -1
- package/dist/server/device-center/local-page.js +17 -0
- package/dist/server/device-center/local.js +430 -41
- package/dist/server/device-center/main.js +11 -1
- package/dist/server/device-center/partition-log.js +293 -0
- package/dist/server/device-center/path-portal.js +11 -3
- package/dist/server/device-center/relay.js +364 -35
- package/dist/server/device-center/report-metadata.js +66 -0
- package/dist/server/device-center/report-upload.js +63 -0
- package/dist/server/device-center/runtime.js +598 -41
- package/dist/server/device-center/smoke-preview.js +3 -0
- package/dist/server/device-center/store.js +203 -41
- package/dist/server/device-center/task-context-budget.js +161 -0
- package/dist/server/device-center/task-journal.js +385 -0
- package/dist/server/device-center/task-plan.js +33 -0
- package/dist/server/device-center/task-progress.js +141 -0
- package/dist/server/device-center/task-recovery.js +452 -0
- package/dist/server/device-center/task-report-outbox.js +216 -0
- package/dist/server/device-center/task-report.js +384 -0
- package/dist/server/device-center/task-scheduler.js +445 -0
- package/dist/server/device-center/task-state.js +333 -0
- package/dist/server/device-center/transfer-stream.js +1006 -0
- package/dist/server/device-center/transfers.js +841 -8
- package/dist/server/dispatch-domain-handlers.js +35 -6
- package/dist/server/dsh/dsh-agent-service.js +6 -0
- package/dist/server/durable/durable-capabilities.js +11 -0
- package/dist/server/engine-probe.js +112 -1
- package/dist/server/global-loader.js +119 -19
- package/dist/server/goal-service.js +10 -5
- package/dist/server/index.js +222 -29
- package/dist/server/managed.js +2 -0
- package/dist/server/markers/builtins/services.js +123 -0
- package/dist/server/markers/builtins/todo.js +189 -0
- package/dist/server/markers-list-tool.js +34 -0
- package/dist/server/message-deletion.js +40 -0
- package/dist/server/message-window.js +9 -0
- package/dist/server/model-admin.js +48 -6
- package/dist/server/model-enrich.js +10 -1
- package/dist/server/orchestrator.js +109 -0
- package/dist/server/patch-executable-lookup.js +396 -0
- package/dist/server/payload-limits.js +2 -0
- package/dist/server/plugin-llm.js +100 -9
- package/dist/server/plugins.js +65 -7
- package/dist/server/preset-fields.js +24 -0
- package/dist/server/preset-share.js +2 -0
- package/dist/server/prompt-base-store.js +85 -0
- package/dist/server/prompt-composer.js +44 -2
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/protocol.js +2 -2
- package/dist/server/reasoning-registry.js +207 -0
- package/dist/server/remote-ssh-service.js +22 -9
- package/dist/server/resolve-global-sdk.js +2 -0
- package/dist/server/session-search.js +1 -1
- package/dist/server/settings-service.js +90 -39
- package/dist/server/skill-content-filler.js +29 -8
- package/dist/server/skill-tool.js +12 -3
- package/dist/server/startup-banner.js +35 -0
- package/dist/server/subagent-helpers.js +4 -1
- package/dist/server/subagents.js +52 -37
- package/dist/server/system-prompt-soul.js +41 -0
- package/dist/server/tool-host-factories.js +1 -0
- package/dist/server/tool-manager.js +17 -9
- package/dist/server/unauthorized-page.js +319 -0
- package/dist/server/univer-service.js +448 -0
- package/dist/server/universal/drivers/dsh-driver.js +379 -0
- package/dist/server/universal/universal-session.js +5 -0
- package/dist/server/webui-context.js +25 -5
- package/dist/test-artifact.md +9 -0
- package/dist/test-preview-modal.md +11 -0
- package/dist/test-report.html +20 -0
- package/docs/README.md +94 -0
- package/docs/architecture-plugins.md +946 -0
- package/docs/device-center.md +106 -12
- package/docs/dsh-engine.md +10 -10
- package/extensions/webui.ts +43 -1
- package/package.json +2 -1
- package/themes/aetheris.css +5 -0
- package/themes/ayu-light.css +6 -0
- package/themes/bases/flow-base.css +68 -68
- package/themes/catppuccin-latte.css +6 -0
- package/themes/catppuccin.css +6 -0
- package/themes/claude-code-dark.css +5 -0
- package/themes/codex.css +6 -0
- package/themes/cyberpunk.css +6 -0
- package/themes/dark-teal.css +6 -1
- package/themes/dazzle.css +6 -0
- package/themes/everforest-light.css +6 -0
- package/themes/flow-light.css +8 -1
- package/themes/flow.css +6 -1
- package/themes/geist.css +6 -0
- package/themes/gruvbox-light.css +6 -0
- package/themes/kanagawa-lotus.css +6 -0
- package/themes/md-preview.css +6 -0
- package/themes/mist.css +6 -0
- package/themes/nord.css +6 -0
- package/themes/one-dark.css +6 -0
- package/themes/paper.css +6 -0
- package/themes/rose-pine-dawn.css +6 -0
- package/themes/sakura.css +6 -0
- package/themes/solarized-light.css +6 -0
- package/themes/tokyo-night.css +6 -0
- package/themes/translucent.css +6 -0
- package/themes/transparent.css +6 -0
- package/themes/vs-code-dark.css +5 -0
- package/themes/wallpaper-flow.css +5 -1
- package/themes/wallpaper-obsidian.css +6 -0
- package/themes/white.css +6 -0
- package/themes/zhujian-dark.css +6 -1
- package/themes/zhujian.css +6 -1
- package/themes/zhupi-dark.css +6 -1
- package/themes/zhupi.css +6 -1
- package/web/dist/.build-assets.json +1 -1
- package/web/dist/assets/BgTasksModal-BDoOpj-F.js +1 -0
- package/web/dist/assets/BgTasksModal-C0i3O1FT.js +1 -0
- package/web/dist/assets/{CanvasStudio-BSg9ghPV.js → CanvasStudio-B8thymBN.js} +3 -3
- package/web/dist/assets/{CanvasStudio-DxW7C3Sz.js → CanvasStudio-zSxyIx1R.js} +3 -3
- package/web/dist/assets/DeviceCenter-DfAHKcid.js +3 -0
- package/web/dist/assets/DeviceCenter-ydvOTlzE.js +3 -0
- package/web/dist/assets/DeviceExecutionSettings-q5Ik8oXg.js +1 -0
- package/web/dist/assets/DeviceGroups-CI14_SRb.js +1 -0
- package/web/dist/assets/DeviceGroups-DvsB5IEU.js +1 -0
- package/web/dist/assets/Dropdown-C4LIhshN.js +1 -0
- package/web/dist/assets/FilePreview-BgGongmv.js +2 -0
- package/web/dist/assets/FilePreview-D4NrhF-U.js +2 -0
- package/web/dist/assets/GlobalSearchModal-BWgU8yPd.js +1 -0
- package/web/dist/assets/GlobalSearchModal-F7fNimKk.js +1 -0
- package/web/dist/assets/GroupConversation-Dh71Pdb-.js +17 -0
- package/web/dist/assets/GroupConversation-ZK3nRmNm.js +17 -0
- package/web/dist/assets/HintTip-c6d0TJoh.js +1 -0
- package/web/dist/assets/ModelConfigModal-YVEgyy34.js +2 -0
- package/web/dist/assets/ModelConfigModal-vWtg3srp.js +2 -0
- package/web/dist/assets/PiSetupModal-BmMrrgB9.js +1 -0
- package/web/dist/assets/ProviderOAuthControls-BGSBIKD7.js +1 -0
- package/web/dist/assets/SCMPanel-B8_EDxNr.js +2 -0
- package/web/dist/assets/SCMPanel-BU58qk3T.js +2 -0
- package/web/dist/assets/SettingsModal-CMUybq3J.js +32 -0
- package/web/dist/assets/SettingsModal-OBmGTh-5.js +32 -0
- package/web/dist/assets/TerminalPanel-0CYvD72B.js +3 -0
- package/web/dist/assets/TerminalPanel-DDavKvFr.js +3 -0
- package/web/dist/assets/TuiOverlayModal-BsA-QTvN.js +1 -0
- package/web/dist/assets/TuiOverlayModal-DzoVQBuG.js +1 -0
- package/web/dist/assets/app-globals-CZxzGvIo.js +123 -0
- package/web/dist/assets/canvas-client-CUnXlQYX.js +1 -0
- package/web/dist/assets/{desktop-7EvUHhKQ.js → desktop-aAKpvKJV.js} +1 -1
- package/web/dist/assets/group-view-state-CGCzYF8k.js +1 -0
- package/web/dist/assets/index-C6DwztqE.css +1 -0
- package/web/dist/assets/index-CuN802Sj.js +93 -0
- package/web/dist/assets/index-EH5Fw9fa.js +93 -0
- package/web/dist/assets/plugin-host-BRWib9DF.js +1 -0
- package/web/dist/assets/remote-ssh-client-CfYSMEZg.js +1 -0
- package/web/dist/assets/theme-D2l_mLNo.js +1 -0
- package/web/dist/index.html +10 -9
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/BgTasksModal-BvnUHjFe.js +0 -1
- package/web/dist/assets/BgTasksModal-DSC8ncW3.js +0 -1
- package/web/dist/assets/DeviceCenter-Bmgr-Bwm.js +0 -3
- package/web/dist/assets/DeviceCenter-Cl9MBXaH.js +0 -3
- package/web/dist/assets/DeviceGroups-aeLD2msl.js +0 -1
- package/web/dist/assets/Dropdown-D1Jl35dw.js +0 -1
- package/web/dist/assets/FilePreview-6CqoDF_P.js +0 -2
- package/web/dist/assets/FilePreview-CJa27G_Q.js +0 -2
- package/web/dist/assets/GlobalSearchModal-CJX35rZL.js +0 -1
- package/web/dist/assets/GlobalSearchModal-CMW0xz0P.js +0 -1
- package/web/dist/assets/GroupConversation-Cq4daBFb.js +0 -1
- package/web/dist/assets/GroupConversation-DoELa83U.js +0 -1
- package/web/dist/assets/ModelConfigModal-Bp7tfKYh.js +0 -2
- package/web/dist/assets/PiSetupModal-DD8srwBJ.js +0 -1
- package/web/dist/assets/ProviderOAuthControls-C6tfXb88.js +0 -1
- package/web/dist/assets/SCMPanel-CghgsyBN.js +0 -2
- package/web/dist/assets/SCMPanel-D8-8aqwC.js +0 -2
- package/web/dist/assets/SettingsModal-BbBe2rYo.js +0 -20
- package/web/dist/assets/SettingsModal-qETiVPKj.js +0 -20
- package/web/dist/assets/TerminalPanel-Da-Xs4Wk.js +0 -3
- package/web/dist/assets/TerminalPanel-DnS4tFQO.js +0 -3
- package/web/dist/assets/TuiOverlayModal-CGyGkKYl.js +0 -1
- package/web/dist/assets/TuiOverlayModal-CpBKjG6x.js +0 -1
- package/web/dist/assets/app-globals-DcyO0UPi.js +0 -1
- package/web/dist/assets/fi-NyN-ZrgD.js +0 -325
- package/web/dist/assets/index-DK3F5ZJo.js +0 -85
- package/web/dist/assets/index-DQMii2he.js +0 -85
- package/web/dist/assets/index-vwBnwzkr.css +0 -1
- package/web/dist/assets/plugin-host-BCnX3P8U.js +0 -1
- package/web/dist/assets/theme-D2DMH6tH.js +0 -1
package/dist/server/index.js
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
* PI_CODING_AGENT_DIR pi config dir (auth/models/skills) — passed to the SDK
|
|
18
18
|
*/
|
|
19
19
|
import { botExecutionMessageAllowed } from "./device-center/bot-execution.js";
|
|
20
|
+
import { ALL_AGENT_TOOL_ENTRIES, effectiveDisabledAgentTools, isToolSupportedOnPlatform } from "./tool-manager.js";
|
|
20
21
|
// MUST be imported before the SDK is loaded so on-disk patches apply cleanly.
|
|
21
22
|
import "./patch-remote-catalog.js";
|
|
22
23
|
import "./patch-turn-end-boundary.js";
|
|
@@ -73,6 +74,8 @@ import { globalLspPool } from "./lsp-tool.js";
|
|
|
73
74
|
import { buildPiWebTokenCookie, decodeCookieToken, isTlsRequest } from "./auth-cookie.js";
|
|
74
75
|
import { isLoopbackAddress, loadServerFileConfig, saveServerFileConfig, tokenWarning } from "./server-config.js";
|
|
75
76
|
import { renderSetupHtml } from "./setup-page.js";
|
|
77
|
+
import { renderUnauthorizedHtml } from "./unauthorized-page.js";
|
|
78
|
+
import { formatStartupBannerLines } from "./startup-banner.js";
|
|
76
79
|
import { getAppVersion, getPkgRoot } from "./pkg-info.js";
|
|
77
80
|
import { handleFileMessage, handleScmMessage, handleBgServerMessage, handlePresetAndDshMessage, handleModelAndProviderMessage, handleTerminalMessage, handleSessionLifecycleMessage, handlePlanAndGoalMessage, handleScheduleMessage, handleSettingsMessage, handleInteractiveResponseMessage, handleRemoteSshMessage, } from "./dispatch-domain-handlers.js";
|
|
78
81
|
import { RemoteSshService, isRemoteWorkspaceUri, serveRemoteWorkspaceFile, setGlobalRemoteSshService, } from "./remote-ssh-service.js";
|
|
@@ -210,6 +213,10 @@ const LOCALE_BASE_URL = process.env.PI_WEB_LOCALE_BASE_URL?.trim() || "https://r
|
|
|
210
213
|
function appVersion() {
|
|
211
214
|
return getAppVersion();
|
|
212
215
|
}
|
|
216
|
+
/** 本次服务进程的运行实例 id(ready 帧的 runId):
|
|
217
|
+
* 重启即变,同一进程内所有连接共享。客户端用它区分「断线重连」与「新实例」,
|
|
218
|
+
* 决定问卷的已答 id 缓存 / 未确认答案能否保留(否则已答问卷会被在途旧快照复活)。 */
|
|
219
|
+
const SERVER_RUN_ID = randomUUID();
|
|
213
220
|
/** On-disk web-build id: the main JS bundle hash from the built index.html.
|
|
214
221
|
* Changes on every rebuild — stale pages compare and reload themselves.
|
|
215
222
|
* Declared near use (below webDist), not here: webDist is a const further
|
|
@@ -519,6 +526,15 @@ app.use((req, res, next) => {
|
|
|
519
526
|
next();
|
|
520
527
|
return;
|
|
521
528
|
}
|
|
529
|
+
const isHtml = Boolean(req.headers.accept?.includes("text/html"));
|
|
530
|
+
if (isHtml) {
|
|
531
|
+
const isLoopback = isLoopbackAddress(req.socket?.remoteAddress);
|
|
532
|
+
res.status(401);
|
|
533
|
+
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
534
|
+
res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
|
|
535
|
+
res.send(renderUnauthorizedHtml({ isLoopback, isStaleCookie: Boolean(cookie), port: PORT }));
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
522
538
|
res
|
|
523
539
|
.status(401)
|
|
524
540
|
.send(cookie
|
|
@@ -1022,6 +1038,10 @@ const deviceCenter = await installLocalDeviceCenter({
|
|
|
1022
1038
|
app,
|
|
1023
1039
|
dataDir: DATA_DIR,
|
|
1024
1040
|
bots: deviceBots,
|
|
1041
|
+
// Reverse-proxied deployments reach the device center (groups) through a real
|
|
1042
|
+
// domain: accept the operator-declared hostnames there too, otherwise every
|
|
1043
|
+
// /api/device-center call is 403 LOCAL_ONLY behind nginx/Caddy.
|
|
1044
|
+
allowHosts: ALLOW_HOSTS,
|
|
1025
1045
|
botCatalog: {
|
|
1026
1046
|
defaultCwd: CWD,
|
|
1027
1047
|
dataDir: DATA_DIR,
|
|
@@ -1033,6 +1053,10 @@ const deviceCenter = await installLocalDeviceCenter({
|
|
|
1033
1053
|
},
|
|
1034
1054
|
projects: () => sharedStateStore.getRecentProjects(""),
|
|
1035
1055
|
defaultModel: () => sharedStateStore.getDefaultModel("pi"),
|
|
1056
|
+
defaultTools: () => {
|
|
1057
|
+
const disabled = new Set(effectiveDisabledAgentTools(sharedStateStore.getSettingsSnapshot("")));
|
|
1058
|
+
return ALL_AGENT_TOOL_ENTRIES.filter((tool) => !disabled.has(tool.name) && isToolSupportedOnPlatform(tool.name)).map((tool) => tool.name);
|
|
1059
|
+
},
|
|
1036
1060
|
peekMcpBridge: (cwd) => mcpProjects.peekBridge(cwd),
|
|
1037
1061
|
},
|
|
1038
1062
|
async connect(config, syncBots) {
|
|
@@ -1051,7 +1075,10 @@ const deviceCenter = await installLocalDeviceCenter({
|
|
|
1051
1075
|
credential: config.credential,
|
|
1052
1076
|
localOrigin,
|
|
1053
1077
|
localAuthToken: AUTH_TOKEN,
|
|
1078
|
+
recovery: runtime.recovery,
|
|
1079
|
+
reportOutbox: runtime.reportOutbox,
|
|
1054
1080
|
onTask: runtime.onTask,
|
|
1081
|
+
onInteractionAnswer: (answer) => runtime.answerTaskInteraction(answer.taskId, answer.attemptId, answer),
|
|
1055
1082
|
onConnect: syncBots,
|
|
1056
1083
|
onCommand: runtime.onCommand,
|
|
1057
1084
|
onDisconnect: () => {
|
|
@@ -1062,6 +1089,7 @@ const deviceCenter = await installLocalDeviceCenter({
|
|
|
1062
1089
|
});
|
|
1063
1090
|
return {
|
|
1064
1091
|
isConnected: connector.isOnline,
|
|
1092
|
+
runtime,
|
|
1065
1093
|
async close() {
|
|
1066
1094
|
setDeviceCollabService(undefined);
|
|
1067
1095
|
connector.close();
|
|
@@ -1360,7 +1388,7 @@ heartbeatTimer.unref?.();
|
|
|
1360
1388
|
import { DshAgentService } from "./dsh/dsh-agent-service.js";
|
|
1361
1389
|
import { UniversalAgentService, ClaudeCodeDriver, probeClaudeCode, CodexDriver } from "./universal/index.js";
|
|
1362
1390
|
import { DurableAgentService } from "./durable/index.js";
|
|
1363
|
-
import { probeAllEngines, probeEngine, getCachedEngineStatuses, updateEngineBackground, isEngineUpdating, ENGINE_PACKAGES, } from "./engine-probe.js";
|
|
1391
|
+
import { probeAllEngines, probeEngine, getCachedEngineStatuses, updateEngineBackground, isEngineUpdating, uninstallEngineBackground, isEngineUninstalling, ENGINE_PACKAGES, } from "./engine-probe.js";
|
|
1364
1392
|
let piServiceInstance = null;
|
|
1365
1393
|
let dshServiceInstance = null;
|
|
1366
1394
|
let claudeServiceInstance = null;
|
|
@@ -1459,8 +1487,8 @@ function ensureEngine(engine) {
|
|
|
1459
1487
|
// 启动时预热默认引擎实例
|
|
1460
1488
|
ensureEngine(currentDefaultEngine);
|
|
1461
1489
|
const service = {
|
|
1462
|
-
attach(clientId, send) {
|
|
1463
|
-
return ensureEngine(resolveClientEngine(clientId)).attach(clientId, send);
|
|
1490
|
+
attach(clientId, send, sendShared) {
|
|
1491
|
+
return ensureEngine(resolveClientEngine(clientId)).attach(clientId, send, sendShared);
|
|
1464
1492
|
},
|
|
1465
1493
|
detach(clientId, send) {
|
|
1466
1494
|
const eng = clientEngines.get(clientId);
|
|
@@ -1569,6 +1597,24 @@ const service = {
|
|
|
1569
1597
|
setLocale(clientId, locale) {
|
|
1570
1598
|
return ensureEngine(resolveClientEngine(clientId)).setLocale(clientId, locale);
|
|
1571
1599
|
},
|
|
1600
|
+
openSharedConversation(viewerId, ownerId, convId, send) {
|
|
1601
|
+
return (ensureEngine(resolveClientEngine(viewerId)).openSharedConversation?.(viewerId, ownerId, convId, send) ?? false);
|
|
1602
|
+
},
|
|
1603
|
+
closeSharedConversation(viewerId) {
|
|
1604
|
+
ensureEngine(resolveClientEngine(viewerId)).closeSharedConversation?.(viewerId);
|
|
1605
|
+
},
|
|
1606
|
+
sharedConversationTarget(viewerId) {
|
|
1607
|
+
return ensureEngine(resolveClientEngine(viewerId)).sharedConversationTarget?.(viewerId);
|
|
1608
|
+
},
|
|
1609
|
+
dispatchSharedConversationMessage(viewerId, msg) {
|
|
1610
|
+
return ensureEngine(resolveClientEngine(viewerId)).dispatchSharedConversationMessage?.(viewerId, msg) ?? false;
|
|
1611
|
+
},
|
|
1612
|
+
resolveSharedQuestion(viewerId, id, answers, cancelled, cancelReason) {
|
|
1613
|
+
return (ensureEngine(resolveClientEngine(viewerId)).resolveSharedQuestion?.(viewerId, id, answers, cancelled, cancelReason) ?? false);
|
|
1614
|
+
},
|
|
1615
|
+
resolveSharedToolApproval(viewerId, ...args) {
|
|
1616
|
+
return ensureEngine(resolveClientEngine(viewerId)).resolveSharedToolApproval?.(viewerId, ...args) ?? false;
|
|
1617
|
+
},
|
|
1572
1618
|
takeOverConversation(targetId, ownerId, convId) {
|
|
1573
1619
|
const targetSvc = ensureEngine(resolveClientEngine(targetId));
|
|
1574
1620
|
if (typeof targetSvc.takeOverConversation === "function") {
|
|
@@ -1803,6 +1849,7 @@ const scheduler = new SchedulerStore(DATA_DIR, {
|
|
|
1803
1849
|
else {
|
|
1804
1850
|
const runHeadless = () => svc.chatFromScheduler({
|
|
1805
1851
|
id: task.id,
|
|
1852
|
+
name: task.name,
|
|
1806
1853
|
cwd: task.cwd,
|
|
1807
1854
|
prompt: task.prompt,
|
|
1808
1855
|
model: task.model,
|
|
@@ -2261,7 +2308,9 @@ wss.on("connection", (ws) => {
|
|
|
2261
2308
|
if (!snapshotRetryTimer) {
|
|
2262
2309
|
snapshotRetryTimer = setTimeout(() => {
|
|
2263
2310
|
snapshotRetryTimer = null;
|
|
2264
|
-
service.
|
|
2311
|
+
if (!service.dispatchSharedConversationMessage?.(clientId ?? "", { type: "get_state" })) {
|
|
2312
|
+
service.get(clientId ?? "")?.flushSnapshot(wasFull);
|
|
2313
|
+
}
|
|
2265
2314
|
}, SNAPSHOT_RETRY_MS);
|
|
2266
2315
|
}
|
|
2267
2316
|
return;
|
|
@@ -2275,7 +2324,26 @@ wss.on("connection", (ws) => {
|
|
|
2275
2324
|
* `send` 必须包在 `sendRaw` **外面**:窗口先裁消息,再算背压字节数——否则
|
|
2276
2325
|
* 背压阈值还在按未裁剪的 10MB 量级估。 */
|
|
2277
2326
|
const windowTap = new MessageWindowTap(sendRaw, readMessageWindowConfig(process.env));
|
|
2278
|
-
const
|
|
2327
|
+
const sendShared = (msg) => {
|
|
2328
|
+
const shared = clientId ? service.sharedConversationTarget?.(clientId) : undefined;
|
|
2329
|
+
windowTap.setScope(shared ? `shared:${JSON.stringify([shared.ownerClientId, shared.conversationId])}` : `local:${clientId}`);
|
|
2330
|
+
windowTap.accept(msg);
|
|
2331
|
+
};
|
|
2332
|
+
const send = (msg) => {
|
|
2333
|
+
if (clientId &&
|
|
2334
|
+
service.sharedConversationTarget?.(clientId) &&
|
|
2335
|
+
(msg.type === "snapshot" ||
|
|
2336
|
+
msg.type === "snapshot_delta" ||
|
|
2337
|
+
msg.type === "tool_delta" ||
|
|
2338
|
+
msg.type === "message_delta" ||
|
|
2339
|
+
msg.type === "terminal_output" ||
|
|
2340
|
+
msg.type === "terminal_list" ||
|
|
2341
|
+
msg.type === "terminal_exit"))
|
|
2342
|
+
return;
|
|
2343
|
+
if (msg.type === "snapshot" || msg.type === "snapshot_delta")
|
|
2344
|
+
windowTap.setScope(`local:${clientId}`);
|
|
2345
|
+
windowTap.accept(msg);
|
|
2346
|
+
};
|
|
2279
2347
|
// Plugins broadcast to every open socket; unregister on close below.
|
|
2280
2348
|
// Plugins broadcast to every open socket; unregister on close below. The
|
|
2281
2349
|
// cid getter lets plugins target THIS socket via host.sendTo(clientId).
|
|
@@ -2359,7 +2427,7 @@ wss.on("connection", (ws) => {
|
|
|
2359
2427
|
const nextSvc = ensureEngine(targetEngine);
|
|
2360
2428
|
const switchGen = ++attachGen;
|
|
2361
2429
|
nextSvc
|
|
2362
|
-
.attach(cid, send)
|
|
2430
|
+
.attach(cid, send, sendShared)
|
|
2363
2431
|
.then((newCs) => {
|
|
2364
2432
|
if (closed) {
|
|
2365
2433
|
nextSvc.detach(cid, send);
|
|
@@ -2411,7 +2479,7 @@ wss.on("connection", (ws) => {
|
|
|
2411
2479
|
setClientEngine(cid, prevEngine);
|
|
2412
2480
|
const rollbackGen = ++attachGen;
|
|
2413
2481
|
ensureEngine(prevEngine)
|
|
2414
|
-
.attach(cid, send)
|
|
2482
|
+
.attach(cid, send, sendShared)
|
|
2415
2483
|
.then((rollbackCs) => {
|
|
2416
2484
|
if (closed || rollbackGen !== attachGen) {
|
|
2417
2485
|
ensureEngine(prevEngine).detach(cid, send);
|
|
@@ -2512,6 +2580,103 @@ wss.on("connection", (ws) => {
|
|
|
2512
2580
|
});
|
|
2513
2581
|
}
|
|
2514
2582
|
};
|
|
2583
|
+
const handleUninstallEngine = async (msg) => {
|
|
2584
|
+
const targetEngine = String(msg.engineId || "").trim();
|
|
2585
|
+
if (targetEngine === "pi") {
|
|
2586
|
+
send({
|
|
2587
|
+
type: "notice",
|
|
2588
|
+
level: "error",
|
|
2589
|
+
text: "Pi 为内置核心引擎,不支持卸载",
|
|
2590
|
+
textEn: "Pi is the built-in core engine and cannot be uninstalled",
|
|
2591
|
+
});
|
|
2592
|
+
return;
|
|
2593
|
+
}
|
|
2594
|
+
const meta = ENGINE_PACKAGES[targetEngine];
|
|
2595
|
+
if (!meta || !meta.packageName) {
|
|
2596
|
+
send({
|
|
2597
|
+
type: "notice",
|
|
2598
|
+
level: "error",
|
|
2599
|
+
text: `不支持或未知的引擎「${targetEngine}」`,
|
|
2600
|
+
textEn: `Unsupported or unknown engine "${targetEngine}"`,
|
|
2601
|
+
});
|
|
2602
|
+
return;
|
|
2603
|
+
}
|
|
2604
|
+
const refusal = managedRefusal("uninstall_engine", MANAGED);
|
|
2605
|
+
if (refusal) {
|
|
2606
|
+
send({ type: "notice", level: "error", text: refusal });
|
|
2607
|
+
return;
|
|
2608
|
+
}
|
|
2609
|
+
if (isEngineUpdating(targetEngine)) {
|
|
2610
|
+
send({
|
|
2611
|
+
type: "notice",
|
|
2612
|
+
level: "warning",
|
|
2613
|
+
text: `引擎「${targetEngine.toUpperCase()}」正在后台更新中,请稍候再试`,
|
|
2614
|
+
textEn: `Engine "${targetEngine.toUpperCase()}" is currently updating, please wait…`,
|
|
2615
|
+
});
|
|
2616
|
+
return;
|
|
2617
|
+
}
|
|
2618
|
+
if (isEngineUninstalling(targetEngine)) {
|
|
2619
|
+
send({
|
|
2620
|
+
type: "notice",
|
|
2621
|
+
level: "warning",
|
|
2622
|
+
text: `引擎「${targetEngine.toUpperCase()}」正在后台卸载中,请稍候…`,
|
|
2623
|
+
textEn: `Engine "${targetEngine.toUpperCase()}" is already uninstalling, please wait…`,
|
|
2624
|
+
});
|
|
2625
|
+
return;
|
|
2626
|
+
}
|
|
2627
|
+
const engineDisplayNames = {
|
|
2628
|
+
pi: { zh: "Pi", en: "Pi" },
|
|
2629
|
+
dsh: { zh: "DSH", en: "DSH" },
|
|
2630
|
+
claude: { zh: "Claude Code", en: "Claude Code" },
|
|
2631
|
+
codex: { zh: "Codex", en: "Codex" },
|
|
2632
|
+
durable: { zh: "Pi Durable", en: "Pi Durable" },
|
|
2633
|
+
};
|
|
2634
|
+
const displayName = engineDisplayNames[targetEngine] ?? {
|
|
2635
|
+
zh: targetEngine.toUpperCase(),
|
|
2636
|
+
en: targetEngine.toUpperCase(),
|
|
2637
|
+
};
|
|
2638
|
+
const broadcastStatuses = (list) => {
|
|
2639
|
+
const payload = JSON.stringify({ type: "engines_status", engines: list });
|
|
2640
|
+
for (const c of wss.clients) {
|
|
2641
|
+
if (c.readyState === WebSocket.OPEN) {
|
|
2642
|
+
try {
|
|
2643
|
+
c.send(payload);
|
|
2644
|
+
}
|
|
2645
|
+
catch {
|
|
2646
|
+
/* ignore */
|
|
2647
|
+
}
|
|
2648
|
+
}
|
|
2649
|
+
}
|
|
2650
|
+
};
|
|
2651
|
+
send({
|
|
2652
|
+
type: "notice",
|
|
2653
|
+
level: "info",
|
|
2654
|
+
text: `正在后台卸载 ${displayName.zh} 引擎(npm uninstall -g ${meta.packageName})…`,
|
|
2655
|
+
textEn: `Uninstalling ${displayName.en} engine in background (npm uninstall -g ${meta.packageName})…`,
|
|
2656
|
+
});
|
|
2657
|
+
const uninstallPromise = uninstallEngineBackground(targetEngine);
|
|
2658
|
+
broadcastStatuses(getCachedEngineStatuses());
|
|
2659
|
+
const res = await uninstallPromise;
|
|
2660
|
+
const freshEngines = await probeAllEngines(true);
|
|
2661
|
+
broadcastStatuses(freshEngines);
|
|
2662
|
+
if (res.ok) {
|
|
2663
|
+
send({
|
|
2664
|
+
type: "notice",
|
|
2665
|
+
level: "info",
|
|
2666
|
+
text: `✅ ${displayName.zh} 引擎卸载完成`,
|
|
2667
|
+
textEn: `✅ ${displayName.en} engine uninstalled successfully`,
|
|
2668
|
+
});
|
|
2669
|
+
}
|
|
2670
|
+
else {
|
|
2671
|
+
const detail = res.error || (res.output ? res.output.slice(-200) : "");
|
|
2672
|
+
send({
|
|
2673
|
+
type: "notice",
|
|
2674
|
+
level: "error",
|
|
2675
|
+
text: `❌ ${displayName.zh} 引擎卸载失败${detail ? `:${detail}` : ""}`,
|
|
2676
|
+
textEn: `❌ Failed to uninstall ${displayName.en} engine${detail ? `: ${detail}` : ""}`,
|
|
2677
|
+
});
|
|
2678
|
+
}
|
|
2679
|
+
};
|
|
2515
2680
|
const dispatch = (msg, wireBytes) => {
|
|
2516
2681
|
if (!clientId) {
|
|
2517
2682
|
if (!pending.push(msg, wireBytes))
|
|
@@ -2527,6 +2692,10 @@ wss.on("connection", (ws) => {
|
|
|
2527
2692
|
void handleUpdateEngine(msg);
|
|
2528
2693
|
return;
|
|
2529
2694
|
}
|
|
2695
|
+
if (msg.type === "uninstall_engine") {
|
|
2696
|
+
void handleUninstallEngine(msg);
|
|
2697
|
+
return;
|
|
2698
|
+
}
|
|
2530
2699
|
if (msg.type === "check_engines") {
|
|
2531
2700
|
void probeAllEngines(msg.force).then((engines) => {
|
|
2532
2701
|
if (!closed)
|
|
@@ -2544,6 +2713,12 @@ wss.on("connection", (ws) => {
|
|
|
2544
2713
|
// 历史窗口按需加载:连接级窗口化直接服务(四个引擎通吃,不依赖引擎实现)。
|
|
2545
2714
|
// 放在引擎分发之前:它不是引擎能力,窗口化关闭时静默无回执(前端保持现状)。
|
|
2546
2715
|
if (msg.type === "load_earlier_messages") {
|
|
2716
|
+
const sharedTarget = service.sharedConversationTarget?.(clientId);
|
|
2717
|
+
if (sharedTarget && msg.conversationId && msg.conversationId !== sharedTarget.conversationId)
|
|
2718
|
+
return;
|
|
2719
|
+
windowTap.setScope(sharedTarget
|
|
2720
|
+
? `shared:${JSON.stringify([sharedTarget.ownerClientId, sharedTarget.conversationId])}`
|
|
2721
|
+
: `local:${clientId}`);
|
|
2547
2722
|
let handled = windowTap.loadEarlier({
|
|
2548
2723
|
count: msg.count,
|
|
2549
2724
|
all: msg.all,
|
|
@@ -2553,9 +2728,14 @@ wss.on("connection", (ws) => {
|
|
|
2553
2728
|
historyGeneration: msg.historyGeneration,
|
|
2554
2729
|
});
|
|
2555
2730
|
if (!handled && cs) {
|
|
2556
|
-
const
|
|
2731
|
+
const shared = service.sharedConversationTarget?.(clientId);
|
|
2732
|
+
const historyOwner = shared?.owner ?? cs;
|
|
2733
|
+
const activeId = shared?.conversationId ??
|
|
2734
|
+
(typeof cs.activeConversationId === "function" ? cs.activeConversationId() : undefined);
|
|
2557
2735
|
const convId = msg.conversationId || activeId;
|
|
2558
|
-
|
|
2736
|
+
if (shared && convId !== shared.conversationId)
|
|
2737
|
+
return;
|
|
2738
|
+
const full = convId ? historyOwner.getMessagesOfConversation?.(convId) : undefined;
|
|
2559
2739
|
if (convId && full && full.length > 0) {
|
|
2560
2740
|
windowTap.rehydrate(convId, full, msg.before);
|
|
2561
2741
|
handled = windowTap.loadEarlier({
|
|
@@ -2568,7 +2748,8 @@ wss.on("connection", (ws) => {
|
|
|
2568
2748
|
});
|
|
2569
2749
|
}
|
|
2570
2750
|
if (!handled && (!msg.conversationId || msg.conversationId === activeId)) {
|
|
2571
|
-
|
|
2751
|
+
if (!service.dispatchSharedConversationMessage?.(clientId, { type: "get_state" }))
|
|
2752
|
+
cs.flushSnapshot(true);
|
|
2572
2753
|
}
|
|
2573
2754
|
}
|
|
2574
2755
|
return;
|
|
@@ -2583,6 +2764,10 @@ wss.on("connection", (ws) => {
|
|
|
2583
2764
|
return;
|
|
2584
2765
|
}
|
|
2585
2766
|
try {
|
|
2767
|
+
if (service.dispatchSharedConversationMessage?.(clientId, msg))
|
|
2768
|
+
return;
|
|
2769
|
+
if (handleInteractiveResponseMessage(msg, cs, service, clientId, send))
|
|
2770
|
+
return;
|
|
2586
2771
|
if (handleFileMessage(msg, cs))
|
|
2587
2772
|
return;
|
|
2588
2773
|
if (handleScmMessage(msg, cs, send))
|
|
@@ -2595,7 +2780,7 @@ wss.on("connection", (ws) => {
|
|
|
2595
2780
|
return;
|
|
2596
2781
|
if (handleTerminalMessage(msg, cs))
|
|
2597
2782
|
return;
|
|
2598
|
-
if (handleSessionLifecycleMessage(msg, cs, send))
|
|
2783
|
+
if (handleSessionLifecycleMessage(msg, cs, send, service, clientId))
|
|
2599
2784
|
return;
|
|
2600
2785
|
if (handlePlanAndGoalMessage(msg, cs))
|
|
2601
2786
|
return;
|
|
@@ -2607,8 +2792,6 @@ wss.on("connection", (ws) => {
|
|
|
2607
2792
|
return;
|
|
2608
2793
|
if (handleCanvasMessage(msg, DATA_DIR, canvasOrchestrator, send, { runBase: cs.cwd }))
|
|
2609
2794
|
return;
|
|
2610
|
-
if (handleInteractiveResponseMessage(msg, cs, service, clientId, send))
|
|
2611
|
-
return;
|
|
2612
2795
|
if (handlePluginMessage(msg, {
|
|
2613
2796
|
pluginMgr,
|
|
2614
2797
|
pluginInstaller,
|
|
@@ -2703,6 +2886,16 @@ wss.on("connection", (ws) => {
|
|
|
2703
2886
|
send({ type: "tool_prompt", name: msg.name, found: false, unsupported: true });
|
|
2704
2887
|
}
|
|
2705
2888
|
break;
|
|
2889
|
+
case "open_shared_conversation":
|
|
2890
|
+
if (!service.openSharedConversation?.(clientId, msg.owner, msg.id, sendShared)) {
|
|
2891
|
+
send({
|
|
2892
|
+
type: "notice",
|
|
2893
|
+
level: "error",
|
|
2894
|
+
text: "共享对话不可用",
|
|
2895
|
+
textEn: "Shared conversation is unavailable.",
|
|
2896
|
+
});
|
|
2897
|
+
}
|
|
2898
|
+
return;
|
|
2706
2899
|
case "take_over_conversation":
|
|
2707
2900
|
if (typeof service.takeOverConversation === "function") {
|
|
2708
2901
|
void service.takeOverConversation(clientId, msg.owner, msg.id);
|
|
@@ -2823,6 +3016,9 @@ wss.on("connection", (ws) => {
|
|
|
2823
3016
|
// which a managed instance never runs.
|
|
2824
3017
|
appVersion: appVersion(),
|
|
2825
3018
|
buildId: buildId(),
|
|
3019
|
+
// 本进程运行实例 id:客户端靠它区分「断线重连」与「换了新服务」,
|
|
3020
|
+
// 决定已答问卷 id / 未确认答案能否跨连接保留(issue: 问卷无限重生)。
|
|
3021
|
+
runId: SERVER_RUN_ID,
|
|
2826
3022
|
managed: MANAGED,
|
|
2827
3023
|
tabs: TABS ? [...TABS] : undefined,
|
|
2828
3024
|
service: SERVICE_INFO ?? undefined,
|
|
@@ -2852,7 +3048,7 @@ wss.on("connection", (ws) => {
|
|
|
2852
3048
|
const msg = parsed;
|
|
2853
3049
|
try {
|
|
2854
3050
|
const botClientId = clientId ?? (msg.type === "hello" ? msg.clientId : undefined);
|
|
2855
|
-
if (botClientId && !botExecutionMessageAllowed(botClientId, msg.type)) {
|
|
3051
|
+
if (botClientId && !botExecutionMessageAllowed(botClientId, msg.type, msg)) {
|
|
2856
3052
|
ws.close(1008, "Bot control command denied");
|
|
2857
3053
|
return;
|
|
2858
3054
|
}
|
|
@@ -2922,7 +3118,7 @@ wss.on("connection", (ws) => {
|
|
|
2922
3118
|
const helloSvc = ensureEngine(resolveClientEngine(cid));
|
|
2923
3119
|
const helloGen = ++attachGen;
|
|
2924
3120
|
helloSvc
|
|
2925
|
-
.attach(cid, send)
|
|
3121
|
+
.attach(cid, send, sendShared)
|
|
2926
3122
|
.then((cs) => {
|
|
2927
3123
|
clearTimeout(slowTimer);
|
|
2928
3124
|
if (closed) {
|
|
@@ -3094,22 +3290,19 @@ if (process.env[RESTART_CHILD_ENV] === "1") {
|
|
|
3094
3290
|
}
|
|
3095
3291
|
}
|
|
3096
3292
|
httpServer.listen(PORT, HOST, () => {
|
|
3097
|
-
console.log("");
|
|
3098
|
-
console.log(" ⚡ pi-web-ui — web chat for the pi coding agent");
|
|
3099
|
-
console.log(` http://localhost:${PORT}`);
|
|
3100
|
-
console.log(` workspace : ${CWD}`);
|
|
3101
|
-
console.log(` session dir : ${SESSION_DIR_ROOT}`);
|
|
3102
|
-
console.log(` pi SDK : v${VERSION}`);
|
|
3103
|
-
// issue #260:全局那份 pi SDK 不是服务在用的那份(自带副本赢在 Node 解析顺序上)。
|
|
3104
|
-
// #321 起默认已反转(resolve-global-sdk 自动跟随更新的那份),这条提示只在
|
|
3105
|
-
// 「进程没跟上」(升级发生在启动后 / 旧构建没注入钩子)或显式 PI_WEB_SDK=bundled
|
|
3106
|
-
// 钉死自带副本时出现。
|
|
3107
3293
|
const sdkNote = sdkOriginNote(sdkCopies(), VERSION);
|
|
3108
|
-
|
|
3109
|
-
|
|
3294
|
+
const bannerLines = formatStartupBannerLines({
|
|
3295
|
+
port: PORT,
|
|
3296
|
+
host: HOST,
|
|
3297
|
+
token: AUTH_TOKEN,
|
|
3298
|
+
cwd: CWD,
|
|
3299
|
+
sessionDir: SESSION_DIR_ROOT,
|
|
3300
|
+
version: VERSION,
|
|
3301
|
+
sdkNote,
|
|
3302
|
+
});
|
|
3303
|
+
for (const line of bannerLines) {
|
|
3304
|
+
console.log(line);
|
|
3110
3305
|
}
|
|
3111
|
-
console.log(` bind : ${HOST}:${PORT}`);
|
|
3112
|
-
console.log(` setup : http://127.0.0.1:${PORT}/setup (仅本机:配 token/监听/白名单)`);
|
|
3113
3306
|
if (autoGeneratedToken) {
|
|
3114
3307
|
console.log("");
|
|
3115
3308
|
console.log(" ╔═══════════════════════════════════════════════════════════════════════════════╗");
|
package/dist/server/managed.js
CHANGED
|
@@ -33,6 +33,8 @@ export const MANAGED_MESSAGES = [
|
|
|
33
33
|
"install_pi_agent",
|
|
34
34
|
/** Updates an agent engine package via background process. */
|
|
35
35
|
"update_engine",
|
|
36
|
+
/** Uninstalls an agent engine package via background process. */
|
|
37
|
+
"uninstall_engine",
|
|
36
38
|
/** Plugin market: fetches and installs a plugin from the network. */
|
|
37
39
|
"plugin_catalog_add",
|
|
38
40
|
/** Plugin install/update/uninstall run as a background job (issue #152). */
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* builtins/services.ts — 后台服务登记(复刻 pi-marker-tools)。
|
|
3
|
+
*/
|
|
4
|
+
export const SVC_NAMESPACE = "svc";
|
|
5
|
+
export function initServiceState() {
|
|
6
|
+
return { services: [], nextId: 1 };
|
|
7
|
+
}
|
|
8
|
+
function parseId(raw) {
|
|
9
|
+
if (raw === undefined)
|
|
10
|
+
return null;
|
|
11
|
+
const n = Number(raw);
|
|
12
|
+
return Number.isInteger(n) && n > 0 ? n : null;
|
|
13
|
+
}
|
|
14
|
+
function toInt(raw) {
|
|
15
|
+
if (raw === undefined || raw === "")
|
|
16
|
+
return undefined;
|
|
17
|
+
const n = Number(raw);
|
|
18
|
+
return Number.isInteger(n) && n >= 0 ? n : undefined;
|
|
19
|
+
}
|
|
20
|
+
function findService(state, id) {
|
|
21
|
+
return state.services.find((s) => s.id === id);
|
|
22
|
+
}
|
|
23
|
+
function lineOf(s) {
|
|
24
|
+
const meta = [s.pid !== undefined ? `pid ${s.pid}` : "", s.port !== undefined ? `:${s.port}` : ""]
|
|
25
|
+
.filter(Boolean)
|
|
26
|
+
.join(" ");
|
|
27
|
+
return `${s.stopped ? "[x]" : "[ ]"} #${s.id}: ${s.name}${meta ? ` (${meta})` : ""}${s.command ? ` — ${s.command}` : ""}`;
|
|
28
|
+
}
|
|
29
|
+
export function describeServices(state) {
|
|
30
|
+
if (!state || state.services.length === 0)
|
|
31
|
+
return "[svc] (空)";
|
|
32
|
+
return state.services.map(lineOf).join("\n");
|
|
33
|
+
}
|
|
34
|
+
export const servicesMarker = {
|
|
35
|
+
name: "svc",
|
|
36
|
+
guidance: [
|
|
37
|
+
"- 后台服务登记(todo 风格):[[svc:add:名称,pid=,port=,cmd=]] 登记;[[svc:stop:<id>]] 标记停止;[[svc:resume:<id>]] 标记运行;[[svc:remove:<id>]] 删除;[[svc:clear:]] 清空。",
|
|
38
|
+
"- 用 bash 启动后台服务(nohup ... &、pm2 start、docker run -d 等)后,立即写 [[svc:add:...]] 登记,尽量带 pid= 和 port=;服务退出或被关闭后写 [[svc:stop:<id>]] 保持清单准确。",
|
|
39
|
+
],
|
|
40
|
+
async apply(token, _ctx, _state) {
|
|
41
|
+
const state = _state;
|
|
42
|
+
const op = token.op;
|
|
43
|
+
switch (op) {
|
|
44
|
+
case "add": {
|
|
45
|
+
const name = token.args[0]?.trim();
|
|
46
|
+
if (!name)
|
|
47
|
+
return { applied: false, error: "svc:add 需要一个名称参数 [[svc:add:<名称>,pid=,port=,cmd=]]" };
|
|
48
|
+
const svc = {
|
|
49
|
+
id: state.nextId++,
|
|
50
|
+
name,
|
|
51
|
+
command: token.kwargs["cmd"],
|
|
52
|
+
pid: toInt(token.kwargs["pid"]),
|
|
53
|
+
port: toInt(token.kwargs["port"]),
|
|
54
|
+
startedAt: Date.now(),
|
|
55
|
+
stopped: false,
|
|
56
|
+
};
|
|
57
|
+
state.services.push(svc);
|
|
58
|
+
const meta = [svc.pid !== undefined ? `pid ${svc.pid}` : "", svc.port !== undefined ? `:${svc.port}` : ""]
|
|
59
|
+
.filter(Boolean)
|
|
60
|
+
.join(" ");
|
|
61
|
+
return { applied: true, feedback: `Registered #${svc.id}: ${name}${meta ? ` (${meta})` : ""}` };
|
|
62
|
+
}
|
|
63
|
+
case "stop": {
|
|
64
|
+
const id = parseId(token.args[0]);
|
|
65
|
+
if (id === null)
|
|
66
|
+
return { applied: false, error: `svc:stop 的 id 无效: "${token.args[0] ?? ""}"` };
|
|
67
|
+
const svc = findService(state, id);
|
|
68
|
+
if (!svc)
|
|
69
|
+
return { applied: false, error: `svc:stop 服务 #${id} 不存在` };
|
|
70
|
+
svc.stopped = true;
|
|
71
|
+
svc.stoppedAt = Date.now();
|
|
72
|
+
return { applied: true, feedback: `#${id} ${svc.name} marked stopped` };
|
|
73
|
+
}
|
|
74
|
+
case "resume": {
|
|
75
|
+
const id = parseId(token.args[0]);
|
|
76
|
+
if (id === null)
|
|
77
|
+
return { applied: false, error: `svc:resume 的 id 无效: "${token.args[0] ?? ""}"` };
|
|
78
|
+
const svc = findService(state, id);
|
|
79
|
+
if (!svc)
|
|
80
|
+
return { applied: false, error: `svc:resume 服务 #${id} 不存在` };
|
|
81
|
+
svc.stopped = false;
|
|
82
|
+
svc.stoppedAt = undefined;
|
|
83
|
+
return { applied: true, feedback: `#${id} ${svc.name} marked running` };
|
|
84
|
+
}
|
|
85
|
+
case "remove": {
|
|
86
|
+
const id = parseId(token.args[0]);
|
|
87
|
+
if (id === null)
|
|
88
|
+
return { applied: false, error: `svc:remove 的 id 无效: "${token.args[0] ?? ""}"` };
|
|
89
|
+
const before = state.services.length;
|
|
90
|
+
state.services = state.services.filter((s) => s.id !== id);
|
|
91
|
+
if (state.services.length === before)
|
|
92
|
+
return { applied: false, error: `svc:remove 服务 #${id} 不存在` };
|
|
93
|
+
return { applied: true, feedback: `Removed #${id}` };
|
|
94
|
+
}
|
|
95
|
+
case "clear": {
|
|
96
|
+
const n = state.services.length;
|
|
97
|
+
state.services = [];
|
|
98
|
+
state.nextId = 1;
|
|
99
|
+
return { applied: true, feedback: `Cleared ${n} service(s)` };
|
|
100
|
+
}
|
|
101
|
+
default:
|
|
102
|
+
return { applied: false, error: `svc 未知操作: ${op}` };
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
overlay(state) {
|
|
106
|
+
if (!state || state.services.length === 0)
|
|
107
|
+
return undefined;
|
|
108
|
+
const running = state.services.filter((s) => !s.stopped).length;
|
|
109
|
+
const lines = state.services.map((s) => {
|
|
110
|
+
const mark = s.stopped ? "○" : "●";
|
|
111
|
+
let line = ` ${mark} #${s.id} ${s.name}`;
|
|
112
|
+
if (s.port !== undefined)
|
|
113
|
+
line += ` :${s.port}`;
|
|
114
|
+
if (s.pid !== undefined)
|
|
115
|
+
line += ` (pid ${s.pid})`;
|
|
116
|
+
if (s.stopped)
|
|
117
|
+
line += " 已停止";
|
|
118
|
+
return line;
|
|
119
|
+
});
|
|
120
|
+
return { tool: "svc", lines: [`${running}/${state.services.length} running`, ...lines] };
|
|
121
|
+
},
|
|
122
|
+
init: initServiceState,
|
|
123
|
+
};
|