clifcode 1.32.2

Open-source AI coding agent for your terminal — tool-calling loop, streaming, sessions, any provider
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
//! Model backend abstraction — API and stub.

use crate::tools::{ApiToolCall, parse_api_tool_calls};
use crate::ui;
use anyhow::Result;
use std::io::{BufRead, BufReader};

/// Token usage from a single API call
#[derive(Debug, Clone, Default)]
pub struct TokenUsage {
    pub prompt_tokens: usize,
    pub completion_tokens: usize,
}

/// Result of a chat call — may contain text, tool calls, or both
pub struct ChatResponse {
    pub content: String,
    pub tool_calls: Vec<ApiToolCall>,
    /// The raw assistant message for re-sending in conversation
    pub raw_message: serde_json::Value,
    /// Whether content was already streamed to terminal (skip print_assistant)
    pub streamed: bool,
    /// Token usage (if available from API)
    pub usage: Option<TokenUsage>,
}

pub enum ModelBackend {
    /// OpenAI-compatible API (OpenRouter, OpenAI, Anthropic, Ollama, etc.)
    Api {
        url: String,
        key: Option<String>,
        model: String,
        max_tokens: usize,
    },
    /// Testing stub (no model)
    Stub,
}

impl ModelBackend {
    pub fn name(&self) -> &str {
        match self {
            ModelBackend::Api { model, .. } => model.as_str(),
            ModelBackend::Stub => "stub",
        }
    }

    pub fn chat_with_tools(
        &self,
        messages: &[serde_json::Value],
        tools: Option<&serde_json::Value>,
    ) -> Result<ChatResponse> {
        match self {
            ModelBackend::Api { url, key, model, max_tokens } => {
                api_chat_with_tools(url, key.as_deref(), model, messages, *max_tokens, tools)
            }
            ModelBackend::Stub => stub_response(messages),
        }
    }

    /// Streaming chat — prints tokens live for API, falls back to non-streaming for others.
    pub fn chat_stream(
        &self,
        messages: &[serde_json::Value],
        tools: Option<&serde_json::Value>,
    ) -> Result<ChatResponse> {
        match self {
            ModelBackend::Api { url, key, model, max_tokens } => {
                api_chat_stream(url, key.as_deref(), model, messages, *max_tokens, tools)
            }
            // Local and stub don't support streaming — fall back
            _ => self.chat_with_tools(messages, tools),
        }
    }
}

// ---------------------------------------------------------------------------
// Stub backend
// ---------------------------------------------------------------------------

fn stub_response(messages: &[serde_json::Value]) -> Result<ChatResponse> {
    let last_content = messages
        .last()
        .and_then(|m| m.get("content"))
        .and_then(|v| v.as_str())
        .unwrap_or("");

    let has_tool_results = messages
        .iter()
        .any(|m| m.get("role").and_then(|v| v.as_str()) == Some("tool"));

    if has_tool_results {
        Ok(ChatResponse {
            content: String::new(),
            tool_calls: vec![ApiToolCall {
                id: "stub_1".into(),
                name: "submit".into(),
                arguments: r#"{"summary":"Explored the workspace."}"#.into(),
            }],
            raw_message: serde_json::json!({
                "role": "assistant",
                "content": null,
                "tool_calls": [{
                    "id": "stub_1",
                    "type": "function",
                    "function": {
                        "name": "submit",
                        "arguments": "{\"summary\":\"Explored the workspace.\"}"
                    }
                }]
            }),
            streamed: false,
            usage: None,
        })
    } else if last_content.len() < 20 {
        Ok(ChatResponse {
            content: "Hello! I'm ClifCode. Give me a coding task and I'll get to work.".into(),
            tool_calls: vec![],
            raw_message: serde_json::json!({
                "role": "assistant",
                "content": "Hello! I'm ClifCode. Give me a coding task and I'll get to work."
            }),
            streamed: false,
            usage: None,
        })
    } else {
        Ok(ChatResponse {
            content: "Let me explore the project.".into(),
            tool_calls: vec![ApiToolCall {
                id: "stub_0".into(),
                name: "run_command".into(),
                arguments: r#"{"command":"ls -la"}"#.into(),
            }],
            raw_message: serde_json::json!({
                "role": "assistant",
                "content": "Let me explore the project.",
                "tool_calls": [{
                    "id": "stub_0",
                    "type": "function",
                    "function": {
                        "name": "run_command",
                        "arguments": "{\"command\":\"ls -la\"}"
                    }
                }]
            }),
            streamed: false,
            usage: None,
        })
    }
}

// ---------------------------------------------------------------------------
// API backend (OpenAI-compatible)
// ---------------------------------------------------------------------------

fn api_chat_with_tools(
    base_url: &str,
    api_key: Option<&str>,
    model: &str,
    messages: &[serde_json::Value],
    max_tokens: usize,
    tools: Option<&serde_json::Value>,
) -> Result<ChatResponse> {
    let url = format!("{}/chat/completions", base_url.trim_end_matches('/'));

    let mut body = serde_json::json!({
        "model": model,
        "messages": messages,
        "max_tokens": max_tokens,
        "temperature": 0.7,
    });

    if let Some(tools) = tools {
        body["tools"] = tools.clone();
    }

    let mut req = ureq::post(&url).set("Content-Type", "application/json");

    if let Some(key) = api_key {
        req = req.set("Authorization", &format!("Bearer {key}"));
    }

    let resp = match req.send_string(&body.to_string()) {
        Ok(r) => r,
        Err(ureq::Error::Status(code, response)) => {
            let body_text: String = response.into_string().unwrap_or_default().chars().take(300).collect();
            return Err(anyhow::anyhow!(
                "API request failed: {url}: status code {code} — {body_text}"
            ));
        }
        Err(e) => {
            return Err(anyhow::anyhow!("API request failed: {url}: {e}"));
        }
    };

    let resp_body: serde_json::Value = resp.into_json()?;

    let content = resp_body
        .pointer("/choices/0/message/content")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .to_string();

    let tool_calls = parse_api_tool_calls(&resp_body);

    let raw_message = resp_body
        .pointer("/choices/0/message")
        .cloned()
        .unwrap_or_else(|| serde_json::json!({"role": "assistant", "content": content}));

    let usage = extract_usage(&resp_body);

    Ok(ChatResponse { content, tool_calls, raw_message, streamed: false, usage })
}

// ---------------------------------------------------------------------------
// Streaming API (SSE)
// ---------------------------------------------------------------------------

fn api_chat_stream(
    base_url: &str,
    api_key: Option<&str>,
    model: &str,
    messages: &[serde_json::Value],
    max_tokens: usize,
    tools: Option<&serde_json::Value>,
) -> Result<ChatResponse> {
    let url = format!("{}/chat/completions", base_url.trim_end_matches('/'));

    let mut body = serde_json::json!({
        "model": model,
        "messages": messages,
        "max_tokens": max_tokens,
        "temperature": 0.7,
        "stream": true,
        "stream_options": { "include_usage": true },
    });

    if let Some(tools) = tools {
        body["tools"] = tools.clone();
    }

    let mut req = ureq::post(&url).set("Content-Type", "application/json");
    if let Some(key) = api_key {
        req = req.set("Authorization", &format!("Bearer {key}"));
    }

    let resp = match req.send_string(&body.to_string()) {
        Ok(r) => r,
        Err(ureq::Error::Status(code, response)) => {
            let body_text: String = response.into_string().unwrap_or_default().chars().take(300).collect();
            return Err(anyhow::anyhow!(
                "API stream request failed: {url}: status code {code} — {body_text}"
            ));
        }
        Err(e) => {
            return Err(anyhow::anyhow!("API stream request failed: {url}: {e}"));
        }
    };

    let reader = BufReader::new(resp.into_reader());

    let mut full_content = String::new();
    let mut started_printing = false;
    let mut usage: Option<TokenUsage> = None;

    // Streaming markdown state
    let mut line_buffer = String::new();
    let mut in_code_block = false;

    // Tool call accumulators: index -> (id, name, arguments_buffer)
    let mut tool_acc: Vec<(String, String, String)> = Vec::new();

    for line_result in reader.lines() {
        let line = match line_result {
            Ok(l) => l,
            Err(_) => break,
        };

        // SSE format: empty lines are separators, "data: " prefix carries payload
        if line.is_empty() || !line.starts_with("data: ") {
            continue;
        }

        let data = &line[6..]; // strip "data: "

        // End of stream
        if data == "[DONE]" {
            break;
        }

        let chunk: serde_json::Value = match serde_json::from_str(data) {
            Ok(v) => v,
            Err(_) => continue,
        };

        let delta = match chunk.pointer("/choices/0/delta") {
            Some(d) => d,
            None => {
                // Final chunk may have usage but no delta
                if let Some(u) = chunk.get("usage") {
                    usage = extract_usage_from_obj(u);
                }
                continue;
            }
        };

        // Stream text content with line-buffered markdown rendering
        if let Some(token) = delta.get("content").and_then(|v| v.as_str()) {
            if !token.is_empty() {
                if !started_printing {
                    print!("\n  {}{}\u{2726} ClifCode{}  ", ui::BOLD, ui::BRIGHT_MAGENTA, ui::RESET);
                    started_printing = true;
                }
                full_content.push_str(token);
                line_buffer.push_str(token);

                // Process completed lines
                while let Some(nl_pos) = line_buffer.find('\n') {
                    let completed_line: String = line_buffer[..nl_pos].to_string();
                    line_buffer = line_buffer[nl_pos + 1..].to_string();

                    // Track code block state
                    if completed_line.trim_start().starts_with("```") {
                        in_code_block = !in_code_block;
                    }

                    let rendered = ui::render_streaming_line(&completed_line, in_code_block && !completed_line.trim_start().starts_with("```"));
                    println!("{rendered}");
                }
            }
        }

        // Extract usage from final chunk (OpenAI/OpenRouter include it)
        if let Some(u) = chunk.get("usage") {
            usage = extract_usage_from_obj(u);
        }

        // Accumulate tool call deltas
        if let Some(tc_array) = delta.get("tool_calls").and_then(|v| v.as_array()) {
            for tc in tc_array {
                let idx = tc.get("index").and_then(|v| v.as_u64()).unwrap_or(0) as usize;

                // Grow accumulator if needed
                while tool_acc.len() <= idx {
                    tool_acc.push((String::new(), String::new(), String::new()));
                }

                // Capture tool call id (only sent in first delta for each tool call)
                if let Some(id) = tc.get("id").and_then(|v| v.as_str()) {
                    if !id.is_empty() {
                        tool_acc[idx].0 = id.to_string();
                    }
                }

                // Capture function name (only sent in first delta)
                if let Some(name) = tc.pointer("/function/name").and_then(|v| v.as_str()) {
                    if !name.is_empty() {
                        tool_acc[idx].1 = name.to_string();
                    }
                }

                // Accumulate function arguments (streamed across multiple deltas)
                if let Some(args) = tc.pointer("/function/arguments").and_then(|v| v.as_str()) {
                    tool_acc[idx].2.push_str(args);
                }
            }
        }
    }

    // Flush remaining line buffer
    if !line_buffer.is_empty() {
        if !started_printing {
            print!("\n  {}{}\u{2726} ClifCode{}  ", ui::BOLD, ui::BRIGHT_MAGENTA, ui::RESET);
            started_printing = true;
        }
        if line_buffer.trim_start().starts_with("```") {
            in_code_block = !in_code_block;
        }
        let rendered = ui::render_streaming_line(&line_buffer, in_code_block && !line_buffer.trim_start().starts_with("```"));
        println!("{rendered}");
    }

    // Finish the streamed output
    if started_printing {
        println!();
    }

    // Build tool calls from accumulated deltas
    let tool_calls: Vec<ApiToolCall> = tool_acc
        .into_iter()
        .filter(|(_, name, _)| !name.is_empty())
        .map(|(id, name, arguments)| ApiToolCall { id, name, arguments })
        .collect();

    // Build raw_message for conversation history
    let raw_message = if tool_calls.is_empty() {
        serde_json::json!({"role": "assistant", "content": full_content})
    } else {
        let tc_json: Vec<serde_json::Value> = tool_calls
            .iter()
            .map(|tc| {
                serde_json::json!({
                    "id": tc.id,
                    "type": "function",
                    "function": {
                        "name": tc.name,
                        "arguments": tc.arguments
                    }
                })
            })
            .collect();

        if full_content.is_empty() {
            serde_json::json!({
                "role": "assistant",
                "content": null,
                "tool_calls": tc_json
            })
        } else {
            serde_json::json!({
                "role": "assistant",
                "content": full_content,
                "tool_calls": tc_json
            })
        }
    };

    Ok(ChatResponse {
        content: full_content,
        tool_calls,
        raw_message,
        streamed: started_printing,
        usage,
    })
}

// ---------------------------------------------------------------------------
// Token usage extraction
// ---------------------------------------------------------------------------

fn extract_usage(resp: &serde_json::Value) -> Option<TokenUsage> {
    resp.get("usage").and_then(extract_usage_from_obj)
}

fn extract_usage_from_obj(u: &serde_json::Value) -> Option<TokenUsage> {
    let prompt = u.get("prompt_tokens").and_then(|v| v.as_u64())? as usize;
    let completion = u.get("completion_tokens").and_then(|v| v.as_u64())? as usize;
    Some(TokenUsage { prompt_tokens: prompt, completion_tokens: completion })
}

/// Quick check if Ollama is running locally (2s timeout to avoid blocking startup)
pub fn detect_ollama() -> bool {
    let agent = ureq::AgentBuilder::new()
        .timeout(std::time::Duration::from_secs(2))
        .build();
    agent.get("http://localhost:11434/api/tags").call().is_ok()
}