Files
Genarrative/server-rs/crates/api-server/src/creation_agent_anchor_templates.rs
kdletters cbc27bad4a
Some checks failed
CI / verify (push) Has been cancelled
init with react+axum+spacetimedb
2026-04-26 18:06:23 +08:00

68 lines
2.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use std::sync::OnceLock;
use serde::Deserialize;
const ANCHOR_TEMPLATE_CONFIG: &str = include_str!("creation_agent_anchor_templates.json");
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CreationAgentAnchorTemplateConfig {
templates: Vec<CreationAgentAnchorTemplate>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CreationAgentAnchorTemplate {
pub template_id: String,
pub display_name: String,
pub creation_goal: String,
pub anchor_questions: Vec<CreationAgentAnchorQuestion>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CreationAgentAnchorQuestion {
pub key: String,
pub label: String,
pub question: String,
pub required_effect: String,
}
static ANCHOR_TEMPLATES: OnceLock<CreationAgentAnchorTemplateConfig> = OnceLock::new();
pub(crate) fn get_creation_agent_anchor_template(
template_id: &str,
) -> Option<&'static CreationAgentAnchorTemplate> {
load_creation_agent_anchor_templates()
.templates
.iter()
.find(|template| template.template_id == template_id)
}
pub(crate) fn render_anchor_question_block(template: &CreationAgentAnchorTemplate) -> String {
let mut lines = vec![
format!("模板目标:{}", template.creation_goal),
"".to_string(),
];
lines.push("本模板只通过以下锚点问题体现差异:".to_string());
for (index, question) in template.anchor_questions.iter().enumerate() {
lines.push(format!(
"{}. {}{} / {}{};达成效果:{}",
index + 1,
question.label,
question.key,
template.display_name,
question.question,
question.required_effect
));
}
lines.join("\n")
}
fn load_creation_agent_anchor_templates() -> &'static CreationAgentAnchorTemplateConfig {
ANCHOR_TEMPLATES.get_or_init(|| {
serde_json::from_str(ANCHOR_TEMPLATE_CONFIG)
.expect("creation_agent_anchor_templates.json 必须是合法配置")
})
}