- plugin-llm 新 crate:llm/upstreams/usage_filter/llm_audit/stats/handlers/api/config/models/store - 下行契约(downstream_key/identity/check_quota/fail)与记账原语(usage::bump/read/bump_day)已下沉 ipass-core - usage_filter 改走 loc::enabled(Switch::Llm) + core::plugin::ctx_index(解除对 gateway 私有 ModuleConfig 的依赖) - gateway 收缩为壳:LlmHandler/LlmHealthHandler/AuditHandler 随 LlmPlugin 装配; /api/admin/trend 留 gateway(需 users+upstreams+apix 三方名册,组合根职责) - 行为等价:262 项回归全绿(gateway 102 + llm 160)
36 lines
1.0 KiB
Rust
36 lines
1.0 KiB
Rust
//! 上游表的文件读写(复用内核 store 的原子写 + 全局文件写锁)。
|
||
//!
|
||
//! `data/llm-upstreams.json` 含上游真实 API Key,**敏感级别等同 `users.json`**。
|
||
|
||
use std::path::PathBuf;
|
||
|
||
use ipass_core::{state, store};
|
||
|
||
use crate::models::UpstreamTable;
|
||
|
||
fn upstreams_path() -> PathBuf {
|
||
store::path_in_data("llm-upstreams.json")
|
||
}
|
||
|
||
pub fn read_upstreams() -> UpstreamTable {
|
||
store::read_json(&upstreams_path(), UpstreamTable::new())
|
||
}
|
||
|
||
pub fn write_upstreams(data: &UpstreamTable) -> Result<(), String> {
|
||
store::write_json(&upstreams_path(), data)
|
||
}
|
||
|
||
/// 加锁「读-改-写」上游表。
|
||
///
|
||
/// 闭包返回 `Ok(T)` 表示提交,`Err(msg)` 表示放弃(不写盘)。
|
||
pub fn update_upstreams<T, F>(f: F) -> Result<T, String>
|
||
where
|
||
F: FnOnce(&mut UpstreamTable) -> Result<T, String>,
|
||
{
|
||
let _guard = state::file_lock().ok_or("内部错误:状态锁不可用")?;
|
||
let mut data = read_upstreams();
|
||
let out = f(&mut data)?;
|
||
write_upstreams(&data)?;
|
||
Ok(out)
|
||
}
|