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
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
use sim_kernel::{ContentId, Expr, Result, Symbol};

use crate::objects::{GatewayResponse, content_id_expr};

use super::vector::GatewayVectorStore;

/// Record kind tag designating a gateway file.
pub const GATEWAY_FILE_KIND: &str = "openai-gateway/file";
/// Record kind tag designating a gateway batch.
pub const GATEWAY_BATCH_KIND: &str = "openai-gateway/batch";
/// Record kind tag designating a gateway thread.
pub const GATEWAY_THREAD_KIND: &str = "openai-gateway/thread";
/// Record kind tag designating a gateway thread message.
pub const GATEWAY_THREAD_MESSAGE_KIND: &str = "openai-gateway/thread-message";

/// A stored response object linking a response id to its content-addressed
/// [`GatewayResponse`] and the records that produced it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StoredGatewayResponse {
    response_id: String,
    content_id: ContentId,
    response: GatewayResponse,
    pub(crate) request_content_id: Option<ContentId>,
    pub(crate) run_content_id: Option<ContentId>,
    pub(crate) event_content_ids: Vec<ContentId>,
    pub(crate) parent_response_id: Option<String>,
}

impl StoredGatewayResponse {
    /// Creates a stored response with no linked request, run, events, or parent.
    pub fn new(
        response_id: impl Into<String>,
        content_id: ContentId,
        response: GatewayResponse,
    ) -> Self {
        Self {
            response_id: response_id.into(),
            content_id,
            response,
            request_content_id: None,
            run_content_id: None,
            event_content_ids: Vec::new(),
            parent_response_id: None,
        }
    }

    /// Returns the public response identifier.
    pub fn response_id(&self) -> &str {
        &self.response_id
    }

    /// Returns the content id of the stored [`GatewayResponse`].
    pub fn content_id(&self) -> &ContentId {
        &self.content_id
    }

    /// Returns the stored response value.
    pub fn response(&self) -> &GatewayResponse {
        &self.response
    }
}

/// Store for response objects keyed by their public response id.
pub trait GatewayResponseObjectStore {
    /// Stores a response object, replacing any existing entry with the same id.
    fn put_response_object(&mut self, response: StoredGatewayResponse) -> Result<()>;
    /// Returns the response object with the given id, if present.
    fn response_object(&self, response_id: &str) -> Option<StoredGatewayResponse>;
}

/// Lifecycle status of a [`GatewayBatch`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum GatewayBatchStatus {
    /// The batch has been accepted but not yet started.
    Queued,
    /// The batch is currently being processed.
    InProgress,
    /// The batch finished processing.
    Completed,
    /// The batch was cancelled before completion.
    Cancelled,
}

impl GatewayBatchStatus {
    /// Returns the OpenAI wire string for this status (e.g. `"in_progress"`).
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Queued => "queued",
            Self::InProgress => "in_progress",
            Self::Completed => "completed",
            Self::Cancelled => "cancelled",
        }
    }
}

/// Per-request tallies for a [`GatewayBatch`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct GatewayBatchCounts {
    total: u64,
    completed: u64,
    failed: u64,
    cancelled: u64,
}

impl GatewayBatchCounts {
    /// Creates a counts record from the total, completed, failed, and cancelled tallies.
    pub fn new(total: u64, completed: u64, failed: u64, cancelled: u64) -> Self {
        Self {
            total,
            completed,
            failed,
            cancelled,
        }
    }

    /// Returns the total number of requests in the batch.
    pub fn total(&self) -> u64 {
        self.total
    }

    /// Returns the number of completed requests.
    pub fn completed(&self) -> u64 {
        self.completed
    }

    /// Returns the number of failed requests.
    pub fn failed(&self) -> u64 {
        self.failed
    }

    /// Returns the number of cancelled requests.
    pub fn cancelled(&self) -> u64 {
        self.cancelled
    }

    /// Encodes the counts as a SIM [`Expr`] map record.
    pub fn to_expr(self) -> Expr {
        Expr::Map(vec![
            field("total", Expr::String(self.total.to_string())),
            field("completed", Expr::String(self.completed.to_string())),
            field("failed", Expr::String(self.failed.to_string())),
            field("cancelled", Expr::String(self.cancelled.to_string())),
        ])
    }
}

