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}
110
111impl Default for Message {
112 fn default() -> Self {
113 Message::simple(Role::User, "")
114 }
115}
116
117impl Message {
118 pub fn simple(role: Role, content: impl Into<String>) -> Self {
120 Message {
121 role,
122 content: content.into(),
123 tool_calls: vec![],
124 tool_call_id: None,
125 images: vec![],
126 }
127 }
128}
129
130pub trait Provider: Send + Sync {
131 fn health_check(&self) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send>>;
133
134 fn list_models(&self) -> Pin<Box<dyn Future<Output = Vec<String>> + Send>>;
135
136 fn select_model(&mut self, name: String);
137
138 fn current_model(&self) -> Option<String>;
139
140 fn chat(
150 &mut self,
151 messages: Vec<Message>,
152 tools: Vec<ToolDefinition>,
153 ) -> Pin<
154 Box<
155 dyn Future<Output = Result<tokio::sync::mpsc::Receiver<ChatMessageResponse>, String>>
156 + Send,
157 >,
158 >;
159
160 fn set_timeout(&mut self, _timeout_secs: u64) {}
162
163 fn set_retries(&mut self, _max_retries: u32) {}
165
166 fn set_think_type(&mut self, _think_type: OllamaThinkType) {}
168}