matrixcode-core 0.3.7

MatrixCode Agent Core - Pure logic, no UI
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
use anyhow::Result;
use async_trait::async_trait;
use futures_util::StreamExt;
use log::debug;
use serde_json::{Value, json};
use tokio::sync::mpsc;

use crate::tools::ToolDefinition;

use super::{
    ChatRequest, ChatResponse, ContentBlock, Message, MessageContent, Provider, Role, StopReason,
    StreamEvent, Usage,
};

pub struct AnthropicProvider {
    api_key: String,
    model: String,
    base_url: String,
    client: reqwest::Client,
    /// Whether this is an Aliyun DashScope endpoint (requires special headers).
    is_dashscope: bool,
}

impl AnthropicProvider {
    pub fn new(api_key: String, model: String, base_url: String) -> Self {
        let is_dashscope = base_url.contains("dashscope.aliyuncs.com");
        Self {
            api_key,
            model,
            base_url,
            client: reqwest::Client::new(),
            is_dashscope,
        }
    }

    fn convert_messages(&self, messages: &[Message]) -> Vec<Value> {
        messages
            .iter()
            .filter(|m| m.role != Role::System)
            .map(|m| {
                let role = match m.role {
                    Role::User | Role::Tool => "user",
                    Role::Assistant => "assistant",
                    Role::System => unreachable!(),
                };

                let content = match &m.content {
                    MessageContent::Text(text) => json!(text),
                    MessageContent::Blocks(blocks) => {
                        let converted: Vec<Value> = blocks
                            .iter()
                            .map(|b| match b {
                                ContentBlock::Text { text } => json!({"type": "text", "text": text}),
                                ContentBlock::ToolUse { id, name, input } => {
                                    json!({"type": "tool_use", "id": id, "name": name, "input": input})
                                }
                                ContentBlock::ToolResult { tool_use_id, content } => {
                                    json!({"type": "tool_result", "tool_use_id": tool_use_id, "content": content})
                                }
                                ContentBlock::Thinking { thinking, signature } => {
                                    let mut obj = json!({"type": "thinking", "thinking": thinking});
                                    if let Some(sig) = signature {
                                        obj["signature"] = json!(sig);
                                    }
                                    obj
                                }
                                ContentBlock::ServerToolUse { id, name, input } => {
                                    json!({"type": "server_tool_use", "id": id, "name": name, "input": input})
                                }
                                ContentBlock::WebSearchResult { tool_use_id, content } => {
                                    json!({"type": "web_search_tool_result", "tool_use_id": tool_use_id, "content": content})
                                }
                            })
                            .collect();
                        json!(converted)
                    }
                };

                json!({"role": role, "content": content})
            })
            .collect()
    }

    /// Convert tools with caching control for Anthropic prompt caching.
    fn convert_tools_with_caching(&self, tools: &[ToolDefinition], enable_caching: bool) -> Vec<Value> {
        let mut converted: Vec<Value> = tools
            .iter()
            .map(|t| {
                json!({
                    "name": t.name,
                    "description": t.description,
                    "input_schema": t.parameters,
                })
            })
            .collect();
        
        // Add cache_control to the last tool definition for tools caching
        if enable_caching && !converted.is_empty() {
            let last_idx = converted.len() - 1;
            if let Some(obj) = converted[last_idx].as_object_mut() {
                obj.insert("cache_control".to_string(), json!({"type": "ephemeral"}));
            }
        }
        
        converted
    }