/// A gateway batch job over an input file, with status and per-request counts.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GatewayBatch {
    id: String,
    input_file_id: String,
    endpoint: String,
    status: GatewayBatchStatus,
    output_file_id: Option<String>,
    error_file_id: Option<String>,
    created_at_ms: u64,
    completed_at_ms: Option<u64>,
    cancelled_at_ms: Option<u64>,
    request_counts: GatewayBatchCounts,
}

impl GatewayBatch {
    /// Creates a new batch in the [`GatewayBatchStatus::Queued`] state.
    pub fn new(
        id: impl Into<String>,
        input_file_id: impl Into<String>,
        endpoint: impl Into<String>,
        created_at_ms: u64,
        request_counts: GatewayBatchCounts,
    ) -> Self {
        Self {
            id: id.into(),
            input_file_id: input_file_id.into(),
            endpoint: endpoint.into(),
            status: GatewayBatchStatus::Queued,
            output_file_id: None,
            error_file_id: None,
            created_at_ms,
            completed_at_ms: None,
            cancelled_at_ms: None,
            request_counts,
        }
    }

    /// Returns the batch transitioned to [`GatewayBatchStatus::Completed`] with
    /// the given output/error files, completion time, and final counts.
    pub fn complete(
        mut self,
        output_file_id: Option<String>,
        error_file_id: Option<String>,
        completed_at_ms: u64,
        request_counts: GatewayBatchCounts,
    ) -> Self {
        self.status = GatewayBatchStatus::Completed;
        self.output_file_id = output_file_id;
        self.error_file_id = error_file_id;
        self.completed_at_ms = Some(completed_at_ms);
        self.request_counts = request_counts;
        self
    }

    /// Returns the batch transitioned to [`GatewayBatchStatus::Cancelled`],
    /// counting any still-queued requests as cancelled.
    pub fn cancel(mut self, cancelled_at_ms: u64) -> Self {
        let queued = self.request_counts.total.saturating_sub(
            self.request_counts.completed
                + self.request_counts.failed
                + self.request_counts.cancelled,
        );
        self.status = GatewayBatchStatus::Cancelled;
        self.cancelled_at_ms = Some(cancelled_at_ms);
        self.request_counts.cancelled += queued;
        self
    }

    /// Returns the batch identifier.
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Returns the id of the input file backing this batch.
    pub fn input_file_id(&self) -> &str {
        &self.input_file_id
    }

    /// Returns the target API endpoint for the batched requests.
    pub fn endpoint(&self) -> &str {
        &self.endpoint
    }

    /// Returns the current lifecycle status.
    pub fn status(&self) -> &GatewayBatchStatus {
        &self.status
    }

    /// Returns the output file id, once the batch has produced one.
    pub fn output_file_id(&self) -> Option<&str> {
        self.output_file_id.as_deref()
    }

    /// Returns the error file id, if the batch produced one.
    pub fn error_file_id(&self) -> Option<&str> {
        self.error_file_id.as_deref()
    }

    /// Returns the creation timestamp in milliseconds since the Unix epoch.
    pub fn created_at_ms(&self) -> u64 {
        self.created_at_ms
    }

    /// Returns the completion timestamp in milliseconds, if completed.
    pub fn completed_at_ms(&self) -> Option<u64> {
        self.completed_at_ms
    }

    /// Returns the cancellation timestamp in milliseconds, if cancelled.
    pub fn cancelled_at_ms(&self) -> Option<u64> {
        self.cancelled_at_ms
    }

    /// Returns the current per-request counts.
    pub fn request_counts(&self) -> GatewayBatchCounts {
        self.request_counts
    }

    /// Encodes the batch as a SIM [`Expr`] map record.
    pub fn to_expr(&self) -> Expr {
        Expr::Map(vec![
            field("kind", Expr::String(GATEWAY_BATCH_KIND.to_owned())),
            field("id", Expr::String(self.id.clone())),
            field("input-file-id", Expr::String(self.input_file_id.clone())),
            field("endpoint", Expr::String(self.endpoint.clone())),
            field("status", Expr::Symbol(Symbol::new(self.status.as_str()))),
            optional_string_field("output-file-id", self.output_file_id.as_deref()),
            optional_string_field("error-file-id", self.error_file_id.as_deref()),
            field(
                "created-at-ms",
                Expr::String(self.created_at_ms.to_string()),
            ),
            optional_u64_field("completed-at-ms", self.completed_at_ms),
            optional_u64_field("cancelled-at-ms", self.cancelled_at_ms),
            field("request-counts", self.request_counts.to_expr()),
        ])
    }
}

