Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions src/agent_box/agents/claude_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,25 @@ async def _parse_user_answer(questions: list[dict], user_reply: str) -> dict:
return json.loads(text)


def _format_answers_for_tool_result(answers: dict) -> str:
"""Render answers as the natural-language string Claude Code expects.

Matches the CLI's own format:
``User has answered your questions: "<q>"="<a>", ... You can now
continue with the user's answers in mind.``
"""
pairs = []
for question, answer in answers.items():
if isinstance(answer, list):
answer = ", ".join(str(a) for a in answer)
pairs.append(f'"{question}"="{answer}"')
return (
"User has answered your questions: "
+ ", ".join(pairs)
+ ". You can now continue with the user's answers in mind."
)


class ClaudeCodeAgent(BaseAgent):
"""Each project gets one ClaudeSDKClient. Session id is tracked externally."""

Expand Down Expand Up @@ -488,21 +507,19 @@ async def run(self, prompt: str, user_id: str = "", channel: str = "") -> AsyncI
)
try:
parsed = await _parse_user_answer(questions, prompt)
content = json.dumps({
"questions": questions,
"answers": parsed["answers"],
})
content = _format_answers_for_tool_result(parsed["answers"])
log.info("parsed AskUserQuestion answers: %s", parsed["answers"])
except Exception:
log.warning(
"failed to parse AskUserQuestion answer with LLM, "
"falling back to response field",
"falling back to raw user reply",
exc_info=True,
)
content = json.dumps({
"questions": questions,
"response": prompt,
})
content = (
"User has answered your questions: "
f"{prompt}. You can now continue with the user's "
"answers in mind."
)
await self._send_tool_result(
client,
tool_use_id=ask["tool_use_id"],
Expand Down
Loading