Spaces:
Running on Zero
Running on Zero
File size: 6,797 Bytes
56f6a56 fdbf570 56f6a56 fdbf570 56f6a56 6464112 56f6a56 fdbf570 6464112 56f6a56 fdbf570 56f6a56 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | """Tests for the notebook-independent OpenClaude adapter."""
from __future__ import annotations
import unittest
from openclaude_compat import (
TOOL_PROTOCOL_MARKER,
TOOL_RECAP_CHARACTERS,
add_system_instruction,
has_tool_protocol,
normalize_openclaude_messages,
)
from openai_compat import tool_protocol_instruction
TOOLS = [
{
"type": "function",
"function": {
"name": "WebFetch",
"description": "Fetch a page.",
"parameters": {
"type": "object",
"properties": {
"url": {"type": "string"},
"prompt": {"type": "string"},
},
"required": ["url", "prompt"],
},
},
}
]
class OpenClaudeCompatibilityTests(unittest.TestCase):
def test_parallel_results_are_mapped_by_id_and_stay_contiguous(self) -> None:
normalized = normalize_openclaude_messages(
[
{"role": "user", "content": "Faça."},
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "read_id",
"type": "function",
"function": {
"name": "Read",
"arguments": '{"file_path":"/tmp/a"}',
},
},
{
"id": "bash_id",
"type": "function",
"function": {
"name": "Bash",
"arguments": '{"command":"pwd"}',
},
},
],
},
{
"role": "tool",
"tool_call_id": "bash_id",
"content": "/root",
},
{
"role": "tool",
"tool_call_id": "read_id",
"content": "1→source",
},
]
)
self.assertEqual(
[message["role"] for message in normalized],
["user", "assistant", "tool", "tool", "user"],
)
self.assertEqual(normalized[2]["name"], "Bash")
self.assertEqual(normalized[3]["name"], "Read")
self.assertIn("Bash result:\n/root", normalized[4]["content"])
self.assertIn("source", normalized[4]["content"])
self.assertNotIn("1→", normalized[4]["content"])
def test_read_recap_is_bounded_and_preserves_head_and_tail(self) -> None:
content = "\n".join(
f"{index}→line-{index}" for index in range(3000)
)
normalized = normalize_openclaude_messages(
[
{"role": "user", "content": "Leia."},
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "read_id",
"type": "function",
"function": {
"name": "Read",
"arguments": '{"file_path":"/tmp/large.txt"}',
},
}
],
},
{
"role": "tool",
"tool_call_id": "read_id",
"content": content,
},
]
)
recap = normalized[-1]["content"]
self.assertLess(len(recap), TOOL_RECAP_CHARACTERS + 100)
self.assertIn("line-0", recap)
self.assertIn("line-2999", recap)
self.assertIn("characters omitted", recap)
def test_unknown_tool_call_id_is_client_error(self) -> None:
with self.assertRaisesRegex(ValueError, "unknown tool_call_id"):
normalize_openclaude_messages(
[
{
"role": "tool",
"tool_call_id": "missing",
"content": "result",
}
]
)
def test_continuation_nudge_and_system_reminder_are_removed(self) -> None:
normalized = normalize_openclaude_messages(
[
{"role": "user", "content": "Faça."},
{
"role": "user",
"content": (
"<system-reminder>internal</system-reminder>"
"Continue with the task. If you were interrupted, "
"resume your thought."
),
},
]
)
self.assertEqual(normalized, [{"role": "user", "content": "Faça."}])
def test_protocol_keeps_webfetch_constraint_without_schema_duplication(self) -> None:
instruction = tool_protocol_instruction(TOOLS)
self.assertIn(TOOL_PROTOCOL_MARKER, instruction)
self.assertIn("WebFetch requires both url and prompt", instruction)
self.assertIn("Available tool names:", instruction)
self.assertNotIn('"parameters":', instruction)
def test_protocol_does_not_call_unlisted_toolsearch(self) -> None:
instruction = tool_protocol_instruction(
[
{
"type": "function",
"function": {
"name": "Bash",
"description": "Run a command.",
"parameters": {"type": "object"},
},
}
]
)
self.assertIn("Deferred tools are unavailable", instruction)
self.assertNotIn("ToolSearch", instruction)
def test_instruction_is_inserted_before_latest_user(self) -> None:
prepared = add_system_instruction(
[
{"role": "system", "content": "base"},
{"role": "user", "content": "first"},
{"role": "assistant", "content": "reply"},
{"role": "user", "content": "latest"},
],
"policy",
)
self.assertEqual(prepared[-2], {"role": "system", "content": "policy"})
self.assertEqual(prepared[-1]["content"], "latest")
def test_existing_protocol_is_detected(self) -> None:
self.assertTrue(
has_tool_protocol(
[{"role": "system", "content": TOOL_PROTOCOL_MARKER}]
)
)
self.assertFalse(has_tool_protocol([{"role": "user", "content": "oi"}]))
if __name__ == "__main__":
unittest.main()
|