This commit is contained in:
Timothy Jaeryang Baek
2026-06-06 13:45:43 +01:00
Unverified
parent 147252a449
commit e8003e6399
9 changed files with 251 additions and 26 deletions
+1 -1
View File
@@ -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<SendMessageResult>(
'/api/chats',
+3
View File
@@ -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 }) });
@@ -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<string, any> | 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);
</script>
<div class="relative">
<div
class="relative {isDragging ? 'ring-2 ring-blue-500 rounded-3xl' : ''}"
ondrop={handleDrop}
onpaste={handlePaste}
ondragover={(e) => { e.preventDefault(); isDragging = true; }}
ondragleave={() => { isDragging = false; }}
role="presentation"
>
<!-- Queued messages (above input, matching open-webui layout) -->
{#if queuedMessages.length > 0}
<div
@@ -321,6 +389,47 @@
<div
class="rounded-3xl shadow-lg border border-gray-100/40 dark:border-white/4 focus-within:border-gray-200/50 focus-within:dark:border-white/8 transition px-1 bg-white dark:bg-gray-500/5"
>
<!-- Uploaded Files Preview -->
{#if attachedUploads.length > 0}
<div class="mx-2 pt-2 flex flex-wrap gap-2">
{#each attachedUploads as upload}
<div class="relative group flex-shrink-0">
{#if upload.loading}
<div class="flex items-center justify-center size-8 bg-gray-50 dark:bg-white/5 rounded-xl border border-gray-200 dark:border-white/10 shadow-sm">
<div class="w-4 h-4 border-2 border-blue-500 border-t-transparent rounded-full animate-spin"></div>
</div>
{:else if upload.type === 'image'}
<img src={upload.url} alt={upload.name} class="size-8 object-cover rounded-xl border border-gray-200 dark:border-white/10 shadow-sm" />
{:else}
<div class="relative group px-2 w-48 h-8 flex items-center gap-1.5 bg-white dark:bg-[#1a1a1a] border border-gray-100 dark:border-white/5 rounded-xl text-left flex-shrink-0 shadow-sm">
<div class="shrink-0">
<Icon name="page-text" size={14} class="text-gray-500 dark:text-gray-400" />
</div>
<div class="flex flex-col justify-center w-full overflow-hidden">
<div class="dark:text-gray-100 text-xs flex justify-between items-center w-full gap-2">
<div class="font-medium truncate flex-1">{upload.name}</div>
<div class="text-[10px] text-gray-500 capitalize shrink-0">{upload.type === 'file' ? 'File' : upload.type}</div>
</div>
</div>
</div>
{/if}
<div class="absolute -top-1.5 -right-1.5 z-10">
<button
class="bg-white text-black border border-white rounded-full group-hover:visible invisible transition outline-none shadow-sm flex items-center justify-center"
type="button"
onclick={() => removeUpload(upload.id)}
aria-label="Remove upload"
>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="size-3.5" aria-hidden="true">
<path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"/>
</svg>
</button>
</div>
</div>
{/each}
</div>
{/if}
<!-- Editor area -->
<div class="px-2.5">
<div
@@ -340,7 +449,7 @@
<div class="ml-0.5 self-end flex items-center">
<PlusMenu
onfiles={(files) => {
/* TODO: handle file uploads */
if (files) processFiles(Array.from(files));
}}
/>
</div>
@@ -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'}
<UserMessage
content={msg.content}
meta={msg.meta}
{siblingIndex}
siblingTotal={siblingIds.length}
onnavigate={(dir) => handleNavigate(msg.id, dir)}
@@ -6,12 +6,13 @@
interface Props {
content: string;
meta?: Record<string, any> | 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}</button
>{/if}{/each}
</div>
{#if meta?.files?.length > 0}
<div class="flex flex-wrap gap-2 mt-3">
{#each meta.files as upload}
{#if upload.type === 'image'}
<img src={upload.url} alt={upload.name || 'image'} class="max-w-[240px] max-h-[240px] rounded-xl object-cover shadow-sm border border-gray-100 dark:border-white/5" />
{:else}
<div class="relative group py-1.5 px-2 w-48 flex items-center gap-1.5 bg-white dark:bg-[#1a1a1a] border border-gray-100 dark:border-white/5 rounded-xl text-left flex-shrink-0 shadow-sm">
<div class="shrink-0">
<Icon name="page-text" size={14} class="text-gray-500 dark:text-gray-400" />
</div>
<div class="flex flex-col justify-center w-full overflow-hidden">
<div class="dark:text-gray-100 text-xs flex justify-between items-center w-full gap-2">
<div class="font-medium truncate flex-1">{upload.name || 'File'}</div>
<div class="text-[10px] text-gray-500 capitalize shrink-0">{upload.type === 'file' ? 'File' : (upload.type || 'File')}</div>
</div>
</div>
</div>
{/if}
{/each}
</div>
{/if}
</div>
</div>
{#if siblingTotal > 1 || onedit}
+1 -1
View File
@@ -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 = {}
+6 -5
View File
@@ -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):
+50 -2
View File
@@ -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
+50 -10
View File
@@ -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: