use super::ir::{AICapabilityIR, ClassDef, EnumDef};
fn quote(raw: &str) -> String {
format!("\"{}\"", raw.replace('\\', "\\\\").replace('"', "\\\""))
}
fn comment_safe(s: &str) -> String {
s.replace(['\n', '\r'], " ")
}
fn header(ir: &AICapabilityIR, what: &str) -> String {
format!(
"// Generated by DomainForge (`--format baml`) from {}.\n\
// {}\n\
// Created: {}. Target BAML syntax: {}. Do not edit by hand.\n\n",
comment_safe(&ir.model_ref),
comment_safe(what),
comment_safe(&ir.created_at),
super::ir::BAML_TARGET_VERSION,
)
}
fn render_enum(e: &EnumDef) -> String {
if e.variants.is_empty() {
return format!("// enum {} has no variants in this model.\n", e.name);
}
let mut s = format!("/// {}\nenum {} {{\n", comment_safe(&e.doc), e.name);
for v in &e.variants {
if v.ident == v.raw {
s.push_str(&format!(" {}\n", v.ident));
} else {
s.push_str(&format!(" {} // {}\n", v.ident, comment_safe(&v.raw)));
}
}
s.push_str("}\n");
s
}
fn render_class(c: &ClassDef) -> String {
let mut s = format!("/// {}\nclass {} {{\n", comment_safe(&c.doc), c.name);
for f in &c.fields {
s.push_str(&format!(
" /// {}\n {} {}\n",
comment_safe(&f.doc),
f.name,
f.type_ref
));
}
s.push_str("}\n");
s
}
pub fn domain(ir: &AICapabilityIR) -> String {
let mut s = header(
ir,
"Domain types: closed enums and the authority request/ruling schema.",
);
for e in [
&ir.entity_enum,
&ir.role_enum,
&ir.resource_enum,
&ir.operation_enum,
&ir.decision_enum,
] {
s.push_str(&render_enum(e));
s.push('\n');
}
s.push_str(&render_class(&ir.request_class));
s.push('\n');
s.push_str(&render_class(&ir.decision_class));
s
}
pub fn functions(ir: &AICapabilityIR) -> String {
let mut s = header(
ir,
"Policy-decision capability: input is the authority request, output is the ruling.",
);
let mut policies = String::new();
if ir.policy_lines.is_empty() {
policies.push_str(" (no authority policies were declared)\n");
} else {
for line in &ir.policy_lines {
policies.push_str(&format!(" - {}\n", comment_safe(line)));
}
}
s.push_str(&format!(
"/// Decide whether an actor holding `actor_role` may perform `operation`\n\
/// on `resource_type`, under the model's authority policies. Capability id:\n\
/// {cap}.\n\
function {func}(request: {req}) -> {ruling} {{\n\
\x20 client {client}\n\
\x20 prompt #\"\n\
\x20 You are the authorization decision service for the \"{model}\" domain.\n\
\x20 Decide the outcome for the request below and justify it strictly from\n\
\x20 the governing policies. If no policy applies, answer Unknown and do NOT\n\
\x20 infer permission. If a required fact is missing, answer Escalate.\n\n\
\x20 Governing policies:\n\
{policies}\n\
\x20 Request:\n\
\x20 actor_role: {{{{ request.actor_role }}}}\n\
\x20 operation: {{{{ request.operation }}}}\n\
\x20 resource_type: {{{{ request.resource_type }}}}\n\n\
\x20 {{{{ ctx.output_format }}}}\n\
\x20 \"#\n\
}}\n",
cap = ir.capability_id,
func = ir.function_name,
req = ir.request_class.name,
ruling = ir.decision_class.name,
client = ir.client_name,
model = comment_safe(&ir.model_ref),
policies = policies.trim_end_matches('\n'),
));
s
}
pub fn tests(ir: &AICapabilityIR) -> String {
let mut s = header(
ir,
"Tests/examples seeded from the DomainForge authority resolver's decisions.",
);
if ir.tests.is_empty() {
s.push_str(
"// No decisive authority cases were produced for this model \
(every tuple resolved to Unknown).\n",
);
return s;
}
for t in &ir.tests {
let refs = if t.applicable_policies.is_empty() {
"none".to_string()
} else {
comment_safe(&t.applicable_policies.join(", "))
};
s.push_str(&format!(
"// Authority case {id}: a \"{role}\" performing \"{op}\" on \"{res}\"\n\
// resolves to decision \"{decision}\" (reason: {reason}; policies: {refs}).\n\
test {name} {{\n\
\x20 functions [{func}]\n\
\x20 args {{\n\
\x20 request {{\n\
\x20 actor_role {role_q}\n\
\x20 operation {op_q}\n\
\x20 resource_type {res_q}\n\
\x20 }}\n\
\x20 }}\n\
}}\n\n",
id = t.id,
role = comment_safe(&t.role),
op = comment_safe(&t.operation),
res = comment_safe(&t.resource_type),
decision = t.decision,
reason = comment_safe(&t.reason_code),
refs = refs,
name = t.name,
func = ir.function_name,
role_q = quote(&t.role),
op_q = quote(&t.operation),
res_q = quote(&t.resource_type),
));
}
s
}
pub fn clients(ir: &AICapabilityIR) -> String {
let mut s = header(
ir,
"Client + generator placeholders. Uncomment one client and one generator to run.",
);
s.push_str(&format!(
"// DomainForge does not lock you into an LLM vendor: the `{client}` client the\n\
// function references is intentionally left undefined. Define it yourself by\n\
// uncommenting and completing ONE of the blocks below, then set the matching\n\
// environment variable. See https://docs.boundaryml.com/ for provider options.\n\
//\n\
// client<llm> {client} {{\n\
// provider <your-provider> // e.g. openai, anthropic, google-ai, vertex-ai, ollama\n\
// options {{\n\
// model <your-model>\n\
// api_key env.YOUR_API_KEY\n\
// }}\n\
// }}\n\
//\n\
// A generator block tells `baml-cli generate` which client library to emit:\n\
//\n\
// generator target {{\n\
// output_type \"python/pydantic\" // or \"typescript\", \"ruby/sorbet\", ...\n\
// output_dir \"../baml_client\"\n\
// version \"{version}\"\n\
// }}\n",
client = ir.client_name,
version = super::ir::BAML_TARGET_VERSION,
));
s
}
pub fn readme(ir: &AICapabilityIR) -> String {
format!(
"# DomainForge BAML capability\n\n\
Generated from `{model}` at {created}.\n\n\
This is a [BAML](https://docs.boundaryml.com/) project (target syntax \
`{version}`) that turns the model's authority policies into a typed,\n\
testable LLM function.\n\n\
## Layout\n\n\
- `baml_src/domain.baml` — enums for the closed role/resource/operation\n \
domains and the `{req}` / `{ruling}` schemas.\n\
- `baml_src/functions.baml` — the `{func}` function; its prompt embeds the\n \
governing policy semantics.\n\
- `baml_src/tests.baml` — one `test` per resolver-grounded authority case,\n \
seeded directly from the DomainForge authority resolver (not hand-written).\n\
- `baml_src/clients.baml` — commented placeholders. **You** pick an LLM\n \
provider and supply credentials; the generated code is vendor-neutral.\n\n\
## Make it runnable\n\n\
1. Uncomment one `client<llm>` block and one `generator` block in\n \
`baml_src/clients.baml` and fill in your provider, model, and API key env var.\n\
2. Install the BAML CLI (pin the version to `{version}`).\n\
3. `baml-cli generate` to emit the client library, then run the tests.\n\n\
Provenance: capability id `{cap}`, source model hash `{hash}`,\n\
generator `domainforge {gen}`.\n",
model = ir.model_ref,
created = ir.created_at,
version = super::ir::BAML_TARGET_VERSION,
req = ir.request_class.name,
ruling = ir.decision_class.name,
func = ir.function_name,
cap = ir.capability_id,
hash = ir.model_hash,
gen = ir.generator_version,
)
}