sim-lib-openai-server 0.1.2

OpenAI-compatible gateway skeleton for SIM.
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
use std::sync::Arc;

use serde_json::{Map, Value, json};
use sim_kernel::{CapabilitySet, Cx, DefaultFactory, Expr, NoopEvalPolicy};

use crate::{
    clock::{GatewayClock, SystemGatewayClock},
    content_id::content_id_for_expr,
    ids::GatewayIdGenerator,
    objects::{GatewayRequest, GatewayResponse},
    routes::responses::{
        RESPONSES_PATH, ResponseIdGenerators, ResponseRuntimeTargets,
        execute_response_request_with_cache_runners_and_federation,
    },
    runtime::{OpenAiPlanCache, grant_capability_set},
    server::GatewayRouteState,
    storage::{
        GatewayBatch, GatewayBatchCounts, GatewayBatchStatus, GatewayFile, GatewayFileStorageRef,
        GatewayResponseObjectStore, GatewayStateStore, GatewayStore,
    },
};

use super::errors::OpenAiRouteError;

/// Route path for batch creation (`POST /v1/batches`).
pub const BATCHES_PATH: &str = "/v1/batches";
/// Path prefix shared by batch retrieval and cancel routes (`/v1/batches/`).
pub const BATCH_RETRIEVAL_PREFIX: &str = "/v1/batches/";
/// Templated route for retrieving a single batch by id (`/v1/batches/{id}`).
pub const BATCH_RETRIEVAL_ROUTE: &str = "/v1/batches/{id}";
/// Templated route for cancelling a batch (`/v1/batches/{id}/cancel`).
pub const BATCH_CANCEL_ROUTE: &str = "/v1/batches/{id}/cancel";
type RouteResult<T> = std::result::Result<T, OpenAiRouteError>;
struct BatchIdGenerators {
    batch: GatewayIdGenerator,
    file: GatewayIdGenerator,
    response: ResponseIdGenerators,
}
impl BatchIdGenerators {
    fn deterministic(start: u64) -> Self {
        Self {
            batch: GatewayIdGenerator::deterministic("batch", start),
            file: GatewayIdGenerator::deterministic("file", start),
            response: ResponseIdGenerators::deterministic(start),
        }
    }
}
struct BatchItem {
    custom_id: String,
    method: String,
    path: String,
    body: Map<String, Value>,
}

struct BatchRunResult {
    output_records: Vec<Value>,
    error_records: Vec<Value>,
    completed: u64,
    failed: u64,
}

/// Handles `POST /v1/batches`, creating a batch and (unless deferred) running
/// its JSONL items through the responses runtime.
pub fn handle_batches(request: &GatewayRequest, state: &GatewayRouteState) -> GatewayResponse {
    let mut clock = SystemGatewayClock;
    let seed = clock.now_ms().unwrap_or(1);
    let mut ids = BatchIdGenerators::deterministic(seed);
    let capabilities = match state.keys().effective_capabilities(request) {
        Ok(capabilities) => capabilities,
        Err(err) => return OpenAiRouteError::internal(err).into_response(),
    };
    match state.store().lock() {
        Ok(mut store) => create_batch(
            &mut *store,
            &mut ids,
            &mut clock,
            request,
            ResponseRuntimeTargets::with_federation(state.runners(), state.federation()),
            &capabilities,
        )
        .unwrap_or_else(OpenAiRouteError::into_response),
        Err(err) => OpenAiRouteError::internal_message(format!("gateway store lock failed: {err}"))
            .into_response(),
    }
}

/// Handles `GET /v1/batches/{id}`, returning the stored batch object.
pub fn handle_batch_retrieval(
    request: &GatewayRequest,
    state: &GatewayRouteState,
) -> GatewayResponse {
    let Some(batch_id) = batch_id_from_path(request.path()) else {
        return OpenAiRouteError::not_found_kind("batch", request.path()).into_response();
    };
    match state.store().lock() {
        Ok(store) => retrieve_batch(&*store, batch_id),
        Err(err) => OpenAiRouteError::internal_message(format!("gateway store lock failed: {err}"))
            .into_response(),
    }
}

