This commit is contained in:
Timothy Jaeryang Baek committed 2026-07-31 17:15:34 -04:00
1 parent 490502a1e0
commit 4aafe5a0e4
17 files changed
+193 -16

No files matched your search

+2 -2
View File
@@ -17,7 +17,7 @@ from cptr.utils.agents.events import (
AgentTextDelta,
AgentToolUpdate,
)
from cptr.utils.agents.prompts import latest_user_text
from cptr.utils.agents.prompts import session_turn_prompt_text
_claude_clients: dict[str, tuple[Any, tuple[Any, ...]]] = {}
@@ -125,7 +125,6 @@ async def run_claude_code_agent(
yield AgentError("Claude Code support requires the claude-agent-sdk Python package")
return
prompt = latest_user_text(messages)
env = os.environ.copy()
home = os.path.expanduser(str(profile["home"])) if profile.get("home") else None
if home:
@@ -143,6 +142,7 @@ async def run_claude_code_agent(
if resume_state:
value = resume_state.get("session_id")
session_id = value if isinstance(value, str) and value else None
prompt = session_turn_prompt_text(messages, resumed=bool(session_id))
options_kwargs: dict[str, Any] = {
"system_prompt": system_prompt or None,
+2 -2
View File
@@ -21,7 +21,7 @@ from cptr.utils.agents.events import (
AgentToolOutputDelta,
AgentToolUpdate,
)
from cptr.utils.agents.prompts import latest_user_text
from cptr.utils.agents.prompts import session_turn_prompt_text
CODEX_STDOUT_CHUNK_SIZE = 64 * 1024
CODEX_MAX_WIRE_MESSAGE_CHARS = 16 * 1024 * 1024
@@ -375,7 +375,7 @@ async def run_codex_agent(
yield AgentError("Codex did not return a thread id")
return
prompt = latest_user_text(messages)
prompt = session_turn_prompt_text(messages, resumed=bool(_resume_thread_id(resume_state)))
turn_input: list[dict[str, Any]] = []
if prompt:
turn_input.append({"type": "text", "text": prompt})
+54 -7
View File
@@ -4,6 +4,8 @@ from __future__ import annotations
from typing import Any
from cptr.env import AGENT_SEED_TRANSCRIPT_MAX_CHARS
def message_text(message: dict[str, Any]) -> str:
content = message.get("content", "")
@@ -14,17 +16,62 @@ def message_text(message: dict[str, Any]) -> str:
return str(content or "")
def latest_user_text(messages: list[dict[str, Any]]) -> str:
"""Return only the current user turn for native-session agents."""
def session_turn_prompt_text(messages: list[dict[str, Any]], resumed: bool) -> str:
"""Return the prompt for a native-session agent turn."""
for message in reversed(messages):
if message.get("role") == "user":
return message_text(message).strip()
return ""
last_user_index = None
for index in range(len(messages) - 1, -1, -1):
if messages[index].get("role") == "user":
last_user_index = index
break
if last_user_index is None:
return ""
prompt = message_text(messages[last_user_index]).strip()
if resumed or AGENT_SEED_TRANSCRIPT_MAX_CHARS <= 0:
return prompt
parts = []
for message in messages[:last_user_index]:
role = message.get("role")
if role not in ("user", "assistant"):
continue
text = message_text(message).strip()
if text:
parts.append(f"<{role}>\n{text}\n</{role}>")
selected = []
total = 0
truncated = False
for part in reversed(parts):
if len(part) > AGENT_SEED_TRANSCRIPT_MAX_CHARS:
selected.append(
"[Earlier conversation truncated.]\n"
+ part[-AGENT_SEED_TRANSCRIPT_MAX_CHARS:]
)
truncated = True
break
next_total = total + len(part) + (2 if selected else 0)
if selected and next_total > AGENT_SEED_TRANSCRIPT_MAX_CHARS:
truncated = True
break
selected.append(part)
total = next_total
if truncated and not selected[-1].startswith("[Earlier conversation truncated.]"):
selected.append("[Earlier conversation truncated.]")
transcript = "\n\n".join(reversed(selected))
if not transcript:
return prompt
return (
"The following is prior conversation context from before this native agent "
"session began. Use it for context, then answer only the latest user request.\n\n"
f"<prior_conversation>\n{transcript}\n</prior_conversation>\n\n"
f"<latest_user_request>\n{prompt}\n</latest_user_request>"
)
def turn_prompt_text(messages: list[dict[str, Any]], system_prompt: str, resumed: bool) -> str:
prompt = latest_user_text(messages)
prompt = session_turn_prompt_text(messages, resumed)
if system_prompt and not resumed:
return f"{system_prompt}\n\n{prompt}" if prompt else system_prompt
return prompt
+19 -2
View File
@@ -2290,7 +2290,15 @@ async def run_chat_task(
pass
asyncio.create_task(auto_answer())
await emit(done=True)
await emit(
done=True,
status="input_required",
title=chat_obj.title if chat_obj else None,
workspace=workspace,
workspace_name=workspace.rstrip("/").rsplit("/", 1)[-1]
if workspace
else "",
)
return
# Check if any call needs approval
@@ -2333,7 +2341,16 @@ async def run_chat_task(
await emit(output=flushed_item)
await emit(output=item)
_task_state.pop(message_id, None)
await emit(done=True)
await emit(
done=True,
status="approval_required",
title=chat_obj.title if chat_obj else None,
tool_name=tc["name"],
workspace=workspace,
workspace_name=workspace.rstrip("/").rsplit("/", 1)[-1]
if workspace
else "",
)
return
# All calls are auto-approved — build UI items