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

Function main

examples/subagent-async/main.go:16–279  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

14)
15
16func main() {
17 fmt.Println("=== SubAgent 异步执行示例 ===")
18 fmt.Println()
19
20 // 1. 创建 Backend
21 backend := backends.NewStateBackend()
22 fmt.Println("✓ 创建 StateBackend")
23
24 // 2. 定义子代理规格
25 specs := []middleware.SubAgentSpec{
26 {
27 Name: "researcher",
28 Description: "深度研究和分析专家",
29 Prompt: "你是一个专注于深度研究的 AI。仔细分析问题,提供详细的研究报告。",
30 },
31 {
32 Name: "data-analyst",
33 Description: "数据分析专家",
34 Prompt: "你是一个数据分析专家。处理和分析大量数据,生成洞察报告。",
35 },
36 }
37 fmt.Printf("✓ 定义了 %d 个子代理规格\n", len(specs))
38
39 // 3. 创建子代理工厂(模拟长时间运行的任务)
40 factory := func(ctx context.Context, spec middleware.SubAgentSpec) (middleware.SubAgent, error) {
41 execFn := func(ctx context.Context, description string, parentContext map[string]any) (string, error) {
42 // 模拟长时间运行的任务
43 fmt.Printf("[%s] 开始执行任务: %s\n", spec.Name, description)
44 time.Sleep(5 * time.Second) // 模拟5秒的处理时间
45
46 result := fmt.Sprintf(`[%s] 任务完成报告
47任务描述: %s
48执行时间: 5秒
49系统提示: %s
50上下文: %v
51
52分析结果:
531. 任务已成功完成
542. 处理了大量数据
553. 生成了详细报告`, spec.Name, description, spec.Prompt, parentContext)
56
57 fmt.Printf("[%s] 任务完成\n", spec.Name)
58 return result, nil
59 }
60 return middleware.NewSimpleSubAgent(spec.Name, spec.Prompt, execFn), nil
61 }
62 fmt.Println("✓ 创建子代理工厂")
63
64 // 4. 创建 SubAgentMiddleware(启用异步执行)
65 subagentMiddleware, err := middleware.NewSubAgentMiddleware(&middleware.SubAgentMiddlewareConfig{
66 Specs: specs,
67 Factory: factory,
68 EnableParallel: true,
69 EnableAsync: true, // 启用异步执行
70 EnableProcessIsolation: false, // 使用 goroutine 模式(演示用)
71 DefaultTimeout: 30 * time.Second,
72 })
73 if err != nil {

Callers

nothing calls this directly

Calls 10

ToolsMethod · 0.95
OnAgentStopMethod · 0.95
NewStateBackendFunction · 0.92
NewSimpleSubAgentFunction · 0.92
NewSubAgentMiddlewareFunction · 0.92
NewFilesystemMiddlewareFunction · 0.92
NewStackFunction · 0.92
PrintfMethod · 0.80
NameMethod · 0.65
ExecuteMethod · 0.65

Tested by

no test coverage detected