feat(agent): 移除未使用的导入并简化条件表达式

This commit is contained in:
徐涛
2026-07-03 13:55:24 +08:00
parent 9e4f50c955
commit e54edbc037
9 changed files with 16 additions and 25 deletions
+1 -2
View File
@@ -15,8 +15,7 @@ use crate::agent::runtime::RuntimeBundle;
use crate::agent::session_memory::SessionMemory; use crate::agent::session_memory::SessionMemory;
use crate::llm::cycle::{CostTracker, CycleConfig, LlmCycle}; use crate::llm::cycle::{CostTracker, CycleConfig, LlmCycle};
use crate::llm::hooks::{HookContext, HookEvent}; use crate::llm::hooks::{HookContext, HookEvent};
use crate::llm::types::message::{ContentBlock, Message}; use crate::llm::types::message::Message;
use crate::llm::types::openai_message::OpenaiChatMessage;
use crate::llm::types::response_v2::MessageResponse; use crate::llm::types::response_v2::MessageResponse;
use crate::memory::store::InMemoryStore; use crate::memory::store::InMemoryStore;
+2 -3
View File
@@ -178,8 +178,8 @@ pub fn content_to_blocks(field: &ContentField) -> Vec<ContentBlock> {
// ponytail: 简化处理 —— URL 直接通过,data URI 拆出 // ponytail: 简化处理 —— URL 直接通过,data URI 拆出
// data:<mime>;base64,<b64> → ImageSource { data: b64, mime, is_url: false }。 // data:<mime>;base64,<b64> → ImageSource { data: b64, mime, is_url: false }。
let url = &image_url.url; let url = &image_url.url;
if let Some(rest) = url.strip_prefix("data:") { if let Some(rest) = url.strip_prefix("data:")
if let Some((mime, b64)) = rest.split_once(";base64,") { && let Some((mime, b64)) = rest.split_once(";base64,") {
return Some(ContentBlock::Image { return Some(ContentBlock::Image {
source: crate::llm::types::message::ImageSource { source: crate::llm::types::message::ImageSource {
data: b64.to_string(), data: b64.to_string(),
@@ -188,7 +188,6 @@ pub fn content_to_blocks(field: &ContentField) -> Vec<ContentBlock> {
}, },
}); });
} }
}
Some(ContentBlock::Image { Some(ContentBlock::Image {
source: crate::llm::types::message::ImageSource { source: crate::llm::types::message::ImageSource {
data: url.clone(), data: url.clone(),
+1 -1
View File
@@ -16,7 +16,7 @@ use crate::llm::compact::{should_compact, microcompact, CompactConfig, CompactSt
use crate::llm::cycle::retry::should_retry; use crate::llm::cycle::retry::should_retry;
use crate::llm::error::LlmError; use crate::llm::error::LlmError;
use crate::llm::hooks::{HookContext, HookExecutor}; use crate::llm::hooks::{HookContext, HookExecutor};
use crate::llm::provider::{LlmProvider, ProviderCapabilities, ProviderFeatures}; use crate::llm::provider::LlmProvider;
use crate::llm::stream::StreamEvent; use crate::llm::stream::StreamEvent;
use crate::llm::types::message::{ContentBlock, Message}; use crate::llm::types::message::{ContentBlock, Message};
use crate::llm::types::request_v2::MessageRequest; use crate::llm::types::request_v2::MessageRequest;
+3 -3
View File
@@ -21,7 +21,7 @@ use tracing::{debug, error, info, warn};
use super::{LlmProvider, ProviderCapabilities, ProviderFeatures}; use super::{LlmProvider, ProviderCapabilities, ProviderFeatures};
use crate::llm::error::LlmError; use crate::llm::error::LlmError;
use crate::llm::types::message::{ContentBlock, ContentBlockType, Message}; use crate::llm::types::message::{ContentBlock, ContentBlockType, Message};
use crate::llm::types::request_v2::{MessageRequest, ThinkingConfig}; use crate::llm::types::request_v2::MessageRequest;
use crate::llm::types::response_v2::{ use crate::llm::types::response_v2::{
MessageResponse, PartialMessageResponse, PartialUsage, StopReason, StreamEvent, MessageResponse, PartialMessageResponse, PartialUsage, StopReason, StreamEvent,
}; };
@@ -193,7 +193,7 @@ impl AnthropicProvider {
.json(&body) .json(&body)
.send() .send()
.await .await
.map_err(|e| Self::map_reqwest_error(e))?; .map_err(Self::map_reqwest_error)?;
let status = response.status(); let status = response.status();
if !status.is_success() { if !status.is_success() {
@@ -229,7 +229,7 @@ impl AnthropicProvider {
.json(&body) .json(&body)
.send() .send()
.await .await
.map_err(|e| Self::map_reqwest_error(e))?; .map_err(Self::map_reqwest_error)?;
let status = response.status(); let status = response.status();
if !status.is_success() { if !status.is_success() {
+1 -1
View File
@@ -22,7 +22,7 @@ use serde_json::Value;
use tracing::{debug, error, info}; use tracing::{debug, error, info};
use super::{LlmProvider, ProviderCapabilities, ProviderFeatures}; use super::{LlmProvider, ProviderCapabilities, ProviderFeatures};
use crate::llm::convert::{blocks_to_content, content_to_blocks, from_openai, to_openai}; use crate::llm::convert::{from_openai, to_openai};
use crate::llm::error::LlmError; use crate::llm::error::LlmError;
use crate::llm::types::message::{ContentBlock, ContentBlockType, Message}; use crate::llm::types::message::{ContentBlock, ContentBlockType, Message};
use crate::llm::types::openai_message::{ContentField, OpenaiChatMessage}; use crate::llm::types::openai_message::{ContentField, OpenaiChatMessage};
+1 -1
View File
@@ -23,7 +23,7 @@ use crate::llm::types::old_stream::LegacyStreamEvent;
use crate::llm::types::response_v2::MessageResponse; use crate::llm::types::response_v2::MessageResponse;
use crate::llm::types::response_v2::StopReason; use crate::llm::types::response_v2::StopReason;
use crate::llm::types::usage::Usage; use crate::llm::types::usage::Usage;
use crate::llm::types::{FinishReason, OpenaiChatChunk, OpenaiToolCall}; use crate::llm::types::{OpenaiChatChunk, OpenaiToolCall};
// 唯一的对外 `StreamEvent` 定义(高精度 IR 事件,来自 `response_v2`)。 // 唯一的对外 `StreamEvent` 定义(高精度 IR 事件,来自 `response_v2`)。
// //
+2 -6
View File
@@ -12,7 +12,9 @@ pub struct StreamOptions {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[derive(Default)]
pub enum ToolChoice { pub enum ToolChoice {
#[default]
None, None,
Auto, Auto,
Required, Required,
@@ -20,12 +22,6 @@ pub enum ToolChoice {
AllowedTools { tool_names: Vec<String> }, AllowedTools { tool_names: Vec<String> },
} }
impl Default for ToolChoice {
fn default() -> Self {
// Default 行为由调用方显式选择;此处选择不暴露任何工具 —— 不暴露比错暴露安全。
ToolChoice::None
}
}
impl Serialize for ToolChoice { impl Serialize for ToolChoice {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+2 -3
View File
@@ -174,8 +174,8 @@ impl ConversationMemory {
} }
} }
if let Some(ref compact_config) = self.config.compact_config { if let Some(ref compact_config) = self.config.compact_config
if should_compact(&self.messages, compact_config, &self.compact_state) { && should_compact(&self.messages, compact_config, &self.compact_state) {
let keep_recent = compact_config.keep_recent; let keep_recent = compact_config.keep_recent;
let freed = microcompact(&mut self.messages, keep_recent); let freed = microcompact(&mut self.messages, keep_recent);
if freed > 0 { if freed > 0 {
@@ -184,7 +184,6 @@ impl ConversationMemory {
let _ = self.compact_state.record_failure(); let _ = self.compact_state.record_failure();
} }
} }
}
} }
} }
+3 -5
View File
@@ -123,14 +123,12 @@ impl KnowledgeStore {
let mut results = Vec::new(); let mut results = Vec::new();
let index = self.index.lock().unwrap(); let index = self.index.lock().unwrap();
for entry in index.iter() { for entry in index.iter() {
if entry.title.to_lowercase().contains(&needle) if (entry.title.to_lowercase().contains(&needle)
|| entry.summary.to_lowercase().contains(&needle) || entry.summary.to_lowercase().contains(&needle)
|| entry.tags.iter().any(|t| t.to_lowercase().contains(&needle)) || entry.tags.iter().any(|t| t.to_lowercase().contains(&needle)))
{ && let Some(page) = self.get_page(&entry.id).await? {
if let Some(page) = self.get_page(&entry.id).await? {
results.push(page); results.push(page);
} }
}
} }
Ok(results) Ok(results)
} }