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;
pub trait EventListener: Send + Sync {
fn on_event(&self, event: FfiEvent);
}
pub trait TraceListener: Send + Sync {
fn on_record(&self, line: String);
}
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())
}))
}
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)
}
pub trait HostTool: Send + Sync {
fn name(&self) -> String;
fn description(&self) -> String;
fn schema_json(&self) -> String;
fn call(&self, args_json: String) -> Result<String, HostError>;
}
pub trait HostModel: Send + Sync {
fn id(&self) -> String;
fn capabilities(&self) -> HostModelCaps;
fn generate(&self, request: HostRequest, sink: Arc<TokenSink>) -> Result<HostReply, HostError>;
}
#[cfg(feature = "knowledge")]
pub trait HostEmbedder: Send + Sync {
fn dims(&self) -> u32;
fn embed(&self, texts: Vec<String>) -> Result<Vec<Vec<f32>>, HostError>;
}
pub trait HostTransport: Send + Sync {
fn send(&self, request: HostHttpRequest, sink: Arc<HttpSink>) -> Result<(), HostError>;
}
#[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 {
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);
}
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)
}
}
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 {
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;
}
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,
});
}
}
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()
}
}
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"),
)),
}
}
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)
}
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)
}
}
#[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)
}
}