crabmate 0.5.0

Rust AI agent: OpenAI-compatible chat/completions, function calling, HTTP serve, ops CLI
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
//! 出站 `chat/completions`:按厂商目录改写 `image_url` 内容块。
//!
//! 会话仍保存 **`/uploads/<文件名>`** 与工作区 **`@路径`**。真正 HTTP 前:文本网关压成纯文本;视觉网关读盘打成 **`data:`** URL。

use std::path::{Path, PathBuf};

use crate::cm_llm::outbound_workspace_images::attach_workspace_image_refs;
use crate::cm_llm::vendor_catalog::resolved_vendor_caps;
use crate::cm_types::{Message, MessageContent};

const MAX_INLINE_IMAGE_BYTES: u64 = 16 * 1024 * 1024;
pub(super) const FLATTEN_PLACEHOLDER: &str = "(用户发送了图片,但当前模型不支持视觉输入。)";

/// 按 **`image_url_content_parts`** 改写 `messages`(就地)。应在请求 JSON 序列化之前、日志预览之后调用,避免把 base64 打进日志。
pub fn rewrite_messages_for_vendor(
    messages: &mut [Message],
    model: &str,
    api_base: &str,
    uploads_dir: Option<&Path>,
    workspace_root: Option<&Path>,
) {
    let allow = resolved_vendor_caps(model, api_base).image_url_content_parts;
    let mut budget = MAX_INLINE_IMAGE_BYTES;
    for msg in messages {
        attach_workspace_image_refs(msg, allow, workspace_root, &mut budget);
        rewrite_one_message(msg, allow, uploads_dir, &mut budget);
    }
}

fn rewrite_one_message(
    msg: &mut Message,
    allow: bool,
    uploads_dir: Option<&Path>,
    budget: &mut u64,
) {
    let Some(MessageContent::Parts(parts)) = msg.content.as_mut() else {
        return;
    };
    if allow {
        *parts = inline_image_parts(std::mem::take(parts), uploads_dir, budget);
        collapse_single_text_part(msg);
    } else {
        flatten_image_parts(msg);
    }
}

fn collapse_single_text_part(msg: &mut Message) {
    let Some(MessageContent::Parts(parts)) = &msg.content else {
        return;
    };
    if parts.len() != 1 {
        return;
    }
    let Some(obj) = parts[0].as_object() else {
        return;
    };
    let is_text = obj.get("type").and_then(|v| v.as_str()) == Some("text");
    if !is_text {
        return;
    }
    let Some(text) = obj.get("text").and_then(|v| v.as_str()) else {
        return;
    };
    msg.content = Some(MessageContent::Text(text.to_string()));
}

fn flatten_image_parts(msg: &mut Message) {
    let Some(MessageContent::Parts(parts)) = &msg.content else {
        return;
    };
    let mut texts = Vec::new();
    let mut dropped_image = false;
    for part in parts {
        let Some(obj) = part.as_object() else {
            continue;
        };
        let typ = obj.get("type").and_then(|v| v.as_str()).unwrap_or("");
        if typ == "text" {
            if let Some(t) = obj.get("text").and_then(|v| v.as_str())
                && !t.trim().is_empty()
            {
                texts.push(t.to_string());
            }
        } else if typ == "image_url" {
            dropped_image = true;
        }
    }
    let mut body = texts.join("\n");
    if dropped_image && body.trim().is_empty() {
        body = FLATTEN_PLACEHOLDER.to_string();
    } else if dropped_image {
        body.push('\n');
        body.push_str(FLATTEN_PLACEHOLDER);
    }
    msg.content = Some(MessageContent::Text(body));
}

fn inline_image_parts(
    parts: Vec<serde_json::Value>,
    uploads_dir: Option<&Path>,
    budget: &mut u64,
) -> Vec<serde_json::Value> {
    let mut out = Vec::with_capacity(parts.len());
    for part in parts {
        match rewrite_image_url_part(&part, uploads_dir, budget) {
            ImagePartOutcome::Keep(v) => out.push(v),
            ImagePartOutcome::SkipNote(note) => {
                out.push(serde_json::json!({"type": "text", "text": note}));
            }
        }
    }
    out
}

enum ImagePartOutcome {
    Keep(serde_json::Value),
    SkipNote(String),
}

