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
use std::io::{self, Write};
use owo_colors::{OwoColorize, Stream::Stdout};
use rmcp::model::{self, CallToolResult};
use crate::{
cli::Cli,
client::{
ChatCompletionMessage, ChatCompletionRequest, Client, Content, MessageRole, ToolChoiceType,
},
error::AppResult,
tool::ManagerArc,
utility::fix_tool_call,
};
pub struct ReplSession {
client: Client,
manager: ManagerArc,
history: Vec<ChatCompletionMessage>,
model: String,
max_tokens: i64,
}
impl ReplSession {
pub fn new(client: Client, manager: ManagerArc, args: &Cli) -> Self {
let history = vec![ChatCompletionMessage {
role: MessageRole::system,
content: Content::Text(manager.system_prompt().into()),
name: None,
tool_calls: None,
tool_call_id: None,
}];
Self {
client,
manager,
history,
model: args.model.clone(),
max_tokens: args.max_tokens,
}
}
pub async fn run(&mut self) -> AppResult<()> {
println!("Database REPL started. Type 'exit' to quit.");
loop {
print!("User> ");
io::stdout().flush()?;
let mut user_input = String::new();
io::stdin().read_line(&mut user_input)?;
let user_input = user_input.trim();
if user_input.eq_ignore_ascii_case("exit") {
break;
}
if user_input.is_empty() {
continue;
}
self.history.push(ChatCompletionMessage {
role: MessageRole::user,
content: Content::Text(user_input.to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
});
'chat_response: loop {
match self
.client
.chat_completion(
ChatCompletionRequest::new(self.model.clone(), self.history.clone())
.tool_choice(ToolChoiceType::Auto)
.max_tokens(self.max_tokens),
)
.await
{
Ok(response) => {
if let Some(choice) = response.choices.into_iter().next() {
let assistant_message = choice.message;
self.history.push(ChatCompletionMessage {
role: MessageRole::assistant,
content: assistant_message
.content
.clone()
.map_or(Content::Text("".into()), Content::Text),
name: assistant_message.name.clone(),
tool_calls: assistant_message.tool_calls.as_ref().map(
|tool_calls| {
tool_calls.iter().cloned().map(fix_tool_call).collect()
},
),
tool_call_id: None,
});
if let Some(tool_calls) = assistant_message.tool_calls {
for (tool_call_id, function_name, params_json) in
tool_calls.into_iter().filter_map(|tool| {
let params: Option<serde_json::Value> = tool
.function
.arguments
.and_then(|args| serde_json::from_str(&args).ok());
Some((tool.id, tool.function.name?, params))
})
{
let tool_result = match function_name.as_str() {
"mysqlGetDatabaseSchema" => {
self.manager.get_database_schema().await
}
"mysqlExecuteQuery" => {
if let Some(params_json) = params_json {
match serde_json::from_value(params_json) {
Ok(params) => {
self.manager.execute_query(params).await
}
Err(err) => Err(err.into()),
}
} else {
Ok(CallToolResult::error(vec![
model::Content::text(format!(
"Missing required parameter for {}",
function_name,
)),
]))
}
}
_ => Ok(CallToolResult::error(vec![model::Content::text(
format!("Unknown tool: {}", function_name),
)])),
};
let result_content = match tool_result {
Ok(result) => {
// Assuming CallToolResult content is a Vec<Content>, and the first one is text.
if let Some(text) = result
.content
.first()
.and_then(|content| content.as_text())
{
text.text.clone()
} else if result.is_error.unwrap_or(false) {
format!(
"Tool error {}: {:?}",
function_name, result.content,
)
} else {
"Tool returned non-text or empty content".into()
}
}
Err(err) => {
format!(
"Error executing tool {}: {}",
function_name, err
)
}
};
self.history.push(ChatCompletionMessage {
role: MessageRole::tool,
tool_call_id: Some(tool_call_id),
name: Some(function_name),
content: Content::Text(result_content),
tool_calls: None,
});
}
continue 'chat_response;
} else if let Some(text) = assistant_message.content {
println!(
"{}",
format!("Assistant> {}", text)
.if_supports_color(Stdout, |text| text.blue())
);
}
}
break 'chat_response;
}
Err(err) => {
println!(
"{}",
format!("ERROR: {}", err).if_supports_color(Stdout, |text| text.red())
);
self.history.pop();
break 'chat_response;
}
}
}
}
Ok(())
}
}