Add mobile-first web GUI with a server-side session store (SQLite)
- store: a swappable Store trait + bundled-SQLite implementation (WAL,
sessions/messages with per-turn token usage, cascade delete). The standard
/v1 API stays stateless; conversation context lives server-side so
multi-session, statistics and advanced settings can grow on top. DB path
configurable (db_path, default ~/.config/strata-ctl/sessions.db).
- api::sessions: session-aware GUI API — session CRUD, history, POST .../chat
(loads stored context, reuses the completion service, persists new turns with
usage; streaming accumulates the assistant turn and persists before [DONE]),
and /api/stats.
- api::web + web/: a mobile-first chat GUI (vanilla ES modules, no build step),
embedded via rust-embed and served at GET / (same-origin, offline, single
binary). Off-canvas session drawer on phones, persistent sidebar on wide
screens; live streaming with a collapsible thinking panel; model output is
inserted via textContent only.
- router_with_store mounts the GUI + session API only when a store is configured;
router()/router_with_auth() are unchanged (API-only).
- Fix: a global [hidden]{display:none!important} reset so an author display rule
(the stats modal's display:flex) cannot defeat the hidden attribute.
7 new tests (2 store unit + 4 session HTTP end-to-end + 1 config); 94 green +
2 ignored; clippy/fmt clean. Validated live on engine 0.1.67 (context retention,
streaming, stats) and via a browser check (modal, send/render, stats open/close).
Future: the Store backend is intended to move to ArangoDB (multi-model).
This commit is contained in:
1 parent
9e6d44fa1a
commit
1e13caa4ed
15 files changed
+1922
-23
No files matched your search
+67
-9
@@ -10,24 +10,67 @@ use axum::response::{sse::Event, sse::Sse, IntoResponse, Json};
|
||||
use axum::routing::{get, post};
|
||||
use axum::Router;
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::anthropic::{self, AnthropicRequest, AnthropicStream};
|
||||
use super::service::ChatService;
|
||||
use super::types::{ApiErrorEnvelope, ChatRequest, ModelCard, ModelList};
|
||||
use super::{sessions, web};
|
||||
use crate::store::Store;
|
||||
|
||||
/// Build the router. Mount this inside any axum `Router` or serve it directly.
|
||||
pub fn router(service: ChatService) -> Router {
|
||||
router_with_auth(service, AuthConfig::default())
|
||||
}
|
||||
|
||||
/// Build the router with optional API-key auth and a Host allowlist.
|
||||
/// Build the router with optional API-key auth and a Host allowlist (no GUI store).
|
||||
pub fn router_with_auth(service: ChatService, auth: AuthConfig) -> Router {
|
||||
Router::new()
|
||||
build_router(
|
||||
AppState {
|
||||
service,
|
||||
store: None,
|
||||
},
|
||||
auth,
|
||||
)
|
||||
}
|
||||
|
||||
/// Build the router with the session store + web GUI enabled, plus auth.
|
||||
pub fn router_with_store(service: ChatService, store: Arc<dyn Store>, auth: AuthConfig) -> Router {
|
||||
build_router(
|
||||
AppState {
|
||||
service,
|
||||
store: Some(store),
|
||||
},
|
||||
auth,
|
||||
)
|
||||
}
|
||||
|
||||
fn build_router(state: AppState, auth: AuthConfig) -> Router {
|
||||
let mut r = Router::new()
|
||||
.route("/health", get(health))
|
||||
.route("/v1/models", get(models))
|
||||
.route("/v1/chat/completions", post(chat_completions))
|
||||
.route("/v1/messages", post(messages))
|
||||
.with_state(AppState { service })
|
||||
.route("/v1/messages", post(messages));
|
||||
// The session-aware API and the web GUI exist only when a store is configured.
|
||||
if state.store.is_some() {
|
||||
r = r
|
||||
.route(
|
||||
"/api/sessions",
|
||||
get(sessions::list_sessions).post(sessions::create_session),
|
||||
)
|
||||
.route(
|
||||
"/api/sessions/{id}",
|
||||
get(sessions::get_session)
|
||||
.delete(sessions::delete_session)
|
||||
.patch(sessions::rename_session),
|
||||
)
|
||||
.route("/api/sessions/{id}/messages", get(sessions::get_messages))
|
||||
.route("/api/sessions/{id}/chat", post(sessions::session_chat))
|
||||
.route("/api/stats", get(sessions::stats))
|
||||
.route("/", get(web::index))
|
||||
.route("/assets/{*path}", get(web::asset));
|
||||
}
|
||||
r.with_state(state)
|
||||
.layer(axum::middleware::from_fn_with_state(auth, auth_middleware))
|
||||
}
|
||||
|
||||
@@ -110,8 +153,9 @@ async fn auth_middleware(
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
service: ChatService,
|
||||
pub(crate) struct AppState {
|
||||
pub(crate) service: ChatService,
|
||||
pub(crate) store: Option<Arc<dyn Store>>,
|
||||
}
|
||||
|
||||
/// An OpenAI error with the HTTP status the code implies.
|
||||
@@ -121,12 +165,12 @@ pub struct Error {
|
||||
}
|
||||
|
||||
impl Error {
|
||||
fn new(status: StatusCode, body: ApiErrorEnvelope) -> Self {
|
||||
pub(crate) fn new(status: StatusCode, body: ApiErrorEnvelope) -> Self {
|
||||
Self { status, body }
|
||||
}
|
||||
|
||||
/// Choose a status from the error's `code`; unknown codes are client 400s.
|
||||
fn from_envelope(env: ApiErrorEnvelope) -> Self {
|
||||
pub(crate) fn from_envelope(env: ApiErrorEnvelope) -> Self {
|
||||
let status = match env.error.code.as_deref() {
|
||||
Some("engine_unavailable") | Some("engine_startup") => StatusCode::SERVICE_UNAVAILABLE,
|
||||
Some("engine_silent") => StatusCode::GATEWAY_TIMEOUT,
|
||||
@@ -136,12 +180,26 @@ impl Error {
|
||||
Self::new(status, env)
|
||||
}
|
||||
|
||||
fn bad_request(msg: impl Into<String>) -> Self {
|
||||
pub(crate) fn bad_request(msg: impl Into<String>) -> Self {
|
||||
Self::new(
|
||||
StatusCode::BAD_REQUEST,
|
||||
ApiErrorEnvelope::new(msg, "invalid_request_error", None),
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn not_found(msg: impl Into<String>) -> Self {
|
||||
Self::new(
|
||||
StatusCode::NOT_FOUND,
|
||||
ApiErrorEnvelope::new(msg, "invalid_request_error", Some("not_found".to_string())),
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn internal(msg: impl Into<String>) -> Self {
|
||||
Self::new(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
ApiErrorEnvelope::new(msg, "server_error", None),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for Error {
|
||||
|
||||
Reference in new issue
Block a user