@@ -7,7 +7,7 @@ use std::path::PathBuf;
7
7
use crate :: configuration:: Code2PromptConfig ;
8
8
use crate :: git:: { get_git_diff, get_git_diff_between_branches, get_git_log} ;
9
9
use crate :: path:: { label, traverse_directory} ;
10
- use crate :: template:: { handlebars_setup, render_template} ;
10
+ use crate :: template:: { handlebars_setup, render_template, OutputFormat } ;
11
11
use crate :: tokenizer:: { count_tokens, TokenizerType } ;
12
12
13
13
/// Represents a live session that holds stateful data about the user's codebase,
@@ -101,24 +101,50 @@ impl Code2PromptSession {
101
101
102
102
/// Constructs a JSON object that merges the session data and your config’s path label.
103
103
pub fn build_template_data ( & self ) -> serde_json:: Value {
104
- serde_json:: json!( {
104
+ let mut data = serde_json:: json!( {
105
105
"absolute_code_path" : label( & self . config. path) ,
106
106
"source_tree" : self . data. source_tree,
107
107
"files" : self . data. files,
108
108
"git_diff" : self . data. git_diff,
109
109
"git_diff_branch" : self . data. git_diff_branch,
110
110
"git_log_branch" : self . data. git_log_branch
111
- } )
111
+ } ) ;
112
+
113
+ // Add user-defined variables to the template data
114
+ if self . config . user_variables . len ( ) > 0 {
115
+ if let Some ( obj) = data. as_object_mut ( ) {
116
+ for ( key, value) in & self . config . user_variables {
117
+ obj. insert ( key. clone ( ) , serde_json:: Value :: String ( value. clone ( ) ) ) ;
118
+ }
119
+ }
120
+ }
121
+
122
+ data
112
123
}
113
124
114
125
/// Renders the final prompt given a template-data JSON object. Returns both
115
126
/// the rendered prompt and the token count information. The session
116
127
/// does not do any printing or user prompting — that’s up to the caller.
117
128
pub fn render_prompt ( & self , template_data : & serde_json:: Value ) -> Result < RenderedPrompt > {
129
+ // ~~~ Template selection ~~~
130
+ let mut template_str = self . config . template_str . clone ( ) ;
131
+ let mut template_name = self . config . template_name . clone ( ) ;
132
+ if self . config . template_str . is_empty ( ) {
133
+ template_str = match self . config . output_format {
134
+ OutputFormat :: Markdown | OutputFormat :: Json => {
135
+ include_str ! ( "./default_template_md.hbs" ) . to_string ( )
136
+ }
137
+ OutputFormat :: Xml => include_str ! ( "./default_template_xml.hbs" ) . to_string ( ) ,
138
+ } ;
139
+ template_name = match self . config . output_format {
140
+ OutputFormat :: Markdown | OutputFormat :: Json => "markdown" . to_string ( ) ,
141
+ OutputFormat :: Xml => "xml" . to_string ( ) ,
142
+ } ;
143
+ }
144
+
118
145
// ~~~ Rendering ~~~
119
- let handlebars = handlebars_setup ( & self . config . template_str , & self . config . template_name ) ?;
120
- let rendered_prompt =
121
- render_template ( & handlebars, & self . config . template_name , template_data) ?;
146
+ let handlebars = handlebars_setup ( & template_str, & template_name) ?;
147
+ let rendered_prompt = render_template ( & handlebars, & template_name, template_data) ?;
122
148
123
149
// ~~~ Informations ~~~
124
150
let tokenizer_type: TokenizerType = self . config . encoding ;
@@ -153,9 +179,30 @@ impl Code2PromptSession {
153
179
154
180
pub fn generate_prompt ( & mut self ) -> Result < RenderedPrompt > {
155
181
self . load_codebase ( ) ?;
156
- self . load_git_diff ( ) ?;
157
- self . load_git_diff_between_branches ( ) ?;
158
- self . load_git_log_between_branches ( ) ?;
182
+
183
+ // ~~~~ Load Git info ~~~
184
+ if self . config . diff_enabled {
185
+ match self . load_git_diff ( ) {
186
+ Ok ( _) => { }
187
+ Err ( e) => log:: warn!( "Git diff could not be loaded: {}" , e) ,
188
+ }
189
+ }
190
+
191
+ // ~~~ Load Git info between branches ~~~
192
+ if self . config . diff_branches . is_some ( ) {
193
+ match self . load_git_diff_between_branches ( ) {
194
+ Ok ( _) => { }
195
+ Err ( e) => log:: warn!( "Git branch diff could not be loaded: {}" , e) ,
196
+ }
197
+ }
198
+
199
+ // ~~~ Load Git log between branches ~~~
200
+ if self . config . log_branches . is_some ( ) {
201
+ match self . load_git_log_between_branches ( ) {
202
+ Ok ( _) => { }
203
+ Err ( e) => log:: warn!( "Git branch log could not be loaded: {}" , e) ,
204
+ }
205
+ }
159
206
let template_data = self . build_template_data ( ) ;
160
207
let rendered = self . render_prompt ( & template_data) ?;
161
208
Ok ( rendered)
0 commit comments