The worst bug of the session. tests/conftest.py built its engine from settings.database_url — the live database — and an autouse fixture calls drop_all() before EVERY test. So every backend run silently annihilated the real install: accounts, discovered spirits, Ghost Logs, devices, all of it. Found it because /sitemap.xml listed zero entities minutes after I had watched live séances mint real ones. Tests now use TEST_DATABASE_URL, or `<configured-db>_test` derived from it, and refuse to start at all if that ever resolves back to the production URL — this box both serves the app and holds the repo, so "don't run tests in prod" is not a workable guard. Proven: inserted a canary row into production, ran 50 tests, canary survived. Before this it would have been dropped. Also in this commit: SEO (routes/seo.py, lib/pageMeta.ts) - Live /sitemap.xml generated from real entity rows, and /robots.txt, both registered BEFORE the SPA catch-all or they'd be served index.html. Crawlers are disallowed from /seance specifically because the open door provisions a guest on arrival — a crawler would fill the users table with wanderers who never existed. - Per-route <title>, description, canonical and JSON-LD. The Codex is the indexable asset here (every spirit is unique long-form prose) and all of it previously shared one static title, so entities competed with each other instead of ranking. Entities are marked up as fictional Persons so a rich result can never imply a record of a real dead human. - public_base_url setting: absolute URLs for crawlers can't be derived from the request, since behind the tunnel the app only sees an internal host. Camera channel, first half (lib/camera.ts, llm scry path) - OllamaClient.generate() now accepts `images`; the configured chat model (minicpm-v4.5:8b) is vision-capable, so the entity can speak about what the seeker's camera actually shows. Verified against a synthetic room image: it named the pale column and the small red cube, then misread them as oak in a farmhouse parlor — real perception, in character. - Frames are captured only on an explicit act, downscaled to 768px and JPEG-compressed, never stored, and the prompt forbids describing faces or guessing identity. CameraEye carries the same generation guard as the EVP listener so closing during the permission prompt can't leave the camera live after teardown. 338 backend tests pass; 375 frontend; i18n parity holds. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
67 lines
2.3 KiB
Python
67 lines
2.3 KiB
Python
from collections.abc import AsyncIterator
|
|
|
|
import httpx
|
|
|
|
from app.config import settings
|
|
|
|
|
|
class OllamaClient:
|
|
def __init__(self, base_url: str | None = None):
|
|
self._base_url = base_url or settings.ollama_base_url
|
|
|
|
async def generate(
|
|
self,
|
|
model: str,
|
|
prompt: str,
|
|
system: str | None = None,
|
|
options: dict | None = None,
|
|
images: list[str] | None = None,
|
|
) -> str:
|
|
"""`images` is a list of raw base64 JPEG/PNG strings (no data-URL
|
|
prefix) for vision-capable models — Ollama's /api/generate takes them
|
|
alongside the prompt. Ignored by text-only models, so passing them is
|
|
safe; the caller is responsible for choosing a model that can see."""
|
|
payload: dict = {"model": model, "prompt": prompt, "stream": False}
|
|
if system is not None:
|
|
payload["system"] = system
|
|
if options:
|
|
payload["options"] = options
|
|
if images:
|
|
payload["images"] = images
|
|
|
|
async with httpx.AsyncClient(base_url=self._base_url, timeout=120.0) as http_client:
|
|
response = await http_client.post("/api/generate", json=payload)
|
|
response.raise_for_status()
|
|
return response.json()["response"]
|
|
|
|
async def generate_stream(
|
|
self,
|
|
model: str,
|
|
prompt: str,
|
|
system: str | None = None,
|
|
options: dict | None = None,
|
|
) -> AsyncIterator[str]:
|
|
"""Yields response tokens as Ollama produces them."""
|
|
payload: dict = {"model": model, "prompt": prompt, "stream": True}
|
|
if system is not None:
|
|
payload["system"] = system
|
|
if options:
|
|
payload["options"] = options
|
|
|
|
async with httpx.AsyncClient(base_url=self._base_url, timeout=300.0) as http_client:
|
|
async with http_client.stream(
|
|
"POST", "/api/generate", json=payload
|
|
) as response:
|
|
response.raise_for_status()
|
|
async for line in response.aiter_lines():
|
|
if not line:
|
|
continue
|
|
import json
|
|
|
|
chunk = json.loads(line)
|
|
token = chunk.get("response", "")
|
|
if token:
|
|
yield token
|
|
if chunk.get("done"):
|
|
return
|