mobius 0.16.33

A small, modular Rust framework for building coding agents
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
//! Shared Chat Completions wire primitives for native providers.

use std::borrow::Cow;
use std::collections::BTreeMap;
use std::sync::Arc;

use serde::Serialize;
use serde::ser::SerializeSeq as _;
use serde_json::Value;

use super::{ImageDataUrl, MAX_TOOL_CALLS, ToolDefinition, image_input, usage_i64};
use crate::protocol::TokenUsage;
use crate::{Error, Result};

#[derive(Serialize)]
pub(super) struct RequestBody<'a, C> {
    model: &'a str,
    stream: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(super) stream_options: Option<StreamOptions>,
    messages: Vec<WireMessage<'a, C>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tools: Option<WireTools<'a>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_choice: Option<&'static str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    parallel_tool_calls: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    reasoning_effort: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    prompt_cache_key: Option<&'a str>,
}

#[derive(Serialize)]
struct WireMessage<'a, C> {
    role: &'a str,
    #[serde(flatten)]
    payload: C,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    tool_calls: Vec<WireToolCall<'a>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_call_id: Option<&'a str>,
}

#[derive(Serialize)]
pub(super) struct StreamOptions {
    pub(super) include_usage: bool,
}

pub(super) enum MessageSource<'a> {
    System(&'a str),
    History { role: &'a str, item: &'a Value },
    Tool(&'a Value),
}

impl<'a, C: Default> RequestBody<'a, C> {
    pub(super) fn new(
        model: &'a str,
        reasoning_effort: Option<&'a str>,
        request: super::ModelRequest<'a>,
        provider: &str,
        content: impl FnMut(MessageSource<'a>) -> Result<C>,
    ) -> Result<Self> {
        let has_tools = !request.tools.is_empty();
        Ok(Self {
            model,
            stream: true,
            stream_options: None,
            messages: wire_messages(request.instructions, request.input, provider, content)?,
            tools: has_tools.then_some(WireTools(request.tools)),
            tool_choice: has_tools.then_some("auto"),
            parallel_tool_calls: has_tools.then_some(true),
            reasoning_effort,
            prompt_cache_key: request.prompt_cache.map(|cache| cache.key),
        })
    }
}

#[derive(Serialize)]
struct WireToolCall<'a> {
    id: &'a str,
    #[serde(rename = "type")]
    kind: &'static str,
    function: WireFunction<'a>,
}

#[derive(Serialize)]
struct WireFunction<'a> {
    name: &'a str,
    arguments: Cow<'a, str>,
}

fn wire_messages<'a, C: Default>(
    instructions: &'a str,
    input: super::ModelInput<'a>,
    provider: &str,
    mut content: impl FnMut(MessageSource<'a>) -> Result<C>,
) -> Result<Vec<WireMessage<'a, C>>> {
    let mut messages = Vec::new();
    if !instructions.trim().is_empty() {
        messages.push(WireMessage {
            role: "system",
            payload: content(MessageSource::System(instructions))?,
            tool_calls: Vec::new(),
            tool_call_id: None,
        });
    }
    for item in input.iter() {
        match item.get("type").and_then(Value::as_str) {
            Some("function_call") => {
                if messages
                    .last()
                    .is_none_or(|message| message.role != "assistant")
                {
                    messages.push(WireMessage {
                        role: "assistant",
                        payload: C::default(),
                        tool_calls: Vec::new(),
                        tool_call_id: None,
                    });
                }
                let call = WireToolCall {
                    id: required_string(item, "call_id", provider)?,
                    kind: "function",
                    function: WireFunction {
                        name: required_string(item, "name", provider)?,
                        arguments: argument_text(item.get("arguments"))?,
                    },
                };
                messages
                    .last_mut()
                    .expect("assistant message was just inserted")
                    .tool_calls
                    .push(call);
            }
            Some("function_call_output") => messages.push(WireMessage {
                role: "tool",
                tool_calls: Vec::new(),
                tool_call_id: Some(required_string(item, "call_id", provider)?),
                payload: content(MessageSource::Tool(
                    item.get("output")
                        .ok_or_else(|| Error::Provider("tool result omitted content".into()))?,
                ))?,
            }),
            Some("message") | None if item.get("role").is_some() => {
                let role = match required_string(item, "role", provider)? {
                    "developer" => "system",
                    role @ ("system" | "user" | "assistant" | "tool") => role,
                    role => {
                        return Err(Error::Provider(
                            format!("unsupported {provider} message role `{role}`").into(),
                        ));
                    }
                };
                messages.push(WireMessage {
                    role,
                    payload: content(MessageSource::History { role, item })?,
                    tool_calls: Vec::new(),
                    tool_call_id: None,
                });
            }
            Some(_) | None => {}
        }
    }
    if !messages
        .iter()
        .any(|message| matches!(message.role, "user" | "assistant" | "tool"))
    {
        return Err(Error::Provider(
            format!("{provider} request has no conversation messages").into(),
        ));
    }
    Ok(messages)
}

pub(super) struct WireTools<'a>(pub(super) &'a [Arc<ToolDefinition>]);

impl Serialize for WireTools<'_> {
    fn serialize<S: serde::Serializer>(
        &self,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error> {
        #[derive(Serialize)]
        struct Function<'a> {
            description: &'a str,
            name: &'a str,
            parameters: &'a Value,
        }
        #[derive(Serialize)]
        struct Tool<'a> {
            function: Function<'a>,
            #[serde(rename = "type")]
            kind: &'static str,
        }
        let mut sequence = serializer.serialize_seq(Some(self.0.len()))?;
        for tool in self.0 {
            sequence.serialize_element(&Tool {
                function: Function {
                    description: &tool.description,
                    name: &tool.name,
                    parameters: &tool.parameters,
                },
                kind: "function",
            })?;
        }
        sequence.end()
    }
}

#[derive(Serialize)]
#[serde(untagged)]
pub(super) enum WireContent<'a> {
    Text(Cow<'a, str>),
    Parts(Vec<WireContentPart<'a>>),
}

#[derive(Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub(super) enum WireContentPart<'a> {
    Text { text: Cow<'a, str> },
    ImageUrl { image_url: ImageUrl<'a> },
}

#[derive(Serialize)]
pub(super) struct ImageUrl<'a> {
    url: ImageDataUrl<'a>,
}

impl<'a> WireContent<'a> {
    pub(super) fn tool_result(output: &'a Value, provider: &str) -> Result<Self> {
        let Some(parts) = output.as_array().filter(|parts| {
            parts
                .iter()
                .any(|part| part.get("type").and_then(Value::as_str) == Some("input_image"))
        }) else {
            return Ok(Self::Text(tool_text(output)?));
        };
        let mut content = Vec::with_capacity(parts.len());
        for part in parts {
            match part.get("type").and_then(Value::as_str) {
                Some("input_text") => content.push(WireContentPart::Text {
                    text: part
                        .get("text")
                        .and_then(Value::as_str)
                        .ok_or_else(|| Error::Provider("invalid tool text".into()))?
                        .into(),
                }),
                Some("file") => content.push(WireContentPart::Text {
                    text: Cow::Owned(format!("Stored file: {}", part["file"])),
                }),
                Some("input_image") => {
                    if let Some((media_type, data)) = image_input(part, provider)? {
                        content.push(WireContentPart::ImageUrl {
                            image_url: ImageUrl {
                                url: ImageDataUrl(media_type, data),
                            },
                        });
                    }
                }
                _ => {
                    return Err(Error::Provider(
                        "selected provider cannot represent this tool observation".into(),
                    ));
                }
            }
        }
        Ok(Self::Parts(content))
    }

    pub(super) fn new(content: Option<&'a Value>, provider: &str) -> Result<Self> {
        let Some(Value::Array(parts)) = content else {
            return Ok(Self::Text(content_text(content)));
        };
        if !parts
            .iter()
            .any(|part| part.get("type").and_then(Value::as_str) == Some("input_image"))
        {
            return Ok(Self::Text(content_text(content)));
        }
        let mut output = Vec::with_capacity(parts.len());
        for part in parts {
            match part.get("type").and_then(Value::as_str) {
                Some("input_text" | "output_text" | "text") => output.push(WireContentPart::Text {
                    text: part
                        .get("text")
                        .and_then(Value::as_str)
                        .unwrap_or_default()
                        .into(),
                }),
                Some("input_image") => {
                    if let Some((media_type, data)) = image_input(part, provider)? {
                        output.push(WireContentPart::ImageUrl {
                            image_url: ImageUrl {
                                url: ImageDataUrl(media_type, data),
                            },
                        });
                    }
                }
                None | Some(_) => {}
            }
        }
        Ok(Self::Parts(output))
    }
}

pub(super) fn content_text(content: Option<&Value>) -> Cow<'_, str> {
    match content {
        Some(Value::String(text)) => Cow::Borrowed(text),
        Some(Value::Array(parts)) => {
            let mut text = parts
                .iter()
                .filter(|part| {
                    matches!(
                        part.get("type").and_then(Value::as_str),
                        Some("input_text" | "output_text" | "text")
                    )
                })
                .filter_map(|part| part.get("text").and_then(Value::as_str));
            match (text.next(), text.next()) {
                (None, _) => Cow::Borrowed(""),
                (Some(first), None) => Cow::Borrowed(first),
                (Some(first), Some(second)) => {
                    Cow::Owned([first, second].into_iter().chain(text).collect())
                }
            }
        }
        Some(value) => Cow::Owned(value.to_string()),
        None => Cow::Borrowed(""),
    }
}

#[derive(Default)]
pub(super) struct ToolCalls(BTreeMap<usize, PendingTool>);

#[derive(Default)]
struct PendingTool {
    id: String,
    name: String,
    arguments: String,
}

impl ToolCalls {
    pub(super) fn append(&mut self, delta: &Value, provider: &str) -> Result<()> {
        for (position, call) in delta
            .get("tool_calls")
            .and_then(Value::as_array)
            .into_iter()
            .flatten()
            .enumerate()
        {
            let index = call
                .get("index")
                .and_then(Value::as_u64)
                .and_then(|index| usize::try_from(index).ok())
                .unwrap_or(position);
            if self.0.len() >= MAX_TOOL_CALLS && !self.0.contains_key(&index) {
                return Err(Error::Provider(
                    format!("{provider} returned more than {MAX_TOOL_CALLS} tool calls").into(),
                ));
            }
            let pending = self.0.entry(index).or_default();
            set_fragment(
                &mut pending.id,
                call.get("id").and_then(Value::as_str),
                "ID",
                provider,
            )?;
            set_fragment(
                &mut pending.name,
                call.pointer("/function/name").and_then(Value::as_str),
                "name",
                provider,
            )?;
            if let Some(arguments) = call.pointer("/function/arguments").and_then(Value::as_str) {
                pending.arguments.push_str(arguments);
            }
        }
        Ok(())
    }

    pub(super) fn finish(self, provider: &str) -> Result<Vec<Value>> {
        self.0
            .into_values()
            .map(|call| {
                let arguments = if call.arguments.is_empty() {
                    "{}".into()
                } else {
                    call.arguments
                };
                Ok(serde_json::json!({
                    "type": "function_call",
                    "call_id": required(call.id, "tool-call ID", provider)?,
                    "name": required(call.name, "tool name", provider)?,
                    "arguments": arguments
                }))
            })
            .collect()
    }
}

pub(super) fn argument_text(arguments: Option<&Value>) -> Result<Cow<'_, str>> {
    match arguments {
        Some(Value::String(arguments)) => {
            serde_json::from_str::<serde::de::IgnoredAny>(arguments)?;
            Ok(Cow::Borrowed(arguments))
        }
        Some(arguments) => Ok(Cow::Owned(serde_json::to_string(arguments)?)),
        None => Err(Error::Provider("function call omitted arguments".into())),
    }
}

pub(super) fn required_string<'a>(
    value: &'a Value,
    field: &str,
    provider: &str,
) -> Result<&'a str> {
    value
        .get(field)
        .and_then(Value::as_str)
        .filter(|value| !value.is_empty())
        .ok_or_else(|| Error::Provider(format!("{provider} value omitted {field}").into()))
}

fn required(value: String, field: &str, provider: &str) -> Result<String> {
    (!value.is_empty())
        .then_some(value)
        .ok_or_else(|| Error::Provider(format!("{provider} response omitted {field}").into()))
}

fn set_fragment(
    target: &mut String,
    fragment: Option<&str>,
    field: &str,
    provider: &str,
) -> Result<()> {
    let Some(fragment) = fragment.filter(|fragment| !fragment.is_empty()) else {
        return Ok(());
    };
    if target.is_empty() {
        target.push_str(fragment);
    } else if target != fragment {
        return Err(Error::Provider(
            format!("{provider} changed a streamed tool-call {field}").into(),
        ));
    }
    Ok(())
}

pub(super) fn decode_usage(usage: Option<&Value>, provider: &str) -> Result<TokenUsage> {
    let value =
        |pointer| -> Result<i64> { Ok(usage_i64(usage, pointer, provider)?.unwrap_or_default()) };
    let cached_input_tokens =
        value("/cached_tokens")?.max(value("/prompt_tokens_details/cached_tokens")?);
    Ok(TokenUsage {
        input_tokens: value("/prompt_tokens")?,
        cached_input_tokens,
        cache_write_input_tokens: value("/prompt_tokens_details/cache_write_tokens")?,
        output_tokens: value("/completion_tokens")?,
        reasoning_output_tokens: value("/completion_tokens_details/reasoning_tokens")?,
        total_tokens: value("/total_tokens")?,
    })
}

pub(super) async fn post(
    client: &reqwest::Client,
    base_url: &str,
    api_key: Option<&str>,
    body: Vec<u8>,
    provider: &str,
) -> Result<reqwest::Response> {
    let mut request = client.post(format!("{base_url}/chat/completions"));
    if let Some(key) = api_key {
        request = request.bearer_auth(key);
    }
    let response = request
        .header(reqwest::header::CONTENT_TYPE, "application/json")
        .body(body)
        .send()
        .await?;
    if !response.status().is_success() {
        return Err(super::transport::status_error(response, provider).await);
    }
    Ok(response)
}

pub(super) fn tool_text(output: &Value) -> Result<Cow<'_, str>> {
    if let Some([part]) = output.as_array().map(Vec::as_slice)
        && part.get("type").and_then(Value::as_str) == Some("input_text")
    {
        return Ok(Cow::Borrowed(
            part.get("text")
                .and_then(Value::as_str)
                .ok_or_else(|| Error::Provider("invalid tool text".into()))?,
        ));
    }
    Ok(Cow::Owned(super::media::output_text(output)?))
}