From cf13e63b58631905bcadabf2e101b8f0e2d5efc7 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 6 Jul 2026 18:11:59 -0500 Subject: [PATCH] refac --- .../components/chat/AssistantMessage.svelte | 77 ++++++- .../components/chat/ChatFilePreview.svelte | 196 ++++++++++++++++++ .../src/lib/components/chat/ChatInput.svelte | 23 ++ cptr/routers/chat.py | 15 ++ cptr/utils/chat_task.py | 10 + cptr/utils/tools.py | 75 +++++++ 6 files changed, 394 insertions(+), 2 deletions(-) create mode 100644 cptr/frontend/src/lib/components/chat/ChatFilePreview.svelte diff --git a/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte b/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte index 0a00b29..448dca5 100644 --- a/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte +++ b/cptr/frontend/src/lib/components/chat/AssistantMessage.svelte @@ -3,11 +3,14 @@ import { get } from 'svelte/store'; import MarkdownRenderer from '$lib/components/markdown/MarkdownRenderer.svelte'; import OutputEditView from './OutputEditView.svelte'; + import ChatFilePreview from './ChatFilePreview.svelte'; import ConsecutiveActivityGroup from './ConsecutiveActivityGroup.svelte'; import ReasoningCollapsible from './ReasoningCollapsible.svelte'; import ToolCallCollapsible from './ToolCallCollapsible.svelte'; import { currentWorkspace, openFileTab } from '$lib/stores'; import { ttsConfigured, ttsEnabled } from '$lib/stores/audio'; + import { tooltip } from '$lib/tooltip'; + import { fileIconName } from '$lib/utils/fileIcon'; import Icon from '../Icon.svelte'; import { t } from '$lib/i18n'; @@ -49,6 +52,7 @@ let editedOutput = $state(null); let copied = $state(false); let showUsageTooltip = $state(false); + let collapsedFiles = $state>({}); let textareaEl: HTMLTextAreaElement; async function startEdit() { @@ -138,6 +142,17 @@ return parts[parts.length - 1]; } + function resolvedFilePath(file: any): string { + const path = String(file?.full_path || file?.path || ''); + if (!path || path.startsWith('/')) return path; + const workspace = file?.workspace || get(currentWorkspace)?.path || ''; + return workspace ? `${workspace.replace(/\/$/, '')}/${path.replace(/^\/+/, '')}` : path; + } + + function toggleFile(key: string) { + collapsedFiles = { ...collapsedFiles, [key]: !collapsedFiles[key] }; + } + /** Human-readable label for a tool call */ function toolLabel(name: string, args: any): string { const _t = $t; @@ -159,6 +174,8 @@ return _t('chat.tool.createFile', { path: shortPath(args.path) }); case 'write_file': return _t('chat.tool.writeFile', { path: shortPath(args.path) }); + case 'display_file': + return `Display ${shortPath(args.path)}`; case 'list_directory': return args.recursive ? _t('chat.tool.listDirectoryRecursive', { path: shortPath(args.path) }) @@ -234,7 +251,13 @@ item: any; } - type DisplayItem = ActivityGroup | MessageItem | ArtifactItem | ImageItem; + interface FileItem { + type: 'file_item'; + item: any; + index: number; + } + + type DisplayItem = ActivityGroup | MessageItem | ArtifactItem | ImageItem | FileItem; const outputText = $derived.by((): string => { return (output || []) @@ -313,7 +336,7 @@ } }; - for (const item of output) { + for (const [index, item] of output.entries()) { if (item.type === 'function_call') { ensureGroup(); currentGroup!.entries.push(item); @@ -335,6 +358,9 @@ } else if (item.type === 'image') { flushGroup(); items.push({ type: 'image_item', item }); + } else if (item.type === 'file') { + flushGroup(); + items.push({ type: 'file_item', item, index }); } // function_call_output items are handled via outputMap, skip standalone render } @@ -469,6 +495,53 @@ {/each} + {:else if displayItem.type === 'file_item'} + {@const file = displayItem.item} + {@const filePath = resolvedFilePath(file)} + {@const fileKey = filePath || file.path || `file-${displayItem.index}`} + {@const collapsed = Boolean(collapsedFiles[fileKey])} +
+
+ + +
+ {#if !collapsed} + + {/if} +
{:else if displayItem.type === 'activity_group'} {#if displayItem.entries.length === 1} {@const item = displayItem.entries[0]} diff --git a/cptr/frontend/src/lib/components/chat/ChatFilePreview.svelte b/cptr/frontend/src/lib/components/chat/ChatFilePreview.svelte new file mode 100644 index 0000000..02864cb --- /dev/null +++ b/cptr/frontend/src/lib/components/chat/ChatFilePreview.svelte @@ -0,0 +1,196 @@ + + +{#if loading} +
Loading...
+{:else if error} +
{error}
+{:else if kind === 'image'} +
+ +
+{:else if kind === 'pdf'} +
+ +
+{:else if kind === 'office'} +
+ +
+{:else if kind === 'html' && content} +
+ +
+{:else if kind === 'svg' && content} +
+ +
+{:else if kind === 'markdown' && content} +
+ +
+{:else if kind === 'json' && jsonData !== null} +
+ +
+{:else if kind === 'csv' && content} +
+ +
+{:else if kind === 'text' && content} +
{content}
+{:else if kind === 'audio'} +
+ +
+{:else if kind === 'video'} +
+ + +
+{/if} + + diff --git a/cptr/frontend/src/lib/components/chat/ChatInput.svelte b/cptr/frontend/src/lib/components/chat/ChatInput.svelte index 12d0a89..c7efd5d 100644 --- a/cptr/frontend/src/lib/components/chat/ChatInput.svelte +++ b/cptr/frontend/src/lib/components/chat/ChatInput.svelte @@ -39,6 +39,7 @@ import Spinner from '$lib/components/common/Spinner.svelte'; import { t } from '$lib/i18n'; import { TAB_DRAG_MIME } from '$lib/constants'; + import { tooltip } from '$lib/tooltip'; // Keep screen awake during voice mode conversations let voiceWakeLock: WakeLockSentinel | null = null; @@ -1029,6 +1030,8 @@ {#if slashCommandIds.includes('compact')}