troupekit-ffi-core 0.4.0

Binding-neutral Troupekit API: plain Rust handles, records and host traits that the UniFFI and PyO3 adapters translate.
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
use crate::error::{ErrorCode, FfiError};
use crate::records::{
    FfiEvent, HostError, HostHeader, HostHttpRequest, HostModelCaps, HostReply, HostRequest,
};
use crate::runtime::check_input;
use async_trait::async_trait;
use serde_json::Value;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::sync::{Arc, Mutex, PoisonError};
use troupekit_core::http::{HttpError, HttpErrorKind};
use troupekit_core::tool::ToolSpec;
use troupekit_tools::{Tool, ToolContext, ToolError, ToolOutput};
use troupekit_trace::{CallbackSink, Trace, TraceSink};

pub(crate) const MAX_TOOL_NAME_BYTES: usize = 64;
pub(crate) const MAX_TOOL_DESCRIPTION_BYTES: usize = 16 * 1024;
pub(crate) const MAX_TOOL_OUTPUT_BYTES: usize = 1024 * 1024;
#[cfg(feature = "knowledge")]
pub(crate) const MAX_EMBED_BATCH: usize = 256;
#[cfg(feature = "knowledge")]
const MAX_EMBED_DIMS: u32 = 65_536;

/// Receives every event of a stream as it happens, as the callback form
/// of `RunStream::next`. Called on one thread per stream,
/// `troupekit-listener`, never the caller's; a slow listener only
/// slows its own delivery, and the buffer drops text deltas meanwhile.
pub trait EventListener: Send + Sync {
    /// Handles one `event`, in `seq` order within its run id.
    /// Input: the event. Output: none; a panic here is caught and
    /// stops only this listener.
    fn on_event(&self, event: FfiEvent);
}

/// Receives every trace record of a traced build or run, as the
/// callback form of `trace_jsonl`. Called on the thread that writes the
/// record, so it should return fast; keys are scrubbed before the call.
pub trait TraceListener: Send + Sync {
    /// Handles one record: one JSON object on one line, no newline.
    /// Input: the record. Output: none; a panic here is caught and
    /// turns this listener off for the rest of the trace.
    fn on_record(&self, line: String);
}

/// Wraps `listener` as a trace sink that catches its panics.
/// Input: the host listener. Output: the sink for `trace_to`.
pub(crate) fn trace_sink(listener: Arc<dyn TraceListener>) -> Arc<dyn TraceSink> {
    Arc::new(CallbackSink::new(move |line: &str| {
        listener.on_record(line.to_owned())
    }))
}

/// Parses trace category names as 0.3 spells them, such as `tools`.
/// Input: the names. Output: the categories, or `InvalidArgument`
/// naming the first unknown name and every known one.
pub(crate) fn trace_categories(names: &[String]) -> Result<Vec<Trace>, FfiError> {
    let mut found = Vec::with_capacity(names.len());
    for name in names {
        check_input("trace", name)?;
        match name.parse::<Trace>() {
            Ok(category) => found.push(category),
            Err(_) => {
                let known: Vec<&str> = Trace::ALL.iter().map(|t| t.as_str()).collect();
                return Err(FfiError::new(
                    ErrorCode::InvalidArgument,
                    format!(
                        "trace: unknown category `{name}`; known: {}",
                        known.join(", ")
                    ),
                ));
            }
        }
    }
    assert_eq!(found.len(), names.len());
    Ok(found)
}

/// A tool written in the host language: a name the model calls it by,
/// a description, a JSON Schema for its arguments and the call itself.
/// troupekit checks name and schema when it is added.
pub trait HostTool: Send + Sync {
    /// Returns the tool name: 1 to 64 bytes of `[a-z0-9_]`.
    /// Called once when the tool is added.
    /// Output: the name the model uses.
    fn name(&self) -> String;

    /// Returns what the tool does, for the model, at most 16 KiB.
    /// Called once when the tool is added.
    /// Output: the description text.
    fn description(&self) -> String;

    /// Returns the JSON Schema of the arguments as JSON object text
    /// with `"type": "object"`. Called once when the tool is added; any
    /// other text is refused with `InvalidArgument`.
    fn schema_json(&self) -> String;

    /// Runs the tool on `args_json`, arguments that match the schema.
    /// Output: the result text, at most 1 MiB, or a `HostError` the
    /// model sees as an error result. Runs on a blocking worker thread.
    fn call(&self, args_json: String) -> Result<String, HostError>;
}

