Refactor tool block update logic in ChatMessage
Browse filesReplaces the use of the last block for tool updates with a search for an existing tool block by UUID. This ensures that updates are correctly grouped with their corresponding tool blocks, improving message block handling.
src/lib/components/chat/ChatMessage.svelte
CHANGED
|
@@ -128,6 +128,8 @@
|
|
| 128 |
| { type: "text"; content: string }
|
| 129 |
| { type: "tool"; uuid: string; updates: MessageToolUpdate[] };
|
| 130 |
|
|
|
|
|
|
|
| 131 |
let blocks = $derived.by(() => {
|
| 132 |
const updates = message.updates ?? [];
|
| 133 |
const res: Block[] = [];
|
|
@@ -155,9 +157,11 @@
|
|
| 155 |
if (last?.type === "text") last.content += chunk;
|
| 156 |
else res.push({ type: "text" as const, content: chunk });
|
| 157 |
} else if (isMessageToolUpdate(update)) {
|
| 158 |
-
const
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
| 161 |
} else {
|
| 162 |
res.push({ type: "tool" as const, uuid: update.uuid, updates: [update] });
|
| 163 |
}
|
|
|
|
| 128 |
| { type: "text"; content: string }
|
| 129 |
| { type: "tool"; uuid: string; updates: MessageToolUpdate[] };
|
| 130 |
|
| 131 |
+
type ToolBlock = Extract<Block, { type: "tool" }>;
|
| 132 |
+
|
| 133 |
let blocks = $derived.by(() => {
|
| 134 |
const updates = message.updates ?? [];
|
| 135 |
const res: Block[] = [];
|
|
|
|
| 157 |
if (last?.type === "text") last.content += chunk;
|
| 158 |
else res.push({ type: "text" as const, content: chunk });
|
| 159 |
} else if (isMessageToolUpdate(update)) {
|
| 160 |
+
const existingBlock = res.find(
|
| 161 |
+
(b): b is ToolBlock => b.type === "tool" && b.uuid === update.uuid
|
| 162 |
+
);
|
| 163 |
+
if (existingBlock) {
|
| 164 |
+
existingBlock.updates.push(update);
|
| 165 |
} else {
|
| 166 |
res.push({ type: "tool" as const, uuid: update.uuid, updates: [update] });
|
| 167 |
}
|