llm-dialect 0.1.2

Pure sans-I/O LLM dialect translation: Anthropic Messages / OpenAI Chat / OpenAI Responses wire formats ↔ a canonical model, plus SSE framer state machines. No runtime, no I/O, no wall clock.
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! Deflate: the items canonical → legacy OpenAI-shaped ChatRequest.
//!
//! Pure data transformation — no I/O, no runtime (enforced by the purity
//! lint): an accumulator folds each inbound message's content items into an
//! OpenAI wire message, tool results split off as role:"tool" messages, and
//! typed cfg fields / extras map onto the ChatRequest the pipeline consumes.
//! Moved verbatim from proxy_api.rs (its former home); lives here because it
//! operates on the items model that owns this tree.

use crate::canonical::ChatRequest;
use crate::error::ProxyError;
use crate::items::{ContentItem, ItemRequest, ResponseFormat, Role};

/// Accumulator for one inbound message as it folds into an OpenAI-dialect
/// wire message. `saw_payload` marks whether anything was folded at all (so
/// empty messages never emit a bare `{"role": ...}`).
#[derive(Default)]
struct MsgAcc {
    saw_payload: bool,
    content: String,
    reasoning: String,
    tool_calls: Vec<serde_json::Value>,
    thinking_blocks: Vec<serde_json::Value>,
}

impl MsgAcc {
    /// Fold a single content item; refuses non-text tool results with 400.
    fn fold(
        &mut self,
        item: &ContentItem,
        out_tool_msgs: &mut Vec<serde_json::Value>,
    ) -> Result<(), ProxyError> {
        match item {
            ContentItem::Text { text } => {
                self.saw_payload = true;
                self.content.push_str(text);
            }
            ContentItem::Thinking {
                text,
                signature,
                redacted_data,
                ..
            } => {
                self.saw_payload = true;
                if let Some(data) = redacted_data {
                    self.thinking_blocks.push(serde_json::json!({
                        "type": "redacted_thinking",
                        "data": data,
                    }));
                } else if signature.is_some() {
                    self.thinking_blocks.push(serde_json::json!({
                        "type": "thinking",
                        "thinking": text,
                        "signature": signature,
                    }));
                } else {
                    self.reasoning.push_str(text);
                }
            }
            ContentItem::ToolCall {
                id,
                name,
                arguments,
            } => {
                self.saw_payload = true;
                self.tool_calls.push(serde_json::json!({
                    "id": id,
                    "type": "function",
                    "function": {
                        "name": name,
                        "arguments": arguments.to_string(),
                    },
                }));
            }
            ContentItem::ToolResult {
                tool_call_id: tid,
                content: c,
                is_error,
            } => {
                // Tool messages must precede the coalesced user text (emitted
                // at message end) — OpenAI upstreams require tool messages to
                // follow the assistant tool_calls turn immediately.
                let mut out = match c {
                    serde_json::Value::String(s) => s.clone(),
                    serde_json::Value::Array(parts) => parts
                        .iter()
                        .filter_map(|p| p["text"].as_str())
                        .collect::<Vec<_>>()
                        .join("\n"),
                    other => {
                        return Err(ProxyError::BadRequest(format!(
                            "tool result content must be text or text parts, got {}",
                            other
                                .as_object()
                                .map(|_| "object")
                                .unwrap_or("non-text value")
                        )));
                    }
                };
                if *is_error {
                    out = format!("[tool error] {out}");
                }
                out_tool_msgs.push(serde_json::json!({
                    "role": "tool",
                    "tool_call_id": tid,
                    "content": out,
                }));
            }
            ContentItem::Refusal { text } => {
                self.saw_payload = true;
                self.content.push_str(text);
            }
        }
        Ok(())
    }

