use crate::output_fmt;
use crate::types::Output;
use agent_first_data::{OutputFormat, OutputTo};
use serde_json::Value;
use std::sync::OnceLock;
static OUTPUT_TO: OnceLock<OutputTo> = OnceLock::new();
pub fn set_output_to(value: OutputTo) {
let _ = OUTPUT_TO.set(value);
}
pub fn output_to() -> OutputTo {
OUTPUT_TO.get().copied().unwrap_or(OutputTo::Split)
}
pub fn emit_cli_error(
msg: &str,
hint: Option<&str>,
format: OutputFormat,
) -> Result<(), agent_first_data::CliEmitterError> {
emit_coded_error(
crate::protocol::error_code::INVALID_REQUEST,
msg,
hint,
format,
)
}
pub fn emit_coded_error(
code: &str,
msg: &str,
hint: Option<&str>,
format: OutputFormat,
) -> Result<(), agent_first_data::CliEmitterError> {
let mut emitter =
agent_first_data::CliEmitter::from_output_to(output_to(), format).with_strict_protocol();
let event = agent_first_data::json_error(code, msg)
.hint_if_some(hint)
.build()
.map_err(agent_first_data::CliEmitterError::Build)?;
emitter.emit(event)
}
pub fn emit_event(
event: agent_first_data::Event,
format: OutputFormat,
) -> Result<(), agent_first_data::CliEmitterError> {
let mut emitter =
agent_first_data::CliEmitter::from_output_to(output_to(), format).with_strict_protocol();
emitter.emit(event)
}
pub fn emit_value(
value: Value,
format: OutputFormat,
) -> Result<(), agent_first_data::CliEmitterError> {
let mut emitter =
agent_first_data::CliEmitter::from_output_to(output_to(), format).with_strict_protocol();
emitter.emit_validated_value(value)
}
pub fn emit_output(
out: &Output,
format: OutputFormat,
) -> Result<(), agent_first_data::CliEmitterError> {
output_fmt::emit_process_output(out, format, output_to())
}
pub fn write_result_text(text: &str) -> std::io::Result<()> {
agent_first_data::write_raw(text, output_to())
}