|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { HTMLAttributes } from "vue"; |
| 3 | +import { computed, ref, watch } from "vue"; |
| 4 | +import { ChevronDown } from "@lucide/vue"; |
| 5 | +import { Button } from "@/components/ui/button"; |
| 6 | +import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible"; |
| 7 | +import { cn } from "@/lib/utils"; |
| 8 | +
|
| 9 | +export interface ReasoningDisclosureProps { |
| 10 | + /** Reasoning text supplied by the consuming app */ |
| 11 | + text: string; |
| 12 | + /** Whether the model is currently streaming reasoning content */ |
| 13 | + isStreaming?: boolean; |
| 14 | + /** Label shown while streaming */ |
| 15 | + streamingLabel?: string; |
| 16 | + /** Label shown after streaming is complete */ |
| 17 | + completeLabel?: string; |
| 18 | + /** Initial open state when not streaming */ |
| 19 | + defaultOpen?: boolean; |
| 20 | + /** Additional CSS classes */ |
| 21 | + class?: HTMLAttributes["class"]; |
| 22 | +} |
| 23 | +
|
| 24 | +const props = withDefaults(defineProps<ReasoningDisclosureProps>(), { |
| 25 | + isStreaming: false, |
| 26 | + streamingLabel: "Thinking...", |
| 27 | + completeLabel: "Thoughts", |
| 28 | + defaultOpen: false, |
| 29 | +}); |
| 30 | +
|
| 31 | +const open = ref(props.isStreaming || props.defaultOpen); |
| 32 | +
|
| 33 | +const lines = computed(() => cleanMarkdown(props.text).split("\n").filter(Boolean)); |
| 34 | +
|
| 35 | +watch(() => props.isStreaming, (isStreaming) => { |
| 36 | + if (isStreaming) { |
| 37 | + open.value = true; |
| 38 | + } |
| 39 | +}, { immediate: true }); |
| 40 | +
|
| 41 | +function cleanMarkdown(text: string): string { |
| 42 | + return text |
| 43 | + .replace(/\*\*(.+?)\*\*/g, "$1") |
| 44 | + .replace(/\*(.+?)\*/g, "$1") |
| 45 | + .replace(/`(.+?)`/g, "$1") |
| 46 | + .replace(/^#+\s+/gm, ""); |
| 47 | +} |
| 48 | +</script> |
| 49 | + |
| 50 | +<template> |
| 51 | + <Collapsible |
| 52 | + v-model:open="open" |
| 53 | + data-slot="reasoning-disclosure" |
| 54 | + :class="cn('relative min-w-0 w-full text-pretty *:first:mt-0 *:last:mb-0', props.class)" |
| 55 | + > |
| 56 | + <CollapsibleTrigger as-child> |
| 57 | + <Button |
| 58 | + type="button" |
| 59 | + class="group p-0! text-muted-foreground" |
| 60 | + variant="link" |
| 61 | + > |
| 62 | + {{ isStreaming ? streamingLabel : completeLabel }} |
| 63 | + <ChevronDown |
| 64 | + v-if="text.length > 0" |
| 65 | + class="size-4 transition-transform duration-200 group-data-[state=open]:rotate-180" |
| 66 | + aria-hidden="true" |
| 67 | + /> |
| 68 | + </Button> |
| 69 | + </CollapsibleTrigger> |
| 70 | + |
| 71 | + <CollapsibleContent> |
| 72 | + <div |
| 73 | + v-for="(line, index) in lines" |
| 74 | + :key="index" |
| 75 | + > |
| 76 | + <span class="whitespace-pre-wrap text-sm font-normal text-muted-foreground">{{ line }}</span> |
| 77 | + </div> |
| 78 | + </CollapsibleContent> |
| 79 | + </Collapsible> |
| 80 | +</template> |
0 commit comments