    /// Emit the accumulated wire message if anything was folded into it.
    fn finish(self, role: &str, name: Option<&str>) -> Option<serde_json::Value> {
        if !self.saw_payload {
            return None;
        }
        let MsgAcc {
            saw_payload: _,
            mut content,
            mut reasoning,
            mut tool_calls,
            mut thinking_blocks,
        } = self;
        let mut msg = serde_json::json!({"role": role});
        if let Some(n) = name {
            msg["name"] = serde_json::json!(n);
        }
        match role {
            "assistant" => {
                // legacy parity: content present unless the turn is
                // tool-call-only (bare null content upstreams reject the
                // explicit null less often than a missing key)
                if !content.is_empty() || tool_calls.is_empty() {
                    msg["content"] = serde_json::Value::String(std::mem::take(&mut content));
                }
                if !reasoning.is_empty() {
                    msg["reasoning_content"] =
                        serde_json::Value::String(std::mem::take(&mut reasoning));
                }
                if !tool_calls.is_empty() {
                    msg["tool_calls"] = serde_json::Value::Array(std::mem::take(&mut tool_calls));
                }
                if !thinking_blocks.is_empty() {
                    msg["thinking_blocks"] =
                        serde_json::Value::Array(std::mem::take(&mut thinking_blocks));
                }
            }
            _ => {
                msg["content"] = serde_json::Value::String(std::mem::take(&mut content));
            }
        }
        Some(msg)
    }
}

/// Canonical tool set → OpenAI `tools` wire array.
fn tools_to_wire(items: &ItemRequest) -> Option<serde_json::Value> {
    if items.tools.is_empty() {
        return None;
    }
    Some(serde_json::Value::Array(
        items
            .tools
            .iter()
            .map(|t| {
                serde_json::json!({
                    "type": "function",
                    "function": {
                        "name": t.name,
                        "description": t.description,
                        "parameters": t.input_schema,
                    },
                })
            })
            .collect(),
    ))
}

fn tool_choice_to_wire(tc: &crate::items::ToolChoice) -> serde_json::Value {
    match tc {
        crate::items::ToolChoice::Auto => serde_json::json!("auto"),
        crate::items::ToolChoice::None => serde_json::json!("none"),
        crate::items::ToolChoice::Required => serde_json::json!("required"),
        crate::items::ToolChoice::Tool { name } => serde_json::json!({
            "type": "function",
            "function": {"name": name},
        }),
    }
}

fn response_format_to_wire(fmt: &ResponseFormat) -> serde_json::Value {
    match fmt {
        ResponseFormat::Text => serde_json::json!({"type":"text"}),
        ResponseFormat::JsonObject => serde_json::json!({"type":"json_object"}),
        ResponseFormat::JsonSchema {
            name,
            schema,
            strict,
        } => serde_json::json!({
            "type": "json_schema",
            "json_schema": { "name": name, "schema": schema, "strict": strict },
        }),
    }
}

