ec3a187dd7
Project CI / AI game creator shell Rust crates (push) Successful in 1m11s
Project CI / AI game creator shell Rust smoke (push) Successful in 1m34s
Project CI / Backend tests (push) Successful in 4m46s
Project CI / AI game creator shell Rust lane 2/2 (push) Successful in 8m31s
Project CI / AI game creator shell Rust lane 1/2 (push) Successful in 9m41s
Project CI / Frontend tests (push) Successful in 2m19s
Project CI / Native shell tests (push) Successful in 7m5s
Project CI / AI game creator shell web tests (push) Successful in 1m54s
Project CI / Repository checks (push) Successful in 2m36s
Reviewed-on: https://git.genarrative.world/git/GenarrativeAI/Genarrative/pulls/503 Co-authored-by: Linghong <ink29535@proton.me> Co-committed-by: Linghong <ink29535@proton.me>
117 lines
3.9 KiB
Rust
117 lines
3.9 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)?;
|
|
let prepared = prepare_game_creator_private_path_for_read(&path, false, "游戏记忆")?;
|
|
if !prepared {
|
|
return Ok(LocalGameMemoryResult {
|
|
scope: scope.to_string(),
|
|
path: path.to_string_lossy().into_owned(),
|
|
content: String::new(),
|
|
exists: false,
|
|
});
|
|
}
|
|
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)?;
|
|
let prepared = prepare_game_creator_private_path_for_read(&path, false, "Agent 记忆")?;
|
|
if !prepared {
|
|
return Ok(LocalAgentMemoryResult {
|
|
task_id: task_id.to_string(),
|
|
path: path.to_string_lossy().into_owned(),
|
|
content: String::new(),
|
|
exists: false,
|
|
});
|
|
}
|
|
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)?;
|
|
crate::write_game_creator_private_file(&path, content.as_bytes(), "Agent 记忆")?;
|
|
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)?;
|
|
crate::write_game_creator_private_file(&path, content.as_bytes(), "游戏记忆")?;
|
|
Ok(LocalGameMemoryResult {
|
|
scope: scope.to_string(),
|
|
path: path.to_string_lossy().into_owned(),
|
|
content: content.to_string(),
|
|
exists: true,
|
|
})
|
|
}
|
|
|
|
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()),
|
|
}
|
|
}
|