embacle 0.14.6

LLM runner library — wraps 12 AI CLI tools as pluggable LLM providers with agent loop, guardrails, and cost tracking
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
// ABOUTME: C FFI bindings for Swift integration, exposing copilot chat completion
// ABOUTME: Provides init/completion/shutdown lifecycle via extern "C" functions
//
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 dravr.ai

use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use std::sync::{Arc, RwLock};
use std::time::Duration;

use serde::{Deserialize, Serialize};

use crate::types::{ChatMessage, ChatRequest, ImagePart, LlmProvider};
use crate::CopilotHeadlessRunner;

// ---------------------------------------------------------------------------
// Internal state
// ---------------------------------------------------------------------------

/// Holds the tokio runtime and copilot runner, created by `embacle_init`
struct FfiState {
    runtime: tokio::runtime::Runtime,
    runner: Box<dyn LlmProvider>,
}

// SAFETY: FfiState fields are Send+Sync (Runtime is Send+Sync, Box<dyn LlmProvider>
// requires Send+Sync from the trait bound). Arc allows concurrent completions
// without holding the lock during block_on.
static STATE: RwLock<Option<Arc<FfiState>>> = RwLock::new(None);

// ---------------------------------------------------------------------------
// OpenAI-compatible request types (deserialization)
// ---------------------------------------------------------------------------

#[derive(Deserialize)]
struct FfiRequest {
    model: Option<String>,
    messages: Vec<FfiMessage>,
    #[serde(default)]
    temperature: Option<f32>,
    #[serde(default)]
    max_tokens: Option<u32>,
    #[serde(default)]
    top_p: Option<f32>,
}

#[derive(Deserialize)]
struct FfiMessage {
    role: String,
    #[serde(default)]
    content: Option<FfiContent>,
}

#[derive(Deserialize)]
#[serde(untagged)]
enum FfiContent {
    Text(String),
    Parts(Vec<FfiContentPart>),
}

#[derive(Deserialize)]
#[serde(tag = "type")]
enum FfiContentPart {
    #[serde(rename = "text")]
    Text { text: String },
    #[serde(rename = "image_url")]
    ImageUrl { image_url: FfiImageUrl },
}

#[derive(Deserialize)]
struct FfiImageUrl {
    url: String,
}

// ---------------------------------------------------------------------------
// OpenAI-compatible response types (serialization)
// ---------------------------------------------------------------------------

#[derive(Serialize)]
struct FfiResponse {
    id: String,
    object: &'static str,
    model: String,
    choices: Vec<FfiChoice>,
    #[serde(skip_serializing_if = "Option::is_none")]
    usage: Option<FfiTokenUsage>,
}

#[derive(Serialize)]
struct FfiChoice {
    index: u32,
    message: FfiResponseMessage,
    finish_reason: String,
}

#[derive(Serialize)]
struct FfiResponseMessage {
    role: &'static str,
    content: String,
}

