lash-provider-openai 0.1.0-alpha.113

OpenAI providers for lash: API-key (OpenRouter, OpenAI, vLLM, etc.) and Codex OAuth (ChatGPT Plus/Pro/Team).
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
//! Scripted local WebSocket server for Codex WebSocket tests.
//!
//! One harness, two consumers: the provider-layer unit tests in
//! [`crate::codex`] drive [`crate::codex::CodexProvider`] directly against it,
//! and the runtime-level test (`tests/codex_websocket_runtime.rs`) drives a
//! full facade turn (`LashCore` + `ProviderHandle`) over the same server via
//! [`crate::codex::CodexProvider::with_endpoint_urls`] and
//! [`crate::codex::CodexProvider::force_websocket_transport`].
//!
//! Compiled for unit tests and, behind the default-on `testing` feature, for
//! integration tests and downstream harnesses.

use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use futures_util::{SinkExt, StreamExt};
use serde_json::{Value, json};
use tokio::net::{TcpListener, TcpStream};
use tokio::task::JoinHandle;
use tokio_tungstenite::tungstenite::handshake::server::{
    Request as WsHandshakeRequest, Response as WsHandshakeResponse,
};
use tokio_tungstenite::tungstenite::protocol::Message as WsMessage;
use tokio_tungstenite::{WebSocketStream, accept_hdr_async};

/// A Codex Responses assistant message item carrying `text`, as emitted in
/// `response.output_item.done` / `response.completed` payloads.
pub fn assistant_item(message_id: &str, text: &str) -> Value {
    json!({
        "type": "message",
        "id": message_id,
        "role": "assistant",
        "status": "completed",
        "phase": "final_answer",
        "content": [{"type": "output_text", "text": text, "annotations": []}]
    })
}

/// A Codex Responses `function_call` item, as emitted in
/// `response.output_item.done` / `response.completed` payloads.
pub fn function_call_item(call_id: &str, tool_name: &str, arguments: &str) -> Value {
    json!({
        "type": "function_call",
        "id": format!("fc_{call_id}"),
        "call_id": call_id,
        "name": tool_name,
        "arguments": arguments,
        "status": "completed"
    })
}

/// What the scripted server does in response to the next `response.create`
/// request it receives. Actions are consumed in order across connections.
#[derive(Clone, Debug)]
pub enum ScriptedWsAction {
    /// Stream a text delta, the completed message item, and
    /// `response.completed`.
    Complete {
        response_id: &'static str,
        message_id: &'static str,
        text: &'static str,
    },
    /// Like [`ScriptedWsAction::Complete`], then close the connection so a
    /// cached socket is dead on reuse.
    CompleteAndClose {
        response_id: &'static str,
        message_id: &'static str,
        text: &'static str,
    },
    /// Emit a completed `function_call` item and `response.completed`,
    /// terminating the turn iteration with a tool call.
    ToolCall {
        response_id: &'static str,
        call_id: &'static str,
        tool_name: &'static str,
        arguments: &'static str,
    },
    /// Terminal `response.completed` with `status: incomplete`.
    Incomplete {
        response_id: &'static str,
        message_id: &'static str,
        text: &'static str,
    },
    /// An `error` event before any output.
    Error { message: &'static str },
    /// Start streaming output, then emit an `error` event mid-stream.
    MidStreamError {
        message_id: &'static str,
        text: &'static str,
        message: &'static str,
    },
    /// Stream partial output and usage, then close cleanly without a terminal
    /// response event.
    CloseAfterStart {
        response_id: &'static str,
        message_id: &'static str,
        text: &'static str,
    },
    /// Accept the request and go silent before any output, forcing the
    /// client's idle timeout.
    IdleBeforeStart,
    /// Stream partial output, then go silent, forcing the idle timeout after
    /// output started.
    IdleAfterStart {
        message_id: &'static str,
        text: &'static str,
    },
}

/// Captured request headers, one inner vec of `(name, value)` pairs per
/// WebSocket handshake the scripted server accepted.
pub type CapturedHandshakes = Arc<Mutex<Vec<Vec<(String, String)>>>>;

/// Handle to a running scripted server. Dropping it aborts the accept loop.
pub struct ScriptedWsServer {
    /// `ws://…` URL to point [`crate::codex::CodexProvider::with_endpoint_urls`] at.
    pub url: String,
    captured: Arc<Mutex<Vec<Value>>>,
    captured_raw: Arc<Mutex<Vec<Vec<u8>>>>,
    handshakes: CapturedHandshakes,
    close_frames: Arc<Mutex<u32>>,
    task: JoinHandle<()>,
}

impl ScriptedWsServer {
    /// Every JSON request the server received, in order.
    pub fn captured(&self) -> Vec<Value> {
        self.captured
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .clone()
    }

