oxillama-server 0.1.3

OpenAI-compatible HTTP API server for OxiLLaMa
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
//! Background run worker for the Assistants API.
//!
//! The worker loops over the `RunQueueReceiver`, processing one run at a time:
//!
//! 1. Transitions the run to `InProgress`.
//! 2. Reads the thread's messages and formats them as a prompt using the same
//!    chat-template logic as `routes/chat.rs`.
//! 3. Sends a `BatchRequest::Generate` to the inference engine via the shared
//!    queue in `AppState`.
//! 4. Appends the assistant's response as a new `ThreadMessage`.
//! 5. Transitions the run to `Completed`.
//!
//! On any error, the run is transitioned to `Failed` with a descriptive error
//! record attached.

use std::sync::Arc;

use tracing::{debug, error, info, warn};

use crate::queue::BatchRequest;
use crate::state::AppState;
use crate::threads::queue::RunQueueReceiver;
use crate::threads::store::ThreadStore;
use crate::threads::stream::{RunEvent, RunEventSender};
use crate::threads::types::{
    MessageRole, Run, RunError, RunStatus, RunStep, RunStepStatus, ThreadMessage,
};
use oxillama_runtime::sampling::SamplerConfig;

/// Spawn the background run worker task.
///
/// The worker runs for the lifetime of the server; it stops when the queue's
/// sender side is dropped (server shutdown).
pub fn spawn_run_worker(store: Arc<ThreadStore>, mut rx: RunQueueReceiver, state: Arc<AppState>) {
    tokio::spawn(async move {
        info!("assistants run worker started");
        while let Some(item) = rx.recv().await {
            let thread_id = item.thread_id.clone();
            let run_id = item.run_id.clone();
            debug!(thread_id, run_id, "run worker picked up item");

            let event_tx = state.run_event_tx_broadcast.clone();

            let result = process_run(
                &item.thread_id,
                &item.run_id,
                item.instructions.as_deref(),
                item.max_tokens,
                &store,
                &state,
                event_tx.as_ref(),
            )
            .await;

            if let Err(e) = result {
                error!(thread_id, run_id, error = %e, "run processing failed");
                // Best-effort: mark the run as failed.
                let store_c = Arc::clone(&store);
                let tid = item.thread_id.clone();
                let rid = item.run_id.clone();
                let err_msg = e.clone();

                // Broadcast failure event if we have a channel.
                if let Some(ref tx) = state.run_event_tx_broadcast {
                    // Attempt to get the run for the broadcast.
                    let store_for_event = Arc::clone(&store);
                    let tid_ev = tid.clone();
                    let rid_ev = rid.clone();
                    if let Some(run) = tokio::task::spawn_blocking(move || {
                        store_for_event.get_run(&tid_ev, &rid_ev)
                    })
                    .await
                    .ok()
                    .and_then(|r| r.ok())
                    {
                        let _ = tx.send(RunEvent::Failed(run));
                    }
                }

                tokio::task::spawn_blocking(move || {
                    let _ = store_c.force_update_run_status(
                        &tid,
                        &rid,
                        RunStatus::Failed,
                        Some(RunError {
                            code: "server_error".to_string(),
                            message: err_msg,
                        }),
                    );
                })
                .await
                .ok();
            }
        }
        info!("assistants run worker queue closed — exiting");
    });
}

/// Broadcast a `RunEvent` if the sender is available (best-effort; ignores lag).
fn maybe_broadcast(event_tx: Option<&RunEventSender>, event: RunEvent) {
    if let Some(tx) = event_tx {
        let _ = tx.send(event);
    }
}

