//! OpenAI-compatible HTTP surface (plan E6.2/E6.3 + SSE): the skull behind //! the dialect every local-LLM harness/CLI speaks. Trust gating, digestion, //! memory and background learning stay server-side and invisible to clients. //! //! Streaming: `stream:true` returns OpenAI-shaped SSE. Generation itself is //! non-incremental today, so the payload arrives as a single content delta + //! terminal chunks ([DONE] included) — protocol-faithful, honestly labeled. use std::sync::Arc; use axum::body::Body; use axum::extract::State; use axum::http::{header, StatusCode}; use axum::response::{IntoResponse, Response}; use axum::routing::{get, post}; use axum::{Json, Router}; use serde_json::json; use crate::ecosystem::Ecosystem; use crate::tokenizer_bpe::Detokenizer; use crate::trust::Verdict; pub struct AppState { pub eco: tokio::sync::Mutex, pub model_id: String, pub detok: Box, } type Shared = Arc; pub fn router(state: Shared) -> Router { Router::new() .route("/v1/models", get(models)) .route("/v1/chat/completions", post(chat)) .route("/v1/completions", post(completion)) .route("/v1/brain/judge", post(brain_judge)) .route("/v1/brain/distill", post(brain_distill)) .with_state(state) } async fn models(State(s): State) -> Json { Json(json!({ "object": "list", "data": [{ "id": s.model_id, "object": "model", "owned_by": "alter-ego", "meta": {"architecture": "SAN", "judge": "resident"} }] })) } fn extract_prompt_chat(body: &serde_json::Value) -> String { body["messages"] .as_array() .map(|msgs| { msgs.iter() .filter_map(|m| m["content"].as_str()) .collect::>() .join("\n") }) .unwrap_or_default() } async fn chat(State(s): State, Json(body): Json) -> Response { let prompt = extract_prompt_chat(&body); dispatch(&s, &body, &prompt).await } async fn completion(State(s): State, Json(body): Json) -> Response { let prompt = body["prompt"].as_str().unwrap_or_default().to_string(); dispatch(&s, &body, &prompt).await } struct Reply { text: String, prompt_toks: usize, completion_toks: usize, verdict: Option<(Verdict, f32)>, } async fn dispatch(s: &Shared, body: &serde_json::Value, prompt: &str) -> Response { let max_tokens = body["max_tokens"].as_u64().unwrap_or(48).clamp(1, 256) as usize; let eval = body["eval"].as_bool().unwrap_or(false); let seed_ids: Vec = hash_prompt(prompt); let mut eco = s.eco.lock().await; let res = if eval { eco.eval_turn(&seed_ids, max_tokens).map(|ids| (ids, None)) } else if !prompt.is_empty() { let use_ids = if seed_ids.len() >= 4 { seed_ids.clone() } else { vec![1, 2, 3] }; eco.turn_text(prompt, &use_ids, max_tokens).map(|r| { let v = r.verdict; let t = r.score.total; (r.final_output_ids, Some((v, t))) }) } else { eco.turn(&seed_ids, max_tokens).map(|r| { let v = r.verdict; let t = r.score.total; (r.output_ids, Some((v, t))) }) }; drop(eco); let (ids, verdict_info) = match res { Ok(v) => v, Err(e) => { return ( StatusCode::INTERNAL_SERVER_ERROR, format!("generation failed: {e}"), ) .into_response() } }; let reply = Reply { text: s.detok.decode(&ids), prompt_toks: seed_ids.len(), completion_toks: ids.len(), verdict: verdict_info, }; if body["stream"].as_bool().unwrap_or(false) { sse_response(s, reply) } else { let finish = finish_reason(max_tokens, reply.completion_toks); json_response(s, reply, finish) } } fn hash_prompt(prompt: &str) -> Vec { prompt .split_whitespace() .map(|w| { let mut h: u32 = 0x811c9dc5; for b in w.bytes() { h ^= b as u32; h = h.wrapping_mul(0x01000193); } (h % 8191) + 1 }) .collect() } fn finish_reason(max_tokens: usize, produced: usize) -> &'static str { if produced >= max_tokens { "length" } else { "stop" } } fn base_chunk(s: &Shared, id: &str, created: u64) -> serde_json::Value { json!({ "id": id, "object": "chat.completion.chunk", "created": created, "model": s.model_id, }) } fn json_response(s: &Shared, reply: Reply, finish: &str) -> Response { let mut jv = json!({ "id": format!("cmpl-{:x}", now()), "object": "chat.completion", "created": now(), "model": s.model_id, "choices": [{ "index": 0, "message": {"role": "assistant", "content": reply.text}, "finish_reason": finish }], "usage": { "prompt_tokens": reply.prompt_toks, "completion_tokens": reply.completion_toks, "total_tokens": reply.prompt_toks + reply.completion_toks }, "alter_ego": {} }); attach_verdict(&mut jv, reply.verdict); (StatusCode::OK, Json(jv)).into_response() } fn sse_response(s: &Shared, reply: Reply) -> Response { let id = format!("cmpl-{:x}", now()); let created = now(); let mut ev = |payload: serde_json::Value| { format!("data: {}\n\n", payload) }; let mut c0 = base_chunk(s, &id, created); c0["choices"] = json!([{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]); let mut c1 = base_chunk(s, &id, created); c1["choices"] = json!([{"index":0,"delta":{"content": reply.text},"finish_reason":null}]); let mut c2 = base_chunk(s, &id, created); c2["choices"] = json!([{"index":0,"delta":{},"finish_reason": finish_reason(usize::MAX, reply.completion_toks)}]); c2["usage"] = json!({"prompt_tokens": reply.prompt_toks, "completion_tokens": reply.completion_toks}); attach_verdict(&mut c2, reply.verdict); let mut body = String::new(); body.push_str(&ev(c0)); body.push_str(&ev(c1)); body.push_str(&ev(c2)); body.push_str("data: [DONE]\n\n"); Response::builder() .status(StatusCode::OK) .header(header::CONTENT_TYPE, "text/event-stream") .header(header::CACHE_CONTROL, "no-cache") .body(Body::from(body)) .unwrap() } fn attach_verdict(jv: &mut serde_json::Value, v: Option<(Verdict, f32)>) { if let Some((verdict, trust)) = v { jv["alter_ego"]["verdict"] = json!(match verdict { Verdict::Commit => "COMMIT", Verdict::Queue => "QUEUE", Verdict::Reject => "REJECT", }); jv["alter_ego"]["trust"] = json!(trust); } } fn now() -> u64 { std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map(|d| d.as_secs()) .unwrap_or(0) } // --------------------------------------------------------------------------- // A6 integration contract v1 — OgRune (body) consumes the brain over HTTP. // --------------------------------------------------------------------------- use crate::memory::Experience; /// POST /v1/brain/judge {"input":str,"output":str} /// -> independent trust evaluation of an interaction (never self-judged). async fn brain_judge(State(s): State, Json(b): Json) -> Response { let input = b["input"].as_str().unwrap_or_default(); let output = b["output"].as_str().unwrap_or_default(); let exp = Experience::with_text( hash_prompt(input), hash_prompt(output), vec![], format!("Q: {input}\nA: {output}"), "ogrune", ); let eco = s.eco.lock().await; let score = eco.judge.score(&exp); let verdict = score.verdict(); Json(json!({ "verdict": match verdict { Verdict::Commit => "COMMIT", Verdict::Queue => "QUEUE", Verdict::Reject => "REJECT" }, "trust": { "structural": score.structural, "consistency": score.consistency, "verifiability": score.verifiability, "novelty": score.novelty, "safety": score.safety, "total": score.total }, "thresholds": {"commit": 0.80, "queue": 0.35} })) .into_response() } /// POST /v1/brain/distill {"text":str,"learn":bool} /// -> chew-1/2/3 structured nutrients (pure by default; learn=true also writes /// them into tri-memory as validated-by-construction ingestion anchors). async fn brain_distill(State(s): State, Json(b): Json) -> Response { let text = b["text"].as_str().unwrap_or_default(); let learn = b["learn"].as_bool().unwrap_or(false); let mut eco = s.eco.lock().await; let doc = crate::digestion::digest_document(text, eco.tokenizer.as_ref(), eco.embedder.as_ref()); if learn { let doc_ref = format!("og{}", eco.step); for (subj, rel, obj) in doc.root_triples(&doc_ref) { eco.memory.semantic.add(crate::memory::Triple { subject: subj, relation: rel, object: obj, source_exp: doc_ref.clone(), }); } for e in doc.experiences() { eco.memory.episodic.record(e); } eco.step += 1; } Json(json!({ "root_digest": doc.root.text, "clusters": doc.root.clusters.iter().map(|c| json!({"members": c.members, "summary": c.summary})).collect::>(), "leaves": doc.leaves.iter().map(|l| json!({ "chunk_id": l.chunk_id, "idea_topic": l.idea_topic, "key_entities": l.key_entities, "core_claims": l.core_claims, "procedural_steps": l.procedural_steps })).collect::>(), "learned": learn })) .into_response() }