tinyharness-lib 0.2.0

core liblary for tinyharness
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::time::Duration;

use reqwest::Client;
use serde::{Deserialize, Serialize};
use tokio_stream::StreamExt;

use crate::provider::{
    ChatMessage, ChatMessageResponse, Message, Role, ToolCall, ToolCallFunction, ToolDefinition,
};

/// Shared inner state for OpenAI-compatible providers (llama.cpp, vLLM, etc.).
///
/// Encapsulates the common `{client, base_url, model}` fields and all shared
/// logic so that provider implementations only need to differ in `list_models()`.
pub struct OpenAiCompatInner {
    client: Client,
    base_url: String,
    model: Option<String>,
}

impl OpenAiCompatInner {
    pub fn new(base_url: String) -> Self {
        let client = Client::builder()
            .connect_timeout(Duration::from_secs(15))
            .read_timeout(Duration::from_secs(300))
            .build()
            .unwrap_or_else(|_| Client::new());
        OpenAiCompatInner {
            client,
            base_url,
            model: None,
        }
    }

    /// Perform a health check against the server's `/health` endpoint.
    pub fn health_check(&self) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send>> {
        let url = format!("{}/health", self.base_url.trim_end_matches('/'));
        let client = self.client.clone();
        Box::pin(async move {
            match client.get(&url).send().await {
                Ok(resp) if resp.status().is_success() => Ok(()),
                Ok(resp) => Err(format!(
                    "Server returned {}: {}",
                    resp.status().as_u16(),
                    resp.text().await.unwrap_or_default()
                )),
                Err(e) => Err(format!("Cannot reach {}: {}", url, e)),
            }
        })
    }

    pub fn select_model(&mut self, name: String) {
        self.model = Some(name);
    }

    pub fn current_model(&self) -> Option<String> {
        self.model.clone()
    }

    /// Return the `/v1/chat/completions` URL for this server.
    pub fn chat_url(&self) -> String {
        format!(
            "{}/v1/chat/completions",
            self.base_url.trim_end_matches('/')
        )
    }

    /// Fetch the model list from the server's `/v1/models` endpoint.
    /// Returns the list of model IDs, or an empty vec on failure.
    pub fn fetch_model_list(&self) -> Pin<Box<dyn Future<Output = Vec<String>> + Send>> {
        let url = format!("{}/v1/models", self.base_url.trim_end_matches('/'));
        let client = self.client.clone();
        let current_model = self.model.clone();
        Box::pin(async move {
            match client.get(&url).send().await {
                Ok(resp) if resp.status().is_success() => {
                    match resp.json::<ModelListResponse>().await {
                        Ok(list) => list.data.into_iter().map(|m| m.id).collect(),
                        Err(_) => current_model.into_iter().collect(),
                    }
                }
                _ => current_model.into_iter().collect(),
            }
        })
    }

    /// Stream chat completions using the OpenAI-compatible API.
    /// Returns a receiver for streaming response chunks, or an error string
    /// if the request cannot be started.
    pub fn chat(
        &self,
        messages: Vec<Message>,
        tools: Vec<ToolDefinition>,
    ) -> Pin<
        Box<
            dyn Future<Output = Result<tokio::sync::mpsc::Receiver<ChatMessageResponse>, String>>
                + Send,
        >,
    > {
        let (send, recv) = tokio::sync::mpsc::channel::<ChatMessageResponse>(1024);

        let model = self.model.clone().unwrap_or_default();
        let openai_messages = messages.into_iter().map(to_openai_message).collect();
        let openai_tools = tools.into_iter().map(to_openai_tool).collect();
        let client = self.client.clone();
        let chat_url = self.chat_url();

        let body = ChatRequest {
            model,
            messages: openai_messages,
            stream: true,
            stream_options: Some(StreamOptions {
                include_usage: true,
            }),
            tools: openai_tools,
        };

        // Spawn the streaming work on a background task
        tokio::spawn(async move {
            let _usage = stream_chat_completions(&client, &chat_url, &body, &send).await;
        });

        Box::pin(async move { Ok(recv) })
    }
}

// ── OpenAI-compatible request/response types ──

#[derive(Serialize)]
pub struct ChatRequest {
    pub model: String,
    pub messages: Vec<OpenAIMessage>,
    pub stream: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream_options: Option<StreamOptions>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tools: Vec<OpenAITool>,
}

#[derive(Serialize)]
pub struct StreamOptions {
    pub include_usage: bool,
}

#[derive(Serialize)]
pub struct OpenAIMessage {
    pub role: String,
    pub content: serde_json::Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_calls: Option<Vec<OpenAIToolCall>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_call_id: Option<String>,
}

#[derive(Serialize)]
pub struct OpenAITool {
    #[serde(rename = "type")]
    pub tool_type: String,
    pub function: OpenAIToolFunction,
}

#[derive(Serialize)]
pub struct OpenAIToolFunction {
    pub name: String,
    pub description: String,
    pub parameters: serde_json::Value,
}

#[derive(Serialize, Deserialize, Clone)]
pub struct OpenAIToolCall {
    #[serde(default)]
    pub index: usize,
    #[serde(default)]
    pub id: String,
    #[serde(rename = "type", default)]
    pub call_type: String,
    #[serde(default)]
    pub function: OpenAIToolCallFunction,
}

#[derive(Serialize, Deserialize, Clone, Default)]
pub struct OpenAIToolCallFunction {
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub arguments: String,
}

#[derive(Deserialize)]
pub struct ChunkChoice {
    pub delta: Delta,
    #[serde(default, rename = "finish_reason")]
    pub _finish_reason: Option<String>,
}

#[derive(Deserialize, Default)]
pub struct Delta {
    #[serde(default)]
    pub content: Option<String>,
    #[serde(default)]
    pub tool_calls: Option<Vec<OpenAIToolCall>>,
}

#[derive(Deserialize)]
pub struct StreamChunk {
    #[serde(default)]
    pub choices: Vec<ChunkChoice>,
    #[serde(default)]
    pub usage: Option<OpenAIUsage>,
}

#[derive(Deserialize, Clone)]
pub struct OpenAIUsage {
    #[serde(default)]
    pub prompt_tokens: u32,
    #[serde(default)]
    pub completion_tokens: u32,
    #[serde(default)]
    pub total_tokens: u32,
}

// ── Model list response types ──

#[derive(Deserialize)]
pub struct ModelListResponse {
    pub data: Vec<ModelEntry>,
}

#[derive(Deserialize)]
pub struct ModelEntry {
    pub id: String,
}

// ── Conversion helpers ──

pub fn to_openai_message(msg: Message) -> OpenAIMessage {
    /// Build the content value: if images are present, use multipart array format;
    /// otherwise use plain string.
    fn build_content(msg: &Message) -> serde_json::Value {
        if msg.images.is_empty() {
            serde_json::Value::String(msg.content.clone())
        } else {
            let mut parts: Vec<serde_json::Value> = Vec::new();
            // Add text part
            if !msg.content.is_empty() {
                parts.push(serde_json::json!({
                    "type": "text",
                    "text": msg.content
                }));
            }
            // Add image parts
            for img in &msg.images {
                parts.push(serde_json::json!({
                    "type": "image_url",
                    "image_url": {
                        "url": img.data_uri()
                    }
                }));
            }
            serde_json::Value::Array(parts)
        }
    }

    match msg.role {
        Role::System => OpenAIMessage {
            role: "system".to_string(),
            content: serde_json::Value::String(msg.content),
            tool_calls: None,
            tool_call_id: None,
        },
        Role::User => OpenAIMessage {
            role: "user".to_string(),
            content: build_content(&msg),
            tool_calls: None,
            tool_call_id: None,
        },
        Role::Assistant => {
            if msg.tool_calls.is_empty() {
                OpenAIMessage {
                    role: "assistant".to_string(),
                    content: serde_json::Value::String(msg.content),
                    tool_calls: None,
                    tool_call_id: None,
                }
            } else {
                let tool_calls: Vec<OpenAIToolCall> = msg
                    .tool_calls
                    .into_iter()
                    .enumerate()
                    .map(|(i, tc)| OpenAIToolCall {
                        index: i,
                        id: String::new(),
                        call_type: "function".to_string(),
                        function: OpenAIToolCallFunction {
                            name: tc.function.name,
                            arguments: tc.function.arguments.to_string(),
                        },
                    })
                    .collect();
                OpenAIMessage {
                    role: "assistant".to_string(),
                    content: serde_json::Value::String(msg.content),
                    tool_calls: Some(tool_calls),
                    tool_call_id: None,
                }
            }
        }
        Role::Tool => OpenAIMessage {
            role: "tool".to_string(),
            content: serde_json::Value::String(msg.content),
            tool_calls: None,
            tool_call_id: None,
        },
    }
}

pub fn to_openai_tool(ti: ToolDefinition) -> OpenAITool {
    OpenAITool {
        tool_type: "function".to_string(),
        function: OpenAIToolFunction {
            name: ti.name,
            description: ti.description,
            parameters: serde_json::to_value(ti.parameters).unwrap_or_default(),
        },
    }
}

/// Stream chat completions from an OpenAI-compatible endpoint.
/// Returns accumulated tool calls and final content via the sender.
/// Also returns the token usage if available.
pub async fn stream_chat_completions(
    client: &reqwest::Client,
    url: &str,
    body: &ChatRequest,
    send: &tokio::sync::mpsc::Sender<ChatMessageResponse>,
) -> Option<crate::provider::TokenUsage> {
    let response = match client.post(url).json(body).send().await {
        Ok(r) => r,
        Err(e) => {
            let _ = send
                .send(ChatMessageResponse {
                    message: ChatMessage {
                        content: format!("Error: {}", e),
                        tool_calls: vec![],
                        thinking: None,
                    },
                    done: true,
                    is_error: true,
                    usage: None,
                })
                .await;
            return None;
        }
    };

    let mut stream = response.bytes_stream();
    let mut buf = String::new();

    let mut acc_tool_calls: HashMap<usize, OpenAIToolCall> = HashMap::new();
    let mut response_content = String::new();
    let mut token_usage: Option<crate::provider::TokenUsage> = None;

    while let Some(chunk_result) = stream.next().await {
        let chunk = match chunk_result {
            Ok(c) => c,
            Err(e) => {
                // Stream read error — surface it rather than silently breaking
                let _ = send
                    .send(ChatMessageResponse {
                        message: ChatMessage {
                            content: format!("\n\nStream error: {}", e),
                            tool_calls: vec![],
                            thinking: None,
                        },
                        done: true,
                        is_error: true,
                        usage: None,
                    })
                    .await;
                break;
            }
        };

        buf.push_str(&String::from_utf8_lossy(&chunk));

        loop {
            match buf.find('\n') {
                None => break,
                Some(pos) => {
                    let line = buf[..pos].trim().to_string();
                    buf = buf[pos + 1..].to_string();

                    if line.is_empty() || line == "data: [DONE]" {
                        continue;
                    }

                    if let Some(data) = line.strip_prefix("data: ")
                        && let Ok(chunk) = serde_json::from_str::<StreamChunk>(data)
                    {
                        // Capture token usage if present (usually in the final chunk)
                        if let Some(usage) = &chunk.usage {
                            token_usage = Some(crate::provider::TokenUsage {
                                prompt_tokens: usage.prompt_tokens,
                                completion_tokens: usage.completion_tokens,
                                total_tokens: usage.total_tokens,
                            });
                        }

                        for choice in chunk.choices {
                            if let Some(content) = &choice.delta.content {
                                response_content.push_str(content);
                            }

                            if let Some(tool_calls) = &choice.delta.tool_calls {
                                for tc in tool_calls {
                                    let entry =
                                        acc_tool_calls.entry(tc.index).or_insert(OpenAIToolCall {
                                            index: tc.index,
                                            id: String::new(),
                                            call_type: "function".to_string(),
                                            function: OpenAIToolCallFunction::default(),
                                        });

                                    if !tc.id.is_empty() {
                                        entry.id = tc.id.clone();
                                    }
                                    if !tc.function.name.is_empty() {
                                        entry.function.name = tc.function.name.clone();
                                    }
                                    entry.function.arguments.push_str(&tc.function.arguments);
                                }
                            }
                        }
                    }
                }
            }
        }

        if !response_content.is_empty() {
            let _ = send
                .send(ChatMessageResponse {
                    message: ChatMessage {
                        content: response_content.clone(),
                        tool_calls: vec![],
                        thinking: None,
                    },
                    done: false,
                    is_error: false,
                    usage: None,
                })
                .await;
            response_content.clear();
        }
    }

    let tool_calls: Vec<ToolCall> = if !acc_tool_calls.is_empty() {
        acc_tool_calls
            .into_values()
            .map(|tc| {
                let args: serde_json::Value =
                    serde_json::from_str(&tc.function.arguments).unwrap_or(serde_json::Value::Null);
                ToolCall {
                    function: ToolCallFunction {
                        name: tc.function.name,
                        arguments: args,
                        thought_signature: None,
                    },
                }
            })
            .collect()
    } else {
        vec![]
    };

    // Send the final response with tool calls and token usage
    let _ = send
        .send(ChatMessageResponse {
            message: ChatMessage {
                content: String::new(),
                tool_calls,
                thinking: None,
            },
            done: true,
            is_error: false,
            usage: token_usage.clone(),
        })
        .await;

    token_usage
}