/// Process a single run end-to-end.
///
/// `event_tx` is an optional broadcast sender for run lifecycle events.
/// When `Some`, lifecycle events are published for SSE streaming consumers.
async fn process_run(
    thread_id: &str,
    run_id: &str,
    instructions: Option<&str>,
    max_tokens: usize,
    store: &Arc<ThreadStore>,
    state: &Arc<AppState>,
    event_tx: Option<&RunEventSender>,
) -> Result<(), String> {
    // Step 1 — transition to InProgress.
    {
        let store_c = Arc::clone(store);
        let tid = thread_id.to_string();
        let rid = run_id.to_string();
        tokio::task::spawn_blocking(move || {
            store_c.update_run_status(&tid, &rid, RunStatus::InProgress, None)
        })
        .await
        .map_err(|e| format!("spawn_blocking join: {e}"))?
        .map_err(|e| format!("update InProgress: {e}"))?;
    }

    // Broadcast InProgress event.
    if event_tx.is_some() {
        let store_c = Arc::clone(store);
        let tid = thread_id.to_string();
        let rid = run_id.to_string();
        if let Ok(Ok(run)) = tokio::task::spawn_blocking(move || store_c.get_run(&tid, &rid)).await
        {
            maybe_broadcast(event_tx, RunEvent::InProgress(run));
        }
    }

    // Step 2 — read messages and format prompt.
    let messages = {
        let store_c = Arc::clone(store);
        let tid = thread_id.to_string();
        tokio::task::spawn_blocking(move || store_c.list_messages(&tid))
            .await
            .map_err(|e| format!("spawn_blocking join: {e}"))?
            .map_err(|e| format!("list_messages: {e}"))?
    };

    let prompt = format_thread_prompt(instructions, &messages);

    if prompt.is_empty() {
        warn!(
            thread_id,
            run_id, "run has empty prompt — completing with empty response"
        );
    }

    // Step 2b — create a MessageCreation RunStep (InProgress).
    let step_id = format!("step-{}", uuid::Uuid::new_v4().as_simple());
    {
        let store_c = Arc::clone(store);
        let tid = thread_id.to_string();
        let rid = run_id.to_string();
        let sid = step_id.clone();
        let step = RunStep::new_message_creation(sid, rid, tid.clone());
        let step_c = step.clone();
        tokio::task::spawn_blocking(move || store_c.append_step(&tid, &step_c.run_id, &step_c))
            .await
            .map_err(|e| format!("spawn_blocking join: {e}"))?
            .map_err(|e| format!("append_step: {e}"))?;
    }

    // Step 3 — send to the inference engine.
    let (reply_tx, reply_rx) =
        tokio::sync::oneshot::channel::<Result<(String, crate::queue::UsageStats), String>>();

    let sampler = SamplerConfig::default();

    state
        .queue
        .send(BatchRequest::Generate {
            prompt,
            max_tokens,
            config: sampler,
            cache_prompt: true,
            lora_selection: vec![],
            reply: reply_tx,
        })
        .await
        .map_err(|_| "inference queue closed during run".to_string())?;

    let generated_text = match reply_rx.await {
        Ok(Ok((text, _usage))) => text,
        Ok(Err(e)) => return Err(format!("inference engine error: {e}")),
        Err(e) => return Err(format!("reply channel closed: {e}")),
    };

    // Broadcast MessageDelta event with the generated text.
    maybe_broadcast(
        event_tx,
        RunEvent::MessageDelta {
            run_id: run_id.to_string(),
            content: generated_text.clone(),
        },
    );

    // Step 4 — retrieve the run to get its ID for the assistant message.
    let run: Run = {
        let store_c = Arc::clone(store);
        let tid = thread_id.to_string();
        let rid = run_id.to_string();
        tokio::task::spawn_blocking(move || store_c.get_run(&tid, &rid))
            .await
            .map_err(|e| format!("spawn_blocking join: {e}"))?
            .map_err(|e| format!("get_run: {e}"))?
    };

    // Append the assistant's message and capture the message ID.
    let assistant_msg_id = format!("msg_{}", uuid::Uuid::new_v4().as_simple());
    {
        let store_c = Arc::clone(store);
        let tid = thread_id.to_string();
        let mid = assistant_msg_id.clone();
        let assistant_msg =
            ThreadMessage::new_assistant(mid, tid.clone(), run.id.clone(), generated_text);
        tokio::task::spawn_blocking(move || store_c.append_message(&tid, &assistant_msg))
            .await
            .map_err(|e| format!("spawn_blocking join: {e}"))?
            .map_err(|e| format!("append_message: {e}"))?;
    }

    // Mark the RunStep as Completed and attach the message ID as step_details.
    {
        let store_c = Arc::clone(store);
        let tid = thread_id.to_string();
        let rid = run_id.to_string();
        let sid = step_id.clone();
        let msg_id = assistant_msg_id.clone();
        tokio::task::spawn_blocking(move || {
            // Update status to Completed.
            store_c.update_step_status(&tid, &rid, &sid, RunStepStatus::Completed)?;
            // Set step_details to include the message ID.
            let mut step = store_c.get_step(&tid, &rid, &sid)?;
            step.step_details =
                Some(crate::threads::types::MessageCreationStepDetails { message_id: msg_id });
            let steps_dir = store_c.steps_dir(&tid, &rid);
            let filename = format!("{sid}.json");
            let json = serde_json::to_string_pretty(&step)
                .map_err(crate::error::ServerError::Serialization)?;
            let mut tmp = tempfile::NamedTempFile::new_in(&steps_dir).map_err(|e| {
                crate::error::ServerError::IoError {
                    context: "create temp file for step details".to_string(),
                    source: e,
                }
            })?;
            use std::io::Write as _;
            tmp.write_all(json.as_bytes())
                .map_err(|e| crate::error::ServerError::IoError {
                    context: "write step details".to_string(),
                    source: e,
                })?;
            tmp.flush()
                .map_err(|e| crate::error::ServerError::IoError {
                    context: "flush step details".to_string(),
                    source: e,
                })?;
            let target = steps_dir.join(&filename);
            tmp.persist(&target)
                .map_err(|e| crate::error::ServerError::IoError {
                    context: format!("persist step details to {}", target.display()),
                    source: e.error,
                })?;
            Ok::<(), crate::error::ServerError>(())
        })
        .await
        .map_err(|e| format!("spawn_blocking join: {e}"))?
        .map_err(|e| format!("update step details: {e}"))?;
    }

    // Step 5 — transition to Completed.
    {
        let store_c = Arc::clone(store);
        let tid = thread_id.to_string();
        let rid = run_id.to_string();
        tokio::task::spawn_blocking(move || {
            store_c.update_run_status(&tid, &rid, RunStatus::Completed, None)
        })
        .await
        .map_err(|e| format!("spawn_blocking join: {e}"))?
        .map_err(|e| format!("update Completed: {e}"))?;
    }

    // Broadcast Completed event.
    if event_tx.is_some() {
        let store_c = Arc::clone(store);
        let tid = thread_id.to_string();
        let rid = run_id.to_string();
        if let Ok(Ok(completed_run)) =
            tokio::task::spawn_blocking(move || store_c.get_run(&tid, &rid)).await
        {
            maybe_broadcast(event_tx, RunEvent::Completed(completed_run));
        }
    }

    info!(thread_id, run_id, "run completed successfully");
    Ok(())
}

