#![allow(
clippy::disallowed_types,
reason = "Bounded local input is read inside spawn_blocking"
)]
use std::io::Read;
use clap::{Args, Subcommand};
#[cfg(feature = "gitflow-jev")]
use gitflow_core::decision::DecisionEngine;
use gitflow_core::decision::{DecisionRequest, Question};
use crate::OutputFormat;
const MAX_INPUT_BYTES: u64 = 131_072;
#[derive(Debug, Subcommand)]
pub enum DecideCommand {
Noul(DecideArgs),
Choice(DecideArgs),
Score(DecideArgs),
Batch(DecideArgs),
Eval(EvalArgs),
Calibrate(CalibrateArgs),
Compare(CompareArgs),
}
#[derive(Debug, Args)]
pub struct EvalArgs {
#[arg(long)]
pub fixtures: String,
#[arg(long, conflicts_with = "live")]
pub responses: Option<String>,
#[arg(long)]
pub live: bool,
#[arg(long, requires = "live")]
pub save_responses: Option<String>,
#[arg(long, requires = "live")]
pub input_price_per_million_usd: Option<f64>,
#[arg(long, requires = "live")]
pub output_price_per_million_usd: Option<f64>,
}
#[derive(Debug, Args)]
pub struct CalibrateArgs {
#[arg(long)]
pub report: String,
#[arg(long)]
pub question: String,
#[arg(long)]
pub target_accuracy: f64,
#[arg(long)]
pub language: Option<String>,
#[arg(long)]
pub risk: Option<String>,
}
#[derive(Debug, Args)]
pub struct CompareArgs {
#[arg(long)]
pub baseline: String,
#[arg(long)]
pub candidate: String,
}
#[derive(Debug, Args)]
pub struct DecideArgs {
#[arg(long)]
input: Option<String>,
}
pub async fn handle(command: DecideCommand, output: OutputFormat) -> miette::Result<()> {
match command {
DecideCommand::Eval(args) => super::decide_eval::evaluate(args, output).await,
DecideCommand::Calibrate(args) => super::decide_eval::calibrate(args, output).await,
DecideCommand::Compare(args) => super::decide_eval::compare(args, output).await,
other => handle_inference(other, output).await,
}
}
async fn handle_inference(command: DecideCommand, output: OutputFormat) -> miette::Result<()> {
let (kind, args) = match command {
DecideCommand::Noul(args) => ("noul", args),
DecideCommand::Choice(args) => ("choice", args),
DecideCommand::Score(args) => ("score", args),
DecideCommand::Batch(args) => ("batch", args),
_ => return Err(miette::miette!("invalid decision command")),
};
let bytes = tokio::task::spawn_blocking(move || read_bounded_input(args.input))
.await
.map_err(|_| miette::miette!("decision input read failed"))??;
let request: DecisionRequest = serde_json::from_slice(&bytes)
.map_err(|_| miette::miette!("decision input JSON is invalid"))?;
request.validate().map_err(|e| miette::miette!("{e}"))?;
if !request
.questions
.values()
.all(|question| matches_kind(question, kind))
{
return Err(miette::miette!("question type does not match subcommand"));
}
#[cfg(feature = "gitflow-jev")]
{
if std::env::var("GF_DECISION_PROVIDER").as_deref() != Ok("jev") {
return Err(miette::miette!("decision provider unavailable"));
}
let engine = gitflow_jev::JevEngine::from_env().map_err(|e| miette::miette!("{e}"))?;
let response = engine
.decide(&request)
.await
.map_err(|e| miette::miette!("{e}"))?;
super::output::print_output(&response, &decision_output_format(output))
}
#[cfg(not(feature = "gitflow-jev"))]
{
let _ = output;
Err(miette::miette!(
"decision provider unavailable: build with feature gitflow-jev"
))
}
}
pub(super) fn decision_output_format(format: OutputFormat) -> OutputFormat {
if matches!(format, OutputFormat::Auto) {
OutputFormat::Json
} else {
format
}
}
fn matches_kind(question: &Question, kind: &str) -> bool {
match kind {
"noul" => matches!(question, Question::Noul { .. }),
"choice" => matches!(question, Question::Choice { .. }),
"score" => matches!(question, Question::Score { .. }),
"batch" => true,
_ => false,
}
}
fn read_bounded_input(path: Option<String>) -> miette::Result<Vec<u8>> {
let input: Box<dyn Read> = if let Some(path) = path {
let safe = gitflow_core::SafePath::new_allow_absolute(&path)
.map_err(|_| miette::miette!("decision input path is invalid"))?;
let file = std::fs::File::open(safe.as_path())
.map_err(|_| miette::miette!("decision input file cannot be opened"))?;
Box::new(file)
} else {
Box::new(std::io::stdin())
};
let mut bytes = Vec::new();
input
.take(MAX_INPUT_BYTES + 1)
.read_to_end(&mut bytes)
.map_err(|_| miette::miette!("decision input cannot be read"))?;
if bytes.len() as u64 > MAX_INPUT_BYTES {
return Err(miette::miette!("decision input is too large"));
}
Ok(bytes)
}
#[cfg(test)]
mod tests {
use gitflow_core::decision::Question;
use super::matches_kind;
#[test]
fn test_should_reject_question_type_mismatch() {
let question = Question::Noul {
instructions: "Is it blocked?".into(),
};
assert!(!matches_kind(&question, "choice"));
assert!(matches_kind(&question, "noul"));
assert!(matches_kind(&question, "batch"));
}
#[test]
fn test_should_use_json_for_default_decision_output() {
assert!(matches!(
super::decision_output_format(crate::OutputFormat::Auto),
crate::OutputFormat::Json
));
}
}