use async_trait::async_trait;
use crate::error::ToolError;
use crate::tool::ToolFunction;
#[derive(Debug, Clone)]
pub struct VertexAiSearchConfig {
pub datastore: String,
pub filter: Option<String>,
pub max_results: usize,
}
#[derive(Debug, Clone)]
pub struct VertexAiSearchTool {
config: VertexAiSearchConfig,
}
impl VertexAiSearchTool {
pub fn new(config: VertexAiSearchConfig) -> Self {
Self { config }
}
pub fn datastore(&self) -> &str {
&self.config.datastore
}
}
#[async_trait]
impl ToolFunction for VertexAiSearchTool {
fn name(&self) -> &str {
"vertex_ai_search"
}
fn description(&self) -> &str {
"Search enterprise data using Vertex AI Search. Returns relevant documents \
and snippets from the configured data store."
}
fn parameters(&self) -> Option<serde_json::Value> {
Some(serde_json::json!({
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query."
}
},
"required": ["query"]
}))
}
async fn call(&self, args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
let query = args
.get("query")
.and_then(|v| v.as_str())
.ok_or_else(|| ToolError::InvalidArgs("Missing query".into()))?;
Ok(serde_json::json!({
"status": "search_requested",
"query": query,
"datastore": self.config.datastore,
"results": []
}))
}
}
pub type DiscoveryEngineSearchTool = VertexAiSearchTool;
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn test_config() -> VertexAiSearchConfig {
VertexAiSearchConfig {
datastore: "projects/my-proj/locations/global/collections/default_collection/dataStores/my-store".into(),
filter: None,
max_results: 10,
}
}
#[test]
fn tool_metadata() {
let tool = VertexAiSearchTool::new(test_config());
assert_eq!(tool.name(), "vertex_ai_search");
assert!(tool.parameters().is_some());
assert!(tool.datastore().contains("my-store"));
}
#[tokio::test]
async fn call_with_query() {
let tool = VertexAiSearchTool::new(test_config());
let result = tool
.call(json!({"query": "machine learning"}))
.await
.unwrap();
assert_eq!(result["query"], "machine learning");
}
#[tokio::test]
async fn missing_query() {
let tool = VertexAiSearchTool::new(test_config());
let result = tool.call(json!({})).await;
assert!(result.is_err());
}
}