tinyharness-lib 0.5.6

Core library for TinyHarness: LLM providers, tools, sessions and context management
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#[cfg(feature = "test-util")]
pub mod mock;
pub mod model_variants;
pub mod openai_compat;
pub mod openai_compat_provider;
pub mod sockudo;
pub mod tool_pairs;

use std::fmt::Display;
use std::future::Future;

use serde::{Deserialize, Serialize};

use crate::SecretString;
use crate::config::Settings;
use crate::image::ImageAttachment;
use crate::provider::openai_compat_provider::OpenAiCompatProvider;
use crate::provider::sockudo::SockudoProvider;

/// Channel capacity for streaming chat responses from providers to the
/// agent loop.
///
/// A small buffer applies backpressure to the provider's HTTP read loop
/// when the UI consumes chunks slowly: once the buffer is full,
/// `send().await` suspends the provider task instead of letting chunks
/// accumulate without bound. 64 chunks is far more than a terminal renders
/// per frame, so streaming throughput is unaffected in practice.
pub const STREAM_CHANNEL_CAPACITY: usize = 64;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolDefinition {
    pub name: String,
    pub description: String,
    pub parameters: schemars::Schema,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
    /// OpenAI tool call ID (e.g. `call_abc123`). Required by OpenAI-compatible
    /// servers when echoing the call back in subsequent assistant messages
    /// and when building matching `tool` result messages. Some servers generate
    /// it for us; others (or local models) may not, in which case we fall
    /// back to a synthetic id at serialization time.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    pub function: ToolCallFunction,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCallFunction {
    pub name: String,
    pub arguments: serde_json::Value,
    /// Gemini `thought_signature` required for multi-turn tool calling.
    /// Gemini returns this on tool calls and requires it back in subsequent
    /// turns. Ollama Cloud doesn't preserve it, so we capture and re-inject it.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub thought_signature: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatMessage {
    pub content: String,
    #[serde(default)]
    pub tool_calls: Vec<ToolCall>,
    /// Thinking/reasoning content from the model, as a **delta** during
    /// streaming. Ollama fills this from its `thinking` field; OpenAI-compatible
    /// servers (OpenRouter, vLLM with a reasoning parser, DeepSeek) from
    /// `delta.reasoning` / `delta.reasoning_content` / `delta.reasoning_details`.
    /// Only populated when the model actually reasons.
    #[serde(default)]
    pub thinking: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct ChatMessageResponse {
    pub message: ChatMessage,
    pub done: bool,
    #[serde(default)]
    pub is_error: bool,
    #[serde(default)]
    pub usage: Option<TokenUsage>,
}

/// Token usage information from the provider.
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct TokenUsage {
    pub prompt_tokens: u32,
    pub completion_tokens: u32,
    pub total_tokens: u32,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum Role {
    System,
    User,
    Assistant,
    Tool,
}

impl Display for Role {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Role::System => f.write_str("System"),
            Role::User => f.write_str("User"),
            Role::Assistant => f.write_str("Assistant"),
            Role::Tool => f.write_str("Tool"),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    pub role: Role,
    pub content: String,
    pub tool_calls: Vec<ToolCall>,
    /// OpenAI `tool_call_id` for `Role::Tool` messages — links the result back
    /// to the originating assistant tool call. Required by OpenAI-compatible
    /// servers. `None` is allowed for round-trips with providers that don't
    /// use it, but the OpenAI-compatible serialiser will substitute a
    /// synthetic id when missing.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tool_call_id: Option<String>,
    /// Optional images attached to the message (multimodal models).
    /// Only meaningful for `User` role messages.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub images: Vec<ImageAttachment>,
    /// Thinking/reasoning chain from the model, captured during streaming
    /// and persisted for debugging. Not sent to the provider in API requests
    /// (skipped in serialization for request types; only stored in sessions).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub thinking: Option<String>,
}

impl Default for Message {
    fn default() -> Self {
        Message::simple(Role::User, "")
    }
}

impl Message {
    /// Create a new message with the given role and content, no tool calls, no images.
    pub fn simple(role: Role, content: impl Into<String>) -> Self {
        Message {
            role,
            content: content.into(),
            tool_calls: vec![],
            tool_call_id: None,
            images: vec![],
            thinking: None,
        }
    }
}

/// Trait for LLM provider backends (OpenAI-compatible endpoints, Sockudo).
///
/// Async methods return `impl Future<...> + Send` (RPITIT) rather than
/// `Pin<Box<dyn Future>>`: no allocation, no extra crates. The trade-off is
/// that the trait is not dyn-compatible — for dynamic dispatch over a
/// runtime-selected backend use [`AnyProvider`], the concrete enum covering
/// all built-in providers.
///
/// Implementations are expected to be cheap to clone where the provider
/// spawns background streaming tasks (see [`Provider::chat`]).
pub trait Provider: Send + Sync {
    /// Check whether the backend is reachable and healthy.
    ///
    /// Called once at startup; a failure is a non-fatal warning — errors
    /// surface on the first real request. Providers whose dedicated health
    /// endpoint may be missing (OpenAI-compatible servers) fall back to the
    /// model list: a non-empty list proves the backend is up.
    fn health_check(&self) -> impl Future<Output = Result<(), String>> + Send;

    /// List model names available on the backend.
    ///
    /// Returns an empty vec when the backend doesn't expose a model list or
    /// the request fails; callers fall back to saved or first-known models.
    fn list_models(&self) -> impl Future<Output = Vec<String>> + Send;

    /// Select the model used for subsequent [`Provider::chat`] requests.
    fn select_model(&mut self, name: String);

    /// The currently selected model, if any.
    fn current_model(&self) -> Option<String>;

    /// Send a chat request and return a receiver for streaming response chunks.
    ///
    /// Returns `Err(String)` if the request cannot be started (e.g. no model selected).
    /// On success, the provider spawns a background task that streams `ChatMessageResponse`
    /// chunks through the returned receiver. The caller drains the receiver until it
    /// receives a chunk with `done: true`; dropping the receiver stops the stream.
    ///
    /// Token usage, when available, is included in the final `ChatMessageResponse`
    /// chunk (in the `usage` field). No separate method is needed to retrieve it.
    fn chat(
        &mut self,
        messages: Vec<Message>,
        tools: Vec<ToolDefinition>,
    ) -> impl Future<Output = Result<tokio::sync::mpsc::Receiver<ChatMessageResponse>, String>> + Send;

    /// Set the request timeout in seconds. Only meaningful for providers that use timeouts.
    fn set_timeout(&mut self, _timeout_secs: u64) {}

    /// Set the maximum number of retries. Only meaningful for providers that use retries.
    fn set_retries(&mut self, _max_retries: u32) {}
}

/// Concrete wrapper over all built-in provider backends.
///
/// The [`Provider`] trait is not dyn-compatible (its async methods use
/// RPITIT return types), so dynamic dispatch over a runtime-selected
/// backend goes through this enum instead of `Arc<Mutex<dyn Provider>>`.
/// Commands and the agent loop share an `Arc<Mutex<AnyProvider>>`.
pub enum AnyProvider {
    OpenAiCompat(crate::provider::openai_compat_provider::OpenAiCompatProvider),
    Sockudo(crate::provider::sockudo::SockudoProvider),
    /// Test-only variant. Constructed via `MockProviderHandle` (feature
    /// `test-util`); never built in production code paths.
    #[cfg(feature = "test-util")]
    Mock(crate::provider::mock::MockProviderHandle),
}

impl AnyProvider {
    /// Build the provider for `kind` from resolved settings values.
    ///
    /// `url` and credentials come from the caller (CLI flags / settings);
    /// `timeout_secs`/`max_retries` are the effective per-provider values
    /// from `Settings::effective_timeout_secs()`/`effective_max_retries()`.
    #[allow(clippy::too_many_arguments)]
    pub fn build(
        kind: crate::config::ProviderKind,
        base_url: String,
        api_key: Option<SecretString>,
        timeout_secs: u64,
        max_retries: u32,
        sockudo: SockudoCredentials,
    ) -> Result<Self, String> {
        Ok(match kind {
            // Ollama serves the OpenAI-compatible API at `/v1`, so it goes
            // through the same provider as llama.cpp / vLLM / hosted gateways.
            crate::config::ProviderKind::Ollama => AnyProvider::OpenAiCompat(
                OpenAiCompatProvider::with_options(base_url, None, timeout_secs, max_retries),
            ),
            crate::config::ProviderKind::LlamaCpp => AnyProvider::OpenAiCompat(
                OpenAiCompatProvider::with_options(base_url, None, timeout_secs, max_retries)
                    .with_static_models(vec!["llama-cpp".to_string()]),
            ),
            crate::config::ProviderKind::Vllm => AnyProvider::OpenAiCompat(
                OpenAiCompatProvider::with_options(base_url, None, timeout_secs, max_retries),
            ),
            crate::config::ProviderKind::OpenAiCompat => {
                // Empty key = explicit no-auth (e.g. `--api-key ""`).
                let key = api_key.filter(|k| !k.is_empty());
                AnyProvider::OpenAiCompat(OpenAiCompatProvider::with_options(
                    base_url,
                    key,
                    timeout_secs,
                    max_retries,
                ))
            }
            crate::config::ProviderKind::Sockudo => AnyProvider::Sockudo(SockudoProvider::new(
                base_url,
                sockudo.app_id,
                sockudo.app_key,
                sockudo.app_secret.unwrap_or_default(),
            )),
        })
    }
}

/// Credentials needed to construct the Sockudo provider.
#[derive(Debug, Clone, Default)]
pub struct SockudoCredentials {
    pub app_id: String,
    pub app_key: String,
    pub app_secret: Option<SecretString>,
}

impl From<&Settings> for SockudoCredentials {
    fn from(settings: &Settings) -> Self {
        SockudoCredentials {
            app_id: settings.sockudo_app_id.clone().unwrap_or_default(),
            app_key: settings.sockudo_app_key.clone().unwrap_or_default(),
            app_secret: settings.sockudo_app_secret.clone(),
        }
    }
}

impl Provider for AnyProvider {
    async fn health_check(&self) -> Result<(), String> {
        match self {
            AnyProvider::OpenAiCompat(p) => p.health_check().await,
            AnyProvider::Sockudo(p) => p.health_check().await,
            #[cfg(feature = "test-util")]
            AnyProvider::Mock(p) => p.health_check().await,
        }
    }

    async fn list_models(&self) -> Vec<String> {
        match self {
            AnyProvider::OpenAiCompat(p) => p.list_models().await,
            AnyProvider::Sockudo(p) => p.list_models().await,
            #[cfg(feature = "test-util")]
            AnyProvider::Mock(p) => p.list_models().await,
        }
    }

    fn select_model(&mut self, name: String) {
        match self {
            AnyProvider::OpenAiCompat(p) => p.select_model(name),
            AnyProvider::Sockudo(p) => p.select_model(name),
            #[cfg(feature = "test-util")]
            AnyProvider::Mock(p) => p.select_model(name),
        }
    }

    fn current_model(&self) -> Option<String> {
        match self {
            AnyProvider::OpenAiCompat(p) => p.current_model(),
            AnyProvider::Sockudo(p) => p.current_model(),
            #[cfg(feature = "test-util")]
            AnyProvider::Mock(p) => p.current_model(),
        }
    }

    async fn chat(
        &mut self,
        messages: Vec<Message>,
        tools: Vec<ToolDefinition>,
    ) -> Result<tokio::sync::mpsc::Receiver<ChatMessageResponse>, String> {
        match self {
            AnyProvider::OpenAiCompat(p) => p.chat(messages, tools).await,
            AnyProvider::Sockudo(p) => p.chat(messages, tools).await,
            #[cfg(feature = "test-util")]
            AnyProvider::Mock(p) => p.chat(messages, tools).await,
        }
    }

    fn set_timeout(&mut self, timeout_secs: u64) {
        match self {
            AnyProvider::OpenAiCompat(p) => p.set_timeout(timeout_secs),
            AnyProvider::Sockudo(p) => p.set_timeout(timeout_secs),
            #[cfg(feature = "test-util")]
            AnyProvider::Mock(p) => p.set_timeout(timeout_secs),
        }
    }

    fn set_retries(&mut self, max_retries: u32) {
        match self {
            AnyProvider::OpenAiCompat(p) => p.set_retries(max_retries),
            AnyProvider::Sockudo(p) => p.set_retries(max_retries),
            #[cfg(feature = "test-util")]
            AnyProvider::Mock(p) => p.set_retries(max_retries),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::provider::mock::{MockProvider, MockProviderHandle};
    use std::sync::{Arc, Mutex as StdMutex};

    #[test]
    fn build_returns_provider_for_each_kind() {
        let creds = SockudoCredentials::default();
        for kind in [
            crate::config::ProviderKind::Ollama,
            crate::config::ProviderKind::LlamaCpp,
            crate::config::ProviderKind::Vllm,
            crate::config::ProviderKind::OpenAiCompat,
            crate::config::ProviderKind::Sockudo,
        ] {
            let provider = AnyProvider::build(
                kind,
                "http://localhost:1234".to_string(),
                Some(SecretString::new("k")),
                30,
                0,
                creds.clone(),
            )
            .unwrap_or_else(|e| panic!("build({kind}) failed: {e}"));
            let _ = provider.current_model();
        }
    }

    #[tokio::test]
    async fn any_provider_delegates_to_mock() {
        let mock = Arc::new(StdMutex::new(MockProvider::new()));
        mock.lock().unwrap().enqueue_text("via any");
        let mut provider = AnyProvider::Mock(MockProviderHandle::new(mock));

        assert_eq!(provider.current_model().as_deref(), Some("mock-model"));
        provider.select_model("m2".to_string());
        assert_eq!(provider.current_model().as_deref(), Some("m2"));

        let mut rx = provider
            .chat(vec![Message::simple(Role::User, "hi")], vec![])
            .await
            .expect("chat via AnyProvider should work");
        let chunk = rx.recv().await.expect("chunk");
        assert!(chunk.done);
        assert_eq!(chunk.message.content, "via any");
    }

    #[tokio::test]
    async fn any_provider_health_check_delegates() {
        let mut mock = MockProvider::new();
        assert!(mock.health_check().await.is_ok());
        mock.set_health_ok(false);
        assert!(mock.health_check().await.is_err());
    }
}