Skip to main content

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
27    where
28        Self::CompletionModel: 'static,
29    {
30        AgentBuilder::new(self.completion_model(model))
31    }
32
33    /// Construct a classic typed extractor builder for `model`.
34    fn extractor<T>(&self, model: impl Into<String>) -> ExtractorBuilder<T>
35    where
36        T: JsonSchema
37            + for<'de> Deserialize<'de>
38            + Serialize
39            + WasmCompatSend
40            + WasmCompatSync
41            + 'static,
42        Self::CompletionModel: 'static,
43    {
44        ExtractorBuilder::new(self.completion_model(model))
45    }
46}
47
48impl<C: rig_core::client::completion::CompletionClient> AgentClientExt for C {}
49
50/// Adds classic agent construction to every portable completion model.
51pub trait AgentModelExt: rig_core::completion::CompletionModel + Sized {
52    /// Convert this model into a classic agent builder.
53    fn into_agent_builder(self) -> AgentBuilder
54    where
55        Self: 'static,
56    {
57        AgentBuilder::new(self)
58    }
59}
60
61impl<M> AgentModelExt for M where M: rig_core::completion::CompletionModel {}