|
| 1 | +import importlib |
| 2 | +import json |
| 3 | +import socket |
| 4 | +import sys |
| 5 | +import threading |
| 6 | +import time |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import pytest |
| 10 | +import uvicorn |
| 11 | +from fastapi import FastAPI, Request |
| 12 | +from fastapi.responses import StreamingResponse |
| 13 | +from fastapi.testclient import TestClient |
| 14 | + |
| 15 | +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) |
| 16 | + |
| 17 | +TOKEN = "test-token" |
| 18 | +RUN = { |
| 19 | + "threadId": "thread-1", |
| 20 | + "runId": "run-1", |
| 21 | + "state": {}, |
| 22 | + "messages": [{"id": "m1", "role": "user", "content": "Say hello"}], |
| 23 | + "tools": [], |
| 24 | + "context": [], |
| 25 | + "forwardedProps": {}, |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +def _sse(events): |
| 30 | + async def stream(): |
| 31 | + for name, data in events: |
| 32 | + yield f"event: {name}\ndata: {json.dumps(data)}\n\n" |
| 33 | + |
| 34 | + return StreamingResponse(stream(), media_type="text/event-stream") |
| 35 | + |
| 36 | + |
| 37 | +def _provider_app(seen): |
| 38 | + app = FastAPI() |
| 39 | + |
| 40 | + # `openai:` is the Responses API in Pydantic AI, so that is the route an OpenAI key and an |
| 41 | + # OpenAI-compatible endpoint both reach. |
| 42 | + @app.post("/v1/responses") |
| 43 | + async def openai_responses(request: Request): |
| 44 | + body = await request.json() |
| 45 | + seen.append(("openai", body["model"])) |
| 46 | + response = { |
| 47 | + "id": "resp", |
| 48 | + "object": "response", |
| 49 | + "created_at": 0, |
| 50 | + "model": body["model"], |
| 51 | + "status": "in_progress", |
| 52 | + "output": [], |
| 53 | + "parallel_tool_calls": True, |
| 54 | + "tool_choice": "auto", |
| 55 | + "tools": [], |
| 56 | + } |
| 57 | + item = {"id": "msg", "type": "message", "role": "assistant", "status": "completed"} |
| 58 | + text = {"type": "output_text", "text": "hello", "annotations": []} |
| 59 | + done = { |
| 60 | + **response, |
| 61 | + "status": "completed", |
| 62 | + "output": [{**item, "content": [text]}], |
| 63 | + "usage": { |
| 64 | + "input_tokens": 1, |
| 65 | + "input_tokens_details": {"cached_tokens": 0}, |
| 66 | + "output_tokens": 1, |
| 67 | + "output_tokens_details": {"reasoning_tokens": 0}, |
| 68 | + "total_tokens": 2, |
| 69 | + }, |
| 70 | + } |
| 71 | + return _sse( |
| 72 | + [ |
| 73 | + ("response.created", {"type": "response.created", "sequence_number": 0, "response": response}), |
| 74 | + ("response.output_item.added", {"type": "response.output_item.added", "sequence_number": 1, "output_index": 0, "item": {**item, "status": "in_progress", "content": []}}), |
| 75 | + ("response.output_text.delta", {"type": "response.output_text.delta", "sequence_number": 2, "item_id": "msg", "output_index": 0, "content_index": 0, "delta": "hello", "logprobs": []}), |
| 76 | + ("response.output_item.done", {"type": "response.output_item.done", "sequence_number": 3, "output_index": 0, "item": {**item, "content": [text]}}), |
| 77 | + ("response.completed", {"type": "response.completed", "sequence_number": 4, "response": done}), |
| 78 | + ] |
| 79 | + ) |
| 80 | + |
| 81 | + @app.post("/v1/messages") |
| 82 | + async def anthropic_messages(request: Request): |
| 83 | + body = await request.json() |
| 84 | + seen.append(("anthropic", body["model"])) |
| 85 | + message = { |
| 86 | + "id": "msg", |
| 87 | + "type": "message", |
| 88 | + "role": "assistant", |
| 89 | + "model": body["model"], |
| 90 | + "stop_sequence": None, |
| 91 | + } |
| 92 | + return _sse( |
| 93 | + [ |
| 94 | + ("message_start", {"type": "message_start", "message": {**message, "content": [], "stop_reason": None, "usage": {"input_tokens": 1, "output_tokens": 0}}}), |
| 95 | + ("content_block_start", {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}), |
| 96 | + ("content_block_delta", {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "hello"}}), |
| 97 | + ("content_block_stop", {"type": "content_block_stop", "index": 0}), |
| 98 | + ("message_delta", {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence": None}, "usage": {"output_tokens": 1}}), |
| 99 | + ("message_stop", {"type": "message_stop"}), |
| 100 | + ] |
| 101 | + ) |
| 102 | + |
| 103 | + return app |
| 104 | + |
| 105 | + |
| 106 | +@pytest.fixture |
| 107 | +def provider(): |
| 108 | + seen = [] |
| 109 | + with socket.socket() as probe: |
| 110 | + probe.bind(("127.0.0.1", 0)) |
| 111 | + port = probe.getsockname()[1] |
| 112 | + server = uvicorn.Server( |
| 113 | + uvicorn.Config(_provider_app(seen), host="127.0.0.1", port=port, log_level="error") |
| 114 | + ) |
| 115 | + thread = threading.Thread(target=server.run, daemon=True) |
| 116 | + thread.start() |
| 117 | + deadline = time.monotonic() + 10 |
| 118 | + while not server.started and time.monotonic() < deadline: |
| 119 | + time.sleep(0.01) |
| 120 | + yield f"http://127.0.0.1:{port}", seen |
| 121 | + server.should_exit = True |
| 122 | + thread.join(timeout=10) |
| 123 | + |
| 124 | + |
| 125 | +def _endpoint(model): |
| 126 | + """What the desktop writes for an OpenAI-compatible endpoint serving `model`.""" |
| 127 | + return ( |
| 128 | + lambda base: { |
| 129 | + "BOT_PROVIDER": "", |
| 130 | + "BOT_MODEL": model, |
| 131 | + "OPENAI_API_KEY": "no-key-needed", |
| 132 | + "OPENAI_BASE_URL": f"{base}/v1", |
| 133 | + "ANTHROPIC_API_KEY": "", |
| 134 | + }, |
| 135 | + ("openai", model), |
| 136 | + ) |
| 137 | + |
| 138 | + |
| 139 | +CHOICES = { |
| 140 | + "an Anthropic key": ( |
| 141 | + lambda base: { |
| 142 | + "BOT_PROVIDER": "anthropic", |
| 143 | + "BOT_MODEL": "claude-sonnet-4-5", |
| 144 | + "ANTHROPIC_API_KEY": "test-key", |
| 145 | + "ANTHROPIC_BASE_URL": base, |
| 146 | + "OPENAI_API_KEY": "", |
| 147 | + "OPENAI_BASE_URL": "", |
| 148 | + }, |
| 149 | + ("anthropic", "claude-sonnet-4-5"), |
| 150 | + ), |
| 151 | + # Written the way Pydantic AI names a model, which still reaches that provider. |
| 152 | + "a model already named with its provider": ( |
| 153 | + lambda base: { |
| 154 | + "BOT_PROVIDER": "anthropic", |
| 155 | + "BOT_MODEL": "anthropic:claude-sonnet-4-5", |
| 156 | + "ANTHROPIC_API_KEY": "test-key", |
| 157 | + "ANTHROPIC_BASE_URL": base, |
| 158 | + "OPENAI_API_KEY": "", |
| 159 | + "OPENAI_BASE_URL": "", |
| 160 | + }, |
| 161 | + ("anthropic", "claude-sonnet-4-5"), |
| 162 | + ), |
| 163 | + "an OpenAI-compatible endpoint": _endpoint("local-model"), |
| 164 | + # Ollama names every model with its tag after a colon, which is the separator Pydantic AI puts |
| 165 | + # between a provider and a model. |
| 166 | + "an Ollama model named with its tag": _endpoint("llama3.1:8b"), |
| 167 | + # And a tag on a model that shares its name with a provider Pydantic AI knows. |
| 168 | + "an Ollama model named like a provider": _endpoint("mistral:7b"), |
| 169 | + "an OpenAI key": ( |
| 170 | + lambda base: { |
| 171 | + "BOT_PROVIDER": "", |
| 172 | + "BOT_MODEL": "gpt-5.5", |
| 173 | + "OPENAI_API_KEY": "test-key", |
| 174 | + "OPENAI_BASE_URL": f"{base}/v1", |
| 175 | + "ANTHROPIC_API_KEY": "", |
| 176 | + }, |
| 177 | + ("openai", "gpt-5.5"), |
| 178 | + ), |
| 179 | +} |
| 180 | + |
| 181 | + |
| 182 | +@pytest.mark.parametrize("choice", list(CHOICES)) |
| 183 | +def test_a_run_reaches_the_model_the_setup_screen_chose(monkeypatch, provider, choice): |
| 184 | + base, seen = provider |
| 185 | + environment, expected = CHOICES[choice] |
| 186 | + monkeypatch.delenv("OPENAI_API_BASE", raising=False) |
| 187 | + monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN) |
| 188 | + monkeypatch.setenv("PYDANTIC_AI_NO_BANNER", "1") |
| 189 | + for key, value in environment(base).items(): |
| 190 | + monkeypatch.setenv(key, value) |
| 191 | + |
| 192 | + from src import main |
| 193 | + |
| 194 | + main = importlib.reload(main) |
| 195 | + response = TestClient(main.app).post( |
| 196 | + "/", json=RUN, headers={"x-openbot-agent-token": TOKEN} |
| 197 | + ) |
| 198 | + |
| 199 | + assert response.status_code == 200 |
| 200 | + assert '"RUN_FINISHED"' in response.text |
| 201 | + assert '"RUN_ERROR"' not in response.text |
| 202 | + assert "hello" in response.text |
| 203 | + assert seen == [expected] |
0 commit comments