@steerable/agent-shell-web 0.9.1 → 0.10.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.
@@ -30,8 +30,10 @@ import type { ExecutedAction } from "@/components/chat/ExecutedActionsCard";
30
30
  import { inspectTaskTitle, type InspectTaskInput } from "@/components/chat/executed-actions-model";
31
31
  import type { ChildInfo } from "@/components/chat/OrchestrationChildrenCard";
32
32
  import {
33
+ applyChildEvent,
33
34
  extractPersistedOrchestrationChildren,
34
35
  foldOrchestrationChildEvents,
36
+ mergeSessionChildren,
35
37
  } from "@/components/chat/orchestration-children-model";
36
38
  import { parseTurnBlocks, type TurnBlock } from "@/components/chat/turn-timeline";
37
39
  import {
@@ -65,7 +67,7 @@ import {
65
67
  setPendingFirstMessage,
66
68
  takePendingFirstMessage,
67
69
  } from "@/lib/pending-first-message";
68
- import { isOrchestrationSettingEnabled } from "@/lib/orchestration-settings";
70
+ import { readOrchestrationSetting } from "@/lib/orchestration-settings";
69
71
  import { pickDefaultAgentId } from "@/brand";
70
72
  import { t } from "@/i18n";
71
73
  import {
@@ -124,7 +126,9 @@ function applyComposerTurnSettings<T extends { metadata?: Record<string, unknown
124
126
  else delete metadata.mode;
125
127
  if (settings.modelOverride) metadata.model = settings.modelOverride;
126
128
  if (settings.effortOverride) metadata.reasoningEffort = settings.effortOverride;
127
- metadata.orchestration = isOrchestrationSettingEnabled();
129
+ // R7:对象形式带深度与并发。布尔形式仍被宿主的 `resolveTurnOrchestration`
130
+ // 接受(旧客户端),但用户调过的上限只有对象形式能到 sidecar。
131
+ metadata.orchestration = readOrchestrationSetting();
128
132
  return { ...input, metadata };
129
133
  }
130
134
 
@@ -205,6 +209,37 @@ function extractPersistedTurnFiles(
205
209
  return seeded;
206
210
  }
207
211
 
212
+ /**
213
+ * 把注册表里的子代理并进重启后的面板(R7-2)。
214
+ *
215
+ * 与面板同一份数据源:注册行回答「这个会话派过谁」,落库事件回答「它们怎么
216
+ * 结束的」。注册表不可用(sidecar 未起、旧版本没有这条 RPC)时原样返回——
217
+ * 面板退化成本次挂载能看到的那些,比整块消失好。
218
+ *
219
+ * @param chatId 会话 id(也是父记录 id 的缺省值)。
220
+ * @param persisted 落库事件水合出的「消息 id → 子代理」。
221
+ * @param orderedIds 会话消息按时间顺序的 id。
222
+ */
223
+ async function mergeRegistryChildren(
224
+ chatId: string,
225
+ persisted: Record<string, ChildInfo[]>,
226
+ orderedIds: readonly string[],
227
+ ): Promise<Record<string, ChildInfo[]>> {
228
+ if (!hasHostBridge()) return persisted;
229
+ try {
230
+ const response = await getHostBridge()!.localBackend.request<{
231
+ children?: Array<Record<string, unknown>>;
232
+ }>({
233
+ method: "GET",
234
+ path: `/api/v2/children?session=${encodeURIComponent(chatId)}`,
235
+ });
236
+ return mergeSessionChildren(persisted, orderedIds, response.children ?? []);
237
+ } catch {
238
+ // 索引读不到不该让会话打不开:面板退化成事件流能重建的那些。
239
+ return persisted;
240
+ }
241
+ }
242
+
208
243
  /**
209
244
  * Routing layer for `/agent/:chatId`.
210
245
  *
@@ -280,8 +315,7 @@ function AgentChatLoader({
280
315
  const [initialOrchestrationChildren, setInitialOrchestrationChildren] =
281
316
  useState<Record<string, ChildInfo[]>>({});
282
317
  const [initialSuggestedReplies, setInitialSuggestedReplies] =
283
- useState<SuggestedRepliesState | null>(null);
284
- // W7-1: 后端在 messages 响应里下发 interrupted(上一轮崩溃/强杀中断)。
318
+ useState<SuggestedRepliesState | null>(null); // W7-1: 后端在 messages 响应里下发 interrupted(上一轮崩溃/强杀中断)。
285
319
  const [initialInterrupted, setInitialInterrupted] = useState(false);
286
320
  // 运行中回合的实时快照:切走再切回时用它在历史消息之上叠出「正在运行」
287
321
  // 的助手气泡 + 部分工具卡片 / 时间线。
@@ -316,10 +350,19 @@ function AgentChatLoader({
316
350
  setInitialTimelines(extractPersistedTimelines(response.messages));
317
351
  setInitialDurations(extractPersistedDurations(response.messages));
318
352
  setInitialTurnFiles(extractPersistedTurnFiles(response.messages));
319
- setInitialOrchestrationChildren(
320
- extractPersistedOrchestrationChildren(response.messages),
353
+ // R7-2:落库事件只覆盖「回合跑到了收尾」的情形。进程被杀时最后一条
354
+ // assistant 消息可能没落库,那些子代理只存在于注册表里——不合并的话
355
+ // 用户重开后看到的比实际派出去的少。
356
+ const persistedChildren = extractPersistedOrchestrationChildren(
357
+ response.messages,
321
358
  );
322
- setInitialSuggestedReplies(
359
+ setInitialOrchestrationChildren(
360
+ await mergeRegistryChildren(
361
+ chatId,
362
+ persistedChildren,
363
+ ordered.map((message) => message.id),
364
+ ),
365
+ ); setInitialSuggestedReplies(
323
366
  extractLatestSuggestedReplies(
324
367
  ordered as ChatMessageWithMetadata[],
325
368
  ),
@@ -536,6 +579,9 @@ function AgentChatView({
536
579
  const [orchestrationChildrenByMessageId, setOrchestrationChildrenByMessageId] =
537
580
  useState<Record<string, ChildInfo[]>>(initialOrchestrationChildren);
538
581
  const pendingChildrenRef = useRef<ChildInfo[]>([]);
582
+ // Spawn clocks for the live turn, so the elapsed column has a start time
583
+ // even before the child's terminal event arrives.
584
+ const pendingChildSpawnsRef = useRef<Map<string, number>>(new Map());
539
585
  // Round counter for the in-flight turn. Bumped on every `round_end` event
540
586
  // emitted by chat-transport (was previously suppressed). Driving this state
541
587
  // lets `StreamingStatus` render "Round 2 · 继续推理..." between LLM bursts —
@@ -652,41 +698,13 @@ function AgentChatView({
652
698
  const childId = ev.payload?.childId as string | undefined;
653
699
  const kind = ev.payload?.kind as string | undefined;
654
700
  if (!childId || !kind) return;
655
- const update = (list: ChildInfo[]): ChildInfo[] => {
656
- const idx = list.findIndex((c) => c.childId === childId);
657
- if (kind === "child_spawned") {
658
- if (idx >= 0) return list;
659
- return [
660
- ...list,
661
- {
662
- childId,
663
- task: ev.payload?.task as string | undefined,
664
- depth: ev.payload?.depth as number | undefined,
665
- profile: ev.payload?.profile as string | undefined,
666
- // 子代理自己的 durable record:委派行靠它跳转右侧过程栏。
667
- recordId: ev.payload?.recordId as string | undefined,
668
- status: "running",
669
- },
670
- ];
671
- }
672
- if (idx < 0) return list;
673
- const status =
674
- kind === "child_completed"
675
- ? "completed"
676
- : kind === "child_failed"
677
- ? "failed"
678
- : kind === "child_cancelled"
679
- ? "cancelled"
680
- : kind === "child_interrupted"
681
- ? "interrupted"
682
- : kind === "child_resumed"
683
- ? "running"
684
- : list[idx].status;
685
- const next = [...list];
686
- next[idx] = { ...list[idx], status };
687
- return next;
688
- };
689
- pendingChildrenRef.current = update(pendingChildrenRef.current);
701
+ // One fold for both paths: the live list and the replayed one must not
702
+ // disagree about what an event means (status, usage, elapsed).
703
+ pendingChildrenRef.current = applyChildEvent(
704
+ pendingChildrenRef.current,
705
+ ev.payload as Record<string, unknown>,
706
+ pendingChildSpawnsRef.current,
707
+ );
690
708
  setCurrentTurnChildren(pendingChildrenRef.current);
691
709
  return;
692
710
  }
@@ -961,6 +979,7 @@ function AgentChatView({
961
979
  setCurrentTurnTimeline([]);
962
980
  pendingChildrenRef.current = [];
963
981
  setCurrentTurnChildren([]);
982
+ pendingChildSpawnsRef.current = new Map();
964
983
  pendingTurnFilesRef.current = [];
965
984
  setCurrentTurnFiles([]);
966
985
  setCurrentRound(1);
@@ -986,7 +1005,7 @@ function AgentChatView({
986
1005
  ...(modelOverride ? { model: modelOverride } : {}),
987
1006
  ...(effortOverride ? { reasoningEffort: effortOverride } : {}),
988
1007
  ...(execPolicy === "full" ? { execPolicy: "full" } : {}),
989
- orchestration: isOrchestrationSettingEnabled(),
1008
+ orchestration: readOrchestrationSetting(),
990
1009
  };
991
1010
  return sendUserMessage({
992
1011
  ...input,
@@ -1003,6 +1022,61 @@ function AgentChatView({
1003
1022
  [handleSubmit],
1004
1023
  );
1005
1024
 
1025
+ // R7-3「继续」:续跑必须由父代理发起(agent_send 带着父会话的上下文与
1026
+ // 预算),所以这里不是直接起一个游离的子代理,而是发一条让父代理续跑的
1027
+ // 用户消息。先问宿主确认可续——已关闭的子代理会让那一轮白跑。
1028
+ const [childActionError, setChildActionError] = useState<string | null>(null);
1029
+ const handleContinueChild = useCallback(
1030
+ async (childId: string, recordId: string) => {
1031
+ if (!hasHostBridge()) return;
1032
+ let hint = childId;
1033
+ try {
1034
+ const res = await getHostBridge()!.localBackend.request<{
1035
+ message?: string;
1036
+ }>({
1037
+ method: "POST",
1038
+ path: `/api/v2/children/${encodeURIComponent(recordId)}/resume?session=${encodeURIComponent(
1039
+ chatId,
1040
+ )}&child=${encodeURIComponent(childId)}`,
1041
+ body: {},
1042
+ });
1043
+ if (res?.message) hint = res.message;
1044
+ } catch (err) {
1045
+ // 409/404 的含义是「这个子代理不能再跑」:把原因说出来,别静默。
1046
+ setChildActionError(err instanceof Error ? err.message : String(err));
1047
+ return;
1048
+ }
1049
+ void handleSubmit({
1050
+ content: t(
1051
+ 'Continue sub-agent {child} by calling agent_send with childId "{child}" and this message: {message}',
1052
+ { child: childId, message: hint },
1053
+ ),
1054
+ });
1055
+ },
1056
+ [chatId, handleSubmit],
1057
+ );
1058
+
1059
+ // R7-3「停止」:协作取消,不需要回合。子代理保留上下文,之后仍可继续。
1060
+ const handleStopChild = useCallback(
1061
+ async (childId: string) => {
1062
+ if (!hasHostBridge()) return;
1063
+ try {
1064
+ await getHostBridge()!.localBackend.request({
1065
+ method: "POST",
1066
+ path: `/api/v2/children/${encodeURIComponent(
1067
+ `${chatId}:child:${childId}`,
1068
+ )}/stop?session=${encodeURIComponent(chatId)}&child=${encodeURIComponent(
1069
+ childId,
1070
+ )}`,
1071
+ body: {},
1072
+ });
1073
+ } catch (err) {
1074
+ setChildActionError(err instanceof Error ? err.message : String(err));
1075
+ }
1076
+ },
1077
+ [chatId],
1078
+ );
1079
+
1006
1080
  // 远端回合结束时,优先发送用户在等待期间排队的内容。第一条启动新回合,
1007
1081
  // 其余条目进入 useChatStream 的本地队列;没有排队内容才重新水合历史。
1008
1082
  useEffect(() => {
@@ -1411,6 +1485,10 @@ function AgentChatView({
1411
1485
  finishedTasks={finishedTasks}
1412
1486
  onInspectTask={handleInspectTask}
1413
1487
  onDismissFinishedTask={dismissFinished}
1488
+ onContinueChild={handleContinueChild}
1489
+ onStopChild={handleStopChild}
1490
+ childActionError={childActionError}
1491
+ onDismissChildActionError={() => setChildActionError(null)}
1414
1492
  onShare={hasHostBridge() ? handleShare : undefined}
1415
1493
  suggestedReplies={visibleSuggestedReplies}
1416
1494
  onSelectSuggestion={handleSelectSuggestion}