    /// Every request payload exactly as received from the WebSocket frame.
    pub fn captured_raw(&self) -> Vec<Vec<u8>> {
        self.captured_raw
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .clone()
    }

    /// Request headers per accepted WebSocket handshake, in order.
    pub fn handshakes(&self) -> Vec<Vec<(String, String)>> {
        self.handshakes
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .clone()
    }

    /// Number of WebSocket Close frames the server has received.
    pub fn close_frame_count(&self) -> u32 {
        *self
            .close_frames
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }
}

impl Drop for ScriptedWsServer {
    fn drop(&mut self) {
        self.task.abort();
    }
}

/// Bind a local WebSocket server that answers successive requests with
/// `actions`, capturing every request payload and handshake headers.
pub async fn spawn_scripted_websocket(actions: Vec<ScriptedWsAction>) -> ScriptedWsServer {
    let listener = TcpListener::bind(("127.0.0.1", 0)).await.expect("bind ws");
    let addr = listener.local_addr().expect("ws addr");
    let actions = Arc::new(Mutex::new(VecDeque::from(actions)));
    let captured = Arc::new(Mutex::new(Vec::new()));
    let captured_raw = Arc::new(Mutex::new(Vec::new()));
    let handshakes = Arc::new(Mutex::new(Vec::new()));
    let close_frames = Arc::new(Mutex::new(0u32));
    let task_actions = Arc::clone(&actions);
    let task_captured = Arc::clone(&captured);
    let task_captured_raw = Arc::clone(&captured_raw);
    let task_handshakes = Arc::clone(&handshakes);
    let task_close_frames = Arc::clone(&close_frames);
    let task = tokio::spawn(async move {
        loop {
            let Ok((stream, _)) = listener.accept().await else {
                break;
            };
            let actions = Arc::clone(&task_actions);
            let captured = Arc::clone(&task_captured);
            let captured_raw = Arc::clone(&task_captured_raw);
            let handshakes = Arc::clone(&task_handshakes);
            let close_frames = Arc::clone(&task_close_frames);
            tokio::spawn(async move {
                #[expect(
                    clippy::result_large_err,
                    reason = "tungstenite fixes the handshake callback error to an HTTP response"
                )]
                let callback = move |request: &WsHandshakeRequest,
                                     response: WsHandshakeResponse| {
                    let headers = request
                        .headers()
                        .iter()
                        .filter_map(|(name, value)| {
                            value
                                .to_str()
                                .ok()
                                .map(|value| (name.as_str().to_string(), value.to_string()))
                        })
                        .collect::<Vec<_>>();
                    handshakes
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner)
                        .push(headers);
                    Ok(response)
                };
                let Ok(mut ws) = accept_hdr_async(stream, callback).await else {
                    return;
                };
                while let Some(Ok(message)) = ws.next().await {
                    let text = match message {
                        WsMessage::Text(text) => text.to_string(),
                        WsMessage::Binary(bytes) => {
                            String::from_utf8(bytes.to_vec()).unwrap_or_default()
                        }
                        WsMessage::Close(_) => {
                            *close_frames
                                .lock()
                                .unwrap_or_else(std::sync::PoisonError::into_inner) += 1;
                            break;
                        }
                        WsMessage::Ping(_) | WsMessage::Pong(_) | WsMessage::Frame(_) => {
                            continue;
                        }
                    };
                    captured_raw
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner)
                        .push(text.as_bytes().to_vec());
                    let request: Value = serde_json::from_str(&text).expect("ws request json");
                    captured
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner)
                        .push(request);
                    let action = actions
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner)
                        .pop_front()
                        .expect("scripted ws action");
                    match action {
                        ScriptedWsAction::Complete {
                            response_id,
                            message_id,
                            text,
                        } => {
                            send_completed_ws_response(&mut ws, response_id, message_id, text)
                                .await;
                        }
                        ScriptedWsAction::CompleteAndClose {
                            response_id,
                            message_id,
                            text,
                        } => {
                            send_completed_ws_response(&mut ws, response_id, message_id, text)
                                .await;
                            let _ = ws.close(None).await;
                            break;
                        }
                        ScriptedWsAction::ToolCall {
                            response_id,
                            call_id,
                            tool_name,
                            arguments,
                        } => {
                            send_tool_call_ws_response(
                                &mut ws,
                                response_id,
                                call_id,
                                tool_name,
                                arguments,
                            )
                            .await;
                        }
                        ScriptedWsAction::Incomplete {
                            response_id,
                            message_id,
                            text,
                        } => {
                            send_incomplete_ws_response(&mut ws, response_id, message_id, text)
                                .await;
                        }
                        ScriptedWsAction::Error { message } => {
                            send_ws_json(
                                &mut ws,
                                json!({"type":"error","error":{"message": message}}),
                            )
                            .await;
                        }
                        ScriptedWsAction::MidStreamError {
                            message_id,
                            text,
                            message,
                        } => {
                            send_ws_json(
                                &mut ws,
                                json!({"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":message_id,"status":"in_progress","phase":"final_answer","content":[]}}),
                            )
                            .await;
                            send_ws_json(
                                &mut ws,
                                json!({"type":"response.output_text.delta","output_index":0,"item_id":message_id,"delta":text}),
                            )
                            .await;
                            send_ws_json(
                                &mut ws,
                                json!({"type":"error","error":{"message": message}}),
                            )
                            .await;
                        }
                        ScriptedWsAction::CloseAfterStart {
                            response_id,
                            message_id,
                            text,
                        } => {
                            send_ws_json(
                                &mut ws,
                                json!({"type":"response.created","response":{"id":response_id,"status":"in_progress","usage":{"input_tokens":4,"output_tokens":1,"total_tokens":5}}}),
                            )
                            .await;
                            send_ws_json(
                                &mut ws,
                                json!({"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":message_id,"status":"in_progress","phase":"final_answer","content":[]}}),
                            )
                            .await;
                            send_ws_json(
                                &mut ws,
                                json!({"type":"response.output_text.delta","output_index":0,"item_id":message_id,"delta":text}),
                            )
                            .await;
                            let _ = ws.close(None).await;
                            break;
                        }
                        ScriptedWsAction::IdleBeforeStart => {
                            tokio::time::sleep(Duration::from_secs(60)).await;
                        }
                        ScriptedWsAction::IdleAfterStart { message_id, text } => {
                            send_ws_json(
                                &mut ws,
                                json!({"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":message_id,"status":"in_progress","phase":"final_answer","content":[]}}),
                            )
                            .await;
                            send_ws_json(
                                &mut ws,
                                json!({"type":"response.output_text.delta","output_index":0,"item_id":message_id,"delta":text}),
                            )
                            .await;
                            tokio::time::sleep(Duration::from_secs(60)).await;
                        }
                    }
                }
            });
        }
    });
    ScriptedWsServer {
        url: format!("ws://{addr}/codex/responses"),
        captured,
        captured_raw,
        handshakes,
        close_frames,
        task,
    }
}

async fn send_ws_json(ws: &mut WebSocketStream<TcpStream>, value: Value) {
    ws.send(WsMessage::Text(value.to_string().into()))
        .await
        .expect("send ws event");
}

async fn send_completed_ws_response(
    ws: &mut WebSocketStream<TcpStream>,
    response_id: &str,
    message_id: &str,
    text: &str,
) {
    let item = assistant_item(message_id, text);
    send_ws_json(
        ws,
        json!({"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":message_id,"status":"in_progress","phase":"final_answer","content":[]}}),
    )
    .await;
    send_ws_json(
        ws,
        json!({"type":"response.output_text.delta","output_index":0,"item_id":message_id,"delta":text}),
    )
    .await;
    send_ws_json(
        ws,
        json!({"type":"response.output_item.done","output_index":0,"item":item}),
    )
    .await;
    send_ws_json(
        ws,
        json!({"type":"response.completed","response":{"id":response_id,"status":"completed","output":[assistant_item(message_id, text)],"usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}}}),
    )
    .await;
}

async fn send_tool_call_ws_response(
    ws: &mut WebSocketStream<TcpStream>,
    response_id: &str,
    call_id: &str,
    tool_name: &str,
    arguments: &str,
) {
    let item = function_call_item(call_id, tool_name, arguments);
    send_ws_json(
        ws,
        json!({"type":"response.output_item.added","output_index":0,"item":item.clone()}),
    )
    .await;
    send_ws_json(
        ws,
        json!({"type":"response.output_item.done","output_index":0,"item":item.clone()}),
    )
    .await;
    send_ws_json(
        ws,
        json!({"type":"response.completed","response":{"id":response_id,"status":"completed","output":[item],"usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}}}),
    )
    .await;
}

async fn send_incomplete_ws_response(
    ws: &mut WebSocketStream<TcpStream>,
    response_id: &str,
    message_id: &str,
    text: &str,
) {
    let item = assistant_item(message_id, text);
    send_ws_json(
        ws,
        json!({"type":"response.output_item.done","output_index":0,"item":item}),
    )
    .await;
    send_ws_json(
        ws,
        json!({"type":"response.completed","response":{"id":response_id,"status":"incomplete","incomplete_details":{"reason":"max_output_tokens"},"output":[assistant_item(message_id, text)],"usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}}}),
    )
    .await;
}