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
|
@@ -26,7 +26,7 @@ import { Type } from "typebox";
|
|
|
26
26
|
import { BgServerTracker } from "./bg-servers.js";
|
|
27
27
|
import { checkAll as checkAllUpdates, collectTargets, compareVersions as compareSemver, resolveNpmRegistry, sortUpdateItems, } from "./update-check.js";
|
|
28
28
|
import { hasActiveSubagentRun, hasPendingWaitSubscription, shouldRetainActive } from "./wait-subscription-scan.js";
|
|
29
|
-
import {
|
|
29
|
+
import { removeQueuedByIndexOrText } from "./queue-utils.js";
|
|
30
30
|
import { syncPluginToolsIntoSession } from "./plugins.js";
|
|
31
31
|
import { SettingsService } from "./settings-service.js";
|
|
32
32
|
import { GoalService } from "./goal-service.js";
|
|
@@ -46,6 +46,7 @@ import { makeEditSoftTool } from "./edit-soft-tool.js";
|
|
|
46
46
|
import { collectSubagentDescendantIds, makeSubagentTools, subagentTitle, withSubagentOwner, } from "./subagents.js";
|
|
47
47
|
import { makeDelegateTaskTool } from "./delegate-task.js";
|
|
48
48
|
import { makeConversationReadTool, parseTranscriptLines, toTranscriptInput, } from "./conversation-read-tool.js";
|
|
49
|
+
import { makeScheduleTools } from "./schedule-agent-tool.js";
|
|
49
50
|
import { buildAttachmentMessages, parseModelSpec } from "./attachments.js";
|
|
50
51
|
import { buildVisionBridgePrompt, findVisionModels, transcribeImages } from "./vision-bridge.js";
|
|
51
52
|
import { BUILTIN_SOUL, DEFAULT_PROMPT_TEMPLATE, buildToolsSchemaText, renderPromptTemplate, resolveSectionTexts, } from "./prompt-composer.js";
|
|
@@ -774,6 +775,35 @@ function collectSessionAnchors(filePath, q, cap = 10) {
|
|
|
774
775
|
export function piSessionsRoot() {
|
|
775
776
|
return process.env.PI_CODING_AGENT_SESSION_DIR || undefined;
|
|
776
777
|
}
|
|
778
|
+
/** Guardrail: only transcripts under the shared sessions root
|
|
779
|
+
* (<agentDir>/sessions/) may be opened/deleted/renamed — never arbitrary files.
|
|
780
|
+
* Shared by deleteSession/renameSession/switchSession so the open path cannot
|
|
781
|
+
* escape the confinement the write paths already enforce. */
|
|
782
|
+
export function isInsideSessionsDir(agentDir, targetPath) {
|
|
783
|
+
const abs = resolve(targetPath);
|
|
784
|
+
const sessionsRoot = resolve(agentDir, "sessions");
|
|
785
|
+
return abs.startsWith(sessionsRoot + sep);
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* 选一个断开的残留会话给新标签认领(纯函数):
|
|
789
|
+
* - 还有别的在线浏览器(非伪客户端且 live)→ 不认领(新标签是第二块屏,
|
|
790
|
+
* issue #10 的隔离必须保留,跑着的对话继续走 elsewhere 只读感知)。
|
|
791
|
+
* - 否则在断开 + 非伪 + 有内容的候选中按(streaming 多 → 最近活跃)取最优;
|
|
792
|
+
* 没有返回 null(调用方走正常新建流程)。
|
|
793
|
+
*/
|
|
794
|
+
export function pickAdoptableOrphan(cands) {
|
|
795
|
+
if (cands.some((c) => !c.pseudo && c.live))
|
|
796
|
+
return null;
|
|
797
|
+
let best = null;
|
|
798
|
+
for (const c of cands) {
|
|
799
|
+
if (c.pseudo || c.live || !c.adoptable)
|
|
800
|
+
continue;
|
|
801
|
+
if (!best || c.streaming > best.streaming || (c.streaming === best.streaming && c.activity > best.activity)) {
|
|
802
|
+
best = c;
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
return best?.id ?? null;
|
|
806
|
+
}
|
|
777
807
|
export class ClientSession {
|
|
778
808
|
clientId;
|
|
779
809
|
/** Set by AgentService.attach: reflects the SERVICE-wide quiesce flag
|
|
@@ -846,6 +876,8 @@ export class ClientSession {
|
|
|
846
876
|
onConversationChanged = undefined;
|
|
847
877
|
/** index.ts 注入:读取插件当前注册的 AI 工具(attach 时拷贝到每个新会话)。 */
|
|
848
878
|
pluginToolsProvider = undefined;
|
|
879
|
+
/** index.ts 经 AgentService 注入:内置调度存储(定时任务 Agent 工具用;未注入时工具直接报错)。 */
|
|
880
|
+
schedulerStore = undefined;
|
|
849
881
|
/** index.ts 注入:读取插件当前注册的斜杠命令(目录展示 + prompt 拦截执行)。 */
|
|
850
882
|
pluginCommandsProvider = undefined;
|
|
851
883
|
/** index.ts 注入:读取插件注册的常驻后台任务(并入 bg_servers 面板)。 */
|
|
@@ -1332,6 +1364,14 @@ export class ClientSession {
|
|
|
1332
1364
|
return !!t && t.enabled;
|
|
1333
1365
|
},
|
|
1334
1366
|
};
|
|
1367
|
+
/** schedule_* 工具的数据宿主:全局调度存储+创建时刻 live 的 cwd/活动对话。 */
|
|
1368
|
+
scheduleToolHost() {
|
|
1369
|
+
return {
|
|
1370
|
+
store: () => this.schedulerStore,
|
|
1371
|
+
cwd: () => this.cwd,
|
|
1372
|
+
activeConversationId: () => this.activeId,
|
|
1373
|
+
};
|
|
1374
|
+
}
|
|
1335
1375
|
/** conversation_read 工具的数据宿主:读本客户端的 conversation 体系 +
|
|
1336
1376
|
* 落盘会话目录。运行中对话按 id(实时消息,含未落盘的);历史按 path,
|
|
1337
1377
|
* 且必须是会话列表里的路径(任意文件不给读)。跨标签页的实时运行不在
|
|
@@ -1826,6 +1866,11 @@ export class ClientSession {
|
|
|
1826
1866
|
// 会话同样注册了它,可自然嵌套读取。不需要 ownerId——读的是本
|
|
1827
1867
|
// 客户端的 conversation 体系与落盘历史,与派发者无关。
|
|
1828
1868
|
makeConversationReadTool(this.conversationReadHost(), () => this.getLang()),
|
|
1869
|
+
// 定时唤醒三件套(schedule_task/list/cancel,issue #193):默认绑定
|
|
1870
|
+
// 发起对话(ownerId,无则活动对话),到期 steer 语义唤醒它;子代理
|
|
1871
|
+
// 会话同样注册(owner 即真正派发的父对话)。开关走统一工具 tab。
|
|
1872
|
+
// DSH 引擎无 customTool 注册面,不接。
|
|
1873
|
+
...makeScheduleTools(this.scheduleToolHost(), ownerId, () => this.getLang()),
|
|
1829
1874
|
],
|
|
1830
1875
|
});
|
|
1831
1876
|
// 终端工具开关从创建起就生效(工具始终注册进注册表,只调活跃集)。
|
|
@@ -2035,12 +2080,13 @@ export class ClientSession {
|
|
|
2035
2080
|
this.startWidgetsTimer();
|
|
2036
2081
|
this.startStallTimer();
|
|
2037
2082
|
}
|
|
2038
|
-
/** Poll extension widgets so TUI-only overlays (e.g. rpiv-todo) stay live.
|
|
2083
|
+
/** Poll extension widgets so TUI-only overlays (e.g. rpiv-todo) stay live.
|
|
2084
|
+
* Skips the refresh when no widgets are mounted — the common case. */
|
|
2039
2085
|
startWidgetsTimer() {
|
|
2040
2086
|
if (this.widgetsTimer)
|
|
2041
2087
|
return;
|
|
2042
2088
|
this.widgetsTimer = setInterval(() => {
|
|
2043
|
-
if (!this.disposed)
|
|
2089
|
+
if (!this.disposed && this.webUi.hasWidgets())
|
|
2044
2090
|
this.webUi.refresh();
|
|
2045
2091
|
}, WIDGET_REFRESH_MS);
|
|
2046
2092
|
}
|
|
@@ -2077,6 +2123,11 @@ export class ClientSession {
|
|
|
2077
2123
|
* TOOL_WATCHDOG_TIMEOUT_MS, abort the session instead of letting the
|
|
2078
2124
|
* conversation hang forever (the SDK bash tool has no default timeout). */
|
|
2079
2125
|
armToolWatchdog(conv, toolCallId) {
|
|
2126
|
+
this.rearmToolWatchdog(conv, toolCallId, TOOL_WATCHDOG_TIMEOUT_MS);
|
|
2127
|
+
}
|
|
2128
|
+
/** (重)布工具挂死看门狗:delayMs 后仍在跑则 abort 整轮。过户时用剩余时间重布
|
|
2129
|
+
* (已逾期的立即触发,语义不变). */
|
|
2130
|
+
rearmToolWatchdog(conv, toolCallId, delayMs) {
|
|
2080
2131
|
const t = setTimeout(() => {
|
|
2081
2132
|
conv.toolWatchdogs.delete(toolCallId);
|
|
2082
2133
|
// The tool finished before the deadline — nothing to do.
|
|
@@ -2094,7 +2145,7 @@ export class ClientSession {
|
|
|
2094
2145
|
// clears any goal / review loop. interruptRun adds a force-reset
|
|
2095
2146
|
// fallback in case the model stream ignores the abort signal.
|
|
2096
2147
|
void this.interruptRun(conv, "工具执行超时");
|
|
2097
|
-
},
|
|
2148
|
+
}, Math.max(0, delayMs));
|
|
2098
2149
|
t.unref?.();
|
|
2099
2150
|
conv.toolWatchdogs.set(toolCallId, t);
|
|
2100
2151
|
}
|
|
@@ -3039,6 +3090,8 @@ export class ClientSession {
|
|
|
3039
3090
|
}
|
|
3040
3091
|
const id = `q-${++this.questionSeq}`;
|
|
3041
3092
|
this.pendingQuestions.set(id, { resolve, questions, conversationId });
|
|
3093
|
+
// 运行列表的「?」角标靠 conversations 推送(问卷登记/解决不经过快照通道)。
|
|
3094
|
+
this.emitConversations();
|
|
3042
3095
|
this.emit({
|
|
3043
3096
|
type: "question_pending",
|
|
3044
3097
|
id,
|
|
@@ -3047,14 +3100,20 @@ export class ClientSession {
|
|
|
3047
3100
|
});
|
|
3048
3101
|
}
|
|
3049
3102
|
/** 前端回答模型提问(question_answer → 恢复 askUser 的 Promise)。id 需匹配
|
|
3050
|
-
* pendingQuestions
|
|
3051
|
-
*
|
|
3103
|
+
* pendingQuestions 中键;未匹配(对方刚回答/取消、页面刷新重发)静默忽略。
|
|
3104
|
+
* 成功 resolve 后同步推 question_retracted + conversations:本页 live 对话框
|
|
3105
|
+
* 靠前者收起(跨页作答时源页就靠它),别处的「?」角标靠后者即时消失。
|
|
3106
|
+
* 返回是否真的恢复了一个挂起提问(跨页作答的送达回执用)。 */
|
|
3052
3107
|
resolveQuestion(id, answers, cancelled) {
|
|
3053
3108
|
const pending = this.pendingQuestions.get(id);
|
|
3054
|
-
if (pending)
|
|
3055
|
-
|
|
3056
|
-
|
|
3057
|
-
|
|
3109
|
+
if (!pending)
|
|
3110
|
+
return false;
|
|
3111
|
+
this.pendingQuestions.delete(id);
|
|
3112
|
+
pending.resolve(cancelled ? null : answers);
|
|
3113
|
+
this.emit({ type: "question_retracted", id });
|
|
3114
|
+
// 同上:角标消失也要即时推送(否则要等到 run 结束别处才知道问完了)。
|
|
3115
|
+
this.emitConversations();
|
|
3116
|
+
return true;
|
|
3058
3117
|
}
|
|
3059
3118
|
/** 快照侧的待答提问(UiState.pendingQuestion):只带当前对话的问卷——切回
|
|
3060
3119
|
* 原对话会重推快照,对话框随之回来(重连/刷新/第二标签页的恢复通道)。 */
|
|
@@ -3080,6 +3139,18 @@ export class ClientSession {
|
|
|
3080
3139
|
this.resolveQuestion(id, answers, cancelled);
|
|
3081
3140
|
return Promise.resolve();
|
|
3082
3141
|
}
|
|
3142
|
+
/** 取某对话的等答复问卷原文(跨页作答的 peek 用;只读,不改变状态)。 */
|
|
3143
|
+
peekPendingQuestion(convId) {
|
|
3144
|
+
for (const [id, p] of this.pendingQuestions) {
|
|
3145
|
+
if (p.conversationId === convId)
|
|
3146
|
+
return { id, questions: p.questions };
|
|
3147
|
+
}
|
|
3148
|
+
return undefined;
|
|
3149
|
+
}
|
|
3150
|
+
/** 跨页作答:把别处问卷的原文推给本页弹框(回答经 answerElsewhereQuestion 回去)。 */
|
|
3151
|
+
pushElsewhereQuestion(owner, convId, q) {
|
|
3152
|
+
this.emit({ type: "elsewhere_question", owner, convId, id: q.id, questions: q.questions });
|
|
3153
|
+
}
|
|
3083
3154
|
/** 关闭所有挂起提问(dispose 时清理):以「取消」解析,避免模型挂死。 */
|
|
3084
3155
|
cancelPendingQuestions() {
|
|
3085
3156
|
for (const [, p] of this.pendingQuestions) {
|
|
@@ -3148,6 +3219,20 @@ export class ClientSession {
|
|
|
3148
3219
|
return { reason: `视觉桥转写失败:${err instanceof Error ? err.message : String(err)}` };
|
|
3149
3220
|
}
|
|
3150
3221
|
}
|
|
3222
|
+
/** 页调用超时计时器:到点按失败 resolve(晚到的 page_response 在
|
|
3223
|
+
* resolvePageCall 里找不到 id 会静默忽略)。过户重建时复用(计时重走). */
|
|
3224
|
+
armPageCallTimeout(id, resolve, timeoutMs) {
|
|
3225
|
+
return setTimeout(() => {
|
|
3226
|
+
// 到点:先删再 resolve——晚到的 page_response 在 resolvePageCall 里找
|
|
3227
|
+
// 找不到 id,会静默忽略(见那里的注释)。
|
|
3228
|
+
if (this.pendingPageCalls.delete(id)) {
|
|
3229
|
+
resolve({
|
|
3230
|
+
ok: false,
|
|
3231
|
+
error: `${Math.round(timeoutMs / 1000)} 秒内没有收到浏览器响应(timeout ${timeoutMs}ms)。请确认 pi-web-ui 页面已打开且 page-picker 扩展已启用。`,
|
|
3232
|
+
});
|
|
3233
|
+
}
|
|
3234
|
+
}, timeoutMs);
|
|
3235
|
+
}
|
|
3151
3236
|
pageCall(req, sig, conversationId) {
|
|
3152
3237
|
return new Promise((resolve) => {
|
|
3153
3238
|
if (sig?.aborted || this.disposed) {
|
|
@@ -3164,18 +3249,9 @@ export class ClientSession {
|
|
|
3164
3249
|
const id = `p-${++this.pageSeq}`;
|
|
3165
3250
|
// 夹取与工具入口同一套规则(防手写脏值/其它调用方绕过 schema)。
|
|
3166
3251
|
const timeoutMs = normalizePageCallTimeoutMs(req.timeoutMs);
|
|
3167
|
-
const timer =
|
|
3168
|
-
// 到点:先删再 resolve——晚到的 page_response 在 resolvePageCall 里找
|
|
3169
|
-
// 找不到 id,会静默忽略(见那里的注释)。
|
|
3170
|
-
if (this.pendingPageCalls.delete(id)) {
|
|
3171
|
-
resolve({
|
|
3172
|
-
ok: false,
|
|
3173
|
-
error: `${Math.round(timeoutMs / 1000)} 秒内没有收到浏览器响应(timeout ${timeoutMs}ms)。请确认 pi-web-ui 页面已打开且 page-picker 扩展已启用。`,
|
|
3174
|
-
});
|
|
3175
|
-
}
|
|
3176
|
-
}, timeoutMs);
|
|
3252
|
+
const timer = this.armPageCallTimeout(id, resolve, timeoutMs);
|
|
3177
3253
|
// 先登记再发:同步回包(同进程假客户端)也不能漏掉。
|
|
3178
|
-
this.pendingPageCalls.set(id, { resolve, timer, conversationId });
|
|
3254
|
+
this.pendingPageCalls.set(id, { resolve, timer, conversationId, req, timeoutMs });
|
|
3179
3255
|
this.emit({ type: "page_request", id, op: req.op, args: req.args, target: req.target, timeoutMs });
|
|
3180
3256
|
});
|
|
3181
3257
|
}
|
|
@@ -3795,7 +3871,15 @@ export class ClientSession {
|
|
|
3795
3871
|
await this.pushSlashCommands();
|
|
3796
3872
|
this.pushSettings();
|
|
3797
3873
|
}
|
|
3798
|
-
catch {
|
|
3874
|
+
catch (err) {
|
|
3875
|
+
console.error(`[settings] marker reload failed (conv ${this.activeId}):`, err);
|
|
3876
|
+
this.emit({
|
|
3877
|
+
type: "notice",
|
|
3878
|
+
level: "error",
|
|
3879
|
+
text: `设置应用失败:${err.message}`,
|
|
3880
|
+
textEn: `Failed to apply settings: ${err.message}`,
|
|
3881
|
+
});
|
|
3882
|
+
}
|
|
3799
3883
|
}
|
|
3800
3884
|
}
|
|
3801
3885
|
}
|
|
@@ -3985,7 +4069,8 @@ export class ClientSession {
|
|
|
3985
4069
|
}
|
|
3986
4070
|
return out;
|
|
3987
4071
|
}
|
|
3988
|
-
/** issue #145:本实例所有正在跑的对话摘要(elsewhere
|
|
4072
|
+
/** issue #145:本实例所有正在跑的对话摘要(elsewhere 列表的本机一半 +
|
|
4073
|
+
* 手动过户的目标定位:convId + 是否有等答复问卷)。 */
|
|
3989
4074
|
streamingSummariesAll() {
|
|
3990
4075
|
const out = [];
|
|
3991
4076
|
for (const conv of this.convs.values()) {
|
|
@@ -3993,10 +4078,62 @@ export class ClientSession {
|
|
|
3993
4078
|
continue;
|
|
3994
4079
|
if (!this.conversationStreaming(conv))
|
|
3995
4080
|
continue;
|
|
3996
|
-
out.push({
|
|
4081
|
+
out.push({
|
|
4082
|
+
title: conv.title,
|
|
4083
|
+
cwd: conv.cwd,
|
|
4084
|
+
isStreaming: true,
|
|
4085
|
+
convId: conv.id,
|
|
4086
|
+
hasQuestion: this.isWaitingOnUser(conv.id),
|
|
4087
|
+
});
|
|
3997
4088
|
}
|
|
3998
4089
|
return out;
|
|
3999
4090
|
}
|
|
4091
|
+
/**
|
|
4092
|
+
* 浏览器重启认领用:本会话是否有值得新标签接管的内容。
|
|
4093
|
+
* 跑着的(主对话/子代理都算)、后台挂着的(listed)、有消息历史的都算;
|
|
4094
|
+
* 纯空白会话(刚建就关了标签)不算 —— 认领它与新建无异,不如走新建流程。
|
|
4095
|
+
* 零 token 冒烟测试的残留会话永远是空白的,因此认领逻辑不会改变它们的行为。
|
|
4096
|
+
*/
|
|
4097
|
+
hasAdoptableContent() {
|
|
4098
|
+
for (const c of this.convs.values()) {
|
|
4099
|
+
try {
|
|
4100
|
+
if (c.session.isStreaming)
|
|
4101
|
+
return true;
|
|
4102
|
+
}
|
|
4103
|
+
catch {
|
|
4104
|
+
// 会话替换中 —— 按未跑处理
|
|
4105
|
+
}
|
|
4106
|
+
if (c.isSubagent)
|
|
4107
|
+
continue;
|
|
4108
|
+
if (c.listed)
|
|
4109
|
+
return true;
|
|
4110
|
+
try {
|
|
4111
|
+
if (c.session.getSessionStats().totalMessages > 0)
|
|
4112
|
+
return true;
|
|
4113
|
+
}
|
|
4114
|
+
catch {
|
|
4115
|
+
// 会话替换中 —— 按无消息处理
|
|
4116
|
+
}
|
|
4117
|
+
}
|
|
4118
|
+
return false;
|
|
4119
|
+
}
|
|
4120
|
+
/** 各对话最近活跃时间的最大值(认领时多个残留按此排序,新的优先)。 */
|
|
4121
|
+
latestActivity() {
|
|
4122
|
+
let at = 0;
|
|
4123
|
+
for (const c of this.convs.values()) {
|
|
4124
|
+
at = Math.max(at, c.lastActiveAt || 0, c.lastSdkEventAt || 0);
|
|
4125
|
+
}
|
|
4126
|
+
return at;
|
|
4127
|
+
}
|
|
4128
|
+
/** 被新标签认领后首帧即被告之(pendingNotices 随 attachSink 下发)。 */
|
|
4129
|
+
noteAdopted() {
|
|
4130
|
+
this.pendingNotices.push({
|
|
4131
|
+
type: "notice",
|
|
4132
|
+
level: "info",
|
|
4133
|
+
text: "已恢复你关闭浏览器前的工作会话(含运行中的对话),可直接继续查看与操作。",
|
|
4134
|
+
textEn: "Restored the workspace session from before the browser was closed, including its running conversations — pick up right where you left off.",
|
|
4135
|
+
});
|
|
4136
|
+
}
|
|
4000
4137
|
/** issue #145:让其他客户端重推 conversations(elsewhere 刷新用;
|
|
4001
4138
|
* 流式集合签名驱动,外层循环安全)。 */
|
|
4002
4139
|
refreshExternalRunning() {
|
|
@@ -4023,6 +4160,8 @@ export class ClientSession {
|
|
|
4023
4160
|
const conv = this.conv;
|
|
4024
4161
|
// 输入框内容被消费(发送/斜杠执行)→ 清掉该会话存过的草稿(best-effort)。
|
|
4025
4162
|
// 快捷短语发送(不碰输入框)同样清:客户端发送成功后会把当前草稿重存回来。
|
|
4163
|
+
// clear() 同时记录 clear 时间戳水位:清掉之后才 landing 的旧 draft_update
|
|
4164
|
+
// (防抖延迟 / 跨 tab 陈旧写,ts <= 水位)由 store 直接丢弃,不复活。
|
|
4026
4165
|
try {
|
|
4027
4166
|
this.drafts.clear(conv.session.sessionId);
|
|
4028
4167
|
}
|
|
@@ -4289,13 +4428,18 @@ export class ClientSession {
|
|
|
4289
4428
|
this.flushSnapshot();
|
|
4290
4429
|
}
|
|
4291
4430
|
/**
|
|
4292
|
-
* Remove ONE queued prompt
|
|
4431
|
+
* Remove ONE queued prompt (the ✕ on a pending bubble) so it is neither
|
|
4293
4432
|
* shown nor eventually delivered. The pi SDK has no per-item queue API, so we
|
|
4294
|
-
* drain the SDK queue (clearQueue), drop the target
|
|
4433
|
+
* drain the SDK queue (clearQueue), drop the target item and re-queue the rest
|
|
4295
4434
|
* in their original order; the SDK re-emits queue_update which re-syncs
|
|
4296
4435
|
* conv.queueSteering / conv.queueFollowUp.
|
|
4436
|
+
*
|
|
4437
|
+
* `index` identifies WHICH bubble was clicked (same duplicate text can be
|
|
4438
|
+
* queued twice — text alone drops the wrong one). It is checked against the
|
|
4439
|
+
* item still at that position; on any mismatch we fall back to first-occurrence
|
|
4440
|
+
* text match (old clients, or the queue shifted between click and handling).
|
|
4297
4441
|
*/
|
|
4298
|
-
async removeQueued(kind, text) {
|
|
4442
|
+
async removeQueued(kind, text, index) {
|
|
4299
4443
|
const conv = this.conv;
|
|
4300
4444
|
// Always-defined display mirrors; also the provenance of bubble rendering.
|
|
4301
4445
|
const local = kind === "steer" ? conv.queueSteering : conv.queueFollowUp;
|
|
@@ -4308,17 +4452,19 @@ export class ClientSession {
|
|
|
4308
4452
|
if (!s) {
|
|
4309
4453
|
// Runtime not bound yet (fresh conversation) — drop the display mirror;
|
|
4310
4454
|
// a later queue_update reconciles any SDK-side state.
|
|
4311
|
-
const
|
|
4312
|
-
if (
|
|
4313
|
-
local.splice(
|
|
4455
|
+
const next = removeQueuedByIndexOrText(local, text, index);
|
|
4456
|
+
if (next.length !== local.length) {
|
|
4457
|
+
local.splice(0, local.length, ...next);
|
|
4458
|
+
}
|
|
4314
4459
|
this.flushSnapshot();
|
|
4315
4460
|
return;
|
|
4316
4461
|
}
|
|
4317
4462
|
const { steering, followUp } = s.clearQueue();
|
|
4318
|
-
//
|
|
4319
|
-
//
|
|
4320
|
-
|
|
4321
|
-
const
|
|
4463
|
+
// 气泡 ✕ 对应的是「第几个气泡」(index),不是「哪段文本」:同一文本排队两次时
|
|
4464
|
+
// 按文本只会删掉第一条,点第二个气泡却删掉第一个。用 index 定位,位置对不上
|
|
4465
|
+
// (队列在点击与执行之间变化)或旧客户端没发 index 时回落到第一处文本匹配。
|
|
4466
|
+
const keptSteering = kind === "steer" ? removeQueuedByIndexOrText(steering, text, index) : steering;
|
|
4467
|
+
const keptFollowUp = kind === "followUp" ? removeQueuedByIndexOrText(followUp, text, index) : followUp;
|
|
4322
4468
|
// Re-queue the survivors in original order. Guard each call so a single
|
|
4323
4469
|
// failure can't leave the queue half-drained silently.
|
|
4324
4470
|
for (const t of keptSteering) {
|
|
@@ -4355,7 +4501,8 @@ export class ClientSession {
|
|
|
4355
4501
|
/** 存指定会话的未发送草稿(`draft_update` 入口,经 DispatchSession.saveDraft)。
|
|
4356
4502
|
* 按消息自带的 sessionId 落键(不按 active 会话:切会话时的「离开刷盘」
|
|
4357
4503
|
* 晚于服务端的切换到达)。空白新会话的转录还没落盘,但 id 内存里已有。
|
|
4358
|
-
* 存完不推快照:同页的草稿本来就是自己打的;恢复走全量快照的 draft 字段。
|
|
4504
|
+
* 存完不推快照:同页的草稿本来就是自己打的;恢复走全量快照的 draft 字段。
|
|
4505
|
+
* 陈旧写由 ComposerDraftsStore 的 clear 水位丢弃(ts <= clearTs 不复活)。 */
|
|
4359
4506
|
saveDraft(sessionId, text, ts) {
|
|
4360
4507
|
try {
|
|
4361
4508
|
if (!sessionId)
|
|
@@ -4819,6 +4966,133 @@ export class ClientSession {
|
|
|
4819
4966
|
conv.unsubscribe = undefined;
|
|
4820
4967
|
void conv.runtime.dispose().catch(() => { });
|
|
4821
4968
|
}
|
|
4969
|
+
/** 过户用的对话摘要(AgentService 拼移动集合 + 容量检查用)。 */
|
|
4970
|
+
takeoverBriefs() {
|
|
4971
|
+
return [...this.convs.values()].map((c) => ({
|
|
4972
|
+
id: c.id,
|
|
4973
|
+
title: c.title,
|
|
4974
|
+
cwd: c.cwd,
|
|
4975
|
+
...(c.parentId ? { parentId: c.parentId } : {}),
|
|
4976
|
+
isSubagent: c.isSubagent,
|
|
4977
|
+
}));
|
|
4978
|
+
}
|
|
4979
|
+
/**
|
|
4980
|
+
* 过户转出:把指定对话(含事件订阅/看门狗计时器/等答复问卷/页调用)从本会话摘除。
|
|
4981
|
+
* - 先修 active:active 被搬且还有剩余 → 切过去(优先主对话);active 被搬且掏空 →
|
|
4982
|
+
* 建空白兜底,建不出来(quiesce)则拒绝搬出(ok:false),绝不留悬空 active。
|
|
4983
|
+
* - 看门狗计时器清掉(toolStartTimes 保留,目标按剩余时间重布)。
|
|
4984
|
+
* - 只搬归属被搬对话的问卷/页调用(conversationId 对得上的;未记归属的留在源会话)。
|
|
4985
|
+
*/
|
|
4986
|
+
async detachTakeoverConversations(ids) {
|
|
4987
|
+
const set = new Set(ids);
|
|
4988
|
+
const convs = [...this.convs.values()].filter((c) => set.has(c.id));
|
|
4989
|
+
if (convs.length === 0)
|
|
4990
|
+
return { ok: false, reason: "missing" };
|
|
4991
|
+
if (set.has(this.activeId)) {
|
|
4992
|
+
const remaining = [...this.convs.values()].find((c) => !set.has(c.id) && !c.isSubagent) ??
|
|
4993
|
+
[...this.convs.values()].find((c) => !set.has(c.id));
|
|
4994
|
+
if (remaining) {
|
|
4995
|
+
await this.switchConversation(remaining.id);
|
|
4996
|
+
}
|
|
4997
|
+
else if (!(await this.newChat())) {
|
|
4998
|
+
return { ok: false, reason: "empty" };
|
|
4999
|
+
}
|
|
5000
|
+
}
|
|
5001
|
+
for (const conv of convs) {
|
|
5002
|
+
this.convs.delete(conv.id);
|
|
5003
|
+
this.clearAllToolWatchdogs(conv);
|
|
5004
|
+
conv.unsubscribe?.();
|
|
5005
|
+
conv.unsubscribe = undefined;
|
|
5006
|
+
}
|
|
5007
|
+
const questions = [];
|
|
5008
|
+
for (const [qid, p] of this.pendingQuestions) {
|
|
5009
|
+
if (p.conversationId !== undefined && set.has(p.conversationId)) {
|
|
5010
|
+
this.pendingQuestions.delete(qid);
|
|
5011
|
+
questions.push({ resolve: p.resolve, questions: p.questions, conversationId: p.conversationId });
|
|
5012
|
+
// 源页面的对话框可能是即时通道弹出的(live),快照为 null 收不掉它 ——
|
|
5013
|
+
// 明确撤回,让源页面立即收起(目标页由转入方重推 question_pending)。
|
|
5014
|
+
this.emit({ type: "question_retracted", id: qid });
|
|
5015
|
+
}
|
|
5016
|
+
}
|
|
5017
|
+
const pageCalls = [];
|
|
5018
|
+
for (const [pid, p] of this.pendingPageCalls) {
|
|
5019
|
+
if (p.conversationId !== undefined && set.has(p.conversationId)) {
|
|
5020
|
+
this.pendingPageCalls.delete(pid);
|
|
5021
|
+
clearTimeout(p.timer);
|
|
5022
|
+
pageCalls.push({ resolve: p.resolve, req: p.req, timeoutMs: p.timeoutMs, conversationId: p.conversationId });
|
|
5023
|
+
}
|
|
5024
|
+
}
|
|
5025
|
+
this.emitConversations();
|
|
5026
|
+
this.flushSnapshot();
|
|
5027
|
+
return { ok: true, payload: { convs, questions, pageCalls } };
|
|
5028
|
+
}
|
|
5029
|
+
/**
|
|
5030
|
+
* 过户转入:把另一会话摘除的对话整体接过来,返回主对话的新 id。
|
|
5031
|
+
* - id 冲突(两边计数器都从 c1 开始,大概率撞上)→ 给搬入方分配新 id,move
|
|
5032
|
+
* 集合内的 parentId/问卷归属同步改写。模型手里旧 runId 的后续子代理工具调用
|
|
5033
|
+
* 会报 unknown(可经列表查新 id);定时唤醒的旧 id 同理回落无头执行。
|
|
5034
|
+
* - 事件订阅/终端投递/问卷/页调用全部重接到本会话并立即重推(问卷对话框在新
|
|
5035
|
+
* 页面直接弹出来)。看门狗按剩余时间重布(已逾期的立即触发)。
|
|
5036
|
+
*/
|
|
5037
|
+
insertTakeoverConvs(payload) {
|
|
5038
|
+
const remap = new Map();
|
|
5039
|
+
for (const conv of payload.convs) {
|
|
5040
|
+
if (this.convs.has(conv.id)) {
|
|
5041
|
+
remap.set(conv.id, this.nextConversationId());
|
|
5042
|
+
}
|
|
5043
|
+
}
|
|
5044
|
+
const fix = (id) => remap.get(id) ?? id;
|
|
5045
|
+
let mainId = "";
|
|
5046
|
+
for (const conv of payload.convs) {
|
|
5047
|
+
conv.id = fix(conv.id);
|
|
5048
|
+
if (conv.parentId)
|
|
5049
|
+
conv.parentId = fix(conv.parentId);
|
|
5050
|
+
if (!conv.isSubagent && !mainId)
|
|
5051
|
+
mainId = conv.id;
|
|
5052
|
+
conv.lastActiveAt = Date.now();
|
|
5053
|
+
conv.terminals.rebindEmit((msg) => this.emitTerminal(conv.id, msg));
|
|
5054
|
+
conv.terminals.onAgentIdle = (terminalId, idleMs, title, lastLines) => this.notifyTerminalIdle(conv.id, terminalId, idleMs, title, lastLines);
|
|
5055
|
+
conv.unsubscribe?.();
|
|
5056
|
+
conv.unsubscribe = conv.session.subscribe((event) => this.onEvent(conv, event));
|
|
5057
|
+
for (const [toolCallId, start] of conv.toolStartTimes) {
|
|
5058
|
+
if (!conv.toolWatchdogs.has(toolCallId)) {
|
|
5059
|
+
this.rearmToolWatchdog(conv, toolCallId, TOOL_WATCHDOG_TIMEOUT_MS - (Date.now() - start));
|
|
5060
|
+
}
|
|
5061
|
+
}
|
|
5062
|
+
this.convs.set(conv.id, conv);
|
|
5063
|
+
}
|
|
5064
|
+
if (!mainId)
|
|
5065
|
+
mainId = payload.convs[0]?.id ?? "";
|
|
5066
|
+
for (const q of payload.questions) {
|
|
5067
|
+
const nid = `q-${++this.questionSeq}`;
|
|
5068
|
+
this.pendingQuestions.set(nid, {
|
|
5069
|
+
resolve: q.resolve,
|
|
5070
|
+
questions: q.questions,
|
|
5071
|
+
conversationId: fix(q.conversationId),
|
|
5072
|
+
});
|
|
5073
|
+
this.emit({ type: "question_pending", id: nid, questions: q.questions });
|
|
5074
|
+
}
|
|
5075
|
+
for (const p of payload.pageCalls) {
|
|
5076
|
+
const nid = `p-${++this.pageSeq}`;
|
|
5077
|
+
const timer = this.armPageCallTimeout(nid, p.resolve, p.timeoutMs);
|
|
5078
|
+
this.pendingPageCalls.set(nid, {
|
|
5079
|
+
resolve: p.resolve,
|
|
5080
|
+
timer,
|
|
5081
|
+
conversationId: fix(p.conversationId),
|
|
5082
|
+
req: p.req,
|
|
5083
|
+
timeoutMs: p.timeoutMs,
|
|
5084
|
+
});
|
|
5085
|
+
this.emit({
|
|
5086
|
+
type: "page_request",
|
|
5087
|
+
id: nid,
|
|
5088
|
+
op: p.req.op,
|
|
5089
|
+
args: p.req.args,
|
|
5090
|
+
target: p.req.target,
|
|
5091
|
+
timeoutMs: p.timeoutMs,
|
|
5092
|
+
});
|
|
5093
|
+
}
|
|
5094
|
+
return mainId;
|
|
5095
|
+
}
|
|
4822
5096
|
/** Switch the ACTIVE conversation without interrupting any other chat. */
|
|
4823
5097
|
async switchConversation(id) {
|
|
4824
5098
|
if (!this.convs.has(id) || id === this.activeId)
|
|
@@ -4954,14 +5228,17 @@ export class ClientSession {
|
|
|
4954
5228
|
})(),
|
|
4955
5229
|
// 子代理带 error 标记:左栏红点提示(普通对话不参与)。
|
|
4956
5230
|
...(conv.isSubagent ? this.subagentRunOutcome(conv) : {}),
|
|
5231
|
+
// 等答复的问卷:左栏「?」角标(主对话/子代理各自挂名下,切过去即可回答)。
|
|
5232
|
+
...(this.isWaitingOnUser(conv.id) ? { hasQuestion: true } : {}),
|
|
4957
5233
|
parentId: conv.parentId,
|
|
4958
5234
|
});
|
|
4959
5235
|
}
|
|
4960
|
-
// issue #145:流式集合签名变化 →
|
|
5236
|
+
// issue #145:流式集合签名变化 → 通知其他客户端重推(左栏「另一处正在运行」近实时)。
|
|
5237
|
+
// 签名含等问卷态(问卷挂起/解决不改变流式集合,不带它已打开的别处页面永远看不到 `?`)。
|
|
4961
5238
|
try {
|
|
4962
5239
|
const sig = JSON.stringify([...this.convs.values()]
|
|
4963
5240
|
.filter((c) => this.conversationStreaming(c))
|
|
4964
|
-
.map((c) => c.id)
|
|
5241
|
+
.map((c) => `${c.id}:${this.isWaitingOnUser(c.id) ? 1 : 0}`)
|
|
4965
5242
|
.sort());
|
|
4966
5243
|
if (sig !== this.lastRunningSig) {
|
|
4967
5244
|
this.lastRunningSig = sig;
|
|
@@ -4981,8 +5258,8 @@ export class ClientSession {
|
|
|
4981
5258
|
});
|
|
4982
5259
|
}
|
|
4983
5260
|
/** List persisted sessions for this client, newest first. */
|
|
4984
|
-
/** issue #145:上次 emit 时本实例流式对话 id
|
|
4985
|
-
* onRunningChanged 让其他客户端重推 conversations(elsewhere 近实时);
|
|
5261
|
+
/** issue #145:上次 emit 时本实例流式对话 id 集合签名(含等问卷态,见 emitConversations)。
|
|
5262
|
+
* 变化时经 onRunningChanged 让其他客户端重推 conversations(elsewhere 近实时);
|
|
4986
5263
|
* 签名相等即停,天然防 ping-pong 循环。 */
|
|
4987
5264
|
lastRunningSig = "";
|
|
4988
5265
|
/** The client asked for the session list at least once (lazy loading) —
|
|
@@ -5080,10 +5357,7 @@ export class ClientSession {
|
|
|
5080
5357
|
async deleteSession(path) {
|
|
5081
5358
|
try {
|
|
5082
5359
|
const abs = resolve(path);
|
|
5083
|
-
|
|
5084
|
-
// (<agentDir>/sessions/) may be deleted — never arbitrary files.
|
|
5085
|
-
const sessionsRoot = resolve(this.agentDir, "sessions");
|
|
5086
|
-
if (!abs.startsWith(sessionsRoot + sep)) {
|
|
5360
|
+
if (!isInsideSessionsDir(this.agentDir, abs)) {
|
|
5087
5361
|
this.emit({
|
|
5088
5362
|
type: "notice",
|
|
5089
5363
|
level: "error",
|
|
@@ -5183,8 +5457,7 @@ export class ClientSession {
|
|
|
5183
5457
|
if (!trimmed)
|
|
5184
5458
|
return;
|
|
5185
5459
|
const abs = resolve(path);
|
|
5186
|
-
|
|
5187
|
-
if (!abs.startsWith(sessionsRoot + sep)) {
|
|
5460
|
+
if (!isInsideSessionsDir(this.agentDir, abs)) {
|
|
5188
5461
|
this.emit({
|
|
5189
5462
|
type: "notice",
|
|
5190
5463
|
level: "error",
|
|
@@ -5679,6 +5952,16 @@ export class ClientSession {
|
|
|
5679
5952
|
let openedTerminals = null;
|
|
5680
5953
|
try {
|
|
5681
5954
|
const targetPath = resolve(path);
|
|
5955
|
+
if (!isInsideSessionsDir(this.agentDir, targetPath)) {
|
|
5956
|
+
this.emit({
|
|
5957
|
+
type: "notice",
|
|
5958
|
+
level: "error",
|
|
5959
|
+
text: "只能打开会话目录中的对话记录",
|
|
5960
|
+
textEn: "Only transcripts inside the session directory can be opened",
|
|
5961
|
+
});
|
|
5962
|
+
this.flushSnapshot();
|
|
5963
|
+
return;
|
|
5964
|
+
}
|
|
5682
5965
|
// A session may already be open in the running-conversation map. Reuse it
|
|
5683
5966
|
// instead of creating a second writer for the same JSONL transcript.
|
|
5684
5967
|
for (const conv of this.convs.values()) {
|
|
@@ -6053,8 +6336,11 @@ export class ClientSession {
|
|
|
6053
6336
|
async openDefaultEntry(path) {
|
|
6054
6337
|
return this.files.openDefaultEntry(path);
|
|
6055
6338
|
}
|
|
6056
|
-
async makeDir(relPath) {
|
|
6057
|
-
|
|
6339
|
+
async makeDir(relPath, setAsCwd = false) {
|
|
6340
|
+
const created = await this.files.makeDir(relPath);
|
|
6341
|
+
if (created && setAsCwd) {
|
|
6342
|
+
await this.setCwd(created);
|
|
6343
|
+
}
|
|
6058
6344
|
}
|
|
6059
6345
|
async cycleModel() {
|
|
6060
6346
|
try {
|
|
@@ -6436,8 +6722,10 @@ export class ClientSession {
|
|
|
6436
6722
|
async dispose() {
|
|
6437
6723
|
this.disposed = true;
|
|
6438
6724
|
this.modelAdmin.dispose();
|
|
6725
|
+
// 关机路径:Windows 下跳过 pty.kill()(issue #215 ConPTY 死锁),只做
|
|
6726
|
+
// TerminateProcess + 状态清理,句柄由 OS 在进程退出时回收。
|
|
6439
6727
|
for (const conv of this.convs.values())
|
|
6440
|
-
conv.terminals.killAll();
|
|
6728
|
+
conv.terminals.killAll({ shutdown: true });
|
|
6441
6729
|
if (this.snapshotTimer) {
|
|
6442
6730
|
clearTimeout(this.snapshotTimer);
|
|
6443
6731
|
this.snapshotTimer = null;
|
|
@@ -6490,6 +6778,8 @@ export class AgentService {
|
|
|
6490
6778
|
pluginBgTasksProvider = undefined;
|
|
6491
6779
|
/** index.ts 注入:停止插件任务(kill_background_server with taskId)。 */
|
|
6492
6780
|
pluginStopBgTask = undefined;
|
|
6781
|
+
/** index.ts 注入:内置调度存储(attach 时拷贝到每个新会话,供 schedule_* 工具)。 */
|
|
6782
|
+
schedulerStore = undefined;
|
|
6493
6783
|
clients = new Map();
|
|
6494
6784
|
/** Quiesce (draining) state — the service refuses NEW work (prompts, forks,
|
|
6495
6785
|
* session resumes, new clients) so a deploy/upgrade/backup can stop cleanly
|
|
@@ -6583,6 +6873,33 @@ export class AgentService {
|
|
|
6583
6873
|
}
|
|
6584
6874
|
return undefined;
|
|
6585
6875
|
}
|
|
6876
|
+
/** issue #193:定时任务唤醒发起对话。逐个客户端找持有方,用 steer 语义投递
|
|
6877
|
+
* (运行时插队、未跑时普通投递,不切用户当前对话);都找不到回 ok:false,
|
|
6878
|
+
* 调用方(index.ts executor)回落无头执行。quiesced 时直接拒绝。 */
|
|
6879
|
+
async wakeConversation(id, text) {
|
|
6880
|
+
if (!id || !text.trim())
|
|
6881
|
+
return { ok: false, error: "唤醒目标或文本为空" };
|
|
6882
|
+
if (this.quiesced)
|
|
6883
|
+
return { ok: false, error: "服务器正忙(quiesced),请稍后重试" };
|
|
6884
|
+
for (const cs of this.clients.values()) {
|
|
6885
|
+
try {
|
|
6886
|
+
const r = await cs.steerOwnConversation(id, text);
|
|
6887
|
+
if (r) {
|
|
6888
|
+
try {
|
|
6889
|
+
cs.flushSnapshot();
|
|
6890
|
+
}
|
|
6891
|
+
catch {
|
|
6892
|
+
// 推送失败不影响已投递的唤醒
|
|
6893
|
+
}
|
|
6894
|
+
return { ok: true, conversationId: id };
|
|
6895
|
+
}
|
|
6896
|
+
}
|
|
6897
|
+
catch {
|
|
6898
|
+
// 单客户端坏了继续找下一个
|
|
6899
|
+
}
|
|
6900
|
+
}
|
|
6901
|
+
return { ok: false, error: "目标对话不在运行中(已关闭或服务重启过)" };
|
|
6902
|
+
}
|
|
6586
6903
|
/** issue #145:别处在某 cwd 下正在跑的对话(同项目并行感知用,不含请求方)。 */
|
|
6587
6904
|
listProjectRunners(cwd, excludeClientId) {
|
|
6588
6905
|
const out = [];
|
|
@@ -6594,14 +6911,15 @@ export class AgentService {
|
|
|
6594
6911
|
}
|
|
6595
6912
|
return out;
|
|
6596
6913
|
}
|
|
6597
|
-
/** issue #145:别处所有正在跑的对话(左栏 elsewhere
|
|
6914
|
+
/** issue #145:别处所有正在跑的对话(左栏 elsewhere 只读感知 + 手动过户用)。
|
|
6915
|
+
* owner/convId 标识过户目标(手动过户入口);DSH 引擎不填(不可过户)。 */
|
|
6598
6916
|
listExternalRunning(excludeClientId) {
|
|
6599
6917
|
const out = [];
|
|
6600
6918
|
for (const [clientId, cs] of this.clients) {
|
|
6601
6919
|
if (clientId === excludeClientId)
|
|
6602
6920
|
continue;
|
|
6603
6921
|
for (const r of cs.streamingSummariesAll())
|
|
6604
|
-
out.push(r);
|
|
6922
|
+
out.push({ ...r, owner: clientId });
|
|
6605
6923
|
}
|
|
6606
6924
|
return out;
|
|
6607
6925
|
}
|
|
@@ -6812,6 +7130,216 @@ export class AgentService {
|
|
|
6812
7130
|
service: toServiceInfo(launchOrigin()),
|
|
6813
7131
|
};
|
|
6814
7132
|
}
|
|
7133
|
+
/** 插件/调度伪客户端:sink 常驻(fire-and-forget 的空函数),不能按浏览器存活判断。 */
|
|
7134
|
+
static isPseudoClientId(id) {
|
|
7135
|
+
return id.startsWith("plugin:") || id.startsWith("scheduler:");
|
|
7136
|
+
}
|
|
7137
|
+
/**
|
|
7138
|
+
* 浏览器重启认领:给 fresh clientId 找一个可接管的断开残留会话(返回旧 id)。
|
|
7139
|
+
* 有别的在线浏览器时返回 null(issue #10 隔离优先)。
|
|
7140
|
+
* 全同步:attach 里的认领段不含 await,并发的新标签后到者看到 sinkCount>0,
|
|
7141
|
+
* 不会抢走同一个残留。
|
|
7142
|
+
*/
|
|
7143
|
+
findAdoptableOrphan(excludeClientId) {
|
|
7144
|
+
const cands = [];
|
|
7145
|
+
for (const [id, cs] of this.clients) {
|
|
7146
|
+
if (id === excludeClientId)
|
|
7147
|
+
continue;
|
|
7148
|
+
const pseudo = AgentService.isPseudoClientId(id);
|
|
7149
|
+
let live = false;
|
|
7150
|
+
let streaming = 0;
|
|
7151
|
+
let adoptable = false;
|
|
7152
|
+
let activity = 0;
|
|
7153
|
+
try {
|
|
7154
|
+
live = cs.sinkCount() > 0;
|
|
7155
|
+
}
|
|
7156
|
+
catch {
|
|
7157
|
+
live = false;
|
|
7158
|
+
}
|
|
7159
|
+
if (!pseudo && !live) {
|
|
7160
|
+
try {
|
|
7161
|
+
streaming = cs.activeConversations();
|
|
7162
|
+
}
|
|
7163
|
+
catch {
|
|
7164
|
+
streaming = 0;
|
|
7165
|
+
}
|
|
7166
|
+
try {
|
|
7167
|
+
adoptable = cs.hasAdoptableContent();
|
|
7168
|
+
}
|
|
7169
|
+
catch {
|
|
7170
|
+
adoptable = false;
|
|
7171
|
+
}
|
|
7172
|
+
try {
|
|
7173
|
+
activity = cs.latestActivity();
|
|
7174
|
+
}
|
|
7175
|
+
catch {
|
|
7176
|
+
activity = 0;
|
|
7177
|
+
}
|
|
7178
|
+
}
|
|
7179
|
+
cands.push({ id, live, pseudo, streaming, adoptable, activity });
|
|
7180
|
+
}
|
|
7181
|
+
const picked = pickAdoptableOrphan(cands);
|
|
7182
|
+
if (!picked)
|
|
7183
|
+
return null;
|
|
7184
|
+
const cs = this.clients.get(picked);
|
|
7185
|
+
return cs ? { oldId: picked, cs } : null;
|
|
7186
|
+
}
|
|
7187
|
+
/**
|
|
7188
|
+
* 跨客户端感知接线(同会话查重 / 同项目并行 / elsewhere 列表 / 跨端 steer)。
|
|
7189
|
+
* attach 尾部与认领分支共用 —— 认领换了 map 键,必须在首帧推送(attachSink)
|
|
7190
|
+
* 前就按新 id 重接,否则 self-exclusion 失效:把自己当成“另一处”(elsewhere
|
|
7191
|
+
* 误报 + prompt/switch 自拦)。尾部会再调一次,幂等。
|
|
7192
|
+
*/
|
|
7193
|
+
wireClient(cs, clientId) {
|
|
7194
|
+
cs.findSessionOwner = (targetPath) => this.findSessionOwner(targetPath, clientId);
|
|
7195
|
+
cs.hasStreamingElsewhere = () => this.hasStreamingElsewhere(clientId);
|
|
7196
|
+
cs.listProjectRunners = (cwd) => this.listProjectRunners(cwd, clientId);
|
|
7197
|
+
cs.listExternalRunning = () => this.listExternalRunning(clientId);
|
|
7198
|
+
cs.notifyExternalClients = (msg) => this.notifyClientsExcept(clientId, msg);
|
|
7199
|
+
cs.onRunningChanged = () => this.pokeExternalRunning(clientId);
|
|
7200
|
+
cs.steerConversationElsewhere = (id, text) => this.steerElsewhere(clientId, id, text);
|
|
7201
|
+
cs.schedulerStore = this.schedulerStore;
|
|
7202
|
+
}
|
|
7203
|
+
/**
|
|
7204
|
+
* 手动过户(take_over_conversation):把 owner 会话的某主对话(含子代理后代、
|
|
7205
|
+
* 等答复问卷/页调用)整体搬到 target 会话并切过去。搬的是 runtime 本体不是
|
|
7206
|
+
* 副本,单 writer 不变 —— 从在线标签页手里接管也是安全的;源会话修好 active
|
|
7207
|
+
* 并推全量刷新,双方都收到去向通知。quiesce 排空期也放行(重连既有工作)。
|
|
7208
|
+
*/
|
|
7209
|
+
async takeOverConversation(targetId, ownerId, convId) {
|
|
7210
|
+
const target = this.clients.get(targetId);
|
|
7211
|
+
if (!target)
|
|
7212
|
+
return;
|
|
7213
|
+
const fail = (text, textEn) => {
|
|
7214
|
+
target.sendNotice({ type: "notice", level: "warning", text, textEn });
|
|
7215
|
+
};
|
|
7216
|
+
if (!ownerId || !convId) {
|
|
7217
|
+
fail("过户目标不明确(缺 owner/id),请重试", "Takeover target unclear (missing owner/id), please retry.");
|
|
7218
|
+
return;
|
|
7219
|
+
}
|
|
7220
|
+
if (ownerId === targetId) {
|
|
7221
|
+
// 自己的对话 → 退化为普通切换。
|
|
7222
|
+
try {
|
|
7223
|
+
await target.switchConversation(convId);
|
|
7224
|
+
}
|
|
7225
|
+
catch {
|
|
7226
|
+
/* switch 内部已用 notice 报错 */
|
|
7227
|
+
}
|
|
7228
|
+
return;
|
|
7229
|
+
}
|
|
7230
|
+
if (AgentService.isPseudoClientId(ownerId)) {
|
|
7231
|
+
fail("定时任务/插件会话不支持过户", "Scheduler/plugin sessions cannot be taken over.");
|
|
7232
|
+
return;
|
|
7233
|
+
}
|
|
7234
|
+
const source = this.clients.get(ownerId);
|
|
7235
|
+
if (!source) {
|
|
7236
|
+
fail("对方会话已不存在,可从历史对话里直接打开", "The source session is gone; reopen it from History instead.");
|
|
7237
|
+
target.refreshExternalRunning();
|
|
7238
|
+
return;
|
|
7239
|
+
}
|
|
7240
|
+
const briefs = source.takeoverBriefs();
|
|
7241
|
+
const main = briefs.find((b) => b.id === convId);
|
|
7242
|
+
if (!main) {
|
|
7243
|
+
fail("对方已经没有这条对话(刚结束或被关闭),左栏稍后自动刷新", "That conversation is gone on the other side; the list refreshes shortly.");
|
|
7244
|
+
target.refreshExternalRunning();
|
|
7245
|
+
return;
|
|
7246
|
+
}
|
|
7247
|
+
if (main.isSubagent) {
|
|
7248
|
+
fail("只能过户主对话(子代理随主对话一起搬)", "Only main conversations can be taken over (subagents move with their parent).");
|
|
7249
|
+
return;
|
|
7250
|
+
}
|
|
7251
|
+
const moveIds = [convId, ...collectSubagentDescendantIds(briefs, convId)];
|
|
7252
|
+
const moveSet = new Set(moveIds);
|
|
7253
|
+
// 容量:与 switchSession 同口径(目标项目非子代理 8 个)。
|
|
7254
|
+
const movedMains = briefs.filter((b) => moveSet.has(b.id) && !b.isSubagent).length;
|
|
7255
|
+
const openInProject = target.takeoverBriefs().filter((b) => b.cwd === main.cwd && !b.isSubagent).length + movedMains;
|
|
7256
|
+
if (openInProject > MAX_OPEN_CONVERSATIONS) {
|
|
7257
|
+
fail(`目标项目运行的对话已达上限(${MAX_OPEN_CONVERSATIONS} 个),请先打开某个对话并离开(不继续对话)以移出列表`, `This project already has the max open conversations (${MAX_OPEN_CONVERSATIONS}). Open one and leave it (without continuing) to remove it from the list.`);
|
|
7258
|
+
return;
|
|
7259
|
+
}
|
|
7260
|
+
try {
|
|
7261
|
+
const detached = await source.detachTakeoverConversations(moveIds);
|
|
7262
|
+
if (!detached.ok) {
|
|
7263
|
+
fail(detached.reason === "empty"
|
|
7264
|
+
? "对方会话只剩这一条对话且服务排空中,稍后再试"
|
|
7265
|
+
: "对方已经没有这条对话(刚结束或被关闭),左栏稍后自动刷新", detached.reason === "empty"
|
|
7266
|
+
? "The source session only has this conversation and the server is draining; try later."
|
|
7267
|
+
: "That conversation is gone on the other side; the list refreshes shortly.");
|
|
7268
|
+
if (detached.reason === "missing")
|
|
7269
|
+
target.refreshExternalRunning();
|
|
7270
|
+
return;
|
|
7271
|
+
}
|
|
7272
|
+
const newMainId = target.insertTakeoverConvs(detached.payload);
|
|
7273
|
+
// 源会话修好 active(detach 内部已处理)→ 推全量刷新 + 告知去向;
|
|
7274
|
+
// 无 sink 时 emit 即丢,无需判断。
|
|
7275
|
+
source.sendNotice({
|
|
7276
|
+
type: "notice",
|
|
7277
|
+
level: "info",
|
|
7278
|
+
text: `「${main.title}」已过户到另一处接管,本页不再持有它。`,
|
|
7279
|
+
textEn: `"${main.title}" was taken over by another page and is no longer held here.`,
|
|
7280
|
+
});
|
|
7281
|
+
target.sendNotice({
|
|
7282
|
+
type: "notice",
|
|
7283
|
+
level: "info",
|
|
7284
|
+
text: `已将「${main.title}」过户到当前页面,可直接继续查看与操作。`,
|
|
7285
|
+
textEn: `"${main.title}" was moved to this page — pick up right where it left off.`,
|
|
7286
|
+
});
|
|
7287
|
+
await target.switchConversation(newMainId);
|
|
7288
|
+
}
|
|
7289
|
+
catch (err) {
|
|
7290
|
+
fail(`过户失败:${err.message}`, `Takeover failed: ${err.message}`);
|
|
7291
|
+
}
|
|
7292
|
+
}
|
|
7293
|
+
/**
|
|
7294
|
+
* 跨页作答预告(peek_elsewhere_question):把 owner 会话里某对话的等答复问卷
|
|
7295
|
+
* 原文取回 target 页展示。只读,不搬迁对话;问卷已不在则直说(并刷新左栏)。
|
|
7296
|
+
*/
|
|
7297
|
+
async peekElsewhereQuestion(targetId, ownerId, convId) {
|
|
7298
|
+
const target = this.clients.get(targetId);
|
|
7299
|
+
if (!target)
|
|
7300
|
+
return;
|
|
7301
|
+
const fail = (text, textEn) => {
|
|
7302
|
+
target.sendNotice({ type: "notice", level: "warning", text, textEn });
|
|
7303
|
+
};
|
|
7304
|
+
if (!ownerId || !convId) {
|
|
7305
|
+
fail("问卷目标不明确(缺 owner/id),请重试", "Question target unclear (missing owner/id), please retry.");
|
|
7306
|
+
return;
|
|
7307
|
+
}
|
|
7308
|
+
if (ownerId === targetId)
|
|
7309
|
+
return; // 自己的问卷走本地通道,不需要预告
|
|
7310
|
+
if (AgentService.isPseudoClientId(ownerId)) {
|
|
7311
|
+
fail("定时任务/插件会话的问卷不支持跨页作答", "Scheduler/plugin session questions cannot be answered cross-page.");
|
|
7312
|
+
return;
|
|
7313
|
+
}
|
|
7314
|
+
const source = this.clients.get(ownerId);
|
|
7315
|
+
const q = source?.peekPendingQuestion(convId);
|
|
7316
|
+
if (!q) {
|
|
7317
|
+
fail("那张问卷已不在(对方刚回答/取消或对话已结束)", "That question is gone (just answered/cancelled there, or the run ended).");
|
|
7318
|
+
target.refreshExternalRunning();
|
|
7319
|
+
return;
|
|
7320
|
+
}
|
|
7321
|
+
target.pushElsewhereQuestion(ownerId, convId, q);
|
|
7322
|
+
}
|
|
7323
|
+
/**
|
|
7324
|
+
* 跨页作答(question_answer 带 owner):把本页提交的答案送到持有方会话。
|
|
7325
|
+
* 问卷已不在(对方刚回答/取消)则明确告知,答案不吞不丢两不沾 —— 没送出就是没送出。
|
|
7326
|
+
*/
|
|
7327
|
+
async answerElsewhereQuestion(targetId, ownerId, id, answers, cancelled) {
|
|
7328
|
+
const target = this.clients.get(targetId);
|
|
7329
|
+
if (!target)
|
|
7330
|
+
return;
|
|
7331
|
+
const source = this.clients.get(ownerId);
|
|
7332
|
+
const ok = source ? source.resolveQuestion(id, answers, cancelled) : false;
|
|
7333
|
+
if (!ok) {
|
|
7334
|
+
target.sendNotice({
|
|
7335
|
+
type: "notice",
|
|
7336
|
+
level: "warning",
|
|
7337
|
+
text: "那张问卷已不在(对方刚回答/取消或对话已结束),你的回答没有送出",
|
|
7338
|
+
textEn: "That question is gone (just answered/cancelled there, or the run ended) — your answer was not delivered.",
|
|
7339
|
+
});
|
|
7340
|
+
target.refreshExternalRunning();
|
|
7341
|
+
}
|
|
7342
|
+
}
|
|
6815
7343
|
/** Get or create the session for a client, racing attach calls safely. */
|
|
6816
7344
|
async attach(clientId, send) {
|
|
6817
7345
|
let cs = this.clients.get(clientId);
|
|
@@ -6821,66 +7349,83 @@ export class AgentService {
|
|
|
6821
7349
|
cs = await inflight;
|
|
6822
7350
|
}
|
|
6823
7351
|
else {
|
|
6824
|
-
//
|
|
6825
|
-
//
|
|
6826
|
-
//
|
|
6827
|
-
//
|
|
6828
|
-
//
|
|
6829
|
-
|
|
6830
|
-
|
|
7352
|
+
// 浏览器重启认领(clientId 存 sessionStorage,关浏览器即失;服务端残留
|
|
7353
|
+
// ClientSession 的运行中对话否则永远卡在“另一处”只读,连看都看不了):
|
|
7354
|
+
// 无其他在线浏览器时,把最近断开的残留会话整体过户给这个新 id
|
|
7355
|
+
// (只换 map 键,不搬 runtime:对话/终端/订阅/cwd 原样保留,
|
|
7356
|
+
// 流式增量经尾部 attachSink 直接推给新 socket)。
|
|
7357
|
+
// 有其他在线标签时不认领(issue #10 隔离优先);quiesce 排空期也放行
|
|
7358
|
+
// (这是重连既有工作,不是新工作)。本段无 await,并发 attach 原子。
|
|
7359
|
+
const orphan = this.findAdoptableOrphan(clientId);
|
|
7360
|
+
if (orphan) {
|
|
7361
|
+
this.clients.delete(orphan.oldId);
|
|
7362
|
+
this.clients.set(clientId, orphan.cs);
|
|
7363
|
+
cs = orphan.cs;
|
|
7364
|
+
// 先按新 id 重接(首帧 attachSink 的 elsewhere/self-exclusion 依赖它)。
|
|
7365
|
+
this.wireClient(cs, clientId);
|
|
7366
|
+
cs.noteAdopted();
|
|
7367
|
+
// 服务重启前记在旧 id 名下的中断记录搬到新 id 名下,尾部
|
|
7368
|
+
// resumeInterrupted 按新 id 消费(只认领一次,不重复恢复)。
|
|
7369
|
+
const inter = this.stateStore.takeInterrupted(orphan.oldId);
|
|
7370
|
+
if (inter?.length)
|
|
7371
|
+
this.stateStore.saveInterrupted(clientId, inter);
|
|
6831
7372
|
}
|
|
6832
|
-
|
|
6833
|
-
|
|
6834
|
-
|
|
6835
|
-
|
|
6836
|
-
|
|
6837
|
-
|
|
6838
|
-
|
|
6839
|
-
|
|
6840
|
-
catch {
|
|
6841
|
-
// gone (unmounted drive / deleted) — fall back to the default
|
|
7373
|
+
else {
|
|
7374
|
+
// Restore this client's last-used workspace when it still exists;
|
|
7375
|
+
// Admission gate: while quiesced, only clients with an EXISTING
|
|
7376
|
+
// session may attach (they can watch their runs drain); brand-new
|
|
7377
|
+
// clients are refused — index.ts closes their socket (4403) and the
|
|
7378
|
+
// browser reconnect loop retries after admission reopens.
|
|
7379
|
+
if (this.quiesced) {
|
|
7380
|
+
throw new QuiesceRejectedError("新连接被拒绝,请等服务器恢复后重试");
|
|
6842
7381
|
}
|
|
6843
|
-
|
|
6844
|
-
|
|
6845
|
-
|
|
6846
|
-
|
|
6847
|
-
|
|
6848
|
-
|
|
6849
|
-
|
|
6850
|
-
|
|
6851
|
-
|
|
6852
|
-
|
|
6853
|
-
|
|
6854
|
-
if (owner?.isStreaming)
|
|
6855
|
-
createOpts = { blank: true, blankTitle: owner.title };
|
|
7382
|
+
// otherwise fall back to the server's configured default cwd.
|
|
7383
|
+
let cwd = this.cwd;
|
|
7384
|
+
const saved = this.stateStore.get(clientId);
|
|
7385
|
+
if (saved.lastCwd && saved.lastCwd !== this.cwd) {
|
|
7386
|
+
try {
|
|
7387
|
+
if (statSync(saved.lastCwd).isDirectory())
|
|
7388
|
+
cwd = saved.lastCwd;
|
|
7389
|
+
}
|
|
7390
|
+
catch {
|
|
7391
|
+
// gone (unmounted drive / deleted) — fall back to the default
|
|
7392
|
+
}
|
|
6856
7393
|
}
|
|
6857
|
-
|
|
6858
|
-
|
|
7394
|
+
// Sessions use the SDK default per-project dir — no per-client dir.
|
|
7395
|
+
// issue #145:新标签页默认恢复项目最近的会话 —— 若那条在别处跑着,
|
|
7396
|
+
// 建之前就决定空白(第二个 writer 根本不会被打开,也无需事后拆 runtime)。
|
|
7397
|
+
// 先做无 I/O 的便宜判断,无人在跑时不扫目录。
|
|
7398
|
+
let createOpts;
|
|
7399
|
+
if (this.hasStreamingElsewhere(clientId)) {
|
|
7400
|
+
try {
|
|
7401
|
+
const infos = await SessionManager.list(cwd, piSessionsRoot());
|
|
7402
|
+
const recent = infos[0]?.path ? resolve(infos[0].path) : undefined;
|
|
7403
|
+
const owner = recent ? this.findSessionOwner(recent, clientId) : null;
|
|
7404
|
+
if (owner?.isStreaming)
|
|
7405
|
+
createOpts = { blank: true, blankTitle: owner.title };
|
|
7406
|
+
}
|
|
7407
|
+
catch {
|
|
7408
|
+
// 列表失败不挡正常恢复
|
|
7409
|
+
}
|
|
6859
7410
|
}
|
|
6860
|
-
|
|
6861
|
-
|
|
6862
|
-
this.pending.delete(clientId);
|
|
6863
|
-
});
|
|
6864
|
-
this.pending.set(clientId, creating);
|
|
6865
|
-
cs = await creating;
|
|
6866
|
-
this.clients.set(clientId, cs);
|
|
6867
|
-
// issue #145 接线提前:首帧 elsewhere 依赖它。
|
|
6868
|
-
cs.findSessionOwner = (targetPath) => this.findSessionOwner(targetPath, clientId);
|
|
6869
|
-
cs.hasStreamingElsewhere = () => this.hasStreamingElsewhere(clientId);
|
|
6870
|
-
cs.listProjectRunners = (cwd) => this.listProjectRunners(cwd, clientId);
|
|
6871
|
-
cs.listExternalRunning = () => this.listExternalRunning(clientId);
|
|
6872
|
-
cs.notifyExternalClients = (msg) => this.notifyClientsExcept(clientId, msg);
|
|
6873
|
-
cs.onRunningChanged = () => this.pokeExternalRunning(clientId);
|
|
6874
|
-
cs.steerConversationElsewhere = (id, text) => this.steerElsewhere(clientId, id, text);
|
|
6875
|
-
// Make sure the restored/default workspace appears in the project list.
|
|
6876
|
-
this.stateStore.remember(clientId, cwd);
|
|
6877
|
-
if (cwd !== this.cwd) {
|
|
6878
|
-
send({
|
|
6879
|
-
type: "notice",
|
|
6880
|
-
level: "info",
|
|
6881
|
-
text: `已恢复上次的工作目录:${cwd}`,
|
|
6882
|
-
textEn: `Restored the last working directory: ${cwd}`,
|
|
7411
|
+
const creating = ClientSession.create(clientId, cwd, this.stateStore, createOpts).finally(() => {
|
|
7412
|
+
this.pending.delete(clientId);
|
|
6883
7413
|
});
|
|
7414
|
+
this.pending.set(clientId, creating);
|
|
7415
|
+
cs = await creating;
|
|
7416
|
+
this.clients.set(clientId, cs);
|
|
7417
|
+
// issue #145 接线提前:首帧 elsewhere 依赖它。
|
|
7418
|
+
this.wireClient(cs, clientId);
|
|
7419
|
+
// Make sure the restored/default workspace appears in the project list.
|
|
7420
|
+
this.stateStore.remember(clientId, cwd);
|
|
7421
|
+
if (cwd !== this.cwd) {
|
|
7422
|
+
send({
|
|
7423
|
+
type: "notice",
|
|
7424
|
+
level: "info",
|
|
7425
|
+
text: `已恢复上次的工作目录:${cwd}`,
|
|
7426
|
+
textEn: `Restored the last working directory: ${cwd}`,
|
|
7427
|
+
});
|
|
7428
|
+
}
|
|
6884
7429
|
}
|
|
6885
7430
|
}
|
|
6886
7431
|
}
|
|
@@ -6901,13 +7446,7 @@ export class AgentService {
|
|
|
6901
7446
|
cs.pluginStopBgTask = this.pluginStopBgTask;
|
|
6902
7447
|
cs.isQuiesced = () => this.quiesced;
|
|
6903
7448
|
// issue #145 跨客户端感知接线(同会话查重 / 同项目并行 / elsewhere 列表)。
|
|
6904
|
-
|
|
6905
|
-
cs.hasStreamingElsewhere = () => this.hasStreamingElsewhere(clientId);
|
|
6906
|
-
cs.listProjectRunners = (cwd) => this.listProjectRunners(cwd, clientId);
|
|
6907
|
-
cs.listExternalRunning = () => this.listExternalRunning(clientId);
|
|
6908
|
-
cs.notifyExternalClients = (msg) => this.notifyClientsExcept(clientId, msg);
|
|
6909
|
-
cs.onRunningChanged = () => this.pokeExternalRunning(clientId);
|
|
6910
|
-
cs.steerConversationElsewhere = (id, text) => this.steerElsewhere(clientId, id, text);
|
|
7449
|
+
this.wireClient(cs, clientId);
|
|
6911
7450
|
// 插件宿主工作区跟随:初次接入也同步一次(恢复的 lastCwd 可能≠服务启动目录),
|
|
6912
7451
|
// notifyCwd 幂等去重;此后 set_cwd 成功时由 cs.onCwdChanged 继续驱动。
|
|
6913
7452
|
cs.onCwdChanged = (abs, roots) => this.onClientCwdChanged?.(abs, roots);
|