@@ -312,6 +312,69 @@ impl AgentCli {
312312 }
313313}
314314
315+ /// Validate a model name for agent creation.
316+ ///
317+ /// Returns the validated model name, resolving aliases if needed.
318+ /// Checks that the model is in valid format (provider/model or known alias).
319+ fn validate_model_name ( model : & str ) -> Result < String > {
320+ use cortex_common:: resolve_model_alias;
321+
322+ // First, resolve any alias (e.g., "sonnet" -> "anthropic/claude-sonnet-4-20250514")
323+ let resolved = resolve_model_alias ( model) ;
324+
325+ // Check if model is in provider/model format or just model name
326+ // Valid formats:
327+ // - "provider/model" (e.g., "anthropic/claude-sonnet-4-20250514")
328+ // - Known model name (e.g., "gpt-4o", "claude-sonnet-4-20250514")
329+ // - Known alias (e.g., "sonnet", "opus")
330+
331+ // If the model contains a '/', it should be in provider/model format
332+ if resolved. contains ( '/' ) {
333+ let parts: Vec < & str > = resolved. splitn ( 2 , '/' ) . collect ( ) ;
334+ if parts. len ( ) != 2 || parts[ 0 ] . is_empty ( ) || parts[ 1 ] . is_empty ( ) {
335+ bail ! (
336+ "Invalid model format: '{}'. Expected 'provider/model' format.\n \
337+ Examples: anthropic/claude-sonnet-4-20250514, openai/gpt-4o\n \
338+ Run 'cortex models list' to see available models.",
339+ model
340+ ) ;
341+ }
342+ // Validate provider is a known provider
343+ let valid_providers = [
344+ "anthropic" ,
345+ "openai" ,
346+ "google" ,
347+ "mistral" ,
348+ "xai" ,
349+ "deepseek" ,
350+ "ollama" ,
351+ ] ;
352+ let provider = parts[ 0 ] . to_lowercase ( ) ;
353+ if !valid_providers. contains ( & provider. as_str ( ) ) {
354+ eprintln ! (
355+ "Warning: Unknown provider '{}'. Known providers: {}" ,
356+ provider,
357+ valid_providers. join( ", " )
358+ ) ;
359+ }
360+ } else {
361+ // Model name without provider - check if it looks valid
362+ // Model names typically contain alphanumeric chars, hyphens, dots, and colons
363+ let valid_chars = resolved
364+ . chars ( )
365+ . all ( |c| c. is_ascii_alphanumeric ( ) || c == '-' || c == '_' || c == '.' || c == ':' ) ;
366+ if !valid_chars || resolved. is_empty ( ) {
367+ bail ! (
368+ "Invalid model name: '{}'. Model names should contain only alphanumeric characters, hyphens, underscores, dots, and colons.\n \
369+ Run 'cortex models list' to see available models.",
370+ model
371+ ) ;
372+ }
373+ }
374+
375+ Ok ( resolved. to_string ( ) )
376+ }
377+
315378/// Get the agents directory path.
316379fn get_agents_dir ( ) -> Result < PathBuf > {
317380 let cortex_home = dirs:: home_dir ( )
@@ -1539,19 +1602,23 @@ async fn run_generate(args: CreateArgs) -> Result<()> {
15391602 } ;
15401603
15411604 // Validate model argument
1542- if args. model . trim ( ) . is_empty ( ) {
1605+ let model_arg = args. model . trim ( ) ;
1606+ if model_arg. is_empty ( ) {
15431607 bail ! ( "Error: Model name cannot be empty" ) ;
15441608 }
15451609
1610+ // Validate the model name format and existence
1611+ let valid_model = validate_model_name ( model_arg) ?;
1612+
15461613 if !fully_automated {
15471614 println ! ( ) ;
15481615 println ! ( "Generating agent configuration..." ) ;
1549- println ! ( " Using model: {}" , args . model ) ;
1616+ println ! ( " Using model: {}" , valid_model ) ;
15501617 println ! ( ) ;
15511618 }
15521619
15531620 // Create generator and generate
1554- let generator = AgentGenerator :: new ( ) . with_model ( & args . model ) ;
1621+ let generator = AgentGenerator :: new ( ) . with_model ( & valid_model ) ;
15551622
15561623 let generated = generator
15571624 . generate ( & description)
0 commit comments