This commit is contained in:
Timothy Jaeryang Baek
2026-06-18 16:58:12 +02:00
Unverified
parent b015269e52
commit d6670791d1
10 changed files with 557 additions and 19 deletions
+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",
+145 -1
View File
@@ -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 ────────────────────
+1 -4
View File
@@ -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}}"
+21 -5
View File
@@ -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: