Skip to main content

tinyharness_lib/provider/
mod.rs

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