mirror of
https://github.com/usestrix/strix.git
synced 2026-09-08 22:21:05 +00:00
Merge 907fbe624a into ff5c8cc8e4
This commit is contained in:
commit
f912728d9b
2 changed files with 102 additions and 1 deletions
|
|
@ -5,6 +5,7 @@ from __future__ import annotations
|
|||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import tempfile
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
|
|
@ -31,6 +32,24 @@ TERMINAL_STATUSES: frozenset[str] = frozenset({"completed", "stopped", "crashed"
|
|||
# on other agents is re-checked on a timer.
|
||||
WaitKind = Literal["user", "agents", "stalled"]
|
||||
|
||||
_CHATGPT_TRANSCRIPT_TAGS = (
|
||||
"analysis",
|
||||
"assistant",
|
||||
"channel",
|
||||
"final",
|
||||
"user",
|
||||
)
|
||||
_CHATGPT_TRANSCRIPT_TAG_ALTERNATION = "|".join(re.escape(tag) for tag in _CHATGPT_TRANSCRIPT_TAGS)
|
||||
_OPTIONAL_TAG_ATTRIBUTES_PATTERN = r"(?:\s+[^>]*)?"
|
||||
_CHATGPT_TRANSCRIPT_TAG_RE = re.compile(
|
||||
rf"</?(?:{_CHATGPT_TRANSCRIPT_TAG_ALTERNATION}){_OPTIONAL_TAG_ATTRIBUTES_PATTERN}\s*/?>",
|
||||
flags=re.IGNORECASE,
|
||||
)
|
||||
|
||||
|
||||
def _strip_chatgpt_transcript_tags(content: str) -> str:
|
||||
return _CHATGPT_TRANSCRIPT_TAG_RE.sub("", content)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class AgentRuntime:
|
||||
|
|
@ -413,7 +432,7 @@ class AgentCoordinator:
|
|||
await self._maybe_snapshot()
|
||||
|
||||
async def cancel_descendants(self, agent_id: str) -> None:
|
||||
tasks = []
|
||||
tasks: list[asyncio.Task[Any]] = []
|
||||
async with self._lock:
|
||||
for aid in reversed(self._subtree_order_locked(agent_id)):
|
||||
task = self.runtimes.get(aid, AgentRuntime()).task
|
||||
|
|
@ -481,6 +500,7 @@ class AgentCoordinator:
|
|||
content = str(message.get("content", ""))
|
||||
if sender == "user":
|
||||
return cast("TResponseInputItem", {"role": "user", "content": content})
|
||||
content = _strip_chatgpt_transcript_tags(content)
|
||||
sender_name = self.names.get(sender, sender)
|
||||
msg_type = message.get("type", "information")
|
||||
priority = message.get("priority", "normal")
|
||||
|
|
|
|||
81
tests/test_agent_messages.py
Normal file
81
tests/test_agent_messages.py
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
"""Tests for agent-to-session message conversion."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from strix.core.agents import AgentCoordinator
|
||||
|
||||
|
||||
CHATGPT_TRANSCRIPT_CONTENT = (
|
||||
"<analysis>checked target</analysis>\n<channel>final</channel>\n<final>done</final>"
|
||||
)
|
||||
CHATGPT_TRANSCRIPT_CONTENT_WITH_ATTRIBUTES = (
|
||||
'<analysis trace="true">checked target</analysis>\n'
|
||||
'<channel name="final">final</channel>\n'
|
||||
'<final reason="done">done</final>\n'
|
||||
"<assistant/>"
|
||||
)
|
||||
USER_LITERAL_TAG_CONTENT = (
|
||||
"Please preserve this XML-like snippet: "
|
||||
'<analysis>literal user content</analysis> and <final reason="example">done</final>'
|
||||
)
|
||||
|
||||
|
||||
def test_message_to_session_item_strips_chatgpt_transcript_tags() -> None:
|
||||
coordinator = AgentCoordinator()
|
||||
coordinator.names["child"] = "Researcher"
|
||||
|
||||
item = coordinator._message_to_session_item(
|
||||
{
|
||||
"from": "child",
|
||||
"type": "information",
|
||||
"priority": "normal",
|
||||
"content": CHATGPT_TRANSCRIPT_CONTENT,
|
||||
}
|
||||
)
|
||||
|
||||
content = str(item["content"])
|
||||
|
||||
assert "<analysis>" not in content
|
||||
assert "</analysis>" not in content
|
||||
assert "<channel>" not in content
|
||||
assert "</channel>" not in content
|
||||
assert "<final>" not in content
|
||||
assert "</final>" not in content
|
||||
assert "checked target" in content
|
||||
assert "done" in content
|
||||
|
||||
|
||||
def test_message_to_session_item_preserves_user_literal_tags() -> None:
|
||||
coordinator = AgentCoordinator()
|
||||
|
||||
item = coordinator._message_to_session_item(
|
||||
{
|
||||
"from": "user",
|
||||
"content": USER_LITERAL_TAG_CONTENT,
|
||||
}
|
||||
)
|
||||
|
||||
assert item["content"] == USER_LITERAL_TAG_CONTENT
|
||||
|
||||
|
||||
def test_message_to_session_item_strips_attributed_chatgpt_transcript_tags() -> None:
|
||||
coordinator = AgentCoordinator()
|
||||
coordinator.names["child"] = "Researcher"
|
||||
|
||||
item = coordinator._message_to_session_item(
|
||||
{
|
||||
"from": "child",
|
||||
"type": "information",
|
||||
"priority": "normal",
|
||||
"content": CHATGPT_TRANSCRIPT_CONTENT_WITH_ATTRIBUTES,
|
||||
}
|
||||
)
|
||||
|
||||
content = str(item["content"])
|
||||
|
||||
assert '<analysis trace="true">' not in content
|
||||
assert '<channel name="final">' not in content
|
||||
assert '<final reason="done">' not in content
|
||||
assert "<assistant/>" not in content
|
||||
assert "checked target" in content
|
||||
assert "done" in content
|
||||
Loading…
Add table
Reference in a new issue