matrixcode-core 0.4.43

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
//! Agent streaming implementation.

use anyhow::Result;
use tokio::time::{Duration, sleep, Instant};

use crate::constants::STREAM_DELTA_BUFFER_SIZE;
use crate::event::AgentEvent;
use crate::providers::{ChatRequest, ChatResponse, ContentBlock, StopReason, StreamEvent, Usage};

use super::types::Agent;

/// Buffered delta for efficient event emission
#[derive(Debug)]
struct BufferedDelta {
    text: String,
    thinking: String,
    last_emit: Instant,
}

impl Default for BufferedDelta {
    fn default() -> Self {
        Self::new()
    }
}

impl BufferedDelta {
    fn new() -> Self {
        Self {
            text: String::new(),
            thinking: String::new(),
            last_emit: Instant::now(),
        }
    }

    /// Add text delta to buffer, returns true if should flush
    fn add_text(&mut self, delta: &str) -> bool {
        self.text.push_str(delta);
        self.should_flush_text()
    }

    /// Add thinking delta to buffer, returns true if should flush
    fn add_thinking(&mut self, delta: &str) -> bool {
        self.thinking.push_str(delta);
        self.should_flush_thinking()
    }

    fn should_flush_text(&self) -> bool {
        self.text.len() >= STREAM_DELTA_BUFFER_SIZE
    }

    fn should_flush_thinking(&self) -> bool {
        self.thinking.len() >= STREAM_DELTA_BUFFER_SIZE
    }

    /// Check if buffer needs flush due to time interval
    fn should_flush_by_time(&self, interval_ms: u64) -> bool {
        self.last_emit.elapsed().as_millis() >= interval_ms as u128
            && (!self.text.is_empty() || !self.thinking.is_empty())
    }

    /// Flush text buffer, returns content if non-empty
    fn flush_text(&mut self) -> Option<String> {
        if self.text.is_empty() {
            return None;
        }
        let content = self.text.clone();
        self.text.clear();
        self.last_emit = Instant::now();
        Some(content)
    }

    /// Flush thinking buffer, returns content if non-empty
    fn flush_thinking(&mut self) -> Option<String> {
        if self.thinking.is_empty() {
            return None;
        }
        let content = self.thinking.clone();
        self.thinking.clear();
        self.last_emit = Instant::now();
        Some(content)
    }

    /// Flush all buffers
    fn flush_all(&mut self) -> (Option<String>, Option<String>) {
        let text = self.flush_text();
        let thinking = self.flush_thinking();
        (text, thinking)
    }
}

/// Wait for cancellation signal, checking periodically.
async fn wait_for_cancel_stream(token: &crate::cancel::CancellationToken) {
    while !token.is_cancelled() {
        sleep(Duration::from_millis(100)).await;
    }
}

impl Agent {
    /// Drain any pending input messages from the channel.
    /// Called during streaming to collect real-time appended messages.
    pub(crate) fn drain_pending_inputs(&mut self) {
        let inputs = self.session.drain_pending_inputs();
        for msg in inputs {
            log::info!(
                "Agent received pending input: {}",
                msg.chars().take(50).collect::<String>()
            );
            self.state.add_pending_input(msg);
        }
    }

    /// Check if there are pending inputs waiting to be processed.
    pub fn has_pending_inputs(&self) -> bool {
        self.state.has_pending_inputs()
    }

    /// Get and clear all pending inputs.
    pub fn take_pending_inputs(&mut self) -> Vec<String> {
        self.state.take_pending_inputs()
    }

