Agent Zero AI framework
Pass through JSON objects with thoughts or headline
linklitie970e1e89435
diff --git a/plugins/_context_doctor/helpers/context_doctor.py b/plugins/_context_doctor/helpers/context_doctor.pyindex 4f4f7b48e..57bcea233 100644--- a/plugins/_context_doctor/helpers/context_doctor.py+++ b/plugins/_context_doctor/helpers/context_doctor.py@@ -49,23 +49,29 @@ def transform_response(response: str, *, suppress_xml: bool) -> str:apply_patch()try:- repaired = repair_json(+ salvage_repaired = repair_json(response,return_objects=True,schema=_A0_SALVAGE_SCHEMA,schema_repair_mode="salvage",)except Exception:- repaired = repair_json(response, return_objects=True)+ salvage_repaired = repair_json(response, return_objects=True)- candidates = repaired if isinstance(repaired, list) else [repaired]+ candidates = (+ salvage_repaired+ if isinstance(salvage_repaired, list)+ else [salvage_repaired]+ )try:no_schema = repair_json(response, return_objects=True)except Exception:no_schema = Noneif isinstance(no_schema, list):valid = [item for item in no_schema if _is_tool_call(item)]- if len(valid) > 1 or (len(valid) == 1 and not _is_tool_call(repaired)):+ if len(valid) > 1 or (+ len(valid) == 1 and not _is_tool_call(salvage_repaired)+ ):candidates = no_schemarepaired = max((item for item in candidates if _is_tool_call(item)),@@ -74,10 +80,19 @@ def transform_response(response: str, *, suppress_xml: bool) -> str:)except Exception:repaired = None+ salvage_repaired = Noneif _is_tool_call(repaired):return json.dumps(repaired, ensure_ascii=False, separators=(",", ":"))+ for candidate in (repaired, salvage_repaired):+ if isinstance(candidate, dict) and (+ "thoughts" in candidate or "headline" in candidate+ ):+ return json.dumps(+ candidate, ensure_ascii=False, separators=(",", ":")+ )+if suppress_xml and "<" in response and ">" in response:return "{}"return json.dumps({"thoughts": [response]}, ensure_ascii=False, separators=(",", ":"))diff --git a/plugins/_context_doctor/tests/test_context_doctor.py b/plugins/_context_doctor/tests/test_context_doctor.pyindex 8ac47e1af..40bd7a4ee 100644--- a/plugins/_context_doctor/tests/test_context_doctor.py+++ b/plugins/_context_doctor/tests/test_context_doctor.py@@ -31,6 +31,24 @@ def test_wraps_raw_text_in_thoughts():)+def test_does_not_wrap_json_with_thoughts_or_headline():+ response = '{"thoughts":["Reasoning"],"headline":"A title"}'++ assert transform_response(response, suppress_xml=True) == response+++def test_does_not_wrap_json_with_only_headline():+ response = '{"headline":"Just a headline"}'++ assert transform_response(response, suppress_xml=True) == response+++def test_does_not_wrap_json_with_only_thoughts():+ response = '{"thoughts":["only thoughts"]}'++ assert transform_response(response, suppress_xml=True) == response++def test_suppresses_xml_when_enabled():assert transform_response("<tool>response</tool>", suppress_xml=True) == "{}"assert transform_response("<tool>response</tool>", suppress_xml=False) == (