sim-lib-openai-server 0.1.5

OpenAI-shaped gateway routes and fixture media surfaces 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
use std::sync::{Arc, Mutex};

use sim_kernel::{
    ContentId, Cx, DefaultFactory, Error, Expr, Factory, NoopEvalPolicy, Object, ObjectCompat,
    Result, Symbol, Table, Value,
};

use crate::{
    objects::{
        GatewayEvent, GatewayRequest, GatewayResponse, GatewayRun, content_id_expr, content_id_hex,
    },
    storage::{
        GatewayBatch, GatewayFile, GatewayResponseObjectStore, GatewayStateStore, GatewayStore,
        GatewayThread, GatewayThreadMessage, GatewayVectorStore, StoredGatewayResponse,
    },
};

/// Gateway store that keeps every record kind in its own SIM [`Table`].
///
/// Each slot is a SIM table value guarded by a shared [`Cx`]; records are
/// wrapped as opaque table values keyed by content id or string id. Implements
/// [`GatewayStore`], [`GatewayResponseObjectStore`], and [`GatewayStateStore`].
pub struct TableGatewayStore {
    cx: Mutex<Cx>,
    requests: Value,
    runs: Value,
    events: Value,
    responses: Value,
    response_objects: Value,
    files: Value,
    file_bytes: Value,
    batches: Value,
    threads: Value,
    thread_messages: Value,
    vector_stores: Value,
}

impl TableGatewayStore {
    /// Creates a store with a fresh table for every record kind.
    ///
    /// Returns an error only if SIM table creation fails, which does not happen
    /// for the in-memory tables used here.
    pub fn new() -> Result<Self> {
        let cx = Mutex::new(Cx::new(Arc::new(NoopEvalPolicy), Arc::new(DefaultFactory)));
        Ok(Self {
            requests: new_table()?,
            runs: new_table()?,
            events: new_table()?,
            responses: new_table()?,
            response_objects: new_table()?,
            files: new_table()?,
            file_bytes: new_table()?,
            batches: new_table()?,
            threads: new_table()?,
            thread_messages: new_table()?,
            vector_stores: new_table()?,
            cx,
        })
    }

    fn set<T>(&self, table: &Value, key: Symbol, value: T) -> Result<()>
    where
        T: TableStored,
    {
        let mut cx = self.cx()?;
        let stored = cx.factory().opaque(Arc::new(StoredTableValue(value)))?;
        table_impl(table)?.set(&mut cx, key, stored)
    }

    fn get<T>(&self, table: &Value, key: Symbol) -> Option<T>
    where
        T: TableStored,
    {
        let mut cx = self.cx().ok()?;
        let value = table_impl(table).ok()?.get(&mut cx, key).ok()?;
        value
            .object()
            .downcast_ref::<StoredTableValue<T>>()
            .map(|stored| stored.0.clone())
    }

    fn cx(&self) -> Result<std::sync::MutexGuard<'_, Cx>> {
        self.cx
            .lock()
            .map_err(|_| Error::PoisonedLock("openai gateway table store"))
    }
}

impl Default for TableGatewayStore {
    fn default() -> Self {
        Self::new().expect("in-memory SIM table creation is infallible")
    }
}

impl GatewayStore for TableGatewayStore {
    fn put_request(&mut self, id: ContentId, request: GatewayRequest) -> Result<()> {
        self.set(&self.requests, content_key(&id), request)
    }

    fn request(&self, id: &ContentId) -> Option<GatewayRequest> {
        self.get(&self.requests, content_key(id))
    }

    fn put_run(&mut self, id: ContentId, run: GatewayRun) -> Result<()> {
        self.set(&self.runs, content_key(&id), run)
    }

    fn run(&self, id: &ContentId) -> Option<GatewayRun> {
        self.get(&self.runs, content_key(id))
    }

    fn put_event(&mut self, id: ContentId, event: GatewayEvent) -> Result<()> {
        self.set(&self.events, content_key(&id), event)
    }

    fn event(&self, id: &ContentId) -> Option<GatewayEvent> {
        self.get(&self.events, content_key(id))
    }

