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()
|
||||
|
||||
Reference in New Issue
Block a user