/// A model run by the host, such as the phone's built-in model.
/// troupekit renders each request for the model's `capabilities` and
/// parses tool calls out of plain text when it has no native tools.
pub trait HostModel: Send + Sync {
    /// Returns the model id, for example `apple/foundation`.
    /// Used in events, errors and the catalog key.
    /// Output: a non-empty id.
    fn id(&self) -> String;

    /// Returns what the model supports.
    /// Read once per request, so it may change when the device does.
    /// Output: the capabilities record.
    fn capabilities(&self) -> HostModelCaps;

    /// Answers `request`, pushing text deltas to `sink` when streaming.
    /// Output: the whole reply with token counts, or a `HostError`.
    /// Runs on a blocking worker thread.
    fn generate(&self, request: HostRequest, sink: Arc<TokenSink>) -> Result<HostReply, HostError>;
}

/// An embedding model run by the host, for knowledge search.
/// Every vector must have `dims` numbers; troupekit checks the shape
/// and sends at most 256 texts per call.
#[cfg(feature = "knowledge")]
pub trait HostEmbedder: Send + Sync {
    /// Returns the length of every vector `embed` returns.
    /// Read once at build.
    /// Output: a dimension count above zero.
    fn dims(&self) -> u32;

    /// Embeds `texts`, one vector per text, in order.
    /// Output: the vectors, or a `HostError`.
    /// Runs on a blocking worker thread.
    fn embed(&self, texts: Vec<String>) -> Result<Vec<Vec<f32>>, HostError>;
}

/// An HTTP client in the host language, used only for model and
/// embedding calls to the resolved vendor. The host must turn off
/// redirect following and report a 3xx as received; troupekit refuses
/// it. The response goes through `HttpSink`: `head`, then `chunk`s.
pub trait HostTransport: Send + Sync {
    /// Sends `request` and reports the response to `sink` before it
    /// returns. Output: `Ok` once the body is done, or a `HostError`
    /// for a network failure. Runs on a blocking worker thread.
    fn send(&self, request: HostHttpRequest, sink: Arc<HttpSink>) -> Result<(), HostError>;
}

/// Where a host model pushes text deltas while it generates. Each
/// delta reaches the run's stream as a `TextDelta` event, except when
/// troupekit emulates tools: then the parsed answer is sent once.
/// Built by troupekit for each `HostModel::generate` call.
#[derive(Debug)]
pub struct TokenSink {
    deltas: Option<tokio::sync::mpsc::Sender<String>>,
    state: Mutex<SinkText>,
    cap: usize,
}

#[derive(Debug, Default)]
struct SinkText {
    bytes: usize,
    pending: String,
}

impl TokenSink {
    /// Pushes one text delta of the reply in progress. Never blocks.
    /// Input: the new text only, not the text so far. Output: none.
    /// Empty deltas and deltas past the reply cap are dropped; while the
    /// queue is full, deltas are joined and sent as one once it drains.
    pub fn delta(&self, text: String) {
        let Some(deltas) = &self.deltas else {
            return;
        };
        if text.is_empty() {
            return;
        }
        let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
        let total = state.bytes.saturating_add(text.len());
        if total > self.cap {
            return;
        }
        state.bytes = total;
        state.pending.push_str(&text);
        let joined = std::mem::take(&mut state.pending);
        if let Err(tokio::sync::mpsc::error::TrySendError::Full(back)) = deltas.try_send(joined) {
            state.pending = back;
        }
        assert!(state.bytes <= self.cap);
        assert!(state.pending.len() <= state.bytes);
    }

    /// Takes the text still waiting because the queue was full, so the
    /// run can send it after the host returns. Output: that text, or an
    /// empty string.
    pub(crate) fn take_pending(&self) -> String {
        let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
        std::mem::take(&mut state.pending)
    }

    pub(crate) fn channel(
        forward: bool,
        cap: usize,
        queue: usize,
    ) -> (Arc<TokenSink>, tokio::sync::mpsc::Receiver<String>) {
        assert!(queue > 0);
        let (sender, receiver) = tokio::sync::mpsc::channel(queue);
        let sink = TokenSink {
            deltas: forward.then_some(sender),
            state: Mutex::new(SinkText::default()),
            cap,
        };
        (Arc::new(sink), receiver)
    }
}

/// Where a host transport reports an HTTP response: `head` once with
/// the status, the final URL and the headers, then `chunk` for each
/// part of the body. Built by troupekit for each `send` call.
pub struct HttpSink {
    state: Mutex<SinkState>,
    cap: usize,
}

struct SinkState {
    head: Option<tokio::sync::oneshot::Sender<crate::host_transport::Head>>,
    chunks: Option<tokio::sync::mpsc::Sender<Result<Vec<u8>, HttpError>>>,
    bytes: usize,
}

