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

+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: