Skip to main content

Schema

Struct Schema 

Source
pub struct Schema { /* private fields */ }
Expand description

JSON Schema body for an LLM tool’s parameters (type / properties / required / …).

Implementations§

Source§

impl Schema

Source

pub fn object() -> Self

Object schema ({"type":"object",…}). Default for tool parameters.

Examples found in repository?
examples/full.rs (line 12)
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_tool(
27        phi::Tool::new_async(
28            "async-echo",
29            "Echo the input back (async handler)",
30            phi::Schema::object()
31                .property("text", phi::Schema::string())
32                .required(["text"]),
33            |args| async move {
34                // Yield once: proves the SDK's runtime drives the future
35                // rather than just polling a ready block.
36                tokio::task::yield_now().await;
37                let text = String::from_utf8_lossy(&args);
38                Ok(phi::ToolResult {
39                    content: format!("async echo: {text}"),
40                    ..Default::default()
41                })
42            },
43        )
44        .timeout_sec(10),
45    );
46
47    m.register_command(
48        "ask",
49        phi::Command::new("Ask a yes/no question", |_args, ctx| {
50            let reply = ctx.confirm("Confirm?", "Proceed with /tmp/x?");
51            if reply.ok {
52                ctx.notify("info", "Confirmed!");
53            } else {
54                ctx.notify("warning", "Declined.");
55            }
56            ctx.submit("follow-up from ask");
57            Ok(())
58        }),
59    );
60
61    m.run()
62}
Source

pub fn string() -> Self

Examples found in repository?
examples/full.rs (line 13)
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_tool(
27        phi::Tool::new_async(
28            "async-echo",
29            "Echo the input back (async handler)",
30            phi::Schema::object()
31                .property("text", phi::Schema::string())
32                .required(["text"]),
33            |args| async move {
34                // Yield once: proves the SDK's runtime drives the future
35                // rather than just polling a ready block.
36                tokio::task::yield_now().await;
37                let text = String::from_utf8_lossy(&args);
38                Ok(phi::ToolResult {
39                    content: format!("async echo: {text}"),
40                    ..Default::default()
41                })
42            },
43        )
44        .timeout_sec(10),
45    );
46
47    m.register_command(
48        "ask",
49        phi::Command::new("Ask a yes/no question", |_args, ctx| {
50            let reply = ctx.confirm("Confirm?", "Proceed with /tmp/x?");
51            if reply.ok {
52                ctx.notify("info", "Confirmed!");
53            } else {
54                ctx.notify("warning", "Declined.");
55            }
56            ctx.submit("follow-up from ask");
57            Ok(())
58        }),
59    );
60
61    m.run()
62}
Source

pub fn number() -> Self

Source

pub fn integer() -> Self

Source

pub fn boolean() -> Self

Source

pub fn array(items: Schema) -> Self

Array schema. items must be a builder schema (not Schema::raw).

Source

pub fn raw(json: impl Into<Vec<u8>>) -> Self

Opaque JSON Schema bytes (the previous Vec<u8> API).

Source

pub fn description(self, d: impl Into<String>) -> Self

Source

pub fn property(self, name: impl Into<String>, schema: Schema) -> Self

Add an object property. No-op on non-object / raw schemas.

Examples found in repository?
examples/full.rs (line 13)
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_tool(
27        phi::Tool::new_async(
28            "async-echo",
29            "Echo the input back (async handler)",
30            phi::Schema::object()
31                .property("text", phi::Schema::string())
32                .required(["text"]),
33            |args| async move {
34                // Yield once: proves the SDK's runtime drives the future
35                // rather than just polling a ready block.
36                tokio::task::yield_now().await;
37                let text = String::from_utf8_lossy(&args);
38                Ok(phi::ToolResult {
39                    content: format!("async echo: {text}"),
40                    ..Default::default()
41                })
42            },
43        )
44        .timeout_sec(10),
45    );
46
47    m.register_command(
48        "ask",
49        phi::Command::new("Ask a yes/no question", |_args, ctx| {
50            let reply = ctx.confirm("Confirm?", "Proceed with /tmp/x?");
51            if reply.ok {
52                ctx.notify("info", "Confirmed!");
53            } else {
54                ctx.notify("warning", "Declined.");
55            }
56            ctx.submit("follow-up from ask");
57            Ok(())
58        }),
59    );
60
61    m.run()
62}
Source

pub fn required<I, S>(self, names: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Mark object property names as required.

Examples found in repository?
examples/full.rs (line 14)
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_tool(
27        phi::Tool::new_async(
28            "async-echo",
29            "Echo the input back (async handler)",
30            phi::Schema::object()
31                .property("text", phi::Schema::string())
32                .required(["text"]),
33            |args| async move {
34                // Yield once: proves the SDK's runtime drives the future
35                // rather than just polling a ready block.
36                tokio::task::yield_now().await;
37                let text = String::from_utf8_lossy(&args);
38                Ok(phi::ToolResult {
39                    content: format!("async echo: {text}"),
40                    ..Default::default()
41                })
42            },
43        )
44        .timeout_sec(10),
45    );
46
47    m.register_command(
48        "ask",
49        phi::Command::new("Ask a yes/no question", |_args, ctx| {
50            let reply = ctx.confirm("Confirm?", "Proceed with /tmp/x?");
51            if reply.ok {
52                ctx.notify("info", "Confirmed!");
53            } else {
54                ctx.notify("warning", "Declined.");
55            }
56            ctx.submit("follow-up from ask");
57            Ok(())
58        }),
59    );
60
61    m.run()
62}
Source

pub fn additional_properties(self, allow: bool) -> Self

Source

pub fn enum_values<I, S>(self, values: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Restrict a string schema to an enum (Codex-style compact enums).

Source

pub fn to_json_bytes(&self) -> Vec<u8>

Serialize to JSON Schema bytes for RegisterTool.

Trait Implementations§

Source§

impl Clone for Schema

Source§

fn clone(&self) -> Schema

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Schema

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&[u8]> for Schema

Source§

fn from(json: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Schema

Source§

fn from(json: Vec<u8>) -> Self

Converts to this type from the input type.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.