SelectTools 选择工具
(ctx context.Context, allTools []Tool, constraints ToolConstraints)
| 203 | |
| 204 | // SelectTools 选择工具 |
| 205 | func (s *DefaultToolSelector) SelectTools(ctx context.Context, allTools []Tool, constraints ToolConstraints) ([]Tool, error) { |
| 206 | if constraints == nil || constraints.GetConstraintType() == ConstraintTypeNone { |
| 207 | return allTools, nil |
| 208 | } |
| 209 | |
| 210 | // 根据约束类型过滤 |
| 211 | switch constraints.GetConstraintType() { |
| 212 | case ConstraintTypeWhitelist, ConstraintTypeRequired: |
| 213 | allowedNames := constraints.GetAllowedTools(ctx) |
| 214 | if allowedNames == nil { |
| 215 | return allTools, nil |
| 216 | } |
| 217 | |
| 218 | allowed := make(map[string]bool, len(allowedNames)) |
| 219 | for _, name := range allowedNames { |
| 220 | allowed[name] = true |
| 221 | } |
| 222 | |
| 223 | filtered := make([]Tool, 0, len(allTools)) |
| 224 | for _, tool := range allTools { |
| 225 | if allowed[tool.Name()] { |
| 226 | filtered = append(filtered, tool) |
| 227 | } |
| 228 | } |
| 229 | return filtered, nil |
| 230 | |
| 231 | case ConstraintTypeBlacklist: |
| 232 | filtered := make([]Tool, 0, len(allTools)) |
| 233 | for _, tool := range allTools { |
| 234 | if constraints.IsAllowed(ctx, tool.Name()) { |
| 235 | filtered = append(filtered, tool) |
| 236 | } |
| 237 | } |
| 238 | return filtered, nil |
| 239 | |
| 240 | default: |
| 241 | return allTools, nil |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // ShouldUseToolChoice 判断是否应该使用 tool_choice |
| 246 | func (s *DefaultToolSelector) ShouldUseToolChoice(ctx context.Context, constraints ToolConstraints) (*ToolChoice, bool) { |