Skip to main content

steeldb/agent/
paddock.rs

1//! Paddock provider — a local OpenAI-compatible server (e.g. Qwen3.5-2B). Speaks
2//! `POST {base_url}/chat/completions` with function/tool calling. Translates the neutral
3//! message/tool types to OpenAI chat messages and back.
4
5use crate::agent::provider::LlmProvider;
6use crate::agent::types::{Block, Msg, Role, Stop, ToolSpec, Turn};
7use async_trait::async_trait;
8use serde_json::{json, Value};
9
10pub struct PaddockProvider {
11    client: reqwest::Client,
12    base_url: String,
13    model: String,
14    api_key: Option<String>,
15    /// Cached after the server rejects a native `tools` field (e.g. ollama's gemma3n) — subsequent
16    /// requests inline the tool schema in the system prompt so the harness can recover `<tool_call>`
17    /// blocks from the reply.
18    prompt_only_tools: std::sync::atomic::AtomicBool,
19}
20
21impl PaddockProvider {
22    pub fn new(base_url: String, model: String, api_key: Option<String>) -> PaddockProvider {
23        PaddockProvider {
24            client: reqwest::Client::new(),
25            base_url,
26            model,
27            api_key,
28            prompt_only_tools: std::sync::atomic::AtomicBool::new(false),
29        }
30    }
31}
32
33/// Inline tool schema for models the server won't accept `tools` for. Emit-format matches Hermes,
34/// which the harness already recovers.
35fn inline_tools_prompt(tools: &[ToolSpec]) -> String {
36    let mut out = String::from(
37        "\n\nYou have access to these tools. To call one, emit ONLY a single line in this exact form (no prose around it):\n<tool_call>{\"name\":\"<tool_name>\",\"arguments\":{...}}</tool_call>\nAfter the tool result comes back, either call another tool or give the final answer as plain text.\n\nAvailable tools:\n",
38    );
39    for t in tools {
40        out.push_str(&format!("- {} — {}\n  schema: {}\n", t.name, t.description, t.schema));
41    }
42    out
43}
44
45/// Flatten neutral messages into OpenAI chat messages (system first).
46fn to_openai_messages(system: &str, msgs: &[Msg]) -> Vec<Value> {
47    let mut out = vec![json!({ "role": "system", "content": system })];
48    for m in msgs {
49        match m.role {
50            Role::User => {
51                // user text and/or tool results
52                let mut text = String::new();
53                for b in &m.blocks {
54                    match b {
55                        Block::Text(t) => {
56                            if !text.is_empty() {
57                                text.push('\n');
58                            }
59                            text.push_str(t);
60                        }
61                        Block::ToolResult { id, content, .. } => {
62                            out.push(json!({ "role": "tool", "tool_call_id": id, "content": content }));
63                        }
64                        _ => {}
65                    }
66                }
67                if !text.is_empty() {
68                    out.push(json!({ "role": "user", "content": text }));
69                }
70            }
71            Role::Assistant => {
72                let mut text = String::new();
73                let mut tool_calls = Vec::new();
74                for b in &m.blocks {
75                    match b {
76                        Block::Text(t) => text.push_str(t),
77                        Block::ToolUse { id, name, input } => {
78                            tool_calls.push(json!({
79                                "id": id,
80                                "type": "function",
81                                "function": { "name": name, "arguments": input.to_string() }
82                            }));
83                        }
84                        _ => {}
85                    }
86                }
87                let mut msg = json!({ "role": "assistant", "content": if text.is_empty() { Value::Null } else { Value::String(text) } });
88                if !tool_calls.is_empty() {
89                    msg["tool_calls"] = Value::Array(tool_calls);
90                }
91                out.push(msg);
92            }
93        }
94    }
95    out
96}
97
98fn to_openai_tools(tools: &[ToolSpec]) -> Vec<Value> {
99    tools
100        .iter()
101        .map(|t| json!({ "type": "function", "function": { "name": t.name, "description": t.description, "parameters": t.schema } }))
102        .collect()
103}
104
105#[async_trait]
106impl LlmProvider for PaddockProvider {
107    fn name(&self) -> &str {
108        "paddock"
109    }
110
111    async fn chat(&self, system: &str, msgs: &[Msg], tools: &[ToolSpec]) -> Result<Turn, String> {
112        use std::sync::atomic::Ordering;
113        let url = format!("{}/chat/completions", self.base_url.trim_end_matches('/'));
114        let mut prompt_only = self.prompt_only_tools.load(Ordering::Relaxed);
115        let resp = loop {
116            let system_full = if prompt_only && !tools.is_empty() { format!("{system}{}", inline_tools_prompt(tools)) } else { system.to_string() };
117            let mut body = json!({
118                "model": self.model,
119                "messages": to_openai_messages(&system_full, msgs),
120                "max_tokens": 2048
121            });
122            if !prompt_only && !tools.is_empty() {
123                body["tools"] = json!(to_openai_tools(tools));
124                body["tool_choice"] = json!("auto");
125            }
126            let mut req = self.client.post(&url).json(&body);
127            if let Some(k) = &self.api_key {
128                req = req.bearer_auth(k);
129            }
130            let resp = req.send().await.map_err(|e| format!("paddock request: {e}"))?;
131            if resp.status().is_success() {
132                break resp;
133            }
134            let status = resp.status();
135            let text = resp.text().await.unwrap_or_default();
136            // fall back to prompt-only when the server rejects a native `tools` field
137            if !prompt_only && !tools.is_empty() && (text.contains("does not support tools") || text.contains("tool_choice")) {
138                self.prompt_only_tools.store(true, Ordering::Relaxed);
139                prompt_only = true;
140                continue;
141            }
142            return Err(format!("paddock {status}: {text}"));
143        };
144        let v: Value = resp.json().await.map_err(|e| format!("paddock decode: {e}"))?;
145        let choice = v.get("choices").and_then(|c| c.get(0)).ok_or("paddock: no choices")?;
146        let message = choice.get("message").ok_or("paddock: no message")?;
147
148        let text = message.get("content").and_then(|c| c.as_str()).unwrap_or("").to_string();
149        let mut tool_uses = Vec::new();
150        if let Some(calls) = message.get("tool_calls").and_then(|c| c.as_array()) {
151            for call in calls {
152                let id = call.get("id").and_then(|x| x.as_str()).unwrap_or("").to_string();
153                let func = call.get("function");
154                let name = func.and_then(|f| f.get("name")).and_then(|x| x.as_str()).unwrap_or("").to_string();
155                let args_str = func.and_then(|f| f.get("arguments")).and_then(|x| x.as_str()).unwrap_or("{}");
156                let input: Value = serde_json::from_str(args_str).unwrap_or(json!({}));
157                tool_uses.push((id, name, input));
158            }
159        }
160        let stop = match choice.get("finish_reason").and_then(|f| f.as_str()) {
161            Some("tool_calls") => Stop::ToolUse,
162            Some("stop") | Some("length") => Stop::EndTurn,
163            _ => {
164                if tool_uses.is_empty() {
165                    Stop::EndTurn
166                } else {
167                    Stop::ToolUse
168                }
169            }
170        };
171        Ok(Turn { text, tool_uses, stop })
172    }
173}