Skip to content

Commit a7bab4d

Browse files
committed
feat: add CodeGraph LLM response parsing and tool call handling in ChatResponse
1 parent 1c25e82 commit a7bab4d

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

crates/codegraph-mcp/src/autoagents/agent_builder.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use autoagents::llm::completion::{CompletionProvider, CompletionRequest, Complet
99
use autoagents::llm::embedding::EmbeddingProvider;
1010
use autoagents::llm::error::LLMError;
1111
use autoagents::llm::models::{ModelListRequest, ModelListResponse, ModelsProvider};
12-
use autoagents::llm::ToolCall;
12+
use autoagents::llm::{FunctionCall, ToolCall};
1313
use codegraph_ai::llm_provider::{LLMProvider as CodeGraphLLM, Message, MessageRole};
14+
use serde::Deserialize;
1415
use std::sync::Arc;
1516

1617
/// Convert CodeGraph Message to AutoAgents ChatMessage
@@ -162,13 +163,54 @@ impl std::fmt::Display for CodeGraphChatResponse {
162163
}
163164
}
164165

166+
/// Helper struct to parse CodeGraph LLM response format
167+
#[derive(Debug, Deserialize)]
168+
struct CodeGraphLLMResponse {
169+
#[serde(default)]
170+
reasoning: Option<String>,
171+
#[serde(default)]
172+
tool_call: Option<CodeGraphToolCall>,
173+
#[serde(default)]
174+
is_final: bool,
175+
}
176+
177+
#[derive(Debug, Deserialize)]
178+
struct CodeGraphToolCall {
179+
tool_name: String,
180+
parameters: serde_json::Value,
181+
}
182+
165183
impl ChatResponse for CodeGraphChatResponse {
166184
fn text(&self) -> Option<String> {
167185
Some(self.content.clone())
168186
}
169187

170188
fn tool_calls(&self) -> Option<Vec<ToolCall>> {
171-
None // CodeGraph doesn't use tool calls in responses
189+
// Try to parse the response as CodeGraph's JSON format
190+
if let Ok(parsed) = serde_json::from_str::<CodeGraphLLMResponse>(&self.content) {
191+
// If there's a tool_call and is_final is false, convert to AutoAgents format
192+
if let Some(tool_call) = parsed.tool_call {
193+
if !parsed.is_final {
194+
// Convert parameters to JSON string
195+
let arguments = serde_json::to_string(&tool_call.parameters)
196+
.unwrap_or_else(|_| "{}".to_string());
197+
198+
let autoagents_tool_call = ToolCall {
199+
id: format!("call_{}", uuid::Uuid::new_v4()),
200+
call_type: "function".to_string(),
201+
function: FunctionCall {
202+
name: tool_call.tool_name,
203+
arguments,
204+
},
205+
};
206+
207+
return Some(vec![autoagents_tool_call]);
208+
}
209+
}
210+
}
211+
212+
// No tool calls in response
213+
None
172214
}
173215
}
174216

0 commit comments

Comments
 (0)