#[derive(Serialize)]
struct FfiTokenUsage {
    #[serde(rename = "prompt_tokens")]
    prompt: u32,
    #[serde(rename = "completion_tokens")]
    completion: u32,
    #[serde(rename = "total_tokens")]
    total: u32,
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Parse a `data:` URI into its MIME type and base64 payload
fn parse_data_uri(url: &str) -> Option<(&str, &str)> {
    let rest = url.strip_prefix("data:")?;
    let semi = rest.find(';')?;
    let mime = &rest[..semi];
    let after_semi = &rest[semi + 1..];
    let data = after_semi.strip_prefix("base64,")?;
    Some((mime, data))
}

/// Extract text content from message parts, ignoring non-text parts
fn extract_text(parts: &[FfiContentPart]) -> String {
    parts
        .iter()
        .filter_map(|p| match p {
            FfiContentPart::Text { text } => Some(text.as_str()),
            FfiContentPart::ImageUrl { .. } => None,
        })
        .collect::<Vec<_>>()
        .join("")
}

/// Convert FFI messages to internal `ChatMessage` types
fn convert_ffi_messages(messages: &[FfiMessage]) -> Result<Vec<ChatMessage>, String> {
    let mut result = Vec::with_capacity(messages.len());

    for msg in messages {
        let content = msg.content.as_ref();
        match msg.role.as_str() {
            "system" => {
                let text = match content {
                    Some(FfiContent::Text(t)) => t.clone(),
                    Some(FfiContent::Parts(parts)) => extract_text(parts),
                    None => String::new(),
                };
                result.push(ChatMessage::system(text));
            }
            "user" => match content {
                Some(FfiContent::Text(t)) => {
                    result.push(ChatMessage::user(t.clone()));
                }
                Some(FfiContent::Parts(parts)) => {
                    let text = extract_text(parts);
                    let mut images = Vec::new();

                    for part in parts {
                        if let FfiContentPart::ImageUrl { image_url } = part {
                            let (mime, data) = parse_data_uri(&image_url.url).ok_or_else(|| {
                                format!(
                                    "unsupported image URL (expected data: URI): {}",
                                    &image_url.url[..image_url.url.len().min(60)]
                                )
                            })?;
                            images.push(
                                ImagePart::new(data, mime)
                                    .map_err(|e| format!("invalid image: {e}"))?,
                            );
                        }
                    }

                    if images.is_empty() {
                        result.push(ChatMessage::user(text));
                    } else {
                        result.push(ChatMessage::user_with_images(text, images));
                    }
                }
                None => {
                    result.push(ChatMessage::user(String::new()));
                }
            },
            "assistant" => {
                let text = match content {
                    Some(FfiContent::Text(t)) => t.clone(),
                    Some(FfiContent::Parts(parts)) => extract_text(parts),
                    None => String::new(),
                };
                result.push(ChatMessage::assistant(text));
            }
            other => {
                return Err(format!("unsupported message role: {other}"));
            }
        }
    }

    Ok(result)
}

/// Build an OpenAI-compatible response JSON from a `ChatResponse`
fn build_response_json(response: &crate::types::ChatResponse) -> String {
    let usage = response.usage.as_ref().map(|u| FfiTokenUsage {
        prompt: u.prompt_tokens,
        completion: u.completion_tokens,
        total: u.total_tokens,
    });

    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos();

    let ffi_response = FfiResponse {
        id: format!("chatcmpl-{nanos:016x}"),
        object: "chat.completion",
        model: response.model.clone(),
        choices: vec![FfiChoice {
            index: 0,
            message: FfiResponseMessage {
                role: "assistant",
                content: response.content.clone(),
            },
            finish_reason: response
                .finish_reason
                .clone()
                .unwrap_or_else(|| "stop".to_owned()),
        }],
        usage,
    };

    // Serialization of these simple types cannot fail
    serde_json::to_string(&ffi_response)
        .unwrap_or_else(|e| format!("{{\"error\":{{\"message\":\"serialization failed: {e}\"}}}}"))
}

/// Convert a Rust string into a malloc'd C string (caller frees with `embacle_free_string`)
fn to_c_string(s: &str) -> *mut c_char {
    CString::new(s).map_or_else(
        |_| {
            eprintln!("embacle: response contains null bytes");
            std::ptr::null_mut()
        },
        CString::into_raw,
    )
}

// ---------------------------------------------------------------------------
// Public FFI API
// ---------------------------------------------------------------------------

/// Initialize the tokio runtime and create the copilot headless runner.
///
/// Reads copilot auth tokens from `~/.config/github-copilot/` and environment
/// variables (`COPILOT_GITHUB_TOKEN`, `GH_TOKEN`, `GITHUB_TOKEN`).
///
/// Returns 0 on success, -1 if already initialized, -2 on runtime creation
/// failure, -3 on runner creation failure.
#[no_mangle]
pub extern "C" fn embacle_init() -> i32 {
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let mut guard = match STATE.write() {
            Ok(g) => g,
            Err(e) => {
                eprintln!("embacle_init: lock poisoned, recovering");
                e.into_inner()
            }
        };

        if guard.is_some() {
            eprintln!("embacle_init: already initialized");
            return -1;
        }

        let runtime = match tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
        {
            Ok(rt) => rt,
            Err(e) => {
                eprintln!("embacle_init: failed to create tokio runtime: {e}");
                return -2;
            }
        };

        let runner: Box<dyn LlmProvider> = runtime.block_on(async {
            Box::new(CopilotHeadlessRunner::from_env().await) as Box<dyn LlmProvider>
        });

        *guard = Some(Arc::new(FfiState { runtime, runner }));
        0
    }));

    result.unwrap_or_else(|_| {
        eprintln!("embacle_init: panic during initialization");
        -2
    })
}

