pprog 0.0.8

An LLM pair programming server with web interface
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use std::collections::HashMap;
use reqwest::Client;
use serde::{Serialize, Deserialize};
use anyhow::Result;
use async_trait::async_trait;

use crate::chat::{CommonMessage, ContentItem, Role};
use crate::config::ProjectConfig;
use super::types::{InferenceError, ModelResponse};
use super::tools::{OpenAITool, OpenAIToolFunction, InputSchema, PropertySchema};
use super::inference::Inference;

#[derive(Serialize)]
struct OpenAIRequest {
    model: String,
    messages: Vec<OpenAIMessage>,
    max_completion_tokens: Option<u32>,
    tools: Option<serde_json::Value>,
}

#[derive(Serialize)]
struct LegacyOpenAIRequest {
    model: String,
    messages: Vec<OpenAIMessage>,
    max_tokens: Option<u32>,
    tools: Option<serde_json::Value>,
}

#[derive(Debug, Deserialize)]
struct OpenAIResponse {
    model: String,
    choices: Vec<OpenAIChoice>,
}

#[derive(Debug, Deserialize)]
struct OpenAIChoice {
    message: OpenAIMessage,
    finish_reason: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
struct OpenAIMessage {
    role: Role,
    #[serde(skip_serializing_if = "Option::is_none")]
    content: Option<OpenAIContent>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_calls: Option<Vec<OpenAIToolCall>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_call_id: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
enum OpenAIContent {
    String(String),
    Array(Vec<ContentItem>),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
struct OpenAIToolCall {
    id: String,
    #[serde(rename = "type")]
    call_type: String,
    function: OpenAIFunctionCall,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
struct OpenAIFunctionCall {
    name: String,
    arguments: String,
}

pub struct OpenAIToolProvider {
    tools: Vec<OpenAITool>,
}

impl OpenAIToolProvider {
    pub fn new() -> Self {
        Self {
            tools: vec![
                Self::read_file_tool(),
                Self::write_file_tool(),
                Self::execute_tool(),
                Self::compile_check_tool(),
            ],
        }
    }

    pub fn get_tools_json(&self) -> Result<serde_json::Value, serde_json::Error> {
        serde_json::to_value(&self.tools)
    }

    fn read_file_tool() -> OpenAITool {
        OpenAITool {
            tool_type: "function".to_string(),
            function: OpenAIToolFunction {
                name: "read_file".to_string(),
                description: "Read file as string using path relative to root directory of project.".to_string(),
                parameters: InputSchema {
                    schema_type: "object".to_string(),
                    properties: {
                        let mut map = HashMap::new();
                        map.insert(
                            "path".to_string(),
                            PropertySchema {
                                property_type: "string".to_string(),
                                description: "The file path relative to the project root directory".to_string(),
                            },
                        );
                        map
                    },
                    required: vec!["path".to_string()],
                },
            },
        }
    }

    fn write_file_tool() -> OpenAITool {
        OpenAITool {
            tool_type: "function".to_string(),
            function: OpenAIToolFunction {
                name: "write_file".to_string(),
                description: "Write string to file at path relative to root directory of project.".to_string(),
                parameters: InputSchema {
                    schema_type: "object".to_string(),
                    properties: {
                        let mut map = HashMap::new();
                        map.insert(
                            "path".to_string(),
                            PropertySchema {
                                property_type: "string".to_string(),
                                description: "The file path relative to the project root directory".to_string(),
                            },
                        );
                        map.insert(
                            "content".to_string(),
                            PropertySchema {
                                property_type: "string".to_string(),
                                description: "The content to write to the file".to_string(),
                            },
                        );
                        map
                    },
                    required: vec!["path".to_string(), "content".to_string()],
                },
            },
        }
    }

    fn execute_tool() -> OpenAITool {
        OpenAITool {
            tool_type: "function".to_string(),
            function: OpenAIToolFunction {
                name: "execute".to_string(),
                description: "Execute bash statements as a single string..".to_string(),
                parameters: InputSchema {
                    schema_type: "object".to_string(),
                    properties: {
                        let mut map = HashMap::new();
                        map.insert(
                            "statement".to_string(),
                            PropertySchema {
                                property_type: "string".to_string(),
                                description: "The bash statement to be executed.".to_string(),
                            },
                        );
                        map
                    },
                    required: vec!["statement".to_string()],
                },
            },
        }
    }

