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
//! Code Mode for Rust agents. The model writes a script, ghostmode runs it in a sandbox, and
//! the host provides the tools the script calls. The model calls only `execute` and `poll`.
//!
//! Write a host, build a [`Sandbox`], and give the model the `execute` and `poll` definitions.
//! Pass each `execute` call's code to [`Sandbox::execute`] and send the [`Status`] JSON back:
//!
//! ```no_run
//! use futures_util::future::{BoxFuture, FutureExt};
//! use ghostmode::host::{self, Call, Host, Tool};
//! # #[cfg(feature = "monty")]
//! use ghostmode::backend::Monty;
//! use ghostmode::{Builder, schema};
//! use serde_json::{Value, json};
//!
//! struct Echo;
//!
//! impl Host for Echo {
//! fn tools(&self) -> Vec<Tool> {
//! let input_schema = json!({
//! "type": "object",
//! "properties": { "text": { "type": "string" } },
//! "required": ["text"],
//! });
//! vec![Tool::new("echo", "Returns the text you pass it.", input_schema)]
//! }
//!
//! fn call(&self, call: &Call) -> BoxFuture<'static, Result<Value, host::Error>> {
//! let text = call.args.get("text").cloned().ok_or_else(|| host::Error::Failed("text is required".into()));
//! async move { text }.boxed()
//! }
//! }
//!
//! # #[cfg(feature = "monty")]
//! # async fn execute_echo() {
//! let mut sandbox = Builder::new(Echo, Monty::default()).build().await;
//! let definitions = schema::mcp_definitions(sandbox.tools());
//! let status = sandbox.execute("echo(text='hello')", false).await;
//! let reply = serde_json::to_value(&status).unwrap();
//! # }
//! ```
//!
//! A sandbox runs scripts in a [`backend::Backend`]. Each feature adds a backend or a host:
//!
//! - `monty`, on by default, adds `backend::Monty`, which runs scripts inside the caller's
//! process. It isn't crash-isolated: a script can panic, abort or hang the caller's process.
//! - `monty-pool` adds `backend::MontyPool`, which runs each session in its own Monty worker
//! process. Untrusted scripts belong there.
//! - `rmcp` adds `mcp::Server`, a host that offers a connected MCP server's tools to scripts.
pub use ;
;