MCPcopy Create free account
hub / github.com/rilldata/rill / Handler

Method Handler

runtime/ai/analyst_agent.go:102–206  ·  view source on GitHub ↗
(ctx context.Context, args *AnalystAgentArgs)

Source from the content-addressed store, hash-verified

100}
101
102func (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

Callers

nothing calls this directly

Calls 14

systemPromptMethod · 0.95
GetSessionFunction · 0.85
FilterByTypeFunction · 0.85
FilterByToolFunction · 0.85
NewTextCompletionMessageFunction · 0.85
MessagesMethod · 0.80
CallToolMethod · 0.80
NewCompletionMessagesMethod · 0.80
MessagesWithChildrenMethod · 0.80
ErrMethod · 0.65

Tested by

no test coverage detected