anyllm_translate 0.9.7

Pure translation layer between Anthropic Messages API and OpenAI Chat Completions
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
//! Reverse streaming: Anthropic SSE events -> OpenAI ChatCompletionChunk SSE.
//!
//! Consumes `Anthropic StreamEvent` items and emits OpenAI `ChatCompletionChunk`
//! objects. This is the inverse of `StreamingTranslator` in `streaming_map.rs`.
//!
//! Used by the `/v1/chat/completions` handler when the incoming request is
//! already in OpenAI format and the backend speaks Anthropic (passthrough path).

use crate::anthropic;
use crate::mapping::reverse_message_map::{
    anthropic_stop_reason_to_openai, AnthropicTranslationContext,
};
use crate::openai;
use crate::openai::streaming::{
    ChatCompletionChunk, ChunkChoice, ChunkDelta, ChunkFunctionCall, ChunkToolCall,
};

/// Sentinel value returned by `process_event` to signal the stream is done.
/// The caller should emit `data: [DONE]\n\n` when it sees this.
pub const DONE_SENTINEL: &str = "[DONE]";

/// State machine that converts Anthropic SSE events into OpenAI ChatCompletionChunk objects.
///
/// Feed events via `process_event`, which returns zero or more chunks to send.
/// When `message_stop` is received, `is_done()` returns true and the caller
/// should emit `data: [DONE]\n\n`.
pub struct ReverseStreamingTranslator {
    message_id: String,
    model: String,
    tool_call_index: i32,
    input_tokens: Option<u32>,
    output_tokens: Option<u32>,
    created: u64,
    done: bool,
    context: AnthropicTranslationContext,
}

impl ReverseStreamingTranslator {
    /// Create a new reverse streaming translator.
    ///
    /// `id` is the Anthropic message ID from the `message_start` event;
    /// it is echoed in every emitted chunk so the client can correlate events.
    /// `tool_call_index` starts at -1 and increments on each new tool call block.
    pub fn new(id: String, model: String) -> Self {
        Self::with_context(id, model, AnthropicTranslationContext::default())
    }

    pub fn with_context(id: String, model: String, context: AnthropicTranslationContext) -> Self {
        Self {
            message_id: id,
            model,
            tool_call_index: -1,
            input_tokens: None,
            output_tokens: None,
            created: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
            done: false,
            context,
        }
    }

    /// Returns true after a `message_stop` event has been processed.
    /// Caller should emit `data: [DONE]\n\n` and close the stream.
    pub fn is_done(&self) -> bool {
        self.done
    }

    /// Process a single Anthropic StreamEvent and return zero or more OpenAI chunks.
    pub fn process_event(&mut self, event: &anthropic::StreamEvent) -> Vec<ChatCompletionChunk> {
        match event {
            anthropic::StreamEvent::MessageStart { message } => {
                self.input_tokens = Some(message.usage.input_tokens);
                if let Some(created) = message.created {
                    self.created = created;
                }
                // Emit first chunk with role
                vec![self.make_chunk(
                    ChunkDelta {
                        role: Some(openai::ChatRole::Assistant),
                        ..Default::default()
                    },
                    None,
                )]
            }
            anthropic::StreamEvent::ContentBlockStart {
                content_block:
                    anthropic::ContentBlock::ToolUse { id, name, .. }
                    | anthropic::ContentBlock::ServerToolUse { id, name, .. },
                ..
            } => {
                self.tool_call_index += 1;
                let tc = ChunkToolCall {
                    index: self.tool_call_index as u32,
                    id: Some(id.clone()),
                    call_type: Some("function".to_string()),
                    function: Some(ChunkFunctionCall {
                        name: Some(self.context.original_tool_name(name)),
                        arguments: Some(String::new()),
                    }),
                };
                vec![self.make_chunk(
                    ChunkDelta {
                        tool_calls: Some(vec![tc]),
                        ..Default::default()
                    },
                    None,
                )]
            }
            // Text and Thinking blocks emit their content via deltas
            anthropic::StreamEvent::ContentBlockStart { .. } => vec![],
            anthropic::StreamEvent::ContentBlockDelta { delta, .. } => match delta {
                anthropic::streaming::Delta::TextDelta { text } => {
                    vec![self.make_chunk(
                        ChunkDelta {
                            content: Some(text.clone()),
                            ..Default::default()
                        },
                        None,
                    )]
                }
                anthropic::streaming::Delta::InputJsonDelta { partial_json } => {
                    if self.tool_call_index < 0 {
                        return vec![];
                    }
                    let tc = ChunkToolCall {
                        index: self.tool_call_index as u32,
                        id: None,
                        call_type: None,
                        function: Some(ChunkFunctionCall {
                            name: None,
                            arguments: Some(partial_json.clone()),
                        }),
                    };
                    vec![self.make_chunk(
                        ChunkDelta {
                            tool_calls: Some(vec![tc]),
                            ..Default::default()
                        },
                        None,
                    )]
                }
                anthropic::streaming::Delta::ThinkingDelta { thinking } => {
                    vec![self.make_chunk(
                        ChunkDelta {
                            reasoning_content: Some(thinking.clone()),
                            ..Default::default()
                        },
                        None,
                    )]
                }
                anthropic::streaming::Delta::SignatureDelta { .. } => vec![],
                _ => vec![],
            },
            anthropic::StreamEvent::ContentBlockStop { .. } => vec![],
            anthropic::StreamEvent::MessageDelta { delta, usage } => {
                if let Some(u) = usage {
                    self.output_tokens = Some(u.output_tokens);
                }
                let finish_reason = delta
                    .stop_reason
                    .as_ref()
                    .map(anthropic_stop_reason_to_openai);
                let mut chunks = vec![self.make_chunk(ChunkDelta::default(), finish_reason)];
                // Emit usage chunk if we have token counts
                if let (Some(input), Some(output)) = (self.input_tokens, self.output_tokens) {
                    chunks.push(ChatCompletionChunk {
                        id: self.message_id.clone(),
                        object: "chat.completion.chunk".to_string(),
                        model: self.model.clone(),
                        // OpenAI streaming spec: the usage chunk intentionally has
                        // choices: []. Some community parsers assume choices[0] always
                        // exists; those parsers are non-compliant with the spec.
                        choices: vec![],
                        usage: Some(openai::ChatUsage {
                            prompt_tokens: input,
                            completion_tokens: output,
                            total_tokens: input + output,
                            completion_tokens_details: None,
                            prompt_tokens_details: None,
                        }),
                        created: Some(self.created),
                        system_fingerprint: None,
                    });
                }
                chunks
            }
            anthropic::StreamEvent::MessageStop {} => {
                self.done = true;
                vec![]
            }
            anthropic::StreamEvent::Ping {} => vec![],
            anthropic::StreamEvent::Error { .. } => {
                self.done = true;
                vec![]
            }
            _ => vec![],
        }
    }

