Backend - Routes moved under /api/, JWT bearer auth via @before_request - DEV_AUTH_BYPASS escape hatch for local dev - In-memory chat history and report state replaced with Postgres tables (preferences, chat_messages, reports, feedback_events) keyed on user - SQLAlchemy 2.x + Alembic migrations run on container start - Graceful Airtable failure handling — bad creds no longer 500 the API - Per-user data isolation via g.user_email from validated token Frontend - React + Vite + TypeScript SPA at /programme-pulse/ - MSAL.js (PKCE, sessionStorage, ID token to backend) - VITE_DEV_AUTH_BYPASS mirrors backend bypass for local dev - Streaming chat via fetch ReadableStream + SSE parsing - Charts via chart.js, markdown via react-markdown + remark-gfm - Full UI parity with the original templates/index.html Deploy (optical-dev split-build pattern) - Dockerfile + docker-compose.yml (name: programme-pulse pinned; app + Postgres; 127.0.0.1 binding only) - deploy/apache-programme-pulse.conf.tmpl with flushpackets=on for SSE - deploy/deploy.sh mirrors OSOP — port auto-pick (5051..5099), apache conf render, frontend build in throwaway node container, rsync to /var/www/html/programme-pulse, /api/health poll Tests - 49 passing; new tests for DB-backed preferences and JWT auth helpers - SQLite-backed test fixture in tests/conftest.py Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
from anthropic import Anthropic
|
|
|
|
MODEL = "claude-sonnet-4-6"
|
|
|
|
|
|
class ClaudeClient:
|
|
def __init__(self, api_key: str):
|
|
self._client = Anthropic(api_key=api_key)
|
|
|
|
def generate_report(self, system_prompt: str, user_prompt: str) -> str:
|
|
response = self._client.messages.create(
|
|
model=MODEL,
|
|
max_tokens=4096,
|
|
system=system_prompt,
|
|
messages=[{"role": "user", "content": user_prompt}],
|
|
)
|
|
return "".join(
|
|
block.text for block in response.content if block.type == "text"
|
|
)
|
|
|
|
def chat(self, messages: list[dict], system_prompt: str) -> str:
|
|
"""Send a conversational turn and return the assistant response."""
|
|
response = self._client.messages.create(
|
|
model=MODEL,
|
|
max_tokens=8192,
|
|
system=system_prompt,
|
|
messages=messages,
|
|
)
|
|
return "".join(
|
|
block.text for block in response.content if block.type == "text"
|
|
)
|
|
|
|
def chat_stream(self, messages: list[dict], system_prompt: str):
|
|
"""Return a streaming context manager that yields text chunks."""
|
|
return self._client.messages.stream(
|
|
model=MODEL,
|
|
max_tokens=8192,
|
|
system=system_prompt,
|
|
messages=messages,
|
|
)
|