    /// Call provider with streaming and emit events in real-time.
    /// Also monitors pending_input_rx for real-time message appending.
    /// Uses buffered delta emission to reduce event frequency.
    pub(crate) async fn call_streaming(&mut self, request: &ChatRequest) -> Result<ChatResponse> {
        const MAX_RETRIES: u32 = 5;
        const RETRY_DELAY_MS: u64 = 1000;
        const FLUSH_INTERVAL_MS: u64 = crate::constants::STREAM_DELTA_FLUSH_INTERVAL_MS;

        let mut attempt = 0;

        loop {
            attempt += 1;
            log::info!(
                "Agent: API call attempt {} with {} messages",
                attempt,
                request.messages.len()
            );

            if self.session.is_cancelled() {
                return Err(anyhow::anyhow!("Operation cancelled"));
            }

            log::info!("Agent: calling provider.chat_stream");
            let rx_result = self.provider.chat_stream(request.clone()).await;
            log::info!("Agent: provider.chat_stream returned");

            match rx_result {
                Ok(mut rx) => {
                    let mut response_content: Vec<ContentBlock> = Vec::new();
                    let mut current_text = String::new();
                    let mut current_thinking = String::new();
                    let mut usage = Usage {
                        input_tokens: 0,
                        output_tokens: 0,
                        cache_creation_input_tokens: 0,
                        cache_read_input_tokens: 0,
                    };
                    let mut should_retry = false;

                    // Buffered delta for efficient emission
                    let mut buffer = BufferedDelta::new();
                    let mut thinking_started = false;
                    let mut text_started = false;

                    loop {
                        // Use biased select! to prioritize stream events over pending input checks
                        // This prevents losing stream events when sleep completes first
                        let event = if let Some(token) = self.session.cancel_token() {
                            tokio::select! {
                                biased;

                                // Primary: receive stream event (highest priority)
                                event = rx.recv() => event,

                                // Cancellation signal (second priority)
                                _ = wait_for_cancel_stream(token) => {
                                    // Flush any pending buffers before cancelling
                                    let (text, thinking) = buffer.flush_all();
                                    if let Some(t) = thinking {
                                        self.emit(AgentEvent::thinking_delta(&t, None))?;
                                    }
                                    if let Some(t) = text {
                                        self.emit(AgentEvent::text_delta(&t))?;
                                    }
                                    return Err(anyhow::anyhow!("Operation cancelled"));
                                }

                                // Check for pending inputs periodically (lowest priority)
                                // Also check for buffer flush by time interval
                                _ = sleep(Duration::from_millis(FLUSH_INTERVAL_MS)) => {
                                    self.drain_pending_inputs();
                                    // Flush buffers if interval elapsed
                                    if buffer.should_flush_by_time(FLUSH_INTERVAL_MS) {
                                        if let Some(t) = buffer.flush_thinking() {
                                            self.emit(AgentEvent::thinking_delta(&t, None))?;
                                        }
                                        if let Some(t) = buffer.flush_text() {
                                            self.emit(AgentEvent::text_delta(&t))?;
                                        }
                                    }
                                    continue;
                                }
                            }
                        } else {
                            // No cancellation token, but still check pending inputs
                            tokio::select! {
                                biased;

                                // Primary: receive stream event (highest priority)
                                event = rx.recv() => event,

                                // Check for pending inputs periodically (lower priority)
                                // Also check for buffer flush by time interval
                                _ = sleep(Duration::from_millis(FLUSH_INTERVAL_MS)) => {
                                    self.drain_pending_inputs();
                                    // Flush buffers if interval elapsed
                                    if buffer.should_flush_by_time(FLUSH_INTERVAL_MS) {
                                        if let Some(t) = buffer.flush_thinking() {
                                            self.emit(AgentEvent::thinking_delta(&t, None))?;
                                        }
                                        if let Some(t) = buffer.flush_text() {
                                            self.emit(AgentEvent::text_delta(&t))?;
                                        }
                                    }
                                    continue;
                                }
                            }
                        };

                        match event {
                            None => break,
                            Some(StreamEvent::FirstByte) => {}
                            Some(StreamEvent::ThinkingDelta(delta)) => {
                                // Check cancellation before emitting
                                if self.session.is_cancelled() {
                                    return Err(anyhow::anyhow!("Operation cancelled"));
                                }
                                if !thinking_started {
                                    self.emit(AgentEvent::thinking_start())?;
                                    thinking_started = true;
                                }
                                current_thinking.push_str(&delta);
                                // Buffer the delta and emit if threshold reached
                                if buffer.add_thinking(&delta) {
                                    if let Some(t) = buffer.flush_thinking() {
                                        self.emit(AgentEvent::thinking_delta(&t, None))?;
                                    }
                                }
                            }
                            Some(StreamEvent::TextDelta(delta)) => {
                                // Check cancellation before emitting
                                if self.session.is_cancelled() {
                                    return Err(anyhow::anyhow!("Operation cancelled"));
                                }
                                if !text_started {
                                    self.emit(AgentEvent::text_start())?;
                                    text_started = true;
                                }
                                current_text.push_str(&delta);
                                // Buffer the delta and emit if threshold reached
                                if buffer.add_text(&delta) {
                                    if let Some(t) = buffer.flush_text() {
                                        self.emit(AgentEvent::text_delta(&t))?;
                                    }
                                }
                            }
                            Some(StreamEvent::ToolUseStart { id, name }) => {
                                // Flush any pending buffers before tool use
                                if let Some(t) = buffer.flush_thinking() {
                                    self.emit(AgentEvent::thinking_delta(&t, None))?;
                                }
                                if let Some(t) = buffer.flush_text() {
                                    self.emit(AgentEvent::text_delta(&t))?;
                                }
                                // Emit events for UI but don't push content blocks
                                // Content will be added from Done event's resp.content
                                if !current_thinking.is_empty() {
                                    self.emit(AgentEvent::thinking_end())?;
                                    current_thinking.clear();
                                }
                                if !current_text.is_empty() {
                                    self.emit(AgentEvent::text_end())?;
                                    current_text.clear();
                                }
                                thinking_started = false;
                                text_started = false;
                                self.emit(AgentEvent::tool_use_start(&id, &name, None))?;
                            }
                            Some(StreamEvent::ToolInputDelta { bytes_so_far: _ }) => {}
                            Some(StreamEvent::ToolInputComplete { id, name, input }) => {
                                self.state.mark_tool_input_previewed(id.clone());
                                self.emit(AgentEvent::tool_use_start(&id, &name, Some(input)))?;
                            }
                            Some(StreamEvent::Usage { output_tokens }) => {
                                self.emit(AgentEvent::usage_with_cache(
                                    0,
                                    output_tokens as u64,
                                    0,
                                    0,
                                ))?;
                                usage.output_tokens = output_tokens;
                            }
                            Some(StreamEvent::Done(resp)) => {
                                // Check cancellation before processing final response
                                if self.session.is_cancelled() {
                                    return Err(anyhow::anyhow!("Operation cancelled"));
                                }

                                // Final drain of pending inputs before completing
                                self.drain_pending_inputs();

                                // Flush any remaining buffered deltas
                                if let Some(t) = buffer.flush_thinking() {
                                    self.emit(AgentEvent::thinking_delta(&t, None))?;
                                }
                                if let Some(t) = buffer.flush_text() {
                                    self.emit(AgentEvent::text_delta(&t))?;
                                }

                                // IMPORTANT: Add current_thinking/current_text to response_content FIRST
                                // before checking for duplicates from resp.content
                                // This ensures all streamed content is preserved
                                if !current_thinking.is_empty() {
                                    self.emit(AgentEvent::thinking_end())?;
                                    // Add to response_content with signature from resp if available
                                    let signature = resp.content.iter()
                                        .find_map(|b| {
                                            if let ContentBlock::Thinking { thinking, signature } = b {
                                                if thinking == &current_thinking {
                                                    signature.clone()
                                                } else {
                                                    None
                                                }
                                            } else {
                                                None
                                            }
                                        });
                                    response_content.push(ContentBlock::Thinking {
                                        thinking: current_thinking.clone(),
                                        signature,
                                    });
                                    current_thinking.clear();
                                }
                                if !current_text.is_empty() {
                                    self.emit(AgentEvent::text_end())?;
                                    // Add to response_content
                                    response_content.push(ContentBlock::Text {
                                        text: current_text.clone(),
                                    });
                                    current_text.clear();
                                }

                                // Then add any additional blocks from final response that are NOT duplicates
                                for block in &resp.content {
                                    // Smart deduplication: compare content, not entire block
                                    let is_duplicate = response_content.iter().any(|b| {
                                        match (b, block) {
                                            // For Thinking blocks, compare thinking content only (signature may differ)
                                            (
                                                ContentBlock::Thinking { thinking: t1, .. },
                                                ContentBlock::Thinking { thinking: t2, .. },
                                            ) => t1 == t2,
                                            // For Text blocks, compare text content
                                            (
                                                ContentBlock::Text { text: t1 },
                                                ContentBlock::Text { text: t2 },
                                            ) => t1 == t2,
                                            // For ToolUse, compare id
                                            (
                                                ContentBlock::ToolUse { id: id1, .. },
                                                ContentBlock::ToolUse { id: id2, .. },
                                            ) => id1 == id2,
                                            // For ToolResult, compare tool_use_id
                                            (
                                                ContentBlock::ToolResult {
                                                    tool_use_id: id1, ..
                                                },
                                                ContentBlock::ToolResult {
                                                    tool_use_id: id2, ..
                                                },
                                            ) => id1 == id2,
                                            // Default: exact comparison
                                            _ => b == block,
                                        }
                                    });
                                    if !is_duplicate {
                                        response_content.push(block.clone());
                                    }
                                }
                                usage = resp.usage;
                            }
                            Some(StreamEvent::Error(msg)) => {
                                if attempt < MAX_RETRIES {
                                    self.emit(AgentEvent::progress(
                                        format!(
                                            "⚠️ Stream error, retrying ({}/{}): {}",
                                            attempt, MAX_RETRIES, &msg
                                        ),
                                        None,
                                    ))?;
                                    let delay = RETRY_DELAY_MS * (1 << (attempt - 1));
                                    tokio::time::sleep(tokio::time::Duration::from_millis(delay))
                                        .await;
                                    should_retry = true;
                                    break;
                                } else {
                                    self.emit(AgentEvent::error(msg.clone(), None, None))?;
                                    return Err(anyhow::anyhow!(
                                        "Stream error after {} retries: {}",
                                        MAX_RETRIES,
                                        msg
                                    ));
                                }
                            }
                        }
                    }

                    if should_retry {
                        continue;
                    }

                    return Ok(ChatResponse {
                        content: response_content,
                        stop_reason: StopReason::EndTurn,
                        usage,
                    });
                }
                Err(e) => {
                    if attempt < MAX_RETRIES {
                        let error_msg = e.to_string();
                        self.emit(AgentEvent::progress(
                            format!(
                                "⚠️ API error, retrying ({}/{}): {}",
                                attempt, MAX_RETRIES, &error_msg
                            ),
                            None,
                        ))?;
                        let delay = RETRY_DELAY_MS * (1 << (attempt - 1));
                        tokio::time::sleep(tokio::time::Duration::from_millis(delay)).await;
                    } else {
                        return Err(anyhow::anyhow!(
                            "API error after {} retries: {}",
                            MAX_RETRIES,
                            e
                        ));
                    }
                }
            }
        }
    }
}