World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
A generation that outlives its request should be reattachable (#172)
WaylandYange7cf700c4f1c
diff --git a/crates/utopia-server/src/api/chat.rs b/crates/utopia-server/src/api/chat.rsindex 18c31310..ee6b016d 100644--- a/crates/utopia-server/src/api/chat.rs+++ b/crates/utopia-server/src/api/chat.rs@@ -2,6 +2,7 @@//! 事件序列:step*(行动轨迹)| sources(引用清单,随检索增量更新)| delta*(增量文本)→ done | error。//! 模型不支持 tool-calling 时自动降级为一次性 RAG 注入。+use crate::live::Frame;use axum::extract::{Path, Query, State};use axum::response::sse::{Event, KeepAlive, Sse};use axum::Json;@@ -326,7 +327,23 @@ pub async fn chat(.await?;let workspace_id = kb.workspace_id;- let stream = async_stream::stream! {+ // 注册表在生成器之前取出来:下面那个 `async_stream!` 会把 `state` 整个搬走+ let live = state.live.clone();++ // 生成过程不挂在这条连接上。+ //+ // **切走一次就丢一个回答**,而且丢得比看上去彻底:整段生成住在下面这个+ // 生成器里,助手消息只在走完时落库;浏览器一导航,axum 丢掉响应体、+ // 生成器 future 被丢弃,于是 LLM 调用当场取消,那句 `append_message`+ // 永远不执行。实测掐断连接时已经收到 1219 字节的正文,二十秒后库里+ // 只剩用户那一行——**那个回答不是存在但没显示,是根本没被生成完**。+ //+ // 所以把生成器交给一个独立任务去驱动,这条连接降级成一个订阅者。+ // 任务不随连接消失,答案照常写完、照常落库,人回来就在。+ //+ // 代价说清楚:**没人看的时候仍然在花钱**。这是有意的——丢答案比多跑一轮贵,+ // 而 `MAX_ROUNDS` 已经给了上限。send 失败(接收端没了)不中断,那正是要点。+ let producer = async_stream::stream! {let ds_names: Vec<String> = mounted_sources.iter().map(|d| d.name.clone()).collect();let tools = tools_schema(can_write, &ds_names);let mut system_prompt = if can_write {@@ -410,9 +427,7 @@ pub async fn chat(}// 会话 id 先行下发(新会话由此告知前端)- yield Ok(Event::default()- .event("conversation")- .data(json!({ "id": conversation_id }).to_string()));+ yield Frame::new("conversation", json!({ "id": conversation_id }).to_string());// 引用清单:跨轮累积、去重、编号稳定。键 = chunk uuid 或 "charter:{slug}#{anchor}"let mut source_ids: Vec<String> = Vec::new();@@ -470,10 +485,10 @@ pub async fn chat(.enumerate().map(|(i, c)| source_json(i + 1, c)).collect();- yield Ok(Event::default().event("sources").data(- serde_json::to_string(&legacy_sources)- .unwrap_or_else(|_| "[]".into()),- ));+ yield Frame::new(+ "sources",+ serde_json::to_string(&legacy_sources).unwrap_or_else(|_| "[]".into()),+ );let mut lmsgs =vec![json!({ "role": "system", "content": legacy_system_prompt(&chunks) })];for (role, content) in &history {@@ -838,13 +853,12 @@ pub async fn chat(obj.insert("at".into(), json!(answer_acc.encode_utf16().count()));}steps_acc.push(step.clone());- yield Ok(Event::default()- .event("step")- .data(serde_json::to_string(&step).unwrap_or_default()));+ yield Frame::new("step", serde_json::to_string(&step).unwrap_or_default());if step["kind"] == "search" || step["kind"] == "docs" {- yield Ok(Event::default()- .event("sources")- .data(serde_json::to_string(&sources).unwrap_or_else(|_| "[]".into())));+ yield Frame::new(+ "sources",+ serde_json::to_string(&sources).unwrap_or_else(|_| "[]".into()),+ );}msgs.push(tool_result_message(&call.id, &result));}@@ -852,21 +866,96 @@ pub async fn chat(}};- Ok(Sse::new(stream).keep_alive(KeepAlive::default()))+ // 生成登记在案,然后**这条连接也只是去「接上」它**——与刷新之后+ // 那条重连走的是同一段代码。两条路分开写的话,迟早只有一条是对的+ let handle = live.begin(conversation_id).await;+ let attached = live.attach(conversation_id).await;+ tokio::spawn(async move {+ let mut producer = std::pin::pin!(producer);+ while let Some(frame) = producer.next().await {+ // 没有订阅者是常态(人走了)。**照发不误**:这里中断就等于+ // 把「切走一次丢一个回答」原样搬回来+ handle.emit(frame).await;+ }+ // 注销之后再接上的人得到「没有在跑的」,那时答案已经落库+ handle.finish().await;+ });++ Ok(sse_from(attached))+}++/// 把一次「接上」变成 SSE:先补一份快照,再照常收增量。+///+/// `None` = 这个会话没有在跑的生成。回一条 `idle` 而不是 404——**客户端+/// 每次打开会话都会问一次**,而「没有在跑」是最常见的答案,不是错误+fn sse_from(+ attached: Option<(+ crate::live::Snapshot,+ tokio::sync::broadcast::Receiver<Frame>,+ )>,+) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {+ let stream = async_stream::stream! {+ let Some((snapshot, mut rx)) = attached else {+ yield to_event(&Frame::new("idle", "{}".into()));+ return;+ };+ yield to_event(&snapshot.to_frame());+ loop {+ match rx.recv().await {+ Ok(frame) => {+ let done = frame.event == "done" || frame.event == "error";+ yield to_event(&frame);+ if done { return; }+ }+ // 生成结束、发送端销毁:正常收尾+ Err(tokio::sync::broadcast::error::RecvError::Closed) => return,+ // 这个客户端读得太慢,被广播缓冲甩下了。**说出来**——+ // 静默继续会让它少掉中间一段而毫不知情+ Err(tokio::sync::broadcast::error::RecvError::Lagged(n)) => {+ yield to_event(&Frame::new(+ "error",+ format!("Fell behind the stream by {n} messages; reopen the conversation"),+ ));+ return;+ }+ }+ }+ };+ Sse::new(stream).keep_alive(KeepAlive::default())+}++fn to_event(frame: &Frame) -> Result<Event, Infallible> {+ Ok(Event::default().event(frame.event).data(&frame.data))+}++/// 接上一次正在跑的生成(刷新页面之后走这里)。+pub async fn reattach(+ State(state): State<AppState>,+ AuthUser(user): AuthUser,+ Path((kb_id, conversation_id)): Path<(Uuid, Uuid)>,+) -> ApiResult<Sse<impl Stream<Item = Result<Event, Infallible>>>> {+ utopia_store::access::require_kb(&state.pool, &user, kb_id, Role::Viewer).await?;+ // **归属要查。** 会话 id 是可以猜的,而这条流会把别人的回答一字不差地念出来+ utopia_store::conversations::require_owned(&state.pool, kb_id, user.id, conversation_id)+ .await?;+ Ok(sse_from(state.live.attach(conversation_id).await))}-fn delta_event(text: &str) -> Result<Event, Infallible> {- Ok(Event::default()- .event("delta")- .data(serde_json::to_string(&json!({ "text": text })).unwrap_or_default()))+/// 生成器产出的是 `Frame`,不是 `axum` 的 `Event`。+/// **广播与快照都要读回事件的内容**,而 `Event` 读不回来(见 `live`)+fn delta_event(text: &str) -> Frame {+ Frame::new(+ "delta",+ serde_json::to_string(&json!({ "text": text })).unwrap_or_default(),+ )}-fn done_event() -> Result<Event, Infallible> {- Ok(Event::default().event("done").data("{}"))+fn done_event() -> Frame {+ Frame::new("done", "{}".into())}-fn error_event(message: &str) -> Result<Event, Infallible> {- Ok(Event::default().event("error").data(message))+fn error_event(message: &str) -> Frame {+ Frame::new("error", message.into())}fn source_json(n: usize, c: &ChunkView) -> serde_json::Value {diff --git a/crates/utopia-server/src/api/mod.rs b/crates/utopia-server/src/api/mod.rsindex 6ddd1527..9c797d4a 100644--- a/crates/utopia-server/src/api/mod.rs+++ b/crates/utopia-server/src/api/mod.rs@@ -243,6 +243,11 @@ pub fn router(state: AppState, cfg: &AppConfig) -> Router {).route("/kbs/{id}/search", post(search_routes::search)).route("/kbs/{id}/chat", post(chat::chat))+ // 刷新页面后重新接上正在生成的那个回答(见 `live`)+ .route(+ "/kbs/{id}/conversations/{conversation_id}/stream",+ get(chat::reattach),+ ).route("/kbs/{id}/conversations", get(chat::list_conversations)).route("/kbs/{id}/conversations/{conversation_id}",diff --git a/crates/utopia-server/src/live.rs b/crates/utopia-server/src/live.rsnew file mode 100644index 00000000..aa743b73--- /dev/null+++ b/crates/utopia-server/src/live.rs@@ -0,0 +1,159 @@+//! 正在生成的回答,能被重新接上。+//!+//! **一条 SSE 流绑在一个 HTTP 请求上,而回答比请求活得长。** 生成已经搬进+//! 独立任务(`api::chat`),所以刷新页面不会再丢答案;但那条流断了就是断了,+//! 刷新之后只能等它落库,中间那段看不见。前端把进行中的那一次搬出组件,+//! 解决的是同一个标签页里切来切去;**刷新、换标签页、换设备都不在其中**。+//!+//! 这里补上最后一段:生成期间把它登记下来,谁都可以再接上。+//!+//! **接上时先给一份快照,不是重放事件。** 事件流会无限长,缓冲它等于把+//! 一次对话的全部增量都留在内存里;而快照的大小就是那个回答本身的大小,+//! 有天然上限。客户端那边也更简单:拿快照覆盖当前状态,然后照常收增量,+//! 不必去想"我重放到哪一条了"。+use serde_json::json;+use std::collections::HashMap;+use std::sync::Arc;+use tokio::sync::{broadcast, RwLock};+use uuid::Uuid;++/// 一个 SSE 事件:事件名 + 已经序列化好的 data。+///+/// 不用 `axum::response::sse::Event`——它没有读回内容的办法,而这里既要+/// 广播出去,又要拿它更新快照。+#[derive(Clone, Debug)]+pub struct Frame {+ pub event: &'static str,+ pub data: String,+}++impl Frame {+ pub fn new(event: &'static str, data: String) -> Self {+ Self { event, data }+ }+}++/// 到此刻为止这个回答长什么样。接上的人先拿到它。+#[derive(Clone, Default, Debug)]+pub struct Snapshot {+ pub content: String,+ pub steps: Vec<serde_json::Value>,+ pub sources: Vec<serde_json::Value>,+}++impl Snapshot {+ /// **快照由事件本身推出来,不另设一套写入口。** 两套写法迟早对不上——+ /// 那正是这个仓库反复踩到的形状(一处认得新字段,另一处不认)+ fn apply(&mut self, f: &Frame) {+ match f.event {+ "delta" => {+ if let Ok(v) = serde_json::from_str::<serde_json::Value>(&f.data) {+ if let Some(t) = v["text"].as_str() {+ self.content.push_str(t);+ }+ }+ }+ "step" => {+ if let Ok(v) = serde_json::from_str(&f.data) {+ self.steps.push(v);+ }+ }+ // sources 是全量重发,不是追加+ "sources" => {+ if let Ok(serde_json::Value::Array(a)) = serde_json::from_str(&f.data) {+ self.sources = a;+ }+ }+ _ => {}+ }+ }++ pub fn to_frame(&self) -> Frame {+ Frame::new(+ "snapshot",+ json!({+ "content": self.content,+ "steps": self.steps,+ "sources": self.sources,+ })+ .to_string(),+ )+ }+}++struct Entry {+ tx: broadcast::Sender<Frame>,+ snap: Arc<RwLock<Snapshot>>,+}++/// 进行中的生成,按会话查。+#[derive(Default)]+pub struct Registry(RwLock<HashMap<Uuid, Entry>>);++/// 一次生成期间握着的把手。发事件、结束时注销。+pub struct Handle {+ conversation_id: Uuid,+ tx: broadcast::Sender<Frame>,+ snap: Arc<RwLock<Snapshot>>,+ registry: Arc<Registry>,+}++impl Handle {+ /// 发一个事件:记进快照,然后广播。+ ///+ /// **广播时仍然握着快照的写锁**,这一点是必需的。只保证「先写后发」+ /// 挡不住重复:接上的人在两步之间订阅,就会既在快照里看到这一段、+ /// 又从广播里再收一次。握着锁发,`attach` 那边握着读锁订阅,两者互斥——+ /// 于是接上的时刻要么整个在这次 emit 之前,要么整个在它之后+ pub async fn emit(&self, frame: Frame) {+ let mut snap = self.snap.write().await;+ snap.apply(&frame);+ // 没有订阅者是常态(人走了),不是错+ let _ = self.tx.send(frame);+ }++ /// 生成结束。**注销之后再接上的人得到的是「没有在跑的」**,+ /// 那时答案已经落库,从库里读就是了+ pub async fn finish(self) {+ self.registry.0.write().await.remove(&self.conversation_id);+ }+}++impl Registry {+ /// 登记一次生成。同一个会话重复登记会顶掉旧的——正常情况下不会发生,+ /// 真发生了也是新的那次说了算+ pub async fn begin(self: &Arc<Self>, conversation_id: Uuid) -> Handle {+ let (tx, _) = broadcast::channel(256);+ let snap = Arc::new(RwLock::new(Snapshot::default()));+ self.0.write().await.insert(+ conversation_id,+ Entry {+ tx: tx.clone(),+ snap: snap.clone(),+ },+ );+ Handle {+ conversation_id,+ tx,+ snap,+ registry: self.clone(),+ }+ }++ /// 接上一次正在跑的生成:拿到此刻的快照,以及之后的增量。+ ///+ /// 返回 `None` = 这个会话没有在跑的生成。**那不是错**,是最常见的情况+ pub async fn attach(+ &self,+ conversation_id: Uuid,+ ) -> Option<(Snapshot, broadcast::Receiver<Frame>)> {+ let map = self.0.read().await;+ let entry = map.get(&conversation_id)?;+ // **握着快照的读锁再订阅。** `emit` 是握着写锁广播的,所以这一段+ // 与任何一次 emit 互斥:拿到的快照与订阅起点严丝合缝,+ // 中间那一小段既不会漏、也不会重+ let guard = entry.snap.read().await;+ let rx = entry.tx.subscribe();+ Some((guard.clone(), rx))+ }+}diff --git a/crates/utopia-server/src/main.rs b/crates/utopia-server/src/main.rsindex 9a1c8a71..34217e0b 100644--- a/crates/utopia-server/src/main.rs+++ b/crates/utopia-server/src/main.rs@@ -11,6 +11,7 @@ mod extraction;mod github_issues;mod ingest_sources;mod jira_issues;+mod live;mod llm_util;mod mappings;mod ontology_index;diff --git a/crates/utopia-server/src/state.rs b/crates/utopia-server/src/state.rsindex e26d9bd1..453a61e0 100644--- a/crates/utopia-server/src/state.rs+++ b/crates/utopia-server/src/state.rs@@ -34,6 +34,8 @@ pub struct AppState {/// 按模型的并发闸门:后台任务调 LLM 前取许可。限额存库,改完即时生效pub model_gates: Arc<crate::llm_util::ModelGates>,pub events: broadcast::Sender<AppEvent>,+ /// 正在生成的回答,按会话查。**刷新页面之后还能接上**(见 `live`)+ pub live: Arc<crate::live::Registry>,}impl AppState {@@ -59,6 +61,7 @@ impl AppState {worker_concurrency: Arc::new(std::sync::atomic::AtomicUsize::new(32)),model_gates: Arc::new(crate::llm_util::ModelGates::default()),events,+ live: Arc::new(crate::live::Registry::default()),}}diff --git a/web/src/api.ts b/web/src/api.tsindex 3ab773b0..3dd9c9e7 100644--- a/web/src/api.ts+++ b/web/src/api.ts@@ -1678,28 +1678,64 @@ export const conversationsApi = {}),};+export interface ChatHandlers {+ onConversation: (id: string) => void;+ onSources: (s: Source[]) => void;+ onStep: (s: ChatStep) => void;+ onDelta: (text: string) => void;+ onDone: () => void;+ onError: (message: string) => void;+ /** 接上一个已经在跑的回答:这是它此刻的样子,**覆盖,不是追加** */+ onSnapshot?: (s: { content: string; steps: ChatStep[]; sources: Source[] }) => void;+ /** 这个会话没有在跑的生成——最常见的答案,不是错误 */+ onIdle?: () => void;+}++/** 接上一个正在生成的回答(刷新页面之后走这里)。+ *+ * 与 `streamChat` 读的是同一条事件流,只是多了开头那份快照。 */+export function reattachChat(+ kbId: string,+ conversationId: string,+ handlers: ChatHandlers,+): () => void {+ return consumeChatStream(+ (signal) =>+ fetch(`/api/v1/kbs/${kbId}/conversations/${conversationId}/stream`, {+ credentials: "include",+ signal,+ }),+ handlers,+ );+}+export function streamChat(kbId: string,body: { conversation_id?: string; message: string },- handlers: {- onConversation: (id: string) => void;- onSources: (s: Source[]) => void;- onStep: (s: ChatStep) => void;- onDelta: (text: string) => void;- onDone: () => void;- onError: (message: string) => void;- },+ handlers: ChatHandlers,): () => void {- const controller = new AbortController();- (async () => {- try {- const res = await fetch(`/api/v1/kbs/${kbId}/chat`, {+ return consumeChatStream(+ (signal) =>+ fetch(`/api/v1/kbs/${kbId}/chat`, {method: "POST",credentials: "include",headers: { "Content-Type": "application/json" },body: JSON.stringify(body),- signal: controller.signal,- });+ signal,+ }),+ handlers,+ );+}++/** SSE 的读法只有一份:发起请求的方式不同,收下来之后的处理完全一样。 */+function consumeChatStream(+ open: (signal: AbortSignal) => Promise<Response>,+ handlers: ChatHandlers,+): () => void {+ const controller = new AbortController();+ (async () => {+ try {+ const res = await open(controller.signal);if (!res.ok || !res.body) {let message = res.statusText;try {@@ -1736,6 +1772,8 @@ export function streamChat(handlers.onStep(JSON.parse(data) as ChatStep);else if (event === "delta")handlers.onDelta((JSON.parse(data) as { text: string }).text);+ else if (event === "snapshot") handlers.onSnapshot?.(JSON.parse(data));+ else if (event === "idle") handlers.onIdle?.();else if (event === "done") handlers.onDone();else if (event === "error") handlers.onError(data);}diff --git a/web/src/pages/Chat.tsx b/web/src/pages/Chat.tsxindex 43b6b234..f4402d49 100644--- a/web/src/pages/Chat.tsx+++ b/web/src/pages/Chat.tsx@@ -26,6 +26,7 @@ import {import { ThinkingOrb, type OrbState } from "thinking-orbs";import {conversationsApi,+ reattachChat,streamChat,type ChatStep,type ConversationRow,@@ -179,6 +180,46 @@ export function Chat() {params: { kbId, conversationId: id },});+ /** 接回一个正在生成的回答。没有在跑的话服务端回 `idle`,什么都不发生。 */+ const attachIfRunning = (id: string, history: Turn[]) => {+ let abort = () => {};+ const stop = reattachChat(kb!.id, id, {+ onConversation: () => {},+ /* **快照到了才建这一轮。** 先摆一个空位再等回答的话,没有在跑的会话+ 上会闪一下空的助手气泡——而那是绝大多数情况。+ 快照是覆盖:它是那个回答此刻的全貌,不是增量 */+ onSnapshot: (s) =>+ liveAnswer.start(+ id,+ [+ ...history,+ {+ role: "assistant",+ content: s.content,+ steps: s.steps.length ? s.steps : undefined,+ sources: s.sources.length ? s.sources : undefined,+ },+ ],+ abort,+ ),+ onSources: (sources) => liveAnswer.patchLast((t) => ({ ...t, sources })),+ onStep: (step) =>+ liveAnswer.patchLast((t) => ({ ...t, steps: [...(t.steps ?? []), step] })),+ onDelta: (text) => liveAnswer.patchLast((t) => ({ ...t, content: t.content + text })),+ onDone: () => {+ liveAnswer.finish();+ invalidateList();+ },+ onError: (message) => {+ liveAnswer.patchLast((t) => ({ ...t, error: message }));+ liveAnswer.finish();+ },+ onIdle: () => {},+ });+ abort = stop;+ abortRef.current = stop;+ };+const loadConversation = async (id: string) => {// 回到正在写的那一场:直接认领,别去库里读——库里要等它写完才有那一行if (liveAnswer.get()?.conversationId === id) {@@ -191,14 +232,21 @@ export function Chat() {try {const { messages } = await conversationsApi.detail(kb!.id, id);sessionStorage.setItem(lastKey(kb!.id), id);- setTurns(- messages.map((m) => ({- role: m.role,- content: m.content,- steps: m.steps.length ? m.steps : undefined,- sources: m.sources.length ? m.sources : undefined,- })),- );+ const history: Turn[] = messages.map((m) => ({+ role: m.role,+ content: m.content,+ steps: m.steps.length ? m.steps : undefined,+ sources: m.sources.length ? m.sources : undefined,+ }));+ setTurns(history);+ /* **刷新之后接回去。** 上面那个 store 只活在这一个页面里;刷新、+ 新标签页、换台机器都拿不到它,而服务端那边生成还在跑。问一句+ 「这个会话有没有在跑的」——没有是最常见的答案,代价是一次会+ 立刻回 `idle` 的请求。+ 最后一条是用户说的话时才问:那正好是「问了但还没答上」的形状 */+ if (history[history.length - 1]?.role === "user") {+ attachIfRunning(id, history);+ }} catch {// 失效链接(会话已删 / 属于别的库):安静回到新对话sessionStorage.removeItem(lastKey(kb!.id));