Skip to main content

Tool

Struct Tool 

Source
pub struct Tool {
    pub name: String,
    pub description: String,
    pub schema: Schema,
    pub timeout_sec: u32,
    pub execute: Box<dyn FnMut(&[u8]) -> Result<ToolResult, String>>,
}
Expand description

An LLM-callable tool. schema is a typed JSON Schema for parameters (same role as Go’s Parameters / Codex’s schemars-generated input schema).

Fields§

§name: String§description: String§schema: Schema§timeout_sec: u32

Host RPC wait for execute, in seconds. 0 = host default (30s).

§execute: Box<dyn FnMut(&[u8]) -> Result<ToolResult, String>>

Implementations§

Source§

impl Tool

Source

pub fn new( name: impl Into<String>, description: impl Into<String>, schema: impl Into<Schema>, execute: impl FnMut(&[u8]) -> Result<ToolResult, String> + 'static, ) -> Self

Examples found in repository?
examples/full.rs (lines 8-21)
5fn main() -> Result<(), phi::Error> {
6    let mut m = phi::Extension::new("full", "0.1.0");
7
8    m.register_tool(phi::Tool::new(
9        "echo",
10        "Echo the input back",
11        phi::Schema::object()
12            .property("text", phi::Schema::string())
13            .required(["text"]),
14        |args| {
15            let text = String::from_utf8_lossy(args);
16            Ok(phi::ToolResult {
17                content: format!("echo: {text}"),
18                ..Default::default()
19            })
20        },
21    ));
22
23    m.register_command(
24        "ask",
25        phi::Command::new("Ask a yes/no question", |_args, ctx| {
26            let reply = ctx.confirm("Confirm?", "Proceed with /tmp/x?");
27            if reply.ok {
28                ctx.notify("info", "Confirmed!");
29            } else {
30                ctx.notify("warning", "Declined.");
31            }
32            ctx.submit("follow-up from ask");
33            Ok(())
34        }),
35    );
36
37    m.run()
38}
Source

pub fn timeout_sec(self, secs: u32) -> Self

Sets how long the host waits for this tool’s result (1–3600; host clamps).

Auto Trait Implementations§

§

impl !RefUnwindSafe for Tool

§

impl !Send for Tool

§

impl !Sync for Tool

§

impl !UnwindSafe for Tool

§

impl Freeze for Tool

§

impl Unpin for Tool

§

impl UnsafeUnpin for Tool

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.