diff --git a/cptr/frontend/src/lib/apis/chat.ts b/cptr/frontend/src/lib/apis/chat.ts index 8fdad08..ad4a84d 100644 --- a/cptr/frontend/src/lib/apis/chat.ts +++ b/cptr/frontend/src/lib/apis/chat.ts @@ -66,7 +66,7 @@ export const sendMessage = ( parentId?: string | null, params: { tool_approval_mode?: string } = {}, regenerationPrompt?: string, - files?: string[] + files?: { id: string; name: string; url: string; type: string }[] ) => fetchJSON( '/api/chats', diff --git a/cptr/frontend/src/lib/apis/files.ts b/cptr/frontend/src/lib/apis/files.ts index b382a7a..525bf0d 100644 --- a/cptr/frontend/src/lib/apis/files.ts +++ b/cptr/frontend/src/lib/apis/files.ts @@ -30,6 +30,9 @@ export const createEntry = (path: string, type: 'file' | 'directory') => export const uploadFiles = (path: string, form: FormData) => fetchHandler('/api/workspace/files/upload', { method: 'POST', body: form }); +export const uploadFile = (form: FormData) => + fetchJSON<{ id: string; url: string }>('/api/files', { method: 'POST', body: form }); + export const downloadArchive = (paths: string[]) => fetchHandler('/api/workspace/files/archive', { method: 'POST', ...jsonBody({ paths }) }); diff --git a/cptr/frontend/src/lib/components/chat/ChatInput.svelte b/cptr/frontend/src/lib/components/chat/ChatInput.svelte index dc950f3..0dbcbf4 100644 --- a/cptr/frontend/src/lib/components/chat/ChatInput.svelte +++ b/cptr/frontend/src/lib/components/chat/ChatInput.svelte @@ -11,11 +11,13 @@ import { createFileMention, extractMentionedFiles, type FileMentionAttrs } from './FileMention'; import FileSuggestionPopup from './FileSuggestionPopup.svelte'; import { searchFiles } from '$lib/apis/files'; + import { uploadFile } from '$lib/apis/files'; import ModelSelector from './ModelSelector.svelte'; import SendButton from './SendButton.svelte'; import PlusMenu from './PlusMenu.svelte'; import DictateButton from './DictateButton.svelte'; import QueuedMessageItem from './QueuedMessageItem.svelte'; + import Icon from '../Icon.svelte'; interface Props { inputText: string; @@ -57,6 +59,63 @@ return _origHighlight(lang, value, opts); }; + // ── File Uploads ──────────────────────────────── + let attachedUploads = $state<{ id: string; name: string; url: string; type: string; loading?: boolean }[]>([]); + let isDragging = $state(false); + + async function processFiles(files: File[]) { + for (const file of files) { + const id = Math.random().toString(36).substring(7); + const isImage = file.type.startsWith('image/'); + const type = isImage ? 'image' : 'file'; + attachedUploads = [...attachedUploads, { id, name: file.name, url: '', type, loading: true }]; + + try { + const form = new FormData(); + form.append('file', file); + const res = await uploadFile(form); + if (res && res.id) { + attachedUploads = attachedUploads.map(u => + u.id === id ? { ...u, id: res.id, url: res.url, loading: false } : u + ); + } else { + attachedUploads = attachedUploads.filter(u => u.id !== id); + } + } catch (err) { + console.error("Upload failed", err); + attachedUploads = attachedUploads.filter(u => u.id !== id); + } + } + } + + function handleDrop(e: DragEvent) { + e.preventDefault(); + isDragging = false; + if (e.dataTransfer?.files) { + processFiles(Array.from(e.dataTransfer.files)); + } + } + + function handlePaste(e: ClipboardEvent) { + if (e.clipboardData?.items) { + const files: File[] = []; + for (const item of Array.from(e.clipboardData.items)) { + if (item.kind === 'file') { + const file = item.getAsFile(); + if (file) files.push(file); + } + } + if (files.length > 0) { + e.preventDefault(); // Stop TipTap from inserting base64 strings + processFiles(files); + } + } + } + + function removeUpload(id: string) { + attachedUploads = attachedUploads.filter(u => u.id !== id); + } + // ── @file mention suggestion ──────────────────── let popupEl: HTMLDivElement | null = null; let popupComponent: Record | null = null; @@ -290,17 +349,26 @@ // TipTap auto-sizes; no-op kept for API compat } - /** Get file paths from @mentions in the current editor content. */ - export function getFiles(): string[] { - if (!editor) return []; - return extractMentionedFiles(editor.getJSON()); + export function getFiles(): any[] { + return attachedUploads.filter(u => !u.loading); + } + + export function clearUploads() { + attachedUploads = []; } // Allow sending during streaming (message will be enqueued server-side) const canSend = $derived(inputText.trim() && selectedModel && !sending); -
+
{ e.preventDefault(); isDragging = true; }} + ondragleave={() => { isDragging = false; }} + role="presentation" +> {#if queuedMessages.length > 0}
+ + {#if attachedUploads.length > 0} +
+ {#each attachedUploads as upload} +
+ {#if upload.loading} +
+
+
+ {:else if upload.type === 'image'} + {upload.name} + {:else} +
+
+ +
+
+
+
{upload.name}
+
{upload.type === 'file' ? 'File' : upload.type}
+
+
+
+ {/if} + +
+ +
+
+ {/each} +
+ {/if}
{ - /* TODO: handle file uploads */ + if (files) processFiles(Array.from(files)); }} />
diff --git a/cptr/frontend/src/lib/components/chat/ChatPanel.svelte b/cptr/frontend/src/lib/components/chat/ChatPanel.svelte index 02724d2..1ea0447 100644 --- a/cptr/frontend/src/lib/components/chat/ChatPanel.svelte +++ b/cptr/frontend/src/lib/components/chat/ChatPanel.svelte @@ -473,6 +473,7 @@ (_: string, id: string, label: string) => `[${label}](file://${id})` ); inputText = ''; + chatInputEl?.clearUploads(); autoScroll = true; await tick(); chatInputEl?.resetHeight(); @@ -876,6 +877,7 @@ {#if msg.role === 'user'} handleNavigate(msg.id, dir)} diff --git a/cptr/frontend/src/lib/components/chat/UserMessage.svelte b/cptr/frontend/src/lib/components/chat/UserMessage.svelte index f4b36b6..1492274 100644 --- a/cptr/frontend/src/lib/components/chat/UserMessage.svelte +++ b/cptr/frontend/src/lib/components/chat/UserMessage.svelte @@ -6,12 +6,13 @@ interface Props { content: string; + meta?: Record | null; siblingIndex?: number; siblingTotal?: number; onedit?: (content: string, submit: boolean) => void; onnavigate?: (direction: -1 | 1) => void; } - let { content, siblingIndex = 0, siblingTotal = 1, onedit, onnavigate }: Props = $props(); + let { content, meta = null, siblingIndex = 0, siblingTotal = 1, onedit, onnavigate }: Props = $props(); type Segment = { type: 'text'; value: string } | { type: 'file'; label: string; path: string }; @@ -153,6 +154,27 @@ />{segment.label}{/if}{/each}
+ {#if meta?.files?.length > 0} +
+ {#each meta.files as upload} + {#if upload.type === 'image'} + {upload.name + {:else} +
+
+ +
+
+
+
{upload.name || 'File'}
+
{upload.type === 'file' ? 'File' : (upload.type || 'File')}
+
+
+
+ {/if} + {/each} +
+ {/if}
{#if siblingTotal > 1 || onedit} diff --git a/cptr/routers/chat.py b/cptr/routers/chat.py index 24253d1..15f17aa 100644 --- a/cptr/routers/chat.py +++ b/cptr/routers/chat.py @@ -312,7 +312,7 @@ class SendMessageRequest(BaseModel): chat_id: Optional[str] = None parent_id: Optional[str] = None regeneration_prompt: Optional[str] = None - files: List[str] = [] + files: List[dict] = [] params: dict = {} diff --git a/cptr/routers/workspace.py b/cptr/routers/workspace.py index eafd66a..3d14cb6 100644 --- a/cptr/routers/workspace.py +++ b/cptr/routers/workspace.py @@ -328,19 +328,20 @@ async def search_files( path: str = Query(..., description="Root path to search"), limit: int = Query(20, description="Max results"), ): - """Fuzzy filename search. Walks tree, matches basename case-insensitively. + """Fuzzy file search. Walks tree, matching basename and relative path case-insensitively. - Results are ranked: exact match > starts-with > contains. - Within each tier, shorter names rank higher (more specific match). + Results are ranked by where/how the query matches: exact name > name prefix > + exact path > path prefix > name contains > path contains. Within each tier, + shorter paths rank higher (more specific match). """ root = Path(path).resolve() if not root.exists() or not root.is_dir(): raise HTTPException(status_code=404, detail=f"Path not found: {path}") - query_lower = query.lower() + query_lower = query.strip().lower().replace("\\", "/") # Collect all matches first, then rank - matches: list[tuple[int, int, SearchResult]] = [] # (score, name_len, result) + matches: list[tuple[int, int, SearchResult]] = [] # (score, path_len, result) max_collect = limit * 10 # collect more than needed for ranking def walk(directory: Path, depth: int = 0): diff --git a/cptr/utils/ai.py b/cptr/utils/ai.py index e0f982b..45eb39d 100644 --- a/cptr/utils/ai.py +++ b/cptr/utils/ai.py @@ -148,7 +148,23 @@ def _to_anthropic_messages(messages: list[dict]) -> list[dict]: role = m["role"] if role == "system": continue # system goes in body.system + content = m.get("content", "") + if isinstance(content, list): + formatted_content = [] + for block in content: + if block.get("type") == "text": + formatted_content.append({"type": "text", "text": block.get("text", "")}) + elif block.get("type") == "image": + formatted_content.append({ + "type": "image", + "source": { + "type": "base64", + "media_type": block.get("media_type", "image/jpeg"), + "data": block.get("base64", "") + } + }) + content = formatted_content if role == "tool": # tool result → Anthropic tool_result block result.append( @@ -283,7 +299,24 @@ def _to_openai_messages(messages: list[dict], instructions: str) -> list[dict]: for m in messages: if m["role"] == "system": continue - result.append(m) + + content = m.get("content", "") + if isinstance(content, list): + formatted_content = [] + for block in content: + if block.get("type") == "text": + formatted_content.append({"type": "text", "text": block.get("text", "")}) + elif block.get("type") == "image": + data_uri = f"data:{block.get('media_type', 'image/jpeg')};base64,{block.get('base64', '')}" + formatted_content.append({ + "type": "image_url", + "image_url": {"url": data_uri} + }) + new_m = dict(m) + new_m["content"] = formatted_content + result.append(new_m) + else: + result.append(m) return result @@ -407,7 +440,22 @@ def _to_responses_input(messages: list[dict], instructions: str) -> list[dict]: } ) else: - items.append({"role": role, "content": m.get("content", "")}) + content = m.get("content", "") + if isinstance(content, list): + formatted_content = [] + for block in content: + if block.get("type") == "text": + formatted_content.append({"type": "input_text", "text": block.get("text", "")}) + elif block.get("type") == "image": + # Not all models support input_image, but this is the Responses API spec + data_uri = f"data:{block.get('media_type', 'image/jpeg')};base64,{block.get('base64', '')}" + formatted_content.append({ + "type": "input_image", + "image_url": data_uri + }) + items.append({"role": role, "content": formatted_content}) + else: + items.append({"role": role, "content": content}) return items diff --git a/cptr/utils/chat_task.py b/cptr/utils/chat_task.py index ecd9a21..2a7cc64 100644 --- a/cptr/utils/chat_task.py +++ b/cptr/utils/chat_task.py @@ -375,18 +375,58 @@ async def _load_message_history(chat_id: str, message_id: str) -> list[dict]: continue entry: dict = {"role": m.role, "content": m.content or ""} - # Inject @file mention contents into user messages + # Transform uploaded images into base64 multimodal blocks; inline text files if m.role == "user": attached_files = (m.meta or {}).get("files", []) - if attached_files: - file_parts = [] - for fpath in attached_files: - try: - text = Path(fpath).read_text(errors="replace")[:50_000] - file_parts.append(f"--- {fpath} ---\n{text}") - except Exception: - file_parts.append(f"--- {fpath} --- (unreadable)") - entry["content"] += "\n\n" + "\n\n".join(file_parts) + images = [ + f for f in attached_files + if isinstance(f, dict) and (f.get("type") == "image" or (f.get("content_type") or "").startswith("image/")) + ] + non_images = [ + f for f in attached_files + if isinstance(f, dict) and f not in images + ] + + if images or non_images: + from cptr.utils.storage import get_storage + import base64 + + text_content = entry["content"] + + # Append file:// references so the AI can read them with view_file + if non_images: + from cptr.utils.storage import UPLOADS_DIR + file_refs = [] + for f in non_images: + file_id = f.get("id") + if not file_id: + continue + name = f.get("name", "file") + file_path = UPLOADS_DIR / file_id + file_refs.append(f"[{name}](file://{file_path})") + if file_refs: + text_content += "\n\nAttached files:\n" + "\n".join(file_refs) + + content_blocks = [{"type": "text", "text": text_content}] if text_content else [] + + for img in images: + file_id = img.get("id") + if not file_id: + continue + data = await get_storage().get(file_id) + if data: + b64_str = base64.b64encode(data).decode("utf-8") + ctype = img.get("content_type") or "image/png" + content_blocks.append({ + "type": "image", + "media_type": ctype, + "base64": b64_str + }) + + if len(content_blocks) > (1 if text_content else 0): + entry["content"] = content_blocks + elif text_content != entry["content"]: + entry["content"] = text_content # Reconstruct tool calls from output items for the provider if m.output: