Skip to main content

troupekit_ffi/
graph.rs

1use crate::agent::AgentBuilder;
2use crate::error::TroupekitError;
3use crate::host::{HostTool, ToolBridge, TraceBridge, TraceListener};
4use crate::records::{GraphResult, Progress};
5use crate::stream::RunStream;
6use std::sync::Arc;
7use troupekit_ffi_core as neutral;
8
9/// Builds a graph of agents and tool steps, as the kit's `Graph` does.
10/// Wraps the neutral `GraphBuilder`; every setter returns a new builder and
11/// forwards unchanged. `build` validates the graph.
12#[derive(Debug, uniffi::Object)]
13pub struct GraphBuilder(Arc<neutral::GraphBuilder>);
14
15impl GraphBuilder {
16    fn wrap(inner: Arc<neutral::GraphBuilder>) -> Arc<GraphBuilder> {
17        Arc::new(GraphBuilder(inner))
18    }
19}
20
21#[uniffi::export]
22impl GraphBuilder {
23    #[uniffi::constructor]
24    pub fn new(name: String) -> Arc<Self> {
25        Self::wrap(neutral::GraphBuilder::new(name))
26    }
27
28    pub fn agent(&self, id: String, agent: Arc<AgentBuilder>) -> Arc<Self> {
29        Self::wrap(self.0.agent(id, agent.0.clone()))
30    }
31
32    pub fn step(&self, id: String, tool: Arc<dyn HostTool>) -> Arc<Self> {
33        Self::wrap(self.0.step(id, Arc::new(ToolBridge(tool))))
34    }
35
36    pub fn edge(&self, from: String, to: String) -> Arc<Self> {
37        Self::wrap(self.0.edge(from, to))
38    }
39
40    pub fn sqlite(&self, path: String) -> Arc<Self> {
41        Self::wrap(self.0.sqlite(path))
42    }
43
44    pub fn max_steps(&self, steps: u32) -> Arc<Self> {
45        Self::wrap(self.0.max_steps(steps))
46    }
47
48    pub fn retries(&self, retries: u32) -> Arc<Self> {
49        Self::wrap(self.0.retries(retries))
50    }
51
52    pub fn tracing(&self, on: bool) -> Arc<Self> {
53        Self::wrap(self.0.tracing(on))
54    }
55
56    pub fn trace(&self, categories: Vec<String>) -> Arc<Self> {
57        Self::wrap(self.0.trace(categories))
58    }
59
60    pub fn trace_content(&self, on: bool) -> Arc<Self> {
61        Self::wrap(self.0.trace_content(on))
62    }
63
64    pub fn trace_jsonl(&self, path: String) -> Arc<Self> {
65        Self::wrap(self.0.trace_jsonl(path))
66    }
67
68    pub fn trace_listener(&self, listener: Arc<dyn TraceListener>) -> Arc<Self> {
69        Self::wrap(self.0.trace_listener(Arc::new(TraceBridge(listener))))
70    }
71
72    pub fn laws(&self, path: String) -> Arc<Self> {
73        Self::wrap(self.0.laws(path))
74    }
75
76    pub fn law(&self, text: String) -> Arc<Self> {
77        Self::wrap(self.0.law(text))
78    }
79
80    pub fn auto_laws(&self, on: bool) -> Arc<Self> {
81        Self::wrap(self.0.auto_laws(on))
82    }
83
84    pub async fn build(&self) -> Result<Arc<GraphHandle>, TroupekitError> {
85        Ok(Arc::new(GraphHandle(self.0.build().await?)))
86    }
87}
88
89/// A built graph: runs, starts, resumes and streams, as the kit's `BuiltGraph`.
90/// Wraps the neutral `GraphHandle`; every method forwards unchanged.
91/// Safe to share between threads.
92#[derive(Debug, uniffi::Object)]
93pub struct GraphHandle(Arc<neutral::GraphHandle>);
94
95#[uniffi::export]
96impl GraphHandle {
97    pub async fn run(&self, input: String) -> Result<String, TroupekitError> {
98        Ok(self.0.run(input).await?)
99    }
100
101    pub async fn run_result(&self, input: String) -> Result<GraphResult, TroupekitError> {
102        Ok(self.0.run_result(input).await?.into())
103    }
104
105    pub async fn start(&self, input: String) -> Result<String, TroupekitError> {
106        Ok(self.0.start(input).await?)
107    }
108
109    pub async fn resume(&self, run_id: String) -> Result<String, TroupekitError> {
110        Ok(self.0.resume(run_id).await?)
111    }
112
113    pub async fn resume_for(
114        &self,
115        run_id: String,
116        budget_ms: u64,
117    ) -> Result<Progress, TroupekitError> {
118        Ok(self.0.resume_for(run_id, budget_ms).await?.into())
119    }
120
121    pub async fn stream(&self, input: String) -> Result<Arc<RunStream>, TroupekitError> {
122        Ok(Arc::new(RunStream(self.0.stream(input).await?)))
123    }
124}