Execute 执行工具
(ctx context.Context, input map[string]any, tctx *tools.ToolContext)
| 77 | |
| 78 | // Execute 执行工具 |
| 79 | func (t *LogicMemoryQueryTool) Execute(ctx context.Context, input map[string]any, tctx *tools.ToolContext) (any, error) { |
| 80 | action, _ := input["action"].(string) |
| 81 | namespace, _ := input["namespace"].(string) |
| 82 | |
| 83 | // 如果没有提供 namespace,尝试从上下文获取 |
| 84 | if namespace == "" && tctx != nil { |
| 85 | // 使用 ThreadID 或 AgentID 作为 namespace |
| 86 | if tctx.ThreadID != "" { |
| 87 | namespace = "thread:" + tctx.ThreadID |
| 88 | } else if tctx.AgentID != "" { |
| 89 | namespace = "agent:" + tctx.AgentID |
| 90 | } |
| 91 | } |
| 92 | if namespace == "" { |
| 93 | return nil, errors.New("namespace is required") |
| 94 | } |
| 95 | |
| 96 | switch action { |
| 97 | case "list": |
| 98 | return t.list(ctx, namespace, input) |
| 99 | case "get": |
| 100 | return t.get(ctx, namespace, input) |
| 101 | case "search": |
| 102 | return t.search(ctx, namespace, input) |
| 103 | case "stats": |
| 104 | return t.stats(ctx, namespace) |
| 105 | default: |
| 106 | return nil, fmt.Errorf("unknown action: %s", action) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Prompt 返回工具使用说明 |
| 111 | func (t *LogicMemoryQueryTool) Prompt() string { |