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
174    /// Constrain decoding to `schema` via the OpenAI-compatible `response_format`.
175    ///
176    /// Ollama, llama.cpp's server, vLLM and LM Studio all accept this and compile the schema into a decoding
177    /// grammar internally, so the model can only emit tokens the schema permits. That is why this works on
178    /// models with no tool-calling ability at all: nothing is being asked of the model except to continue, and
179    /// the sampler does the rest.
180    ///
181    /// Two degradations, because not every server implements the whole thing:
182    ///   * a server that rejects `json_schema` is retried with `json_object`, which constrains the output to
183    ///     *some* JSON and leaves the shape to the prompt;
184    ///   * a server that rejects `response_format` entirely returns `Ok(None)`, so the caller falls back rather
185    ///     than seeing an error it cannot act on.
186    async fn chat_json(
187        &self,
188        system: &str,
189        msgs: &[Msg],
190        schema: &Value,
191        name: &str,
192    ) -> Result<Option<Value>, String> {
193        let url = format!("{}/chat/completions", self.base_url.trim_end_matches('/'));
194
195        // Do NOT put the schema JSON in the prompt. The first version did, on the reasoning that the grammar
196        // guarantees shape while only the prompt conveys intent — and `qwen2.5:0.5b` promptly returned the facet
197        // names "surface", "token", "head" and "tail", which are the schema's own field names. A small model
198        // treats anything in its context as material to copy, so handing it a vocabulary of key names invites
199        // exactly that. The grammar already enforces the shape; the caller's own system prompt explains the task.
200        let system_full = format!("{system}\n\nReply with JSON only. No prose, no code fence.");
201
202        for mode in ["json_schema", "json_object"] {
203            let mut body = json!({
204                "model": self.model,
205                "messages": to_openai_messages(&system_full, msgs),
206                "max_tokens": 2048,
207            });
208            body["response_format"] = if mode == "json_schema" {
209                json!({ "type": "json_schema",
210                        "json_schema": { "name": name, "strict": true, "schema": schema } })
211            } else {
212                json!({ "type": "json_object" })
213            };
214
215            let mut req = self.client.post(&url).json(&body);
216            if let Some(k) = &self.api_key {
217                req = req.bearer_auth(k);
218            }
219            let resp = req.send().await.map_err(|e| format!("paddock request: {e}"))?;
220            if !resp.status().is_success() {
221                let status = resp.status();
222                let text = resp.text().await.unwrap_or_default();
223                // this server has no structured-output support at all; let the caller fall back
224                if text.contains("response_format") || status.as_u16() == 400 {
225                    continue;
226                }
227                return Err(format!("paddock {status}: {text}"));
228            }
229            let v: Value = resp.json().await.map_err(|e| format!("paddock decode: {e}"))?;
230            let content = v
231                .get("choices")
232                .and_then(|c| c.get(0))
233                .and_then(|c| c.get("message"))
234                .and_then(|m| m.get("content"))
235                .and_then(|c| c.as_str())
236                .unwrap_or("");
237            if content.trim().is_empty() {
238                continue;
239            }
240            // A constrained reply should be bare JSON, but a server that only honoured `json_object` may still
241            // wrap it in prose or a code fence, so reuse the tolerant extractor.
242            if let Ok(parsed) = serde_json::from_str::<Value>(content.trim()) {
243                return Ok(Some(parsed));
244            }
245            if let Some(parsed) = crate::vocabulary::extract_json(content) {
246                return Ok(Some(parsed));
247            }
248        }
249        Ok(None)
250    }
251}