MCPcopy Create free account
hub / github.com/astercloud/aster / Initialize

Method Initialize

pkg/agent/tool_manager.go:51–100  ·  view source on GitHub ↗

Initialize 初始化工具管理器 将所有工具添加到索引,并根据配置决定哪些工具活跃

(toolNames []string, activeByDefault bool)

Source from the content-addressed store, hash-verified

49// Initialize 初始化工具管理器
50// 将所有工具添加到索引,并根据配置决定哪些工具活跃
51func (tm *ToolManager) Initialize(toolNames []string, activeByDefault bool) error {
52 tm.mu.Lock()
53 defer tm.mu.Unlock()
54
55 ctx := context.Background() // 用于日志记录
56
57 for _, name := range toolNames {
58 tool, err := tm.registry.Create(name, nil)
59 if err != nil {
60 toolMgrLog.Warn(ctx, "failed to create tool", map[string]any{"name": name, "error": err})
61 continue
62 }
63
64 // 检查是否是核心工具(始终活跃)
65 isCore := tm.isCoreToolLocked(name)
66
67 // 决定是否活跃
68 isActive := activeByDefault || isCore
69
70 // 检查是否实现了 DeferrableTool 接口
71 if deferrable, ok := tool.(tools.DeferrableTool); ok {
72 config := deferrable.DeferConfig()
73 if config != nil && config.DeferLoading && !isCore {
74 isActive = false
75 }
76 }
77
78 // 添加到索引
79 source := "builtin"
80 if err := tm.index.IndexTool(tool, source, !isActive); err != nil {
81 toolMgrLog.Warn(ctx, "failed to index tool", map[string]any{"name": name, "error": err})
82 continue
83 }
84
85 if isActive {
86 tm.activeTools[name] = tool
87 toolMgrLog.Debug(ctx, "tool loaded (active)", map[string]any{"name": name})
88 } else {
89 entry := tm.index.GetTool(name)
90 if entry != nil {
91 tm.deferredTools[name] = *entry
92 toolMgrLog.Debug(ctx, "tool indexed (deferred)", map[string]any{"name": name})
93 }
94 }
95 }
96
97 toolMgrLog.Info(ctx, "initialized", map[string]any{"active_tools": len(tm.activeTools), "deferred_tools": len(tm.deferredTools)})
98
99 return nil
100}
101
102// isCoreToolLocked 检查是否是核心工具(需要持有锁)
103func (tm *ToolManager) isCoreToolLocked(name string) bool {

Callers

nothing calls this directly

Calls 8

isCoreToolLockedMethod · 0.95
DeferConfigMethod · 0.80
IndexToolMethod · 0.80
CreateMethod · 0.65
WarnMethod · 0.65
DebugMethod · 0.65
InfoMethod · 0.65
GetToolMethod · 0.45

Tested by

no test coverage detected