This commit is contained in:
Timothy Jaeryang Baek committed 2026-06-19 16:33:56 +02:00
1 parent 5aa656e19e
commit e446771d83
3 files changed
+330 -214

No files matched your search

+2 -196
View File
@@ -8,15 +8,11 @@ from __future__ import annotations
import asyncio
import json
import logging
import platform
import re
import uuid
from datetime import date
from pathlib import Path
from cptr.env import CHAT_MAX_ITERATIONS, CHAT_TOOL_MAX_CHARS
from cptr.utils.context import should_compact
from cptr.utils.skills import discover_skills, load_skill, build_catalog_xml, format_skill_content
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
from cptr.socket.main import emit_to_user
@@ -32,6 +28,7 @@ from cptr.utils.crypto import decrypt_key
from cptr.utils.tools import ALL_TOOLS, execute_tool, get_tool_list, _fn_to_schema, create_artifact
from cptr.utils.chat_export import export_chat_to_file
from cptr.utils.json_parser import extract_json
from cptr.utils.prompt_templates import load_system_prompt as _load_system_prompt
logger = logging.getLogger(__name__)
@@ -243,197 +240,6 @@ async def reconcile_chat_state():
logger.info("[reconcile] Recovered %d chat(s) on startup", len(healed_chats))
# ── System prompt ───────────────────────────────────────────
def _get_file_tree(workspace: str, max_entries: int = 200) -> str:
"""Generate a compact file tree listing for the workspace."""
ws = Path(workspace)
if not ws.is_dir():
return ""
ignore = {
".git",
"node_modules",
"__pycache__",
".venv",
"venv",
".next",
"build",
"dist",
".cptr",
".svelte-kit",
".DS_Store",
}
entries = []
for item in sorted(ws.iterdir()):
if item.name in ignore:
continue
suffix = "/" if item.is_dir() else ""
entries.append(f" {item.name}{suffix}")
if item.is_dir():
try:
for child in sorted(item.iterdir()):
if child.name in ignore:
continue
csuffix = "/" if child.is_dir() else ""
entries.append(f" {child.name}{csuffix}")
if len(entries) >= max_entries:
entries.append(" ...")
break
except PermissionError:
pass
if len(entries) >= max_entries:
break
return "\n".join(entries)
INSTRUCTION_FILENAMES = ["MEMORY.md", "AGENTS.md", "AGENT.md", "CLAUDE.md"]
def _load_instruction_files(workspace: str, max_bytes: int = 32_000) -> str:
"""Load well-known AI instruction files from workspace root.
Scans for MEMORY.md, AGENTS.md, AGENT.md, CLAUDE.md.
All found files are concatenated (not first-found-wins).
"""
ws = Path(workspace)
if not ws.is_dir():
return ""
parts: list[str] = []
total = 0
for name in INSTRUCTION_FILENAMES:
path = ws / name
if path.is_file():
remaining = max_bytes - total
if remaining <= 0:
break
try:
content = path.read_text(errors="replace")[:remaining].strip()
except OSError:
continue
if content:
parts.append(f"# {name}\n{content}")
total += len(content)
logger.debug("[instructions] Loaded %s (%d bytes)", name, len(content))
return "\n\n".join(parts)
# ── Template engine ─────────────────────────────────────────
_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{{INSTRUCTIONS}}"
"\n\n{{SKILLS}}"
"\n\nWorkspace: {{WORKSPACE_NAME}}"
"\nFiles:\n{{FILE_TREE}}"
)
def _render_template(template: str, variables: dict[str, str]) -> str:
"""Render {{VARIABLE}} placeholders in a template string.
- Known variables are substituted with their values.
- Unrecognized {{...}} tokens are left as-is.
- Cleans up excess blank lines left by empty variable substitutions.
"""
def _replace(match: re.Match) -> str:
key = match.group(1)
if key in variables:
return variables[key]
return match.group(0) # leave unrecognized tokens as-is
result = _TEMPLATE_RE.sub(_replace, template)
# Clean up triple+ blank lines left by empty substitutions
result = re.sub(r"\n{3,}", "\n\n", result)
return result.strip()
def _build_template_variables(workspace: str, model: str = "") -> dict[str, str]:
"""Build the dict of template variable values for the current context."""
ws_path = Path(workspace)
# Build instructions content
instructions = _load_instruction_files(workspace)
if instructions:
instructions_block = (
f"<instructions>\n{instructions}\n</instructions>"
"\n\nThe above <instructions> were loaded from instruction files in the workspace root. "
"These files persist across sessions. "
"You can update them with your file tools to save learnings, decisions, or "
"project conventions for future sessions."
)
else:
instructions_block = ""
# Build skills catalog
skills = discover_skills(workspace)
skills_block = build_catalog_xml(skills)
return {
"WORKSPACE_NAME": ws_path.name if ws_path.is_dir() else "",
"WORKSPACE_PATH": str(ws_path),
"FILE_TREE": _get_file_tree(workspace),
"INSTRUCTIONS": instructions_block,
"SKILLS": skills_block,
"OS": platform.system().replace("Darwin", "macOS"),
"DATE": date.today().isoformat(),
"MODEL": model,
}
async def _load_system_prompt(workspace: str, model: str = "") -> str:
"""Load system prompt with template variable rendering.
Resolution order (most specific wins):
1. .cptr/system.md in the workspace (file override)
2. Per-model system_prompt from chat.models config
3. Global (*) system_prompt from chat.models config
4. DEFAULT_SYSTEM_PROMPT constant (hardcoded fallback)
All sources support {{VARIABLE}} template substitution.
"""
template = None
# 1. Workspace file override (.cptr/system.md)
ws_prompt = Path(workspace) / ".cptr" / "system.md"
if ws_prompt.is_file():
template = ws_prompt.read_text(errors="replace").strip()
# 2 & 3. Config-based prompts (per-model → global)
if template is None:
try:
chat_models_config = await Config.get("chat.models") or {}
# Per-model prompt
if model:
model_prompt = (
chat_models_config.get(model, {}).get("params", {}).get("system_prompt")
)
if model_prompt:
template = model_prompt
# Global prompt
if template is None:
global_prompt = (
chat_models_config.get("*", {}).get("params", {}).get("system_prompt")
)
if global_prompt:
template = global_prompt
except Exception:
logger.debug("[system_prompt] Failed to load from config", exc_info=True)
# 4. Hardcoded fallback
if template is None:
template = DEFAULT_SYSTEM_PROMPT
# Render template variables
variables = _build_template_variables(workspace, model)
return _render_template(template, variables)
VOICE_MODE_SYSTEM_PROMPT = (
"You are in voice mode. Keep responses brief, conversational, and easy to hear aloud. "
"Prefer one or two short paragraphs. Ask at most one focused follow-up question when needed. "