Files
Genarrative/apps/ai-game-creator-shell/src-tauri/src/agent/runtime_adapter.rs
T
lhk229 47a3240e81
Project CI / Repository checks (pull_request) Successful in 4m51s
Project CI / Frontend tests (pull_request) Successful in 7m39s
Project CI / Backend tests (pull_request) Successful in 11m23s
Project CI / Native shell tests (pull_request) Failing after 13m38s
按 rustfmt 修正 V1 删除后残留的格式偏差
Repository checks 门禁的 cargo fmt --check 要求,仅格式化、无行为变化
2026-09-08 11:25:17 +00:00

143 lines
5.8 KiB
Rust

use super::*;
use agent_runtime_core::{AgentCatalog, AgentDescriptor, RunProfileCatalog, RunProfileDefinition};
fn build_game_creator_runtime_agent_catalog() -> Result<AgentCatalog, String> {
let mut agents = vec![AgentDescriptor::try_new(
GAME_CREATOR_PROJECT_SUPERVISOR_AGENT_ID,
PROJECT_SUPERVISOR_AGENT_DEFINITION.id,
std::iter::empty::<&str>(),
)
.and_then(|agent| {
agent.with_metadata(serde_json::json!({
"groupId": PROJECT_SUPERVISOR_AGENT_DEFINITION.id,
"roleLabel": PROJECT_SUPERVISOR_AGENT_ROLES[0].role,
"toolId": PROJECT_SUPERVISOR_AGENT_ROLES[0].tool_id,
"capabilityAuthority": "game-creator-tool-policy-snapshot"
}))
})
.map_err(|error| format!("AI 游戏创作 Agent catalog 无效:{error}"))?];
for group in GAME_CREATOR_AGENT_GROUP_DEFINITIONS {
for role in group.roles {
agents.push(
AgentDescriptor::try_new(role.task_id, role.id, std::iter::empty::<&str>())
.and_then(|agent| {
agent.with_metadata(serde_json::json!({
"groupId": group.id,
"groupLabel": group.label,
"roleLabel": role.role,
"toolId": role.tool_id,
"capabilityAuthority": "game-creator-tool-policy-snapshot"
}))
})
.map_err(|error| format!("AI 游戏创作 Agent catalog 无效:{error}"))?,
);
}
}
let catalog = AgentCatalog::try_new(agents)
.map_err(|error| format!("AI 游戏创作 Agent catalog 无效:{error}"))?;
let task_graph = build_game_creation_seed_task_graph("AI 游戏创作 Agent catalog 验证")
.map_err(|error| format!("AI 游戏创作任务图无效:{error}"))?;
platform_agent::validate_game_creation_task_agents(&task_graph, &catalog)
.map_err(|error| format!("AI 游戏创作 Agent catalog 与任务图不一致:{error}"))?;
Ok(catalog)
}
pub(crate) fn game_creator_runtime_agent_catalog() -> Result<&'static AgentCatalog, String> {
static CATALOG: OnceLock<Result<AgentCatalog, String>> = OnceLock::new();
CATALOG
.get_or_init(build_game_creator_runtime_agent_catalog)
.as_ref()
.map_err(Clone::clone)
}
fn build_game_creator_runtime_run_profile_catalog() -> Result<RunProfileCatalog, String> {
let standard = RunProfileDefinition::try_new(
AGENT_RUNTIME_RUN_PROFILE_STANDARD,
std::iter::empty::<&str>(),
"game-creator-standard-completion",
)
.and_then(|profile| {
profile.with_metadata(serde_json::json!({
"capabilityAuthority": "game-creator-tool-policy-snapshot",
"completionAuthority": "game-creator-runtime-finalization"
}))
})
.map_err(|error| format!("AI 游戏创作 Run Profile catalog 无效:{error}"))?;
let autonomous = RunProfileDefinition::try_new(
AGENT_RUNTIME_RUN_PROFILE_AUTONOMOUS_GAME_BUILD,
std::iter::empty::<&str>(),
"game-creator-autonomous-completion",
)
.and_then(|profile| {
profile.with_metadata(serde_json::json!({
"rootAgentId": GAME_CREATOR_PROJECT_SUPERVISOR_AGENT_ID,
"capabilityAuthority": "game-creator-tool-policy-snapshot",
"completionAuthority": AGENT_RUNTIME_AUTONOMOUS_COMPLETION_CONTRACT_SCHEMA_VERSION
}))
})
.map_err(|error| format!("AI 游戏创作 Run Profile catalog 无效:{error}"))?;
RunProfileCatalog::try_new([standard, autonomous])
.map_err(|error| format!("AI 游戏创作 Run Profile catalog 无效:{error}"))
}
pub(crate) fn game_creator_runtime_run_profile_catalog(
) -> Result<&'static RunProfileCatalog, String> {
static CATALOG: OnceLock<Result<RunProfileCatalog, String>> = OnceLock::new();
CATALOG
.get_or_init(build_game_creator_runtime_run_profile_catalog)
.as_ref()
.map_err(Clone::clone)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn game_creator_runtime_agent_catalog_matches_the_existing_role_directory() {
let catalog = game_creator_runtime_agent_catalog().expect("agent catalog");
// catalog 恰好是「一个组外单节点(Supervisor)+ 各专业组角色」。
let mut expected =
std::collections::BTreeSet::from(
[GAME_CREATOR_PROJECT_SUPERVISOR_AGENT_ID.to_string()],
);
for group in GAME_CREATOR_AGENT_GROUP_DEFINITIONS {
expected.extend(group.roles.iter().map(|role| role.task_id.to_string()));
}
assert_eq!(
catalog
.iter()
.map(|agent| agent.id().to_string())
.collect::<std::collections::BTreeSet<_>>(),
expected
);
assert_eq!(
catalog
.get(GAME_CREATOR_PROJECT_SUPERVISOR_AGENT_ID)
.map(AgentDescriptor::role),
Some(PROJECT_SUPERVISOR_AGENT_DEFINITION.id)
);
}
#[test]
fn game_creator_runtime_run_profiles_are_adapter_registered() {
let profiles = game_creator_runtime_run_profile_catalog().expect("profile catalog");
assert_eq!(
profiles
.iter()
.map(|profile| profile.id())
.collect::<Vec<_>>(),
vec![
AGENT_RUNTIME_RUN_PROFILE_STANDARD,
AGENT_RUNTIME_RUN_PROFILE_AUTONOMOUS_GAME_BUILD
]
);
assert_eq!(
profiles
.get(AGENT_RUNTIME_RUN_PROFILE_AUTONOMOUS_GAME_BUILD)
.map(RunProfileDefinition::completion_policy_id),
Some("game-creator-autonomous-completion")
);
}
}