@steerable/agent-shell-web 0.11.1 → 0.12.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/AssistantMessage.tsx +10 -0
- package/src/components/chat/LocalChatPanel.tsx +9 -0
- package/src/components/chat/MessageList.tsx +18 -0
- package/src/components/chat/TurnProcessGroup.tsx +11 -0
- package/src/components/chat/TurnUsageLine.tsx +45 -0
- package/src/components/chat/turn-usage.ts +165 -0
- package/src/pages/AgentPage.tsx +62 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@steerable/agent-shell-web",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Steerable framework — product-neutral renderer SPA source for @steerable/agent-shell (React + Vite). Consumed by product web entry points via the `@/` alias (anchored at this package's src/) and the createProductViteConfig factory. Published as a source package (src/), plus the compiled vite.base config factory (vite.base.js + .d.ts) for Node-side config loading.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -45,8 +45,8 @@
|
|
|
45
45
|
"react-router-dom": "^6.30.0",
|
|
46
46
|
"rehype-raw": "^7.0.0",
|
|
47
47
|
"remark-gfm": "^4.0.1",
|
|
48
|
-
"@steerable/agent-protocol": "0.
|
|
49
|
-
"@steerable/agent-ui": "0.
|
|
48
|
+
"@steerable/agent-protocol": "0.12.0",
|
|
49
|
+
"@steerable/agent-ui": "0.12.0"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@tailwindcss/vite": "^4.3.0",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"typescript": "^5.8.3",
|
|
60
60
|
"vite": "^5.4.21",
|
|
61
61
|
"vitest": "^3.2.4",
|
|
62
|
-
"@steerable/pack-sdk": "0.
|
|
62
|
+
"@steerable/pack-sdk": "0.12.0"
|
|
63
63
|
},
|
|
64
64
|
"scripts": {
|
|
65
65
|
"gen": "echo agent-shell-web",
|
|
@@ -29,6 +29,7 @@ import type { TurnBlock } from './turn-timeline';
|
|
|
29
29
|
import { TurnProcessGroup } from './TurnProcessGroup';
|
|
30
30
|
import { TurnFilesCard } from './TurnFilesCard';
|
|
31
31
|
import type { TurnFile } from './turn-files';
|
|
32
|
+
import type { TurnUsage } from './turn-usage';
|
|
32
33
|
import { stripNextStepsTags } from './next-steps-tags';
|
|
33
34
|
import {
|
|
34
35
|
formatTokenSpeed,
|
|
@@ -151,6 +152,13 @@ interface AssistantMessageProps {
|
|
|
151
152
|
* 气泡之下,点击用系统默认应用打开。空/undefined = 不渲染。
|
|
152
153
|
*/
|
|
153
154
|
turnFiles?: TurnFile[];
|
|
155
|
+
/**
|
|
156
|
+
* R6:本回合的账单(local-backend 把 sidecar 的 `stream.done.usage` 经
|
|
157
|
+
* `turn_usage` SSE + messageMetadata.usage 落库)。面板据此显示「这一轮
|
|
158
|
+
* 花了多少、其中子代理占多少」;父级自己的花费不在任何子代理行里,
|
|
159
|
+
* 所以这份数据没有替代品。
|
|
160
|
+
*/
|
|
161
|
+
turnUsage?: TurnUsage;
|
|
154
162
|
/**
|
|
155
163
|
* 分享当前对话(截图复制到剪贴板)。只传给最近一条助手消息,画在时间戳
|
|
156
164
|
* 行上,跟复制 / 重新生成同一排。
|
|
@@ -458,6 +466,7 @@ export function AssistantMessage({
|
|
|
458
466
|
durationMs,
|
|
459
467
|
llmSpeed,
|
|
460
468
|
turnFiles,
|
|
469
|
+
turnUsage,
|
|
461
470
|
onShare,
|
|
462
471
|
onInspectTask,
|
|
463
472
|
onContinueChild,
|
|
@@ -555,6 +564,7 @@ export function AssistantMessage({
|
|
|
555
564
|
chatId={chatId}
|
|
556
565
|
onInspectTask={onInspectTask}
|
|
557
566
|
orchestrationChildren={orchestrationChildren}
|
|
567
|
+
turnUsage={turnUsage}
|
|
558
568
|
onContinueChild={onContinueChild}
|
|
559
569
|
onStopChild={onStopChild}
|
|
560
570
|
emptyFallback={
|
|
@@ -18,6 +18,7 @@ import type { InspectTaskInput } from './executed-actions-model';
|
|
|
18
18
|
import type { ChildInfo } from './OrchestrationChildrenCard';
|
|
19
19
|
import type { TurnBlock } from './turn-timeline';
|
|
20
20
|
import type { TurnFile } from './turn-files';
|
|
21
|
+
import type { TurnUsage } from './turn-usage';
|
|
21
22
|
import type { RetryState } from './RetryStatus';
|
|
22
23
|
import type { TurnFailureCardProps } from './TurnFailureCard';
|
|
23
24
|
import type { LlmSpeedSnapshot } from './process-status';
|
|
@@ -117,6 +118,10 @@ export interface LocalChatPanelProps {
|
|
|
117
118
|
turnFilesByMessageId?: Record<string, TurnFile[]>;
|
|
118
119
|
/** 当轮产物文件(流尾声到达、尚未归档到落库 id 的尾部消息用)。 */
|
|
119
120
|
currentTurnFiles?: TurnFile[];
|
|
121
|
+
/** R6 回合账单(按落库消息 id 键控),转发给 MessageList。 */
|
|
122
|
+
turnUsageByMessageId?: Record<string, TurnUsage>;
|
|
123
|
+
/** 当轮账单:与 currentTurnFiles 同款尾部回退。 */
|
|
124
|
+
currentTurnUsage?: TurnUsage;
|
|
120
125
|
/** P3.1: in-flight child agents + per-message reconciliation map. */
|
|
121
126
|
currentTurnChildren?: ChildInfo[];
|
|
122
127
|
orchestrationChildrenByMessageId?: Record<string, ChildInfo[]>;
|
|
@@ -206,6 +211,8 @@ export function LocalChatPanel({
|
|
|
206
211
|
llmSpeedByMessageId,
|
|
207
212
|
turnFilesByMessageId,
|
|
208
213
|
currentTurnFiles,
|
|
214
|
+
turnUsageByMessageId,
|
|
215
|
+
currentTurnUsage,
|
|
209
216
|
currentTurnChildren,
|
|
210
217
|
orchestrationChildrenByMessageId,
|
|
211
218
|
currentRound,
|
|
@@ -526,6 +533,8 @@ export function LocalChatPanel({
|
|
|
526
533
|
llmSpeedByMessageId={llmSpeedByMessageId}
|
|
527
534
|
turnFilesByMessageId={turnFilesByMessageId}
|
|
528
535
|
currentTurnFiles={currentTurnFiles}
|
|
536
|
+
turnUsageByMessageId={turnUsageByMessageId}
|
|
537
|
+
currentTurnUsage={currentTurnUsage}
|
|
529
538
|
currentTurnChildren={currentTurnChildren}
|
|
530
539
|
orchestrationChildrenByMessageId={orchestrationChildrenByMessageId}
|
|
531
540
|
currentRound={currentRound}
|
|
@@ -22,6 +22,7 @@ import type { ChildInfo } from './OrchestrationChildrenCard';
|
|
|
22
22
|
import type { ChatMode } from './ChatInput';
|
|
23
23
|
import type { TurnBlock } from './turn-timeline';
|
|
24
24
|
import { followedPreviewPath, type TurnFile } from './turn-files';
|
|
25
|
+
import type { TurnUsage } from './turn-usage';
|
|
25
26
|
import { publishConversationPreview } from '@/layouts/conversation-preview';
|
|
26
27
|
import { inferDurationMs, readPersistedDurationMs } from './elapsed';
|
|
27
28
|
import type { LlmSpeedSnapshot } from './process-status';
|
|
@@ -138,6 +139,13 @@ interface MessageListProps {
|
|
|
138
139
|
* 与 currentTurnActions 同款尾部回退。
|
|
139
140
|
*/
|
|
140
141
|
currentTurnFiles?: TurnFile[];
|
|
142
|
+
/**
|
|
143
|
+
* R6: 每回合的账单,按落库消息 id 键控(与水合/归档同 turnFiles 一路)。
|
|
144
|
+
* 这是唯一同时含父级与子代理的两段数字的地方——面板行内只有子代理那一份。
|
|
145
|
+
*/
|
|
146
|
+
turnUsageByMessageId?: Record<string, TurnUsage>;
|
|
147
|
+
/** 当轮账单:与 currentTurnFiles 同款尾部回退。 */
|
|
148
|
+
currentTurnUsage?: TurnUsage;
|
|
141
149
|
/**
|
|
142
150
|
* Tool calls accumulated for the in-flight assistant message that the
|
|
143
151
|
* backend hasn't assigned a DB id to yet. Rendered under the latest
|
|
@@ -219,6 +227,8 @@ export function MessageList({
|
|
|
219
227
|
llmSpeedByMessageId,
|
|
220
228
|
turnFilesByMessageId,
|
|
221
229
|
currentTurnFiles,
|
|
230
|
+
turnUsageByMessageId,
|
|
231
|
+
currentTurnUsage,
|
|
222
232
|
currentTurnChildren,
|
|
223
233
|
orchestrationChildrenByMessageId,
|
|
224
234
|
currentRound,
|
|
@@ -513,6 +523,13 @@ export function MessageList({
|
|
|
513
523
|
const turnFiles = persistedTurnFiles
|
|
514
524
|
?? (isTurnFilesTail ? currentTurnFiles : undefined);
|
|
515
525
|
|
|
526
|
+
// R6: 同 turnFiles 的尾部回退。区别是它没有「非空才算」
|
|
527
|
+
// 的条件——一份全零的账单仍然说明这一轮跑过。
|
|
528
|
+
const persistedTurnUsage = turnUsageByMessageId?.[message.id];
|
|
529
|
+
const isTurnUsageTail = isLast && currentTurnUsage !== undefined;
|
|
530
|
+
const turnUsage =
|
|
531
|
+
persistedTurnUsage ?? (isTurnUsageTail ? currentTurnUsage : undefined);
|
|
532
|
+
|
|
516
533
|
const metadataJson =
|
|
517
534
|
typeof message.messageMetadata === 'string'
|
|
518
535
|
? message.messageMetadata
|
|
@@ -567,6 +584,7 @@ export function MessageList({
|
|
|
567
584
|
durationMs={durationMs}
|
|
568
585
|
llmSpeed={llmSpeed}
|
|
569
586
|
turnFiles={turnFiles}
|
|
587
|
+
turnUsage={turnUsage}
|
|
570
588
|
onShare={message.id === lastAssistantId ? onShare : undefined}
|
|
571
589
|
onInspectTask={onInspectTask}
|
|
572
590
|
onContinueChild={onContinueChild}
|
|
@@ -10,6 +10,8 @@ import { ToolsFlow } from './ExecutedActionsCard';
|
|
|
10
10
|
import type { InspectTaskInput } from './executed-actions-model';
|
|
11
11
|
import type { ChildInfo } from './OrchestrationChildrenCard';
|
|
12
12
|
import { OrchestrationChildrenCard } from './OrchestrationChildrenCard';
|
|
13
|
+
import { TurnUsageLine } from './TurnUsageLine';
|
|
14
|
+
import type { TurnUsage } from './turn-usage';
|
|
13
15
|
import {
|
|
14
16
|
estimateReasoningDurationMs,
|
|
15
17
|
processStatusLabel,
|
|
@@ -266,6 +268,7 @@ function ProcessBlocks({
|
|
|
266
268
|
thinkingElapsedByIndex,
|
|
267
269
|
onContinueChild,
|
|
268
270
|
onStopChild,
|
|
271
|
+
turnUsage,
|
|
269
272
|
}: {
|
|
270
273
|
blocks: TurnBlock[];
|
|
271
274
|
isStreaming: boolean;
|
|
@@ -279,6 +282,8 @@ function ProcessBlocks({
|
|
|
279
282
|
thinkingElapsedByIndex?: Array<number | undefined>;
|
|
280
283
|
onContinueChild?: (childId: string, recordId: string) => void;
|
|
281
284
|
onStopChild?: (childId: string) => void;
|
|
285
|
+
/** R6:本回合账单,画在子代理看板下方。 */
|
|
286
|
+
turnUsage?: TurnUsage;
|
|
282
287
|
}) {
|
|
283
288
|
return (
|
|
284
289
|
<div className="space-y-1.5">
|
|
@@ -302,6 +307,8 @@ function ProcessBlocks({
|
|
|
302
307
|
onContinue={onContinueChild}
|
|
303
308
|
onStop={onStopChild}
|
|
304
309
|
/>
|
|
310
|
+
{/* R6 回合账单:紧随看板,读的是「父级 + 子代理」的整份数字。 */}
|
|
311
|
+
<TurnUsageLine usage={turnUsage} />
|
|
305
312
|
</div>
|
|
306
313
|
);
|
|
307
314
|
}
|
|
@@ -316,6 +323,7 @@ export function TurnProcessGroup({
|
|
|
316
323
|
orchestrationChildren,
|
|
317
324
|
onContinueChild,
|
|
318
325
|
onStopChild,
|
|
326
|
+
turnUsage,
|
|
319
327
|
emptyFallback,
|
|
320
328
|
streamingHint,
|
|
321
329
|
startedAtMs,
|
|
@@ -337,6 +345,8 @@ export function TurnProcessGroup({
|
|
|
337
345
|
onContinueChild?: (childId: string, recordId: string) => void;
|
|
338
346
|
/** 「停止」一个正在跑的子代理(R7)。 */
|
|
339
347
|
onStopChild?: (childId: string) => void;
|
|
348
|
+
/** R6:本回合账单(父级 + 子代理合计),画在子代理看板下方。 */
|
|
349
|
+
turnUsage?: TurnUsage;
|
|
340
350
|
emptyFallback: ReactNode;
|
|
341
351
|
/** Shown while streaming after a tools row and before the summary lands. */
|
|
342
352
|
streamingHint?: ReactNode;
|
|
@@ -472,6 +482,7 @@ export function TurnProcessGroup({
|
|
|
472
482
|
thinkingElapsedByIndex={thinkingElapsedByIndex}
|
|
473
483
|
onContinueChild={onContinueChild}
|
|
474
484
|
onStopChild={onStopChild}
|
|
485
|
+
turnUsage={turnUsage}
|
|
475
486
|
/>
|
|
476
487
|
)}
|
|
477
488
|
{showStreamingHint ? streamingHint : null}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { t } from '@/i18n';
|
|
2
|
+
import { formatTurnTokens, summarizeTurnUsage, type TurnUsage } from './turn-usage';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* TurnUsageLine — 本回合的账单,一行。**只在这一轮派过子代理时渲染。**
|
|
6
|
+
*
|
|
7
|
+
* 为什么不无条件显示:需求要的是「看得见子代理花了多少」,而一个没派活
|
|
8
|
+
* 的回合把 token 数摆到每条消息下方只是噪音。判据是「这一轮有没有子代理
|
|
9
|
+
* 的花费」,包含「有子代理但报告不出 token」那种——那正是最需要说明的情形。
|
|
10
|
+
*
|
|
11
|
+
* 为什么不显示成本:`stream.done.usage.costUsd` 只估算父级那一份(子代理
|
|
12
|
+
* 按各自 profile 的模型跑,单价不同),拿它当整轮成本会偏低。宁可只报
|
|
13
|
+
* token,也不报一个错的钱。
|
|
14
|
+
*/
|
|
15
|
+
export function TurnUsageLine({ usage }: { usage?: TurnUsage }) {
|
|
16
|
+
if (!usage) return null;
|
|
17
|
+
const breakdown = summarizeTurnUsage(usage);
|
|
18
|
+
if (!breakdown.hasChildren) return null;
|
|
19
|
+
if (breakdown.totalTokens <= 0) return null;
|
|
20
|
+
return (
|
|
21
|
+
<p
|
|
22
|
+
className="flex flex-wrap items-center gap-x-1.5 text-[10px] tabular-nums text-[var(--agent-muted-foreground,#6b7280)]"
|
|
23
|
+
data-testid="turn-usage"
|
|
24
|
+
>
|
|
25
|
+
<span>
|
|
26
|
+
{t('This turn {tokens} tokens', { tokens: formatTurnTokens(breakdown.totalTokens) })}
|
|
27
|
+
</span>
|
|
28
|
+
<span data-testid="turn-usage-children">
|
|
29
|
+
{t('· sub-agents {tokens} ({percent}%)', {
|
|
30
|
+
tokens: formatTurnTokens(breakdown.childTokens),
|
|
31
|
+
percent: breakdown.childSharePercent,
|
|
32
|
+
})}
|
|
33
|
+
</span>
|
|
34
|
+
{breakdown.partial ? (
|
|
35
|
+
// 有子代理的引擎不报 token,合计只是下限。不加这句,偏小的数字会
|
|
36
|
+
// 被读成完整账单。
|
|
37
|
+
<span className="italic" data-testid="turn-usage-partial">
|
|
38
|
+
{t('· excludes sub-agents whose engine reports no tokens')}
|
|
39
|
+
</span>
|
|
40
|
+
) : null}
|
|
41
|
+
</p>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default TurnUsageLine;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 回合用量(「这一轮花了多少,其中子代理占多少」)的前端模型。
|
|
3
|
+
*
|
|
4
|
+
* 数据源:local-backend 在回合收尾时把 sidecar 的 `stream.done.usage` 原样
|
|
5
|
+
* 经 `turn_usage` SSE 事件下发,并持久化进助手消息的 messageMetadata(键
|
|
6
|
+
* `usage`)供刷新后水合。
|
|
7
|
+
*
|
|
8
|
+
* 为什么要在前端再算「占比」而不是后端直接给:子代理行各自只带自己那一份
|
|
9
|
+
* (`orchestration_child.usage`),父代理自己的花费不在任何子代理行里,
|
|
10
|
+
* 所以「本回合总计 = 父级 + 子代理」这件事只有拿到整份 usage 才成立。这里
|
|
11
|
+
* 只是把同一份数字拆成可展示的两段,不是重新计费。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** 一个子代理的账单,与 sidecar `usage.children[]` 同形。 */
|
|
15
|
+
export interface TurnChildUsage {
|
|
16
|
+
childId: string;
|
|
17
|
+
profile?: string;
|
|
18
|
+
promptTokens: number;
|
|
19
|
+
completionTokens: number;
|
|
20
|
+
totalTokens: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 一份回合账单。
|
|
25
|
+
*
|
|
26
|
+
* **读法(容易读错,所以写在这里)**:`promptTokens` / `completionTokens` /
|
|
27
|
+
* `totalTokens` 是**父代理自己**的花费;`child*Tokens` 与 `children[]` 是
|
|
28
|
+
* 子代理那一部分,二者是**并列相加**的关系,不是「总计与其中一部分」。这是
|
|
29
|
+
* sidecar `stream.done.usage` 的原有语义(R5 只在 `usage` 上追加键,没有改
|
|
30
|
+
* 键的含义),所以「本回合总共花了多少」= 这两者之和。
|
|
31
|
+
*/
|
|
32
|
+
export interface TurnUsage {
|
|
33
|
+
promptTokens: number;
|
|
34
|
+
completionTokens: number;
|
|
35
|
+
totalTokens: number;
|
|
36
|
+
cachedPromptTokens?: number;
|
|
37
|
+
costUsd?: number;
|
|
38
|
+
/** 子代理合计(与上面的父级数并列,不相交)。 */
|
|
39
|
+
childPromptTokens?: number;
|
|
40
|
+
childCompletionTokens?: number;
|
|
41
|
+
childTotalTokens?: number;
|
|
42
|
+
/** 每个子代理一项;外部引擎子代理不在其中(它们报不出 token)。 */
|
|
43
|
+
children?: TurnChildUsage[];
|
|
44
|
+
/** 报了「用量未知」的子代理数量(外部引擎)。这些花费不在合计里。 */
|
|
45
|
+
unknownUsageChildren?: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function readTokenCount(raw: unknown): number | undefined {
|
|
49
|
+
if (typeof raw !== 'number' || !Number.isFinite(raw) || raw < 0) return undefined;
|
|
50
|
+
return raw;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function parseChildUsage(raw: unknown): TurnChildUsage | null {
|
|
54
|
+
if (!raw || typeof raw !== 'object') return null;
|
|
55
|
+
const rec = raw as Record<string, unknown>;
|
|
56
|
+
if (typeof rec.childId !== 'string' || !rec.childId) return null;
|
|
57
|
+
const promptTokens = readTokenCount(rec.promptTokens);
|
|
58
|
+
const completionTokens = readTokenCount(rec.completionTokens);
|
|
59
|
+
const totalTokens = readTokenCount(rec.totalTokens);
|
|
60
|
+
if (promptTokens === undefined || completionTokens === undefined || totalTokens === undefined) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
childId: rec.childId,
|
|
65
|
+
...(typeof rec.profile === 'string' && rec.profile ? { profile: rec.profile } : {}),
|
|
66
|
+
promptTokens,
|
|
67
|
+
completionTokens,
|
|
68
|
+
totalTokens,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 解析 SSE 事件或已落库的元数据;形状不符返回 null。
|
|
74
|
+
*
|
|
75
|
+
* 三个 token 数是硬要求——缺了它们这条记录不构成账单;其余字段缺失只影响
|
|
76
|
+
* 展示细节(`cachedPromptTokens` / `costUsd` / `children[]` 都在早期版本里
|
|
77
|
+
* 不存在)。与 `parseTurnFiles` 同约定:坏数据返回 null 而不是抛。
|
|
78
|
+
*/
|
|
79
|
+
export function parseTurnUsage(raw: unknown): TurnUsage | null {
|
|
80
|
+
if (!raw || typeof raw !== 'object') return null;
|
|
81
|
+
const rec = raw as Record<string, unknown>;
|
|
82
|
+
const promptTokens = readTokenCount(rec.promptTokens);
|
|
83
|
+
const completionTokens = readTokenCount(rec.completionTokens);
|
|
84
|
+
const totalTokens = readTokenCount(rec.totalTokens);
|
|
85
|
+
if (promptTokens === undefined || completionTokens === undefined || totalTokens === undefined) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
const children = Array.isArray(rec.children)
|
|
89
|
+
? rec.children.map(parseChildUsage).filter((child): child is TurnChildUsage => child !== null)
|
|
90
|
+
: undefined;
|
|
91
|
+
const cachedPromptTokens = readTokenCount(rec.cachedPromptTokens);
|
|
92
|
+
const costUsd = readTokenCount(rec.costUsd);
|
|
93
|
+
const childPromptTokens = readTokenCount(rec.childPromptTokens);
|
|
94
|
+
const childCompletionTokens = readTokenCount(rec.childCompletionTokens);
|
|
95
|
+
const childTotalTokens = readTokenCount(rec.childTotalTokens);
|
|
96
|
+
const unknownUsageChildren = readTokenCount(rec.unknownUsageChildren);
|
|
97
|
+
return {
|
|
98
|
+
promptTokens,
|
|
99
|
+
completionTokens,
|
|
100
|
+
totalTokens,
|
|
101
|
+
...(cachedPromptTokens !== undefined ? { cachedPromptTokens } : {}),
|
|
102
|
+
...(costUsd !== undefined ? { costUsd } : {}),
|
|
103
|
+
...(childPromptTokens !== undefined ? { childPromptTokens } : {}),
|
|
104
|
+
...(childCompletionTokens !== undefined ? { childCompletionTokens } : {}),
|
|
105
|
+
...(childTotalTokens !== undefined ? { childTotalTokens } : {}),
|
|
106
|
+
...(children && children.length > 0 ? { children } : {}),
|
|
107
|
+
...(unknownUsageChildren !== undefined && unknownUsageChildren > 0
|
|
108
|
+
? { unknownUsageChildren }
|
|
109
|
+
: {}),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface TurnUsageBreakdown {
|
|
114
|
+
/** 父代理自己的花费(`usage.totalTokens` 原值)。 */
|
|
115
|
+
parentTokens: number;
|
|
116
|
+
/** 子代理合计。没有子代理时为 0。 */
|
|
117
|
+
childTokens: number;
|
|
118
|
+
/** 整轮总计:父级 + 子代理。 */
|
|
119
|
+
totalTokens: number;
|
|
120
|
+
/** 子代理占总计的百分比;总计为 0 时为 0。 */
|
|
121
|
+
childSharePercent: number;
|
|
122
|
+
/** 有子代理确实花了 token。 */
|
|
123
|
+
hasChildren: boolean;
|
|
124
|
+
/**
|
|
125
|
+
* 有一个以上子代理未报用量(外部引擎),所以合计只是下限。
|
|
126
|
+
* 前端必须据此加「不完整」标记,否则偏小的合计会被读成完整账单。
|
|
127
|
+
*/
|
|
128
|
+
partial: boolean;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 把一份 usage 折成可展示的三段。
|
|
133
|
+
*
|
|
134
|
+
* `childTotalTokens` 缺失时退化为按 `children[]` 求和——上游在只有外部引擎
|
|
135
|
+
* 子代理的路径上只填 `unknownUsageChildren`,此时子代理合计确实是 0(不是
|
|
136
|
+
* 「没有子代理」),`partial` 负责说明这个 0 不代表没花钱。
|
|
137
|
+
*/
|
|
138
|
+
export function summarizeTurnUsage(usage: TurnUsage): TurnUsageBreakdown {
|
|
139
|
+
const summed = usage.children?.reduce((acc, child) => acc + child.totalTokens, 0);
|
|
140
|
+
const childTokens = usage.childTotalTokens ?? summed ?? 0;
|
|
141
|
+
const parentTokens = usage.totalTokens;
|
|
142
|
+
// 父级与子代理是两份并列的账单(见 TurnUsage 注释),整轮总计是相加。
|
|
143
|
+
const totalTokens = parentTokens + childTokens;
|
|
144
|
+
return {
|
|
145
|
+
parentTokens,
|
|
146
|
+
childTokens,
|
|
147
|
+
totalTokens,
|
|
148
|
+
childSharePercent: totalTokens > 0 ? Math.round((childTokens / totalTokens) * 100) : 0,
|
|
149
|
+
// 「派过子代理」包含报了未知用量的那些:它们一个数都没给,但确实在
|
|
150
|
+
// 这一轮花了钱,所以这一行必须出现,并由 `partial` 说明合计只是下限。
|
|
151
|
+
hasChildren:
|
|
152
|
+
childTokens > 0 ||
|
|
153
|
+
(usage.children?.length ?? 0) > 0 ||
|
|
154
|
+
(usage.unknownUsageChildren ?? 0) > 0,
|
|
155
|
+
partial: (usage.unknownUsageChildren ?? 0) > 0,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** 与面板行内一致的精简 token 数(1.2k / 3.40M)。 */
|
|
160
|
+
export function formatTurnTokens(n: number): string {
|
|
161
|
+
if (!Number.isFinite(n) || n < 0) return '0';
|
|
162
|
+
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(2)}M`;
|
|
163
|
+
if (n >= 1_000) return `${(n / 1_000).toFixed(1)}k`;
|
|
164
|
+
return String(Math.round(n));
|
|
165
|
+
}
|
package/src/pages/AgentPage.tsx
CHANGED
|
@@ -41,6 +41,7 @@ import {
|
|
|
41
41
|
type LlmSpeedSnapshot,
|
|
42
42
|
} from "@/components/chat/process-status";
|
|
43
43
|
import { parseTurnFiles, type TurnFile } from "@/components/chat/turn-files";
|
|
44
|
+
import { parseTurnUsage, type TurnUsage } from "@/components/chat/turn-usage";
|
|
44
45
|
import { readPersistedDurationMs } from "@/components/chat/elapsed";
|
|
45
46
|
import {
|
|
46
47
|
extractLatestSuggestedReplies,
|
|
@@ -209,6 +210,27 @@ function extractPersistedTurnFiles(
|
|
|
209
210
|
return seeded;
|
|
210
211
|
}
|
|
211
212
|
|
|
213
|
+
/** R6: 每回合的账单,从助手消息元数据水合(与 turnFiles 同法)。 */
|
|
214
|
+
function extractPersistedTurnUsage(
|
|
215
|
+
messages: ChatMessageWithMetadata[] | undefined,
|
|
216
|
+
): Record<string, TurnUsage> {
|
|
217
|
+
const seeded: Record<string, TurnUsage> = {};
|
|
218
|
+
if (!messages || messages.length === 0) return seeded;
|
|
219
|
+
for (const message of messages) {
|
|
220
|
+
if (message.role !== "assistant" || !message.messageMetadata) continue;
|
|
221
|
+
try {
|
|
222
|
+
const metadata = JSON.parse(message.messageMetadata) as {
|
|
223
|
+
usage?: unknown;
|
|
224
|
+
};
|
|
225
|
+
const usage = parseTurnUsage(metadata.usage);
|
|
226
|
+
if (usage) seeded[message.id] = usage;
|
|
227
|
+
} catch {
|
|
228
|
+
// Ignore malformed legacy metadata.
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return seeded;
|
|
232
|
+
}
|
|
233
|
+
|
|
212
234
|
/**
|
|
213
235
|
* 把注册表里的子代理并进重启后的面板(R7-2)。
|
|
214
236
|
*
|
|
@@ -312,6 +334,9 @@ function AgentChatLoader({
|
|
|
312
334
|
const [initialTurnFiles, setInitialTurnFiles] = useState<
|
|
313
335
|
Record<string, TurnFile[]>
|
|
314
336
|
>({});
|
|
337
|
+
const [initialTurnUsage, setInitialTurnUsage] = useState<
|
|
338
|
+
Record<string, TurnUsage>
|
|
339
|
+
>({});
|
|
315
340
|
const [initialOrchestrationChildren, setInitialOrchestrationChildren] =
|
|
316
341
|
useState<Record<string, ChildInfo[]>>({});
|
|
317
342
|
const [initialSuggestedReplies, setInitialSuggestedReplies] =
|
|
@@ -350,6 +375,7 @@ function AgentChatLoader({
|
|
|
350
375
|
setInitialTimelines(extractPersistedTimelines(response.messages));
|
|
351
376
|
setInitialDurations(extractPersistedDurations(response.messages));
|
|
352
377
|
setInitialTurnFiles(extractPersistedTurnFiles(response.messages));
|
|
378
|
+
setInitialTurnUsage(extractPersistedTurnUsage(response.messages));
|
|
353
379
|
// R7-2:落库事件只覆盖「回合跑到了收尾」的情形。进程被杀时最后一条
|
|
354
380
|
// assistant 消息可能没落库,那些子代理只存在于注册表里——不合并的话
|
|
355
381
|
// 用户重开后看到的比实际派出去的少。
|
|
@@ -387,6 +413,7 @@ function AgentChatLoader({
|
|
|
387
413
|
setInitialTimelines({});
|
|
388
414
|
setInitialDurations({});
|
|
389
415
|
setInitialTurnFiles({});
|
|
416
|
+
setInitialTurnUsage({});
|
|
390
417
|
setInitialOrchestrationChildren({});
|
|
391
418
|
setInitialSuggestedReplies(null);
|
|
392
419
|
setInitialInterrupted(false);
|
|
@@ -414,6 +441,7 @@ function AgentChatLoader({
|
|
|
414
441
|
initialTimelines={initialTimelines}
|
|
415
442
|
initialDurations={initialDurations}
|
|
416
443
|
initialTurnFiles={initialTurnFiles}
|
|
444
|
+
initialTurnUsage={initialTurnUsage}
|
|
417
445
|
initialOrchestrationChildren={initialOrchestrationChildren}
|
|
418
446
|
initialSuggestedReplies={initialSuggestedReplies}
|
|
419
447
|
initialInterrupted={initialInterrupted}
|
|
@@ -431,6 +459,7 @@ interface AgentChatViewProps {
|
|
|
431
459
|
initialTimelines: Record<string, TurnBlock[]>;
|
|
432
460
|
initialDurations: Record<string, number>;
|
|
433
461
|
initialTurnFiles: Record<string, TurnFile[]>;
|
|
462
|
+
initialTurnUsage: Record<string, TurnUsage>;
|
|
434
463
|
initialOrchestrationChildren: Record<string, ChildInfo[]>;
|
|
435
464
|
initialSuggestedReplies: SuggestedRepliesState | null;
|
|
436
465
|
/** W7-1: 打开会话时上一轮处于中断态(崩溃/强杀,无完成记录)。 */
|
|
@@ -448,6 +477,7 @@ function AgentChatView({
|
|
|
448
477
|
initialTimelines,
|
|
449
478
|
initialDurations,
|
|
450
479
|
initialTurnFiles,
|
|
480
|
+
initialTurnUsage,
|
|
451
481
|
initialOrchestrationChildren,
|
|
452
482
|
initialSuggestedReplies,
|
|
453
483
|
initialInterrupted,
|
|
@@ -571,6 +601,14 @@ function AgentChatView({
|
|
|
571
601
|
// 归档后占位 id 消息仍在屏上的这段时间,都靠它渲染尾部消息的文件列表。
|
|
572
602
|
const [currentTurnFiles, setCurrentTurnFiles] = useState<TurnFile[]>([]);
|
|
573
603
|
const pendingTurnFilesRef = useRef<TurnFile[]>([]);
|
|
604
|
+
// R6 同款:turn_usage 也赶在 message_id 之前到,随同一批队列归档。
|
|
605
|
+
const [turnUsageByMessageId, setTurnUsageByMessageId] = useState<
|
|
606
|
+
Record<string, TurnUsage>
|
|
607
|
+
>(initialTurnUsage);
|
|
608
|
+
const [currentTurnUsage, setCurrentTurnUsage] = useState<TurnUsage | undefined>(
|
|
609
|
+
undefined,
|
|
610
|
+
);
|
|
611
|
+
const pendingTurnUsageRef = useRef<TurnUsage | undefined>(undefined);
|
|
574
612
|
const turnStartedAtRef = useRef<number | null>(null);
|
|
575
613
|
const [currentTurnStartedAtMs, setCurrentTurnStartedAtMs] = useState<
|
|
576
614
|
number | undefined
|
|
@@ -679,6 +717,16 @@ function AgentChatView({
|
|
|
679
717
|
}
|
|
680
718
|
return;
|
|
681
719
|
}
|
|
720
|
+
if (ev.event === "turn_usage") {
|
|
721
|
+
// R6:同 turn_files,收尾时发一次,message_id 到达时归档到落库 id。
|
|
722
|
+
// 解析失败不打断回合——账单缺一块比丢一整个回合轻。
|
|
723
|
+
const usage = parseTurnUsage(ev.payload?.usage);
|
|
724
|
+
if (usage) {
|
|
725
|
+
pendingTurnUsageRef.current = usage;
|
|
726
|
+
setCurrentTurnUsage(usage);
|
|
727
|
+
}
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
682
730
|
if (ev.event === "round_end") {
|
|
683
731
|
// chat-transport surfaces `completion executing` as round_end. Bumping
|
|
684
732
|
// the counter here is safe even if the round emits no tools — the
|
|
@@ -761,6 +809,14 @@ function AgentChatView({
|
|
|
761
809
|
}));
|
|
762
810
|
}
|
|
763
811
|
pendingTurnFilesRef.current = [];
|
|
812
|
+
const queuedTurnUsage = pendingTurnUsageRef.current;
|
|
813
|
+
if (queuedTurnUsage) {
|
|
814
|
+
setTurnUsageByMessageId((prev) => ({
|
|
815
|
+
...prev,
|
|
816
|
+
[messageId]: queuedTurnUsage,
|
|
817
|
+
}));
|
|
818
|
+
}
|
|
819
|
+
pendingTurnUsageRef.current = undefined;
|
|
764
820
|
setTailAssistantDbId(messageId);
|
|
765
821
|
}
|
|
766
822
|
// AI 标题更新走的是后台 IPC 广播(chat-title-updated)而不是 SSE——
|
|
@@ -992,6 +1048,8 @@ function AgentChatView({
|
|
|
992
1048
|
pendingChildSpawnsRef.current = new Map();
|
|
993
1049
|
pendingTurnFilesRef.current = [];
|
|
994
1050
|
setCurrentTurnFiles([]);
|
|
1051
|
+
pendingTurnUsageRef.current = undefined;
|
|
1052
|
+
setCurrentTurnUsage(undefined);
|
|
995
1053
|
setCurrentRound(1);
|
|
996
1054
|
setRetryState(null);
|
|
997
1055
|
const started = Date.now();
|
|
@@ -1165,6 +1223,8 @@ function AgentChatView({
|
|
|
1165
1223
|
setCurrentTurnChildren([]);
|
|
1166
1224
|
pendingTurnFilesRef.current = [];
|
|
1167
1225
|
setCurrentTurnFiles([]);
|
|
1226
|
+
pendingTurnUsageRef.current = undefined;
|
|
1227
|
+
setCurrentTurnUsage(undefined);
|
|
1168
1228
|
setCurrentRound(1);
|
|
1169
1229
|
setRetryState(null);
|
|
1170
1230
|
const started = Date.now();
|
|
@@ -1484,6 +1544,8 @@ function AgentChatView({
|
|
|
1484
1544
|
llmSpeedByMessageId={llmSpeedByMessageId}
|
|
1485
1545
|
turnFilesByMessageId={turnFilesByMessageId}
|
|
1486
1546
|
currentTurnFiles={currentTurnFiles}
|
|
1547
|
+
turnUsageByMessageId={turnUsageByMessageId}
|
|
1548
|
+
currentTurnUsage={currentTurnUsage}
|
|
1487
1549
|
currentTurnChildren={effectiveCurrentTurnChildren}
|
|
1488
1550
|
orchestrationChildrenByMessageId={orchestrationChildrenByMessageId}
|
|
1489
1551
|
currentRound={currentRound}
|