    /// Build the base JSON body shared by streaming and non-streaming requests.
    fn build_body(&self, request: &ChatRequest) -> Value {
        let mut body = json!({
            "model": self.model,
            "max_tokens": request.max_tokens,
            "messages": self.convert_messages(&request.messages),
        });

        // Add prompt caching for system prompt (Anthropic-specific)
        if request.enable_caching && !self.is_dashscope {
            if let Some(system) = &request.system {
                // System prompt caching: add cache_control to enable caching
                body["system"] = json!([
                    {
                        "type": "text",
                        "text": system,
                        "cache_control": {"type": "ephemeral"}
                    }
                ]);
            }
        } else if let Some(system) = &request.system {
            body["system"] = json!(system);
        }

        if !request.tools.is_empty() {
            let tools = self.convert_tools_with_caching(&request.tools, request.enable_caching && !self.is_dashscope);
            body["tools"] = json!(tools);
        }

        if !request.server_tools.is_empty() {
            body["tools"] = json!(body["tools"]
                .as_array()
                .map(|t| {
                    let mut tools = t.clone();
                    for st in &request.server_tools {
                        tools.push(serde_json::to_value(st).unwrap_or_default());
                    }
                    tools
                })
                .unwrap_or_else(|| request.server_tools.iter().map(|st| serde_json::to_value(st).unwrap_or_default()).collect()));
        }

        // DashScope does not support Anthropic's extended thinking feature
        if request.think && !self.is_dashscope {
            body["thinking"] = thinking_config(&self.model);
        }

        body
    }
}

/// Models that require the new `adaptive` thinking mode instead of the
/// legacy `enabled`+`budget_tokens` form. Conservative allow-list: if we
/// don't recognize the name we default to the legacy shape (which older
/// models and most third-party gateways understand).
fn thinking_config(model: &str) -> Value {
    let adaptive = model.contains("opus-4-7") || model.contains("opus-4.7");
    if adaptive {
        json!({"type": "adaptive"})
    } else {
        json!({"type": "enabled", "budget_tokens": 2048})
    }
}

#[async_trait]
impl Provider for AnthropicProvider {
    fn context_size(&self) -> Option<u32> {
        context_window_for(&self.model)
    }

    fn clone_box(&self) -> Box<dyn Provider> {
        Box::new(Self {
            api_key: self.api_key.clone(),
            model: self.model.clone(),
            base_url: self.base_url.clone(),
            client: reqwest::Client::new(),
            is_dashscope: self.is_dashscope,
        })
    }

    async fn chat(&self, request: ChatRequest) -> Result<ChatResponse> {
        let body = self.build_body(&request);

        let url = format!("{}/v1/messages", self.base_url);
        let mut req = self
            .client
            .post(&url)
            .header("User-Agent", "curl/8.0")
            .json(&body);

        // DashScope uses Bearer auth
        if self.is_dashscope {
            req = req.header("Authorization", format!("Bearer {}", self.api_key));
        } else {
            req = req.header("x-api-key", &self.api_key)
                .header("anthropic-version", "2023-06-01")
                .header("anthropic-beta", "prompt-caching-2024-07-31");
        }

        let response = req.send().await?;

        let status = response.status();
        let response_body: Value = response.json().await?;

        if !status.is_success() {
            let err_msg = response_body["error"]["message"]
                .as_str()
                .unwrap_or("unknown error");
            anyhow::bail!("Anthropic API error ({}): {}", status, err_msg);
        }

        let stop_reason = match response_body["stop_reason"].as_str() {
            Some("tool_use") => StopReason::ToolUse,
            Some("max_tokens") => StopReason::MaxTokens,
            _ => StopReason::EndTurn,
        };

        let content = response_body["content"]
            .as_array()
            .unwrap_or(&vec![])
            .iter()
            .filter_map(|block| match block["type"].as_str()? {
                "text" => Some(ContentBlock::Text {
                    text: block["text"].as_str()?.to_string(),
                }),
                "tool_use" => Some(ContentBlock::ToolUse {
                    id: block["id"].as_str()?.to_string(),
                    name: block["name"].as_str()?.to_string(),
                    input: block["input"].clone(),
                }),
                "thinking" => Some(ContentBlock::Thinking {
                    thinking: block["thinking"].as_str()?.to_string(),
                    signature: block["signature"].as_str().map(String::from),
                }),
                "server_tool_use" => Some(ContentBlock::ServerToolUse {
                    id: block["id"].as_str()?.to_string(),
                    name: block["name"].as_str()?.to_string(),
                    input: block["input"].clone(),
                }),
                "web_search_tool_result" => {
                    let tool_use_id = block["tool_use_id"].as_str()?.to_string();
                    let content = parse_web_search_content(&block["content"]);
                    Some(ContentBlock::WebSearchResult {
                        tool_use_id,
                        content,
                    })
                }
                _ => None,
            })
            .collect();

        Ok(ChatResponse {
            content,
            stop_reason,
            usage: parse_usage(&response_body["usage"]),
        })
    }

