Skip to main content

tinyharness_lib/provider/
mod.rs

1pub mod llama_cpp;
2pub mod ollama;
3pub mod openai_compat;
4pub mod vllm;
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    pub function: ToolCallFunction,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct ToolCallFunction {
29    pub name: String,
30    pub arguments: serde_json::Value,
31    /// Gemini `thought_signature` required for multi-turn tool calling.
32    /// Gemini returns this on tool calls and requires it back in subsequent
33    /// turns. Ollama Cloud doesn't preserve it, so we capture and re-inject it.
34    #[serde(skip_serializing_if = "Option::is_none", default)]
35    pub thought_signature: Option<String>,
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct ChatMessage {
40    pub content: String,
41    #[serde(default)]
42    pub tool_calls: Vec<ToolCall>,
43    /// Thinking/reasoning content from the model (Ollama's `thinking` field).
44    /// Only populated when the model supports reasoning (e.g. qwen2.5 variants).
45    #[serde(default)]
46    pub thinking: Option<String>,
47}
48
49#[derive(Debug, Clone, Deserialize)]
50pub struct ChatMessageResponse {
51    pub message: ChatMessage,
52    pub done: bool,
53    #[serde(default)]
54    pub is_error: bool,
55    #[serde(default)]
56    pub usage: Option<TokenUsage>,
57}
58
59/// Token usage information from the provider.
60#[derive(Debug, Clone, Default, Deserialize, Serialize)]
61pub struct TokenUsage {
62    pub prompt_tokens: u32,
63    pub completion_tokens: u32,
64    pub total_tokens: u32,
65}
66
67#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
68pub enum Role {
69    System,
70    User,
71    Assistant,
72    Tool,
73}
74
75impl Display for Role {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        match self {
78            Role::System => f.write_str("System"),
79            Role::User => f.write_str("User"),
80            Role::Assistant => f.write_str("Assistant"),
81            Role::Tool => f.write_str("Tool"),
82        }
83    }
84}
85
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct Message {
88    pub role: Role,
89    pub content: String,
90    pub tool_calls: Vec<ToolCall>,
91    /// Optional images attached to the message (multimodal models).
92    /// Only meaningful for `User` role messages.
93    #[serde(default, skip_serializing_if = "Vec::is_empty")]
94    pub images: Vec<ImageAttachment>,
95}
96
97impl Message {
98    /// Create a new message with the given role and content, no tool calls, no images.
99    pub fn simple(role: Role, content: impl Into<String>) -> Self {
100        Message {
101            role,
102            content: content.into(),
103            tool_calls: vec![],
104            images: vec![],
105        }
106    }
107}
108
109pub trait Provider: Send + Sync {
110    /// Check whether the backend is reachable and healthy.
111    fn health_check(&self) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send>>;
112
113    fn list_models(&self) -> Pin<Box<dyn Future<Output = Vec<String>> + Send>>;
114
115    fn select_model(&mut self, name: String);
116
117    fn current_model(&self) -> Option<String>;
118
119    /// Send a chat request and return a receiver for streaming response chunks.
120    ///
121    /// Returns `Err(String)` if the request cannot be started (e.g. no model selected).
122    /// On success, the provider spawns a background task that streams `ChatMessageResponse`
123    /// chunks through the returned receiver. The caller drains the receiver until it
124    /// receives a chunk with `done: true`.
125    ///
126    /// Token usage, when available, is included in the final `ChatMessageResponse`
127    /// chunk (in the `usage` field). No separate method is needed to retrieve it.
128    fn chat(
129        &mut self,
130        messages: Vec<Message>,
131        tools: Vec<ToolDefinition>,
132    ) -> Pin<
133        Box<
134            dyn Future<Output = Result<tokio::sync::mpsc::Receiver<ChatMessageResponse>, String>>
135                + Send,
136        >,
137    >;
138
139    /// Set the request timeout in seconds. Only meaningful for providers that use timeouts.
140    fn set_timeout(&mut self, _timeout_secs: u64) {}
141
142    /// Set the maximum number of retries. Only meaningful for providers that use retries.
143    fn set_retries(&mut self, _max_retries: u32) {}
144
145    /// Set the think/reasoning level. Only meaningful for Ollama.
146    fn set_think_type(&mut self, _think_type: OllamaThinkType) {}
147}