Docker 容器化部署
- 提供 Mode A (Alpine musl, ~23MB) 和 Mode B (Distroless glibc, ~87MB)
两种镜像,Docker Compose 一键启动
- build.rs 支持 SKIP_DASHBOARD_BUILD 跳过前端构建
- 国内镜像加速 (npm/apt/apk) 通过 USE_MIRRORS build-arg 控制
安全:Cookie-Based 鉴权系统
- HttpOnly/SameSite=Strict Cookie 会话管理(24h 过期自动清理)
- 登录/登出/验证接口 + 中间件注入
- 前端登录页面 + 退出按钮
- 三层 CORS:localhost 鉴权 / 全放通 bookmarklet / 受保护路由
- 书签脚本 fetch 添加 credentials:'include'
Coordinator 模式 (P2)
- 4 个 meta-tool (delegate_task/check_task/task_stop/synthesize)
- WorkerPool + Semaphore 并发控制 + 超时保护
- 前端协调者模式开关
Hook 系统:UserPromptSubmit 事件 (P2)
- 第 13 个生命周期事件,fire-and-forget 审计
FTS5 全文搜索 (P3)
- agent_sessions_fts + agent_messages_fts 虚拟表
- search_history Agent 工具 + /api/search/history HTTP 接口
- 前端防抖搜索框 + 仅当前会话筛选
工具加载优化 (P3)
- defer_loading 延迟加载 (7 个重型工具)
- is_readonly 只读标记 (9 个查询工具)
- classifier_summary 工具目录供 LLM 按需判断
模型回退策略 (P3)
- LLM_FALLBACK_MODEL 优先回退 + LLM_FALLBACK_CHAIN 链式轮换
- LlmClient model 改为 Arc<RwLock> 支持运行时切换
- 连续 3 次过载后自动切换
压缩记忆桥接 (P3)
- 压缩丢弃消息 → 子代理提取持久记忆 (extract_memories_from_compaction)
git2 依赖修复
- 切换到 vendored-libgit2,消除 OpenSSL 系统依赖
145 lines
5.5 KiB
Rust
145 lines
5.5 KiB
Rust
// src/agent/hooks/traits.rs
|
||
//
|
||
// Agent 生命周期 Hook traits。
|
||
|
||
use async_trait::async_trait;
|
||
use std::time::Duration;
|
||
|
||
use super::matcher::ToolMatchFilter;
|
||
use super::types::{
|
||
HookEvent, PermissionDeniedContext, PermissionRequestAction, PermissionRequestContext,
|
||
PostCompactContext, PostToolUseAction, PostToolUseContext, PostToolUseFailureContext,
|
||
PreCompactContext, PreToolUseAction, PreToolUseContext, SessionStartContext,
|
||
SessionStopContext, StepCompleteContext, SubagentStartContext, SubagentStopContext,
|
||
UserPromptSubmitContext,
|
||
};
|
||
|
||
// ── Hook Trait ──
|
||
|
||
/// Agent 生命周期 Hook trait。
|
||
/// 所有方法都有默认空实现,只需覆写关心的 hook 点。
|
||
#[async_trait]
|
||
pub trait AgentHook: Send + Sync {
|
||
/// Hook 名称(用于日志和调试)
|
||
fn name(&self) -> &str;
|
||
|
||
/// 声明该 hook 订阅的生命周期事件。
|
||
/// 返回空切片表示订阅所有事件(向后兼容的默认行为)。
|
||
/// HookRegistry 用此信息预计算事件→hook 索引,避免无关调用。
|
||
fn subscribed_events(&self) -> &[HookEvent] {
|
||
&[] // 空 = 订阅全部
|
||
}
|
||
|
||
/// Per-hook 超时时间。返回 `None` 使用全局默认值 (30s)。
|
||
fn timeout(&self) -> Option<Duration> {
|
||
None
|
||
}
|
||
|
||
/// 声明此 hook 关心哪些工具调用。
|
||
///
|
||
/// 返回的 `ToolMatchFilter` 用于在 dispatch 时过滤无关的工具调用,
|
||
/// 减少不必要的 hook 执行。默认返回空过滤器(匹配所有工具)。
|
||
///
|
||
/// 仅对 `PreToolUse`、`PostToolUse`、`PostToolUseFailure` 事件生效。
|
||
/// 其他事件忽略此过滤器。
|
||
fn match_filter(&self) -> ToolMatchFilter {
|
||
ToolMatchFilter::default()
|
||
}
|
||
|
||
// ── 原有 5 个生命周期事件 ──
|
||
|
||
/// 会话创建/恢复时调用。
|
||
async fn on_session_start(&self, _ctx: &SessionStartContext) {}
|
||
|
||
/// 用户提交新提示词时调用(fire-and-forget)。
|
||
/// 在 context building 之前触发,hook 可以记录日志、触发边车操作或注入审计上下文。
|
||
async fn on_user_prompt_submit(&self, _ctx: &UserPromptSubmitContext) {}
|
||
|
||
/// 工具执行前调用。可返回 Continue/Block/MutateInput/PermissionRequired。
|
||
async fn pre_tool_use(&self, _ctx: &PreToolUseContext) -> PreToolUseAction {
|
||
PreToolUseAction::Continue
|
||
}
|
||
|
||
/// 工具执行后调用。可返回 Continue 或 MutateOutput。
|
||
async fn post_tool_use(&self, _ctx: &PostToolUseContext) -> PostToolUseAction {
|
||
PostToolUseAction::Continue
|
||
}
|
||
|
||
/// 每个 ReAct step 完成后调用。
|
||
async fn on_step_complete(&self, _ctx: &StepCompleteContext) {}
|
||
|
||
/// 会话终止时调用。
|
||
async fn on_session_stop(&self, _ctx: &SessionStopContext<'_>) {}
|
||
|
||
// ── 新增 4 个生命周期事件(默认 no-op) ──
|
||
|
||
/// 子代理启动时调用。
|
||
async fn on_subagent_start(&self, _ctx: &SubagentStartContext) {}
|
||
|
||
/// 子代理停止时调用。
|
||
async fn on_subagent_stop(&self, _ctx: &SubagentStopContext) {}
|
||
|
||
/// 上下文压缩前调用。
|
||
async fn on_pre_compact(&self, _ctx: &PreCompactContext) {}
|
||
|
||
/// 上下文压缩后调用。
|
||
async fn on_post_compact(&self, _ctx: &PostCompactContext) {}
|
||
|
||
/// 工具执行失败时调用(独立于 PostToolUse,专注错误处理)。
|
||
async fn on_post_tool_use_failure(
|
||
&self,
|
||
_ctx: &PostToolUseFailureContext,
|
||
) -> PostToolUseAction {
|
||
PostToolUseAction::Continue
|
||
}
|
||
|
||
/// 权限请求前调用(参考 Claude Code PermissionRequest hook)。
|
||
///
|
||
/// Hook 可以覆盖权限决策或注入附加上下文。此方法在 PermissionChecker
|
||
/// 做出初步决策后、最终返回前触发。
|
||
///
|
||
/// 返回 `PermissionRequestAction::Continue` 保持当前决策不变。
|
||
async fn on_permission_request(
|
||
&self,
|
||
_ctx: &PermissionRequestContext,
|
||
) -> PermissionRequestAction {
|
||
PermissionRequestAction::Continue
|
||
}
|
||
|
||
/// 权限被拒绝后调用(参考 Claude Code PermissionDenied hook)。
|
||
///
|
||
/// 仅用于审计/日志/监控——返回值不影响执行流程。
|
||
/// 在权限被最终拒绝后触发(规则拒绝、Classifier 拒绝或用户拒绝)。
|
||
async fn on_permission_denied(&self, _ctx: &PermissionDeniedContext) {}
|
||
}
|
||
|
||
// ── Async Hook Trait ──
|
||
|
||
/// Fire-and-forget hook trait(Phase 4.1+)。
|
||
///
|
||
/// 与 `AgentHook` 分离设计:
|
||
/// - `AgentHook` 的返回值(`PreToolUseAction` / `PostToolUseAction`)会立即影响执行流程
|
||
/// - `AsyncAgentHook` 不返回决策——它适合用于后台操作(上传、远程日志上报等)
|
||
///
|
||
/// HookRegistry 并行调度两类 hook:sync hook 的结果用于决策,async hook 仅 fire-and-forget。
|
||
/// Async hook dispatch 默认超时 5 秒(仅限方法返回,后台工作继续独立执行)。
|
||
#[async_trait]
|
||
pub trait AsyncAgentHook: Send + Sync {
|
||
/// Hook 名称(用于日志和调试)
|
||
fn name(&self) -> &str;
|
||
|
||
/// 声明订阅的事件(默认全部)。与 AgentHook 相同的索引机制。
|
||
fn subscribed_events(&self) -> &[HookEvent] {
|
||
&[]
|
||
}
|
||
|
||
/// 工具执行后异步回调(fire-and-forget)
|
||
async fn on_post_tool_use_async(&self, _ctx: PostToolUseContext) {}
|
||
|
||
/// 工具执行失败时异步回调
|
||
async fn on_post_tool_use_failure_async(&self, _ctx: PostToolUseFailureContext) {}
|
||
|
||
/// 会话结束时异步回调
|
||
async fn on_session_stop_async(&self, _ctx: SessionStopContext<'_>) {}
|
||
}
|