2026-06-13 16:32:33 +02:00
|
|
|
# retoor <retoor@molodetz.nl>
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
|
2026-06-13 16:32:33 +02:00
|
|
|
import json
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
import requests
|
|
|
|
|
from starlette.requests import Request
|
|
|
|
|
from tests.conftest import BASE_URL, run_async
|
|
|
|
|
from devplacepy.database import get_table, set_setting
|
|
|
|
|
from devplacepy.utils import generate_uid
|
|
|
|
|
import devplacepy.services.openai_gateway.gateway as gwmod
|
|
|
|
|
from devplacepy.services.openai_gateway import GatewayService
|
2026-06-13 16:32:33 +02:00
|
|
|
class FakeResp_openai_gateway:
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
def __init__(self, status=200, payload=None, ctype="application/json", content=b""):
|
|
|
|
|
self.status_code = status
|
|
|
|
|
self._payload = payload
|
|
|
|
|
self.text = json.dumps(payload) if payload is not None else ""
|
|
|
|
|
self.headers = {"content-type": ctype}
|
|
|
|
|
self.content = content
|
|
|
|
|
|
|
|
|
|
def json(self):
|
|
|
|
|
if self._payload is None:
|
|
|
|
|
raise ValueError("no json")
|
|
|
|
|
return self._payload
|
feat: add project file system with CRUD, upload, inline editing, and video attachment support
- Add new `/projects/{slug}/files` endpoint group for per-project filesystem operations including directory and file CRUD, upload, and inline editing with public read and owner write access
- Extend attachment system to support video formats (webm, ogv, mov, m4v) with proper file icons and MIME types
- Implement configurable allowed file types via `allowed_file_types` site setting, replacing hardcoded `ALLOWED_UPLOAD_TYPES` with dynamic `allowed_extensions()` and `is_extension_allowed()` functions
- Add `delete_all_project_files()` call in `delete_content_item()` to clean up project files when a project is deleted
- Create database indexes on `project_files` table for `(project_uid, path)` and `(project_uid, parent_path)` to optimize file lookups
- Introduce `docs_prose.py` module with `render_prose()` function that renders Markdown content inside `data-render` divs using mistune, enabling dynamic prose rendering in documentation pages
- Enhance docs search with Markdown-aware text stripping (`_demarkdown()`) and improved HTML/script/style sanitization for better search indexing
- Update documentation API samples to reflect new attachment response fields (`is_image`, `is_video`, `mime_type`) and note video format support
- Update README to document the new project files endpoint and clarify AI gateway attribution for guest Devii sessions
2026-06-08 22:51:09 +02:00
|
|
|
class FakeRequest:
|
|
|
|
|
def __init__(self, method, url, json_body):
|
|
|
|
|
self.method = method
|
|
|
|
|
self.url = url
|
|
|
|
|
self.json_body = json_body
|
|
|
|
|
self.extensions = {}
|
2026-06-13 16:32:33 +02:00
|
|
|
class FakeClient_openai_gateway:
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
def __init__(self, *a, **k):
|
|
|
|
|
self.calls = []
|
|
|
|
|
|
feat: add project file system with CRUD, upload, inline editing, and video attachment support
- Add new `/projects/{slug}/files` endpoint group for per-project filesystem operations including directory and file CRUD, upload, and inline editing with public read and owner write access
- Extend attachment system to support video formats (webm, ogv, mov, m4v) with proper file icons and MIME types
- Implement configurable allowed file types via `allowed_file_types` site setting, replacing hardcoded `ALLOWED_UPLOAD_TYPES` with dynamic `allowed_extensions()` and `is_extension_allowed()` functions
- Add `delete_all_project_files()` call in `delete_content_item()` to clean up project files when a project is deleted
- Create database indexes on `project_files` table for `(project_uid, path)` and `(project_uid, parent_path)` to optimize file lookups
- Introduce `docs_prose.py` module with `render_prose()` function that renders Markdown content inside `data-render` divs using mistune, enabling dynamic prose rendering in documentation pages
- Enhance docs search with Markdown-aware text stripping (`_demarkdown()`) and improved HTML/script/style sanitization for better search indexing
- Update documentation API samples to reflect new attachment response fields (`is_image`, `is_video`, `mime_type`) and note video format support
- Update README to document the new project files endpoint and clarify AI gateway attribution for guest Devii sessions
2026-06-08 22:51:09 +02:00
|
|
|
def build_request(self, method, url, headers=None, json=None, content=None):
|
|
|
|
|
return FakeRequest(method, url, json)
|
|
|
|
|
|
|
|
|
|
async def send(self, request):
|
|
|
|
|
self.calls.append((request.url, request.json_body))
|
|
|
|
|
body = request.json_body or {}
|
2026-06-13 16:32:33 +02:00
|
|
|
return FakeResp_openai_gateway(
|
2026-06-09 18:48:08 +02:00
|
|
|
payload={
|
|
|
|
|
"id": "x",
|
|
|
|
|
"model": body.get("model"),
|
|
|
|
|
"choices": [{"message": {"content": "hi there"}}],
|
|
|
|
|
}
|
|
|
|
|
)
|
feat: add project file system with CRUD, upload, inline editing, and video attachment support
- Add new `/projects/{slug}/files` endpoint group for per-project filesystem operations including directory and file CRUD, upload, and inline editing with public read and owner write access
- Extend attachment system to support video formats (webm, ogv, mov, m4v) with proper file icons and MIME types
- Implement configurable allowed file types via `allowed_file_types` site setting, replacing hardcoded `ALLOWED_UPLOAD_TYPES` with dynamic `allowed_extensions()` and `is_extension_allowed()` functions
- Add `delete_all_project_files()` call in `delete_content_item()` to clean up project files when a project is deleted
- Create database indexes on `project_files` table for `(project_uid, path)` and `(project_uid, parent_path)` to optimize file lookups
- Introduce `docs_prose.py` module with `render_prose()` function that renders Markdown content inside `data-render` divs using mistune, enabling dynamic prose rendering in documentation pages
- Enhance docs search with Markdown-aware text stripping (`_demarkdown()`) and improved HTML/script/style sanitization for better search indexing
- Update documentation API samples to reflect new attachment response fields (`is_image`, `is_video`, `mime_type`) and note video format support
- Update README to document the new project files endpoint and clarify AI gateway attribution for guest Devii sessions
2026-06-08 22:51:09 +02:00
|
|
|
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
async def post(self, url, headers=None, json=None, timeout=None):
|
|
|
|
|
self.calls.append((url, json))
|
2026-06-13 16:32:33 +02:00
|
|
|
return FakeResp_openai_gateway(
|
2026-06-09 18:48:08 +02:00
|
|
|
payload={
|
|
|
|
|
"id": "x",
|
|
|
|
|
"model": json.get("model"),
|
|
|
|
|
"choices": [{"message": {"content": "hi there"}}],
|
|
|
|
|
}
|
|
|
|
|
)
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
|
|
|
|
|
async def request(self, method, url, headers=None, content=None):
|
2026-06-13 16:32:33 +02:00
|
|
|
return FakeResp_openai_gateway(payload={"ok": True})
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
|
|
|
|
|
async def aclose(self):
|
|
|
|
|
pass
|
2026-06-13 16:32:33 +02:00
|
|
|
def _make_request_openai_gateway(headers=None, cookies=None):
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
headers = headers or {}
|
|
|
|
|
raw = [(k.lower().encode(), v.encode()) for k, v in headers.items()]
|
|
|
|
|
if cookies:
|
2026-06-09 18:48:08 +02:00
|
|
|
raw.append(
|
|
|
|
|
(b"cookie", "; ".join(f"{k}={v}" for k, v in cookies.items()).encode())
|
|
|
|
|
)
|
|
|
|
|
return Request(
|
|
|
|
|
{
|
|
|
|
|
"type": "http",
|
|
|
|
|
"method": "POST",
|
|
|
|
|
"path": "/openai/v1/chat/completions",
|
|
|
|
|
"query_string": b"",
|
|
|
|
|
"headers": raw,
|
|
|
|
|
"state": {},
|
|
|
|
|
}
|
|
|
|
|
)
|
2026-06-13 16:32:33 +02:00
|
|
|
def _make_admin_openai_gateway(role="Admin"):
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
username = f"gw_{generate_uid()[:8]}"
|
|
|
|
|
api_key = generate_uid()
|
2026-06-09 18:48:08 +02:00
|
|
|
get_table("users").insert(
|
|
|
|
|
{
|
|
|
|
|
"uid": generate_uid(),
|
|
|
|
|
"username": username,
|
Add the trust and safety subsystem and the App Store compliance work
Implements the moderation and consent obligations a social platform carries,
so the web version and any client that speaks to it enforce the same rules.
Moderation core (services/moderation/, database/moderation.py): a reportable
target registry, the content filter and its choke points, the report queue with
atomic resolution, enforcement actions, consent tracking, maturity gating, and
account deletion with a grace window.
Surfaces: POST /reports plus the member report list, /admin/moderation and the
per-report admin view, /workspaces, terms acceptance at /auth/terms, consent and
account deletion under /profile, the report button and dialog partials, the
maturity gate, and the moderation stylesheet and ReportDialog client.
Every user-generated surface stays reportable by construction: new content tables
are registered in REPORTABLE_TARGETS or listed in UNREPORTABLE_TABLES with a
reason, and the registry test fails the suite on anything left unclassified.
Docs: community guidelines, content moderation, intellectual property, privacy,
terms, contact, and the admin-only moderation operations page, plus the
moderation API group and the Devii moderation actions.
Compliance record: applecomp.md is the requirement register, applechanges.md the
gap analysis against this codebase, and appleimpl.md the implementation design
they resolve to.
Tests cover the report flow, admin moderation, consent, account deletion, terms
acceptance, workspaces, and the registry invariant across the unit, api, and e2e
tiers.
2026-08-09 00:18:20 +02:00
|
|
|
"terms_version": "1",
|
2026-06-09 18:48:08 +02:00
|
|
|
"email": f"{username}@t.dev",
|
|
|
|
|
"api_key": api_key,
|
|
|
|
|
"role": role,
|
|
|
|
|
"is_active": True,
|
|
|
|
|
}
|
|
|
|
|
)
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
return username, api_key
|
2026-06-13 16:32:33 +02:00
|
|
|
def _config(page, **fields):
|
|
|
|
|
page.request.post(f"{BASE_URL}/admin/services/openai/config", form=fields)
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
|
2026-06-09 18:48:08 +02:00
|
|
|
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
def test_model_is_forced(local_db, monkeypatch):
|
2026-06-13 16:32:33 +02:00
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeClient_openai_gateway)
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_force_model"] = True
|
|
|
|
|
cfg["gateway_model"] = "deepseek-chat"
|
|
|
|
|
rt = svc.runtime()
|
2026-06-09 18:48:08 +02:00
|
|
|
run_async(
|
|
|
|
|
rt.handle_chat(
|
|
|
|
|
{"model": "gpt-4", "messages": [{"role": "user", "content": "hi"}]},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "test"),
|
|
|
|
|
"test",
|
2026-07-19 18:57:43 +02:00
|
|
|
"default",
|
2026-06-09 18:48:08 +02:00
|
|
|
)
|
|
|
|
|
)
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
assert rt._client.calls[-1][1]["model"] == "deepseek-chat"
|
|
|
|
|
|
|
|
|
|
|
2026-06-17 00:11:14 +02:00
|
|
|
def test_model_route_overrides_upstream(local_db, monkeypatch):
|
|
|
|
|
from devplacepy.services.openai_gateway import routing
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeClient_openai_gateway)
|
|
|
|
|
routing.provider_store.set(
|
|
|
|
|
routing.ProviderIn(
|
|
|
|
|
name="gwroute",
|
|
|
|
|
base_url="https://routed.example/v1/chat/completions",
|
|
|
|
|
api_key="routed-key",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
routing.model_store.set(
|
|
|
|
|
routing.ModelRouteIn(
|
|
|
|
|
source_model="custom-pro",
|
|
|
|
|
provider="gwroute",
|
|
|
|
|
target_model="vendor/pro",
|
|
|
|
|
kind="chat",
|
|
|
|
|
price_output_per_m=9.0,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
try:
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_vision_enabled"] = False
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
run_async(
|
|
|
|
|
rt.handle_chat(
|
|
|
|
|
{"model": "custom-pro", "messages": [{"role": "user", "content": "hi"}]},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "test"),
|
|
|
|
|
"test",
|
2026-07-19 18:57:43 +02:00
|
|
|
"default",
|
2026-06-17 00:11:14 +02:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
url, body = rt._client.calls[-1]
|
|
|
|
|
assert str(url) == "https://routed.example/v1/chat/completions"
|
|
|
|
|
assert body["model"] == "vendor/pro"
|
|
|
|
|
finally:
|
|
|
|
|
routing.model_store.remove("custom-pro")
|
|
|
|
|
routing.provider_store.remove("gwroute")
|
|
|
|
|
|
|
|
|
|
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
def test_vision_rewrites_image_to_text(local_db, monkeypatch):
|
2026-06-13 16:32:33 +02:00
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeClient_openai_gateway)
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_vision_enabled"] = True
|
|
|
|
|
cfg["gateway_vision_key"] = "" # no key -> placeholder text, still rewrites to str
|
|
|
|
|
rt = svc.runtime()
|
2026-06-09 18:48:08 +02:00
|
|
|
msgs = [
|
|
|
|
|
{
|
|
|
|
|
"role": "user",
|
|
|
|
|
"content": [
|
|
|
|
|
{"type": "text", "text": "what is this"},
|
|
|
|
|
{"type": "image_url", "image_url": {"url": "http://x/y.png"}},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
]
|
2026-07-19 18:57:43 +02:00
|
|
|
run_async(rt.handle_chat({"messages": msgs}, cfg, ("guest", "test"), "test", "default"))
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
sent = rt._client.calls[-1][1]["messages"][0]["content"]
|
|
|
|
|
assert isinstance(sent, str) and "vision" in sent.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_streaming_emits_sse(local_db, monkeypatch):
|
2026-06-13 16:32:33 +02:00
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeClient_openai_gateway)
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
rt = svc.runtime()
|
2026-06-09 18:48:08 +02:00
|
|
|
resp = run_async(
|
|
|
|
|
rt.handle_chat(
|
|
|
|
|
{"messages": [{"role": "user", "content": "hi"}], "stream": True},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "test"),
|
|
|
|
|
"test",
|
2026-07-19 18:57:43 +02:00
|
|
|
"default",
|
2026-06-09 18:48:08 +02:00
|
|
|
)
|
|
|
|
|
)
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
|
|
|
|
|
async def drain():
|
|
|
|
|
out = []
|
|
|
|
|
async for chunk in resp.body_iterator:
|
|
|
|
|
out.append(chunk if isinstance(chunk, str) else chunk.decode())
|
|
|
|
|
return "".join(out)
|
|
|
|
|
|
|
|
|
|
body = run_async(drain())
|
|
|
|
|
assert "chat.completion.chunk" in body
|
|
|
|
|
assert "[DONE]" in body
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_authorize_static_access_key(local_db):
|
|
|
|
|
set_setting("gateway_require_auth", "1")
|
|
|
|
|
set_setting("gateway_access_key", "topsecret")
|
2026-06-12 05:37:12 +02:00
|
|
|
try:
|
|
|
|
|
svc = GatewayService()
|
2026-06-13 16:32:33 +02:00
|
|
|
assert svc.authorize(_make_request_openai_gateway(headers={"X-API-KEY": "topsecret"})) is True
|
|
|
|
|
assert svc.authorize(_make_request_openai_gateway(headers={"X-API-KEY": "wrong"})) is False
|
2026-06-12 05:37:12 +02:00
|
|
|
finally:
|
|
|
|
|
set_setting("gateway_access_key", "")
|
|
|
|
|
set_setting("gateway_require_auth", "1")
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_authorize_admin_and_user_toggles(local_db):
|
|
|
|
|
set_setting("gateway_require_auth", "1")
|
|
|
|
|
set_setting("gateway_access_key", "")
|
|
|
|
|
set_setting("gateway_allow_admins", "1")
|
|
|
|
|
set_setting("gateway_allow_users", "0")
|
2026-06-12 05:37:12 +02:00
|
|
|
try:
|
2026-06-13 16:32:33 +02:00
|
|
|
_, admin_key = _make_admin_openai_gateway(role="Admin")
|
|
|
|
|
_, member_key = _make_admin_openai_gateway(role="Member")
|
2026-06-12 05:37:12 +02:00
|
|
|
svc = GatewayService()
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
|
2026-06-12 05:37:12 +02:00
|
|
|
assert (
|
2026-06-13 16:32:33 +02:00
|
|
|
svc.authorize(_make_request_openai_gateway(headers={"Authorization": f"Bearer {admin_key}"}))
|
2026-06-12 05:37:12 +02:00
|
|
|
is True
|
|
|
|
|
)
|
2026-06-13 16:32:33 +02:00
|
|
|
assert svc.authorize(_make_request_openai_gateway(headers={"X-API-KEY": member_key})) is False
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
|
2026-06-12 05:37:12 +02:00
|
|
|
set_setting("gateway_allow_users", "1")
|
2026-06-13 16:32:33 +02:00
|
|
|
assert svc.authorize(_make_request_openai_gateway(headers={"X-API-KEY": member_key})) is True
|
2026-06-12 05:37:12 +02:00
|
|
|
finally:
|
|
|
|
|
set_setting("gateway_allow_admins", "1")
|
|
|
|
|
set_setting("gateway_allow_users", "1")
|
|
|
|
|
set_setting("gateway_require_auth", "1")
|
|
|
|
|
set_setting("gateway_access_key", "")
|
feat: add api key auth, devii agent, openai gateway, and admin service management
This commit introduces a comprehensive set of new features including API key authentication with CLI management commands (get, reset, backfill), a Devii agentic assistant with WebSocket terminal and session bootstrap, an OpenAI-compatible LLM gateway service, and an admin service management panel. It also adds Playwright browser automation for bot support, configures internal gateway URLs, refactors content editing/deletion to support JSON API responses, and updates documentation across AGENTS.md, README.md, and the developer docs site.
2026-06-08 17:38:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_authorize_require_auth_off_is_open(local_db):
|
|
|
|
|
set_setting("gateway_require_auth", "0")
|
2026-06-12 05:37:12 +02:00
|
|
|
try:
|
|
|
|
|
svc = GatewayService()
|
2026-06-13 16:32:33 +02:00
|
|
|
assert svc.authorize(_make_request_openai_gateway()) is True
|
2026-06-12 05:37:12 +02:00
|
|
|
finally:
|
|
|
|
|
set_setting("gateway_require_auth", "1")
|
2026-06-14 03:06:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class FakeEmbedClient_openai_gateway:
|
|
|
|
|
def __init__(self, *a, **k):
|
|
|
|
|
self.calls = []
|
|
|
|
|
|
|
|
|
|
def build_request(self, method, url, headers=None, json=None, content=None):
|
|
|
|
|
return FakeRequest(method, url, json)
|
|
|
|
|
|
|
|
|
|
async def send(self, request):
|
|
|
|
|
self.calls.append((request.url, request.json_body))
|
|
|
|
|
body = request.json_body or {}
|
|
|
|
|
return FakeResp_openai_gateway(
|
|
|
|
|
payload={
|
|
|
|
|
"object": "list",
|
|
|
|
|
"model": body.get("model"),
|
|
|
|
|
"data": [{"object": "embedding", "index": 0, "embedding": [0.1, 0.2]}],
|
|
|
|
|
"usage": {"prompt_tokens": 5, "total_tokens": 5},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def aclose(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_embeddings_remaps_alias_to_configured_model(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeEmbedClient_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_force_model"] = False
|
|
|
|
|
cfg["gateway_embed_enabled"] = True
|
|
|
|
|
cfg["gateway_embed_model"] = "qwen/qwen3-embedding-8b"
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
run_async(
|
|
|
|
|
rt.handle_embeddings(
|
|
|
|
|
{"model": "molodetz~embed", "input": "hello"},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "test"),
|
|
|
|
|
"test",
|
2026-07-19 18:57:43 +02:00
|
|
|
"default",
|
2026-06-14 03:06:18 +02:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
assert rt._client.calls[-1][1]["model"] == "qwen/qwen3-embedding-8b"
|
|
|
|
|
|
|
|
|
|
|
2026-06-14 03:34:21 +02:00
|
|
|
def test_embeddings_success_records_ledger(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeEmbedClient_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_force_model"] = True
|
|
|
|
|
cfg["gateway_embed_enabled"] = True
|
|
|
|
|
cfg["gateway_embed_model"] = "qwen/qwen3-embedding-8b"
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
before = rt.embed_calls
|
|
|
|
|
resp = run_async(
|
|
|
|
|
rt.handle_embeddings(
|
|
|
|
|
{"model": "molodetz~embed", "input": "hello"},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "ledger_probe"),
|
|
|
|
|
"test",
|
2026-07-19 18:57:43 +02:00
|
|
|
"default",
|
2026-06-14 03:34:21 +02:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
payload = json.loads(bytes(resp.body).decode())
|
|
|
|
|
assert payload["data"][0]["embedding"] == [0.1, 0.2]
|
|
|
|
|
assert rt.embed_calls == before + 1
|
|
|
|
|
row = get_table("gateway_usage_ledger").find_one(owner_id="ledger_probe")
|
|
|
|
|
assert row is not None
|
|
|
|
|
assert row["backend"] == "embed"
|
|
|
|
|
assert row["endpoint"] == "embeddings"
|
|
|
|
|
assert row["requested_model"] == "molodetz~embed"
|
|
|
|
|
assert row["model"] == "qwen/qwen3-embedding-8b"
|
|
|
|
|
assert row["success"] == 1
|
|
|
|
|
|
|
|
|
|
|
2026-06-14 03:06:18 +02:00
|
|
|
def test_embeddings_disabled_returns_503(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeEmbedClient_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_embed_enabled"] = False
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
resp = run_async(
|
|
|
|
|
rt.handle_embeddings(
|
2026-07-19 18:57:43 +02:00
|
|
|
{"input": "hello"}, cfg, ("guest", "test"), "test", "default"
|
2026-06-14 03:06:18 +02:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
assert resp.status_code == 503
|
|
|
|
|
|
|
|
|
|
|
2026-07-09 02:52:54 +02:00
|
|
|
class FakeImageClient_openai_gateway:
|
|
|
|
|
def __init__(self, *a, **k):
|
|
|
|
|
self.calls = []
|
|
|
|
|
|
|
|
|
|
def build_request(self, method, url, headers=None, json=None, content=None):
|
|
|
|
|
return FakeRequest(method, url, json)
|
|
|
|
|
|
|
|
|
|
async def send(self, request):
|
|
|
|
|
self.calls.append((request.url, request.json_body))
|
|
|
|
|
body = request.json_body or {}
|
|
|
|
|
return FakeResp_openai_gateway(
|
|
|
|
|
payload={
|
|
|
|
|
"created": 1,
|
|
|
|
|
"model": body.get("model"),
|
|
|
|
|
"data": [{"b64_json": "aGVsbG8="}],
|
|
|
|
|
"usage": {"cost": 0.05},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def aclose(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_images_remaps_alias_to_configured_model(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeImageClient_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_force_model"] = False
|
|
|
|
|
cfg["gateway_image_enabled"] = True
|
|
|
|
|
cfg["gateway_image_model"] = "black-forest-labs/flux.2-pro"
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
run_async(
|
|
|
|
|
rt.handle_images(
|
|
|
|
|
{
|
|
|
|
|
"model": "molodetz-img-small",
|
|
|
|
|
"prompt": "award emblem",
|
|
|
|
|
"size": "512x512",
|
|
|
|
|
},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "test"),
|
|
|
|
|
"test",
|
2026-07-19 18:57:43 +02:00
|
|
|
"default",
|
2026-07-09 02:52:54 +02:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
assert rt._client.calls[-1][1]["model"] == "black-forest-labs/flux.2-pro"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_image_route_overrides_upstream(local_db, monkeypatch):
|
|
|
|
|
from devplacepy.services.openai_gateway import routing
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeImageClient_openai_gateway)
|
|
|
|
|
routing.provider_store.set(
|
|
|
|
|
routing.ProviderIn(
|
|
|
|
|
name="gwimg",
|
|
|
|
|
base_url="https://routed.example/v1/chat/completions",
|
|
|
|
|
api_key="img-key",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
routing.model_store.set(
|
|
|
|
|
routing.ModelRouteIn(
|
|
|
|
|
source_model="custom-img",
|
|
|
|
|
provider="gwimg",
|
|
|
|
|
target_model="vendor/flux-pro",
|
|
|
|
|
kind="image",
|
|
|
|
|
price_input_per_m=0.06,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
try:
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
run_async(
|
|
|
|
|
rt.handle_images(
|
|
|
|
|
{
|
|
|
|
|
"model": "custom-img",
|
|
|
|
|
"prompt": "trophy",
|
|
|
|
|
"response_format": "b64_json",
|
|
|
|
|
},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "test"),
|
|
|
|
|
"test",
|
2026-07-19 18:57:43 +02:00
|
|
|
"default",
|
2026-07-09 02:52:54 +02:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
url, body = rt._client.calls[-1]
|
|
|
|
|
assert str(url) == "https://routed.example/v1/images"
|
|
|
|
|
assert body["model"] == "vendor/flux-pro"
|
|
|
|
|
finally:
|
|
|
|
|
routing.model_store.remove("custom-img")
|
|
|
|
|
routing.provider_store.remove("gwimg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_images_success_records_ledger(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeImageClient_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_image_enabled"] = True
|
|
|
|
|
cfg["gateway_image_model"] = "black-forest-labs/flux.2-pro"
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
before = rt.image_calls
|
|
|
|
|
resp = run_async(
|
|
|
|
|
rt.handle_images(
|
|
|
|
|
{"model": "molodetz-img-small", "prompt": "badge"},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "img_ledger"),
|
|
|
|
|
"test",
|
2026-07-19 18:57:43 +02:00
|
|
|
"default",
|
2026-07-09 02:52:54 +02:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
payload = json.loads(bytes(resp.body).decode())
|
|
|
|
|
assert payload["data"][0]["b64_json"] == "aGVsbG8="
|
|
|
|
|
assert rt.image_calls == before + 1
|
|
|
|
|
row = get_table("gateway_usage_ledger").find_one(owner_id="img_ledger")
|
|
|
|
|
assert row is not None
|
|
|
|
|
assert row["backend"] == "image"
|
|
|
|
|
assert row["endpoint"] == "images/generations"
|
|
|
|
|
assert row["requested_model"] == "molodetz-img-small"
|
|
|
|
|
assert row["model"] == "black-forest-labs/flux.2-pro"
|
|
|
|
|
assert row["success"] == 1
|
|
|
|
|
assert float(row["cost_usd"]) == 0.05
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_images_disabled_returns_503(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeImageClient_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_image_enabled"] = False
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
resp = run_async(
|
|
|
|
|
rt.handle_images(
|
2026-07-19 18:57:43 +02:00
|
|
|
{"prompt": "badge"}, cfg, ("guest", "test"), "test", "default"
|
2026-07-09 02:52:54 +02:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
assert resp.status_code == 503
|
|
|
|
|
|
|
|
|
|
|
2026-06-14 03:06:18 +02:00
|
|
|
def test_compute_cost_embed_branch():
|
|
|
|
|
from devplacepy.services.openai_gateway.usage import (
|
|
|
|
|
Pricing,
|
|
|
|
|
compute_cost,
|
|
|
|
|
normalize_usage,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
pricing = Pricing(
|
|
|
|
|
chat_cache_hit_per_m=0.0,
|
|
|
|
|
chat_cache_miss_per_m=0.0,
|
|
|
|
|
chat_output_per_m=0.0,
|
|
|
|
|
vision_input_per_m=0.0,
|
|
|
|
|
vision_output_per_m=0.0,
|
|
|
|
|
embed_input_per_m=0.01,
|
|
|
|
|
)
|
|
|
|
|
usage = {"prompt_tokens": 1_000_000, "total_tokens": 1_000_000}
|
|
|
|
|
norm = normalize_usage(usage)
|
|
|
|
|
total, input_cost, output_cost, native = compute_cost(
|
|
|
|
|
usage, norm, pricing, "embed"
|
|
|
|
|
)
|
|
|
|
|
assert native is False
|
|
|
|
|
assert output_cost == 0.0
|
|
|
|
|
assert abs(total - 0.01) < 1e-9
|
|
|
|
|
assert abs(input_cost - 0.01) < 1e-9
|
2026-07-19 18:57:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_app_reference_stored_in_ledger_for_chat(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeClient_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_force_model"] = True
|
|
|
|
|
cfg["gateway_model"] = "deepseek-chat"
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
run_async(
|
|
|
|
|
rt.handle_chat(
|
|
|
|
|
{"messages": [{"role": "user", "content": "ref_test"}]},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "ref_probe"),
|
|
|
|
|
"test-ua",
|
|
|
|
|
"my-custom-app",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
row = get_table("gateway_usage_ledger").find_one(owner_id="ref_probe")
|
|
|
|
|
assert row is not None
|
|
|
|
|
assert row["app_reference"] == "my-custom-app"
|
|
|
|
|
assert row["backend"] == "chat"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_app_reference_defaults_when_not_passed(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeClient_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_force_model"] = True
|
|
|
|
|
cfg["gateway_model"] = "deepseek-chat"
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
run_async(
|
|
|
|
|
rt.handle_chat(
|
|
|
|
|
{"messages": [{"role": "user", "content": "default_test"}]},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "default_probe"),
|
|
|
|
|
"test-ua",
|
|
|
|
|
"default",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
row = get_table("gateway_usage_ledger").find_one(owner_id="default_probe")
|
|
|
|
|
assert row is not None
|
|
|
|
|
assert row["app_reference"] == "default"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_app_reference_stored_in_ledger_for_embeddings(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeEmbedClient_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_embed_enabled"] = True
|
|
|
|
|
cfg["gateway_embed_model"] = "qwen/qwen3-embedding-8b"
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
run_async(
|
|
|
|
|
rt.handle_embeddings(
|
|
|
|
|
{"input": "hello"},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "embed_ref"),
|
|
|
|
|
"test-ua",
|
|
|
|
|
"devplace-embed-test-v-1-0-0",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
row = get_table("gateway_usage_ledger").find_one(owner_id="embed_ref")
|
|
|
|
|
assert row is not None
|
|
|
|
|
assert row["app_reference"] == "devplace-embed-test-v-1-0-0"
|
|
|
|
|
assert row["backend"] == "embed"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_app_reference_stored_in_ledger_for_images(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeImageClient_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_image_enabled"] = True
|
|
|
|
|
cfg["gateway_image_model"] = "black-forest-labs/flux.2-pro"
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
run_async(
|
|
|
|
|
rt.handle_images(
|
|
|
|
|
{"prompt": "ref badge"},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "img_ref"),
|
|
|
|
|
"test-ua",
|
|
|
|
|
"devplace-image-test-v-1-0-0",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
row = get_table("gateway_usage_ledger").find_one(owner_id="img_ref")
|
|
|
|
|
assert row is not None
|
|
|
|
|
assert row["app_reference"] == "devplace-image-test-v-1-0-0"
|
|
|
|
|
assert row["backend"] == "image"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_app_reference_column_exists(local_db):
|
|
|
|
|
db = get_table("gateway_usage_ledger").db
|
|
|
|
|
if "gateway_usage_ledger" not in db.tables:
|
|
|
|
|
return
|
|
|
|
|
rows = list(db.query("PRAGMA table_info('gateway_usage_ledger')"))
|
|
|
|
|
column_names = [r["name"] for r in rows]
|
|
|
|
|
assert "app_reference" in column_names
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_chat_unknown_model_falls_back_to_default(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeClient_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_force_model"] = False
|
|
|
|
|
cfg["gateway_model"] = "deepseek-v4-flash"
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
run_async(
|
|
|
|
|
rt.handle_chat(
|
|
|
|
|
{"model": "nonexistent-model-v99", "messages": [{"role": "user", "content": "hi"}]},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "fallback_test_chat"),
|
|
|
|
|
"test",
|
|
|
|
|
"default",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
assert rt._client.calls[-1][1]["model"] == "deepseek-v4-flash"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_embeddings_unknown_model_falls_back_to_default(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeEmbedClient_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_force_model"] = False
|
|
|
|
|
cfg["gateway_embed_enabled"] = True
|
|
|
|
|
cfg["gateway_embed_model"] = "qwen/qwen3-embedding-8b"
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
run_async(
|
|
|
|
|
rt.handle_embeddings(
|
|
|
|
|
{"model": "nonexistent-embed-model", "input": "hello"},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "fallback_test_embed"),
|
|
|
|
|
"test",
|
|
|
|
|
"default",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
assert rt._client.calls[-1][1]["model"] == "qwen/qwen3-embedding-8b"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_images_unknown_model_falls_back_to_default(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeImageClient_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
cfg["gateway_force_model"] = False
|
|
|
|
|
cfg["gateway_image_enabled"] = True
|
|
|
|
|
cfg["gateway_image_model"] = "black-forest-labs/flux.2-pro"
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
run_async(
|
|
|
|
|
rt.handle_images(
|
|
|
|
|
{"model": "nonexistent-image-model", "prompt": "test badge"},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "fallback_test_img"),
|
|
|
|
|
"test",
|
|
|
|
|
"default",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
assert rt._client.calls[-1][1]["model"] == "black-forest-labs/flux.2-pro"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FakeClientWithUsage_openai_gateway(FakeClient_openai_gateway):
|
|
|
|
|
async def send(self, request):
|
|
|
|
|
self.calls.append((request.url, request.json_body))
|
|
|
|
|
body = request.json_body or {}
|
|
|
|
|
return FakeResp_openai_gateway(
|
|
|
|
|
payload={
|
|
|
|
|
"id": "x",
|
|
|
|
|
"model": body.get("model"),
|
|
|
|
|
"choices": [{"message": {"content": "hi there"}}],
|
|
|
|
|
"usage": {"prompt_tokens": 7, "completion_tokens": 3, "total_tokens": 10},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_stream_options_stripped_from_upstream(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeClient_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
run_async(
|
|
|
|
|
rt.handle_chat(
|
|
|
|
|
{
|
|
|
|
|
"messages": [{"role": "user", "content": "hi"}],
|
|
|
|
|
"stream": True,
|
|
|
|
|
"stream_options": {"include_usage": True},
|
|
|
|
|
},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "test"),
|
|
|
|
|
"test",
|
|
|
|
|
"default",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
upstream_payload = rt._client.calls[-1][1]
|
|
|
|
|
assert "stream_options" not in upstream_payload
|
|
|
|
|
assert upstream_payload["stream"] is False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_include_usage_emits_usage_chunk(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeClientWithUsage_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
resp = run_async(
|
|
|
|
|
rt.handle_chat(
|
|
|
|
|
{
|
|
|
|
|
"messages": [{"role": "user", "content": "hi"}],
|
|
|
|
|
"stream": True,
|
|
|
|
|
"stream_options": {"include_usage": True},
|
|
|
|
|
},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "test"),
|
|
|
|
|
"test",
|
|
|
|
|
"default",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def drain():
|
|
|
|
|
out = []
|
|
|
|
|
async for chunk in resp.body_iterator:
|
|
|
|
|
out.append(chunk if isinstance(chunk, str) else chunk.decode())
|
|
|
|
|
return "".join(out)
|
|
|
|
|
|
|
|
|
|
body = run_async(drain())
|
|
|
|
|
assert '"usage"' in body
|
|
|
|
|
assert '"total_tokens": 10' in body
|
|
|
|
|
assert "[DONE]" in body
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_no_usage_chunk_without_include_usage(local_db, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeClientWithUsage_openai_gateway)
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
cfg = svc.effective_config()
|
|
|
|
|
rt = svc.runtime()
|
|
|
|
|
resp = run_async(
|
|
|
|
|
rt.handle_chat(
|
|
|
|
|
{"messages": [{"role": "user", "content": "hi"}], "stream": True},
|
|
|
|
|
cfg,
|
|
|
|
|
("guest", "test"),
|
|
|
|
|
"test",
|
|
|
|
|
"default",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def drain():
|
|
|
|
|
out = []
|
|
|
|
|
async for chunk in resp.body_iterator:
|
|
|
|
|
out.append(chunk if isinstance(chunk, str) else chunk.decode())
|
|
|
|
|
return "".join(out)
|
|
|
|
|
|
|
|
|
|
body = run_async(drain())
|
|
|
|
|
assert '"usage"' not in body
|
|
|
|
|
assert "[DONE]" in body
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_models_endpoint_publishes_molodetz(local_db):
|
|
|
|
|
from devplacepy.services.openai_gateway import routing
|
|
|
|
|
|
|
|
|
|
routing.seed_default_deepseek_routes()
|
|
|
|
|
try:
|
|
|
|
|
svc = GatewayService()
|
|
|
|
|
resp = svc._models_response()
|
|
|
|
|
payload = json.loads(resp.body.decode())
|
|
|
|
|
ids = {m["id"] for m in payload["data"]}
|
|
|
|
|
assert payload["object"] == "list"
|
|
|
|
|
assert {"molodetz", "molodetz-pro"} <= ids
|
|
|
|
|
for model in payload["data"]:
|
|
|
|
|
assert model["object"] == "model"
|
|
|
|
|
assert model["owned_by"] == "molodetz"
|
|
|
|
|
finally:
|
|
|
|
|
routing.model_store.remove("molodetz")
|
|
|
|
|
routing.model_store.remove("molodetz-pro")
|