    async fn chat_stream(&self, request: ChatRequest) -> Result<mpsc::Receiver<StreamEvent>> {
        let mut body = self.build_body(&request);
        body["stream"] = json!(true);

        let url = format!("{}/v1/messages", self.base_url);
        let mut req = self
            .client
            .post(&url)
            .header("User-Agent", "curl/8.0")
            .json(&body);

        // DashScope uses Bearer auth and requires SSE header for streaming
        if self.is_dashscope {
            req = req
                .header("Authorization", format!("Bearer {}", self.api_key))
                .header("X-DashScope-SSE", "enable");
        } else {
            req = req.header("x-api-key", &self.api_key)
                .header("anthropic-version", "2023-06-01")
                .header("anthropic-beta", "prompt-caching-2024-07-31");
        }

        let response = req.send().await?;

        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await.unwrap_or_default();
            anyhow::bail!("Anthropic API error ({}): {}", status, text);
        }

        let (tx, rx) = mpsc::channel(64);
        tokio::spawn(async move {
            let mut stream = response.bytes_stream();
            let mut buffer = String::new();
            let mut sent_first_byte = false;

            // In-flight block assembly: index → partial data
            let mut blocks: Vec<AssembledBlock> = Vec::new();
            let mut stop_reason = StopReason::EndTurn;
            let mut usage = Usage::default();

            while let Some(chunk) = stream.next().await {
                let chunk = match chunk {
                    Ok(c) => c,
                    Err(e) => {
                        let _ = tx
                            .send(StreamEvent::Error(format!("stream read error: {}", e)))
                            .await;
                        return;
                    }
                };

                if !sent_first_byte {
                    sent_first_byte = true;
                    let _ = tx.send(StreamEvent::FirstByte).await;
                }

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

                while let Some(frame) = take_next_sse_frame(&mut buffer) {
                    if handle_sse_frame(
                        &frame,
                        &mut blocks,
                        &mut stop_reason,
                        &mut usage,
                        &tx,
                    )
                    .await
                    {
                        return;
                    }
                }
            }

            if let Some(frame) = take_trailing_sse_frame(&mut buffer)
                && handle_sse_frame(&frame, &mut blocks, &mut stop_reason, &mut usage, &tx).await {
                    return;
                }

            if sent_first_byte {
                debug!(
                    "stream ended without explicit message_stop; finalizing best-effort"
                );
                let _ = tx
                    .send(StreamEvent::Done(finalize_incomplete_stream(
                        std::mem::take(&mut blocks),
                        stop_reason,
                        usage,
                    )))
                    .await;
            } else {
                let _ = tx
                    .send(StreamEvent::Error(
                        "stream ended before any events were received".to_string(),
                    ))
                    .await;
            }
        });

        Ok(rx)
    }
}

fn take_next_sse_frame(buffer: &mut String) -> Option<String> {
    let lf = buffer.find("\n\n").map(|pos| (pos, 2usize));
    let crlf = buffer.find("\r\n\r\n").map(|pos| (pos, 4usize));
    let (pos, delim_len) = match (lf, crlf) {
        (Some(a), Some(b)) => {
            if a.0 <= b.0 {
                a
            } else {
                b
            }
        }
        (Some(a), None) => a,
        (None, Some(b)) => b,
        (None, None) => return None,
    };

    let frame = buffer[..pos].to_string();
    buffer.drain(..pos + delim_len);
    Some(frame)
}

