@qjp88995/agent-chat-web 0.4.0 → 0.6.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.
@@ -2,6 +2,7 @@ import type { AgentTraceData } from '../stream/wire.js';
2
2
  import {
3
3
  appendReasoningBlock,
4
4
  appendTextBlock,
5
+ endSegmentBlock,
5
6
  mergeTraceBlocks,
6
7
  type MessageBlock,
7
8
  } from '../trace/index.js';
@@ -208,6 +209,21 @@ function buildLiveTurn(
208
209
  blocks = appendReasoningBlock(blocks, d, e.ts ?? Date.now());
209
210
  break;
210
211
  }
212
+ // 一段思考 / 正文的完整帧:`hide` / `replace` 模式下这一段要显示的内容只在
213
+ // 这里(增量的 `text` 是 null),`show` 下全文已被置 null、照旧只看增量。
214
+ // 正文的拍平副本跟着改(复制按钮读它);思考的不改——它只喂长度估算,
215
+ // 理由同 `endReasoningOnStreaming`。
216
+ case 'text-end': {
217
+ if (typeof e.text !== 'string') break;
218
+ const r = endSegmentBlock(blocks, 'text', e.text);
219
+ text = text.slice(0, text.length - r.replaced) + e.text;
220
+ blocks = r.blocks;
221
+ break;
222
+ }
223
+ case 'reasoning-end':
224
+ if (typeof e.text !== 'string') break;
225
+ blocks = endSegmentBlock(blocks, 'reasoning', e.text, e.ts).blocks;
226
+ break;
211
227
  case 'run-finish':
212
228
  streaming = false;
213
229
  break;
@@ -7,6 +7,7 @@ export {
7
7
  appendReasoningBlock,
8
8
  appendTextBlock,
9
9
  blocksFromParts,
10
+ endSegmentBlock,
10
11
  hiddenRunKeys,
11
12
  mergeTraceBlocks,
12
13
  type MessageBlock,
@@ -27,6 +27,9 @@ export interface LiveMessage {
27
27
  * 交错,见 message-blocks.ts),本字段不再单独渲染。deepseek-v4-flash 偶发把整轮
28
28
  * 回复整段塞进 reasoning_content、`text` 一个 token 都不产——那种情况下 reasoning
29
29
  * 块就是这条消息唯一可见的内容(折叠展示),不会是空气泡。
30
+ *
31
+ * 记的是**流过来的量**:带全文的 `reasoning-end`(出口 `hide` / `replace` 模式)只
32
+ * 覆盖块、不改这里——它喂运行状态行的估算,改成占位长度会让数字中途往回跳。
30
33
  */
31
34
  reasoning: string;
32
35
  traces: AgentTraceData[];
@@ -19,7 +19,12 @@ import { buildTraceTree, type TraceNode } from './trace-tree.js';
19
19
  * 同款,供长度判断/兼容既有断言),但**渲染以块序列为权威**。
20
20
  */
21
21
  export type MessageBlock =
22
- | { type: 'text'; text: string }
22
+ | {
23
+ type: 'text';
24
+ text: string;
25
+ /** 流式专用:见 reasoning 块的同名字段。 */
26
+ closedLength?: number;
27
+ }
23
28
  | { type: 'trace'; key: string }
24
29
  | {
25
30
  type: 'reasoning';
@@ -37,6 +42,13 @@ export type MessageBlock =
37
42
  durationMs?: number;
38
43
  /** 流式专用:本块第一个 delta 的到达时刻,用来现算 durationMs。 */
39
44
  startedAt?: number;
45
+ /**
46
+ * 流式专用:本块开头已被结束帧定稿的字数(见 `endSegmentBlock`)。之后的增量
47
+ * 属于新的一段,下一个结束帧只覆盖这个位置之后的部分——相邻的两段并在一个块里时
48
+ * 不会把前一段一起盖掉(与落库后 `blocksFromParts` 把相邻段拼接成一块一致)。
49
+ * 缺省 = 0。
50
+ */
51
+ closedLength?: number;
40
52
  };
41
53
 
42
54
  /**
@@ -50,11 +62,54 @@ export function appendTextBlock(
50
62
  if (delta === '') return blocks;
51
63
  const last = blocks[blocks.length - 1];
52
64
  if (last && last.type === 'text') {
53
- return [...blocks.slice(0, -1), { type: 'text', text: last.text + delta }];
65
+ return [...blocks.slice(0, -1), { ...last, text: last.text + delta }];
54
66
  }
55
67
  return [...blocks, { type: 'text', text: delta }];
56
68
  }
57
69
 
70
+ /**
71
+ * 一段流式内容的**完整帧**到了(深度 0 的 `reasoning-end`、子 Agent 的
72
+ * `reasoning-end` / `text-end`,载荷是这一段的全文):用全文覆盖这一段。
73
+ *
74
+ * 出口按调用方的模式改写过程(runtime `./sse` 的 `createProcessView`):`show` 下
75
+ * 结束帧的全文已被置 null,调用方根本不会走到这里;`hide` / `replace` 下增量只是
76
+ * 占位(或干脆没有),这一段真正要显示的内容(占位 / 替换文本)只在结束帧上。
77
+ *
78
+ * 「这一段」= 末块若同类型,是它 `closedLength` 之后的部分(之前的增量);否则
79
+ * 这一段没有任何增量上屏,新起一块。空全文不新建块,覆盖成空的块整个删掉。
80
+ *
81
+ * `replaced` 是被覆盖掉的字数——调用方据此同步拍平副本。思考块的时长推到 `now`
82
+ * (缺省不动)。
83
+ */
84
+ export function endSegmentBlock(
85
+ blocks: MessageBlock[],
86
+ type: 'text' | 'reasoning',
87
+ text: string,
88
+ now?: number
89
+ ): { blocks: MessageBlock[]; replaced: number } {
90
+ const last = blocks[blocks.length - 1];
91
+ if (!last || last.type !== type) {
92
+ if (text === '') return { blocks, replaced: 0 };
93
+ return {
94
+ blocks: [...blocks, { type, text, closedLength: text.length }],
95
+ replaced: 0,
96
+ };
97
+ }
98
+ const closed = Math.min(last.closedLength ?? 0, last.text.length);
99
+ const next = last.text.slice(0, closed) + text;
100
+ const replaced = last.text.length - closed;
101
+ if (next === '') return { blocks: blocks.slice(0, -1), replaced };
102
+ const block: MessageBlock = {
103
+ ...last,
104
+ text: next,
105
+ closedLength: next.length,
106
+ };
107
+ if (block.type === 'reasoning' && now != null && block.startedAt != null) {
108
+ block.durationMs = now - block.startedAt;
109
+ }
110
+ return { blocks: [...blocks.slice(0, -1), block], replaced };
111
+ }
112
+
58
113
  /**
59
114
  * 追加一段思考增量:并入末尾的 reasoning 块,否则起一个**新** reasoning 块。
60
115
  * 「起新块」正是修复的关键——一旦中间夹了 text/工具块,后续思考不会再并回前