style(tools, llm): 统一导入顺序与代码格式
This commit is contained in:
+38
-35
@@ -9,10 +9,10 @@ pub use usage::{CostTracker, Usage};
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
|
||||
use futures_core::stream::Stream;
|
||||
use async_stream::stream;
|
||||
use futures_core::stream::Stream;
|
||||
|
||||
use crate::llm::compact::{should_compact, microcompact, CompactConfig, CompactState};
|
||||
use crate::llm::compact::{CompactConfig, CompactState, microcompact, should_compact};
|
||||
use crate::llm::cycle::retry::should_retry;
|
||||
use crate::llm::error::LlmError;
|
||||
use crate::llm::hooks::{HookContext, HookExecutor};
|
||||
@@ -113,8 +113,12 @@ impl LlmCycle {
|
||||
note = "请改用 Message::system_text() + with_messages()"
|
||||
)]
|
||||
pub fn with_system_prompt(mut self, prompt: String) -> Self {
|
||||
self.messages
|
||||
.insert(0, Message::System { content: vec![ContentBlock::Text { text: prompt }] });
|
||||
self.messages.insert(
|
||||
0,
|
||||
Message::System {
|
||||
content: vec![ContentBlock::Text { text: prompt }],
|
||||
},
|
||||
);
|
||||
self
|
||||
}
|
||||
|
||||
@@ -188,8 +192,8 @@ impl LlmCycle {
|
||||
};
|
||||
|
||||
if let Some(ref executor) = self.hook_executor {
|
||||
let ctx = HookContext::new(crate::llm::hooks::HookEvent::PreRequest)
|
||||
.with_request(&request);
|
||||
let ctx =
|
||||
HookContext::new(crate::llm::hooks::HookEvent::PreRequest).with_request(&request);
|
||||
let results = executor
|
||||
.execute(crate::llm::hooks::HookEvent::PreRequest, &ctx)
|
||||
.await;
|
||||
@@ -218,7 +222,8 @@ impl LlmCycle {
|
||||
}
|
||||
Err(e) => {
|
||||
if let Some(ref executor) = self.hook_executor {
|
||||
let ctx = HookContext::new(crate::llm::hooks::HookEvent::OnError).with_error(&e);
|
||||
let ctx =
|
||||
HookContext::new(crate::llm::hooks::HookEvent::OnError).with_error(&e);
|
||||
executor
|
||||
.execute(crate::llm::hooks::HookEvent::OnError, &ctx)
|
||||
.await;
|
||||
@@ -359,8 +364,8 @@ impl LlmCycle {
|
||||
|
||||
// PreRequest hook
|
||||
if let Some(ref executor) = self.hook_executor {
|
||||
let ctx = HookContext::new(crate::llm::hooks::HookEvent::PreRequest)
|
||||
.with_request(&request);
|
||||
let ctx =
|
||||
HookContext::new(crate::llm::hooks::HookEvent::PreRequest).with_request(&request);
|
||||
let results = executor
|
||||
.execute(crate::llm::hooks::HookEvent::PreRequest, &ctx)
|
||||
.await;
|
||||
@@ -496,8 +501,8 @@ impl LlmCycle {
|
||||
}
|
||||
Err(e) => {
|
||||
if let Some(ref executor) = self.hook_executor {
|
||||
let ctx = HookContext::new(crate::llm::hooks::HookEvent::OnError)
|
||||
.with_error(&e);
|
||||
let ctx =
|
||||
HookContext::new(crate::llm::hooks::HookEvent::OnError).with_error(&e);
|
||||
executor
|
||||
.execute(crate::llm::hooks::HookEvent::OnError, &ctx)
|
||||
.await;
|
||||
@@ -593,11 +598,8 @@ impl LlmCycle {
|
||||
// 真实 tool_call_id 而非 tool_name 充当 —— 这条 FIX-A 修复与 Phase 2 消息切换
|
||||
// 同步生效。
|
||||
// ponytail: Phase 2 直接存储 Message::ToolResult,is_error 由 ToolInvocation.output 推断。
|
||||
self.messages.push(Message::tool_result(
|
||||
result.tool_call_id,
|
||||
content,
|
||||
is_error,
|
||||
));
|
||||
self.messages
|
||||
.push(Message::tool_result(result.tool_call_id, content, is_error));
|
||||
}
|
||||
|
||||
// 每轮工具执行后触发 compaction
|
||||
@@ -641,9 +643,7 @@ fn has_tool_calls_in_response(response: &MessageResponse) -> bool {
|
||||
/// ponytail: 当前 Phase 0 实现,`arguments_json_string` 内含 JSON 序列化的 input。
|
||||
/// 消费方在调用 `registry.invoke_all()` 时反序列化一次。该小段冗余序列化
|
||||
/// 在 Phase 2 切换为 `Vec<Message>` 后可整体消除。
|
||||
fn extract_tool_calls_from_response(
|
||||
response: &MessageResponse,
|
||||
) -> Vec<(String, String, String)> {
|
||||
fn extract_tool_calls_from_response(response: &MessageResponse) -> Vec<(String, String, String)> {
|
||||
let mut out = Vec::new();
|
||||
if let Message::Assistant { content } = &response.message {
|
||||
for block in content {
|
||||
@@ -665,7 +665,11 @@ fn truncate_tool_result(s: &str, max_bytes: usize) -> String {
|
||||
while end > 0 && !s.is_char_boundary(end) {
|
||||
end -= 1;
|
||||
}
|
||||
format!("{}\n\n[... truncated, original size: {} bytes ...]", &s[..end], s.len())
|
||||
format!(
|
||||
"{}\n\n[... truncated, original size: {} bytes ...]",
|
||||
&s[..end],
|
||||
s.len()
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -675,7 +679,7 @@ mod tests {
|
||||
use crate::tools::{BaseTool, ToolRegistry};
|
||||
use async_trait::async_trait;
|
||||
use futures_core::Stream;
|
||||
use serde_json::{json, Value};
|
||||
use serde_json::{Value, json};
|
||||
use std::pin::Pin;
|
||||
|
||||
/// 模拟 Provider —— 预定义响应序列,按调用顺序返回。
|
||||
@@ -729,9 +733,7 @@ mod tests {
|
||||
id: String::new(),
|
||||
model: String::new(),
|
||||
message: Message::Assistant {
|
||||
content: vec![ContentBlock::Text {
|
||||
text: text.into(),
|
||||
}],
|
||||
content: vec![ContentBlock::Text { text: text.into() }],
|
||||
},
|
||||
usage: empty_usage(),
|
||||
stop_reason: StopReason::Stop,
|
||||
@@ -755,7 +757,9 @@ mod tests {
|
||||
MessageResponse {
|
||||
id: String::new(),
|
||||
model: String::new(),
|
||||
message: Message::Assistant { content: tool_blocks },
|
||||
message: Message::Assistant {
|
||||
content: tool_blocks,
|
||||
},
|
||||
usage: empty_usage(),
|
||||
stop_reason: StopReason::ToolUse,
|
||||
extra: std::collections::HashMap::new(),
|
||||
@@ -810,16 +814,13 @@ mod tests {
|
||||
let messages = cycle.messages();
|
||||
assert_eq!(messages.len(), 4);
|
||||
assert!(matches!(messages[0], Message::User { .. }));
|
||||
assert!(matches!(
|
||||
messages[1],
|
||||
Message::Assistant {
|
||||
content: _,
|
||||
}
|
||||
));
|
||||
assert!(matches!(messages[1], Message::Assistant { content: _ }));
|
||||
if let Message::Assistant { content } = &messages[1] {
|
||||
assert!(content
|
||||
.iter()
|
||||
.any(|b| matches!(b, ContentBlock::ToolUse { .. })));
|
||||
assert!(
|
||||
content
|
||||
.iter()
|
||||
.any(|b| matches!(b, ContentBlock::ToolUse { .. }))
|
||||
);
|
||||
}
|
||||
assert!(matches!(
|
||||
messages[2],
|
||||
@@ -875,7 +876,9 @@ mod tests {
|
||||
registry.register(std::sync::Arc::new(AddTool)).unwrap();
|
||||
|
||||
let result = cycle.submit_with_tools("test".to_string(), ®istry).await;
|
||||
assert!(matches!(result, Err(LlmError::Other(msg)) if msg.contains("达到最大工具循环轮次")));
|
||||
assert!(
|
||||
matches!(result, Err(LlmError::Other(msg)) if msg.contains("达到最大工具循环轮次"))
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
Reference in New Issue
Block a user