fix(rpc): harden Python protocol compatibility

This commit is contained in:
Wolfgang Schoenberger
2026-07-29 05:44:49 -07:00
parent 4df68d6043
commit 0f033ed706
6 changed files with 208 additions and 15 deletions
+56
View File
@@ -4,6 +4,8 @@ import unittest
from omp_rpc import (
AgentEndEvent,
AutoCompactionEndEvent,
AutoCompactionStartEvent,
ExtensionUiRequest,
SessionState,
TodoReminderEvent,
@@ -79,6 +81,11 @@ class ProtocolParsingTests(unittest.TestCase):
"parameters": {"type": "object"},
}
],
"contextUsage": {
"tokens": 12345,
"contextWindow": 200000,
"percent": 6.1725,
},
}
)
@@ -90,6 +97,10 @@ class ProtocolParsingTests(unittest.TestCase):
# Legacy bare-string systemPrompt is accepted and wrapped to a tuple.
self.assertEqual(state.system_prompt, ("You are useful.",))
self.assertEqual(state.dump_tools[0].name, "read")
assert state.context_usage is not None
self.assertEqual(state.context_usage.tokens, 12345)
self.assertEqual(state.context_usage.context_window, 200000)
self.assertEqual(state.context_usage.percent, 6.1725)
assert state.model is not None and state.model.thinking is not None
self.assertEqual(
state.model.thinking.efforts, ("minimal", "low", "medium", "high")
@@ -161,16 +172,43 @@ class ProtocolParsingTests(unittest.TestCase):
}
],
"messageCount": 1,
"isTerminal": False,
}
)
self.assertIsInstance(notification, AgentEndEvent)
self.assertEqual(assistant_text(notification.messages[0]), "hello")
self.assertEqual(notification.message_count, 1)
self.assertFalse(notification.is_terminal)
legacy = AgentEndEvent(notification.messages, "agent_end")
self.assertEqual(legacy.type, "agent_end")
self.assertIsNone(legacy.message_count)
self.assertIsNone(legacy.is_terminal)
def test_parse_current_compaction_variants(self) -> None:
start = parse_notification(
{
"type": "auto_compaction_start",
"reason": "incomplete",
"action": "snapcompact",
}
)
end = parse_notification(
{
"type": "auto_compaction_end",
"action": "shake",
"result": None,
"aborted": False,
"willRetry": False,
}
)
self.assertIsInstance(start, AutoCompactionStartEvent)
self.assertEqual(start.reason, "incomplete")
self.assertEqual(start.action, "snapcompact")
self.assertIsInstance(end, AutoCompactionEndEvent)
self.assertEqual(end.action, "shake")
def test_parse_extension_ui_request(self) -> None:
notification = parse_notification(
@@ -191,6 +229,24 @@ class ProtocolParsingTests(unittest.TestCase):
self.assertTrue(notification.requires_response())
self.assertFalse(notification.is_passive())
def test_parse_open_url_request(self) -> None:
notification = parse_notification(
{
"type": "extension_ui_request",
"id": "ui-oauth",
"method": "open_url",
"url": "https://example.com/oauth",
"launchUrl": "http://127.0.0.1:8123/redirect",
"instructions": "Open this URL to continue.",
}
)
self.assertIsInstance(notification, ExtensionUiRequest)
self.assertEqual(notification.method, "open_url")
self.assertEqual(notification.url, "https://example.com/oauth")
self.assertEqual(notification.launch_url, "http://127.0.0.1:8123/redirect")
self.assertTrue(notification.is_passive())
def test_parse_todo_reminder_notification(self) -> None:
notification = parse_notification(
{