跳转到内容

Skill/Tool 动态激活与 Activator 模式

学习目标

理解 LobeHub 中 Skill 和 Tool 的动态激活机制,包括 LobeActivator 的意图驱动激活、三级分类管理、chat mode vs agent mode 权限隔离。

前置知识

本章涉及 Skill 设计和工具调用的通用原理,建议先阅读:

下文假设你已理解上述概念,直接聚焦 LobeHub 的具体实现。

项目实践

LobeActivator:意图驱动的工具激活

LobeActivator 是一个 always-on 工具,负责在 Agent 对话中识别用户意图并动态激活所需的 Skills 和 Tools。

三级工具分类管理

LobeHub 将内置工具分为三级:

分类含义工具示例
defaultToolIds默认启用的核心工具activator、skills、skill-store、web-browsing、knowledge-base、memory、local-system、cloud-sandbox、topic-reference、agent-documents、task、lobe-agent
alwaysOnToolIds始终启用、不可禁用activator、skills、skill-store
runtimeManagedToolIds由运行时条件决定启用cloud-sandbox、knowledge-base、local-system、memory、remote-device、lobe-agent、web-browsing

关键设计runtimeManagedToolIds 的启用状态不由用户选择决定,而是由系统条件自动判断:

  • cloud-sandbox:仅在云端运行时启用
  • knowledge-base:仅当用户有已启用的知识库时启用
  • memory:仅当全局记忆设置启用时启用
  • local-system:仅 Desktop 模式下启用

Chat Mode vs Agent Mode 权限隔离

// 伪代码:chat mode 允许的工具
const chatModeAllowedToolIds = [
KnowledgeBaseManifest.identifier,
MemoryManifest.identifier,
WebBrowsingManifest.identifier,
];

在 chat mode(chatConfig.enableAgentMode === false)下:

  • 仅允许上述 3 个工具
  • 禁用 alwaysOnToolIds 中的 activator 和 skill-store(防止意外激活其他工具)
  • 禁用 allowExplicitActivation(activator 不能偷偷激活其他工具)

安全考量:chat mode 是简单的问答场景,用户不应该意外触发复杂的 Agent 工具链。

手动模式排除

当用户使用 manual skill-activate 模式时,排除工具发现相关的工具:

// 伪代码:手动模式排除
const manualModeExcludeToolIds = [
LobeActivatorManifest.identifier, // activator 自身
SkillStoreManifest.identifier, // skill store
];

这是为了避免在手动模式下,用户还需要管理 activator 和 skill-store 这两个工具发现工具。

问题与规避

问题场景规避策略
Activator 误激活LLM 错误理解用户意图Activator 的 prompt 经过精心设计,需要高置信度才激活
Chat mode 下工具遗漏需要的工具不在 chatModeAllowedToolIds扩展白名单或切换到 agent mode
Runtime 条件变化用户启用知识库后工具未激活运行时条件检查在每次 Agent 调用时执行
新工具不在默认列表新增内置工具忘记添加到分类添加新工具后同步更新 defaultToolIds/runtimeManagedToolIds

设计取舍

为什么需要 always-on 工具?

Activator 和 Skills 工具必须在每次 Agent 调用时可用,否则 Agent 无法发现和激活新工具。将它们设为 alwaysOn 确保不受用户配置影响。

代价:即使不需要激活,这些工具也占用 Token(工具描述 + schema)。

为什么 chat mode 禁用 activator?

在简单的问答场景中,activator 的意图识别可能导致意外的工具激活。禁用 activator 确保 chat mode 下用户完全控制哪些工具被使用。

参考来源

  • LobeHub 内置工具分类 — packages/builtin-tools/src/index.ts
  • Skill 设计原理 — 通用 Skill 架构