pub struct Command {
pub description: String,
pub needs_args: bool,
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§needs_args: boolLeave /name in the composer on picker accept / bare submit.
handler: Box<dyn FnMut(&str, &mut Context<'_>) -> Result<(), String>>Implementations§
Source§impl Command
impl Command
Sourcepub fn new(
description: impl Into<String>,
handler: impl FnMut(&str, &mut Context<'_>) -> Result<(), String> + 'static,
) -> Self
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
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}Sourcepub fn needs_args(self) -> Self
pub fn needs_args(self) -> Self
Mark this command as requiring arguments so the host fills the composer instead of auto-submitting an empty invocation.
Auto Trait Implementations§
impl !RefUnwindSafe for Command
impl !Send for Command
impl !Sync for Command
impl !UnwindSafe for Command
impl Freeze for Command
impl Unpin for Command
impl UnsafeUnpin for Command
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more