/// Handles `POST /v1/batches/{id}/cancel`, cancelling a queued or in-progress batch.
pub fn handle_batch_cancel(request: &GatewayRequest, state: &GatewayRouteState) -> GatewayResponse {
    let Some(batch_id) = cancel_batch_id_from_path(request.path()) else {
        return OpenAiRouteError::not_found_kind("batch", request.path()).into_response();
    };
    let mut clock = SystemGatewayClock;
    match state.store().lock() {
        Ok(mut store) => cancel_batch(&mut *store, &mut clock, batch_id)
            .unwrap_or_else(OpenAiRouteError::into_response),
        Err(err) => OpenAiRouteError::internal_message(format!("gateway store lock failed: {err}"))
            .into_response(),
    }
}

/// Returns the JSON object for a stored batch, or a not-found error response.
pub fn retrieve_batch<S>(store: &S, batch_id: &str) -> GatewayResponse
where
    S: GatewayStateStore,
{
    store
        .batch(batch_id)
        .map(|batch| GatewayResponse::json(200, batch_json(&batch).to_string().into_bytes()))
        .unwrap_or_else(|| OpenAiRouteError::not_found_kind("batch", batch_id).into_response())
}

fn create_batch<S, C>(
    store: &mut S,
    ids: &mut BatchIdGenerators,
    clock: &mut C,
    request: &GatewayRequest,
    targets: ResponseRuntimeTargets<'_>,
    capabilities: &CapabilitySet,
) -> RouteResult<GatewayResponse>
where
    S: GatewayStore + GatewayResponseObjectStore + GatewayStateStore,
    C: GatewayClock,
{
    let object = request_object(request.body())?;
    let input_file_id = required_string(&object, "input_file_id")?.to_owned();
    let endpoint = required_string(&object, "endpoint")?.to_owned();
    if endpoint != RESPONSES_PATH {
        return Err(OpenAiRouteError::bad_request(
            format!("unsupported batch endpoint: {endpoint}"),
            Some("endpoint"),
            "unsupported_endpoint",
        ));
    }
    let input_bytes = store
        .file_bytes(&input_file_id)
        .ok_or_else(|| OpenAiRouteError::not_found_kind("file", &input_file_id))?;
    let lines = jsonl_lines(&input_bytes)?;
    let created_at_ms = clock.now_ms().map_err(OpenAiRouteError::internal)?;
    let batch = GatewayBatch::new(
        ids.batch.next_id().map_err(OpenAiRouteError::internal)?,
        input_file_id,
        endpoint,
        created_at_ms,
        GatewayBatchCounts::new(lines.len() as u64, 0, 0, 0),
    );
    store
        .put_batch(batch.clone())
        .map_err(OpenAiRouteError::internal)?;
    if object
        .get("defer")
        .and_then(Value::as_bool)
        .unwrap_or(false)
    {
        return Ok(GatewayResponse::json(
            200,
            batch_json(&batch).to_string().into_bytes(),
        ));
    }

    let result = run_batch_items(store, ids, clock, &batch, &lines, targets, capabilities)?;
    let output_file_id = store_jsonl_file(
        store,
        ids,
        clock,
        batch.id(),
        "output",
        "batch_output",
        &result.output_records,
    )?;
    let error_file_id = store_jsonl_file(
        store,
        ids,
        clock,
        batch.id(),
        "errors",
        "batch_error",
        &result.error_records,
    )?;
    let completed = batch.complete(
        output_file_id,
        error_file_id,
        clock.now_ms().map_err(OpenAiRouteError::internal)?,
        GatewayBatchCounts::new(
            result.completed + result.failed,
            result.completed,
            result.failed,
            0,
        ),
    );
    store
        .put_batch(completed.clone())
        .map_err(OpenAiRouteError::internal)?;
    Ok(GatewayResponse::json(
        200,
        batch_json(&completed).to_string().into_bytes(),
    ))
}

