@@ -2,6 +2,58 @@ import { loadConfig, getActiveProfile } from '../config/loader.js';
22import { createProvider } from '../providers/factory.js' ;
33import { JamError } from '../utils/errors.js' ;
44import type { CliOverrides } from '../config/schema.js' ;
5+ import { readFile , writeFile , mkdir } from 'node:fs/promises' ;
6+ import { join } from 'node:path' ;
7+ import { homedir } from 'node:os' ;
8+
9+ export async function runModelsSet ( model : string , options : CliOverrides ) : Promise < void > {
10+ try {
11+ // Validate: list available models and check if the requested one exists
12+ const config = await loadConfig ( process . cwd ( ) , options ) ;
13+ const profile = getActiveProfile ( config ) ;
14+ const adapter = await createProvider ( profile ) ;
15+
16+ let available : string [ ] ;
17+ try {
18+ available = await adapter . listModels ( ) ;
19+ } catch {
20+ available = [ ] ;
21+ }
22+
23+ if ( available . length > 0 && ! available . includes ( model ) ) {
24+ process . stderr . write ( `Warning: "${ model } " not found in available models for ${ profile . provider } .\n` ) ;
25+ process . stderr . write ( `Available: ${ available . join ( ', ' ) } \n\n` ) ;
26+ process . stderr . write ( `Setting it anyway — it may work if the provider accepts it.\n\n` ) ;
27+ }
28+
29+ // Read or create ~/.jam/config.json
30+ const configDir = join ( homedir ( ) , '.jam' ) ;
31+ const configPath = join ( configDir , 'config.json' ) ;
32+
33+ let existing : Record < string , unknown > = { } ;
34+ try {
35+ existing = JSON . parse ( await readFile ( configPath , 'utf-8' ) ) as Record < string , unknown > ;
36+ } catch { /* file doesn't exist yet */ }
37+
38+ // Merge the model into the active profile
39+ const profileName = ( existing [ 'defaultProfile' ] as string ) ?? 'default' ;
40+ const profiles = ( existing [ 'profiles' ] ?? { } ) as Record < string , Record < string , unknown > > ;
41+ const activeProfile = profiles [ profileName ] ?? { } ;
42+
43+ profiles [ profileName ] = { ...activeProfile , model } ;
44+ existing [ 'profiles' ] = profiles ;
45+
46+ await mkdir ( configDir , { recursive : true } ) ;
47+ await writeFile ( configPath , JSON . stringify ( existing , null , 2 ) + '\n' ) ;
48+
49+ process . stdout . write ( `Model set to "${ model } " for profile "${ profileName } ".\n` ) ;
50+ process . stdout . write ( `Config: ${ configPath } \n` ) ;
51+ } catch ( err ) {
52+ const jamErr = JamError . fromUnknown ( err ) ;
53+ process . stderr . write ( `Error: ${ jamErr . message } \n` ) ;
54+ process . exit ( 1 ) ;
55+ }
56+ }
557
658export async function runModelsList ( options : CliOverrides ) : Promise < void > {
759 try {
0 commit comments