|
1 | 1 | <script lang="ts"> |
2 | 2 | import { Card } from '$lib/components/ui/card'; |
3 | | - import { User, Bot } from '@lucide/svelte'; |
| 3 | + import { Button } from '$lib/components/ui/button'; |
| 4 | + import { Tooltip, TooltipContent, TooltipTrigger } from '$lib/components/ui/tooltip'; |
| 5 | + import { User, Bot, Edit, Copy, Trash2, RefreshCw } from '@lucide/svelte'; |
| 6 | + import type { ChatRole } from '$lib/types/chat'; |
| 7 | + import type { ChatMessageData } from '$lib/types/chat'; |
| 8 | + import ThinkingSection from './ThinkingSection.svelte'; |
| 9 | + import MarkdownContent from './MarkdownContent.svelte'; |
| 10 | + import { parseThinkingContent } from '$lib/utils/thinking'; |
4 | 11 |
|
5 | | - let { class: className = '', message }: { class?: string; message: ChatMessageData } = $props(); |
| 12 | + interface Props { |
| 13 | + class?: string; |
| 14 | + message: ChatMessageData; |
| 15 | + onEdit?: (message: ChatMessageData) => void; |
| 16 | + onDelete?: (message: ChatMessageData) => void; |
| 17 | + onCopy?: (message: ChatMessageData) => void; |
| 18 | + onRegenerate?: (message: ChatMessageData) => void; |
| 19 | + } |
| 20 | +
|
| 21 | + let { |
| 22 | + class: className = '', |
| 23 | + message, |
| 24 | + onEdit, |
| 25 | + onDelete, |
| 26 | + onCopy, |
| 27 | + onRegenerate |
| 28 | + }: Props = $props(); |
| 29 | +
|
| 30 | + // Parse thinking content for assistant messages |
| 31 | + const parsedContent = $derived(() => { |
| 32 | + if (message.role === 'assistant') { |
| 33 | + const parsed = parseThinkingContent(message.content); |
| 34 | + return { |
| 35 | + thinking: message.thinking || parsed.thinking, |
| 36 | + content: parsed.cleanContent || message.content |
| 37 | + }; |
| 38 | + } |
| 39 | + return { thinking: null, content: message.content }; |
| 40 | + }); |
| 41 | +
|
| 42 | + // Handle copy to clipboard |
| 43 | + function handleCopy() { |
| 44 | + navigator.clipboard.writeText(message.content); |
| 45 | + onCopy?.(message); |
| 46 | + } |
| 47 | +
|
| 48 | + // Handle edit action |
| 49 | + function handleEdit() { |
| 50 | + onEdit?.(message); |
| 51 | + } |
| 52 | +
|
| 53 | + // Handle delete action |
| 54 | + function handleDelete() { |
| 55 | + onDelete?.(message); |
| 56 | + } |
| 57 | +
|
| 58 | + // Handle regenerate action |
| 59 | + function handleRegenerate() { |
| 60 | + onRegenerate?.(message); |
| 61 | + } |
6 | 62 | </script> |
7 | 63 |
|
8 | | -<div class="flex gap-3 {className} {message.role === 'user' ? 'justify-end' : 'justify-start'}"> |
9 | | - <!-- {#if message.role === 'assistant'} |
10 | | - <div |
11 | | - class="bg-background flex h-8 w-8 shrink-0 select-none items-center justify-center rounded-md border shadow" |
12 | | - > |
13 | | - <Bot class="h-4 w-4" /> |
14 | | - </div> |
15 | | - {/if} --> |
| 64 | +{#snippet messageActions(config?: { role: ChatRole })} |
| 65 | + <div |
| 66 | + class="pointer-events-none inset-0 flex items-center gap-1 opacity-0 transition-all duration-150 group-hover:pointer-events-auto group-hover:opacity-100" |
| 67 | + > |
| 68 | + <Tooltip> |
| 69 | + <TooltipTrigger> |
| 70 | + <Button variant="ghost" size="sm" class="h-6 w-6 p-0" onclick={handleCopy}> |
| 71 | + <Copy class="h-3 w-3" /> |
| 72 | + </Button> |
| 73 | + </TooltipTrigger> |
| 74 | + <TooltipContent> |
| 75 | + <p>Copy</p> |
| 76 | + </TooltipContent> |
| 77 | + </Tooltip> |
| 78 | + {#if config?.role === 'user'} |
| 79 | + <Tooltip> |
| 80 | + <TooltipTrigger> |
| 81 | + <Button variant="ghost" size="sm" class="h-6 w-6 p-0" onclick={handleEdit}> |
| 82 | + <Edit class="h-3 w-3" /> |
| 83 | + </Button> |
| 84 | + </TooltipTrigger> |
| 85 | + <TooltipContent> |
| 86 | + <p>Edit</p> |
| 87 | + </TooltipContent> |
| 88 | + </Tooltip> |
| 89 | + {:else if config?.role === 'assistant'} |
| 90 | + <Tooltip> |
| 91 | + <TooltipTrigger> |
| 92 | + <Button |
| 93 | + variant="ghost" |
| 94 | + size="sm" |
| 95 | + class="h-6 w-6 p-0" |
| 96 | + onclick={handleRegenerate} |
| 97 | + > |
| 98 | + <RefreshCw class="h-3 w-3" /> |
| 99 | + </Button> |
| 100 | + </TooltipTrigger> |
| 101 | + <TooltipContent> |
| 102 | + <p>Regenerate</p> |
| 103 | + </TooltipContent> |
| 104 | + </Tooltip> |
| 105 | + {/if} |
| 106 | + <!-- This will be implemented after fully migrating to SvelteKit --> |
16 | 107 |
|
17 | | - <Card |
18 | | - class="max-w-[80%] gap-2 px-4 py-3 {message.role === 'user' |
19 | | - ? 'bg-primary text-primary-foreground' |
20 | | - : 'bg-muted'}" |
| 108 | + <!-- <Tooltip> |
| 109 | + <TooltipTrigger> |
| 110 | + <Button |
| 111 | + variant="ghost" |
| 112 | + size="sm" |
| 113 | + class="text-destructive hover:text-destructive h-6 w-6 p-0" |
| 114 | + onclick={handleDelete} |
| 115 | + > |
| 116 | + <Trash2 class="h-3 w-3" /> |
| 117 | + </Button> |
| 118 | + </TooltipTrigger> |
| 119 | + <TooltipContent> |
| 120 | + <p>Delete</p> |
| 121 | + </TooltipContent> |
| 122 | + </Tooltip> --> |
| 123 | + </div> |
| 124 | + |
| 125 | + <div |
| 126 | + class="{config?.role === 'user' |
| 127 | + ? 'right-0' |
| 128 | + : 'left-0'} text-muted-foreground absolute text-xs transition-all duration-150 group-hover:pointer-events-none group-hover:opacity-0" |
21 | 129 | > |
22 | | - <div class="whitespace-pre-wrap text-sm"> |
23 | | - {message.content} |
| 130 | + {message.timestamp ? new Date(message.timestamp).toLocaleTimeString() : ''} |
| 131 | + </div> |
| 132 | +{/snippet} |
| 133 | + |
| 134 | +{#if message.role === 'user'} |
| 135 | + <div |
| 136 | + class="group flex flex-col items-end gap-2 {className}" |
| 137 | + role="group" |
| 138 | + aria-label="User message with actions" |
| 139 | + > |
| 140 | + <Card class="bg-primary text-primary-foreground max-w-[80%] rounded-2xl px-2.5 py-1.5"> |
| 141 | + <div class="text-md whitespace-pre-wrap"> |
| 142 | + {message.content} |
| 143 | + </div> |
| 144 | + </Card> |
| 145 | + |
| 146 | + <div class="relative flex h-6 items-center"> |
| 147 | + {@render messageActions({ role: 'user' })} |
24 | 148 | </div> |
25 | | - {#if message.timestamp} |
26 | | - <div class="text-xs opacity-70"> |
27 | | - {new Date(message.timestamp).toLocaleTimeString()} |
| 149 | + </div> |
| 150 | +{:else} |
| 151 | + <div |
| 152 | + class="text-md leading-7.5 group w-full {className}" |
| 153 | + role="group" |
| 154 | + aria-label="Assistant message with actions" |
| 155 | + > |
| 156 | + {#if parsedContent().thinking} |
| 157 | + <ThinkingSection thinking={parsedContent().thinking || ''} /> |
| 158 | + {/if} |
| 159 | + {#if message.role === 'assistant'} |
| 160 | + <MarkdownContent content={parsedContent().content} /> |
| 161 | + {:else} |
| 162 | + <div class="whitespace-pre-wrap text-sm"> |
| 163 | + {parsedContent().content} |
28 | 164 | </div> |
29 | 165 | {/if} |
30 | | - </Card> |
31 | 166 |
|
32 | | - <!-- {#if message.role === 'user'} |
33 | | - <div |
34 | | - class="bg-background flex h-8 w-8 shrink-0 select-none items-center justify-center rounded-md border shadow" |
35 | | - > |
36 | | - <User class="h-4 w-4" /> |
37 | | - </div> |
38 | | - {/if} --> |
39 | | -</div> |
| 167 | + {#if message.timestamp} |
| 168 | + <div class="relative mt-2 flex h-6 items-center"> |
| 169 | + {@render messageActions({ role: 'assistant' })} |
| 170 | + </div> |
| 171 | + {/if} |
| 172 | + </div> |
| 173 | +{/if} |
0 commit comments