fn take_trailing_sse_frame(buffer: &mut String) -> Option<String> {
    let frame = buffer.trim().trim_end_matches('\r').to_string();
    buffer.clear();
    if frame.is_empty() {
        None
    } else {
        Some(frame)
    }
}

fn extract_sse_data_line(frame: &str) -> Option<String> {
    for line in frame.lines() {
        let line = line.trim_end_matches('\r');
        // Support both "data: " (Anthropic) and "data:" (DashScope)
        if let Some(rest) = line.strip_prefix("data: ") {
            return Some(rest.to_string());
        }
        if let Some(rest) = line.strip_prefix("data:") {
            return Some(rest.to_string());
        }
    }
    None
}

async fn handle_sse_frame(
    frame: &str,
    blocks: &mut Vec<AssembledBlock>,
    stop_reason: &mut StopReason,
    usage: &mut Usage,
    tx: &mpsc::Sender<StreamEvent>,
) -> bool {
    let Some(data_line) = extract_sse_data_line(frame) else {
        return false;
    };

    let evt: Value = match serde_json::from_str(&data_line) {
        Ok(v) => v,
        Err(_) => return false,
    };

    handle_sse_event(evt, blocks, stop_reason, usage, tx).await
}

async fn handle_sse_event(
    evt: Value,
    blocks: &mut Vec<AssembledBlock>,
    stop_reason: &mut StopReason,
    usage: &mut Usage,
    tx: &mpsc::Sender<StreamEvent>,
) -> bool {
    match evt["type"].as_str().unwrap_or("") {
        "message_start" => {
            // Initial usage payload — `input_tokens` is final
            // (they don't grow during streaming) but
            // `output_tokens` starts near 0 and is updated by
            // subsequent `message_delta` events.
            *usage = merge_usage(usage.clone(), &evt["message"]["usage"]);
            debug!(
                "message_start: usage_json={}",
                serde_json::to_string(&evt["message"]["usage"]).unwrap_or_default()
            );
            debug!(
                "message_start parsed: input={}, output={}, cache_read={}, cache_created={}",
                usage.input_tokens, usage.output_tokens,
                usage.cache_read_input_tokens, usage.cache_creation_input_tokens
            );
        }
        "content_block_start" => {
            let idx = evt["index"].as_u64().unwrap_or(0) as usize;
            let block = &evt["content_block"];
            let kind = block["type"].as_str().unwrap_or("");
            while blocks.len() <= idx {
                blocks.push(AssembledBlock::default());
            }
            match kind {
                "text" => {
                    blocks[idx] = AssembledBlock::Text(String::new());
                }
                "thinking" => {
                    blocks[idx] = AssembledBlock::Thinking {
                        text: String::new(),
                        signature: None,
                    };
                }
                "tool_use" => {
                    let id = block["id"].as_str().unwrap_or("").to_string();
                    let name = block["name"].as_str().unwrap_or("").to_string();
                    blocks[idx] = AssembledBlock::ToolUse {
                        id: id.clone(),
                        name: name.clone(),
                        input_json: String::new(),
                    };
                    let _ = tx.send(StreamEvent::ToolUseStart { id, name }).await;
                }
                "server_tool_use" => {
                    let id = block["id"].as_str().unwrap_or("").to_string();
                    let name = block["name"].as_str().unwrap_or("").to_string();
                    blocks[idx] = AssembledBlock::ServerToolUse {
                        id: id.clone(),
                        name: name.clone(),
                        input_json: String::new(),
                    };
                    let _ = tx.send(StreamEvent::ToolUseStart { id, name }).await;
                }
                "web_search_tool_result" => {
                    let tool_use_id = block["tool_use_id"].as_str().unwrap_or("").to_string();
                    blocks[idx] = AssembledBlock::WebSearchResult {
                        tool_use_id,
                        content_json: String::new(),
                    };
                }
                _ => {}
            }
        }
        "content_block_delta" => {
            let idx = evt["index"].as_u64().unwrap_or(0) as usize;
            let delta = &evt["delta"];
            let dt = delta["type"].as_str().unwrap_or("");
            if idx >= blocks.len() {
                return false;
            }
            match (dt, &mut blocks[idx]) {
                ("text_delta", AssembledBlock::Text(buf)) => {
                    if let Some(t) = delta["text"].as_str() {
                        buf.push_str(t);
                        let _ = tx.send(StreamEvent::TextDelta(t.to_string())).await;
                    }
                }
                ("thinking_delta", AssembledBlock::Thinking { text, .. }) => {
                    if let Some(t) = delta["thinking"].as_str() {
                        text.push_str(t);
                        let _ = tx.send(StreamEvent::ThinkingDelta(t.to_string())).await;
                    }
                }
                ("signature_delta", AssembledBlock::Thinking { signature, .. }) => {
                    if let Some(s) = delta["signature"].as_str() {
                        signature.get_or_insert_with(String::new).push_str(s);
                    }
                }
                ("input_json_delta", AssembledBlock::ToolUse { input_json, .. }) => {
                    if let Some(p) = delta["partial_json"].as_str() {
                        input_json.push_str(p);
                        let _ = tx
                            .send(StreamEvent::ToolInputDelta {
                                bytes_so_far: input_json.len(),
                            })
                            .await;
                    }
                }
                ("input_json_delta", AssembledBlock::ServerToolUse { input_json, .. }) => {
                    if let Some(p) = delta["partial_json"].as_str() {
                        input_json.push_str(p);
                        let _ = tx
                            .send(StreamEvent::ToolInputDelta {
                                bytes_so_far: input_json.len(),
                            })
                            .await;
                    }
                }
                _ => {}
            }
        }
        "message_delta" => {
            if let Some(sr) = evt["delta"]["stop_reason"].as_str() {
                *stop_reason = match sr {
                    "tool_use" => StopReason::ToolUse,
                    "max_tokens" => StopReason::MaxTokens,
                    _ => StopReason::EndTurn,
                };
            }
            // `usage` on message_delta reflects cumulative
            // counts for the current message — most notably
            // the final `output_tokens`.
            *usage = merge_usage(usage.clone(), &evt["usage"]);
            debug!(
                "message_delta: input={}, output={}, cache_read={}, cache_created={}",
                usage.input_tokens, usage.output_tokens,
                usage.cache_read_input_tokens, usage.cache_creation_input_tokens
            );
        }
        "message_stop" => {
            debug!(
                "Message completed: stop_reason={}, usage={}",
                match *stop_reason {
                    StopReason::EndTurn => "end_turn",
                    StopReason::ToolUse => "tool_use",
                    StopReason::MaxTokens => "max_tokens",
                },
                usage.output_tokens
            );
            debug!(
                "message_stop final usage: cache_read={}, cache_created={}",
                usage.cache_read_input_tokens, usage.cache_creation_input_tokens
            );
            let _ = tx
                .send(StreamEvent::Done(finalize_incomplete_stream(
                    std::mem::take(blocks),
                    stop_reason.clone(),
                    usage.clone(),
                )))
                .await;
            return true;
        }
        "error" => {
            let msg = evt["error"]["message"]
                .as_str()
                .unwrap_or("unknown stream error")
                .to_string();
            let _ = tx.send(StreamEvent::Error(msg)).await;
            return true;
        }
        _ => {}
    }

    false
}

fn build_chat_response(
    blocks: Vec<AssembledBlock>,
    stop_reason: StopReason,
    usage: Usage,
) -> ChatResponse {
    let content: Vec<ContentBlock> = blocks.into_iter().filter_map(|b| b.finish()).collect();
    ChatResponse {
        content,
        stop_reason,
        usage,
    }
}

fn finalize_incomplete_stream(
    blocks: Vec<AssembledBlock>,
    stop_reason: StopReason,
    usage: Usage,
) -> ChatResponse {
    build_chat_response(blocks, stop_reason, usage)
}

#[derive(Default)]
enum AssembledBlock {
    #[default]
    Empty,
    Text(String),
    Thinking {
        text: String,
        signature: Option<String>,
    },
    ToolUse {
        id: String,
        name: String,
        input_json: String,
    },
    ServerToolUse {
        id: String,
        name: String,
        input_json: String,
    },
    WebSearchResult {
        tool_use_id: String,
        content_json: String,
    },
}

impl AssembledBlock {
    fn finish(self) -> Option<ContentBlock> {
        match self {
            AssembledBlock::Empty => None,
            AssembledBlock::Text(text) => Some(ContentBlock::Text { text }),
            AssembledBlock::Thinking { text, signature } => Some(ContentBlock::Thinking {
                thinking: text,
                signature,
            }),
            AssembledBlock::ToolUse {
                id,
                name,
                input_json,
            } => {
                let input: Value = if input_json.is_empty() {
                    json!({})
                } else {
                    serde_json::from_str(&input_json).unwrap_or(json!({}))
                };
                Some(ContentBlock::ToolUse { id, name, input })
            }
            AssembledBlock::ServerToolUse {
                id,
                name,
                input_json,
            } => {
                let input: Value = if input_json.is_empty() {
                    json!({})
                } else {
                    serde_json::from_str(&input_json).unwrap_or(json!({}))
                };
                Some(ContentBlock::ServerToolUse { id, name, input })
            }
            AssembledBlock::WebSearchResult {
                tool_use_id,
                content_json,
            } => {
                let content: Value = if content_json.is_empty() {
                    json!({"results": []})
                } else {
                    serde_json::from_str(&content_json).unwrap_or(json!({"results": []}))
                };
                Some(ContentBlock::WebSearchResult {
                    tool_use_id,
                    content: parse_web_search_content(&content),
                })
            }
        }
    }
}

/// Parse the provider's `usage` blob (non-streaming response) into our
/// internal `Usage` struct. Missing fields default to 0.
fn parse_usage(u: &Value) -> Usage {
    Usage {
        input_tokens: u["input_tokens"].as_u64().unwrap_or(0) as u32,
        output_tokens: u["output_tokens"].as_u64().unwrap_or(0) as u32,
        cache_creation_input_tokens: u["cache_creation_input_tokens"].as_u64().unwrap_or(0) as u32,
        cache_read_input_tokens: u["cache_read_input_tokens"].as_u64().unwrap_or(0) as u32,
    }
}

/// Merge a fresh usage payload into the accumulated one. Non-zero new values
/// override prior ones — this matches the streaming protocol where
/// `message_start` gives input counts and `message_delta` updates output.
fn merge_usage(mut acc: Usage, u: &Value) -> Usage {
    let fresh = parse_usage(u);
    if fresh.input_tokens > 0 {
        acc.input_tokens = fresh.input_tokens;
    }
    if fresh.output_tokens > 0 {
        acc.output_tokens = fresh.output_tokens;
    }
    if fresh.cache_creation_input_tokens > 0 {
        acc.cache_creation_input_tokens = fresh.cache_creation_input_tokens;
    }
    if fresh.cache_read_input_tokens > 0 {
        acc.cache_read_input_tokens = fresh.cache_read_input_tokens;
    }
    acc
}

/// Parse web search content from the API response.
fn parse_web_search_content(value: &serde_json::Value) -> crate::providers::WebSearchContent {
    let results = value["results"]
        .as_array()
        .map(|arr| {
            arr.iter()
                .filter_map(|item| {
                    Some(crate::providers::WebSearchResultItem {
                        title: item["title"].as_str().map(String::from),
                        url: item["url"].as_str()?.to_string(),
                        encrypted_content: item["encrypted_content"].as_str().map(String::from),
                        snippet: item["snippet"].as_str().map(String::from),
                    })
                })
                .collect()
        })
        .unwrap_or_default();

    crate::providers::WebSearchContent { results }
}

/// Best-effort mapping from an Anthropic-ish model name to its context
/// window size. Honours the `CONTEXT_SIZE` env variable first so users
/// can pin a value when running through a proxy gateway (e.g. Kimi via
/// an Anthropic-shaped endpoint). Returns `None` only when we truly
/// cannot infer; callers treat that as "hide the fullness bar".
fn context_window_for(model: &str) -> Option<u32> {
    if let Ok(raw) = std::env::var("CONTEXT_SIZE")
        && let Ok(n) = raw.trim().parse::<u32>()
            && n > 0 {
                return Some(n);
            }
    let m = model.to_ascii_lowercase();
    
    // Claude Opus 4.7 and the 1M-context variants expose a 1M window.
    if m.contains("[1m]") || m.contains("opus-4-7") || m.contains("opus-4.7") {
        return Some(1_000_000);
    }
    // Claude 3.5/4 series: 200K standard window
    if m.contains("claude-3") || m.contains("claude-4") || m.contains("claude-opus") || m.contains("claude-sonnet") {
        return Some(200_000);
    }
    // Claude 2: 100K
    if m.contains("claude-2") {
        return Some(100_000);
    }
    // Claude Instant: 100K
    if m.contains("claude-instant") {
        return Some(100_000);
    }
    // Kimi K2 / K2.5 ship with a 128K window on the public endpoint.
    if m.contains("kimi") {
        return Some(128_000);
    }
    // DeepSeek via Anthropic endpoint
    if m.contains("deepseek") {
        return Some(128_000);
    }
    // GLM models (Zhipu AI) via Anthropic-compatible endpoints
    // GLM-4 has 128K, GLM-5 typically has 128K context
    if m.contains("glm") {
        return Some(128_000);
    }
    // Qwen models via Anthropic-compatible endpoints (DashScope)
    if m.contains("qwen") {
        if m.contains("qwen-max") || m.contains("qwen2.5") {
            return Some(128_000);
        }
        return Some(32_000);
    }
    // Default fallback for unknown models: assume 128K (reasonable for modern models)
    // This ensures context usage is always displayed
    Some(128_000)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn take_next_sse_frame_supports_crlf_delimiter() {
        let mut buffer = concat!(
            "event: message_start\r\n",
            "data: {\"type\":\"message_start\"}\r\n\r\n",
            "data: {\"type\":\"message_stop\"}\r\n\r\n"
        )
        .to_string();

        let first = take_next_sse_frame(&mut buffer).expect("first frame");
        assert!(first.contains("message_start"));

        let second = take_next_sse_frame(&mut buffer).expect("second frame");
        assert!(second.contains("message_stop"));
        assert!(buffer.is_empty());
    }

    #[test]
    fn take_trailing_sse_frame_returns_unterminated_event() {
        let mut buffer = "data: {\"type\":\"message_stop\"}\r\n".to_string();
        let frame = take_trailing_sse_frame(&mut buffer).expect("trailing frame");
        assert_eq!(frame, "data: {\"type\":\"message_stop\"}");
        assert!(buffer.is_empty());
    }

    #[test]
    fn extract_sse_data_line_supports_optional_space() {
        assert_eq!(
            extract_sse_data_line("event: x\r\ndata: {\"k\":1}\r"),
            Some("{\"k\":1}".to_string())
        );
        assert_eq!(
            extract_sse_data_line("event: x\r\ndata:{\"k\":2}\r"),
            Some("{\"k\":2}".to_string())
        );
    }

    #[test]
    fn finalize_incomplete_stream_keeps_accumulated_content() {
        let response = finalize_incomplete_stream(
            vec![AssembledBlock::Text("partial reply".to_string())],
            StopReason::EndTurn,
            Usage::default(),
        );

        assert_eq!(response.stop_reason, StopReason::EndTurn);
        assert_eq!(response.content.len(), 1);
        match &response.content[0] {
            ContentBlock::Text { text } => assert_eq!(text, "partial reply"),
            other => panic!("unexpected block: {other:?}"),
        }
    }
}