mermaid-model 0.30.0

Model-layer core for Mermaid: provider adapters, wire types, capability catalog, and the shared utilities they need.
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
//! Anthropic's own tools, driven end to end against a [`MockProvider`].
//!
//! Four things must hold: a turn that allows them declares them in place of
//! Mermaid's schemas for the same tools; a native call reaches the reducer as
//! the Mermaid tool it stands for, with its id noted on the continuation; the
//! next request sends it back in the native form; and a model that refuses
//! them is retried with Mermaid's schemas and remembered.

use serde_json::{Value, json};

use super::anthropic::AnthropicAdapter;
use super::mock_http::{MockProvider, Reply};
use crate::models::ModelError;
use crate::models::config::{ModelConfig, NativeTools};
use crate::models::stream::StreamEvent;
use crate::models::traits::Model;
use crate::models::types::{ChatMessage, ModelResponse};

const SSE: &str = "text/event-stream";

fn fixture(name: &str) -> String {
    let path = format!(
        "{}/tests/fixtures/streams/anthropic/{name}",
        env!("CARGO_MANIFEST_DIR")
    );
    std::fs::read_to_string(path).expect("stream fixture under tests/fixtures/streams/anthropic")
}

fn adapter(provider: &MockProvider) -> AnthropicAdapter {
    AnthropicAdapter::new(
        "key".into(),
        "claude-nova-7".into(),
        format!("{}/v1", provider.url),
    )
    .expect("adapter")
}

fn tool(name: &str) -> Value {
    json!({"type": "function", "function": {
        "name": name,
        "description": format!("{name} tool"),
        "parameters": {"type": "object", "properties": {}},
    }})
}

fn config(native: NativeTools) -> ModelConfig {
    ModelConfig {
        tools: [
            "read_file",
            "write_file",
            "edit_file",
            "apply_patch",
            "execute_command",
        ]
        .into_iter()
        .map(tool)
        .collect(),
        native_tools: native,
        ..ModelConfig::default()
    }
}

const BOTH: NativeTools = NativeTools {
    text_editor: true,
    shell: true,
    computer: false,
};

fn tool_names(body: &Value) -> Vec<String> {
    body["tools"]
        .as_array()
        .expect("tools")
        .iter()
        .map(|t| t["name"].as_str().expect("name").to_string())
        .collect()
}

async fn chat(
    model: &dyn Model,
    history: &[ChatMessage],
    config: &ModelConfig,
) -> (Result<ModelResponse, ModelError>, Vec<StreamEvent>) {
    let (tx, mut rx) = tokio::sync::mpsc::channel(256);
    let drain = tokio::spawn(async move {
        let mut events = Vec::new();
        while let Some(event) = rx.recv().await {
            events.push(event);
        }
        events
    });
    let result = model.chat(history, config, Some(tx)).await;
    (result, drain.await.expect("drain"))
}

#[tokio::test]
async fn native_tools_replace_the_schemas_they_stand_for() {
    let provider = MockProvider::start(|_| Reply::stream(SSE, &fixture("text.sse"))).await;
    let adapter = adapter(&provider);
    let (result, _) = chat(&adapter, &[ChatMessage::user("hi")], &config(BOTH)).await;
    result.expect("chat");

    let body = &provider.bodies_to("/messages")[0];
    assert_eq!(
        tool_names(body),
        [
            "apply_patch",
            "execute_command",
            "str_replace_based_edit_tool",
            "bash"
        ]
    );
    let tools = body["tools"].as_array().expect("tools");
    assert_eq!(tools[2]["type"], "text_editor_20250728");
    assert_eq!(tools[3]["type"], "bash_20250124");
    assert!(tools[2].get("input_schema").is_none(), "schema-less");
    assert!(
        tools[3].get("cache_control").is_some(),
        "the cache marker stays on the last tool"
    );
}

#[tokio::test]
async fn a_turn_that_does_not_allow_them_sends_mermaids_schemas() {
    let provider = MockProvider::start(|_| Reply::stream(SSE, &fixture("text.sse"))).await;
    let adapter = adapter(&provider);
    let (result, _) = chat(
        &adapter,
        &[ChatMessage::user("hi")],
        &config(NativeTools::default()),
    )
    .await;
    result.expect("chat");
    let body = &provider.bodies_to("/messages")[0];
    assert_eq!(
        tool_names(body),
        [
            "read_file",
            "write_file",
            "edit_file",
            "apply_patch",
            "execute_command"
        ]
    );
}

