feat(ui): code blocks and chat messages from shadcn-vue's code-block, message and bubble
This commit is contained in:
1 parent
d876c150eb
commit
63adbc71ef
67 files changed
+1770
-310
No files matched your search
+34
-31
@@ -1,5 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { cn } from "@/lib/utils";
|
||||
import { Bubble, BubbleContent } from "@/components/ui/bubble";
|
||||
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 ChatToolCall from "./ChatToolCall.vue";
|
||||
@@ -9,7 +11,8 @@ 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.
|
||||
* transcript offers belongs to the bubble rather than beside it. A finished
|
||||
* reply carries a copy action beneath it.
|
||||
*/
|
||||
withDefaults(
|
||||
defineProps<{
|
||||
@@ -26,33 +29,33 @@ withDefaults(
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="flex" :class="assistant ? 'justify-start' : 'justify-end'">
|
||||
<div
|
||||
:class="
|
||||
cn(
|
||||
'max-w-[min(78ch,92%)] rounded-[0.85rem] px-3.5 py-2.5 text-sm leading-normal [overflow-wrap:anywhere]',
|
||||
assistant ? 'bg-muted' : 'bg-primary/15',
|
||||
)
|
||||
"
|
||||
>
|
||||
<div v-if="assistant && tools.length" class="mb-2 grid gap-0.5 text-xs">
|
||||
<ChatToolCall
|
||||
v-for="(tool, i) in tools"
|
||||
:key="i"
|
||||
:tool="tool"
|
||||
:streaming="streaming"
|
||||
/>
|
||||
</div>
|
||||
<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>
|
||||
<ChatMarkdown v-if="assistant" :text="text" />
|
||||
<div v-else class="whitespace-pre-wrap">{{ text }}</div>
|
||||
<slot />
|
||||
</div>
|
||||
</div>
|
||||
<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="assistant && tools.length" class="mb-2 grid gap-0.5 text-xs">
|
||||
<ChatToolCall
|
||||
v-for="(tool, i) in tools"
|
||||
:key="i"
|
||||
:tool="tool"
|
||||
:streaming="streaming"
|
||||
/>
|
||||
</div>
|
||||
<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>
|
||||
<ChatMarkdown v-if="assistant" :text="text" />
|
||||
<div v-else class="whitespace-pre-wrap">{{ text }}</div>
|
||||
<slot />
|
||||
</BubbleContent>
|
||||
</Bubble>
|
||||
<MessageFooter v-if="assistant && !streaming && text" class="px-0">
|
||||
<CodeBlockCopyButton :value="text" />
|
||||
</MessageFooter>
|
||||
</MessageContent>
|
||||
</Message>
|
||||
</template>
|
||||
@@ -1,6 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from "vue";
|
||||
|
||||
import CodeView from "@/base/CodeView.vue";
|
||||
import ChatInline from "./ChatInline.vue";
|
||||
import { parseMarkdown } from "./markdown";
|
||||
|
||||
@@ -18,10 +19,7 @@ const blocks = computed(() => parseMarkdown(props.text));
|
||||
<template>
|
||||
<div class="[overflow-wrap:anywhere]">
|
||||
<template v-for="(block, i) in blocks" :key="i">
|
||||
<pre
|
||||
v-if="block.kind === 'pre'"
|
||||
class="my-2 overflow-auto rounded-md border border-border bg-background/60 p-3"
|
||||
><code class="font-mono text-[0.85em]">{{ block.code }}</code></pre>
|
||||
<CodeView v-if="block.kind === 'pre'" :code="block.code" :lang="block.lang" class="my-2" />
|
||||
|
||||
<table
|
||||
v-else-if="block.kind === 'table'"
|
||||
|
||||
@@ -21,7 +21,7 @@ export type ChatInline =
|
||||
export type ChatBlock =
|
||||
| { kind: "p" | "h1" | "h2" | "h3" | "blockquote"; inline: ChatInline[] }
|
||||
| { kind: "ul" | "ol"; items: ChatInline[][] }
|
||||
| { kind: "pre"; code: string }
|
||||
| { kind: "pre"; code: string; lang: string }
|
||||
| { kind: "table"; head: ChatInline[][]; rows: ChatInline[][][] };
|
||||
|
||||
const INLINE =
|
||||
@@ -67,6 +67,7 @@ export function parseMarkdown(text: string | null | undefined): ChatBlock[] {
|
||||
let paragraph: string[] = [];
|
||||
let list: { tag: "ul" | "ol"; items: string[] } | null = null;
|
||||
let code: string[] | null = null;
|
||||
let lang = "";
|
||||
|
||||
const flushParagraph = () => {
|
||||
if (paragraph.length) {
|
||||
@@ -89,10 +90,11 @@ export function parseMarkdown(text: string | null | undefined): ChatBlock[] {
|
||||
flushParagraph();
|
||||
flushList();
|
||||
if (code) {
|
||||
blocks.push({ kind: "pre", code: code.join("\n") });
|
||||
blocks.push({ kind: "pre", code: code.join("\n"), lang });
|
||||
code = null;
|
||||
} else {
|
||||
code = [];
|
||||
lang = line.slice(3).trim().split(/\s/)[0];
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -167,7 +169,7 @@ export function parseMarkdown(text: string | null | undefined): ChatBlock[] {
|
||||
}
|
||||
flushParagraph();
|
||||
flushList();
|
||||
if (code) blocks.push({ kind: "pre", code: code.join("\n") });
|
||||
if (code) blocks.push({ kind: "pre", code: code.join("\n"), lang });
|
||||
|
||||
return blocks;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user