tinyharness_lib/provider/
mod.rs1pub 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 #[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 #[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 #[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#[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 #[serde(default, skip_serializing_if = "Option::is_none")]
104 pub tool_call_id: Option<String>,
105 #[serde(default, skip_serializing_if = "Vec::is_empty")]
108 pub images: Vec<ImageAttachment>,
109 #[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 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 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 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 fn set_timeout(&mut self, _timeout_secs: u64) {}
168
169 fn set_retries(&mut self, _max_retries: u32) {}
171
172 fn set_think_type(&mut self, _think_type: OllamaThinkType) {}
174}