impl std::fmt::Debug for HttpSink {
    /// Writes the byte cap and the bytes taken so far.
    /// The body is not shown.
    /// Output: the debug text.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let bytes = self
            .state
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .bytes;
        f.debug_struct("HttpSink")
            .field("cap", &self.cap)
            .field("bytes", &bytes)
            .finish_non_exhaustive()
    }
}

impl HttpSink {
    pub(crate) fn open(
        head: tokio::sync::oneshot::Sender<crate::host_transport::Head>,
        chunks: tokio::sync::mpsc::Sender<Result<Vec<u8>, HttpError>>,
        cap: usize,
    ) -> HttpSink {
        HttpSink {
            state: Mutex::new(SinkState {
                head: Some(head),
                chunks: Some(chunks),
                bytes: 0,
            }),
            cap,
        }
    }

    pub(crate) fn close(&self) {
        let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
        state.head = None;
        state.chunks = None;
    }

    /// Reports the response head: `status`, the `url` it came from (the
    /// request URL, since redirects are never followed), and `headers`.
    /// Call it once, before any chunk; a second is ignored. Output: none.
    pub fn head(&self, status: u16, url: String, headers: Vec<HostHeader>) {
        let sender = self
            .state
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .head
            .take();
        if let Some(sender) = sender {
            let headers = headers.into_iter().map(|h| (h.name, h.value)).collect();
            let _ = sender.send(crate::host_transport::Head {
                status,
                url,
                headers,
            });
        }
    }

    /// Reports the next part of the response body. Call it from the
    /// host's own thread, after `head`. It blocks while the reader is
    /// behind, and bytes past the reply cap end the body. Output: true
    /// while the core reads on; false once the sink is closed, the reply
    /// is over the cap or `head` was not sent, and the host must then stop
    /// reading and cancel the request.
    pub fn chunk(&self, bytes: Vec<u8>) -> bool {
        let (sender, over) = {
            let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
            if state.head.is_some() {
                return false;
            }
            let total = state.bytes.saturating_add(bytes.len());
            let over = total > self.cap;
            state.bytes = total.min(self.cap.saturating_add(1));
            let sender = if over {
                state.chunks.take()
            } else {
                state.chunks.clone()
            };
            (sender, over)
        };
        let Some(sender) = sender else {
            return false;
        };
        if over {
            let _ = sender.blocking_send(Err(HttpError::new(
                HttpErrorKind::Rejected,
                "host transport reply is over the reply cap",
            )));
            return false;
        }
        sender.blocking_send(Ok(bytes)).is_ok()
    }
}

/// Runs `call`, a read of host code, and turns a panic in it into a
/// `Panic` error naming `what`, so no host panic crosses the boundary.
/// Output: what `call` returns, or that error.
pub(crate) fn guarded<T>(
    what: &str,
    call: impl FnOnce() -> Result<T, FfiError>,
) -> Result<T, FfiError> {
    match catch_unwind(AssertUnwindSafe(call)) {
        Ok(result) => result,
        Err(_) => Err(FfiError::new(
            ErrorCode::Panic,
            format!("host {what} panicked"),
        )),
    }
}

/// Checks host tool `host` and wraps it as a kit tool: name 1 to 64
/// bytes of `[a-z0-9_]`, description at most 16 KiB, schema a JSON
/// object schema with `"type": "object"` that compiles. Output: the
/// tool, or `InvalidArgument` naming the bad part.
pub(crate) fn wrap_tool(host: Arc<dyn HostTool>) -> Result<Arc<dyn Tool>, FfiError> {
    let tool: Arc<dyn Tool> = Arc::new(host_tool(host)?);
    Ok(tool)
}

/// Checks host tool `host` as `wrap_tool` does and returns the adapter
/// itself, for a graph step that takes a tool by value.
/// Output: the adapter, or `InvalidArgument` naming the bad part.
pub(crate) fn host_tool(host: Arc<dyn HostTool>) -> Result<HostToolAdapter, FfiError> {
    let name = host.name();
    let name_ok = !name.is_empty()
        && name.len() <= MAX_TOOL_NAME_BYTES
        && name
            .bytes()
            .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_');
    if !name_ok {
        return Err(FfiError::new(
            ErrorCode::InvalidArgument,
            "host tool name must be 1 to 64 bytes of [a-z0-9_]",
        ));
    }
    let description = host.description();
    if description.len() > MAX_TOOL_DESCRIPTION_BYTES {
        return Err(FfiError::new(
            ErrorCode::InvalidArgument,
            format!("host tool `{name}` description is over 16 KiB"),
        ));
    }
    let schema_text = host.schema_json();
    crate::runtime::check_input("host tool schema", &schema_text)?;
    let schema = match serde_json::from_str::<Value>(&schema_text) {
        Ok(schema @ Value::Object(_)) => schema,
        _ => {
            return Err(FfiError::new(
                ErrorCode::InvalidArgument,
                format!("host tool `{name}` schema must be a JSON object"),
            ))
        }
    };
    if let Err(why) = troupekit_tools::compile_schema(&schema) {
        return Err(FfiError::new(
            ErrorCode::InvalidArgument,
            format!("host tool `{name}` schema: {why}"),
        ));
    }
    assert!(schema.is_object());
    let spec = ToolSpec {
        name,
        description,
        schema,
        hosted: None,
    };
    Ok(HostToolAdapter { host, spec })
}

