pi-web-ui 0.89.0 → 0.90.1
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 +65 -1
- package/README.md +7 -4
- package/README.zh-CN.md +6 -3
- package/bin/pi-web-ui.mjs +37 -10
- package/deploy/pi-web-ui.service +36 -31
- package/dist/server/agent-service.js +651 -112
- package/dist/server/auth-cookie.js +23 -0
- package/dist/server/client-state.js +13 -0
- package/dist/server/composer-drafts.js +20 -2
- package/dist/server/dsh/dsh-agent-service.js +14 -8
- package/dist/server/file-archives.js +258 -0
- package/dist/server/file-transfer-routes.js +113 -0
- package/dist/server/files-service.js +22 -3
- package/dist/server/index.js +169 -31
- package/dist/server/marker-service.js +3 -1
- package/dist/server/plugins.js +12 -4
- package/dist/server/queue-utils.js +16 -0
- package/dist/server/schedule-agent-tool.js +240 -0
- package/dist/server/scheduler-tasks.js +9 -0
- package/dist/server/settings-service.js +68 -1
- package/dist/server/terminals.js +50 -20
- package/dist/server/tool-manager.js +11 -1
- package/dist/server/update-check.js +10 -6
- package/dist/server/webui-context.js +4 -0
- package/dist/server/ws-unknown-types.js +44 -0
- package/package.json +35 -32
- package/web/dist/assets/TerminalPanel-CkxM4Byl.js +3 -0
- package/web/dist/assets/index-DJIV4A-t.js +355 -0
- package/web/dist/assets/index-DLdcRBs_.css +1 -0
- package/web/dist/assets/markdown-CCeVCbci.css +1 -0
- package/web/dist/assets/markdown-eUQn_o9D.js +296 -0
- package/web/dist/assets/react-CR5VJ85Q.js +9 -0
- package/web/dist/assets/rolldown-runtime-hePW80VL.js +1 -0
- package/web/dist/assets/xterm-B1njb2Cr.css +1 -0
- package/web/dist/assets/xterm-Cf5xxe4g.js +16 -0
- package/web/dist/index.html +6 -4
- package/web/dist/assets/TerminalPanel-BOUmN3uu.js +0 -6
- package/web/dist/assets/TerminalPanel-DOrYoP_4.css +0 -32
- package/web/dist/assets/index-3XU67qMh.js +0 -376
- package/web/dist/assets/index-B-gdvWtW.css +0 -10
- package/web/dist/assets/markdown-D3PKeHAZ.js +0 -311
- package/web/dist/assets/react-w24rH0km.js +0 -24
- package/web/dist/assets/xterm-B96xOxS9.js +0 -38
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// schedule-agent-tool.ts — 把内置调度器暴露给 Agent(issue #193)
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
// 背景:pi-web-ui 早有完整调度基建(SchedulerStore:cron/间隔触发+持久化+
|
|
5
|
+
// 无头执行,见 scheduler-tasks.ts),但只停留在插件 API 与图形面板层面 ——
|
|
6
|
+
// AI 被用户要求“每小时检查一次”“半小时后提醒我”时手里没有工具,只能用
|
|
7
|
+
// sleep 死循环假装答应(休眠的会话根本推不回消息)。
|
|
8
|
+
//
|
|
9
|
+
// 本文件注册三个标准 pi 引擎 customTool(DSH 引擎无 customTool 注册面,不接):
|
|
10
|
+
// schedule_task → 建任务(默认绑定发起对话+单次,用熟即删);
|
|
11
|
+
// schedule_list → 看全部任务(含下次触发/上次结果);
|
|
12
|
+
// schedule_cancel → 按 id 删任务。
|
|
13
|
+
// 到期执行走 index.ts 的 executor:目标对话还在 → steer 语义唤醒它(不切用户
|
|
14
|
+
// 当前对话);不在了(关闭/重启)→ 回落原有无头执行;单次任务触发后自动删除。
|
|
15
|
+
//
|
|
16
|
+
// 双语约定(issue #91):definition 走 bilingual(en, zh) 内联双语;per-call
|
|
17
|
+
// 返回文本按 lang 取 pick(lang, zh, en, key, vars),缺表回落英文内联。
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
import { defineTool } from "@earendil-works/pi-coding-agent";
|
|
20
|
+
import { Type } from "typebox";
|
|
21
|
+
import { bilingual, pick } from "./i18n.js";
|
|
22
|
+
import { SCHEDULE_CANCEL_TOOL_NAME, SCHEDULE_LIST_TOOL_NAME, SCHEDULE_TASK_TOOL_NAME } from "./tool-manager.js";
|
|
23
|
+
import { computeNextFire, describeIntervalMs, SCHEDULER_MIN_INTERVAL_MS, } from "./scheduler-tasks.js";
|
|
24
|
+
import { parseCronSpec } from "./plugin-schedule.js";
|
|
25
|
+
/** 用户写法 → 调度规格。纯函数,可单测。
|
|
26
|
+
* 收:5 字段 cron("0 * * * *")、相对时间("in 30m" / "in 1h" / "in 2d")、
|
|
27
|
+
* 裸时长("30m" / "1h",裸数字按分钟)。单位 s/m/h/d/w(大小写不敏感)。 */
|
|
28
|
+
export function parseScheduleSpec(raw) {
|
|
29
|
+
const s = String(raw ?? "").trim();
|
|
30
|
+
if (!s)
|
|
31
|
+
throw new Error("empty");
|
|
32
|
+
const single = s.replace(/\s+/g, " ");
|
|
33
|
+
// 5 字段即按 cron 试("in 30m"是 2 段,不会误判)。
|
|
34
|
+
if (single.split(" ").length === 5 && parseCronSpec(single)) {
|
|
35
|
+
return { kind: "cron", spec: single };
|
|
36
|
+
}
|
|
37
|
+
const m = /^(?:in\s+)?(\d+(?:\.\d+)?)\s*(s|sec|secs|second|seconds|m|min|mins|minute|minutes|h|hour|hours|d|day|days|w|week|weeks)?$/i.exec(single);
|
|
38
|
+
if (!m) {
|
|
39
|
+
throw new Error(`无法解析的时间写法:${s}(要 5 字段 cron 如 "0 * * * *",或相对时间如 "in 30m" / "in 1h" / "30m")`);
|
|
40
|
+
}
|
|
41
|
+
const n = Number(m[1]);
|
|
42
|
+
const unit = (m[2] ?? "m").toLowerCase();
|
|
43
|
+
const mult = unit.startsWith("s")
|
|
44
|
+
? 1000
|
|
45
|
+
: unit.startsWith("m")
|
|
46
|
+
? 60_000
|
|
47
|
+
: unit.startsWith("h")
|
|
48
|
+
? 3_600_000
|
|
49
|
+
: unit.startsWith("d")
|
|
50
|
+
? 86_400_000
|
|
51
|
+
: 7 * 86_400_000; // w
|
|
52
|
+
const ms = Math.floor(n * mult);
|
|
53
|
+
if (!Number.isFinite(ms) || ms <= 0)
|
|
54
|
+
throw new Error(`时长非法:${s}`);
|
|
55
|
+
return { kind: "interval", spec: String(ms) };
|
|
56
|
+
}
|
|
57
|
+
function fmtTime(ms, lang) {
|
|
58
|
+
if (ms === null || !Number.isFinite(ms))
|
|
59
|
+
return lang === "zh" ? "(未知)" : "(unknown)";
|
|
60
|
+
try {
|
|
61
|
+
return new Date(ms).toLocaleString(lang === "zh" ? "zh-CN" : "en-US", { hour12: false });
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return new Date(ms).toISOString();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/** 任务一行摘要(列表用;cron 显示原样,interval 中文用“每 X”,英文用秒数)。 */
|
|
68
|
+
function taskLine(t, lang) {
|
|
69
|
+
const when = t.kind === "cron"
|
|
70
|
+
? `cron ${t.spec}`
|
|
71
|
+
: lang === "zh"
|
|
72
|
+
? describeIntervalMs(Number(t.spec))
|
|
73
|
+
: `every ${Math.round(Number(t.spec) / 1000)}s`;
|
|
74
|
+
const state = !t.enabled
|
|
75
|
+
? lang === "zh"
|
|
76
|
+
? "已暂停"
|
|
77
|
+
: "paused"
|
|
78
|
+
: t.running
|
|
79
|
+
? lang === "zh"
|
|
80
|
+
? "执行中"
|
|
81
|
+
: "running"
|
|
82
|
+
: lang === "zh"
|
|
83
|
+
? "启用"
|
|
84
|
+
: "on";
|
|
85
|
+
const once = t.oneShot ? (lang === "zh" ? " · 单次" : " · one-shot") : "";
|
|
86
|
+
const target = t.conversationId
|
|
87
|
+
? lang === "zh"
|
|
88
|
+
? ` · 汇报→对话${t.conversationId}`
|
|
89
|
+
: ` · reports→${t.conversationId}`
|
|
90
|
+
: lang === "zh"
|
|
91
|
+
? " · 无头执行"
|
|
92
|
+
: " · headless";
|
|
93
|
+
const last = t.lastRun
|
|
94
|
+
? lang === "zh"
|
|
95
|
+
? ` · 上次${t.lastRun.ok ? "成功" : `失败(${t.lastRun.error ?? "未知错误"})`}`
|
|
96
|
+
: ` · last ${t.lastRun.ok ? "ok" : `failed (${t.lastRun.error ?? "unknown"})`}`
|
|
97
|
+
: "";
|
|
98
|
+
return `• ${t.id} · ${t.name} · ${when} · ${state}${once}${target} · ${lang === "zh" ? "下次" : "next"} ${fmtTime(t.nextFire, lang)}${last}`;
|
|
99
|
+
}
|
|
100
|
+
export function makeScheduleTools(host, ownerConversationId, lang) {
|
|
101
|
+
const getLang = lang ?? (() => "en");
|
|
102
|
+
const text = (t, details = {}) => ({
|
|
103
|
+
content: [{ type: "text", text: t }],
|
|
104
|
+
details,
|
|
105
|
+
});
|
|
106
|
+
const noStore = () => text(pick(getLang(), "当前引擎不支持定时任务(调度存储未接入)。", "Scheduled tasks are not wired for the current engine.", "sched.not.wired"));
|
|
107
|
+
const taskTool = defineTool({
|
|
108
|
+
name: SCHEDULE_TASK_TOOL_NAME,
|
|
109
|
+
label: "Schedule a wake-up in this conversation",
|
|
110
|
+
description: bilingual("Create a scheduled wake-up in the CURRENT conversation: at the given cron time or after the given delay, the scheduler automatically delivers your prompt back into this conversation so you continue the work and report to the user. " +
|
|
111
|
+
"Use it when the user asks for periodic checks, delayed reminders, or scheduled summaries — do NOT fake it with sleep loops (a sleeping script can never push a message back). " +
|
|
112
|
+
'Schedule accepts a 5-field cron ("0 * * * *" = hourly) or a relative delay ("in 30m", "in 1h", "30m"); minimum interval is 60s. ' +
|
|
113
|
+
"One-shot by default (recurring=false auto-deletes the task after it fires); pass recurring=true for repeats. " +
|
|
114
|
+
"If the conversation is gone when it fires (closed/restarted), the task runs headless in its project and the report stays in the scheduler panel history. " +
|
|
115
|
+
"Manage with schedule_list / schedule_cancel; the user can also cancel from the background-tasks panel.", "在**当前对话**里创建一个定时唤醒:到指定 cron 时间或延迟后,调度器自动把你的 prompt 投回本对话,你继续执行并向用户汇报。" +
|
|
116
|
+
"用户要求定时巡检、延时提醒、定时汇总时用它 —— 不要用 sleep 死循环假装答应(休眠的脚本推不回任何消息)。" +
|
|
117
|
+
"时间写法收 5 字段 cron(“0 * * * *”=每小时)或相对延迟(“in 30m”“in 1h”“30m”);最短间隔 60s。" +
|
|
118
|
+
"默认单次(recurring=false 触发后自动删除);传 recurring=true 做周期任务。" +
|
|
119
|
+
"触发时对话已不在(关闭/重启)则回落无头执行,报告留在调度面板历史里。" +
|
|
120
|
+
"用 schedule_list / schedule_cancel 管理;用户也可从后台任务面板取消。"),
|
|
121
|
+
promptSnippet: "schedule a wake-up in this conversation (cron or delay), auto-report on fire",
|
|
122
|
+
parameters: Type.Object({
|
|
123
|
+
schedule: Type.String({
|
|
124
|
+
description: 'When to fire: 5-field cron ("0 * * * *" hourly) or relative delay ("in 30m", "in 1h", "30m"). Minimum 60s.',
|
|
125
|
+
}),
|
|
126
|
+
prompt: Type.String({
|
|
127
|
+
description: "Instruction delivered into this conversation on fire (e.g. what to check and report). Max 8000 chars.",
|
|
128
|
+
}),
|
|
129
|
+
label: Type.Optional(Type.String({ description: "Short name shown in the background-tasks panel. Defaults to the prompt head." })),
|
|
130
|
+
recurring: Type.Optional(Type.Boolean({ description: "Repeat on schedule (default false = one-shot, auto-deleted after firing)." })),
|
|
131
|
+
}),
|
|
132
|
+
execute: async (_id, p) => {
|
|
133
|
+
const store = host.store();
|
|
134
|
+
if (!store)
|
|
135
|
+
return noStore();
|
|
136
|
+
let parsed;
|
|
137
|
+
try {
|
|
138
|
+
parsed = parseScheduleSpec(p.schedule);
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
return text(pick(getLang(), `时间写法非法:${String(p.schedule ?? "")}(要 5 字段 cron 如 "0 * * * *" 或相对时间如 "in 30m"):${err.message}`, `Invalid schedule: ${String(p.schedule ?? "")} (want 5-field cron like "0 * * * *" or a delay like "in 30m"): ${err.message}`, "sched.task.bad.schedule", { schedule: String(p.schedule ?? "") }));
|
|
142
|
+
}
|
|
143
|
+
if (parsed.kind === "interval" && Number(parsed.spec) < SCHEDULER_MIN_INTERVAL_MS) {
|
|
144
|
+
return text(pick(getLang(), `间隔太短(最短 60s,防 token 烧穿)。`, `Interval too short (minimum 60s, prevents token burn).`, "sched.task.interval.too.short"));
|
|
145
|
+
}
|
|
146
|
+
const prompt = String(p.prompt ?? "").trim();
|
|
147
|
+
if (!prompt) {
|
|
148
|
+
return text(pick(getLang(), "触发指令(prompt)不能为空:写清楚触发时要检查什么、汇报什么。", "Prompt must not be empty: say what to check and report on fire.", "sched.task.empty.prompt"));
|
|
149
|
+
}
|
|
150
|
+
const cwd = String(host.cwd() ?? "").trim();
|
|
151
|
+
if (!cwd) {
|
|
152
|
+
return text(pick(getLang(), "当前没有工作目录,无法创建定时任务。", "No working directory, cannot create the scheduled task.", "sched.task.no.cwd"));
|
|
153
|
+
}
|
|
154
|
+
const labelRaw = typeof p.label === "string" ? p.label.trim().slice(0, 80) : "";
|
|
155
|
+
const name = labelRaw || prompt.split("\n")[0].slice(0, 40) || "定时任务";
|
|
156
|
+
const convId = (ownerConversationId ?? "").trim() || String(host.activeConversationId() ?? "").trim();
|
|
157
|
+
const recurring = p.recurring === true;
|
|
158
|
+
let task;
|
|
159
|
+
try {
|
|
160
|
+
task = store.upsert({
|
|
161
|
+
name,
|
|
162
|
+
cwd,
|
|
163
|
+
kind: parsed.kind,
|
|
164
|
+
spec: parsed.spec,
|
|
165
|
+
prompt,
|
|
166
|
+
conversationId: convId,
|
|
167
|
+
oneShot: !recurring,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
catch (err) {
|
|
171
|
+
return text(String(err?.message ?? err));
|
|
172
|
+
}
|
|
173
|
+
const L = getLang();
|
|
174
|
+
const next = computeNextFire(task, Date.now());
|
|
175
|
+
const head = L === "zh" ? `定时任务已创建:${task.id}(${task.name})` : `Scheduled task created: ${task.id} (${task.name})`;
|
|
176
|
+
const whenLine = L === "zh"
|
|
177
|
+
? `触发:${parsed.kind === "cron" ? `cron ${parsed.spec}` : describeIntervalMs(Number(parsed.spec))},下次 ${fmtTime(next, L)}${recurring ? "(周期)" : "(单次,触发后自动删除)"}`
|
|
178
|
+
: `Fires: ${parsed.kind === "cron" ? `cron ${parsed.spec}` : `every ${Math.round(Number(parsed.spec) / 1000)}s`}, next ${fmtTime(next, L)}${recurring ? " (recurring)" : " (one-shot, auto-deleted after firing)"}`;
|
|
179
|
+
const targetLine = convId
|
|
180
|
+
? L === "zh"
|
|
181
|
+
? `汇报:触发时自动唤醒本对话(${convId});对话已不在则无头执行,报告进调度面板历史。`
|
|
182
|
+
: `Reports: wakes this conversation (${convId}) on fire; runs headless (report in panel history) if it is gone.`
|
|
183
|
+
: L === "zh"
|
|
184
|
+
? "汇报:无头执行,报告进调度面板历史(创建时没拿到对话 id)。"
|
|
185
|
+
: "Reports: headless run, report in panel history (no conversation id captured at creation).";
|
|
186
|
+
const tail = L === "zh"
|
|
187
|
+
? `取消:schedule_cancel(id="${task.id}"),或后台任务面板手动取消;查看:schedule_list。`
|
|
188
|
+
: `Cancel: schedule_cancel(id="${task.id}"), or from the background-tasks panel; inspect: schedule_list.`;
|
|
189
|
+
return text(`${head}\n${whenLine}\n${targetLine}\n${tail}`, { task });
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
const listTool = defineTool({
|
|
193
|
+
name: SCHEDULE_LIST_TOOL_NAME,
|
|
194
|
+
label: "List scheduled tasks",
|
|
195
|
+
description: bilingual("List all built-in scheduled tasks (all projects): id, name, cron/interval, on/paused, one-shot, target conversation, next fire, last run. Use it to inspect before cancelling, or to answer the user about what is scheduled.", "列出全部内置定时任务(跨项目):id、名称、cron/间隔、启用/暂停、单次、目标对话、下次触发、上次结果。取消前先看,或回答用户“排了什么”时用。"),
|
|
196
|
+
promptSnippet: "list scheduled wake-up tasks",
|
|
197
|
+
parameters: Type.Object({}),
|
|
198
|
+
execute: async () => {
|
|
199
|
+
const store = host.store();
|
|
200
|
+
if (!store)
|
|
201
|
+
return noStore();
|
|
202
|
+
const L = getLang();
|
|
203
|
+
const tasks = store.list();
|
|
204
|
+
if (tasks.length === 0) {
|
|
205
|
+
return text(pick(getLang(), "当前没有定时任务。用 schedule_task 创建(cron 或 in 30m 这类延迟,最短 60s)。", "No scheduled tasks. Create one with schedule_task (cron or a delay like in 30m, minimum 60s).", "sched.list.empty"), { tasks: [] });
|
|
206
|
+
}
|
|
207
|
+
const lines = tasks.slice(0, 50).map((t) => taskLine(t, L));
|
|
208
|
+
const more = tasks.length > 50
|
|
209
|
+
? L === "zh"
|
|
210
|
+
? `\n…还有 ${tasks.length - 50} 条没列(去后台任务面板看全量)。`
|
|
211
|
+
: `\n…${tasks.length - 50} more not shown (see the background-tasks panel).`
|
|
212
|
+
: "";
|
|
213
|
+
const head = L === "zh" ? `定时任务(${tasks.length}):` : `Scheduled tasks (${tasks.length}):`;
|
|
214
|
+
return text(`${head}\n${lines.join("\n")}${more}`, { tasks });
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
const cancelTool = defineTool({
|
|
218
|
+
name: SCHEDULE_CANCEL_TOOL_NAME,
|
|
219
|
+
label: "Cancel a scheduled task",
|
|
220
|
+
description: bilingual("Delete a scheduled task by id (see schedule_list). Deleting stops all future fires; use it when the user says the schedule is no longer needed.", "按 id 删除定时任务(id 见 schedule_list)。删除后不再触发;用户说不用排了时用它。"),
|
|
221
|
+
promptSnippet: "cancel a scheduled task by id",
|
|
222
|
+
parameters: Type.Object({
|
|
223
|
+
id: Type.String({ description: "Task id from schedule_list." }),
|
|
224
|
+
}),
|
|
225
|
+
execute: async (_id, p) => {
|
|
226
|
+
const store = host.store();
|
|
227
|
+
if (!store)
|
|
228
|
+
return noStore();
|
|
229
|
+
const id = String(p.id ?? "").trim();
|
|
230
|
+
if (!id) {
|
|
231
|
+
return text(pick(getLang(), "要删哪个任务?给 id(先 schedule_list 查)。", "Which task? Give its id (see schedule_list first).", "sched.cancel.empty.id"));
|
|
232
|
+
}
|
|
233
|
+
if (!store.remove(id)) {
|
|
234
|
+
return text(pick(getLang(), `没有 id=${id} 的任务(可能已触发自删或被删过,用 schedule_list 看现有的)。`, `No task id=${id} (may have fired-and-deleted or been removed; see schedule_list).`, "sched.cancel.not.found", { id }));
|
|
235
|
+
}
|
|
236
|
+
return text(pick(getLang(), `定时任务 ${id} 已删除,不再触发。`, `Scheduled task ${id} deleted, will not fire again.`, "sched.cancel.ok", { id }), { id });
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
return [taskTool, listTool, cancelTool];
|
|
240
|
+
}
|
|
@@ -76,6 +76,9 @@ export function normalizeSchedulerInput(input, now = Date.now()) {
|
|
|
76
76
|
throw new Error("模型格式非法(应为 provider/id)");
|
|
77
77
|
const thinkingLevel = String(input.thinkingLevel ?? "").trim();
|
|
78
78
|
const catchUp = input.catchUp === "once" ? "once" : "skip";
|
|
79
|
+
const conversationId = String(input.conversationId ?? "")
|
|
80
|
+
.trim()
|
|
81
|
+
.slice(0, 128);
|
|
79
82
|
return {
|
|
80
83
|
id,
|
|
81
84
|
name,
|
|
@@ -88,6 +91,8 @@ export function normalizeSchedulerInput(input, now = Date.now()) {
|
|
|
88
91
|
model,
|
|
89
92
|
thinkingLevel,
|
|
90
93
|
catchUp,
|
|
94
|
+
conversationId,
|
|
95
|
+
oneShot: input.oneShot === true,
|
|
91
96
|
createdAt: now,
|
|
92
97
|
updatedAt: now,
|
|
93
98
|
};
|
|
@@ -224,6 +229,8 @@ export class SchedulerStore {
|
|
|
224
229
|
model: r.model,
|
|
225
230
|
thinkingLevel: r.thinkingLevel,
|
|
226
231
|
catchUp: r.catchUp,
|
|
232
|
+
conversationId: r.conversationId,
|
|
233
|
+
oneShot: r.oneShot,
|
|
227
234
|
});
|
|
228
235
|
task.createdAt = typeof r.createdAt === "number" && Number.isFinite(r.createdAt) ? r.createdAt : Date.now();
|
|
229
236
|
task.updatedAt =
|
|
@@ -280,6 +287,8 @@ export class SchedulerStore {
|
|
|
280
287
|
model: t.model,
|
|
281
288
|
thinkingLevel: t.thinkingLevel,
|
|
282
289
|
catchUp: t.catchUp,
|
|
290
|
+
conversationId: t.conversationId,
|
|
291
|
+
oneShot: t.oneShot,
|
|
283
292
|
createdAt: t.createdAt,
|
|
284
293
|
updatedAt: t.updatedAt,
|
|
285
294
|
nextFire: !t.enabled ? null : (this.nextFire.get(t.id) ?? computeNextFire(t, Math.max(now, t.updatedAt))),
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* 预设存取 + 何时需要 reload」,真正动 runtime 的 session.reload() 走宿主回调
|
|
7
7
|
* (reloadSession 里还会刷新斜杠命令目录)。
|
|
8
8
|
*/
|
|
9
|
-
import { existsSync, readdirSync } from "node:fs";
|
|
9
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
10
10
|
import { basename, dirname, join } from "node:path";
|
|
11
11
|
import { fileURLToPath } from "node:url";
|
|
12
12
|
import { extensionKey, normalizeDisabledPluginTools, normalizeRetryMaxAttempts, normalizeSkillList, normalizeUiLayout, } from "./client-state.js";
|
|
@@ -105,15 +105,65 @@ export class SettingsService {
|
|
|
105
105
|
}
|
|
106
106
|
return false;
|
|
107
107
|
}
|
|
108
|
+
/** 判断某个扩展 id 是否仍存在(loader 之外的兜底,供禁用残留清理用):
|
|
109
|
+
* npm: 包 → 任一 npm 根还有目录,或还列在任一 settings.json packages 里
|
|
110
|
+
* (只删了 node_modules 但配置还在 = 保守保留,等下次 loader 同步);
|
|
111
|
+
* 路径型 → 文件/目录任一存在。空串一律不存在。 */
|
|
112
|
+
extensionStillOnDisk(id) {
|
|
113
|
+
if (!id)
|
|
114
|
+
return false;
|
|
115
|
+
if (id.startsWith("npm:")) {
|
|
116
|
+
const pkg = id.slice(4);
|
|
117
|
+
if (!pkg)
|
|
118
|
+
return false;
|
|
119
|
+
const cwd = this.host.cwd();
|
|
120
|
+
const agentDir = this.host.agentDir();
|
|
121
|
+
for (const npmRoot of [join(agentDir, "npm", "node_modules"), join(cwd, ".pi", "npm", "node_modules")]) {
|
|
122
|
+
try {
|
|
123
|
+
if (existsSync(join(npmRoot, pkg)))
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
catch { }
|
|
127
|
+
}
|
|
128
|
+
return this.packageStillListed(pkg);
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
return existsSync(id);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/** 包名是否还列在全局/项目 settings.json 的 packages 里(文件缺失/坏 JSON 当没列)。 */
|
|
138
|
+
packageStillListed(pkg) {
|
|
139
|
+
const wanted = `npm:${pkg}`;
|
|
140
|
+
for (const file of [join(this.host.agentDir(), "settings.json"), join(this.host.cwd(), ".pi", "settings.json")]) {
|
|
141
|
+
try {
|
|
142
|
+
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
143
|
+
if (!Array.isArray(parsed?.packages))
|
|
144
|
+
continue;
|
|
145
|
+
for (const entry of parsed.packages) {
|
|
146
|
+
if (typeof entry !== "string")
|
|
147
|
+
continue;
|
|
148
|
+
if (entry === wanted || entry === pkg)
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch { }
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
108
156
|
push() {
|
|
109
157
|
const disabledSkills = new Set(this.settings.disabledSkills);
|
|
110
158
|
const reviewDisabledSkills = new Set(this.settings.reviewDisabledSkills);
|
|
111
159
|
const disabledExts = new Set(this.settings.disabledExtensions);
|
|
112
160
|
let loadedSkillNames = null;
|
|
161
|
+
let loadedExtNames = null;
|
|
113
162
|
try {
|
|
114
163
|
const loadedSkills = this.host.getSession().resourceLoader.getSkills().skills;
|
|
115
164
|
const loadedExts = this.host.getSession().resourceLoader.getExtensions().extensions;
|
|
116
165
|
loadedSkillNames = new Set(loadedSkills.map((s) => s.name));
|
|
166
|
+
loadedExtNames = new Set(loadedExts.map((e) => extensionKey(e)));
|
|
117
167
|
// Prune entries that no longer exist on disk AND aren't disabled
|
|
118
168
|
// (e.g. a skill/extension file was deleted). Disabled entries are
|
|
119
169
|
// kept so they can be re-enabled even when filtered out of the loader.
|
|
@@ -179,6 +229,18 @@ export class SettingsService {
|
|
|
179
229
|
// Disabled entries that still exist on disk are re-added (with the
|
|
180
230
|
// last-known description) so they can be re-enabled; entries whose
|
|
181
231
|
// source file was deleted are dropped instead of being resurrected.
|
|
232
|
+
// disabledExtensions 同理:已卸载(loader 里没有、磁盘/配置里也没有)的扩展
|
|
233
|
+
// 从禁用记录里剔除并持久化 —— 否则卸载过的扩展以“已禁用”灰条永生(issue #192)。
|
|
234
|
+
// loadedExtNames 非 null 即 session 就绪;取不到(catch 分支)时保守跳过。
|
|
235
|
+
if (loadedExtNames !== null) {
|
|
236
|
+
const staleExts = this.settings.disabledExtensions.filter((id) => !loadedExtNames.has(id) && !this.extensionStillOnDisk(id));
|
|
237
|
+
if (staleExts.length > 0) {
|
|
238
|
+
this.settings.disabledExtensions = this.settings.disabledExtensions.filter((n) => !staleExts.includes(n));
|
|
239
|
+
this.host.stateStore.saveSettings(this.host.clientId, {
|
|
240
|
+
disabledExtensions: this.settings.disabledExtensions,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
182
244
|
for (const name of this.settings.disabledSkills) {
|
|
183
245
|
if (this.knownSkills.has(name))
|
|
184
246
|
continue;
|
|
@@ -186,8 +248,12 @@ export class SettingsService {
|
|
|
186
248
|
continue;
|
|
187
249
|
this.knownSkills.set(name, { name, description: "", enabled: false });
|
|
188
250
|
}
|
|
251
|
+
// (上面的 stale 清理已把卸载项移出禁用记录;这里再按磁盘挡一次,
|
|
252
|
+
// session 未就绪跳过清理时也不复活幽灵。)
|
|
189
253
|
for (const id of this.settings.disabledExtensions) {
|
|
190
254
|
if (!this.knownExtensions.has(id)) {
|
|
255
|
+
if (!this.extensionStillOnDisk(id))
|
|
256
|
+
continue;
|
|
191
257
|
this.knownExtensions.set(id, {
|
|
192
258
|
id,
|
|
193
259
|
name: id.startsWith("npm:") ? id : basename(id),
|
|
@@ -630,6 +696,7 @@ export class SettingsService {
|
|
|
630
696
|
this.host.emit({ type: "notice", level: "info", text: "设置已应用", textEn: "Settings applied" });
|
|
631
697
|
}
|
|
632
698
|
catch (err) {
|
|
699
|
+
console.error(`[settings] apply reload failed (client ${this.host.clientId}):`, err);
|
|
633
700
|
this.host.emit({
|
|
634
701
|
type: "notice",
|
|
635
702
|
level: "error",
|
package/dist/server/terminals.js
CHANGED
|
@@ -660,6 +660,11 @@ export class TerminalManager {
|
|
|
660
660
|
this.workspaceRoot = workspaceRoot;
|
|
661
661
|
this.lang = lang;
|
|
662
662
|
}
|
|
663
|
+
/** 过户:对话整体搬到另一个 ClientSession 时,把输出/退出事件改发给新宿主。
|
|
664
|
+
* PTY 本体不动(进程不重启,输出缓冲保留),只换投递地址. */
|
|
665
|
+
rebindEmit(emit) {
|
|
666
|
+
this.emit = emit;
|
|
667
|
+
}
|
|
663
668
|
/** Start a plain interactive shell in the given directory. */
|
|
664
669
|
create(id, cwd, cols, rows, fallbackCwd, title, opts) {
|
|
665
670
|
const valid = this.validateId(id);
|
|
@@ -735,12 +740,7 @@ export class TerminalManager {
|
|
|
735
740
|
if (!existing.exited) {
|
|
736
741
|
this.flushPending(existing);
|
|
737
742
|
existing.exited = true;
|
|
738
|
-
|
|
739
|
-
existing.pty.kill();
|
|
740
|
-
}
|
|
741
|
-
catch {
|
|
742
|
-
// already dead
|
|
743
|
-
}
|
|
743
|
+
this.killNative(existing);
|
|
744
744
|
}
|
|
745
745
|
cols = existing.cols || cols;
|
|
746
746
|
rows = existing.rows || rows;
|
|
@@ -1333,12 +1333,7 @@ export class TerminalManager {
|
|
|
1333
1333
|
w.cb(null);
|
|
1334
1334
|
this.flushPending(entry);
|
|
1335
1335
|
entry.exited = true;
|
|
1336
|
-
|
|
1337
|
-
entry.pty.kill();
|
|
1338
|
-
}
|
|
1339
|
-
catch {
|
|
1340
|
-
// already dead
|
|
1341
|
-
}
|
|
1336
|
+
this.killNative(entry);
|
|
1342
1337
|
this.terms.delete(id);
|
|
1343
1338
|
this.emit({ type: "terminal_exit", terminalId: id, exitCode: null });
|
|
1344
1339
|
this.emitList();
|
|
@@ -1358,19 +1353,54 @@ export class TerminalManager {
|
|
|
1358
1353
|
entry.title = trimmed;
|
|
1359
1354
|
this.emitList();
|
|
1360
1355
|
}
|
|
1361
|
-
/**
|
|
1362
|
-
|
|
1356
|
+
/**
|
|
1357
|
+
* Kill the native PTY (issue #215:Windows ConPTY 关机死锁)。
|
|
1358
|
+
*
|
|
1359
|
+
* Windows 下 `pty.kill()` 底层走 `ClosePseudoConsole`,管道有未排空数据时会
|
|
1360
|
+
* 内核级同步死锁,直接冻住单线程事件循环(进程内 setTimeout 看门狗与二次
|
|
1361
|
+
* Ctrl+C 全灭)。所以 Windows 先走 Node 原生的 `process.kill(pid)`
|
|
1362
|
+
*(TerminateProcess,从不阻塞)把 shell 干掉:
|
|
1363
|
+
* - 关机(shutdown=true):到此为止,不再调 `pty.kill()`。进程马上就
|
|
1364
|
+
* `process.exit`,HPC 句柄由 OS 回收;再调只会自找死锁。
|
|
1365
|
+
* - 日常(tab 关闭/移出对话):子进程已死、管道见 EOF 后再调 `pty.kill()`
|
|
1366
|
+
* 释放 HPC 句柄,此时不再阻塞(长驻服务必须释放句柄,不能像关机那样一走了之)。
|
|
1367
|
+
* 非 Windows 原样直调 `pty.kill()`。
|
|
1368
|
+
*/
|
|
1369
|
+
killNative(entry, shutdown = false) {
|
|
1370
|
+
if (process.platform === "win32") {
|
|
1371
|
+
const pid = entry.pty.pid;
|
|
1372
|
+
if (typeof pid === "number") {
|
|
1373
|
+
try {
|
|
1374
|
+
process.kill(pid);
|
|
1375
|
+
}
|
|
1376
|
+
catch {
|
|
1377
|
+
// already dead
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
if (shutdown)
|
|
1381
|
+
return;
|
|
1382
|
+
}
|
|
1383
|
+
try {
|
|
1384
|
+
entry.pty.kill();
|
|
1385
|
+
}
|
|
1386
|
+
catch {
|
|
1387
|
+
// already dead
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
/**
|
|
1391
|
+
* Kill every terminal owned by this conversation.
|
|
1392
|
+
* @param opts.shutdown=true = 进程关机路径(disposeAll):Windows 下跳过
|
|
1393
|
+
* `pty.kill()`(见 killNative),只做 TerminateProcess + 状态清理,50ms 内退出。
|
|
1394
|
+
* 日常移出对话走默认 false,必须释放 HPC 句柄。
|
|
1395
|
+
*/
|
|
1396
|
+
killAll(opts) {
|
|
1397
|
+
const shutdown = opts?.shutdown === true;
|
|
1363
1398
|
for (const entry of this.terms.values()) {
|
|
1364
1399
|
this.disarmIdleWatch(entry);
|
|
1365
1400
|
if (entry.exited)
|
|
1366
1401
|
continue;
|
|
1367
1402
|
entry.exited = true;
|
|
1368
|
-
|
|
1369
|
-
entry.pty.kill();
|
|
1370
|
-
}
|
|
1371
|
-
catch {
|
|
1372
|
-
// already dead
|
|
1373
|
-
}
|
|
1403
|
+
this.killNative(entry, shutdown);
|
|
1374
1404
|
}
|
|
1375
1405
|
for (const entry of this.terms.values()) {
|
|
1376
1406
|
for (const wake of entry.waiters)
|
|
@@ -50,9 +50,14 @@ export const BROWSER_PAGE_TOOL_NAME = "browser_page";
|
|
|
50
50
|
/** 别的对话读取工具(定义见 conversation-read-tool.ts):运行中对话(含子代理)+
|
|
51
51
|
* 历史会话转录,只读。 */
|
|
52
52
|
export const CONVERSATION_READ_TOOL_NAME = "conversation_read";
|
|
53
|
+
/** 定时任务工具(定义见 schedule-agent-tool.ts):创建/查看/取消内置调度任务,
|
|
54
|
+
* 到期自动唤醒发起对话执行 prompt 并汇报。 */
|
|
55
|
+
export const SCHEDULE_TASK_TOOL_NAME = "schedule_task";
|
|
56
|
+
export const SCHEDULE_LIST_TOOL_NAME = "schedule_list";
|
|
57
|
+
export const SCHEDULE_CANCEL_TOOL_NAME = "schedule_cancel";
|
|
53
58
|
/** 旧工具名(持久化迁移用;新代码一律用 MARKERS_LIST_TOOL_NAME)。 */
|
|
54
59
|
export const LEGACY_MARKERS_LIST_TOOL_NAME = "markers_list";
|
|
55
|
-
/** 可开关的 Agent 工具总目录(共
|
|
60
|
+
/** 可开关的 Agent 工具总目录(共 23 个;bash 本体与 SDK 内置 edit/read
|
|
56
61
|
* 不进目录——关了 agent 就残了,不给关)。 */
|
|
57
62
|
export const AGENT_TOOL_CATALOG = [
|
|
58
63
|
...TERMINAL_TOOL_NAMES.map((name) => ({
|
|
@@ -76,6 +81,11 @@ export const AGENT_TOOL_CATALOG = [
|
|
|
76
81
|
{ name: BROWSER_PAGE_TOOL_NAME, group: "other", defaultOn: false, dshVisible: false },
|
|
77
82
|
// 只读别的对话(含子代理实时消息与历史转录),默认开;DSH 引擎没有该 customTool。
|
|
78
83
|
{ name: CONVERSATION_READ_TOOL_NAME, group: "other", defaultOn: true, dshVisible: false },
|
|
84
|
+
// 定时/延时唤醒:默认开(不打开 AI 根本不知道能定时;60s 间隔底线+面板可随时取消),
|
|
85
|
+
// DSH 引擎没有该 customTool(走 goal-rpc,无 customTool 注册面)。
|
|
86
|
+
{ name: SCHEDULE_TASK_TOOL_NAME, group: "other", defaultOn: true, dshVisible: false },
|
|
87
|
+
{ name: SCHEDULE_LIST_TOOL_NAME, group: "other", defaultOn: true, dshVisible: false },
|
|
88
|
+
{ name: SCHEDULE_CANCEL_TOOL_NAME, group: "other", defaultOn: true, dshVisible: false },
|
|
79
89
|
];
|
|
80
90
|
const KNOWN_NAMES = new Set(AGENT_TOOL_CATALOG.map((t) => t.name));
|
|
81
91
|
/** 是否为本表登记的可开关工具(未知名一律 false,不抛错)。 */
|
|
@@ -265,12 +265,16 @@ function readSettingsPackagesFile(file) {
|
|
|
265
265
|
function readGitCloneNameVersion(installDir) {
|
|
266
266
|
try {
|
|
267
267
|
const pkg = JSON.parse(readFileSync(join(installDir, "package.json"), "utf8"));
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
268
|
+
// Name and version fall back independently: a clone with a version but
|
|
269
|
+
// no usable name still reports its version (name falls back to the
|
|
270
|
+
// source shorthand at the call site), and vice versa.
|
|
271
|
+
return {
|
|
272
|
+
...(typeof pkg.name === "string" && pkg.name ? { name: pkg.name } : {}),
|
|
273
|
+
...(typeof pkg.version === "string" && pkg.version ? { version: pkg.version } : {}),
|
|
274
|
+
};
|
|
271
275
|
}
|
|
272
276
|
catch {
|
|
273
|
-
return
|
|
277
|
+
return {};
|
|
274
278
|
}
|
|
275
279
|
}
|
|
276
280
|
/**
|
|
@@ -301,8 +305,8 @@ export function listGitExtensions(agentDir, projectCwd, env = process.env) {
|
|
|
301
305
|
const installDir = join(gitRoot, parsed.host, ...parsed.path.split("/"));
|
|
302
306
|
const nv = readGitCloneNameVersion(installDir);
|
|
303
307
|
out.push({
|
|
304
|
-
name: nv
|
|
305
|
-
version: nv
|
|
308
|
+
name: nv.name ?? parsed.shorthand,
|
|
309
|
+
version: nv.version ?? "?",
|
|
306
310
|
kind: "git-extension",
|
|
307
311
|
source: parsed.shorthand,
|
|
308
312
|
installDir,
|
|
@@ -104,6 +104,10 @@ export class WebUIContext {
|
|
|
104
104
|
}
|
|
105
105
|
this.push();
|
|
106
106
|
};
|
|
107
|
+
/** Whether any widgets are mounted — the 2s poll skips refresh when false. */
|
|
108
|
+
hasWidgets() {
|
|
109
|
+
return this.widgets.size > 0;
|
|
110
|
+
}
|
|
107
111
|
/** Re-render all widgets and push when content changed (polled + on demand). */
|
|
108
112
|
refresh() {
|
|
109
113
|
let changed = false;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unknown inbound WS message-type counter —— 独立成模块是为了可单测
|
|
3
|
+
* (`server/index.ts` 起 WS + SDK,不适合在单测里 import)。
|
|
4
|
+
*
|
|
5
|
+
* 背景:`dispatch` 的 `switch (msg.type)` 的 `default:` 分支原来是空的,
|
|
6
|
+
* 版本错位(旧前端/新服务端)或插件发错 type 时静默丢弃,只能抓包排查。
|
|
7
|
+
* 这里只做计数 + 节流 warn,不改变任何已知类型的行为。
|
|
8
|
+
*/
|
|
9
|
+
/** 同一 type 两次 warn 之间最小间隔(防刷屏),可单测覆盖。 */
|
|
10
|
+
export const UNKNOWN_WS_WARN_INTERVAL_MS = 60_000;
|
|
11
|
+
/** 未知 type 种类上限:type 字符串远端可控,无上限则内存无界增长。
|
|
12
|
+
* 超限后的新 type 不再建 key,只进溢出计数。 */
|
|
13
|
+
export const MAX_UNKNOWN_WS_TYPES = 500;
|
|
14
|
+
const counts = new Map();
|
|
15
|
+
const lastWarnAt = new Map();
|
|
16
|
+
let overflowed = 0;
|
|
17
|
+
/**
|
|
18
|
+
* 记录一次未知 type,计数永远 +1;同一 type 在 `UNKNOWN_WS_WARN_INTERVAL_MS`
|
|
19
|
+
* 内只返回一次 `warn: true`(调用方决定是否 `console.warn`)。
|
|
20
|
+
*/
|
|
21
|
+
export function recordUnknownWsType(type, now = Date.now()) {
|
|
22
|
+
if (!counts.has(type) && counts.size >= MAX_UNKNOWN_WS_TYPES) {
|
|
23
|
+
overflowed++;
|
|
24
|
+
return { count: overflowed, warn: false };
|
|
25
|
+
}
|
|
26
|
+
const count = (counts.get(type) ?? 0) + 1;
|
|
27
|
+
counts.set(type, count);
|
|
28
|
+
const last = lastWarnAt.get(type) ?? -Infinity;
|
|
29
|
+
if (now - last >= UNKNOWN_WS_WARN_INTERVAL_MS) {
|
|
30
|
+
lastWarnAt.set(type, now);
|
|
31
|
+
return { count, warn: true };
|
|
32
|
+
}
|
|
33
|
+
return { count, warn: false };
|
|
34
|
+
}
|
|
35
|
+
/** 只读快照(排障用,返回拷贝,调用方改不动内部表)。 */
|
|
36
|
+
export function unknownWsCounts() {
|
|
37
|
+
return new Map(counts);
|
|
38
|
+
}
|
|
39
|
+
/** 单测隔离用。 */
|
|
40
|
+
export function resetUnknownWsTypes() {
|
|
41
|
+
counts.clear();
|
|
42
|
+
lastWarnAt.clear();
|
|
43
|
+
overflowed = 0;
|
|
44
|
+
}
|