Skip to main content

Command

Struct Command 

Source
pub struct Command {
    pub description: String,
    pub handler: Box<dyn FnMut(&str, &mut Context<'_>) -> Result<(), String>>,
}
Expand description

A slash command. The handler receives the raw argument string and a Context for host interaction (notify / confirm / submit / …).

Fields§

§description: String§handler: Box<dyn FnMut(&str, &mut Context<'_>) -> Result<(), String>>

Implementations§

Source§

impl Command

Source

pub fn new( description: impl Into<String>, handler: impl FnMut(&str, &mut Context<'_>) -> Result<(), String> + 'static, ) -> Self

Examples found in repository?
examples/full.rs (lines 28-37)
5fn main() -> Result<(), phi::Error> {
6    let mut m = phi::Extension::new("full", "0.1.0");
7
8    m.register_tool(
9        phi::Tool::new(
10            "echo",
11            "Echo the input back",
12            phi::Schema::object()
13                .property("text", phi::Schema::string())
14                .required(["text"]),
15            |args| {
16                let text = String::from_utf8_lossy(args);
17                Ok(phi::ToolResult {
18                    content: format!("echo: {text}"),
19                    ..Default::default()
20                })
21            },
22        )
23        .detail_from_args(|args| String::from_utf8_lossy(args).into_owned()),
24    );
25
26    m.register_command(
27        "ask",
28        phi::Command::new("Ask a yes/no question", |_args, ctx| {
29            let reply = ctx.confirm("Confirm?", "Proceed with /tmp/x?");
30            if reply.ok {
31                ctx.notify("info", "Confirmed!");
32            } else {
33                ctx.notify("warning", "Declined.");
34            }
35            ctx.submit("follow-up from ask");
36            Ok(())
37        }),
38    );
39
40    m.run()
41}
More examples
Hide additional examples
examples/hello.rs (lines 10-15)
5fn main() -> Result<(), phi::Error> {
6    let mut m = phi::Extension::new("hello", "0.1.0");
7
8    m.register_command(
9        "hello",
10        phi::Command::new("Say hi", |_args, ctx| {
11            ctx.notify("info", "Hello!");
12            // ctx.submit("follow-up"); // after /hello returns
13            // ctx.send_user_message("…"); // enqueue a turn anytime
14            Ok(())
15        }),
16    );
17
18    m.on_user_input(|_ev| {
19        // return Some(phi::UserInputResult { handled: true, ..Default::default() }) to swallow
20        // return Some(phi::UserInputResult { text: Some("rewritten".into()), ..Default::default() }) to transform
21        None
22    });
23
24    m.on_tool_call(|_ev| {
25        // return Some(phi::ToolCallResult { block: true, reason: "...".into(), ..Default::default() }) to deny
26        None
27    });
28
29    m.on_tool_result(|_ev| {
30        // return Some(phi::ToolResultResult { stop: true, ..Default::default() }) to end the agent loop
31        None
32    });
33
34    m.on_turn_stopping(|_ev| {
35        // return Some(phi::TurnStoppingResult { continue_: true, message: "check X".into(), ..Default::default() }) to steer
36        None
37    });
38
39    m.subscribe(pxb::Event::SessionStart, |ev| {
40        let _ = ev; // Reason, PreviousSessionID, …
41    });
42
43    m.run()
44}

Auto Trait Implementations§

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.