refactor(kernel,desktop): chat.rs 瘦身 Phase 2 — 548→458行 (-16%)
Some checks failed
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Rust Check (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled

- 提取 translate_event() 函数: LoopEvent→StreamChatEvent 翻译独立
- 提取 Kernel::try_intercept_schedule(): 调度拦截下沉到 kernel
- 新增 ScheduleInterceptResult 类型导出
- 所有缝测试 14/14 PASS,无回归

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
iven
2026-04-21 22:25:10 +08:00
parent 591af5802c
commit ae7322e610
3 changed files with 217 additions and 203 deletions

View File

@@ -13,6 +13,109 @@ pub struct ChatModeConfig {
pub subagent_enabled: Option<bool>,
}
/// Result of a successful schedule intent interception.
pub struct ScheduleInterceptResult {
/// Pre-built streaming receiver with confirmation message.
pub rx: mpsc::Receiver<zclaw_runtime::LoopEvent>,
/// Human-readable task description.
pub task_description: String,
/// Natural language description of the schedule.
pub natural_description: String,
/// Cron expression.
pub cron_expression: String,
}
impl Kernel {
/// Try to intercept a schedule intent from the user's message.
///
/// If the message contains a clear schedule intent (e.g., "每天早上9点提醒我查房"),
/// parse it, create a trigger, and return a streaming receiver with the
/// confirmation message. Returns `Ok(None)` if no interception occurred.
pub async fn try_intercept_schedule(
&self,
message: &str,
agent_id: &AgentId,
) -> Result<Option<ScheduleInterceptResult>> {
if !zclaw_runtime::nl_schedule::has_schedule_intent(message) {
return Ok(None);
}
let parse_result = zclaw_runtime::nl_schedule::parse_nl_schedule(message, agent_id);
match parse_result {
zclaw_runtime::nl_schedule::ScheduleParseResult::Exact(ref parsed)
if parsed.confidence >= 0.8 =>
{
let trigger_id = format!(
"sched_{}_{}",
chrono::Utc::now().timestamp_millis(),
&uuid::Uuid::new_v4().to_string()[..8]
);
let trigger_config = zclaw_hands::TriggerConfig {
id: trigger_id.clone(),
name: parsed.task_description.clone(),
hand_id: "_reminder".to_string(),
trigger_type: zclaw_hands::TriggerType::Schedule {
cron: parsed.cron_expression.clone(),
},
enabled: true,
max_executions_per_hour: 60,
};
match self.create_trigger(trigger_config).await {
Ok(_entry) => {
tracing::info!(
"[Kernel] Schedule trigger created: {} (cron: {})",
trigger_id, parsed.cron_expression
);
let confirm_msg = format!(
"已为您设置定时任务:\n\n- **任务**{}\n- **时间**{}\n- **Cron**`{}`\n\n任务已激活,将在设定时间自动执行。",
parsed.task_description,
parsed.natural_description,
parsed.cron_expression,
);
let (tx, rx) = mpsc::channel(32);
if tx.send(zclaw_runtime::LoopEvent::Delta(confirm_msg)).await.is_err() {
tracing::warn!("[Kernel] Failed to send confirm msg to channel");
}
if tx.send(zclaw_runtime::LoopEvent::Complete(
zclaw_runtime::AgentLoopResult {
response: String::new(),
input_tokens: 0,
output_tokens: 0,
iterations: 1,
}
)).await.is_err() {
tracing::warn!("[Kernel] Failed to send complete to channel");
}
drop(tx);
Ok(Some(ScheduleInterceptResult {
rx,
task_description: parsed.task_description.clone(),
natural_description: parsed.natural_description.clone(),
cron_expression: parsed.cron_expression.clone(),
}))
}
Err(e) => {
tracing::warn!(
"[Kernel] Failed to create schedule trigger, falling through to LLM: {}", e
);
Ok(None)
}
}
}
_ => {
tracing::debug!(
"[Kernel] Schedule intent detected but not confident enough, falling through to LLM"
);
Ok(None)
}
}
}
}
use zclaw_runtime::{AgentLoop, tool::builtin::PathValidator};
use super::Kernel;