Skip to main content

llm_tool/types/
resource.rs

1//! Resource definitions and read output types.
2
3use alloc::string::{String, ToString};
4
5use serde::{Deserialize, Serialize};
6
7/// Describes a resource or resource template available in the registry.
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct ResourceDefinition {
11    /// Resource URI (e.g. `file:///path` or `config://app`) or template pattern.
12    #[serde(rename = "uriTemplate")]
13    pub uri_template: String,
14    /// Human-readable name.
15    pub name: String,
16    /// Optional description.
17    #[serde(default, skip_serializing_if = "String::is_empty")]
18    pub description: String,
19    /// Optional MIME type.
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub mime_type: Option<String>,
22}
23
24/// Backwards-compatible type alias for [`ResourceDefinition`].
25pub type ResourceTemplateDefinition = ResourceDefinition;
26
27/// A content block inside a resource read output.
28#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
29#[serde(untagged)]
30pub enum ResourceOutputContent {
31    /// UTF-8 text content.
32    #[serde(rename_all = "camelCase")]
33    Text {
34        /// Resource URI.
35        uri: String,
36        /// Optional MIME type.
37        #[serde(skip_serializing_if = "Option::is_none")]
38        mime_type: Option<String>,
39        /// Text string.
40        text: String,
41    },
42    /// Base64 binary content.
43    #[serde(rename_all = "camelCase")]
44    Blob {
45        /// Resource URI.
46        uri: String,
47        /// Optional MIME type.
48        #[serde(skip_serializing_if = "Option::is_none")]
49        mime_type: Option<String>,
50        /// Base64 blob data.
51        blob: String,
52    },
53}
54
55/// The output returned by reading a resource.
56#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
57pub struct ResourceOutput {
58    /// Returned content blocks.
59    pub contents: alloc::vec::Vec<ResourceOutputContent>,
60}
61
62impl ResourceOutput {
63    /// Create a text resource output.
64    pub fn text(uri: impl Into<String>, mime_type: Option<&str>, text: impl Into<String>) -> Self {
65        Self {
66            contents: alloc::vec![ResourceOutputContent::Text {
67                uri: uri.into(),
68                mime_type: mime_type.map(ToString::to_string),
69                text: text.into(),
70            }],
71        }
72    }
73
74    /// Create a binary blob resource output.
75    pub fn blob(uri: impl Into<String>, mime_type: Option<&str>, blob: impl Into<String>) -> Self {
76        Self {
77            contents: alloc::vec![ResourceOutputContent::Blob {
78                uri: uri.into(),
79                mime_type: mime_type.map(ToString::to_string),
80                blob: blob.into(),
81            }],
82        }
83    }
84}