Elffuss BraidHub

kikocis/utopia public

dev · 4.7 KiBRawPermalink
1use sqlx::PgPool;
2use std::path::PathBuf;
3use std::sync::Arc;
4use tokio::sync::broadcast;
5use utopia_core::config::AppConfig;
6use utopia_search::SearchIndex;
7use uuid::Uuid;
8
9/// 服务内事件(SSE 推送给前端做局部刷新)。
10#[derive(Clone, Debug, serde::Serialize)]
11pub struct AppEvent {
12 /// None = 不属于任何库。告警的角标是跨库的,而系统级告警根本没有库
13 pub kb_id: Option<Uuid>,
14 /// document = 文档摄入/抽取状态变化;review = 审核队列变化;
15 /// alert = 告警中心有变动
16 pub kind: &'static str,
17 pub document_id: Option<Uuid>,
18}
19
20#[derive(Clone)]
21pub struct AppState {
22 pub pool: PgPool,
23 pub jwt_secret: String,
24 pub search: Arc<SearchIndex>,
25 /// Charter(内置文档)内存索引:chat 的 search_docs 工具用
26 pub docs: Arc<utopia_search::DocsIndex>,
27 /// 原始文件字节的存取接缝(内容寻址,key = sha256);当前实现为本地磁盘
28 pub blob: Arc<dyn crate::blob::BlobStore>,
29 pub open_registration: bool,
30 /// 强制 Secure cookie(配置项);未强制时按请求的 X-Forwarded-Proto 逐次判定
31 pub cookie_secure: bool,
32 /// worker 并发数:调度循环每轮热读——系统设置改动即时生效
33 pub worker_concurrency: Arc<std::sync::atomic::AtomicUsize>,
34 /// 按模型的并发闸门:后台任务调 LLM 前取许可。限额存库,改完即时生效
35 pub model_gates: Arc<crate::llm_util::ModelGates>,
36 pub events: broadcast::Sender<AppEvent>,
37 /// 正在生成的回答,按会话查。**刷新页面之后还能接上**(见 `live`)
38 pub live: Arc<crate::live::Registry>,
39}
40
41impl AppState {
42 /// `jwt_secret` 由入口解析:环境变量给了就是它,否则是库里那条(首启时生成)。
43 /// 不从 cfg 里取,是因为到这一步它必须已经是确定的一个值,而不是 Option。
44 pub fn new(
45 pool: PgPool,
46 cfg: &AppConfig,
47 search: Arc<SearchIndex>,
48 jwt_secret: String,
49 ) -> Self {
50 let (events, _) = broadcast::channel(256);
51 let data_dir = PathBuf::from(&cfg.data_dir);
52 let blob = Arc::new(crate::blob::LocalBlobStore::new(data_dir.join("files")));
53 Self {
54 pool,
55 jwt_secret,
56 search,
57 docs: Arc::new(crate::docs_corpus::build_index()),
58 blob,
59 open_registration: cfg.open_registration,
60 cookie_secure: cfg.cookie_secure,
61 worker_concurrency: Arc::new(std::sync::atomic::AtomicUsize::new(32)),
62 model_gates: Arc::new(crate::llm_util::ModelGates::default()),
63 events,
64 live: Arc::new(crate::live::Registry::default()),
65 }
66 }
67
68 /// 无订阅者时 send 返回 Err——正常情况,静默忽略。
69 pub fn emit_document(&self, kb_id: Uuid, document_id: Uuid) {
70 let _ = self.events.send(AppEvent {
71 kb_id: Some(kb_id),
72 kind: "document",
73 document_id: Some(document_id),
74 });
75 }
76
77 pub fn emit_review(&self, kb_id: Uuid) {
78 let _ = self.events.send(AppEvent {
79 kb_id: Some(kb_id),
80 kind: "review",
81 document_id: None,
82 });
83 }
84
85 /// 一句记忆抽出了等人点头的事实(0015)。对话里那张确认卡按这个刷新——
86 /// 抽取是异步的,卡片只能在任务完成时长出来,而不是在助手回话的那一刻
87 pub fn emit_pending(&self, kb_id: Uuid) {
88 let _ = self.events.send(AppEvent {
89 kb_id: Some(kb_id),
90 kind: "pending",
91 document_id: None,
92 });
93 }
94
95 /// 图变了。推理往图里加过边之后要发一次——它不经过文档管道,
96 /// 而 `document` 那条事件是文档管道专用的
97 pub fn emit_graph(&self, kb_id: Uuid) {
98 let _ = self.events.send(AppEvent {
99 kb_id: Some(kb_id),
100 kind: "graph",
101 document_id: None,
102 });
103 }
104
105 pub fn emit_source(&self, kb_id: Uuid) {
106 let _ = self.events.send(AppEvent {
107 kb_id: Some(kb_id),
108 kind: "source",
109 document_id: None,
110 });
111 }
112
113 /// 告警有变动。**不带任何数据,也不判权限**——收到的人一律重取列表,
114 /// 而"谁能看见什么"在列表查询里判且只判一次。
115 ///
116 /// 代价是没权限的人也会被叫醒重取一次,拿到的仍是空。换来的是推送这条路上
117 /// 一行权限逻辑都没有,不存在"推送和列表判得不一样"这种漏。
118 pub fn emit_alert(&self) {
119 let _ = self.events.send(AppEvent {
120 kb_id: None,
121 kind: "alert",
122 document_id: None,
123 });
124 }
125}