    fn make_chunk(
        &self,
        delta: ChunkDelta,
        finish_reason: Option<openai::FinishReason>,
    ) -> ChatCompletionChunk {
        ChatCompletionChunk {
            id: self.message_id.clone(),
            object: "chat.completion.chunk".to_string(),
            model: self.model.clone(),
            choices: vec![ChunkChoice {
                index: 0,
                delta,
                finish_reason,
                logprobs: None,
            }],
            usage: None,
            created: Some(self.created),
            system_fingerprint: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::anthropic::messages::{ContentBlock, StopReason, Usage};
    use crate::anthropic::streaming::*;

    fn make_translator() -> ReverseStreamingTranslator {
        ReverseStreamingTranslator::new("chatcmpl-test".to_string(), "gpt-4o".to_string())
    }

    #[test]
    fn message_start_emits_role_chunk() {
        let mut t = make_translator();
        let event = StreamEvent::MessageStart {
            message: MessageStartData {
                id: "msg_123".to_string(),
                msg_type: "message".to_string(),
                role: "assistant".to_string(),
                content: vec![],
                model: "claude-sonnet".to_string(),
                stop_reason: None,
                stop_sequence: None,
                usage: Usage {
                    input_tokens: 10,
                    output_tokens: 0,
                    cache_creation_input_tokens: None,
                    cache_read_input_tokens: None,
                    ..Default::default()
                },
                created: Some(1700000000),
            },
        };
        let chunks = t.process_event(&event);
        assert_eq!(chunks.len(), 1);
        assert_eq!(
            chunks[0].choices[0].delta.role,
            Some(openai::ChatRole::Assistant)
        );
        assert!(chunks[0].choices[0].finish_reason.is_none());
    }

    #[test]
    fn text_delta_emits_content_chunk() {
        let mut t = make_translator();
        let event = StreamEvent::ContentBlockDelta {
            index: 0,
            delta: Delta::TextDelta {
                text: "Hello".to_string(),
            },
        };
        let chunks = t.process_event(&event);
        assert_eq!(chunks.len(), 1);
        assert_eq!(chunks[0].choices[0].delta.content.as_deref(), Some("Hello"));
    }

    #[test]
    fn tool_use_streaming() {
        let mut t = make_translator();
        // Start tool use block
        let start = StreamEvent::ContentBlockStart {
            index: 0,
            content_block: ContentBlock::ToolUse {
                id: "call_123".to_string(),
                name: "get_weather".to_string(),
                input: serde_json::Value::Object(serde_json::Map::new()),
            },
        };
        let chunks = t.process_event(&start);
        assert_eq!(chunks.len(), 1);
        let tc = &chunks[0].choices[0].delta.tool_calls.as_ref().unwrap()[0];
        assert_eq!(tc.id.as_deref(), Some("call_123"));
        assert_eq!(
            tc.function.as_ref().unwrap().name.as_deref(),
            Some("get_weather")
        );

        // Delta with args
        let delta = StreamEvent::ContentBlockDelta {
            index: 0,
            delta: Delta::InputJsonDelta {
                partial_json: "{\"loc".to_string(),
            },
        };
        let chunks = t.process_event(&delta);
        assert_eq!(chunks.len(), 1);
        let tc = &chunks[0].choices[0].delta.tool_calls.as_ref().unwrap()[0];
        assert_eq!(tc.index, 0);
        assert!(tc.id.is_none()); // Only first chunk has id
        assert_eq!(
            tc.function.as_ref().unwrap().arguments.as_deref(),
            Some("{\"loc")
        );
    }

    #[test]
    fn server_tool_use_streaming_restores_original_name() {
        let req: openai::ChatCompletionRequest = serde_json::from_value(serde_json::json!({
            "model": "claude",
            "messages": [{"role": "user", "content": "hi"}],
            "tools": [{"type": "function", "function": {"name": "bad.name", "parameters": {"type": "object"}}}],
            "max_tokens": 100
        }))
        .unwrap();
        let context =
            crate::mapping::reverse_message_map::AnthropicTranslationContext::from_openai_request(
                &req,
            );
        let mut t = ReverseStreamingTranslator::with_context(
            "chatcmpl-test".to_string(),
            "gpt-4o".to_string(),
            context,
        );
        let event = StreamEvent::ContentBlockStart {
            index: 0,
            content_block: ContentBlock::ServerToolUse {
                id: "srv_1".to_string(),
                name: "bad_name".to_string(),
                input: serde_json::json!({}),
            },
        };
        let chunks = t.process_event(&event);
        let tc = &chunks[0].choices[0].delta.tool_calls.as_ref().unwrap()[0];
        assert_eq!(tc.id.as_deref(), Some("srv_1"));
        assert_eq!(
            tc.function.as_ref().unwrap().name.as_deref(),
            Some("bad.name")
        );
    }

    #[test]
    fn thinking_delta_emits_reasoning_content() {
        let mut t = make_translator();
        let event = StreamEvent::ContentBlockDelta {
            index: 0,
            delta: Delta::ThinkingDelta {
                thinking: "Let me think...".to_string(),
            },
        };
        let chunks = t.process_event(&event);
        assert_eq!(chunks.len(), 1);
        assert_eq!(
            chunks[0].choices[0].delta.reasoning_content.as_deref(),
            Some("Let me think...")
        );
    }

    #[test]
    fn message_delta_emits_finish_reason_and_usage() {
        let mut t = make_translator();
        // Set input tokens via message_start
        let start = StreamEvent::MessageStart {
            message: MessageStartData {
                id: "msg_1".to_string(),
                msg_type: "message".to_string(),
                role: "assistant".to_string(),
                content: vec![],
                model: "claude".to_string(),
                stop_reason: None,
                stop_sequence: None,
                usage: Usage {
                    input_tokens: 10,
                    output_tokens: 0,
                    cache_creation_input_tokens: None,
                    cache_read_input_tokens: None,
                    ..Default::default()
                },
                created: None,
            },
        };
        t.process_event(&start);

        let event = StreamEvent::MessageDelta {
            delta: MessageDeltaData {
                stop_reason: Some(StopReason::EndTurn),
                stop_sequence: None,
                ..Default::default()
            },
            usage: Some(DeltaUsage { output_tokens: 5 }),
        };
        let chunks = t.process_event(&event);
        assert_eq!(chunks.len(), 2); // finish chunk + usage chunk
        assert_eq!(
            chunks[0].choices[0].finish_reason,
            Some(openai::FinishReason::Stop)
        );
        let usage = chunks[1].usage.as_ref().unwrap();
        assert_eq!(usage.prompt_tokens, 10);
        assert_eq!(usage.completion_tokens, 5);
        assert_eq!(usage.total_tokens, 15);
    }

    #[test]
    fn message_stop_sets_done() {
        let mut t = make_translator();
        assert!(!t.is_done());
        t.process_event(&StreamEvent::MessageStop {});
        assert!(t.is_done());
    }

    #[test]
    fn ping_produces_no_chunks() {
        let mut t = make_translator();
        let chunks = t.process_event(&StreamEvent::Ping {});
        assert!(chunks.is_empty());
    }

    #[test]
    fn multiple_tool_calls_track_index() {
        let mut t = make_translator();
        // First tool
        let start1 = StreamEvent::ContentBlockStart {
            index: 0,
            content_block: ContentBlock::ToolUse {
                id: "call_1".to_string(),
                name: "fn_a".to_string(),
                input: serde_json::Value::Object(serde_json::Map::new()),
            },
        };
        let chunks = t.process_event(&start1);
        assert_eq!(
            chunks[0].choices[0].delta.tool_calls.as_ref().unwrap()[0].index,
            0
        );

        // Second tool
        let start2 = StreamEvent::ContentBlockStart {
            index: 1,
            content_block: ContentBlock::ToolUse {
                id: "call_2".to_string(),
                name: "fn_b".to_string(),
                input: serde_json::Value::Object(serde_json::Map::new()),
            },
        };
        let chunks = t.process_event(&start2);
        assert_eq!(
            chunks[0].choices[0].delta.tool_calls.as_ref().unwrap()[0].index,
            1
        );
    }
}