Skip to main content

providers_kit/
hosted.rs

1//! Closed provider-hosted request vocabulary and normalized hosted output.
2//!
3//! Declare [`HostedTool`] through [`crate::generation::ProviderToolSpec`]. The exact
4//! selected Model checks support and renders the request; no tools-kit executor is
5//! involved. [`HostedToolOutput`] retains reported activity without inventing
6//! missing status or execution proof. Code declarations alone do not establish
7//! execution, and hosted runtime failures may remain accepted result data.
8
9use serde::{Deserialize, Serialize};
10
11use crate::error::RemoteFailure;
12
13pub use crate::generation::{Artifact, Citation};
14
15/// Closed provider-hosted request declaration.
16#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
17#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]
18pub enum HostedTool {
19    /// Provider-managed web search.
20    WebSearch,
21    /// Provider-managed URL retrieval from URLs present in the request.
22    UrlContext,
23    /// Provider-managed search over exact provider store identifiers.
24    FileSearch {
25        /// Exact provider-native store identifiers to search.
26        store_ids: Vec<String>,
27    },
28    /// Provider-managed code execution.
29    CodeExecution,
30}
31
32/// Provider-managed hosted execution status.
33#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
34#[serde(tag = "state", rename_all = "snake_case")]
35pub enum HostedToolStatus {
36    /// The provider is still executing the hosted tool.
37    InProgress,
38    /// The provider reports that the tool operation completed.
39    ///
40    /// Runtime results may still contain execution failures; completion alone
41    /// does not establish that generated code ran successfully.
42    Completed,
43    /// Hosted execution ended without completing successfully.
44    Incomplete,
45    /// Hosted execution failed.
46    Failed {
47        /// Classified accepted failure with retained native evidence.
48        error: RemoteFailure,
49    },
50}
51
52/// Native action performed by hosted web research.
53#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
54#[serde(tag = "type", rename_all = "snake_case")]
55pub enum HostedWebSearchAction {
56    /// A search, retaining only queries and sources actually reported by the provider.
57    Search {
58        /// Primary query, when identified separately.
59        query: Option<String>,
60        /// Search queries reported by the provider.
61        queries: Vec<String>,
62        /// URL-bearing sources considered by the provider.
63        sources: Vec<HostedWebSearchSource>,
64    },
65    /// A page opened by the provider.
66    OpenPage {
67        /// Target URL, when returned by the provider.
68        url: Option<String>,
69    },
70    /// Text located within a page.
71    FindInPage {
72        /// Native target URL.
73        url: String,
74        /// Native search pattern.
75        pattern: String,
76    },
77}
78
79/// Source attached to hosted web search.
80///
81/// # Examples
82///
83/// ```
84/// use providers_kit::hosted::HostedWebSearchSource;
85/// let value = HostedWebSearchSource { title: None, url: "https://example.com/source".into() };
86/// assert_eq!(value.url, "https://example.com/source");
87/// ```
88///
89#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
90pub struct HostedWebSearchSource {
91    /// Source title, when available.
92    pub title: Option<String>,
93    /// Absolute source URL.
94    pub url: String,
95}
96
97/// One hosted web-search result.
98///
99/// # Examples
100///
101/// ```
102/// use providers_kit::hosted::HostedWebSearchResult;
103/// let value = HostedWebSearchResult {
104///     title: None,
105///     url: "https://example.com/source".into(),
106///     snippet: Some("excerpt".into()),
107/// };
108/// assert_eq!(value.url, "https://example.com/source");
109/// ```
110///
111#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
112pub struct HostedWebSearchResult {
113    /// Result title, when available.
114    pub title: Option<String>,
115    /// Absolute result URL.
116    pub url: String,
117    /// Result excerpt, when available.
118    pub snippet: Option<String>,
119}
120
121/// Executable code generated by a hosted runtime.
122///
123/// A code declaration alone does not prove that execution occurred or succeeded.
124/// Inspect separately returned execution status, results, and artifacts.
125///
126/// # Examples
127///
128/// ```
129/// use providers_kit::hosted::HostedExecutableCode;
130/// let value = HostedExecutableCode { language: Some("python".into()), code: "print(1 + 1)".into() };
131/// assert_eq!(value.code, "print(1 + 1)");
132/// ```
133///
134#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
135pub struct HostedExecutableCode {
136    /// Programming language, when reported.
137    pub language: Option<String>,
138    /// Provider-returned executable source declaration; execution requires separate evidence.
139    pub code: String,
140}
141
142/// Result produced by hosted code execution.
143///
144/// # Examples
145///
146/// ```
147/// use providers_kit::hosted::HostedCodeExecutionResult;
148/// let value = HostedCodeExecutionResult {
149///     status: None,
150///     logs: None,
151///     stdout: Some("2\n".into()),
152///     stderr: None,
153///     exit_code: Some(0),
154/// };
155/// assert_eq!(value.exit_code, Some(0));
156/// ```
157///
158#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
159pub struct HostedCodeExecutionResult {
160    /// Provider-authored execution status, when available.
161    pub status: Option<HostedToolStatus>,
162    /// Combined or otherwise unclassified runtime output, when returned.
163    ///
164    /// This is distinct from provider-identified stdout and stderr.
165    pub logs: Option<String>,
166    /// Captured standard output, when returned.
167    pub stdout: Option<String>,
168    /// Captured standard error, when returned.
169    pub stderr: Option<String>,
170    /// Process exit code, when reported.
171    pub exit_code: Option<i64>,
172}
173
174/// One hosted file-search result.
175///
176/// # Examples
177///
178/// ```
179/// use providers_kit::hosted::HostedFileSearchResult;
180/// let value = HostedFileSearchResult {
181///     file_id: "file-1".into(),
182///     file_name: None,
183///     text: Some("excerpt".into()),
184///     score: None,
185/// };
186/// assert_eq!(value.file_id, "file-1");
187/// assert_eq!(value.text.as_deref(), Some("excerpt"));
188/// ```
189///
190#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
191pub struct HostedFileSearchResult {
192    /// Exact provider-native file identifier.
193    pub file_id: String,
194    /// File name, when returned.
195    pub file_name: Option<String>,
196    /// Matching text excerpt, when returned.
197    pub text: Option<String>,
198    /// Provider-reported relevance score, when returned.
199    pub score: Option<f64>,
200}
201
202/// One normalized provider-hosted operation or unpaired native activity.
203///
204/// Related invocation and result records are combined only when native identity
205/// or an unambiguous native ordering establishes the relationship. A result from
206/// an earlier turn can retain absent action or source fields. Opaque native replay
207/// remains in the returned assistant continuation, independently of this projection.
208#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
209#[serde(tag = "family", rename_all = "snake_case")]
210pub enum HostedToolOutput {
211    /// Provider-managed web-search output.
212    WebSearch {
213        /// Provider-authored execution status, when available.
214        status: Option<HostedToolStatus>,
215        /// Search action details, when returned.
216        action: Option<HostedWebSearchAction>,
217        /// Normalized search results.
218        results: Vec<HostedWebSearchResult>,
219        /// Citations returned by the provider.
220        citations: Vec<Citation>,
221    },
222    /// Provider-managed file-search output.
223    FileSearch {
224        /// Provider-authored execution status, when available.
225        status: Option<HostedToolStatus>,
226        /// Search queries issued by the provider.
227        queries: Vec<String>,
228        /// `None` means the provider omitted result details; `Some([])` means included-empty.
229        results: Option<Vec<HostedFileSearchResult>>,
230        /// Citations returned by the provider.
231        citations: Vec<Citation>,
232    },
233    /// Provider-managed code-execution output.
234    CodeExecution {
235        /// Provider-authored execution status, when available.
236        status: Option<HostedToolStatus>,
237        /// Provider-returned executable code declarations, independently of execution results.
238        code: Vec<HostedExecutableCode>,
239        /// Normalized execution results.
240        results: Vec<HostedCodeExecutionResult>,
241        /// Artifacts produced by execution.
242        artifacts: Vec<Artifact>,
243    },
244    /// Provider-managed URL-retrieval output.
245    UrlContext {
246        /// URL supplied to this tool invocation, when reported.
247        requested_url: Option<String>,
248        /// URL the provider attempted to retrieve, when reported.
249        retrieved_url: Option<String>,
250        /// Provider-authored execution status, when available.
251        status: Option<HostedToolStatus>,
252        /// Citations returned by the provider.
253        citations: Vec<Citation>,
254    },
255}