/// Deflate the items canonical back into the legacy OpenAI-shaped ChatRequest
/// the pipeline consumes. Item order is semantic; tool results split off as
/// role:"tool" messages (each carrying its tool_call_id — a bare role:"tool"
/// message 400s on every real upstream).
pub fn items_to_chat_request(items: &ItemRequest) -> Result<ChatRequest, ProxyError> {
    let mut messages: Vec<serde_json::Value> = Vec::new();
    for m in &items.messages {
        let role = match m.role {
            Role::System => "system",
            Role::User => "user",
            Role::Assistant => "assistant",
            // a canonical tool message never becomes a wire role:"tool"
            // framing message (no tool_call_id on it); its results split off
            // below, so any accompanying text surfaces as a user turn
            Role::Tool => "user",
        };
        let mut acc = MsgAcc::default();
        for item in &m.items {
            acc.fold(item, &mut messages)?;
        }
        if let Some(msg) = acc.finish(role, m.metadata.name.as_deref()) {
            messages.push(msg);
        }
    }
    let mut v = serde_json::json!({
        "model": items.model,
        "messages": messages,
        "stream": items.stream,
    });
    if let Some(mt) = items.max_tokens {
        v["max_tokens"] = serde_json::json!(mt);
    }
    if let Some(t) = items.temperature {
        v["temperature"] = serde_json::json!(t);
    }
    if let Some(p) = items.top_p {
        v["top_p"] = serde_json::json!(p);
    }
    if let Some(s) = &items.stop_sequences {
        v["stop"] = serde_json::json!(s);
    }
    // A trailing assistant turn carrying text is Anthropic's prefill: the
    // model must continue that text rather than open a new turn. Marked here
    // because only the canonical knows the shape; the provider decides what
    // the upstream can be told (see providers::openai::request_body).
    if items.messages.last().is_some_and(|m| {
        m.role == Role::Assistant
            && m.items.iter().any(
                |i| matches!(i, ContentItem::Text { text } if !text.trim().is_empty()),
            )
            // a turn still holding tool calls is a mid-loop replay awaiting
            // results, not a prefix the model is meant to continue
            && !m
                .items
                .iter()
                .any(|i| matches!(i, ContentItem::ToolCall { .. }))
    }) {
        v[crate::canonical::PREFILL_MARKER] = serde_json::json!(true);
    }
    if let Some(tools) = tools_to_wire(items) {
        v["tools"] = tools;
    }
    if let Some(tc) = &items.tool_choice {
        v["tool_choice"] = tool_choice_to_wire(tc);
    }
    if let Some(fmt) = &items.response_format {
        v["response_format"] = response_format_to_wire(fmt);
    }
    // an Anthropic-origin `thinking` object (possibly {"type":"disabled"})
    // rides in extra and must win over the typed cfg
    if let Some(raw) = items.extra.get("thinking").filter(|x| x.is_object()) {
        v["thinking"] = raw.clone();
    } else if let Some(th) = &items.thinking {
        // only Anthropic-shaped thinking with a real budget is emittable;
        // effort-only maps to reasoning_effort for the dialects that take it
        if let Some(b) = th.budget_tokens {
            v["thinking"] = serde_json::json!({
                "type": "enabled",
                "budget_tokens": b,
            });
        }
        if let Some(e) = &th.effort {
            v["reasoning_effort"] = serde_json::json!(e);
        }
    }
    // extras fill gaps; never clobber the keys this translator just computed.
    // Responses-only fields (captured from that surface's wildcard extras)
    // are dropped here: they 400 on chat-completions upstreams, and the
    // Responses provider never reads them out of the deflated request.
    const RESPONSES_ONLY_EXTRA: &[&str] = &[
        "background",
        "conversation",
        "include",
        "previous_response_id",
        "truncation",
    ];
    for (k, v2) in &items.extra {
        if v.get(k).is_none() && !RESPONSES_ONLY_EXTRA.contains(&k.as_str()) {
            v[k] = v2.clone();
        }
    }
    // Client-derived extras are flattened into ChatRequest fields here; a
    // typed-field collision must surface as a 500 body, not a request-task panic.
    serde_json::from_value(v).map_err(|e| {
        ProxyError::Internal(anyhow::anyhow!(
            "items→chat deflate produced invalid request: {e}"
        ))
    })
}

#[cfg(test)]
mod items_deflate_tests {
    use super::*;

    /// Anthropic prefill: the trailing assistant turn is a prefix to continue,
    /// and plain OpenAI chat cannot say so on its own.
    #[test]
    fn trailing_assistant_text_is_marked_as_prefill() {
        let items = crate::dialect::anthropic::req::from_anthropic(&serde_json::json!({
            "model":"m","max_tokens":100,
            "messages":[
                {"role":"user","content":"Name a colour."},
                {"role":"assistant","content":"The colour is"}
            ]
        }))
        .unwrap();
        let chat = items_to_chat_request(&items).unwrap();
        assert_eq!(
            chat.extra.get(crate::canonical::PREFILL_MARKER),
            Some(&serde_json::json!(true))
        );
    }

    #[test]
    fn trailing_user_turn_is_not_a_prefill() {
        let items = crate::dialect::anthropic::req::from_anthropic(&serde_json::json!({
            "model":"m","max_tokens":100,
            "messages":[{"role":"user","content":"hi"}]
        }))
        .unwrap();
        let chat = items_to_chat_request(&items).unwrap();
        assert!(chat.extra.get(crate::canonical::PREFILL_MARKER).is_none());
    }

    #[test]
    fn trailing_tool_call_turn_is_not_a_prefill() {
        // mid-loop replay awaiting tool results, not a prefix to continue
        let items = crate::dialect::anthropic::req::from_anthropic(&serde_json::json!({
            "model":"m","max_tokens":100,
            "messages":[
                {"role":"user","content":"go"},
                {"role":"assistant","content":[
                    {"type":"text","text":"let me check"},
                    {"type":"tool_use","id":"t1","name":"bash","input":{}}
                ]}
            ]
        }))
        .unwrap();
        let chat = items_to_chat_request(&items).unwrap();
        assert!(chat.extra.get(crate::canonical::PREFILL_MARKER).is_none());
    }

