重构: 巨函数拆分 + 清理历史标记注释 + custom_prompt/停止按钮/tunnel 改进

This commit is contained in:
lxy
2026-07-31 21:36:12 +08:00
parent 365af554da
commit bd9031d35d
73 changed files with 1421 additions and 1064 deletions
+26 -6
View File
@@ -123,6 +123,31 @@ pub(crate) enum StreamResult {
InitFailed { retryable: bool, error: String },
}
/// 将 chunk 携带的 tool_calls delta 累加到累积表。
///
/// 每个 `ToolCallDelta` 按 `index` 归位到同一个 `ToolCallDraft`
/// - `id` 覆盖(后到的 id 视为权威,匹配 OpenAI/Anthropic 协议行为)
/// - `function_name` / `function_arguments` 增量拼接(流式分片到达)
///
/// 抽取自 stream_llm 的 chunk match arm(原最深 7 层嵌套点),纯状态累加无 emit/return 副作用。
fn accumulate_tool_calls(
tc_deltas: &[df_ai::provider::ToolCallDelta],
tool_calls_acc: &mut HashMap<u32, ToolCallDraft>,
) {
for tc_delta in tc_deltas {
let draft = tool_calls_acc.entry(tc_delta.index).or_default();
if let Some(id) = &tc_delta.id {
draft.id = id.clone();
}
if let Some(name) = &tc_delta.function_name {
draft.name.push_str(name);
}
if let Some(args) = &tc_delta.function_arguments {
draft.args.push_str(args);
}
}
}
/// 流式接收 LLM 响应。
///
/// 三类异常处理(返回 StreamResult 显式区分出口):
@@ -339,12 +364,7 @@ pub(crate) async fn stream_llm(
let _ = app_handle.state::<crate::state::AppState>().ai_event_bus.publish_event(ev);
}
if let Some(tc_deltas) = &chunk.tool_calls {
for tc_delta in tc_deltas {
let draft = tool_calls_acc.entry(tc_delta.index).or_default();
if let Some(id) = &tc_delta.id { draft.id = id.clone(); }
if let Some(name) = &tc_delta.function_name { draft.name.push_str(name); }
if let Some(args) = &tc_delta.function_arguments { draft.args.push_str(args); }
}
accumulate_tool_calls(tc_deltas, &mut tool_calls_acc);
}
if let Some(u) = &chunk.usage {
final_usage = Some(u.clone());