    fn put_response(&mut self, id: ContentId, response: GatewayResponse) -> Result<()> {
        self.set(&self.responses, content_key(&id), response)
    }

    fn response(&self, id: &ContentId) -> Option<GatewayResponse> {
        self.get(&self.responses, content_key(id))
    }
}

impl GatewayResponseObjectStore for TableGatewayStore {
    fn put_response_object(&mut self, response: StoredGatewayResponse) -> Result<()> {
        self.set(
            &self.responses,
            content_key(response.content_id()),
            response.response().clone(),
        )?;
        self.set(
            &self.response_objects,
            Symbol::new(response.response_id()),
            response,
        )
    }

    fn response_object(&self, response_id: &str) -> Option<StoredGatewayResponse> {
        self.get(&self.response_objects, Symbol::new(response_id))
    }
}

impl GatewayStateStore for TableGatewayStore {
    fn put_file(&mut self, file: GatewayFile, bytes: Vec<u8>) -> Result<()> {
        self.set(&self.file_bytes, Symbol::new(file.id()), bytes)?;
        self.set(&self.files, Symbol::new(file.id()), file)
    }

    fn file(&self, file_id: &str) -> Option<GatewayFile> {
        self.get(&self.files, Symbol::new(file_id))
    }

    fn file_bytes(&self, file_id: &str) -> Option<Vec<u8>> {
        self.get(&self.file_bytes, Symbol::new(file_id))
    }

    fn put_batch(&mut self, batch: GatewayBatch) -> Result<()> {
        self.set(&self.batches, Symbol::new(batch.id()), batch)
    }

    fn batch(&self, batch_id: &str) -> Option<GatewayBatch> {
        self.get(&self.batches, Symbol::new(batch_id))
    }

    fn put_thread(&mut self, thread: GatewayThread) -> Result<()> {
        self.set(&self.threads, Symbol::new(thread.id()), thread)
    }

    fn thread(&self, thread_id: &str) -> Option<GatewayThread> {
        self.get(&self.threads, Symbol::new(thread_id))
    }

    fn put_thread_message(&mut self, message: GatewayThreadMessage) -> Result<()> {
        let key = Symbol::new(message.thread_id());
        let mut messages = self.thread_messages(message.thread_id());
        messages.push(message);
        self.set(&self.thread_messages, key, ThreadMessages(messages))
    }

    fn thread_messages(&self, thread_id: &str) -> Vec<GatewayThreadMessage> {
        self.get::<ThreadMessages>(&self.thread_messages, Symbol::new(thread_id))
            .map(|messages| messages.0)
            .unwrap_or_default()
    }

    fn put_vector_store(&mut self, vector_store: GatewayVectorStore) -> Result<()> {
        self.set(
            &self.vector_stores,
            Symbol::new(vector_store.id()),
            vector_store,
        )
    }

    fn vector_store(&self, vector_store_id: &str) -> Option<GatewayVectorStore> {
        self.get(&self.vector_stores, Symbol::new(vector_store_id))
    }
}

trait TableStored: Clone + Send + Sync + 'static {
    fn label() -> &'static str;
    fn to_expr(&self) -> Expr;
}

#[sim_citizen_derive::non_citizen(
    reason = "OpenAI table storage wrapper; class-backed descriptor is the wrapped openai/* value",
    kind = "marker",
    descriptor = "openai/TableStored"
)]
#[derive(Clone)]
struct StoredTableValue<T: TableStored>(T);

impl<T: TableStored> Object for StoredTableValue<T> {
    fn display(&self, _cx: &mut Cx) -> Result<String> {
        Ok(format!("#<{}>", T::label()))
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

impl<T: TableStored> ObjectCompat for StoredTableValue<T> {
    fn as_expr(&self, _cx: &mut Cx) -> Result<Expr> {
        Ok(self.0.to_expr())
    }
}

#[derive(Clone)]
struct ThreadMessages(Vec<GatewayThreadMessage>);

impl TableStored for GatewayRequest {
    fn label() -> &'static str {
        "openai-gateway-table-request"
    }

    fn to_expr(&self) -> Expr {
        self.to_expr()
    }
}

impl TableStored for GatewayRun {
    fn label() -> &'static str {
        "openai-gateway-table-run"
    }

