Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,8 @@ def _clear_responses_pending_state(self) -> None:

@extension.extensible
async def process_tools(self, msg: str):
# Pre-flight: normalize any non-tool-call output before the framework sees it
msg = extract_tools.ensure_valid_tool_response(msg)
# search for tool usage requests in agent message
tool_request = extract_tools.json_parse_dirty(msg)

Expand Down
32 changes: 32 additions & 0 deletions helpers/extract_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,35 @@ def replace_unescaped_newlines(match):
r'(?<=: ")(.*?)(?=")', replace_unescaped_newlines, json_string, flags=re.DOTALL
)
return fixed_string


def ensure_valid_tool_response(raw_response: str) -> str:
"""
Pre-flight checker: if the agent's own output isn't a valid tool-call
envelope, normalize it into one BEFORE the framework sees it.

Priority ladder:
1. Already valid {"tool_name":..., "tool_args":{...}} -> pass through
2. Has a parseable json_root with tool_name but missing args -> wrap in response()
3. Completely non-JSON (plain prose) -> wrap entire output as response().text
4. Empty/null -> return valid empty response()
"""
import json
if not raw_response or not isinstance(raw_response, str):
return json.dumps({"tool_name": "response", "tool_args": {"text": ""}})

stripped = raw_response.strip()

# Case 1: Already valid tool-call envelope
if stripped.startswith('{'):
parsed = json_parse_dirty(stripped)
if parsed and _is_tool_request(parsed):
return stripped # pass through - safe
# Case 2: It's JSON but not a tool call - wrap in response()
if parsed and isinstance(parsed, dict):
content = json.dumps(parsed, ensure_ascii=False)
return json.dumps({"tool_name": "response", "tool_args": {"text": content}})

# Case 3: Not valid JSON at all - wrap entire output as response() text
safe_text = stripped.replace('\\', '\\\\').replace('"', '\\"')[:32000]
return json.dumps({"tool_name": "response", "tool_args": {"text": safe_text}})