    /// C1 regression: a Responses tool round-trip must never emit a bare
    /// role:"tool" message (no tool_call_id) — upstreams 400 on those.
    #[test]
    fn responses_tool_loop_never_emits_bare_tool_message() {
        let raw = serde_json::json!({
            "model": "m",
            "input": [
                {"type":"message","role":"user","content":"run ls"},
                {"type":"function_call","call_id":"fc_1","name":"bash","arguments":"{}"},
                {"type":"function_call_output","call_id":"fc_1","output":"file.txt"}
            ]
        });
        let items = crate::dialect::openai_responses::req::from_openai_responses(&raw).unwrap();
        let chat = items_to_chat_request(&items).unwrap();
        let v = serde_json::to_value(&chat).unwrap();
        for msg in v["messages"].as_array().unwrap() {
            if msg["role"] == "tool" {
                assert!(
                    msg["tool_call_id"].as_str().is_some_and(|s| !s.is_empty()),
                    "bare tool message without tool_call_id: {msg}"
                );
            }
        }
        // exactly one tool message, paired with fc_1
        let tools: Vec<_> = v["messages"]
            .as_array()
            .unwrap()
            .iter()
            .filter(|m| m["role"] == "tool")
            .collect();
        assert_eq!(tools.len(), 1);
        assert_eq!(tools[0]["tool_call_id"], "fc_1");
        // the assistant turn carries the call
        let assistant = v["messages"][1].clone();
        assert_eq!(assistant["role"], "assistant");
        assert_eq!(assistant["tool_calls"][0]["id"], "fc_1");
    }

    #[test]
    fn tool_result_error_prefix_survives_deflation() {
        let items = crate::dialect::anthropic::req::from_anthropic(&serde_json::json!({
            "model":"m","max_tokens":10,
            "messages":[
                {"role":"user","content":"go"},
                {"role":"assistant","content":[{"type":"tool_use","id":"t1","name":"bash","input":{}}]},
                {"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","content":"boom","is_error":true}]}
            ]
        }))
        .unwrap();
        let chat = items_to_chat_request(&items).unwrap();
        let tool = chat
            .messages
            .iter()
            .find(|m| m.role == "tool")
            .expect("tool message");
        assert_eq!(tool.tool_call_id.as_deref(), Some("t1"));
        assert!(
            tool.text().starts_with("[tool error] "),
            "is_error must annotate the text: {:?}",
            tool.text()
        );
    }

    #[test]
    fn signed_thinking_becomes_thinking_blocks_unsigned_becomes_reasoning() {
        let items = crate::dialect::anthropic::req::from_anthropic(&serde_json::json!({
            "model":"m","max_tokens":100,
            "messages":[
                {"role":"user","content":"hi"},
                {"role":"assistant","content":[
                    {"type":"thinking","thinking":"plan","signature":"sig1"},
                    {"type":"text","text":"answer"}
                ]}
            ]
        }))
        .unwrap();
        let chat = items_to_chat_request(&items).unwrap();
        let assistant = &chat.messages[1];
        let blocks = assistant.thinking_blocks.as_ref().expect("thinking_blocks");
        assert_eq!(blocks[0]["signature"], "sig1");
        assert_eq!(assistant.text(), "answer");
        assert!(assistant.tool_calls.is_none());
        // and no reasoning_content alias for signed blocks
        assert!(assistant.reasoning_content.is_none());
    }

    #[test]
    fn thinking_disabled_stays_disabled() {
        let items = crate::dialect::anthropic::req::from_anthropic(&serde_json::json!({
            "model":"m","max_tokens":100,
            "thinking":{"type":"disabled"},
            "messages":[{"role":"user","content":"hi"}]
        }))
        .unwrap();
        let chat = items_to_chat_request(&items).unwrap();
        assert_eq!(
            chat.extra.get("thinking"),
            Some(&serde_json::json!({"type":"disabled"})),
            "disabled must not flip to enabled"
        );
    }

    #[test]
    fn no_cap_means_no_max_tokens_field() {
        let raw = serde_json::json!({"model":"m","input":"hi"});
        let items = crate::dialect::openai_responses::req::from_openai_responses(&raw).unwrap();
        let chat = items_to_chat_request(&items).unwrap();
        assert!(chat.max_tokens.is_none());
    }

    #[test]
    fn per_message_name_survives_deflation() {
        let raw = serde_json::json!({
            "model": "m",
            "messages": [
                {"role":"user","name":"alice","content":"hi"},
                {"role":"assistant","name":"bot","content":"yo"},
            ]
        });
        let items = crate::dialect::openai_chat::req::from_openai_chat(&raw).unwrap();
        let chat = items_to_chat_request(&items).unwrap();
        let v = serde_json::to_value(&chat).unwrap();
        assert_eq!(v["messages"][0]["name"], "alice");
        assert_eq!(v["messages"][1]["name"], "bot");
    }

    #[test]
    fn responses_only_extras_do_not_reach_chat_upstreams() {
        let raw = serde_json::json!({
            "model": "m",
            "input": "hi",
            "background": true,
            "previous_response_id": "resp_1",
            "conversation": "conv_1",
            "include": ["output_logprobs"],
            "truncation": "auto",
            "store": false,
            "metadata": {"k":"v"},
            "stream_options": {"include_usage": true},
        });
        let items = crate::dialect::openai_responses::req::from_openai_responses(&raw).unwrap();
        let chat = items_to_chat_request(&items).unwrap();
        for k in [
            "background",
            "previous_response_id",
            "conversation",
            "include",
            "truncation",
        ] {
            assert!(!chat.extra.contains_key(k), "{k} must be dropped");
        }
        // chat-legal fields survive; stream_options lands in the typed field
        assert!(chat.extra.contains_key("store"));
        assert!(chat.extra.contains_key("metadata"));
        assert!(chat.stream_options.is_some());
    }

    #[test]
    fn non_text_tool_result_content_is_a_hard_error() {
        // hand-built: every dialect req adapter coerces to strings, so this
        // guards against future ingress paths shipping structured content
        // that would silently stringify
        let mut items =
            crate::dialect::openai_responses::req::from_openai_responses(&serde_json::json!({
                "model": "m",
                "input": [
                    {"type":"function_call","call_id":"fc_1","name":"bash","arguments":"{}"},
                    {"type":"function_call_output","call_id":"fc_1","output":"ok"}
                ]
            }))
            .unwrap();
        for m in &mut items.messages {
            for it in &mut m.items {
                if let ContentItem::ToolResult { content, .. } = it {
                    *content = serde_json::json!({"structured": true});
                }
            }
        }
        assert!(items_to_chat_request(&items).is_err());
    }

    #[test]
    fn unsigned_thinking_deflates_to_reasoning_not_null_signed_block() {
        let items = crate::dialect::anthropic::req::from_anthropic(&serde_json::json!({
            "model":"m","max_tokens":100,
            "messages":[
                {"role":"user","content":"hi"},
                {"role":"assistant","content":[
                    {"type":"thinking","thinking":"plan"},
                    {"type":"text","text":"answer"}
                ]}
            ]
        }))
        .unwrap();
        let chat = items_to_chat_request(&items).unwrap();
        let assistant = &chat.messages[1];
        let v = serde_json::to_value(assistant).unwrap();
        assert!(
            v.get("thinking_blocks").is_none(),
            "unsigned thinking must not emit a thinking block: {v}"
        );
        assert_eq!(assistant.reasoning_content.as_deref(), Some("plan"));
    }

    #[test]
    fn tool_message_precedes_coalesced_user_text() {
        // OpenAI hard-400s on a user message inserted between the assistant
        // tool_calls turn and its tool results.
        let items = crate::dialect::anthropic::req::from_anthropic(&serde_json::json!({
            "model":"m","max_tokens":100,
            "messages":[
                {"role":"user","content":"go"},
                {"role":"assistant","content":[{"type":"tool_use","id":"t1","name":"bash","input":{}}]},
                {"role":"user","content":[
                    {"type":"text","text":"note to self"},
                    {"type":"tool_result","tool_use_id":"t1","content":"out"}
                ]}
            ]
        }))
        .unwrap();
        let chat = items_to_chat_request(&items).unwrap();
        let roles: Vec<&str> = chat.messages.iter().map(|m| m.role.as_str()).collect();
        assert_eq!(roles, vec!["user", "assistant", "tool", "user"]);
        assert_eq!(chat.messages[2].tool_call_id.as_deref(), Some("t1"));
        assert_eq!(chat.messages[3].text(), "note to self");
    }
}