Files
Genarrative/apps/ai-game-creator-shell/src-tauri/src/project/memory.rs
T
AIGameCreator App be89296492 拆分 AI 游戏创作客户端大型模块
拆分 App 认证、壳层、运行配置与项目摘要模块
拆分 Tauri 项目能力与 Rust 测试领域模块
拆分界面测试与 Agent Runtime 真实 E2E 套件
补充源码扫描和客户端模块化文档约定
2026-07-21 22:53:29 +08:00

131 lines
4.5 KiB
Rust

use super::*;
pub(crate) fn read_local_game_memory_at(
root: &Path,
scope: &str,
) -> Result<LocalGameMemoryResult, String> {
let (scope, path) = memory_file_path(root, scope)?;
match fs::read_to_string(&path) {
Ok(content) => Ok(LocalGameMemoryResult {
scope: scope.to_string(),
path: path.to_string_lossy().into_owned(),
content,
exists: true,
}),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(LocalGameMemoryResult {
scope: scope.to_string(),
path: path.to_string_lossy().into_owned(),
content: String::new(),
exists: false,
}),
Err(error) => Err(format!("读取记忆失败:{}: {error}", path.display())),
}
}
pub(crate) fn read_local_agent_memory_at(
root: &Path,
task_id: &str,
) -> Result<LocalAgentMemoryResult, String> {
let relative_path = agent_role_memory_relative_path_for_task(task_id)?;
let path = resolve_local_project_path(root, &relative_path)?;
match fs::read_to_string(&path) {
Ok(content) => Ok(LocalAgentMemoryResult {
task_id: task_id.to_string(),
path: path.to_string_lossy().into_owned(),
content,
exists: true,
}),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(LocalAgentMemoryResult {
task_id: task_id.to_string(),
path: path.to_string_lossy().into_owned(),
content: String::new(),
exists: false,
}),
Err(error) => Err(format!("读取 Agent 记忆失败:{}: {error}", path.display())),
}
}
pub(crate) fn write_local_agent_memory_at(
root: &Path,
task_id: &str,
content: &str,
) -> Result<LocalAgentMemoryResult, String> {
let relative_path = agent_role_memory_relative_path_for_task(task_id)?;
let path = resolve_local_project_path(root, &relative_path)?;
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)
.map_err(|error| format!("创建 Agent 记忆目录失败:{}: {error}", parent.display()))?;
}
fs::write(&path, content)
.map_err(|error| format!("写入 Agent 记忆失败:{}: {error}", path.display()))?;
Ok(LocalAgentMemoryResult {
task_id: task_id.to_string(),
path: path.to_string_lossy().into_owned(),
content: content.to_string(),
exists: true,
})
}
pub(crate) fn write_local_game_memory_at(
root: &Path,
scope: &str,
content: &str,
) -> Result<LocalGameMemoryResult, String> {
let (scope, path) = memory_file_path(root, scope)?;
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)
.map_err(|error| format!("创建记忆目录失败:{}: {error}", parent.display()))?;
}
fs::write(&path, content)
.map_err(|error| format!("写入记忆失败:{}: {error}", path.display()))?;
Ok(LocalGameMemoryResult {
scope: scope.to_string(),
path: path.to_string_lossy().into_owned(),
content: content.to_string(),
exists: true,
})
}
pub(crate) fn delete_local_game_memory_at(
root: &Path,
scope: &str,
) -> Result<LocalGameMemoryResult, String> {
let (scope, path) = memory_file_path(root, scope)?;
match fs::remove_file(&path) {
Ok(()) => Ok(LocalGameMemoryResult {
scope: scope.to_string(),
path: path.to_string_lossy().into_owned(),
content: String::new(),
exists: false,
}),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(LocalGameMemoryResult {
scope: scope.to_string(),
path: path.to_string_lossy().into_owned(),
content: String::new(),
exists: false,
}),
Err(error) => Err(format!("删除记忆失败:{}: {error}", path.display())),
}
}
pub(crate) fn memory_file_path<'a>(
root: &Path,
scope: &'a str,
) -> Result<(&'a str, PathBuf), String> {
match scope {
"short" | "session" => Ok((
"short",
resolve_local_project_path(root, "memory/session.md")?,
)),
"long" | "project" => Ok((
"long",
resolve_local_project_path(root, "memory/project.md")?,
)),
"blackboard" => Ok((
"blackboard",
resolve_local_project_path(root, PROJECT_BLACKBOARD_MEMORY_PATH)?,
)),
_ => Err("记忆范围只能是 short、long 或 blackboard".to_string()),
}
}