fn rewrite_image_url_part(
    part: &serde_json::Value,
    uploads_dir: Option<&Path>,
    budget: &mut u64,
) -> ImagePartOutcome {
    let Some(obj) = part.as_object() else {
        return ImagePartOutcome::Keep(part.clone());
    };
    if obj.get("type").and_then(|v| v.as_str()) != Some("image_url") {
        return ImagePartOutcome::Keep(part.clone());
    }
    let Some(url) = obj
        .get("image_url")
        .and_then(|v| v.get("url"))
        .and_then(|v| v.as_str())
    else {
        return ImagePartOutcome::Keep(part.clone());
    };
    let url = url.trim();
    if url.starts_with("data:") || looks_like_http_url(url) {
        return ImagePartOutcome::Keep(part.clone());
    }
    let Some(name) = uploads_file_name(url) else {
        return ImagePartOutcome::SkipNote(omit_note(InlineFail::BadPath, url));
    };
    let shown = format!("/uploads/{name}");
    let Some(dir) = uploads_dir else {
        return ImagePartOutcome::SkipNote(omit_note(InlineFail::NoDir, &shown));
    };
    match read_upload_as_data_url(dir, &name, budget) {
        Ok(data_url) => {
            let mut cloned = part.clone();
            if let Some(img) = cloned.get_mut("image_url").and_then(|v| v.as_object_mut()) {
                img.insert("url".into(), serde_json::Value::String(data_url));
            }
            ImagePartOutcome::Keep(cloned)
        }
        Err(fail) => ImagePartOutcome::SkipNote(omit_note(fail, &shown)),
    }
}

#[derive(Clone, Copy)]
pub(super) enum InlineFail {
    BadPath,
    NoDir,
    Read,
    Empty,
    TooLarge,
    NotImage,
}

pub(super) fn omit_note(fail: InlineFail, shown: &str) -> String {
    let why = match fail {
        InlineFail::TooLarge => "附图超过出站大小上限",
        InlineFail::NotImage => "附图不是 JPEG/PNG/GIF/WebP",
        InlineFail::Empty => "附图为空",
        InlineFail::NoDir | InlineFail::Read | InlineFail::BadPath => "附图已过期或无法读取",
    };
    format!("{why},已省略:{shown}")
}

pub(super) fn bytes_to_data_url(bytes: &[u8], budget: &mut u64) -> Result<String, InlineFail> {
    let len = bytes.len() as u64;
    if len == 0 {
        return Err(InlineFail::Empty);
    }
    if len > *budget {
        return Err(InlineFail::TooLarge);
    }
    let mime = sniff_image_mime(bytes).ok_or(InlineFail::NotImage)?;
    *budget -= len;
    use base64::Engine as _;
    let b64 = base64::engine::general_purpose::STANDARD.encode(bytes);
    Ok(format!("data:{mime};base64,{b64}"))
}

fn looks_like_http_url(url: &str) -> bool {
    let u = url.to_ascii_lowercase();
    u.starts_with("https://") || u.starts_with("http://")
}

fn uploads_file_name(url: &str) -> Option<String> {
    let t = url.trim();
    if t.contains("..") || t.contains('\\') || t.contains("//") {
        return None;
    }
    let name = t.strip_prefix("/uploads/")?;
    if name.is_empty() || name.contains('/') {
        return None;
    }
    Some(name.to_string())
}

fn read_upload_as_data_url(
    dir: &Path,
    name: &str,
    budget: &mut u64,
) -> Result<String, InlineFail> {
    let path = safe_upload_path(dir, name).ok_or(InlineFail::BadPath)?;
    let bytes = std::fs::read(&path).map_err(|_| InlineFail::Read)?;
    bytes_to_data_url(&bytes, budget)
}

fn safe_upload_path(dir: &Path, name: &str) -> Option<PathBuf> {
    if name.is_empty() || name.contains('/') || name.contains('\\') || name.contains("..") {
        return None;
    }
    Some(dir.join(name))
}

fn sniff_image_mime(bytes: &[u8]) -> Option<&'static str> {
    if is_jpeg_magic(bytes) {
        return Some("image/jpeg");
    }
    if is_png_magic(bytes) {
        return Some("image/png");
    }
    if is_gif_magic(bytes) {
        return Some("image/gif");
    }
    if is_webp_magic(bytes) {
        return Some("image/webp");
    }
    None
}

fn is_jpeg_magic(bytes: &[u8]) -> bool {
    bytes.len() >= 3 && bytes[0] == 0xFF && bytes[1] == 0xD8 && bytes[2] == 0xFF
}

