This commit is contained in:
Timothy Jaeryang Baek committed 2026-07-31 21:15:11 -04:00
1 parent 0bbf9a44c9
commit 1b7bea00a4
17 files changed
+816 -238

No files matched your search

+82 -23
View File
@@ -42,7 +42,7 @@ from cptr.models import (
from cptr.socket.main import emit_to_user
from cptr.utils.ai import (
ChatCompletionForm,
chat_completion,
generate_json,
stream_anthropic,
stream_openai_completions,
stream_openai_responses,
@@ -59,7 +59,6 @@ from cptr.utils.tools import (
_fn_to_schema,
)
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
from cptr.utils.agents.events import (
AgentDone,
@@ -725,6 +724,54 @@ TITLE_PROMPT = (
"Do not include quotes around the JSON. Do not explain."
)
TOOL_APPROVAL_REVIEW_PROMPT = (
"Decide whether Computer may run the proposed tool call without asking the user. "
'Return ONLY JSON: {"decision":"approve" or "deny","reason":"short reason"}. '
"Approve only when the call is a reasonable next step for the user's request and the "
"arguments do not appear destructive, surprising, credential-seeking, or unrelated. "
"Deny when uncertain."
)
async def review_tool_approval(
*,
messages: list[dict],
assistant_text: str,
connection: dict,
model: str,
tool_name: str,
arguments: dict,
) -> bool:
"""Return True when Auto mode can run a pending tool without prompting."""
latest_user = ""
for message in reversed(messages):
if message.get("role") == "user":
latest_user = _plain_message_text(message.get("content"))
break
args_text = json.dumps(arguments, ensure_ascii=False, default=str)
if len(args_text) > 4000:
args_text = args_text[:3500] + "\n...(truncated)"
parsed = await generate_json(
model_id=await Config.get("tool_approval.review.model"),
active_connection=connection,
active_model=model,
messages=[
{
"role": "user",
"content": (
f"Latest user request:\n{latest_user[:2000]}\n\n"
f"Assistant text before tool call:\n{assistant_text[-2000:]}\n\n"
f"Tool: {tool_name}\nArguments:\n{args_text}"
),
}
],
system=TOOL_APPROVAL_REVIEW_PROMPT,
max_tokens=150,
request_params={"temperature": 0},
timeout_seconds=20,
)
return str((parsed or {}).get("decision") or "").strip().lower() == "approve"
async def generate_chat_title(
chat_id: str,
@@ -735,14 +782,13 @@ async def generate_chat_title(
):
"""Generate a proper title for a new chat via a lightweight LLM call.
Uses the shared chat_completion() helper from ai.py.
Uses the configured title model, inheriting the active/default model when unset.
On success, updates the DB and emits a socket event so the frontend
updates the tab label in real time.
"""
provider = connection["provider"]
api_key = decrypt_key(connection.get("api_key", ""), _get_jwt_secret())
base_url = connection.get("base_url") or _default_base_url(provider)
api_type = connection.get("api_type", "chat_completions")
title_generation_enabled = await Config.get("chat.title_generation.enabled")
if title_generation_enabled is False or title_generation_enabled == "false":
return
# Truncate very long messages to keep the prompt small
truncated = user_message[:200].strip()
@@ -750,19 +796,14 @@ async def generate_chat_title(
truncated += "..."
try:
text = await chat_completion(
provider=provider,
base_url=base_url,
api_key=api_key,
model=model,
parsed = await generate_json(
model_id=await Config.get("chat.title_generation.model"),
active_connection=connection,
active_model=model,
messages=[{"role": "user", "content": truncated}],
system=TITLE_PROMPT,
max_tokens=50,
api_type=api_type,
)
# Parse the JSON title
parsed = extract_json(text)
title = (parsed.get("title", "") if isinstance(parsed, dict) else "").strip()
if not title:
return
@@ -1924,9 +1965,26 @@ async def run_chat_task(
name = item.get("name", "")
tool = ALL_TOOLS.get(name)
needs_approval = not (
approval_mode == "full" or (approval_mode == "auto" and tool and tool["auto"])
should_auto = approval_mode == "full" or (
approval_mode == "auto" and tool and tool["auto"]
)
if (
not should_auto
and approval_mode == "auto"
and not item.get("approved")
and tool
and await review_tool_approval(
messages=messages,
assistant_text=content,
connection=connection,
model=model,
tool_name=name,
arguments=item.get("arguments") or {},
)
):
item["approved"] = True
should_auto = True
needs_approval = not should_auto
if needs_approval and not item.get("approved"):
item["status"] = "pending"
await _save_message(
@@ -2056,11 +2114,8 @@ async def run_chat_task(
summary = await summarize_messages(
drop_zone,
loaded_summary,
provider,
base_url,
api_key,
connection,
model,
api_type=api_type,
)
checkpoint_message_id = _summary_checkpoint_message_id(
@@ -2747,7 +2802,11 @@ async def run_chat_task(
fallback = first_user.content[:50].strip() or "New Chat"
if chat_obj.title == fallback:
await generate_chat_title(
chat_id, user_id, connection, model, first_user.content
chat_id,
user_id,
connection,
model,
first_user.content,
)
except Exception:
logger.debug(