@@ -29,32 +29,51 @@ type Context = Vec<ChatMessage>;
2929type Scope = HashMap < String , Value > ;
3030type Interpretation = Result < ( Context , PdlBlock ) , Box < dyn Error > > ;
3131
32- struct Interpreter {
32+ struct Interpreter < ' a > {
3333 // batch: u32,
3434 // role: Role,
3535 // cwd: Box<PathBuf>,
3636 // id_stack: Vec<String>,
37- // jinja_env: Environment<'a>,
37+ jinja_env : Environment < ' a > ,
3838 rt : Runtime ,
3939 scope : Vec < Scope > ,
4040 debug : bool ,
41+ emit : bool ,
4142}
4243
43- impl Interpreter {
44+ impl < ' a > Interpreter < ' a > {
4445 fn new ( ) -> Self {
46+ let mut jinja_env = Environment :: new ( ) ;
47+ // PDL uses custom variable delimeters, because {{ }} have pre-defined meaning in yaml
48+ jinja_env. set_syntax (
49+ SyntaxConfig :: builder ( )
50+ . variable_delimiters ( "${" , "}" )
51+ . build ( )
52+ . unwrap ( ) ,
53+ ) ;
54+
4555 Self {
4656 // batch: 0,
4757 // role: Role::User,
4858 // cwd: Box::new(current_dir().unwrap_or(PathBuf::from("/"))),
4959 // id_stack: vec![],
50- // jinja_env: Environment::new() ,
60+ jinja_env : jinja_env ,
5161 rt : Runtime :: new ( ) . unwrap ( ) ,
5262 scope : vec ! [ Scope :: new( ) ] ,
5363 debug : false ,
64+ emit : true ,
5465 }
5566 }
5667
57- fn run ( & mut self , program : & PdlBlock , context : Context ) -> Interpretation {
68+ fn run_with_emit (
69+ & mut self ,
70+ program : & PdlBlock ,
71+ context : Context ,
72+ emit : bool ,
73+ ) -> Interpretation {
74+ let prior_emit = self . emit ;
75+ self . emit = emit;
76+
5877 let ( messages, trace) = match program {
5978 PdlBlock :: String ( s) => self . run_string ( s, context) ,
6079 PdlBlock :: Call ( block) => self . run_call ( block, context) ,
@@ -64,22 +83,31 @@ impl Interpreter {
6483 _ => Err ( Box :: from ( format ! ( "Unsupported block {:?}" , program) ) ) ,
6584 } ?;
6685
67- pretty_print ( & messages) ;
86+ if match program {
87+ PdlBlock :: Call ( _) | PdlBlock :: Text ( _) => false ,
88+ _ => self . emit ,
89+ } {
90+ // eprintln!("{:?}", program);
91+ pretty_print ( & messages) ;
92+ }
93+ self . emit = prior_emit;
94+
6895 Ok ( ( messages, trace) )
6996 }
7097
98+ fn run_quiet ( & mut self , program : & PdlBlock , context : Context ) -> Interpretation {
99+ self . run_with_emit ( program, context, false )
100+ }
101+
102+ fn run ( & mut self , program : & PdlBlock , context : Context ) -> Interpretation {
103+ self . run_with_emit ( program, context, self . emit )
104+ }
105+
71106 // Evaluate as a Jinja2 expression
72107 fn eval ( & self , expr : & String ) -> Result < PdlBlock , Box < dyn Error > > {
73- let mut env = Environment :: new ( ) ;
74- // PDL uses custom variable delimeters, because {{ }} have pre-defined meaning in yaml
75- env. set_syntax (
76- SyntaxConfig :: builder ( )
77- . variable_delimiters ( "${" , "}" )
78- . build ( ) ?,
79- ) ;
80- env. add_template ( expr, expr. as_str ( ) ) ?;
81- let tmpl = env. get_template ( expr) ?;
82- let result = tmpl. render ( self . scope . last ( ) . unwrap_or ( & HashMap :: new ( ) ) ) ?;
108+ let result = self
109+ . jinja_env
110+ . render_str ( expr. as_str ( ) , self . scope . last ( ) . unwrap_or ( & HashMap :: new ( ) ) ) ?;
83111 if self . debug {
84112 eprintln ! ( "Eval '{}' -> {}" , & expr, & result) ;
85113 }
@@ -168,12 +196,14 @@ impl Interpreter {
168196 } ;
169197
170198 let ( options, tools) = self . to_ollama_model_options ( & block. parameters ) ;
171- println ! ( "MODEL OPTIONS {:?}" , options) ;
199+ if self . debug {
200+ println ! ( "Model options {:?}" , options) ;
201+ }
172202
173203 let messages = match & block. input {
174204 Some ( input) => {
175205 // TODO ignoring trace
176- let ( messages, _trace) = self . run ( & * input, context) ?;
206+ let ( messages, _trace) = self . run_quiet ( & * input, context) ?;
177207 messages
178208 }
179209 None => context,
0 commit comments