From d6670791d123a5da5ea9040befe19fe831691cad Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 18 Jun 2026 16:58:12 +0200 Subject: [PATCH] refac --- cptr/frontend/src/lib/apis/chat.ts | 27 ++- .../src/lib/components/Admin/Models.svelte | 2 - .../src/lib/components/chat/ChatInput.svelte | 207 +++++++++++++++++- .../src/lib/components/chat/ChatPanel.svelte | 66 +++++- .../chat/FileSuggestionPopup.svelte | 2 +- .../lib/components/chat/StatusModal.svelte | 78 +++++++ cptr/frontend/src/lib/i18n/locales/en.json | 17 ++ cptr/routers/chat.py | 146 +++++++++++- cptr/utils/chat_task.py | 5 +- cptr/utils/context.py | 26 ++- 10 files changed, 557 insertions(+), 19 deletions(-) create mode 100644 cptr/frontend/src/lib/components/chat/StatusModal.svelte diff --git a/cptr/frontend/src/lib/apis/chat.ts b/cptr/frontend/src/lib/apis/chat.ts index ecd0afb..e9deb29 100644 --- a/cptr/frontend/src/lib/apis/chat.ts +++ b/cptr/frontend/src/lib/apis/chat.ts @@ -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(`/api/chats/${chatId}`); +export const getChat = (chatId: string, modelId?: string) => { + const suffix = modelId ? `?model_id=${encodeURIComponent(modelId)}` : ''; + return fetchJSON(`/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(`/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 })); diff --git a/cptr/frontend/src/lib/components/Admin/Models.svelte b/cptr/frontend/src/lib/components/Admin/Models.svelte index 952fa95..7160191 100644 --- a/cptr/frontend/src/lib/components/Admin/Models.svelte +++ b/cptr/frontend/src/lib/components/Admin/Models.svelte @@ -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}} diff --git a/cptr/frontend/src/lib/components/chat/ChatInput.svelte b/cptr/frontend/src/lib/components/chat/ChatInput.svelte index 4e045a8..2806329 100644 --- a/cptr/frontend/src/lib/components/chat/ChatInput.svelte +++ b/cptr/frontend/src/lib/components/chat/ChatInput.svelte @@ -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)); @@ -890,6 +961,136 @@ {/if} + {#if showSlashCommands} +
+
+ Commands +
+ {#if slashCommandIds.includes('compact')} + + {/if} + {#if slashCommandIds.includes('plan')} + + {/if} + {#if slashCommandIds.includes('status')} + + {/if} +
+ {/if} +
@@ -1014,7 +1215,7 @@ ([]); let currentMessageId = $state(null); + let contextUsage = $state(null); + let showStatusModal = $state(false); let previousChats = $state([]); 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 @@
{/if} + +{#if showStatusModal} + (showStatusModal = false)} /> +{/if} diff --git a/cptr/frontend/src/lib/components/chat/FileSuggestionPopup.svelte b/cptr/frontend/src/lib/components/chat/FileSuggestionPopup.svelte index 1faccb8..3b2725d 100644 --- a/cptr/frontend/src/lib/components/chat/FileSuggestionPopup.svelte +++ b/cptr/frontend/src/lib/components/chat/FileSuggestionPopup.svelte @@ -41,7 +41,7 @@ No files found {:else} -
+
Files
diff --git a/cptr/frontend/src/lib/components/chat/StatusModal.svelte b/cptr/frontend/src/lib/components/chat/StatusModal.svelte new file mode 100644 index 0000000..94d4a5d --- /dev/null +++ b/cptr/frontend/src/lib/components/chat/StatusModal.svelte @@ -0,0 +1,78 @@ + + + +
+
+

+ {$t('chat.commandStatus')} +

+ + {contextUsage ? `${contextPercent}%` : $t('chat.statusUnknown')} + +
+ +
+
+
+
+
+ + {$t('chat.statusEstimated')} + + + {#if contextUsage} + {tokenCount.toLocaleString()} / {contextUsage.threshold.toLocaleString()} + {:else} + {$t('chat.statusUnknown')} + {/if} + +
+
+ +
+
+ {$t('chat.statusChatId')} +
+ {#if chatId} + + {:else} +
+ {$t('chat.statusNoChat')} +
+ {/if} +
+
+
diff --git a/cptr/frontend/src/lib/i18n/locales/en.json b/cptr/frontend/src/lib/i18n/locales/en.json index 090c843..ec67bbc 100644 --- a/cptr/frontend/src/lib/i18n/locales/en.json +++ b/cptr/frontend/src/lib/i18n/locales/en.json @@ -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", diff --git a/cptr/routers/chat.py b/cptr/routers/chat.py index 6cb90d8..26e05d6 100644 --- a/cptr/routers/chat.py +++ b/cptr/routers/chat.py @@ -251,7 +251,11 @@ async def _fetch_provider_models(conn: dict) -> list[str]: @router.get("/{chat_id}") -async def get_chat(chat_id: str, request: Request): +async def get_chat( + chat_id: str, + request: Request, + model_id: str | None = Query(None, description="Model used for system prompt context"), +): """Get chat metadata + all messages.""" user_id = _get_user(request) chat = await Chat.get_by_id(chat_id) @@ -259,6 +263,7 @@ async def get_chat(chat_id: str, request: Request): raise HTTPException(404, "chat not found") messages = await ChatMessage.get_all_by_chat(chat_id) + context_usage = await _get_chat_context_usage(chat, model_id) return { "chat": { "id": chat.id, @@ -270,6 +275,7 @@ async def get_chat(chat_id: str, request: Request): "updated_at": chat.updated_at, }, "messages": [_message_dict(m) for m in messages], + "context_usage": context_usage, } @@ -297,6 +303,77 @@ def _message_dict(m) -> dict: return d +async def _get_context_leaf_message_id(chat) -> str | None: + if chat.current_message_id: + return chat.current_message_id + messages = await ChatMessage.get_all_by_chat(chat.id) + return messages[-1].id if messages else None + + +async def _get_chat_context_usage(chat, model_id: str | None = None) -> dict | None: + message_id = await _get_context_leaf_message_id(chat) + if not message_id: + return None + + from cptr.utils.chat_task import _load_message_history, _load_system_prompt + from cptr.utils.context import ( + build_context_usage, + estimate_context_usage, + estimate_messages_tokens, + ) + + messages, existing_summary = await _load_message_history(chat.id, message_id) + workspace = (chat.meta or {}).get("workspace", "") + model = model_id or await _infer_chat_model(chat.id) + system = await _load_system_prompt(workspace, model or "") + if existing_summary: + system += f"\n\n[CONVERSATION SUMMARY]\n{existing_summary}" + + usage_checkpoint = await _get_latest_usage_checkpoint(chat.id, message_id) + if usage_checkpoint: + trailing_messages, usage = usage_checkpoint + tokens = usage.get("input_tokens") + if isinstance(tokens, int) and tokens > 0: + tokens += usage.get("output_tokens", 0) + if trailing_messages: + tokens += estimate_messages_tokens( + [{"role": m.role, "content": m.content or ""} for m in trailing_messages] + ) + return build_context_usage(tokens, source="estimated") + + return estimate_context_usage(messages, system) + + +async def _infer_chat_model(chat_id: str) -> str: + messages = await ChatMessage.get_all_by_chat(chat_id) + for message in reversed(messages): + if message.model: + return message.model + return await Config.get("chat.default_model") or "" + + +async def _get_latest_usage_checkpoint( + chat_id: str, message_id: str +) -> tuple[list[ChatMessage], dict] | None: + all_msgs = await ChatMessage.get_all_by_chat(chat_id) + msg_map = {m.id: m for m in all_msgs} + chain = [] + cur = msg_map.get(message_id) + while cur: + chain.append(cur) + cur = msg_map.get(cur.parent_id) if cur.parent_id else None + chain.reverse() + + for index in range(len(chain) - 1, -1, -1): + message = chain[index] + if message.chat_summary: + return None + usage = message.usage or {} + if message.role == "assistant" and usage.get("input_tokens"): + return chain[index + 1 :], usage + return None + + # ── Delete a chat ─────────────────────────────────────────── @@ -332,6 +409,10 @@ class SendMessageRequest(BaseModel): params: dict = {} +class CompactRequest(BaseModel): + model_id: str + + @router.post("") async def send_message(body: SendMessageRequest, request: Request): """Send a message. Omit chat_id to create a new chat. @@ -451,6 +532,69 @@ async def send_message(body: SendMessageRequest, request: Request): return resp +# ── Manual context compaction ─────────────────────────────── + + +@router.post("/{chat_id}/compact") +async def compact_chat(chat_id: str, body: CompactRequest, request: Request): + """Summarize older active-branch messages and store a compaction checkpoint.""" + user_id = _get_user(request) + chat = await Chat.get_by_id(chat_id) + if not chat or chat.user_id != user_id: + raise HTTPException(404, "chat not found") + if await _chat_has_active_generation(chat_id): + raise HTTPException(409, "wait for the current response to finish before compacting") + + message_id = await _get_context_leaf_message_id(chat) + if not message_id: + return {"ok": True, "compacted": False, "reason": "empty", "context_usage": None} + current_msg = await ChatMessage.get_by_id(message_id) + if not current_msg or not current_msg.parent_id: + usage = await _get_chat_context_usage(chat, body.model_id) + return {"ok": True, "compacted": False, "reason": "too_short", "context_usage": usage} + + connection, bare_model = await _resolve_connection(body.model_id, request.app.state) + + from cptr.utils.chat_task import _load_message_history + from cptr.utils.summarize import summarize_messages + + messages, existing_summary = await _load_message_history(chat_id, current_msg.parent_id) + if not messages: + usage = await _get_chat_context_usage(chat, body.model_id) + return {"ok": True, "compacted": False, "reason": "too_short", "context_usage": usage} + + provider = connection["provider"] + api_key = decrypt_key(connection.get("api_key", ""), _get_jwt_secret()) + from cptr.utils.chat_task import _default_base_url + + base_url = connection.get("base_url") or _default_base_url(provider) + api_type = connection.get("api_type", "chat_completions") + + summary = await summarize_messages( + messages, + existing_summary, + provider, + base_url, + api_key, + bare_model, + api_type=api_type, + ) + await ChatMessage.update(message_id, chat_summary=summary) + + from cptr.utils.chat_export import export_chat_to_file + + await export_chat_to_file(chat_id) + usage = await _get_chat_context_usage(chat, body.model_id) + return { + "ok": True, + "compacted": True, + "dropped_messages": len(messages), + "kept_messages": 1, + "summary_chars": len(summary), + "context_usage": usage, + } + + # ── Approve / reject a pending tool call ──────────────────── diff --git a/cptr/utils/chat_task.py b/cptr/utils/chat_task.py index a5e112a..7eb2688 100644 --- a/cptr/utils/chat_task.py +++ b/cptr/utils/chat_task.py @@ -325,10 +325,7 @@ _TEMPLATE_RE = re.compile(r"\{\{(\w+)\}\}") DEFAULT_SYSTEM_PROMPT = ( "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.\n\n" - "For complex tasks, create an implementation plan first using " - "create_artifact. Then wait for an explicit approval message before using " - "tools or implementing." + "Use them to help the user with their coding tasks." "\n\n{{INSTRUCTIONS}}" "\n\n{{SKILLS}}" "\n\nWorkspace: {{WORKSPACE_NAME}}" diff --git a/cptr/utils/context.py b/cptr/utils/context.py index c0d8aca..269d58f 100644 --- a/cptr/utils/context.py +++ b/cptr/utils/context.py @@ -1,8 +1,4 @@ -"""Context estimation for chat compaction. - -Uses a character-based heuristic (len/4) to estimate token counts. -A follow-up will add real usage data from API responses for precision. -""" +"""Context usage helpers for chat compaction.""" from __future__ import annotations @@ -62,6 +58,26 @@ def should_compact( return total > threshold +def build_context_usage(tokens: int, *, threshold: int | None = None, source: str) -> dict: + """Return context fullness stats for estimated token counts.""" + resolved_threshold = threshold or _get_threshold() + percent = round((tokens / resolved_threshold) * 100) if resolved_threshold > 0 else 0 + return { + "tokens": tokens, + "estimated_tokens": tokens, + "threshold": resolved_threshold, + "percent": max(0, percent), + "source": source, + } + + +def estimate_context_usage(messages: list[dict], system_prompt: str) -> dict: + """Return context fullness stats using the same estimate as compaction.""" + threshold = _get_threshold() + estimated_tokens = estimate_tokens(system_prompt) + estimate_messages_tokens(messages) + return build_context_usage(estimated_tokens, threshold=threshold, source="estimated") + + def _get_threshold() -> int: """Read threshold: app_config (admin UI) > config.toml [chat] > env var/default.""" try: