chabeau 0.7.3

A full-screen terminal chat interface that connects to various AI APIs for real-time conversations
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
use std::collections::HashSet;
use std::time::Instant;

use super::App;
use crate::api::{ChatMessage, ChatToolDefinition, ChatToolFunction};
use crate::core::chat_stream::StreamParams;
use serde_json::json;
use tokio_util::sync::CancellationToken;

const MCP_RESOURCES_MARKER: &str = "MCP resources and templates (by server id):";
const MCP_PAYLOAD_NOTE_MARKER: &str = "MCP tool payload retention note:";

impl App {
    pub fn is_current_stream(&self, stream_id: u64) -> bool {
        self.session.current_stream_id == stream_id
    }

    pub fn end_streaming(&mut self) {
        self.ui.end_streaming();
        self.session.stream_cancel_token = None;
    }

    pub fn cancel_current_stream(&mut self) {
        self.conversation().cancel_current_stream();
    }

    pub fn begin_mcp_operation(&mut self) -> CancellationToken {
        let token = CancellationToken::new();
        self.session.stream_cancel_token = Some(token.clone());
        self.ui.stream_interrupted = false;
        self.ui
            .begin_activity(crate::core::app::ui_state::ActivityKind::McpOperation);
        token
    }

    pub fn end_mcp_operation_if_active(&mut self) {
        if matches!(
            self.ui.activity_kind(),
            Some(crate::core::app::ui_state::ActivityKind::McpOperation)
        ) {
            self.ui
                .end_activity(crate::core::app::ui_state::ActivityKind::McpOperation);
        }
        self.session.stream_cancel_token = None;
    }

    pub fn has_interruptible_activity(&self) -> bool {
        self.ui.is_streaming || self.session.stream_cancel_token.is_some()
    }

    pub fn enable_auto_scroll(&mut self) {
        self.ui.auto_scroll = true;
    }

    pub fn build_stream_params(
        &mut self,
        api_messages: Vec<ChatMessage>,
        cancel_token: CancellationToken,
        stream_id: u64,
    ) -> StreamParams {
        let base_messages = api_messages.clone();
        let tools = if self.session.mcp_tools_unsupported {
            None
        } else {
            self.collect_mcp_tools()
        };
        let mut api_messages = api_messages;
        if tools.is_some() {
            inject_mcp_preamble(&mut api_messages);
            if let Some(resources_text) = self.collect_mcp_resource_list() {
                inject_mcp_resources(&mut api_messages, &resources_text);
            }
            self.session.mcp_tools_enabled = true;
        } else {
            self.session.mcp_tools_enabled = false;
        }
        if let Some(note) = self.build_mcp_payload_note() {
            inject_mcp_payload_note(&mut api_messages, &note);
        }
        self.inject_tool_payload_history(&mut api_messages);
        self.inject_tool_summary_history(&mut api_messages);
        self.session
            .tool_pipeline
            .set_continuation(api_messages.clone(), base_messages);
        StreamParams {
            client: self.session.client.clone(),
            base_url: self.session.base_url.clone(),
            api_key: self.session.api_key.clone(),
            provider_name: self.session.provider_name.clone(),
            model: self.session.model.clone(),
            api_messages,
            tools,
            cancel_token,
            stream_id,
        }
    }

    pub fn last_retry_time(&self) -> Instant {
        self.session.last_retry_time
    }

    pub fn update_last_retry_time(&mut self, instant: Instant) {
        self.session.last_retry_time = instant;
    }