/// Format the thread's messages as a single prompt string.
///
/// Mirrors the chat-template logic from `routes/chat.rs::format_chat_prompt`.
/// If `instructions` is provided it is prepended as a `system` message.
fn format_thread_prompt(instructions: Option<&str>, messages: &[ThreadMessage]) -> String {
    let mut prompt = String::new();

    if let Some(sys) = instructions {
        if !sys.is_empty() {
            prompt.push_str("<|system|>\n");
            prompt.push_str(sys);
            prompt.push_str("\n<|end|>\n");
        }
    }

    for msg in messages {
        let content = msg.text_content();
        match msg.role {
            MessageRole::User => {
                prompt.push_str("<|user|>\n");
                prompt.push_str(content);
                prompt.push_str("\n<|end|>\n");
            }
            MessageRole::Assistant => {
                prompt.push_str("<|assistant|>\n");
                prompt.push_str(content);
                prompt.push_str("\n<|end|>\n");
            }
        }
    }

    prompt.push_str("<|assistant|>\n");
    prompt
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::queue::UsageStats;
    use crate::threads::types::{Thread, ThreadMessage};
    use std::env::temp_dir;
    use std::sync::Arc;
    use uuid::Uuid;

    fn make_store(tag: &str) -> Arc<ThreadStore> {
        let id = Uuid::new_v4().as_simple().to_string();
        let dir = temp_dir().join(format!("oxillama_thread_worker_test_{tag}_{id}"));
        Arc::new(ThreadStore::new(dir).expect("ThreadStore::new"))
    }

    fn make_state_with_mock_worker() -> (Arc<AppState>, tokio::task::JoinHandle<()>) {
        let (tx, mut rx) = tokio::sync::mpsc::channel::<BatchRequest>(16);

        let handle = tokio::spawn(async move {
            while let Some(req) = rx.recv().await {
                if let BatchRequest::Generate { reply, .. } = req {
                    let _ = reply.send(Ok((
                        "mock assistant response".to_string(),
                        UsageStats {
                            prompt_tokens: 5,
                            completion_tokens: 4,
                            total_tokens: 9,
                        },
                    )));
                }
            }
        });

        let state = Arc::new(AppState::new(
            tx,
            "test-model".to_string(),
            oxillama_runtime::sampling::SamplerConfig::default(),
            None,
            0,
        ));

        (state, handle)
    }

    #[test]
    fn format_thread_prompt_with_instructions() {
        let msgs = vec![ThreadMessage::new_user(
            "m1".into(),
            "t1".into(),
            "hi there".into(),
        )];
        let prompt = format_thread_prompt(Some("Be helpful."), &msgs);
        assert!(prompt.contains("<|system|>"), "should have system block");
        assert!(prompt.contains("Be helpful."));
        assert!(prompt.contains("<|user|>"));
        assert!(prompt.contains("hi there"));
        assert!(prompt.ends_with("<|assistant|>\n"));
    }

    #[test]
    fn format_thread_prompt_without_instructions() {
        let msgs = vec![ThreadMessage::new_user(
            "m1".into(),
            "t1".into(),
            "question".into(),
        )];
        let prompt = format_thread_prompt(None, &msgs);
        assert!(!prompt.contains("<|system|>"));
        assert!(prompt.contains("question"));
        assert!(prompt.ends_with("<|assistant|>\n"));
    }

    #[test]
    fn format_thread_prompt_mixed_roles() {
        let msgs = vec![
            ThreadMessage::new_user("m1".into(), "t1".into(), "hello".into()),
            ThreadMessage::new_assistant("m2".into(), "t1".into(), "run_1".into(), "hi".into()),
            ThreadMessage::new_user("m3".into(), "t1".into(), "follow up".into()),
        ];
        let prompt = format_thread_prompt(None, &msgs);
        // All three messages + trailing assistant prompt.
        assert_eq!(prompt.matches("<|user|>").count(), 2);
        assert_eq!(prompt.matches("<|assistant|>").count(), 2); // 1 from history + 1 trailing
    }

    #[tokio::test]
    async fn worker_processes_run_to_completed() {
        let store = make_store("worker_complete");
        let (state, _worker_handle) = make_state_with_mock_worker();

        // Set up thread and run.
        let thread = Thread {
            id: "thread_wc".to_string(),
            object: "thread".to_string(),
            created_at: 0,
            metadata: serde_json::json!({}),
        };
        store.create_thread(&thread).expect("create thread");

        let msg = ThreadMessage::new_user("msg_1".into(), "thread_wc".into(), "hello".into());
        store.append_message("thread_wc", &msg).expect("append");

        let run = Run {
            id: "run_wc".to_string(),
            object: "thread.run".to_string(),
            created_at: 0,
            thread_id: "thread_wc".to_string(),
            status: RunStatus::Queued,
            model: "test-model".to_string(),
            last_error: None,
        };
        store.create_run("thread_wc", &run).expect("create run");

        // Spawn worker and submit item.
        let (tx, rx) = crate::threads::queue::new_run_queue();
        spawn_run_worker(Arc::clone(&store), rx, Arc::clone(&state));

        tx.send(crate::threads::queue::RunWorkItem {
            thread_id: "thread_wc".to_string(),
            run_id: "run_wc".to_string(),
            model: None,
            instructions: None,
            max_tokens: 64,
        })
        .expect("send work item");

        // Poll until terminal state.
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
        loop {
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
            let current_run = store.get_run("thread_wc", "run_wc").expect("get run");
            if current_run.status.is_terminal() {
                assert_eq!(current_run.status, RunStatus::Completed);
                break;
            }
            if std::time::Instant::now() > deadline {
                panic!("run did not complete within deadline");
            }
        }

        // Verify assistant message was appended.
        let msgs = store.list_messages("thread_wc").expect("list messages");
        assert_eq!(msgs.len(), 2, "should have user + assistant message");
        assert_eq!(msgs[1].role, MessageRole::Assistant);
        assert_eq!(msgs[1].text_content(), "mock assistant response");
    }
}