Merge pull request #266 from chumyin/fix/provider-chatresponse-unification

fix(provider): complete ChatResponse integration across runtime surfaces
This commit is contained in:
Chummy 2026-02-16 21:21:43 +08:00 committed by GitHub
commit fa0c77385c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 608 additions and 213 deletions

View file

@ -959,7 +959,7 @@ mod tests {
use super::*;
use crate::memory::{Memory, MemoryCategory, SqliteMemory};
use crate::observability::NoopObserver;
use crate::providers::{ChatMessage, Provider};
use crate::providers::{ChatMessage, ChatResponse, Provider, ToolCall};
use crate::tools::{Tool, ToolResult};
use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
@ -1026,27 +1026,23 @@ mod tests {
message: &str,
_model: &str,
_temperature: f64,
) -> anyhow::Result<String> {
) -> anyhow::Result<ChatResponse> {
tokio::time::sleep(self.delay).await;
Ok(format!("echo: {message}"))
Ok(ChatResponse::with_text(format!("echo: {message}")))
}
}
struct ToolCallingProvider;
fn tool_call_payload() -> String {
serde_json::json!({
"content": "",
"tool_calls": [{
"id": "call_1",
"type": "function",
"function": {
"name": "mock_price",
"arguments": "{\"symbol\":\"BTC\"}"
}
}]
})
.to_string()
fn tool_call_payload() -> ChatResponse {
ChatResponse {
text: Some(String::new()),
tool_calls: vec![ToolCall {
id: "call_1".into(),
name: "mock_price".into(),
arguments: r#"{"symbol":"BTC"}"#.into(),
}],
}
}
#[async_trait::async_trait]
@ -1057,7 +1053,7 @@ mod tests {
_message: &str,
_model: &str,
_temperature: f64,
) -> anyhow::Result<String> {
) -> anyhow::Result<ChatResponse> {
Ok(tool_call_payload())
}
@ -1066,12 +1062,14 @@ mod tests {
messages: &[ChatMessage],
_model: &str,
_temperature: f64,
) -> anyhow::Result<String> {
) -> anyhow::Result<ChatResponse> {
let has_tool_results = messages
.iter()
.any(|msg| msg.role == "user" && msg.content.contains("[Tool results]"));
if has_tool_results {
Ok("BTC is currently around $65,000 based on latest tool output.".to_string())
Ok(ChatResponse::with_text(
"BTC is currently around $65,000 based on latest tool output.",
))
} else {
Ok(tool_call_payload())
}

View file

@ -32,7 +32,10 @@ fn split_message_for_telegram(message: &str) -> Vec<String> {
pos + 1
} else {
// Try space as fallback
search_area.rfind(' ').unwrap_or(TELEGRAM_MAX_MESSAGE_LENGTH) + 1
search_area
.rfind(' ')
.unwrap_or(TELEGRAM_MAX_MESSAGE_LENGTH)
+ 1
}
} else if let Some(pos) = search_area.rfind(' ') {
pos + 1
@ -916,7 +919,8 @@ mod tests {
#[test]
fn telegram_split_at_newline() {
let text_block = "Line of text\n".repeat(TELEGRAM_MAX_MESSAGE_LENGTH / 13);
let line = "Line of text\n";
let text_block = line.repeat(TELEGRAM_MAX_MESSAGE_LENGTH / line.len() + 1);
let chunks = split_message_for_telegram(&text_block);
assert!(chunks.len() >= 2);
for chunk in chunks {