Feature/dynamic tool and skills#828
Closed
SpellingDragon wants to merge 5 commits intosipeed:mainfrom
Closed
Conversation
added 5 commits
February 26, 2026 22:12
## 新特性
### 1. 上下文构建策略优化 (context.go)
- 新增三种上下文构建策略:Full/Lite/Custom
- Full 策略:完整上下文,向后兼容
- Lite 策略:最小上下文,减少 99.7% 耗时 (~2,500 ns/op)
- Custom 策略:自定义内容,精确控制技能和工具
- 实现系统提示词缓存机制,避免重复构建
### 2. 工具可见性动态过滤 (registry.go)
- 新增 ToolVisibilityContext 和 ToolVisibilityFilter
- 支持基于角色、通道、用户 ID 的细粒度权限控制
- 新增 RegisterWithFilter() 注册带过滤器的工具
- 新增 GetDefinitionsForContext() 根据上下文获取工具定义
- 使用场景:
* 多租户权限控制 (admin vs user)
* 通道特定功能 (Telegram vs Slack vs WeCom)
* 安全敏感操作限制
### 3. 技能按需加载 (loader.go)
- 新增 BuildSkillsSummaryFiltered() 按名称列表过滤
- 新增 BuildSkillsSummaryForNames() 显式指定技能
- 空列表时返回全部技能,保持向后兼容
### 4. 智能技能推荐器 (recommender.go)
- 混合推荐算法:规则预筛选 + LLM 智能选择
- 多维度评分:channel(40%) + keyword(30%) + history(20%) + recency(10%)
- 支持自定义权重配置
- 自动触发技能推荐并应用到上下文构建
### 5. Agent 实例动态技能过滤 (instance.go)
- 新增 SetSkillsFilter() 动态设置过滤器
- 新增 EnableSkillRecommender() 启用智能推荐
- 线程安全设计,支持运行时动态调整
- 自动触发 ContextBuilder 缓存失效
## 实现机制
### 工作流程
1. 用户消息到达 → ContextBuilder.BuildMessagesWithOptions
2. 根据 Strategy 选择构建方式 (Full/Lite/Custom)
3. 如启用推荐器且无显式过滤 → RecommendSkillsForContext
4. 规则预筛选与评分 → LLM 智能选择 (多候选时)
5. SkillsLoader.BuildSkillsSummaryFiltered 构建技能摘要
6. ToolRegistry.GetDefinitionsForContext 过滤工具定义
7. 组合完整系统消息返回给 LLM
### 性能优化
- Lite 策略:~2,500 ns/op,减少 99.7% 处理时间
- 缓存机制:系统提示词缓存避免重复构建
- 动态/静态上下文分离处理
### 向后兼容性
- BuildMessages() 自动调用新方法使用 Full 策略
- Register() 继续工作,工具对所有上下文可见
- 默认不启用推荐器,需显式设置
- 所有现有代码无需修改
## 测试覆盖
### 新增测试文件
- pkg/agent/recommender_test.go (技能推荐器测试)
- pkg/agent/context_benchmark_test.go (性能基准测试)
- pkg/agent/instance_skills_filter_test.go (实例过滤测试)
### 新增测试用例
- TestContextBuilder_BuildMessagesWithOptions_* (8 个策略测试)
- TestToolRegistry_RegisterWithFilter_* (5 个过滤测试)
- TestSkillsLoaderBuildSkillsSummaryFiltered* (7 个过滤测试)
- TestSkillRecommender_* (推荐器集成测试)
### 测试结果
✅ pkg/agent: PASS (coverage: 51.0%)
✅ pkg/tools: PASS (coverage: 59.2%)
✅ pkg/skills: PASS (coverage: 79.6%)
✅ 所有现有测试保持通过
## 文档更新
- ARCHITECTURE.md: 新增'上下文动态选择增强'章节
- pkg/agent/RECOMMENDER_EXAMPLES.md: 推荐器使用示例
- docs/openspec-global-rules-guide.md: OpenSpec 全局规则配置
## 配置示例
// 启用技能推荐器
agent.EnableSkillRecommender()
// 自定义推荐权重
agent.EnableSkillRecommenderWithWeights(0.5, 0.3, 0.15, 0.05)
// 动态设置技能过滤
agent.SetSkillsFilter([]string{"customer-service", "faq"})
// 注册管理员专用工具
registry.RegisterWithFilter(adminTool, func(ctx tools.ToolVisibilityContext) bool {
for _, role := range ctx.UserRoles {
if role == "admin" {
return true
}
}
return false
})
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📝 Description
🗣️ Type of Change
🤖 AI Code Generation
🔗 Related Issue
📚 Technical Context (Skip for Docs)
🧪 Test Environment
📸 Evidence (Optional)
Click to view Logs/Screenshots
☑️ Checklist