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
use async_stream::stream;
use axum::response::{
sse::{Event, KeepAlive},
Sse,
};
use eventsource_stream::Eventsource as EventsourceExt;
use futures_util::StreamExt;
use serde_json::{json, Value};
use std::collections::BTreeMap;
use std::sync::Arc;
use tracing::{error, warn};
use crate::{
session::SessionStore,
types::{ChatMessage, ChatRequest, ChatStreamChunk},
};
pub struct StreamArgs {
pub client: reqwest::Client,
pub url: String,
pub api_key: Arc<String>,
pub chat_req: ChatRequest,
pub response_id: String,
pub sessions: SessionStore,
pub prior_messages: Vec<ChatMessage>,
/// The fully translated request messages (including replayed history).
/// Used to save correct session history so turn-level reasoning can be
/// recovered when Codex replays the conversation without previous_response_id.
pub request_messages: Vec<ChatMessage>,
pub model: String,
}
struct ToolCallAccum {
id: String,
name: String,
arguments: String,
}
/// Translate an upstream Chat Completions SSE stream into a Responses API SSE stream.
///
/// Text response event sequence:
/// response.created → response.output_item.added (message) → response.output_text.delta*
/// → response.output_item.done → response.completed
///
/// Tool call response event sequence:
/// response.created → [accumulate deltas] → response.output_item.added (function_call)
/// → response.function_call_arguments.delta → response.output_item.done → response.completed
pub fn translate_stream(
args: StreamArgs,
) -> Sse<impl futures_util::Stream<Item = Result<Event, std::convert::Infallible>>> {
let StreamArgs {
client,
url,
api_key,
chat_req,
response_id,
sessions,
prior_messages,
request_messages,
model,
} = args;
let msg_item_id = format!("msg_{}", uuid::Uuid::new_v4().simple());
let event_stream = stream! {
yield Ok(Event::default()
.event("response.created")
.data(json!({
"type": "response.created",
"response": { "id": &response_id, "status": "in_progress", "model": &model }
}).to_string()));
let mut builder = client.post(&url).header("Content-Type", "application/json");
if !api_key.is_empty() {
builder = builder.bearer_auth(api_key.as_str());
}
let upstream = match builder.json(&chat_req).send().await {
Ok(r) if r.status().is_success() => r,
Ok(r) => {
let status = r.status();
let body = r.text().await.unwrap_or_default();
error!("upstream {status}: {body}");
yield Ok(Event::default().event("response.failed").data(
json!({"type": "response.failed", "response": {"id": &response_id, "status": "failed", "error": {"code": status.as_u16().to_string(), "message": body}}}).to_string()
));
return;
}
Err(e) => {
error!("upstream request failed: {e}");
yield Ok(Event::default().event("response.failed").data(
json!({"type": "response.failed", "response": {"id": &response_id, "status": "failed", "error": {"code": "connection_error", "message": e.to_string()}}}).to_string()
));
return;
}
};
let mut accumulated_text = String::new();
let mut accumulated_reasoning = String::new();
let mut tool_calls: BTreeMap<usize, ToolCallAccum> = BTreeMap::new();
let mut emitted_message_item = false;
let mut source = upstream.bytes_stream().eventsource();
while let Some(ev) = source.next().await {
match ev {
Err(e) => {
warn!("SSE parse error: {e}");
break;
}
Ok(ev) if ev.data.trim() == "[DONE]" => break,
Ok(ev) if ev.data.is_empty() => continue,
Ok(ev) => {
match serde_json::from_str::<ChatStreamChunk>(&ev.data) {
Err(e) => warn!("chunk parse error: {e} — data: {}", ev.data),
Ok(chunk) => {
for choice in &chunk.choices {
// Reasoning/thinking content (kimi-k2.6 etc.)
if let Some(rc) = choice.delta.reasoning_content.as_deref() {
if !rc.is_empty() {
accumulated_reasoning.push_str(rc);
}
}
// Text content
let content = choice.delta.content.as_deref().unwrap_or("");
if !content.is_empty() {
if !emitted_message_item {
yield Ok(Event::default()
.event("response.output_item.added")
.data(json!({
"type": "response.output_item.added",
"output_index": 0,
"item": { "type": "message", "id": &msg_item_id, "role": "assistant", "content": [], "status": "in_progress" }
}).to_string()));
emitted_message_item = true;
}
accumulated_text.push_str(content);
yield Ok(Event::default()
.event("response.output_text.delta")
.data(json!({
"type": "response.output_text.delta",
"item_id": &msg_item_id,
"output_index": 0,
"content_index": 0,
"delta": content
}).to_string()));
}
// Tool call deltas — accumulate by index
if let Some(delta_calls) = &choice.delta.tool_calls {
for dc in delta_calls {
let entry = tool_calls.entry(dc.index).or_insert(ToolCallAccum {
id: String::new(),
name: String::new(),
arguments: String::new(),
});
if let Some(id) = &dc.id {
if !id.is_empty() { entry.id.clone_from(id); }
}
if let Some(func) = &dc.function {
if let Some(n) = &func.name {
if !n.is_empty() { entry.name.push_str(n); }
}
if let Some(a) = &func.arguments {
entry.arguments.push_str(a);
}
}
}
}
}
}
}
}
}
}
// Close message item if one was opened
if emitted_message_item {
yield Ok(Event::default()
.event("response.output_item.done")
.data(json!({
"type": "response.output_item.done",
"output_index": 0,
"item": {
"type": "message",
"id": &msg_item_id,
"role": "assistant",
"status": "completed",
"content": [{"type": "output_text", "text": &accumulated_text}]
}
}).to_string()));
}
// Emit function_call items for each accumulated tool call
let base_index: usize = if emitted_message_item { 1 } else { 0 };
let mut fc_items: Vec<Value> = Vec::new();
for (rel_idx, (_, tc)) in tool_calls.iter().enumerate() {
let fc_item_id = format!("fc_{}", uuid::Uuid::new_v4().simple());
let output_index = base_index + rel_idx;
yield Ok(Event::default()
.event("response.output_item.added")
.data(json!({
"type": "response.output_item.added",
"output_index": output_index,
"item": {
"type": "function_call",
"id": &fc_item_id,
"call_id": &tc.id,
"name": &tc.name,
"arguments": "",
"status": "in_progress"
}
}).to_string()));
if !tc.arguments.is_empty() {
yield Ok(Event::default()
.event("response.function_call_arguments.delta")
.data(json!({
"type": "response.function_call_arguments.delta",
"item_id": &fc_item_id,
"output_index": output_index,
"delta": &tc.arguments
}).to_string()));
}
yield Ok(Event::default()
.event("response.output_item.done")
.data(json!({
"type": "response.output_item.done",
"output_index": output_index,
"item": {
"type": "function_call",
"id": &fc_item_id,
"call_id": &tc.id,
"name": &tc.name,
"arguments": &tc.arguments,
"status": "completed"
}
}).to_string()));
fc_items.push(json!({
"type": "function_call",
"id": fc_item_id,
"call_id": &tc.id,
"name": &tc.name,
"arguments": &tc.arguments,
"status": "completed"
}));
}
// Persist turn to session store
// Store reasoning_content per call_id so translate.rs can inject it
// back when Codex replays function_call items in the next request.
for tc in tool_calls.values() {
if !tc.id.is_empty() {
sessions.store_reasoning(tc.id.clone(), accumulated_reasoning.clone());
}
}
let assistant_tool_calls: Option<Vec<Value>> = if tool_calls.is_empty() {
None
} else {
Some(tool_calls.values().map(|tc| json!({
"id": &tc.id,
"type": "function",
"function": { "name": &tc.name, "arguments": &tc.arguments }
})).collect())
};
let assistant_msg = ChatMessage {
role: "assistant".into(),
content: if accumulated_text.is_empty() { None } else { Some(accumulated_text.clone()) },
reasoning_content: if accumulated_reasoning.is_empty() { None } else { Some(accumulated_reasoning.clone()) },
tool_calls: assistant_tool_calls,
tool_call_id: None,
name: None,
};
// Index reasoning by turn fingerprint so it can be recovered when
// Codex replays the full conversation in input[] without previous_response_id.
if !accumulated_reasoning.is_empty() {
sessions.store_turn_reasoning(&request_messages, &assistant_msg, accumulated_reasoning.clone());
}
let mut messages = prior_messages;
messages.push(assistant_msg);
sessions.save_with_id(response_id.clone(), messages);
// Build output array for response.completed
let mut output_items: Vec<Value> = Vec::new();
if emitted_message_item {
output_items.push(json!({
"type": "message",
"id": &msg_item_id,
"role": "assistant",
"status": "completed",
"content": [{"type": "output_text", "text": &accumulated_text}]
}));
}
output_items.extend(fc_items);
yield Ok(Event::default()
.event("response.completed")
.data(json!({
"type": "response.completed",
"response": {
"id": &response_id,
"status": "completed",
"model": &model,
"output": output_items
}
}).to_string()));
};
Sse::new(event_stream).keep_alive(KeepAlive::default())
}