fn run_batch_items<S, C>(
    store: &mut S,
    ids: &mut BatchIdGenerators,
    clock: &mut C,
    batch: &GatewayBatch,
    lines: &[(usize, String)],
    targets: ResponseRuntimeTargets<'_>,
    capabilities: &CapabilitySet,
) -> RouteResult<BatchRunResult>
where
    S: GatewayStore + GatewayResponseObjectStore + GatewayStateStore,
    C: GatewayClock,
{
    let (mut cx, seat) = Cx::new_seated(Arc::new(NoopEvalPolicy), Arc::new(DefaultFactory));
    grant_capability_set(&seat, &mut cx, capabilities);
    let mut cache = OpenAiPlanCache::new();
    let mut result = BatchRunResult {
        output_records: Vec::new(),
        error_records: Vec::new(),
        completed: 0,
        failed: 0,
    };
    for (line_number, line) in lines {
        match batch_item(line, batch.endpoint()) {
            Ok(item) => {
                let request = item_request(&item)?;
                let execution = execute_response_request_with_cache_runners_and_federation(
                    &mut cx,
                    store,
                    &mut cache,
                    &mut ids.response,
                    clock,
                    &request,
                    targets,
                );
                let response = execution.response();
                if (200..300).contains(&response.status()) {
                    result.completed += 1;
                    result.output_records.push(output_record(&item, response)?);
                } else {
                    result.failed += 1;
                    result.error_records.push(response_error_record(
                        &item.custom_id,
                        Some(response.status()),
                        response,
                    )?);
                }
            }
            Err(error) => {
                result.failed += 1;
                result
                    .error_records
                    .push(line_error_record(*line_number, error)?);
            }
        }
    }
    Ok(result)
}

fn cancel_batch<S, C>(store: &mut S, clock: &mut C, batch_id: &str) -> RouteResult<GatewayResponse>
where
    S: GatewayStateStore,
    C: GatewayClock,
{
    let batch = store
        .batch(batch_id)
        .ok_or_else(|| OpenAiRouteError::not_found_kind("batch", batch_id))?;
    let batch = if matches!(
        batch.status(),
        GatewayBatchStatus::Queued | GatewayBatchStatus::InProgress
    ) {
        batch.cancel(clock.now_ms().map_err(OpenAiRouteError::internal)?)
    } else {
        batch
    };
    store
        .put_batch(batch.clone())
        .map_err(OpenAiRouteError::internal)?;
    Ok(GatewayResponse::json(
        200,
        batch_json(&batch).to_string().into_bytes(),
    ))
}

use crate::routes::request_json::{request_object, required_string};

fn jsonl_lines(bytes: &[u8]) -> RouteResult<Vec<(usize, String)>> {
    let text = std::str::from_utf8(bytes).map_err(|err| {
        OpenAiRouteError::bad_request(
            format!("batch input file must be UTF-8 JSONL: {err}"),
            Some("input_file_id"),
            "invalid_batch_file",
        )
    })?;
    Ok(text
        .lines()
        .enumerate()
        .filter_map(|(index, line)| {
            let line = line.trim();
            (!line.is_empty()).then(|| (index + 1, line.to_owned()))
        })
        .collect())
}

fn batch_item(line: &str, endpoint: &str) -> RouteResult<BatchItem> {
    let value = serde_json::from_str::<Value>(line).map_err(|err| {
        OpenAiRouteError::invalid_json(format!("invalid JSONL batch line: {err}"))
    })?;
    let object = value.as_object().ok_or_else(|| {
        OpenAiRouteError::bad_request(
            "batch line must be a JSON object",
            None,
            "invalid_batch_line",
        )
    })?;
    let custom_id = required_string(object, "custom_id")?.to_owned();
    let method = required_string(object, "method")?.to_owned();
    if method != "POST" {
        return Err(OpenAiRouteError::bad_request(
            "batch item method must be POST",
            Some("method"),
            "unsupported_method",
        ));
    }
    let path = object
        .get("url")
        .or_else(|| object.get("path"))
        .and_then(Value::as_str)
        .ok_or_else(|| OpenAiRouteError::missing_required("url"))?
        .to_owned();
    if path != endpoint {
        return Err(OpenAiRouteError::bad_request(
            format!("batch item endpoint mismatch: {path}"),
            Some("url"),
            "endpoint_mismatch",
        ));
    }
    let body = object
        .get("body")
        .and_then(Value::as_object)
        .cloned()
        .ok_or_else(|| OpenAiRouteError::missing_required("body"))?;
    Ok(BatchItem {
        custom_id,
        method,
        path,
        body,
    })
}

