feat(http_server): add section headers to _payload_to_text for model parsing

The flattened text output now uses markdown headers so the model can
distinguish L0 identity from L1 summary from L2 episodes from handles.
This replaces undifferentiated newline-separated text with structured
sections: ## L0 Identity, ## L1 Recent Summary, ## Rich Club,
## L2 Episode, ## Handles.
This commit is contained in:
Apunkt 2026-06-03 16:50:02 +02:00
parent 7559bac57f
commit eec312dcb4
No known key found for this signature in database
2 changed files with 7 additions and 5 deletions

View file

@ -73,16 +73,17 @@ def _payload_to_text(result: Any) -> str:
if not isinstance(result, dict):
return str(result)
parts: list[str] = []
headers = {"l0": "## L0 Identity", "l1": "## L1 Recent Summary", "rich_club": "## Rich Club"}
for key in ("l0", "l1", "rich_club"):
val = result.get(key)
if isinstance(val, str) and val.strip():
parts.append(val.strip())
parts.append(f"{headers[key]}\n{val.strip()}")
l2 = result.get("l2")
if isinstance(l2, list):
for item in l2:
text = item if isinstance(item, str) else json.dumps(item, ensure_ascii=False)
if text.strip():
parts.append(text.strip())
parts.append(f"## L2 Episode\n{text.strip()}")
# Compact handles (populated at minimal; also present at standard/deep).
handles = [
result.get(k)
@ -90,7 +91,7 @@ def _payload_to_text(result: Any) -> str:
]
handles = [h for h in handles if isinstance(h, str) and h.strip()]
if handles:
parts.append(" ".join(handles))
parts.append(f"## Handles\n{' '.join(handles)}")
return "\n\n".join(parts)

View file

@ -158,7 +158,8 @@ def test_session_context_text_format(tmp_path, monkeypatch):
assert headers["content-type"].startswith("text/plain")
# Flattened block keeps the human layers in order, drops token ints.
assert body == (
"pinned identity\n\nrecent summary\n\nhub concepts\n\nepisode one\n\nepisode two"
"## L0 Identity\npinned identity\n\n## L1 Recent Summary\nrecent summary\n\n"
"## Rich Club\nhub concepts\n\n## L2 Episode\nepisode one\n\n## L2 Episode\nepisode two"
)
@ -168,7 +169,7 @@ def test_payload_to_text_renders_handles_at_minimal():
from iai_mcp.http_server import _payload_to_text
text = _payload_to_text(_fake_dispatch(None, "minimal_payload", {}))
assert text == "<iai:970d1b7629da7946> <sess:abc pend:2> <topic:d3256b25>"
assert text == "## Handles\n<iai:970d1b7629da7946> <sess:abc pend:2> <topic:d3256b25>"
def test_rpc_post_passthrough(tmp_path, monkeypatch):