(ctx context.Context, args *AnalystAgentArgs)
| 100 | } |
| 101 | |
| 102 | func (t *AnalystAgent) Handler(ctx context.Context, args *AnalystAgentArgs) (*AnalystAgentResult, error) { |
| 103 | s := GetSession(ctx) |
| 104 | |
| 105 | // Determine if it's the first invocation of the agent in this session. |
| 106 | first := len(s.Messages(FilterByType(MessageTypeCall), FilterByTool(AnalystAgentName))) == 1 |
| 107 | |
| 108 | // If a specific dashboard is being explored, we pre-invoke some relevant tool calls for that dashboard. |
| 109 | // TODO: This uses `first`, but that may not be safe if the user has navigated to another dashboard. We probably need some more sophisticated de-duplication here. |
| 110 | var metricsViewNames []string |
| 111 | if first { |
| 112 | if args.Explore != "" { |
| 113 | _, metricsView, err := t.getValidExploreAndMetricsView(ctx, args.Explore) |
| 114 | if err != nil { |
| 115 | return nil, err |
| 116 | } |
| 117 | metricsViewNames = append(metricsViewNames, metricsView.Meta.Name.Name) |
| 118 | } else if args.Canvas != "" { |
| 119 | // Pre-invoke the get_canvas tool to get the canvas definition. |
| 120 | _, err := s.CallTool(ctx, RoleAssistant, GetCanvasName, nil, &GetCanvasArgs{ |
| 121 | Canvas: args.Canvas, |
| 122 | }) |
| 123 | if err != nil && errors.Is(err, ctx.Err()) { // Don't exit on non-context errors |
| 124 | return nil, err |
| 125 | } |
| 126 | |
| 127 | _, metricsViews, err := t.getValidCanvasAndMetricsViews(ctx, args.Canvas) |
| 128 | if err != nil { |
| 129 | return nil, err |
| 130 | } |
| 131 | |
| 132 | for _, res := range metricsViews { |
| 133 | metricsViewNames = append(metricsViewNames, res.Meta.Name.Name) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // Pre-invoke the query_metrics_view tool for each metrics view tied to the explore/canvas. |
| 138 | for _, mvName := range metricsViewNames { |
| 139 | _, err := s.CallTool(ctx, RoleAssistant, QueryMetricsViewSummaryName, nil, &QueryMetricsViewSummaryArgs{ |
| 140 | MetricsView: mvName, |
| 141 | }) |
| 142 | if err != nil && errors.Is(err, ctx.Err()) { // Don't exit on non-context errors |
| 143 | return nil, err |
| 144 | } |
| 145 | |
| 146 | _, err = s.CallTool(ctx, RoleAssistant, GetMetricsViewName, nil, &GetMetricsViewArgs{ |
| 147 | MetricsView: mvName, |
| 148 | }) |
| 149 | if err != nil && errors.Is(err, ctx.Err()) { // Don't exit on non-context errors |
| 150 | return nil, err |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // If no specific dashboard is being explored, we pre-invoke the list_metrics_views tool. |
| 156 | if first && len(metricsViewNames) == 0 { |
| 157 | _, err := s.CallTool(ctx, RoleAssistant, ListMetricsViewsName, nil, &ListMetricsViewsArgs{}) |
| 158 | if err != nil && errors.Is(err, ctx.Err()) { // Don't exit on non-context errors |
| 159 | return nil, err |
nothing calls this directly
no test coverage detected