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
//! # funera-orchestrate
//!
//! **Easy-to-use orchestration layer for [funera-core].**
//!
//! This crate provides a high-level agent API (`Agent`) and a runtime container
//! (`AgentRuntime`) that together let you integrate funera's LLM agent runtime
//! into your own projects with minimal boilerplate.
//!
//! ## Features
//!
//! | Feature | Default | Description |
//! |---------|---------|-------------|
//! | `deepseek` | ✅ | DeepSeek provider |
//! | `openai` | ❌ | OpenAI provider |
//! | `tool` | ✅ | Tool system (trait, registry, executor) |
//! | `funera-builtin-tools` | ❌ | Built-in tools (Read, Write, Edit, Shell) |
//! | `security` | ❌ | Tool policy enforcement |
//! | `middleware` | ❌ | Event interception pipeline (Inspector + Mutator) |
//! | `skill` | ❌ | Skill loading and prompt injection |
//! | `sandbox` | ❌ | Kernel-level subprocess isolation |
//!
//! ---
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! use funera_orchestrate::{Agent, AgentRuntime, DeepSeekProvider};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let runtime = AgentRuntime::<DeepSeekProvider>::builder()
//! .api_key(std::env::var("DEEPSEEK_API_KEY")?)
//! .model("deepseek-v4-flash")
//! .build()?;
//!
//! let agent = Agent::builder()
//! .system_prompt("You are a helpful assistant.")
//! .build();
//!
//! let resp = agent.fire("Hello!", &runtime).await?;
//! println!("{}", resp.content);
//! Ok(())
//! }
//! ```
//!
//! ## Core Concepts
//!
//! | Concept | Type | Description |
//! |---------|------|-------------|
//! | **Runtime** | [`AgentRuntime`] | Shared infrastructure + conversation session |
//! | **Agent** | [`Agent`] | Behavioural config (system prompt, callbacks) |
//! | **One-shot** | [`Agent::fire`] | Temporary session, discarded after call |
//! | **Multi-turn** | [`Agent::send`] | Persistent session across calls |
//! | **Streaming** | [`fire_stream`](Agent::fire_stream) / [`send_stream`](Agent::send_stream) | Token-by-token streaming |
//! | **Approval** | [`ApprovalHandle`] | Lightweight cloneable handle for tool-call approval |
//!
//! ## Examples
//!
//! ### One-shot query with stream
//!
//! ```rust,no_run
//! # use funera_orchestrate::{Agent, AgentEvent, AgentRuntime, DeepSeekProvider};
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let runtime = AgentRuntime::<DeepSeekProvider>::builder()
//! .api_key(std::env::var("DEEPSEEK_API_KEY")?)
//! .model("deepseek-v4-flash")
//! .build()?;
//!
//! let agent = Agent::builder()
//! .on_token(|t| print!("{t}"))
//! .build();
//!
//! let mut rx = agent.fire_stream("Explain Rust's ownership model", &runtime).await?;
//! while let Some(event) = rx.recv().await {
//! if let AgentEvent::Text(t) = event {
//! print!("{t}");
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ### Multi-turn conversation with callbacks
//!
//! ```rust,no_run
//! # use funera_orchestrate::{Agent, AgentRuntime, DeepSeekProvider};
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let runtime = AgentRuntime::<DeepSeekProvider>::builder()
//! .api_key(std::env::var("DEEPSEEK_API_KEY")?)
//! .model("deepseek-v4-flash")
//! .build()?;
//!
//! let agent = Agent::builder()
//! .system_prompt("You are helpful.")
//! .on_tool_call(|name, _| eprintln!("[tool] {name}"))
//! .on_turn_start(|| eprintln!("--- turn ---"))
//! .build();
//!
//! let handle = agent.send("Hi, I'm Alice.", runtime).await?;
//! let (runtime, _resp) = handle.await?;
//! let handle = agent.send("What's my name?", runtime).await?;
//! let (_runtime, _resp) = handle.await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Switching models on the same provider
//!
//! ```rust,no_run
//! # use funera_orchestrate::{Agent, AgentRuntime, DeepSeekProvider};
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let fast = AgentRuntime::<DeepSeekProvider>::builder()
//! .api_key(std::env::var("DEEPSEEK_API_KEY")?)
//! .model("deepseek-v4-flash")
//! .build()?;
//!
//! let powerful = AgentRuntime::<DeepSeekProvider>::builder()
//! .api_key(std::env::var("DEEPSEEK_API_KEY")?)
//! .model("deepseek-r1")
//! .build()?;
//!
//! let agent = Agent::builder().build();
//!
//! let (fast, _) = agent.send("Hello", fast).await?.await?; // fast model
//! agent.fire("What is Rust?", &powerful).await?; // powerful model (temp)
//! let (_fast, _) = agent.send("Tell me more", fast).await?.await?; // back to fast
//! # Ok(())
//! # }
//! ```
//!
//! ### Security with tool-call approval
//!
//! Requires the `security` feature (and optionally `funera-builtin-tools`, `sandbox`).
//! Use [`ApprovalHandle`] to approve or reject tool calls from a spawned task
//! while the agent is running — works with `fire()`, `send()`, and `send_stream()`.
//!
//! ```rust,no_run
//! # use funera_orchestrate::{Agent, AgentRuntime, ApprovalHandle, DeepSeekProvider};
//! # use std::time::Duration;
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let (approval_tx, mut approval_rx) = tokio::sync::mpsc::unbounded_channel();
//!
//! let runtime = AgentRuntime::<DeepSeekProvider>::builder()
//! .api_key(std::env::var("DEEPSEEK_API_KEY")?)
//! .model("deepseek-v4-flash")
//! .on_approval_required(move |call_id, tool, reason| {
//! eprintln!("[{tool}] needs approval: {reason}");
//! let _ = approval_tx.send(call_id.to_string());
//! })
//! .with_approval_timeout(Duration::from_secs(30))
//! .build()?;
//!
//! // Clone the ApprovalHandle *before* send() consumes the runtime.
//! let approver: ApprovalHandle = runtime.approval_handle();
//! tokio::spawn(async move {
//! while let Some(call_id) = approval_rx.recv().await {
//! approver.approve_tool_call(&call_id, true).await.ok();
//! }
//! });
//!
//! let agent = Agent::builder().build();
//! let (_runtime, resp) = agent.send("do something", runtime).await?.await?;
//! println!("{}", resp.content);
//! # Ok(())
//! # }
//! ```
//!
//! ## Module Structure
//!
//! - [`runtime`] — [`AgentRuntimeBuilder`] and [`AgentRuntime`]
//! - [`agent`] — [`AgentBuilder`] and [`Agent`]
//! - [`send_handle`] — [`SendHandle`], [`SendStreamHandle`], [`FireStreamHandle`], [`ApprovalHandle`]
//! - [`dispatcher`] — Event bus subscription and callback dispatch
//! - [`event`] — [`AgentEvent`] enum
//! - [`response`] — [`ChatResponse`] and [`ToolCallInfo`]
//! - [`error`] — [`OrchestrateError`]
pub use ;
pub use CallbackRegistry;
pub use OrchestrateError;
pub use ;
pub use DeepSeekProvider;
pub use OpenAIProvider;
pub use ;
pub use ;
pub use ApprovalHandle;
pub use ;
// Re-export security policy types for convenience.
pub use ;
pub use ;
// Re-export core event types for direct access
pub use EnvStateEvent;
pub use ;
pub use TokenEvent;
/// Middleware 相关的类型和 trait。
///
/// 该模块提供了 [`InspectorMiddleware`]、[`MutatorMiddleware`] 等核心 trait,
/// 以及 [`MiddlewareChain`]、[`MiddlewareBundle`] 等构建管道所需的类型。
///
/// ## Feature gate
///
/// 需要启用 `middleware` feature:
///
/// ```toml
/// funera-orchestrate = { features = ["middleware"] }
/// ```