Compare commits
103 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b1e210de5d | |||
| cdd067fb8e | |||
| 0b8fe7b2df | |||
| e03c10cde5 | |||
| 9858e06b73 | |||
| ccc5126ae0 | |||
| 4d29bb3ca4 | |||
| 64b20754a1 | |||
| 6fb9bade08 | |||
| 70475865cc | |||
| 98b4b46b11 | |||
| 834c2c5bfe | |||
| b7ac24f2ff | |||
| 69bf5edefe | |||
| 4c0d161f10 | |||
| c24a44618f | |||
| c183a13db2 | |||
| c682d2b320 | |||
| aa61ec54b9 | |||
| b79229a5b0 | |||
| 7d1a89475a | |||
| 69b8697389 | |||
| 0367dac3cd | |||
| 828fc7d16a | |||
| fcadec8bf6 | |||
| 2674a49574 | |||
| 4c55168d52 | |||
| 863ebe8e20 | |||
| b6b866c9fc | |||
| f709528def | |||
| d23273590f | |||
| 53bbea42aa | |||
| d806416f93 | |||
| c6419f8931 | |||
| 790a7059ce | |||
| 171410cf26 | |||
| 504753eef9 | |||
| 15a03dba91 | |||
| c9c75b6be3 | |||
| 0e80e8b9df | |||
| 6e72d5f95a | |||
| 35b0e74a41 | |||
| 2542fa2342 | |||
| e36bf35b2a | |||
| 19aa2b17d7 | |||
| 43e0de24d6 | |||
| 2a81ea8f76 | |||
| 810d32dcc3 | |||
| 6621a99149 | |||
| 801a90746b | |||
| 202baa17e2 | |||
| f1ab1accaf | |||
| bb4619aefe | |||
| 82f16bfd6a | |||
| 7912c83ca3 | |||
| bc19f52052 | |||
| 0122e018ac | |||
| 7474055c97 | |||
| 0478ee7568 | |||
| 46a4b874bd | |||
| 9777eaf9e1 | |||
| bb2103e81e | |||
| 2a1f125fa5 | |||
| 8fd6fa9ece | |||
| 3875ed4e4f | |||
| c071fd12e1 | |||
| 6427b71043 | |||
| b132efe6a0 | |||
| d7639d9d0a | |||
| a591cfa343 | |||
| ca37c4a780 | |||
| 7527ef3adf | |||
| a172a71a70 | |||
| 600791ea5d | |||
| 0212058677 | |||
| 684460903f | |||
| 0a0df266c2 | |||
| 1e4942e0b8 | |||
| 2988482791 | |||
| 56ad5f7046 | |||
| b361377088 | |||
| ce461b6ff9 | |||
| bddd741fcc | |||
| 2db7e6f2e5 | |||
| b23fd19c4b | |||
| 4e3842495f | |||
| 434c394651 | |||
| 20d44bb3b3 | |||
| d8d01a5fff | |||
| 3318839551 | |||
| 4fe5b8daea | |||
| 53f76f58ea | |||
| b918522816 | |||
| eb8cd19e46 | |||
| 09dd983c4e | |||
| 35a2e10bc8 | |||
| 530a90f237 | |||
| bde4a994b7 | |||
| 6a62db2e85 | |||
| 47db945393 | |||
| 6293d9df0a | |||
| 32285891fc | |||
| 7aef6484c3 |
+42
-5
@@ -10,6 +10,12 @@ POSTGRES_DB=raggr
|
||||
# JWT Configuration
|
||||
JWT_SECRET_KEY=your-secret-key-here
|
||||
|
||||
# Metrics Configuration
|
||||
# Bearer token for the /metrics endpoint (Prometheus scrape + app token gate).
|
||||
# Generate with: openssl rand -hex 32
|
||||
# Leave empty to keep /metrics returning 403 (fail-closed).
|
||||
METRICS_TOKEN=
|
||||
|
||||
# Paperless Configuration
|
||||
PAPERLESS_TOKEN=your-paperless-token
|
||||
BASE_URL=192.168.1.5:8000
|
||||
@@ -18,6 +24,7 @@ BASE_URL=192.168.1.5:8000
|
||||
# If set, uses llama-server as the primary LLM backend with OpenAI as fallback
|
||||
LLAMA_SERVER_URL=http://192.168.1.213:8080/v1
|
||||
LLAMA_MODEL_NAME=llama-3.1-8b-instruct
|
||||
TTFT_TIMEOUT_SECONDS=30
|
||||
|
||||
# Embedding Server Configuration
|
||||
# If set, uses a custom OpenAI-compatible embedding server (e.g. llama-server)
|
||||
@@ -107,11 +114,21 @@ ALLOWED_IMESSAGE_NUMBERS=
|
||||
# IMESSAGE_RATE_LIMIT_MAX=10
|
||||
# IMESSAGE_RATE_LIMIT_WINDOW=60
|
||||
|
||||
# Google Calendar Configuration (via gws CLI)
|
||||
GOOGLE_CALENDAR_ENABLED=true
|
||||
# Export credentials: gws auth export --unmasked > credentials.json
|
||||
# The file is mounted into the container at /app/config/gws-credentials.json
|
||||
GOOGLE_WORKSPACE_CLI_CREDENTIALS_FILE=/app/config/gws-credentials.json
|
||||
# Google Calendar + Gmail Configuration (Python SDK)
|
||||
# Run scripts/google_auth.py to generate token.json from your OAuth2 credentials:
|
||||
# python scripts/google_auth.py --credentials credentials.json --token token.json
|
||||
# Then mount token.json into the container (see docker-compose.yml).
|
||||
# If GOOGLE_TOKEN_FILE points to a valid token, both Calendar and Gmail are enabled.
|
||||
GOOGLE_TOKEN_FILE=/app/config/google-token.json
|
||||
|
||||
# Cronicle Integration
|
||||
# Shared secret for Cronicle to authenticate cron job trigger requests.
|
||||
# Generate with: openssl rand -hex 32
|
||||
CRON_SECRET=
|
||||
# Cronicle server URL and API key (from Cronicle Admin → API Keys)
|
||||
# Used to auto-register scheduled jobs on startup.
|
||||
CRONICLE_URL=http://cronicle:3012
|
||||
CRONICLE_API_KEY=
|
||||
|
||||
# SimbaKit / PetKit Configuration
|
||||
# PetKit account credentials (used by SimbaKit to poll device data)
|
||||
@@ -122,6 +139,18 @@ PETKIT_TIMEZONE=America/New_York
|
||||
# Internal URL for SimbaKit service (Docker network)
|
||||
SIMBAKIT_URL=http://simbakit:8585
|
||||
|
||||
# Homebox inventory (admin-only agent tools via homebox-mcp, needs Homebox >= 0.26)
|
||||
# API key from Homebox Profile -> API Keys (starts with hb_). Leave empty to disable.
|
||||
HOMEBOX_URL=https://homebox.example.com
|
||||
HOMEBOX_TOKEN=
|
||||
|
||||
# BookStack wiki (admin-only agent tools via bookstack-mcp: read, create, update; no deletes)
|
||||
# API token from BookStack Profile -> API Tokens (user needs the "Access System API" permission).
|
||||
# Leave empty to disable.
|
||||
BOOKSTACK_BASE_URL=https://bookstack.example.com
|
||||
BOOKSTACK_TOKEN_ID=
|
||||
BOOKSTACK_TOKEN_SECRET=
|
||||
|
||||
# Obsidian Configuration (headless sync)
|
||||
# Obsidian account credentials (used for `ob login` on container startup)
|
||||
OBSIDIAN_EMAIL=your-obsidian-email
|
||||
@@ -136,3 +165,11 @@ OBSIDIAN_DEVICE_NAME=simbarag
|
||||
OBSIDIAN_CONTINUOUS_SYNC=false
|
||||
# Local path to Obsidian vault (where files are synced)
|
||||
OBSIDIAN_VAULT_PATH=/app/data/obsidian
|
||||
|
||||
# LangGraph checkpoint store (SQLite). Must be on a persistent volume.
|
||||
# Both blue and green containers share this volume via docker-compose.
|
||||
CHECKPOINT_DB_PATH=/app/checkpoints/checkpoints.db
|
||||
|
||||
# Beli restaurant recommendations
|
||||
# Beli account credentials for each profile are stored encrypted in the database.
|
||||
# No additional env vars needed — auth is done via the Beli API at login time.
|
||||
|
||||
@@ -12,6 +12,7 @@ wheels/
|
||||
# Environment files
|
||||
.env
|
||||
credentials.json
|
||||
token.json
|
||||
|
||||
# Database files
|
||||
database/
|
||||
|
||||
@@ -4,33 +4,39 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
|
||||
|
||||
## Project Overview
|
||||
|
||||
SimbaRAG is a RAG (Retrieval-Augmented Generation) conversational AI system for querying information about Simba (a cat). It ingests documents from Paperless-NGX, stores embeddings in PostgreSQL via pgvector, and uses LLMs (Ollama or OpenAI) to answer questions.
|
||||
SimbaRAG is a RAG (Retrieval-Augmented Generation) conversational AI system for querying information about Simba (a cat) and the household. It ingests documents from Paperless-NGX, Obsidian, and Immich, stores embeddings in PostgreSQL via pgvector (Chroma client), uses LLMs (Ollama or OpenAI) to answer questions with a tool-using LangChain agent, and exposes chat over web, iMessage, and WhatsApp.
|
||||
|
||||
## Commands
|
||||
|
||||
### Development
|
||||
|
||||
```bash
|
||||
# Start environment
|
||||
docker compose up --build
|
||||
# Start environment (raggr runs as raggr-blue/raggr-green behind compose profiles)
|
||||
docker compose --profile blue up --build
|
||||
|
||||
# View logs
|
||||
docker compose logs -f raggr
|
||||
# View logs (Makefile resolves the active color)
|
||||
make logs
|
||||
|
||||
# Reindex vector store (all sources / Obsidian only)
|
||||
make reindex
|
||||
make reindex-obsidian
|
||||
```
|
||||
|
||||
### Database Migrations (Aerich/Tortoise ORM)
|
||||
|
||||
```bash
|
||||
# Generate migration (must run in Docker with DB access)
|
||||
docker compose exec raggr aerich migrate --name describe_change
|
||||
make migrate-new
|
||||
|
||||
# Apply migrations (auto-runs on startup, manual if needed)
|
||||
docker compose exec raggr aerich upgrade
|
||||
make migrate
|
||||
|
||||
# View migration history
|
||||
docker compose exec raggr aerich history
|
||||
make migrate-history
|
||||
```
|
||||
|
||||
Migrations must be backward-compatible: blue-green deploys run `aerich upgrade` in the new container while the old code still serves.
|
||||
|
||||
### Frontend
|
||||
|
||||
```bash
|
||||
@@ -42,9 +48,10 @@ yarn dev # Dev server (rarely needed, backend serves frontend)
|
||||
|
||||
### Production
|
||||
|
||||
Zero-downtime blue-green deploy: `raggr-blue` (port 8080) and `raggr-green` (port 8081) alternate behind Caddy health checks. See `docs/deployment.md`.
|
||||
|
||||
```bash
|
||||
docker compose build raggr
|
||||
docker compose up -d
|
||||
make redeploy # git pull + scripts/deploy.sh (build new color, wait healthy, stop old)
|
||||
```
|
||||
|
||||
## Architecture
|
||||
@@ -53,9 +60,11 @@ docker compose up -d
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ Docker Compose │
|
||||
├─────────────────────────────────────────────────────────────┤
|
||||
│ raggr (port 8080) │ postgres (port 5432) │
|
||||
│ raggr-blue/green (8080/8081) │ postgres (port 5432) │
|
||||
│ ├── Quart backend │ PostgreSQL 16 + pgvector│
|
||||
│ └── React frontend (served) │ │
|
||||
│ ├── React frontend (served) │ │
|
||||
│ └── Twilio webhooks (iMessage/ │ │
|
||||
│ WhatsApp) │ │
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
@@ -63,33 +72,52 @@ docker compose up -d
|
||||
- `app.py` - Quart application entry, serves API and static frontend
|
||||
- `main.py` - RAG logic, document indexing, LLM interaction, LangChain agent
|
||||
- `llm.py` - LLM client with Ollama primary, OpenAI fallback
|
||||
- `aerich_config.py` - Database migration configuration
|
||||
- `blueprints/` - API routes organized as Quart blueprints
|
||||
- `users/` - OIDC auth, JWT tokens, RBAC with LDAP groups
|
||||
- `conversation/` - Chat conversations and message history
|
||||
- `rag/` - Document indexing endpoints (admin-only)
|
||||
- `conversation/` - Chat conversations, message history, streamed answers
|
||||
- `rag/` - Document indexing endpoints (admin-only); logic in `rag/logic.py`
|
||||
- `email/` - Email integration (Mailgun webhooks, Fernet encryption, IMAP, parsing)
|
||||
- `imessage/` - Twilio webhook receiver for iMessage (HMAC-validated, number allowlist)
|
||||
- `whatsapp/` - Twilio webhook receiver for WhatsApp (TWIML replies, SendBlue send support)
|
||||
- `scheduled_messages/` - Scheduled message delivery with background scheduler
|
||||
- `config/` - Configuration modules
|
||||
- `db.py` - Database setup
|
||||
- `oidc_config.py` - OIDC authentication configuration
|
||||
- `utils/` - Reusable utilities
|
||||
- `utils/` - Reusable utilities and household integrations
|
||||
- `chunker.py` - Document chunking for embeddings
|
||||
- `cleaner.py` - PDF cleaning and summarization
|
||||
- `image_process.py` - Image description with LLM
|
||||
- `request.py` - Paperless-NGX API client
|
||||
- `obsidian_service.py`, `mealie_service.py`, `ynab_service.py`, `s3_client.py`, `simbakit_service.py` - integrations with Obsidian notes, Mealie recipes, YNAB budgets, S3, and SimbaKit
|
||||
- `homebox_mcp.py` - Homebox inventory tools via the `homebox-mcp` stdio MCP server; admin-only (compiled into `app.admin_agent`, picked per user by `get_agent_for`)
|
||||
- `bookstack_mcp.py` - BookStack wiki tools (read/create/update, no deletes) via the `bookstack-mcp` npm stdio server; admin-only like Homebox
|
||||
- `mcp_tools.py` - shared allowlist + admin-gate wrapping for stdio MCP integrations
|
||||
- `scripts/` - Administrative and utility scripts
|
||||
- `add_user.py` - Create users manually
|
||||
- `user_message_stats.py` - User message statistics
|
||||
- `manage_vectorstore.py` - Vector store management CLI
|
||||
- `inspect_vector_store.py` - Inspect ChromaDB contents
|
||||
- `query.py` - Query generation utilities
|
||||
- `manage_vectorstore.py` / `inspect_vector_store.py` - Vector store CLI
|
||||
- `index_immich.py` - Index Immich photos into the vector store
|
||||
- `link_imessage.py` - Link iMessage contacts to users
|
||||
- `deploy.sh` - Blue-green deployment script
|
||||
- `migrations/` - Database migration files
|
||||
- `tests/` - pytest test suite (`tests/unit/` covers email/crypto, YNAB, Obsidian, SimbaKit, rate limiting, user model)
|
||||
|
||||
**simbakit/** (standalone subproject):
|
||||
- Self-hosted PetKit device poller + web dashboard for Simba's smart devices (feeder, litter box, fountain, purifier)
|
||||
- Own `pyproject.toml`, `uv.lock`, and Dockerfile; Python 3.14 + uv
|
||||
- Polls PetKit cloud API every 2 min into SQLite; Chart.js dashboard; docs in `simbakit/docs/`
|
||||
- Queried from the main app via `utils/simbakit_service.py`
|
||||
|
||||
**Frontend** (`raggr-frontend/`):
|
||||
- React 19 with Rsbuild bundler
|
||||
- Tailwind CSS for styling
|
||||
- Built to `dist/`, served by backend at `/`
|
||||
- Key components: `ChatScreen`, `ConversationsPage`, `LoginScreen`, `AdminPanel`, `ScheduledMessagesPanel`, `ToolBubble` (agent tool-call display)
|
||||
|
||||
**Auth Flow**: LLDAP → Authelia (OIDC) → Backend JWT → Frontend localStorage
|
||||
|
||||
**Data Sources**: Paperless-NGX documents, Obsidian vault, Immich photos (indexed via `scripts/index_immich.py` / `make reindex`)
|
||||
|
||||
## Testing
|
||||
|
||||
Always run `make test` before pushing code to ensure all tests pass.
|
||||
@@ -99,12 +127,23 @@ make test # Run tests
|
||||
make test-cov # Run tests with coverage
|
||||
```
|
||||
|
||||
Note: `make test` may fail locally (no pytest on PATH); use `.venv/bin/pytest tests/ -q` instead.
|
||||
|
||||
## Git Workflow
|
||||
|
||||
- The host runs **Gitea** (`git.torrtle.co`), not GitHub, despite the remote name. Use the **`tea` CLI** for PRs (Gitea flavor: `tea pr create`, `tea pr list`, etc. — no `tea create`).
|
||||
- Typical flow: commit locally, `git push -u origin <branch>`, then `tea pr create -t "title" -d "body"`.
|
||||
- "kermit" is the user's slang for **commit** (e.g. "kermit and push" = commit and push). There is no kermit tool.
|
||||
- Keep one logical change per branch/PR; don't ride along on stale feature branches.
|
||||
- Don't commit `.DS_Store`, `chroma_db/`, `chromadb/`, `.planning/` (local artifacts).
|
||||
|
||||
## Key Patterns
|
||||
|
||||
- All endpoints are async (`async def`)
|
||||
- Use `@jwt_refresh_token_required` for authenticated endpoints
|
||||
- Use `@admin_required` for admin-only endpoints (checks `lldap_admin` group)
|
||||
- Tortoise ORM models in `blueprints/*/models.py`
|
||||
- External integrations are service classes in `utils/*_service.py`
|
||||
- Frontend API services in `raggr-frontend/src/api/`
|
||||
|
||||
## Environment Variables
|
||||
@@ -112,6 +151,15 @@ make test-cov # Run tests with coverage
|
||||
See `.env.example`. Key ones:
|
||||
- `DATABASE_URL` - PostgreSQL connection
|
||||
- `OIDC_*` - Authelia OIDC configuration
|
||||
- `OLLAMA_URL` - Local LLM server
|
||||
- `OPENAI_API_KEY` - Fallback LLM
|
||||
- `LLAMA_SERVER_URL` / `LLAMA_MODEL_NAME` - Local Llama/Ollama LLM server
|
||||
- `EMBEDDING_SERVER_URL` / `EMBEDDING_MODEL_NAME` - Embedding model
|
||||
- `OPENAI_API_KEY` - Fallback LLM; `TAVILY_API_KEY` - web search tool
|
||||
- `PAPERLESS_TOKEN` / `BASE_URL` - Document source
|
||||
- `TWILIO_*`, `ALLOWED_IMESSAGE_NUMBERS`, `ALLOWED_WHATSAPP_NUMBERS` - iMessage/WhatsApp webhooks
|
||||
- `MAILGUN_*`, `EMAIL_HMAC_SECRET`, `FERNET_KEY` - email integration
|
||||
- `SENDBLUE_*` - SMS sending for scheduled messages
|
||||
- `YNAB_*`, `MEALIE_*`, `OBSIDIAN_*`, `IMMICH_*`, `SIMBAKIT_URL`, `HOMEBOX_URL`/`HOMEBOX_TOKEN`, `BOOKSTACK_BASE_URL`/`BOOKSTACK_TOKEN_ID`/`BOOKSTACK_TOKEN_SECRET` - household integrations
|
||||
|
||||
## Documentation
|
||||
|
||||
Project docs live in `docs/` (mkdocs): `deployment.md`, `development.md`, `authentication.md`, `MIGRATIONS.md`, `VECTORSTORE.md`, `TASKS.md`, `ynab_integration/`.
|
||||
|
||||
@@ -15,6 +15,12 @@ RUN apt-get update && apt-get install -y \
|
||||
# Add uv to PATH
|
||||
ENV PATH="/root/.local/bin:$PATH"
|
||||
|
||||
# Homebox MCP server (stdio), pinned so the tool surface can't drift
|
||||
RUN uv tool install homebox-mcp==1.1.0
|
||||
|
||||
# BookStack MCP server (stdio), pinned for the same reason
|
||||
RUN npm install -g bookstack-mcp@5.6.0
|
||||
|
||||
# Copy dependency files
|
||||
COPY pyproject.toml ./
|
||||
|
||||
|
||||
@@ -1,36 +1,64 @@
|
||||
.PHONY: deploy redeploy build up down restart logs migrate migrate-new frontend test
|
||||
.PHONY: deploy redeploy build up down restart logs migrate migrate-new migrate-history frontend test test-cov reindex reindex-obsidian
|
||||
|
||||
# Blue-green: detect the running color (default blue when nothing is running)
|
||||
ACTIVE_COLOR := $(shell docker compose ps --status running --services 2>/dev/null | grep -oE 'raggr-(blue|green)' | head -1 | cut -d- -f2)
|
||||
ifeq ($(ACTIVE_COLOR),)
|
||||
ACTIVE_COLOR := blue
|
||||
endif
|
||||
RAGGR := raggr-$(ACTIVE_COLOR)
|
||||
# Both profiles enabled for commands that only touch existing containers
|
||||
# (never use this with `up`, which would start both colors)
|
||||
COMPOSE := docker compose --profile blue --profile green
|
||||
|
||||
# Build and deploy
|
||||
deploy: build up
|
||||
|
||||
# Zero-downtime blue-green swap (see scripts/deploy.sh)
|
||||
redeploy:
|
||||
git pull && $(MAKE) down && $(MAKE) up
|
||||
git pull && ./scripts/deploy.sh
|
||||
|
||||
build:
|
||||
docker compose build raggr
|
||||
docker compose --profile $(ACTIVE_COLOR) build $(RAGGR)
|
||||
|
||||
up:
|
||||
docker compose up -d
|
||||
docker compose --profile $(ACTIVE_COLOR) up -d
|
||||
|
||||
down:
|
||||
docker compose down
|
||||
$(COMPOSE) down
|
||||
|
||||
restart:
|
||||
docker compose restart raggr
|
||||
$(COMPOSE) restart $(RAGGR)
|
||||
|
||||
logs:
|
||||
docker compose logs -f raggr
|
||||
$(COMPOSE) logs -f $(RAGGR)
|
||||
|
||||
# Vector store reindexing
|
||||
reindex:
|
||||
$(COMPOSE) exec $(RAGGR) python -c "\
|
||||
import asyncio; \
|
||||
from blueprints.rag.logic import delete_all_documents, index_documents, index_obsidian_documents; \
|
||||
delete_all_documents(); \
|
||||
asyncio.run(index_documents()); \
|
||||
asyncio.run(index_obsidian_documents()); \
|
||||
print('Reindex complete')"
|
||||
|
||||
reindex-obsidian:
|
||||
$(COMPOSE) exec $(RAGGR) python -c "\
|
||||
import asyncio; \
|
||||
from blueprints.rag.logic import delete_documents_by_metadata, index_obsidian_documents; \
|
||||
delete_documents_by_metadata('source', 'obsidian'); \
|
||||
asyncio.run(index_obsidian_documents()); \
|
||||
print('Obsidian reindex complete')"
|
||||
|
||||
# Database migrations
|
||||
migrate:
|
||||
docker compose exec raggr aerich upgrade
|
||||
$(COMPOSE) exec $(RAGGR) aerich upgrade
|
||||
|
||||
migrate-new:
|
||||
@read -p "Migration name: " name; \
|
||||
docker compose exec raggr aerich migrate --name $$name
|
||||
@./scripts/migrate-new.sh
|
||||
|
||||
migrate-history:
|
||||
docker compose exec raggr aerich history
|
||||
$(COMPOSE) exec $(RAGGR) aerich history
|
||||
|
||||
# Tests
|
||||
test:
|
||||
|
||||
@@ -127,8 +127,8 @@ cp .env.example .env
|
||||
# Development (local PostgreSQL only)
|
||||
docker compose -f docker-compose.dev.yml up -d
|
||||
|
||||
# Or full Docker deployment
|
||||
docker compose up -d
|
||||
# Or full Docker deployment (app runs as raggr-blue; see docs/deployment.md)
|
||||
docker compose --profile blue up -d
|
||||
```
|
||||
|
||||
4. **Access the application**
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
# Streaming Implementation Summary
|
||||
|
||||
## Overview
|
||||
The `/stream-query` endpoint uses Server-Sent Events (SSE) to give real-time feedback
|
||||
during AI query processing: tool-execution status **and** token-by-token streaming of the
|
||||
assistant's answer.
|
||||
|
||||
## Backend (`blueprints/conversation/__init__.py`)
|
||||
|
||||
### `POST /api/conversation/stream-query`
|
||||
Streams SSE frames (`data: {json}\n\n`) by iterating `main_agent.astream_events(..., version="v2")`.
|
||||
Event types emitted to the client:
|
||||
|
||||
- `tool_start` — a tool began (`{ "type": "tool_start", "tool": <name> }`), from `on_tool_start`.
|
||||
- `tool_end` — a tool finished, from `on_tool_end`.
|
||||
- `content` — an incremental answer chunk (`{ "type": "content", "delta": <text> }`), from
|
||||
`on_chat_model_stream`. Chunks are accumulated server-side into `streamed`.
|
||||
- `response` — the final, authoritative message (`{ "type": "response", "message": <full text> }`).
|
||||
Sourced from `on_chain_end` (falling back to the accumulated `streamed` tokens). This is the
|
||||
text that is persisted to the DB via `add_message_to_conversation(..., speaker="simba")`.
|
||||
- `error` — an error message.
|
||||
- The stream terminates with `data: [DONE]`.
|
||||
|
||||
The user's message is persisted before streaming begins; the assistant message is persisted once
|
||||
the stream completes.
|
||||
|
||||
### `POST /api/conversation/query` (legacy, non-streaming)
|
||||
Backwards-compatible endpoint that returns the complete response in one JSON payload
|
||||
(`{ "response": <text> }`). Kept for compatibility; the frontend uses `/stream-query`.
|
||||
|
||||
## Frontend
|
||||
|
||||
### API service (`src/api/conversationService.ts`)
|
||||
- `streamQuery(query, conversation_id, onEvent, signal?, imageKey?)` — POSTs to `/stream-query`
|
||||
and drives the response through `_readSSEStream()`.
|
||||
- `_readSSEStream()` — reads the `ReadableStream`, splits on `\n\n`, parses `data:` frames into
|
||||
`SSEEvent` objects, and stops on `[DONE]`.
|
||||
- `SSEEvent` union: `tool_start | tool_end | content | response | error`.
|
||||
|
||||
### Chat state (`src/hooks/useChat.ts`)
|
||||
`sendMessage()` consumes the stream and maintains a unified `messages` array:
|
||||
|
||||
- `tool_start` → appends a `tool` bubble (mapped to a friendly label via `TOOL_MESSAGES`) and
|
||||
resets the streaming pointer so any text after a tool call starts a fresh bubble.
|
||||
- `content` → on the first delta, appends a new `simba` bubble and flips `streamingActiveRef` /
|
||||
`streaming`; subsequent deltas append to that bubble's text.
|
||||
- `response` → overwrites the live `simba` bubble with the authoritative message (guaranteeing
|
||||
parity with what was persisted), or appends it if nothing streamed (e.g. an instant answer).
|
||||
- `error` → surfaces a user-facing error.
|
||||
|
||||
A `streaming` boolean is exposed so the UI can hide the redundant loading indicator once tokens
|
||||
start arriving.
|
||||
|
||||
### Components
|
||||
- `ChatScreen.tsx` — renders `messages` (`ToolBubble` / `AnswerBubble` / `QuestionBubble`) and
|
||||
shows the standalone loading-dots `AnswerBubble` only before the first token
|
||||
(`(isLoading && !streaming) || messagesLoading`).
|
||||
- `AnswerBubble.tsx` — renders the assistant message as markdown, or animated loading dots when
|
||||
`loading` is set.
|
||||
- `ToolBubble.tsx` — renders tool-status messages as inline badges.
|
||||
|
||||
## User Experience
|
||||
1. User sends a message.
|
||||
2. Loading dots appear.
|
||||
3. Tool status bubbles appear as tools run (e.g. "Searching Simba's records...").
|
||||
4. The answer streams in word-by-word into a single assistant bubble.
|
||||
5. On completion the bubble is reconciled with the persisted final text.
|
||||
|
||||
## Testing
|
||||
```bash
|
||||
# Backend
|
||||
make test # or: .venv/bin/python -m pytest tests/
|
||||
|
||||
# Frontend
|
||||
cd raggr-frontend && yarn build
|
||||
|
||||
# Full stack (manual)
|
||||
docker compose up --build
|
||||
```
|
||||
@@ -1,10 +1,18 @@
|
||||
import asyncio
|
||||
import hmac
|
||||
import logging
|
||||
import os
|
||||
from datetime import timedelta
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from quart import Quart, jsonify, render_template, send_from_directory
|
||||
from quart import (
|
||||
Quart,
|
||||
Response,
|
||||
jsonify,
|
||||
render_template,
|
||||
request,
|
||||
send_from_directory,
|
||||
)
|
||||
from quart_jwt_extended import JWTManager, get_jwt_identity, jwt_refresh_token_required
|
||||
from tortoise import Tortoise
|
||||
|
||||
@@ -16,8 +24,14 @@ import blueprints.users
|
||||
import blueprints.whatsapp
|
||||
import blueprints.imessage
|
||||
import blueprints.scheduled_messages
|
||||
import blueprints.cron
|
||||
import blueprints.beli
|
||||
import blueprints.users.models
|
||||
from config.db import TORTOISE_CONFIG
|
||||
from metrics import render_metrics
|
||||
from blueprints.conversation.agents import build_agent
|
||||
from utils.bookstack_mcp import load_bookstack_tools
|
||||
from utils.homebox_mcp import load_homebox_tools
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
@@ -54,6 +68,8 @@ app.register_blueprint(blueprints.rag.rag_blueprint)
|
||||
app.register_blueprint(blueprints.whatsapp.whatsapp_blueprint)
|
||||
app.register_blueprint(blueprints.imessage.imessage_blueprint)
|
||||
app.register_blueprint(blueprints.scheduled_messages.scheduled_messages_blueprint)
|
||||
app.register_blueprint(blueprints.cron.cron_blueprint)
|
||||
app.register_blueprint(blueprints.beli.beli_blueprint)
|
||||
|
||||
|
||||
async def _obsidian_sync_loop():
|
||||
@@ -84,19 +100,32 @@ async def lifespan():
|
||||
await Tortoise.init(config=TORTOISE_CONFIG)
|
||||
logging.info("Tortoise ORM initialized successfully")
|
||||
|
||||
watcher_task = None
|
||||
if os.getenv("OBSIDIAN_CONTINUOUS_SYNC") == "true":
|
||||
watcher_task = asyncio.create_task(_obsidian_sync_loop())
|
||||
import aiosqlite
|
||||
from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver
|
||||
|
||||
from blueprints.scheduled_messages.scheduler import scheduled_messages_loop
|
||||
checkpoint_path = os.getenv("CHECKPOINT_DB_PATH", "/app/checkpoints/checkpoints.db")
|
||||
async with aiosqlite.connect(checkpoint_path) as conn:
|
||||
checkpointer = AsyncSqliteSaver(conn)
|
||||
await checkpointer.setup()
|
||||
app.main_agent = build_agent(checkpointer)
|
||||
admin_tools = await load_homebox_tools() + await load_bookstack_tools()
|
||||
app.admin_agent = (
|
||||
build_agent(checkpointer, admin_tools) if admin_tools else app.main_agent
|
||||
)
|
||||
logging.info(f"Agent checkpointer ready at {checkpoint_path}")
|
||||
|
||||
scheduler_task = asyncio.create_task(scheduled_messages_loop())
|
||||
from utils.cronicle import ensure_beli_nightly_event
|
||||
|
||||
yield
|
||||
await ensure_beli_nightly_event()
|
||||
|
||||
scheduler_task.cancel()
|
||||
if watcher_task is not None:
|
||||
watcher_task.cancel()
|
||||
watcher_task = None
|
||||
if os.getenv("OBSIDIAN_CONTINUOUS_SYNC") == "true":
|
||||
watcher_task = asyncio.create_task(_obsidian_sync_loop())
|
||||
|
||||
yield
|
||||
|
||||
if watcher_task is not None:
|
||||
watcher_task.cancel()
|
||||
|
||||
logging.info("Closing Tortoise ORM connections...")
|
||||
await Tortoise.close_connections()
|
||||
@@ -129,6 +158,27 @@ ALLOWED_STATIC_EXTENSIONS = {
|
||||
ALLOWED_JSON_FILES = {"manifest.json"}
|
||||
|
||||
|
||||
# Prometheus metrics endpoint. Fail-closed: 403 unless a valid bearer token
|
||||
# matching METRICS_TOKEN is presented. Registered before the catch-all SPA
|
||||
# route so it is never shadowed.
|
||||
@app.get("/metrics")
|
||||
async def metrics_endpoint():
|
||||
expected = os.environ.get("METRICS_TOKEN", "")
|
||||
if not expected:
|
||||
return "unauthorized", 403
|
||||
auth = request.headers.get("Authorization", "")
|
||||
if not auth.startswith("Bearer "):
|
||||
return "unauthorized", 403
|
||||
provided = auth[len("Bearer ") :].strip()
|
||||
if not hmac.compare_digest(provided.encode(), expected.encode()):
|
||||
return "unauthorized", 403
|
||||
return Response(
|
||||
render_metrics(),
|
||||
status=200,
|
||||
content_type="text/plain; version=0.0.4",
|
||||
)
|
||||
|
||||
|
||||
# Serve the React app for all routes (catch-all)
|
||||
@app.route("/", defaults={"path": ""})
|
||||
@app.route("/<path:path>")
|
||||
@@ -144,6 +194,16 @@ async def serve_react_app(path):
|
||||
return await render_template("index.html")
|
||||
|
||||
|
||||
# Health check used by the Docker healthcheck and Caddy's active health checks
|
||||
@app.route("/api/health", methods=["GET"])
|
||||
async def health():
|
||||
try:
|
||||
await Tortoise.get_connection("default").execute_query("SELECT 1")
|
||||
except Exception:
|
||||
return jsonify({"status": "unhealthy"}), 503
|
||||
return jsonify({"status": "ok"})
|
||||
|
||||
|
||||
@app.route("/api/messages", methods=["GET"])
|
||||
@jwt_refresh_token_required
|
||||
async def get_messages():
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
import hmac
|
||||
import logging
|
||||
import os
|
||||
|
||||
from quart import Blueprint, jsonify, request
|
||||
from quart_jwt_extended import get_jwt_identity, jwt_refresh_token_required
|
||||
|
||||
from .logic import (
|
||||
MAX_PROFILES_PER_USER,
|
||||
_get_access_token,
|
||||
_refresh_friend_sample,
|
||||
refresh_profile,
|
||||
schedule_nightly_refresh,
|
||||
)
|
||||
from .models import BeliProfile, BeliRestaurant, BeliStatus
|
||||
|
||||
beli_blueprint = Blueprint("beli", __name__, url_prefix="/api/beli")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _check_cron_secret() -> bool:
|
||||
expected = os.environ.get("CRON_SECRET", "")
|
||||
if not expected:
|
||||
return False
|
||||
provided = request.headers.get("X-Cron-Secret", "")
|
||||
return hmac.compare_digest(provided.encode(), expected.encode())
|
||||
|
||||
|
||||
def _serialize_profile(profile: BeliProfile) -> dict:
|
||||
return {
|
||||
"id": str(profile.id),
|
||||
"beli_email": profile.beli_email,
|
||||
"beli_user_id": profile.beli_user_id,
|
||||
"enabled": profile.enabled,
|
||||
"last_refreshed_at": (
|
||||
profile.last_refreshed_at.isoformat() if profile.last_refreshed_at else None
|
||||
),
|
||||
"last_error": profile.last_error,
|
||||
"created_at": profile.created_at.isoformat(),
|
||||
}
|
||||
|
||||
|
||||
def _serialize_restaurant(r: BeliRestaurant) -> dict:
|
||||
return {
|
||||
"id": str(r.id),
|
||||
"profile_id": str(r.profile_id),
|
||||
"beli_business_id": r.beli_business_id,
|
||||
"name": r.name,
|
||||
"city": r.city,
|
||||
"neighborhood": r.neighborhood,
|
||||
"cuisine": r.cuisine,
|
||||
"price_range": r.price_range,
|
||||
"status": r.status.value,
|
||||
"rank_index": r.rank_index,
|
||||
"score": float(r.score) if r.score is not None else None,
|
||||
"notes": r.notes,
|
||||
"tags": r.tags,
|
||||
"fetched_at": r.fetched_at.isoformat(),
|
||||
}
|
||||
|
||||
|
||||
@beli_blueprint.route("/profiles", methods=["GET"])
|
||||
@jwt_refresh_token_required
|
||||
async def list_profiles():
|
||||
user_id = get_jwt_identity()
|
||||
profiles = await BeliProfile.filter(user_id=user_id).order_by("created_at").all()
|
||||
return jsonify([_serialize_profile(p) for p in profiles])
|
||||
|
||||
|
||||
@beli_blueprint.route("/profiles", methods=["POST"])
|
||||
@jwt_refresh_token_required
|
||||
async def create_profile():
|
||||
user_id = get_jwt_identity()
|
||||
|
||||
count = await BeliProfile.filter(user_id=user_id).count()
|
||||
if count >= MAX_PROFILES_PER_USER:
|
||||
return (
|
||||
jsonify(
|
||||
{"error": f"Maximum {MAX_PROFILES_PER_USER} Beli profiles per user"}
|
||||
),
|
||||
400,
|
||||
)
|
||||
|
||||
data = await request.get_json()
|
||||
if not data:
|
||||
return jsonify({"error": "Invalid payload"}), 400
|
||||
|
||||
email = (data.get("beli_email") or "").strip()
|
||||
password = (data.get("beli_password") or "").strip()
|
||||
if not email or not password:
|
||||
return jsonify({"error": "beli_email and beli_password are required"}), 400
|
||||
|
||||
if await BeliProfile.exists(user_id=user_id, beli_email=email):
|
||||
return jsonify({"error": "A profile with this email already exists"}), 409
|
||||
|
||||
from utils.beli_service import authenticate
|
||||
|
||||
try:
|
||||
session = await authenticate(email, password)
|
||||
except Exception as e:
|
||||
return jsonify({"error": f"Beli authentication failed: {e}"}), 400
|
||||
|
||||
profile = await BeliProfile.create(
|
||||
user_id=user_id,
|
||||
beli_email=email,
|
||||
beli_password=password,
|
||||
beli_user_id=session["user_id"],
|
||||
beli_refresh_token=session["refresh_token"],
|
||||
enabled=True,
|
||||
)
|
||||
return jsonify(_serialize_profile(profile)), 201
|
||||
|
||||
|
||||
@beli_blueprint.route("/profiles/<profile_id>", methods=["DELETE"])
|
||||
@jwt_refresh_token_required
|
||||
async def delete_profile(profile_id: str):
|
||||
user_id = get_jwt_identity()
|
||||
profile = await BeliProfile.get_or_none(id=profile_id, user_id=user_id)
|
||||
if not profile:
|
||||
return jsonify({"error": "Not found"}), 404
|
||||
|
||||
await BeliRestaurant.filter(profile=profile).delete()
|
||||
await profile.delete()
|
||||
return jsonify({"status": "deleted"})
|
||||
|
||||
|
||||
@beli_blueprint.route("/profiles/<profile_id>/refresh", methods=["POST"])
|
||||
@jwt_refresh_token_required
|
||||
async def refresh_profile_endpoint(profile_id: str):
|
||||
user_id = get_jwt_identity()
|
||||
profile = await BeliProfile.get_or_none(id=profile_id, user_id=user_id)
|
||||
if not profile:
|
||||
return jsonify({"error": "Not found"}), 404
|
||||
|
||||
result = await refresh_profile(str(profile.id))
|
||||
return jsonify(result)
|
||||
|
||||
|
||||
@beli_blueprint.route("/restaurants", methods=["GET"])
|
||||
@jwt_refresh_token_required
|
||||
async def list_restaurants():
|
||||
user_id = get_jwt_identity()
|
||||
profile_ids = [
|
||||
p.id for p in await BeliProfile.filter(user_id=user_id).only("id").all()
|
||||
]
|
||||
|
||||
qs = BeliRestaurant.filter(profile_id__in=profile_ids)
|
||||
|
||||
status_filter = request.args.get("status")
|
||||
city_filter = request.args.get("city")
|
||||
if status_filter:
|
||||
try:
|
||||
qs = qs.filter(status=BeliStatus(status_filter))
|
||||
except ValueError:
|
||||
return jsonify({"error": f"Invalid status: {status_filter}"}), 400
|
||||
if city_filter:
|
||||
qs = qs.filter(city__icontains=city_filter)
|
||||
|
||||
restaurants = await qs.order_by("rank_index", "name").all()
|
||||
return jsonify([_serialize_restaurant(r) for r in restaurants])
|
||||
|
||||
|
||||
@beli_blueprint.route("/profiles/<profile_id>/refresh-friends", methods=["POST"])
|
||||
@jwt_refresh_token_required
|
||||
async def refresh_friends_endpoint(profile_id: str):
|
||||
user_id = get_jwt_identity()
|
||||
profile = await BeliProfile.get_or_none(id=profile_id, user_id=user_id)
|
||||
if not profile:
|
||||
return jsonify({"error": "Not found"}), 404
|
||||
|
||||
try:
|
||||
access_token = await _get_access_token(profile)
|
||||
upserted = await _refresh_friend_sample(profile, access_token)
|
||||
return jsonify({"ok": True, "upserted": upserted})
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@beli_blueprint.route("/cron/refresh", methods=["POST"])
|
||||
async def cron_nightly_refresh():
|
||||
if not _check_cron_secret():
|
||||
return jsonify({"error": "Unauthorized"}), 401
|
||||
|
||||
result = await schedule_nightly_refresh()
|
||||
return jsonify(result)
|
||||
@@ -0,0 +1,252 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import random
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from utils.beli_service import (
|
||||
RANKING_CATEGORIES,
|
||||
REQUEST_SPACING,
|
||||
authenticate,
|
||||
extract_business,
|
||||
fetch_bookmarks,
|
||||
fetch_following,
|
||||
fetch_user_rankings,
|
||||
is_token_expired,
|
||||
refresh_access_token,
|
||||
)
|
||||
|
||||
from tortoise.functions import Max
|
||||
|
||||
from .models import BeliProfile, BeliRestaurant, BeliStatus
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
MAX_PROFILES_PER_USER = 3
|
||||
NIGHTLY_BATCH_SIZE = 3
|
||||
NIGHTLY_WINDOW_SECONDS = 4 * 3600 # spread refreshes across a 4-hour window
|
||||
FRIEND_SAMPLE_SIZE = 5 # friends to randomly refresh per profile per night
|
||||
|
||||
|
||||
async def _get_access_token(profile: BeliProfile) -> str:
|
||||
"""Return a live access token, refreshing or re-authenticating as needed."""
|
||||
if profile.beli_refresh_token and not is_token_expired(profile.beli_refresh_token):
|
||||
try:
|
||||
token = await refresh_access_token(profile.beli_refresh_token)
|
||||
return token
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"Refresh token failed for profile %s, falling back to password",
|
||||
profile.id,
|
||||
)
|
||||
|
||||
session = await authenticate(profile.beli_email, profile.beli_password)
|
||||
profile.beli_user_id = session["user_id"]
|
||||
profile.beli_refresh_token = session["refresh_token"]
|
||||
await profile.save()
|
||||
return session["access_token"]
|
||||
|
||||
|
||||
async def _refresh_friend_sample(profile: BeliProfile, access_token: str) -> int:
|
||||
"""Fetch rankings for a random sample of followed users and upsert them."""
|
||||
try:
|
||||
following = await fetch_following(access_token, profile.beli_user_id)
|
||||
except Exception as e:
|
||||
logger.debug("Beli following fetch skipped for profile %s: %s", profile.id, e)
|
||||
return 0
|
||||
|
||||
if not following:
|
||||
return 0
|
||||
|
||||
# Pick the FRIEND_SAMPLE_SIZE stalest friends — never-fetched first, then oldest
|
||||
fetched_rows = (
|
||||
await BeliRestaurant.filter(profile=profile)
|
||||
.exclude(friend_beli_user_id="")
|
||||
.group_by("friend_beli_user_id")
|
||||
.annotate(last_fetched=Max("fetched_at"))
|
||||
.values("friend_beli_user_id", "last_fetched")
|
||||
)
|
||||
last_fetched: dict[str, datetime] = {
|
||||
r["friend_beli_user_id"]: r["last_fetched"] for r in fetched_rows
|
||||
}
|
||||
_epoch = datetime.fromtimestamp(0, tz=timezone.utc)
|
||||
sample = sorted(
|
||||
following,
|
||||
key=lambda f: last_fetched.get(str(f.get("id") or ""), _epoch),
|
||||
)[:FRIEND_SAMPLE_SIZE]
|
||||
upserted = 0
|
||||
|
||||
for friend in sample:
|
||||
friend_id = str(friend.get("id") or "")
|
||||
friend_name = friend.get("full_name") or friend.get("username") or friend_id
|
||||
if not friend_id:
|
||||
continue
|
||||
|
||||
for category in RANKING_CATEGORIES:
|
||||
await asyncio.sleep(REQUEST_SPACING)
|
||||
try:
|
||||
results = await fetch_user_rankings(access_token, friend_id, category)
|
||||
except Exception as e:
|
||||
# 405 = private profile; log quietly and skip
|
||||
logger.debug(
|
||||
"Beli friend ranking skipped (%s/%s): %s", friend_id, category, e
|
||||
)
|
||||
continue
|
||||
|
||||
for idx, item in enumerate(results):
|
||||
biz = extract_business(item, rank_index=idx + 1)
|
||||
if not biz:
|
||||
continue
|
||||
await BeliRestaurant.update_or_create(
|
||||
profile=profile,
|
||||
beli_business_id=biz["beli_business_id"],
|
||||
status=BeliStatus.BEEN,
|
||||
friend_beli_user_id=friend_id,
|
||||
beli_category=category,
|
||||
defaults={
|
||||
"friend_name": friend_name,
|
||||
"name": biz["name"],
|
||||
"city": biz["city"],
|
||||
"neighborhood": biz["neighborhood"],
|
||||
"cuisine": biz["cuisine"],
|
||||
"price_range": biz["price_range"],
|
||||
"rank_index": biz["rank_index"],
|
||||
"score": biz["score"],
|
||||
"notes": None,
|
||||
"tags": biz["tags"],
|
||||
},
|
||||
)
|
||||
upserted += 1
|
||||
|
||||
if upserted:
|
||||
logger.info(
|
||||
"Beli profile %s: %d friend restaurants upserted from %d friends",
|
||||
profile.id,
|
||||
upserted,
|
||||
len(sample),
|
||||
)
|
||||
return upserted
|
||||
|
||||
|
||||
async def refresh_profile(profile_id: str) -> dict:
|
||||
"""Fetch the latest rankings and bookmarks for a profile and upsert them."""
|
||||
profile = await BeliProfile.get_or_none(id=profile_id)
|
||||
if not profile or not profile.enabled:
|
||||
return {"skipped": True}
|
||||
|
||||
try:
|
||||
access_token = await _get_access_token(profile)
|
||||
|
||||
upserted = 0
|
||||
|
||||
# Fetch all ranking categories (restaurants the user has been to and ranked)
|
||||
for category in RANKING_CATEGORIES:
|
||||
await asyncio.sleep(REQUEST_SPACING)
|
||||
results = await fetch_user_rankings(
|
||||
access_token, profile.beli_user_id, category
|
||||
)
|
||||
for idx, item in enumerate(results):
|
||||
biz = extract_business(item, rank_index=idx + 1)
|
||||
if not biz:
|
||||
continue
|
||||
await BeliRestaurant.update_or_create(
|
||||
profile=profile,
|
||||
beli_business_id=biz["beli_business_id"],
|
||||
status=BeliStatus.BEEN,
|
||||
friend_beli_user_id="",
|
||||
beli_category=category,
|
||||
defaults={
|
||||
"name": biz["name"],
|
||||
"city": biz["city"],
|
||||
"neighborhood": biz["neighborhood"],
|
||||
"cuisine": biz["cuisine"],
|
||||
"price_range": biz["price_range"],
|
||||
"rank_index": biz["rank_index"],
|
||||
"score": biz["score"],
|
||||
"notes": biz["notes"],
|
||||
"tags": biz["tags"],
|
||||
},
|
||||
)
|
||||
upserted += 1
|
||||
|
||||
# Fetch bookmarks (want-to-try list) — non-fatal if unavailable
|
||||
await asyncio.sleep(REQUEST_SPACING)
|
||||
try:
|
||||
bookmarks = await fetch_bookmarks(access_token)
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"Beli bookmarks fetch failed for profile %s: %s", profile_id, e
|
||||
)
|
||||
bookmarks = []
|
||||
for item in bookmarks:
|
||||
biz = extract_business(item)
|
||||
if not biz:
|
||||
continue
|
||||
await BeliRestaurant.update_or_create(
|
||||
profile=profile,
|
||||
beli_business_id=biz["beli_business_id"],
|
||||
status=BeliStatus.WANT_TO_TRY,
|
||||
friend_beli_user_id="",
|
||||
beli_category="",
|
||||
defaults={
|
||||
"name": biz["name"],
|
||||
"city": biz["city"],
|
||||
"neighborhood": biz["neighborhood"],
|
||||
"cuisine": biz["cuisine"],
|
||||
"price_range": biz["price_range"],
|
||||
"rank_index": None,
|
||||
"score": None,
|
||||
"notes": biz["notes"],
|
||||
"tags": biz["tags"],
|
||||
},
|
||||
)
|
||||
upserted += 1
|
||||
|
||||
profile.last_refreshed_at = datetime.now(timezone.utc)
|
||||
profile.last_error = None
|
||||
await profile.save()
|
||||
|
||||
logger.info(
|
||||
"Beli profile %s refreshed: %d restaurants upserted", profile_id, upserted
|
||||
)
|
||||
return {"ok": True, "upserted": upserted}
|
||||
|
||||
except Exception as e:
|
||||
profile.last_error = str(e)
|
||||
await profile.save()
|
||||
logger.error("Failed to refresh Beli profile %s: %s", profile_id, e)
|
||||
return {"error": str(e)}
|
||||
|
||||
|
||||
async def _refresh_after_delay(profile_id: str, delay: float) -> None:
|
||||
await asyncio.sleep(delay)
|
||||
profile = await BeliProfile.get_or_none(id=profile_id)
|
||||
if not profile:
|
||||
return
|
||||
await refresh_profile(profile_id)
|
||||
# Friend sampling only runs during nightly scheduled refreshes, not manual ones
|
||||
try:
|
||||
access_token = await _get_access_token(profile)
|
||||
await _refresh_friend_sample(profile, access_token)
|
||||
except Exception as e:
|
||||
logger.warning("Friend sample failed for profile %s: %s", profile_id, e)
|
||||
|
||||
|
||||
async def schedule_nightly_refresh() -> dict:
|
||||
"""Pick the NIGHTLY_BATCH_SIZE stalest profiles and schedule them with random delays."""
|
||||
profiles = (
|
||||
await BeliProfile.filter(enabled=True)
|
||||
.order_by("last_refreshed_at")
|
||||
.limit(NIGHTLY_BATCH_SIZE)
|
||||
.all()
|
||||
)
|
||||
|
||||
if not profiles:
|
||||
logger.info("No enabled Beli profiles to refresh")
|
||||
return {"scheduled": 0}
|
||||
|
||||
for profile in profiles:
|
||||
delay = random.uniform(0, NIGHTLY_WINDOW_SECONDS)
|
||||
asyncio.create_task(_refresh_after_delay(str(profile.id), delay))
|
||||
logger.info("Scheduled Beli refresh for profile %s in %.0fs", profile.id, delay)
|
||||
|
||||
return {"scheduled": len(profiles)}
|
||||
@@ -0,0 +1,66 @@
|
||||
import enum
|
||||
import uuid
|
||||
|
||||
from tortoise import fields
|
||||
from tortoise.models import Model
|
||||
|
||||
from blueprints.email.crypto_service import EncryptedTextField
|
||||
|
||||
|
||||
class BeliStatus(str, enum.Enum):
|
||||
BEEN = "been"
|
||||
WANT_TO_TRY = "want_to_try"
|
||||
|
||||
|
||||
class BeliProfile(Model):
|
||||
id = fields.UUIDField(primary_key=True, default=uuid.uuid4)
|
||||
user = fields.ForeignKeyField("models.User", related_name="beli_profiles")
|
||||
beli_email = fields.CharField(max_length=255)
|
||||
beli_password = EncryptedTextField()
|
||||
beli_user_id = fields.CharField(max_length=255, null=True)
|
||||
# refresh token lasts 7 days; we store it encrypted to avoid re-authenticating each run
|
||||
beli_refresh_token = EncryptedTextField(null=True)
|
||||
last_refreshed_at = fields.DatetimeField(null=True)
|
||||
last_error = fields.TextField(null=True)
|
||||
enabled = fields.BooleanField(default=True)
|
||||
created_at = fields.DatetimeField(auto_now_add=True)
|
||||
updated_at = fields.DatetimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
table = "beli_profiles"
|
||||
unique_together = (("user", "beli_email"),)
|
||||
|
||||
|
||||
class BeliRestaurant(Model):
|
||||
id = fields.UUIDField(primary_key=True, default=uuid.uuid4)
|
||||
profile = fields.ForeignKeyField("models.BeliProfile", related_name="restaurants")
|
||||
beli_business_id = fields.CharField(max_length=255)
|
||||
# Empty string = profile owner's own ranking; UUID string = a followed user's ranking
|
||||
friend_beli_user_id = fields.CharField(max_length=255, default="")
|
||||
friend_name = fields.CharField(max_length=255, null=True)
|
||||
# Beli category code: RES / DES / BAR / BAK; empty for bookmarks
|
||||
beli_category = fields.CharField(max_length=10, default="")
|
||||
name = fields.CharField(max_length=500)
|
||||
city = fields.CharField(max_length=255, null=True)
|
||||
neighborhood = fields.CharField(max_length=255, null=True)
|
||||
cuisine = fields.JSONField(default=list)
|
||||
price_range = fields.CharField(max_length=10, null=True)
|
||||
status = fields.CharEnumField(enum_type=BeliStatus, max_length=20)
|
||||
rank_index = fields.IntField(null=True)
|
||||
score = fields.DecimalField(max_digits=5, decimal_places=2, null=True)
|
||||
notes = fields.TextField(null=True)
|
||||
tags = fields.JSONField(default=list)
|
||||
fetched_at = fields.DatetimeField(auto_now=True)
|
||||
created_at = fields.DatetimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
table = "beli_restaurants"
|
||||
unique_together = (
|
||||
(
|
||||
"profile",
|
||||
"beli_business_id",
|
||||
"status",
|
||||
"friend_beli_user_id",
|
||||
"beli_category",
|
||||
),
|
||||
)
|
||||
@@ -1,5 +1,9 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
import uuid
|
||||
|
||||
from quart import Blueprint, jsonify, make_response, request
|
||||
@@ -9,69 +13,116 @@ from quart_jwt_extended import (
|
||||
)
|
||||
|
||||
import blueprints.users.models
|
||||
from utils.image_process import analyze_user_image
|
||||
from metrics import count_result, observe_response, time_response
|
||||
from utils.image_process import image_content_block
|
||||
from utils.strip_markdown import extract_agent_text
|
||||
from utils.image_upload import ImageValidationError, process_image
|
||||
from utils.s3_client import generate_presigned_url as s3_presigned_url
|
||||
from utils.s3_client import get_image as s3_get_image
|
||||
from utils.s3_client import upload_image as s3_upload_image
|
||||
|
||||
from .agents import main_agent
|
||||
from .logic import (
|
||||
add_message_to_conversation,
|
||||
get_conversation_by_id,
|
||||
get_agent_for,
|
||||
persist_agent_tool_messages,
|
||||
tool_result_text,
|
||||
)
|
||||
from .memory import get_memories_for_user
|
||||
from .memory import build_system_prompt_with_memories
|
||||
from .models import (
|
||||
Conversation,
|
||||
PydConversation,
|
||||
PydListConversation,
|
||||
Speaker,
|
||||
)
|
||||
from .prompts import SIMBA_SYSTEM_PROMPT
|
||||
|
||||
TTFT_TIMEOUT = float(os.getenv("TTFT_TIMEOUT_SECONDS", "30"))
|
||||
|
||||
_THINK_OPEN = "<think>"
|
||||
_THINK_CLOSE = "</think>"
|
||||
_THINK_RE = re.compile(r"<think>.*?</think>", re.DOTALL)
|
||||
|
||||
|
||||
def _route_content(
|
||||
buf: str, text: str, in_thinking: bool
|
||||
) -> tuple[str, str, bool, str]:
|
||||
"""Split a streaming content chunk into (thinking_text, content_text).
|
||||
|
||||
Handles tags split across chunk boundaries by keeping a small trailing
|
||||
buffer. Returns the updated in_thinking state and any leftover buffer.
|
||||
"""
|
||||
combined = buf + text
|
||||
thinking_parts: list[str] = []
|
||||
content_parts: list[str] = []
|
||||
|
||||
while combined:
|
||||
tag, hold = (
|
||||
(_THINK_CLOSE, len(_THINK_CLOSE) - 1)
|
||||
if in_thinking
|
||||
else (_THINK_OPEN, len(_THINK_OPEN) - 1)
|
||||
)
|
||||
idx = combined.find(tag)
|
||||
if idx == -1:
|
||||
# Tag not yet complete — hold back enough bytes to catch a split.
|
||||
if len(combined) <= hold:
|
||||
return (
|
||||
"".join(thinking_parts),
|
||||
"".join(content_parts),
|
||||
in_thinking,
|
||||
combined,
|
||||
)
|
||||
safe, leftover = combined[:-hold], combined[-hold:]
|
||||
(thinking_parts if in_thinking else content_parts).append(safe)
|
||||
return (
|
||||
"".join(thinking_parts),
|
||||
"".join(content_parts),
|
||||
in_thinking,
|
||||
leftover,
|
||||
)
|
||||
(thinking_parts if in_thinking else content_parts).append(combined[:idx])
|
||||
combined = combined[idx + len(tag) :]
|
||||
in_thinking = not in_thinking
|
||||
|
||||
return "".join(thinking_parts), "".join(content_parts), in_thinking, ""
|
||||
|
||||
|
||||
conversation_blueprint = Blueprint(
|
||||
"conversation_api", __name__, url_prefix="/api/conversation"
|
||||
)
|
||||
|
||||
_SYSTEM_PROMPT = SIMBA_SYSTEM_PROMPT
|
||||
# Detached persistence tasks. A strong reference keeps them from being garbage
|
||||
# collected mid-flight when the SSE stream is cancelled by a client disconnect.
|
||||
_persist_tasks: set[asyncio.Task] = set()
|
||||
|
||||
|
||||
async def _build_system_prompt_with_memories(user_id: str) -> str:
|
||||
"""Append user memories to the base system prompt."""
|
||||
memories = await get_memories_for_user(user_id)
|
||||
if not memories:
|
||||
return _SYSTEM_PROMPT
|
||||
memory_block = "\n".join(f"- {m}" for m in memories)
|
||||
return f"{_SYSTEM_PROMPT}\n\nUSER MEMORIES (facts the user has asked you to remember):\n{memory_block}"
|
||||
def _persist_in_background(coro) -> asyncio.Task:
|
||||
task = asyncio.create_task(coro)
|
||||
_persist_tasks.add(task)
|
||||
task.add_done_callback(_persist_tasks.discard)
|
||||
return task
|
||||
|
||||
|
||||
def _build_messages_payload(
|
||||
conversation,
|
||||
async def _get_owned_conversation(conversation_id, user_id) -> Conversation | None:
|
||||
"""Fetch a conversation only if it belongs to the given user."""
|
||||
return await Conversation.get_or_none(id=conversation_id, user_id=user_id)
|
||||
|
||||
|
||||
def _build_user_message(
|
||||
query_text: str,
|
||||
image_description: str | None = None,
|
||||
system_prompt: str | None = None,
|
||||
) -> list:
|
||||
recent_messages = (
|
||||
conversation.messages[-10:]
|
||||
if len(conversation.messages) > 10
|
||||
else conversation.messages
|
||||
)
|
||||
messages_payload = [{"role": "system", "content": system_prompt or _SYSTEM_PROMPT}]
|
||||
for msg in recent_messages[:-1]: # Exclude the message we just added
|
||||
role = "user" if msg.speaker == "user" else "assistant"
|
||||
text = msg.text
|
||||
if msg.image_key and role == "user":
|
||||
text = f"[User sent an image]\n{text}"
|
||||
messages_payload.append({"role": role, "content": text})
|
||||
image_block: dict | None = None,
|
||||
) -> dict:
|
||||
"""Build the single new user message for this turn.
|
||||
|
||||
# Build the current user message with optional image description
|
||||
if image_description:
|
||||
content = f"[Image analysis: {image_description}]"
|
||||
if query_text:
|
||||
content = f"{query_text}\n\n{content}"
|
||||
History is restored by the LangGraph checkpointer; only the current
|
||||
message is passed to avoid duplicating prior turns in checkpoint state.
|
||||
"""
|
||||
if image_block:
|
||||
content = [
|
||||
{"type": "text", "text": query_text or "The user sent this image."},
|
||||
image_block,
|
||||
]
|
||||
else:
|
||||
content = query_text
|
||||
messages_payload.append({"role": "user", "content": content})
|
||||
return messages_payload
|
||||
return {"role": "user", "content": content}
|
||||
|
||||
|
||||
@conversation_blueprint.post("/query")
|
||||
@@ -82,8 +133,9 @@ async def query():
|
||||
data = await request.get_json()
|
||||
query = data.get("query")
|
||||
conversation_id = data.get("conversation_id")
|
||||
conversation = await get_conversation_by_id(conversation_id)
|
||||
await conversation.fetch_related("messages")
|
||||
conversation = await _get_owned_conversation(conversation_id, current_user_uuid)
|
||||
if conversation is None:
|
||||
return jsonify({"error": "conversation not found"}), 404
|
||||
await add_message_to_conversation(
|
||||
conversation=conversation,
|
||||
message=query,
|
||||
@@ -91,24 +143,30 @@ async def query():
|
||||
user=user,
|
||||
)
|
||||
|
||||
system_prompt = await _build_system_prompt_with_memories(str(user.id))
|
||||
messages_payload = _build_messages_payload(
|
||||
conversation, query, system_prompt=system_prompt
|
||||
)
|
||||
payload = {"messages": messages_payload}
|
||||
agent_config = {
|
||||
"configurable": {"user_id": str(user.id), "is_admin": user.is_admin()}
|
||||
}
|
||||
with time_response(stream=False):
|
||||
system_prompt = await build_system_prompt_with_memories(str(user.id))
|
||||
payload = {"messages": [_build_user_message(query)]}
|
||||
agent_config = {
|
||||
"configurable": {
|
||||
"user_id": str(user.id),
|
||||
"is_admin": user.is_admin(),
|
||||
"thread_id": str(conversation.id),
|
||||
"system_prompt": system_prompt,
|
||||
}
|
||||
}
|
||||
|
||||
response = await main_agent.ainvoke(payload, config=agent_config)
|
||||
message = response.get("messages", [])[-1].content
|
||||
await add_message_to_conversation(
|
||||
conversation=conversation,
|
||||
message=message,
|
||||
speaker="simba",
|
||||
user=user,
|
||||
)
|
||||
return jsonify({"response": message})
|
||||
response = await get_agent_for(user).ainvoke(payload, config=agent_config)
|
||||
message = extract_agent_text(response.get("messages", [])[-1].content)
|
||||
await persist_agent_tool_messages(
|
||||
conversation, user, response.get("messages", [])
|
||||
)
|
||||
await add_message_to_conversation(
|
||||
conversation=conversation,
|
||||
message=message,
|
||||
speaker="simba",
|
||||
user=user,
|
||||
)
|
||||
return jsonify({"response": message})
|
||||
|
||||
|
||||
@conversation_blueprint.post("/upload-image")
|
||||
@@ -159,8 +217,9 @@ async def stream_query():
|
||||
query_text = data.get("query")
|
||||
conversation_id = data.get("conversation_id")
|
||||
image_key = data.get("image_key")
|
||||
conversation = await get_conversation_by_id(conversation_id)
|
||||
await conversation.fetch_related("messages")
|
||||
conversation = await _get_owned_conversation(conversation_id, current_user_uuid)
|
||||
if conversation is None:
|
||||
return jsonify({"error": "conversation not found"}), 404
|
||||
await add_message_to_conversation(
|
||||
conversation=conversation,
|
||||
message=query_text or "",
|
||||
@@ -169,39 +228,175 @@ async def stream_query():
|
||||
image_key=image_key,
|
||||
)
|
||||
|
||||
# If an image was uploaded, analyze it with the vision model
|
||||
image_description = None
|
||||
# If an image was uploaded, attach it to the agent request directly
|
||||
image_block = None
|
||||
if image_key:
|
||||
try:
|
||||
image_bytes, _ = await s3_get_image(image_key)
|
||||
image_description = await analyze_user_image(image_bytes)
|
||||
logging.info(f"Image analysis complete for {image_key}")
|
||||
image_block = image_content_block(image_bytes)
|
||||
except Exception as e:
|
||||
logging.error(f"Failed to analyze image: {e}")
|
||||
image_description = "[Image could not be analyzed]"
|
||||
logging.error(f"Failed to load image {image_key}: {e}")
|
||||
query_text = f"{query_text or ''}\n\n[An image was sent but could not be loaded.]".strip()
|
||||
|
||||
system_prompt = await _build_system_prompt_with_memories(str(user.id))
|
||||
messages_payload = _build_messages_payload(
|
||||
conversation, query_text or "", image_description, system_prompt=system_prompt
|
||||
)
|
||||
payload = {"messages": messages_payload}
|
||||
system_prompt = await build_system_prompt_with_memories(str(user.id))
|
||||
payload = {"messages": [_build_user_message(query_text or "", image_block)]}
|
||||
agent_config = {
|
||||
"configurable": {"user_id": str(user.id), "is_admin": user.is_admin()}
|
||||
"configurable": {
|
||||
"user_id": str(user.id),
|
||||
"is_admin": user.is_admin(),
|
||||
"thread_id": str(conversation.id),
|
||||
"system_prompt": system_prompt,
|
||||
}
|
||||
}
|
||||
# Capture the agent reference while the request/app context is still active.
|
||||
# Quart exits the app context before iterating the SSE response body, so
|
||||
# current_app cannot be accessed inside event_generator().
|
||||
main_agent = get_agent_for(user)
|
||||
|
||||
async def event_generator():
|
||||
final_message = None
|
||||
streamed = ""
|
||||
# Tag-routing state for <think>…</think> blocks.
|
||||
in_thinking = False
|
||||
tag_buf = ""
|
||||
# Fallback heartbeat for models that emit empty chunks before thinking text.
|
||||
last_thinking = 0.0
|
||||
turn_has_content = False
|
||||
|
||||
# Timing / observability
|
||||
t0 = time.monotonic()
|
||||
llm_start_at: float | None = None
|
||||
first_content_at: float | None = None
|
||||
tool_timers: dict[str, float] = {} # run_id -> start time
|
||||
tool_spans: list[dict] = []
|
||||
n_tokens = 0
|
||||
model_name: str | None = None
|
||||
cancelled = False
|
||||
|
||||
async def persist(text: str) -> None:
|
||||
await add_message_to_conversation(
|
||||
conversation=conversation,
|
||||
message=text,
|
||||
speaker="simba",
|
||||
user=user,
|
||||
)
|
||||
|
||||
def _log_summary(*, error: str | None = None) -> None:
|
||||
now = time.monotonic()
|
||||
thinking_ms = (
|
||||
round((first_content_at - llm_start_at) * 1000)
|
||||
if llm_start_at and first_content_at
|
||||
else None
|
||||
)
|
||||
ttft_ms = (
|
||||
round((first_content_at - t0) * 1000) if first_content_at else None
|
||||
)
|
||||
logging.info(
|
||||
json.dumps(
|
||||
{
|
||||
"event": "stream_request",
|
||||
"conv": str(conversation.id),
|
||||
"user": user.username,
|
||||
"total_ms": round((now - t0) * 1000),
|
||||
"thinking_ms": thinking_ms,
|
||||
"ttft_ms": ttft_ms,
|
||||
"tokens": n_tokens,
|
||||
"tools": tool_spans,
|
||||
"model": model_name,
|
||||
"cancelled": cancelled,
|
||||
**({"error": error} if error else {}),
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
async for event in main_agent.astream_events(
|
||||
aiter = main_agent.astream_events(
|
||||
payload, version="v2", config=agent_config
|
||||
):
|
||||
).__aiter__()
|
||||
while True:
|
||||
# Apply a short TTFT timeout only during the window between
|
||||
# on_chat_model_start and the first content token. Before the
|
||||
# LLM call starts (tool phase) and after the first token, let
|
||||
# the httpx read timeout handle stalls instead.
|
||||
if llm_start_at is not None and first_content_at is None:
|
||||
ttft_wait = TTFT_TIMEOUT - (time.monotonic() - llm_start_at)
|
||||
timeout = max(ttft_wait, 1.0)
|
||||
else:
|
||||
timeout = None
|
||||
|
||||
try:
|
||||
if timeout is not None:
|
||||
event = await asyncio.wait_for(
|
||||
aiter.__anext__(), timeout=timeout
|
||||
)
|
||||
else:
|
||||
event = await aiter.__anext__()
|
||||
except StopAsyncIteration:
|
||||
break
|
||||
except asyncio.TimeoutError:
|
||||
_log_summary(error=f"TTFT timeout after {TTFT_TIMEOUT:.0f}s")
|
||||
yield f"data: {json.dumps({'type': 'error', 'message': 'Simba took too long to respond. Please try again.', 'stalled': True})}\n\n"
|
||||
return
|
||||
|
||||
event_type = event.get("event")
|
||||
|
||||
if event_type == "on_tool_start":
|
||||
tool_timers[event["run_id"]] = time.monotonic()
|
||||
yield f"data: {json.dumps({'type': 'tool_start', 'tool': event['name']})}\n\n"
|
||||
|
||||
elif event_type == "on_tool_end":
|
||||
start = tool_timers.pop(event["run_id"], None)
|
||||
tool_spans.append(
|
||||
{
|
||||
"tool": event["name"],
|
||||
"ms": round((time.monotonic() - start) * 1000)
|
||||
if start
|
||||
else None,
|
||||
}
|
||||
)
|
||||
yield f"data: {json.dumps({'type': 'tool_end', 'tool': event['name']})}\n\n"
|
||||
# Persist tool output so later turns keep the fetched context
|
||||
output = event.get("data", {}).get("output")
|
||||
if output is not None:
|
||||
await add_message_to_conversation(
|
||||
conversation=conversation,
|
||||
message=tool_result_text(event["name"], output),
|
||||
speaker="tool",
|
||||
user=user,
|
||||
)
|
||||
|
||||
elif event_type == "on_chat_model_start":
|
||||
turn_has_content = False
|
||||
if llm_start_at is None:
|
||||
llm_start_at = time.monotonic()
|
||||
if model_name is None:
|
||||
model_name = event.get("metadata", {}).get(
|
||||
"ls_model_name"
|
||||
) or event.get("name")
|
||||
|
||||
elif event_type == "on_chat_model_stream":
|
||||
chunk = event.get("data", {}).get("chunk")
|
||||
raw = getattr(chunk, "content", None)
|
||||
if isinstance(raw, str) and raw:
|
||||
think_text, content_text, in_thinking, tag_buf = _route_content(
|
||||
tag_buf, raw, in_thinking
|
||||
)
|
||||
if think_text:
|
||||
yield f"data: {json.dumps({'type': 'thinking_delta', 'delta': think_text})}\n\n"
|
||||
if content_text:
|
||||
if first_content_at is None:
|
||||
first_content_at = time.monotonic()
|
||||
turn_has_content = True
|
||||
n_tokens += 1
|
||||
streamed += content_text
|
||||
yield f"data: {json.dumps({'type': 'content', 'delta': content_text})}\n\n"
|
||||
elif not turn_has_content:
|
||||
# Empty chunk before any content — heartbeat for models
|
||||
# that don't emit <think> tags at all.
|
||||
now = time.monotonic()
|
||||
if now - last_thinking >= 1.0:
|
||||
last_thinking = now
|
||||
yield f"data: {json.dumps({'type': 'thinking'})}\n\n"
|
||||
|
||||
elif event_type == "on_chain_end":
|
||||
output = event.get("data", {}).get("output")
|
||||
@@ -211,22 +406,34 @@ async def stream_query():
|
||||
last_msg = msgs[-1]
|
||||
content = getattr(last_msg, "content", None)
|
||||
if isinstance(content, str) and content:
|
||||
final_message = content
|
||||
final_message = _THINK_RE.sub("", content).strip()
|
||||
|
||||
except asyncio.CancelledError:
|
||||
cancelled = True
|
||||
_log_summary()
|
||||
text = final_message or streamed
|
||||
if text:
|
||||
_persist_in_background(persist(text))
|
||||
raise
|
||||
except Exception as e:
|
||||
count_result(stream=True, result="error")
|
||||
_log_summary(error=str(e))
|
||||
yield f"data: {json.dumps({'type': 'error', 'message': str(e)})}\n\n"
|
||||
return
|
||||
|
||||
final_message = final_message or streamed
|
||||
if final_message:
|
||||
await add_message_to_conversation(
|
||||
conversation=conversation,
|
||||
message=final_message,
|
||||
speaker="simba",
|
||||
user=user,
|
||||
)
|
||||
# Persist via a detached, shielded task so a disconnect at this
|
||||
# point (after generation completes) still commits the answer.
|
||||
task = _persist_in_background(persist(final_message))
|
||||
await asyncio.shield(task)
|
||||
yield f"data: {json.dumps({'type': 'response', 'message': final_message})}\n\n"
|
||||
else:
|
||||
yield f"data: {json.dumps({'type': 'error', 'message': 'No response generated'})}\n\n"
|
||||
|
||||
observe_response(stream=True, seconds=time.monotonic() - t0)
|
||||
count_result(stream=True, result="success")
|
||||
_log_summary()
|
||||
yield "data: [DONE]\n\n"
|
||||
|
||||
return await make_response(
|
||||
@@ -243,16 +450,22 @@ async def stream_query():
|
||||
@conversation_blueprint.route("/<conversation_id>")
|
||||
@jwt_refresh_token_required
|
||||
async def get_conversation(conversation_id: str):
|
||||
conversation = await Conversation.get(id=conversation_id)
|
||||
user_uuid = get_jwt_identity()
|
||||
conversation = await _get_owned_conversation(conversation_id, user_uuid)
|
||||
if conversation is None:
|
||||
return jsonify({"error": "conversation not found"}), 404
|
||||
await conversation.fetch_related("messages")
|
||||
|
||||
# Manually serialize the conversation with messages
|
||||
messages = []
|
||||
for msg in conversation.messages:
|
||||
# Tool messages hold raw fetched context for the LLM; only surface
|
||||
# their header line (e.g. "[web_search result]") in the UI.
|
||||
text = msg.text.split("\n", 1)[0] if msg.speaker == Speaker.TOOL else msg.text
|
||||
messages.append(
|
||||
{
|
||||
"id": str(msg.id),
|
||||
"text": msg.text,
|
||||
"text": text,
|
||||
"speaker": msg.speaker.value,
|
||||
"created_at": msg.created_at.isoformat(),
|
||||
"image_key": msg.image_key,
|
||||
@@ -288,7 +501,54 @@ async def create_conversation():
|
||||
async def get_all_conversations():
|
||||
user_uuid = get_jwt_identity()
|
||||
user = await blueprints.users.models.User.get(id=user_uuid)
|
||||
conversations = Conversation.filter(user=user).order_by("-updated_at")
|
||||
serialized_conversations = await PydListConversation.from_queryset(conversations)
|
||||
|
||||
query = Conversation.filter(user=user)
|
||||
|
||||
search = request.args.get("search", "").strip()
|
||||
if search:
|
||||
query = query.filter(name__icontains=search)
|
||||
|
||||
query = query.order_by("-updated_at")
|
||||
|
||||
# `limit` is optional: when omitted we return the full list (backward
|
||||
# compatible with any non-frontend callers). `offset` only applies with it.
|
||||
limit = request.args.get("limit", type=int)
|
||||
if limit is not None:
|
||||
offset = request.args.get("offset", default=0, type=int)
|
||||
query = query.offset(offset).limit(limit)
|
||||
|
||||
serialized_conversations = await PydListConversation.from_queryset(query)
|
||||
|
||||
return jsonify(serialized_conversations.model_dump())
|
||||
|
||||
|
||||
@conversation_blueprint.patch("/<conversation_id>")
|
||||
@jwt_refresh_token_required
|
||||
async def rename_conversation(conversation_id: str):
|
||||
user_uuid = get_jwt_identity()
|
||||
body = await request.get_json()
|
||||
name = (body or {}).get("name", "").strip()
|
||||
if not name:
|
||||
return jsonify({"error": "name is required"}), 400
|
||||
|
||||
conversation = await _get_owned_conversation(conversation_id, user_uuid)
|
||||
if conversation is None:
|
||||
return jsonify({"error": "conversation not found"}), 404
|
||||
|
||||
conversation.name = name[:255]
|
||||
await conversation.save()
|
||||
|
||||
serialized_conversation = await PydConversation.from_tortoise_orm(conversation)
|
||||
return jsonify(serialized_conversation.model_dump())
|
||||
|
||||
|
||||
@conversation_blueprint.delete("/<conversation_id>")
|
||||
@jwt_refresh_token_required
|
||||
async def delete_conversation(conversation_id: str):
|
||||
user_uuid = get_jwt_identity()
|
||||
conversation = await _get_owned_conversation(conversation_id, user_uuid)
|
||||
if conversation is None:
|
||||
return jsonify({"error": "conversation not found"}), 404
|
||||
|
||||
await conversation.delete()
|
||||
return jsonify({"success": True})
|
||||
|
||||
+437
-339
File diff suppressed because it is too large
Load Diff
@@ -1,9 +1,55 @@
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import tortoise.exceptions
|
||||
from langchain_core.messages import ToolMessage
|
||||
from quart import current_app
|
||||
|
||||
import blueprints.users.models
|
||||
|
||||
from .models import Conversation, ConversationMessage
|
||||
|
||||
# Cap persisted tool output so replayed history stays within the LLM context
|
||||
# budget (llama-server slots are 32k tokens; base prompt + tools already use ~6k).
|
||||
TOOL_RESULT_MAX_CHARS = 4000
|
||||
|
||||
|
||||
def tool_result_text(tool_name: str, output) -> str:
|
||||
"""Format a tool's output for persistence as a conversation message."""
|
||||
content = getattr(output, "content", output)
|
||||
if isinstance(content, (list, tuple)):
|
||||
content = "\n".join(str(item) for item in content)
|
||||
text = str(content)
|
||||
if len(text) > TOOL_RESULT_MAX_CHARS:
|
||||
text = text[:TOOL_RESULT_MAX_CHARS] + "\n[truncated]"
|
||||
return f"[{tool_name} result]\n{text}"
|
||||
|
||||
|
||||
def get_agent_for(user: blueprints.users.models.User):
|
||||
"""Admins get the agent with admin-only tools (Homebox, BookStack); everyone else the main one."""
|
||||
if user.is_admin():
|
||||
return getattr(current_app, "admin_agent", current_app.main_agent)
|
||||
return current_app.main_agent
|
||||
|
||||
|
||||
async def persist_agent_tool_messages(
|
||||
conversation: Conversation,
|
||||
user: blueprints.users.models.User,
|
||||
response_messages: list,
|
||||
) -> None:
|
||||
"""Persist ToolMessages from an agent response as 'tool' conversation messages.
|
||||
|
||||
Keeps fetched context (web searches, RAG chunks, etc.) available to later
|
||||
turns, which otherwise only see the final text answers.
|
||||
"""
|
||||
for msg in response_messages:
|
||||
if isinstance(msg, ToolMessage):
|
||||
await add_message_to_conversation(
|
||||
conversation=conversation,
|
||||
message=tool_result_text(msg.name or "tool", msg),
|
||||
speaker="tool",
|
||||
user=user,
|
||||
)
|
||||
|
||||
|
||||
async def create_conversation(name: str = "") -> Conversation:
|
||||
conversation = await Conversation.create(name=name)
|
||||
@@ -16,19 +62,26 @@ async def add_message_to_conversation(
|
||||
speaker: str,
|
||||
user: blueprints.users.models.User,
|
||||
image_key: str | None = None,
|
||||
message_handle: str | None = None,
|
||||
) -> ConversationMessage:
|
||||
print(conversation, message, speaker)
|
||||
# Bump updated_at on every message so conversations sort by last activity.
|
||||
# `update_fields` bypasses the auto_now recompute, so set it explicitly.
|
||||
update_fields = ["updated_at"]
|
||||
conversation.updated_at = datetime.now(timezone.utc)
|
||||
|
||||
# Name the conversation after the first user message
|
||||
if speaker == "user" and not await conversation.messages.all().exists():
|
||||
conversation.name = message[:100]
|
||||
await conversation.save()
|
||||
update_fields.append("name")
|
||||
|
||||
await conversation.save(update_fields=update_fields)
|
||||
|
||||
message = await ConversationMessage.create(
|
||||
text=message,
|
||||
speaker=speaker,
|
||||
conversation=conversation,
|
||||
image_key=image_key,
|
||||
message_handle=message_handle,
|
||||
)
|
||||
|
||||
return message
|
||||
@@ -62,7 +115,9 @@ async def get_conversation_for_user(user: blueprints.users.models.User) -> Conve
|
||||
async def get_conversation_for_channel(
|
||||
user: blueprints.users.models.User, channel: str
|
||||
) -> Conversation:
|
||||
conversation = await Conversation.filter(user=user, channel=channel).first()
|
||||
conversation = await (
|
||||
Conversation.filter(user=user, channel=channel).order_by("-updated_at").first()
|
||||
)
|
||||
if conversation is None:
|
||||
conversation = await Conversation.create(
|
||||
name=f"{user.username}'s {channel} chat", user=user, channel=channel
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from .models import UserMemory
|
||||
from .prompts import SIMBA_SYSTEM_PROMPT
|
||||
|
||||
|
||||
async def get_memories_for_user(user_id: str) -> list[str]:
|
||||
@@ -7,6 +8,14 @@ async def get_memories_for_user(user_id: str) -> list[str]:
|
||||
return [m.content for m in memories]
|
||||
|
||||
|
||||
async def build_system_prompt_with_memories(user_id: str) -> str:
|
||||
memories = await get_memories_for_user(user_id)
|
||||
if not memories:
|
||||
return SIMBA_SYSTEM_PROMPT
|
||||
block = "\n".join(f"- {m}" for m in memories)
|
||||
return f"{SIMBA_SYSTEM_PROMPT}\n\nUSER MEMORIES (facts the user has asked you to remember):\n{block}"
|
||||
|
||||
|
||||
async def save_memory(user_id: str, content: str) -> str:
|
||||
"""Save a new memory or touch an existing one (exact-match dedup)."""
|
||||
existing = await UserMemory.filter(user_id=user_id, content=content).first()
|
||||
|
||||
@@ -11,6 +11,7 @@ from tortoise.models import Model
|
||||
class Speaker(enum.Enum):
|
||||
USER = "user"
|
||||
SIMBA = "simba"
|
||||
TOOL = "tool"
|
||||
|
||||
|
||||
class Conversation(Model):
|
||||
@@ -36,6 +37,7 @@ class ConversationMessage(Model):
|
||||
created_at = fields.DatetimeField(auto_now_add=True)
|
||||
speaker = fields.CharEnumField(enum_type=Speaker, max_length=10)
|
||||
image_key = fields.CharField(max_length=512, null=True, default=None)
|
||||
message_handle = fields.CharField(max_length=255, null=True, default=None)
|
||||
|
||||
class Meta:
|
||||
table = "conversation_messages"
|
||||
|
||||
@@ -49,6 +49,12 @@ You have access to Ryan's meal plans, recipes, and shopping lists through Mealie
|
||||
- Use mealie_create_meal_plan to add meals to the plan
|
||||
Always use these tools when asked about meals, recipes, what's for dinner, or shopping lists.
|
||||
|
||||
HOME INVENTORY (Homebox Integration):
|
||||
If homebox_* tools are available, use them for questions about household items: where something is stored, what's in a location, warranties, and maintenance. Search before creating items to avoid duplicates, and use homebox_set_item to edit an existing item (name, description, notes, quantity, purchase info, custom fields) instead of creating a new one.
|
||||
|
||||
WIKI (BookStack Integration):
|
||||
If bookstack_* tools are available, use them to look up and write household documentation in the BookStack wiki. Search before creating books, chapters, or pages to avoid duplicates, and prefer updating an existing page over creating a near-copy. Write page content as markdown.
|
||||
|
||||
NOTES & RESEARCH (Obsidian Integration):
|
||||
You have access to Ryan's Obsidian vault through the Obsidian integration. When users ask about research, personal notes, or information that might be stored in markdown files, use the appropriate Obsidian tools:
|
||||
- Use obsidian_search_notes to search through your vault for relevant information
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
import hmac
|
||||
import logging
|
||||
import os
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from quart import Blueprint, current_app, jsonify, request
|
||||
|
||||
from blueprints.users.decorators import admin_required
|
||||
from blueprints.scheduled_messages.models import MessageChannel
|
||||
from .models import CronJob
|
||||
|
||||
cron_blueprint = Blueprint("cron_api", __name__, url_prefix="/api/cron")
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _serialize(job: CronJob) -> dict:
|
||||
return {
|
||||
"id": str(job.id),
|
||||
"name": job.name,
|
||||
"description": job.description,
|
||||
"prompt": job.prompt,
|
||||
"recipient": job.recipient,
|
||||
"channel": job.channel.value,
|
||||
"enabled": job.enabled,
|
||||
"last_run_at": job.last_run_at.isoformat() if job.last_run_at else None,
|
||||
"last_status": job.last_status,
|
||||
"last_error": job.last_error,
|
||||
"created_at": job.created_at.isoformat(),
|
||||
"updated_at": job.updated_at.isoformat(),
|
||||
}
|
||||
|
||||
|
||||
def _check_cron_secret() -> bool:
|
||||
expected = os.environ.get("CRON_SECRET", "")
|
||||
if not expected:
|
||||
return False
|
||||
provided = request.headers.get("X-Cron-Secret", "")
|
||||
return hmac.compare_digest(provided.encode(), expected.encode())
|
||||
|
||||
|
||||
@cron_blueprint.route("/", methods=["GET"])
|
||||
@admin_required
|
||||
async def list_jobs():
|
||||
jobs = await CronJob.all().order_by("name")
|
||||
return jsonify([_serialize(j) for j in jobs])
|
||||
|
||||
|
||||
@cron_blueprint.route("/", methods=["POST"])
|
||||
@admin_required
|
||||
async def create_job():
|
||||
data = await request.get_json()
|
||||
if not data:
|
||||
return jsonify({"error": "Invalid payload"}), 400
|
||||
|
||||
name = (data.get("name") or "").strip()
|
||||
prompt = (data.get("prompt") or "").strip()
|
||||
recipient = (data.get("recipient") or "").strip()
|
||||
channel_str = data.get("channel")
|
||||
description = (data.get("description") or "").strip() or None
|
||||
|
||||
if not name or not prompt or not recipient or not channel_str:
|
||||
return jsonify(
|
||||
{"error": "name, prompt, recipient, and channel are required"}
|
||||
), 400
|
||||
|
||||
try:
|
||||
channel = MessageChannel(channel_str)
|
||||
except ValueError:
|
||||
return jsonify(
|
||||
{"error": f"Invalid channel: {channel_str}. Must be 'imessage' or 'email'"}
|
||||
), 400
|
||||
|
||||
job = await CronJob.create(
|
||||
name=name,
|
||||
description=description,
|
||||
prompt=prompt,
|
||||
recipient=recipient,
|
||||
channel=channel,
|
||||
enabled=bool(data.get("enabled", True)),
|
||||
)
|
||||
return jsonify(_serialize(job)), 201
|
||||
|
||||
|
||||
@cron_blueprint.route("/<job_id>", methods=["PUT"])
|
||||
@admin_required
|
||||
async def update_job(job_id: str):
|
||||
job = await CronJob.get_or_none(id=job_id)
|
||||
if not job:
|
||||
return jsonify({"error": "Not found"}), 404
|
||||
|
||||
data = await request.get_json()
|
||||
if not data:
|
||||
return jsonify({"error": "Invalid payload"}), 400
|
||||
|
||||
if "name" in data:
|
||||
job.name = data["name"].strip()
|
||||
if "description" in data:
|
||||
job.description = (data["description"] or "").strip() or None
|
||||
if "prompt" in data:
|
||||
job.prompt = data["prompt"].strip()
|
||||
if "recipient" in data:
|
||||
job.recipient = data["recipient"].strip()
|
||||
if "channel" in data:
|
||||
try:
|
||||
job.channel = MessageChannel(data["channel"])
|
||||
except ValueError:
|
||||
return jsonify({"error": f"Invalid channel: {data['channel']}"}), 400
|
||||
if "enabled" in data:
|
||||
job.enabled = bool(data["enabled"])
|
||||
|
||||
await job.save()
|
||||
return jsonify(_serialize(job))
|
||||
|
||||
|
||||
@cron_blueprint.route("/<job_id>", methods=["DELETE"])
|
||||
@admin_required
|
||||
async def delete_job(job_id: str):
|
||||
job = await CronJob.get_or_none(id=job_id)
|
||||
if not job:
|
||||
return jsonify({"error": "Not found"}), 404
|
||||
|
||||
await job.delete()
|
||||
return jsonify({"status": "deleted"})
|
||||
|
||||
|
||||
@cron_blueprint.route("/<job_id>/trigger", methods=["POST"])
|
||||
async def trigger_job(job_id: str):
|
||||
if not _check_cron_secret():
|
||||
return jsonify({"error": "Unauthorized"}), 401
|
||||
|
||||
job = await CronJob.get_or_none(id=job_id)
|
||||
if not job:
|
||||
return jsonify({"error": "Not found"}), 404
|
||||
|
||||
if not job.enabled:
|
||||
return jsonify({"skipped": True}), 200
|
||||
|
||||
try:
|
||||
import uuid
|
||||
|
||||
response = await current_app.main_agent.ainvoke(
|
||||
{"messages": [{"role": "user", "content": job.prompt}]},
|
||||
config={"configurable": {"thread_id": f"cron-{uuid.uuid4()}"}},
|
||||
)
|
||||
result = response.get("messages", [])[-1].content
|
||||
|
||||
if job.channel == MessageChannel.IMESSAGE:
|
||||
from blueprints.imessage import send_imessage
|
||||
from utils.strip_markdown import strip_markdown
|
||||
|
||||
await send_imessage(job.recipient, strip_markdown(result))
|
||||
|
||||
elif job.channel == MessageChannel.EMAIL:
|
||||
from blueprints.email import send_email_reply
|
||||
|
||||
await send_email_reply(
|
||||
to=job.recipient,
|
||||
subject=job.name,
|
||||
body=result,
|
||||
)
|
||||
|
||||
job.last_run_at = datetime.now(timezone.utc)
|
||||
job.last_status = "success"
|
||||
job.last_error = None
|
||||
await job.save()
|
||||
logger.info(f"Cron job {job.id} ({job.name}) executed successfully")
|
||||
return jsonify({"ok": True})
|
||||
|
||||
except Exception as e:
|
||||
job.last_run_at = datetime.now(timezone.utc)
|
||||
job.last_status = "failed"
|
||||
job.last_error = str(e)
|
||||
await job.save()
|
||||
logger.error(f"Cron job {job.id} ({job.name}) failed: {e}")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
@@ -0,0 +1,22 @@
|
||||
from tortoise import fields
|
||||
from tortoise.models import Model
|
||||
|
||||
from blueprints.scheduled_messages.models import MessageChannel
|
||||
|
||||
|
||||
class CronJob(Model):
|
||||
id = fields.UUIDField(primary_key=True)
|
||||
name = fields.CharField(max_length=255)
|
||||
description = fields.TextField(null=True)
|
||||
prompt = fields.TextField()
|
||||
recipient = fields.CharField(max_length=255)
|
||||
channel = fields.CharEnumField(enum_type=MessageChannel, max_length=20)
|
||||
enabled = fields.BooleanField(default=True)
|
||||
last_run_at = fields.DatetimeField(null=True)
|
||||
last_status = fields.CharField(max_length=50, null=True)
|
||||
last_error = fields.TextField(null=True)
|
||||
created_at = fields.DatetimeField(auto_now_add=True)
|
||||
updated_at = fields.DatetimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
table = "cron_jobs"
|
||||
@@ -7,14 +7,15 @@ import time
|
||||
from collections import defaultdict
|
||||
|
||||
import httpx
|
||||
from quart import Blueprint, request
|
||||
from quart import Blueprint, current_app, request
|
||||
|
||||
from blueprints.users.models import User
|
||||
from blueprints.conversation.logic import (
|
||||
get_conversation_for_channel,
|
||||
add_message_to_conversation,
|
||||
persist_agent_tool_messages,
|
||||
)
|
||||
from blueprints.conversation.agents import main_agent
|
||||
from blueprints.conversation.models import Speaker
|
||||
from blueprints.conversation.prompts import SIMBA_SYSTEM_PROMPT
|
||||
from . import models # noqa: F401 — register Tortoise ORM models
|
||||
from .helpers import generate_email_token, get_user_email_address # noqa: F401
|
||||
@@ -193,17 +194,20 @@ async def webhook():
|
||||
# Build messages payload
|
||||
try:
|
||||
messages = await conversation.messages.all()
|
||||
recent_messages = list(messages)[-10:]
|
||||
recent_messages = list(messages)[-20:]
|
||||
|
||||
messages_payload = [{"role": "system", "content": SIMBA_SYSTEM_PROMPT}]
|
||||
for msg in recent_messages[:-1]:
|
||||
role = "user" if msg.speaker == "user" else "assistant"
|
||||
role = "user" if msg.speaker == Speaker.USER else "assistant"
|
||||
messages_payload.append({"role": role, "content": msg.text})
|
||||
messages_payload.append({"role": "user", "content": body})
|
||||
|
||||
logger.info(f"Invoking LangChain agent with {len(messages_payload)} messages")
|
||||
response = await main_agent.ainvoke({"messages": messages_payload})
|
||||
response = await current_app.main_agent.ainvoke({"messages": messages_payload})
|
||||
response_text = response.get("messages", [])[-1].content
|
||||
await persist_agent_tool_messages(
|
||||
conversation, user, response.get("messages", [])
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Error invoking agent for email: {e}")
|
||||
response_text = "Sorry, I'm having trouble thinking right now."
|
||||
|
||||
@@ -4,11 +4,27 @@ Encryption service for email credentials.
|
||||
Provides transparent Fernet encryption for sensitive fields in the database.
|
||||
"""
|
||||
|
||||
import base64
|
||||
import os
|
||||
from cryptography.fernet import Fernet
|
||||
from tortoise import fields
|
||||
|
||||
|
||||
def _is_fernet_token(value: str) -> bool:
|
||||
"""Return True if value looks like a Fernet token.
|
||||
|
||||
Fernet tokens are URL-safe base64 and decode to at least 73 bytes
|
||||
(1 version + 8 timestamp + 16 IV + 16 min-ciphertext + 32 HMAC).
|
||||
The first decoded byte is always 0x80 (the Fernet version byte).
|
||||
"""
|
||||
try:
|
||||
padded = value + "=" * (-len(value) % 4)
|
||||
raw = base64.urlsafe_b64decode(padded.encode())
|
||||
return len(raw) >= 73 and raw[0] == 0x80
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
class EncryptedTextField(fields.TextField):
|
||||
"""
|
||||
Custom Tortoise ORM field that transparently encrypts/decrypts text values.
|
||||
@@ -38,10 +54,16 @@ class EncryptedTextField(fields.TextField):
|
||||
return self.fernet.encrypt(value.encode()).decode()
|
||||
|
||||
def to_python_value(self, value: str) -> str:
|
||||
"""Decrypt value when loading from database."""
|
||||
"""Decrypt value when loading from database.
|
||||
|
||||
Plaintext values (e.g. freshly-set before the first DB round-trip) are
|
||||
returned as-is. Values that look like Fernet tokens are decrypted — a
|
||||
wrong key still raises InvalidToken, preserving the security guarantee.
|
||||
"""
|
||||
if value is None:
|
||||
return None
|
||||
# Decrypt Fernet token
|
||||
if not _is_fernet_token(value):
|
||||
return value
|
||||
return self.fernet.decrypt(value.encode()).decode()
|
||||
|
||||
|
||||
|
||||
+198
-40
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
import os
|
||||
import hmac
|
||||
import logging
|
||||
@@ -10,11 +11,14 @@ from quart import Blueprint, request, jsonify
|
||||
|
||||
from blueprints.users.models import User
|
||||
from blueprints.conversation.logic import (
|
||||
get_agent_for,
|
||||
get_conversation_for_channel,
|
||||
add_message_to_conversation,
|
||||
persist_agent_tool_messages,
|
||||
)
|
||||
from blueprints.conversation.agents import main_agent
|
||||
from blueprints.conversation.prompts import SIMBA_SYSTEM_PROMPT
|
||||
from blueprints.conversation.models import Conversation, ConversationMessage
|
||||
from blueprints.conversation.memory import build_system_prompt_with_memories
|
||||
from utils.strip_markdown import extract_agent_text, strip_markdown
|
||||
|
||||
imessage_blueprint = Blueprint("imessage_api", __name__, url_prefix="/api/imessage")
|
||||
|
||||
@@ -57,25 +61,60 @@ def _check_rate_limit(phone_number: str) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
async def send_imessage(to_number: str, content: str) -> dict:
|
||||
async def send_imessage(
|
||||
to_number: str,
|
||||
content: str,
|
||||
reply_to_handle: str | None = None,
|
||||
) -> dict:
|
||||
"""Send an iMessage via SendBlue API."""
|
||||
from_number = os.getenv("SENDBLUE_FROM_NUMBER", "")
|
||||
|
||||
payload: dict = {
|
||||
"number": to_number,
|
||||
"from_number": from_number,
|
||||
"content": content,
|
||||
}
|
||||
if reply_to_handle:
|
||||
payload["reply_to"] = {"message_handle": reply_to_handle}
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.post(
|
||||
f"{SENDBLUE_API_BASE}/api/send-message",
|
||||
headers=_get_sendblue_headers(),
|
||||
json={
|
||||
"number": to_number,
|
||||
"from_number": from_number,
|
||||
"content": content,
|
||||
},
|
||||
json=payload,
|
||||
timeout=30,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
|
||||
async def send_typing_indicator(
|
||||
to_number: str,
|
||||
state: str = "start",
|
||||
max_duration_ms: int | None = None,
|
||||
) -> None:
|
||||
"""Send or stop a typing indicator. Errors are logged and swallowed."""
|
||||
from_number = os.getenv("SENDBLUE_FROM_NUMBER", "")
|
||||
payload: dict = {
|
||||
"number": to_number,
|
||||
"from_number": from_number,
|
||||
"state": state,
|
||||
}
|
||||
if max_duration_ms is not None:
|
||||
payload["max_duration_ms"] = max_duration_ms
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
await client.post(
|
||||
f"{SENDBLUE_API_BASE}/api/send-typing-indicator",
|
||||
headers=_get_sendblue_headers(),
|
||||
json=payload,
|
||||
timeout=10,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to send typing indicator ({state}): {e}")
|
||||
|
||||
|
||||
def validate_sendblue_signature(f):
|
||||
"""Decorator to validate the SendBlue webhook signing secret."""
|
||||
|
||||
@@ -108,20 +147,30 @@ async def webhook():
|
||||
return jsonify({"error": "Invalid payload"}), 400
|
||||
|
||||
from_number = data.get("from_number")
|
||||
content = data.get("content")
|
||||
content = data.get("content") or ""
|
||||
media_url = data.get("media_url")
|
||||
is_outbound = data.get("is_outbound", False)
|
||||
|
||||
# Threading fields from SendBlue
|
||||
inbound_handle = data.get("message_handle")
|
||||
reply_to_handle = (data.get("reply_to") or {}).get("message_handle")
|
||||
originator_handle = (data.get("thread_originator") or {}).get("message_handle")
|
||||
|
||||
# Ignore outbound messages (our own replies echoed back)
|
||||
if is_outbound:
|
||||
return jsonify({"status": "ignored"}), 200
|
||||
|
||||
if not from_number or not content:
|
||||
return jsonify({"error": "Missing from_number or content"}), 400
|
||||
if not from_number:
|
||||
return jsonify({"error": "Missing from_number"}), 400
|
||||
|
||||
if not content and not media_url:
|
||||
return jsonify({"error": "Missing content and media_url"}), 400
|
||||
|
||||
content = content.strip()
|
||||
if not content:
|
||||
if not content and not media_url:
|
||||
await send_imessage(
|
||||
from_number, "I received an empty message. Please send some text!"
|
||||
from_number,
|
||||
"I received an empty message. Please send some text or an image!",
|
||||
)
|
||||
return jsonify({"status": "ok"}), 200
|
||||
|
||||
@@ -176,56 +225,165 @@ async def webhook():
|
||||
await send_imessage(from_number, "Sorry, this feature is restricted to admins.")
|
||||
return jsonify({"status": "forbidden"}), 200
|
||||
|
||||
# Get or create conversation
|
||||
# /threads command — list recent iMessage conversations
|
||||
if content.strip().lower() == "/threads":
|
||||
threads = (
|
||||
await Conversation.filter(user=user, channel="imessage")
|
||||
.order_by("-updated_at")
|
||||
.limit(5)
|
||||
)
|
||||
if not threads:
|
||||
await send_imessage(from_number, "No threads found.")
|
||||
else:
|
||||
lines = ["Your recent threads (most recent first):"]
|
||||
for i, t in enumerate(threads, 1):
|
||||
updated = t.updated_at.strftime("%b %-d")
|
||||
lines.append(f"{i}. {t.name} ({updated})")
|
||||
lines.append("\nReply to any message to continue its thread.")
|
||||
await send_imessage(from_number, "\n".join(lines))
|
||||
return jsonify({"status": "ok"}), 200
|
||||
|
||||
# Determine conversation based on iMessage reply threading:
|
||||
# - Reply to an existing message → continue that conversation
|
||||
# - New standalone message → start a fresh conversation
|
||||
try:
|
||||
conversation = await get_conversation_for_channel(user=user, channel="imessage")
|
||||
await conversation.fetch_related("messages")
|
||||
if reply_to_handle:
|
||||
lookup_handle = originator_handle or reply_to_handle
|
||||
parent_msg = await ConversationMessage.get_or_none(
|
||||
message_handle=lookup_handle,
|
||||
conversation__user=user,
|
||||
conversation__channel="imessage",
|
||||
)
|
||||
if parent_msg:
|
||||
conversation = await Conversation.get(id=parent_msg.conversation_id)
|
||||
logger.info(
|
||||
f"Continuing conversation {conversation.id} via reply to {lookup_handle!r}"
|
||||
)
|
||||
else:
|
||||
# Handle not found (pre-migration messages) — fall back to most recent
|
||||
logger.warning(
|
||||
f"Reply handle {lookup_handle!r} not in DB; falling back to most recent conversation"
|
||||
)
|
||||
conversation = await get_conversation_for_channel(
|
||||
user=user, channel="imessage"
|
||||
)
|
||||
else:
|
||||
conversation = await Conversation.create(
|
||||
name="New iMessage",
|
||||
user=user,
|
||||
channel="imessage",
|
||||
)
|
||||
logger.info(
|
||||
f"New iMessage conversation {conversation.id} for {user.username}"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get conversation for user {user.username}: {e}")
|
||||
logger.error(f"Failed to get/create conversation for {user.username}: {e}")
|
||||
await send_imessage(
|
||||
from_number, "Sorry, something went wrong. Please try again later."
|
||||
)
|
||||
return jsonify({"status": "error"}), 200
|
||||
|
||||
# Add user message to conversation
|
||||
# Save the inbound message now so it's persisted before we ack
|
||||
await add_message_to_conversation(
|
||||
conversation=conversation,
|
||||
message=content,
|
||||
speaker="user",
|
||||
user=user,
|
||||
message_handle=inbound_handle,
|
||||
)
|
||||
|
||||
# Build messages payload for LangChain agent
|
||||
# Fire LLM processing in the background so we can ack SendBlue immediately.
|
||||
# SendBlue retries if it doesn't receive a 200 within its timeout (~5s).
|
||||
asyncio.ensure_future(
|
||||
_process_and_reply(
|
||||
from_number=from_number,
|
||||
inbound_handle=inbound_handle,
|
||||
conversation=conversation,
|
||||
user=user,
|
||||
content=content,
|
||||
media_url=media_url,
|
||||
)
|
||||
)
|
||||
|
||||
return jsonify({"status": "ok"}), 200
|
||||
|
||||
|
||||
async def _process_and_reply(
|
||||
from_number: str,
|
||||
inbound_handle: str | None,
|
||||
conversation: Conversation,
|
||||
user,
|
||||
content: str,
|
||||
media_url: str | None,
|
||||
) -> None:
|
||||
"""Run the LLM agent and send the reply. Called as a background task."""
|
||||
# Attach image if present
|
||||
image_block = None
|
||||
if media_url:
|
||||
try:
|
||||
from utils.image_process import image_content_block
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(media_url, timeout=30)
|
||||
resp.raise_for_status()
|
||||
image_block = image_content_block(resp.content)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to load iMessage image: {e}")
|
||||
content = f"{content or ''}\n\n[An image was sent but could not be loaded.]".strip()
|
||||
|
||||
await send_typing_indicator(from_number, state="start", max_duration_ms=120_000)
|
||||
|
||||
response_text = "Sorry, I'm having trouble thinking right now."
|
||||
try:
|
||||
messages = await conversation.messages.all()
|
||||
recent_messages = list(messages)[-10:]
|
||||
if image_block:
|
||||
user_content = [
|
||||
{"type": "text", "text": content or "The user sent this image."},
|
||||
image_block,
|
||||
]
|
||||
else:
|
||||
user_content = content
|
||||
|
||||
messages_payload = [{"role": "system", "content": SIMBA_SYSTEM_PROMPT}]
|
||||
|
||||
for msg in recent_messages[:-1]:
|
||||
role = "user" if msg.speaker == "user" else "assistant"
|
||||
messages_payload.append({"role": role, "content": msg.text})
|
||||
|
||||
messages_payload.append({"role": "user", "content": content})
|
||||
|
||||
logger.info(f"Invoking LangChain agent with {len(messages_payload)} messages")
|
||||
response = await main_agent.ainvoke({"messages": messages_payload})
|
||||
response_text = response.get("messages", [])[-1].content
|
||||
system_prompt = await build_system_prompt_with_memories(str(user.id))
|
||||
system_prompt += (
|
||||
"\n\nIMPORTANT: You are replying via iMessage. Use plain text only. "
|
||||
"Do not use markdown tables, headers, bullet points, bold, italics, "
|
||||
"or any other markdown formatting. Write in natural prose."
|
||||
)
|
||||
agent_config = {
|
||||
"configurable": {
|
||||
"user_id": str(user.id),
|
||||
"is_admin": user.is_admin(),
|
||||
"thread_id": str(conversation.id),
|
||||
"system_prompt": system_prompt,
|
||||
}
|
||||
}
|
||||
logger.info(f"Invoking LangChain agent for conversation {conversation.id}")
|
||||
response = await get_agent_for(user).ainvoke(
|
||||
{"messages": [{"role": "user", "content": user_content}]},
|
||||
config=agent_config,
|
||||
)
|
||||
response_text = extract_agent_text(response.get("messages", [])[-1].content)
|
||||
await persist_agent_tool_messages(
|
||||
conversation, user, response.get("messages", [])
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error invoking agent: {e}")
|
||||
response_text = "Sorry, I'm having trouble thinking right now."
|
||||
finally:
|
||||
await send_typing_indicator(from_number, state="stop")
|
||||
|
||||
send_resp = await send_imessage(
|
||||
from_number,
|
||||
strip_markdown(response_text),
|
||||
reply_to_handle=inbound_handle,
|
||||
)
|
||||
outbound_handle = (send_resp or {}).get("message_handle")
|
||||
|
||||
# Save and send response
|
||||
await add_message_to_conversation(
|
||||
conversation=conversation,
|
||||
message=response_text,
|
||||
speaker="simba",
|
||||
user=user,
|
||||
message_handle=outbound_handle,
|
||||
)
|
||||
|
||||
from utils.strip_markdown import strip_markdown
|
||||
|
||||
await send_imessage(from_number, strip_markdown(response_text))
|
||||
|
||||
return jsonify({"status": "ok"}), 200
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import hmac
|
||||
import logging
|
||||
import os
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from quart import Blueprint, request, jsonify
|
||||
@@ -102,6 +104,11 @@ async def create_message():
|
||||
use_agent=use_agent,
|
||||
created_by_id=user_id,
|
||||
)
|
||||
|
||||
from utils.cronicle import create_message_event
|
||||
|
||||
await create_message_event(str(msg.id), msg.scheduled_at, recurrence_enum.value)
|
||||
|
||||
return jsonify(_serialize(msg)), 201
|
||||
|
||||
|
||||
@@ -112,13 +119,14 @@ async def update_message(msg_id: str):
|
||||
if not msg:
|
||||
return jsonify({"error": "Not found"}), 404
|
||||
|
||||
if msg.status != MessageStatus.PENDING:
|
||||
return jsonify({"error": "Can only update pending messages"}), 400
|
||||
if msg.status not in (MessageStatus.PENDING, MessageStatus.FAILED):
|
||||
return jsonify({"error": "Can only update pending or failed messages"}), 400
|
||||
|
||||
data = await request.get_json()
|
||||
if not data:
|
||||
return jsonify({"error": "Invalid payload"}), 400
|
||||
|
||||
rescheduled = False
|
||||
if "recipient" in data:
|
||||
msg.recipient = data["recipient"].strip()
|
||||
if "channel" in data:
|
||||
@@ -145,18 +153,47 @@ async def update_message(msg_id: str):
|
||||
if scheduled_at <= datetime.now(timezone.utc):
|
||||
return jsonify({"error": "scheduled_at must be in the future"}), 400
|
||||
msg.scheduled_at = scheduled_at
|
||||
rescheduled = True
|
||||
except ValueError:
|
||||
return jsonify({"error": "Invalid scheduled_at format"}), 400
|
||||
if "status" in data and data["status"] == "cancelled":
|
||||
msg.status = MessageStatus.CANCELLED
|
||||
elif rescheduled:
|
||||
msg.status = MessageStatus.PENDING
|
||||
msg.error_message = None
|
||||
|
||||
if msg.channel == MessageChannel.EMAIL and not msg.subject:
|
||||
return jsonify({"error": "subject is required for email messages"}), 400
|
||||
|
||||
await msg.save()
|
||||
|
||||
if rescheduled and msg.status == MessageStatus.PENDING:
|
||||
from utils.cronicle import create_message_event
|
||||
|
||||
await create_message_event(str(msg.id), msg.scheduled_at, msg.recurrence.value)
|
||||
|
||||
return jsonify(_serialize(msg))
|
||||
|
||||
|
||||
def _check_cron_secret() -> bool:
|
||||
expected = os.environ.get("CRON_SECRET", "")
|
||||
if not expected:
|
||||
return False
|
||||
provided = request.headers.get("X-Cron-Secret", "")
|
||||
return hmac.compare_digest(provided.encode(), expected.encode())
|
||||
|
||||
|
||||
@scheduled_messages_blueprint.route("/trigger", methods=["POST"])
|
||||
async def trigger_due_messages():
|
||||
if not _check_cron_secret():
|
||||
return jsonify({"error": "Unauthorized"}), 401
|
||||
|
||||
from .scheduler import process_due_messages
|
||||
|
||||
result = await process_due_messages()
|
||||
return jsonify(result)
|
||||
|
||||
|
||||
@scheduled_messages_blueprint.route("/<msg_id>", methods=["DELETE"])
|
||||
@admin_required
|
||||
async def delete_message(msg_id: str):
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from quart import current_app
|
||||
|
||||
from .models import ScheduledMessage, MessageChannel, MessageStatus, Recurrence
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
POLL_INTERVAL = 15
|
||||
|
||||
RECURRENCE_DELTAS = {
|
||||
Recurrence.DAILY: relativedelta(days=1),
|
||||
Recurrence.WEEKLY: relativedelta(weeks=1),
|
||||
@@ -16,27 +15,20 @@ RECURRENCE_DELTAS = {
|
||||
}
|
||||
|
||||
|
||||
async def _run_agent(prompt: str) -> str:
|
||||
"""Run a prompt through the LangChain agent and return the response text."""
|
||||
from blueprints.conversation.agents import main_agent
|
||||
from blueprints.conversation.prompts import SIMBA_SYSTEM_PROMPT
|
||||
|
||||
messages_payload = [
|
||||
{"role": "system", "content": SIMBA_SYSTEM_PROMPT},
|
||||
{"role": "user", "content": prompt},
|
||||
]
|
||||
response = await main_agent.ainvoke({"messages": messages_payload})
|
||||
async def _run_agent(prompt: str, thread_id: str) -> str:
|
||||
response = await current_app.main_agent.ainvoke(
|
||||
{"messages": [{"role": "user", "content": prompt}]},
|
||||
config={"configurable": {"thread_id": thread_id}},
|
||||
)
|
||||
return response.get("messages", [])[-1].content
|
||||
|
||||
|
||||
async def _schedule_next_occurrence(msg: ScheduledMessage):
|
||||
"""Create the next pending occurrence for a recurring message."""
|
||||
delta = RECURRENCE_DELTAS.get(msg.recurrence)
|
||||
if not delta:
|
||||
return
|
||||
|
||||
next_at = msg.scheduled_at + delta
|
||||
# If we missed several intervals, advance until we're in the future
|
||||
now = datetime.now(timezone.utc)
|
||||
while next_at <= now:
|
||||
next_at += delta
|
||||
@@ -56,58 +48,55 @@ async def _schedule_next_occurrence(msg: ScheduledMessage):
|
||||
)
|
||||
|
||||
|
||||
async def scheduled_messages_loop():
|
||||
"""Background loop that polls for and sends due scheduled messages."""
|
||||
logger.info(f"Scheduled messages loop started (interval={POLL_INTERVAL}s)")
|
||||
async def process_due_messages() -> dict:
|
||||
"""Process all currently due pending messages. Returns a result summary."""
|
||||
now = datetime.now(timezone.utc)
|
||||
due = await ScheduledMessage.filter(
|
||||
status=MessageStatus.PENDING,
|
||||
scheduled_at__lte=now,
|
||||
).all()
|
||||
|
||||
while True:
|
||||
sent, failed = 0, 0
|
||||
for msg in due:
|
||||
try:
|
||||
now = datetime.now(timezone.utc)
|
||||
due = await ScheduledMessage.filter(
|
||||
status=MessageStatus.PENDING,
|
||||
scheduled_at__lte=now,
|
||||
).all()
|
||||
send_content = msg.content
|
||||
|
||||
for msg in due:
|
||||
try:
|
||||
send_content = msg.content
|
||||
if msg.use_agent:
|
||||
send_content = await _run_agent(
|
||||
msg.content, f"scheduled-{uuid.uuid4()}"
|
||||
)
|
||||
|
||||
if msg.use_agent:
|
||||
send_content = await _run_agent(msg.content)
|
||||
if msg.channel == MessageChannel.IMESSAGE:
|
||||
from blueprints.imessage import send_imessage
|
||||
from utils.strip_markdown import strip_markdown
|
||||
|
||||
if msg.channel == MessageChannel.IMESSAGE:
|
||||
from blueprints.imessage import send_imessage
|
||||
from utils.strip_markdown import strip_markdown
|
||||
await send_imessage(msg.recipient, strip_markdown(send_content))
|
||||
|
||||
await send_imessage(msg.recipient, strip_markdown(send_content))
|
||||
elif msg.channel == MessageChannel.EMAIL:
|
||||
from blueprints.email import send_email_reply
|
||||
|
||||
elif msg.channel == MessageChannel.EMAIL:
|
||||
from blueprints.email import send_email_reply
|
||||
await send_email_reply(
|
||||
to=msg.recipient,
|
||||
subject=msg.subject or "(no subject)",
|
||||
body=send_content,
|
||||
)
|
||||
|
||||
await send_email_reply(
|
||||
to=msg.recipient,
|
||||
subject=msg.subject or "(no subject)",
|
||||
body=send_content,
|
||||
)
|
||||
msg.status = MessageStatus.SENT
|
||||
msg.error_message = None
|
||||
await msg.save()
|
||||
logger.info(
|
||||
f"Sent scheduled {msg.channel.value} message {msg.id} to {msg.recipient}"
|
||||
)
|
||||
sent += 1
|
||||
|
||||
msg.status = MessageStatus.SENT
|
||||
msg.error_message = None
|
||||
await msg.save()
|
||||
logger.info(
|
||||
f"Sent scheduled {msg.channel.value} message {msg.id} to {msg.recipient}"
|
||||
)
|
||||
if msg.recurrence != Recurrence.NONE:
|
||||
await _schedule_next_occurrence(msg)
|
||||
|
||||
# Schedule next occurrence for recurring messages
|
||||
if msg.recurrence != Recurrence.NONE:
|
||||
await _schedule_next_occurrence(msg)
|
||||
except Exception as e:
|
||||
msg.status = MessageStatus.FAILED
|
||||
msg.error_message = str(e)
|
||||
await msg.save()
|
||||
logger.error(f"Failed to send scheduled message {msg.id}: {e}")
|
||||
failed += 1
|
||||
|
||||
except Exception as e:
|
||||
msg.status = MessageStatus.FAILED
|
||||
msg.error_message = str(e)
|
||||
await msg.save()
|
||||
logger.error(f"Failed to send scheduled message {msg.id}: {e}")
|
||||
|
||||
except Exception:
|
||||
logger.exception("Error in scheduled messages loop")
|
||||
|
||||
await asyncio.sleep(POLL_INTERVAL)
|
||||
return {"sent": sent, "failed": failed}
|
||||
|
||||
@@ -9,11 +9,12 @@ from twilio.twiml.messaging_response import MessagingResponse
|
||||
|
||||
from blueprints.users.models import User
|
||||
from blueprints.conversation.logic import (
|
||||
get_agent_for,
|
||||
get_conversation_for_channel,
|
||||
add_message_to_conversation,
|
||||
persist_agent_tool_messages,
|
||||
)
|
||||
from blueprints.conversation.agents import main_agent
|
||||
from blueprints.conversation.prompts import SIMBA_SYSTEM_PROMPT
|
||||
from utils.strip_markdown import extract_agent_text
|
||||
|
||||
whatsapp_blueprint = Blueprint("whatsapp_api", __name__, url_prefix="/api/whatsapp")
|
||||
|
||||
@@ -163,7 +164,6 @@ async def webhook():
|
||||
# Get or create a conversation for this user
|
||||
try:
|
||||
conversation = await get_conversation_for_channel(user=user, channel="whatsapp")
|
||||
await conversation.fetch_related("messages")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get conversation for user {user.username}: {e}")
|
||||
return _twiml_response("Sorry, something went wrong. Please try again later.")
|
||||
@@ -176,27 +176,23 @@ async def webhook():
|
||||
user=user,
|
||||
)
|
||||
|
||||
# Build messages payload for LangChain agent with system prompt and conversation history
|
||||
try:
|
||||
# Get last 10 messages for conversation history
|
||||
messages = await conversation.messages.all()
|
||||
recent_messages = list(messages)[-10:]
|
||||
|
||||
# Build messages payload
|
||||
messages_payload = [{"role": "system", "content": SIMBA_SYSTEM_PROMPT}]
|
||||
|
||||
# Add recent conversation history (exclude the message we just added)
|
||||
for msg in recent_messages[:-1]:
|
||||
role = "user" if msg.speaker == "user" else "assistant"
|
||||
messages_payload.append({"role": role, "content": msg.text})
|
||||
|
||||
# Add current query
|
||||
messages_payload.append({"role": "user", "content": body})
|
||||
|
||||
# Invoke LangChain agent
|
||||
logger.info(f"Invoking LangChain agent with {len(messages_payload)} messages")
|
||||
response = await main_agent.ainvoke({"messages": messages_payload})
|
||||
response_text = response.get("messages", [])[-1].content
|
||||
agent_config = {
|
||||
"configurable": {
|
||||
"user_id": str(user.id),
|
||||
"is_admin": user.is_admin(),
|
||||
"thread_id": str(conversation.id),
|
||||
}
|
||||
}
|
||||
logger.info(f"Invoking LangChain agent for conversation {conversation.id}")
|
||||
response = await get_agent_for(user).ainvoke(
|
||||
{"messages": [{"role": "user", "content": body}]},
|
||||
config=agent_config,
|
||||
)
|
||||
response_text = extract_agent_text(response.get("messages", [])[-1].content)
|
||||
await persist_agent_tool_messages(
|
||||
conversation, user, response.get("messages", [])
|
||||
)
|
||||
|
||||
# Log YNAB availability
|
||||
if os.getenv("YNAB_ACCESS_TOKEN"):
|
||||
|
||||
@@ -17,6 +17,8 @@ TORTOISE_CONFIG = {
|
||||
"blueprints.users.models",
|
||||
"blueprints.email.models",
|
||||
"blueprints.scheduled_messages.models",
|
||||
"blueprints.cron.models",
|
||||
"blueprints.beli.models",
|
||||
"aerich.models",
|
||||
],
|
||||
"default_connection": "default",
|
||||
|
||||
+96
-68
@@ -1,5 +1,88 @@
|
||||
version: "3.8"
|
||||
|
||||
x-raggr-common: &raggr-common
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
image: torrtle/simbarag:latest
|
||||
environment:
|
||||
- PAPERLESS_TOKEN=${PAPERLESS_TOKEN}
|
||||
- BASE_URL=${BASE_URL}
|
||||
- OLLAMA_URL=${OLLAMA_URL:-http://localhost:11434}
|
||||
- OPENAI_API_KEY=${OPENAI_API_KEY}
|
||||
- EMBEDDING_SERVER_URL=${EMBEDDING_SERVER_URL}
|
||||
- EMBEDDING_MODEL_NAME=${EMBEDDING_MODEL_NAME}
|
||||
- JWT_SECRET_KEY=${JWT_SECRET_KEY}
|
||||
- LLAMA_SERVER_URL=${LLAMA_SERVER_URL}
|
||||
- LLAMA_MODEL_NAME=${LLAMA_MODEL_NAME}
|
||||
- OIDC_ISSUER=${OIDC_ISSUER}
|
||||
- OIDC_CLIENT_ID=${OIDC_CLIENT_ID}
|
||||
- OIDC_CLIENT_SECRET=${OIDC_CLIENT_SECRET}
|
||||
- OIDC_REDIRECT_URI=${OIDC_REDIRECT_URI}
|
||||
- OIDC_USE_DISCOVERY=${OIDC_USE_DISCOVERY:-true}
|
||||
- DATABASE_URL=${DATABASE_URL:-postgres://raggr:changeme@postgres:5432/raggr}
|
||||
- TAVILY_API_KEY=${TAVILIY_API_KEY}
|
||||
- YNAB_ACCESS_TOKEN=${YNAB_ACCESS_TOKEN}
|
||||
- YNAB_BUDGET_ID=${YNAB_BUDGET_ID}
|
||||
- MEALIE_BASE_URL=${MEALIE_BASE_URL}
|
||||
- MEALIE_API_TOKEN=${MEALIE_API_TOKEN}
|
||||
- TWILIO_ACCOUNT_SID=${TWILIO_ACCOUNT_SID}
|
||||
- TWILIO_AUTH_TOKEN=${TWILIO_AUTH_TOKEN}
|
||||
- TWILIO_WHATSAPP_NUMBER=${TWILIO_WHATSAPP_NUMBER}
|
||||
- ALLOWED_WHATSAPP_NUMBERS=${ALLOWED_WHATSAPP_NUMBERS}
|
||||
- TWILIO_SIGNATURE_VALIDATION=${TWILIO_SIGNATURE_VALIDATION:-true}
|
||||
- TWILIO_WEBHOOK_URL=${TWILIO_WEBHOOK_URL:-}
|
||||
- OBSIDIAN_EMAIL=${OBSIDIAN_EMAIL}
|
||||
- OBSIDIAN_PASSWORD=${OBSIDIAN_PASSWORD}
|
||||
- OBSIDIAN_VAULT_ID=${OBSIDIAN_VAULT_ID}
|
||||
- OBSIDIAN_E2E_PASSWORD=${OBSIDIAN_E2E_PASSWORD}
|
||||
- OBSIDIAN_DEVICE_NAME=${OBSIDIAN_DEVICE_NAME}
|
||||
- OBSIDIAN_CONTINUOUS_SYNC=${OBSIDIAN_CONTINUOUS_SYNC:-false}
|
||||
- OBSIDIAN_VAULT_PATH=${OBSIDIAN_VAULT_PATH:-/app/data/obsidian}
|
||||
- S3_ENDPOINT_URL=${S3_ENDPOINT_URL}
|
||||
- S3_ACCESS_KEY_ID=${S3_ACCESS_KEY_ID}
|
||||
- S3_SECRET_ACCESS_KEY=${S3_SECRET_ACCESS_KEY}
|
||||
- S3_BUCKET_NAME=${S3_BUCKET_NAME:-asksimba-images}
|
||||
- S3_REGION=${S3_REGION:-garage}
|
||||
- OLLAMA_HOST=${OLLAMA_HOST:-http://localhost:11434}
|
||||
- FERNET_KEY=${FERNET_KEY}
|
||||
- GOOGLE_TOKEN_FILE=${GOOGLE_TOKEN_FILE:-/app/config/google-token.json}
|
||||
- MAILGUN_API_KEY=${MAILGUN_API_KEY}
|
||||
- MAILGUN_DOMAIN=${MAILGUN_DOMAIN}
|
||||
- MAILGUN_WEBHOOK_SIGNING_KEY=${MAILGUN_WEBHOOK_SIGNING_KEY}
|
||||
- MAILGUN_SIGNATURE_VALIDATION=${MAILGUN_SIGNATURE_VALIDATION:-true}
|
||||
- EMAIL_RATE_LIMIT_MAX=${EMAIL_RATE_LIMIT_MAX:-5}
|
||||
- EMAIL_RATE_LIMIT_WINDOW=${EMAIL_RATE_LIMIT_WINDOW:-300}
|
||||
- SENDBLUE_API_KEY=${SENDBLUE_API_KEY}
|
||||
- SENDBLUE_API_SECRET=${SENDBLUE_API_SECRET}
|
||||
- SENDBLUE_FROM_NUMBER=${SENDBLUE_FROM_NUMBER}
|
||||
- SENDBLUE_WEBHOOK_SECRET=${SENDBLUE_WEBHOOK_SECRET}
|
||||
- SENDBLUE_SIGNATURE_VALIDATION=${SENDBLUE_SIGNATURE_VALIDATION:-true}
|
||||
- ALLOWED_IMESSAGE_NUMBERS=${ALLOWED_IMESSAGE_NUMBERS}
|
||||
- SIMBAKIT_URL=${SIMBAKIT_URL:-http://simbakit:8585}
|
||||
- CRON_SECRET=${CRON_SECRET}
|
||||
- CRONICLE_URL=${CRONICLE_URL:-http://cronicle:3012}
|
||||
- CRONICLE_API_KEY=${CRONICLE_API_KEY}
|
||||
- HOMEBOX_URL=${HOMEBOX_URL}
|
||||
- HOMEBOX_TOKEN=${HOMEBOX_TOKEN}
|
||||
- BOOKSTACK_BASE_URL=${BOOKSTACK_BASE_URL}
|
||||
- BOOKSTACK_TOKEN_ID=${BOOKSTACK_TOKEN_ID}
|
||||
- BOOKSTACK_TOKEN_SECRET=${BOOKSTACK_TOKEN_SECRET}
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
volumes:
|
||||
- ./obvault:/app/data/obsidian
|
||||
- ./token.json:/app/config/google-token.json:ro
|
||||
- raggr-checkpoints:/app/checkpoints
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://localhost:8080/api/health"]
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
start_period: 60s
|
||||
restart: unless-stopped
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: pgvector/pgvector:pg16
|
||||
@@ -19,76 +102,20 @@ services:
|
||||
retries: 5
|
||||
restart: unless-stopped
|
||||
|
||||
raggr:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
image: torrtle/simbarag:latest
|
||||
# Blue-green deployment: exactly one of raggr-blue/raggr-green serves at a
|
||||
# time; Caddy health-checks both ports and routes to the healthy one.
|
||||
# Use scripts/deploy.sh (make redeploy) to swap colors with zero downtime.
|
||||
raggr-blue:
|
||||
<<: *raggr-common
|
||||
profiles: ["blue"]
|
||||
ports:
|
||||
- "8080:8080"
|
||||
environment:
|
||||
- PAPERLESS_TOKEN=${PAPERLESS_TOKEN}
|
||||
- BASE_URL=${BASE_URL}
|
||||
- OLLAMA_URL=${OLLAMA_URL:-http://localhost:11434}
|
||||
- OPENAI_API_KEY=${OPENAI_API_KEY}
|
||||
- EMBEDDING_SERVER_URL=${EMBEDDING_SERVER_URL}
|
||||
- EMBEDDING_MODEL_NAME=${EMBEDDING_MODEL_NAME}
|
||||
- JWT_SECRET_KEY=${JWT_SECRET_KEY}
|
||||
- LLAMA_SERVER_URL=${LLAMA_SERVER_URL}
|
||||
- LLAMA_MODEL_NAME=${LLAMA_MODEL_NAME}
|
||||
- OIDC_ISSUER=${OIDC_ISSUER}
|
||||
- OIDC_CLIENT_ID=${OIDC_CLIENT_ID}
|
||||
- OIDC_CLIENT_SECRET=${OIDC_CLIENT_SECRET}
|
||||
- OIDC_REDIRECT_URI=${OIDC_REDIRECT_URI}
|
||||
- OIDC_USE_DISCOVERY=${OIDC_USE_DISCOVERY:-true}
|
||||
- DATABASE_URL=${DATABASE_URL:-postgres://raggr:changeme@postgres:5432/raggr}
|
||||
- TAVILY_API_KEY=${TAVILIY_API_KEY}
|
||||
- YNAB_ACCESS_TOKEN=${YNAB_ACCESS_TOKEN}
|
||||
- YNAB_BUDGET_ID=${YNAB_BUDGET_ID}
|
||||
- MEALIE_BASE_URL=${MEALIE_BASE_URL}
|
||||
- MEALIE_API_TOKEN=${MEALIE_API_TOKEN}
|
||||
- TWILIO_ACCOUNT_SID=${TWILIO_ACCOUNT_SID}
|
||||
- TWILIO_AUTH_TOKEN=${TWILIO_AUTH_TOKEN}
|
||||
- TWILIO_WHATSAPP_NUMBER=${TWILIO_WHATSAPP_NUMBER}
|
||||
- ALLOWED_WHATSAPP_NUMBERS=${ALLOWED_WHATSAPP_NUMBERS}
|
||||
- TWILIO_SIGNATURE_VALIDATION=${TWILIO_SIGNATURE_VALIDATION:-true}
|
||||
- TWILIO_WEBHOOK_URL=${TWILIO_WEBHOOK_URL:-}
|
||||
- OBSIDIAN_EMAIL=${OBSIDIAN_EMAIL}
|
||||
- OBSIDIAN_PASSWORD=${OBSIDIAN_PASSWORD}
|
||||
- OBSIDIAN_VAULT_ID=${OBSIDIAN_VAULT_ID}
|
||||
- OBSIDIAN_E2E_PASSWORD=${OBSIDIAN_E2E_PASSWORD}
|
||||
- OBSIDIAN_DEVICE_NAME=${OBSIDIAN_DEVICE_NAME}
|
||||
- OBSIDIAN_CONTINUOUS_SYNC=${OBSIDIAN_CONTINUOUS_SYNC:-false}
|
||||
- OBSIDIAN_VAULT_PATH=${OBSIDIAN_VAULT_PATH:-/app/data/obsidian}
|
||||
- S3_ENDPOINT_URL=${S3_ENDPOINT_URL}
|
||||
- S3_ACCESS_KEY_ID=${S3_ACCESS_KEY_ID}
|
||||
- S3_SECRET_ACCESS_KEY=${S3_SECRET_ACCESS_KEY}
|
||||
- S3_BUCKET_NAME=${S3_BUCKET_NAME:-asksimba-images}
|
||||
- S3_REGION=${S3_REGION:-garage}
|
||||
- OLLAMA_HOST=${OLLAMA_HOST:-http://localhost:11434}
|
||||
- FERNET_KEY=${FERNET_KEY}
|
||||
- GOOGLE_CALENDAR_ENABLED=${GOOGLE_CALENDAR_ENABLED:-}
|
||||
- GOOGLE_WORKSPACE_CLI_CREDENTIALS_FILE=${GOOGLE_WORKSPACE_CLI_CREDENTIALS_FILE:-/app/config/gws-credentials.json}
|
||||
- MAILGUN_API_KEY=${MAILGUN_API_KEY}
|
||||
- MAILGUN_DOMAIN=${MAILGUN_DOMAIN}
|
||||
- MAILGUN_WEBHOOK_SIGNING_KEY=${MAILGUN_WEBHOOK_SIGNING_KEY}
|
||||
- MAILGUN_SIGNATURE_VALIDATION=${MAILGUN_SIGNATURE_VALIDATION:-true}
|
||||
- EMAIL_RATE_LIMIT_MAX=${EMAIL_RATE_LIMIT_MAX:-5}
|
||||
- EMAIL_RATE_LIMIT_WINDOW=${EMAIL_RATE_LIMIT_WINDOW:-300}
|
||||
- SENDBLUE_API_KEY=${SENDBLUE_API_KEY}
|
||||
- SENDBLUE_API_SECRET=${SENDBLUE_API_SECRET}
|
||||
- SENDBLUE_FROM_NUMBER=${SENDBLUE_FROM_NUMBER}
|
||||
- SENDBLUE_WEBHOOK_SECRET=${SENDBLUE_WEBHOOK_SECRET}
|
||||
- SENDBLUE_SIGNATURE_VALIDATION=${SENDBLUE_SIGNATURE_VALIDATION:-true}
|
||||
- ALLOWED_IMESSAGE_NUMBERS=${ALLOWED_IMESSAGE_NUMBERS}
|
||||
- SIMBAKIT_URL=${SIMBAKIT_URL:-http://simbakit:8585}
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
volumes:
|
||||
- ./obvault:/app/data/obsidian
|
||||
- ./credentials.json:/app/config/gws-credentials.json:ro
|
||||
restart: unless-stopped
|
||||
|
||||
raggr-green:
|
||||
<<: *raggr-common
|
||||
profiles: ["green"]
|
||||
ports:
|
||||
- "8081:8080"
|
||||
|
||||
simbakit:
|
||||
build:
|
||||
@@ -105,3 +132,4 @@ services:
|
||||
volumes:
|
||||
postgres_data:
|
||||
simbakit-data:
|
||||
raggr-checkpoints:
|
||||
|
||||
+76
-8
@@ -96,20 +96,88 @@ The dev environment:
|
||||
- Rebuilds frontend on changes
|
||||
- Auto-applies migrations on startup
|
||||
|
||||
### Production
|
||||
### Production (zero-downtime blue-green)
|
||||
|
||||
The app runs as one of two identical compose services: `raggr-blue`
|
||||
(host port 8080) or `raggr-green` (host port 8081), gated behind compose
|
||||
profiles so only one color runs at a time. Caddy health-checks both ports and
|
||||
routes traffic to whichever is healthy.
|
||||
|
||||
```bash
|
||||
# Build and deploy
|
||||
docker compose build raggr
|
||||
docker compose up -d
|
||||
# Pull latest and swap colors with zero downtime
|
||||
make redeploy
|
||||
|
||||
# View logs
|
||||
docker compose logs -f raggr
|
||||
# Or run the swap directly
|
||||
./scripts/deploy.sh
|
||||
|
||||
# Verify migrations applied
|
||||
docker compose exec raggr aerich history
|
||||
# View logs / exec into the active color (Makefile resolves the color)
|
||||
make logs
|
||||
make migrate-history
|
||||
```
|
||||
|
||||
`scripts/deploy.sh` does the following:
|
||||
|
||||
1. Detects the running color via `docker compose ps`.
|
||||
2. Builds the inactive color.
|
||||
3. Starts it with `docker compose up -d --wait`, which blocks until the
|
||||
container healthcheck (`GET /api/health`) passes.
|
||||
4. If the new color never becomes healthy, it is stopped and the old color
|
||||
keeps serving (the script exits non-zero).
|
||||
5. Otherwise the old color is stopped with a 30s grace period so in-flight
|
||||
SSE streams can finish, then removed.
|
||||
|
||||
To target a color manually, enable its profile:
|
||||
|
||||
```bash
|
||||
docker compose --profile blue up -d raggr-blue
|
||||
docker compose --profile blue --profile green ps
|
||||
```
|
||||
|
||||
#### Caddy configuration
|
||||
|
||||
Caddy needs both upstreams with active health checks:
|
||||
|
||||
```caddyfile
|
||||
ask.torrtle.co {
|
||||
reverse_proxy host.docker.internal:8080 host.docker.internal:8081 {
|
||||
lb_policy first # prefer blue when both are healthy
|
||||
lb_try_duration 5s # retry on the other upstream during the swap
|
||||
lb_try_interval 250ms
|
||||
health_uri /api/health
|
||||
health_interval 2s
|
||||
health_timeout 2s
|
||||
fail_duration 10s
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### One-time cutover from the old `raggr` service
|
||||
|
||||
The first deploy after this change lands needs manual steps, because the old
|
||||
`raggr` container still holds port 8080 and the deploy script doesn't know
|
||||
about it:
|
||||
|
||||
```bash
|
||||
# 1. Update the Caddy config to the two-upstream block below and reload Caddy
|
||||
# 2. On the server:
|
||||
git pull
|
||||
docker compose --profile green up -d --build --wait raggr-green # port 8081, no conflict
|
||||
docker stop -t 30 $(docker ps -q --filter name=raggr-1) # old raggr container
|
||||
docker rm $(docker ps -aq --filter name=raggr-1)
|
||||
# From here on, `make redeploy` handles swaps (green is now the active color)
|
||||
```
|
||||
|
||||
#### Blue-green caveats
|
||||
|
||||
- **Migrations must be backward-compatible.** The new color runs
|
||||
`aerich upgrade` on startup while the old color is still serving the
|
||||
previous code. Additive changes (new tables/columns) are safe; destructive
|
||||
changes (drop/rename a column the old code reads) need a two-step deploy:
|
||||
ship code that stops using the column first, then drop it in a later deploy.
|
||||
- **Obsidian sync overlap.** During the ~30s swap window both containers run
|
||||
pull-only `ob sync` against the shared `./obvault` mount. Both are
|
||||
read-only pulls, so this is low risk, but avoid deploying mid-reindex.
|
||||
|
||||
### Fresh Deploy (New Database)
|
||||
|
||||
On first deploy with an empty database, `startup-dev.sh` runs `aerich init-db` instead of `aerich upgrade`. This creates all tables from the current models.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import os
|
||||
import logging
|
||||
|
||||
import httpx
|
||||
from openai import OpenAI
|
||||
from dotenv import load_dotenv
|
||||
|
||||
@@ -13,7 +14,15 @@ class LLMClient:
|
||||
def __init__(self):
|
||||
llama_url = os.getenv("LLAMA_SERVER_URL")
|
||||
if llama_url:
|
||||
self.client = OpenAI(base_url=llama_url, api_key="not-needed")
|
||||
# Tight read timeout so a wedged llama-server fails fast instead
|
||||
# of hanging for the OpenAI client's 600s default.
|
||||
read_timeout = float(os.getenv("LLAMA_TIMEOUT_SECONDS", "120"))
|
||||
self.client = OpenAI(
|
||||
base_url=llama_url,
|
||||
api_key="not-needed",
|
||||
timeout=httpx.Timeout(read_timeout, connect=10.0),
|
||||
max_retries=1,
|
||||
)
|
||||
self.model = os.getenv("LLAMA_MODEL_NAME", "llama-3.1-8b-instruct")
|
||||
self.PROVIDER = "llama_server"
|
||||
logging.info("Using llama_server as LLM backend")
|
||||
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
import time
|
||||
from contextlib import contextmanager
|
||||
from prometheus_client import (
|
||||
CollectorRegistry,
|
||||
Counter,
|
||||
Histogram,
|
||||
generate_latest,
|
||||
)
|
||||
|
||||
REGISTRY = CollectorRegistry(auto_describe=True)
|
||||
|
||||
CHAT_RESPONSE_SECONDS = Histogram(
|
||||
"raggr_chat_response_seconds",
|
||||
"End-to-end chat response time in seconds.",
|
||||
labelnames=("stream",),
|
||||
buckets=(0.5, 1, 2, 5, 10, 30, 60, 120, 300),
|
||||
registry=REGISTRY,
|
||||
)
|
||||
|
||||
CHAT_REQUESTS_TOTAL = Counter(
|
||||
"raggr_chat_requests_total",
|
||||
"Total chat requests by stream mode and result.",
|
||||
labelnames=("stream", "result"),
|
||||
registry=REGISTRY,
|
||||
)
|
||||
|
||||
|
||||
def _s(stream: bool) -> str:
|
||||
return "true" if stream else "false"
|
||||
|
||||
|
||||
@contextmanager
|
||||
def time_response(stream: bool):
|
||||
t0 = time.monotonic()
|
||||
result = "success"
|
||||
try:
|
||||
yield
|
||||
except Exception:
|
||||
result = "error"
|
||||
raise
|
||||
finally:
|
||||
CHAT_RESPONSE_SECONDS.labels(stream=_s(stream)).observe(time.monotonic() - t0)
|
||||
CHAT_REQUESTS_TOTAL.labels(stream=_s(stream), result=result).inc()
|
||||
|
||||
|
||||
def observe_response(stream: bool, seconds: float) -> None:
|
||||
CHAT_RESPONSE_SECONDS.labels(stream=_s(stream)).observe(seconds)
|
||||
|
||||
|
||||
def count_result(stream: bool, result: str) -> None:
|
||||
CHAT_REQUESTS_TOTAL.labels(stream=_s(stream), result=result).inc()
|
||||
|
||||
|
||||
def render_metrics() -> bytes:
|
||||
return generate_latest(REGISTRY)
|
||||
@@ -0,0 +1,71 @@
|
||||
from tortoise import BaseDBAsyncClient
|
||||
|
||||
RUN_IN_TRANSACTION = True
|
||||
|
||||
|
||||
async def upgrade(db: BaseDBAsyncClient) -> str:
|
||||
return """
|
||||
ALTER TABLE "conversation_messages" ADD "message_handle" VARCHAR(255);
|
||||
COMMENT ON COLUMN "conversation_messages"."speaker" IS 'USER: user\nSIMBA: simba\nTOOL: tool';"""
|
||||
|
||||
|
||||
async def downgrade(db: BaseDBAsyncClient) -> str:
|
||||
return """
|
||||
ALTER TABLE "conversation_messages" DROP COLUMN "message_handle";
|
||||
COMMENT ON COLUMN "conversation_messages"."speaker" IS 'USER: user\nSIMBA: simba';"""
|
||||
|
||||
|
||||
MODELS_STATE = (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+RDb4fcQnU70wr5gLCakbirkcorljkA2GRhUcjJu064VpF6S3YdoflIR2QKbfS"
|
||||
"UbAYkiQSOZKtk1KuUCa6P8rwZAPTfMuEYYJByWlwKYAFuZ8SPV49MSNOPBnxNiExYkpBLB"
|
||||
"P/KbEUSzJGYX0Nna9AKelnVtKKSXmZTIqKw3phcVi7DmqofxRW4ZVXiR8tey223JGWv5Sr"
|
||||
"7nGstZ8ZENP2bemd3eXn7xmhupwO7yMif9E3DJd/M2bT7xluD1SN77i9xlv9CiXXzQ2o3m"
|
||||
"60bq/Q7H7E5//aobpufhfnxM/wXvCLjLGb75fV3Ym8bD0dipo4ECu5hmWHPit+Dmwmvcer"
|
||||
"5WKrbnpIcvxB79y0Om+LFogW51whn1ALxnNAe3qnf4UYKMoBfQNaX7+5EicLxBrQ60bnWr"
|
||||
"8VSSbvqxNfmfn82wGYJVEQkBj/dYYpW8Meh+pu9uDjTjyBc4PEkwFYj8JOfAND8vEKWTAi"
|
||||
"kwH9S9ff859PhIz573a303/HE1zY60bO5DAGJ2LEgMko260XBstl5NS1rLlrWfnhXeKOVT"
|
||||
"GDCoI12bZVzJwiBFXMnBrYUqY3WXbDSdXPM+YFFd+bwmQLlG/9o+gKU+SQ+N4GCWxzpElY"
|
||||
"3jjnaBG3i+dlDobPLX93asUIiHgMn/McYTvnzuX0Lb8JVvrl1HKuJiVSFwZsH3fHw9KoAG"
|
||||
"JcvJ4A7uQjMaVcbPkd8eVc7N7uh9/Zpru1++EL2/M+t5cf/wK3jcna"
|
||||
)
|
||||
@@ -0,0 +1,86 @@
|
||||
from tortoise import BaseDBAsyncClient
|
||||
|
||||
RUN_IN_TRANSACTION = True
|
||||
|
||||
|
||||
async def upgrade(db: BaseDBAsyncClient) -> str:
|
||||
return """
|
||||
CREATE TABLE IF NOT EXISTS "cron_jobs" (
|
||||
"id" UUID NOT NULL PRIMARY KEY,
|
||||
"name" VARCHAR(255) NOT NULL,
|
||||
"description" TEXT,
|
||||
"prompt" TEXT NOT NULL,
|
||||
"recipient" VARCHAR(255) NOT NULL,
|
||||
"channel" VARCHAR(20) NOT NULL,
|
||||
"enabled" BOOL NOT NULL DEFAULT True,
|
||||
"last_run_at" TIMESTAMPTZ,
|
||||
"last_status" VARCHAR(50),
|
||||
"last_error" TEXT,
|
||||
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
COMMENT ON COLUMN "cron_jobs"."channel" IS 'IMESSAGE: imessage\nEMAIL: email';"""
|
||||
|
||||
|
||||
async def downgrade(db: BaseDBAsyncClient) -> str:
|
||||
return """
|
||||
DROP TABLE IF EXISTS "cron_jobs";"""
|
||||
|
||||
|
||||
MODELS_STATE = (
|
||||
"eJztXW1v27YW/iuCPnVAFuSlybbgYoCdqK3X2C5iZ1tXFwItMbEWidQoqalR9L9fkpZsia"
|
||||
"Jky69Syg9bapKHIh8ekec8PKS+6R62oRscX2P0BZIAhA5G+pX2TUfAg/Qf0vwjTQe+v8hl"
|
||||
"CSEYu1zASpXkOWAchARYIc18AG4AaZINA4s4fvwwFLkuS8QWLeigx0VShJz/ImiG+BGGE0"
|
||||
"hoxqfPNNlBNvwKg+Sn/2Q+ONC1M+12bPZsnm6GU5+n3d93bt7wkuxxY9PCbuShRWl/Gk4w"
|
||||
"mhePIsc+ZjIs7xEiSEAI7VQ3WCvjbidJsxbThJBEcN5Ue5FgwwcQuQwM/X8PEbIYBhp/Ev"
|
||||
"vf69/1CvBQqBm0DgoZFt++z3q16DNP1dmjrt+17l6dX/7Ee4mD8JHwTI6I/p0LghDMRDmu"
|
||||
"CyD53xyU1xNA5FAm5QUwaUPXgTFJWOC40KEEyASg9VDTPfDVdCF6DCf059nFRQmMf7buOJ"
|
||||
"K0FIcSU72eaX0vzjqb5TFIFxBaBLIumyDMA3lDc0LHg3Iws5ICpHYsepz8o6YA0z7YfeRO"
|
||||
"45egBN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LmFCGoYubUwBYyvclrN55W/TyjKKj43hQmW6B8mx9Fl1OROvG91wSjP/BY"
|
||||
"l9C8SdZR6QffaSHzXzxWpG7zSd2q56nUMar8kdNUyyrY1YKYsqqlVrVPsOdXom0XEk3R0X"
|
||||
"1jqjZy1EZOYzdy1vxqz0G+19OYU5QkQmu4t4KoCkmrQ9xw2eZDWQT4ZrsOh96V28HHbNTp"
|
||||
"YsWUKkJNMaU/3sBW/XbNLrmqFiSONdElVFWcc1TGVIFFmdrQVMX3/Kx4WicesEPGvG7njE"
|
||||
"QxK8W+WiSlU4qtmJSI8k4XYdj01agAYly8mQDu5IPGhXGDxd8zLI4b3Nu3DHdmD27tW4YH"
|
||||
"XV6+/x9WSMI2"
|
||||
)
|
||||
@@ -0,0 +1,131 @@
|
||||
from tortoise import BaseDBAsyncClient
|
||||
|
||||
RUN_IN_TRANSACTION = True
|
||||
|
||||
|
||||
async def upgrade(db: BaseDBAsyncClient) -> str:
|
||||
return """
|
||||
CREATE TABLE IF NOT EXISTS "cron_jobs" (
|
||||
"id" UUID NOT NULL PRIMARY KEY,
|
||||
"name" VARCHAR(255) NOT NULL,
|
||||
"description" TEXT,
|
||||
"prompt" TEXT NOT NULL,
|
||||
"recipient" VARCHAR(255) NOT NULL,
|
||||
"channel" VARCHAR(20) NOT NULL,
|
||||
"enabled" BOOL NOT NULL DEFAULT True,
|
||||
"last_run_at" TIMESTAMPTZ,
|
||||
"last_status" VARCHAR(50),
|
||||
"last_error" TEXT,
|
||||
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
COMMENT ON COLUMN "cron_jobs"."channel" IS 'IMESSAGE: imessage\nEMAIL: email';
|
||||
CREATE TABLE IF NOT EXISTS "beli_profiles" (
|
||||
"id" UUID NOT NULL PRIMARY KEY,
|
||||
"beli_email" VARCHAR(255) NOT NULL,
|
||||
"beli_password" TEXT NOT NULL,
|
||||
"beli_user_id" VARCHAR(255),
|
||||
"beli_refresh_token" TEXT,
|
||||
"last_refreshed_at" TIMESTAMPTZ,
|
||||
"last_error" TEXT,
|
||||
"enabled" BOOL NOT NULL DEFAULT True,
|
||||
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"user_id" UUID NOT NULL REFERENCES "users" ("id") ON DELETE CASCADE,
|
||||
CONSTRAINT "uid_beli_profil_user_id_f4bba0" UNIQUE ("user_id", "beli_email")
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS "beli_restaurants" (
|
||||
"id" UUID NOT NULL PRIMARY KEY,
|
||||
"beli_business_id" VARCHAR(255) NOT NULL,
|
||||
"name" VARCHAR(500) NOT NULL,
|
||||
"city" VARCHAR(255),
|
||||
"neighborhood" VARCHAR(255),
|
||||
"cuisine" JSONB NOT NULL,
|
||||
"price_range" VARCHAR(10),
|
||||
"status" VARCHAR(20) NOT NULL,
|
||||
"rank_index" INT,
|
||||
"score" DECIMAL(5,2),
|
||||
"notes" TEXT,
|
||||
"tags" JSONB NOT NULL,
|
||||
"fetched_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"profile_id" UUID NOT NULL REFERENCES "beli_profiles" ("id") ON DELETE CASCADE,
|
||||
CONSTRAINT "uid_beli_restau_profile_921fe6" UNIQUE ("profile_id", "beli_business_id", "status")
|
||||
);
|
||||
COMMENT ON COLUMN "beli_restaurants"."status" IS 'BEEN: been\nWANT_TO_TRY: want_to_try';"""
|
||||
|
||||
|
||||
async def downgrade(db: BaseDBAsyncClient) -> str:
|
||||
return """
|
||||
DROP TABLE IF EXISTS "beli_profiles";
|
||||
DROP TABLE IF EXISTS "cron_jobs";
|
||||
DROP TABLE I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
"KFCFpzis0d3ioxyaanIeSnk9OMOzx7zo07ybanCuGUFgPrQWR4xABNZWdiC3AUksNIFYkL"
|
||||
"5wHSNG0pn7pTMlt/AsXFDJLTzTjpUnl8mTy5p8cplHjk8KPbDz94YJM3W3bEwsZGs9tIsB"
|
||||
"poDUy0K3htdjOm3f1J67Ihjp7aahbyLo+7GyxaEzyfYdhe1jOqMSb8LIisJNHYA8+bnSAm"
|
||||
"v51KtuBpxc6JIP2MX1BXGUD6GE2IKZTx1v7jiVxjYrJxFd6mRokhkvD+a70XBQoJYrEQbH"
|
||||
"e4Rf8LNh6sGJYpl+8KWuoZ5ajqahaQUm8k/JDWtakQgW5TwPS+kwSxdpIJ/HaupQwyYKb/"
|
||||
"NdsSozYkJqcnuTybVdPLe2c1OreBus+RmXXVUdXClTCNEEfewMxtp4qI3vyP5cbMpiI1QL"
|
||||
"vIZsycUa+KjRF8yjXngMYFZoq8P/jqC7ez7+z9exJ86hrKBu2sDiA7eUYcmqSOg0Fm4kfi"
|
||||
"Xg3KjXvX7n9sXlyTnD4yY6+TKneMgJeEdPFkcdlgKCzJWHDjgE4IGDZ/H6n9SXi//2i/8M"
|
||||
"Bvp28dCspOSum8Vdy2DTs+jXfLApZvwqxiWyUjI0kSZOd4xOMHmejcVvbZAiqyNN2hXegd"
|
||||
"jXnLc4fHx85aSMhwerOo3ZEF78Rc0Nz8WPO3A3Mr0J7kgxh/4Vej5343IxJ5ESEZPurYVa"
|
||||
"I0OjAohxdTEBbG/El7dL+PI2hy8vOqGzhJssPKHzYO5Jbavu3hyRo27k+PF/70kbqw=="
|
||||
)
|
||||
@@ -0,0 +1,89 @@
|
||||
from tortoise import BaseDBAsyncClient
|
||||
|
||||
RUN_IN_TRANSACTION = True
|
||||
|
||||
|
||||
async def upgrade(db: BaseDBAsyncClient) -> str:
|
||||
return """
|
||||
ALTER TABLE "beli_restaurants" DROP CONSTRAINT IF EXISTS "uid_beli_restau_profile_921fe6";
|
||||
ALTER TABLE "beli_restaurants" ADD "friend_name" VARCHAR(255);
|
||||
ALTER TABLE "beli_restaurants" ADD "friend_beli_user_id" VARCHAR(255) NOT NULL DEFAULT '';
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "uid_beli_restau_profile_1e4068" ON "beli_restaurants" ("profile_id", "beli_business_id", "status", "friend_beli_user_id");"""
|
||||
|
||||
|
||||
async def downgrade(db: BaseDBAsyncClient) -> str:
|
||||
return """
|
||||
DROP INDEX IF EXISTS "uid_beli_restau_profile_1e4068";
|
||||
ALTER TABLE "beli_restaurants" DROP COLUMN "friend_name";
|
||||
ALTER TABLE "beli_restaurants" DROP COLUMN "friend_beli_user_id";
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "uid_beli_restau_profile_921fe6" ON "beli_restaurants" ("profile_id", "beli_business_id", "status");"""
|
||||
|
||||
|
||||
MODELS_STATE = (
|
||||
"eJztXe9zmzgT/lcYf+rN5DJx0vTuMu/cjJ3Q1m1sd2Lner26w8ggx1xAcPxo6un0f38lAT"
|
||||
"YIgY1tbJTqw11qoRXwaJF2n11J31u2Y0DLP7120Ffo+SAwHdS6Ur63ELAh/gf3+onSAq67"
|
||||
"ukoKAjC1qICeqkmvgKkfeEAP8MUZsHyIiwzo657pxjdDoWWRQkfHFU30sCoKkflfCLXAeY"
|
||||
"DBHHr4wucvuNhEBvwG/eSn+6jNTGgZmec2DXJvWq4FC5eW3d/3bl7TmuR2U013rNBGq9ru"
|
||||
"Ipg7aFk9DE3jlMiQaw8QQQ8E0Ei9BnnK+LWTouiJcUHghXD5qMaqwIAzEFoEjNb/ZiHSCQ"
|
||||
"YKvRP538s/WxXgwVATaE0UECy+/4jeavXOtLRFbnX9tnP34uLVL/QtHT948OhFikjrBxUE"
|
||||
"AYhEKa4rIOnfHJTXc+DxoUzqM2DiB90GxqRgheNKhxIgE4C2Q61lg2+aBdFDMMc/zy8vS2"
|
||||
"D8q3NHkcS1KJQO1utI6wfxpfPoGoF0BaHuQfLKGgjyQN7gK4FpQz6YWU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n5WUEZhmRWBkyPRZ9Gs+ZB"
|
||||
"rz1hWja1kpGWBL0/87xtiYbOXG4rc21JbVkSbtbdCB2NectzhRpfjKSVk0CazqNGZbg+Jz"
|
||||
"YTc83SHuwN1iP01wR4pDPl+h53OX3xdzEikRMeneWqg18mlUADGuLiaA7Y348nYJX97m8O"
|
||||
"VF+8yWcJOF+8wezD2pbdbdmyNy1OVIP/4PbwAAKw=="
|
||||
)
|
||||
@@ -0,0 +1,87 @@
|
||||
from tortoise import BaseDBAsyncClient
|
||||
|
||||
RUN_IN_TRANSACTION = True
|
||||
|
||||
|
||||
async def upgrade(db: BaseDBAsyncClient) -> str:
|
||||
return """
|
||||
DROP INDEX IF EXISTS "uid_beli_restau_profile_1e4068";
|
||||
ALTER TABLE "beli_restaurants" ADD "beli_category" VARCHAR(10) NOT NULL DEFAULT '';
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "uid_beli_restau_profile_93dc73" ON "beli_restaurants" ("profile_id", "beli_business_id", "status", "friend_beli_user_id", "beli_category");"""
|
||||
|
||||
|
||||
async def downgrade(db: BaseDBAsyncClient) -> str:
|
||||
return """
|
||||
DROP INDEX IF EXISTS "uid_beli_restau_profile_93dc73";
|
||||
ALTER TABLE "beli_restaurants" DROP COLUMN "beli_category";
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "uid_beli_restau_profile_1e4068" ON "beli_restaurants" ("profile_id", "beli_business_id", "status", "friend_beli_user_id");"""
|
||||
|
||||
|
||||
MODELS_STATE = (
|
||||
"eJztXW1v27YW/iuCP3VAFsRJ023BxQA7cVu3sV3EzrquLgRaomMtEqXppalR9L9fkpJsiS"
|
||||
"Jly69iyg9baoqHkh4ekec855D83nBcE9rB6bWLvkI/AKHlosaV9r2BgAPxP7jXT7QG8Lzl"
|
||||
"VVIQgolNBYxMTXoFTILQB0aIL06BHUBcZMLA8C0vuRmKbJsUugauaKGHZVGErP8iqIfuAw"
|
||||
"xn0McXPn/BxRYy4TcYpD+9R31qQdvMPb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CSJqb11Ue9UELs6Mwmrj9z3UqTDyunEF3oZGQRg6cI5rvhoC"
|
||||
"9Qy6UIg+M9wi/42bSM8ESzrSD8sreBcmmNTiLLDi0UnJIb7skgJViU08Es88tYrqSBYrq7"
|
||||
"ZUAdezK8NbpiVWbEpNTk3c9O8u3DwE/Mbnc6/SttAiEao4+t/kgfDfTRHVnGjz1e7KvqoV"
|
||||
"+TlftYAx91+oJF1IW7heaFNtoj9Ai6u+NdQgPD9Xk7g0LDcoDNB24hw3LasdBpIlxL/ErA"
|
||||
"uelcd3ut2xeXJ+dMuCfVyZdFQ9QNeTvUioOTCwFJxspDxyVD8MDBUzz/p/XV5L/55D+Fob"
|
||||
"FZ2kReUoW46hXiUjHpZ9GvxZh0EhioGL7MS6kIZja+smUQk0kHry1+K2OZeR2p0+YRLYh9"
|
||||
"zVmDE7ZLrpyUhevAsk5t9o0QH7y75vEZSQduF0urgzsiDqF9hX7A3d9AzElkROSke/dCrZ"
|
||||
"FPowKISXU5AWyuxZc3S/jyJocvF23kW8JNCjfyPZh7srdZd2eOyFHXe/34P9+ncVw="
|
||||
)
|
||||
@@ -32,6 +32,8 @@ dependencies = [
|
||||
"langchain-openai>=1.1.6",
|
||||
"langchain>=1.2.0",
|
||||
"langchain-community>=0.4.1",
|
||||
"langgraph-checkpoint-sqlite>=2.0.0",
|
||||
"aiosqlite>=0.20.0",
|
||||
"jq>=1.10.0",
|
||||
"tavily-python>=0.7.17",
|
||||
"ynab>=1.3.0",
|
||||
@@ -40,6 +42,11 @@ dependencies = [
|
||||
"ollama>=0.6.1",
|
||||
"twilio>=9.10.2",
|
||||
"aioboto3>=13.0.0",
|
||||
"prometheus-client>=0.26.0",
|
||||
"google-api-python-client>=2.0.0",
|
||||
"google-auth-oauthlib>=1.0.0",
|
||||
"google-auth-httplib2>=0.2.0",
|
||||
"langchain-mcp-adapters>=0.3.2",
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
"react": "^19.1.1",
|
||||
"react-dom": "^19.1.1",
|
||||
"react-markdown": "^10.1.0",
|
||||
"react-router-dom": "^7.18.3",
|
||||
"remark-gfm": "^4.0.1",
|
||||
"tailwind-merge": "^3.5.0",
|
||||
"watch": "^1.0.2"
|
||||
},
|
||||
|
||||
@@ -60,7 +60,7 @@ body {
|
||||
::-webkit-scrollbar-thumb { background: var(--color-sand); border-radius: 99px; }
|
||||
::-webkit-scrollbar-thumb:hover { background: var(--color-warm-gray); }
|
||||
|
||||
/* ── Markdown in answer bubbles ─────────────────────── */
|
||||
/* ── Markdown in assistant answers ──────────────────── */
|
||||
.markdown-content p { margin: 0.5em 0; line-height: 1.7; }
|
||||
.markdown-content p:first-child { margin-top: 0; }
|
||||
.markdown-content p:last-child { margin-bottom: 0; }
|
||||
@@ -117,6 +117,29 @@ body {
|
||||
.markdown-content strong { font-weight: 700; }
|
||||
.markdown-content em { font-style: italic; }
|
||||
|
||||
.markdown-content table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin: 0.75em 0;
|
||||
font-size: 0.9em;
|
||||
display: block;
|
||||
overflow-x: auto;
|
||||
}
|
||||
.markdown-content th,
|
||||
.markdown-content td {
|
||||
border: 1px solid var(--color-sand-light);
|
||||
padding: 0.4em 0.6em;
|
||||
text-align: left;
|
||||
vertical-align: top;
|
||||
}
|
||||
.markdown-content thead th {
|
||||
background: var(--color-sand-light);
|
||||
font-weight: 700;
|
||||
}
|
||||
.markdown-content tbody tr:nth-child(even) {
|
||||
background: color-mix(in srgb, var(--color-sand-light) 35%, transparent);
|
||||
}
|
||||
|
||||
/* ── Animations ─────────────────────────────────────── */
|
||||
@keyframes fadeSlideUp {
|
||||
from { opacity: 0; transform: translateY(10px); }
|
||||
@@ -126,6 +149,14 @@ body {
|
||||
animation: fadeSlideUp 0.3s ease-out forwards;
|
||||
}
|
||||
|
||||
@keyframes drawer-in {
|
||||
from { transform: translateX(-100%); }
|
||||
to { transform: translateX(0); }
|
||||
}
|
||||
.animate-drawer-in {
|
||||
animation: drawer-in 0.25s ease-out;
|
||||
}
|
||||
|
||||
@keyframes catPulse {
|
||||
0%, 80%, 100% { opacity: 0.25; transform: scale(0.75); }
|
||||
40% { opacity: 1; transform: scale(1); }
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { Route, Routes } from "react-router-dom";
|
||||
import "./App.css";
|
||||
import { AuthProvider } from "./contexts/AuthContext";
|
||||
import { ChatScreen } from "./components/ChatScreen";
|
||||
import { ConversationsPage } from "./components/ConversationsPage";
|
||||
import { LoginScreen } from "./components/LoginScreen";
|
||||
import { useAuthCheck } from "./hooks/useAuthCheck";
|
||||
import catIcon from "./assets/cat.png";
|
||||
@@ -23,14 +25,23 @@ const AppContainer = () => {
|
||||
);
|
||||
}
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return <LoginScreen setAuthenticated={setAuthenticated} />;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
{isAuthenticated ? (
|
||||
<ChatScreen setAuthenticated={setAuthenticated} isAdmin={isAdmin} />
|
||||
) : (
|
||||
<LoginScreen setAuthenticated={setAuthenticated} />
|
||||
)}
|
||||
</>
|
||||
<Routes>
|
||||
<Route path="/conversations" element={<ConversationsPage />} />
|
||||
{/* One ChatScreen instance serves both the home ("/") and per-conversation
|
||||
("/c/:id") routes. Using a single catch-all route keeps that instance
|
||||
mounted across the navigate() that fires when the first message creates
|
||||
a conversation — a remount here would abort the in-flight SSE stream
|
||||
(and drop the answer before it's persisted). */}
|
||||
<Route
|
||||
path="*"
|
||||
element={<ChatScreen setAuthenticated={setAuthenticated} isAdmin={isAdmin} />}
|
||||
/>
|
||||
</Routes>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -1,17 +1,20 @@
|
||||
import { userService } from "./userService";
|
||||
|
||||
export type SSEEvent =
|
||||
| { type: "thinking" }
|
||||
| { type: "thinking_delta"; delta: string }
|
||||
| { type: "tool_start"; tool: string }
|
||||
| { type: "tool_end"; tool: string }
|
||||
| { type: "content"; delta: string }
|
||||
| { type: "response"; message: string }
|
||||
| { type: "error"; message: string };
|
||||
| { type: "error"; message: string; stalled?: boolean };
|
||||
|
||||
export type SSEEventCallback = (event: SSEEvent) => void;
|
||||
|
||||
interface Message {
|
||||
id: string;
|
||||
text: string;
|
||||
speaker: "user" | "simba";
|
||||
speaker: "user" | "simba" | "tool";
|
||||
created_at: string;
|
||||
image_key?: string | null;
|
||||
}
|
||||
@@ -25,18 +28,10 @@ interface Conversation {
|
||||
user_id?: string;
|
||||
}
|
||||
|
||||
interface QueryRequest {
|
||||
query: string;
|
||||
}
|
||||
|
||||
interface QueryResponse {
|
||||
response: string;
|
||||
}
|
||||
|
||||
interface CreateConversationRequest {
|
||||
user_id: string;
|
||||
}
|
||||
|
||||
class ConversationService {
|
||||
private baseUrl = "/api";
|
||||
private conversationBaseUrl = "/api/conversation";
|
||||
@@ -107,9 +102,51 @@ class ConversationService {
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
async getAllConversations(): Promise<Conversation[]> {
|
||||
async renameConversation(
|
||||
conversationId: string,
|
||||
name: string,
|
||||
): Promise<Conversation> {
|
||||
const response = await userService.fetchWithRefreshToken(
|
||||
`${this.conversationBaseUrl}/`,
|
||||
`${this.conversationBaseUrl}/${conversationId}`,
|
||||
{
|
||||
method: "PATCH",
|
||||
body: JSON.stringify({ name }),
|
||||
},
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to rename conversation");
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
async deleteConversation(conversationId: string): Promise<void> {
|
||||
const response = await userService.fetchWithRefreshToken(
|
||||
`${this.conversationBaseUrl}/${conversationId}`,
|
||||
{
|
||||
method: "DELETE",
|
||||
},
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to delete conversation");
|
||||
}
|
||||
}
|
||||
|
||||
async getAllConversations(params?: {
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
search?: string;
|
||||
}): Promise<Conversation[]> {
|
||||
const search = new URLSearchParams();
|
||||
if (params?.limit != null) search.set("limit", String(params.limit));
|
||||
if (params?.offset != null) search.set("offset", String(params.offset));
|
||||
if (params?.search) search.set("search", params.search);
|
||||
const qs = search.toString();
|
||||
|
||||
const response = await userService.fetchWithRefreshToken(
|
||||
`${this.conversationBaseUrl}/${qs ? `?${qs}` : ""}`,
|
||||
{
|
||||
method: "GET",
|
||||
},
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import React from "react";
|
||||
import React, { useState } from "react";
|
||||
import ReactMarkdown from "react-markdown";
|
||||
import { cn } from "../lib/utils";
|
||||
import remarkGfm from "remark-gfm";
|
||||
import { Copy, Check } from "lucide-react";
|
||||
|
||||
type AnswerBubbleProps = {
|
||||
text: string;
|
||||
@@ -8,32 +9,41 @@ type AnswerBubbleProps = {
|
||||
};
|
||||
|
||||
export const AnswerBubble = React.memo(({ text, loading }: AnswerBubbleProps) => {
|
||||
return (
|
||||
<div className="flex justify-start message-enter">
|
||||
<div
|
||||
className={cn(
|
||||
"max-w-[78%] rounded-3xl rounded-bl-md",
|
||||
"bg-warm-white border border-sand-light/70",
|
||||
"shadow-sm shadow-sand/30",
|
||||
"overflow-hidden",
|
||||
)}
|
||||
>
|
||||
<div className="h-0.5 w-full bg-gradient-to-r from-amber-soft via-amber-glow/50 to-transparent" />
|
||||
const [copied, setCopied] = useState(false);
|
||||
|
||||
<div className="px-4 py-3">
|
||||
{loading ? (
|
||||
<div className="flex items-center gap-1.5 py-1 px-1">
|
||||
<span className="loading-dot w-2 h-2 rounded-full bg-amber-soft inline-block" />
|
||||
<span className="loading-dot w-2 h-2 rounded-full bg-amber-soft inline-block" />
|
||||
<span className="loading-dot w-2 h-2 rounded-full bg-amber-soft inline-block" />
|
||||
</div>
|
||||
) : (
|
||||
<div className="markdown-content text-sm leading-relaxed text-charcoal">
|
||||
<ReactMarkdown>{text}</ReactMarkdown>
|
||||
</div>
|
||||
)}
|
||||
const handleCopy = async (event: React.MouseEvent) => {
|
||||
event.stopPropagation();
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
} catch {
|
||||
// Clipboard unavailable (e.g. insecure context) — ignore
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="message-enter">
|
||||
{loading ? (
|
||||
<div className="flex items-center gap-1.5 py-1">
|
||||
<span className="loading-dot w-2 h-2 rounded-full bg-amber-soft inline-block" />
|
||||
<span className="loading-dot w-2 h-2 rounded-full bg-amber-soft inline-block" />
|
||||
<span className="loading-dot w-2 h-2 rounded-full bg-amber-soft inline-block" />
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="group relative -mx-3 -my-2 rounded-xl px-3 py-2 transition-colors duration-150 hover:bg-amber-soft/10">
|
||||
<div className="markdown-content text-[15px] leading-relaxed text-charcoal">
|
||||
<ReactMarkdown remarkPlugins={[remarkGfm]}>{text}</ReactMarkdown>
|
||||
</div>
|
||||
<button
|
||||
onClick={handleCopy}
|
||||
aria-label={copied ? "Copied" : "Copy message"}
|
||||
className="absolute top-0 right-0 flex h-7 w-7 items-center justify-center rounded-lg border border-sand-light/60 bg-warm-white text-warm-gray shadow-sm opacity-0 transition-opacity duration-150 group-hover:opacity-100 hover:text-charcoal hover:bg-cream-dark focus:opacity-100 focus:outline-none cursor-pointer"
|
||||
>
|
||||
{copied ? <Check size={14} className="text-green-600" /> : <Copy size={14} />}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { useCallback, useState, useRef } from "react";
|
||||
import { LogOut, Shield, Clock, PanelLeftClose, PanelLeftOpen, Menu, X } from "lucide-react";
|
||||
import React, { useCallback, useState, useRef } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { PanelLeftOpen, Menu, X } from "lucide-react";
|
||||
import { QuestionBubble } from "./QuestionBubble";
|
||||
import { AnswerBubble } from "./AnswerBubble";
|
||||
import { ToolBubble } from "./ToolBubble";
|
||||
import { ThinkingBubble } from "./ThinkingBubble";
|
||||
import { MessageInput } from "./MessageInput";
|
||||
import { ConversationList } from "./ConversationList";
|
||||
import { SidebarContent } from "./SidebarContent";
|
||||
import { AdminPanel } from "./AdminPanel";
|
||||
import { ScheduledMessagesPanel } from "./ScheduledMessagesPanel";
|
||||
import { cn } from "../lib/utils";
|
||||
@@ -18,6 +20,7 @@ type ChatScreenProps = {
|
||||
};
|
||||
|
||||
export const ChatScreen = ({ setAuthenticated, isAdmin }: ChatScreenProps) => {
|
||||
const navigate = useNavigate();
|
||||
const [query, setQuery] = useState("");
|
||||
const [simbaMode, setSimbaMode] = useState(false);
|
||||
const [showConversations, setShowConversations] = useState(false);
|
||||
@@ -39,8 +42,9 @@ export const ChatScreen = ({ setAuthenticated, isAdmin }: ChatScreenProps) => {
|
||||
const {
|
||||
conversations,
|
||||
selectedConversation,
|
||||
selectConversation,
|
||||
createConversation,
|
||||
renameConversation,
|
||||
deleteConversation,
|
||||
refreshConversations,
|
||||
} = useConversations();
|
||||
|
||||
@@ -48,13 +52,21 @@ export const ChatScreen = ({ setAuthenticated, isAdmin }: ChatScreenProps) => {
|
||||
|
||||
const {
|
||||
messages,
|
||||
setMessages,
|
||||
isLoading,
|
||||
streaming,
|
||||
thinking,
|
||||
messagesLoading,
|
||||
pendingImage,
|
||||
setPendingImage,
|
||||
sendMessage,
|
||||
stopGeneration,
|
||||
error,
|
||||
clearError,
|
||||
stalled,
|
||||
retryStalled,
|
||||
thinkingText,
|
||||
thinkingDurationMs,
|
||||
} = useChat({
|
||||
selectedConversation,
|
||||
createConversation,
|
||||
refreshConversations,
|
||||
onSessionExpired,
|
||||
@@ -65,18 +77,17 @@ export const ChatScreen = ({ setAuthenticated, isAdmin }: ChatScreenProps) => {
|
||||
isLoadingRef.current = isLoading;
|
||||
|
||||
const handleSelectConversation = useCallback(
|
||||
async (conversation: { title: string; id: string }) => {
|
||||
(conversation: { title: string; id: string }) => {
|
||||
setShowConversations(false);
|
||||
const loaded = await selectConversation(conversation);
|
||||
setMessages(loaded);
|
||||
navigate(`/c/${conversation.id}`);
|
||||
},
|
||||
[selectConversation, setMessages],
|
||||
[navigate],
|
||||
);
|
||||
|
||||
const handleCreateNewConversation = useCallback(async () => {
|
||||
await createConversation();
|
||||
setMessages([]);
|
||||
}, [createConversation, setMessages]);
|
||||
const handleCreateNewConversation = useCallback(() => {
|
||||
setShowConversations(false);
|
||||
navigate("/");
|
||||
}, [navigate]);
|
||||
|
||||
const handleQuestionSubmit = useCallback(() => {
|
||||
sendMessage(query, simbaMode);
|
||||
@@ -87,14 +98,20 @@ export const ChatScreen = ({ setAuthenticated, isAdmin }: ChatScreenProps) => {
|
||||
setQuery(event.target.value);
|
||||
}, []);
|
||||
|
||||
const handleKeyDown = useCallback((event: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||
const kev = event as unknown as React.KeyboardEvent<HTMLTextAreaElement>;
|
||||
if (kev.key === "Enter" && !kev.shiftKey) {
|
||||
kev.preventDefault();
|
||||
const handleKeyDown = useCallback((event: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
||||
if (event.key === "Enter" && !event.shiftKey) {
|
||||
event.preventDefault();
|
||||
handleQuestionSubmit();
|
||||
}
|
||||
}, [handleQuestionSubmit]);
|
||||
|
||||
const handleToggleSimbaMode = useCallback(() => setSimbaMode((v) => !v), []);
|
||||
|
||||
const handleDeleteConversation = useCallback(
|
||||
(id: string) => deleteConversation(id),
|
||||
[deleteConversation],
|
||||
);
|
||||
|
||||
const handleImageSelect = useCallback((file: File) => setPendingImage(file), [setPendingImage]);
|
||||
const handleClearImage = useCallback(() => setPendingImage(null), [setPendingImage]);
|
||||
|
||||
@@ -104,6 +121,16 @@ export const ChatScreen = ({ setAuthenticated, isAdmin }: ChatScreenProps) => {
|
||||
setAuthenticated(false);
|
||||
};
|
||||
|
||||
const sidebarProps = {
|
||||
conversations,
|
||||
onCreateNewConversation: handleCreateNewConversation,
|
||||
onSelectConversation: handleSelectConversation,
|
||||
onRenameConversation: renameConversation,
|
||||
onDeleteConversation: handleDeleteConversation,
|
||||
selectedId: selectedConversation?.id,
|
||||
isAdmin,
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="h-screen h-[100dvh] flex flex-row bg-cream overflow-hidden">
|
||||
{/* Desktop Sidebar */}
|
||||
@@ -129,65 +156,41 @@ export const ChatScreen = ({ setAuthenticated, isAdmin }: ChatScreenProps) => {
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex flex-col h-full">
|
||||
<div className="flex items-center justify-between px-4 py-4 border-b border-white/8">
|
||||
<div className="flex items-center gap-2.5">
|
||||
<img src={catIcon} alt="Simba" className="w-12 h-12" />
|
||||
<h2
|
||||
className="text-lg font-bold text-cream tracking-tight"
|
||||
style={{ fontFamily: "var(--font-display)" }}
|
||||
>
|
||||
asksimba
|
||||
</h2>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => setSidebarCollapsed(true)}
|
||||
className="w-7 h-7 rounded-lg flex items-center justify-center text-cream/40 hover:text-cream hover:bg-white/10 transition-all cursor-pointer"
|
||||
>
|
||||
<PanelLeftClose size={15} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 overflow-y-auto px-2 py-3">
|
||||
<ConversationList
|
||||
conversations={conversations}
|
||||
onCreateNewConversation={handleCreateNewConversation}
|
||||
onSelectConversation={handleSelectConversation}
|
||||
selectedId={selectedConversation?.id}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="px-2 pb-3 pt-2 border-t border-white/8 flex flex-col gap-0.5">
|
||||
{isAdmin && (
|
||||
<>
|
||||
<button
|
||||
onClick={() => setShowAdminPanel(true)}
|
||||
className="flex items-center gap-2 w-full px-3 py-2 rounded-xl text-sm text-cream/50 hover:text-cream hover:bg-white/8 transition-all cursor-pointer"
|
||||
>
|
||||
<Shield size={14} />
|
||||
<span>Admin</span>
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setShowScheduler(true)}
|
||||
className="flex items-center gap-2 w-full px-3 py-2 rounded-xl text-sm text-cream/50 hover:text-cream hover:bg-white/8 transition-all cursor-pointer"
|
||||
>
|
||||
<Clock size={14} />
|
||||
<span>Scheduler</span>
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
<button
|
||||
onClick={handleLogout}
|
||||
className="flex items-center gap-2 w-full px-3 py-2 rounded-xl text-sm text-cream/50 hover:text-cream hover:bg-white/8 transition-all cursor-pointer"
|
||||
>
|
||||
<LogOut size={14} />
|
||||
<span>Sign out</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<SidebarContent
|
||||
{...sidebarProps}
|
||||
onShowAdmin={() => setShowAdminPanel(true)}
|
||||
onShowScheduler={() => setShowScheduler(true)}
|
||||
onLogout={handleLogout}
|
||||
onCollapse={() => setSidebarCollapsed(true)}
|
||||
/>
|
||||
)}
|
||||
</aside>
|
||||
|
||||
{/* Mobile drawer */}
|
||||
{showConversations && (
|
||||
<div className="md:hidden fixed inset-0 z-50 flex">
|
||||
<aside className="w-72 max-w-[80%] h-full bg-sidebar-bg shadow-xl animate-drawer-in">
|
||||
<SidebarContent
|
||||
{...sidebarProps}
|
||||
onShowAdmin={() => {
|
||||
setShowConversations(false);
|
||||
setShowAdminPanel(true);
|
||||
}}
|
||||
onShowScheduler={() => {
|
||||
setShowConversations(false);
|
||||
setShowScheduler(true);
|
||||
}}
|
||||
onLogout={handleLogout}
|
||||
onCloseDrawer={() => setShowConversations(false)}
|
||||
/>
|
||||
</aside>
|
||||
<div
|
||||
className="flex-1 bg-charcoal/40 backdrop-blur-sm"
|
||||
onClick={() => setShowConversations(false)}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{showAdminPanel && <AdminPanel onClose={() => setShowAdminPanel(false)} />}
|
||||
{showScheduler && <ScheduledMessagesPanel onClose={() => setShowScheduler(false)} />}
|
||||
|
||||
@@ -202,35 +205,17 @@ export const ChatScreen = ({ setAuthenticated, isAdmin }: ChatScreenProps) => {
|
||||
asksimba
|
||||
</h1>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
className="w-8 h-8 rounded-xl flex items-center justify-center text-warm-gray hover:text-charcoal hover:bg-cream-dark transition-all cursor-pointer"
|
||||
onClick={() => setShowConversations((v) => !v)}
|
||||
>
|
||||
{showConversations ? <X size={16} /> : <Menu size={16} />}
|
||||
</button>
|
||||
<button
|
||||
className="w-8 h-8 rounded-xl flex items-center justify-center text-warm-gray hover:text-charcoal hover:bg-cream-dark transition-all cursor-pointer"
|
||||
onClick={handleLogout}
|
||||
>
|
||||
<LogOut size={15} />
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
className="w-8 h-8 rounded-xl flex items-center justify-center text-warm-gray hover:text-charcoal hover:bg-cream-dark transition-all cursor-pointer"
|
||||
onClick={() => setShowConversations(true)}
|
||||
aria-label="Open menu"
|
||||
>
|
||||
<Menu size={16} />
|
||||
</button>
|
||||
</header>
|
||||
|
||||
{messages.length === 0 ? (
|
||||
{messages.length === 0 && !messagesLoading ? (
|
||||
<div className="flex-1 flex flex-col items-center justify-center px-4 gap-6">
|
||||
{showConversations && (
|
||||
<div className="md:hidden w-full max-w-2xl bg-warm-white rounded-2xl border border-sand-light p-3 shadow-sm">
|
||||
<ConversationList
|
||||
conversations={conversations}
|
||||
onCreateNewConversation={handleCreateNewConversation}
|
||||
onSelectConversation={handleSelectConversation}
|
||||
selectedId={selectedConversation?.id}
|
||||
variant="light"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<div className="relative">
|
||||
<div className="absolute -inset-6 bg-amber-soft/20 rounded-full blur-3xl" />
|
||||
<img src={catIcon} alt="Simba" className="relative w-36 h-36" />
|
||||
@@ -247,52 +232,113 @@ export const ChatScreen = ({ setAuthenticated, isAdmin }: ChatScreenProps) => {
|
||||
handleQueryChange={handleQueryChange}
|
||||
handleKeyDown={handleKeyDown}
|
||||
handleQuestionSubmit={handleQuestionSubmit}
|
||||
setSimbaMode={setSimbaMode}
|
||||
simbaMode={simbaMode}
|
||||
onToggleSimbaMode={handleToggleSimbaMode}
|
||||
isLoading={isLoading}
|
||||
onStop={stopGeneration}
|
||||
pendingImage={pendingImage}
|
||||
onImageSelect={handleImageSelect}
|
||||
onClearImage={handleClearImage}
|
||||
autoFocus
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<div className="flex-1 overflow-y-auto px-4 py-6">
|
||||
<div className="max-w-2xl mx-auto flex flex-col gap-3">
|
||||
{showConversations && (
|
||||
<div className="md:hidden mb-3 bg-warm-white rounded-2xl border border-sand-light p-3 shadow-sm">
|
||||
<ConversationList
|
||||
conversations={conversations}
|
||||
onCreateNewConversation={handleCreateNewConversation}
|
||||
onSelectConversation={handleSelectConversation}
|
||||
selectedId={selectedConversation?.id}
|
||||
variant="light"
|
||||
/>
|
||||
<div className="max-w-3xl mx-auto flex flex-col gap-6">
|
||||
{messages.map((msg, index) => {
|
||||
// When the model has been thinking and content is now streaming,
|
||||
// render ThinkingBubble just before the streaming simba message
|
||||
// so it appears above the answer rather than below it.
|
||||
const showThinkingHere =
|
||||
streaming &&
|
||||
isLoading &&
|
||||
(thinking || thinkingText || thinkingDurationMs !== null) &&
|
||||
index === messages.length - 1 &&
|
||||
msg.speaker === "simba";
|
||||
|
||||
const bubble =
|
||||
msg.speaker === "tool" ? (
|
||||
<ToolBubble key={index} text={msg.text} />
|
||||
) : msg.speaker === "simba" ? (
|
||||
<AnswerBubble key={index} text={msg.text} />
|
||||
) : (
|
||||
<QuestionBubble key={index} text={msg.text} image_key={msg.image_key} />
|
||||
);
|
||||
|
||||
if (showThinkingHere) {
|
||||
return (
|
||||
<React.Fragment key={index}>
|
||||
<ThinkingBubble
|
||||
text={thinkingText}
|
||||
durationMs={thinkingDurationMs}
|
||||
active={thinking}
|
||||
/>
|
||||
{bubble}
|
||||
</React.Fragment>
|
||||
);
|
||||
}
|
||||
return bubble;
|
||||
})}
|
||||
|
||||
{/* ThinkingBubble before content arrives (no streaming message yet) */}
|
||||
{isLoading && !streaming && (thinking || thinkingText || thinkingDurationMs !== null) && (
|
||||
<ThinkingBubble
|
||||
text={thinkingText}
|
||||
durationMs={thinkingDurationMs}
|
||||
active={thinking}
|
||||
/>
|
||||
)}
|
||||
|
||||
{((isLoading && !streaming && !thinking) || messagesLoading) && (
|
||||
<AnswerBubble text="" loading={true} />
|
||||
)}
|
||||
|
||||
{error && (
|
||||
<div className="flex justify-center message-enter">
|
||||
<div className="inline-flex items-center gap-2 px-3 py-1.5 rounded-full bg-red-50 border border-red-200 text-xs text-red-600">
|
||||
<span>{error}</span>
|
||||
<button
|
||||
onClick={clearError}
|
||||
className="text-red-400 hover:text-red-600 transition-colors cursor-pointer"
|
||||
aria-label="Dismiss error"
|
||||
>
|
||||
<X size={13} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{messages.map((msg, index) => {
|
||||
if (msg.speaker === "tool")
|
||||
return <ToolBubble key={index} text={msg.text} />;
|
||||
if (msg.speaker === "simba")
|
||||
return <AnswerBubble key={index} text={msg.text} />;
|
||||
return <QuestionBubble key={index} text={msg.text} image_key={msg.image_key} />;
|
||||
})}
|
||||
{stalled && (
|
||||
<div className="flex justify-center message-enter">
|
||||
<div className="inline-flex items-center gap-2 px-3 py-1.5 rounded-full bg-red-50 border border-red-200 text-xs text-red-600">
|
||||
<span>Simba didn't respond in time.</span>
|
||||
<button
|
||||
onClick={retryStalled}
|
||||
className="font-medium underline underline-offset-2 hover:text-red-800 transition-colors cursor-pointer"
|
||||
>
|
||||
Retry
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isLoading && <AnswerBubble text="" loading={true} />}
|
||||
<div ref={messagesEndRef} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<footer className="border-t border-sand-light/40 bg-cream">
|
||||
<div className="max-w-2xl mx-auto px-4 py-3">
|
||||
<div className="max-w-3xl mx-auto px-4 py-3">
|
||||
<MessageInput
|
||||
query={query}
|
||||
handleQueryChange={handleQueryChange}
|
||||
handleKeyDown={handleKeyDown}
|
||||
handleQuestionSubmit={handleQuestionSubmit}
|
||||
setSimbaMode={setSimbaMode}
|
||||
simbaMode={simbaMode}
|
||||
onToggleSimbaMode={handleToggleSimbaMode}
|
||||
isLoading={isLoading}
|
||||
onStop={stopGeneration}
|
||||
pendingImage={pendingImage}
|
||||
onImageSelect={handleImageSelect}
|
||||
onClearImage={handleClearImage}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Plus } from "lucide-react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { Check, MoreHorizontal, Pencil, Plus, Trash2, X } from "lucide-react";
|
||||
import { cn } from "../lib/utils";
|
||||
|
||||
type Conversation = {
|
||||
@@ -10,6 +11,8 @@ type ConversationProps = {
|
||||
conversations: Conversation[];
|
||||
onSelectConversation: (conversation: Conversation) => void;
|
||||
onCreateNewConversation: () => void;
|
||||
onRenameConversation?: (id: string, title: string) => void;
|
||||
onDeleteConversation?: (id: string) => void;
|
||||
selectedId?: string;
|
||||
variant?: "dark" | "light";
|
||||
};
|
||||
@@ -18,9 +21,49 @@ export const ConversationList = ({
|
||||
conversations,
|
||||
onSelectConversation,
|
||||
onCreateNewConversation,
|
||||
onRenameConversation,
|
||||
onDeleteConversation,
|
||||
selectedId,
|
||||
variant = "dark",
|
||||
}: ConversationProps) => {
|
||||
const [menuOpenId, setMenuOpenId] = useState<string | null>(null);
|
||||
const [editingId, setEditingId] = useState<string | null>(null);
|
||||
const [editValue, setEditValue] = useState("");
|
||||
const [confirmDeleteId, setConfirmDeleteId] = useState<string | null>(null);
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const isDark = variant === "dark";
|
||||
|
||||
// Close any open row menu when clicking elsewhere.
|
||||
useEffect(() => {
|
||||
if (!menuOpenId && !confirmDeleteId) return;
|
||||
const close = () => {
|
||||
setMenuOpenId(null);
|
||||
setConfirmDeleteId(null);
|
||||
};
|
||||
window.addEventListener("click", close);
|
||||
return () => window.removeEventListener("click", close);
|
||||
}, [menuOpenId, confirmDeleteId]);
|
||||
|
||||
useEffect(() => {
|
||||
if (editingId) inputRef.current?.focus();
|
||||
}, [editingId]);
|
||||
|
||||
const startRename = (conv: Conversation) => {
|
||||
setMenuOpenId(null);
|
||||
setEditingId(conv.id);
|
||||
setEditValue(conv.title);
|
||||
};
|
||||
|
||||
const commitRename = () => {
|
||||
if (editingId && editValue.trim()) {
|
||||
onRenameConversation?.(editingId, editValue.trim());
|
||||
}
|
||||
setEditingId(null);
|
||||
};
|
||||
|
||||
const hasActions = Boolean(onRenameConversation || onDeleteConversation);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-1">
|
||||
<button
|
||||
@@ -28,7 +71,7 @@ export const ConversationList = ({
|
||||
className={cn(
|
||||
"flex items-center gap-2 w-full px-3 py-2 rounded-xl",
|
||||
"text-sm transition-all duration-150 cursor-pointer mb-1",
|
||||
variant === "dark"
|
||||
isDark
|
||||
? "text-cream/60 hover:text-cream hover:bg-white/8"
|
||||
: "text-warm-gray hover:text-charcoal hover:bg-cream-dark",
|
||||
)}
|
||||
@@ -39,24 +82,148 @@ export const ConversationList = ({
|
||||
|
||||
{conversations.map((conv) => {
|
||||
const isActive = conv.id === selectedId;
|
||||
const isEditing = editingId === conv.id;
|
||||
|
||||
if (isEditing) {
|
||||
return (
|
||||
<div
|
||||
key={conv.id}
|
||||
className={cn(
|
||||
"flex items-center gap-1 px-1.5 py-1 rounded-xl",
|
||||
isDark ? "bg-white/12" : "bg-cream-dark",
|
||||
)}
|
||||
>
|
||||
<input
|
||||
ref={inputRef}
|
||||
value={editValue}
|
||||
onChange={(e) => setEditValue(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter") commitRename();
|
||||
if (e.key === "Escape") setEditingId(null);
|
||||
}}
|
||||
className={cn(
|
||||
"flex-1 min-w-0 bg-transparent px-1.5 py-1 text-sm rounded-lg",
|
||||
"focus:outline-none",
|
||||
isDark
|
||||
? "text-cream placeholder:text-cream/40"
|
||||
: "text-charcoal placeholder:text-warm-gray/50",
|
||||
)}
|
||||
/>
|
||||
<button
|
||||
onClick={commitRename}
|
||||
className={cn(
|
||||
"w-6 h-6 rounded-lg flex items-center justify-center shrink-0 cursor-pointer transition-colors",
|
||||
isDark
|
||||
? "text-cream/60 hover:text-cream hover:bg-white/10"
|
||||
: "text-warm-gray hover:text-charcoal hover:bg-sand-light",
|
||||
)}
|
||||
>
|
||||
<Check size={14} />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setEditingId(null)}
|
||||
className={cn(
|
||||
"w-6 h-6 rounded-lg flex items-center justify-center shrink-0 cursor-pointer transition-colors",
|
||||
isDark
|
||||
? "text-cream/60 hover:text-cream hover:bg-white/10"
|
||||
: "text-warm-gray hover:text-charcoal hover:bg-sand-light",
|
||||
)}
|
||||
>
|
||||
<X size={14} />
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
key={conv.id}
|
||||
onClick={() => onSelectConversation(conv)}
|
||||
className={cn(
|
||||
"w-full px-3 py-2 rounded-xl text-left",
|
||||
"text-sm truncate transition-all duration-150 cursor-pointer",
|
||||
variant === "dark"
|
||||
? isActive
|
||||
? "bg-white/12 text-cream font-medium"
|
||||
: "text-cream/60 hover:text-cream hover:bg-white/8"
|
||||
: isActive
|
||||
? "bg-cream-dark text-charcoal font-medium"
|
||||
: "text-warm-gray hover:text-charcoal hover:bg-cream-dark",
|
||||
<div key={conv.id} className="group relative flex items-center">
|
||||
<button
|
||||
onClick={() => onSelectConversation(conv)}
|
||||
className={cn(
|
||||
"flex-1 min-w-0 px-3 py-2 rounded-xl text-left",
|
||||
"text-sm truncate transition-all duration-150 cursor-pointer",
|
||||
hasActions && "pr-8",
|
||||
isDark
|
||||
? isActive
|
||||
? "bg-white/12 text-cream font-medium"
|
||||
: "text-cream/60 hover:text-cream hover:bg-white/8"
|
||||
: isActive
|
||||
? "bg-cream-dark text-charcoal font-medium"
|
||||
: "text-warm-gray hover:text-charcoal hover:bg-cream-dark",
|
||||
)}
|
||||
>
|
||||
{conv.title}
|
||||
</button>
|
||||
|
||||
{hasActions && (
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
setConfirmDeleteId(null);
|
||||
setMenuOpenId((prev) => (prev === conv.id ? null : conv.id));
|
||||
}}
|
||||
className={cn(
|
||||
"absolute right-1.5 w-6 h-6 rounded-lg flex items-center justify-center cursor-pointer transition-all",
|
||||
"opacity-0 group-hover:opacity-100 focus:opacity-100",
|
||||
menuOpenId === conv.id && "opacity-100",
|
||||
isDark
|
||||
? "text-cream/50 hover:text-cream hover:bg-white/10"
|
||||
: "text-warm-gray hover:text-charcoal hover:bg-sand-light",
|
||||
)}
|
||||
>
|
||||
<MoreHorizontal size={15} />
|
||||
</button>
|
||||
)}
|
||||
>
|
||||
{conv.title}
|
||||
</button>
|
||||
|
||||
{menuOpenId === conv.id && (
|
||||
<div
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
className={cn(
|
||||
"absolute right-1.5 top-9 z-20 w-36 rounded-xl p-1 shadow-lg",
|
||||
isDark
|
||||
? "bg-sidebar-bg border border-white/10"
|
||||
: "bg-warm-white border border-sand-light",
|
||||
)}
|
||||
>
|
||||
{onRenameConversation && (
|
||||
<button
|
||||
onClick={() => startRename(conv)}
|
||||
className={cn(
|
||||
"flex items-center gap-2 w-full px-2.5 py-1.5 rounded-lg text-sm cursor-pointer transition-colors",
|
||||
isDark
|
||||
? "text-cream/70 hover:text-cream hover:bg-white/10"
|
||||
: "text-warm-gray hover:text-charcoal hover:bg-cream-dark",
|
||||
)}
|
||||
>
|
||||
<Pencil size={13} />
|
||||
<span>Rename</span>
|
||||
</button>
|
||||
)}
|
||||
{onDeleteConversation && (
|
||||
<button
|
||||
onClick={() => {
|
||||
if (confirmDeleteId === conv.id) {
|
||||
onDeleteConversation(conv.id);
|
||||
setMenuOpenId(null);
|
||||
setConfirmDeleteId(null);
|
||||
} else {
|
||||
setConfirmDeleteId(conv.id);
|
||||
}
|
||||
}}
|
||||
className={cn(
|
||||
"flex items-center gap-2 w-full px-2.5 py-1.5 rounded-lg text-sm cursor-pointer transition-colors",
|
||||
"text-red-400 hover:bg-red-500/10",
|
||||
)}
|
||||
>
|
||||
<Trash2 size={13} />
|
||||
<span>
|
||||
{confirmDeleteId === conv.id ? "Confirm delete" : "Delete"}
|
||||
</span>
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
type Conversation = {
|
||||
title: string;
|
||||
id: string;
|
||||
};
|
||||
|
||||
type ConversationMenuProps = {
|
||||
conversations: Conversation[];
|
||||
};
|
||||
|
||||
export const ConversationMenu = ({ conversations }: ConversationMenuProps) => {
|
||||
return (
|
||||
<div className="absolute bg-white w-md rounded-md shadow-xl m-4 p-4">
|
||||
<p className="py-2 px-4 rounded-md w-full text-xl font-bold">askSimba!</p>
|
||||
{conversations.map((conversation) => (
|
||||
<p
|
||||
key={conversation.id}
|
||||
className="py-2 px-4 rounded-md hover:bg-stone-200 w-full text-xl font-bold cursor-pointer"
|
||||
>
|
||||
{conversation.title}
|
||||
</p>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,94 @@
|
||||
import { useCallback } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { ArrowLeft, Search } from "lucide-react";
|
||||
import { ConversationList } from "./ConversationList";
|
||||
import { useConversationBrowser } from "../hooks/useConversationBrowser";
|
||||
import type { Conversation } from "../hooks/useConversations";
|
||||
import { Input } from "./ui/input";
|
||||
import catIcon from "../assets/cat.png";
|
||||
|
||||
export const ConversationsPage = () => {
|
||||
const navigate = useNavigate();
|
||||
const {
|
||||
items,
|
||||
search,
|
||||
setSearch,
|
||||
hasMore,
|
||||
loading,
|
||||
loadMore,
|
||||
renameConversation,
|
||||
deleteConversation,
|
||||
} = useConversationBrowser();
|
||||
|
||||
const handleSelect = useCallback(
|
||||
(conversation: Conversation) => navigate(`/c/${conversation.id}`),
|
||||
[navigate],
|
||||
);
|
||||
|
||||
const handleCreateNew = useCallback(() => navigate("/"), [navigate]);
|
||||
|
||||
return (
|
||||
<div className="h-screen h-[100dvh] flex flex-col bg-cream overflow-hidden">
|
||||
<header className="flex items-center gap-3 px-4 py-3 bg-warm-white border-b border-sand-light/60">
|
||||
<button
|
||||
onClick={() => navigate(-1)}
|
||||
className="w-8 h-8 rounded-xl flex items-center justify-center text-warm-gray hover:text-charcoal hover:bg-cream-dark transition-all cursor-pointer"
|
||||
aria-label="Back"
|
||||
>
|
||||
<ArrowLeft size={16} />
|
||||
</button>
|
||||
<img src={catIcon} alt="Simba" className="w-10 h-10" />
|
||||
<h1
|
||||
className="text-base font-bold text-charcoal"
|
||||
style={{ fontFamily: "var(--font-display)" }}
|
||||
>
|
||||
All conversations
|
||||
</h1>
|
||||
</header>
|
||||
|
||||
<div className="flex-1 overflow-y-auto">
|
||||
<div className="max-w-2xl mx-auto px-4 py-6 flex flex-col gap-4">
|
||||
<div className="relative">
|
||||
<Search
|
||||
size={15}
|
||||
className="absolute left-3 top-1/2 -translate-y-1/2 text-warm-gray/60"
|
||||
/>
|
||||
<Input
|
||||
value={search}
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
placeholder="Search conversations..."
|
||||
className="h-10 pl-9"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="bg-warm-white rounded-2xl border border-sand-light p-3 shadow-sm">
|
||||
<ConversationList
|
||||
conversations={items}
|
||||
onCreateNewConversation={handleCreateNew}
|
||||
onSelectConversation={handleSelect}
|
||||
onRenameConversation={renameConversation}
|
||||
onDeleteConversation={deleteConversation}
|
||||
variant="light"
|
||||
/>
|
||||
|
||||
{items.length === 0 && !loading && (
|
||||
<p className="text-center text-sm text-warm-gray/70 py-6">
|
||||
{search ? "No conversations match your search." : "No conversations yet."}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{hasMore && (
|
||||
<button
|
||||
onClick={loadMore}
|
||||
disabled={loading}
|
||||
className="self-center px-4 py-2 rounded-xl text-sm text-warm-gray hover:text-charcoal hover:bg-cream-dark transition-all cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
{loading ? "Loading..." : "Load more"}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -1,160 +1,204 @@
|
||||
import React, { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { ArrowUp, ImagePlus, X } from "lucide-react";
|
||||
import React, { useEffect, useMemo, useRef } from "react";
|
||||
import { ArrowUp, ImagePlus, Square, X } from "lucide-react";
|
||||
import { cn } from "../lib/utils";
|
||||
import { Textarea } from "./ui/textarea";
|
||||
|
||||
type MessageInputProps = {
|
||||
handleQueryChange: (event: React.ChangeEvent<HTMLTextAreaElement>) => void;
|
||||
handleKeyDown: (event: React.ChangeEvent<HTMLTextAreaElement>) => void;
|
||||
handleKeyDown: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void;
|
||||
handleQuestionSubmit: () => void;
|
||||
setSimbaMode: (val: boolean) => void;
|
||||
simbaMode: boolean;
|
||||
onToggleSimbaMode: () => void;
|
||||
query: string;
|
||||
isLoading: boolean;
|
||||
onStop: () => void;
|
||||
pendingImage: File | null;
|
||||
onImageSelect: (file: File) => void;
|
||||
onClearImage: () => void;
|
||||
autoFocus?: boolean;
|
||||
};
|
||||
|
||||
export const MessageInput = React.memo(({
|
||||
query,
|
||||
handleKeyDown,
|
||||
handleQueryChange,
|
||||
handleQuestionSubmit,
|
||||
setSimbaMode,
|
||||
isLoading,
|
||||
pendingImage,
|
||||
onImageSelect,
|
||||
onClearImage,
|
||||
}: MessageInputProps) => {
|
||||
const [simbaMode, setLocalSimbaMode] = useState(false);
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
export const MessageInput = React.memo(
|
||||
({
|
||||
query,
|
||||
handleKeyDown,
|
||||
handleQueryChange,
|
||||
handleQuestionSubmit,
|
||||
simbaMode,
|
||||
onToggleSimbaMode,
|
||||
isLoading,
|
||||
onStop,
|
||||
pendingImage,
|
||||
onImageSelect,
|
||||
onClearImage,
|
||||
autoFocus,
|
||||
}: MessageInputProps) => {
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||
|
||||
// Create blob URL once per file, revoke on cleanup
|
||||
const previewUrl = useMemo(
|
||||
() => (pendingImage ? URL.createObjectURL(pendingImage) : null),
|
||||
[pendingImage],
|
||||
);
|
||||
// Create blob URL once per file, revoke on cleanup
|
||||
const previewUrl = useMemo(
|
||||
() => (pendingImage ? URL.createObjectURL(pendingImage) : null),
|
||||
[pendingImage],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (previewUrl) URL.revokeObjectURL(previewUrl);
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (previewUrl) URL.revokeObjectURL(previewUrl);
|
||||
};
|
||||
}, [previewUrl]);
|
||||
|
||||
// Auto-grow the textarea to fit its content (capped by max-height in CSS).
|
||||
useEffect(() => {
|
||||
const el = textareaRef.current;
|
||||
if (!el) return;
|
||||
el.style.height = "auto";
|
||||
el.style.height = `${el.scrollHeight}px`;
|
||||
}, [query]);
|
||||
|
||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (file) {
|
||||
onImageSelect(file);
|
||||
}
|
||||
// Reset so the same file can be re-selected
|
||||
e.target.value = "";
|
||||
};
|
||||
}, [previewUrl]);
|
||||
|
||||
const toggleSimbaMode = () => {
|
||||
const next = !simbaMode;
|
||||
setLocalSimbaMode(next);
|
||||
setSimbaMode(next);
|
||||
};
|
||||
const handlePaste = (e: React.ClipboardEvent<HTMLTextAreaElement>) => {
|
||||
if (isLoading) return;
|
||||
const items = e.clipboardData?.items;
|
||||
if (!items) return;
|
||||
for (const item of items) {
|
||||
if (item.type.startsWith("image/")) {
|
||||
const file = item.getAsFile();
|
||||
if (file) {
|
||||
e.preventDefault();
|
||||
onImageSelect(file);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (file) {
|
||||
onImageSelect(file);
|
||||
}
|
||||
// Reset so the same file can be re-selected
|
||||
e.target.value = "";
|
||||
};
|
||||
const canSend = !isLoading && Boolean(query.trim() || pendingImage);
|
||||
|
||||
const canSend = !isLoading && (query.trim() || pendingImage);
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
"rounded-2xl bg-warm-white border border-sand shadow-md shadow-sand/30",
|
||||
"transition-shadow duration-200 focus-within:shadow-lg focus-within:shadow-amber-soft/20",
|
||||
"focus-within:border-amber-soft/60",
|
||||
)}
|
||||
>
|
||||
{/* Image preview */}
|
||||
{pendingImage && (
|
||||
<div className="px-3 pt-3">
|
||||
<div className="relative inline-block">
|
||||
<img
|
||||
src={previewUrl!}
|
||||
alt="Pending upload"
|
||||
className="h-20 rounded-lg object-cover border border-sand"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClearImage}
|
||||
className="absolute -top-1.5 -right-1.5 w-5 h-5 rounded-full bg-charcoal text-white flex items-center justify-center hover:bg-charcoal/80 transition-colors cursor-pointer"
|
||||
>
|
||||
<X size={12} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
"rounded-2xl bg-warm-white border border-sand shadow-md shadow-sand/30",
|
||||
"transition-shadow duration-200 focus-within:shadow-lg focus-within:shadow-amber-soft/20",
|
||||
"focus-within:border-amber-soft/60",
|
||||
)}
|
||||
>
|
||||
{/* Image preview */}
|
||||
{pendingImage && (
|
||||
<div className="px-3 pt-3">
|
||||
<div className="relative inline-block">
|
||||
<img
|
||||
src={previewUrl!}
|
||||
alt="Pending upload"
|
||||
className="h-20 rounded-lg object-cover border border-sand"
|
||||
/>
|
||||
{/* Textarea */}
|
||||
<Textarea
|
||||
ref={textareaRef}
|
||||
onChange={handleQueryChange}
|
||||
onKeyDown={handleKeyDown}
|
||||
onPaste={handlePaste}
|
||||
value={query}
|
||||
rows={2}
|
||||
placeholder="Ask Simba anything..."
|
||||
className="min-h-[60px] max-h-40 overflow-y-auto"
|
||||
autoFocus={autoFocus}
|
||||
/>
|
||||
|
||||
{/* Hidden file input */}
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
onChange={handleFileChange}
|
||||
className="hidden"
|
||||
/>
|
||||
|
||||
{/* Bottom toolbar */}
|
||||
<div className="flex items-center justify-between px-3 pb-2.5 pt-1">
|
||||
<div className="flex items-center gap-3">
|
||||
{/* Simba mode toggle */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClearImage}
|
||||
className="absolute -top-1.5 -right-1.5 w-5 h-5 rounded-full bg-charcoal text-white flex items-center justify-center hover:bg-charcoal/80 transition-colors cursor-pointer"
|
||||
onClick={onToggleSimbaMode}
|
||||
className="flex items-center gap-2 group cursor-pointer select-none"
|
||||
>
|
||||
<X size={12} />
|
||||
<div className={cn("toggle-track", simbaMode && "checked")}>
|
||||
<div className="toggle-thumb" />
|
||||
</div>
|
||||
<span className="text-xs text-warm-gray group-hover:text-charcoal transition-colors">
|
||||
simba mode
|
||||
</span>
|
||||
</button>
|
||||
|
||||
{/* Image attach button */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => fileInputRef.current?.click()}
|
||||
disabled={isLoading}
|
||||
className={cn(
|
||||
"w-7 h-7 rounded-lg flex items-center justify-center transition-all cursor-pointer",
|
||||
isLoading
|
||||
? "text-warm-gray/40 cursor-not-allowed"
|
||||
: "text-warm-gray hover:text-charcoal hover:bg-cream-dark",
|
||||
)}
|
||||
>
|
||||
<ImagePlus size={16} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Textarea */}
|
||||
<Textarea
|
||||
onChange={handleQueryChange}
|
||||
onKeyDown={handleKeyDown}
|
||||
value={query}
|
||||
rows={2}
|
||||
placeholder="Ask Simba anything..."
|
||||
className="min-h-[60px] max-h-40"
|
||||
/>
|
||||
|
||||
{/* Hidden file input */}
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
onChange={handleFileChange}
|
||||
className="hidden"
|
||||
/>
|
||||
|
||||
{/* Bottom toolbar */}
|
||||
<div className="flex items-center justify-between px-3 pb-2.5 pt-1">
|
||||
<div className="flex items-center gap-3">
|
||||
{/* Simba mode toggle */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={toggleSimbaMode}
|
||||
className="flex items-center gap-2 group cursor-pointer select-none"
|
||||
>
|
||||
<div className={cn("toggle-track", simbaMode && "checked")}>
|
||||
<div className="toggle-thumb" />
|
||||
</div>
|
||||
<span className="text-xs text-warm-gray group-hover:text-charcoal transition-colors">
|
||||
simba mode
|
||||
</span>
|
||||
</button>
|
||||
|
||||
{/* Image attach button */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => fileInputRef.current?.click()}
|
||||
disabled={isLoading}
|
||||
className={cn(
|
||||
"w-7 h-7 rounded-lg flex items-center justify-center transition-all cursor-pointer",
|
||||
isLoading
|
||||
? "text-warm-gray/40 cursor-not-allowed"
|
||||
: "text-warm-gray hover:text-charcoal hover:bg-cream-dark",
|
||||
)}
|
||||
>
|
||||
<ImagePlus size={16} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Send button */}
|
||||
<button
|
||||
type="submit"
|
||||
onClick={handleQuestionSubmit}
|
||||
disabled={!canSend}
|
||||
className={cn(
|
||||
"w-8 h-8 rounded-full flex items-center justify-center",
|
||||
"transition-all duration-200 cursor-pointer",
|
||||
"shadow-sm",
|
||||
!canSend
|
||||
? "bg-sand text-warm-gray/50 cursor-not-allowed shadow-none"
|
||||
: "bg-amber-glow text-white hover:bg-amber-dark hover:shadow-md hover:shadow-amber-glow/30 active:scale-95",
|
||||
{/* Send / Stop button */}
|
||||
{isLoading ? (
|
||||
<button
|
||||
type="button"
|
||||
onClick={onStop}
|
||||
aria-label="Stop generating"
|
||||
className={cn(
|
||||
"w-8 h-8 rounded-full flex items-center justify-center shadow-sm",
|
||||
"bg-charcoal text-white transition-all duration-200 cursor-pointer",
|
||||
"hover:bg-charcoal/85 active:scale-95",
|
||||
)}
|
||||
>
|
||||
<Square size={13} strokeWidth={2.5} fill="currentColor" />
|
||||
</button>
|
||||
) : (
|
||||
<button
|
||||
type="submit"
|
||||
onClick={handleQuestionSubmit}
|
||||
disabled={!canSend}
|
||||
aria-label="Send message"
|
||||
className={cn(
|
||||
"w-8 h-8 rounded-full flex items-center justify-center",
|
||||
"transition-all duration-200 cursor-pointer shadow-sm",
|
||||
!canSend
|
||||
? "bg-sand text-warm-gray/50 cursor-not-allowed shadow-none"
|
||||
: "bg-amber-glow text-white hover:bg-amber-dark hover:shadow-md hover:shadow-amber-glow/30 active:scale-95",
|
||||
)}
|
||||
>
|
||||
<ArrowUp size={15} strokeWidth={2.5} />
|
||||
</button>
|
||||
)}
|
||||
>
|
||||
<ArrowUp size={15} strokeWidth={2.5} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
import { Link } from "react-router-dom";
|
||||
import {
|
||||
Clock,
|
||||
LogOut,
|
||||
PanelLeftClose,
|
||||
Shield,
|
||||
ListTree,
|
||||
X,
|
||||
} from "lucide-react";
|
||||
import { ConversationList } from "./ConversationList";
|
||||
import type { Conversation } from "../hooks/useConversations";
|
||||
import catIcon from "../assets/cat.png";
|
||||
|
||||
type SidebarContentProps = {
|
||||
conversations: Conversation[];
|
||||
onCreateNewConversation: () => void;
|
||||
onSelectConversation: (conversation: Conversation) => void;
|
||||
onRenameConversation: (id: string, title: string) => void;
|
||||
onDeleteConversation: (id: string) => void;
|
||||
selectedId?: string;
|
||||
isAdmin: boolean;
|
||||
onShowAdmin: () => void;
|
||||
onShowScheduler: () => void;
|
||||
onLogout: () => void;
|
||||
/** Desktop: collapse the sidebar. Renders a PanelLeftClose button. */
|
||||
onCollapse?: () => void;
|
||||
/** Mobile: close the drawer. Renders an X button. */
|
||||
onCloseDrawer?: () => void;
|
||||
};
|
||||
|
||||
/**
|
||||
* The dark sidebar body shared by the desktop sidebar and the mobile drawer.
|
||||
* Shows the recent conversations, a link to the full list, and admin/account
|
||||
* actions. Exactly one of `onCollapse`/`onCloseDrawer` is expected per usage.
|
||||
*/
|
||||
export const SidebarContent = ({
|
||||
conversations,
|
||||
onCreateNewConversation,
|
||||
onSelectConversation,
|
||||
onRenameConversation,
|
||||
onDeleteConversation,
|
||||
selectedId,
|
||||
isAdmin,
|
||||
onShowAdmin,
|
||||
onShowScheduler,
|
||||
onLogout,
|
||||
onCollapse,
|
||||
onCloseDrawer,
|
||||
}: SidebarContentProps) => {
|
||||
return (
|
||||
<div className="flex flex-col h-full">
|
||||
<div className="flex items-center justify-between px-4 py-4 border-b border-white/8">
|
||||
<div className="flex items-center gap-2.5">
|
||||
<img src={catIcon} alt="Simba" className="w-12 h-12" />
|
||||
<h2
|
||||
className="text-lg font-bold text-cream tracking-tight"
|
||||
style={{ fontFamily: "var(--font-display)" }}
|
||||
>
|
||||
asksimba
|
||||
</h2>
|
||||
</div>
|
||||
{onCollapse && (
|
||||
<button
|
||||
onClick={onCollapse}
|
||||
className="w-7 h-7 rounded-lg flex items-center justify-center text-cream/40 hover:text-cream hover:bg-white/10 transition-all cursor-pointer"
|
||||
>
|
||||
<PanelLeftClose size={15} />
|
||||
</button>
|
||||
)}
|
||||
{onCloseDrawer && (
|
||||
<button
|
||||
onClick={onCloseDrawer}
|
||||
className="w-7 h-7 rounded-lg flex items-center justify-center text-cream/40 hover:text-cream hover:bg-white/10 transition-all cursor-pointer"
|
||||
>
|
||||
<X size={16} />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex-1 overflow-y-auto px-2 py-3">
|
||||
<ConversationList
|
||||
conversations={conversations}
|
||||
onCreateNewConversation={onCreateNewConversation}
|
||||
onSelectConversation={onSelectConversation}
|
||||
onRenameConversation={onRenameConversation}
|
||||
onDeleteConversation={onDeleteConversation}
|
||||
selectedId={selectedId}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="px-2 pb-3 pt-2 border-t border-white/8 flex flex-col gap-0.5">
|
||||
<Link
|
||||
to="/conversations"
|
||||
onClick={onCloseDrawer}
|
||||
className="flex items-center gap-2 w-full px-3 py-2 rounded-xl text-sm text-cream/50 hover:text-cream hover:bg-white/8 transition-all cursor-pointer"
|
||||
>
|
||||
<ListTree size={14} />
|
||||
<span>See all conversations</span>
|
||||
</Link>
|
||||
{isAdmin && (
|
||||
<>
|
||||
<button
|
||||
onClick={onShowAdmin}
|
||||
className="flex items-center gap-2 w-full px-3 py-2 rounded-xl text-sm text-cream/50 hover:text-cream hover:bg-white/8 transition-all cursor-pointer"
|
||||
>
|
||||
<Shield size={14} />
|
||||
<span>Admin</span>
|
||||
</button>
|
||||
<button
|
||||
onClick={onShowScheduler}
|
||||
className="flex items-center gap-2 w-full px-3 py-2 rounded-xl text-sm text-cream/50 hover:text-cream hover:bg-white/8 transition-all cursor-pointer"
|
||||
>
|
||||
<Clock size={14} />
|
||||
<span>Scheduler</span>
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
<button
|
||||
onClick={onLogout}
|
||||
className="flex items-center gap-2 w-full px-3 py-2 rounded-xl text-sm text-cream/50 hover:text-cream hover:bg-white/8 transition-all cursor-pointer"
|
||||
>
|
||||
<LogOut size={14} />
|
||||
<span>Sign out</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,78 @@
|
||||
import React, { useState, useEffect, useRef } from "react";
|
||||
import { ChevronDown } from "lucide-react";
|
||||
import { cn } from "../lib/utils";
|
||||
|
||||
type ThinkingBubbleProps = {
|
||||
text: string;
|
||||
durationMs: number | null;
|
||||
active: boolean;
|
||||
};
|
||||
|
||||
export const ThinkingBubble = React.memo(
|
||||
({ text, durationMs, active }: ThinkingBubbleProps) => {
|
||||
const [expanded, setExpanded] = useState(true);
|
||||
const textRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
// Auto-scroll thinking text as it streams in.
|
||||
useEffect(() => {
|
||||
if (active && textRef.current) {
|
||||
textRef.current.scrollTop = textRef.current.scrollHeight;
|
||||
}
|
||||
}, [text, active]);
|
||||
|
||||
// Collapse automatically when thinking finishes.
|
||||
useEffect(() => {
|
||||
if (durationMs !== null) {
|
||||
setExpanded(false);
|
||||
}
|
||||
}, [durationMs]);
|
||||
|
||||
const label = active
|
||||
? "Thinking…"
|
||||
: durationMs !== null
|
||||
? `Thought for ${(durationMs / 1000).toFixed(1)}s`
|
||||
: "Thought";
|
||||
|
||||
return (
|
||||
<div className="flex justify-center message-enter">
|
||||
<div className="w-full max-w-2xl">
|
||||
<button
|
||||
onClick={() => setExpanded((v) => !v)}
|
||||
className={cn(
|
||||
"w-full flex items-center justify-between gap-2",
|
||||
"px-3 py-1.5 rounded-xl",
|
||||
"bg-leaf-pale border border-leaf-light/50",
|
||||
"text-xs text-leaf-dark italic",
|
||||
"hover:bg-leaf-light/20 transition-colors cursor-pointer",
|
||||
active && "animate-pulse",
|
||||
)}
|
||||
>
|
||||
<span>{label}</span>
|
||||
{text && (
|
||||
<ChevronDown
|
||||
size={13}
|
||||
className={cn(
|
||||
"text-leaf-dark/60 transition-transform duration-200 shrink-0",
|
||||
expanded && "rotate-180",
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
</button>
|
||||
|
||||
{expanded && text && (
|
||||
<div
|
||||
ref={textRef}
|
||||
className={cn(
|
||||
"mt-1 px-3 py-2 rounded-xl max-h-48 overflow-y-auto",
|
||||
"bg-leaf-pale/40 border border-leaf-light/30",
|
||||
"text-xs text-leaf-dark/70 font-mono leading-relaxed whitespace-pre-wrap",
|
||||
)}
|
||||
>
|
||||
{text}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -1,16 +1,19 @@
|
||||
import React from "react";
|
||||
import { cn } from "../lib/utils";
|
||||
|
||||
export const ToolBubble = React.memo(({ text }: { text: string }) => (
|
||||
<div className="flex justify-center message-enter">
|
||||
<div
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1.5 px-3 py-1 rounded-full",
|
||||
"bg-leaf-pale border border-leaf-light/50",
|
||||
"text-xs text-leaf-dark italic",
|
||||
)}
|
||||
>
|
||||
{text}
|
||||
export const ToolBubble = React.memo(
|
||||
({ text, pulse = false }: { text: string; pulse?: boolean }) => (
|
||||
<div className="flex justify-center message-enter">
|
||||
<div
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1.5 px-3 py-1 rounded-full",
|
||||
"bg-leaf-pale border border-leaf-light/50",
|
||||
"text-xs text-leaf-dark italic",
|
||||
pulse && "animate-pulse",
|
||||
)}
|
||||
>
|
||||
{text}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
));
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import { cn } from "../../lib/utils";
|
||||
|
||||
export interface TextareaProps
|
||||
extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {}
|
||||
extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
|
||||
ref?: React.Ref<HTMLTextAreaElement>;
|
||||
}
|
||||
|
||||
export const Textarea = ({ className, ...props }: TextareaProps) => {
|
||||
export const Textarea = ({ className, ref, ...props }: TextareaProps) => {
|
||||
return (
|
||||
<textarea
|
||||
ref={ref}
|
||||
className={cn(
|
||||
"flex w-full resize-none rounded-xl border-0 bg-transparent px-3 py-2.5",
|
||||
"text-sm text-charcoal placeholder:text-warm-gray/50",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { createContext, useContext, useState, ReactNode } from "react";
|
||||
import { createContext, useContext, useState, type ReactNode } from "react";
|
||||
import { userService } from "../api/userService";
|
||||
|
||||
interface AuthContextType {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useState, useCallback, useEffect, useRef } from "react";
|
||||
import { useMatch } from "react-router-dom";
|
||||
import { conversationService } from "../api/conversationService";
|
||||
import type { Conversation } from "./useConversations";
|
||||
|
||||
@@ -29,7 +30,6 @@ const TOOL_MESSAGES: Record<string, string> = {
|
||||
const simbaAnswers = ["meow.", "hiss...", "purrrrrr", "yowOWROWWowowr"];
|
||||
|
||||
type UseChatOptions = {
|
||||
selectedConversation: Conversation | null;
|
||||
createConversation: () => Promise<Conversation>;
|
||||
refreshConversations: () => Promise<void>;
|
||||
onSessionExpired: () => void;
|
||||
@@ -37,24 +37,45 @@ type UseChatOptions = {
|
||||
};
|
||||
|
||||
export function useChat({
|
||||
selectedConversation,
|
||||
createConversation,
|
||||
refreshConversations,
|
||||
onSessionExpired,
|
||||
scrollToBottom,
|
||||
}: UseChatOptions) {
|
||||
// ChatScreen renders under a single catch-all route so it isn't remounted
|
||||
// when the first message navigates to /c/:id (which would abort the stream),
|
||||
// so the id comes from matching the URL rather than useParams.
|
||||
const conversationId = useMatch("/c/:conversationId")?.params.conversationId;
|
||||
const [messages, setMessages] = useState<Message[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [messagesLoading, setMessagesLoading] = useState(false);
|
||||
const [pendingImage, setPendingImage] = useState<File | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [stalled, setStalled] = useState(false);
|
||||
const [streaming, setStreaming] = useState(false);
|
||||
const [thinking, setThinking] = useState(false);
|
||||
const [thinkingText, setThinkingText] = useState("");
|
||||
const [thinkingDurationMs, setThinkingDurationMs] = useState<number | null>(null);
|
||||
|
||||
const stalledQueryRef = useRef<string | null>(null);
|
||||
const thinkingStartRef = useRef<number | null>(null);
|
||||
|
||||
const isMountedRef = useRef(true);
|
||||
// True once tokens for the current assistant reply have started arriving, so
|
||||
// new content appends to the live bubble instead of creating another one.
|
||||
const streamingActiveRef = useRef(false);
|
||||
const abortControllerRef = useRef<AbortController | null>(null);
|
||||
// Set to a conversation id we just created locally, so the route-change
|
||||
// loader below doesn't wipe the optimistic message + in-flight stream.
|
||||
const skipLoadForIdRef = useRef<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
isMountedRef.current = true;
|
||||
return () => {
|
||||
isMountedRef.current = false;
|
||||
abortControllerRef.current?.abort();
|
||||
// Intentionally do NOT abort on unmount — navigation away lets the
|
||||
// stream finish in the background so the response gets persisted.
|
||||
// stopGeneration() still aborts on explicit user cancel.
|
||||
};
|
||||
}, []);
|
||||
|
||||
@@ -66,19 +87,76 @@ export function useChat({
|
||||
[scrollToBottom],
|
||||
);
|
||||
|
||||
// Load messages whenever the active conversation (URL) changes. Clearing
|
||||
// immediately makes the switch feel instant instead of lingering on the old
|
||||
// thread until the fetch resolves.
|
||||
useEffect(() => {
|
||||
// Skip only for a conversation we just created locally (its optimistic
|
||||
// message + stream are already in state). Guard against the null sentinel
|
||||
// colliding with the "no conversation" (home) route.
|
||||
if (
|
||||
skipLoadForIdRef.current !== null &&
|
||||
skipLoadForIdRef.current === conversationId
|
||||
) {
|
||||
skipLoadForIdRef.current = null;
|
||||
return;
|
||||
}
|
||||
if (!conversationId) {
|
||||
setMessages([]);
|
||||
setMessagesLoading(false);
|
||||
return;
|
||||
}
|
||||
let cancelled = false;
|
||||
setMessages([]);
|
||||
setMessagesLoading(true);
|
||||
(async () => {
|
||||
try {
|
||||
const fetched = await conversationService.getConversation(conversationId);
|
||||
if (cancelled) return;
|
||||
setMessages(
|
||||
(fetched.messages ?? []).map((m) => ({
|
||||
text: m.text,
|
||||
speaker: m.speaker,
|
||||
image_key: m.image_key,
|
||||
})),
|
||||
);
|
||||
scrollToBottom();
|
||||
} catch (err) {
|
||||
if (!cancelled) {
|
||||
console.error("Failed to load messages:", err);
|
||||
setMessages([]);
|
||||
}
|
||||
} finally {
|
||||
if (!cancelled) setMessagesLoading(false);
|
||||
}
|
||||
})();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [conversationId, scrollToBottom]);
|
||||
|
||||
const sendMessage = useCallback(
|
||||
async (query: string, simbaMode: boolean) => {
|
||||
if ((!query.trim() && !pendingImage) || isLoading) return;
|
||||
|
||||
let activeConversation = selectedConversation;
|
||||
let createdNew = false;
|
||||
if (!activeConversation) {
|
||||
activeConversation = await createConversation();
|
||||
createdNew = true;
|
||||
let activeId = conversationId;
|
||||
if (!activeId) {
|
||||
const created = await createConversation();
|
||||
// Prevent the route-change loader from clearing what we're about to add.
|
||||
skipLoadForIdRef.current = created.id;
|
||||
activeId = created.id;
|
||||
}
|
||||
|
||||
const imageFile = pendingImage;
|
||||
|
||||
setError(null);
|
||||
setStalled(false);
|
||||
stalledQueryRef.current = null;
|
||||
setThinkingText("");
|
||||
setThinkingDurationMs(null);
|
||||
thinkingStartRef.current = null;
|
||||
streamingActiveRef.current = false;
|
||||
setStreaming(false);
|
||||
updateMessages((prev) => prev.concat([{ text: query, speaker: "user" }]));
|
||||
setPendingImage(null);
|
||||
setIsLoading(true);
|
||||
@@ -101,7 +179,7 @@ export function useChat({
|
||||
if (imageFile) {
|
||||
const uploadResult = await conversationService.uploadImage(
|
||||
imageFile,
|
||||
activeConversation.id,
|
||||
activeId,
|
||||
);
|
||||
imageKey = uploadResult.image_key;
|
||||
|
||||
@@ -119,52 +197,111 @@ export function useChat({
|
||||
|
||||
await conversationService.streamQuery(
|
||||
query,
|
||||
activeConversation.id,
|
||||
activeId,
|
||||
(event) => {
|
||||
if (!isMountedRef.current) return;
|
||||
if (event.type === "tool_start") {
|
||||
if (event.type === "thinking") {
|
||||
setThinking(true);
|
||||
if (thinkingStartRef.current === null) thinkingStartRef.current = Date.now();
|
||||
} else if (event.type === "thinking_delta") {
|
||||
setThinking(true);
|
||||
if (thinkingStartRef.current === null) thinkingStartRef.current = Date.now();
|
||||
setThinkingText((prev) => prev + event.delta);
|
||||
} else if (event.type === "tool_start") {
|
||||
// Any text after a tool call belongs in a fresh bubble.
|
||||
streamingActiveRef.current = false;
|
||||
setThinking(false);
|
||||
const friendly =
|
||||
TOOL_MESSAGES[event.tool] ?? `Using ${event.tool}...`;
|
||||
updateMessages((prev) =>
|
||||
prev.concat([{ text: friendly, speaker: "tool" }]),
|
||||
);
|
||||
} else if (event.type === "content") {
|
||||
setThinking(false);
|
||||
if (thinkingStartRef.current !== null && thinkingDurationMs === null) {
|
||||
setThinkingDurationMs(Date.now() - thinkingStartRef.current);
|
||||
}
|
||||
if (streamingActiveRef.current) {
|
||||
updateMessages((prev) => {
|
||||
const updated = [...prev];
|
||||
const last = updated[updated.length - 1];
|
||||
updated[updated.length - 1] = {
|
||||
...last,
|
||||
text: last.text + event.delta,
|
||||
};
|
||||
return updated;
|
||||
});
|
||||
} else {
|
||||
streamingActiveRef.current = true;
|
||||
setStreaming(true);
|
||||
updateMessages((prev) =>
|
||||
prev.concat([{ text: event.delta, speaker: "simba" }]),
|
||||
);
|
||||
}
|
||||
} else if (event.type === "response") {
|
||||
updateMessages((prev) =>
|
||||
prev.concat([{ text: event.message, speaker: "simba" }]),
|
||||
);
|
||||
// Reconcile the streamed bubble with the authoritative (persisted)
|
||||
// text; if nothing streamed, append it as a new message.
|
||||
if (streamingActiveRef.current) {
|
||||
updateMessages((prev) => {
|
||||
const updated = [...prev];
|
||||
updated[updated.length - 1] = {
|
||||
...updated[updated.length - 1],
|
||||
text: event.message,
|
||||
};
|
||||
return updated;
|
||||
});
|
||||
streamingActiveRef.current = false;
|
||||
} else {
|
||||
updateMessages((prev) =>
|
||||
prev.concat([{ text: event.message, speaker: "simba" }]),
|
||||
);
|
||||
}
|
||||
} else if (event.type === "error") {
|
||||
console.error("Stream error:", event.message);
|
||||
if (!isMountedRef.current) return;
|
||||
if (event.stalled) {
|
||||
stalledQueryRef.current = query;
|
||||
setStalled(true);
|
||||
} else {
|
||||
setError("Simba ran into a problem answering that.");
|
||||
}
|
||||
}
|
||||
},
|
||||
abortController.signal,
|
||||
imageKey,
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.name === "AbortError") {
|
||||
console.log("Request was aborted");
|
||||
} catch (err) {
|
||||
if (err instanceof Error && err.name === "AbortError") {
|
||||
// User cancelled — not an error to surface.
|
||||
} else {
|
||||
console.error("Failed to send query:", error);
|
||||
console.error("Failed to send query:", err);
|
||||
if (
|
||||
error instanceof Error &&
|
||||
error.message.includes("Session expired")
|
||||
err instanceof Error &&
|
||||
err.message.includes("Session expired")
|
||||
) {
|
||||
onSessionExpired();
|
||||
} else if (isMountedRef.current) {
|
||||
setError("Couldn't reach Simba. Check your connection and try again.");
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (isMountedRef.current) {
|
||||
setIsLoading(false);
|
||||
if (createdNew) {
|
||||
refreshConversations();
|
||||
}
|
||||
setStreaming(false);
|
||||
setThinking(false);
|
||||
// Refresh after every send so the just-used conversation floats to
|
||||
// the top of the sidebar (sorted by last activity) and picks up the
|
||||
// auto-generated title on the first message.
|
||||
refreshConversations();
|
||||
}
|
||||
streamingActiveRef.current = false;
|
||||
abortControllerRef.current = null;
|
||||
}
|
||||
},
|
||||
[
|
||||
pendingImage,
|
||||
isLoading,
|
||||
selectedConversation,
|
||||
conversationId,
|
||||
createConversation,
|
||||
refreshConversations,
|
||||
onSessionExpired,
|
||||
@@ -172,12 +309,37 @@ export function useChat({
|
||||
],
|
||||
);
|
||||
|
||||
const stopGeneration = useCallback(() => {
|
||||
abortControllerRef.current?.abort();
|
||||
setIsLoading(false);
|
||||
}, []);
|
||||
|
||||
const clearError = useCallback(() => setError(null), []);
|
||||
|
||||
const retryStalled = useCallback(() => {
|
||||
const q = stalledQueryRef.current;
|
||||
if (!q) return;
|
||||
setStalled(false);
|
||||
stalledQueryRef.current = null;
|
||||
sendMessage(q, false);
|
||||
}, [sendMessage]);
|
||||
|
||||
return {
|
||||
messages,
|
||||
setMessages: updateMessages,
|
||||
isLoading,
|
||||
streaming,
|
||||
thinking,
|
||||
messagesLoading,
|
||||
pendingImage,
|
||||
setPendingImage,
|
||||
sendMessage,
|
||||
stopGeneration,
|
||||
error,
|
||||
clearError,
|
||||
stalled,
|
||||
retryStalled,
|
||||
thinkingText,
|
||||
thinkingDurationMs,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { conversationService } from "../api/conversationService";
|
||||
import type { Conversation } from "./useConversations";
|
||||
|
||||
const PAGE_SIZE = 20;
|
||||
|
||||
/**
|
||||
* Data source for the dedicated /conversations page: server-side search plus
|
||||
* "Load more" pagination. Detects whether more rows exist by asking for one
|
||||
* extra item beyond the page size.
|
||||
*/
|
||||
export function useConversationBrowser() {
|
||||
const [items, setItems] = useState<Conversation[]>([]);
|
||||
const [search, setSearchValue] = useState("");
|
||||
const [hasMore, setHasMore] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
// Guards against a slow earlier request overwriting a newer one.
|
||||
const requestIdRef = useRef(0);
|
||||
|
||||
const fetchPage = useCallback(
|
||||
async (offset: number, searchTerm: string) => {
|
||||
const requestId = ++requestIdRef.current;
|
||||
setLoading(true);
|
||||
try {
|
||||
const fetched = await conversationService.getAllConversations({
|
||||
limit: PAGE_SIZE + 1,
|
||||
offset,
|
||||
search: searchTerm || undefined,
|
||||
});
|
||||
if (requestId !== requestIdRef.current) return;
|
||||
|
||||
const page = fetched
|
||||
.slice(0, PAGE_SIZE)
|
||||
.map((c) => ({ id: c.id, title: c.name }));
|
||||
setHasMore(fetched.length > PAGE_SIZE);
|
||||
setItems((prev) => (offset === 0 ? page : [...prev, ...page]));
|
||||
} catch (err) {
|
||||
if (requestId === requestIdRef.current) {
|
||||
console.error("Failed to load conversations:", err);
|
||||
}
|
||||
} finally {
|
||||
if (requestId === requestIdRef.current) setLoading(false);
|
||||
}
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
// Debounce search changes; reset to the first page on each new term.
|
||||
useEffect(() => {
|
||||
const handle = setTimeout(() => fetchPage(0, search), 250);
|
||||
return () => clearTimeout(handle);
|
||||
}, [search, fetchPage]);
|
||||
|
||||
const loadMore = useCallback(() => {
|
||||
if (loading || !hasMore) return;
|
||||
fetchPage(items.length, search);
|
||||
}, [loading, hasMore, items.length, search, fetchPage]);
|
||||
|
||||
const renameConversation = useCallback(
|
||||
async (id: string, title: string) => {
|
||||
const trimmed = title.trim();
|
||||
if (!trimmed) return;
|
||||
const previous = items;
|
||||
setItems((prev) =>
|
||||
prev.map((c) => (c.id === id ? { ...c, title: trimmed } : c)),
|
||||
);
|
||||
try {
|
||||
await conversationService.renameConversation(id, trimmed);
|
||||
} catch (err) {
|
||||
console.error("Failed to rename conversation:", err);
|
||||
setItems(previous);
|
||||
}
|
||||
},
|
||||
[items],
|
||||
);
|
||||
|
||||
const deleteConversation = useCallback(
|
||||
async (id: string) => {
|
||||
const previous = items;
|
||||
setItems((prev) => prev.filter((c) => c.id !== id));
|
||||
try {
|
||||
await conversationService.deleteConversation(id);
|
||||
} catch (err) {
|
||||
console.error("Failed to delete conversation:", err);
|
||||
setItems(previous);
|
||||
}
|
||||
},
|
||||
[items],
|
||||
);
|
||||
|
||||
return {
|
||||
items,
|
||||
search,
|
||||
setSearch: setSearchValue,
|
||||
hasMore,
|
||||
loading,
|
||||
loadMore,
|
||||
renameConversation,
|
||||
deleteConversation,
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useState, useCallback, useEffect } from "react";
|
||||
import { useState, useCallback, useEffect, useMemo } from "react";
|
||||
import { useMatch, useNavigate } from "react-router-dom";
|
||||
import { conversationService } from "../api/conversationService";
|
||||
|
||||
export type Conversation = {
|
||||
@@ -6,20 +7,22 @@ export type Conversation = {
|
||||
id: string;
|
||||
};
|
||||
|
||||
type Message = {
|
||||
text: string;
|
||||
speaker: "simba" | "user" | "tool";
|
||||
image_key?: string | null;
|
||||
};
|
||||
// The sidebar/drawer only shows the most recent conversations; the full list
|
||||
// lives on the dedicated /conversations page.
|
||||
const SIDEBAR_LIMIT = 10;
|
||||
|
||||
export function useConversations() {
|
||||
const navigate = useNavigate();
|
||||
// ChatScreen now renders under a single catch-all route, so useParams no
|
||||
// longer captures the id. Derive it from the URL directly instead.
|
||||
const conversationId = useMatch("/c/:conversationId")?.params.conversationId;
|
||||
const [conversations, setConversations] = useState<Conversation[]>([]);
|
||||
const [selectedConversation, setSelectedConversation] =
|
||||
useState<Conversation | null>(null);
|
||||
|
||||
const refreshConversations = useCallback(async () => {
|
||||
try {
|
||||
const fetched = await conversationService.getAllConversations();
|
||||
const fetched = await conversationService.getAllConversations({
|
||||
limit: SIDEBAR_LIMIT,
|
||||
});
|
||||
setConversations(fetched.map((c) => ({ id: c.id, title: c.name })));
|
||||
} catch (err) {
|
||||
console.error("Failed to load conversations:", err);
|
||||
@@ -30,40 +33,69 @@ export function useConversations() {
|
||||
refreshConversations();
|
||||
}, [refreshConversations]);
|
||||
|
||||
const selectConversation = useCallback(
|
||||
async (conversation: Conversation): Promise<Message[]> => {
|
||||
setSelectedConversation(conversation);
|
||||
try {
|
||||
const fetched = await conversationService.getConversation(
|
||||
conversation.id,
|
||||
);
|
||||
return fetched.messages.map((m) => ({
|
||||
text: m.text,
|
||||
speaker: m.speaker,
|
||||
image_key: m.image_key,
|
||||
}));
|
||||
} catch (err) {
|
||||
console.error("Failed to load messages:", err);
|
||||
return [];
|
||||
// The active conversation is derived from the URL. Fall back to a minimal
|
||||
// object while the list is still loading (e.g. on a deep-link refresh).
|
||||
const selectedConversation = useMemo<Conversation | null>(() => {
|
||||
if (!conversationId) return null;
|
||||
return (
|
||||
conversations.find((c) => c.id === conversationId) ?? {
|
||||
id: conversationId,
|
||||
title: "",
|
||||
}
|
||||
},
|
||||
[],
|
||||
);
|
||||
);
|
||||
}, [conversationId, conversations]);
|
||||
|
||||
const createConversation = useCallback(async (): Promise<Conversation> => {
|
||||
const newConv = await conversationService.createConversation();
|
||||
const conversation = { title: newConv.name, id: newConv.id };
|
||||
setConversations((prev) => [conversation, ...prev]);
|
||||
setSelectedConversation(conversation);
|
||||
navigate(`/c/${conversation.id}`);
|
||||
return conversation;
|
||||
}, []);
|
||||
}, [navigate]);
|
||||
|
||||
const renameConversation = useCallback(
|
||||
async (id: string, title: string): Promise<void> => {
|
||||
const trimmed = title.trim();
|
||||
if (!trimmed) return;
|
||||
// Optimistic update, roll back on failure. selectedConversation is
|
||||
// derived from `conversations`, so it updates automatically.
|
||||
const previous = conversations;
|
||||
setConversations((prev) =>
|
||||
prev.map((c) => (c.id === id ? { ...c, title: trimmed } : c)),
|
||||
);
|
||||
try {
|
||||
await conversationService.renameConversation(id, trimmed);
|
||||
} catch (err) {
|
||||
console.error("Failed to rename conversation:", err);
|
||||
setConversations(previous);
|
||||
}
|
||||
},
|
||||
[conversations],
|
||||
);
|
||||
|
||||
const deleteConversation = useCallback(
|
||||
async (id: string): Promise<void> => {
|
||||
const previous = conversations;
|
||||
const wasActive = conversationId === id;
|
||||
setConversations((prev) => prev.filter((c) => c.id !== id));
|
||||
// Navigating home clears the message view via the route change.
|
||||
if (wasActive) navigate("/");
|
||||
try {
|
||||
await conversationService.deleteConversation(id);
|
||||
} catch (err) {
|
||||
console.error("Failed to delete conversation:", err);
|
||||
setConversations(previous);
|
||||
}
|
||||
},
|
||||
[conversations, conversationId, navigate],
|
||||
);
|
||||
|
||||
return {
|
||||
conversations,
|
||||
selectedConversation,
|
||||
setSelectedConversation,
|
||||
selectConversation,
|
||||
createConversation,
|
||||
renameConversation,
|
||||
deleteConversation,
|
||||
refreshConversations,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom/client';
|
||||
import { BrowserRouter } from 'react-router-dom';
|
||||
import App from './App';
|
||||
|
||||
const rootEl = document.getElementById('root');
|
||||
@@ -7,7 +8,9 @@ if (rootEl) {
|
||||
const root = ReactDOM.createRoot(rootEl);
|
||||
root.render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
<BrowserRouter>
|
||||
<App />
|
||||
</BrowserRouter>
|
||||
</React.StrictMode>,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -598,6 +598,11 @@ comma-separated-tokens@^2.0.0:
|
||||
resolved "https://registry.yarnpkg.com/comma-separated-tokens/-/comma-separated-tokens-2.0.3.tgz#4e89c9458acb61bc8fef19f4529973b2392839ee"
|
||||
integrity sha512-Fu4hJdvzeylCfQPp9SGWidpzrMs7tTrlu6Vb8XGaRGck8QSNZJJp538Wrb60Lax4fPwR64ViY468OIUTbRlGZg==
|
||||
|
||||
cookie@^1.0.1:
|
||||
version "1.1.1"
|
||||
resolved "https://registry.yarnpkg.com/cookie/-/cookie-1.1.1.tgz#3bb9bdfc82369db9c2f69c93c9c3ceb310c88b3c"
|
||||
integrity sha512-ei8Aos7ja0weRpFzJnEA9UHJ/7XQmqglbRwnf2ATjcB9Wq874VKH9kfjjirM6UhU2/E5fFYadylyhFldcqSidQ==
|
||||
|
||||
core-js@~3.47.0:
|
||||
version "3.47.0"
|
||||
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.47.0.tgz#436ef07650e191afeb84c24481b298bd60eb4a17"
|
||||
@@ -695,6 +700,11 @@ es-set-tostringtag@^2.1.0:
|
||||
has-tostringtag "^1.0.2"
|
||||
hasown "^2.0.2"
|
||||
|
||||
escape-string-regexp@^5.0.0:
|
||||
version "5.0.0"
|
||||
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-5.0.0.tgz#4683126b500b61762f2dbebace1806e8be31b1c8"
|
||||
integrity sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==
|
||||
|
||||
estree-util-is-identifier-name@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/estree-util-is-identifier-name/-/estree-util-is-identifier-name-3.0.0.tgz#0b5ef4c4ff13508b34dcd01ecfa945f61fce5dbd"
|
||||
@@ -1011,6 +1021,11 @@ magic-string@^0.30.21:
|
||||
dependencies:
|
||||
"@jridgewell/sourcemap-codec" "^1.5.5"
|
||||
|
||||
markdown-table@^3.0.0:
|
||||
version "3.0.4"
|
||||
resolved "https://registry.yarnpkg.com/markdown-table/-/markdown-table-3.0.4.tgz#fe44d6d410ff9d6f2ea1797a3f60aa4d2b631c2a"
|
||||
integrity sha512-wiYz4+JrLyb/DqW2hkFJxP7Vd7JuTDm77fvbM8VfEQdmSMqcImWeeRbHwZjBjIFki/VaMK2BhFi7oUUZeM5bqw==
|
||||
|
||||
marked@^16.3.0:
|
||||
version "16.4.2"
|
||||
resolved "https://registry.yarnpkg.com/marked/-/marked-16.4.2.tgz#4959a64be6c486f0db7467ead7ce288de54290a3"
|
||||
@@ -1021,6 +1036,16 @@ math-intrinsics@^1.1.0:
|
||||
resolved "https://registry.yarnpkg.com/math-intrinsics/-/math-intrinsics-1.1.0.tgz#a0dd74be81e2aa5c2f27e65ce283605ee4e2b7f9"
|
||||
integrity sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==
|
||||
|
||||
mdast-util-find-and-replace@^3.0.0:
|
||||
version "3.0.2"
|
||||
resolved "https://registry.yarnpkg.com/mdast-util-find-and-replace/-/mdast-util-find-and-replace-3.0.2.tgz#70a3174c894e14df722abf43bc250cbae44b11df"
|
||||
integrity sha512-Tmd1Vg/m3Xz43afeNxDIhWRtFZgM2VLyaf4vSTYwudTyeuTneoL3qtWMA5jeLyz/O1vDJmmV4QuScFCA2tBPwg==
|
||||
dependencies:
|
||||
"@types/mdast" "^4.0.0"
|
||||
escape-string-regexp "^5.0.0"
|
||||
unist-util-is "^6.0.0"
|
||||
unist-util-visit-parents "^6.0.0"
|
||||
|
||||
mdast-util-from-markdown@^2.0.0:
|
||||
version "2.0.3"
|
||||
resolved "https://registry.yarnpkg.com/mdast-util-from-markdown/-/mdast-util-from-markdown-2.0.3.tgz#c95822b91aab75f18a4cbe8b2f51b873ed2cf0c7"
|
||||
@@ -1039,6 +1064,71 @@ mdast-util-from-markdown@^2.0.0:
|
||||
micromark-util-types "^2.0.0"
|
||||
unist-util-stringify-position "^4.0.0"
|
||||
|
||||
mdast-util-gfm-autolink-literal@^2.0.0:
|
||||
version "2.0.1"
|
||||
resolved "https://registry.yarnpkg.com/mdast-util-gfm-autolink-literal/-/mdast-util-gfm-autolink-literal-2.0.1.tgz#abd557630337bd30a6d5a4bd8252e1c2dc0875d5"
|
||||
integrity sha512-5HVP2MKaP6L+G6YaxPNjuL0BPrq9orG3TsrZ9YXbA3vDw/ACI4MEsnoDpn6ZNm7GnZgtAcONJyPhOP8tNJQavQ==
|
||||
dependencies:
|
||||
"@types/mdast" "^4.0.0"
|
||||
ccount "^2.0.0"
|
||||
devlop "^1.0.0"
|
||||
mdast-util-find-and-replace "^3.0.0"
|
||||
micromark-util-character "^2.0.0"
|
||||
|
||||
mdast-util-gfm-footnote@^2.0.0:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/mdast-util-gfm-footnote/-/mdast-util-gfm-footnote-2.1.0.tgz#7778e9d9ca3df7238cc2bd3fa2b1bf6a65b19403"
|
||||
integrity sha512-sqpDWlsHn7Ac9GNZQMeUzPQSMzR6Wv0WKRNvQRg0KqHh02fpTz69Qc1QSseNX29bhz1ROIyNyxExfawVKTm1GQ==
|
||||
dependencies:
|
||||
"@types/mdast" "^4.0.0"
|
||||
devlop "^1.1.0"
|
||||
mdast-util-from-markdown "^2.0.0"
|
||||
mdast-util-to-markdown "^2.0.0"
|
||||
micromark-util-normalize-identifier "^2.0.0"
|
||||
|
||||
mdast-util-gfm-strikethrough@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/mdast-util-gfm-strikethrough/-/mdast-util-gfm-strikethrough-2.0.0.tgz#d44ef9e8ed283ac8c1165ab0d0dfd058c2764c16"
|
||||
integrity sha512-mKKb915TF+OC5ptj5bJ7WFRPdYtuHv0yTRxK2tJvi+BDqbkiG7h7u/9SI89nRAYcmap2xHQL9D+QG/6wSrTtXg==
|
||||
dependencies:
|
||||
"@types/mdast" "^4.0.0"
|
||||
mdast-util-from-markdown "^2.0.0"
|
||||
mdast-util-to-markdown "^2.0.0"
|
||||
|
||||
mdast-util-gfm-table@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/mdast-util-gfm-table/-/mdast-util-gfm-table-2.0.0.tgz#7a435fb6223a72b0862b33afbd712b6dae878d38"
|
||||
integrity sha512-78UEvebzz/rJIxLvE7ZtDd/vIQ0RHv+3Mh5DR96p7cS7HsBhYIICDBCu8csTNWNO6tBWfqXPWekRuj2FNOGOZg==
|
||||
dependencies:
|
||||
"@types/mdast" "^4.0.0"
|
||||
devlop "^1.0.0"
|
||||
markdown-table "^3.0.0"
|
||||
mdast-util-from-markdown "^2.0.0"
|
||||
mdast-util-to-markdown "^2.0.0"
|
||||
|
||||
mdast-util-gfm-task-list-item@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/mdast-util-gfm-task-list-item/-/mdast-util-gfm-task-list-item-2.0.0.tgz#e68095d2f8a4303ef24094ab642e1047b991a936"
|
||||
integrity sha512-IrtvNvjxC1o06taBAVJznEnkiHxLFTzgonUdy8hzFVeDun0uTjxxrRGVaNFqkU1wJR3RBPEfsxmU6jDWPofrTQ==
|
||||
dependencies:
|
||||
"@types/mdast" "^4.0.0"
|
||||
devlop "^1.0.0"
|
||||
mdast-util-from-markdown "^2.0.0"
|
||||
mdast-util-to-markdown "^2.0.0"
|
||||
|
||||
mdast-util-gfm@^3.0.0:
|
||||
version "3.1.0"
|
||||
resolved "https://registry.yarnpkg.com/mdast-util-gfm/-/mdast-util-gfm-3.1.0.tgz#2cdf63b92c2a331406b0fb0db4c077c1b0331751"
|
||||
integrity sha512-0ulfdQOM3ysHhCJ1p06l0b0VKlhU0wuQs3thxZQagjcjPrlFRqY215uZGHHJan9GEAXd9MbfPjFJz+qMkVR6zQ==
|
||||
dependencies:
|
||||
mdast-util-from-markdown "^2.0.0"
|
||||
mdast-util-gfm-autolink-literal "^2.0.0"
|
||||
mdast-util-gfm-footnote "^2.0.0"
|
||||
mdast-util-gfm-strikethrough "^2.0.0"
|
||||
mdast-util-gfm-table "^2.0.0"
|
||||
mdast-util-gfm-task-list-item "^2.0.0"
|
||||
mdast-util-to-markdown "^2.0.0"
|
||||
|
||||
mdast-util-mdx-expression@^2.0.0:
|
||||
version "2.0.1"
|
||||
resolved "https://registry.yarnpkg.com/mdast-util-mdx-expression/-/mdast-util-mdx-expression-2.0.1.tgz#43f0abac9adc756e2086f63822a38c8d3c3a5096"
|
||||
@@ -1153,6 +1243,85 @@ micromark-core-commonmark@^2.0.0:
|
||||
micromark-util-symbol "^2.0.0"
|
||||
micromark-util-types "^2.0.0"
|
||||
|
||||
micromark-extension-gfm-autolink-literal@^2.0.0:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/micromark-extension-gfm-autolink-literal/-/micromark-extension-gfm-autolink-literal-2.1.0.tgz#6286aee9686c4462c1e3552a9d505feddceeb935"
|
||||
integrity sha512-oOg7knzhicgQ3t4QCjCWgTmfNhvQbDDnJeVu9v81r7NltNCVmhPy1fJRX27pISafdjL+SVc4d3l48Gb6pbRypw==
|
||||
dependencies:
|
||||
micromark-util-character "^2.0.0"
|
||||
micromark-util-sanitize-uri "^2.0.0"
|
||||
micromark-util-symbol "^2.0.0"
|
||||
micromark-util-types "^2.0.0"
|
||||
|
||||
micromark-extension-gfm-footnote@^2.0.0:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/micromark-extension-gfm-footnote/-/micromark-extension-gfm-footnote-2.1.0.tgz#4dab56d4e398b9853f6fe4efac4fc9361f3e0750"
|
||||
integrity sha512-/yPhxI1ntnDNsiHtzLKYnE3vf9JZ6cAisqVDauhp4CEHxlb4uoOTxOCJ+9s51bIB8U1N1FJ1RXOKTIlD5B/gqw==
|
||||
dependencies:
|
||||
devlop "^1.0.0"
|
||||
micromark-core-commonmark "^2.0.0"
|
||||
micromark-factory-space "^2.0.0"
|
||||
micromark-util-character "^2.0.0"
|
||||
micromark-util-normalize-identifier "^2.0.0"
|
||||
micromark-util-sanitize-uri "^2.0.0"
|
||||
micromark-util-symbol "^2.0.0"
|
||||
micromark-util-types "^2.0.0"
|
||||
|
||||
micromark-extension-gfm-strikethrough@^2.0.0:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/micromark-extension-gfm-strikethrough/-/micromark-extension-gfm-strikethrough-2.1.0.tgz#86106df8b3a692b5f6a92280d3879be6be46d923"
|
||||
integrity sha512-ADVjpOOkjz1hhkZLlBiYA9cR2Anf8F4HqZUO6e5eDcPQd0Txw5fxLzzxnEkSkfnD0wziSGiv7sYhk/ktvbf1uw==
|
||||
dependencies:
|
||||
devlop "^1.0.0"
|
||||
micromark-util-chunked "^2.0.0"
|
||||
micromark-util-classify-character "^2.0.0"
|
||||
micromark-util-resolve-all "^2.0.0"
|
||||
micromark-util-symbol "^2.0.0"
|
||||
micromark-util-types "^2.0.0"
|
||||
|
||||
micromark-extension-gfm-table@^2.0.0:
|
||||
version "2.1.1"
|
||||
resolved "https://registry.yarnpkg.com/micromark-extension-gfm-table/-/micromark-extension-gfm-table-2.1.1.tgz#fac70bcbf51fe65f5f44033118d39be8a9b5940b"
|
||||
integrity sha512-t2OU/dXXioARrC6yWfJ4hqB7rct14e8f7m0cbI5hUmDyyIlwv5vEtooptH8INkbLzOatzKuVbQmAYcbWoyz6Dg==
|
||||
dependencies:
|
||||
devlop "^1.0.0"
|
||||
micromark-factory-space "^2.0.0"
|
||||
micromark-util-character "^2.0.0"
|
||||
micromark-util-symbol "^2.0.0"
|
||||
micromark-util-types "^2.0.0"
|
||||
|
||||
micromark-extension-gfm-tagfilter@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/micromark-extension-gfm-tagfilter/-/micromark-extension-gfm-tagfilter-2.0.0.tgz#f26d8a7807b5985fba13cf61465b58ca5ff7dc57"
|
||||
integrity sha512-xHlTOmuCSotIA8TW1mDIM6X2O1SiX5P9IuDtqGonFhEK0qgRI4yeC6vMxEV2dgyr2TiD+2PQ10o+cOhdVAcwfg==
|
||||
dependencies:
|
||||
micromark-util-types "^2.0.0"
|
||||
|
||||
micromark-extension-gfm-task-list-item@^2.0.0:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/micromark-extension-gfm-task-list-item/-/micromark-extension-gfm-task-list-item-2.1.0.tgz#bcc34d805639829990ec175c3eea12bb5b781f2c"
|
||||
integrity sha512-qIBZhqxqI6fjLDYFTBIa4eivDMnP+OZqsNwmQ3xNLE4Cxwc+zfQEfbs6tzAo2Hjq+bh6q5F+Z8/cksrLFYWQQw==
|
||||
dependencies:
|
||||
devlop "^1.0.0"
|
||||
micromark-factory-space "^2.0.0"
|
||||
micromark-util-character "^2.0.0"
|
||||
micromark-util-symbol "^2.0.0"
|
||||
micromark-util-types "^2.0.0"
|
||||
|
||||
micromark-extension-gfm@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/micromark-extension-gfm/-/micromark-extension-gfm-3.0.0.tgz#3e13376ab95dd7a5cfd0e29560dfe999657b3c5b"
|
||||
integrity sha512-vsKArQsicm7t0z2GugkCKtZehqUm31oeGBV/KVSorWSy8ZlNAv7ytjFhvaryUiCUJYqs+NoE6AFhpQvBTM6Q4w==
|
||||
dependencies:
|
||||
micromark-extension-gfm-autolink-literal "^2.0.0"
|
||||
micromark-extension-gfm-footnote "^2.0.0"
|
||||
micromark-extension-gfm-strikethrough "^2.0.0"
|
||||
micromark-extension-gfm-table "^2.0.0"
|
||||
micromark-extension-gfm-tagfilter "^2.0.0"
|
||||
micromark-extension-gfm-task-list-item "^2.0.0"
|
||||
micromark-util-combine-extensions "^2.0.0"
|
||||
micromark-util-types "^2.0.0"
|
||||
|
||||
micromark-factory-destination@^2.0.0:
|
||||
version "2.0.1"
|
||||
resolved "https://registry.yarnpkg.com/micromark-factory-destination/-/micromark-factory-destination-2.0.1.tgz#8fef8e0f7081f0474fbdd92deb50c990a0264639"
|
||||
@@ -1464,6 +1633,21 @@ react-refresh@^0.18.0:
|
||||
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.18.0.tgz#2dce97f4fe932a4d8142fa1630e475c1729c8062"
|
||||
integrity sha512-QgT5//D3jfjJb6Gsjxv0Slpj23ip+HtOpnNgnb2S5zU3CB26G/IDPGoy4RJB42wzFE46DRsstbW6tKHoKbhAxw==
|
||||
|
||||
react-router-dom@^7.18.3:
|
||||
version "7.18.3"
|
||||
resolved "https://registry.yarnpkg.com/react-router-dom/-/react-router-dom-7.18.3.tgz#0688c91b4b376c8b0dcaf0a0064975d52a32e0f7"
|
||||
integrity sha512-ytVbyBBM7vMfRCam25r0WMhSVSom909A8p+8m0/f1w853dz/xfFu6etAT2SEbVoSnI+ZoPRDqIsQXVT89gp7kg==
|
||||
dependencies:
|
||||
react-router "7.18.3"
|
||||
|
||||
react-router@7.18.3:
|
||||
version "7.18.3"
|
||||
resolved "https://registry.yarnpkg.com/react-router/-/react-router-7.18.3.tgz#2a3257aa7c5edd5a71f878063e4c7f3fcfc4b76a"
|
||||
integrity sha512-gyXgtdr5uACJ5b1Q4udzjVV+tb/rlHIMJKuJ0e89R4Kzgz47z/rgP0dIKxktqIEUhDHluGTPJJH/wRha7CyqsA==
|
||||
dependencies:
|
||||
cookie "^1.0.1"
|
||||
set-cookie-parser "^2.6.0"
|
||||
|
||||
react@^19.1.1:
|
||||
version "19.2.4"
|
||||
resolved "https://registry.yarnpkg.com/react/-/react-19.2.4.tgz#438e57baa19b77cb23aab516cf635cd0579ee09a"
|
||||
@@ -1485,6 +1669,18 @@ readdirp@~3.6.0:
|
||||
dependencies:
|
||||
picomatch "^2.2.1"
|
||||
|
||||
remark-gfm@^4.0.1:
|
||||
version "4.0.1"
|
||||
resolved "https://registry.yarnpkg.com/remark-gfm/-/remark-gfm-4.0.1.tgz#33227b2a74397670d357bf05c098eaf8513f0d6b"
|
||||
integrity sha512-1quofZ2RQ9EWdeN34S79+KExV1764+wCUGop5CPL1WGdD0ocPpu91lzPGbwWMECpEpd42kJGQwzRfyov9j4yNg==
|
||||
dependencies:
|
||||
"@types/mdast" "^4.0.0"
|
||||
mdast-util-gfm "^3.0.0"
|
||||
micromark-extension-gfm "^3.0.0"
|
||||
remark-parse "^11.0.0"
|
||||
remark-stringify "^11.0.0"
|
||||
unified "^11.0.0"
|
||||
|
||||
remark-parse@^11.0.0:
|
||||
version "11.0.0"
|
||||
resolved "https://registry.yarnpkg.com/remark-parse/-/remark-parse-11.0.0.tgz#aa60743fcb37ebf6b069204eb4da304e40db45a1"
|
||||
@@ -1506,6 +1702,15 @@ remark-rehype@^11.0.0:
|
||||
unified "^11.0.0"
|
||||
vfile "^6.0.0"
|
||||
|
||||
remark-stringify@^11.0.0:
|
||||
version "11.0.0"
|
||||
resolved "https://registry.yarnpkg.com/remark-stringify/-/remark-stringify-11.0.0.tgz#4c5b01dd711c269df1aaae11743eb7e2e7636fd3"
|
||||
integrity sha512-1OSmLd3awB/t8qdoEOMazZkNsfVTeY4fTsgzcQFdXNq8ToTN4ZGwrMnlda4K6smTFKD+GRV6O48i6Z4iKgPPpw==
|
||||
dependencies:
|
||||
"@types/mdast" "^4.0.0"
|
||||
mdast-util-to-markdown "^2.0.0"
|
||||
unified "^11.0.0"
|
||||
|
||||
safe-buffer@~5.2.0:
|
||||
version "5.2.1"
|
||||
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
|
||||
@@ -1521,6 +1726,11 @@ semver@^7.5.3:
|
||||
resolved "https://registry.yarnpkg.com/semver/-/semver-7.7.4.tgz#28464e36060e991fa7a11d0279d2d3f3b57a7e8a"
|
||||
integrity sha512-vFKC2IEtQnVhpT78h1Yp8wzwrf8CM+MzKMHGJZfBtzhZNycRFnXsHk6E5TxIkkMsgNS7mdX3AGB7x2QM2di4lA==
|
||||
|
||||
set-cookie-parser@^2.6.0:
|
||||
version "2.7.2"
|
||||
resolved "https://registry.yarnpkg.com/set-cookie-parser/-/set-cookie-parser-2.7.2.tgz#ccd08673a9ae5d2e44ea2a2de25089e67c7edf68"
|
||||
integrity sha512-oeM1lpU/UvhTxw+g3cIfxXHyJRc/uidd3yK1P242gzHds0udQBYzs3y8j4gCCW+ZJ7ad0yctld8RYO+bdurlvw==
|
||||
|
||||
simple-update-notifier@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/simple-update-notifier/-/simple-update-notifier-2.0.0.tgz#d70b92bdab7d6d90dfd73931195a30b6e3d7cebb"
|
||||
|
||||
Executable
+48
@@ -0,0 +1,48 @@
|
||||
#!/bin/bash
|
||||
# Zero-downtime blue-green deploy.
|
||||
#
|
||||
# Builds the inactive color, starts it, waits for its healthcheck to pass,
|
||||
# then gracefully stops the old color. Caddy health-checks both ports
|
||||
# (8080 = blue, 8081 = green) and routes traffic to whichever is healthy,
|
||||
# so the swap is invisible to clients.
|
||||
#
|
||||
# Caveats:
|
||||
# - Migrations run on the new container's startup while the old one still
|
||||
# serves, so they must be backward-compatible (additive changes only;
|
||||
# destructive changes need a two-step deploy).
|
||||
# - During the brief overlap both containers run pull-only Obsidian sync
|
||||
# against the shared ./obvault mount.
|
||||
set -euo pipefail
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
running_services=$(docker compose ps --status running --services)
|
||||
|
||||
if echo "$running_services" | grep -qx "raggr-blue"; then
|
||||
OLD=blue NEW=green
|
||||
elif echo "$running_services" | grep -qx "raggr-green"; then
|
||||
OLD=green NEW=blue
|
||||
else
|
||||
OLD="" NEW=blue
|
||||
fi
|
||||
|
||||
echo "==> Active: ${OLD:-none} | Deploying: $NEW"
|
||||
|
||||
echo "==> Building raggr-$NEW..."
|
||||
docker compose --profile "$NEW" build "raggr-$NEW"
|
||||
|
||||
echo "==> Starting raggr-$NEW and waiting for healthy..."
|
||||
if ! docker compose --profile "$NEW" up -d --wait "raggr-$NEW"; then
|
||||
echo "!! raggr-$NEW failed to become healthy${OLD:+; raggr-$OLD is still serving}" >&2
|
||||
docker compose --profile "$NEW" logs --tail 50 "raggr-$NEW" >&2 || true
|
||||
docker compose --profile "$NEW" stop -t 5 "raggr-$NEW" || true
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -n "$OLD" ]; then
|
||||
echo "==> raggr-$NEW healthy; stopping raggr-$OLD (30s grace for in-flight streams)..."
|
||||
docker compose --profile "$OLD" stop -t 30 "raggr-$OLD"
|
||||
docker compose --profile "$OLD" rm -f "raggr-$OLD"
|
||||
fi
|
||||
|
||||
echo "==> Deploy complete: raggr-$NEW is live"
|
||||
@@ -0,0 +1,62 @@
|
||||
# /// script
|
||||
# requires-python = ">=3.13"
|
||||
# dependencies = [
|
||||
# "google-auth-oauthlib>=1.0.0",
|
||||
# ]
|
||||
# ///
|
||||
"""One-time OAuth2 setup to generate token.json for Google Calendar and Gmail.
|
||||
|
||||
Usage:
|
||||
uv run scripts/google_auth.py [--credentials credentials.json] [--token token.json]
|
||||
|
||||
Requires credentials.json downloaded from Google Cloud Console
|
||||
(APIs & Services → Credentials → OAuth 2.0 Client ID → Desktop app).
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from google_auth_oauthlib.flow import InstalledAppFlow
|
||||
|
||||
SCOPES = [
|
||||
"https://www.googleapis.com/auth/calendar.events",
|
||||
"https://www.googleapis.com/auth/gmail.readonly",
|
||||
]
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Authorize Google APIs for raggr")
|
||||
parser.add_argument(
|
||||
"--credentials",
|
||||
default="credentials.json",
|
||||
help="Path to OAuth2 credentials JSON from Google Cloud Console",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--token",
|
||||
default="token.json",
|
||||
help="Output path for the generated token file",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
creds_path = Path(args.credentials)
|
||||
if not creds_path.exists():
|
||||
print(f"Error: credentials file not found: {creds_path}")
|
||||
print(
|
||||
"Download it from Google Cloud Console → APIs & Services → Credentials → OAuth 2.0 Client ID"
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
flow = InstalledAppFlow.from_client_secrets_file(str(creds_path), SCOPES)
|
||||
creds = flow.run_local_server(port=0)
|
||||
|
||||
token_path = Path(args.token)
|
||||
token_path.write_text(creds.to_json())
|
||||
print(f"Token saved to {token_path}")
|
||||
print(
|
||||
f"Mount this file into Docker: - ./{token_path}:/app/config/google-token.json:ro"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,238 @@
|
||||
"""
|
||||
Manage Beli profiles and restaurant data.
|
||||
|
||||
Usage:
|
||||
uv run python scripts/manage_beli.py add <username> <beli_email> <beli_password>
|
||||
uv run python scripts/manage_beli.py list
|
||||
uv run python scripts/manage_beli.py refresh <profile_id>
|
||||
uv run python scripts/manage_beli.py refresh-all
|
||||
uv run python scripts/manage_beli.py remove <profile_id>
|
||||
|
||||
Examples:
|
||||
uv run python scripts/manage_beli.py add ryan me@gmail.com mybelipass
|
||||
uv run python scripts/manage_beli.py list
|
||||
uv run python scripts/manage_beli.py refresh 3f2a1b...
|
||||
uv run python scripts/manage_beli.py refresh-all
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from tortoise import Tortoise
|
||||
|
||||
load_dotenv()
|
||||
|
||||
DATABASE_URL = os.getenv(
|
||||
"DATABASE_URL", "postgres://raggr:changeme@localhost:5432/raggr"
|
||||
)
|
||||
|
||||
TORTOISE_CONFIG = {
|
||||
"connections": {"default": DATABASE_URL},
|
||||
"apps": {
|
||||
"models": {
|
||||
"models": [
|
||||
"blueprints.users.models",
|
||||
"blueprints.beli.models",
|
||||
"blueprints.email.models",
|
||||
"blueprints.conversation.models",
|
||||
"blueprints.scheduled_messages.models",
|
||||
"blueprints.cron.models",
|
||||
],
|
||||
"default_connection": "default",
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
async def _init():
|
||||
await Tortoise.init(config=TORTOISE_CONFIG)
|
||||
|
||||
|
||||
async def _close():
|
||||
await Tortoise.close_connections()
|
||||
|
||||
|
||||
async def cmd_add(username: str, beli_email: str, beli_password: str):
|
||||
from blueprints.beli.logic import MAX_PROFILES_PER_USER
|
||||
from blueprints.beli.models import BeliProfile
|
||||
from blueprints.users.models import User
|
||||
from utils.beli_service import authenticate
|
||||
|
||||
user = await User.get_or_none(username=username)
|
||||
if not user:
|
||||
print(f"Error: no user with username '{username}'")
|
||||
return False
|
||||
|
||||
count = await BeliProfile.filter(user=user).count()
|
||||
if count >= MAX_PROFILES_PER_USER:
|
||||
print(
|
||||
f"Error: user already has {MAX_PROFILES_PER_USER} Beli profiles (maximum)"
|
||||
)
|
||||
return False
|
||||
|
||||
if await BeliProfile.exists(user=user, beli_email=beli_email):
|
||||
print(f"Error: profile for '{beli_email}' already exists for this user")
|
||||
return False
|
||||
|
||||
print(f"Authenticating with Beli as {beli_email}...")
|
||||
try:
|
||||
session = await authenticate(beli_email, beli_password)
|
||||
except Exception as e:
|
||||
print(f"Error: Beli authentication failed — {e}")
|
||||
return False
|
||||
|
||||
profile = await BeliProfile.create(
|
||||
user=user,
|
||||
beli_email=beli_email,
|
||||
beli_password=beli_password,
|
||||
beli_user_id=session["user_id"],
|
||||
beli_refresh_token=session["refresh_token"],
|
||||
enabled=True,
|
||||
)
|
||||
print(f"Profile created: {profile.id}")
|
||||
print(f" Beli user ID : {profile.beli_user_id}")
|
||||
print(f" Beli email : {profile.beli_email}")
|
||||
print(f" Owner : {username}")
|
||||
return True
|
||||
|
||||
|
||||
async def cmd_list():
|
||||
from blueprints.beli.models import BeliProfile, BeliRestaurant
|
||||
|
||||
profiles = await BeliProfile.all().prefetch_related("user").order_by("created_at")
|
||||
if not profiles:
|
||||
print("No Beli profiles found.")
|
||||
return
|
||||
|
||||
for p in profiles:
|
||||
count = await BeliRestaurant.filter(profile=p).count()
|
||||
refreshed = (
|
||||
p.last_refreshed_at.strftime("%Y-%m-%d %H:%M UTC")
|
||||
if p.last_refreshed_at
|
||||
else "never"
|
||||
)
|
||||
status = "enabled" if p.enabled else "DISABLED"
|
||||
print(f"{p.id} [{status}]")
|
||||
print(f" Owner : {p.user.email}")
|
||||
print(f" Beli : {p.beli_email} (user_id={p.beli_user_id})")
|
||||
print(f" Restaurants: {count} | Last refresh: {refreshed}")
|
||||
if p.last_error:
|
||||
print(f" Last error : {p.last_error}")
|
||||
print()
|
||||
|
||||
|
||||
async def cmd_refresh(profile_id: str):
|
||||
from blueprints.beli.logic import refresh_profile
|
||||
from blueprints.beli.models import BeliProfile
|
||||
|
||||
profile = await BeliProfile.get_or_none(id=profile_id)
|
||||
if not profile:
|
||||
print(f"Error: no profile with id '{profile_id}'")
|
||||
return False
|
||||
|
||||
print(f"Refreshing profile {profile_id} ({profile.beli_email})...")
|
||||
result = await refresh_profile(profile_id)
|
||||
if result.get("ok"):
|
||||
print(f"Done — {result['upserted']} restaurants upserted")
|
||||
elif result.get("skipped"):
|
||||
print("Skipped (profile disabled)")
|
||||
else:
|
||||
print(f"Failed: {result.get('error')}")
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
async def cmd_refresh_all():
|
||||
from blueprints.beli.models import BeliProfile
|
||||
from blueprints.beli.logic import refresh_profile
|
||||
|
||||
profiles = (
|
||||
await BeliProfile.filter(enabled=True).order_by("last_refreshed_at").all()
|
||||
)
|
||||
if not profiles:
|
||||
print("No enabled Beli profiles found.")
|
||||
return
|
||||
|
||||
print(f"Refreshing {len(profiles)} profile(s)...")
|
||||
for p in profiles:
|
||||
print(f"\n {p.beli_email} ({p.id})")
|
||||
result = await refresh_profile(str(p.id))
|
||||
if result.get("ok"):
|
||||
print(f" → {result['upserted']} restaurants upserted")
|
||||
else:
|
||||
print(f" → failed: {result.get('error')}")
|
||||
|
||||
|
||||
async def cmd_remove(profile_id: str):
|
||||
from blueprints.beli.models import BeliProfile, BeliRestaurant
|
||||
|
||||
profile = await BeliProfile.get_or_none(id=profile_id)
|
||||
if not profile:
|
||||
print(f"Error: no profile with id '{profile_id}'")
|
||||
return False
|
||||
|
||||
deleted = await BeliRestaurant.filter(profile=profile).count()
|
||||
await BeliRestaurant.filter(profile=profile).delete()
|
||||
await profile.delete()
|
||||
print(
|
||||
f"Removed profile {profile_id} ({profile.beli_email}) and {deleted} restaurants"
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
def usage():
|
||||
print(__doc__)
|
||||
|
||||
|
||||
async def main():
|
||||
if len(sys.argv) < 2:
|
||||
usage()
|
||||
sys.exit(1)
|
||||
|
||||
cmd = sys.argv[1]
|
||||
|
||||
await _init()
|
||||
try:
|
||||
if cmd == "add":
|
||||
if len(sys.argv) != 5:
|
||||
print(
|
||||
"Usage: uv run python scripts/manage_beli.py add <username> <beli_email> <beli_password>"
|
||||
)
|
||||
sys.exit(1)
|
||||
ok = await cmd_add(sys.argv[2], sys.argv[3], sys.argv[4])
|
||||
sys.exit(0 if ok else 1)
|
||||
|
||||
elif cmd == "list":
|
||||
await cmd_list()
|
||||
|
||||
elif cmd == "refresh":
|
||||
if len(sys.argv) != 3:
|
||||
print(
|
||||
"Usage: uv run python scripts/manage_beli.py refresh <profile_id>"
|
||||
)
|
||||
sys.exit(1)
|
||||
ok = await cmd_refresh(sys.argv[2])
|
||||
sys.exit(0 if ok else 1)
|
||||
|
||||
elif cmd == "refresh-all":
|
||||
await cmd_refresh_all()
|
||||
|
||||
elif cmd == "remove":
|
||||
if len(sys.argv) != 3:
|
||||
print("Usage: uv run python scripts/manage_beli.py remove <profile_id>")
|
||||
sys.exit(1)
|
||||
ok = await cmd_remove(sys.argv[2])
|
||||
sys.exit(0 if ok else 1)
|
||||
|
||||
else:
|
||||
print(f"Unknown command: {cmd}")
|
||||
usage()
|
||||
sys.exit(1)
|
||||
finally:
|
||||
await _close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Executable
+43
@@ -0,0 +1,43 @@
|
||||
#!/bin/bash
|
||||
# Generate a new aerich migration.
|
||||
# If a raggr container is running, exec into it.
|
||||
# Otherwise, spin up the local postgres service and run aerich from the venv.
|
||||
set -euo pipefail
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
# Load .env so POSTGRES_* vars are available locally
|
||||
if [ -f .env ]; then
|
||||
set -a
|
||||
# shellcheck disable=SC1091
|
||||
source .env
|
||||
set +a
|
||||
fi
|
||||
|
||||
POSTGRES_USER="${POSTGRES_USER:-raggr}"
|
||||
POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-changeme}"
|
||||
POSTGRES_DB="${POSTGRES_DB:-raggr}"
|
||||
LOCAL_DB_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:5432/${POSTGRES_DB}"
|
||||
|
||||
read -rp "Migration name: " name
|
||||
|
||||
RUNNING=$(docker compose ps --status running --services 2>/dev/null | grep -oE 'raggr-(blue|green)' | head -1 || true)
|
||||
|
||||
if [ -n "$RUNNING" ]; then
|
||||
echo "==> Using running container: $RUNNING"
|
||||
docker compose --profile blue --profile green exec "$RUNNING" aerich migrate --name "$name"
|
||||
else
|
||||
echo "==> No raggr container running — using local postgres"
|
||||
docker compose up -d postgres
|
||||
|
||||
echo "==> Waiting for postgres to be ready..."
|
||||
until docker compose exec -T postgres pg_isready -U "$POSTGRES_USER" > /dev/null 2>&1; do
|
||||
sleep 1
|
||||
done
|
||||
|
||||
echo "==> Applying existing migrations..."
|
||||
DATABASE_URL="$LOCAL_DB_URL" .venv/bin/aerich upgrade
|
||||
|
||||
echo "==> Generating migration: $name"
|
||||
DATABASE_URL="$LOCAL_DB_URL" .venv/bin/aerich migrate --name "$name"
|
||||
fi
|
||||
@@ -0,0 +1,103 @@
|
||||
"""Tests for the admin-only BookStack MCP tool wrapping."""
|
||||
|
||||
import os
|
||||
from unittest.mock import patch
|
||||
|
||||
from langchain_core.tools import StructuredTool
|
||||
|
||||
from utils.bookstack_mcp import (
|
||||
ADMIN_ONLY_MESSAGE,
|
||||
BOOKSTACK_TOOL_ALLOWLIST,
|
||||
load_bookstack_tools,
|
||||
select_bookstack_tools,
|
||||
)
|
||||
|
||||
ADMIN = {"configurable": {"is_admin": True}}
|
||||
NON_ADMIN = {"configurable": {"is_admin": False}}
|
||||
|
||||
|
||||
def _mcp_tool(name: str, error: Exception | None = None):
|
||||
"""Build a stub shaped like a langchain-mcp-adapters tool."""
|
||||
calls = []
|
||||
|
||||
async def call(runtime=None, **arguments):
|
||||
calls.append(arguments)
|
||||
if error:
|
||||
raise error
|
||||
return ([{"type": "text", "text": "ok"}], None)
|
||||
|
||||
tool = StructuredTool(
|
||||
name=name,
|
||||
description=f"{name} description",
|
||||
args_schema={"type": "object", "properties": {"query": {"type": "string"}}},
|
||||
coroutine=call,
|
||||
response_format="content_and_artifact",
|
||||
)
|
||||
return tool, calls
|
||||
|
||||
|
||||
class TestSelectBookstackTools:
|
||||
def test_filters_to_allowlist_and_prefixes(self):
|
||||
stubs = [
|
||||
_mcp_tool(n)[0] for n in ("search_content", "delete_page", "update_page")
|
||||
]
|
||||
tools = select_bookstack_tools(stubs)
|
||||
assert [t.name for t in tools] == [
|
||||
"bookstack_search_content",
|
||||
"bookstack_update_page",
|
||||
]
|
||||
|
||||
def test_never_exposes_destructive_tools(self):
|
||||
for name in BOOKSTACK_TOOL_ALLOWLIST:
|
||||
assert not name.startswith("delete_")
|
||||
assert name not in ("permanently_delete", "restore_deleted")
|
||||
|
||||
def test_allows_reads_and_writes(self):
|
||||
assert "get_page" in BOOKSTACK_TOOL_ALLOWLIST
|
||||
assert "create_page" in BOOKSTACK_TOOL_ALLOWLIST
|
||||
assert "update_page" in BOOKSTACK_TOOL_ALLOWLIST
|
||||
|
||||
|
||||
class TestAdminGuard:
|
||||
async def test_admin_call_delegates(self):
|
||||
stub, calls = _mcp_tool("search_content")
|
||||
[tool] = select_bookstack_tools([stub])
|
||||
assert await tool.ainvoke({"query": "wifi"}, config=ADMIN) == "ok"
|
||||
assert calls == [{"query": "wifi"}]
|
||||
|
||||
async def test_non_admin_is_refused_without_calling_server(self):
|
||||
stub, calls = _mcp_tool("search_content")
|
||||
[tool] = select_bookstack_tools([stub])
|
||||
assert (
|
||||
await tool.ainvoke({"query": "wifi"}, config=NON_ADMIN)
|
||||
== ADMIN_ONLY_MESSAGE
|
||||
)
|
||||
assert ADMIN_ONLY_MESSAGE == "BookStack access is restricted to admin users."
|
||||
assert calls == []
|
||||
|
||||
async def test_server_errors_are_returned_as_text(self):
|
||||
stub, _ = _mcp_tool("search_content", error=RuntimeError("401 Unauthorized"))
|
||||
[tool] = select_bookstack_tools([stub])
|
||||
out = await tool.ainvoke({"query": "wifi"}, config=ADMIN)
|
||||
assert out == "BookStack error: 401 Unauthorized"
|
||||
|
||||
|
||||
class TestLoadBookstackTools:
|
||||
async def test_returns_empty_when_unconfigured(self):
|
||||
env = {
|
||||
"BOOKSTACK_BASE_URL": "https://wiki",
|
||||
"BOOKSTACK_TOKEN_ID": "id",
|
||||
"BOOKSTACK_TOKEN_SECRET": "",
|
||||
}
|
||||
with patch.dict(os.environ, env):
|
||||
assert await load_bookstack_tools() == []
|
||||
|
||||
async def test_returns_empty_when_server_fails(self):
|
||||
env = {
|
||||
"BOOKSTACK_BASE_URL": "https://wiki",
|
||||
"BOOKSTACK_TOKEN_ID": "id",
|
||||
"BOOKSTACK_TOKEN_SECRET": "secret",
|
||||
"BOOKSTACK_MCP_COMMAND": "/nonexistent/bookstack-mcp",
|
||||
}
|
||||
with patch.dict(os.environ, env):
|
||||
assert await load_bookstack_tools() == []
|
||||
@@ -0,0 +1,125 @@
|
||||
"""Tests for the admin-only Homebox MCP tool wrapping and agent routing."""
|
||||
|
||||
import os
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
|
||||
from langchain_core.tools import StructuredTool
|
||||
from quart import Quart
|
||||
|
||||
from utils.homebox_mcp import (
|
||||
ADMIN_ONLY_MESSAGE,
|
||||
HOMEBOX_TOOL_ALLOWLIST,
|
||||
load_homebox_tools,
|
||||
select_homebox_tools,
|
||||
)
|
||||
|
||||
ADMIN = {"configurable": {"is_admin": True}}
|
||||
NON_ADMIN = {"configurable": {"is_admin": False}}
|
||||
|
||||
|
||||
def _mcp_tool(name: str, result=None, error: Exception | None = None):
|
||||
"""Build a stub shaped like a langchain-mcp-adapters tool."""
|
||||
calls = []
|
||||
|
||||
async def call(runtime=None, **arguments):
|
||||
calls.append(arguments)
|
||||
if error:
|
||||
raise error
|
||||
return (
|
||||
result if result is not None else ([{"type": "text", "text": "ok"}], None)
|
||||
)
|
||||
|
||||
tool = StructuredTool(
|
||||
name=name,
|
||||
description=f"{name} description",
|
||||
args_schema={
|
||||
"type": "object",
|
||||
"properties": {"query": {"type": "string"}},
|
||||
},
|
||||
coroutine=call,
|
||||
response_format="content_and_artifact",
|
||||
)
|
||||
return tool, calls
|
||||
|
||||
|
||||
class TestSelectHomeboxTools:
|
||||
def test_filters_to_allowlist_and_prefixes(self):
|
||||
stubs = [_mcp_tool(n)[0] for n in ("search_items", "delete_item", "get_item")]
|
||||
tools = select_homebox_tools(stubs)
|
||||
assert [t.name for t in tools] == ["homebox_search_items", "homebox_get_item"]
|
||||
|
||||
def test_exposes_item_editor(self):
|
||||
assert "set_item" in HOMEBOX_TOOL_ALLOWLIST
|
||||
|
||||
def test_never_exposes_deletes(self):
|
||||
assert not any(name.startswith("delete_") for name in HOMEBOX_TOOL_ALLOWLIST)
|
||||
|
||||
def test_truncates_long_descriptions(self):
|
||||
stub, _ = _mcp_tool("search_items")
|
||||
stub.description = "word " * 200
|
||||
[tool] = select_homebox_tools([stub])
|
||||
assert len(tool.description) <= 300
|
||||
|
||||
|
||||
class TestAdminGuard:
|
||||
async def test_admin_call_delegates_and_flattens_content(self):
|
||||
stub, calls = _mcp_tool(
|
||||
"search_items",
|
||||
result=(
|
||||
[{"type": "text", "text": "Drill"}, {"type": "text", "text": "Garage"}],
|
||||
None,
|
||||
),
|
||||
)
|
||||
[tool] = select_homebox_tools([stub])
|
||||
out = await tool.ainvoke({"query": "drill"}, config=ADMIN)
|
||||
assert out == "Drill\nGarage"
|
||||
assert calls == [{"query": "drill"}]
|
||||
|
||||
async def test_non_admin_is_refused_without_calling_server(self):
|
||||
stub, calls = _mcp_tool("search_items")
|
||||
[tool] = select_homebox_tools([stub])
|
||||
assert (
|
||||
await tool.ainvoke({"query": "drill"}, config=NON_ADMIN)
|
||||
== ADMIN_ONLY_MESSAGE
|
||||
)
|
||||
assert await tool.ainvoke({"query": "drill"}) == ADMIN_ONLY_MESSAGE
|
||||
assert calls == []
|
||||
|
||||
async def test_server_errors_are_returned_as_text(self):
|
||||
stub, _ = _mcp_tool("search_items", error=RuntimeError("cannot reach Homebox"))
|
||||
[tool] = select_homebox_tools([stub])
|
||||
out = await tool.ainvoke({"query": "drill"}, config=ADMIN)
|
||||
assert out == "Homebox error: cannot reach Homebox"
|
||||
|
||||
def test_config_not_exposed_in_schema(self):
|
||||
[tool] = select_homebox_tools([_mcp_tool("search_items")[0]])
|
||||
assert list(tool.tool_call_schema["properties"]) == ["query"]
|
||||
|
||||
|
||||
class TestLoadHomeboxTools:
|
||||
async def test_returns_empty_when_unconfigured(self):
|
||||
with patch.dict(os.environ, {"HOMEBOX_URL": "", "HOMEBOX_TOKEN": ""}):
|
||||
assert await load_homebox_tools() == []
|
||||
|
||||
async def test_returns_empty_when_server_fails(self):
|
||||
env = {
|
||||
"HOMEBOX_URL": "http://homebox",
|
||||
"HOMEBOX_TOKEN": "hb_x",
|
||||
"HOMEBOX_MCP_COMMAND": "/nonexistent/homebox-mcp",
|
||||
}
|
||||
with patch.dict(os.environ, env):
|
||||
assert await load_homebox_tools() == []
|
||||
|
||||
|
||||
class TestGetAgentFor:
|
||||
async def test_routes_admin_to_admin_agent(self):
|
||||
from blueprints.conversation.logic import get_agent_for
|
||||
|
||||
app = Quart(__name__)
|
||||
app.main_agent, app.admin_agent = "main", "admin"
|
||||
admin = SimpleNamespace(is_admin=lambda: True)
|
||||
user = SimpleNamespace(is_admin=lambda: False)
|
||||
async with app.app_context():
|
||||
assert get_agent_for(admin) == "admin"
|
||||
assert get_agent_for(user) == "main"
|
||||
@@ -0,0 +1,109 @@
|
||||
"""Tests for the /metrics endpoint (token gate) and the metrics helpers."""
|
||||
|
||||
import os
|
||||
import time
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from quart import Quart
|
||||
|
||||
import app as app_module
|
||||
from metrics import CHAT_REQUESTS_TOTAL, render_metrics, time_response
|
||||
|
||||
|
||||
def _make_app() -> Quart:
|
||||
"""Build a minimal Quart app exposing only the production /metrics handler.
|
||||
|
||||
Reuses the real handler from ``app.py`` but registers it on a fresh app
|
||||
without the full app's ``@app.while_serving`` lifespan, so no Tortoise DB
|
||||
connection is attempted and the route can be tested hermetically.
|
||||
"""
|
||||
minimal = Quart(__name__)
|
||||
minimal.add_url_rule(
|
||||
"/metrics",
|
||||
view_func=app_module.app.view_functions["metrics_endpoint"],
|
||||
methods=["GET"],
|
||||
)
|
||||
return minimal
|
||||
|
||||
|
||||
def _counter_value(stream: str, result: str) -> float:
|
||||
return CHAT_REQUESTS_TOTAL.labels(stream=stream, result=result)._value.get()
|
||||
|
||||
|
||||
class TestMetricsEndpoint:
|
||||
async def test_403_when_token_unset(self):
|
||||
app = _make_app()
|
||||
with patch.dict(os.environ, {}, clear=True):
|
||||
async with app.test_client() as client:
|
||||
resp = await client.get("/metrics")
|
||||
assert resp.status_code == 403
|
||||
|
||||
async def test_403_when_token_empty(self):
|
||||
app = _make_app()
|
||||
with patch.dict(os.environ, {"METRICS_TOKEN": ""}):
|
||||
async with app.test_client() as client:
|
||||
resp = await client.get("/metrics")
|
||||
assert resp.status_code == 403
|
||||
|
||||
async def test_403_wrong_token(self):
|
||||
app = _make_app()
|
||||
with patch.dict(os.environ, {"METRICS_TOKEN": "expected-secret"}):
|
||||
async with app.test_client() as client:
|
||||
resp = await client.get(
|
||||
"/metrics",
|
||||
headers={"Authorization": "Bearer wrong-secret"},
|
||||
)
|
||||
assert resp.status_code == 403
|
||||
|
||||
async def test_403_missing_auth_header(self):
|
||||
app = _make_app()
|
||||
with patch.dict(os.environ, {"METRICS_TOKEN": "expected-secret"}):
|
||||
async with app.test_client() as client:
|
||||
resp = await client.get("/metrics")
|
||||
assert resp.status_code == 403
|
||||
|
||||
async def test_200_correct_token_and_metric_present(self):
|
||||
app = _make_app()
|
||||
with patch.dict(os.environ, {"METRICS_TOKEN": "expected-secret"}):
|
||||
with time_response(stream=False):
|
||||
time.sleep(0.001)
|
||||
async with app.test_client() as client:
|
||||
resp = await client.get(
|
||||
"/metrics",
|
||||
headers={"Authorization": "Bearer expected-secret"},
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
assert "text/plain" in resp.content_type
|
||||
body = await resp.get_data(as_text=True)
|
||||
assert "raggr_chat_response_seconds" in body
|
||||
|
||||
|
||||
class TestTimeResponse:
|
||||
def test_counts_success(self):
|
||||
before = _counter_value("false", "success")
|
||||
with time_response(stream=False):
|
||||
pass
|
||||
after = _counter_value("false", "success")
|
||||
assert after == before + 1
|
||||
|
||||
def test_counts_error_on_exception(self):
|
||||
before = _counter_value("false", "error")
|
||||
with pytest.raises(RuntimeError):
|
||||
with time_response(stream=False):
|
||||
raise RuntimeError("boom")
|
||||
after = _counter_value("false", "error")
|
||||
assert after == before + 1
|
||||
|
||||
def test_observes_histogram_value(self):
|
||||
with time_response(stream=False):
|
||||
time.sleep(0.001)
|
||||
out = render_metrics().decode()
|
||||
assert "raggr_chat_response_seconds" in out
|
||||
|
||||
def test_stream_labels_distinct(self):
|
||||
before = _counter_value("true", "success")
|
||||
with time_response(stream=True):
|
||||
pass
|
||||
after = _counter_value("true", "success")
|
||||
assert after == before + 1
|
||||
@@ -0,0 +1,182 @@
|
||||
"""
|
||||
Beli API client.
|
||||
|
||||
Reverse-engineered from ProjectBarks/beli-api (mirrored at git.torrtle.co/ryan/beli-api).
|
||||
Hosts and auth flow discovered from observed traffic — may break without notice.
|
||||
|
||||
Auth: POST /api/token/ → {access, refresh} (access: 20 min, refresh: 7 days)
|
||||
The API rejects requests without an Origin header (app uses capacitor://localhost;
|
||||
https://localhost also works per external clients).
|
||||
"""
|
||||
|
||||
import base64
|
||||
import json
|
||||
import logging
|
||||
import random
|
||||
import time
|
||||
|
||||
import httpx
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
HOST_ONBOARD = "https://backoffice-service-onboarding-t57o3dxfca-nn.a.run.app"
|
||||
HOST_API = "https://backoffice-service-t57o3dxfca-nn.a.run.app"
|
||||
|
||||
# 3-letter category codes used by the Beli API
|
||||
RANKING_CATEGORIES = ["RES", "DES", "BAR", "BAK"]
|
||||
|
||||
# 350 ms between requests — community-polite interval per API reference
|
||||
REQUEST_SPACING = 0.35
|
||||
|
||||
_USER_AGENTS = [
|
||||
"Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
||||
"Mozilla/5.0 (Linux; Android 16; SM-S928U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.7827.91 Mobile Safari/537.36",
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Safari/605.1.15",
|
||||
]
|
||||
|
||||
|
||||
def _pick_ua() -> str:
|
||||
return random.choice(_USER_AGENTS)
|
||||
|
||||
|
||||
def _base_headers(access_token: str | None = None, ua: str | None = None) -> dict:
|
||||
h = {
|
||||
"User-Agent": ua or _pick_ua(),
|
||||
"Origin": "https://localhost",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
if access_token:
|
||||
h["Authorization"] = f"Bearer {access_token}"
|
||||
return h
|
||||
|
||||
|
||||
def _decode_jwt_claims(token: str) -> dict:
|
||||
payload = token.split(".")[1]
|
||||
payload += "=" * (-len(payload) % 4)
|
||||
return json.loads(base64.urlsafe_b64decode(payload))
|
||||
|
||||
|
||||
def is_token_expired(token: str | None, skew_seconds: int = 60) -> bool:
|
||||
if not token:
|
||||
return True
|
||||
try:
|
||||
return _decode_jwt_claims(token)["exp"] - skew_seconds <= time.time()
|
||||
except Exception:
|
||||
return True
|
||||
|
||||
|
||||
async def authenticate(email: str, password: str) -> dict:
|
||||
"""Login with email+password. Returns {access_token, refresh_token, user_id}."""
|
||||
ua = _pick_ua()
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.post(
|
||||
f"{HOST_ONBOARD}/api/token/",
|
||||
json={"email": email, "password": password},
|
||||
headers=_base_headers(ua=ua),
|
||||
timeout=15,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
|
||||
claims = _decode_jwt_claims(data["access"])
|
||||
return {
|
||||
"access_token": data["access"],
|
||||
"refresh_token": data["refresh"],
|
||||
"user_id": claims["user_id"],
|
||||
}
|
||||
|
||||
|
||||
async def refresh_access_token(refresh_token: str) -> str:
|
||||
"""Exchange a refresh token for a new access token."""
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.post(
|
||||
f"{HOST_ONBOARD}/api/token/refresh/",
|
||||
json={"refresh": refresh_token},
|
||||
headers=_base_headers(),
|
||||
timeout=15,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()["access"]
|
||||
|
||||
|
||||
async def fetch_user_rankings(
|
||||
access_token: str, user_id: str, category: str
|
||||
) -> list[dict]:
|
||||
"""Fetch a user's ranked places for one category (RES/DES/BAR/BAK)."""
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(
|
||||
f"{HOST_API}/api/get-ranking/",
|
||||
params={"user": user_id, "category": category},
|
||||
headers=_base_headers(access_token),
|
||||
timeout=30,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json().get("results") or []
|
||||
|
||||
|
||||
async def fetch_following(access_token: str, user_id: str) -> list[dict]:
|
||||
"""Fetch users that user_id follows. Returns list of user objects with id, full_name, username."""
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(
|
||||
f"{HOST_API}/api/following/{user_id}/",
|
||||
headers=_base_headers(access_token),
|
||||
timeout=30,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
# Endpoint returns either a paginated {results:[]} or bare list
|
||||
if isinstance(data, list):
|
||||
return data
|
||||
return data.get("results") or []
|
||||
|
||||
|
||||
async def fetch_bookmarks(access_token: str) -> list[dict]:
|
||||
"""Fetch the authenticated user's bookmarked (want-to-try) places.
|
||||
|
||||
The endpoint takes no query params — it returns bookmarks for whoever
|
||||
owns the bearer token.
|
||||
"""
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(
|
||||
f"{HOST_API}/api/get-bookmark/",
|
||||
headers=_base_headers(access_token),
|
||||
timeout=30,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json().get("results") or []
|
||||
|
||||
|
||||
def extract_business(item: dict, rank_index: int | None = None) -> dict | None:
|
||||
"""Pull place fields from a ranking or bookmark result item.
|
||||
|
||||
Ranking items have a nested 'business' object (integer id) plus top-level
|
||||
score fields. The score is stored as 'value' (sentiment seed sent by client;
|
||||
server computes the displayed 0-10 score server-side).
|
||||
Rank is the position in the results list, not a field in the response.
|
||||
Notes are stored separately via data-user-business-text; we don't fetch them.
|
||||
"""
|
||||
biz = item.get("business") or item
|
||||
biz_id = biz.get("id") or item.get("business_id")
|
||||
if not biz_id:
|
||||
return None
|
||||
|
||||
price_key = biz.get("price_key")
|
||||
if not price_key:
|
||||
price_int = biz.get("price")
|
||||
if price_int:
|
||||
price_key = "$" * price_int
|
||||
|
||||
return {
|
||||
"beli_business_id": str(biz_id),
|
||||
"name": biz.get("name", ""),
|
||||
"city": biz.get("city"),
|
||||
"neighborhood": biz.get("neighborhood") or biz.get("borough"),
|
||||
"cuisine": biz.get("cuisines") or [],
|
||||
"price_range": price_key,
|
||||
"rank_index": rank_index,
|
||||
"score": item.get("value"),
|
||||
"notes": None,
|
||||
"tags": item.get("tags") or [],
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
"""BookStack wiki tools, loaded from the bookstack-mcp server over stdio.
|
||||
|
||||
Admin-only: the tools are only compiled into the admin agent, and each call
|
||||
re-checks ``is_admin`` in the run config as a second line of defense.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
from langchain_core.tools import BaseTool, StructuredTool
|
||||
|
||||
from utils.mcp_tools import admin_only_message, load_stdio_tools, select_tools
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Read + create/update only. The server exposes 43 tools with writes enabled;
|
||||
# exposing all of them would blow the local model's 32k context and allow
|
||||
# deletes, shelf/attachment management, and recycle-bin purges.
|
||||
BOOKSTACK_TOOL_ALLOWLIST = (
|
||||
"search_content",
|
||||
"get_books",
|
||||
"get_book",
|
||||
"get_chapters",
|
||||
"get_pages",
|
||||
"get_page",
|
||||
"get_recent_changes",
|
||||
"create_book",
|
||||
"create_chapter",
|
||||
"create_page",
|
||||
"update_page",
|
||||
"create_comment",
|
||||
)
|
||||
|
||||
LABEL = "BookStack"
|
||||
ADMIN_ONLY_MESSAGE = admin_only_message(LABEL)
|
||||
|
||||
|
||||
def select_bookstack_tools(mcp_tools: list[BaseTool]) -> list[StructuredTool]:
|
||||
"""Keep allowlisted tools (in allowlist order) and wrap them admin-only."""
|
||||
return select_tools(mcp_tools, BOOKSTACK_TOOL_ALLOWLIST, "bookstack", LABEL)
|
||||
|
||||
|
||||
async def load_bookstack_tools() -> list[StructuredTool]:
|
||||
"""Start bookstack-mcp and return the wrapped tools, or [] if unavailable."""
|
||||
url = os.getenv("BOOKSTACK_BASE_URL")
|
||||
token_id = os.getenv("BOOKSTACK_TOKEN_ID")
|
||||
token_secret = os.getenv("BOOKSTACK_TOKEN_SECRET")
|
||||
if not url or not token_id or not token_secret:
|
||||
logger.info(
|
||||
"BookStack MCP not configured "
|
||||
"(BOOKSTACK_BASE_URL/BOOKSTACK_TOKEN_ID/BOOKSTACK_TOKEN_SECRET unset)"
|
||||
)
|
||||
return []
|
||||
|
||||
return await load_stdio_tools(
|
||||
server="bookstack",
|
||||
command=os.getenv("BOOKSTACK_MCP_COMMAND", "bookstack-mcp"),
|
||||
env={
|
||||
"BOOKSTACK_BASE_URL": url,
|
||||
"BOOKSTACK_TOKEN_ID": token_id,
|
||||
"BOOKSTACK_TOKEN_SECRET": token_secret,
|
||||
# Server-side write gate; the allowlist above keeps deletes out.
|
||||
"BOOKSTACK_ENABLE_WRITE": "true",
|
||||
},
|
||||
allowlist=BOOKSTACK_TOOL_ALLOWLIST,
|
||||
prefix="bookstack",
|
||||
label=LABEL,
|
||||
)
|
||||
@@ -0,0 +1,233 @@
|
||||
import logging
|
||||
import os
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
import httpx
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _base_url() -> str:
|
||||
return os.environ.get("CRONICLE_URL", "").rstrip("/")
|
||||
|
||||
|
||||
def _api_key() -> str:
|
||||
return os.environ.get("CRONICLE_API_KEY", "")
|
||||
|
||||
|
||||
def _is_configured() -> bool:
|
||||
return bool(_base_url() and _api_key())
|
||||
|
||||
|
||||
async def _get_first_category_id() -> str:
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(
|
||||
f"{_base_url()}/api/app/get_categories/v1",
|
||||
params={"api_key": _api_key()},
|
||||
timeout=10,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
if data.get("code") != 0:
|
||||
raise RuntimeError(f"Cronicle get_categories error: {data}")
|
||||
rows = data.get("rows", [])
|
||||
if not rows:
|
||||
raise RuntimeError("Cronicle has no categories configured")
|
||||
return rows[0]["id"]
|
||||
|
||||
|
||||
def _make_trigger_script() -> str:
|
||||
"""Node.js script that tries raggr-green then falls back to raggr-blue."""
|
||||
cron_secret = os.environ.get("CRON_SECRET", "")
|
||||
path = "/api/scheduled-messages/trigger"
|
||||
return f"""#!/usr/bin/env node
|
||||
const http = require('http');
|
||||
function post(host) {{
|
||||
return new Promise((resolve, reject) => {{
|
||||
const req = http.request(
|
||||
{{ hostname: host, port: 8080, path: '{path}', method: 'POST',
|
||||
headers: {{'X-Cron-Secret': '{cron_secret}'}} }},
|
||||
res => {{ res.resume(); resolve(res.statusCode); }}
|
||||
);
|
||||
req.setTimeout(300000, () => {{ req.destroy(); reject(new Error('timeout')); }});
|
||||
req.on('error', reject);
|
||||
req.end();
|
||||
}});
|
||||
}}
|
||||
post('raggr-green').catch(() => post('raggr-blue')).catch(err => {{
|
||||
console.error('trigger failed:', err.message);
|
||||
process.exitCode = 1;
|
||||
}});
|
||||
"""
|
||||
|
||||
|
||||
def _build_timing(scheduled_at: datetime, recurrence: str) -> dict:
|
||||
"""Build a Cronicle timing object from a scheduled datetime and recurrence.
|
||||
|
||||
- none: one-shot (fires exactly once at the given UTC datetime)
|
||||
- daily: fires every day at the same hour/minute
|
||||
- weekly: fires every week on the same weekday and hour/minute
|
||||
- monthly: fires every month on the same day-of-month and hour/minute
|
||||
"""
|
||||
utc = scheduled_at.astimezone(timezone.utc)
|
||||
# Round up to the next minute if there are sub-minute seconds so Cronicle
|
||||
# fires after the message is due, not before.
|
||||
if utc.second or utc.microsecond:
|
||||
utc = (utc + timedelta(minutes=1)).replace(second=0, microsecond=0)
|
||||
h, m = utc.hour, utc.minute
|
||||
|
||||
if recurrence == "daily":
|
||||
return {"hours": [h], "minutes": [m]}
|
||||
elif recurrence == "weekly":
|
||||
# weekday: 0=Sunday in Cronicle (Python isoweekday: Mon=1..Sun=7)
|
||||
wd = utc.isoweekday() % 7 # convert to 0=Sunday
|
||||
return {"weekdays": [wd], "hours": [h], "minutes": [m]}
|
||||
elif recurrence == "monthly":
|
||||
return {"days": [utc.day], "hours": [h], "minutes": [m]}
|
||||
else:
|
||||
# one-shot
|
||||
return {
|
||||
"years": [utc.year],
|
||||
"months": [utc.month],
|
||||
"days": [utc.day],
|
||||
"hours": [utc.hour],
|
||||
"minutes": [utc.minute],
|
||||
}
|
||||
|
||||
|
||||
def _make_beli_refresh_script() -> str:
|
||||
"""Node.js script that triggers the Beli nightly refresh endpoint."""
|
||||
cron_secret = os.environ.get("CRON_SECRET", "")
|
||||
path = "/api/beli/cron/refresh"
|
||||
return f"""#!/usr/bin/env node
|
||||
const http = require('http');
|
||||
function post(host) {{
|
||||
return new Promise((resolve, reject) => {{
|
||||
const req = http.request(
|
||||
{{ hostname: host, port: 8080, path: '{path}', method: 'POST',
|
||||
headers: {{'X-Cron-Secret': '{cron_secret}'}} }},
|
||||
res => {{ res.resume(); resolve(res.statusCode); }}
|
||||
);
|
||||
req.setTimeout(30000, () => {{ req.destroy(); reject(new Error('timeout')); }});
|
||||
req.on('error', reject);
|
||||
req.end();
|
||||
}});
|
||||
}}
|
||||
post('raggr-green').catch(() => post('raggr-blue')).catch(err => {{
|
||||
console.error('beli refresh trigger failed:', err.message);
|
||||
process.exitCode = 1;
|
||||
}});
|
||||
"""
|
||||
|
||||
|
||||
async def ensure_beli_nightly_event() -> str | None:
|
||||
"""Create (once) a daily 1 AM Cronicle event that triggers Beli nightly refresh.
|
||||
|
||||
Safe to call on every startup — checks for an existing event by title first.
|
||||
Returns the Cronicle event ID, or None if Cronicle is not configured.
|
||||
"""
|
||||
if not _is_configured():
|
||||
return None
|
||||
|
||||
title = "Beli Nightly Refresh"
|
||||
try:
|
||||
category_id = await _get_first_category_id()
|
||||
|
||||
# Check whether an event with this title already exists.
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.get(
|
||||
f"{_base_url()}/api/app/get_schedule/v1",
|
||||
params={"api_key": _api_key()},
|
||||
timeout=10,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
existing = resp.json().get("rows", [])
|
||||
|
||||
if any(e.get("title") == title for e in existing):
|
||||
logger.info("Cronicle: Beli nightly event already registered")
|
||||
return next(e["id"] for e in existing if e.get("title") == title)
|
||||
|
||||
script = _make_beli_refresh_script()
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.post(
|
||||
f"{_base_url()}/api/app/create_event/v1",
|
||||
json={
|
||||
"api_key": _api_key(),
|
||||
"title": title,
|
||||
"enabled": 1,
|
||||
"plugin": "shellplug",
|
||||
"target": "allgrp",
|
||||
"category": category_id,
|
||||
"timing": {"hours": [1], "minutes": [0]},
|
||||
"timezone": "UTC",
|
||||
"timeout": 60,
|
||||
"params": {"script": script},
|
||||
"notes": "Auto-created: triggers Beli nightly restaurant refresh",
|
||||
},
|
||||
timeout=10,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
if data.get("code") != 0:
|
||||
raise RuntimeError(f"Cronicle create_event error: {data}")
|
||||
event_id = data["id"]
|
||||
logger.info("Cronicle: created Beli nightly event %s", event_id)
|
||||
return event_id
|
||||
|
||||
except Exception:
|
||||
logger.exception("Failed to register Beli nightly Cronicle event")
|
||||
return None
|
||||
|
||||
|
||||
async def create_message_event(
|
||||
msg_id: str, scheduled_at: datetime, recurrence: str = "none"
|
||||
) -> str | None:
|
||||
"""Create a Cronicle event for a scheduled message.
|
||||
|
||||
Uses one-shot timing for one-time messages, recurring timing otherwise.
|
||||
Returns the Cronicle event ID, or None if Cronicle is not configured.
|
||||
"""
|
||||
if not _is_configured():
|
||||
return None
|
||||
|
||||
try:
|
||||
category_id = await _get_first_category_id()
|
||||
timing = _build_timing(scheduled_at, recurrence)
|
||||
script = _make_trigger_script()
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
resp = await client.post(
|
||||
f"{_base_url()}/api/app/create_event/v1",
|
||||
json={
|
||||
"api_key": _api_key(),
|
||||
"title": f"Scheduled Message {msg_id}",
|
||||
"enabled": 1,
|
||||
"plugin": "shellplug",
|
||||
"target": "allgrp",
|
||||
"category": category_id,
|
||||
"timing": timing,
|
||||
"timezone": "UTC",
|
||||
"timeout": 360,
|
||||
"params": {"script": script},
|
||||
"notes": f"Auto-created for scheduled message {msg_id} (recurrence={recurrence})",
|
||||
},
|
||||
timeout=10,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
if data.get("code") != 0:
|
||||
raise RuntimeError(f"Cronicle create_event error: {data}")
|
||||
event_id = data["id"]
|
||||
logger.info(
|
||||
"Cronicle: created %s event %s for message %s",
|
||||
recurrence,
|
||||
event_id,
|
||||
msg_id,
|
||||
)
|
||||
return event_id
|
||||
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Failed to create Cronicle event for message %s — will not fire", msg_id
|
||||
)
|
||||
return None
|
||||
@@ -0,0 +1,177 @@
|
||||
"""Google Calendar and Gmail service using the Python SDK."""
|
||||
|
||||
import asyncio
|
||||
import base64
|
||||
import os
|
||||
from typing import Any
|
||||
|
||||
from google.auth.transport.requests import Request
|
||||
from google.oauth2.credentials import Credentials
|
||||
from googleapiclient.discovery import build
|
||||
|
||||
SCOPES = [
|
||||
"https://www.googleapis.com/auth/calendar.events",
|
||||
"https://www.googleapis.com/auth/gmail.readonly",
|
||||
]
|
||||
|
||||
|
||||
class GoogleService:
|
||||
def __init__(self):
|
||||
token_path = os.getenv("GOOGLE_TOKEN_FILE", "/app/config/google-token.json")
|
||||
if not os.path.exists(token_path):
|
||||
raise FileNotFoundError(f"Google token file not found: {token_path}")
|
||||
|
||||
self._token_path = token_path
|
||||
creds = Credentials.from_authorized_user_file(token_path, SCOPES)
|
||||
if creds.expired and creds.refresh_token:
|
||||
creds.refresh(Request())
|
||||
with open(token_path, "w") as f:
|
||||
f.write(creds.to_json())
|
||||
self._creds = creds
|
||||
|
||||
self._calendar = build("calendar", "v3", credentials=creds)
|
||||
self._gmail = build("gmail", "v1", credentials=creds)
|
||||
|
||||
async def get_calendar_events(
|
||||
self,
|
||||
time_min: str,
|
||||
time_max: str,
|
||||
calendar_id: str = "primary",
|
||||
max_results: int = 20,
|
||||
) -> list[dict[str, Any]]:
|
||||
def _fetch():
|
||||
result = (
|
||||
self._calendar.events()
|
||||
.list(
|
||||
calendarId=calendar_id,
|
||||
timeMin=time_min,
|
||||
timeMax=time_max,
|
||||
maxResults=max_results,
|
||||
singleEvents=True,
|
||||
orderBy="startTime",
|
||||
)
|
||||
.execute()
|
||||
)
|
||||
return result.get("items", [])
|
||||
|
||||
return await asyncio.to_thread(_fetch)
|
||||
|
||||
async def create_calendar_event(
|
||||
self,
|
||||
summary: str,
|
||||
start_time: str,
|
||||
end_time: str,
|
||||
description: str = "",
|
||||
location: str = "",
|
||||
calendar_id: str = "primary",
|
||||
) -> dict[str, Any]:
|
||||
def _create():
|
||||
body: dict[str, Any] = {
|
||||
"summary": summary,
|
||||
"start": {"dateTime": start_time},
|
||||
"end": {"dateTime": end_time},
|
||||
}
|
||||
if description:
|
||||
body["description"] = description
|
||||
if location:
|
||||
body["location"] = location
|
||||
return (
|
||||
self._calendar.events()
|
||||
.insert(calendarId=calendar_id, body=body)
|
||||
.execute()
|
||||
)
|
||||
|
||||
return await asyncio.to_thread(_create)
|
||||
|
||||
async def search_emails(
|
||||
self,
|
||||
query: str,
|
||||
max_results: int = 10,
|
||||
) -> list[dict[str, Any]]:
|
||||
def _fetch():
|
||||
result = (
|
||||
self._gmail.users()
|
||||
.messages()
|
||||
.list(userId="me", q=query, maxResults=max_results)
|
||||
.execute()
|
||||
)
|
||||
messages = result.get("messages", [])
|
||||
|
||||
emails = []
|
||||
for msg in messages:
|
||||
msg_data = (
|
||||
self._gmail.users()
|
||||
.messages()
|
||||
.get(
|
||||
userId="me",
|
||||
id=msg["id"],
|
||||
format="metadata",
|
||||
metadataHeaders=["From", "To", "Subject", "Date"],
|
||||
)
|
||||
.execute()
|
||||
)
|
||||
headers = {
|
||||
h["name"]: h["value"]
|
||||
for h in msg_data.get("payload", {}).get("headers", [])
|
||||
}
|
||||
emails.append(
|
||||
{
|
||||
"id": msg_data["id"],
|
||||
"from": headers.get("From", ""),
|
||||
"to": headers.get("To", ""),
|
||||
"subject": headers.get("Subject", ""),
|
||||
"date": headers.get("Date", ""),
|
||||
"snippet": msg_data.get("snippet", ""),
|
||||
}
|
||||
)
|
||||
return emails
|
||||
|
||||
return await asyncio.to_thread(_fetch)
|
||||
|
||||
async def get_email(self, message_id: str) -> dict[str, Any]:
|
||||
def _fetch():
|
||||
msg_data = (
|
||||
self._gmail.users()
|
||||
.messages()
|
||||
.get(userId="me", id=message_id, format="full")
|
||||
.execute()
|
||||
)
|
||||
headers = {
|
||||
h["name"]: h["value"]
|
||||
for h in msg_data.get("payload", {}).get("headers", [])
|
||||
}
|
||||
|
||||
body = _extract_body(msg_data.get("payload", {}))
|
||||
|
||||
return {
|
||||
"id": msg_data["id"],
|
||||
"from": headers.get("From", ""),
|
||||
"to": headers.get("To", ""),
|
||||
"subject": headers.get("Subject", ""),
|
||||
"date": headers.get("Date", ""),
|
||||
"body": body[:4000],
|
||||
"snippet": msg_data.get("snippet", ""),
|
||||
}
|
||||
|
||||
return await asyncio.to_thread(_fetch)
|
||||
|
||||
|
||||
def _extract_body(payload: dict[str, Any]) -> str:
|
||||
mime = payload.get("mimeType", "")
|
||||
data = payload.get("body", {}).get("data", "")
|
||||
|
||||
if mime == "text/plain" and data:
|
||||
return base64.urlsafe_b64decode(data).decode("utf-8", errors="replace")
|
||||
|
||||
if mime == "text/html" and data:
|
||||
import html2text
|
||||
|
||||
html = base64.urlsafe_b64decode(data).decode("utf-8", errors="replace")
|
||||
return html2text.html2text(html)
|
||||
|
||||
for part in payload.get("parts", []):
|
||||
result = _extract_body(part)
|
||||
if result:
|
||||
return result
|
||||
|
||||
return ""
|
||||
@@ -0,0 +1,57 @@
|
||||
"""Homebox inventory tools, loaded from the homebox-mcp server over stdio.
|
||||
|
||||
Admin-only: the tools are only compiled into the admin agent, and each call
|
||||
re-checks ``is_admin`` in the run config as a second line of defense.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
from langchain_core.tools import BaseTool, StructuredTool
|
||||
|
||||
from utils.mcp_tools import admin_only_message, load_stdio_tools, select_tools
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Read + safe writes only. The server exposes ~39 tools; exposing all of them
|
||||
# would blow the local model's 32k context and allow deletes.
|
||||
HOMEBOX_TOOL_ALLOWLIST = (
|
||||
"search_items",
|
||||
"get_item",
|
||||
"list_locations",
|
||||
"location_contents",
|
||||
"list_tags",
|
||||
"warranties_expiring",
|
||||
"inventory_stats",
|
||||
"list_maintenance",
|
||||
"create_item",
|
||||
"set_item",
|
||||
"move_item",
|
||||
"log_maintenance",
|
||||
)
|
||||
|
||||
LABEL = "Homebox"
|
||||
ADMIN_ONLY_MESSAGE = admin_only_message(LABEL)
|
||||
|
||||
|
||||
def select_homebox_tools(mcp_tools: list[BaseTool]) -> list[StructuredTool]:
|
||||
"""Keep allowlisted tools (in allowlist order) and wrap them admin-only."""
|
||||
return select_tools(mcp_tools, HOMEBOX_TOOL_ALLOWLIST, "homebox", LABEL)
|
||||
|
||||
|
||||
async def load_homebox_tools() -> list[StructuredTool]:
|
||||
"""Start homebox-mcp and return the wrapped tools, or [] if unavailable."""
|
||||
url = os.getenv("HOMEBOX_URL")
|
||||
token = os.getenv("HOMEBOX_TOKEN")
|
||||
if not url or not token:
|
||||
logger.info("Homebox MCP not configured (HOMEBOX_URL/HOMEBOX_TOKEN unset)")
|
||||
return []
|
||||
|
||||
return await load_stdio_tools(
|
||||
server="homebox",
|
||||
command=os.getenv("HOMEBOX_MCP_COMMAND", "homebox-mcp"),
|
||||
env={"HOMEBOX_URL": url, "HOMEBOX_TOKEN": token},
|
||||
allowlist=HOMEBOX_TOOL_ALLOWLIST,
|
||||
prefix="homebox",
|
||||
label=LABEL,
|
||||
)
|
||||
+21
-38
@@ -1,5 +1,6 @@
|
||||
from ollama import Client
|
||||
import argparse
|
||||
import io
|
||||
import os
|
||||
import logging
|
||||
from PIL import Image, ExifTags
|
||||
@@ -76,48 +77,30 @@ def describe_simba_image(input):
|
||||
return result
|
||||
|
||||
|
||||
async def analyze_user_image(file_bytes: bytes) -> str:
|
||||
"""Analyze an image uploaded by a user and return a text description.
|
||||
def _to_jpeg_bytes(file_bytes: bytes) -> bytes:
|
||||
"""Normalize image bytes to JPEG, handling HEIC/HEIF and other formats."""
|
||||
img = Image.open(io.BytesIO(file_bytes))
|
||||
if img.mode not in ("RGB", "L"):
|
||||
img = img.convert("RGB")
|
||||
buf = io.BytesIO()
|
||||
img.save(buf, format="JPEG", quality=85)
|
||||
return buf.getvalue()
|
||||
|
||||
Uses llama-server (OpenAI-compatible API) with vision support.
|
||||
Falls back to OpenAI if llama-server is not configured.
|
||||
|
||||
def image_content_block(file_bytes: bytes) -> dict:
|
||||
"""Build an OpenAI-style image_url content part from raw image bytes.
|
||||
|
||||
Normalizes to JPEG so HEIC/HEIF and other formats work, then encodes as a
|
||||
base64 data URL suitable for a multimodal chat message.
|
||||
"""
|
||||
import base64
|
||||
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
llama_url = os.getenv("LLAMA_SERVER_URL")
|
||||
if llama_url:
|
||||
aclient = AsyncOpenAI(base_url=llama_url, api_key="not-needed")
|
||||
model = os.getenv("LLAMA_MODEL_NAME", "llama-3.1-8b-instruct")
|
||||
else:
|
||||
aclient = AsyncOpenAI()
|
||||
model = "gpt-4o-mini"
|
||||
|
||||
b64 = base64.b64encode(file_bytes).decode("utf-8")
|
||||
|
||||
response = await aclient.chat.completions.create(
|
||||
model=model,
|
||||
messages=[
|
||||
{
|
||||
"role": "system",
|
||||
"content": "You are a helpful image analyst. Describe what you see in the image in detail. Be thorough but concise.",
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{"type": "text", "text": "Please describe this image in detail."},
|
||||
{
|
||||
"type": "image_url",
|
||||
"image_url": {
|
||||
"url": f"data:image/jpeg;base64,{b64}",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
)
|
||||
return response.choices[0].message.content
|
||||
jpeg_bytes = _to_jpeg_bytes(file_bytes)
|
||||
b64 = base64.b64encode(jpeg_bytes).decode("utf-8")
|
||||
return {
|
||||
"type": "image_url",
|
||||
"image_url": {"url": f"data:image/jpeg;base64,{b64}"},
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
"""Shared helpers for loading admin-only agent tools from stdio MCP servers.
|
||||
|
||||
Each integration (Homebox, BookStack, ...) picks an allowlist of server tools;
|
||||
they are renamed ``<prefix>_<name>``, get trimmed descriptions, and re-check
|
||||
``is_admin`` in the run config on every call as a second line of defense
|
||||
(the tools are only compiled into the admin agent in the first place).
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
from langchain_core.tools import BaseTool, StructuredTool
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
DESCRIPTION_MAX_CHARS = 300
|
||||
|
||||
|
||||
def content_to_text(content) -> str:
|
||||
"""Flatten MCP content blocks into plain text for the LLM and persistence."""
|
||||
if isinstance(content, str):
|
||||
return content
|
||||
if isinstance(content, (list, tuple)):
|
||||
parts = []
|
||||
for block in content:
|
||||
if isinstance(block, dict):
|
||||
parts.append(block.get("text") or f"[{block.get('type', 'content')}]")
|
||||
else:
|
||||
parts.append(str(block))
|
||||
return "\n".join(parts)
|
||||
return str(content)
|
||||
|
||||
|
||||
def short_description(description: str) -> str:
|
||||
description = " ".join((description or "").split())
|
||||
if len(description) > DESCRIPTION_MAX_CHARS:
|
||||
description = description[: DESCRIPTION_MAX_CHARS - 3].rstrip() + "..."
|
||||
return description
|
||||
|
||||
|
||||
def admin_only_message(label: str) -> str:
|
||||
return f"{label} access is restricted to admin users."
|
||||
|
||||
|
||||
def wrap_admin_only(inner: BaseTool, prefix: str, label: str) -> StructuredTool:
|
||||
"""Rename an MCP tool to ``<prefix>_<name>`` and gate it on ``is_admin``."""
|
||||
|
||||
async def call(config: RunnableConfig, **kwargs) -> str:
|
||||
if not (config or {}).get("configurable", {}).get("is_admin"):
|
||||
return admin_only_message(label)
|
||||
try:
|
||||
result = await inner.coroutine(**kwargs)
|
||||
except Exception as e:
|
||||
return f"{label} error: {e}"
|
||||
content = result[0] if isinstance(result, tuple) else result
|
||||
return content_to_text(content)
|
||||
|
||||
return StructuredTool(
|
||||
name=f"{prefix}_{inner.name}",
|
||||
description=short_description(inner.description),
|
||||
args_schema=inner.args_schema,
|
||||
coroutine=call,
|
||||
metadata=inner.metadata,
|
||||
)
|
||||
|
||||
|
||||
def select_tools(
|
||||
mcp_tools: list[BaseTool], allowlist: tuple[str, ...], prefix: str, label: str
|
||||
) -> list[StructuredTool]:
|
||||
"""Keep allowlisted tools (in allowlist order) and wrap them admin-only."""
|
||||
by_name = {t.name: t for t in mcp_tools}
|
||||
missing = [name for name in allowlist if name not in by_name]
|
||||
if missing:
|
||||
logger.warning(f"{label} MCP: server is missing tools {missing}")
|
||||
return [
|
||||
wrap_admin_only(by_name[name], prefix, label)
|
||||
for name in allowlist
|
||||
if name in by_name
|
||||
]
|
||||
|
||||
|
||||
async def load_stdio_tools(
|
||||
server: str,
|
||||
command: str,
|
||||
env: dict[str, str],
|
||||
allowlist: tuple[str, ...],
|
||||
prefix: str,
|
||||
label: str,
|
||||
) -> list[StructuredTool]:
|
||||
"""Start a stdio MCP server and return its wrapped tools, or [] on failure."""
|
||||
try:
|
||||
from langchain_mcp_adapters.client import MultiServerMCPClient
|
||||
|
||||
client = MultiServerMCPClient(
|
||||
{
|
||||
server: {
|
||||
"transport": "stdio",
|
||||
"command": command,
|
||||
"args": [],
|
||||
"env": {**env, "PATH": os.environ.get("PATH", "")},
|
||||
}
|
||||
}
|
||||
)
|
||||
tools = select_tools(await client.get_tools(), allowlist, prefix, label)
|
||||
except Exception:
|
||||
logger.exception(f"{label} MCP failed to load; continuing without it")
|
||||
return []
|
||||
|
||||
logger.info(f"{label} MCP: loaded {len(tools)} tools")
|
||||
return tools
|
||||
@@ -1,5 +1,26 @@
|
||||
import re
|
||||
|
||||
_THINK_RE = re.compile(r"<think>.*?</think>", re.DOTALL)
|
||||
|
||||
|
||||
def extract_agent_text(content) -> str:
|
||||
"""Extract plain text from a LangChain AIMessage content.
|
||||
|
||||
Handles both str and list-of-blocks content (the latter occurs with some
|
||||
tool-using or vision models), and strips <think>...</think> tokens emitted
|
||||
by reasoning models like Qwen3.
|
||||
"""
|
||||
if isinstance(content, list):
|
||||
text = "".join(
|
||||
block.get("text", "")
|
||||
if isinstance(block, dict) and block.get("type") != "thinking"
|
||||
else (str(block) if not isinstance(block, dict) else "")
|
||||
for block in content
|
||||
)
|
||||
else:
|
||||
text = str(content) if content is not None else ""
|
||||
return _THINK_RE.sub("", text).strip()
|
||||
|
||||
|
||||
def strip_markdown(text: str) -> str:
|
||||
"""Strip markdown formatting from text for plain-text channels like iMessage."""
|
||||
|
||||
@@ -235,6 +235,77 @@ class YNABService:
|
||||
else 0,
|
||||
}
|
||||
|
||||
def get_category_allocations(self, month: Optional[str] = None) -> dict[str, Any]:
|
||||
"""Get all category allocations (assigned and available) grouped by category group.
|
||||
|
||||
Args:
|
||||
month: Month in YYYY-MM format (defaults to current month)
|
||||
|
||||
Returns:
|
||||
Dictionary with all categories grouped, showing assigned and available amounts.
|
||||
"""
|
||||
if not month:
|
||||
month = datetime.now().strftime("%Y-%m-01")
|
||||
else:
|
||||
if len(month) == 7:
|
||||
month = f"{month}-01"
|
||||
|
||||
month_date_obj = datetime.strptime(month, "%Y-%m-%d").date()
|
||||
|
||||
# Get category groups so we know which group each category belongs to
|
||||
categories_response = self.categories_api.get_categories(self.budget_id)
|
||||
category_group_map: dict[str, str] = {}
|
||||
group_order: list[str] = []
|
||||
for group in categories_response.data.category_groups or []:
|
||||
if group.deleted or group.hidden:
|
||||
continue
|
||||
if group.name not in group_order:
|
||||
group_order.append(group.name)
|
||||
for cat in group.categories or []:
|
||||
category_group_map[cat.id] = group.name
|
||||
|
||||
# Get month data for assigned/available amounts
|
||||
month_response = self.months_api.get_plan_month(self.budget_id, month_date_obj)
|
||||
month_data = month_response.data.month
|
||||
|
||||
groups: dict[str, list] = {g: [] for g in group_order}
|
||||
total_assigned = 0.0
|
||||
total_available = 0.0
|
||||
|
||||
for category in month_data.categories or []:
|
||||
if category.deleted or category.hidden:
|
||||
continue
|
||||
|
||||
assigned = category.budgeted / 1000
|
||||
activity = category.activity / 1000
|
||||
available = category.balance / 1000
|
||||
|
||||
group_name = category_group_map.get(category.id, "Other")
|
||||
if group_name not in groups:
|
||||
groups[group_name] = []
|
||||
|
||||
groups[group_name].append(
|
||||
{
|
||||
"name": category.name,
|
||||
"assigned": round(assigned, 2),
|
||||
"activity": round(activity, 2),
|
||||
"available": round(available, 2),
|
||||
}
|
||||
)
|
||||
total_assigned += assigned
|
||||
total_available += available
|
||||
|
||||
return {
|
||||
"month": month[:7],
|
||||
"group_order": group_order,
|
||||
"groups": groups,
|
||||
"total_assigned": round(total_assigned, 2),
|
||||
"total_available": round(total_available, 2),
|
||||
"to_be_budgeted": round(month_data.to_be_budgeted / 1000, 2)
|
||||
if month_data.to_be_budgeted
|
||||
else 0,
|
||||
}
|
||||
|
||||
def get_spending_insights(self, months_back: int = 3) -> dict[str, Any]:
|
||||
"""Generate insights about spending patterns and budget health.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user