Skip to main content

agentlib_core/
reasoning.rs

1use crate::middleware::MiddlewarePipeline;
2use crate::provider::ModelProvider;
3use crate::tool::ToolRegistry;
4use crate::types::AgentPolicy;
5use crate::types::{ExecutionContext, PlanTask};
6use anyhow::Result;
7use async_trait::async_trait;
8use serde::{Deserialize, Serialize};
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
11#[serde(tag = "type", rename_all = "snake_case")]
12pub enum ReasoningStep {
13    Thought {
14        content: String,
15        engine: String,
16    },
17    Plan {
18        tasks: Vec<PlanTask>,
19        engine: String,
20    },
21    ToolCall {
22        tool_name: String,
23        args: serde_json::Value,
24        call_id: String,
25        engine: String,
26    },
27    ToolResult {
28        tool_name: String,
29        call_id: String,
30        result: serde_json::Value,
31        error: Option<String>,
32        engine: String,
33    },
34    Reflection {
35        assessment: String,
36        needs_revision: bool,
37        engine: String,
38    },
39    Response {
40        content: String,
41        engine: String,
42    },
43}
44
45pub struct ReasoningContext<'a> {
46    pub ctx: &'a mut ExecutionContext,
47    pub model: &'a dyn ModelProvider,
48    pub tools: &'a ToolRegistry,
49    pub middleware: &'a MiddlewarePipeline,
50    pub policy: AgentPolicy,
51}
52
53impl<'a> ReasoningContext<'a> {
54    pub fn push_step(&mut self, step: ReasoningStep) {
55        self.ctx.steps.push(step);
56        // TODO: Emit event
57    }
58
59    pub async fn call_tool(
60        &mut self,
61        name: &str,
62        args: serde_json::Value,
63        _call_id: String,
64    ) -> Result<serde_json::Value> {
65        // TODO: Apply middleware and call tool
66        self.tools.call_tool(name, args).await
67    }
68}
69
70#[async_trait]
71pub trait ReasoningEngine: Send + Sync {
72    fn name(&self) -> &str;
73    async fn execute(&self, r_ctx: &mut ReasoningContext<'_>) -> Result<String>;
74}