feat(chat): show each tool call where it happened in the reply

This commit is contained in:
sam committed 2026-10-24 20:48:43 +11:00
1 parent 2bcfcf6ea8
commit 9387cb1f5e
9 files changed
+67 -20

No files matched your search

+36 -16
View File
@@ -31,21 +31,31 @@ const props = withDefaults(
{ tools: () => [], media: () => [], assistant: false, streaming: false },
);
// Consecutive calls fold into one counted line. A navigation chip is an
// action for the operator, so it always stands alone and ends the run.
const runs = computed(() => {
const out: ToolCall[][] = [];
// The reply in the order it happened: text up to each call's offset, then the
// call. Consecutive calls at one point fold into one line; a navigation chip
// is an action for the operator, so it always stands alone. A call recorded
// without an offset sits at the start.
type Segment = { text: string } | { tools: ToolCall[] };
const segments = computed(() => {
const chars = Array.from(props.text);
const out: Segment[] = [];
let at = 0;
let open = false;
for (const tool of props.tools) {
if (tool.path) {
out.push([tool]);
const offset = Math.min(Math.max(tool.text_offset ?? 0, at), chars.length);
if (offset > at) {
out.push({ text: chars.slice(at, offset).join("") });
at = offset;
open = false;
} else if (open) out[out.length - 1].push(tool);
}
const last = out[out.length - 1];
if (!tool.path && open && last && "tools" in last) last.tools.push(tool);
else {
out.push([tool]);
open = true;
out.push({ tools: [tool] });
open = !tool.path;
}
}
if (at < chars.length) out.push({ text: chars.slice(at).join("") });
return out;
});
</script>
@@ -55,12 +65,6 @@ const runs = computed(() => {
<MessageContent>
<Bubble :variant="assistant ? 'muted' : 'tinted'" class="max-w-[min(78ch,92%)]">
<BubbleContent class="[overflow-wrap:anywhere]">
<Trace v-if="assistant && tools.length" class="mb-2">
<template v-for="(run, i) in runs" :key="i">
<ChatToolGroup v-if="run.length > 1" :tools="run" :streaming="streaming" />
<ChatToolCall v-else :tool="run[0]" :streaming="streaming" />
</template>
</Trace>
<div v-if="media.length" class="mb-2 flex flex-wrap gap-1.5">
<ChatAttachment
v-for="(attachment, i) in media"
@@ -68,7 +72,23 @@ const runs = computed(() => {
:attachment="attachment"
/>
</div>
<ChatMarkdown v-if="assistant" :text="text" />
<template v-if="assistant">
<template v-for="(segment, i) in segments" :key="i">
<ChatMarkdown v-if="'text' in segment" :text="segment.text" />
<Trace v-else class="my-2">
<ChatToolGroup
v-if="segment.tools.length > 1"
:tools="segment.tools"
:streaming="streaming && i === segments.length - 1"
/>
<ChatToolCall
v-else
:tool="segment.tools[0]"
:streaming="streaming && i === segments.length - 1"
/>
</Trace>
</template>
</template>
<div v-else class="whitespace-pre-wrap">{{ text }}</div>
<slot />
</BubbleContent>
+2
View File
@@ -47,6 +47,8 @@ export interface ChatToolCall {
text?: string;
failed?: boolean;
duration_ms?: number;
/** Runes of reply text before the call: where in the reply it happened. */
text_offset?: number;
/** A dashboard_navigate result: the page to route to, instead of a tool line. */
path?: string;
label?: string;