fn is_png_magic(bytes: &[u8]) -> bool {
    bytes.len() >= 8 && bytes.starts_with(b"\x89PNG\r\n\x1a\n")
}

fn is_gif_magic(bytes: &[u8]) -> bool {
    bytes.len() >= 6 && (bytes.starts_with(b"GIF87a") || bytes.starts_with(b"GIF89a"))
}

fn is_webp_magic(bytes: &[u8]) -> bool {
    bytes.len() >= 12 && bytes.starts_with(b"RIFF") && &bytes[8..12] == b"WEBP"
}

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

    const PNG_1X1: &[u8] = &[
        0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44,
        0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F,
        0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x63, 0x00,
        0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x49,
        0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
    ];

    #[test]
    fn flatten_drops_image_url_for_text_deepseek() {
        let mut msgs = vec![message_user_with_images("看图", &["/uploads/a.png".into()])];
        rewrite_messages_for_vendor(
            &mut msgs,
            "deepseek-v4-flash",
            "https://api.deepseek.com/v1",
            None,
            None,
        );
        let MessageContent::Text(t) = msgs[0].content.as_ref().expect("text") else {
            panic!("expected flattened text");
        };
        assert!(t.contains("看图"));
        assert!(t.contains("不支持视觉"));
        assert!(!t.contains("image_url"));
    }

    #[test]
    fn flatten_text_deepseek_on_proxy_host() {
        let mut msgs = vec![message_user_with_images("看图", &["/uploads/a.png".into()])];
        rewrite_messages_for_vendor(
            &mut msgs,
            "deepseek-v4-flash",
            "https://llm.example.com/v1",
            None,
            None,
        );
        let MessageContent::Text(t) = msgs[0].content.as_ref().expect("text") else {
            panic!("expected flattened text");
        };
        assert!(t.contains("不支持视觉"));
    }

    #[test]
    fn vision_inlines_png_as_data_url() {
        let dir = tempfile::tempdir().expect("tmp");
        std::fs::write(dir.path().join("a.png"), PNG_1X1).expect("write");
        let mut msgs = vec![message_user_with_images("描述", &["/uploads/a.png".into()])];
        rewrite_messages_for_vendor(
            &mut msgs,
            "deepseek-v4-flash-vision-exp",
            "https://api.deepseek.com/v1",
            Some(dir.path()),
            None,
        );
        let MessageContent::Parts(parts) = msgs[0].content.as_ref().expect("parts") else {
            panic!("expected parts");
        };
        let url = parts[1]["image_url"]["url"].as_str().expect("url");
        assert!(url.starts_with("data:image/png;base64,"));
        assert!(parts[0]["text"].as_str() == Some("描述"));
    }

    #[test]
    fn vision_missing_file_becomes_note() {
        let dir = tempfile::tempdir().expect("tmp");
        let mut msgs = vec![message_user_with_images("", &["/uploads/gone.png".into()])];
        rewrite_messages_for_vendor(
            &mut msgs,
            "deepseek-v4-flash-vision-exp",
            "https://api.deepseek.com/v1",
            Some(dir.path()),
            None,
        );
        match msgs[0].content.as_ref() {
            Some(MessageContent::Text(t)) => assert!(t.contains("已过期") || t.contains("无法读取")),
            Some(MessageContent::Parts(p)) => {
                let joined: String = p
                    .iter()
                    .filter_map(|v| v.get("text").and_then(|x| x.as_str()))
                    .collect();
                assert!(joined.contains("已过期") || joined.contains("无法读取"));
            }
            _ => panic!("expected note"),
        }
    }

    #[test]
    fn vision_non_image_file_explains_type() {
        let dir = tempfile::tempdir().expect("tmp");
        std::fs::write(dir.path().join("a.png"), b"not-an-image").expect("write");
        let mut msgs = vec![message_user_with_images("", &["/uploads/a.png".into()])];
        rewrite_messages_for_vendor(
            &mut msgs,
            "deepseek-v4-flash-vision-exp",
            "https://api.deepseek.com/v1",
            Some(dir.path()),
            None,
        );
        let joined = match msgs[0].content.as_ref() {
            Some(MessageContent::Text(t)) => t.clone(),
            Some(MessageContent::Parts(p)) => p
                .iter()
                .filter_map(|v| v.get("text").and_then(|x| x.as_str()))
                .collect(),
            _ => panic!("expected note"),
        };
        assert!(joined.contains("不是 JPEG/PNG/GIF/WebP"));
    }

    #[test]
    fn over_budget_jpeg_is_too_large() {
        let dir = tempfile::tempdir().expect("tmp");
        std::fs::write(dir.path().join("a.jpg"), [0xFF, 0xD8, 0xFF, 0x01]).expect("write");
        let mut budget = 3;
        assert!(matches!(
            read_upload_as_data_url(dir.path(), "a.jpg", &mut budget),
            Err(InlineFail::TooLarge)
        ));
    }

    #[test]
    fn rejects_path_escape_in_uploads_name() {
        assert!(uploads_file_name("/uploads/../etc/passwd").is_none());
        assert!(uploads_file_name("/uploads/a/b.png").is_none());
        assert_eq!(
            uploads_file_name("/uploads/ok.png").as_deref(),
            Some("ok.png")
        );
    }

    #[test]
    fn vision_inlines_workspace_at_ref() {
        let ws = tempfile::tempdir().expect("tmp");
        std::fs::write(ws.path().join("plot.png"), PNG_1X1).expect("write");
        let mut msgs = vec![crate::cm_types::Message::user_only(
            "看 @plot.png".to_string(),
        )];
        rewrite_messages_for_vendor(
            &mut msgs,
            "deepseek-v4-flash-vision-exp",
            "https://api.deepseek.com/v1",
            None,
            Some(ws.path()),
        );
        let MessageContent::Parts(parts) = msgs[0].content.as_ref().expect("parts") else {
            panic!("expected parts");
        };
        let url = parts
            .iter()
            .find_map(|p| p.get("image_url").and_then(|i| i.get("url")).and_then(|u| u.as_str()))
            .expect("data url");
        assert!(url.starts_with("data:image/png;base64,"));
        assert!(parts.iter().any(|p| p.get("text").and_then(|t| t.as_str()) == Some("看 @plot.png")));
    }

    #[test]
    fn text_model_does_not_embed_workspace_png_bytes() {
        let ws = tempfile::tempdir().expect("tmp");
        std::fs::write(ws.path().join("plot.png"), PNG_1X1).expect("write");
        let mut msgs = vec![crate::cm_types::Message::user_only(
            "看 @plot.png".to_string(),
        )];
        rewrite_messages_for_vendor(
            &mut msgs,
            "deepseek-v4-flash",
            "https://api.deepseek.com/v1",
            None,
            Some(ws.path()),
        );
        let MessageContent::Text(t) = msgs[0].content.as_ref().expect("text") else {
            panic!("expected text");
        };
        assert!(t.contains("看 @plot.png"));
        assert!(t.contains("不支持视觉"));
        assert!(!t.contains("data:image"));
    }

    #[test]
    fn text_model_placeholder_once_for_at_and_uploads() {
        let ws = tempfile::tempdir().expect("tmp");
        std::fs::write(ws.path().join("plot.png"), PNG_1X1).expect("write");
        let mut msgs = vec![message_user_with_images(
            "看 @plot.png",
            &["/uploads/a.png".into()],
        )];
        rewrite_messages_for_vendor(
            &mut msgs,
            "deepseek-v4-flash",
            "https://api.deepseek.com/v1",
            None,
            Some(ws.path()),
        );
        let MessageContent::Text(t) = msgs[0].content.as_ref().expect("text") else {
            panic!("expected text");
        };
        assert_eq!(t.matches("不支持视觉").count(), 1);
    }

    #[test]
    fn vision_inlines_at_ref_despite_illegal_sibling_token() {
        let ws = tempfile::tempdir().expect("tmp");
        std::fs::write(ws.path().join("plot.png"), PNG_1X1).expect("write");
        let mut msgs = vec![crate::cm_types::Message::user_only(
            "看 @plot.png 和 @/etc/passwd".to_string(),
        )];
        rewrite_messages_for_vendor(
            &mut msgs,
            "deepseek-v4-flash-vision-exp",
            "https://api.deepseek.com/v1",
            None,
            Some(ws.path()),
        );
        let MessageContent::Parts(parts) = msgs[0].content.as_ref().expect("parts") else {
            panic!("expected parts");
        };
        assert!(parts.iter().any(|p| p
            .get("image_url")
            .and_then(|i| i.get("url"))
            .and_then(|u| u.as_str())
            .is_some_and(|u| u.starts_with("data:image/png;base64,"))));
    }
}