/// Where a [`GatewayFile`]'s bytes live.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum GatewayFileStorageRef {
    /// Bytes held in an in-memory content-addressed blob.
    Memory {
        /// Content id of the stored bytes.
        content_id: ContentId,
    },
    /// Bytes held on the table-backed filesystem at the given path.
    TableFs {
        /// Filesystem path of the stored bytes.
        path: String,
    },
}

impl GatewayFileStorageRef {
    /// Creates a [`GatewayFileStorageRef::Memory`] reference for `content_id`.
    pub fn memory(content_id: ContentId) -> Self {
        Self::Memory { content_id }
    }

    /// Creates a [`GatewayFileStorageRef::TableFs`] reference for `path`.
    pub fn table_fs(path: impl Into<String>) -> Self {
        Self::TableFs { path: path.into() }
    }

    /// Encodes the storage reference as a SIM [`Expr`] map record.
    pub fn to_expr(&self) -> Expr {
        match self {
            Self::Memory { content_id } => Expr::Map(vec![
                field("kind", Expr::Symbol(Symbol::new("memory"))),
                field("content-id", content_id_expr(content_id)),
            ]),
            Self::TableFs { path } => Expr::Map(vec![
                field("kind", Expr::Symbol(Symbol::new("table-fs"))),
                field("path", Expr::String(path.clone())),
            ]),
        }
    }
}

/// A gateway file record: its metadata plus a reference to where its bytes live.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GatewayFile {
    id: String,
    filename: String,
    purpose: String,
    bytes: u64,
    created_at_ms: u64,
    storage_ref: GatewayFileStorageRef,
}

impl GatewayFile {
    /// Creates a file record with the given metadata and storage reference.
    pub fn new(
        id: impl Into<String>,
        filename: impl Into<String>,
        purpose: impl Into<String>,
        bytes: u64,
        created_at_ms: u64,
        storage_ref: GatewayFileStorageRef,
    ) -> Self {
        Self {
            id: id.into(),
            filename: filename.into(),
            purpose: purpose.into(),
            bytes,
            created_at_ms,
            storage_ref,
        }
    }

    /// Returns the file identifier.
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Returns the original filename.
    pub fn filename(&self) -> &str {
        &self.filename
    }

    /// Returns the declared purpose of the file (e.g. `"batch"`, `"assistants"`).
    pub fn purpose(&self) -> &str {
        &self.purpose
    }

    /// Returns the file size in bytes.
    pub fn bytes(&self) -> u64 {
        self.bytes
    }

    /// Returns the creation timestamp in milliseconds since the Unix epoch.
    pub fn created_at_ms(&self) -> u64 {
        self.created_at_ms
    }

    /// Returns a reference to where the file's bytes are stored.
    pub fn storage_ref(&self) -> &GatewayFileStorageRef {
        &self.storage_ref
    }

    /// Encodes the file record as a SIM [`Expr`] map record.
    pub fn to_expr(&self) -> Expr {
        Expr::Map(vec![
            field("kind", Expr::String(GATEWAY_FILE_KIND.to_owned())),
            field("id", Expr::String(self.id.clone())),
            field("filename", Expr::String(self.filename.clone())),
            field("purpose", Expr::String(self.purpose.clone())),
            field("bytes", Expr::String(self.bytes.to_string())),
            field(
                "created-at-ms",
                Expr::String(self.created_at_ms.to_string()),
            ),
            field("storage-ref", self.storage_ref.to_expr()),
        ])
    }
}

/// A gateway thread record with creation time and key/value metadata.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GatewayThread {
    id: String,
    created_at_ms: u64,
    metadata: Vec<(String, String)>,
}

impl GatewayThread {
    /// Creates a thread record with the given id, creation time, and metadata.
    pub fn new(id: impl Into<String>, created_at_ms: u64, metadata: Vec<(String, String)>) -> Self {
        Self {
            id: id.into(),
            created_at_ms,
            metadata,
        }
    }

    /// Returns the thread identifier.
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Returns the creation timestamp in milliseconds since the Unix epoch.
    pub fn created_at_ms(&self) -> u64 {
        self.created_at_ms
    }

    /// Returns the thread's key/value metadata pairs.
    pub fn metadata(&self) -> &[(String, String)] {
        &self.metadata
    }

    /// Encodes the thread record as a SIM [`Expr`] map record.
    pub fn to_expr(&self) -> Expr {
        Expr::Map(vec![
            field("kind", Expr::String(GATEWAY_THREAD_KIND.to_owned())),
            field("id", Expr::String(self.id.clone())),
            field(
                "created-at-ms",
                Expr::String(self.created_at_ms.to_string()),
            ),
            field("metadata", metadata_expr(&self.metadata)),
        ])
    }
}

