-
Notifications
You must be signed in to change notification settings - Fork 442
fix(tool): remove unnecessary schema validation #375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,7 +15,7 @@ pub use extension::*; | |||||||||||||||||
| pub use meta::*; | ||||||||||||||||||
| pub use prompt::*; | ||||||||||||||||||
| pub use resource::*; | ||||||||||||||||||
| use serde::{Deserialize, Serialize}; | ||||||||||||||||||
| use serde::{Deserialize, Serialize, de::DeserializeOwned}; | ||||||||||||||||||
| use serde_json::Value; | ||||||||||||||||||
| pub use tool::*; | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -1260,12 +1260,32 @@ impl CallToolResult { | |||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /// Validate that content or structured content is provided | ||||||||||||||||||
| pub fn validate(&self) -> Result<(), &'static str> { | ||||||||||||||||||
| match (&self.content, &self.structured_content) { | ||||||||||||||||||
| (None, None) => Err("either content or structured_content must be provided"), | ||||||||||||||||||
| _ => Ok(()), | ||||||||||||||||||
| /// Convert the `structured_content` part of response into a certain type. | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// # About json schema validation | ||||||||||||||||||
| /// Since rust is a strong type language, we don't need to do json schema validation here. | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// But if you do have to validate the response data, you can use [`jsonschema`](https://crates.io/crates/jsonschema) crate. | ||||||||||||||||||
| pub fn into_typed<T>(self) -> Result<T, serde_json::Error> | ||||||||||||||||||
| where | ||||||||||||||||||
| T: DeserializeOwned, | ||||||||||||||||||
| { | ||||||||||||||||||
| let raw_text = match (self.structured_content, &self.content) { | ||||||||||||||||||
| (Some(value), _) => return serde_json::from_value(value), | ||||||||||||||||||
| (None, Some(contents)) => { | ||||||||||||||||||
| if let Some(text) = contents.first().and_then(|c| c.as_text()) { | ||||||||||||||||||
| let text = &text.text; | ||||||||||||||||||
| Some(text) | ||||||||||||||||||
|
||||||||||||||||||
| Some(text) | |
| // Collect all text content from the vector | |
| let texts: Vec<&str> = contents.iter() | |
| .filter_map(|c| c.as_text().map(|t| t.text.as_str())) | |
| .collect(); | |
| if !texts.is_empty() { | |
| // Concatenate all text content with a space separator | |
| Some(texts.join(" ")) |
Copilot
AI
Aug 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line will always attempt to deserialize null into type T, which will likely fail for most types. Consider returning an error indicating that no structured content or valid text content was found instead of trying to deserialize null.
| serde_json::from_value(serde_json::Value::Null) | |
| Err(serde_json::Error::custom("No structured content or valid text content found")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method only considers the first content item when extracting text. This behavior should be documented in the method's docstring to clarify that only the first text content will be used for deserialization.