rig_agent/client.rs
1//! Classic runtime construction extensions for portable completion clients and models.
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6use crate::{agent::AgentBuilder, extractor::ExtractorBuilder};
7use rig_core::wasm_compat::{WasmCompatSend, WasmCompatSync};
8
9/// Classic-runtime construction sugar layered on any portable completion client.
10///
11/// Builds on `completion_model` / `CompletionModel` from its supertrait bound
12/// [`rig_core::client::completion::CompletionClient`] and adds the classic
13/// runtime's `agent` and `extractor` builders. The supertrait bound is what lets
14/// the default bodies call `self.completion_model(..)`, so nothing needs
15/// re-forwarding if the portable trait grows a method.
16///
17/// Provider authors implement the portable
18/// [`rig_core::client::completion::CompletionClient`]; this extension trait is
19/// blanket-implemented for every type that does. Callers need *both* traits in
20/// scope to use the full surface — importing `AgentClientExt` alone does not
21/// bring `completion_model` into method-resolution scope, since that method
22/// belongs to the supertrait. `use rig::prelude::*;` brings both in at once for
23/// the full `completion_model` + `agent` + `extractor` surface.
24pub trait AgentClientExt: rig_core::client::completion::CompletionClient {
25 /// Construct a classic agent builder for `model`.
26 fn agent(&self, model: impl Into<String>) -> AgentBuilder<Self::CompletionModel> {
27 AgentBuilder::new(self.completion_model(model))
28 }
29
30 /// Construct a classic typed extractor builder for `model`.
31 fn extractor<T>(&self, model: impl Into<String>) -> ExtractorBuilder<Self::CompletionModel, T>
32 where
33 T: JsonSchema
34 + for<'de> Deserialize<'de>
35 + Serialize
36 + WasmCompatSend
37 + WasmCompatSync
38 + 'static,
39 {
40 ExtractorBuilder::new(self.completion_model(model))
41 }
42}
43
44impl<C: rig_core::client::completion::CompletionClient> AgentClientExt for C {}
45
46/// Adds classic agent construction to every portable completion model.
47pub trait AgentModelExt: rig_core::completion::CompletionModel + Sized {
48 /// Convert this model into a classic agent builder.
49 fn into_agent_builder(self) -> AgentBuilder<Self> {
50 AgentBuilder::new(self)
51 }
52}
53
54impl<M> AgentModelExt for M where M: rig_core::completion::CompletionModel {}