Skip to main content

tinyharness_lib/provider/
mod.rs

1pub mod llama_cpp;
2pub mod ollama;
3pub mod openai_compat;
4pub mod openai_compat_provider;
5pub mod sockudo;
6pub mod vllm;
7
8use std::fmt::Display;
9use std::future::Future;
10use std::pin::Pin;
11
12use serde::{Deserialize, Serialize};
13
14use crate::config::OllamaThinkType;
15use crate::image::ImageAttachment;
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct ToolDefinition {
19    pub name: String,
20    pub description: String,
21    pub parameters: schemars::Schema,
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct ToolCall {
26    /// OpenAI tool call ID (e.g. `call_abc123`). Required by OpenAI-compatible
27    /// servers when echoing the call back in subsequent assistant messages
28    /// and when building matching `tool` result messages. Some servers generate
29    /// it for us; others (or local models) may not, in which case we fall
30    /// back to a synthetic id at serialization time.
31    #[serde(default, skip_serializing_if = "Option::is_none")]
32    pub id: Option<String>,
33    pub function: ToolCallFunction,
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct ToolCallFunction {
38    pub name: String,
39    pub arguments: serde_json::Value,
40    /// Gemini `thought_signature` required for multi-turn tool calling.
41    /// Gemini returns this on tool calls and requires it back in subsequent
42    /// turns. Ollama Cloud doesn't preserve it, so we capture and re-inject it.
43    #[serde(skip_serializing_if = "Option::is_none", default)]
44    pub thought_signature: Option<String>,
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct ChatMessage {
49    pub content: String,
50    #[serde(default)]
51    pub tool_calls: Vec<ToolCall>,
52    /// Thinking/reasoning content from the model (Ollama's `thinking` field).
53    /// Only populated when the model supports reasoning (e.g. qwen2.5 variants).
54    #[serde(default)]
55    pub thinking: Option<String>,
56}
57
58#[derive(Debug, Clone, Deserialize)]
59pub struct ChatMessageResponse {
60    pub message: ChatMessage,
61    pub done: bool,
62    #[serde(default)]
63    pub is_error: bool,
64    #[serde(default)]
65    pub usage: Option<TokenUsage>,
66}
67
68/// Token usage information from the provider.
69#[derive(Debug, Clone, Default, Deserialize, Serialize)]
70pub struct TokenUsage {
71    pub prompt_tokens: u32,
72    pub completion_tokens: u32,
73    pub total_tokens: u32,
74}
75
76#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
77pub enum Role {
78    System,
79    User,
80    Assistant,
81    Tool,
82}
83
84impl Display for Role {
85    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86        match self {
87            Role::System => f.write_str("System"),
88            Role::User => f.write_str("User"),
89            Role::Assistant => f.write_str("Assistant"),
90            Role::Tool => f.write_str("Tool"),
91        }
92    }
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct Message {
97    pub role: Role,
98    pub content: String,
99    pub tool_calls: Vec<ToolCall>,
100    /// OpenAI `tool_call_id` for `Role::Tool` messages — links the result back
101    /// to the originating assistant tool call. Required by OpenAI-compatible
102    /// servers. `None` is allowed for round-trips with providers that don't
103    /// use it, but the OpenAI-compatible serialiser will substitute a
104    /// synthetic id when missing.
105    #[serde(default, skip_serializing_if = "Option::is_none")]
106    pub tool_call_id: Option<String>,
107    /// Optional images attached to the message (multimodal models).
108    /// Only meaningful for `User` role messages.
109    #[serde(default, skip_serializing_if = "Vec::is_empty")]
110    pub images: Vec<ImageAttachment>,
111}
112
113impl Default for Message {
114    fn default() -> Self {
115        Message::simple(Role::User, "")
116    }
117}
118
119impl Message {
120    /// Create a new message with the given role and content, no tool calls, no images.
121    pub fn simple(role: Role, content: impl Into<String>) -> Self {
122        Message {
123            role,
124            content: content.into(),
125            tool_calls: vec![],
126            tool_call_id: None,
127            images: vec![],
128        }
129    }
130}
131
132pub trait Provider: Send + Sync {
133    /// Check whether the backend is reachable and healthy.
134    fn health_check(&self) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send>>;
135
136    fn list_models(&self) -> Pin<Box<dyn Future<Output = Vec<String>> + Send>>;
137
138    fn select_model(&mut self, name: String);
139
140    fn current_model(&self) -> Option<String>;
141
142    /// Send a chat request and return a receiver for streaming response chunks.
143    ///
144    /// Returns `Err(String)` if the request cannot be started (e.g. no model selected).
145    /// On success, the provider spawns a background task that streams `ChatMessageResponse`
146    /// chunks through the returned receiver. The caller drains the receiver until it
147    /// receives a chunk with `done: true`.
148    ///
149    /// Token usage, when available, is included in the final `ChatMessageResponse`
150    /// chunk (in the `usage` field). No separate method is needed to retrieve it.
151    fn chat(
152        &mut self,
153        messages: Vec<Message>,
154        tools: Vec<ToolDefinition>,
155    ) -> Pin<
156        Box<
157            dyn Future<Output = Result<tokio::sync::mpsc::Receiver<ChatMessageResponse>, String>>
158                + Send,
159        >,
160    >;
161
162    /// Set the request timeout in seconds. Only meaningful for providers that use timeouts.
163    fn set_timeout(&mut self, _timeout_secs: u64) {}
164
165    /// Set the maximum number of retries. Only meaningful for providers that use retries.
166    fn set_retries(&mut self, _max_retries: u32) {}
167
168    /// Set the think/reasoning level. Only meaningful for Ollama.
169    fn set_think_type(&mut self, _think_type: OllamaThinkType) {}
170}