/// A single message belonging to a [`GatewayThread`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GatewayThreadMessage {
    id: String,
    thread_id: String,
    role: String,
    content: String,
    created_at_ms: u64,
}

impl GatewayThreadMessage {
    /// Creates a thread message with the given id, thread, role, content, and time.
    pub fn new(
        id: impl Into<String>,
        thread_id: impl Into<String>,
        role: impl Into<String>,
        content: impl Into<String>,
        created_at_ms: u64,
    ) -> Self {
        Self {
            id: id.into(),
            thread_id: thread_id.into(),
            role: role.into(),
            content: content.into(),
            created_at_ms,
        }
    }

    /// Returns the message identifier.
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Returns the id of the thread this message belongs to.
    pub fn thread_id(&self) -> &str {
        &self.thread_id
    }

    /// Returns the message role (e.g. `"user"`, `"assistant"`).
    pub fn role(&self) -> &str {
        &self.role
    }

    /// Returns the message text content.
    pub fn content(&self) -> &str {
        &self.content
    }

    /// Returns the creation timestamp in milliseconds since the Unix epoch.
    pub fn created_at_ms(&self) -> u64 {
        self.created_at_ms
    }

    /// Encodes the thread message as a SIM [`Expr`] map record.
    pub fn to_expr(&self) -> Expr {
        Expr::Map(vec![
            field("kind", Expr::String(GATEWAY_THREAD_MESSAGE_KIND.to_owned())),
            field("id", Expr::String(self.id.clone())),
            field("thread-id", Expr::String(self.thread_id.clone())),
            field("role", Expr::String(self.role.clone())),
            field("content", Expr::String(self.content.clone())),
            field(
                "created-at-ms",
                Expr::String(self.created_at_ms.to_string()),
            ),
        ])
    }
}

/// Store for the gateway's durable account state: files, batches, threads,
/// thread messages, and vector stores, all keyed by their string ids.
pub trait GatewayStateStore {
    /// Stores a file record together with its raw bytes.
    fn put_file(&mut self, file: GatewayFile, bytes: Vec<u8>) -> Result<()>;
    /// Returns the file record with the given id, if present.
    fn file(&self, file_id: &str) -> Option<GatewayFile>;
    /// Returns the raw bytes for the given file id, if present.
    fn file_bytes(&self, file_id: &str) -> Option<Vec<u8>>;

    /// Stores a batch record, replacing any existing entry with the same id.
    fn put_batch(&mut self, batch: GatewayBatch) -> Result<()>;
    /// Returns the batch with the given id, if present.
    fn batch(&self, batch_id: &str) -> Option<GatewayBatch>;

    /// Stores a thread record, replacing any existing entry with the same id.
    fn put_thread(&mut self, thread: GatewayThread) -> Result<()>;
    /// Returns the thread with the given id, if present.
    fn thread(&self, thread_id: &str) -> Option<GatewayThread>;

    /// Appends a message to its thread.
    fn put_thread_message(&mut self, message: GatewayThreadMessage) -> Result<()>;
    /// Returns the messages for the given thread id, in insertion order.
    fn thread_messages(&self, thread_id: &str) -> Vec<GatewayThreadMessage>;

    /// Stores a vector store, replacing any existing entry with the same id.
    fn put_vector_store(&mut self, vector_store: GatewayVectorStore) -> Result<()>;
    /// Returns the vector store with the given id, if present.
    fn vector_store(&self, vector_store_id: &str) -> Option<GatewayVectorStore>;
}

fn metadata_expr(metadata: &[(String, String)]) -> Expr {
    Expr::Map(
        metadata
            .iter()
            .map(|(key, value)| (Expr::String(key.clone()), Expr::String(value.clone())))
            .collect(),
    )
}

use sim_value::build::entry as field;

fn optional_string_field(name: &str, value: Option<&str>) -> (Expr, Expr) {
    field(
        name,
        value
            .map(|value| Expr::String(value.to_owned()))
            .unwrap_or(Expr::Nil),
    )
}

fn optional_u64_field(name: &str, value: Option<u64>) -> (Expr, Expr) {
    field(
        name,
        value
            .map(|value| Expr::String(value.to_string()))
            .unwrap_or(Expr::Nil),
    )
}