Skip to main content

rmcp_agent/agent/
intermediate.rs

1use langchain_rust::{
2    agent::AgentError,
3    schemas::{AgentAction, FunctionCallResponse, LogTools, Message},
4};
5use serde_json::json;
6
7pub trait IntermediateStep: Send + Sync {
8    fn append_to_conversation(&self, thoughts: &mut Vec<Message>) -> Result<(), AgentError>;
9}
10
11impl IntermediateStep for (AgentAction, String) {
12    fn append_to_conversation(&self, thoughts: &mut Vec<Message>) -> Result<(), AgentError> {
13        let (action, observation) = (&self.0, &self.1);
14
15        let LogTools { tool_id, tools } = match serde_json::from_str(&action.log) {
16            Ok(log_tools) => log_tools,
17            Err(e) => return Err(AgentError::SerdeJsonError(e)),
18        };
19
20        let tools: Vec<FunctionCallResponse> = match serde_json::from_str(&tools) {
21            Ok(tools) => tools,
22            Err(e) => return Err(AgentError::SerdeJsonError(e)),
23        };
24
25        if thoughts.is_empty() {
26            thoughts.push(Message::new_ai_message("").with_tool_calls(json!(tools)));
27        }
28        thoughts.push(Message::new_tool_message(observation, tool_id));
29
30        Ok(())
31    }
32}