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

Function NewSubAgentMiddleware

pkg/middleware/subagent.go:84–143  ·  view source on GitHub ↗

NewSubAgentMiddleware 创建子代理中间件

(config *SubAgentMiddlewareConfig)

Source from the content-addressed store, hash-verified

82
83// NewSubAgentMiddleware 创建子代理中间件
84func NewSubAgentMiddleware(config *SubAgentMiddlewareConfig) (*SubAgentMiddleware, error) {
85 // 设置默认超时
86 defaultTimeout := config.DefaultTimeout
87 if defaultTimeout == 0 {
88 defaultTimeout = 1 * time.Hour
89 }
90
91 m := &SubAgentMiddleware{
92 BaseMiddleware: NewBaseMiddleware("subagent", 200),
93 agents: make(map[string]SubAgent),
94 factory: config.Factory,
95 enableParallel: config.EnableParallel,
96 enableAsync: config.EnableAsync,
97 enableProcessIsolation: config.EnableProcessIsolation,
98 defaultTimeout: defaultTimeout,
99 }
100
101 // 创建或使用提供的管理器
102 switch {
103 case config.Manager != nil:
104 m.manager = config.Manager
105 case config.EnableProcessIsolation:
106 m.manager = builtin.NewFileSubagentManager()
107 saLog.Info(context.Background(), "using FileSubagentManager", nil)
108 case config.EnableAsync:
109 m.manager = newGoroutineSubagentManager(m)
110 saLog.Info(context.Background(), "using in-memory async manager", nil)
111 }
112
113 // 默认启用 general-purpose 子代理
114 specs := config.Specs
115 if config.EnableGeneralPurpose || (len(specs) == 0 && !config.EnableParallel) {
116 // 添加通用子代理规格
117 generalPurposeSpec := SubAgentSpec{
118 Name: "general-purpose",
119 Description: "通用子代理,用于执行复杂、多步骤的隔离任务",
120 Prompt: `你是一个通用的 AI 助手,专注于执行复杂的、多步骤的任务。
121你有完整的工具集,可以独立完成被委托的任务。
122请仔细分析任务需求,制定计划并逐步执行。`,
123 InheritMiddlewares: true, // 继承父代理的中间件
124 }
125 specs = append([]SubAgentSpec{generalPurposeSpec}, specs...)
126 }
127
128 // 初始化子代理
129 if config.Factory != nil {
130 for _, spec := range specs {
131 agent, err := config.Factory(context.Background(), spec)
132 if err != nil {
133 saLog.Error(context.Background(), "failed to create subagent", map[string]any{"name": spec.Name, "error": err.Error()})
134 continue
135 }
136 m.agents[spec.Name] = agent
137 saLog.Info(context.Background(), "created subagent", map[string]any{"name": spec.Name})
138 }
139 }
140
141 saLog.Info(context.Background(), "initialized", map[string]any{"subagents": len(m.agents)})

Calls 5

NewFileSubagentManagerFunction · 0.92
NewBaseMiddlewareFunction · 0.85
InfoMethod · 0.65
ErrorMethod · 0.65