fn item_request(item: &BatchItem) -> RouteResult<GatewayRequest> {
    let mut body = item.body.clone();
    body.insert("store".to_owned(), Value::Bool(true));
    body.insert("stream".to_owned(), Value::Bool(false));
    let body = serde_json::to_vec(&Value::Object(body)).map_err(|err| {
        OpenAiRouteError::internal_message(format!("failed to encode batch item body: {err}"))
    })?;
    Ok(GatewayRequest::new(
        item.method.clone(),
        item.path.clone(),
        vec![("Content-Type".to_owned(), "application/json".to_owned())],
        body,
    ))
}

fn output_record(item: &BatchItem, response: &GatewayResponse) -> RouteResult<Value> {
    Ok(json!({
        "id": item.custom_id,
        "custom_id": item.custom_id,
        "response": {
            "status_code": response.status(),
            "body": response_json(response)?,
        },
    }))
}

fn response_error_record(
    custom_id: &str,
    status: Option<u16>,
    response: &GatewayResponse,
) -> RouteResult<Value> {
    Ok(json!({
        "custom_id": custom_id,
        "response": status.map(|status| json!({ "status_code": status })),
        "error": response_json(response)?["error"].clone(),
    }))
}

fn line_error_record(line_number: usize, error: OpenAiRouteError) -> RouteResult<Value> {
    let response = error.into_response();
    Ok(json!({
        "line": line_number,
        "error": response_json(&response)?["error"].clone(),
    }))
}

fn response_json(response: &GatewayResponse) -> RouteResult<Value> {
    serde_json::from_slice(response.body()).map_err(|err| {
        OpenAiRouteError::internal_message(format!("gateway response body is not JSON: {err}"))
    })
}

fn store_jsonl_file<S, C>(
    store: &mut S,
    ids: &mut BatchIdGenerators,
    clock: &mut C,
    batch_id: &str,
    suffix: &str,
    purpose: &str,
    records: &[Value],
) -> RouteResult<Option<String>>
where
    S: GatewayStateStore,
    C: GatewayClock,
{
    if records.is_empty() {
        return Ok(None);
    }
    let mut text = records
        .iter()
        .map(Value::to_string)
        .collect::<Vec<_>>()
        .join("\n");
    text.push('\n');
    let bytes = text.into_bytes();
    let content_id =
        content_id_for_expr(&Expr::Bytes(bytes.clone())).map_err(OpenAiRouteError::internal)?;
    let file = GatewayFile::new(
        ids.file.next_id().map_err(OpenAiRouteError::internal)?,
        format!("{batch_id}-{suffix}.jsonl"),
        purpose,
        bytes.len() as u64,
        clock.now_ms().map_err(OpenAiRouteError::internal)?,
        GatewayFileStorageRef::memory(content_id),
    );
    let file_id = file.id().to_owned();
    store
        .put_file(file, bytes)
        .map_err(OpenAiRouteError::internal)?;
    Ok(Some(file_id))
}

fn batch_id_from_path(path: &str) -> Option<&str> {
    super::path::id_from_path(path, BATCH_RETRIEVAL_PREFIX)
}

fn cancel_batch_id_from_path(path: &str) -> Option<&str> {
    super::path::id_from_path_with_suffix(path, BATCH_RETRIEVAL_PREFIX, "/cancel")
}

fn batch_json(batch: &GatewayBatch) -> Value {
    json!({
        "id": batch.id(),
        "object": "batch",
        "endpoint": batch.endpoint(),
        "input_file_id": batch.input_file_id(),
        "status": batch.status().as_str(),
        "output_file_id": batch.output_file_id(),
        "error_file_id": batch.error_file_id(),
        "created_at": batch.created_at_ms(),
        "completed_at": batch.completed_at_ms(),
        "cancelled_at": batch.cancelled_at_ms(),
        "request_counts": counts_json(batch.request_counts()),
    })
}

fn counts_json(counts: GatewayBatchCounts) -> Value {
    json!({
        "total": counts.total(),
        "completed": counts.completed(),
        "failed": counts.failed(),
        "cancelled": counts.cancelled(),
    })
}