    fn to_expr(&self) -> Expr {
        self.to_expr()
    }
}

impl TableStored for GatewayEvent {
    fn label() -> &'static str {
        "openai-gateway-table-event"
    }

    fn to_expr(&self) -> Expr {
        self.to_expr()
    }
}

impl TableStored for GatewayResponse {
    fn label() -> &'static str {
        "openai-gateway-table-response"
    }

    fn to_expr(&self) -> Expr {
        self.to_expr()
    }
}

impl TableStored for StoredGatewayResponse {
    fn label() -> &'static str {
        "openai-gateway-table-stored-response"
    }

    fn to_expr(&self) -> Expr {
        Expr::Map(vec![
            field("response-id", Expr::String(self.response_id().to_owned())),
            field("content-id", content_id_expr(self.content_id())),
            field("response", self.response().to_expr()),
            optional_content_id_field("request-content-id", self.request_content_id.as_ref()),
            optional_content_id_field("run-content-id", self.run_content_id.as_ref()),
            field(
                "event-content-ids",
                Expr::Vector(
                    self.event_content_ids
                        .iter()
                        .map(content_id_expr)
                        .collect::<Vec<_>>(),
                ),
            ),
            field(
                "parent-response-id",
                self.parent_response_id
                    .as_ref()
                    .map(|id| Expr::String(id.clone()))
                    .unwrap_or(Expr::Nil),
            ),
            field(
                "owner-key-id",
                self.owner_key_id
                    .as_ref()
                    .map(|id| Expr::String(id.clone()))
                    .unwrap_or(Expr::Nil),
            ),
        ])
    }
}

impl TableStored for GatewayFile {
    fn label() -> &'static str {
        "openai-gateway-table-file"
    }

    fn to_expr(&self) -> Expr {
        self.to_expr()
    }
}

impl TableStored for Vec<u8> {
    fn label() -> &'static str {
        "openai-gateway-table-bytes"
    }

    fn to_expr(&self) -> Expr {
        Expr::Bytes(self.clone())
    }
}

impl TableStored for GatewayBatch {
    fn label() -> &'static str {
        "openai-gateway-table-batch"
    }

    fn to_expr(&self) -> Expr {
        self.to_expr()
    }
}

impl TableStored for GatewayThread {
    fn label() -> &'static str {
        "openai-gateway-table-thread"
    }

    fn to_expr(&self) -> Expr {
        self.to_expr()
    }
}

impl TableStored for GatewayThreadMessage {
    fn label() -> &'static str {
        "openai-gateway-table-thread-message"
    }

    fn to_expr(&self) -> Expr {
        self.to_expr()
    }
}

impl TableStored for GatewayVectorStore {
    fn label() -> &'static str {
        "openai-gateway-table-vector-store"
    }

    fn to_expr(&self) -> Expr {
        self.to_expr()
    }
}

impl TableStored for ThreadMessages {
    fn label() -> &'static str {
        "openai-gateway-table-thread-messages"
    }

    fn to_expr(&self) -> Expr {
        Expr::Vector(
            self.0
                .iter()
                .map(GatewayThreadMessage::to_expr)
                .collect::<Vec<_>>(),
        )
    }
}

fn new_table() -> Result<Value> {
    DefaultFactory.table(Vec::new())
}

fn table_impl(value: &Value) -> Result<&dyn Table> {
    value
        .object()
        .as_table_impl()
        .ok_or_else(|| Error::Eval("gateway table store slot is not a table".to_owned()))
}

fn content_key(id: &ContentId) -> Symbol {
    Symbol::new(content_id_hex(id))
}

use sim_value::build::entry as field;

fn optional_content_id_field(name: &str, value: Option<&ContentId>) -> (Expr, Expr) {
    field(name, value.map(content_id_expr).unwrap_or(Expr::Nil))
}