    fn collect_mcp_tools(&self) -> Option<Vec<ChatToolDefinition>> {
        let mut tools = Vec::new();
        let mut has_resources = false;
        let mut can_list_resources = false;
        let mut any_enabled = false;

        for server in self.mcp.servers() {
            if !server.config.is_enabled() {
                continue;
            }
            any_enabled = true;
            if server
                .server_details
                .as_ref()
                .map(|details| details.capabilities.resources.is_some())
                .unwrap_or(true)
            {
                can_list_resources = true;
            }
            if let Some(resources) = &server.cached_resources {
                if !resources.resources.is_empty() {
                    has_resources = true;
                }
                if resources.next_cursor.is_some() {
                    has_resources = true;
                    can_list_resources = true;
                }
            }
            if let Some(templates) = &server.cached_resource_templates {
                if !templates.resource_templates.is_empty() {
                    has_resources = true;
                }
                if templates.next_cursor.is_some() {
                    has_resources = true;
                    can_list_resources = true;
                }
            }
            let Some(list) = &server.cached_tools else {
                continue;
            };

            let allowed_tools = server.allowed_tools();
            let server_label = if server.config.display_name.is_empty() {
                server.config.id.as_str()
            } else {
                server.config.display_name.as_str()
            };

            for tool in &list.tools {
                if let Some(allowed) = allowed_tools {
                    if !allowed
                        .iter()
                        .any(|name| name.eq_ignore_ascii_case(&tool.name))
                    {
                        continue;
                    }
                }

                let Ok(parameters) = serde_json::to_value(&tool.input_schema) else {
                    continue;
                };

                let description = tool
                    .description
                    .clone()
                    .or_else(|| tool.title.clone())
                    .or_else(|| tool.annotations.as_ref().and_then(|ann| ann.title.clone()))
                    .map(|desc| format!("[MCP: {}] {}", server_label, desc))
                    .or_else(|| Some(format!("[MCP: {}] MCP tool", server_label)));

                tools.push(ChatToolDefinition {
                    kind: "function".to_string(),
                    function: ChatToolFunction {
                        name: tool.name.clone(),
                        description,
                        parameters,
                    },
                });
            }
        }

        if has_resources {
            tools.push(ChatToolDefinition {
                kind: "function".to_string(),
                function: ChatToolFunction {
                    name: crate::mcp::MCP_READ_RESOURCE_TOOL.to_string(),
                    description: Some(
                        "Read an MCP resource by server_id and uri from the MCP resources list (including templates)."
                            .to_string(),
                    ),
                    parameters: json!({
                        "type": "object",
                        "required": ["server_id", "uri"],
                        "properties": {
                            "server_id": {
                                "type": "string",
                                "description": "MCP server id from the resources list."
                            },
                            "uri": {
                                "type": "string",
                                "description": "Resource URI to read."
                            }
                        }
                    }),
                },
            });
        }

        if can_list_resources {
            tools.push(ChatToolDefinition {
                kind: "function".to_string(),
                function: ChatToolFunction {
                    name: crate::mcp::MCP_LIST_RESOURCES_TOOL.to_string(),
                    description: Some(
                        "List MCP resources or templates for a server. Use the cursor from the MCP resources list to page results. Set kind to \"resources\" (default) or \"templates\"."
                            .to_string(),
                    ),
                    parameters: json!({
                        "type": "object",
                        "required": ["server_id"],
                        "properties": {
                            "server_id": {
                                "type": "string",
                                "description": "MCP server id from the resources list."
                            },
                            "cursor": {
                                "type": "string",
                                "description": "Opaque pagination cursor from a previous response."
                            },
                            "kind": {
                                "type": "string",
                                "description": "List type: \"resources\" (default) or \"templates\"."
                            }
                        }
                    }),
                },
            });
        }

        if any_enabled {
            tools.push(ChatToolDefinition {
                kind: "function".to_string(),
                function: ChatToolFunction {
                    name: crate::mcp::MCP_INSTANT_RECALL_TOOL.to_string(),
                    description: Some(
                        "Recall the full payload from a prior MCP tool call using a tool_call_id."
                            .to_string(),
                    ),
                    parameters: json!({
                        "type": "object",
                        "required": ["tool_call_id"],
                        "properties": {
                            "tool_call_id": {
                                "type": "string",
                                "description": "Tool call id to recall."
                            }
                        }
                    }),
                },
            });
        }

        if tools.is_empty() {
            None
        } else {
            Some(tools)
        }
    }

    fn collect_mcp_resource_list(&self) -> Option<String> {
        let mut lines = Vec::new();

        for server in self.mcp.servers() {
            if !server.config.is_enabled() {
                continue;
            }
            if let Some(list) = &server.cached_resources {
                for resource in &list.resources {
                    let mut line = format!("- {}: {}", server.config.id, resource.uri);
                    if let Some(title) = resource.title.as_ref().or(resource.description.as_ref()) {
                        if !title.trim().is_empty() {
                            line.push_str(&format!(" - {}", title.trim()));
                        }
                    }
                    lines.push(line);
                }

                if let Some(cursor) = list.next_cursor.as_deref() {
                    lines.push(format!(
                        "- {}: more resources available (cursor: {}). Use {} with kind=\"resources\".",
                        server.config.id,
                        cursor,
                        crate::mcp::MCP_LIST_RESOURCES_TOOL
                    ));
                }
            }

            if let Some(list) = &server.cached_resource_templates {
                for template in &list.resource_templates {
                    let mut line = format!(
                        "- {}: template {} ({})",
                        server.config.id, template.name, template.uri_template
                    );
                    if let Some(title) = template.title.as_ref().or(template.description.as_ref()) {
                        if !title.trim().is_empty() {
                            line.push_str(&format!(" - {}", title.trim()));
                        }
                    }
                    lines.push(line);
                }

                if let Some(cursor) = list.next_cursor.as_deref() {
                    lines.push(format!(
                        "- {}: more templates available (cursor: {}). Use {} with kind=\"templates\".",
                        server.config.id,
                        cursor,
                        crate::mcp::MCP_LIST_RESOURCES_TOOL
                    ));
                }
            }
        }

        if lines.is_empty() {
            None
        } else {
            let mut output = String::from(MCP_RESOURCES_MARKER);
            output.push('\n');
            output.push_str(&lines.join("\n"));
            Some(output)
        }
    }

    fn build_mcp_payload_note(&self) -> Option<String> {
        if self.session.mcp_disabled {
            return None;
        }
        let mut entries = Vec::new();
        for server in self.mcp.servers() {
            if !server.config.is_enabled() {
                continue;
            }
            let policy = match server.config.tool_payloads() {
                crate::core::config::data::McpToolPayloadRetention::Turn => {
                    "default (turn)".to_string()
                }
                crate::core::config::data::McpToolPayloadRetention::Window => {
                    let window = server.config.tool_payload_window();
                    format!("window({})", window)
                }
                crate::core::config::data::McpToolPayloadRetention::All => "all".to_string(),
            };
            entries.push(format!("{}: {}", server.config.id, policy));
        }

        if entries.is_empty() {
            return None;
        }

        let mut note = format!(
            "{MCP_PAYLOAD_NOTE_MARKER} Default MCP tool output policy: only the current turn's raw outputs stay in chat context to save tokens; older outputs are summarized. Full payloads remain available via chabeau_instant_recall using call_id, which reinserts earlier outputs from system memory (NO software limit on retention)."
        );
        note.push_str("\nConfigure per server in config.toml with tool_payloads: turn (current turn only), window (last N raw outputs; set tool_payload_window), all (keep all raw outputs in context; token-expensive).");
        note.push_str("\nMCP tool payload policy by server: ");
        note.push_str(&entries.join(" | "));
        Some(note)
    }

    fn inject_tool_payload_history(&self, api_messages: &mut Vec<ChatMessage>) {
        if self.session.tool_pipeline.tool_payload_history.is_empty() {
            return;
        }

        let mut existing = HashSet::new();
        for message in api_messages.iter() {
            if message.role == "tool" {
                if let Some(id) = message.tool_call_id.as_ref() {
                    existing.insert(id.clone());
                }
            }
        }

        let mut history_messages = Vec::new();
        for entry in &self.session.tool_pipeline.tool_payload_history {
            if let Some(id) = entry.tool_call_id.as_ref() {
                if existing.contains(id) {
                    continue;
                }
            }
            history_messages.push(entry.assistant_message.clone());
            history_messages.push(entry.tool_message.clone());
        }

        if history_messages.is_empty() {
            return;
        }

        let insert_pos = api_messages
            .iter()
            .rposition(|msg| msg.role == "user")
            .unwrap_or(api_messages.len());
        api_messages.splice(insert_pos..insert_pos, history_messages);
    }

    fn inject_tool_summary_history(&self, api_messages: &mut Vec<ChatMessage>) {
        if self.session.tool_pipeline.tool_result_history.is_empty() {
            return;
        }

        let mut raw_ids = HashSet::new();
        for entry in &self.session.tool_pipeline.tool_payload_history {
            if let Some(id) = entry.tool_call_id.as_ref() {
                raw_ids.insert(id.clone());
            }
        }
        for message in &self.session.tool_pipeline.tool_results {
            if let Some(id) = message.tool_call_id.as_ref() {
                raw_ids.insert(id.clone());
            }
        }

        let mut summaries = Vec::new();
        for record in &self.session.tool_pipeline.tool_result_history {
            if let Some(id) = record.tool_call_id.as_ref() {
                if raw_ids.contains(id) {
                    continue;
                }
            }
            summaries.push(ChatMessage {
                role: "assistant".to_string(),
                content: {
                    let mut summary = format!(
                        "TOOL SUMMARY (system-added per MCP payload policy): {}",
                        record.summary
                    );
                    if let Some(id) = record
                        .tool_call_id
                        .as_ref()
                        .map(|value| value.trim())
                        .filter(|value| !value.is_empty())
                    {
                        summary.push_str(&format!(
                            " (call_id={id}; use chabeau_instant_recall for full output)"
                        ));
                    }
                    summary
                },
                name: None,
                tool_call_id: None,
                tool_calls: None,
            });
        }

        if summaries.is_empty() {
            return;
        }

        let insert_pos = api_messages
            .iter()
            .rposition(|msg| msg.role == "user")
            .unwrap_or(api_messages.len());
        api_messages.splice(insert_pos..insert_pos, summaries);
    }
}

fn inject_mcp_preamble(api_messages: &mut Vec<ChatMessage>) {
    let preamble = crate::core::builtin_mcp::builtin_mcp_preamble().trim();
    if preamble.is_empty() {
        return;
    }

    if let Some(message) = api_messages.iter_mut().find(|msg| msg.role == "system") {
        if !message.content.contains(preamble) {
            if !message.content.trim().is_empty() {
                message.content.push_str("\n\n");
            }
            message.content.push_str(preamble);
        }
        return;
    }

    api_messages.insert(
        0,
        ChatMessage {
            role: "system".to_string(),
            content: preamble.to_string(),
            name: None,
            tool_call_id: None,
            tool_calls: None,
        },
    );
}

fn inject_mcp_resources(api_messages: &mut Vec<ChatMessage>, resources_text: &str) {
    if resources_text.trim().is_empty() {
        return;
    }

    inject_or_replace_system_block(api_messages, MCP_RESOURCES_MARKER, resources_text);
}

fn inject_mcp_payload_note(api_messages: &mut Vec<ChatMessage>, note: &str) {
    if note.trim().is_empty() {
        return;
    }

    inject_or_replace_system_block(api_messages, MCP_PAYLOAD_NOTE_MARKER, note);
}

pub(crate) fn abbreviate_args(raw: &str) -> String {
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        return "(none)".to_string();
    }

    let mut summary = String::new();
    let mut count = 0;
    for ch in trimmed.chars() {
        if ch.is_whitespace() {
            if summary.ends_with(' ') {
                continue;
            }
            summary.push(' ');
        } else {
            summary.push(ch);
        }
        count += 1;
        if count >= 60 {
            summary.push('');
            break;
        }
    }
    summary
}

fn inject_or_replace_system_block(api_messages: &mut Vec<ChatMessage>, marker: &str, block: &str) {
    if let Some(message) = api_messages.iter_mut().find(|msg| msg.role == "system") {
        if let Some(start) = message.content.find(marker) {
            let suffix_start = message.content[start..]
                .find("\n\n")
                .map(|offset| start + offset)
                .unwrap_or_else(|| message.content.len());
            let mut updated = String::with_capacity(
                message.content.len().saturating_sub(suffix_start - start) + block.len(),
            );
            updated.push_str(&message.content[..start]);
            updated.push_str(block);
            if suffix_start < message.content.len() {
                updated.push_str(&message.content[suffix_start..]);
            }
            message.content = updated;
            return;
        }

        if !message.content.trim().is_empty() {
            message.content.push_str("\n\n");
        }
        message.content.push_str(block);
        return;
    }

    api_messages.insert(
        0,
        ChatMessage {
            role: "system".to_string(),
            content: block.to_string(),
            name: None,
            tool_call_id: None,
            tool_calls: None,
        },
    );
}