#[tokio::test]
async fn native_calls_reach_the_harness_as_its_own_tools_and_go_back_as_written() {
    let provider =
        MockProvider::start(|_| Reply::stream(SSE, &fixture("native_tool_calls.sse"))).await;
    let adapter = adapter(&provider);
    let (result, events) = chat(&adapter, &[ChatMessage::user("fix it")], &config(BOTH)).await;
    let response = result.expect("chat");

    // What the reducer (and so every gate) sees.
    let streamed: Vec<(String, Value)> = events
        .iter()
        .filter_map(|e| match e {
            StreamEvent::ToolCall(tc) => {
                Some((tc.function.name.clone(), tc.function.arguments.clone()))
            },
            _ => None,
        })
        .collect();
    assert_eq!(
        streamed,
        [
            (
                "edit_file".to_string(),
                json!({"path": "src/lib.rs", "target_content": "old", "replacement_content": "new"})
            ),
            (
                "execute_command".to_string(),
                json!({"command": "cargo test", "timeout": 300})
            ),
            ("read_file".to_string(), json!({"path": "a.txt"})),
        ]
    );
    let calls = response.tool_calls.clone().expect("tool calls");
    assert_eq!(calls[0].function.name, "edit_file");
    let continuation = response
        .provider_continuation
        .clone()
        .expect("continuation notes the native calls");
    assert!(continuation.is_anthropic_native_call("toolu_edit"));
    assert!(continuation.is_anthropic_native_call("toolu_bash"));
    assert!(!continuation.is_anthropic_native_call("toolu_read"));

    // The next request replays the native calls natively and the Mermaid call
    // as it was.
    let mut assistant = ChatMessage::assistant("").with_provider_continuation(continuation);
    assistant.tool_calls = Some(calls);
    let history = vec![
        ChatMessage::user("fix it"),
        assistant,
        ChatMessage::tool("toolu_edit", "edit_file", "Edited src/lib.rs"),
        ChatMessage::tool("toolu_bash", "execute_command", "ok"),
        ChatMessage::tool("toolu_read", "read_file", "a"),
    ];
    let (result, _) = chat(&adapter, &history, &config(BOTH)).await;
    result.expect("second chat");
    let sent = provider.bodies_to("/messages");
    let replayed = &sent[1]["messages"][1]["content"];
    assert_eq!(
        replayed[0],
        json!({"type": "tool_use", "id": "toolu_edit", "name": "str_replace_based_edit_tool",
               "input": {"command": "str_replace", "path": "src/lib.rs", "old_str": "old", "new_str": "new"}})
    );
    assert_eq!(
        replayed[1],
        json!({"type": "tool_use", "id": "toolu_bash", "name": "bash",
               "input": {"command": "cargo test"}})
    );
    assert_eq!(replayed[2]["name"], "read_file");

    // A later turn without the native tools sends Mermaid's names throughout.
    let (result, _) = chat(&adapter, &history, &config(NativeTools::default())).await;
    result.expect("third chat");
    let sent = provider.bodies_to("/messages");
    let replayed = &sent[2]["messages"][1]["content"];
    assert_eq!(replayed[0]["name"], "edit_file");
    assert_eq!(replayed[1]["name"], "execute_command");
}

#[tokio::test]
async fn a_model_that_refuses_them_is_retried_with_mermaids_schemas_and_remembered() {
    let provider = MockProvider::start(|r| {
        let native = r.body["tools"]
            .as_array()
            .is_some_and(|tools| tools.iter().any(|t| t["type"] != "custom"));
        if native {
            return Reply::error(
                400,
                &json!({"type": "error", "error": {
                    "type": "invalid_request_error",
                    "message": "tools.5: Input tag 'text_editor_20250728' found using 'type' does not match any of the expected tags",
                }}),
            );
        }
        Reply::stream(SSE, &fixture("text.sse"))
    })
    .await;
    let adapter = adapter(&provider);

    let (result, events) = chat(&adapter, &[ChatMessage::user("hi")], &config(BOTH)).await;
    assert_eq!(result.expect("retry succeeds").content, "Hello, world");
    assert!(
        events
            .iter()
            .any(|e| matches!(e, StreamEvent::Status(s) if s.contains("text editor"))),
        "the user hears what was taken back"
    );
    let sent = provider.bodies_to("/messages");
    assert_eq!(sent.len(), 2);
    assert!(tool_names(&sent[1]).contains(&"read_file".to_string()));

    // Remembered: the next turn goes straight to Mermaid's schemas.
    let (result, _) = chat(&adapter, &[ChatMessage::user("hi")], &config(BOTH)).await;
    result.expect("third call");
    let sent = provider.bodies_to("/messages");
    assert_eq!(sent.len(), 3);
    assert!(!tool_names(&sent[2]).contains(&"bash".to_string()));
}

const COMPUTER: NativeTools = NativeTools {
    text_editor: false,
    shell: false,
    computer: true,
};

fn computer_config() -> ModelConfig {
    ModelConfig {
        tools: ["read_file", "computer"].into_iter().map(tool).collect(),
        native_tools: COMPUTER,
        ..ModelConfig::default()
    }
}

#[tokio::test]
async fn the_computer_toolset_stands_in_for_the_computer_tool() {
    let provider = MockProvider::start(|_| Reply::stream(SSE, &fixture("text.sse"))).await;
    let adapter = adapter(&provider);
    let (result, _) = chat(&adapter, &[ChatMessage::user("hi")], &computer_config()).await;
    result.expect("chat");
    let tools = provider.bodies_to("/messages")[0]["tools"]
        .as_array()
        .expect("tools")
        .clone();
    assert_eq!(tools.len(), 2);
    assert_eq!(tools[0]["name"], "read_file");
    assert_eq!(tools[1]["type"], "computer_toolset_20260801");
    assert!(
        tools[1].get("name").is_none(),
        "the toolset entry takes no name"
    );
}

#[tokio::test]
async fn member_calls_reach_the_harness_as_computer_actions_and_go_back_as_written() {
    let provider =
        MockProvider::start(|_| Reply::stream(SSE, &fixture("computer_toolset.sse"))).await;
    let adapter = adapter(&provider);
    let (result, _) = chat(
        &adapter,
        &[ChatMessage::user("click it")],
        &computer_config(),
    )
    .await;
    let response = result.expect("chat");
    let calls = response.tool_calls.clone().expect("tool calls");
    assert_eq!(calls[0].function.name, "computer");
    assert_eq!(
        calls[0].function.arguments,
        json!({"action": "left_click", "coordinate": [512, 384]})
    );
    assert_eq!(calls[1].function.arguments, json!({"action": "screenshot"}));
    let continuation = response
        .provider_continuation
        .clone()
        .expect("continuation");
    assert!(continuation.is_anthropic_native_call("toolu_click"));

    // Replayed as members, and each result echoes the toolset.
    let mut assistant = ChatMessage::assistant("").with_provider_continuation(continuation);
    assistant.tool_calls = Some(calls);
    let history = vec![
        ChatMessage::user("click it"),
        assistant,
        ChatMessage::tool("toolu_click", "computer", "OK"),
        ChatMessage::tool("toolu_shot", "computer", "[screenshot 1024x768]")
            .with_images(vec!["iVBORw0KGgo=".to_string()]),
    ];
    let (result, _) = chat(&adapter, &history, &computer_config()).await;
    result.expect("second chat");
    let sent = provider.bodies_to("/messages");
    let replayed = &sent[1]["messages"][1]["content"];
    assert_eq!(
        replayed[0],
        json!({"type": "tool_use", "id": "toolu_click", "name": "left_click",
               "input": {"coordinate": [512, 384]}, "toolset_name": "computer"})
    );
    let results = &sent[1]["messages"][2]["content"];
    assert_eq!(results[0]["toolset_name"], "computer");
    assert_eq!(results[0]["content"], "OK");
    assert_eq!(results[1]["toolset_name"], "computer");
    assert_eq!(results[1]["content"][1]["type"], "image");

    // A turn without the toolset sends Mermaid's tool and plain results.
    let mut plain = computer_config();
    plain.native_tools = NativeTools::default();
    let (result, _) = chat(&adapter, &history, &plain).await;
    result.expect("third chat");
    let sent = provider.bodies_to("/messages");
    assert_eq!(sent[2]["messages"][1]["content"][0]["name"], "computer");
    assert!(
        sent[2]["messages"][2]["content"][0]
            .get("toolset_name")
            .is_none()
    );
}

#[tokio::test]
async fn a_model_that_refuses_the_toolset_keeps_the_text_editor() {
    let provider = MockProvider::start(|r| {
        let toolset = r.body["tools"].as_array().is_some_and(|tools| {
            tools
                .iter()
                .any(|t| t["type"] == "computer_toolset_20260801")
        });
        if toolset {
            return Reply::error(
                400,
                &json!({"type": "error", "error": {
                    "type": "invalid_request_error",
                    "message": "'claude-nova-7' does not support tool types: computer_toolset_20260801. Did you mean one of text_editor_20250728, computer_20251124?",
                }}),
            );
        }
        Reply::stream(SSE, &fixture("text.sse"))
    })
    .await;
    let adapter = adapter(&provider);
    let mut config = computer_config();
    config.native_tools.text_editor = true;
    config.tools = ["read_file", "write_file", "edit_file", "computer"]
        .into_iter()
        .map(tool)
        .collect();
    let (result, _) = chat(&adapter, &[ChatMessage::user("hi")], &config).await;
    result.expect("retry succeeds");
    let sent = provider.bodies_to("/messages");
    assert_eq!(sent.len(), 2);
    let names = tool_names_or_types(&sent[1]);
    assert!(names.contains(&"computer".to_string()), "{names:?}");
    assert!(
        names.contains(&"str_replace_based_edit_tool".to_string()),
        "the editor is not blamed: {names:?}"
    );
}

fn tool_names_or_types(body: &Value) -> Vec<String> {
    body["tools"]
        .as_array()
        .expect("tools")
        .iter()
        .map(|t| {
            t["name"]
                .as_str()
                .or_else(|| t["type"].as_str())
                .unwrap_or_default()
                .to_string()
        })
        .collect()
}