use super::*;
pub(super) fn tool_error(message: impl Into<String>) -> CallToolResult {
CallToolResult::structured_error(json!({ "ok": false, "error": message.into() }))
}
pub(super) fn tool_text(i18n: &crate::text::Texts, key: &str, args: &[(&str, String)]) -> String {
let args: Vec<(&str, pikopiko::lang::Arg)> =
args.iter().map(|(k, v)| (*k, v.clone().into())).collect();
i18n.tr_args(key, &args)
}
pub(super) fn resolve_dialect(
name: Option<&str>,
i18n: &crate::text::Texts,
) -> Result<Dialect, CallToolResult> {
match name {
None => Ok(Dialect::PIKOPIKO),
Some(n) => Dialect::builtin(n).ok_or_else(|| {
let names = Dialect::BUILTIN
.iter()
.map(|d| d.name)
.collect::<Vec<_>>()
.join(", ");
tool_error(tool_text(
i18n,
"mcp-tool-unknown-dialect",
&[("name", n.to_string()), ("names", names)],
))
}),
}
}
pub(super) struct Compiled {
pub(super) song: Option<Song>,
pub(super) check: CheckReport,
}
pub(super) fn compile(
mml: &str,
dialect: Option<&str>,
includes: Option<&std::collections::BTreeMap<String, String>>,
libs: &[pikopiko::synth::tones::user::UserLibrary],
i18n: &crate::text::Texts,
) -> Result<Compiled, CallToolResult> {
let total = mml.len() + includes.map_or(0, |m| m.iter().map(|(k, v)| k.len() + v.len()).sum());
if total > MAX_SOURCE_BYTES {
return Err(tool_error(tool_text(
i18n,
"mcp-tool-too-large",
&[
("total", total.to_string()),
("max", MAX_SOURCE_BYTES.to_string()),
],
)));
}
let dialect = resolve_dialect(dialect, i18n)?;
let sources = pikopiko::compiler::include::Sources::new(
includes
.into_iter()
.flatten()
.map(|(k, v)| (k.clone(), v.clone())),
);
let loader = pikopiko::synth::tones::user::WithLibraries::new(Some(&sources), libs);
let opts = CompileOptions {
dialect,
synths: pikopiko::synth::all(),
include: Some(&loader),
..Default::default()
};
let (song, diags) = pikopiko::compile_with(mml, &opts);
Ok(Compiled {
song,
check: CheckReport::new(diags)
.in_lang(i18n.lang())
.with_messages(|d| i18n.diagnostic(d)),
})
}
impl PikoServer {
pub(super) fn compile_song(
&self,
mml: &str,
dialect: Option<&str>,
includes: Option<&std::collections::BTreeMap<String, String>>,
what: &str,
) -> Result<(Song, CheckReport), CallToolResult> {
let c = compile(mml, dialect, includes, &self.config.tones, &self.i18n)?;
match c.song {
Some(song) => Ok((song, c.check)),
None => Err(failed(&c.check, what, &self.i18n)),
}
}
}
pub(super) fn report_json(check: &CheckReport, extra: Vec<(&str, Value)>) -> Value {
let mut m = match serde_json::to_value(check) {
Ok(Value::Object(m)) => m,
_ => Map::new(),
};
for (k, v) in extra {
m.insert(k.to_string(), v);
}
Value::Object(m)
}
pub(super) fn failed(check: &CheckReport, what: &str, i18n: &crate::text::Texts) -> CallToolResult {
let message = tool_text(
i18n,
"mcp-tool-failed",
&[
("what", i18n.tr(what)),
("errors", check.errors.to_string()),
],
);
CallToolResult::structured_error(report_json(check, vec![("error", Value::String(message))]))
}
pub(super) use crate::inline::base64;
pub(super) fn fnv1a(parts: &[&[u8]]) -> u64 {
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
for p in parts {
for &b in *p {
h ^= u64::from(b);
h = h.wrapping_mul(0x0000_0100_0000_01b3);
}
h ^= 0xff; h = h.wrapping_mul(0x0000_0100_0000_01b3);
}
h
}
pub(super) fn valid_file_name(n: &str) -> bool {
(1..=64).contains(&n.len())
&& n.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
}