143 lines
4.9 KiB
Vue
143 lines
4.9 KiB
Vue
<script setup lang="ts">
|
|
import { computed } from "vue";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
MessageScroller,
|
|
MessageScrollerContent,
|
|
MessageScrollerItem,
|
|
MessageScrollerProvider,
|
|
MessageScrollerViewport,
|
|
} from "@/components/ui/message-scroller";
|
|
import ChatAskCard from "./ChatAskCard.vue";
|
|
import ChatBubble from "./ChatBubble.vue";
|
|
import ChatChooseCard from "./ChatChooseCard.vue";
|
|
import ChatUpdateCard from "./ChatUpdateCard.vue";
|
|
import ChatEmptyState from "./ChatEmptyState.vue";
|
|
import ChatJumpButton from "./ChatJumpButton.vue";
|
|
import { Alert, AlertDescription } from "@/components/ui/alert";
|
|
import { Skeleton } from "@/components/ui/skeleton";
|
|
import {
|
|
currentSession,
|
|
isLoadingSession,
|
|
messages,
|
|
selectSession,
|
|
sendMessage,
|
|
sessionError,
|
|
streamingTurn,
|
|
type ChatMessage,
|
|
} from "./state";
|
|
|
|
/**
|
|
* The conversation: the messages so far, the turn still arriving, and what the
|
|
* panel says before either exists.
|
|
*
|
|
* The scroller follows the tail, and stops following the moment the reader
|
|
* scrolls up rather than yanking them back on every streamed token. A watcher
|
|
* that set scrollTop by hand would have no "reader scrolled away" state and no
|
|
* way back down, so MessageScroller owns this.
|
|
*/
|
|
const emit = defineEmits<{ prompt: [text: string] }>();
|
|
|
|
function parts(message: ChatMessage, index: number) {
|
|
const from = message.from ?? message.From ?? "";
|
|
// Anything this browser's composer did not send is Archie's answer.
|
|
return {
|
|
id: message.message_id || message.MessageID || `m${index}`,
|
|
text: message.text ?? message.Text ?? "",
|
|
tools: message.tool_calls || message.ToolCalls || [],
|
|
// Attachments have no bytes left to render -- the record kept their
|
|
// metadata only -- so the bubble labels them.
|
|
media: message.media || [],
|
|
assistant: from !== "web",
|
|
update: message.update,
|
|
choose: message.choose,
|
|
};
|
|
}
|
|
|
|
const bubbles = computed(() => messages.value.map(parts));
|
|
|
|
// The live turn is the row the finished message will take, under the same id:
|
|
// the scroller sees one row grow and settle, not one leave and another arrive.
|
|
const rows = computed(() => {
|
|
const turn = streamingTurn.value;
|
|
if (!turn) return bubbles.value.map((bubble) => ({ ...bubble, live: false }));
|
|
return [
|
|
...bubbles.value.map((bubble) => ({ ...bubble, live: false })),
|
|
{
|
|
id: `m${bubbles.value.length}`,
|
|
text: turn.text || "…",
|
|
tools: turn.tools,
|
|
media: [],
|
|
assistant: true,
|
|
update: undefined,
|
|
choose: undefined,
|
|
live: true,
|
|
},
|
|
];
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<MessageScrollerProvider auto-scroll default-scroll-position="last-anchor">
|
|
<MessageScroller class="min-h-0 flex-1">
|
|
<MessageScrollerViewport>
|
|
<MessageScrollerContent class="gap-4 p-4">
|
|
<template v-if="isLoadingSession && !bubbles.length">
|
|
<Skeleton class="h-14 w-3/5 self-end rounded-xl" />
|
|
<Skeleton class="h-24 w-4/5 rounded-xl" />
|
|
<Skeleton class="h-10 w-2/5 self-end rounded-xl" />
|
|
</template>
|
|
|
|
<div
|
|
v-else-if="sessionError && !bubbles.length"
|
|
class="flex flex-col items-center gap-3 p-4"
|
|
>
|
|
<Alert variant="destructive" class="max-w-md"><AlertDescription>{{ sessionError }}</AlertDescription></Alert>
|
|
<Button variant="outline" size="sm" @click="selectSession(currentSession)">Retry</Button>
|
|
</div>
|
|
|
|
<ChatEmptyState
|
|
v-else-if="!bubbles.length && !streamingTurn"
|
|
@prompt="emit('prompt', $event)"
|
|
/>
|
|
|
|
<template v-else>
|
|
<MessageScrollerItem
|
|
v-for="row in rows"
|
|
:key="row.id"
|
|
:message-id="row.id"
|
|
:scroll-anchor="!row.assistant"
|
|
>
|
|
<ChatBubble
|
|
:text="row.text"
|
|
:tools="row.tools"
|
|
:media="row.media"
|
|
:assistant="row.assistant"
|
|
:streaming="row.live"
|
|
>
|
|
<template v-if="row.live && streamingTurn">
|
|
<Button
|
|
v-if="streamingTurn.isError && streamingTurn.turn"
|
|
variant="outline"
|
|
size="sm"
|
|
class="mt-2"
|
|
@click="sendMessage({ turn: streamingTurn.turn })"
|
|
>Retry</Button>
|
|
<ChatAskCard v-if="streamingTurn.ask" :ask="streamingTurn.ask" />
|
|
</template>
|
|
<template v-else>
|
|
<ChatUpdateCard v-if="row.update" :update="row.update" />
|
|
<ChatChooseCard v-if="row.choose" :choose="row.choose" />
|
|
</template>
|
|
</ChatBubble>
|
|
</MessageScrollerItem>
|
|
</template>
|
|
</MessageScrollerContent>
|
|
</MessageScrollerViewport>
|
|
|
|
<ChatJumpButton />
|
|
</MessageScroller>
|
|
</MessageScrollerProvider>
|
|
</template>
|