/// Send a chat completion request and return the response as a JSON string.
///
/// `request_json` must be a null-terminated UTF-8 string in `OpenAI` chat
/// completions format. `timeout_seconds` sets the maximum wait time (0 means
/// no timeout; the runner's internal timeout still applies).
///
/// Returns a malloc'd JSON string on success (free with [`embacle_free_string`]),
/// or `NULL` on error. Errors are logged to stderr.
///
/// # Safety
///
/// - `request_json` must point to a valid null-terminated UTF-8 C string.
/// - The returned pointer must be freed exactly once with [`embacle_free_string`].
#[no_mangle]
pub extern "C" fn embacle_chat_completion(
    request_json: *const c_char,
    timeout_seconds: i32,
) -> *mut c_char {
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        if request_json.is_null() {
            eprintln!("embacle_chat_completion: request_json is NULL");
            return std::ptr::null_mut();
        }

        // SAFETY: caller guarantees valid null-terminated UTF-8
        let json_str = unsafe { CStr::from_ptr(request_json) };
        let json_str = match json_str.to_str() {
            Ok(s) => s,
            Err(e) => {
                eprintln!("embacle_chat_completion: invalid UTF-8: {e}");
                return std::ptr::null_mut();
            }
        };

        let ffi_request: FfiRequest = match serde_json::from_str(json_str) {
            Ok(r) => r,
            Err(e) => {
                eprintln!("embacle_chat_completion: invalid request JSON: {e}");
                return std::ptr::null_mut();
            }
        };

        let messages = match convert_ffi_messages(&ffi_request.messages) {
            Ok(m) => m,
            Err(e) => {
                eprintln!("embacle_chat_completion: {e}");
                return std::ptr::null_mut();
            }
        };

        let mut chat_request = ChatRequest::new(messages);
        chat_request.model = ffi_request.model;
        chat_request.temperature = ffi_request.temperature;
        chat_request.max_tokens = ffi_request.max_tokens;
        chat_request.top_p = ffi_request.top_p;

        let timeout = u64::try_from(timeout_seconds)
            .ok()
            .filter(|&s| s > 0)
            .map(Duration::from_secs);

        // Clone the Arc so the read lock is released before blocking
        let state = STATE
            .read()
            .unwrap_or_else(|e| {
                eprintln!("embacle_chat_completion: lock poisoned, recovering");
                e.into_inner()
            })
            .as_ref()
            .cloned();

        let Some(state) = state else {
            eprintln!("embacle_chat_completion: not initialized (call embacle_init first)");
            return std::ptr::null_mut();
        };

        let result = state.runtime.block_on(async {
            let completion = state.runner.complete(&chat_request);
            match timeout {
                Some(duration) => tokio::time::timeout(duration, completion)
                    .await
                    .unwrap_or_else(|_| {
                        Err(crate::types::RunnerError::timeout(format!(
                            "completion timed out after {timeout_seconds}s"
                        )))
                    }),
                None => completion.await,
            }
        });

        match result {
            Ok(response) => {
                let json = build_response_json(&response);
                to_c_string(&json)
            }
            Err(e) => {
                eprintln!("embacle_chat_completion: {:?}: {}", e.kind, e.message);
                std::ptr::null_mut()
            }
        }
    }));

    result.unwrap_or_else(|_| {
        eprintln!("embacle_chat_completion: panic during completion");
        std::ptr::null_mut()
    })
}

