Files
Genarrative/server-rs/crates/spacetime-module/src/agc_models.rs
T
suzmii 4f3f0f24ff 接入AGC后台模型目录与对话模型选择
新增后台 AGC 模型目录、别名、启停和默认项管理

客户端设置页恢复原状,对话框右下角按别名选择模型

服务端按稳定模型标识映射并校验实际模型白名单

修复 AGC 配套后端端口漂移、启动等待和 SpacetimeDB 版本检查

补充迁移、文档、启动与模型选择测试
2026-09-05 19:10:49 +08:00

67 lines
2.2 KiB
Rust

use crate::*;
#[spacetimedb::table(accessor = agc_model_catalog)]
pub struct AgcModelCatalogRow {
#[primary_key]
pub id: u8,
pub catalog_json: String,
pub revision: u64,
pub updated_at: Timestamp,
}
#[spacetimedb::procedure]
pub fn read_agc_model_catalog(ctx: &mut ProcedureContext) -> Result<String, String> {
let caller = ctx.sender();
ctx.try_with_tx(|tx| {
crate::editor_project_storage::require_editor_generation_runtime_service_identity(
tx, caller,
)?;
Ok(tx
.db
.agc_model_catalog()
.id()
.find(0)
.map(|row| row.catalog_json)
.unwrap_or_else(|| {
serde_json::to_string(&module_runtime::AgcModelCatalog::default())
.expect("default catalog")
}))
})
}
#[spacetimedb::procedure]
pub fn save_agc_model_catalog(
ctx: &mut ProcedureContext,
catalog_json: String,
) -> Result<String, String> {
let catalog: module_runtime::AgcModelCatalog =
serde_json::from_str(&catalog_json).map_err(|_| "模型目录格式无效".to_string())?;
catalog.validate()?;
let caller = ctx.sender();
ctx.try_with_tx(|tx| {
crate::editor_project_storage::require_editor_generation_runtime_service_identity(
tx, caller,
)?;
let mut catalog = catalog.clone();
let previous = tx.db.agc_model_catalog().id().find(0);
let revision = previous.as_ref().map(|r| r.revision).unwrap_or(0);
if catalog.revision != revision {
return Err(module_runtime::AGC_MODEL_CATALOG_CONFLICT.into());
}
catalog.revision = revision.checked_add(1).ok_or("模型目录版本溢出")?;
let catalog_json = serde_json::to_string(&catalog).map_err(|_| "模型目录序列化失败")?;
let row = AgcModelCatalogRow {
id: 0,
catalog_json: catalog_json.clone(),
revision: catalog.revision,
updated_at: tx.timestamp,
};
if previous.is_some() {
tx.db.agc_model_catalog().id().update(row);
} else {
tx.db.agc_model_catalog().insert(row);
}
Ok(catalog_json)
})
}