@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.
- package/package.json +4 -4
- package/src/components/chat/AskUserQuestionMenu.tsx +22 -0
- package/src/components/chat/AssistantMessage.tsx +7 -0
- package/src/components/chat/ChatInput.tsx +21 -0
- package/src/components/chat/LocalChatPanel.tsx +16 -0
- package/src/components/chat/MessageList.tsx +34 -0
- package/src/components/chat/OrchestrationChildrenCard.tsx +140 -22
- package/src/components/chat/SessionOrchestrationFlow.tsx +136 -110
- package/src/components/chat/TurnProcessGroup.tsx +21 -0
- package/src/components/chat/orchestration-children-model.ts +274 -28
- package/src/components/chat/orchestration-flow-model.ts +43 -0
- package/src/components/settings/AgentsSettingsPanel.tsx +45 -0
- package/src/components/settings/OrchestrationSettingsPanel.tsx +81 -5
- package/src/components/settings/UsagePanel.tsx +12 -0
- package/src/lib/local-api.ts +7 -0
- package/src/lib/orchestration-settings.ts +122 -0
- package/src/pages/AgentPage.tsx +121 -43
|
@@ -15,6 +15,8 @@ import {
|
|
|
15
15
|
LuX,
|
|
16
16
|
} from 'react-icons/lu';
|
|
17
17
|
import {
|
|
18
|
+
depthFromLineage,
|
|
19
|
+
layerByDepth,
|
|
18
20
|
type OrchestrationChildNode,
|
|
19
21
|
type OrchestrationFlowData,
|
|
20
22
|
type OrchestrationNodeStatus,
|
|
@@ -305,12 +307,114 @@ export function SessionOrchestrationFlow({
|
|
|
305
307
|
return () => document.removeEventListener('pointerdown', handlePointerDown);
|
|
306
308
|
}, [expanded, isAllCompleted]);
|
|
307
309
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
310
|
+
// Layered by depth: "who may spawn whom" is a permission, so the shape of
|
|
311
|
+
// the tree has to be visible instead of one flat row of branches.
|
|
312
|
+
const layers = layerByDepth(nodes);
|
|
313
|
+
|
|
314
|
+
const renderNode = (node: OrchestrationChildNode, index: number) => {
|
|
315
|
+
const done = node.status === 'completed';
|
|
316
|
+
const active = node.status === 'running' || node.status === 'pending';
|
|
317
|
+
const stopped = node.status === 'cancelled' || node.status === 'interrupted';
|
|
318
|
+
const canInspect = Boolean(chatId && onInspectTask && node.recordId);
|
|
319
|
+
return (
|
|
320
|
+
<div
|
|
321
|
+
key={node.childId || `node-${index}`}
|
|
322
|
+
className="flex flex-col justify-between rounded-agent-md border border-agent-border/80 bg-agent-muted/20 p-2.5 text-xs shadow-2xs transition-all hover:border-agent-border hover:bg-agent-muted/30"
|
|
323
|
+
>
|
|
324
|
+
<div>
|
|
325
|
+
{/* 节点顶栏:Todo 式勾选/圆点 + 分支编号 + 状态 */}
|
|
326
|
+
<div className="flex items-center justify-between gap-1 border-b border-agent-border/40 pb-1.5">
|
|
327
|
+
<div className="flex items-center gap-1 min-w-0 flex-1">
|
|
328
|
+
<FlowNodeStatusIcon status={node.status} />
|
|
329
|
+
<span className="font-semibold text-agent-foreground text-[11px] truncate whitespace-nowrap">
|
|
330
|
+
{t('Branch #{index}', { index: index + 1 })}
|
|
331
|
+
</span>
|
|
332
|
+
</div>
|
|
333
|
+
|
|
334
|
+
<div className="flex items-center gap-1 shrink-0">
|
|
335
|
+
<span
|
|
336
|
+
className={`shrink-0 whitespace-nowrap rounded-full border px-1.5 py-0.2 text-[10px] font-medium leading-tight ${
|
|
337
|
+
done
|
|
338
|
+
? 'border-emerald-500/20 bg-emerald-500/10 text-emerald-700 dark:text-emerald-300'
|
|
339
|
+
: active
|
|
340
|
+
? 'border-blue-500/20 bg-blue-500/10 text-blue-700 dark:text-blue-300'
|
|
341
|
+
: stopped
|
|
342
|
+
? 'border-agent-border bg-agent-muted text-agent-foreground'
|
|
343
|
+
: 'border-agent-border bg-agent-muted text-agent-muted-foreground'
|
|
344
|
+
}`}
|
|
345
|
+
>
|
|
346
|
+
{statusBadgeText(node.status)}
|
|
347
|
+
</span>
|
|
348
|
+
{canInspect && chatId ? (
|
|
349
|
+
<button
|
|
350
|
+
type="button"
|
|
351
|
+
onClick={() =>
|
|
352
|
+
onInspectTask?.({
|
|
353
|
+
id: node.childId,
|
|
354
|
+
chatId,
|
|
355
|
+
recordId: node.recordId,
|
|
356
|
+
live: node.status === 'running',
|
|
357
|
+
title: node.task || t('Subagent run'),
|
|
358
|
+
})
|
|
359
|
+
}
|
|
360
|
+
className="inline-flex shrink-0 whitespace-nowrap items-center gap-0.5 rounded px-1 text-[10px] text-agent-muted-foreground hover:bg-agent-muted hover:text-agent-foreground"
|
|
361
|
+
title={t('View process')}
|
|
362
|
+
>
|
|
363
|
+
<LuExternalLink className="h-2.5 w-2.5 shrink-0" />
|
|
364
|
+
<span>{t('Process')}</span>
|
|
365
|
+
</button>
|
|
366
|
+
) : null}
|
|
367
|
+
</div>
|
|
368
|
+
</div>
|
|
369
|
+
|
|
370
|
+
{/* 任务内容 (Todo 项文本) */}
|
|
371
|
+
<div className="mt-1.5 leading-relaxed">
|
|
372
|
+
<p
|
|
373
|
+
className={`line-clamp-3 text-[11px] ${
|
|
374
|
+
done
|
|
375
|
+
? 'text-agent-foreground/90 font-medium'
|
|
376
|
+
: active
|
|
377
|
+
? 'text-agent-foreground font-semibold'
|
|
378
|
+
: 'text-agent-muted-foreground'
|
|
379
|
+
}`}
|
|
380
|
+
title={node.task}
|
|
381
|
+
>
|
|
382
|
+
{node.task || t('Subtask ({id})', { id: node.childId })}
|
|
383
|
+
</p>
|
|
384
|
+
</div>
|
|
385
|
+
|
|
386
|
+
{/* 追加指令 (Steer) */}
|
|
387
|
+
{node.steers.length > 0 ? (
|
|
388
|
+
<div className="mt-1.5 space-y-0.5 rounded border border-amber-800/20 bg-amber-100 px-1.5 py-1 text-[10px] text-amber-950">
|
|
389
|
+
{node.steers.map((s, sIdx) => (
|
|
390
|
+
<div key={sIdx} className="flex items-start gap-1">
|
|
391
|
+
<LuMessageSquare className="mt-0.5 h-2.5 w-2.5 shrink-0 text-amber-900" />
|
|
392
|
+
<span className="truncate">{s.message}</span>
|
|
393
|
+
</div>
|
|
394
|
+
))}
|
|
395
|
+
</div>
|
|
396
|
+
) : null}
|
|
397
|
+
</div>
|
|
398
|
+
|
|
399
|
+
{/* 底部产出结果 (Answer 绿色结果徽章) */}
|
|
400
|
+
{node.answer ? (
|
|
401
|
+
<div className="mt-2 rounded bg-emerald-500/10 border border-emerald-500/20 px-2 py-1 text-[11px]">
|
|
402
|
+
<div className="flex items-center gap-1 font-semibold text-emerald-800 dark:text-emerald-200 text-[10px]">
|
|
403
|
+
<LuCheck className="h-3 w-3" />
|
|
404
|
+
<span>{t('Conclusion:')}</span>
|
|
405
|
+
<span className="font-mono text-emerald-950 dark:text-emerald-50 truncate">
|
|
406
|
+
{node.answer}
|
|
407
|
+
</span>
|
|
408
|
+
</div>
|
|
409
|
+
</div>
|
|
410
|
+
) : node.error ? (
|
|
411
|
+
<div className="mt-2 rounded bg-agent-destructive/10 px-1.5 py-1 text-[10px] text-agent-destructive truncate">
|
|
412
|
+
{t('Failed: {error}', { error: node.error })}
|
|
413
|
+
</div>
|
|
414
|
+
) : null}
|
|
415
|
+
</div>
|
|
416
|
+
);
|
|
417
|
+
};
|
|
314
418
|
|
|
315
419
|
return (
|
|
316
420
|
<div
|
|
@@ -433,113 +537,35 @@ export function SessionOrchestrationFlow({
|
|
|
433
537
|
{/* 2. 向下的分叉流线 (带箭头指向每个并行分支) */}
|
|
434
538
|
<ForkConnector count={nodes.length} />
|
|
435
539
|
|
|
436
|
-
{/* 3. 并行子任务节点网格 (
|
|
437
|
-
<div className=
|
|
438
|
-
{
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
540
|
+
{/* 3. 并行子任务节点网格 (按深度分层;单层时不画层级标签) */}
|
|
541
|
+
<div className="w-full space-y-2">
|
|
542
|
+
{layers.map((layer, layerIndex) => (
|
|
543
|
+
<div key={`layer-${layerIndex}`} className="space-y-1.5">
|
|
544
|
+
{layers.length > 1 ? (
|
|
545
|
+
// 只有多层时才标层级:单层加个「深度 1」只是噪音。
|
|
546
|
+
<div className="flex items-center gap-1.5 text-[10px] font-medium text-agent-muted-foreground">
|
|
547
|
+
<span className="h-px flex-1 bg-agent-border/60" />
|
|
548
|
+
<span>
|
|
549
|
+
{t('Depth {n}', {
|
|
550
|
+
n: layer[0]?.depth ?? depthFromLineage(layer[0]?.childId ?? '0.1'),
|
|
551
|
+
})}
|
|
552
|
+
</span>
|
|
553
|
+
<span className="h-px flex-1 bg-agent-border/60" />
|
|
554
|
+
</div>
|
|
555
|
+
) : null}
|
|
445
556
|
<div
|
|
446
|
-
|
|
447
|
-
|
|
557
|
+
className={`grid w-full gap-2.5 ${
|
|
558
|
+
layer.length === 1
|
|
559
|
+
? 'grid-cols-1'
|
|
560
|
+
: layer.length === 2
|
|
561
|
+
? 'grid-cols-2'
|
|
562
|
+
: 'grid-cols-2 sm:grid-cols-3'
|
|
563
|
+
}`}
|
|
448
564
|
>
|
|
449
|
-
|
|
450
|
-
{/* 节点顶栏:Todo 式勾选/圆点 + 分支编号 + 状态 */}
|
|
451
|
-
<div className="flex items-center justify-between gap-1 border-b border-agent-border/40 pb-1.5">
|
|
452
|
-
<div className="flex items-center gap-1 min-w-0 flex-1">
|
|
453
|
-
<FlowNodeStatusIcon status={node.status} />
|
|
454
|
-
<span className="font-semibold text-agent-foreground text-[11px] truncate whitespace-nowrap">
|
|
455
|
-
{t('Branch #{index}', { index: index + 1 })}
|
|
456
|
-
</span>
|
|
457
|
-
</div>
|
|
458
|
-
|
|
459
|
-
<div className="flex items-center gap-1 shrink-0">
|
|
460
|
-
<span
|
|
461
|
-
className={`shrink-0 whitespace-nowrap rounded-full border px-1.5 py-0.2 text-[10px] font-medium leading-tight ${
|
|
462
|
-
done
|
|
463
|
-
? 'border-emerald-500/20 bg-emerald-500/10 text-emerald-700 dark:text-emerald-300'
|
|
464
|
-
: active
|
|
465
|
-
? 'border-blue-500/20 bg-blue-500/10 text-blue-700 dark:text-blue-300'
|
|
466
|
-
: stopped
|
|
467
|
-
? 'border-agent-border bg-agent-muted text-agent-foreground'
|
|
468
|
-
: 'border-agent-border bg-agent-muted text-agent-muted-foreground'
|
|
469
|
-
}`}
|
|
470
|
-
>
|
|
471
|
-
{statusBadgeText(node.status)}
|
|
472
|
-
</span>
|
|
473
|
-
{canInspect && chatId ? (
|
|
474
|
-
<button
|
|
475
|
-
type="button"
|
|
476
|
-
onClick={() =>
|
|
477
|
-
onInspectTask?.({
|
|
478
|
-
id: node.childId,
|
|
479
|
-
chatId,
|
|
480
|
-
recordId: node.recordId,
|
|
481
|
-
live: node.status === 'running',
|
|
482
|
-
title: node.task || t('Subagent run'),
|
|
483
|
-
})
|
|
484
|
-
}
|
|
485
|
-
className="inline-flex shrink-0 whitespace-nowrap items-center gap-0.5 rounded px-1 text-[10px] text-agent-muted-foreground hover:bg-agent-muted hover:text-agent-foreground"
|
|
486
|
-
title={t('View process')}
|
|
487
|
-
>
|
|
488
|
-
<LuExternalLink className="h-2.5 w-2.5 shrink-0" />
|
|
489
|
-
<span>{t('Process')}</span>
|
|
490
|
-
</button>
|
|
491
|
-
) : null}
|
|
492
|
-
</div>
|
|
493
|
-
</div>
|
|
494
|
-
|
|
495
|
-
{/* 任务内容 (Todo 项文本) */}
|
|
496
|
-
<div className="mt-1.5 leading-relaxed">
|
|
497
|
-
<p
|
|
498
|
-
className={`line-clamp-3 text-[11px] ${
|
|
499
|
-
done
|
|
500
|
-
? 'text-agent-foreground/90 font-medium'
|
|
501
|
-
: active
|
|
502
|
-
? 'text-agent-foreground font-semibold'
|
|
503
|
-
: 'text-agent-muted-foreground'
|
|
504
|
-
}`}
|
|
505
|
-
title={node.task}
|
|
506
|
-
>
|
|
507
|
-
{node.task || t('Subtask ({id})', { id: node.childId })}
|
|
508
|
-
</p>
|
|
509
|
-
</div>
|
|
510
|
-
|
|
511
|
-
{/* 追加指令 (Steer) */}
|
|
512
|
-
{node.steers.length > 0 ? (
|
|
513
|
-
<div className="mt-1.5 space-y-0.5 rounded border border-amber-800/20 bg-amber-100 px-1.5 py-1 text-[10px] text-amber-950">
|
|
514
|
-
{node.steers.map((s, sIdx) => (
|
|
515
|
-
<div key={sIdx} className="flex items-start gap-1">
|
|
516
|
-
<LuMessageSquare className="mt-0.5 h-2.5 w-2.5 shrink-0 text-amber-900" />
|
|
517
|
-
<span className="truncate">{s.message}</span>
|
|
518
|
-
</div>
|
|
519
|
-
))}
|
|
520
|
-
</div>
|
|
521
|
-
) : null}
|
|
522
|
-
</div>
|
|
523
|
-
|
|
524
|
-
{/* 底部产出结果 (Answer 绿色结果徽章) */}
|
|
525
|
-
{node.answer ? (
|
|
526
|
-
<div className="mt-2 rounded bg-emerald-500/10 border border-emerald-500/20 px-2 py-1 text-[11px]">
|
|
527
|
-
<div className="flex items-center gap-1 font-semibold text-emerald-800 dark:text-emerald-200 text-[10px]">
|
|
528
|
-
<LuCheck className="h-3 w-3" />
|
|
529
|
-
<span>{t('Conclusion:')}</span>
|
|
530
|
-
<span className="font-mono text-emerald-950 dark:text-emerald-50 truncate">
|
|
531
|
-
{node.answer}
|
|
532
|
-
</span>
|
|
533
|
-
</div>
|
|
534
|
-
</div>
|
|
535
|
-
) : node.error ? (
|
|
536
|
-
<div className="mt-2 rounded bg-agent-destructive/10 px-1.5 py-1 text-[10px] text-agent-destructive truncate">
|
|
537
|
-
{t('Failed: {error}', { error: node.error })}
|
|
538
|
-
</div>
|
|
539
|
-
) : null}
|
|
565
|
+
{layer.map((node, index) => renderNode(node, index))}
|
|
540
566
|
</div>
|
|
541
|
-
|
|
542
|
-
|
|
567
|
+
</div>
|
|
568
|
+
))}
|
|
543
569
|
</div>
|
|
544
570
|
|
|
545
571
|
{/* 4. 向下的汇聚流线 (两路分支引出,汇入 Join 节点) */}
|
|
@@ -9,6 +9,7 @@ import { Markdown } from './Markdown';
|
|
|
9
9
|
import { ToolsFlow } from './ExecutedActionsCard';
|
|
10
10
|
import type { InspectTaskInput } from './executed-actions-model';
|
|
11
11
|
import type { ChildInfo } from './OrchestrationChildrenCard';
|
|
12
|
+
import { OrchestrationChildrenCard } from './OrchestrationChildrenCard';
|
|
12
13
|
import {
|
|
13
14
|
estimateReasoningDurationMs,
|
|
14
15
|
processStatusLabel,
|
|
@@ -263,6 +264,8 @@ function ProcessBlocks({
|
|
|
263
264
|
thinkingDisplay,
|
|
264
265
|
keepFinishedThinkingOpen,
|
|
265
266
|
thinkingElapsedByIndex,
|
|
267
|
+
onContinueChild,
|
|
268
|
+
onStopChild,
|
|
266
269
|
}: {
|
|
267
270
|
blocks: TurnBlock[];
|
|
268
271
|
isStreaming: boolean;
|
|
@@ -274,6 +277,8 @@ function ProcessBlocks({
|
|
|
274
277
|
thinkingDisplay: ThinkingDisplayMode;
|
|
275
278
|
keepFinishedThinkingOpen: boolean;
|
|
276
279
|
thinkingElapsedByIndex?: Array<number | undefined>;
|
|
280
|
+
onContinueChild?: (childId: string, recordId: string) => void;
|
|
281
|
+
onStopChild?: (childId: string) => void;
|
|
277
282
|
}) {
|
|
278
283
|
return (
|
|
279
284
|
<div className="space-y-1.5">
|
|
@@ -289,6 +294,14 @@ function ProcessBlocks({
|
|
|
289
294
|
keepFinishedThinkingOpen={keepFinishedThinkingOpen}
|
|
290
295
|
thinkingElapsedByIndex={thinkingElapsedByIndex}
|
|
291
296
|
/>
|
|
297
|
+
{/* R7 子代理看板:挂在过程块之后、正文之前。≥2 个子代理才渲染,
|
|
298
|
+
单次委派已在工具行里有同一条进度。 */}
|
|
299
|
+
<OrchestrationChildrenCard
|
|
300
|
+
children={orchestrationChildren ?? []}
|
|
301
|
+
agents={agents}
|
|
302
|
+
onContinue={onContinueChild}
|
|
303
|
+
onStop={onStopChild}
|
|
304
|
+
/>
|
|
292
305
|
</div>
|
|
293
306
|
);
|
|
294
307
|
}
|
|
@@ -301,6 +314,8 @@ export function TurnProcessGroup({
|
|
|
301
314
|
chatId,
|
|
302
315
|
onInspectTask,
|
|
303
316
|
orchestrationChildren,
|
|
317
|
+
onContinueChild,
|
|
318
|
+
onStopChild,
|
|
304
319
|
emptyFallback,
|
|
305
320
|
streamingHint,
|
|
306
321
|
startedAtMs,
|
|
@@ -318,6 +333,10 @@ export function TurnProcessGroup({
|
|
|
318
333
|
onInspectTask?: (task: InspectTaskInput) => void;
|
|
319
334
|
/** 本回合子代理:委派行据此跳转到子代理过程。 */
|
|
320
335
|
orchestrationChildren?: ChildInfo[];
|
|
336
|
+
/** 「继续」一个已可续的子代理(R7)。 */
|
|
337
|
+
onContinueChild?: (childId: string, recordId: string) => void;
|
|
338
|
+
/** 「停止」一个正在跑的子代理(R7)。 */
|
|
339
|
+
onStopChild?: (childId: string) => void;
|
|
321
340
|
emptyFallback: ReactNode;
|
|
322
341
|
/** Shown while streaming after a tools row and before the summary lands. */
|
|
323
342
|
streamingHint?: ReactNode;
|
|
@@ -451,6 +470,8 @@ export function TurnProcessGroup({
|
|
|
451
470
|
thinkingDisplay={thinkingDisplay}
|
|
452
471
|
keepFinishedThinkingOpen={!collapseWhenFinished}
|
|
453
472
|
thinkingElapsedByIndex={thinkingElapsedByIndex}
|
|
473
|
+
onContinueChild={onContinueChild}
|
|
474
|
+
onStopChild={onStopChild}
|
|
454
475
|
/>
|
|
455
476
|
)}
|
|
456
477
|
{showStreamingHint ? streamingHint : null}
|
|
@@ -24,6 +24,32 @@ export interface ChildInfo {
|
|
|
24
24
|
status: string;
|
|
25
25
|
/** 子代理自己的 durable record;右侧过程面板据此回看它的推理与工具。 */
|
|
26
26
|
recordId?: string;
|
|
27
|
+
/**
|
|
28
|
+
* R5: this child's bill. Absent while running, and absent for a child on
|
|
29
|
+
* an engine that reports no tokens at all (external ACP) — the two mean
|
|
30
|
+
* different things, so the row shows "not counted" for the settled one
|
|
31
|
+
* instead of `0 tok`.
|
|
32
|
+
*/
|
|
33
|
+
usage?: { promptTokens: number; completionTokens: number; totalTokens: number };
|
|
34
|
+
/** Wall-clock the child ran, from spawn to terminal event. */
|
|
35
|
+
elapsedMs?: number;
|
|
36
|
+
/** R2: the fork re-read the parent history on a different model route. */
|
|
37
|
+
cacheNote?: 'cold';
|
|
38
|
+
parentModel?: string;
|
|
39
|
+
childModel?: string;
|
|
40
|
+
/**
|
|
41
|
+
* R7-3: this child stopped to ask the user something.
|
|
42
|
+
*
|
|
43
|
+
* Set by `child_needs_input`; cleared when the parent hands over the answer
|
|
44
|
+
* (the delegation result that follows), so the row stops saying 「等待你的
|
|
45
|
+
* 回答」 once the user has answered.
|
|
46
|
+
*/
|
|
47
|
+
needsInput?: { intro: string };
|
|
48
|
+
/**
|
|
49
|
+
* R7-3: this child asked for input and the parent has since passed the
|
|
50
|
+
* answer on. It is not running any more, so the row reads 「已结束 · 已转交」.
|
|
51
|
+
*/
|
|
52
|
+
handedOver?: boolean;
|
|
27
53
|
}
|
|
28
54
|
|
|
29
55
|
/**
|
|
@@ -105,6 +131,8 @@ const STATUS_MAP: Record<string, OrchestrationTaskStatus> = {
|
|
|
105
131
|
cancelled: 'skipped',
|
|
106
132
|
// Interrupted = paused-but-resumable; renders as pending (not terminal).
|
|
107
133
|
interrupted: 'pending',
|
|
134
|
+
// Blocked on the user: still open, and not something 「继续」 can resolve.
|
|
135
|
+
needs_input: 'ask_user_paused',
|
|
108
136
|
};
|
|
109
137
|
|
|
110
138
|
/**
|
|
@@ -150,42 +178,184 @@ export function shouldShowOrchestrationBoard(children: ChildInfo[]): boolean {
|
|
|
150
178
|
export function foldOrchestrationChildEvents(
|
|
151
179
|
events: ReadonlyArray<Record<string, unknown>>,
|
|
152
180
|
): ChildInfo[] {
|
|
153
|
-
|
|
181
|
+
// Spawn timestamps by childId: the elapsed time is only known when a
|
|
182
|
+
// terminal event arrives, and nothing else records when the child started.
|
|
183
|
+
const spawnedAtMs = new Map<string, number>();
|
|
184
|
+
let list: ChildInfo[] = [];
|
|
154
185
|
for (const ev of events) {
|
|
155
186
|
if (!ev || typeof ev !== 'object') continue;
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
187
|
+
list = applyChildEvent(list, ev, spawnedAtMs);
|
|
188
|
+
}
|
|
189
|
+
return list;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Fold one `agent.child` event into the live child list.
|
|
194
|
+
*
|
|
195
|
+
* The live stream and a replay must read an event the same way, or the panel
|
|
196
|
+
* shows one thing while streaming and another after a reload. `spawnedAtMs`
|
|
197
|
+
* carries the spawn clock across calls; omit it for a one-shot fold and the
|
|
198
|
+
* elapsed column simply stays empty.
|
|
199
|
+
*
|
|
200
|
+
* @param list The children accumulated so far.
|
|
201
|
+
* @param ev One `agent.child` lifecycle payload.
|
|
202
|
+
* @param spawnedAtMs Spawn timestamps by childId (mutated).
|
|
203
|
+
* @returns The list after this event.
|
|
204
|
+
*/
|
|
205
|
+
export function applyChildEvent(
|
|
206
|
+
list: ChildInfo[],
|
|
207
|
+
ev: Record<string, unknown>,
|
|
208
|
+
spawnedAtMs: Map<string, number> = new Map(),
|
|
209
|
+
): ChildInfo[] {
|
|
210
|
+
const kind = typeof ev.kind === 'string' ? ev.kind : '';
|
|
211
|
+
const childId = typeof ev.childId === 'string' ? ev.childId : '';
|
|
212
|
+
if (!kind || !childId) return list;
|
|
213
|
+
const idx = list.findIndex((c) => c.childId === childId);
|
|
214
|
+
if (kind === 'child_spawned') {
|
|
215
|
+
if (idx >= 0) return list;
|
|
216
|
+
spawnedAtMs.set(childId, typeof ev.atMs === 'number' ? ev.atMs : Date.now());
|
|
217
|
+
return [
|
|
218
|
+
...list,
|
|
219
|
+
{
|
|
163
220
|
childId,
|
|
164
221
|
task: typeof ev.task === 'string' ? ev.task : undefined,
|
|
165
222
|
depth: typeof ev.depth === 'number' ? ev.depth : undefined,
|
|
166
|
-
|
|
223
|
+
...(typeof ev.profile === 'string' ? { profile: ev.profile } : {}),
|
|
167
224
|
// 子代理自己的 durable record:委派行靠它跳转右侧过程栏。
|
|
168
|
-
|
|
225
|
+
...(typeof ev.recordId === 'string' ? { recordId: ev.recordId } : {}),
|
|
226
|
+
// R2: the fork re-read the parent prefix on another model. A hint,
|
|
227
|
+
// not an error — the spawn still succeeded.
|
|
228
|
+
...(ev.cacheNote === 'cold' ? { cacheNote: 'cold' as const } : {}),
|
|
229
|
+
...(typeof ev.parentModel === 'string' ? { parentModel: ev.parentModel } : {}),
|
|
230
|
+
...(typeof ev.childModel === 'string' ? { childModel: ev.childModel } : {}),
|
|
169
231
|
status: 'running',
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
}
|
|
173
|
-
if (idx < 0) continue;
|
|
174
|
-
const status =
|
|
175
|
-
kind === 'child_completed'
|
|
176
|
-
? 'completed'
|
|
177
|
-
: kind === 'child_failed'
|
|
178
|
-
? 'failed'
|
|
179
|
-
: kind === 'child_cancelled'
|
|
180
|
-
? 'cancelled'
|
|
181
|
-
: kind === 'child_interrupted'
|
|
182
|
-
? 'interrupted'
|
|
183
|
-
: kind === 'child_resumed'
|
|
184
|
-
? 'running'
|
|
185
|
-
: list[idx].status;
|
|
186
|
-
list[idx] = { ...list[idx], status };
|
|
232
|
+
},
|
|
233
|
+
];
|
|
187
234
|
}
|
|
188
|
-
return list;
|
|
235
|
+
if (idx < 0) return list;
|
|
236
|
+
if (kind === 'child_needs_input') {
|
|
237
|
+
// The child stopped and is waiting on the user. It is not running and it
|
|
238
|
+
// is not settled — marking it `interrupted` would offer 「继续」, which is
|
|
239
|
+
// wrong: what it needs is an answer.
|
|
240
|
+
const next = [...list];
|
|
241
|
+
next[idx] = {
|
|
242
|
+
...list[idx],
|
|
243
|
+
status: 'needs_input',
|
|
244
|
+
needsInput: { intro: typeof ev.intro === 'string' ? ev.intro : '' },
|
|
245
|
+
handedOver: undefined,
|
|
246
|
+
};
|
|
247
|
+
return next;
|
|
248
|
+
}
|
|
249
|
+
const status =
|
|
250
|
+
kind === 'child_completed'
|
|
251
|
+
? 'completed'
|
|
252
|
+
: kind === 'child_failed'
|
|
253
|
+
? 'failed'
|
|
254
|
+
: kind === 'child_cancelled'
|
|
255
|
+
? 'cancelled'
|
|
256
|
+
: kind === 'child_interrupted'
|
|
257
|
+
? 'interrupted'
|
|
258
|
+
: kind === 'child_resumed'
|
|
259
|
+
? 'running'
|
|
260
|
+
: list[idx].status;
|
|
261
|
+
const next = [...list];
|
|
262
|
+
next[idx] = {
|
|
263
|
+
...list[idx],
|
|
264
|
+
status,
|
|
265
|
+
...(isChildUsage(ev.usage) ? { usage: ev.usage } : {}),
|
|
266
|
+
...elapsedOf(spawnedAtMs.get(childId), ev),
|
|
267
|
+
};
|
|
268
|
+
return next;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** Statuses `agent_send` can continue. `closed` is deliberately absent. */
|
|
272
|
+
const RESUMABLE_CHILD_STATUSES = new Set([
|
|
273
|
+
'completed',
|
|
274
|
+
'interrupted',
|
|
275
|
+
'cancelled',
|
|
276
|
+
'budget_exhausted',
|
|
277
|
+
]);
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* The child a question came from, for the ask-user card's source line.
|
|
281
|
+
*
|
|
282
|
+
* Returns the first row waiting on the user: the host shows one card at a
|
|
283
|
+
* time, so "which child asked" has exactly one answer while a card is open.
|
|
284
|
+
*
|
|
285
|
+
* @param children This turn's children.
|
|
286
|
+
* @returns The child that asked, or null when nothing is waiting.
|
|
287
|
+
*/
|
|
288
|
+
export function childAwaitingInput(
|
|
289
|
+
children: readonly ChildInfo[],
|
|
290
|
+
): ChildInfo | null {
|
|
291
|
+
return children.find((child) => Boolean(child.needsInput)) ?? null;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Whether 「继续」 applies to this child.
|
|
296
|
+
*
|
|
297
|
+
* Derived from the status rather than stored: a resumed child goes back to
|
|
298
|
+
* `running`, and a stored flag would have to be cleared on every resume.
|
|
299
|
+
* `needs_input` is deliberately absent — that child is waiting for an
|
|
300
|
+
* answer, and resuming it would just ask the same question again.
|
|
301
|
+
*
|
|
302
|
+
* @param child The child row.
|
|
303
|
+
* @returns True when `agent_send` can continue it.
|
|
304
|
+
*/
|
|
305
|
+
export function isChildResumable(child: ChildInfo): boolean {
|
|
306
|
+
if (child.needsInput) return false;
|
|
307
|
+
return RESUMABLE_CHILD_STATUSES.has(child.status);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function isChildUsage(
|
|
311
|
+
value: unknown,
|
|
312
|
+
): value is { promptTokens: number; completionTokens: number; totalTokens: number } {
|
|
313
|
+
if (!value || typeof value !== 'object') return false;
|
|
314
|
+
const record = value as Record<string, unknown>;
|
|
315
|
+
return (
|
|
316
|
+
typeof record.promptTokens === 'number'
|
|
317
|
+
&& typeof record.completionTokens === 'number'
|
|
318
|
+
&& typeof record.totalTokens === 'number'
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* How long the child ran, when both ends carry a wall clock.
|
|
324
|
+
*
|
|
325
|
+
* Both are required: replaying a turn persisted before `atMs` existed would
|
|
326
|
+
* otherwise report a zero-length run, which reads as "it did nothing".
|
|
327
|
+
*/
|
|
328
|
+
function elapsedOf(
|
|
329
|
+
spawnedAtMs: number | undefined,
|
|
330
|
+
ev: Record<string, unknown>,
|
|
331
|
+
): { elapsedMs?: number } {
|
|
332
|
+
if (spawnedAtMs === undefined) return {};
|
|
333
|
+
const atMs = typeof ev.atMs === 'number' ? ev.atMs : undefined;
|
|
334
|
+
if (atMs === undefined) return {};
|
|
335
|
+
const elapsed = atMs - spawnedAtMs;
|
|
336
|
+
return elapsed >= 0 ? { elapsedMs: elapsed } : {};
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Mark a child's question as handed over once the parent has re-delegated.
|
|
341
|
+
*
|
|
342
|
+
* The old row keeps existing (it is a real delegation that ran), but it must
|
|
343
|
+
* stop saying 「等待你的回答」 — the user answered, and the work continued in a
|
|
344
|
+
* new child. Without this the panel shows two rows both waiting on the user.
|
|
345
|
+
*
|
|
346
|
+
* @param list The children accumulated so far.
|
|
347
|
+
* @param childId The child that asked.
|
|
348
|
+
* @returns The list with that child marked handed over.
|
|
349
|
+
*/
|
|
350
|
+
export function markChildHandedOver(
|
|
351
|
+
list: ChildInfo[],
|
|
352
|
+
childId: string,
|
|
353
|
+
): ChildInfo[] {
|
|
354
|
+
const idx = list.findIndex((child) => child.childId === childId);
|
|
355
|
+
if (idx < 0 || !list[idx].needsInput) return list;
|
|
356
|
+
const next = [...list];
|
|
357
|
+
next[idx] = { ...list[idx], status: 'completed', handedOver: true, needsInput: undefined };
|
|
358
|
+
return next;
|
|
189
359
|
}
|
|
190
360
|
|
|
191
361
|
/**
|
|
@@ -222,3 +392,79 @@ export function extractPersistedOrchestrationChildren(
|
|
|
222
392
|
}
|
|
223
393
|
return seeded;
|
|
224
394
|
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* 把注册表里的子代理并进重启后的面板(R7-2)。
|
|
398
|
+
*
|
|
399
|
+
* 落库的 `orchestrationChildEvents` 只覆盖「回合跑到了收尾」的情形:进程被杀
|
|
400
|
+
* 时最后一条 assistant 消息可能根本没落库,那些子代理只存在于注册表里。没有
|
|
401
|
+
* 这一步,用户重开后看到的比实际派出去的少。
|
|
402
|
+
*
|
|
403
|
+
* 已经出现在某条消息里的子代理不重复添加(事件流比注册行更新)。剩下的挂到
|
|
404
|
+
* 最后一条 assistant 消息上——那是这个会话最近一次派发的归处;会话里一条
|
|
405
|
+
* assistant 消息都没有时(首个回合就被杀)返回空,因为没有面板可挂。
|
|
406
|
+
*
|
|
407
|
+
* @param persisted 落库事件水合出的「消息 id → 子代理」。
|
|
408
|
+
* @param orderedMessageIds 会话消息按时间顺序的 id。
|
|
409
|
+
* @param sessionChildren 注册表返回的本会话子代理。
|
|
410
|
+
* @returns 合并后的映射;未变化时返回原对象。
|
|
411
|
+
*/
|
|
412
|
+
export function mergeSessionChildren(
|
|
413
|
+
persisted: Record<string, ChildInfo[]>,
|
|
414
|
+
orderedMessageIds: readonly string[],
|
|
415
|
+
sessionChildren: ReadonlyArray<Record<string, unknown>>,
|
|
416
|
+
): Record<string, ChildInfo[]> {
|
|
417
|
+
if (sessionChildren.length === 0) return persisted;
|
|
418
|
+
const seen = new Set<string>();
|
|
419
|
+
for (const list of Object.values(persisted)) {
|
|
420
|
+
for (const child of list) seen.add(child.childId);
|
|
421
|
+
}
|
|
422
|
+
const unseen = sessionChildren
|
|
423
|
+
.map(toChildInfo)
|
|
424
|
+
.filter((child): child is ChildInfo => child !== null && !seen.has(child.childId));
|
|
425
|
+
if (unseen.length === 0) return persisted;
|
|
426
|
+
const target = [...orderedMessageIds].reverse().find((id) => id in persisted)
|
|
427
|
+
?? undefined;
|
|
428
|
+
if (target === undefined) return persisted;
|
|
429
|
+
return {
|
|
430
|
+
...persisted,
|
|
431
|
+
[target]: [...persisted[target], ...unseen],
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* 注册行 → 面板行。
|
|
437
|
+
*
|
|
438
|
+
* 注册行没有任务文本(那是子代理自己的记录),也没有耗时;状态已经是合成
|
|
439
|
+
* 结果(`interrupted` 表示行未终结且本进程没有活句柄),正好是面板要的。
|
|
440
|
+
*
|
|
441
|
+
* @param raw `agent.children.list` 的一行。
|
|
442
|
+
* @returns 面板行;缺 childId 时返回 null。
|
|
443
|
+
*/
|
|
444
|
+
function toChildInfo(raw: Record<string, unknown>): ChildInfo | null {
|
|
445
|
+
const childId = typeof raw.childId === 'string' ? raw.childId : '';
|
|
446
|
+
if (!childId) return null;
|
|
447
|
+
const status = typeof raw.status === 'string' ? raw.status : 'interrupted';
|
|
448
|
+
const prompt = typeof raw.promptTokens === 'number' ? raw.promptTokens : null;
|
|
449
|
+
const completion =
|
|
450
|
+
typeof raw.completionTokens === 'number' ? raw.completionTokens : null;
|
|
451
|
+
return {
|
|
452
|
+
childId,
|
|
453
|
+
status,
|
|
454
|
+
...(typeof raw.depth === 'number' ? { depth: raw.depth } : {}),
|
|
455
|
+
...(typeof raw.profile === 'string' ? { profile: raw.profile } : {}),
|
|
456
|
+
...(typeof raw.recordId === 'string' ? { recordId: raw.recordId } : {}),
|
|
457
|
+
// `usageKnown: false` 是外部引擎:它跑过,但没人计过 token。
|
|
458
|
+
...(raw.usageKnown === false
|
|
459
|
+
? {}
|
|
460
|
+
: prompt !== null || completion !== null
|
|
461
|
+
? {
|
|
462
|
+
usage: {
|
|
463
|
+
promptTokens: prompt ?? 0,
|
|
464
|
+
completionTokens: completion ?? 0,
|
|
465
|
+
totalTokens: (prompt ?? 0) + (completion ?? 0),
|
|
466
|
+
},
|
|
467
|
+
}
|
|
468
|
+
: {}),
|
|
469
|
+
};
|
|
470
|
+
}
|