pub(crate) struct HostToolAdapter {
    host: Arc<dyn HostTool>,
    spec: ToolSpec,
}

#[async_trait]
impl Tool for HostToolAdapter {
    fn spec(&self) -> &ToolSpec {
        &self.spec
    }

    async fn call(&self, args: Value, _ctx: &ToolContext) -> Result<ToolOutput, ToolError> {
        let host = self.host.clone();
        let args_json = args.to_string();
        let called = tokio::task::spawn_blocking(move || {
            catch_unwind(AssertUnwindSafe(|| host.call(args_json)))
        })
        .await;
        let panicked = || ToolOutput::error(format!("host tool `{}` panicked", self.spec.name));
        let output = match called {
            Ok(Ok(Ok(text))) => ToolOutput::text(text),
            Ok(Ok(Err(error))) => ToolOutput::error(error.message),
            Ok(Err(_)) | Err(_) => panicked(),
        };
        if output.content.len() > MAX_TOOL_OUTPUT_BYTES {
            return Ok(ToolOutput::error("tool output over 1 MiB"));
        }
        assert!(output.content.len() <= MAX_TOOL_OUTPUT_BYTES);
        Ok(output)
    }
}

/// Checks host embedder `host` and wraps it as a kit embedder: `dims`
/// read once, 1 to 65 536. Each call sends at most 256 texts to the host
/// and checks every batch's count, length and finite values.
/// Output: the embedder, or `InvalidArgument` for a bad `dims`.
#[cfg(feature = "knowledge")]
pub(crate) fn wrap_embedder(
    host: Arc<dyn HostEmbedder>,
) -> Result<Arc<dyn troupekit_context::embed::Embedder>, FfiError> {
    let dims = host.dims();
    if dims == 0 || dims > MAX_EMBED_DIMS {
        return Err(FfiError::new(
            ErrorCode::InvalidArgument,
            "host embedder dims must be 1 to 65536",
        ));
    }
    assert!(dims > 0);
    Ok(Arc::new(HostEmbedderAdapter {
        host,
        dims: dims as usize,
    }))
}

#[cfg(feature = "knowledge")]
struct HostEmbedderAdapter {
    host: Arc<dyn HostEmbedder>,
    dims: usize,
}

#[cfg(feature = "knowledge")]
#[async_trait]
impl troupekit_context::embed::Embedder for HostEmbedderAdapter {
    fn dims(&self) -> usize {
        self.dims
    }

    async fn embed(
        &self,
        texts: &[String],
    ) -> Result<Vec<Vec<f32>>, troupekit_context::ContextError> {
        use troupekit_context::ContextError;
        let mut vectors = Vec::with_capacity(texts.len());
        for batch in texts.chunks(MAX_EMBED_BATCH) {
            let host = self.host.clone();
            let sent = batch.to_vec();
            let called = tokio::task::spawn_blocking(move || {
                catch_unwind(AssertUnwindSafe(|| host.embed(sent)))
            })
            .await;
            let got = match called {
                Ok(Ok(Ok(got))) => got,
                Ok(Ok(Err(error))) => {
                    return Err(ContextError::Embed(format!(
                        "host embedder: {}",
                        error.message
                    )))
                }
                Ok(Err(_)) | Err(_) => {
                    return Err(ContextError::Embed("host embedder panicked".into()))
                }
            };
            if got.len() != batch.len() {
                return Err(ContextError::Embed(format!(
                    "host embedder returned {} vectors for {} texts",
                    got.len(),
                    batch.len()
                )));
            }
            if got
                .iter()
                .any(|v| v.len() != self.dims || v.iter().any(|x| !x.is_finite()))
            {
                return Err(ContextError::Embed(format!(
                    "host embedder vectors must be {} finite numbers",
                    self.dims
                )));
            }
            vectors.extend(got);
        }
        assert_eq!(vectors.len(), texts.len());
        Ok(vectors)
    }
}