|
| 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 JSONResponse, 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 event in events: |
| 32 | + yield event |
| 33 | + |
| 34 | + return StreamingResponse(stream(), media_type="text/event-stream") |
| 35 | + |
| 36 | + |
| 37 | +def _provider_app(seen): |
| 38 | + app = FastAPI() |
| 39 | + |
| 40 | + @app.post("/v1/chat/completions") |
| 41 | + async def openai_chat(request: Request): |
| 42 | + body = await request.json() |
| 43 | + seen.append(("openai", body["model"])) |
| 44 | + if not body.get("stream"): |
| 45 | + return JSONResponse( |
| 46 | + { |
| 47 | + "id": "c", |
| 48 | + "object": "chat.completion", |
| 49 | + "created": 0, |
| 50 | + "model": body["model"], |
| 51 | + "choices": [ |
| 52 | + { |
| 53 | + "index": 0, |
| 54 | + "finish_reason": "stop", |
| 55 | + "message": {"role": "assistant", "content": "hello"}, |
| 56 | + } |
| 57 | + ], |
| 58 | + } |
| 59 | + ) |
| 60 | + chunk = { |
| 61 | + "id": "c", |
| 62 | + "object": "chat.completion.chunk", |
| 63 | + "created": 0, |
| 64 | + "model": body["model"], |
| 65 | + "choices": [ |
| 66 | + { |
| 67 | + "index": 0, |
| 68 | + "delta": {"role": "assistant", "content": "hello"}, |
| 69 | + "finish_reason": None, |
| 70 | + } |
| 71 | + ], |
| 72 | + } |
| 73 | + done = {**chunk, "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]} |
| 74 | + return _sse( |
| 75 | + [f"data: {json.dumps(chunk)}\n\n", f"data: {json.dumps(done)}\n\n", "data: [DONE]\n\n"] |
| 76 | + ) |
| 77 | + |
| 78 | + @app.post("/v1/messages") |
| 79 | + async def anthropic_messages(request: Request): |
| 80 | + body = await request.json() |
| 81 | + seen.append(("anthropic", body["model"])) |
| 82 | + message = { |
| 83 | + "id": "msg", |
| 84 | + "type": "message", |
| 85 | + "role": "assistant", |
| 86 | + "model": body["model"], |
| 87 | + "stop_sequence": None, |
| 88 | + } |
| 89 | + if not body.get("stream"): |
| 90 | + return JSONResponse( |
| 91 | + { |
| 92 | + **message, |
| 93 | + "content": [{"type": "text", "text": "hello"}], |
| 94 | + "stop_reason": "end_turn", |
| 95 | + "usage": {"input_tokens": 1, "output_tokens": 1}, |
| 96 | + } |
| 97 | + ) |
| 98 | + events = [ |
| 99 | + ("message_start", {"type": "message_start", "message": {**message, "content": [], "stop_reason": None, "usage": {"input_tokens": 1, "output_tokens": 0}}}), |
| 100 | + ("content_block_start", {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}), |
| 101 | + ("content_block_delta", {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "hello"}}), |
| 102 | + ("content_block_stop", {"type": "content_block_stop", "index": 0}), |
| 103 | + ("message_delta", {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence": None}, "usage": {"output_tokens": 1}}), |
| 104 | + ("message_stop", {"type": "message_stop"}), |
| 105 | + ] |
| 106 | + return _sse([f"event: {name}\ndata: {json.dumps(data)}\n\n" for name, data in events]) |
| 107 | + |
| 108 | + return app |
| 109 | + |
| 110 | + |
| 111 | +@pytest.fixture |
| 112 | +def provider(): |
| 113 | + seen = [] |
| 114 | + with socket.socket() as probe: |
| 115 | + probe.bind(("127.0.0.1", 0)) |
| 116 | + port = probe.getsockname()[1] |
| 117 | + server = uvicorn.Server( |
| 118 | + uvicorn.Config(_provider_app(seen), host="127.0.0.1", port=port, log_level="error") |
| 119 | + ) |
| 120 | + thread = threading.Thread(target=server.run, daemon=True) |
| 121 | + thread.start() |
| 122 | + deadline = time.monotonic() + 10 |
| 123 | + while not server.started and time.monotonic() < deadline: |
| 124 | + time.sleep(0.01) |
| 125 | + yield f"http://127.0.0.1:{port}", seen |
| 126 | + server.should_exit = True |
| 127 | + thread.join(timeout=10) |
| 128 | + |
| 129 | + |
| 130 | +CHOICES = { |
| 131 | + "an Anthropic key": ( |
| 132 | + lambda base: { |
| 133 | + "BOT_PROVIDER": "anthropic", |
| 134 | + "BOT_MODEL": "claude-sonnet-4-5", |
| 135 | + "ANTHROPIC_API_KEY": "test-key", |
| 136 | + "ANTHROPIC_BASE_URL": base, |
| 137 | + "OPENAI_API_KEY": "", |
| 138 | + "OPENAI_BASE_URL": "", |
| 139 | + }, |
| 140 | + ("anthropic", "claude-sonnet-4-5"), |
| 141 | + ), |
| 142 | + "an OpenAI-compatible endpoint": ( |
| 143 | + lambda base: { |
| 144 | + "BOT_PROVIDER": "", |
| 145 | + "BOT_MODEL": "local-model", |
| 146 | + "OPENAI_API_KEY": "no-key-needed", |
| 147 | + "OPENAI_BASE_URL": f"{base}/v1", |
| 148 | + "ANTHROPIC_API_KEY": "", |
| 149 | + }, |
| 150 | + ("openai", "local-model"), |
| 151 | + ), |
| 152 | + # The default: Compose passes `gpt-5.5` when the model screen names no model, and Agno sends a |
| 153 | + # temperature and a `top_p` on every request, which LiteLLM refuses for that reasoning model. |
| 154 | + "an OpenAI key": ( |
| 155 | + lambda base: { |
| 156 | + "BOT_PROVIDER": "", |
| 157 | + "BOT_MODEL": "gpt-5.5", |
| 158 | + "OPENAI_API_KEY": "test-key", |
| 159 | + "OPENAI_BASE_URL": f"{base}/v1", |
| 160 | + "ANTHROPIC_API_KEY": "", |
| 161 | + }, |
| 162 | + ("openai", "gpt-5.5"), |
| 163 | + ), |
| 164 | +} |
| 165 | + |
| 166 | + |
| 167 | +@pytest.mark.parametrize("choice", list(CHOICES)) |
| 168 | +def test_a_run_reaches_the_model_the_setup_screen_chose(monkeypatch, provider, choice): |
| 169 | + base, seen = provider |
| 170 | + environment, expected = CHOICES[choice] |
| 171 | + monkeypatch.delenv("OPENAI_API_BASE", raising=False) |
| 172 | + monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN) |
| 173 | + for key, value in environment(base).items(): |
| 174 | + monkeypatch.setenv(key, value) |
| 175 | + |
| 176 | + from src import main |
| 177 | + |
| 178 | + main = importlib.reload(main) |
| 179 | + response = TestClient(main.app).post( |
| 180 | + "/agui", json=RUN, headers={"x-openbot-agent-token": TOKEN} |
| 181 | + ) |
| 182 | + |
| 183 | + assert response.status_code == 200 |
| 184 | + assert '"RUN_FINISHED"' in response.text |
| 185 | + assert '"RUN_ERROR"' not in response.text |
| 186 | + assert seen == [expected] |
0 commit comments