fix: 完善素材库检索功能的vertex搜索逻辑

修复内容:
- 使用现有的outfit_search_commands中的execute_vertex_ai_search服务
- 删除重复的vertex搜索实现代码
- 将convert_vertex_response_to_search_results函数设为公共
- 简化material_search_commands,直接复用现有服务

 现在素材检索功能可以正确调用Google VET API进行搜索
 编译通过,功能完整可用
This commit is contained in:
imeepos 2025-07-25 15:28:42 +08:00
parent 82d9ccfe21
commit 5d198c9909
2 changed files with 9 additions and 75 deletions

View File

@ -29,7 +29,7 @@ pub async fn generate_material_search_query(
let mut query_parts = Vec::new();
// 添加基础描述
query_parts.push("fashion model".to_string());
query_parts.push("model".to_string());
// 添加风格信息
if options.include_styles.unwrap_or(true) {
@ -227,34 +227,16 @@ pub async fn quick_material_search(
/// 执行素材搜索的内部函数
async fn execute_material_search(
_gemini_service: &mut GeminiService,
gemini_service: &mut GeminiService,
request: &SearchRequest,
) -> Result<SearchResponse, anyhow::Error> {
// 直接调用底层的搜索逻辑
execute_vertex_ai_search_internal(request).await
}
// 直接使用outfit_search_commands中的vertex搜索服务
use crate::presentation::commands::outfit_search_commands::execute_vertex_ai_search;
/// 内部Vertex AI搜索实现
async fn execute_vertex_ai_search_internal(
request: &SearchRequest,
) -> Result<SearchResponse, anyhow::Error> {
use crate::infrastructure::gemini_service::{GeminiConfig, GeminiService};
println!("🔍 开始执行素材库搜索...");
println!("搜索查询: {}", request.query);
// 创建Gemini服务实例
let config = GeminiConfig::default();
let mut gemini_service = GeminiService::new(Some(config))?;
// 这里复制outfit_search_commands中的核心搜索逻辑
// 为了避免重复代码我们直接调用outfit search的逻辑
// 检查网络连接
check_network_connectivity().await?;
// 获取访问令牌
let access_token = get_google_access_token().await?;
// 执行搜索
execute_vertex_search_with_token(&access_token, request).await
execute_vertex_ai_search(gemini_service, request).await
}
/// 简单的颜色名称到HSV转换函数
@ -277,51 +259,3 @@ fn parse_color_name_to_hsv(color_name: &str) -> crate::data::models::material_se
_ => MaterialColorHSV { hue: 0.0, saturation: 0.5, value: 0.8 }, // 默认中性色
}
}
/// 检查网络连接
async fn check_network_connectivity() -> Result<(), anyhow::Error> {
// 简单的网络连接检查
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(5))
.build()?;
let response = client
.get("https://www.google.com")
.send()
.await?;
if response.status().is_success() {
Ok(())
} else {
Err(anyhow::anyhow!("网络连接检查失败"))
}
}
/// 获取Google访问令牌
async fn get_google_access_token() -> Result<String, anyhow::Error> {
let config = GeminiConfig::default();
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(30))
.build()?;
// 这里应该实现实际的OAuth2流程
// 暂时返回一个占位符,实际使用中需要实现完整的认证逻辑
Ok("placeholder_token".to_string())
}
/// 使用访问令牌执行Vertex搜索
async fn execute_vertex_search_with_token(
_access_token: &str,
request: &SearchRequest,
) -> Result<SearchResponse, anyhow::Error> {
// 这里应该实现实际的Vertex AI Search API调用
// 暂时返回一个模拟的响应
Ok(SearchResponse {
results: Vec::new(),
total_size: 0,
next_page_token: None,
search_time_ms: 100,
searched_at: Utc::now(),
})
}

View File

@ -436,7 +436,7 @@ async fn check_network_connectivity() -> Result<(), anyhow::Error> {
}
/// 执行 Vertex AI Search 搜索
async fn execute_vertex_ai_search(
pub async fn execute_vertex_ai_search(
_gemini_service: &mut GeminiService,
request: &SearchRequest,
) -> Result<SearchResponse, anyhow::Error> {
@ -661,7 +661,7 @@ async fn get_google_access_token() -> Result<String, anyhow::Error> {
}
/// 将 Vertex AI Search 响应转换为我们的搜索结果格式
fn convert_vertex_response_to_search_results(
pub fn convert_vertex_response_to_search_results(
vertex_response: &serde_json::Value,
_request: &SearchRequest,
) -> Result<SearchResponse, anyhow::Error> {