This commit is contained in:
Timothy Jaeryang Baek committed 2026-06-18 16:58:12 +02:00
1 parent b015269e52
commit d6670791d1
10 files changed
+557 -19

No files matched your search

+26 -1
View File
@@ -28,9 +28,18 @@ export interface ChatInfo {
is_active?: boolean;
}
export interface ContextUsage {
tokens: number;
estimated_tokens: number;
threshold: number;
percent: number;
source: 'estimated';
}
export interface ChatDetail {
chat: ChatInfo;
messages: ChatMessageRow[];
context_usage?: ContextUsage | null;
}
export interface SendMessageResult {
@@ -48,6 +57,16 @@ export interface ChatSendParams {
voice_mode?: boolean;
}
export interface CompactChatResult {
ok: boolean;
compacted: boolean;
reason?: string;
dropped_messages?: number;
kept_messages?: number;
summary_chars?: number;
context_usage?: ContextUsage | null;
}
// ── Queries ─────────────────────────────────────────────────
export const getChats = (
@@ -61,7 +80,10 @@ export const getChats = (
`/api/chats?workspace=${encodeURIComponent(workspace)}&limit=${limit}&offset=${offset}&sort_by=${sortBy}&sort_dir=${sortDir}`
);
export const getChat = (chatId: string) => fetchJSON<ChatDetail>(`/api/chats/${chatId}`);
export const getChat = (chatId: string, modelId?: string) => {
const suffix = modelId ? `?model_id=${encodeURIComponent(modelId)}` : '';
return fetchJSON<ChatDetail>(`/api/chats/${chatId}${suffix}`);
};
export const deleteChat = (chatId: string) =>
fetchJSON<{ ok: boolean }>(`/api/chats/${chatId}`, { method: 'DELETE' });
@@ -106,6 +128,9 @@ export const approveToolCall = (
export const cancelTask = (chatId: string, messageId: string) =>
fetchJSON(`/api/chats/${chatId}/messages/${messageId}/cancel`, { method: 'POST' });
export const compactChat = (chatId: string, modelId: string) =>
fetchJSON<CompactChatResult>(`/api/chats/${chatId}/compact`, jsonBody({ model_id: modelId }));
export const updateCurrentMessage = (chatId: string, messageId: string) =>
fetchJSON<{ ok: boolean }>(`/api/chats/${chatId}/current`, jsonBody({ message_id: messageId }));
@@ -58,8 +58,6 @@
const DEFAULT_PROMPT_PLACEHOLDER = `You are a helpful coding assistant. You have access to tools to read, search, and modify files in the workspace. Use them to help the user with their coding tasks.
For complex tasks, create an implementation plan first using create_artifact. Then wait for an explicit approval message before using tools or implementing.
{{INSTRUCTIONS}}
Workspace: {{WORKSPACE_NAME}}
@@ -16,6 +16,7 @@
import { searchFiles } from '$lib/apis/files';
import { getSkills } from '$lib/apis/skills';
import { uploadFile } from '$lib/apis/files';
import type { ContextUsage } from '$lib/apis/chat';
import ModelSelector from '../common/ModelSelector.svelte';
import SendButton from './SendButton.svelte';
import PlusMenu from './PlusMenu.svelte';
@@ -59,8 +60,12 @@
streaming?: boolean;
workspace?: string;
placeholder?: string;
contextUsage?: ContextUsage | null;
queuedMessages?: { id: string; content: string }[];
onsend: () => void;
oncompact?: () => void;
onplan?: () => void;
onstatus?: () => void;
oncancel?: () => void;
onqueuesendnow?: (id: string) => void;
onqueueedit?: (id: string) => void;
@@ -73,8 +78,12 @@
streaming = false,
workspace = '',
placeholder = 'Message...',
contextUsage = null,
queuedMessages = [],
onsend,
oncompact,
onplan,
onstatus,
oncancel,
onqueuesendnow,
onqueueedit,
@@ -94,6 +103,7 @@
let voiceCaptureStream: MediaStream | null = null;
let voiceCaptureChunks: Blob[] = [];
let voiceCaptureMimeType = 'audio/webm';
let selectedSlashCommandIndex = $state(0);
const voiceModeAvailable = $derived(
$ttsEnabled && $ttsConfigured && ($voiceModeSttMode === 'browser' || $sttConfigured)
);
@@ -503,6 +513,26 @@
spellcheck: 'true'
},
handleKeyDown: (view, event) => {
if (showSlashCommands) {
if (event.key === 'ArrowDown') {
event.preventDefault();
selectedSlashCommandIndex =
(selectedSlashCommandIndex + 1) % Math.max(slashCommandIds.length, 1);
return true;
}
if (event.key === 'ArrowUp') {
event.preventDefault();
selectedSlashCommandIndex =
(selectedSlashCommandIndex - 1 + slashCommandIds.length) %
Math.max(slashCommandIds.length, 1);
return true;
}
if (event.key === 'Enter') {
event.preventDefault();
runSlashCommand(slashCommandIds[selectedSlashCommandIndex]);
return true;
}
}
if (event.key === 'Enter' && !event.shiftKey) {
// Don't send while suggestion popup is open — let it confirm selection
if (popupComponent || skillPopupComponent) return false;
@@ -519,7 +549,7 @@
}
if (!inside) {
event.preventDefault();
onsend();
handleSubmit();
return true;
}
}
@@ -787,7 +817,7 @@
}
inputText = text;
await tick();
onsend();
handleSubmit();
};
recognition.onerror = (event: any) => {
@@ -861,6 +891,47 @@
}
});
const slashCommandQuery = $derived(inputText.trim().toLowerCase());
const slashCommandIds = $derived.by(() => {
if (!slashCommandQuery.startsWith('/')) return [];
const ids: string[] = [];
if (oncompact && '/compact'.startsWith(slashCommandQuery)) ids.push('compact');
if (onplan && '/plan'.startsWith(slashCommandQuery)) ids.push('plan');
if (onstatus && '/status'.startsWith(slashCommandQuery)) ids.push('status');
return ids;
});
const showSlashCommands = $derived(slashCommandIds.length > 0);
const contextPercent = $derived(Math.max(0, Math.round(contextUsage?.percent ?? 0)));
const contextCirclePercent = $derived(Math.min(contextPercent, 100));
const contextCircleOffset = $derived(50.27 * (1 - contextCirclePercent / 100));
$effect(() => {
if (selectedSlashCommandIndex >= slashCommandIds.length) selectedSlashCommandIndex = 0;
});
function runSlashCommand(commandId: string | undefined) {
if (commandId === 'compact' && oncompact) {
inputText = '';
oncompact();
return;
}
if (commandId === 'plan' && onplan) {
inputText = '';
onplan();
return;
}
if (commandId === 'status' && onstatus) {
inputText = '';
onstatus();
return;
}
onsend();
}
function handleSubmit() {
runSlashCommand(slashCommandIds[0]);
}
// Allow sending during streaming (message will be enqueued server-side)
const canSend = $derived(!!(inputText.trim() && selectedModel && !sending));
</script>
@@ -890,6 +961,136 @@
</div>
{/if}
{#if showSlashCommands}
<div
class="absolute left-2 bottom-full mb-1 z-50 w-60 max-h-40 overflow-y-auto rounded-xl bg-white dark:bg-[#1a1a1a] border border-gray-150 dark:border-white/6 shadow-xl p-0.5"
>
<div class="mb-0.5 px-2 pt-1 pb-0.5 text-[10px] leading-none text-gray-400 dark:text-gray-600">
Commands
</div>
{#if slashCommandIds.includes('compact')}
<button
type="button"
class="flex items-center gap-2 w-full h-6 px-2 rounded-xl text-xs text-left transition-colors duration-75
{slashCommandIds[selectedSlashCommandIndex] === 'compact'
? 'bg-gray-50 dark:bg-white/5 text-gray-900 dark:text-white'
: 'text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-white/5 hover:text-gray-900 dark:hover:text-white'} disabled:opacity-50"
disabled={sending || streaming}
onmousedown={(e) => e.preventDefault()}
onclick={() => {
runSlashCommand('compact');
}}
onmouseenter={() => (selectedSlashCommandIndex = slashCommandIds.indexOf('compact'))}
>
<span class="flex items-center justify-center w-4 shrink-0 text-gray-400 dark:text-gray-500">
<svg class="size-3.5 -rotate-90" viewBox="0 0 20 20" aria-hidden="true">
<circle
cx="10"
cy="10"
r="8"
fill="none"
stroke="currentColor"
stroke-width="2"
class="opacity-20"
/>
<circle
cx="10"
cy="10"
r="8"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-dasharray="50.27"
style={`stroke-dashoffset: ${contextCircleOffset};`}
/>
</svg>
</span>
<span class="flex-1 min-w-0 flex items-baseline gap-1.5 overflow-hidden">
<span class="truncate">{$t('chat.commandCompact')}</span>
<span class="text-[10px] text-gray-400 dark:text-gray-600 truncate shrink-0">
{$t('chat.commandCompactPercent', { percent: contextPercent })}
</span>
</span>
</button>
{/if}
{#if slashCommandIds.includes('plan')}
<button
type="button"
class="flex items-center gap-2 w-full h-6 px-2 rounded-xl text-xs text-left transition-colors duration-75
{slashCommandIds[selectedSlashCommandIndex] === 'plan'
? 'bg-gray-50 dark:bg-white/5 text-gray-900 dark:text-white'
: 'text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-white/5 hover:text-gray-900 dark:hover:text-white'}"
onmousedown={(e) => e.preventDefault()}
onclick={() => {
runSlashCommand('plan');
}}
onmouseenter={() => (selectedSlashCommandIndex = slashCommandIds.indexOf('plan'))}
>
<span class="flex items-center justify-center w-4 shrink-0 text-gray-400 dark:text-gray-500">
<svg
class="size-3.5"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.75"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
<polyline points="14 2 14 8 20 8" />
<line x1="9" y1="13" x2="15" y2="13" />
<line x1="9" y1="17" x2="15" y2="17" />
</svg>
</span>
<span class="flex-1 min-w-0 flex items-baseline gap-1.5 overflow-hidden">
<span class="truncate">{$t('chat.commandPlan')}</span>
<span class="text-[10px] text-gray-400 dark:text-gray-600 truncate shrink-0">
{$planMode ? $t('chat.commandPlanOn') : $t('chat.commandPlanOff')}
</span>
</span>
</button>
{/if}
{#if slashCommandIds.includes('status')}
<button
type="button"
class="flex items-center gap-2 w-full h-6 px-2 rounded-xl text-xs text-left transition-colors duration-75
{slashCommandIds[selectedSlashCommandIndex] === 'status'
? 'bg-gray-50 dark:bg-white/5 text-gray-900 dark:text-white'
: 'text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-white/5 hover:text-gray-900 dark:hover:text-white'}"
onmousedown={(e) => e.preventDefault()}
onclick={() => {
runSlashCommand('status');
}}
onmouseenter={() => (selectedSlashCommandIndex = slashCommandIds.indexOf('status'))}
>
<span class="flex items-center justify-center w-4 shrink-0 text-gray-400 dark:text-gray-500">
<svg
class="size-3.5"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.75"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M12 14l4-4" />
<path d="M3.34 19a10 10 0 1 1 17.32 0" />
</svg>
</span>
<span class="flex-1 min-w-0 flex items-baseline gap-1.5 overflow-hidden">
<span class="truncate">{$t('chat.commandStatus')}</span>
<span class="text-[10px] text-gray-400 dark:text-gray-600 truncate shrink-0">
{$t('chat.commandStatusDesc')}
</span>
</span>
</button>
{/if}
</div>
{/if}
<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"
>
@@ -1014,7 +1215,7 @@
<SendButton
{canSend}
{streaming}
{onsend}
onsend={handleSubmit}
{oncancel}
onvoice={voiceModeAvailable ? toggleVoiceMode : undefined}
voiceActive={$voiceModeEnabled || voiceListening}
@@ -6,6 +6,7 @@
sendMessage as apiSendMessage,
approveToolCall,
cancelTask,
compactChat as apiCompactChat,
updateCurrentMessage,
updateMessage,
createMessage,
@@ -13,7 +14,8 @@
queueDelete as apiQueueDelete,
type ChatMessageRow,
type ChatSendParams,
type ChatInfo
type ChatInfo,
type ContextUsage
} from '$lib/apis/chat';
import {
chatModels,
@@ -49,6 +51,7 @@
import UserMessage from './UserMessage.svelte';
import AssistantMessage from './AssistantMessage.svelte';
import ChatHistory from './ChatHistory.svelte';
import StatusModal from './StatusModal.svelte';
import Spinner from '../common/Spinner.svelte';
import { toast } from 'svelte-sonner';
import { t } from '$lib/i18n';
@@ -71,6 +74,8 @@
let selectedModel = $state('');
let allMessages = $state<ChatMessageRow[]>([]);
let currentMessageId = $state<string | null>(null);
let contextUsage = $state<ContextUsage | null>(null);
let showStatusModal = $state(false);
let previousChats = $state<ChatInfo[]>([]);
let messagesEl: HTMLDivElement;
let chatInputEl: ChatInput;
@@ -277,11 +282,12 @@
const savedScroll = !isInitialLoad && messagesEl ? messagesEl.scrollTop : -1;
try {
const data = await getChat(id);
const data = await getChat(id, selectedModel || undefined);
// Discard stale response if a newer loadChat was called while we waited
if (gen !== loadGeneration) return;
allMessages = data.messages;
currentMessageId = data.chat.current_message_id;
contextUsage = data.context_usage ?? null;
// Update tab label with the real title from the DB
if (tabId && data.chat.title) {
chatTitle = data.chat.title;
@@ -613,6 +619,20 @@
let text = inputText.trim();
if (!text || !selectedModel) return;
if (sending) return;
if (text === '/compact') {
await handleManualCompact();
return;
}
if (text === '/plan') {
handlePlanCommand();
inputText = '';
return;
}
if (text === '/status') {
handleStatusCommand();
inputText = '';
return;
}
stopTtsPlayback();
if (shouldStreamTts()) void unlockTtsAudioPlayback();
sending = true;
@@ -747,6 +767,40 @@
}
}
async function handleManualCompact() {
if (!chatId) {
toast.message($t('chat.compactNoChat'));
return;
}
if (!selectedModel || sending || streaming) return;
sending = true;
inputText = '';
const toastId = toast.loading($t('chat.compacting'));
try {
const result = await apiCompactChat(chatId, selectedModel);
contextUsage = result.context_usage ?? contextUsage;
if (result.compacted) {
toast.success($t('chat.compactDone'), { id: toastId });
} else {
toast.message($t('chat.compactSkipped'), { id: toastId });
}
await loadChat(chatId);
} catch (err: any) {
toast.error(err?.message || $t('chat.compactFailed'), { id: toastId });
} finally {
sending = false;
chatInputEl?.focus();
}
}
function handlePlanCommand() {
planMode.set(!get(planMode));
}
function handleStatusCommand() {
showStatusModal = true;
}
// ── Queue actions ──────────────────────────────────────────
async function handleQueueSendNow(messageId: string) {
@@ -1397,7 +1451,11 @@
{sending}
{streaming}
{workspace}
{contextUsage}
onsend={send}
oncompact={handleManualCompact}
onplan={handlePlanCommand}
onstatus={handleStatusCommand}
oncancel={handleCancel}
{queuedMessages}
onqueuesendnow={handleQueueSendNow}
@@ -1408,3 +1466,7 @@
</div>
{/if}
</div>
{#if showStatusModal}
<StatusModal {chatId} {contextUsage} onclose={() => (showStatusModal = false)} />
{/if}
@@ -41,7 +41,7 @@
No files found
</div>
{:else}
<div class="px-2 pt-1 pb-0.5 text-[10px] leading-none text-gray-400 dark:text-gray-600">
<div class="mb-0.5 px-2 pt-1 pb-0.5 text-[10px] leading-none text-gray-400 dark:text-gray-600">
Files
</div>
<div bind:this={listEl}>
@@ -0,0 +1,78 @@
<script lang="ts">
import Modal from '../Modal.svelte';
import type { ContextUsage } from '$lib/apis/chat';
import { t } from '$lib/i18n';
interface Props {
chatId: string | null;
contextUsage: ContextUsage | null;
onclose: () => void;
}
let { chatId, contextUsage, onclose }: Props = $props();
let copied = $state(false);
const contextPercent = $derived(Math.max(0, Math.round(contextUsage?.percent ?? 0)));
const progressWidth = $derived(`${Math.min(contextPercent, 100)}%`);
const tokenCount = $derived(contextUsage?.tokens ?? contextUsage?.estimated_tokens ?? 0);
function copyChatId() {
if (!chatId) return;
navigator.clipboard.writeText(chatId);
copied = true;
setTimeout(() => (copied = false), 1600);
}
</script>
<Modal {onclose} class="w-full max-w-[300px] mx-4">
<div class="px-4 py-3.5">
<div class="flex items-baseline justify-between gap-3 mb-4">
<h2 class="text-sm font-medium text-gray-900 dark:text-white">
{$t('chat.commandStatus')}
</h2>
<span class="text-[11px] font-mono text-gray-400 dark:text-gray-600">
{contextUsage ? `${contextPercent}%` : $t('chat.statusUnknown')}
</span>
</div>
<div class="mb-4">
<div class="h-1 overflow-hidden rounded-full bg-gray-100 dark:bg-white/6">
<div
class="h-full rounded-full bg-gray-900 dark:bg-white transition-[width] duration-200"
style={`width: ${progressWidth};`}
></div>
</div>
<div class="mt-1.5 flex items-baseline justify-between gap-3">
<span class="text-[10px] text-gray-400 dark:text-gray-600">
{$t('chat.statusEstimated')}
</span>
<span class="font-mono text-[10px] text-gray-400 dark:text-gray-600">
{#if contextUsage}
{tokenCount.toLocaleString()} / {contextUsage.threshold.toLocaleString()}
{:else}
{$t('chat.statusUnknown')}
{/if}
</span>
</div>
</div>
<div>
<div class="text-[10px] text-gray-400 dark:text-gray-600 mb-1">
{$t('chat.statusChatId')}
</div>
{#if chatId}
<button
type="button"
class="block w-full text-left font-mono text-[11px] leading-relaxed text-gray-700 dark:text-gray-300 break-all hover:text-gray-900 dark:hover:text-white transition-colors"
onclick={copyChatId}
>
{copied ? $t('about.copied') : chatId}
</button>
{:else}
<div class="font-mono text-[11px] leading-relaxed text-gray-700 dark:text-gray-300">
{$t('chat.statusNoChat')}
</div>
{/if}
</div>
</div>
</Modal>
@@ -566,6 +566,23 @@
"chat.scrollToBottom": "Scroll to bottom",
"chat.removeUpload": "Remove upload",
"chat.sendNow": "Send now",
"chat.commandCompact": "Compact",
"chat.commandCompactPercent": "{{percent}}% full",
"chat.commandPlan": "Plan mode",
"chat.commandPlanOn": "On",
"chat.commandPlanOff": "Off",
"chat.commandStatus": "Status",
"chat.commandStatusDesc": "Context usage, chat id",
"chat.statusContextUsage": "Context usage",
"chat.statusEstimated": "Estimated context",
"chat.statusChatId": "Chat id",
"chat.statusNoChat": "none",
"chat.statusUnknown": "unknown",
"chat.compacting": "Compacting context...",
"chat.compactDone": "Context compacted",
"chat.compactSkipped": "Nothing to compact yet",
"chat.compactFailed": "Failed to compact context",
"chat.compactNoChat": "Start a chat before compacting",
"chat.prevMessage": "Previous message",
"chat.nextMessage": "Next message",
"chat.editMessage": "Edit message",