/// Free a string returned by embacle functions.
///
/// Passing `NULL` is a no-op.
///
/// # Safety
///
/// - `ptr` must have been returned by [`embacle_chat_completion`] or be `NULL`.
/// - `ptr` must not be freed more than once.
#[no_mangle]
pub extern "C" fn embacle_free_string(ptr: *mut c_char) {
    if ptr.is_null() {
        return;
    }
    let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        // SAFETY: ptr was allocated by CString::into_raw in to_c_string
        unsafe {
            drop(CString::from_raw(ptr));
        }
    }));
}

/// Shutdown the tokio runtime and release all resources.
///
/// After calling this, [`embacle_chat_completion`] returns `NULL` until
/// [`embacle_init`] is called again.
#[no_mangle]
pub extern "C" fn embacle_shutdown() {
    let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let mut guard = match STATE.write() {
            Ok(g) => g,
            Err(e) => {
                eprintln!("embacle_shutdown: lock poisoned, recovering");
                e.into_inner()
            }
        };
        if let Some(arc) = guard.take() {
            // Unwrap the Arc — if other threads still hold references,
            // wait for them to finish first
            match Arc::try_unwrap(arc) {
                Ok(state) => {
                    state.runtime.shutdown_timeout(Duration::from_secs(5));
                }
                Err(still_shared) => {
                    // Other completions are still in flight; put it back and
                    // let the runtime shut down when the last Arc drops
                    eprintln!("embacle_shutdown: waiting for in-flight completions");
                    *guard = Some(still_shared);
                }
            }
        }
    }));
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_data_uri_valid_png() {
        let url = "data:image/png;base64,iVBORw0KGgo=";
        let (mime, data) = parse_data_uri(url).unwrap();
        assert_eq!(mime, "image/png");
        assert_eq!(data, "iVBORw0KGgo=");
    }

    #[test]
    fn parse_data_uri_valid_jpeg() {
        let url = "data:image/jpeg;base64,/9j/4AAQ";
        let (mime, data) = parse_data_uri(url).unwrap();
        assert_eq!(mime, "image/jpeg");
        assert_eq!(data, "/9j/4AAQ");
    }

    #[test]
    fn parse_data_uri_invalid_no_prefix() {
        assert!(parse_data_uri("https://example.com/img.png").is_none());
    }

    #[test]
    fn parse_data_uri_invalid_no_base64() {
        assert!(parse_data_uri("data:image/png;charset=utf-8,abc").is_none());
    }

    #[test]
    fn convert_simple_text_messages() {
        let messages = vec![
            FfiMessage {
                role: "system".to_owned(),
                content: Some(FfiContent::Text("Be concise".to_owned())),
            },
            FfiMessage {
                role: "user".to_owned(),
                content: Some(FfiContent::Text("Hello".to_owned())),
            },
        ];
        let result = convert_ffi_messages(&messages).unwrap();
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].content, "Be concise");
        assert_eq!(result[1].content, "Hello");
    }

    #[test]
    fn convert_user_multipart_with_image() {
        let messages = vec![FfiMessage {
            role: "user".to_owned(),
            content: Some(FfiContent::Parts(vec![
                FfiContentPart::Text {
                    text: "What is this?".to_owned(),
                },
                FfiContentPart::ImageUrl {
                    image_url: FfiImageUrl {
                        url: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==".to_owned(),
                    },
                },
            ])),
        }];
        let result = convert_ffi_messages(&messages).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].content, "What is this?");
        assert!(result[0].images.is_some());
        assert_eq!(result[0].images.as_ref().unwrap().len(), 1);
    }

    #[test]
    fn convert_unsupported_role_fails() {
        let messages = vec![FfiMessage {
            role: "function".to_owned(),
            content: Some(FfiContent::Text("result".to_owned())),
        }];
        assert!(convert_ffi_messages(&messages).is_err());
    }

    #[test]
    fn convert_missing_content_produces_empty() {
        let messages = vec![FfiMessage {
            role: "user".to_owned(),
            content: None,
        }];
        let result = convert_ffi_messages(&messages).unwrap();
        assert_eq!(result[0].content, "");
    }

    #[test]
    fn convert_non_data_uri_fails() {
        let messages = vec![FfiMessage {
            role: "user".to_owned(),
            content: Some(FfiContent::Parts(vec![FfiContentPart::ImageUrl {
                image_url: FfiImageUrl {
                    url: "https://example.com/img.png".to_owned(),
                },
            }])),
        }];
        let err = convert_ffi_messages(&messages).unwrap_err();
        assert!(err.contains("unsupported image URL"));
    }

    #[test]
    fn build_response_json_basic() {
        let response = crate::types::ChatResponse {
            content: "Hello world".to_owned(),
            model: "test-model".to_owned(),
            usage: Some(crate::types::TokenUsage {
                prompt_tokens: 10,
                completion_tokens: 5,
                total_tokens: 15,
            }),
            finish_reason: Some("stop".to_owned()),
            warnings: None,
            tool_calls: None,
        };
        let json = build_response_json(&response);
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed["object"], "chat.completion");
        assert_eq!(parsed["model"], "test-model");
        assert_eq!(parsed["choices"][0]["message"]["content"], "Hello world");
        assert_eq!(parsed["choices"][0]["message"]["role"], "assistant");
        assert_eq!(parsed["choices"][0]["finish_reason"], "stop");
        assert_eq!(parsed["usage"]["prompt_tokens"], 10);
        assert_eq!(parsed["usage"]["total_tokens"], 15);
    }

    #[test]
    fn build_response_json_no_usage() {
        let response = crate::types::ChatResponse {
            content: "Hi".to_owned(),
            model: "m".to_owned(),
            usage: None,
            finish_reason: None,
            warnings: None,
            tool_calls: None,
        };
        let json = build_response_json(&response);
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert!(parsed.get("usage").is_none());
        assert_eq!(parsed["choices"][0]["finish_reason"], "stop");
    }

    #[test]
    fn request_json_round_trip() {
        let json = r#"{
            "model": "claude-opus-4.6-fast",
            "messages": [
                {"role": "system", "content": "Be helpful"},
                {"role": "user", "content": "Hi"}
            ],
            "temperature": 0.7,
            "max_tokens": 100
        }"#;
        let req: FfiRequest = serde_json::from_str(json).unwrap();
        assert_eq!(req.model.as_deref(), Some("claude-opus-4.6-fast"));
        assert_eq!(req.messages.len(), 2);
        assert_eq!(req.temperature, Some(0.7));
        assert_eq!(req.max_tokens, Some(100));
    }

    #[test]
    fn request_json_multipart_content() {
        let json = r#"{
            "messages": [{
                "role": "user",
                "content": [
                    {"type": "text", "text": "Describe this"},
                    {"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,/9j/4AAQ"}}
                ]
            }]
        }"#;
        let req: FfiRequest = serde_json::from_str(json).unwrap();
        assert_eq!(req.messages.len(), 1);
        match &req.messages[0].content {
            Some(FfiContent::Parts(parts)) => assert_eq!(parts.len(), 2),
            _ => panic!("expected Parts variant"),
        }
    }

    #[test]
    fn request_json_minimal() {
        let json = r#"{"messages": [{"role": "user", "content": "hi"}]}"#;
        let req: FfiRequest = serde_json::from_str(json).unwrap();
        assert!(req.model.is_none());
        assert!(req.temperature.is_none());
        assert_eq!(req.messages.len(), 1);
    }

    #[test]
    fn to_c_string_and_free() {
        let ptr = to_c_string("hello");
        assert!(!ptr.is_null());
        let s = unsafe { CStr::from_ptr(ptr) }.to_str().unwrap();
        assert_eq!(s, "hello");
        embacle_free_string(ptr);
    }

    #[test]
    fn free_null_is_noop() {
        embacle_free_string(std::ptr::null_mut());
    }
}