    fn compile_check_tool() -> OpenAITool {
        OpenAITool {
            tool_type: "function".to_string(),
            function: OpenAIToolFunction {
                name: "compile_check".to_string(),
                description: "Check if project compiles or runs without error.".to_string(),
                parameters: InputSchema {
                    schema_type: "object".to_string(),
                    properties: HashMap::new(),
                    required: vec![],
                },
            },
        }
    }
}

pub struct OpenAIInference {
    model: String,
    client: Client,
    api_url: String,
    api_key: String,
    max_output_tokens: u32,
    tool_provider: OpenAIToolProvider,
}

impl std::default::Default for OpenAIInference {
    fn default() -> Self {
        let config = ProjectConfig::load().unwrap_or_default();
        
        OpenAIInference {
            model: config.model,
            client: Client::new(),
            api_url: config.api_url,
            api_key: config.api_key,
            max_output_tokens: config.max_output_tokens,
            tool_provider: OpenAIToolProvider::new(),
        }
    }
}

#[async_trait]
impl Inference for OpenAIInference {
    fn new() -> Self {
        Self::default()
    }

    async fn query_model(&self, messages: Vec<CommonMessage>, system_message: Option<&str>) -> Result<ModelResponse, InferenceError> {
        let mut openai_messages: Vec<OpenAIMessage> = messages.into_iter().map(|msg| {
            let mut openai_message = OpenAIMessage {
                role: msg.role,
                content: Some(OpenAIContent::String("".to_string())),
                tool_calls: None,
                tool_call_id: None,
            };
            for content_item in msg.content {
                match content_item {
                    ContentItem::Text { text } => {
                        openai_message.content = Some(OpenAIContent::String(text));
                    },
                    ContentItem::ToolUse { id, name, input } => {
                        if self.model.as_str() != "deepseek-reasoner" {
                            openai_message.tool_calls = Some(vec![OpenAIToolCall {
                                id,
                                call_type: "function".to_string(),
                                function: OpenAIFunctionCall {
                                    name,
                                    arguments: input.to_string(),
                                }
                            }]);
                        }
                    },
                    ContentItem::ToolResult { tool_use_id, content } => {
                        openai_message.role = Role::Tool;
                        openai_message.tool_call_id = Some(tool_use_id);
                        openai_message.content = Some(OpenAIContent::String(content));
                    }
                }
            }
            openai_message
        }).collect();

        if let Some(sys_msg) = system_message {
            match self.model.as_str() {
                "o1" | "o1-mini" => {
                    openai_messages.insert(0, OpenAIMessage {
                        role: Role::Developer,
                        content: Some(OpenAIContent::String(sys_msg.to_string())),
                        tool_calls: None,
                        tool_call_id: None,
                    });
                },
                "deepseek-reasoner" => {
                    let mut deepseek_sys_msg = String::new();
                    deepseek_sys_msg.push_str(sys_msg);
                    let tools_string = self.tool_provider.get_tools_json().unwrap();
                    let tools_system_msg = format!(r#"
Tool definitions:
{}

When tool is needed return as JSON in format 
{{ 
    "name": "function_name", 
    "inputs": {{ 
        "first_input_name": "first_input_value", 
        "second_input_name", "second_input_value", 
        ... 
    }} 
}} 
surrounded by triple backticks.  

For example if you were going to use a tool called 'read_file' the response would look like 
```tool_use
{{ "name": "read_file", "inputs": {{ "path": "index.js" }} }}
```

Only use one tool at a time. Do not assume anything about contents of files, use read_file instead.

The user may also questions about the code base.  If a user asks a question DO NOT write to the files but instead read files to answer question."#, tools_string.to_string());
                    deepseek_sys_msg.push_str(&tools_system_msg);
                    openai_messages.insert(0, OpenAIMessage {
                        role: Role::System,
                        content: Some(OpenAIContent::String(deepseek_sys_msg)),
                        tool_calls: None,
                        tool_call_id: None,
                    });
                },
                _ => {
                    openai_messages.insert(0, OpenAIMessage {
                        role: Role::System,
                        content: Some(OpenAIContent::String(sys_msg.to_string())),
                        tool_calls: None,
                        tool_call_id: None,
                    });
                },
            };
        }

        let tools = self.tool_provider.get_tools_json()
            .map_err(|e| InferenceError::SerializationError(e.to_string())).ok();

        let request: serde_json::Value = match self.model.as_str() {
            "o1" | "o1-mini" => serde_json::to_value(OpenAIRequest {
                model: self.model.clone(),
                messages: openai_messages,
                max_completion_tokens: Some(self.max_output_tokens),
                tools,
            }).unwrap(),
            "deepseek-reasoner" => serde_json::to_value(LegacyOpenAIRequest {
                model: self.model.clone(),
                messages: openai_messages,
                max_tokens: Some(self.max_output_tokens),
                tools: None,
            }).unwrap(),
            _ => serde_json::to_value(LegacyOpenAIRequest {
                model: self.model.clone(),
                messages: openai_messages,
                max_tokens: Some(self.max_output_tokens),
                tools,
            }).unwrap()
        };

        let response = self.client
            .post(&self.api_url)
            .header("Content-Type", "application/json")
            .header("Authorization", format!("Bearer {}", self.api_key))
            .json(&request)
            .send()
            .await
            .map_err(|e| InferenceError::NetworkError(e.to_string()))?;

        let status = response.status();
        let response_text = response.text().await
            .map_err(|e| InferenceError::NetworkError(e.to_string()))?;

        if !status.is_success() {
            return Err(InferenceError::ApiError(status, response_text));
        }

        let openai_response: OpenAIResponse = serde_json::from_str(&response_text)
            .map_err(|e| InferenceError::InvalidResponse(format!("Failed to parse OpenAI response: {}", e)))?;

        if openai_response.choices.is_empty() {
            return Err(InferenceError::InvalidResponse("No choices in OpenAI response".to_string()));
        }
        
        let mut content: Vec<ContentItem> = Vec::new();
        if let Some(openai_content) = openai_response.choices[0].message.content.clone() {
            match openai_content {
                OpenAIContent::String(text) => content.push(ContentItem::Text { text }),
                OpenAIContent::Array(..) => {},
            }
        }
        if let Some(tool_calls) = &openai_response.choices[0].message.tool_calls {
            for tool_call in tool_calls {
                let input = serde_json::from_str(&tool_call.function.arguments)?;
                content.push(
                    ContentItem::ToolUse {
                        id: tool_call.id.clone(),
                        name: tool_call.function.name.clone(),
                        input,
                    }
                )

            }
        }

        let model_response = ModelResponse {
            content,
            model: openai_response.model,
            role: openai_response.choices[0].message.role.to_string(),
            message_type: "text".to_string(),
            stop_reason: openai_response.choices[0].finish_reason.clone(),
            stop_sequence: None,
            total_tokens: 0, // OpenAI doesn't provide token count in response
        };
        Ok(model_response)
    }

    async fn get_token_count(&self, messages: Vec<CommonMessage>, system_message: Option<&str>) -> Result<u64, InferenceError> {
        let mut total_tokens = 0;
        if let Some(system_message) = system_message {
            total_tokens += (system_message.len() as u64 + 1) / 2;
        }
        for message in messages {
            for content in message.content {
                match content {
                    ContentItem::Text { text } => {
                        total_tokens += (text.len() as u64 + 1) / 2;
                    },
                    ContentItem::ToolUse { input, .. } => {
                        total_tokens += (input.to_string().len() as u64 + 1) / 2;
                    },
                    ContentItem::ToolResult { content, .. } => {
                        total_tokens += (content.len() as u64 + 1) / 2;
                    }
                }
            }
        }
        let tool_token_count: u64 = self.tool_provider.tools.iter().map(|tool| {
            let tool_name_tokens = (tool.function.name.len() as u64 + 1) / 2;
            let tool_description_tokens = (tool.function.description.len() as u64 + 1) / 2;
            let mut param_tokens = 0;
            for (_, prop) in tool.function.parameters.properties.iter() {
                param_tokens += (prop.property_type.len() as u64 + 1) / 2;
                param_tokens += (prop.description.len() as u64 + 1) / 2;
            }
            tool_name_tokens + tool_description_tokens + param_tokens
        }).sum();
        total_tokens += tool_token_count;
        Ok(total_tokens)
    }

}