From d1abb32ae2a69fd24cf7059b170fdb6ca8c5c3d0 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 22 Jun 2026 13:56:59 +0200 Subject: [PATCH] refac --- cptr/frontend/src/lib/apis/admin.ts | 6 +- .../src/lib/components/Admin/Models.svelte | 75 ++++++++++++++++++- cptr/routers/chat.py | 8 +- cptr/utils/chat_task.py | 16 +++- cptr/utils/context.py | 65 ++++++++++++++-- 5 files changed, 155 insertions(+), 15 deletions(-) diff --git a/cptr/frontend/src/lib/apis/admin.ts b/cptr/frontend/src/lib/apis/admin.ts index 40fec56..4b6eec0 100644 --- a/cptr/frontend/src/lib/apis/admin.ts +++ b/cptr/frontend/src/lib/apis/admin.ts @@ -109,7 +109,11 @@ export const verifyConnection = (id: string) => export interface ModelConfigEntry { is_active?: boolean; - params?: { request_params?: Record; system_prompt?: string }; + params?: { + request_params?: Record; + system_prompt?: string; + compact_token_threshold?: number; + }; } export interface ModelConfigResponse { diff --git a/cptr/frontend/src/lib/components/Admin/Models.svelte b/cptr/frontend/src/lib/components/Admin/Models.svelte index 6b5812d..0f98ea7 100644 --- a/cptr/frontend/src/lib/components/Admin/Models.svelte +++ b/cptr/frontend/src/lib/components/Admin/Models.svelte @@ -24,6 +24,7 @@ is_active: boolean; rows: ParamRow[]; systemPrompt: string; + compactTokenThreshold: number | null; dirty: boolean; }; @@ -35,6 +36,7 @@ let globalRows = $state([]); let globalSystemPrompt = $state(''); + let globalCompactTokenThreshold = $state(null); let globalDirty = $state(false); let globalExpanded = $state(false); let showVariables = $state(false); @@ -87,6 +89,12 @@ Files: })); } + function parseCompactTokenThreshold(config: ModelConfigEntry | undefined): number | null { + const value = config?.params?.compact_token_threshold; + const threshold = typeof value === 'number' ? value : Number(value); + return Number.isFinite(threshold) && threshold > 0 ? Math.floor(threshold) : null; + } + function rowsToRequestParams(rows: ParamRow[]): Record { const result: Record = {}; for (const { key, value } of rows) { @@ -110,7 +118,9 @@ Files: if (!preserveDirty || !globalDirty) { globalRows = parseRows(config['*']); globalSystemPrompt = config['*']?.params?.system_prompt || ''; - globalExpanded = globalRows.length > 0 || !!globalSystemPrompt; + globalCompactTokenThreshold = parseCompactTokenThreshold(config['*']); + globalExpanded = + globalRows.length > 0 || !!globalSystemPrompt || globalCompactTokenThreshold !== null; } models = data.models.map((m) => { @@ -125,6 +135,7 @@ Files: is_active: mc?.is_active !== false, rows: parseRows(mc), systemPrompt: (mc?.params as any)?.system_prompt || '', + compactTokenThreshold: parseCompactTokenThreshold(mc), dirty: false }; }); @@ -183,11 +194,23 @@ Files: } } - function buildParams(rows: ParamRow[], systemPrompt: string): Record { + function readThresholdInput(e: Event): number | null { + const value = (e.target as HTMLInputElement).value; + if (!value.trim()) return null; + const threshold = Number(value); + return Number.isFinite(threshold) && threshold > 0 ? Math.floor(threshold) : null; + } + + function buildParams( + rows: ParamRow[], + systemPrompt: string, + compactThreshold: number | null + ): Record { const params: Record = {}; const rp = rowsToRequestParams(rows); if (Object.keys(rp).length) params.request_params = rp; if (systemPrompt.trim()) params.system_prompt = systemPrompt.trim(); + if (compactThreshold && compactThreshold > 0) params.compact_token_threshold = compactThreshold; return params; } @@ -198,7 +221,7 @@ Files: if (globalDirty) { promises.push( updateModelConfig('*', { - params: buildParams(globalRows, globalSystemPrompt) + params: buildParams(globalRows, globalSystemPrompt, globalCompactTokenThreshold) }) ); } @@ -206,7 +229,7 @@ Files: if (model.dirty) { promises.push( updateModelConfig(model.id, { - params: buildParams(model.rows, model.systemPrompt) + params: buildParams(model.rows, model.systemPrompt, model.compactTokenThreshold) }) ); } @@ -328,6 +351,34 @@ Files: {/snippet} +{#snippet compactThresholdField( + inputId: string, + value: number | null, + onInput: (value: number | null) => void +)} +
+ +
+ onInput(readThresholdInput(e))} + min="1" + step="1" + placeholder={$t('admin.compactTokenThreshold')} + class="w-24 h-7 px-2 rounded-lg text-xs bg-gray-100 dark:bg-white/6 text-gray-700 dark:text-gray-300 border border-gray-200 dark:border-white/8 outline-none focus:border-blue-400 dark:focus:border-blue-500 transition-colors" + /> + {$t('admin.compactTokenThresholdUnit')} +
+
+{/snippet} +
{#if loading}
@@ -412,6 +463,14 @@ Files: {#if globalExpanded} + {@render compactThresholdField( + 'global-compact-threshold', + globalCompactTokenThreshold, + (value) => { + globalCompactTokenThreshold = value; + globalDirty = true; + } + )} {@render systemPromptField( globalSystemPrompt, (v) => { @@ -470,6 +529,14 @@ Files: {#if selectedId === model.id} + {@render compactThresholdField( + `model-${model.id}-compact-threshold`, + model.compactTokenThreshold, + (value) => { + model.compactTokenThreshold = value; + model.dirty = true; + } + )} {@render systemPromptField( model.systemPrompt, (v) => { diff --git a/cptr/routers/chat.py b/cptr/routers/chat.py index 623a647..523afc4 100644 --- a/cptr/routers/chat.py +++ b/cptr/routers/chat.py @@ -322,11 +322,13 @@ async def _get_chat_context_usage(chat, model_id: str | None = None) -> dict | N build_context_usage, estimate_context_usage, estimate_messages_tokens, + load_compact_token_threshold, ) 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) + compact_token_threshold = await load_compact_token_threshold(model) system = await _load_system_prompt(workspace, model or "", user_id=chat.user_id) if existing_summary: system += f"\n\n[CONVERSATION SUMMARY]\n{existing_summary}" @@ -341,9 +343,11 @@ async def _get_chat_context_usage(chat, model_id: str | None = None) -> dict | N tokens += estimate_messages_tokens( [{"role": m.role, "content": m.content or ""} for m in trailing_messages] ) - return build_context_usage(tokens, source="estimated") + return build_context_usage( + tokens, threshold=compact_token_threshold, source="estimated" + ) - return estimate_context_usage(messages, system) + return estimate_context_usage(messages, system, threshold=compact_token_threshold) async def _infer_chat_model(chat_id: str) -> str: diff --git a/cptr/utils/chat_task.py b/cptr/utils/chat_task.py index a5d9f98..8981d62 100644 --- a/cptr/utils/chat_task.py +++ b/cptr/utils/chat_task.py @@ -11,7 +11,7 @@ import logging import uuid from cptr.env import CHAT_MAX_ITERATIONS, CHAT_TOOL_MAX_CHARS -from cptr.utils.context import should_compact +from cptr.utils.context import resolve_compact_token_threshold, should_compact from cptr.utils.skills import discover_skills, load_skill, format_skill_content from cptr.utils.summarize import summarize_messages from cptr.models import Chat, ChatMessage, Config @@ -1149,6 +1149,7 @@ async def run_chat_task( chat_obj = await Chat.get_by_id(chat_id) chat_params = (chat_obj.meta or {}).get("params", {}) if chat_obj else {} + configured_model = (msg.model if msg else None) or model system = await _load_system_prompt(workspace, model, user_id=user_id) messages, loaded_summary = await _load_message_history(chat_id, message_id) if loaded_summary: @@ -1222,17 +1223,28 @@ async def run_chat_task( chat_request_params = chat_params.get("request_params") or {} global_rp = {} model_rp = {} + compact_token_threshold = None try: chat_models_config = await Config.get("chat.models") or {} global_rp = chat_models_config.get("*", {}).get("params", {}).get("request_params", {}) model_rp = chat_models_config.get(model, {}).get("params", {}).get("request_params", {}) + compact_token_threshold = resolve_compact_token_threshold( + configured_model, chat_models_config=chat_models_config + ) except Exception: pass + compact_token_threshold = compact_token_threshold or resolve_compact_token_threshold() request_params = {**global_rp, **model_rp, **chat_request_params} or None for _iteration in range(CHAT_MAX_ITERATIONS): # ── Context compaction: summarize older messages if too large ── - if should_compact(messages, system, last_usage, new_messages_since): + if should_compact( + messages, + system, + last_usage, + new_messages_since, + threshold=compact_token_threshold, + ): target_keep = max(2, len(messages) * 2 // 5) split_idx = _find_safe_split(messages, target_keep) drop_zone = messages[:split_idx] diff --git a/cptr/utils/context.py b/cptr/utils/context.py index 269d58f..4f8fcf0 100644 --- a/cptr/utils/context.py +++ b/cptr/utils/context.py @@ -35,6 +35,7 @@ def should_compact( system_prompt: str, last_usage: dict | None = None, new_messages_since: int = 0, + threshold: int | None = None, ) -> bool: """True when estimated tokens exceed the compact token threshold. @@ -43,7 +44,7 @@ def should_compact( estimates the new messages appended since that call. Falls back to full estimation when no usage data exists. """ - threshold = _get_threshold() + resolved_threshold = threshold or _get_threshold() if last_usage and last_usage.get("input_tokens"): # Real base from last API call + estimate only new additions @@ -51,11 +52,11 @@ def should_compact( if new_messages_since > 0: new_msgs = messages[-new_messages_since:] base += estimate_messages_tokens(new_msgs) - return base > threshold + return base > resolved_threshold # Full estimation fallback total = estimate_tokens(system_prompt) + estimate_messages_tokens(messages) - return total > threshold + return total > resolved_threshold def build_context_usage(tokens: int, *, threshold: int | None = None, source: str) -> dict: @@ -71,11 +72,63 @@ def build_context_usage(tokens: int, *, threshold: int | None = None, source: st } -def estimate_context_usage(messages: list[dict], system_prompt: str) -> dict: +def estimate_context_usage( + messages: list[dict], system_prompt: str, *, threshold: int | None = None +) -> dict: """Return context fullness stats using the same estimate as compaction.""" - threshold = _get_threshold() + resolved_threshold = threshold or _get_threshold() estimated_tokens = estimate_tokens(system_prompt) + estimate_messages_tokens(messages) - return build_context_usage(estimated_tokens, threshold=threshold, source="estimated") + return build_context_usage(estimated_tokens, threshold=resolved_threshold, source="estimated") + + +def _parse_positive_int(value: object) -> int | None: + try: + parsed = int(value) + except (TypeError, ValueError): + return None + return parsed if parsed > 0 else None + + +def resolve_compact_token_threshold( + model: str | None = None, + *, + chat_models_config: dict | None = None, + global_cap: int | None = None, +) -> int: + """Return the effective compaction threshold for a model. + + The existing global threshold is the cap. Model-level values can lower it + for smaller context windows, but never raise it. + """ + cap = global_cap or _get_threshold() + model_threshold = None + + if chat_models_config: + candidate_keys = [] + if model: + candidate_keys.append(model) + if "/" in model: + candidate_keys.append(model.split("/", 1)[1]) + candidate_keys.append("*") + + for key in candidate_keys: + params = chat_models_config.get(key, {}).get("params", {}) + model_threshold = _parse_positive_int(params.get("compact_token_threshold")) + if model_threshold: + break + + return min(model_threshold, cap) if model_threshold else cap + + +async def load_compact_token_threshold(model: str | None = None) -> int: + """Load the effective compaction threshold from persisted model config.""" + try: + from cptr.models import Config + + chat_models_config = await Config.get("chat.models") or {} + except Exception: + chat_models_config = {} + return resolve_compact_token_threshold(model, chat_models_config=chat_models_config) def _get_threshold() -> int: