reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
//! # Local Ollama Fallback
//!
//! Local deployment compatibility for GLM-4.6 when cloud API fails.
//! Uses ollama for local inference with graceful degradation.

// use anyhow::{Context, Result};
use reqwest::Client;
use serde::{Deserialize, Serialize};
// use std::time::Duration;
use tokio::time::timeout;
use tracing::{debug, info, warn};

use crate::glm46::types::{
    ChatMessage, ChatRequest, ChatResponse, Choice, FinishReason, GLM46Error, GLM46Result,
    OllamaConfig, TokenUsage,
};

/// Ollama client for local fallback
#[derive(Debug, Clone)]
pub struct OllamaClient {
    config: OllamaConfig,
    http_client: Client,
}

impl OllamaClient {
    /// Create new Ollama client
    pub fn new(config: OllamaConfig) -> Self {
        let http_client = Client::builder()
            .timeout(config.timeout)
            .user_agent("reasonkit-glm46/0.1.0")
            .build()
            .expect("Failed to create HTTP client");

        Self {
            config,
            http_client,
        }
    }

    /// Check if Ollama is available
    pub async fn is_available(&self) -> bool {
        let response = self
            .http_client
            .get(format!("{}/api/version", self.config.url))
            .send()
            .await;

        response.is_ok_and(|r| r.status().is_success())
    }

    /// Execute chat completion with fallback
    pub async fn chat_completion(&self, request: ChatRequest) -> GLM46Result<ChatResponse> {
        if !self.config.enabled {
            return Err(GLM46Error::Config("Ollama fallback disabled".to_string()));
        }

        debug!(
            "Executing fallback request via Ollama: {}",
            self.config.model
        );

        let ollama_request = OllamaRequest::from_chat_request(request, &self.config);

        let response = timeout(self.config.timeout, self.send_request(&ollama_request))
            .await
            .map_err(|_| GLM46Error::Timeout(self.config.timeout))?
            .map_err(GLM46Error::Network)?;

        self.parse_response(response).await
    }

    /// Stream chat completion via Ollama
    pub async fn stream_chat_completion(
        &self,
        request: ChatRequest,
    ) -> GLM46Result<tokio::sync::mpsc::UnboundedReceiver<crate::glm46::types::StreamChunk>> {
        if !self.config.enabled {
            return Err(GLM46Error::Config("Ollama fallback disabled".to_string()));
        }

        let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
        let client = self.clone();

        tokio::spawn(async move {
            let ollama_request = OllamaRequest::from_chat_request(request, &client.config);

            match client.send_stream_request(ollama_request, tx).await {
                Ok(_) => debug!("Ollama stream completed successfully"),
                Err(e) => warn!("Ollama stream error: {:?}", e),
            }
        });

        Ok(rx)
    }

    /// Send request to Ollama API
    async fn send_request(
        &self,
        request: &OllamaRequest,
    ) -> std::result::Result<reqwest::Response, reqwest::Error> {
        let url = format!("{}/api/chat", self.config.url);

        debug!("Sending request to Ollama at: {}", url);

        let response = self
            .http_client
            .post(&url)
            .header("Content-Type", "application/json")
            .json(request)
            .send()
            .await?;

        Ok(response)
    }

    /// Send streaming request to Ollama
    async fn send_stream_request(
        &self,
        request: OllamaRequest,
        tx: tokio::sync::mpsc::UnboundedSender<crate::glm46::types::StreamChunk>,
    ) -> GLM46Result<()> {
        let url = format!("{}/api/chat", self.config.url);

        let request_builder = self
            .http_client
            .post(&url)
            .header("Content-Type", "application/json");

        // Add streaming parameters
        let mut stream_request = request.clone();
        stream_request.stream = true;

        let response = request_builder.json(&stream_request).send().await?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            return Err(GLM46Error::API {
                message: format!("Ollama API error {}: {}", status, body),
                code: Some(status.as_u16().to_string()),
            });
        }

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

        use futures::stream::StreamExt;

        while let Some(chunk_result) = stream.next().await {
            let chunk = chunk_result?;
            let chunk_str = String::from_utf8_lossy(&chunk);
            buffer.push_str(&chunk_str);

            // Process complete JSON objects
            while let Some(end_idx) = buffer.find('}') {
                if let Some(start_idx) = buffer.rfind('{') {
                    if start_idx < end_idx {
                        let json_str = buffer[start_idx..=end_idx].to_string();
                        buffer = buffer[end_idx + 1..].to_string();

                        match serde_json::from_str::<OllamaResponse>(&json_str) {
                            Ok(ollama_response) => {
                                let chunk = crate::glm46::types::StreamChunk {
                                    id: "ollama-stream".to_string(),
                                    object: "chat.completion.chunk".to_string(),
                                    created: std::time::SystemTime::now()
                                        .duration_since(std::time::UNIX_EPOCH)
                                        .unwrap_or_default()
                                        .as_secs(),
                                    model: self.config.model.clone(),
                                    choices: vec![crate::glm46::types::StreamChoice {
                                        index: 0,
                                        delta: crate::glm46::types::ChatMessageDelta {
                                            role: if ollama_response.message.role == "assistant" {
                                                Some(crate::glm46::types::MessageRole::Assistant)
                                            } else {
                                                None
                                            },
                                            content: Some(ollama_response.message.content.clone()),
                                            tool_calls: None,
                                        },
                                        finish_reason: if ollama_response.done {
                                            Some(crate::glm46::types::FinishReason::Stop)
                                        } else {
                                            None
                                        },
                                    }],
                                    usage: ollama_response.done.then(|| {
                                        crate::glm46::types::TokenUsage {
                                            prompt_tokens: ollama_response
                                                .prompt_eval_count
                                                .unwrap_or(0),
                                            completion_tokens: ollama_response
                                                .eval_count
                                                .unwrap_or(0),
                                            total_tokens: ollama_response
                                                .prompt_eval_count
                                                .unwrap_or(0)
                                                + ollama_response.eval_count.unwrap_or(0),
                                        }
                                    }),
                                };

                                if tx.send(chunk).is_err() {
                                    break; // Channel closed
                                }
                            }
                            Err(e) => {
                                warn!(
                                    "Failed to parse Ollama stream chunk: {:?}\nData: {}",
                                    e, json_str
                                );
                            }
                        }
                    }
                }
                // If no valid JSON found, clear buffer and continue
                buffer.clear();
            }
        }

        Ok(())
    }

    /// Parse Ollama response
    async fn parse_response(&self, response: reqwest::Response) -> GLM46Result<ChatResponse> {
        let status = response.status();
        let body = response.text().await?;

        if !status.is_success() {
            return Err(GLM46Error::API {
                message: format!("Ollama error {}: {}", status, body),
                code: Some(status.as_u16().to_string()),
            });
        }

        let ollama_response: OllamaResponse =
            serde_json::from_str(&body).map_err(GLM46Error::Json)?;

        debug!(
            "Ollama response: done={}, content_length={}",
            ollama_response.done,
            ollama_response.message.content.len()
        );

        Ok(ollama_response.into_chat_response())
    }
}

/// Ollama API request format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OllamaRequest {
    pub model: String,
    pub messages: Vec<OllamaMessage>,
    pub format: Option<String>, // "json" for structured output
    pub stream: bool,
    pub options: Option<OllamaOptions>,
}

impl OllamaRequest {
    pub fn from_chat_request(request: ChatRequest, config: &OllamaConfig) -> Self {
        Self {
            model: config.model.clone(),
            messages: request.messages.into_iter().map(Into::into).collect(),
            format: if request.response_format.is_some() {
                Some("json".to_string())
            } else {
                None
            },
            stream: false,
            options: Some(OllamaOptions {
                temperature: Some(request.temperature),
                num_predict: Some(request.max_tokens as u32),
                top_p: request.top_p,
                stop: request.stop,
            }),
        }
    }
}

/// Ollama message format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OllamaMessage {
    pub role: String,
    pub content: String,
}

impl From<ChatMessage> for OllamaMessage {
    fn from(msg: ChatMessage) -> Self {
        Self {
            role: match msg.role {
                crate::glm46::types::MessageRole::System => "system".to_string(),
                crate::glm46::types::MessageRole::User => "user".to_string(),
                crate::glm46::types::MessageRole::Assistant => "assistant".to_string(),
                crate::glm46::types::MessageRole::Tool => "tool".to_string(),
            },
            content: msg.content,
        }
    }
}

/// Ollama generation options
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OllamaOptions {
    pub temperature: Option<f32>,
    pub num_predict: Option<u32>,
    pub top_p: Option<f32>,
    pub stop: Option<Vec<String>>,
}

/// Ollama API response format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OllamaResponse {
    pub message: OllamaMessage,
    pub done: bool,
    pub prompt_eval_count: Option<usize>,
    pub eval_count: Option<usize>,
    pub total_duration: Option<u64>,
    pub load_duration: Option<u64>,
}

impl OllamaResponse {
    pub fn into_chat_response(self) -> ChatResponse {
        ChatResponse {
            id: format!("ollama-{}", uuid::Uuid::new_v4()),
            object: "chat.completion".to_string(),
            created: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
            model: "ollama-local".to_string(),
            choices: vec![Choice {
                index: 0,
                message: ChatMessage {
                    role: crate::glm46::types::MessageRole::Assistant,
                    content: self.message.content,
                    tool_calls: None,
                    tool_call_id: None,
                },
                finish_reason: if self.done {
                    FinishReason::Stop
                } else {
                    FinishReason::Length
                },
            }],
            usage: TokenUsage {
                prompt_tokens: self.prompt_eval_count.unwrap_or(0),
                completion_tokens: self.eval_count.unwrap_or(0),
                total_tokens: self.prompt_eval_count.unwrap_or(0) + self.eval_count.unwrap_or(0),
            },
            system_fingerprint: Some(format!(
                "{:?}+{:?}",
                self.total_duration, self.load_duration
            )),
        }
    }
}

/// Fallback manager for GLM-4.6 client
#[derive(Debug)]
pub struct FallbackManager {
    ollama_client: OllamaClient,
    enabled: bool,
    failure_threshold: u32,
    consecutive_failures: std::sync::atomic::AtomicU32,
}

impl FallbackManager {
    pub fn new(config: OllamaConfig) -> Self {
        Self {
            ollama_client: OllamaClient::new(config.clone()),
            enabled: config.enabled,
            failure_threshold: config.fallback_threshold,
            consecutive_failures: std::sync::atomic::AtomicU32::new(0),
        }
    }

    /// Check if fallback should be used
    pub async fn should_use_fallback(&self) -> bool {
        self.enabled
            && self.ollama_client.is_available().await
            && self
                .consecutive_failures
                .load(std::sync::atomic::Ordering::Relaxed)
                >= self.failure_threshold
    }

    /// Record API failure
    pub fn record_failure(&self) {
        let failures = self
            .consecutive_failures
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
            + 1;
        warn!("GLM-4.6 API failure #{}", failures);

        if failures >= self.failure_threshold {
            info!(
                "Activating Ollama fallback after {} consecutive failures",
                failures
            );
        }
    }

    /// Record API success
    pub fn record_success(&self) {
        self.consecutive_failures
            .store(0, std::sync::atomic::Ordering::Relaxed);
    }

    /// Execute fallback request
    pub async fn execute_fallback(&self, request: ChatRequest) -> GLM46Result<ChatResponse> {
        if !self.should_use_fallback().await {
            return Err(GLM46Error::Config(
                "Ollama fallback not available".to_string(),
            ));
        }

        info!("Executing request via Ollama fallback");
        self.ollama_client.chat_completion(request).await
    }

    /// Get current status
    pub async fn get_status(&self) -> FallbackStatus {
        FallbackStatus {
            enabled: self.enabled,
            ollama_available: self.ollama_client.is_available().await,
            consecutive_failures: self
                .consecutive_failures
                .load(std::sync::atomic::Ordering::Relaxed),
            failure_threshold: self.failure_threshold,
            active: self.should_use_fallback().await,
        }
    }
}

/// Fallback status information
#[derive(Debug, Clone)]
pub struct FallbackStatus {
    pub enabled: bool,
    pub ollama_available: bool,
    pub consecutive_failures: u32,
    pub failure_threshold: u32,
    pub active: bool,
}