use unilang::data::{ ErrorCode, ErrorData, OutputData };
use unilang::interpreter::ExecutionContext;
use unilang::semantic::VerifiedCommand;
use unilang::types::Value;
use crate::output::{ OutputFormat, OutputOptions };
use super::shared::require_claude_paths;
use crate::usage::map_model_shorthand;
#[ inline ]
pub fn model_routine( cmd : VerifiedCommand, _ctx : ExecutionContext ) -> Result< OutputData, ErrorData >
{
let opts = OutputOptions::from_cmd( &cmd )?;
if opts.is_table()
{
return Err( ErrorData::new(
ErrorCode::ArgumentTypeMismatch,
"format::table is not supported by .model".to_string(),
) );
}
let paths = require_claude_paths()?;
let set_val = match cmd.arguments.get( "set" )
{
Some( Value::String( s ) ) => Some( s.clone() ),
_ => None,
};
if let Some( ref val ) = set_val
{
let model_id = map_model_shorthand( val )
.ok_or_else( || ErrorData::new(
ErrorCode::ArgumentTypeMismatch,
format!( "set:: must be one of: opus, sonnet, haiku, default; got {val:?}" ),
) )?;
claude_profile_core::account::set_session_model( &paths, model_id );
Ok( OutputData::new( format!( "model set: {val}\n" ), "text" ) )
}
else
{
let model = claude_profile_core::account::get_session_model( &paths );
let text = match opts.format
{
OutputFormat::Json =>
{
match &model
{
Some( m ) => format!( "{{\"model\":\"{m}\"}}\n" ),
None => "{\"model\":null}\n".to_string(),
}
}
OutputFormat::Text | OutputFormat::Table =>
{
match &model
{
Some( m ) => format!( "model: {m}\n" ),
None => "model: (unset)\n".to_string(),
}
}
};
Ok( OutputData::new( text, "text" ) )
}
}