* 「结构签名」——只在图的结构(节点 / 边 / 工具状态 / 思考步骤标题)真正变化时改变; * 正文逐字增长(text-delta)不改变签名。 * * 用途:流式期间 message 每个增量都是新对象,若直接用它当 useMemo 依赖,会导致 * 每个字符都重建 + 重新布局整张图 → 节点/连线持续抖动。改用结构签名当依赖后, * 只有「真正多了一个节点 / 工具状态翻转 / 新的思考步骤」时才重排,推理图保持稳定。
( msg: UIMessage | null | undefined, userQuery?: string | null, )
| 45 | * 只有「真正多了一个节点 / 工具状态翻转 / 新的思考步骤」时才重排,推理图保持稳定。 |
| 46 | */ |
| 47 | function graphSignature( |
| 48 | msg: UIMessage | null | undefined, |
| 49 | userQuery?: string | null, |
| 50 | ): string { |
| 51 | const sig: string[] = [`q:${userQuery?.trim() ? 1 : 0}`]; |
| 52 | for (const part of msg?.parts ?? []) { |
| 53 | if (part.kind === "tool-call") { |
| 54 | const r = part.call.result; |
| 55 | const st = r ? (r.ok ? "ok" : "err") : "pend"; |
| 56 | sig.push( |
| 57 | `t:${part.call.id}:${part.call.name}:${st}:${part.call.synthetic ? 1 : 0}`, |
| 58 | ); |
| 59 | } else if (part.kind === "text") { |
| 60 | // 只取 【TYPE】标题行(不含正文)——正文逐字增长不进签名、不触发重排 |
| 61 | const re = /【([A-Za-z]+)】([^\n]*)/g; |
| 62 | let m: RegExpExecArray | null; |
| 63 | while ((m = re.exec(part.text)) !== null) { |
| 64 | sig.push(`h:${m[1].toUpperCase()}:${m[2].replace(/\*\*/g, "").trim()}`); |
| 65 | } |
| 66 | } |
| 67 | // reasoning 段不进流程图,忽略 |
| 68 | } |
| 69 | return sig.join(";"); |
| 70 | } |
| 71 | |
| 72 | interface Props { |
| 73 | message: UIMessage | null; |