@@ -4,10 +4,7 @@ use crate::{client::Model, function::Functions};
4
4
5
5
use anyhow:: { Context , Result } ;
6
6
use inquire:: { validator:: Validation , Text } ;
7
- use std:: {
8
- fs:: { self , read_to_string} ,
9
- path:: Path ,
10
- } ;
7
+ use std:: { fs:: read_to_string, path:: Path } ;
11
8
12
9
use serde:: { Deserialize , Serialize } ;
13
10
@@ -36,7 +33,6 @@ impl Agent {
36
33
bail ! ( "Unknown agent `{name}`" ) ;
37
34
}
38
35
let functions_file_path = functions_dir. join ( "functions.json" ) ;
39
- let variables_path = Config :: agent_variables_file ( name) ?;
40
36
let rag_path = Config :: agent_rag_file ( name, "rag" ) ?;
41
37
let config_path = Config :: agent_config_file ( name) ?;
42
38
let agent_config = if config_path. exists ( ) {
@@ -45,7 +41,7 @@ impl Agent {
45
41
AgentConfig :: new ( & config. read ( ) )
46
42
} ;
47
43
let mut definition = AgentDefinition :: load ( & definition_file_path) ?;
48
- init_variables ( & variables_path , & mut definition. variables )
44
+ init_variables ( & mut definition. variables , & agent_config . variables )
49
45
. context ( "Failed to init variables" ) ?;
50
46
51
47
let functions = if functions_file_path. exists ( ) {
@@ -94,18 +90,6 @@ impl Agent {
94
90
} )
95
91
}
96
92
97
- pub fn save_config ( & self ) -> Result < ( ) > {
98
- let config_path = Config :: agent_config_file ( & self . name ) ?;
99
- ensure_parent_exists ( & config_path) ?;
100
- let content = serde_yaml:: to_string ( & self . config ) ?;
101
- fs:: write ( & config_path, content) . with_context ( || {
102
- format ! ( "Failed to save agent config to '{}'" , config_path. display( ) )
103
- } ) ?;
104
-
105
- println ! ( "✨ Saved agent config to '{}'." , config_path. display( ) ) ;
106
- Ok ( ( ) )
107
- }
108
-
109
93
pub fn export ( & self ) -> Result < String > {
110
94
let mut agent = self . clone ( ) ;
111
95
agent. definition . instructions = self . interpolated_instructions ( ) ;
@@ -118,7 +102,7 @@ impl Agent {
118
102
. display ( )
119
103
. to_string ( )
120
104
. into ( ) ;
121
- value[ "variables_file " ] = Config :: agent_variables_file ( & self . name ) ?
105
+ value[ "config_file " ] = Config :: agent_config_file ( & self . name ) ?
122
106
. display ( )
123
107
. to_string ( )
124
108
. into ( ) ;
@@ -166,8 +150,6 @@ impl Agent {
166
150
match self . definition . variables . iter_mut ( ) . find ( |v| v. name == key) {
167
151
Some ( variable) => {
168
152
variable. value = value. to_string ( ) ;
169
- let variables_path = Config :: agent_variables_file ( & self . name ) ?;
170
- save_variables ( & variables_path, self . variables ( ) ) ?;
171
153
Ok ( ( ) )
172
154
}
173
155
None => bail ! ( "Unknown variable '{key}'" ) ,
@@ -229,6 +211,8 @@ pub struct AgentConfig {
229
211
pub top_p : Option < f64 > ,
230
212
pub use_tools : Option < String > ,
231
213
pub agent_prelude : Option < String > ,
214
+ #[ serde( default ) ]
215
+ pub variables : IndexMap < String , String > ,
232
216
}
233
217
234
218
impl AgentConfig {
@@ -364,65 +348,37 @@ fn list_agents_impl() -> Result<Vec<String>> {
364
348
Ok ( agents)
365
349
}
366
350
367
- fn init_variables ( variables_path : & Path , variables : & mut [ AgentVariable ] ) -> Result < ( ) > {
351
+ fn init_variables (
352
+ variables : & mut [ AgentVariable ] ,
353
+ config_variable : & IndexMap < String , String > ,
354
+ ) -> Result < ( ) > {
368
355
if variables. is_empty ( ) {
369
356
return Ok ( ( ) ) ;
370
357
}
371
- let variable_values = if variables_path. exists ( ) {
372
- let content = read_to_string ( variables_path) . with_context ( || {
373
- format ! (
374
- "Failed to read variables from '{}'" ,
375
- variables_path. display( )
376
- )
377
- } ) ?;
378
- let variable_values: IndexMap < String , String > = serde_yaml:: from_str ( & content) ?;
379
- variable_values
380
- } else {
381
- Default :: default ( )
382
- } ;
383
- let mut initialized = false ;
384
358
for variable in variables. iter_mut ( ) {
385
- match variable_values . get ( & variable. name ) {
359
+ match config_variable . get ( & variable. name ) {
386
360
Some ( value) => variable. value = value. to_string ( ) ,
387
361
None => {
388
- if !initialized {
389
- println ! ( "The agent has the variables and is initializing them..." ) ;
390
- initialized = true ;
362
+ if let Some ( value ) = variable . default . clone ( ) {
363
+ variable . value = value ;
364
+ continue ;
391
365
}
392
366
if * IS_STDOUT_TERMINAL {
393
- let mut text =
394
- Text :: new ( & variable . description ) . with_validator ( |input : & str | {
367
+ let value = Text :: new ( & variable . description )
368
+ . with_validator ( |input : & str | {
395
369
if input. trim ( ) . is_empty ( ) {
396
370
Ok ( Validation :: Invalid ( "This field is required" . into ( ) ) )
397
371
} else {
398
372
Ok ( Validation :: Valid )
399
373
}
400
- } ) ;
401
- if let Some ( default) = & variable. default {
402
- text = text. with_default ( default) ;
403
- }
404
- let value = text. prompt ( ) ?;
374
+ } )
375
+ . prompt ( ) ?;
405
376
variable. value = value;
406
377
} else {
407
378
bail ! ( "Failed to init agent variables in the script mode." ) ;
408
379
}
409
380
}
410
381
}
411
382
}
412
- if initialized {
413
- save_variables ( variables_path, variables) ?;
414
- }
415
- Ok ( ( ) )
416
- }
417
-
418
- fn save_variables ( variables_path : & Path , variables : & [ AgentVariable ] ) -> Result < ( ) > {
419
- ensure_parent_exists ( variables_path) ?;
420
- let variable_values: IndexMap < String , String > = variables
421
- . iter ( )
422
- . map ( |v| ( v. name . clone ( ) , v. value . clone ( ) ) )
423
- . collect ( ) ;
424
- let content = serde_yaml:: to_string ( & variable_values) ?;
425
- fs:: write ( variables_path, content)
426
- . with_context ( || format ! ( "Failed to save variables to '{}'" , variables_path. display( ) ) ) ?;
427
383
Ok ( ( ) )
428
384
}
0 commit comments