refactor: 移除文件状态并修复工具上下文关联

This commit is contained in:
2026-06-16 20:18:34 +08:00
parent 1d78567102
commit f608c27f8a
9 changed files with 552 additions and 402 deletions
+1 -1
View File
@@ -31,8 +31,8 @@
pub mod models;
use sqlx::postgres::PgPoolOptions;
use sqlx::PgPool;
use sqlx::postgres::PgPoolOptions;
use std::time::Duration;
/// ## 数据库管理器
+39 -1
View File
@@ -14,7 +14,13 @@ use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use crate::state::AuthState;
/// 认证状态
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthState {
pub token: String,
pub account_id: String,
pub base_url: String,
}
// ─── 认证状态 ───
@@ -50,6 +56,38 @@ pub async fn save_auth(pool: &PgPool, auth: &AuthState) -> Result<(), String> {
Ok(())
}
// ─── 运行时状态 (get_updates_buf) ───
/// 从数据库加载运行时状态 (get_updates_buf)
pub async fn load_runtime(pool: &PgPool) -> Option<String> {
let row = sqlx::query_as::<_, (serde_json::Value,)>(&
"SELECT value FROM app_state WHERE key = 'runtime'",
)
.fetch_optional(pool)
.await
.ok()??;
row.0.as_str().map(|s| s.to_string())
}
/// 保存运行时状态 (get_updates_buf) 到数据库
pub async fn save_runtime(pool: &PgPool, buf: &str) -> Result<(), String> {
let value = serde_json::Value::String(buf.to_string());
sqlx::query(
r#"
INSERT INTO app_state (key, value, updated_at)
VALUES ('runtime', $1, NOW())
ON CONFLICT (key) DO UPDATE
SET value = EXCLUDED.value, updated_at = NOW()
"#,
)
.bind(&value)
.execute(pool)
.await
.map_err(|e| format!("保存 runtime 到数据库失败: {}", e))?;
Ok(())
}
// ─── 聊天记录 ───
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]