forked from retoor/devplacepy
Render markdown in gist and project sidebar titles; add content visibility and GIF picker
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3006,3 +3006,207 @@ def test_responses_protocol_default_provider(local_db, monkeypatch):
|
||||
assert resp.status_code == 200
|
||||
payload = json.loads(bytes(resp.body).decode())
|
||||
assert payload["object"] == "chat.completion"
|
||||
|
||||
|
||||
EMULATED_CALL_TEXT_openai_gateway = (
|
||||
"Sure.\n<tool_call>\n"
|
||||
'{"name": "bash", "arguments": {"command": "ls"}}\n'
|
||||
"</tool_call>"
|
||||
)
|
||||
BASH_TOOL_openai_gateway = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "bash",
|
||||
"description": "Run a shell command",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {"command": {"type": "string"}},
|
||||
"required": ["command"],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _emulated_obj_openai_gateway(model, text):
|
||||
obj = _resp_obj_openai_gateway(model)
|
||||
obj["output"][0]["content"][0]["text"] = text
|
||||
return obj
|
||||
|
||||
|
||||
class FakeEmulatedClient_openai_gateway(FakeResponsesClient_openai_gateway):
|
||||
text = EMULATED_CALL_TEXT_openai_gateway
|
||||
|
||||
async def send(self, request, stream=False):
|
||||
self.calls.append((request.url, request.json_body))
|
||||
model = (request.json_body or {}).get("model", "")
|
||||
obj = _emulated_obj_openai_gateway(model, self.text)
|
||||
events = [{"type": "response.created", "response": {"id": "resp_1"}}]
|
||||
for i in range(0, len(self.text), 4):
|
||||
events.append(
|
||||
{"type": "response.output_text.delta", "delta": self.text[i : i + 4]}
|
||||
)
|
||||
events.append({"type": "response.completed", "response": obj})
|
||||
return FakeResponsesResp_openai_gateway(payload=obj, events=events)
|
||||
|
||||
|
||||
def _setup_emulated_route_openai_gateway():
|
||||
from devplacepy.services.openai_gateway import routing
|
||||
|
||||
routing.provider_store.set(
|
||||
routing.ProviderIn(
|
||||
name="gwmeta",
|
||||
base_url="https://api.meta.ai/v1/responses",
|
||||
api_key="sk-test",
|
||||
protocol="responses",
|
||||
)
|
||||
)
|
||||
routing.model_store.set(
|
||||
routing.ModelRouteIn(
|
||||
source_model="meta-chat",
|
||||
provider="gwmeta",
|
||||
target_model="muse-spark-1.3-contributor",
|
||||
kind="chat",
|
||||
tools_mode="emulated",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _emulated_chat_openai_gateway(monkeypatch, body, **tools):
|
||||
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeEmulatedClient_openai_gateway)
|
||||
_setup_emulated_route_openai_gateway()
|
||||
svc = GatewayService()
|
||||
cfg = svc.effective_config()
|
||||
cfg["gateway_vision_enabled"] = False
|
||||
rt = svc.runtime()
|
||||
resp = run_async(rt.handle_chat(body, cfg, ("guest", "test"), "test", "default"))
|
||||
return rt, resp
|
||||
|
||||
|
||||
def test_emulated_tools_meta_responses_nonstream(local_db, monkeypatch):
|
||||
body = _chat_body_openai_gateway(tools=[BASH_TOOL_openai_gateway], tool_choice="auto")
|
||||
try:
|
||||
rt, resp = _emulated_chat_openai_gateway(monkeypatch, body)
|
||||
url, sent = rt._client.calls[-1]
|
||||
assert str(url) == "https://api.meta.ai/v1/responses"
|
||||
assert "tools" not in sent and "tool_choice" not in sent
|
||||
assert "<tools>" in sent["instructions"] and '"bash"' in sent["instructions"]
|
||||
assert resp.status_code == 200
|
||||
message = json.loads(bytes(resp.body).decode())["choices"][0]
|
||||
assert message["finish_reason"] == "tool_calls"
|
||||
call = message["message"]["tool_calls"][0]
|
||||
assert call["function"]["name"] == "bash"
|
||||
assert json.loads(call["function"]["arguments"]) == {"command": "ls"}
|
||||
assert message["message"]["content"] == "Sure."
|
||||
finally:
|
||||
_teardown_responses_route_openai_gateway()
|
||||
|
||||
|
||||
def test_emulated_tools_meta_responses_stream(local_db, monkeypatch):
|
||||
body = _chat_body_openai_gateway(
|
||||
stream=True, tools=[BASH_TOOL_openai_gateway]
|
||||
)
|
||||
try:
|
||||
rt, resp = _emulated_chat_openai_gateway(monkeypatch, body)
|
||||
|
||||
async def drain():
|
||||
out = []
|
||||
async for chunk in resp.body_iterator:
|
||||
out.append(chunk if isinstance(chunk, str) else chunk.decode())
|
||||
return "".join(out)
|
||||
|
||||
raw = run_async(drain())
|
||||
chunks = [
|
||||
json.loads(line[5:])
|
||||
for line in raw.splitlines()
|
||||
if line.startswith("data:") and "[DONE]" not in line
|
||||
]
|
||||
text = "".join(
|
||||
c["choices"][0]["delta"].get("content", "")
|
||||
for c in chunks
|
||||
if c.get("choices")
|
||||
)
|
||||
calls = [
|
||||
tc
|
||||
for c in chunks
|
||||
for choice in c.get("choices") or []
|
||||
for tc in choice["delta"].get("tool_calls") or []
|
||||
]
|
||||
assert "<tool_call>" not in text and text.strip() == "Sure."
|
||||
assert len(calls) == 1 and calls[0]["function"]["name"] == "bash"
|
||||
finishes = [
|
||||
c["choices"][0]["finish_reason"]
|
||||
for c in chunks
|
||||
if c.get("choices") and c["choices"][0].get("finish_reason")
|
||||
]
|
||||
assert finishes[-1] == "tool_calls"
|
||||
assert raw.rstrip().endswith("[DONE]")
|
||||
finally:
|
||||
_teardown_responses_route_openai_gateway()
|
||||
|
||||
|
||||
def test_emulated_tools_convert_history_for_the_meta_transpiler(local_db, monkeypatch):
|
||||
history = [
|
||||
{"role": "user", "content": "go"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": None,
|
||||
"tool_calls": [
|
||||
{
|
||||
"id": "call_1",
|
||||
"type": "function",
|
||||
"function": {"name": "bash", "arguments": '{"command": "ls"}'},
|
||||
}
|
||||
],
|
||||
},
|
||||
{"role": "tool", "tool_call_id": "call_1", "content": "a.txt"},
|
||||
]
|
||||
body = _chat_body_openai_gateway(messages=history, tools=[BASH_TOOL_openai_gateway])
|
||||
try:
|
||||
rt, resp = _emulated_chat_openai_gateway(monkeypatch, body)
|
||||
_, sent = rt._client.calls[-1]
|
||||
assert resp.status_code == 200
|
||||
assert all(item.get("type") == "message" for item in sent["input"])
|
||||
joined = json.dumps(sent["input"])
|
||||
assert "<tool_call>" in joined and "<tool_response>" in joined and "a.txt" in joined
|
||||
assert "function_call" not in joined
|
||||
finally:
|
||||
_teardown_responses_route_openai_gateway()
|
||||
|
||||
|
||||
def test_emulated_tools_over_the_cap_is_a_400_with_no_upstream_call(local_db, monkeypatch):
|
||||
from devplacepy.services.openai_gateway import tool_emulation
|
||||
|
||||
body = _chat_body_openai_gateway(
|
||||
tools=[BASH_TOOL_openai_gateway] * (tool_emulation.MAX_TOOLS + 1)
|
||||
)
|
||||
try:
|
||||
rt, resp = _emulated_chat_openai_gateway(monkeypatch, body)
|
||||
assert resp.status_code == 400
|
||||
assert rt._client.calls == []
|
||||
finally:
|
||||
_teardown_responses_route_openai_gateway()
|
||||
|
||||
|
||||
def test_native_route_keeps_forwarding_tools(local_db, monkeypatch):
|
||||
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeResponsesClient_openai_gateway)
|
||||
FakeResponsesClient_openai_gateway.behavior = "ok"
|
||||
_setup_responses_route_openai_gateway()
|
||||
try:
|
||||
svc = GatewayService()
|
||||
cfg = svc.effective_config()
|
||||
cfg["gateway_vision_enabled"] = False
|
||||
rt = svc.runtime()
|
||||
run_async(
|
||||
rt.handle_chat(
|
||||
_chat_body_openai_gateway(tools=[BASH_TOOL_openai_gateway]),
|
||||
cfg,
|
||||
("guest", "test"),
|
||||
"test",
|
||||
"default",
|
||||
)
|
||||
)
|
||||
_, sent = rt._client.calls[-1]
|
||||
assert "tools" in sent
|
||||
assert "<tools>" not in sent["instructions"]
|
||||
finally:
|
||||
_teardown_responses_route_openai_gateway()
|
||||
|
||||
@@ -0,0 +1,286 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from devplacepy.services.openai_gateway import tool_emulation as te
|
||||
|
||||
BASH = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "bash",
|
||||
"description": "Run a shell command",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {"command": {"type": "string"}},
|
||||
"required": ["command"],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _payload(**over):
|
||||
body = {
|
||||
"model": "m",
|
||||
"messages": [
|
||||
{"role": "system", "content": "Be terse."},
|
||||
{"role": "user", "content": "list files"},
|
||||
],
|
||||
"tools": [BASH],
|
||||
}
|
||||
body.update(over)
|
||||
return body
|
||||
|
||||
|
||||
def _block(name="bash", arguments=None):
|
||||
arguments = {"command": "ls"} if arguments is None else arguments
|
||||
return f'{te.CALL_OPEN}\n{json.dumps({"name": name, "arguments": arguments})}\n{te.CALL_CLOSE}'
|
||||
|
||||
|
||||
def test_request_strips_native_fields_and_injects_catalog():
|
||||
out = te.emulate_request(_payload(tool_choice="auto", parallel_tool_calls=False))
|
||||
for key in te.REMOVED_PAYLOAD_KEYS:
|
||||
assert key not in out
|
||||
system = out["messages"][0]["content"]
|
||||
assert system.startswith("Be terse.")
|
||||
assert '"name": "bash"' in system
|
||||
assert te.CALL_OPEN in system
|
||||
assert "at most one tool call" in system
|
||||
assert out["stop"] == [te.RESPONSE_OPEN]
|
||||
|
||||
|
||||
def test_request_does_not_mutate_the_input():
|
||||
original = _payload()
|
||||
snapshot = json.dumps(original, sort_keys=True)
|
||||
te.emulate_request(original)
|
||||
assert json.dumps(original, sort_keys=True) == snapshot
|
||||
|
||||
|
||||
def test_system_message_is_created_when_absent():
|
||||
out = te.emulate_request(_payload(messages=[{"role": "user", "content": "hi"}]))
|
||||
assert out["messages"][0]["role"] == "system"
|
||||
assert "<tools>" in out["messages"][0]["content"]
|
||||
|
||||
|
||||
def test_tool_choice_none_leaves_the_catalog_out():
|
||||
out = te.emulate_request(_payload(tool_choice="none"))
|
||||
assert "<tools>" not in out["messages"][0]["content"]
|
||||
|
||||
|
||||
def test_tool_choice_required_and_named_add_a_directive():
|
||||
required = te.emulate_request(_payload(tool_choice="required"))
|
||||
assert "MUST call at least one tool" in required["messages"][0]["content"]
|
||||
named = te.emulate_request(
|
||||
_payload(tool_choice={"type": "function", "function": {"name": "bash"}})
|
||||
)
|
||||
assert 'MUST call the tool "bash"' in named["messages"][0]["content"]
|
||||
|
||||
|
||||
def test_history_roundtrips_into_the_same_format_the_model_emits():
|
||||
history = [
|
||||
{"role": "user", "content": "go"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "ok",
|
||||
"tool_calls": [
|
||||
{
|
||||
"id": "call_1",
|
||||
"type": "function",
|
||||
"function": {"name": "bash", "arguments": '{"command": "ls"}'},
|
||||
},
|
||||
{
|
||||
"id": "call_2",
|
||||
"type": "function",
|
||||
"function": {"name": "bash", "arguments": '{"command": "pwd"}'},
|
||||
},
|
||||
],
|
||||
},
|
||||
{"role": "tool", "tool_call_id": "call_1", "content": "a.txt"},
|
||||
{"role": "tool", "tool_call_id": "call_2", "content": "/tmp"},
|
||||
]
|
||||
out = te.emulate_request(_payload(messages=history))
|
||||
roles = [m["role"] for m in out["messages"]]
|
||||
assert roles == ["system", "user", "assistant", "user"]
|
||||
assistant = out["messages"][2]
|
||||
assert "tool_calls" not in assistant
|
||||
assert assistant["content"].count(te.CALL_OPEN) == 2
|
||||
results = out["messages"][3]["content"]
|
||||
assert results.count(te.RESPONSE_OPEN) == 2
|
||||
assert '"name": "bash"' in results and "a.txt" in results
|
||||
|
||||
|
||||
def test_caps_reject_loudly():
|
||||
with pytest.raises(te.ToolEmulationError):
|
||||
te.emulate_request(_payload(tools=[BASH] * (te.MAX_TOOLS + 1)))
|
||||
huge = json.loads(json.dumps(BASH))
|
||||
huge["function"]["description"] = "x" * (te.MAX_CATALOG_CHARS + 1)
|
||||
with pytest.raises(te.ToolEmulationError):
|
||||
te.emulate_request(_payload(tools=[huge]))
|
||||
with pytest.raises(te.ToolEmulationError):
|
||||
te.emulate_request(_payload(tools=[{"type": "retrieval"}]))
|
||||
|
||||
|
||||
def test_existing_stop_sequences_are_kept_and_capped():
|
||||
out = te.emulate_request(_payload(stop="END"))
|
||||
assert out["stop"] == ["END", te.RESPONSE_OPEN]
|
||||
full = te.emulate_request(_payload(stop=["a", "b", "c", "d"]))
|
||||
assert full["stop"] == ["a", "b", "c", "d"]
|
||||
|
||||
|
||||
def test_request_without_tools_or_tool_history_is_untouched():
|
||||
plain = {"model": "m", "messages": [{"role": "user", "content": "hi"}]}
|
||||
assert te.emulate_request(plain) is plain
|
||||
|
||||
|
||||
def test_parse_extracts_calls_and_remaining_text():
|
||||
declared = te.declared_tools(_payload())
|
||||
text = "Sure.\n" + _block() + "\n" + _block(arguments={"command": "pwd"})
|
||||
remaining, calls, warnings = te.parse_tool_calls(text, declared)
|
||||
assert remaining == "Sure."
|
||||
assert [json.loads(c["function"]["arguments"])["command"] for c in calls] == ["ls", "pwd"]
|
||||
assert all(c["id"].startswith("call_") and c["type"] == "function" for c in calls)
|
||||
assert warnings == []
|
||||
|
||||
|
||||
def test_parse_is_tolerant_of_fences_arrays_and_unterminated_blocks():
|
||||
declared = te.declared_tools(_payload())
|
||||
fenced = f'{te.CALL_OPEN}\n```json\n{{"name": "bash", "arguments": {{"command": "ls"}}}}\n```\n{te.CALL_CLOSE}'
|
||||
assert len(te.parse_tool_calls(fenced, declared)[1]) == 1
|
||||
array = f'{te.CALL_OPEN}[{{"name":"bash","arguments":{{"command":"a"}}}},{{"name":"bash","arguments":{{"command":"b"}}}}]{te.CALL_CLOSE}'
|
||||
assert len(te.parse_tool_calls(array, declared)[1]) == 2
|
||||
unterminated = f'{te.CALL_OPEN}{{"name": "bash", "arguments": {{"command": "ls"}}}}'
|
||||
assert len(te.parse_tool_calls(unterminated, declared)[1]) == 1
|
||||
string_args = f'{te.CALL_OPEN}{{"name":"bash","arguments":"{{\\"command\\": \\"ls\\"}}"}}{te.CALL_CLOSE}'
|
||||
assert len(te.parse_tool_calls(string_args, declared)[1]) == 1
|
||||
|
||||
|
||||
def test_parse_rejects_invalid_calls_loudly_and_keeps_the_text():
|
||||
declared = te.declared_tools(_payload())
|
||||
for bad in (
|
||||
_block(name="nope"),
|
||||
_block(arguments={}),
|
||||
f"{te.CALL_OPEN}not json{te.CALL_CLOSE}",
|
||||
):
|
||||
remaining, calls, warnings = te.parse_tool_calls(bad, declared)
|
||||
assert calls == []
|
||||
assert warnings
|
||||
assert te.CALL_OPEN in remaining
|
||||
|
||||
|
||||
def test_parallel_and_per_turn_caps():
|
||||
declared = te.declared_tools(_payload())
|
||||
text = "".join(_block() for _ in range(3))
|
||||
_, calls, warnings = te.parse_tool_calls(text, declared, allow_parallel=False)
|
||||
assert len(calls) == 1 and warnings
|
||||
many = "".join(_block() for _ in range(te.MAX_CALLS_PER_TURN + 3))
|
||||
assert len(te.parse_tool_calls(many, declared)[1]) == te.MAX_CALLS_PER_TURN
|
||||
|
||||
|
||||
def test_convert_completion_builds_openai_tool_calls():
|
||||
declared = te.declared_tools(_payload())
|
||||
completion = {"choices": [{"message": {"role": "assistant", "content": "x\n" + _block()}, "finish_reason": "stop"}]}
|
||||
out, warnings = te.convert_completion(completion, declared)
|
||||
choice = out["choices"][0]
|
||||
assert choice["finish_reason"] == "tool_calls"
|
||||
assert choice["message"]["content"] == "x"
|
||||
assert choice["message"]["tool_calls"][0]["function"]["name"] == "bash"
|
||||
assert warnings == []
|
||||
|
||||
|
||||
def test_convert_completion_with_only_a_call_has_null_content():
|
||||
declared = te.declared_tools(_payload())
|
||||
completion = {"choices": [{"message": {"content": _block()}, "finish_reason": "stop"}]}
|
||||
out, _ = te.convert_completion(completion, declared)
|
||||
assert out["choices"][0]["message"]["content"] is None
|
||||
|
||||
|
||||
def _chunk(content=None, finish=None, **extra):
|
||||
delta = {} if content is None else {"content": content}
|
||||
return {
|
||||
"id": "c",
|
||||
"object": "chat.completion.chunk",
|
||||
"model": "m",
|
||||
"choices": [{"index": 0, "delta": delta, "finish_reason": finish}],
|
||||
**extra,
|
||||
}
|
||||
|
||||
|
||||
def _run_stream(pieces, declared=None, finish="stop", allow_parallel=True):
|
||||
stream = te.ToolCallStream(declared or te.declared_tools(_payload()), allow_parallel)
|
||||
out = []
|
||||
for piece in pieces:
|
||||
out.extend(stream.feed(_chunk(piece)))
|
||||
out.extend(stream.feed(_chunk(None, finish)))
|
||||
return out
|
||||
|
||||
|
||||
def _texts(chunks):
|
||||
return "".join(
|
||||
c["choices"][0]["delta"].get("content", "") for c in chunks if c.get("choices")
|
||||
)
|
||||
|
||||
|
||||
def _calls(chunks):
|
||||
found = []
|
||||
for c in chunks:
|
||||
for choice in c.get("choices") or []:
|
||||
found.extend(choice["delta"].get("tool_calls") or [])
|
||||
return found
|
||||
|
||||
|
||||
def test_stream_passes_plain_text_through_unchanged():
|
||||
out = _run_stream(["hel", "lo ", "world"])
|
||||
assert _texts(out) == "hello world"
|
||||
assert _calls(out) == []
|
||||
assert out[-1]["choices"][0]["finish_reason"] == "stop"
|
||||
|
||||
|
||||
def test_stream_converts_a_call_split_across_arbitrary_chunk_boundaries():
|
||||
full = "Sure.\n" + _block()
|
||||
for size in (1, 2, 3, 5, 7, 50):
|
||||
pieces = [full[i : i + size] for i in range(0, len(full), size)]
|
||||
out = _run_stream(pieces)
|
||||
assert _texts(out).strip() == "Sure."
|
||||
calls = _calls(out)
|
||||
assert len(calls) == 1
|
||||
assert calls[0]["function"]["name"] == "bash"
|
||||
assert json.loads(calls[0]["function"]["arguments"]) == {"command": "ls"}
|
||||
assert calls[0]["index"] == 0
|
||||
assert out[-1]["choices"][0]["finish_reason"] == "tool_calls"
|
||||
|
||||
|
||||
def test_stream_never_leaks_call_markup_into_content():
|
||||
out = _run_stream(["a <tool", "_call>", '{"name":"bash","arguments":{"command":"ls"}}', "</tool_call>"])
|
||||
assert te.CALL_OPEN not in _texts(out)
|
||||
|
||||
|
||||
def test_stream_emits_a_lookalike_prefix_that_never_became_a_call():
|
||||
out = _run_stream(["use <tool", " carefully"])
|
||||
assert _texts(out) == "use <tool carefully"
|
||||
|
||||
|
||||
def test_stream_invalid_call_is_flushed_as_text_with_a_warning():
|
||||
stream = te.ToolCallStream(te.declared_tools(_payload()))
|
||||
out = []
|
||||
for piece in [_block(name="nope")]:
|
||||
out.extend(stream.feed(_chunk(piece)))
|
||||
out.extend(stream.feed(_chunk(None, "stop")))
|
||||
assert _calls(out) == []
|
||||
assert te.CALL_OPEN in _texts(out)
|
||||
assert stream.warnings
|
||||
|
||||
|
||||
def test_stream_without_a_finish_chunk_flushes_on_finish():
|
||||
stream = te.ToolCallStream(te.declared_tools(_payload()))
|
||||
out = stream.feed(_chunk(_block()))
|
||||
out.extend(stream.finish())
|
||||
assert len(_calls(out)) == 1
|
||||
|
||||
|
||||
def test_stream_keeps_usage_chunks_and_other_deltas():
|
||||
stream = te.ToolCallStream(te.declared_tools(_payload()))
|
||||
usage = {"id": "c", "choices": [], "usage": {"total_tokens": 3}}
|
||||
assert stream.feed(usage) == [usage]
|
||||
out = stream.feed({"id": "c", "choices": [{"index": 0, "delta": {"role": "assistant"}, "finish_reason": None}]})
|
||||
assert out[0]["choices"][0]["delta"] == {"role": "assistant"}
|
||||
Reference in New Issue
Block a user