#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct FfiUsage {
pub input_tokens: u64,
pub output_tokens: u64,
pub cost_usd_micros: u64,
}
#[non_exhaustive]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RunResult {
pub text: String,
pub output_json: String,
pub usage: FfiUsage,
pub model_calls: u32,
pub run_id: String,
}
impl RunResult {
pub(crate) fn from_outcome(outcome: &troupekit_kit::RunOutcome) -> RunResult {
RunResult {
text: outcome.text().to_string(),
output_json: outcome.output().to_string(),
usage: crate::stream::usage_of(outcome.usage()),
model_calls: outcome.model_calls(),
run_id: outcome.run_id().to_string(),
}
}
}
#[non_exhaustive]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct GraphResult {
pub output_json: String,
pub state_json: String,
pub usage: FfiUsage,
pub steps: u64,
pub run_id: String,
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Progress {
Done { text: String },
Pending { run_id: String },
}
impl Progress {
pub(crate) fn from_kit(progress: troupekit_kit::Progress) -> Result<Progress, crate::FfiError> {
match progress {
troupekit_kit::Progress::Done(text) => Ok(Progress::Done { text }),
troupekit_kit::Progress::Pending { run_id } => Ok(Progress::Pending {
run_id: run_id.to_string(),
}),
#[allow(unreachable_patterns)]
_ => Err(crate::FfiError::new(
crate::ErrorCode::Internal,
"resume_for returned a progress state this version does not know",
)),
}
}
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FfiPause {
Approval { node: String },
Question { node: String },
Budget { limit: String },
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FfiEventKind {
RunStarted {
workflow: String,
},
StepStarted {
step: String,
persona: Option<String>,
},
TextDelta {
text: String,
},
ToolCallStarted {
call_id: String,
name: String,
},
ToolCallFinished {
call_id: String,
is_error: bool,
},
StepFinished {
step: String,
usage: FfiUsage,
},
RunPaused {
reason: FfiPause,
},
RunFinished {
usage: FfiUsage,
},
RunFailed {
error: String,
},
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FfiEvent {
pub run_id: String,
pub seq: u64,
pub at_ms: i64,
pub kind: FfiEventKind,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HostModelCaps {
pub tools: bool,
pub json_schema: bool,
pub streaming: bool,
pub context_tokens: u32,
}
impl Default for HostModelCaps {
fn default() -> Self {
HostModelCaps {
tools: false,
json_schema: false,
streaming: false,
context_tokens: 4096,
}
}
}
#[non_exhaustive]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct HostMessage {
pub role: String,
pub text: String,
pub tool_calls_json: Option<String>,
pub tool_call_id: Option<String>,
}
#[non_exhaustive]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct HostRequest {
pub system: Option<String>,
pub messages: Vec<HostMessage>,
pub tools_json: Option<String>,
pub schema_json: Option<String>,
pub max_tokens: u32,
}
#[non_exhaustive]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct HostReply {
pub text: String,
pub input_tokens: u64,
pub output_tokens: u64,
pub tool_calls_json: Option<String>,
}
const SECRET_HEADERS: [&str; 4] = [
"authorization",
"proxy-authorization",
"x-api-key",
"x-goog-api-key",
];
#[non_exhaustive]
#[derive(Clone, Default, PartialEq, Eq)]
pub struct HostHeader {
pub name: String,
pub value: String,
pub sensitive: bool,
}
impl HostHeader {
pub fn new(name: impl Into<String>, value: impl Into<String>) -> HostHeader {
let name = name.into();
let sensitive = is_secret_name(&name);
HostHeader {
name,
value: value.into(),
sensitive,
}
}
fn hidden(&self) -> bool {
self.sensitive || is_secret_name(&self.name)
}
}
impl std::fmt::Debug for HostHeader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let value: &dyn std::fmt::Debug = if self.hidden() {
&"[redacted]"
} else {
&self.value
};
f.debug_struct("HostHeader")
.field("name", &self.name)
.field("value", value)
.field("sensitive", &self.sensitive)
.finish()
}
}
fn is_secret_name(name: &str) -> bool {
SECRET_HEADERS
.iter()
.any(|secret| name.eq_ignore_ascii_case(secret))
}
#[non_exhaustive]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct HostHttpRequest {
pub method: String,
pub url: String,
pub headers: Vec<HostHeader>,
pub body: Vec<u8>,
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("{message}")]
pub struct HostError {
pub message: String,
}
impl HostError {
pub fn new(message: impl Into<String>) -> HostError {
HostError {
message: message.into(),
}
}
}
#[cfg(feature = "knowledge")]
#[non_exhaustive]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct KnowledgeConfig {
pub dir: String,
pub index: Option<String>,
pub store: Option<String>,
pub allow_failed: bool,
}
#[cfg(feature = "knowledge")]
impl KnowledgeConfig {
pub fn new(dir: impl Into<String>) -> KnowledgeConfig {
KnowledgeConfig {
dir: dir.into(),
..KnowledgeConfig::default()
}
}
}
#[cfg(feature = "knowledge")]
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MemoryConfig {
pub user: String,
pub session: String,
pub sqlite: Option<String>,
pub preamble: bool,
}
#[cfg(feature = "knowledge")]
impl MemoryConfig {
pub fn new(user: impl Into<String>, session: impl Into<String>) -> MemoryConfig {
MemoryConfig {
user: user.into(),
session: session.into(),
sqlite: None,
preamble: true,
}
}
}