125 lines
4.9 KiB
Vue
125 lines
4.9 KiB
Vue
<script setup lang="ts">
|
|
import { Alert, AlertDescription } from "@/components/ui/alert";
|
|
import { computed } from "vue";
|
|
|
|
import { Bubble, BubbleContent } from "@/components/ui/bubble";
|
|
import { Trace } from "@/components/ui/trace";
|
|
import { CodeBlockCopyButton } from "@/components/ui/code-block";
|
|
import { Message, MessageContent, MessageFooter } from "@/components/ui/message";
|
|
import ChatAttachment from "./ChatAttachment.vue";
|
|
import ChatMarkdown from "./ChatMarkdown.vue";
|
|
import ChatThinking from "./ChatThinking.vue";
|
|
import ChatToolCall from "./ChatToolCall.vue";
|
|
import ChatToolGroup from "./ChatToolGroup.vue";
|
|
import type { ChatMedia, ChatToolCall as ToolCall } from "./state";
|
|
|
|
/**
|
|
* One message, from either side of the conversation. There is no speaker
|
|
* label: which side of the panel it sits on, and its fill, are what say who is
|
|
* talking. A trailing action arrives through the slot, so the retry the
|
|
* transcript offers belongs to the bubble rather than beside it. A finished
|
|
* reply carries a copy action beneath it.
|
|
*/
|
|
const props = withDefaults(
|
|
defineProps<{
|
|
text: string;
|
|
tools?: ToolCall[];
|
|
/** Attachments the message carried, which have no bytes left to render. */
|
|
media?: ChatMedia[];
|
|
assistant?: boolean;
|
|
/** The turn is still arriving: a tool call with no outcome is running. */
|
|
streaming?: boolean;
|
|
/** The live turn's thinking so far, rendered as a collapsible trace. */
|
|
reasoning?: string;
|
|
/** The live turn is still arriving and has not reasoned yet. */
|
|
thinkingPending?: boolean;
|
|
/** The text says why the turn failed rather than answering. */
|
|
error?: boolean;
|
|
}>(),
|
|
{ tools: () => [], media: () => [], assistant: false, streaming: false, reasoning: "", thinkingPending: false },
|
|
);
|
|
|
|
// 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) {
|
|
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;
|
|
}
|
|
const last = out[out.length - 1];
|
|
if (!tool.path && open && last && "tools" in last) last.tools.push(tool);
|
|
else {
|
|
out.push({ tools: [tool] });
|
|
open = !tool.path;
|
|
}
|
|
}
|
|
if (at < chars.length) out.push({ text: chars.slice(at).join("") });
|
|
return out;
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<Message :align="assistant ? 'start' : 'end'">
|
|
<MessageContent>
|
|
<Bubble :variant="assistant ? 'muted' : 'tinted'" class="max-w-[min(78ch,92%)]">
|
|
<BubbleContent class="[overflow-wrap:anywhere]">
|
|
<div v-if="media.length" class="mb-2 flex flex-wrap gap-1.5">
|
|
<ChatAttachment
|
|
v-for="(attachment, i) in media"
|
|
:key="i"
|
|
:attachment="attachment"
|
|
/>
|
|
</div>
|
|
<template v-if="assistant">
|
|
<ChatThinking
|
|
v-if="reasoning || thinkingPending"
|
|
:text="reasoning"
|
|
:pending="thinkingPending"
|
|
/>
|
|
<template v-if="error">
|
|
<Trace v-if="tools.length" class="mb-2">
|
|
<ChatToolGroup v-if="tools.length > 1" :tools="tools" :streaming="false" />
|
|
<ChatToolCall v-else :tool="tools[0]" :streaming="false" />
|
|
</Trace>
|
|
<Alert variant="destructive" class="text-label"><AlertDescription>{{ text }}</AlertDescription></Alert>
|
|
</template>
|
|
<template v-for="(segment, i) in segments" v-else :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>
|
|
</Bubble>
|
|
<MessageFooter
|
|
v-if="assistant && !streaming && text"
|
|
class="px-0 transition-opacity focus-within:opacity-100 [@media(hover:hover)]:opacity-0 [@media(hover:hover)]:group-hover/message:opacity-100"
|
|
>
|
|
<CodeBlockCopyButton :value="text" />
|
|
</MessageFooter>
|
|
</MessageContent>
|
|
</Message>
|
|
</template>
|