pi-web-ui 0.114.3 → 0.115.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 +33 -1
- package/bin/pi-web-ui.mjs +69 -7
- package/dist/server/agent-service-daemon.js +440 -32
- package/dist/server/agent-service.js +849 -378
- package/dist/server/canvas/canvas-dispatch.js +13 -1
- package/dist/server/client-state.js +25 -0
- 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 +72 -9
- package/dist/server/device-center/bot-presets.js +46 -0
- package/dist/server/device-center/collab-service.js +328 -10
- package/dist/server/device-center/collab-tool.js +738 -12
- 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 +19 -0
- package/dist/server/device-center/execution-settings.js +100 -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 +389 -40
- package/dist/server/device-center/file-sync-paths.js +171 -0
- package/dist/server/device-center/file-sync.js +180 -91
- package/dist/server/device-center/group-context-delta.js +37 -0
- package/dist/server/device-center/group-context-message.js +208 -0
- package/dist/server/device-center/group-context.js +109 -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 +178 -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.js +1544 -80
- 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 +18 -0
- package/dist/server/device-center/local.js +356 -40
- package/dist/server/device-center/main.js +11 -1
- 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 +549 -41
- package/dist/server/device-center/store.js +66 -3
- 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 +400 -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 +815 -8
- package/dist/server/dispatch-domain-handlers.js +21 -3
- package/dist/server/dsh/dsh-agent-service.js +6 -0
- package/dist/server/goal-service.js +10 -5
- package/dist/server/index.js +89 -13
- package/dist/server/message-deletion.js +40 -0
- package/dist/server/message-window.js +9 -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 +1 -0
- 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 +5 -2
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/protocol.js +2 -2
- 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/subagent-helpers.js +4 -1
- package/dist/server/subagents.js +50 -36
- package/dist/server/tool-host-factories.js +1 -0
- package/dist/server/universal/universal-session.js +5 -0
- package/dist/server/webui-context.js +25 -5
- package/docs/README.md +94 -0
- package/docs/device-center.md +100 -12
- package/docs/dsh-engine.md +10 -10
- package/extensions/webui.ts +43 -1
- package/package.json +1 -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-BYzPgmoq.js +1 -0
- package/web/dist/assets/BgTasksModal-ni0VTmAQ.js +1 -0
- package/web/dist/assets/{CanvasStudio-BSg9ghPV.js → CanvasStudio-BCUeEf25.js} +3 -3
- package/web/dist/assets/{CanvasStudio-DxW7C3Sz.js → CanvasStudio-DNmDNBod.js} +3 -3
- package/web/dist/assets/DeviceCenter-BcBhRgL6.js +3 -0
- package/web/dist/assets/DeviceCenter-CmL5FZmt.js +3 -0
- package/web/dist/assets/DeviceGroups-CLdpdnI7.js +1 -0
- package/web/dist/assets/DeviceGroups-CgBaDPrD.js +1 -0
- package/web/dist/assets/Dropdown-CGXD0DfT.js +1 -0
- package/web/dist/assets/FilePreview-B8iqnBjy.js +2 -0
- package/web/dist/assets/FilePreview-BqviZKCN.js +2 -0
- package/web/dist/assets/GlobalSearchModal-mevF-ySi.js +1 -0
- package/web/dist/assets/GlobalSearchModal-qL99FN_F.js +1 -0
- package/web/dist/assets/GroupConversation-B88Yy-lt.js +3 -0
- package/web/dist/assets/GroupConversation-BwP5NVOE.js +3 -0
- package/web/dist/assets/HintTip-LHwkS2jV.js +1 -0
- package/web/dist/assets/ModelConfigModal-Do3Fe3jH.js +2 -0
- package/web/dist/assets/PiSetupModal-DE5-gEtI.js +1 -0
- package/web/dist/assets/ProviderOAuthControls-6UMARucA.js +1 -0
- package/web/dist/assets/SCMPanel-Hi6QqC1Q.js +2 -0
- package/web/dist/assets/SCMPanel-ezY_ZtCu.js +2 -0
- package/web/dist/assets/SettingsModal-6HgeEsJ8.js +20 -0
- package/web/dist/assets/SettingsModal-CF-7MwqS.js +20 -0
- package/web/dist/assets/TerminalPanel-CTp_BZ4S.js +3 -0
- package/web/dist/assets/TerminalPanel-CkYxC-Gh.js +3 -0
- package/web/dist/assets/TuiOverlayModal-1cRLP8IF.js +1 -0
- package/web/dist/assets/TuiOverlayModal-2xlptqWX.js +1 -0
- package/web/dist/assets/app-globals-DZNa9v8u.js +325 -0
- package/web/dist/assets/canvas-client-Bye9nSFO.js +1 -0
- package/web/dist/assets/{desktop-7EvUHhKQ.js → desktop-kjBoE3Nu.js} +1 -1
- package/web/dist/assets/group-view-state-CGCzYF8k.js +1 -0
- package/web/dist/assets/index-CsFs_HMb.css +1 -0
- package/web/dist/assets/index-DHeeHS_l.js +85 -0
- package/web/dist/assets/index-DTZch8uz.js +85 -0
- package/web/dist/assets/plugin-host-C8mNgghx.js +1 -0
- package/web/dist/assets/remote-ssh-client-CeP3CLUW.js +1 -0
- package/web/dist/assets/theme-BXGpyTMh.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
|
@@ -61,26 +61,21 @@ import { SandboxManager, evaluateSandboxJail, isSameOrSubpath, withSandboxJailGa
|
|
|
61
61
|
import { readPermissionFromSession } from "./permission-preset.js";
|
|
62
62
|
import { McpBridge, getMcpToolServer } from "./mcp-bridge.js";
|
|
63
63
|
import { getBotExecution } from "./device-center/bot-execution.js";
|
|
64
|
+
import { checkGroupTaskPlanGuard } from "./device-center/group-plan-guard.js";
|
|
65
|
+
import { prepareBotExecutionSystemPrompt } from "./device-center/execution-system-prompt.js";
|
|
66
|
+
import { extractGroupContextMetrics, groupContextMessage, recordGroupContextMetrics, repairGroupContextRequest, } from "./device-center/group-context-message.js";
|
|
64
67
|
import { createWorkspaceSnapshot, restoreWorkspaceSnapshot, findMatchingWorkspaceSnapshot, readWorkspaceSnapshotsFromSession, appendWorkspaceSnapshotToSession, withWorkspaceSnapshotGate, } from "./workspace-snapshot.js";
|
|
65
68
|
import { createScheduleToolHost, createConversationReadHost, createClaimToolHost, createCompactContextHost, createSkillToolHost, } from "./tool-host-factories.js";
|
|
66
69
|
import { toSubagentSnapshot, listSubagentSnapshots, subagentRunOutcome, roleOutcomeOf, countActiveSubagents, } from "./subagent-helpers.js";
|
|
67
70
|
import { createSubagentTask, restartSubagentTask, requestSubagentCompletion, isSubagentTaskPending, } from "./subagent-task.js";
|
|
68
71
|
import { resolveUserMessageEntryId, resolveConversationMessageEntry } from "./message-entry-resolver.js";
|
|
72
|
+
import { messageDeletionTargets, undeletedContextEntries } from "./message-deletion.js";
|
|
69
73
|
import { fillSkillContents } from "./skill-content-filler.js";
|
|
70
74
|
import { estimateNonSystemTokens, renderMainCompose } from "./prompt-assembler.js";
|
|
71
75
|
import { makeCommunicateTool } from "./communicate-tool.js";
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
let address = communicationAddresses.get(endpoint);
|
|
76
|
-
if (!address) {
|
|
77
|
-
address = `comm-${++communicationAddressSequence}`;
|
|
78
|
-
communicationAddresses.set(endpoint, address);
|
|
79
|
-
}
|
|
80
|
-
return address;
|
|
81
|
-
}
|
|
82
|
-
import { conversationCommunication, } from "./conversation-communication.js";
|
|
83
|
-
import { executeContextBudgetPruning } from "./context-budget-runner.js";
|
|
76
|
+
import { conversationIdentity, bindConversationParent, hasConversationParentBinding, conversationParent, parentConversationIdentity, } from "./conversation-identity.js";
|
|
77
|
+
const communicationAddress = conversationIdentity;
|
|
78
|
+
import { conversationCommunication, formatCommunicationMessage, } from "./conversation-communication.js";
|
|
84
79
|
import { requestContextExtension, enableRequestEvidence } from "./request-context.js";
|
|
85
80
|
import { removeContextEvidence, withContextEvidenceRead, copyContextEvidence, buildContextEvidenceIndex, } from "./context-evidence.js";
|
|
86
81
|
import { installCompactionInstructions } from "./compaction-instructions.js";
|
|
@@ -143,6 +138,7 @@ import { buildVisionBridgePrompt, findVisionModels, transcribeImages } from "./v
|
|
|
143
138
|
import { generateScmCommitMessage } from "./scm-commitmsg.js";
|
|
144
139
|
import { estimateBaseContext, estimateContextWeights, reconcileContextCategories, } from "./context-usage.js";
|
|
145
140
|
import { BUILTIN_SOUL, DEFAULT_PROMPT_TEMPLATE, buildToolsSchemaText, renderPromptTemplate, resolveSectionTexts, } from "./prompt-composer.js";
|
|
141
|
+
import { createPromptBaseObservers, readPromptBase } from "./prompt-base-store.js";
|
|
146
142
|
import { listAllMcpServers, checkMcpServer, installMcpServer, saveMcpServer as saveMcpServerFile, deleteMcpServer as deleteMcpServerFile, toggleMcpServer as toggleMcpServerFile, } from "./mcp-manager.js";
|
|
147
143
|
import { fetchRemoteMcpMarket, fetchRemoteSkillMarket, fetchRemoteSkillContent } from "./market-service.js";
|
|
148
144
|
import { McpOAuthUi } from "./mcp-oauth-ui.js";
|
|
@@ -715,13 +711,14 @@ export class ClientSession {
|
|
|
715
711
|
// 父对话 = 真正派发它的会话(按会话归属的 host 包装填入)。直接用 active
|
|
716
712
|
// 会错:后台对话运行时用户可能正看着别的项目对话,孩子会被记到无关
|
|
717
713
|
// 对话名下、沉到别的项目组底部(issue #95)。缺省才回退到 active。
|
|
718
|
-
conv.parentId = parent?.id ?? parentId
|
|
714
|
+
conv.parentId = parent?.id ?? parentId;
|
|
715
|
+
bindConversationParent(conv, parent);
|
|
719
716
|
conv.subagentType = type;
|
|
720
717
|
conv.subagentPrompt = prompt;
|
|
721
718
|
conv.subagentThinking = thinking;
|
|
722
719
|
conv.notifyOnDone = notifyOnDone ?? true;
|
|
723
720
|
conv.subagentTask = createSubagentTask(conv.notifyOnDone);
|
|
724
|
-
conv.subagentTaskPending = () => this.
|
|
721
|
+
conv.subagentTaskPending = () => this.resolveExecution(conversationIdentity(conv))?.session.subagentTaskPending(conv) ?? false;
|
|
725
722
|
// 安全模式与工具白名单(issue #727):优先从 extra 取,缺省按 type 与模板自动推导
|
|
726
723
|
const securityMode = extra?.securityMode ?? resolveSecurityMode(type, apply);
|
|
727
724
|
conv.subagentSecurityMode = securityMode;
|
|
@@ -753,7 +750,12 @@ export class ClientSession {
|
|
|
753
750
|
// 子代理会话同样订阅 SDK 事件:否则 onEvent 永不触发,点开查看时没有
|
|
754
751
|
// message_delta 流式增量、快照也不刷新,只能靠切走切回时的 flushSnapshot
|
|
755
752
|
// 看到新内容(dismiss/释放流程本来就会 unsubscribe,不泄漏)。
|
|
756
|
-
conv.unsubscribe =
|
|
753
|
+
conv.unsubscribe = runtime.session.subscribe((event) => {
|
|
754
|
+
const owner = this.findConversationHome?.(runtime.session)?.session ?? spawnOwner;
|
|
755
|
+
if (owner.convs.get(conv.id) === conv && conv.runtime === runtime && conv.session === runtime.session) {
|
|
756
|
+
owner.onEvent(conv, event);
|
|
757
|
+
}
|
|
758
|
+
});
|
|
757
759
|
// 扩展绑定(rpc 模式);用 headless 的 Web UI context:
|
|
758
760
|
// 扩展绑定时不会因缺方法崩,UI 输出也不下发(不会与主对话的 widget/status 冲突)。
|
|
759
761
|
try {
|
|
@@ -846,16 +848,27 @@ export class ClientSession {
|
|
|
846
848
|
assertChildCurrent();
|
|
847
849
|
// 触发回合(后台执行;失败转识为通知)。把 Promise 挂在对话上:目标模式的
|
|
848
850
|
// 「等本回合结束」要先等它落定,才能区分「还没开跑」与「已跑完」。
|
|
851
|
+
const generation = conv.subagentRunGeneration ?? 0;
|
|
852
|
+
const currentHolder = () => {
|
|
853
|
+
const owner = this.findConversationHome?.(runtime.session)?.session ?? this;
|
|
854
|
+
return owner.convs.get(conv.id) === conv &&
|
|
855
|
+
conv.runtime === runtime &&
|
|
856
|
+
conv.session === runtime.session &&
|
|
857
|
+
(conv.subagentRunGeneration ?? 0) === generation
|
|
858
|
+
? owner
|
|
859
|
+
: undefined;
|
|
860
|
+
};
|
|
849
861
|
const onSpawnAbort = () => {
|
|
850
862
|
// SDK sendUserMessage has no signal option; abort only the captured child session.
|
|
851
|
-
|
|
863
|
+
if (currentHolder())
|
|
864
|
+
void runtime.session.abort().catch(() => { });
|
|
852
865
|
};
|
|
853
866
|
signal?.addEventListener("abort", onSpawnAbort, { once: true });
|
|
854
867
|
holder.assertSubagentWorktreeAdmission(conv);
|
|
855
868
|
conv.kickoff = runtime.session
|
|
856
869
|
.sendUserMessage(prompt)
|
|
857
870
|
.catch((err) => {
|
|
858
|
-
|
|
871
|
+
currentHolder()?.emit({
|
|
859
872
|
type: "notice",
|
|
860
873
|
level: "error",
|
|
861
874
|
text: `子代理 ${conversationId} 启动失败: ${err instanceof Error ? err.message : String(err)}`,
|
|
@@ -864,7 +877,13 @@ export class ClientSession {
|
|
|
864
877
|
})
|
|
865
878
|
.finally(() => {
|
|
866
879
|
signal?.removeEventListener("abort", onSpawnAbort);
|
|
880
|
+
const owner = currentHolder();
|
|
881
|
+
if (!owner)
|
|
882
|
+
return;
|
|
867
883
|
conv.promptInFlight = false;
|
|
884
|
+
if (conv.isSubagent)
|
|
885
|
+
owner.scheduleSubagentCompletionNotice(conv);
|
|
886
|
+
owner.emitConversations();
|
|
868
887
|
});
|
|
869
888
|
if (persist) {
|
|
870
889
|
holder.pushProjects().catch(() => { });
|
|
@@ -930,22 +949,70 @@ export class ClientSession {
|
|
|
930
949
|
}
|
|
931
950
|
getSubagentSnapshot(convId) {
|
|
932
951
|
const conv = this.convs.get(convId);
|
|
933
|
-
|
|
952
|
+
if (!conv?.session)
|
|
953
|
+
return undefined;
|
|
954
|
+
this.parentExecution(conv);
|
|
955
|
+
return toSubagentSnapshot(conv);
|
|
934
956
|
}
|
|
935
|
-
/**
|
|
936
|
-
* 旧 host 闭包仍指向源会话,按 id 全局查才能看到还在跑的子代理,而不是误判
|
|
937
|
-
* 「已移出/不存在」)。findSubagentOwner 按别名表把旧 id 解析成改名后的当前 id。 */
|
|
957
|
+
/** Resolve a stable execution address through its current holder. */
|
|
938
958
|
subagentSnapshotAnywhere(convId) {
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
959
|
+
const found = this.resolveExecution(convId);
|
|
960
|
+
return found?.session.getSubagentSnapshot(found.convId);
|
|
961
|
+
}
|
|
962
|
+
/** One resolver for runtime identities. Bare ids are local, or globally unique. */
|
|
963
|
+
resolveExecutionAddress(id) {
|
|
964
|
+
if (id.startsWith("exec-")) {
|
|
965
|
+
for (const conv of this.convs.values())
|
|
966
|
+
if (conversationIdentity(conv) === id)
|
|
967
|
+
return conv.id;
|
|
968
|
+
return undefined;
|
|
969
|
+
}
|
|
970
|
+
return this.convs.has(id) ? id : undefined;
|
|
971
|
+
}
|
|
972
|
+
resolveExecution(id) {
|
|
973
|
+
const owners = new Set([this, ...(this.allSubagentSessions?.() ?? [])]);
|
|
974
|
+
if (id.startsWith("exec-")) {
|
|
975
|
+
for (const session of owners) {
|
|
976
|
+
for (const conv of session.convs.values()) {
|
|
977
|
+
if (conversationIdentity(conv) === id)
|
|
978
|
+
return { session, convId: conv.id, conv };
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
return undefined;
|
|
982
|
+
}
|
|
983
|
+
const local = this.convs.get(id);
|
|
984
|
+
if (local)
|
|
985
|
+
return { session: this, convId: local.id, conv: local };
|
|
986
|
+
const matches = [...owners].flatMap((session) => {
|
|
987
|
+
const conv = session.convs.get(id);
|
|
988
|
+
return conv ? [{ session, convId: conv.id, conv }] : [];
|
|
989
|
+
});
|
|
990
|
+
return matches.length === 1 ? matches[0] : undefined;
|
|
991
|
+
}
|
|
992
|
+
/** Bind legacy relations once; an absent parent remains absent even after id reuse. */
|
|
993
|
+
parentExecution(child) {
|
|
994
|
+
if (!hasConversationParentBinding(child)) {
|
|
995
|
+
const home = [...new Set([this, ...(this.allSubagentSessions?.() ?? [])])].find((owner) => owner.convs.get(child.id) === child);
|
|
996
|
+
const parent = child.parentId ? (home ?? this).resolveExecution(child.parentId)?.conv : undefined;
|
|
997
|
+
bindConversationParent(child, parent);
|
|
998
|
+
}
|
|
999
|
+
const parent = conversationParent(child);
|
|
1000
|
+
return parent ? this.resolveExecution(conversationIdentity(parent)) : undefined;
|
|
1001
|
+
}
|
|
1002
|
+
parentExecutionKey(child) {
|
|
1003
|
+
this.parentExecution(child);
|
|
1004
|
+
return parentConversationIdentity(child);
|
|
1005
|
+
}
|
|
1006
|
+
/** UI parent ids are meaningful only inside this holder's namespace. */
|
|
1007
|
+
localParentId(child) {
|
|
1008
|
+
const parent = this.parentExecution(child);
|
|
1009
|
+
return parent?.session === this ? parent.convId : undefined;
|
|
943
1010
|
}
|
|
944
1011
|
/** 由该父对话派出的子代理处在主对话下第几层:沿 parentId 链数祖先(主对话派的 = 1)。 */
|
|
945
1012
|
subagentDepthUnder(parentId) {
|
|
946
1013
|
let depth = 0;
|
|
947
1014
|
const seen = new Set();
|
|
948
|
-
for (let id = parentId; id && !seen.has(id); id = this.subagentSnapshotAnywhere(id)?.
|
|
1015
|
+
for (let id = parentId; id && !seen.has(id); id = this.subagentSnapshotAnywhere(id)?.parentExecutionId) {
|
|
949
1016
|
seen.add(id);
|
|
950
1017
|
depth++;
|
|
951
1018
|
}
|
|
@@ -958,62 +1025,48 @@ export class ClientSession {
|
|
|
958
1025
|
}
|
|
959
1026
|
/** B20:convId 当前由哪个 ClientSession 持有(本会话优先;查不到兜底本会话)。 */
|
|
960
1027
|
subagentOwnerOf(convId) {
|
|
961
|
-
|
|
962
|
-
return this;
|
|
963
|
-
if (this.convs.has(convId))
|
|
964
|
-
return this;
|
|
965
|
-
return this.findSubagentOwner?.(convId)?.session ?? this;
|
|
1028
|
+
return convId ? (this.resolveExecution(convId)?.session ?? this) : this;
|
|
966
1029
|
}
|
|
967
1030
|
/** B20:解析到当前(可能被过户改名)的对话 id;查不到原样返回。 */
|
|
968
1031
|
subagentCurrentId(convId) {
|
|
969
|
-
|
|
970
|
-
return convId;
|
|
971
|
-
return this.findSubagentOwner?.(convId)?.convId ?? convId;
|
|
1032
|
+
return this.resolveExecution(convId)?.convId ?? convId;
|
|
972
1033
|
}
|
|
973
1034
|
listSubagentSnapshots(scope, forSelfId) {
|
|
1035
|
+
for (const conv of this.convs.values())
|
|
1036
|
+
this.parentExecution(conv);
|
|
974
1037
|
const own = listSubagentSnapshots(this.convs.values(), scope);
|
|
975
1038
|
// B20:带 forSelfId 才并入会话外本族(祖先链能走到 forSelfId 的子代理,
|
|
976
1039
|
// 过户搬走后 wait_all 的「无参展开」与 list 仍能看到它们);不传 = 只列本会话。
|
|
977
1040
|
if (!forSelfId || !this.allSubagentSessions)
|
|
978
1041
|
return own;
|
|
979
|
-
const seen = new Set(own.map((s) => s.convId));
|
|
1042
|
+
const seen = new Set(own.map((s) => s.executionId ?? s.convId));
|
|
980
1043
|
const out = [...own];
|
|
981
1044
|
for (const other of this.allSubagentSessions()) {
|
|
982
1045
|
if (other === this)
|
|
983
1046
|
continue;
|
|
984
1047
|
for (const s of other.listSubagentSnapshots(scope)) {
|
|
985
|
-
if (seen.has(s.convId))
|
|
1048
|
+
if (seen.has(s.executionId ?? s.convId))
|
|
986
1049
|
continue;
|
|
987
1050
|
if (!this.subagentInFamilyOf(s, forSelfId))
|
|
988
1051
|
continue;
|
|
989
|
-
seen.add(s.convId);
|
|
1052
|
+
seen.add(s.executionId ?? s.convId);
|
|
990
1053
|
out.push(s);
|
|
991
1054
|
}
|
|
992
1055
|
}
|
|
993
1056
|
return out;
|
|
994
1057
|
}
|
|
995
|
-
/**
|
|
996
|
-
* 当前持有方 + 别名解析:过户撞 id 改名后,自省 id 与孩子们记录的 parentId
|
|
997
|
-
* 都可能还是建时旧 id(c1→c2),不比显式相等,比的是「解析后的当前 id」。 */
|
|
1058
|
+
/** Family membership follows bound object identities, never matching display ids. */
|
|
998
1059
|
subagentInFamilyOf(s, selfId) {
|
|
999
|
-
|
|
1060
|
+
const self = this.resolveExecution(selfId)?.conv;
|
|
1061
|
+
if (!self)
|
|
1062
|
+
return false;
|
|
1063
|
+
let cur = this.resolveExecution(s.executionId ?? s.convId)?.conv;
|
|
1000
1064
|
const seen = new Set();
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
if (
|
|
1065
|
+
while (cur && !seen.has(cur)) {
|
|
1066
|
+
seen.add(cur);
|
|
1067
|
+
cur = this.parentExecution(cur)?.conv;
|
|
1068
|
+
if (cur === self)
|
|
1005
1069
|
return true;
|
|
1006
|
-
const found = this.findSubagentOwner?.(curId);
|
|
1007
|
-
if (!found)
|
|
1008
|
-
return false;
|
|
1009
|
-
if (found.convId === selfId)
|
|
1010
|
-
return true;
|
|
1011
|
-
const snap = found.session === this
|
|
1012
|
-
? this.getSubagentSnapshot(found.convId)
|
|
1013
|
-
: found.session.getSubagentSnapshot(found.convId);
|
|
1014
|
-
if (!snap)
|
|
1015
|
-
return false;
|
|
1016
|
-
curId = snap.parentId;
|
|
1017
1070
|
}
|
|
1018
1071
|
return false;
|
|
1019
1072
|
}
|
|
@@ -1028,7 +1081,11 @@ export class ClientSession {
|
|
|
1028
1081
|
}
|
|
1029
1082
|
// B20:parentId 是建时旧 id 时先解析成当前 id(过户撞 id 改名后,新子代理
|
|
1030
1083
|
// 的 parentId 必须指向改名后的主对话,否则族展开/lists 都认不出它)。
|
|
1031
|
-
|
|
1084
|
+
// Runtime-bound callers already supply an id in this holder. A global alias
|
|
1085
|
+
// for another client's cN must never replace an existing local parent.
|
|
1086
|
+
const canonParent = parentId ? this.resolveExecution(parentId)?.convId : undefined;
|
|
1087
|
+
if (parentId && !canonParent)
|
|
1088
|
+
throw new Error("Subagent parent is unavailable or ambiguous");
|
|
1032
1089
|
return this.spawnSubagentConversation(prompt, type, cwd, tpl, model, canonParent, persist, undefined, thinking, notifyOnDone, isolation, extra, signal);
|
|
1033
1090
|
}
|
|
1034
1091
|
requireSubagentOwner(anchor, ownerId) {
|
|
@@ -1037,7 +1094,7 @@ export class ClientSession {
|
|
|
1037
1094
|
const conv = holder.convs.get(home?.convId ?? ownerId);
|
|
1038
1095
|
if (!conv || conv.session !== anchor.session)
|
|
1039
1096
|
throw new Error("Subagent parent runtime is no longer current");
|
|
1040
|
-
return conv
|
|
1097
|
+
return conversationIdentity(conv);
|
|
1041
1098
|
}
|
|
1042
1099
|
subagentRunOutcome(conv) {
|
|
1043
1100
|
return subagentRunOutcome(conv);
|
|
@@ -1081,7 +1138,7 @@ export class ClientSession {
|
|
|
1081
1138
|
}
|
|
1082
1139
|
async stopSubagentTask(convId) {
|
|
1083
1140
|
const owner = this.subagentOwnerOf(convId);
|
|
1084
|
-
const conv = owner.convs.get(
|
|
1141
|
+
const conv = owner.convs.get(this.subagentCurrentId(convId));
|
|
1085
1142
|
if (!conv)
|
|
1086
1143
|
return;
|
|
1087
1144
|
conv.subagentTaskStopped = true;
|
|
@@ -1090,10 +1147,13 @@ export class ClientSession {
|
|
|
1090
1147
|
if (conv.session.isStreaming || !conv.session.isIdle) {
|
|
1091
1148
|
await owner.interruptRun(conv, pick(owner.getLang(), "用户停止子代理", "User stopped the subagent", "agent.subagent.stop.user"));
|
|
1092
1149
|
}
|
|
1093
|
-
|
|
1094
|
-
|
|
1150
|
+
const currentOwner = this.resolveExecution(conversationIdentity(conv))?.session;
|
|
1151
|
+
if (!currentOwner)
|
|
1152
|
+
return;
|
|
1153
|
+
currentOwner.scheduleSubagentCompletionNotice(conv);
|
|
1154
|
+
currentOwner.emitConversations();
|
|
1095
1155
|
if (conv.subagentWorktree && !conv.subagentWorktree.finalized) {
|
|
1096
|
-
void
|
|
1156
|
+
void currentOwner.finalizeSubagentWorktree(conv).catch(() => { });
|
|
1097
1157
|
}
|
|
1098
1158
|
}
|
|
1099
1159
|
/** Admission lock shared with communication and inherited-cwd spawns. */
|
|
@@ -1154,16 +1214,17 @@ export class ClientSession {
|
|
|
1154
1214
|
conv.subagentError = undefined;
|
|
1155
1215
|
conv.subagentErrorNotified = undefined;
|
|
1156
1216
|
// Remove the previous round from debounce and circuit-breaker buffers.
|
|
1157
|
-
|
|
1158
|
-
|
|
1217
|
+
const parentKey = this.parentExecutionKey(conv);
|
|
1218
|
+
if (parentKey) {
|
|
1219
|
+
const pending = this.pendingSubagentNotices.get(parentKey);
|
|
1159
1220
|
if (pending) {
|
|
1160
1221
|
pending.subagents.delete(conv);
|
|
1161
1222
|
if (pending.subagents.size === 0) {
|
|
1162
1223
|
clearTimeout(pending.timer);
|
|
1163
|
-
this.pendingSubagentNotices.delete(
|
|
1224
|
+
this.pendingSubagentNotices.delete(parentKey);
|
|
1164
1225
|
}
|
|
1165
1226
|
}
|
|
1166
|
-
this.
|
|
1227
|
+
this.parentExecution(conv)?.conv.suspendedSubagents?.delete(conv);
|
|
1167
1228
|
}
|
|
1168
1229
|
conv.promptInFlight = true;
|
|
1169
1230
|
const isCurrent = () => conv.session === session && conv.subagentRunGeneration === generation;
|
|
@@ -1177,7 +1238,9 @@ export class ClientSession {
|
|
|
1177
1238
|
const error = err instanceof Error ? err.message : String(err);
|
|
1178
1239
|
conv.subagentError = error;
|
|
1179
1240
|
conv.subagentErrorNotified = error;
|
|
1180
|
-
const holder = this.
|
|
1241
|
+
const holder = this.resolveExecution(conversationIdentity(conv))?.session;
|
|
1242
|
+
if (!holder)
|
|
1243
|
+
return;
|
|
1181
1244
|
holder.emit({
|
|
1182
1245
|
type: "notice",
|
|
1183
1246
|
level: "error",
|
|
@@ -1188,8 +1251,8 @@ export class ClientSession {
|
|
|
1188
1251
|
finally {
|
|
1189
1252
|
if (isCurrent()) {
|
|
1190
1253
|
conv.promptInFlight = false;
|
|
1191
|
-
const holder = this.
|
|
1192
|
-
if (holder
|
|
1254
|
+
const holder = this.resolveExecution(conversationIdentity(conv))?.session;
|
|
1255
|
+
if (holder?.convs.get(conv.id) === conv) {
|
|
1193
1256
|
holder.scheduleSubagentCompletionNotice(conv);
|
|
1194
1257
|
holder.emitConversations();
|
|
1195
1258
|
}
|
|
@@ -1204,7 +1267,7 @@ export class ClientSession {
|
|
|
1204
1267
|
* 合并为一条聚合消息唤醒父对话,避免碎片化多次打断父对话。
|
|
1205
1268
|
*/
|
|
1206
1269
|
scheduleSubagentCompletionNotice(conv) {
|
|
1207
|
-
const parentId = conv.parentId;
|
|
1270
|
+
const parentId = this.parentExecutionKey ? this.parentExecutionKey(conv) : conv.parentId;
|
|
1208
1271
|
if (!parentId || conv.notifyOnDone === false || this.subagentTaskPending?.(conv))
|
|
1209
1272
|
return;
|
|
1210
1273
|
if (conv.subagentDoneNotified || conv.subagentConsumedByParent)
|
|
@@ -1229,7 +1292,7 @@ export class ClientSession {
|
|
|
1229
1292
|
refreshCompletionState(parentId) {
|
|
1230
1293
|
const owner = this.subagentOwnerOf(parentId);
|
|
1231
1294
|
owner.emitConversations();
|
|
1232
|
-
if (owner.activeId === parentId)
|
|
1295
|
+
if (owner.activeId === (this.subagentCurrentId?.(parentId) ?? parentId))
|
|
1233
1296
|
owner.flushSnapshot();
|
|
1234
1297
|
}
|
|
1235
1298
|
/**
|
|
@@ -1309,7 +1372,7 @@ export class ClientSession {
|
|
|
1309
1372
|
this.deliveringSubagentNotices.delete(parentId);
|
|
1310
1373
|
this.refreshCompletionState(parentId);
|
|
1311
1374
|
const owner = this.subagentOwnerOf(parentId);
|
|
1312
|
-
const parent = owner.convs.get(
|
|
1375
|
+
const parent = owner.convs.get(this.subagentCurrentId?.(parentId) ?? parentId);
|
|
1313
1376
|
if (parent?.notifyOnDone === "manual" && !parent.subagentDoneNotified && !parent.subagentConsumedByParent) {
|
|
1314
1377
|
owner.scheduleSubagentCompletionNotice(parent);
|
|
1315
1378
|
}
|
|
@@ -1347,19 +1410,10 @@ export class ClientSession {
|
|
|
1347
1410
|
return;
|
|
1348
1411
|
// 寻找父对话(支持跨 ClientSession 查找)
|
|
1349
1412
|
const parentOwner = this.subagentOwnerOf(parentId);
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
//
|
|
1353
|
-
|
|
1354
|
-
const projectCwd = toNotify[0]?.cwd ?? this.cwd;
|
|
1355
|
-
for (const c of parentOwner.convs.values()) {
|
|
1356
|
-
if (!c.isSubagent && c.cwd === projectCwd) {
|
|
1357
|
-
targetConv = c;
|
|
1358
|
-
isFallback = true;
|
|
1359
|
-
break;
|
|
1360
|
-
}
|
|
1361
|
-
}
|
|
1362
|
-
}
|
|
1413
|
+
const currentParentId = this.subagentCurrentId?.(parentId) ?? parentId;
|
|
1414
|
+
const targetConv = parentOwner.convs.get(currentParentId);
|
|
1415
|
+
// Completion belongs only to its originating parent. Sharing a project
|
|
1416
|
+
// directory does not authorize injecting it into another conversation.
|
|
1363
1417
|
// 无存活主对话时优雅降级为系统 Notice,不报错不中断
|
|
1364
1418
|
if (!targetConv || !targetConv.session) {
|
|
1365
1419
|
for (const c of toNotify)
|
|
@@ -1423,7 +1477,7 @@ export class ClientSession {
|
|
|
1423
1477
|
for (const c of toNotify) {
|
|
1424
1478
|
c.subagentDoneNotified = true;
|
|
1425
1479
|
}
|
|
1426
|
-
const noticeText = formatSubagentsCompletionNotice(toNotify, this.getLang()
|
|
1480
|
+
const noticeText = formatSubagentsCompletionNotice(toNotify, this.getLang());
|
|
1427
1481
|
if (targetConv.notifyOnDone === "manual") {
|
|
1428
1482
|
targetConv.subagentDoneNotified = false;
|
|
1429
1483
|
targetConv.subagentConsumedByParent = false;
|
|
@@ -1535,8 +1589,13 @@ export class ClientSession {
|
|
|
1535
1589
|
emitTerminal(conversationId, msg) {
|
|
1536
1590
|
// Background conversations keep collecting output in their own PTY buffer.
|
|
1537
1591
|
// Do not stream it into the active xterm; push the retained window on switch.
|
|
1538
|
-
if (msg.type === "terminal_output" && conversationId !== this.activeId)
|
|
1592
|
+
if (msg.type === "terminal_output" && conversationId !== this.activeId) {
|
|
1593
|
+
const conv = this.convs.get(conversationId);
|
|
1594
|
+
if (conv)
|
|
1595
|
+
for (const send of this.sharedTerminalSinks?.get(conv) ?? [])
|
|
1596
|
+
send({ ...msg, conversationId });
|
|
1539
1597
|
return;
|
|
1598
|
+
}
|
|
1540
1599
|
if (msg.type === "terminal_exit") {
|
|
1541
1600
|
this.updatesAllCache = null;
|
|
1542
1601
|
}
|
|
@@ -1584,12 +1643,15 @@ export class ClientSession {
|
|
|
1584
1643
|
* prompt with the same images skips the vision API call entirely — editing
|
|
1585
1644
|
* a question doesn't re-burn tokens on re-transcribing identical screenshots.
|
|
1586
1645
|
*/
|
|
1587
|
-
/**
|
|
1588
|
-
*
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
*
|
|
1592
|
-
|
|
1646
|
+
/**
|
|
1647
|
+
* SYSTEM.md / APPEND_SYSTEM.md 的 base **不存这里**——见 prompt-base-store.ts。
|
|
1648
|
+
*
|
|
1649
|
+
* 归属必须是「哪个 loader / 哪个 session」,不是「哪个浏览器客户端」:曾经是
|
|
1650
|
+
* `lastBaseSystemPrompt` 字段(本类 = 一个浏览器客户端),切项目时新会话没有
|
|
1651
|
+
* SYSTEM.md → base 为 undefined → 旧值没人清 → 上一个项目的提示词泄漏到本项目
|
|
1652
|
+
* 的预览与新建对话(issue #812)。任何「最近一次看到的值」形态的客户端级字段都
|
|
1653
|
+
* 会复发这个 bug,读取一律走 readPromptBase(conv.session.resourceLoader)。
|
|
1654
|
+
*/
|
|
1593
1655
|
/**
|
|
1594
1656
|
* 延迟加载(工具按需加载)模式下,按 session 对象存「已加载」工具集。
|
|
1595
1657
|
*
|
|
@@ -1939,7 +2001,8 @@ export class ClientSession {
|
|
|
1939
2001
|
}
|
|
1940
2002
|
/** 当前活动会话的工具/资源快照 → composer 输入。cwd 取活动对话的。 */
|
|
1941
2003
|
composeInputs(src) {
|
|
1942
|
-
const preset = src.preset ?? this.
|
|
2004
|
+
const preset = src.preset ?? this.settingsSvc.current.defaultAgentPreset ?? "standard";
|
|
2005
|
+
const promptBase = readPromptBase(src.session?.resourceLoader);
|
|
1943
2006
|
// 技能名录指纹观测(只打日志,不干预组装;预览与 run 共用此入口,
|
|
1944
2007
|
// 变化才记一行,首轮静默)。用未注文的目录(fill 前),全文注入不影响指纹。
|
|
1945
2008
|
this.noteSkillCatalogDigest(src.skills);
|
|
@@ -1949,7 +2012,8 @@ export class ClientSession {
|
|
|
1949
2012
|
const isRemote = isRemoteWorkspaceUri(realCwd);
|
|
1950
2013
|
return {
|
|
1951
2014
|
cwd: formatCwdForPrompt(realCwd),
|
|
1952
|
-
|
|
2015
|
+
// 空串 = 没有 SYSTEM.md(loader 记录的 base 原文,绝不沿用别的会话)。
|
|
2016
|
+
systemPromptFile: promptBase.system || undefined,
|
|
1953
2017
|
builtinSoul: BUILTIN_SOUL,
|
|
1954
2018
|
selectedTools: src.selectedTools,
|
|
1955
2019
|
toolSnippets: src.toolSnippets,
|
|
@@ -1960,7 +2024,7 @@ export class ClientSession {
|
|
|
1960
2024
|
piReadme: PI_DOC_PATHS.readme,
|
|
1961
2025
|
piDocs: PI_DOC_PATHS.docs,
|
|
1962
2026
|
piExamples: PI_DOC_PATHS.examples,
|
|
1963
|
-
appendFiles:
|
|
2027
|
+
appendFiles: [...promptBase.append],
|
|
1964
2028
|
windowsPersona: isRemote
|
|
1965
2029
|
? buildRemoteWorkspacePersona(realCwd)
|
|
1966
2030
|
: process.platform === "win32"
|
|
@@ -1982,13 +2046,16 @@ export class ClientSession {
|
|
|
1982
2046
|
}
|
|
1983
2047
|
/** skill 全文注入({{skills}} 全文模式):最好努力读名单里技能的文件正文。 */
|
|
1984
2048
|
fillSkillContents(skills) {
|
|
1985
|
-
return fillSkillContents(skills, this.settingsSvc.current.skillsFullText
|
|
2049
|
+
return fillSkillContents(skills, this.settingsSvc.current.skillsFullText, {
|
|
2050
|
+
maxFileBytes: this.settingsSvc.current.skillsMaxFileBytes,
|
|
2051
|
+
maxTotalBytes: this.settingsSvc.current.skillsFullTextMaxBytes,
|
|
2052
|
+
});
|
|
1986
2053
|
}
|
|
1987
2054
|
/** 渲染当前组合模板。当存在自定义模板/覆盖,或者当前会话预设非 standard(如 code/minimal/ask/reader),
|
|
1988
2055
|
* 或者存在被禁用的工具时,必须渲染完整系统提示词,保证工具门控、技能隐藏与 Guidelines 严格对齐当前预设;
|
|
1989
2056
|
* 仅在完全默认且全功能 standard 状态下返回 undefined 让 SDK 拼装。 */
|
|
1990
2057
|
renderMainCompose(src) {
|
|
1991
|
-
const preset = src.preset ?? this.
|
|
2058
|
+
const preset = src.preset ?? this.settingsSvc.current.defaultAgentPreset ?? "standard";
|
|
1992
2059
|
const showSkills = presetShowsSkillCatalog(preset);
|
|
1993
2060
|
const effectiveSkills = showSkills ? this.fillSkillContents(src.skills) : [];
|
|
1994
2061
|
return renderMainCompose(this.composeInputs(src), {
|
|
@@ -2071,6 +2138,8 @@ export class ClientSession {
|
|
|
2071
2138
|
filePath: s.filePath ?? "",
|
|
2072
2139
|
})),
|
|
2073
2140
|
preset,
|
|
2141
|
+
// base(SYSTEM.md / APPEND_SYSTEM.md)跟着 loader 走:算哪个会话就读哪个会话。
|
|
2142
|
+
session: sess,
|
|
2074
2143
|
}));
|
|
2075
2144
|
// 模板/覆盖渲染(无自定义模板时用默认模板渲染完整提示词,确保与真实 run 规则一致且包含预设过滤)。
|
|
2076
2145
|
const tpl = (this.settingsSvc.current.promptTemplate ?? "").trim();
|
|
@@ -2178,13 +2247,14 @@ export class ClientSession {
|
|
|
2178
2247
|
await this.communicate(fromRunId, "send", { to: toRunId, message: payload });
|
|
2179
2248
|
}
|
|
2180
2249
|
subagentHost = {
|
|
2250
|
+
resolveConversationId: (id) => this.resolveExecution(id)?.convId,
|
|
2181
2251
|
consultParent: (selfId, questions, signal) => this.consultParent(selfId, questions, signal),
|
|
2182
2252
|
answerConsult: (selfId, runId, requestId, answers) => this.answerConsult(selfId, runId, requestId, answers),
|
|
2183
2253
|
escalateConsult: (selfId, runId, requestId) => this.answerConsult(selfId, runId, requestId),
|
|
2184
2254
|
getPendingConsults: (parentId) => this.getPendingConsults(parentId),
|
|
2185
2255
|
setConsultWaiting: (parentId, waiting) => {
|
|
2186
2256
|
const owner = this.subagentOwnerOf(parentId);
|
|
2187
|
-
const parent = owner.convs.get(
|
|
2257
|
+
const parent = owner.convs.get(this.subagentCurrentId(parentId));
|
|
2188
2258
|
if (parent) {
|
|
2189
2259
|
if (waiting)
|
|
2190
2260
|
consultWaitingParents.add(parent);
|
|
@@ -2201,7 +2271,7 @@ export class ClientSession {
|
|
|
2201
2271
|
},
|
|
2202
2272
|
completeSubagent: (selfId, summary) => {
|
|
2203
2273
|
const owner = this.subagentOwnerOf(selfId);
|
|
2204
|
-
const conv = owner.convs.get(
|
|
2274
|
+
const conv = owner.convs.get(this.subagentCurrentId(selfId));
|
|
2205
2275
|
if (!conv?.parentId)
|
|
2206
2276
|
throw new Error("Only a child task can complete itself.");
|
|
2207
2277
|
conv.subagentTask = requestSubagentCompletion(conv.subagentTask ?? createSubagentTask(conv.notifyOnDone), summary);
|
|
@@ -2215,15 +2285,7 @@ export class ClientSession {
|
|
|
2215
2285
|
await owner.finalizeSubagentWorktree(conv);
|
|
2216
2286
|
}
|
|
2217
2287
|
},
|
|
2218
|
-
getSubagent: (convId) =>
|
|
2219
|
-
// B20:本会话查不到就去「当前持有方」查 —— 过户把子代理整体搬走后,
|
|
2220
|
-
// 旧 host 仍指向源会话(搬不动闭包),不跨会话解析 wait_all/get_result
|
|
2221
|
-
// 会以为子代理「已移出/不存在」而其实还在跑。
|
|
2222
|
-
if (this.convs.has(convId))
|
|
2223
|
-
return this.getSubagentSnapshot(convId);
|
|
2224
|
-
const found = this.findSubagentOwner?.(convId);
|
|
2225
|
-
return found && found.session !== this ? found.session.getSubagentSnapshot(found.convId) : undefined;
|
|
2226
|
-
},
|
|
2288
|
+
getSubagent: (convId) => this.subagentSnapshotAnywhere(convId),
|
|
2227
2289
|
listSubagents: (scope, forSelfId) => this.listSubagentSnapshots(scope, forSelfId),
|
|
2228
2290
|
markSubagentsConsumed: (runIds) => {
|
|
2229
2291
|
for (const id of runIds) {
|
|
@@ -2401,6 +2463,8 @@ export class ClientSession {
|
|
|
2401
2463
|
return createSkillToolHost({
|
|
2402
2464
|
resolveTarget: (oid) => ((oid ?? "").trim() !== "" ? this.convs.get(oid.trim()) : this.convs.get(this.activeId)),
|
|
2403
2465
|
getDisabledSkills: () => this.settingsSvc.current.disabledSkills,
|
|
2466
|
+
// 单文件上限跟随设置(与全文注入同口径);每次调用实时读,改设置不必重建工具。
|
|
2467
|
+
getMaxFileBytes: () => this.settingsSvc.current.skillsMaxFileBytes,
|
|
2404
2468
|
ownerId,
|
|
2405
2469
|
});
|
|
2406
2470
|
}
|
|
@@ -2492,7 +2556,6 @@ export class ClientSession {
|
|
|
2492
2556
|
pendingQuestions = new Map();
|
|
2493
2557
|
/** 任务计划管理器(Plan Mode / Step State Machine)。 */
|
|
2494
2558
|
planManager;
|
|
2495
|
-
approvalSeq = 0;
|
|
2496
2559
|
/** 确保对话拥有待审批项 Map */
|
|
2497
2560
|
ensurePendingApprovals(conv) {
|
|
2498
2561
|
if (!conv.pendingApprovals)
|
|
@@ -2807,18 +2870,29 @@ export class ClientSession {
|
|
|
2807
2870
|
executionMcpBridge(cwd) {
|
|
2808
2871
|
return this.botExecution ? this.botMcpBridge : ClientSession.mcpProjects?.getBridge(cwd);
|
|
2809
2872
|
}
|
|
2810
|
-
|
|
2873
|
+
botToolAuthorization(name, cwd = this.cwd, params, phase = "execute") {
|
|
2811
2874
|
const bot = this.botExecution;
|
|
2812
2875
|
if (!bot)
|
|
2813
|
-
return true;
|
|
2876
|
+
return { allowed: true };
|
|
2814
2877
|
bot.assertActive();
|
|
2878
|
+
if (bot.planOnly && phase === "execute") {
|
|
2879
|
+
const denial = checkGroupTaskPlanGuard(name, params ?? {}, true);
|
|
2880
|
+
if (denial)
|
|
2881
|
+
return { allowed: false, reason: denial.reason };
|
|
2882
|
+
}
|
|
2815
2883
|
const mcp = this.executionMcpBridge(cwd)?.getTools() ?? [];
|
|
2816
2884
|
const matches = mcp.filter((tool) => tool.name === name);
|
|
2817
|
-
if (!bot.allowsTool(name))
|
|
2818
|
-
return false;
|
|
2819
|
-
if (matches.length)
|
|
2820
|
-
return
|
|
2821
|
-
return true;
|
|
2885
|
+
if (!(phase === "register" ? bot.allowsToolRegistration(name) : bot.allowsTool(name, params ?? {})))
|
|
2886
|
+
return { allowed: false, reason: "Tool is not authorized for this Bot" };
|
|
2887
|
+
if (matches.length && !(matches.length === 1 && bot.bot.mcpServers.includes(getMcpToolServer(matches[0]) ?? "")))
|
|
2888
|
+
return { allowed: false, reason: "Bot MCP tool authorization mismatch" };
|
|
2889
|
+
return { allowed: true };
|
|
2890
|
+
}
|
|
2891
|
+
botToolAllowed(name, cwd = this.cwd, params) {
|
|
2892
|
+
return this.botToolAuthorization(name, cwd, params).allowed;
|
|
2893
|
+
}
|
|
2894
|
+
botToolRegistrationAllowed(name, cwd = this.cwd) {
|
|
2895
|
+
return this.botToolAuthorization(name, cwd, undefined, "register").allowed;
|
|
2822
2896
|
}
|
|
2823
2897
|
static async create(clientId, cwd, stateStore, opts) {
|
|
2824
2898
|
const botExecution = getBotExecution(clientId);
|
|
@@ -3016,96 +3090,97 @@ export class ClientSession {
|
|
|
3016
3090
|
await ClientSession.mcpProjects?.prepare(effectiveCwd);
|
|
3017
3091
|
const sdkBuiltins = sandboxIdentity || this.botExecution ? [] : await getSdkBuiltinExtensionFactories();
|
|
3018
3092
|
const sandboxRuntimeGate = createSandboxRuntimeGate(() => this.sandboxes.getExecutionIdentity(effectiveCwd, sandboxIdentity), effectiveCwd);
|
|
3093
|
+
// 本次 runtime 的提示词 base(SYSTEM.md / APPEND_SYSTEM.md 原文)—— 由
|
|
3094
|
+
// prompt-base-store 登记到**本 loader**(session 本体)名下,而不是本客户端:
|
|
3095
|
+
// base 缺失就写空、每次 reload 覆盖,所以切项目不可能沿用上一个项目的内容,
|
|
3096
|
+
// 过户搬 session 时记录也跟着走(issue #812)。
|
|
3097
|
+
const promptBase = createPromptBaseObservers({
|
|
3098
|
+
// 返回给 SDK 的值:主会话原样返回 base —— 默认分支据此保留 .pi/SYSTEM.md
|
|
3099
|
+
// (TUI 同语义);以前恒返回 undefined,组合模板未激活时 SYSTEM.md 被静默
|
|
3100
|
+
// 丢弃。组合模板激活时 before_agent_start 整体换掉提示词,二者不叠加。
|
|
3101
|
+
// 子代理模板 replace:有 base 时整体替换(无 base 时留给下方 persona
|
|
3102
|
+
// 扩展做灵魂段替换,保留工具/文档自动段)。
|
|
3103
|
+
system: (base) => base && apply && apply.promptMode === "replace" && this.templatePrompt(apply).trim()
|
|
3104
|
+
? this.templatePrompt(apply)
|
|
3105
|
+
: base,
|
|
3106
|
+
// APPEND_SYSTEM.md 之上再叠运行时段(模板 append / 计划模式 / 审查者 / 沙箱 /
|
|
3107
|
+
// 平台 persona / 终端引导 / 标记工具引导)。base 已由工厂登记给 loader,
|
|
3108
|
+
// {{append}} 用的就是那份原文 —— 这里 push 的额外段不会回灌到 composer。
|
|
3109
|
+
append: (base) => {
|
|
3110
|
+
const out = [...base];
|
|
3111
|
+
if (apply && apply.promptMode === "append" && this.templatePrompt(apply).trim()) {
|
|
3112
|
+
out.push(this.templatePrompt(apply));
|
|
3113
|
+
}
|
|
3114
|
+
// 主会话自定义「追加」已并入组合模板的 {{append}} 覆盖,不再在此注入。
|
|
3115
|
+
// 计划模式:只规划不实施 —— 硬闸门(server/plan-mode.ts 拒写类/旁路
|
|
3116
|
+
// 工具与非常规 bash)之外再补一段软约束,每轮重建提示词时读当前
|
|
3117
|
+
// 会话状态,切换后下一次 reload 生效。提示词正文可在设置面板改
|
|
3118
|
+
// (追加/替换内置默认,见 buildPlanModePrompt)。
|
|
3119
|
+
// ⚠️ 这一段与平台无关(曾在 win32 分支里被误嵌套,导致非 Windows
|
|
3120
|
+
// 上计划模式的软约束从来没进过提示词,只有硬闸门在挡)。
|
|
3121
|
+
if (this.planModeOf(ownerId)) {
|
|
3122
|
+
const planPrefs = this.settingsSvc.current;
|
|
3123
|
+
out.push(buildPlanModePrompt(planPrefs.planModePromptMode, planPrefs.planModePrompt));
|
|
3124
|
+
}
|
|
3125
|
+
// 审查者模式(自动委派):本对话只审阅,活由服务端派给常驻执行对话。
|
|
3126
|
+
// 与计划模式**互斥优先级**:计划模式开着时以计划模式为准,不派活。
|
|
3127
|
+
if (this.delegateModeOf(ownerId) && !this.planModeOf(ownerId)) {
|
|
3128
|
+
out.push(DELEGATION_SYSTEM_PROMPT);
|
|
3129
|
+
}
|
|
3130
|
+
const activeSandbox = this.sandboxes.getExecutionIdentity(effectiveCwd, sandboxIdentity);
|
|
3131
|
+
if (activeSandbox) {
|
|
3132
|
+
out.push(`[Sandbox Isolation Mode] You are operating inside an isolated sandbox workspace "${activeSandbox.name}" at "${activeSandbox.path}".` +
|
|
3133
|
+
(activeSandbox.sourcePath
|
|
3134
|
+
? ` This sandbox is an isolated copy of "${activeSandbox.sourcePath}". NEVER read from or modify "${activeSandbox.sourcePath}" directly; make all changes exclusively within the current sandbox directory "${activeSandbox.path}".`
|
|
3135
|
+
: ` Keep all file operations strictly inside "${activeSandbox.path}".`) +
|
|
3136
|
+
" Only restricted local file editing is supported. Shell commands, scripts, terminals, evaluation, and unknown tools are unavailable without an OS isolation backend.");
|
|
3137
|
+
}
|
|
3138
|
+
const realCwd = restoreWorkspaceUriFromSessionDir(effectiveCwd);
|
|
3139
|
+
if (isRemoteWorkspaceUri(realCwd)) {
|
|
3140
|
+
// 远程 SSH 工作区:明确告知 AI 当前处于远程主机以及所有工具已透明桥接至远端,
|
|
3141
|
+
// 绝不注入本地 WINDOWS_PERSONA 以免误导模型以为在本地 Windows 环境。
|
|
3142
|
+
out.push(buildRemoteWorkspacePersona(realCwd));
|
|
3143
|
+
}
|
|
3144
|
+
else if (process.platform === "win32") {
|
|
3145
|
+
// Windows 专属 persona:bash 工具跑 Git Bash 且无默认超时、终端
|
|
3146
|
+
// 是交互式 TTY——注入约束避免 heredoc/交互/长驻命令挂死整个会话;
|
|
3147
|
+
// GBK 老中文文件让模型改用终端按正确编码读(iconv/chcp/Get-Content)。
|
|
3148
|
+
out.push(WINDOWS_PERSONA);
|
|
3149
|
+
}
|
|
3150
|
+
// 终端引导只教「开关开着且预设下仍可用」的工具(见 tool-manager.ts 语义总表)。
|
|
3151
|
+
const presetForGuidance = this.convs.get(ownerId ?? "")?.agentPreset ?? this.settingsSvc.current.defaultAgentPreset ?? "standard";
|
|
3152
|
+
if (isTerminalGuidanceOn(effectiveDisabledAgentTools(this.settingsSvc.current), presetForGuidance)) {
|
|
3153
|
+
// 终端工具使用引导(全平台):告诉模型什么场景该用持久终端
|
|
3154
|
+
// 而不是一次性 bash——没有这段模型几乎从不主动选终端工具。
|
|
3155
|
+
// 组内工具全关时不注入(不教 AI 用不存在的工具)。
|
|
3156
|
+
out.push(TERMINAL_TOOLS_GUIDANCE);
|
|
3157
|
+
}
|
|
3158
|
+
// bash 管道限制已并入 bash 工具自身的 description,不再作为独立提示段注入。
|
|
3159
|
+
// 内置标记工具引导(按总开关/分组开关过滤)
|
|
3160
|
+
const markerGuidance = this.markerSvc.buildGuidance();
|
|
3161
|
+
if (markerGuidance)
|
|
3162
|
+
out.push(markerGuidance);
|
|
3163
|
+
return out;
|
|
3164
|
+
},
|
|
3165
|
+
});
|
|
3019
3166
|
const services = await createAgentSessionServices({
|
|
3020
3167
|
cwd: effectiveCwd,
|
|
3021
3168
|
modelRuntime: this.sharedModelRuntime,
|
|
3022
|
-
// 设置面板钩子(官方 SDK 的 resourceLoader overrides
|
|
3023
|
-
//
|
|
3024
|
-
//
|
|
3025
|
-
//
|
|
3026
|
-
// 子代理带模板(apply)时:prompt/skills/extensions 改读模板视图——
|
|
3027
|
-
//
|
|
3028
|
-
//
|
|
3029
|
-
// 整体替换 base。append 模式把模板提示词追加到
|
|
3030
|
-
// 末尾(此时主会话的自定义 prompt 不再叠加,角色由模板定义);非空
|
|
3169
|
+
// 设置面板钩子(官方 SDK 的 resourceLoader overrides):override 在每次
|
|
3170
|
+
// resourceLoader.reload() 时重放,且读取 this.settings 的当前值——因此
|
|
3171
|
+
// session.reload() 即可让系统提示词 / 技能 / 插件开关生效,新对话(新 runtime)
|
|
3172
|
+
// 也会自动带上当前设置。
|
|
3173
|
+
// 子代理带模板(apply)时:prompt/skills/extensions 改读模板视图——replace 模式
|
|
3174
|
+
// 把基座换成模板提示词(无 SYSTEM.md 时由下方 pi-webui-persona 内联扩展换掉
|
|
3175
|
+
// 灵魂段),append 模式把模板提示词追加到末尾(此时角色由模板定义);非空
|
|
3031
3176
|
// 白名单取代主会话开关(只启用这些),空白名单 = 跟随主会话。
|
|
3032
3177
|
resourceLoaderOptions: {
|
|
3033
3178
|
// Filter before import: extensionsOverride runs after arbitrary module initialization.
|
|
3034
3179
|
...sandboxExtensionOptions(Boolean(sandboxIdentity) || Boolean(this.botExecution)),
|
|
3035
|
-
//
|
|
3036
|
-
//
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
systemPromptOverride: (base) => {
|
|
3040
|
-
if (typeof base === "string" && base) {
|
|
3041
|
-
this.lastBaseSystemPrompt = base;
|
|
3042
|
-
if (apply && apply.promptMode === "replace" && this.templatePrompt(apply).trim()) {
|
|
3043
|
-
return this.templatePrompt(apply);
|
|
3044
|
-
}
|
|
3045
|
-
}
|
|
3046
|
-
return undefined;
|
|
3047
|
-
},
|
|
3048
|
-
appendSystemPromptOverride: (base) => {
|
|
3049
|
-
// 记录 SDK APPEND_SYSTEM.md base(composer {{append}} 自动内容)。
|
|
3050
|
-
if (!apply)
|
|
3051
|
-
this.lastSdkAppendFiles = base.slice();
|
|
3052
|
-
const out = [...base];
|
|
3053
|
-
if (this.botExecution?.bot.systemPrompt)
|
|
3054
|
-
out.push(this.botExecution.bot.systemPrompt);
|
|
3055
|
-
if (apply && apply.promptMode === "append" && this.templatePrompt(apply).trim()) {
|
|
3056
|
-
out.push(this.templatePrompt(apply));
|
|
3057
|
-
}
|
|
3058
|
-
// 主会话自定义「追加」已并入组合模板的 {{append}} 覆盖,不再在此注入。
|
|
3059
|
-
// 计划模式:只规划不实施 —— 硬闸门(server/plan-mode.ts 拒写类/旁路
|
|
3060
|
-
// 工具与非常规 bash)之外再补一段软约束,每轮重建提示词时读当前
|
|
3061
|
-
// 会话状态,切换后下一次 reload 生效。提示词正文可在设置面板改
|
|
3062
|
-
// (追加/替换内置默认,见 buildPlanModePrompt)。
|
|
3063
|
-
// ⚠️ 这一段与平台无关(曾在 win32 分支里被误嵌套,导致非 Windows
|
|
3064
|
-
// 上计划模式的软约束从来没进过提示词,只有硬闸门在挡)。
|
|
3065
|
-
if (this.planModeOf(ownerId)) {
|
|
3066
|
-
const planPrefs = this.settingsSvc.current;
|
|
3067
|
-
out.push(buildPlanModePrompt(planPrefs.planModePromptMode, planPrefs.planModePrompt));
|
|
3068
|
-
}
|
|
3069
|
-
// 审查者模式(自动委派):本对话只审阅,活由服务端派给常驻执行对话。
|
|
3070
|
-
// 与计划模式**互斥优先级**:计划模式开着时以计划模式为准,不派活。
|
|
3071
|
-
if (this.delegateModeOf(ownerId) && !this.planModeOf(ownerId)) {
|
|
3072
|
-
out.push(DELEGATION_SYSTEM_PROMPT);
|
|
3073
|
-
}
|
|
3074
|
-
const activeSandbox = this.sandboxes.getExecutionIdentity(effectiveCwd, sandboxIdentity);
|
|
3075
|
-
if (activeSandbox) {
|
|
3076
|
-
out.push(`[Sandbox Isolation Mode] You are operating inside an isolated sandbox workspace "${activeSandbox.name}" at "${activeSandbox.path}".` +
|
|
3077
|
-
(activeSandbox.sourcePath
|
|
3078
|
-
? ` This sandbox is an isolated copy of "${activeSandbox.sourcePath}". NEVER read from or modify "${activeSandbox.sourcePath}" directly; make all changes exclusively within the current sandbox directory "${activeSandbox.path}".`
|
|
3079
|
-
: ` Keep all file operations strictly inside "${activeSandbox.path}".`) +
|
|
3080
|
-
" Only restricted local file editing is supported. Shell commands, scripts, terminals, evaluation, and unknown tools are unavailable without an OS isolation backend.");
|
|
3081
|
-
}
|
|
3082
|
-
const realCwd = restoreWorkspaceUriFromSessionDir(effectiveCwd);
|
|
3083
|
-
if (isRemoteWorkspaceUri(realCwd)) {
|
|
3084
|
-
// 远程 SSH 工作区:明确告知 AI 当前处于远程主机以及所有工具已透明桥接至远端,
|
|
3085
|
-
// 绝不注入本地 WINDOWS_PERSONA 以免误导模型以为在本地 Windows 环境。
|
|
3086
|
-
out.push(buildRemoteWorkspacePersona(realCwd));
|
|
3087
|
-
}
|
|
3088
|
-
else if (process.platform === "win32") {
|
|
3089
|
-
// Windows 专属 persona:bash 工具跑 Git Bash 且无默认超时、终端
|
|
3090
|
-
// 是交互式 TTY——注入约束避免 heredoc/交互/长驻命令挂死整个会话;
|
|
3091
|
-
// GBK 老中文文件让模型改用终端按正确编码读(iconv/chcp/Get-Content)。
|
|
3092
|
-
out.push(WINDOWS_PERSONA);
|
|
3093
|
-
}
|
|
3094
|
-
// 终端引导只教「开关开着且预设下仍可用」的工具(见 tool-manager.ts 语义总表)。
|
|
3095
|
-
const presetForGuidance = this.convs.get(ownerId ?? "")?.agentPreset ?? this.settingsSvc.current.defaultAgentPreset ?? "standard";
|
|
3096
|
-
if (isTerminalGuidanceOn(effectiveDisabledAgentTools(this.settingsSvc.current), presetForGuidance)) {
|
|
3097
|
-
// 终端工具使用引导(全平台):告诉模型什么场景该用持久终端
|
|
3098
|
-
// 而不是一次性 bash——没有这段模型几乎从不主动选终端工具。
|
|
3099
|
-
// 组内工具全关时不注入(不教 AI 用不存在的工具)。
|
|
3100
|
-
out.push(TERMINAL_TOOLS_GUIDANCE);
|
|
3101
|
-
}
|
|
3102
|
-
// bash 管道限制已并入 bash 工具自身的 description,不再作为独立提示段注入。
|
|
3103
|
-
// 内置标记工具引导(按总开关/分组开关过滤)
|
|
3104
|
-
const markerGuidance = this.markerSvc.buildGuidance();
|
|
3105
|
-
if (markerGuidance)
|
|
3106
|
-
out.push(markerGuidance);
|
|
3107
|
-
return out;
|
|
3108
|
-
},
|
|
3180
|
+
// 提示词基座(SYSTEM.md / APPEND_SYSTEM.md):登记归属 + 决定返回给 SDK 的值,
|
|
3181
|
+
// 见 prompt-base-store.ts(issue #812)。
|
|
3182
|
+
systemPromptOverride: promptBase.systemPromptOverride,
|
|
3183
|
+
appendSystemPromptOverride: promptBase.appendSystemPromptOverride,
|
|
3109
3184
|
// 技能:模板非空白名单时只启用白名单里的(显式配置优先于预设);
|
|
3110
3185
|
// 否则按主会话禁用集过滤。预设拿掉 skill 加载器时(minimal/code/ask)
|
|
3111
3186
|
// 名录同步隐藏——列出来但调不动只是噪音(见 tool-manager.ts 语义总表)。
|
|
@@ -3163,8 +3238,9 @@ export class ClientSession {
|
|
|
3163
3238
|
pi.on("tool_call", (event) => {
|
|
3164
3239
|
if (this.botExecution) {
|
|
3165
3240
|
try {
|
|
3166
|
-
|
|
3167
|
-
|
|
3241
|
+
const auth = this.botToolAuthorization(event.toolName, effectiveCwd, event.input);
|
|
3242
|
+
if (!auth.allowed)
|
|
3243
|
+
return { block: true, reason: auth.reason ?? "Tool is not authorized for this Bot" };
|
|
3168
3244
|
}
|
|
3169
3245
|
catch {
|
|
3170
3246
|
return { block: true, reason: "Bot execution authorization expired" };
|
|
@@ -3174,6 +3250,35 @@ export class ClientSession {
|
|
|
3174
3250
|
});
|
|
3175
3251
|
},
|
|
3176
3252
|
},
|
|
3253
|
+
{
|
|
3254
|
+
name: "pi-webui-device-group-context",
|
|
3255
|
+
hidden: true,
|
|
3256
|
+
factory: (pi) => {
|
|
3257
|
+
// SDK context runs after per-turn and virtual-model compaction,
|
|
3258
|
+
// immediately before conversion to provider messages (also on tool continuation).
|
|
3259
|
+
pi.on("context", (event, ctx) => {
|
|
3260
|
+
const bot = this.botExecution;
|
|
3261
|
+
if (!bot?.groupContext)
|
|
3262
|
+
return;
|
|
3263
|
+
const messages = repairGroupContextRequest(bot.groupContext, event.messages, bot.bot.id);
|
|
3264
|
+
const metrics = messages !== event.messages
|
|
3265
|
+
? messages.at(-1).details.metrics
|
|
3266
|
+
: extractGroupContextMetrics(bot.groupContext, [], "", "none", true);
|
|
3267
|
+
recordGroupContextMetrics(ctx.sessionManager.getSessionId(), metrics);
|
|
3268
|
+
if (messages !== event.messages)
|
|
3269
|
+
return { messages };
|
|
3270
|
+
});
|
|
3271
|
+
pi.on("before_agent_start", (_event, ctx) => {
|
|
3272
|
+
const bot = this.botExecution;
|
|
3273
|
+
const current = bot?.groupContext;
|
|
3274
|
+
if (!current)
|
|
3275
|
+
return;
|
|
3276
|
+
const message = groupContextMessage(current, ctx.sessionManager.buildSessionProjection().messages, bot.bot.id);
|
|
3277
|
+
recordGroupContextMetrics(ctx.sessionManager.getSessionId(), message.details.metrics);
|
|
3278
|
+
return { message };
|
|
3279
|
+
});
|
|
3280
|
+
},
|
|
3281
|
+
},
|
|
3177
3282
|
{
|
|
3178
3283
|
name: "pi-webui-persona",
|
|
3179
3284
|
hidden: true,
|
|
@@ -3182,8 +3287,7 @@ export class ClientSession {
|
|
|
3182
3287
|
// 我们是链上最后一个 handler:先把早前扩展的首尾增补摘出来(pre/post),
|
|
3183
3288
|
// 只在 SDK 原始提示词 core 上做事,最后原样套回去——否则返回 systemPrompt
|
|
3184
3289
|
// 会整体替换,把别的扩展注入的内容(如 <invoked_skill>)一起丢掉。
|
|
3185
|
-
const {
|
|
3186
|
-
const rewrap = (next) => pre + next + post;
|
|
3290
|
+
const { core, rewrap } = prepareBotExecutionSystemPrompt(event, this.botExecution);
|
|
3187
3291
|
// 子代理模板 replace(无 SYSTEM.md 时):默认分支拼好的提示词里
|
|
3188
3292
|
// 把灵魂段换成模板提示词,自动段保留;SYSTEM.md 情形已在
|
|
3189
3293
|
// systemPromptOverride 整体替换,此处边界不存在会自然跳过。
|
|
@@ -3253,6 +3357,9 @@ export class ClientSession {
|
|
|
3253
3357
|
filePath: s.filePath ?? "",
|
|
3254
3358
|
})),
|
|
3255
3359
|
preset: currentPreset,
|
|
3360
|
+
// SYSTEM.md / APPEND_SYSTEM.md 的 base 取自**本会话**的 loader,不是
|
|
3361
|
+
// 「最近一次 reload 观察到」的客户端级字段(issue #812)。
|
|
3362
|
+
session: sess ?? undefined,
|
|
3256
3363
|
});
|
|
3257
3364
|
if (rendered) {
|
|
3258
3365
|
return { systemPrompt: rewrap(rendered) };
|
|
@@ -3271,6 +3378,9 @@ export class ClientSession {
|
|
|
3271
3378
|
],
|
|
3272
3379
|
},
|
|
3273
3380
|
});
|
|
3381
|
+
// override 的首次重放发生在 createAgentSessionServices 内部(那时还没有加载器
|
|
3382
|
+
// 句柄):拿到 loader 后补登记一次,后续 session.reload() 直接按 loader 写。
|
|
3383
|
+
promptBase.bind(services.resourceLoader);
|
|
3274
3384
|
// 桥接工具目标(问卷 / 页面):每次调用都解析「现在谁持有这条对话」,
|
|
3275
3385
|
// 而不是认这个 runtime 是在哪个 ClientSession 里建出来的(过户会换主)。
|
|
3276
3386
|
// anchor 在拿到 created.session 后回填(SDK 的 runtime.session 就是它)。
|
|
@@ -3457,9 +3567,11 @@ export class ClientSession {
|
|
|
3457
3567
|
? makeSubagentTools(withSubagentOwner(this.communicationBoundSubagentHost(bridgeAnchor), ownerId, () => this.requireSubagentOwner(bridgeAnchor, ownerId)), undefined,
|
|
3458
3568
|
// B20:selfId 必须调用时刻解析,不能死用建时旧 id —— 过户撞 id
|
|
3459
3569
|
// 改名后 wait_all 的「排除自身/展开后代」要按 runtime 的当前归属查。
|
|
3460
|
-
() =>
|
|
3461
|
-
|
|
3462
|
-
|
|
3570
|
+
() => {
|
|
3571
|
+
const home = bridgeAnchor.session && this.findConversationHome?.(bridgeAnchor.session);
|
|
3572
|
+
const conv = home?.session.convs.get(home.convId);
|
|
3573
|
+
return conv ? conversationIdentity(conv) : undefined;
|
|
3574
|
+
})
|
|
3463
3575
|
: makeSubagentTools(this.subagentHost)),
|
|
3464
3576
|
makeCommunicateTool({
|
|
3465
3577
|
communicate: (action, args, signal) => this.communicateFromSession(bridgeAnchor, action, args, signal),
|
|
@@ -3470,7 +3582,7 @@ export class ClientSession {
|
|
|
3470
3582
|
? []
|
|
3471
3583
|
: ownerId
|
|
3472
3584
|
? [
|
|
3473
|
-
makeDelegateTaskTool(withSubagentOwner(this.
|
|
3585
|
+
makeDelegateTaskTool(withSubagentOwner(this.communicationBoundSubagentHost(bridgeAnchor), ownerId, () => this.requireSubagentOwner(bridgeAnchor, ownerId))),
|
|
3474
3586
|
]
|
|
3475
3587
|
: [makeDelegateTaskTool(this.subagentHost)]),
|
|
3476
3588
|
// 结构化任务计划更新(Plan Mode / Step State Machine)。
|
|
@@ -3484,7 +3596,7 @@ export class ClientSession {
|
|
|
3484
3596
|
};
|
|
3485
3597
|
}, (msg) => this.emit(msg), () => this.flushSnapshot()),
|
|
3486
3598
|
// 标准引擎的 ask_user_question:模型调用 → 浏览器富渲染问卷(复用 DSH
|
|
3487
|
-
// 的 question_pending/question_answer 协议,前端
|
|
3599
|
+
// 的 question_pending/question_answer 协议,前端 QuestionDialog)。
|
|
3488
3600
|
// DSH 引擎不经此(它走 goal-rpc 的 userQuestions provider)。
|
|
3489
3601
|
makeAskUserQuestionTool(bridge, ownerId),
|
|
3490
3602
|
// 浏览器页面工具:模型调用 → page_request 给浏览器 → page-picker 扩展
|
|
@@ -3639,16 +3751,147 @@ export class ClientSession {
|
|
|
3639
3751
|
makeGoalStatus() {
|
|
3640
3752
|
return this.goalSvc.makeGoalStatus();
|
|
3641
3753
|
}
|
|
3754
|
+
readConversationIndependent(session) {
|
|
3755
|
+
let independent = !isPseudoClientId(this.clientId ?? "") && !this.clientId?.startsWith("device-task-");
|
|
3756
|
+
for (const entry of session.sessionManager.getEntries()) {
|
|
3757
|
+
if (entry.type === "custom" && entry.customType === "webui/independent")
|
|
3758
|
+
independent = entry.data?.independent !== false;
|
|
3759
|
+
}
|
|
3760
|
+
return independent;
|
|
3761
|
+
}
|
|
3762
|
+
setConversationIndependent(id, independent) {
|
|
3763
|
+
const conv = this.convs.get(id);
|
|
3764
|
+
if (!conv)
|
|
3765
|
+
return;
|
|
3766
|
+
conv.session.sessionManager.appendCustomEntry("webui/independent", { independent });
|
|
3767
|
+
conv.independent = independent;
|
|
3768
|
+
this.emitConversations();
|
|
3769
|
+
}
|
|
3642
3770
|
/** Allocate a stable conversation id before constructing its runtime/tools. */
|
|
3643
3771
|
nextConversationId() {
|
|
3644
3772
|
return `c${++this.convSeq}`;
|
|
3645
3773
|
}
|
|
3646
3774
|
/** Wrap a fresh runtime as a new conversation record. */
|
|
3775
|
+
conversationViewers = new Map();
|
|
3776
|
+
sharedTerminalSinks = new Map();
|
|
3777
|
+
emitSharedInteraction(msg) {
|
|
3778
|
+
this.emit(msg);
|
|
3779
|
+
}
|
|
3780
|
+
sharedPendingMessages() {
|
|
3781
|
+
const messages = [];
|
|
3782
|
+
for (const request of this.pendingQuestions.values()) {
|
|
3783
|
+
if (request.settled || request.audience !== "user")
|
|
3784
|
+
continue;
|
|
3785
|
+
messages.push({
|
|
3786
|
+
type: "question_pending",
|
|
3787
|
+
id: request.id,
|
|
3788
|
+
questions: request.questions,
|
|
3789
|
+
deadline: request.deadline,
|
|
3790
|
+
conversationId: request.conversationId,
|
|
3791
|
+
conversationTitle: this.convs.get(request.conversationId)?.title,
|
|
3792
|
+
});
|
|
3793
|
+
}
|
|
3794
|
+
for (const approval of this.pendingApprovalsForSnapshot())
|
|
3795
|
+
messages.push({ type: "tool_approval_pending", ...approval });
|
|
3796
|
+
return messages;
|
|
3797
|
+
}
|
|
3798
|
+
subscribeConversation(conv, send) {
|
|
3799
|
+
if (this.convs.get(conv.id) !== conv)
|
|
3800
|
+
throw new Error("Conversation is no longer available");
|
|
3801
|
+
let viewers = this.conversationViewers.get(conv);
|
|
3802
|
+
if (!viewers)
|
|
3803
|
+
this.conversationViewers.set(conv, (viewers = new Set()));
|
|
3804
|
+
let timer;
|
|
3805
|
+
let closed = false;
|
|
3806
|
+
let previous;
|
|
3807
|
+
const pushNow = () => {
|
|
3808
|
+
timer = undefined;
|
|
3809
|
+
if (closed || this.convs.get(conv.id) !== conv)
|
|
3810
|
+
return;
|
|
3811
|
+
const state = this.snapshotForConversation(conv);
|
|
3812
|
+
const prior = previous;
|
|
3813
|
+
previous = state;
|
|
3814
|
+
const appendOnly = prior &&
|
|
3815
|
+
state.sessionId === prior.sessionId &&
|
|
3816
|
+
state.messages.length >= prior.messages.length &&
|
|
3817
|
+
prior.messages.every((message, index) => state.messages[index] === message);
|
|
3818
|
+
if (appendOnly) {
|
|
3819
|
+
const { messages, ...light } = state;
|
|
3820
|
+
send({
|
|
3821
|
+
type: "snapshot_delta",
|
|
3822
|
+
conversationId: conv.id,
|
|
3823
|
+
rev: state.rev,
|
|
3824
|
+
baseRev: prior.rev,
|
|
3825
|
+
appended: messages.slice(prior.messages.length),
|
|
3826
|
+
state: light,
|
|
3827
|
+
});
|
|
3828
|
+
}
|
|
3829
|
+
else
|
|
3830
|
+
send({ type: "snapshot", state });
|
|
3831
|
+
};
|
|
3832
|
+
const push = () => {
|
|
3833
|
+
if (!timer && !closed)
|
|
3834
|
+
timer = setTimeout(pushNow, 60);
|
|
3835
|
+
};
|
|
3836
|
+
const deltaSink = (message) => {
|
|
3837
|
+
if (!closed &&
|
|
3838
|
+
this.convs.get(conv.id) === conv &&
|
|
3839
|
+
(message.type === "message_delta" || message.type === "tool_delta" || message.type === "terminal_list"
|
|
3840
|
+
? message.conversationId === conv.id
|
|
3841
|
+
: (message.type === "terminal_output" || message.type === "terminal_exit") &&
|
|
3842
|
+
message.conversationId === conv.id &&
|
|
3843
|
+
message.ownerKind === "conversation"))
|
|
3844
|
+
send(message);
|
|
3845
|
+
};
|
|
3846
|
+
let terminalSinks = this.sharedTerminalSinks.get(conv);
|
|
3847
|
+
if (!terminalSinks)
|
|
3848
|
+
this.sharedTerminalSinks.set(conv, (terminalSinks = new Set()));
|
|
3849
|
+
terminalSinks.add(deltaSink);
|
|
3850
|
+
this.attachSink(deltaSink);
|
|
3851
|
+
viewers.add(push);
|
|
3852
|
+
pushNow();
|
|
3853
|
+
return () => {
|
|
3854
|
+
closed = true;
|
|
3855
|
+
if (timer)
|
|
3856
|
+
clearTimeout(timer);
|
|
3857
|
+
this.detachSink(deltaSink);
|
|
3858
|
+
terminalSinks.delete(deltaSink);
|
|
3859
|
+
if (!terminalSinks.size)
|
|
3860
|
+
this.sharedTerminalSinks.delete(conv);
|
|
3861
|
+
viewers.delete(push);
|
|
3862
|
+
if (!viewers.size)
|
|
3863
|
+
this.conversationViewers.delete(conv);
|
|
3864
|
+
};
|
|
3865
|
+
}
|
|
3866
|
+
snapshotForConversation(conv) {
|
|
3867
|
+
if (this.convs.get(conv.id) !== conv)
|
|
3868
|
+
throw new Error("Conversation is no longer available");
|
|
3869
|
+
const state = this.buildLightState(++this.snapRev, true, conv);
|
|
3870
|
+
return {
|
|
3871
|
+
...state,
|
|
3872
|
+
pendingApprovals: state.pendingApprovals?.filter((entry) => entry.conversationId === conv.id),
|
|
3873
|
+
cwd: conv.cwd,
|
|
3874
|
+
messages: this.messagesOf(conv),
|
|
3875
|
+
};
|
|
3876
|
+
}
|
|
3877
|
+
async abortConversation(conv) {
|
|
3878
|
+
if (this.convs.get(conv.id) !== conv)
|
|
3879
|
+
return;
|
|
3880
|
+
conv.goalGeneration++;
|
|
3881
|
+
conv.queueSteering = [];
|
|
3882
|
+
conv.queueFollowUp = [];
|
|
3883
|
+
conv.queuedSteeringEntries = [];
|
|
3884
|
+
conv.queuedFollowUpEntries = [];
|
|
3885
|
+
await this.interruptRun(conv, "已停止");
|
|
3886
|
+
for (const push of this.conversationViewers?.get(conv) ?? [])
|
|
3887
|
+
push();
|
|
3888
|
+
}
|
|
3647
3889
|
makeConversation(runtime, id, terminals) {
|
|
3648
3890
|
const conv = {
|
|
3649
3891
|
id,
|
|
3650
3892
|
title: conversationTitle(runtime.session),
|
|
3651
3893
|
isSubagent: false,
|
|
3894
|
+
independent: this.readConversationIndependent(runtime.session),
|
|
3652
3895
|
runtime,
|
|
3653
3896
|
session: runtime.session,
|
|
3654
3897
|
cwd: restoreWorkspaceUriFromSessionDir(runtime.cwd),
|
|
@@ -3692,6 +3935,7 @@ export class ClientSession {
|
|
|
3692
3935
|
toolWatchdogs: new Map(),
|
|
3693
3936
|
workspaceSnapshots: readWorkspaceSnapshotsFromSession(runtime.session.sessionManager),
|
|
3694
3937
|
};
|
|
3938
|
+
conversationIdentity(conv);
|
|
3695
3939
|
this.restorePlan(conv);
|
|
3696
3940
|
return conv;
|
|
3697
3941
|
}
|
|
@@ -4095,6 +4339,7 @@ export class ClientSession {
|
|
|
4095
4339
|
this.onRunEvent({
|
|
4096
4340
|
...ev,
|
|
4097
4341
|
conversationId: conv.id,
|
|
4342
|
+
sessionId: conv.session.sessionId,
|
|
4098
4343
|
clientId: this.clientId,
|
|
4099
4344
|
// 通道插件按它认领「自己那一轮」——过户换持有客户端、对话被改 id 都不影响。
|
|
4100
4345
|
...(conv.pluginRunId ? { runId: conv.pluginRunId } : {}),
|
|
@@ -4890,6 +5135,10 @@ export class ClientSession {
|
|
|
4890
5135
|
// Deferred settings reload: settings (system prompt / skills /
|
|
4891
5136
|
// extensions) changed while the run was streaming — applying now
|
|
4892
5137
|
// would have torn down the in-flight run.
|
|
5138
|
+
const settingsTarget = this.conversationSettingsTargets.get(conv);
|
|
5139
|
+
if (settingsTarget && this.settingsSvc.consumePendingReload(settingsTarget) && !this.disposed) {
|
|
5140
|
+
void this.settingsSvc.applyRuntime(false, settingsTarget).catch(() => { });
|
|
5141
|
+
}
|
|
4893
5142
|
if (this.settingsSvc.hasPendingReload() && !this.disposed) {
|
|
4894
5143
|
this.settingsSvc.consumePendingReload();
|
|
4895
5144
|
void this.applySettingsReload();
|
|
@@ -4951,6 +5200,12 @@ export class ClientSession {
|
|
|
4951
5200
|
break;
|
|
4952
5201
|
}
|
|
4953
5202
|
case "agent_settled": {
|
|
5203
|
+
// agent_end can precede both SDK idle and kickoff's finally. Retry at
|
|
5204
|
+
// each settlement boundary; the scheduler rechecks manual dependencies.
|
|
5205
|
+
const owner = this.findConversationHome?.(conv.session)?.session ?? this;
|
|
5206
|
+
if (owner.convs.get(conv.id) === conv && conv.isSubagent && !conv.promptInFlight && !conv.session.isStreaming) {
|
|
5207
|
+
owner.scheduleSubagentCompletionNotice(conv);
|
|
5208
|
+
}
|
|
4954
5209
|
// 记录本会话自己的 Base 基线:currentBaseTokens 必须传 conv(否则串成
|
|
4955
5210
|
// 活跃会话的基线);快照未就绪时保持 undefined,宁可少补偿也不把 0 钉死。
|
|
4956
5211
|
// 只有在真正获得了有效的大模型回复 usage 时才落账(被 abort 或网络失败全 0 不落账,
|
|
@@ -5060,7 +5315,7 @@ export class ClientSession {
|
|
|
5060
5315
|
// stale state. Only the ACTIVE conversation streams to the browser —
|
|
5061
5316
|
// background conversations would clobber the streaming view; their
|
|
5062
5317
|
// state arrives via snapshot when switched to.
|
|
5063
|
-
if (conv.id !== this.conv.id)
|
|
5318
|
+
if (conv.id !== this.conv.id && !this.conversationViewers?.has(conv))
|
|
5064
5319
|
break;
|
|
5065
5320
|
const ame = event.assistantMessageEvent;
|
|
5066
5321
|
const m = event.message;
|
|
@@ -5106,6 +5361,8 @@ export class ClientSession {
|
|
|
5106
5361
|
// #259 实测 8 子代理 × 5 次 bash → 8 条全量快照共 39.5MB,而激活对话一个
|
|
5107
5362
|
// 字节都没变。后台对话的内容在切过去时取(switch_session / get_state 强制
|
|
5108
5363
|
// 全量),它在左栏的运行态由 emitConversations 走另一条通道。
|
|
5364
|
+
for (const push of this.conversationViewers?.get(conv) ?? [])
|
|
5365
|
+
push();
|
|
5109
5366
|
if (conv.id !== this.conv.id)
|
|
5110
5367
|
return;
|
|
5111
5368
|
const eventMsgRole = event.type === "message_start" || event.type === "message_end"
|
|
@@ -5288,8 +5545,7 @@ export class ClientSession {
|
|
|
5288
5545
|
cache.delete(key);
|
|
5289
5546
|
}
|
|
5290
5547
|
/** Build every UiState field EXCEPT messages (the expensive part). */
|
|
5291
|
-
buildLightState(rev, withDraft = false) {
|
|
5292
|
-
const conv = this.conv;
|
|
5548
|
+
buildLightState(rev, withDraft = false, conv = this.conv) {
|
|
5293
5549
|
const state = conv.session.agent.state;
|
|
5294
5550
|
const model = state.model;
|
|
5295
5551
|
let stats = {
|
|
@@ -5299,7 +5555,7 @@ export class ClientSession {
|
|
|
5299
5555
|
contextUsage: { tokens: null, contextWindow: 0, percent: null },
|
|
5300
5556
|
};
|
|
5301
5557
|
try {
|
|
5302
|
-
const s = this.sessionStats();
|
|
5558
|
+
const s = this.sessionStats(conv.session);
|
|
5303
5559
|
stats = {
|
|
5304
5560
|
totalMessages: s.totalMessages,
|
|
5305
5561
|
tokens: s.tokens,
|
|
@@ -5343,20 +5599,21 @@ export class ClientSession {
|
|
|
5343
5599
|
// 用户主目录(右栏 🏠):进程内不变,模块级求值一次,不在热路径调 homedir()。
|
|
5344
5600
|
homeDir: HOME_WIRE,
|
|
5345
5601
|
desktopDir: DESKTOP_WIRE,
|
|
5346
|
-
sessionId:
|
|
5347
|
-
sessionFile:
|
|
5348
|
-
conversationId:
|
|
5602
|
+
sessionId: conv.session.sessionId,
|
|
5603
|
+
sessionFile: conv.session.sessionFile,
|
|
5604
|
+
conversationId: conv.id,
|
|
5605
|
+
sharedConversation: null,
|
|
5349
5606
|
// 临时对话标记(issue #285):提示条/转正按钮跟当前对话走。
|
|
5350
5607
|
// 不能只靠 conversations 列表 —— 空白的临时对话不在运行列表里(shownInRunningList
|
|
5351
5608
|
// 只列有内容的),刚新建时列表里查不到它,提示条就永远不出现。
|
|
5352
5609
|
// 必须**恒存在**(不能只在 true 时展开):转正后它由 true→false,而增量快照是
|
|
5353
5610
|
// `{...ui, ...d.state}` 浅合并,缺字段会把 true 残留下来。
|
|
5354
|
-
isEphemeral: !!
|
|
5355
|
-
sandbox: this.sandboxes?.getByCwd(
|
|
5356
|
-
worktree: this.worktreeInfo ?? null,
|
|
5611
|
+
isEphemeral: !!conv.isEphemeral,
|
|
5612
|
+
sandbox: this.sandboxes?.getByCwd(conv.cwd) ?? null,
|
|
5613
|
+
worktree: conv.cwd === this.cwd ? (this.worktreeInfo ?? null) : null,
|
|
5357
5614
|
rev,
|
|
5358
5615
|
streamingMessage,
|
|
5359
|
-
isStreaming:
|
|
5616
|
+
isStreaming: conv.session.isStreaming,
|
|
5360
5617
|
completionPending: this.completionPending(conv),
|
|
5361
5618
|
turnTiming: conv.turnTiming ?? null,
|
|
5362
5619
|
model: model
|
|
@@ -5370,7 +5627,7 @@ export class ClientSession {
|
|
|
5370
5627
|
thinkingLevel: state.thinkingLevel,
|
|
5371
5628
|
// Only the levels the current model actually supports — the SDK clamps
|
|
5372
5629
|
// anything else, so the UI must not offer (or must disable) the rest.
|
|
5373
|
-
availableThinkingLevels:
|
|
5630
|
+
availableThinkingLevels: conv.session.getAvailableThinkingLevels(),
|
|
5374
5631
|
queue: {
|
|
5375
5632
|
steering: conv.queueSteering,
|
|
5376
5633
|
followUp: conv.queueFollowUp,
|
|
@@ -5382,13 +5639,13 @@ export class ClientSession {
|
|
|
5382
5639
|
errorMessage: state.errorMessage,
|
|
5383
5640
|
retry: conv.retryState ?? null,
|
|
5384
5641
|
compaction: conv.compactionState ?? null,
|
|
5385
|
-
pendingQuestion: this.pendingQuestionForSnapshot(),
|
|
5386
|
-
actionSuggestions: this.markerSvc.getActionSuggestions(
|
|
5387
|
-
pendingApproval: this.pendingApprovalForSnapshot(),
|
|
5642
|
+
pendingQuestion: this.pendingQuestionForSnapshot(conv),
|
|
5643
|
+
actionSuggestions: this.markerSvc.getActionSuggestions(conv.id) ?? null,
|
|
5644
|
+
pendingApproval: this.pendingApprovalForSnapshot(conv),
|
|
5388
5645
|
pendingApprovals: this.pendingApprovalsForSnapshot(),
|
|
5389
5646
|
// 任务计划看板状态同样是会话级:快照恒给 PlanState 或 null(不用 undefined),
|
|
5390
5647
|
// 否则 snapshot_delta 里 key 缺席 → 前端 spread 浅合并会残留上一对话的 plan。
|
|
5391
|
-
plan: this.planManager.getPlan(
|
|
5648
|
+
plan: this.planManager.getPlan(conv.id) ?? null,
|
|
5392
5649
|
// 计划模式是**会话级**开关:快照恒给布尔(不用 undefined),否则
|
|
5393
5650
|
// snapshot_delta 里 key 缺席 → 前端 spread 合并会残留上一对话的 true。
|
|
5394
5651
|
planMode: conv?.planMode === true,
|
|
@@ -5409,7 +5666,7 @@ export class ClientSession {
|
|
|
5409
5666
|
// 未发送草稿只跟全量快照走(切会话/new_chat/get_state):增量 delta 里
|
|
5410
5667
|
// 这个 key 必须整个缺席(不能是显式的 undefined——前端 delta 合并是
|
|
5411
5668
|
// {...ui, ...d.state} 整批覆盖,显式 undefined 会把上次全量带回的草稿洗掉)。
|
|
5412
|
-
...(withDraft ? { draft: this.draftForSnapshot() } : {}),
|
|
5669
|
+
...(withDraft ? { draft: this.draftForSnapshot(conv) } : {}),
|
|
5413
5670
|
engineCapabilities: DEFAULT_PI_CAPABILITIES,
|
|
5414
5671
|
tools: state.tools.map((t) => t.name),
|
|
5415
5672
|
version: ++this.version,
|
|
@@ -5434,6 +5691,9 @@ export class ClientSession {
|
|
|
5434
5691
|
emitSnapshotNow(forceFull = false) {
|
|
5435
5692
|
if (this.disposed)
|
|
5436
5693
|
return;
|
|
5694
|
+
for (const viewers of this.conversationViewers?.values() ?? [])
|
|
5695
|
+
for (const push of viewers)
|
|
5696
|
+
push();
|
|
5437
5697
|
if (this.convs.size === 0) {
|
|
5438
5698
|
// 自愈兜底:没有任何活跃对话,异步触发 newChat 恢复状态,避免抛错挂死
|
|
5439
5699
|
void this.newChat()
|
|
@@ -5567,8 +5827,7 @@ export class ClientSession {
|
|
|
5567
5827
|
const seen = new Set();
|
|
5568
5828
|
while (root.parentId && !seen.has(root)) {
|
|
5569
5829
|
seen.add(root);
|
|
5570
|
-
const
|
|
5571
|
-
const parent = owner.convs.get(owner.subagentCurrentId(root.parentId));
|
|
5830
|
+
const parent = this.parentExecution(root)?.conv;
|
|
5572
5831
|
if (!parent || !sameCwd(parent.cwd, conv.cwd))
|
|
5573
5832
|
break;
|
|
5574
5833
|
root = parent;
|
|
@@ -5586,8 +5845,7 @@ export class ClientSession {
|
|
|
5586
5845
|
const seen = new Set();
|
|
5587
5846
|
while (root.parentId && !seen.has(root)) {
|
|
5588
5847
|
seen.add(root);
|
|
5589
|
-
const
|
|
5590
|
-
const parent = holder.convs.get(holder.subagentCurrentId(root.parentId));
|
|
5848
|
+
const parent = owner.parentExecution(root)?.conv;
|
|
5591
5849
|
if (!parent || !sameCwd(parent.cwd, conv.cwd))
|
|
5592
5850
|
break;
|
|
5593
5851
|
root = parent;
|
|
@@ -5610,11 +5868,16 @@ export class ClientSession {
|
|
|
5610
5868
|
}
|
|
5611
5869
|
getPendingConsults(parentId) {
|
|
5612
5870
|
const parentOwner = this.subagentOwnerOf(parentId);
|
|
5613
|
-
const currentParentId =
|
|
5871
|
+
const currentParentId = this.subagentCurrentId(parentId);
|
|
5614
5872
|
const parent = parentOwner.convs.get(currentParentId);
|
|
5615
5873
|
if (!parent)
|
|
5616
5874
|
return [];
|
|
5617
|
-
const result = conversationCommunication
|
|
5875
|
+
const result = conversationCommunication
|
|
5876
|
+
.incomingRequests(parent)
|
|
5877
|
+
.map((request) => {
|
|
5878
|
+
const source = conversationCommunication.requestSource(request.requestId);
|
|
5879
|
+
return { ...request, runId: source ? conversationIdentity(source) : request.runId };
|
|
5880
|
+
});
|
|
5618
5881
|
for (const owner of new Set([this, ...(this.allSubagentSessions?.() ?? [])])) {
|
|
5619
5882
|
for (const request of owner.pendingQuestions.values()) {
|
|
5620
5883
|
const child = owner.convs.get(request.conversationId);
|
|
@@ -5622,9 +5885,10 @@ export class ClientSession {
|
|
|
5622
5885
|
continue;
|
|
5623
5886
|
if (!request.settled &&
|
|
5624
5887
|
request.audience === "parent" &&
|
|
5625
|
-
child
|
|
5888
|
+
child &&
|
|
5889
|
+
owner.parentExecution(child)?.conv === parent &&
|
|
5626
5890
|
sameCwd(child.cwd, parent.cwd)) {
|
|
5627
|
-
result.push({ runId: child
|
|
5891
|
+
result.push({ runId: conversationIdentity(child), requestId: request.id, questions: request.questions });
|
|
5628
5892
|
}
|
|
5629
5893
|
}
|
|
5630
5894
|
}
|
|
@@ -5639,29 +5903,25 @@ export class ClientSession {
|
|
|
5639
5903
|
}
|
|
5640
5904
|
resolveCommunicationTarget(senderId, to) {
|
|
5641
5905
|
const owner = this.subagentOwnerOf(senderId);
|
|
5642
|
-
const sender = owner.convs.get(
|
|
5906
|
+
const sender = owner.convs.get(this.subagentCurrentId(senderId));
|
|
5643
5907
|
if (!sender)
|
|
5644
5908
|
return undefined;
|
|
5645
5909
|
if (to === "parent") {
|
|
5646
|
-
|
|
5647
|
-
return undefined;
|
|
5648
|
-
const parentOwner = owner.subagentOwnerOf(sender.parentId);
|
|
5649
|
-
return parentOwner.convs.get(parentOwner.subagentCurrentId(sender.parentId));
|
|
5910
|
+
return owner.parentExecution(sender)?.conv;
|
|
5650
5911
|
}
|
|
5651
5912
|
if (to === "root") {
|
|
5652
5913
|
let root = sender;
|
|
5653
5914
|
const seen = new Set();
|
|
5654
5915
|
while (root.parentId && !seen.has(root)) {
|
|
5655
5916
|
seen.add(root);
|
|
5656
|
-
const
|
|
5657
|
-
const parent = parentOwner.convs.get(parentOwner.subagentCurrentId(root.parentId));
|
|
5917
|
+
const parent = owner.parentExecution(root)?.conv;
|
|
5658
5918
|
if (!parent)
|
|
5659
5919
|
break;
|
|
5660
5920
|
root = parent;
|
|
5661
5921
|
}
|
|
5662
5922
|
return root;
|
|
5663
5923
|
}
|
|
5664
|
-
if (to.startsWith("
|
|
5924
|
+
if (to.startsWith("exec-")) {
|
|
5665
5925
|
for (const candidate of new Set([this, ...(this.allSubagentSessions?.() ?? [])])) {
|
|
5666
5926
|
for (const conv of candidate.convs.values())
|
|
5667
5927
|
if (communicationAddress(conv) === to)
|
|
@@ -5689,6 +5949,16 @@ export class ClientSession {
|
|
|
5689
5949
|
};
|
|
5690
5950
|
return {
|
|
5691
5951
|
...this.subagentHost,
|
|
5952
|
+
resolveConversationId: (id) => current().session.resolveExecution(id)?.convId,
|
|
5953
|
+
getSubagent: (id) => current().session.subagentHost.getSubagent(id),
|
|
5954
|
+
listSubagents: (scope, self) => current().session.subagentHost.listSubagents(scope, self),
|
|
5955
|
+
steerSubagent: (id, message) => current().session.subagentHost.steerSubagent(id, message),
|
|
5956
|
+
stopSubagent: (id) => current().session.subagentHost.stopSubagent(id),
|
|
5957
|
+
markSubagentsConsumed: (ids) => current().session.subagentHost.markSubagentsConsumed?.(ids),
|
|
5958
|
+
finalizeSubagentWorktree: (id) => current().session.subagentHost.finalizeSubagentWorktree?.(id) ?? Promise.resolve(),
|
|
5959
|
+
dismissSubagent: (id, options) => current().session.subagentHost.dismissSubagent?.(id, options) ??
|
|
5960
|
+
Promise.resolve({ ok: false, reason: "unsupported" }),
|
|
5961
|
+
getActiveConversationId: () => current().session.activeId,
|
|
5692
5962
|
spawnSubagent: (prompt, type, cwd, templateName, model, _parentId, persist, thinking, notifyOnDone, isolation, extra, signal) => {
|
|
5693
5963
|
// Resolve by runtime identity: a moved parent's new id can also name
|
|
5694
5964
|
// the replacement conversation in the original client.
|
|
@@ -5701,15 +5971,15 @@ export class ClientSession {
|
|
|
5701
5971
|
},
|
|
5702
5972
|
consultParent: (_self, questions, signal) => {
|
|
5703
5973
|
const home = current();
|
|
5704
|
-
return home.session.consultParent(home.convId, questions, signal);
|
|
5974
|
+
return home.session.consultParent(conversationIdentity(home.session.convs.get(home.convId)), questions, signal);
|
|
5705
5975
|
},
|
|
5706
5976
|
answerConsult: (_self, runId, requestId, answers) => {
|
|
5707
5977
|
const home = current();
|
|
5708
|
-
return home.session.answerConsult(home.convId, runId, requestId, answers);
|
|
5978
|
+
return home.session.answerConsult(conversationIdentity(home.session.convs.get(home.convId)), runId, requestId, answers);
|
|
5709
5979
|
},
|
|
5710
5980
|
escalateConsult: (_self, runId, requestId) => {
|
|
5711
5981
|
const home = current();
|
|
5712
|
-
return home.session.answerConsult(home.convId, runId, requestId);
|
|
5982
|
+
return home.session.answerConsult(conversationIdentity(home.session.convs.get(home.convId)), runId, requestId);
|
|
5713
5983
|
},
|
|
5714
5984
|
completeSubagent: (_self, summary) => {
|
|
5715
5985
|
const home = current();
|
|
@@ -5717,7 +5987,7 @@ export class ClientSession {
|
|
|
5717
5987
|
},
|
|
5718
5988
|
getPendingConsults: () => {
|
|
5719
5989
|
const home = current();
|
|
5720
|
-
return home.session.getPendingConsults(home.convId);
|
|
5990
|
+
return home.session.getPendingConsults(conversationIdentity(home.session.convs.get(home.convId)));
|
|
5721
5991
|
},
|
|
5722
5992
|
setConsultWaiting: (_self, waiting) => {
|
|
5723
5993
|
const home = current();
|
|
@@ -5739,17 +6009,18 @@ export class ClientSession {
|
|
|
5739
6009
|
const seen = new Set();
|
|
5740
6010
|
while (!seen.has(conv)) {
|
|
5741
6011
|
seen.add(conv);
|
|
5742
|
-
if (conv.subagentWorktree && isSameOrSubpath(
|
|
5743
|
-
return conv.subagentWorktree.mainRepoPath;
|
|
6012
|
+
if (conv.subagentWorktree && isSameOrSubpath(conv.cwd, conv.subagentWorktree.worktreePath))
|
|
6013
|
+
return normalizePathKey(conv.subagentWorktree.mainRepoPath);
|
|
5744
6014
|
if (!conv.parentId)
|
|
5745
6015
|
break;
|
|
5746
|
-
const
|
|
5747
|
-
const parent = parentOwner.convs.get(parentOwner.subagentCurrentId(conv.parentId));
|
|
6016
|
+
const parent = this.parentExecution(conv)?.conv;
|
|
5748
6017
|
if (!parent)
|
|
5749
6018
|
break;
|
|
5750
6019
|
conv = parent;
|
|
5751
6020
|
}
|
|
5752
|
-
|
|
6021
|
+
// A child may execute in an independent checkout. Its registered ancestry,
|
|
6022
|
+
// rather than that checkout's path, determines the collaboration project.
|
|
6023
|
+
return normalizePathKey(conv.cwd);
|
|
5753
6024
|
}
|
|
5754
6025
|
communicationPending(conv) {
|
|
5755
6026
|
return conversationCommunication.pending(conv) || conversationCommunication.inbox(conv).length > 0;
|
|
@@ -5862,7 +6133,7 @@ export class ClientSession {
|
|
|
5862
6133
|
if (conv.subagentTaskStopped)
|
|
5863
6134
|
throw new Error("Communication target task was stopped");
|
|
5864
6135
|
const busy = conv.session.isStreaming || conv.promptInFlight;
|
|
5865
|
-
const text =
|
|
6136
|
+
const text = formatCommunicationMessage(envelope, source);
|
|
5866
6137
|
if (!busy) {
|
|
5867
6138
|
if (conv.isSubagent && (conv.subagentDoneNotified || conv.subagentConsumedByParent)) {
|
|
5868
6139
|
conv.subagentDoneNotified = false;
|
|
@@ -5903,11 +6174,11 @@ export class ClientSession {
|
|
|
5903
6174
|
}
|
|
5904
6175
|
answerConsult(selfId, runId, requestId, answers) {
|
|
5905
6176
|
const parentOwner = this.subagentOwnerOf(selfId);
|
|
5906
|
-
const parent = parentOwner.convs.get(
|
|
6177
|
+
const parent = parentOwner.convs.get(this.subagentCurrentId(selfId));
|
|
5907
6178
|
const owner = this.subagentOwnerOf(runId);
|
|
5908
|
-
const child = owner.convs.get(
|
|
6179
|
+
const child = owner.convs.get(this.subagentCurrentId(runId));
|
|
5909
6180
|
if (parent && child && conversationCommunication.requestSource(requestId) === child) {
|
|
5910
|
-
if (child
|
|
6181
|
+
if (owner.parentExecution(child)?.conv !== parent)
|
|
5911
6182
|
return { ok: false, error: "Only the direct parent may use the compatibility answer action." };
|
|
5912
6183
|
return this.communicate(parent.id, answers ? "reply" : "escalate", {
|
|
5913
6184
|
requestId,
|
|
@@ -5917,7 +6188,7 @@ export class ClientSession {
|
|
|
5917
6188
|
const request = owner.pendingQuestions.get(requestId);
|
|
5918
6189
|
if (!parent ||
|
|
5919
6190
|
!child ||
|
|
5920
|
-
child
|
|
6191
|
+
owner.parentExecution(child)?.conv !== parent ||
|
|
5921
6192
|
!sameCwd(parent.cwd, child.cwd) ||
|
|
5922
6193
|
!request ||
|
|
5923
6194
|
request.settled ||
|
|
@@ -5935,7 +6206,7 @@ export class ClientSession {
|
|
|
5935
6206
|
}
|
|
5936
6207
|
async consultParent(selfId, questions, signal) {
|
|
5937
6208
|
const owner = this.subagentOwnerOf(selfId);
|
|
5938
|
-
const child = owner.convs.get(
|
|
6209
|
+
const child = owner.convs.get(this.subagentCurrentId(selfId));
|
|
5939
6210
|
if (!child?.isSubagent || owner.disposed || signal?.aborted)
|
|
5940
6211
|
return Promise.resolve(null);
|
|
5941
6212
|
const parent = child.parentId ? owner.resolveCommunicationTarget(child.id, "parent") : undefined;
|
|
@@ -5954,7 +6225,8 @@ export class ClientSession {
|
|
|
5954
6225
|
const child = owner.convs.get(request.conversationId);
|
|
5955
6226
|
if (!request.settled &&
|
|
5956
6227
|
request.audience === "parent" &&
|
|
5957
|
-
child
|
|
6228
|
+
child &&
|
|
6229
|
+
owner.parentExecution(child)?.conv === parent &&
|
|
5958
6230
|
sameCwd(parent.cwd, child.cwd)) {
|
|
5959
6231
|
request.audience = "user";
|
|
5960
6232
|
changed = true;
|
|
@@ -5990,11 +6262,11 @@ export class ClientSession {
|
|
|
5990
6262
|
}
|
|
5991
6263
|
/** 快照侧的待答提问(UiState.pendingQuestion):只带当前对话的问卷——切回
|
|
5992
6264
|
* 原对话会重推快照,对话框随之回来(重连/刷新/第二标签页的恢复通道)。 */
|
|
5993
|
-
pendingQuestionForSnapshot() {
|
|
6265
|
+
pendingQuestionForSnapshot(conversation = this.conv) {
|
|
5994
6266
|
for (const [id, p] of this.pendingQuestions) {
|
|
5995
6267
|
if (p.audience !== "user" || p.settled)
|
|
5996
6268
|
continue;
|
|
5997
|
-
if (p.conversationId !== undefined && p.conversationId !==
|
|
6269
|
+
if (p.conversationId !== undefined && p.conversationId !== conversation.id)
|
|
5998
6270
|
continue;
|
|
5999
6271
|
const conv = p.conversationId !== undefined ? this.convs.get(p.conversationId) : this.conv;
|
|
6000
6272
|
const conversationTitle = conv?.title;
|
|
@@ -6074,7 +6346,7 @@ export class ClientSession {
|
|
|
6074
6346
|
resolve({ decision: "deny", reason: "会话已关闭" });
|
|
6075
6347
|
return;
|
|
6076
6348
|
}
|
|
6077
|
-
const id = `appr-${
|
|
6349
|
+
const id = `appr-${randomUUID()}`;
|
|
6078
6350
|
const targetConv = conversationId ? this.convs.get(conversationId) : this.conv;
|
|
6079
6351
|
const resolvedConvId = conversationId ?? targetConv?.id ?? this.activeId;
|
|
6080
6352
|
const conv = (resolvedConvId ? this.convs.get(resolvedConvId) : undefined) ?? targetConv;
|
|
@@ -6374,8 +6646,7 @@ export class ClientSession {
|
|
|
6374
6646
|
})))
|
|
6375
6647
|
.sort((a, b) => a.createdAt - b.createdAt);
|
|
6376
6648
|
}
|
|
6377
|
-
pendingApprovalForSnapshot() {
|
|
6378
|
-
const conv = this.conv;
|
|
6649
|
+
pendingApprovalForSnapshot(conv = this.conv) {
|
|
6379
6650
|
if (!conv?.pendingApprovals || conv.pendingApprovals.size === 0)
|
|
6380
6651
|
return null;
|
|
6381
6652
|
for (const [id, p] of conv.pendingApprovals) {
|
|
@@ -6673,8 +6944,10 @@ export class ClientSession {
|
|
|
6673
6944
|
/** 前端回页面调用结果(index.ts 的 page_response → cs.resolvePageCall)。
|
|
6674
6945
|
* 找不到 id 就静默忽略:那是正常竞态(超时后才迟到、页面刷新后重发、旧链接
|
|
6675
6946
|
* 残留),不是错误,也没人能处理。 */
|
|
6676
|
-
resolvePageCall(id, ok, result, error) {
|
|
6947
|
+
resolvePageCall(id, ok, result, error, conversationId) {
|
|
6677
6948
|
const pending = this.pendingPageCalls.get(id);
|
|
6949
|
+
if (conversationId && pending?.conversationId !== conversationId)
|
|
6950
|
+
throw new Error("Page request does not belong to the shared conversation");
|
|
6678
6951
|
if (!pending)
|
|
6679
6952
|
return;
|
|
6680
6953
|
this.pendingPageCalls.delete(id);
|
|
@@ -6883,10 +7156,8 @@ export class ClientSession {
|
|
|
6883
7156
|
* 投错。(注意不能用 runtime 对象比:SDK 会把工厂返回值包成新的 AgentSessionRuntime
|
|
6884
7157
|
* 实例,而 `runtime.session` 与本工厂的 `created.session` 是同一个对象。) */
|
|
6885
7158
|
findConversationHome = undefined;
|
|
6886
|
-
/**
|
|
6887
|
-
*
|
|
6888
|
-
* 后代)整体搬去另一会话后,旧 host 闭包仍指向源会话,subagent 工具族靠它现查持有方;
|
|
6889
|
-
* - allSubagentSessions:全部客户端会话(wait_all/list 的「本族跨会话展开」用)。 */
|
|
7159
|
+
/** Legacy lookup rejects globally ambiguous ids. Runtime routing uses resolveExecution
|
|
7160
|
+
* over live holders; allSubagentSessions also supplies cross-holder family traversal. */
|
|
6890
7161
|
findSubagentOwner = undefined;
|
|
6891
7162
|
allSubagentSessions = undefined;
|
|
6892
7163
|
/** issue #145 跨客户端同会话感知 —— attach 时由 AgentService 接线:
|
|
@@ -6901,6 +7172,7 @@ export class ClientSession {
|
|
|
6901
7172
|
* 当别处已持有该 session 文件时,直接把该会话本体从持有方迁移过来,避免创建第二个
|
|
6902
7173
|
* runtime 导致 pi-background-tasks 等扩展激活冲突与分支分叉。 */
|
|
6903
7174
|
takeOverConversationElsewhere = undefined;
|
|
7175
|
+
prepareBotSessionResume = undefined;
|
|
6904
7176
|
/** 插件 steer 跨客户端兜底钩子:attach 时由 AgentService 接线(见 steerElsewhere),
|
|
6905
7177
|
* 在其他客户端的 conversations 里找对话并由持有方执行 steer,未持有回 undefined。 */
|
|
6906
7178
|
steerConversationElsewhere = undefined;
|
|
@@ -7209,13 +7481,13 @@ export class ClientSession {
|
|
|
7209
7481
|
* Callers that need the authoritative value can still call the session
|
|
7210
7482
|
* directly; every cache hit here is at most STATS_CACHE_MS stale.
|
|
7211
7483
|
*/
|
|
7212
|
-
sessionStats() {
|
|
7484
|
+
sessionStats(session = this.session) {
|
|
7213
7485
|
const now = Date.now();
|
|
7214
7486
|
const hit = this.sessionStatsCache;
|
|
7215
|
-
if (hit && hit.session ===
|
|
7487
|
+
if (hit && hit.session === session && now - hit.at < STATS_CACHE_MS)
|
|
7216
7488
|
return hit.value;
|
|
7217
|
-
const value =
|
|
7218
|
-
this.sessionStatsCache = { at: now, session
|
|
7489
|
+
const value = session.getSessionStats();
|
|
7490
|
+
this.sessionStatsCache = { at: now, session, value };
|
|
7219
7491
|
return value;
|
|
7220
7492
|
}
|
|
7221
7493
|
scheduleSnapshot() {
|
|
@@ -7604,6 +7876,10 @@ export class ClientSession {
|
|
|
7604
7876
|
pushSettings() {
|
|
7605
7877
|
this.settingsSvc.push();
|
|
7606
7878
|
}
|
|
7879
|
+
/** Explicit resource discovery for the settings panel; never reloads the runtime. */
|
|
7880
|
+
refreshSettings() {
|
|
7881
|
+
this.settingsSvc.refreshResources();
|
|
7882
|
+
}
|
|
7607
7883
|
/**
|
|
7608
7884
|
* 统一会话配置与门控收口方法(单一事实源)。
|
|
7609
7885
|
* 无论是新建、切换、回滚、编辑重问、看门狗重置还是设置变更:
|
|
@@ -7863,8 +8139,28 @@ export class ClientSession {
|
|
|
7863
8139
|
return this.settingsSvc.savePreset(name);
|
|
7864
8140
|
}
|
|
7865
8141
|
/** Replace the current settings with the named preset and apply it. */
|
|
7866
|
-
|
|
7867
|
-
|
|
8142
|
+
conversationSettingsTargets = new WeakMap();
|
|
8143
|
+
conversationSettingsTarget(conv) {
|
|
8144
|
+
let target = this.conversationSettingsTargets.get(conv);
|
|
8145
|
+
if (!target) {
|
|
8146
|
+
const runtime = conv.runtime;
|
|
8147
|
+
target = {
|
|
8148
|
+
isStreaming: () => runtime.session.isStreaming || Boolean(conv.promptInFlight),
|
|
8149
|
+
reloadSession: async () => {
|
|
8150
|
+
if (this.disposed || this.convs.get(conv.id) !== conv || conv.runtime !== runtime)
|
|
8151
|
+
throw new Error("Conversation runtime no longer available");
|
|
8152
|
+
await runtime.session.reload();
|
|
8153
|
+
if (this.disposed || this.convs.get(conv.id) !== conv || conv.runtime !== runtime)
|
|
8154
|
+
return;
|
|
8155
|
+
this.applySessionOverrides(conv);
|
|
8156
|
+
},
|
|
8157
|
+
};
|
|
8158
|
+
this.conversationSettingsTargets.set(conv, target);
|
|
8159
|
+
}
|
|
8160
|
+
return target;
|
|
8161
|
+
}
|
|
8162
|
+
async applyPreset(name, conv) {
|
|
8163
|
+
return this.settingsSvc.applyPreset(name, conv ? this.conversationSettingsTarget(conv) : undefined);
|
|
7868
8164
|
}
|
|
7869
8165
|
/** Remove a named preset. */
|
|
7870
8166
|
async deletePreset(name) {
|
|
@@ -8501,7 +8797,7 @@ export class ClientSession {
|
|
|
8501
8797
|
if (this.botExecution) {
|
|
8502
8798
|
session.setActiveToolsByName(session
|
|
8503
8799
|
.getAllTools()
|
|
8504
|
-
.filter((tool) => this.
|
|
8800
|
+
.filter((tool) => this.botToolRegistrationAllowed(tool.name))
|
|
8505
8801
|
.map((tool) => tool.name));
|
|
8506
8802
|
return;
|
|
8507
8803
|
}
|
|
@@ -8563,7 +8859,11 @@ export class ClientSession {
|
|
|
8563
8859
|
const subagentOff = disabled.has("conversation") || disabled.has("subagent") || SPAWN_TOOLS_BARRED.has(session);
|
|
8564
8860
|
syncSubagentOverride(session, subagentOff, (name) => mergeConversationTools([
|
|
8565
8861
|
...(ownerId
|
|
8566
|
-
? makeSubagentTools(withSubagentOwner(this.communicationBoundSubagentHost({ session }), ownerId, () => this.requireSubagentOwner({ session }, ownerId)), undefined, () =>
|
|
8862
|
+
? makeSubagentTools(withSubagentOwner(this.communicationBoundSubagentHost({ session }), ownerId, () => this.requireSubagentOwner({ session }, ownerId)), undefined, () => {
|
|
8863
|
+
const home = this.findConversationHome?.(session);
|
|
8864
|
+
const conv = home?.session.convs.get(home.convId);
|
|
8865
|
+
return conv ? conversationIdentity(conv) : undefined;
|
|
8866
|
+
})
|
|
8567
8867
|
: makeSubagentTools(this.subagentHost)),
|
|
8568
8868
|
makeConversationReadTool(this.conversationReadHost(), () => this.getLang()),
|
|
8569
8869
|
]).find((tool) => tool.name === name));
|
|
@@ -9090,7 +9390,7 @@ export class ClientSession {
|
|
|
9090
9390
|
def = {
|
|
9091
9391
|
...def,
|
|
9092
9392
|
execute: async (...args) => {
|
|
9093
|
-
if (!this.botToolAllowed(def.name, cwdOverride))
|
|
9393
|
+
if (!this.botToolAllowed(def.name, cwdOverride, args[1]))
|
|
9094
9394
|
throw new Error("Tool is not authorized for this Bot");
|
|
9095
9395
|
return execute(...args);
|
|
9096
9396
|
},
|
|
@@ -9140,7 +9440,7 @@ export class ClientSession {
|
|
|
9140
9440
|
if (this.botExecution) {
|
|
9141
9441
|
const mcp = this.executionMcpBridge(cwd ?? this.cwd)?.getTools() ?? [];
|
|
9142
9442
|
return (mcp.includes(t) &&
|
|
9143
|
-
this.
|
|
9443
|
+
this.botToolRegistrationAllowed(t.name, cwd) &&
|
|
9144
9444
|
!["bash", "read", "write", "edit", "powershell", "ls", "grep", "find"].includes(t.name));
|
|
9145
9445
|
}
|
|
9146
9446
|
if (t.pluginId && disabledPlugins.has(t.pluginId))
|
|
@@ -9156,7 +9456,7 @@ export class ClientSession {
|
|
|
9156
9456
|
...def,
|
|
9157
9457
|
execute: async (...args) => {
|
|
9158
9458
|
const current = this.executionMcpBridge(cwd ?? this.cwd)?.getTools() ?? [];
|
|
9159
|
-
if (!current.includes(tool) || !this.botToolAllowed(tool.name, cwd))
|
|
9459
|
+
if (!current.includes(tool) || !this.botToolAllowed(tool.name, cwd, args[1]))
|
|
9160
9460
|
throw new Error("Bot MCP tool authorization expired");
|
|
9161
9461
|
return execute(...args);
|
|
9162
9462
|
},
|
|
@@ -9332,14 +9632,14 @@ export class ClientSession {
|
|
|
9332
9632
|
let pendingQuestions = false;
|
|
9333
9633
|
for (const owner of owners) {
|
|
9334
9634
|
for (const child of owner.convs.values()) {
|
|
9335
|
-
if (child
|
|
9635
|
+
if (owner.parentExecution(child)?.conv !== conv)
|
|
9336
9636
|
continue;
|
|
9337
9637
|
if (owner.subagentTaskPending(child, new Set(seen)) ||
|
|
9338
9638
|
(child.notifyOnDone !== false && !child.subagentDoneNotified && !child.subagentConsumedByParent))
|
|
9339
9639
|
activeChildren = true;
|
|
9340
9640
|
}
|
|
9341
9641
|
for (const request of owner.pendingQuestions?.values() ?? []) {
|
|
9342
|
-
if (!request.settled && request.conversationId === conv
|
|
9642
|
+
if (!request.settled && owner.convs.get(request.conversationId) === conv)
|
|
9343
9643
|
pendingQuestions = true;
|
|
9344
9644
|
}
|
|
9345
9645
|
}
|
|
@@ -9357,7 +9657,11 @@ export class ClientSession {
|
|
|
9357
9657
|
return true;
|
|
9358
9658
|
const owners = new Set([this, ...(this.allSubagentSessions?.() ?? [])]);
|
|
9359
9659
|
for (const owner of owners) {
|
|
9360
|
-
|
|
9660
|
+
const key = conversationIdentity(conv);
|
|
9661
|
+
if (owner.pendingSubagentNotices.has(key) || owner.deliveringSubagentNotices.has(key))
|
|
9662
|
+
return true;
|
|
9663
|
+
if (owner.convs.get(conv.id) === conv &&
|
|
9664
|
+
(owner.pendingSubagentNotices.has(conv.id) || owner.deliveringSubagentNotices.has(conv.id)))
|
|
9361
9665
|
return true;
|
|
9362
9666
|
}
|
|
9363
9667
|
return false;
|
|
@@ -9457,6 +9761,7 @@ export class ClientSession {
|
|
|
9457
9761
|
cwd: conv.cwd,
|
|
9458
9762
|
isStreaming: streaming,
|
|
9459
9763
|
convId: conv.id,
|
|
9764
|
+
independent: conv.independent !== false,
|
|
9460
9765
|
hasQuestion: !!pq,
|
|
9461
9766
|
...(pq?.title ? { questionTitle: pq.title } : {}),
|
|
9462
9767
|
...(sessionFile ? { sessionFile } : {}),
|
|
@@ -9787,7 +10092,7 @@ export class ClientSession {
|
|
|
9787
10092
|
.map((c) => {
|
|
9788
10093
|
let label;
|
|
9789
10094
|
if (c.isSubagent) {
|
|
9790
|
-
const parent =
|
|
10095
|
+
const parent = this.parentExecution(c)?.conv;
|
|
9791
10096
|
label = parent ? `本窗口「${parent.title}」的子代理「${c.title}」` : `本窗口子代理「${c.title}」`;
|
|
9792
10097
|
}
|
|
9793
10098
|
else {
|
|
@@ -10168,12 +10473,16 @@ export class ClientSession {
|
|
|
10168
10473
|
* 本轮已停止并标红,用户点「重试」再触发一轮 LLM 调用。不新增用户气泡——
|
|
10169
10474
|
* 用 display:false 的 custom 消息 triggerTurn 续跑,模型基于完整上下文
|
|
10170
10475
|
* (含上次报错)继续生成。流式中 / 无可重试失败时只发 notice 拒绝。 */
|
|
10171
|
-
async retryLast() {
|
|
10172
|
-
const conv = this.conv;
|
|
10476
|
+
async retryLast(targetConversation) {
|
|
10477
|
+
const conv = targetConversation ?? this.conv;
|
|
10478
|
+
if (targetConversation && this.convs.get(conv.id) !== conv)
|
|
10479
|
+
throw new Error("Retry conversation is no longer available");
|
|
10480
|
+
const runtime = conv.runtime;
|
|
10481
|
+
const s = conv.session;
|
|
10482
|
+
const current = () => this.convs.get(conv.id) === conv && conv.runtime === runtime && conv.session === s;
|
|
10173
10483
|
try {
|
|
10174
10484
|
if (this.quiesceBlocked())
|
|
10175
10485
|
return;
|
|
10176
|
-
const s = this.session;
|
|
10177
10486
|
if (s.isStreaming) {
|
|
10178
10487
|
this.emit({
|
|
10179
10488
|
type: "notice",
|
|
@@ -10230,6 +10539,8 @@ export class ClientSession {
|
|
|
10230
10539
|
],
|
|
10231
10540
|
display: false,
|
|
10232
10541
|
}, { triggerTurn: true });
|
|
10542
|
+
if (!current())
|
|
10543
|
+
return;
|
|
10233
10544
|
conv.promptedSinceActive = true;
|
|
10234
10545
|
conv.lastActiveAt = Date.now();
|
|
10235
10546
|
conv.lastSdkEventAt = Date.now();
|
|
@@ -10257,8 +10568,13 @@ export class ClientSession {
|
|
|
10257
10568
|
* item still at that position; on any mismatch we fall back to first-occurrence
|
|
10258
10569
|
* text match (old clients, or the queue shifted between click and handling).
|
|
10259
10570
|
*/
|
|
10260
|
-
async removeQueued(kind, text, index, id) {
|
|
10261
|
-
const conv = this.conv;
|
|
10571
|
+
async removeQueued(kind, text, index, id, targetConversation) {
|
|
10572
|
+
const conv = targetConversation ?? this.conv;
|
|
10573
|
+
if (targetConversation && this.convs.get(conv.id) !== conv)
|
|
10574
|
+
throw new Error("Queue conversation is no longer available");
|
|
10575
|
+
const runtime = conv.runtime;
|
|
10576
|
+
const s = conv.session;
|
|
10577
|
+
const current = () => this.convs.get(conv.id) === conv && conv.runtime === runtime && conv.session === s;
|
|
10262
10578
|
// Always-defined display mirrors; also the provenance of bubble rendering.
|
|
10263
10579
|
const local = kind === "steer" ? conv.queueSteering : conv.queueFollowUp;
|
|
10264
10580
|
let targetText = text;
|
|
@@ -10281,7 +10597,6 @@ export class ClientSession {
|
|
|
10281
10597
|
return;
|
|
10282
10598
|
}
|
|
10283
10599
|
}
|
|
10284
|
-
const s = this.conv.session;
|
|
10285
10600
|
if (!s) {
|
|
10286
10601
|
// Runtime not bound yet (fresh conversation) — drop the display mirror;
|
|
10287
10602
|
// a later queue_update reconciles any SDK-side state.
|
|
@@ -10352,11 +10667,17 @@ export class ClientSession {
|
|
|
10352
10667
|
// Re-queue the survivors in original order. Guard each call so a single
|
|
10353
10668
|
// failure can't leave the queue half-drained silently.
|
|
10354
10669
|
for (const entry of keptSteeringEntries) {
|
|
10670
|
+
if (!current())
|
|
10671
|
+
return;
|
|
10355
10672
|
try {
|
|
10356
10673
|
conv.queuedSteeringEntries.push(entry);
|
|
10357
10674
|
await s.steer(entry.text, entry.images);
|
|
10675
|
+
if (!current())
|
|
10676
|
+
return;
|
|
10358
10677
|
}
|
|
10359
10678
|
catch (err) {
|
|
10679
|
+
if (!current())
|
|
10680
|
+
return;
|
|
10360
10681
|
conv.queuedSteeringEntries.pop();
|
|
10361
10682
|
this.emit({
|
|
10362
10683
|
type: "notice",
|
|
@@ -10367,11 +10688,17 @@ export class ClientSession {
|
|
|
10367
10688
|
}
|
|
10368
10689
|
}
|
|
10369
10690
|
for (const entry of keptFollowUpEntries) {
|
|
10691
|
+
if (!current())
|
|
10692
|
+
return;
|
|
10370
10693
|
try {
|
|
10371
10694
|
conv.queuedFollowUpEntries.push(entry);
|
|
10372
10695
|
await s.followUp(entry.text, entry.images);
|
|
10696
|
+
if (!current())
|
|
10697
|
+
return;
|
|
10373
10698
|
}
|
|
10374
10699
|
catch (err) {
|
|
10700
|
+
if (!current())
|
|
10701
|
+
return;
|
|
10375
10702
|
conv.queuedFollowUpEntries.pop();
|
|
10376
10703
|
this.emit({
|
|
10377
10704
|
type: "notice",
|
|
@@ -10394,10 +10721,14 @@ export class ClientSession {
|
|
|
10394
10721
|
* 晚于服务端的切换到达)。空白新会话的转录还没落盘,但 id 内存里已有。
|
|
10395
10722
|
* 存完不推快照:同页的草稿本来就是自己打的;恢复走全量快照的 draft 字段。
|
|
10396
10723
|
* 陈旧写由 ComposerDraftsStore 的 clear 水位丢弃(ts <= clearTs 不复活)。 */
|
|
10397
|
-
saveDraft(sessionId, text, ts) {
|
|
10724
|
+
saveDraft(sessionId, text, ts, targetConversation) {
|
|
10398
10725
|
try {
|
|
10399
10726
|
if (!sessionId)
|
|
10400
10727
|
return;
|
|
10728
|
+
if (targetConversation &&
|
|
10729
|
+
(this.convs.get(targetConversation.id) !== targetConversation ||
|
|
10730
|
+
targetConversation.session.sessionId !== sessionId))
|
|
10731
|
+
throw new Error("Draft conversation is no longer available or does not match the session");
|
|
10401
10732
|
this.drafts.save(sessionId, text, ts);
|
|
10402
10733
|
}
|
|
10403
10734
|
catch {
|
|
@@ -10405,9 +10736,9 @@ export class ClientSession {
|
|
|
10405
10736
|
}
|
|
10406
10737
|
}
|
|
10407
10738
|
/** 当前活跃对话的草稿(全量快照用;增量 snapshot_delta 传 withDraft=false 不带)。 */
|
|
10408
|
-
draftForSnapshot() {
|
|
10739
|
+
draftForSnapshot(conv = this.conv) {
|
|
10409
10740
|
try {
|
|
10410
|
-
return this.drafts.get(
|
|
10741
|
+
return this.drafts.get(conv.session.sessionId) ?? null;
|
|
10411
10742
|
}
|
|
10412
10743
|
catch {
|
|
10413
10744
|
return null;
|
|
@@ -10669,7 +11000,7 @@ export class ClientSession {
|
|
|
10669
11000
|
}, (m) =>
|
|
10670
11001
|
// 子代理带模板时按原模板重建(conv.subagentTemplate 是派发时工厂
|
|
10671
11002
|
// 用的同一快照);普通对话 undefined,行为不变。
|
|
10672
|
-
createAgentSessionRuntime(this.makeRuntimeFactory(conv.terminals, conv.subagentTemplate, conv.id, undefined, undefined, conv.isSubagent && this.subagentAtDepthLimit(conv
|
|
11003
|
+
createAgentSessionRuntime(this.makeRuntimeFactory(conv.terminals, conv.subagentTemplate, conv.id, undefined, undefined, conv.isSubagent && this.subagentAtDepthLimit(this.parentExecutionKey(conv))), {
|
|
10673
11004
|
cwd: sessionBaseDir,
|
|
10674
11005
|
agentDir: this.agentDir,
|
|
10675
11006
|
sessionManager: m,
|
|
@@ -10739,7 +11070,7 @@ export class ClientSession {
|
|
|
10739
11070
|
* 空白新对话」——/new <prompt> 只在 true 时投递首条提示;false 表示没能进入
|
|
10740
11071
|
* 新对话(准入关闭 / 同项目对话数达上限 / runtime 创建失败),此时照发会把
|
|
10741
11072
|
* 首条提示投进用户原本正在用的那个对话里。 */
|
|
10742
|
-
async newChat(_preset, ephemeral, onReady) {
|
|
11073
|
+
async newChat(_preset, ephemeral, onReady, independent = true) {
|
|
10743
11074
|
if (this.quiesceBlocked())
|
|
10744
11075
|
return false;
|
|
10745
11076
|
// Reuse an already-open blank conversation instead of piling up new ones
|
|
@@ -10767,6 +11098,7 @@ export class ClientSession {
|
|
|
10767
11098
|
this.pushSettings();
|
|
10768
11099
|
this.flushSnapshot();
|
|
10769
11100
|
}
|
|
11101
|
+
this.setConversationIndependent(active.id, independent);
|
|
10770
11102
|
onReady?.(active.id);
|
|
10771
11103
|
return true;
|
|
10772
11104
|
}
|
|
@@ -10780,6 +11112,7 @@ export class ClientSession {
|
|
|
10780
11112
|
await this.selectAgentPreset(_preset);
|
|
10781
11113
|
else
|
|
10782
11114
|
this.flushSnapshot();
|
|
11115
|
+
this.setConversationIndependent(conv.id, independent);
|
|
10783
11116
|
onReady?.(conv.id);
|
|
10784
11117
|
return true;
|
|
10785
11118
|
}
|
|
@@ -10848,6 +11181,8 @@ export class ClientSession {
|
|
|
10848
11181
|
});
|
|
10849
11182
|
const conv = this.makeConversation(runtime, conversationId, terminals);
|
|
10850
11183
|
conv.cwd = this.cwd;
|
|
11184
|
+
conv.independent = independent;
|
|
11185
|
+
conv.session.sessionManager.appendCustomEntry("webui/independent", { independent });
|
|
10851
11186
|
if (ephemeral)
|
|
10852
11187
|
conv.isEphemeral = true;
|
|
10853
11188
|
if (_preset) {
|
|
@@ -11216,7 +11551,7 @@ export class ClientSession {
|
|
|
11216
11551
|
// Also retain a parent while any live first-party subagent conversation
|
|
11217
11552
|
// points at it: dropping an idle in-memory parent orphans the child row
|
|
11218
11553
|
// (the child vanishes from Running Chats with no result available).
|
|
11219
|
-
const hasLiveChild = [...this.convs.values()].some((child) => child
|
|
11554
|
+
const hasLiveChild = [...new Set([this, ...(this.allSubagentSessions?.() ?? [])])].some((owner) => [...owner.convs.values()].some((child) => owner.parentExecution(child)?.conv === conv));
|
|
11220
11555
|
// Ephemeral transcripts have no history fallback: once started, only an
|
|
11221
11556
|
// explicit close may discard them, even after revisiting without a prompt.
|
|
11222
11557
|
const retainFinished = (Boolean(conv.isEphemeral) || Boolean(this.settingsSvc?.current.retainFinishedConversations)) &&
|
|
@@ -11359,7 +11694,7 @@ export class ClientSession {
|
|
|
11359
11694
|
id: c.id,
|
|
11360
11695
|
title: c.title,
|
|
11361
11696
|
cwd: c.cwd,
|
|
11362
|
-
...(c
|
|
11697
|
+
...(this.localParentId(c) ? { parentId: this.localParentId(c) } : {}),
|
|
11363
11698
|
isSubagent: c.isSubagent,
|
|
11364
11699
|
isEphemeral: !!c.isEphemeral,
|
|
11365
11700
|
...(sessionFile ? { sessionFile } : {}),
|
|
@@ -11380,6 +11715,8 @@ export class ClientSession {
|
|
|
11380
11715
|
const convs = [...this.convs.values()].filter((c) => set.has(c.id));
|
|
11381
11716
|
if (convs.length === 0)
|
|
11382
11717
|
return { ok: false, reason: "missing" };
|
|
11718
|
+
for (const conv of this.convs.values())
|
|
11719
|
+
this.parentExecution(conv);
|
|
11383
11720
|
for (const conv of convs)
|
|
11384
11721
|
conv.transferring = true;
|
|
11385
11722
|
try {
|
|
@@ -11471,17 +11808,33 @@ export class ClientSession {
|
|
|
11471
11808
|
if (this.botExecution)
|
|
11472
11809
|
throw new Error("Bot sessions cannot receive transferred conversations");
|
|
11473
11810
|
const remap = new Map();
|
|
11811
|
+
const incomingIds = new Set(payload.convs.map((conv) => conv.id));
|
|
11812
|
+
const reserved = new Set([...this.convs.keys(), ...incomingIds]);
|
|
11813
|
+
// Resolve legacy parent links before any ids mutate or destination ids collide.
|
|
11814
|
+
for (const conv of payload.convs) {
|
|
11815
|
+
if (!hasConversationParentBinding(conv)) {
|
|
11816
|
+
bindConversationParent(conv, payload.convs.find((candidate) => candidate.id === conv.parentId));
|
|
11817
|
+
}
|
|
11818
|
+
}
|
|
11474
11819
|
for (const conv of payload.convs) {
|
|
11475
11820
|
if (this.convs.has(conv.id)) {
|
|
11476
|
-
|
|
11821
|
+
let next;
|
|
11822
|
+
do
|
|
11823
|
+
next = this.nextConversationId();
|
|
11824
|
+
while (reserved.has(next));
|
|
11825
|
+
reserved.add(next);
|
|
11826
|
+
remap.set(conv.id, next);
|
|
11477
11827
|
}
|
|
11478
11828
|
}
|
|
11479
11829
|
const fix = (id) => remap.get(id) ?? id;
|
|
11830
|
+
const parentDisplayIds = new Map(payload.convs.map((conv) => {
|
|
11831
|
+
const parent = conversationParent(conv);
|
|
11832
|
+
return [conv, parent ? (payload.convs.includes(parent) ? fix(parent.id) : parent.id) : undefined];
|
|
11833
|
+
}));
|
|
11480
11834
|
let mainId = "";
|
|
11481
11835
|
for (const conv of payload.convs) {
|
|
11482
11836
|
conv.id = fix(conv.id);
|
|
11483
|
-
|
|
11484
|
-
conv.parentId = fix(conv.parentId);
|
|
11837
|
+
conv.parentId = parentDisplayIds.get(conv);
|
|
11485
11838
|
if (!conv.isSubagent && !mainId) {
|
|
11486
11839
|
mainId = conv.id;
|
|
11487
11840
|
// 过户是用户显式动作:这条对话必须在某一页看得见。`listed` 一旦置位就进本页的
|
|
@@ -11561,7 +11914,7 @@ export class ClientSession {
|
|
|
11561
11914
|
for (const a of payload.approvals) {
|
|
11562
11915
|
if (settledApprovalRequests.has(a))
|
|
11563
11916
|
continue;
|
|
11564
|
-
const nid = `appr-${
|
|
11917
|
+
const nid = `appr-${randomUUID()}`;
|
|
11565
11918
|
const aConvId = fix(a.conversationId);
|
|
11566
11919
|
const targetConv = this.convs.get(aConvId);
|
|
11567
11920
|
if (targetConv) {
|
|
@@ -11639,8 +11992,22 @@ export class ClientSession {
|
|
|
11639
11992
|
}
|
|
11640
11993
|
/** Switch the ACTIVE conversation without interrupting any other chat. */
|
|
11641
11994
|
async switchConversation(id) {
|
|
11642
|
-
if (id === this.activeId)
|
|
11995
|
+
if (id === this.activeId) {
|
|
11996
|
+
// A shared view may hide the still-active local conversation. Returning
|
|
11997
|
+
// to it needs a full snapshot (the browser may hold the remote rev)
|
|
11998
|
+
// and an acknowledgement to finish the browser's navigation transition.
|
|
11999
|
+
this.flushSnapshot(true);
|
|
12000
|
+
this.emit({
|
|
12001
|
+
type: "transition_progress",
|
|
12002
|
+
kind: "conversation",
|
|
12003
|
+
target: id,
|
|
12004
|
+
phase: "done",
|
|
12005
|
+
progress: 100,
|
|
12006
|
+
text: "对话切换完成",
|
|
12007
|
+
textEn: "Conversation switched",
|
|
12008
|
+
});
|
|
11643
12009
|
return;
|
|
12010
|
+
}
|
|
11644
12011
|
if (!this.convs.has(id)) {
|
|
11645
12012
|
this.emit({
|
|
11646
12013
|
type: "transition_progress",
|
|
@@ -11757,7 +12124,7 @@ export class ClientSession {
|
|
|
11757
12124
|
// listed subagents hang under them, so both rows remain clickable.
|
|
11758
12125
|
const visibleParents = new Set([...this.convs.values()]
|
|
11759
12126
|
.filter((conv) => conv.listed)
|
|
11760
|
-
.map((conv) => conv
|
|
12127
|
+
.map((conv) => this.localParentId(conv))
|
|
11761
12128
|
.filter(Boolean));
|
|
11762
12129
|
for (const conv of this.convs.values()) {
|
|
11763
12130
|
if (!this.shownInRunningList(conv) && !visibleParents.has(conv.id))
|
|
@@ -11773,6 +12140,7 @@ export class ClientSession {
|
|
|
11773
12140
|
}
|
|
11774
12141
|
conversations.push({
|
|
11775
12142
|
id: conv.id,
|
|
12143
|
+
independent: conv.independent !== false,
|
|
11776
12144
|
title: conv.title,
|
|
11777
12145
|
cwd: conv.cwd,
|
|
11778
12146
|
messageCount,
|
|
@@ -11801,7 +12169,7 @@ export class ClientSession {
|
|
|
11801
12169
|
const pq = this.getPendingQuestionForConv(conv.id);
|
|
11802
12170
|
return pq ? { hasQuestion: true, questionId: pq.id, questionTitle: pq.title } : {};
|
|
11803
12171
|
})(),
|
|
11804
|
-
parentId: conv
|
|
12172
|
+
parentId: this.localParentId(conv),
|
|
11805
12173
|
model: conv.session.model
|
|
11806
12174
|
? `${conv.session.model.provider}/${conv.session.model.id}`
|
|
11807
12175
|
: (conv.subagentTemplate?.model ?? undefined),
|
|
@@ -12157,33 +12525,46 @@ export class ClientSession {
|
|
|
12157
12525
|
else
|
|
12158
12526
|
await this.newChat();
|
|
12159
12527
|
// displaceActive() may have RETAINED the old conversation as a
|
|
12160
|
-
// background run
|
|
12161
|
-
//
|
|
12162
|
-
//
|
|
12163
|
-
//
|
|
12164
|
-
//
|
|
12165
|
-
//
|
|
12166
|
-
//
|
|
12528
|
+
// background run — the file is still held, so we cannot yank it from a
|
|
12529
|
+
// live runtime. What counts as "live" here is **runtime activity**
|
|
12530
|
+
// (streaming / compacting), not `listed`: `listed` is the left-panel
|
|
12531
|
+
// listing rule, and once displacement started retaining idle chats in
|
|
12532
|
+
// the running list, treating it as "still running" made deleting the
|
|
12533
|
+
// ONLY session fail forever with a bogus "still running in the
|
|
12534
|
+
// background" notice (smoke left-panel-delete has been red since
|
|
12535
|
+
// 2026-10-31 — hidden because a unit failure skipped the smoke step).
|
|
12536
|
+
// A retained-but-idle holder is released instead: deleting an explicit
|
|
12537
|
+
// transcript is user intent, and an idle runtime holding it has no
|
|
12538
|
+
// more work to do.
|
|
12167
12539
|
const stillHeld = [...this.convs.values()].find(holdsTarget);
|
|
12168
12540
|
if (stillHeld) {
|
|
12169
|
-
let stillRunning
|
|
12541
|
+
let stillRunning;
|
|
12170
12542
|
try {
|
|
12171
|
-
stillRunning = stillHeld.session.isStreaming ||
|
|
12543
|
+
stillRunning = stillHeld.session.isStreaming || stillHeld.session.isCompacting;
|
|
12172
12544
|
}
|
|
12173
12545
|
catch {
|
|
12174
|
-
|
|
12546
|
+
stillRunning = true; // 会话正在替换 —— 保守地等它
|
|
12547
|
+
}
|
|
12548
|
+
if (stillRunning) {
|
|
12549
|
+
this.emit({
|
|
12550
|
+
type: "notice",
|
|
12551
|
+
level: "warning",
|
|
12552
|
+
text: "对话仍在后台运行,已停止删除;请等待其结束后再删除",
|
|
12553
|
+
textEn: "Conversation is still running in the background; delete aborted — wait for it to finish and retry",
|
|
12554
|
+
});
|
|
12555
|
+
return;
|
|
12556
|
+
}
|
|
12557
|
+
// 空闲但被持有(后台保留 / 置换未释放)→ 放掉它占用的运行时再删文件。
|
|
12558
|
+
this.removeConversation(stillHeld.id);
|
|
12559
|
+
if ([...this.convs.values()].some(holdsTarget)) {
|
|
12560
|
+
this.emit({
|
|
12561
|
+
type: "notice",
|
|
12562
|
+
level: "warning",
|
|
12563
|
+
text: "该对话仍被占用,无法删除;请先关闭它再重试",
|
|
12564
|
+
textEn: "The conversation is still held; close it and retry the delete",
|
|
12565
|
+
});
|
|
12566
|
+
return;
|
|
12175
12567
|
}
|
|
12176
|
-
this.emit({
|
|
12177
|
-
type: "notice",
|
|
12178
|
-
level: "warning",
|
|
12179
|
-
text: stillRunning
|
|
12180
|
-
? "对话仍在后台运行,已停止删除;请等待其结束后再删除"
|
|
12181
|
-
: "未能切换到其他对话,已取消删除本次操作",
|
|
12182
|
-
textEn: stillRunning
|
|
12183
|
-
? "Conversation is still running in the background; delete aborted — wait for it to finish and retry"
|
|
12184
|
-
: "Could not switch to another conversation; delete cancelled",
|
|
12185
|
-
});
|
|
12186
|
-
return;
|
|
12187
12568
|
}
|
|
12188
12569
|
}
|
|
12189
12570
|
// #145 同款守卫:删之前查一遍其他客户端是否持有这份转录 ——
|
|
@@ -12548,8 +12929,12 @@ export class ClientSession {
|
|
|
12548
12929
|
return true;
|
|
12549
12930
|
}
|
|
12550
12931
|
};
|
|
12551
|
-
const descendants = collectSubagentDescendantIds([...this.convs.values()].map((c) => ({
|
|
12552
|
-
|
|
12932
|
+
const descendants = collectSubagentDescendantIds([...this.convs.values()].map((c) => ({
|
|
12933
|
+
id: conversationIdentity(c),
|
|
12934
|
+
parentId: this.parentExecutionKey(c),
|
|
12935
|
+
isSubagent: c.isSubagent,
|
|
12936
|
+
})), conversationIdentity(conv))
|
|
12937
|
+
.map((did) => this.resolveExecution(did)?.conv)
|
|
12553
12938
|
.filter((c) => !!c);
|
|
12554
12939
|
if (force) {
|
|
12555
12940
|
await this.forceDismissConversation(conv, descendants, isStreaming);
|
|
@@ -12673,7 +13058,8 @@ export class ClientSession {
|
|
|
12673
13058
|
return;
|
|
12674
13059
|
}
|
|
12675
13060
|
}
|
|
12676
|
-
this.
|
|
13061
|
+
if (this.convs.get(conv.id) === conv && conv.id !== this.activeId)
|
|
13062
|
+
this.removeConversation(conv.id);
|
|
12677
13063
|
this.emitConversations();
|
|
12678
13064
|
this.flushSnapshot();
|
|
12679
13065
|
}
|
|
@@ -12705,7 +13091,7 @@ export class ClientSession {
|
|
|
12705
13091
|
continue;
|
|
12706
13092
|
if (isStreaming(d)) {
|
|
12707
13093
|
try {
|
|
12708
|
-
await this.subagentHost.stopSubagent(d
|
|
13094
|
+
await this.subagentHost.stopSubagent(conversationIdentity(d));
|
|
12709
13095
|
stopped++;
|
|
12710
13096
|
}
|
|
12711
13097
|
catch {
|
|
@@ -12722,7 +13108,7 @@ export class ClientSession {
|
|
|
12722
13108
|
const deferred = [];
|
|
12723
13109
|
for (const d of descendants) {
|
|
12724
13110
|
const cur = this.convs.get(d.id);
|
|
12725
|
-
if (
|
|
13111
|
+
if (cur !== d || cur === conv)
|
|
12726
13112
|
continue;
|
|
12727
13113
|
if (cur.id === this.activeId) {
|
|
12728
13114
|
deferred.push(cur);
|
|
@@ -12793,14 +13179,13 @@ export class ClientSession {
|
|
|
12793
13179
|
let d = 0;
|
|
12794
13180
|
let cur = this.convs.get(id);
|
|
12795
13181
|
const seen = new Set([id]);
|
|
12796
|
-
while (cur
|
|
12797
|
-
|
|
13182
|
+
while (cur) {
|
|
13183
|
+
const parent = this.parentExecution(cur);
|
|
13184
|
+
if (!parent || seen.has(conversationIdentity(parent.conv)))
|
|
12798
13185
|
break;
|
|
12799
|
-
seen.add(
|
|
13186
|
+
seen.add(conversationIdentity(parent.conv));
|
|
12800
13187
|
d++;
|
|
12801
|
-
cur =
|
|
12802
|
-
if (!cur)
|
|
12803
|
-
break;
|
|
13188
|
+
cur = parent.conv;
|
|
12804
13189
|
}
|
|
12805
13190
|
return d;
|
|
12806
13191
|
};
|
|
@@ -12817,15 +13202,14 @@ export class ClientSession {
|
|
|
12817
13202
|
return true;
|
|
12818
13203
|
let cur = conv;
|
|
12819
13204
|
const seen = new Set();
|
|
12820
|
-
while (cur
|
|
12821
|
-
|
|
12822
|
-
|
|
12823
|
-
if (seen.has(cur.parentId))
|
|
12824
|
-
return false;
|
|
12825
|
-
seen.add(cur.parentId);
|
|
12826
|
-
cur = this.convs.get(cur.parentId);
|
|
12827
|
-
if (!cur)
|
|
13205
|
+
while (cur) {
|
|
13206
|
+
const parent = this.parentExecution(cur);
|
|
13207
|
+
if (!parent || seen.has(conversationIdentity(parent.conv)))
|
|
12828
13208
|
return false;
|
|
13209
|
+
if (parent.conv === this.convs.get(root))
|
|
13210
|
+
return true;
|
|
13211
|
+
seen.add(conversationIdentity(parent.conv));
|
|
13212
|
+
cur = parent.conv;
|
|
12829
13213
|
}
|
|
12830
13214
|
return false;
|
|
12831
13215
|
};
|
|
@@ -12876,7 +13260,7 @@ export class ClientSession {
|
|
|
12876
13260
|
}
|
|
12877
13261
|
// Re-check live children: earlier removals in this same batch may
|
|
12878
13262
|
// have cleared the guard; still-running children block the parent.
|
|
12879
|
-
const liveChild = [...this.convs.values()].some((child) => child
|
|
13263
|
+
const liveChild = [...this.convs.values()].some((child) => this.parentExecution(child)?.conv === cur && isStreaming(child));
|
|
12880
13264
|
if (liveChild) {
|
|
12881
13265
|
skippedRunning++;
|
|
12882
13266
|
continue;
|
|
@@ -12959,7 +13343,7 @@ export class ClientSession {
|
|
|
12959
13343
|
* current runtime, which would otherwise stop a response merely because the
|
|
12960
13344
|
* user opened history while it was streaming.
|
|
12961
13345
|
*/
|
|
12962
|
-
async switchSession(path, sessionId) {
|
|
13346
|
+
async switchSession(path, sessionId, independent) {
|
|
12963
13347
|
if (this.quiesceBlocked())
|
|
12964
13348
|
return;
|
|
12965
13349
|
const timing = startLoadTiming();
|
|
@@ -13012,6 +13396,8 @@ export class ClientSession {
|
|
|
13012
13396
|
for (const conv of this.convs.values()) {
|
|
13013
13397
|
const sessionFile = conv.session.sessionFile;
|
|
13014
13398
|
if (sessionFile && resolve(sessionFile) === targetPath) {
|
|
13399
|
+
if (independent !== undefined)
|
|
13400
|
+
this.setConversationIndependent(conv.id, independent);
|
|
13015
13401
|
await this.switchConversation(conv.id);
|
|
13016
13402
|
outcome = "reused";
|
|
13017
13403
|
this.emit({
|
|
@@ -13032,7 +13418,16 @@ export class ClientSession {
|
|
|
13032
13418
|
// 伪客户端(定时任务等):不允许过户抢占,直接拦截;
|
|
13033
13419
|
// 空闲或离线宽限期残骸:直接执行跨客户端自动过户(Auto-Takeover),
|
|
13034
13420
|
// 搬移 runtime 本体,保持单 writer,避免双 runtime 导致激活冲突与分支分叉。
|
|
13421
|
+
if (this.botExecution) {
|
|
13422
|
+
this.botExecution.assertActive();
|
|
13423
|
+
if (targetPath !== resolve(this.botExecution.authorizedResumePath ?? ""))
|
|
13424
|
+
throw new Error("Bot transcript resume is not authorized");
|
|
13425
|
+
await this.prepareBotSessionResume?.(targetPath);
|
|
13426
|
+
this.botExecution.assertActive();
|
|
13427
|
+
}
|
|
13035
13428
|
const owner = this.findSessionOwner?.(targetPath);
|
|
13429
|
+
if (owner && this.botExecution)
|
|
13430
|
+
throw new Error("Bot transcript still has an active owner");
|
|
13036
13431
|
if (owner && owner.isStreaming) {
|
|
13037
13432
|
this.emit({
|
|
13038
13433
|
type: "notice",
|
|
@@ -13147,6 +13542,10 @@ export class ClientSession {
|
|
|
13147
13542
|
return;
|
|
13148
13543
|
}
|
|
13149
13544
|
const conv = this.makeConversation(openedRuntime, conversationId, openedTerminals);
|
|
13545
|
+
if (independent !== undefined) {
|
|
13546
|
+
conv.session.sessionManager.appendCustomEntry("webui/independent", { independent });
|
|
13547
|
+
conv.independent = independent;
|
|
13548
|
+
}
|
|
13150
13549
|
// Deliberately resumed — must not be dismissed when the user later
|
|
13151
13550
|
// switches away without sending a new message.
|
|
13152
13551
|
conv.listed = true;
|
|
@@ -13177,7 +13576,8 @@ export class ClientSession {
|
|
|
13177
13576
|
await this.restoreProjectProviderKeysForCwd(targetCwd);
|
|
13178
13577
|
if (this.disposed || this.activeId !== convId || this.cwd !== targetCwd)
|
|
13179
13578
|
return;
|
|
13180
|
-
|
|
13579
|
+
if (!this.botExecution)
|
|
13580
|
+
await this.restoreProjectModelForCwd(targetCwd);
|
|
13181
13581
|
if (this.disposed || this.activeId !== convId || this.cwd !== targetCwd)
|
|
13182
13582
|
return;
|
|
13183
13583
|
this.flushSnapshot();
|
|
@@ -13242,24 +13642,26 @@ export class ClientSession {
|
|
|
13242
13642
|
* Map a rendered user-message id (`u-<timestamp>-<seq>`, assigned in
|
|
13243
13643
|
* serialize.ts) back to its append-only session entry id.
|
|
13244
13644
|
*/
|
|
13245
|
-
resolveUserMessageEntryId(messageId) {
|
|
13246
|
-
return resolveUserMessageEntryId(this.session.sessionManager.buildContextEntries(), messageId);
|
|
13645
|
+
resolveUserMessageEntryId(messageId, targetConversation) {
|
|
13646
|
+
return resolveUserMessageEntryId(undeletedContextEntries((targetConversation ?? this.conv).session.sessionManager.buildContextEntries()), messageId);
|
|
13247
13647
|
}
|
|
13248
13648
|
/**
|
|
13249
13649
|
* Map any rendered message id (u-*, a-*, t-*, b-*, c-*, or raw entry id)
|
|
13250
13650
|
* back to its append-only session entry in the given conversation.
|
|
13251
13651
|
*/
|
|
13252
13652
|
resolveMessageEntry(conv, messageId) {
|
|
13253
|
-
return resolveConversationMessageEntry(conv.session.sessionManager.buildContextEntries(), messageId, (m) => this.uiMessageKey(conv, m).n, (id) => conv.session.sessionManager.getEntry(id));
|
|
13653
|
+
return resolveConversationMessageEntry(undeletedContextEntries(conv.session.sessionManager.buildContextEntries()), messageId, (m) => this.uiMessageKey(conv, m).n, (id) => conv.session.sessionManager.getEntry(id));
|
|
13254
13654
|
}
|
|
13255
13655
|
/**
|
|
13256
13656
|
* 获取被某个压缩卡片折叠的历史消息(issue #398,按需惰性加载)。
|
|
13257
13657
|
*/
|
|
13258
|
-
getCompactedMessages(compactionMessageId, targetConvId) {
|
|
13259
|
-
const targetConv =
|
|
13658
|
+
getCompactedMessages(compactionMessageId, targetConvId, send = this.emit.bind(this)) {
|
|
13659
|
+
const targetConv = targetConvId ? this.convs.get(targetConvId) : this.conv;
|
|
13660
|
+
if (!targetConv)
|
|
13661
|
+
throw new Error("Conversation no longer available");
|
|
13260
13662
|
const sm = targetConv.session.sessionManager;
|
|
13261
13663
|
const result = getCompactedMessages(sm, compactionMessageId, (m) => this.uiMessageKey(targetConv, m).n);
|
|
13262
|
-
|
|
13664
|
+
send({
|
|
13263
13665
|
type: "compacted_messages_result",
|
|
13264
13666
|
compactionMessageId,
|
|
13265
13667
|
conversationId: targetConv.id,
|
|
@@ -13582,7 +13984,54 @@ export class ClientSession {
|
|
|
13582
13984
|
* Replace a past question and its later history in the same SDK session/file.
|
|
13583
13985
|
* Attachments are re-sent through prompt() after removing the original turn.
|
|
13584
13986
|
*/
|
|
13585
|
-
async
|
|
13987
|
+
async deleteMessage(messageId, conversationId) {
|
|
13988
|
+
if (this.quiesceBlocked())
|
|
13989
|
+
return;
|
|
13990
|
+
const target = conversationId ? this.convs.get(conversationId) : this.conv;
|
|
13991
|
+
try {
|
|
13992
|
+
if (!target)
|
|
13993
|
+
throw new Error("Conversation is no longer available");
|
|
13994
|
+
const session = target.session;
|
|
13995
|
+
if (session.isStreaming)
|
|
13996
|
+
throw new Error("Stop generation before deleting a message");
|
|
13997
|
+
const manager = session.sessionManager;
|
|
13998
|
+
if (typeof manager.appendContextEdit !== "function")
|
|
13999
|
+
throw new Error("This engine does not support message deletion");
|
|
14000
|
+
const entry = this.resolveMessageEntry(target, messageId);
|
|
14001
|
+
if (!entry)
|
|
14002
|
+
throw new Error("Message not found");
|
|
14003
|
+
const ids = messageDeletionTargets(manager.buildContextEntries(), entry.id);
|
|
14004
|
+
try {
|
|
14005
|
+
for (const id of ids)
|
|
14006
|
+
manager.appendContextEdit(id, null);
|
|
14007
|
+
}
|
|
14008
|
+
catch (error) {
|
|
14009
|
+
// SDK mutates indexes before writing; restore the committed transcript on failure.
|
|
14010
|
+
if (session.sessionFile)
|
|
14011
|
+
manager.setSessionFile(session.sessionFile);
|
|
14012
|
+
throw error;
|
|
14013
|
+
}
|
|
14014
|
+
finally {
|
|
14015
|
+
session.agent.state.messages = manager.buildSessionContext().messages;
|
|
14016
|
+
target.uiMessageCache.clear();
|
|
14017
|
+
target.userSeqByTs.clear();
|
|
14018
|
+
target.lastMessagesSig = "";
|
|
14019
|
+
target.lastMessagesArray = [];
|
|
14020
|
+
}
|
|
14021
|
+
this.invalidateSessionInfos();
|
|
14022
|
+
this.scheduleSessionsRefresh();
|
|
14023
|
+
}
|
|
14024
|
+
catch (error) {
|
|
14025
|
+
this.emit({
|
|
14026
|
+
type: "notice",
|
|
14027
|
+
level: "error",
|
|
14028
|
+
text: `删除消息失败:${error.message}`,
|
|
14029
|
+
textEn: `Failed to delete message: ${error.message}`,
|
|
14030
|
+
});
|
|
14031
|
+
}
|
|
14032
|
+
this.flushSnapshot();
|
|
14033
|
+
}
|
|
14034
|
+
async editMessage(messageId, text, attachments, targetConversation) {
|
|
13586
14035
|
if (this.quiesceBlocked())
|
|
13587
14036
|
return;
|
|
13588
14037
|
const trimmed = text.trim();
|
|
@@ -13597,7 +14046,11 @@ export class ClientSession {
|
|
|
13597
14046
|
this.flushSnapshot();
|
|
13598
14047
|
return;
|
|
13599
14048
|
}
|
|
13600
|
-
|
|
14049
|
+
if (targetConversation && this.convs.get(targetConversation.id) !== targetConversation)
|
|
14050
|
+
throw new Error("Edit conversation is no longer available");
|
|
14051
|
+
const entryId = targetConversation
|
|
14052
|
+
? this.resolveUserMessageEntryId(messageId, targetConversation)
|
|
14053
|
+
: this.resolveUserMessageEntryId(messageId);
|
|
13601
14054
|
if (!entryId) {
|
|
13602
14055
|
this.emit({
|
|
13603
14056
|
type: "notice",
|
|
@@ -13608,7 +14061,7 @@ export class ClientSession {
|
|
|
13608
14061
|
this.flushSnapshot();
|
|
13609
14062
|
return;
|
|
13610
14063
|
}
|
|
13611
|
-
const target = this.conv;
|
|
14064
|
+
const target = targetConversation ?? this.conv;
|
|
13612
14065
|
const runtime = target.runtime;
|
|
13613
14066
|
const session = target.session;
|
|
13614
14067
|
const sessionFile = session.sessionFile;
|
|
@@ -13924,18 +14377,18 @@ export class ClientSession {
|
|
|
13924
14377
|
* 恰好应答一次:任何失败都以 ok:false 的 scm_data(kind "commitmsg")收尾,
|
|
13925
14378
|
* 前端按钮不会卡在转圈。
|
|
13926
14379
|
*/
|
|
13927
|
-
async scmGenCommitMessage(reqId) {
|
|
14380
|
+
async scmGenCommitMessage(reqId, conv = this.conv, send = this.emit.bind(this)) {
|
|
13928
14381
|
const lang = this.getLang();
|
|
13929
|
-
const cwd =
|
|
14382
|
+
const cwd = conv.cwd;
|
|
13930
14383
|
const reply = (ok, extra) => {
|
|
13931
|
-
|
|
14384
|
+
send({ type: "scm_data", reqId, kind: "commitmsg", ok, ...extra });
|
|
13932
14385
|
};
|
|
13933
14386
|
const fail = (err) => {
|
|
13934
14387
|
reply(false, { error: err instanceof Error ? err.message : String(err) });
|
|
13935
14388
|
};
|
|
13936
14389
|
try {
|
|
13937
|
-
const runtime =
|
|
13938
|
-
const model =
|
|
14390
|
+
const runtime = conv.runtime.services.modelRuntime;
|
|
14391
|
+
const model = conv.session?.model;
|
|
13939
14392
|
if (!model) {
|
|
13940
14393
|
throw new Error(pick(lang, "当前没有可用模型——先在顶栏选择一个模型再生成", "No model available — pick one in the top bar first", "scm.commitmsg.no.model"));
|
|
13941
14394
|
}
|
|
@@ -13959,6 +14412,17 @@ export class ClientSession {
|
|
|
13959
14412
|
fail(err);
|
|
13960
14413
|
}
|
|
13961
14414
|
}
|
|
14415
|
+
/** Shared viewers use a captured workspace instead of active getters. */
|
|
14416
|
+
createConversationFiles(conv, send) {
|
|
14417
|
+
const cwd = conv.cwd;
|
|
14418
|
+
return new FilesService({
|
|
14419
|
+
emit: send,
|
|
14420
|
+
isDisposed: () => this.disposed || this.convs.get(conv.id) !== conv,
|
|
14421
|
+
getCwd: () => cwd,
|
|
14422
|
+
getActiveCwd: () => cwd,
|
|
14423
|
+
getLang: () => this.getLang(),
|
|
14424
|
+
});
|
|
14425
|
+
}
|
|
13962
14426
|
/** Read a workspace file for the preview panel (size-capped, binary-safe). */
|
|
13963
14427
|
async readFile(relPath) {
|
|
13964
14428
|
return this.files.readFile(relPath);
|
|
@@ -14623,17 +15087,17 @@ export class ClientSession {
|
|
|
14623
15087
|
// Goal / review
|
|
14624
15088
|
// ---------------------------------------------------------------------------
|
|
14625
15089
|
/** Goal family delegates to GoalService (see goal-service.ts). */
|
|
14626
|
-
async setGoal(goalText, opts) {
|
|
14627
|
-
return this.goalSvc.setGoal(goalText, opts);
|
|
15090
|
+
async setGoal(goalText, opts, conv) {
|
|
15091
|
+
return this.goalSvc.setGoal(goalText, { ...opts, ...(conv ? { targetConvId: conv.id } : {}) });
|
|
14628
15092
|
}
|
|
14629
|
-
async startGoalWizard(text, opts) {
|
|
14630
|
-
return this.goalSvc.startGoalWizard(text, opts);
|
|
15093
|
+
async startGoalWizard(text, opts, conv) {
|
|
15094
|
+
return this.goalSvc.startGoalWizard(text, opts, conv);
|
|
14631
15095
|
}
|
|
14632
|
-
async setGoalPrefs(opts) {
|
|
14633
|
-
return this.goalSvc.setGoalPrefs(opts);
|
|
15096
|
+
async setGoalPrefs(opts, conv) {
|
|
15097
|
+
return this.goalSvc.setGoalPrefs(opts, conv);
|
|
14634
15098
|
}
|
|
14635
|
-
async clearGoal() {
|
|
14636
|
-
return this.goalSvc.clearGoal();
|
|
15099
|
+
async clearGoal(conv) {
|
|
15100
|
+
return this.goalSvc.clearGoal(conv);
|
|
14637
15101
|
}
|
|
14638
15102
|
/**
|
|
14639
15103
|
* Run a git diff (unstaged + staged) in a conversation's workspace, or
|
|
@@ -14667,11 +15131,11 @@ export class ClientSession {
|
|
|
14667
15131
|
/** Switch to a specific model by "provider/id" (e.g. "anthropic/claude-sonnet-5").
|
|
14668
15132
|
* 失败时只发 notice 不抛错(UI 路径靠 notice 提示,见 cycleModel 等调用方)。
|
|
14669
15133
|
* 需要「失败即拒绝」的无头路径(插件/定时任务)用 switchModelOrThrow。 */
|
|
14670
|
-
async setModel(modelId) {
|
|
15134
|
+
async setModel(modelId, conv = this.conv) {
|
|
14671
15135
|
if (this.botExecution)
|
|
14672
15136
|
throw new Error("Bot model is fixed");
|
|
14673
15137
|
try {
|
|
14674
|
-
const mr =
|
|
15138
|
+
const mr = conv.runtime.services.modelRuntime;
|
|
14675
15139
|
const slash = modelId.indexOf("/");
|
|
14676
15140
|
if (slash <= 0 || slash === modelId.length - 1) {
|
|
14677
15141
|
throw new Error(`无效的模型 ID:${modelId}`);
|
|
@@ -14682,15 +15146,19 @@ export class ClientSession {
|
|
|
14682
15146
|
if (!model)
|
|
14683
15147
|
throw new Error(`模型不存在:${modelId}`);
|
|
14684
15148
|
// 先恢复 provider key,再 setModel(否则 checkAuth 鉴权失败)
|
|
14685
|
-
await this.restoreKeyForModel(modelId,
|
|
14686
|
-
|
|
15149
|
+
await this.restoreKeyForModel(modelId, conv.cwd);
|
|
15150
|
+
if (this.convs.get(conv.id) !== conv)
|
|
15151
|
+
return;
|
|
15152
|
+
await this.applySessionModel(conv.session, model);
|
|
14687
15153
|
// Immediately remember the model + the key it uses for the current
|
|
14688
15154
|
// project (not only after a turn). This is what makes project switching
|
|
14689
15155
|
// restore both the model and the provider key.
|
|
14690
|
-
this.
|
|
15156
|
+
if (conv === this.conv)
|
|
15157
|
+
this.rememberProjectModel(modelId);
|
|
14691
15158
|
// 换模型后按新模型的窗口重算软上限覆盖已由 applySessionModel 完成(issue #229/#773)。
|
|
14692
15159
|
// 切换成功 → 立即通知插件(#542:不发消息也能收到);失败路径落在 catch 里,不发。
|
|
14693
|
-
this.
|
|
15160
|
+
if (conv === this.conv)
|
|
15161
|
+
this.notifyPluginModelChange();
|
|
14694
15162
|
}
|
|
14695
15163
|
catch (err) {
|
|
14696
15164
|
this.emit({
|
|
@@ -14788,13 +15256,13 @@ export class ClientSession {
|
|
|
14788
15256
|
throw new Error(`切换模型失败(${modelId}),当前仍是 ${curId} —— 请检查模型 ID 与供应商密钥`);
|
|
14789
15257
|
}
|
|
14790
15258
|
/** Set the thinking level for future turns. */
|
|
14791
|
-
setThinking(level) {
|
|
15259
|
+
setThinking(level, conv = this.conv) {
|
|
14792
15260
|
try {
|
|
14793
15261
|
const thinkingLevel = level;
|
|
14794
|
-
|
|
14795
|
-
const cur =
|
|
15262
|
+
conv.session.setThinkingLevel(thinkingLevel, { persist: true });
|
|
15263
|
+
const cur = conv.session.model;
|
|
14796
15264
|
if (cur) {
|
|
14797
|
-
|
|
15265
|
+
conv.session.settingsManager.setModelThinkingLevel(cur.provider, cur.id, thinkingLevel);
|
|
14798
15266
|
}
|
|
14799
15267
|
}
|
|
14800
15268
|
catch (err) {
|
|
@@ -14825,7 +15293,10 @@ export class ClientSession {
|
|
|
14825
15293
|
this.emit({ type: "commands", commands, path });
|
|
14826
15294
|
this.emit({ type: "notice", level: "info", text: `命令已保存:${path}`, textEn: `Command saved: ${path}` });
|
|
14827
15295
|
}
|
|
15296
|
+
onDisposed;
|
|
14828
15297
|
async dispose() {
|
|
15298
|
+
this.onDisposed?.();
|
|
15299
|
+
this.onDisposed = undefined;
|
|
14829
15300
|
// Escalate incoming decisions while this holder is still live, then close execution identities.
|
|
14830
15301
|
for (const conv of this.convs.values()) {
|
|
14831
15302
|
for (const request of conversationCommunication.incomingRequests(conv)) {
|