Repository navigation
feat(ai): create, list and delete alerts from the chat (create_alert, list_alerts, delete_alert) - #10233
feat(ai): create, list and delete alerts from the chat (create_alert, list_alerts, delete_alert)#10233nishantmonu51 wants to merge 6 commits into
create_alert, list_alerts, delete_alert)#10233Conversation
…`, `list_alerts`, `delete_alert`) Adds tools that let the analyst create alerts for the user, list the user's own alerts and delete them, through the admin service's create- and delete-for-user API. Following the review of #10122, the chat only creates and deletes: to change an alert, the analyst links to its page, where the user edits it in the dialog. - `create_alert` saves what the alert dialog saves: a formula alert over the dialog's `MetricsViewAggregation` query (with its sort and comparison measures), or an AI alert whose condition the `ai` resolver checks. It requires an explore, writes comparisons as Rill time offsets, and checks AI alerts against the parser's schedule rules (a cron of at most once an hour) before saving. - What it creates can only reach project members: the admin service defaults recipients to the user and rejects others. - `list_alerts` reads the user's alerts from the runtime catalog: name, display name, explore, a one-line condition and the URL of the alert's page. - `delete_alert` only deletes the user's own alerts, and only once the user confirmed in a message after the tool asked for it, so what the tools read can't stand in for the user's confirmation. - The tools are only offered to users in prod deployments, not to services or to the sessions of AI reports and alerts. - Tests create alerts through the real admin service, so the YAML, parser and reconcile path is covered. Based on #10122. Co-authored-by: dfliess <dfliess@gmail.com>
nishantmonu51
left a comment
There was a problem hiding this comment.
A few things inline about creating and editing alerts.
| opts := &adminv1.AlertOptions{ | ||
| DisplayName: args.DisplayName, | ||
| MetricsViewName: explore.MetricsView, | ||
| WebOpenPath: "/explore/" + args.Explore, |
There was a problem hiding this comment.
An Explore named Revenue #1 is saved as /explore/Revenue #1, so getExploreName reads only Revenue and the criteria alert page can't find its Explore. Could we use url.PathEscape(args.Explore) here and decode the segment in ListAlerts.Handler? AlertForm.svelte already escapes this when creating an alert.
There was a problem hiding this comment.
Fixed in 8fee0ab: the open path uses url.PathEscape(args.Explore) and list_alerts decodes it, tested with an explore named Revenue #1.
| } else if args.Comparison != "" { | ||
| return errors.New("invalid input: comparison only applies to criteria of type 'absolute_change' or 'percent_change'") | ||
| } | ||
| if percentOfTotal { |
There was a problem hiding this comment.
A percent_of_total criterion without dimension compares the overall total with itself, so a condition like “any country is under 20% of revenue” can save an alert that never fires. Can setCriteria require a split dimension for percent-of-total, and criteriaHaving reject thresholds outside 0–100, matching the checks in form-utils.ts?
There was a problem hiding this comment.
Fixed in 8fee0ab: percent-of-total criteria now need a dimension and a threshold from 0 to 100.
| // The tools only create and delete: to change an alert or report, the user edits it in its dialog. | ||
|
|
||
| // confirmBeforeCreating is the part of the create tools' descriptions that makes the model confirm with the user. | ||
| const confirmBeforeCreating = "Before calling this tool, show the user a short preview of what you will create (name, data, condition or content, schedule and recipients), then end your turn and wait for them to confirm. " + |
There was a problem hiding this comment.
Could we make this preview a required step in the handler? If the analyst skips it, CreateAlert.Handler can schedule emails or Slack DMs to project members immediately. A confirmation action from the chat UI could commit the exact draft the user saw, including recipients and schedule. create_report could use the same flow.
There was a problem hiding this comment.
Leaving as is here: the earlier review picked limiting recipients to project members as the check enforced in code for creates, and a required preview with a chat UI confirm across both create tools is a larger change I have flagged as a follow-up decision.
| } | ||
| root = parent | ||
| } | ||
| if root.ID != current { |
There was a problem hiding this comment.
A delete proposal stays eligible after the user declines it or moves on, since proposedInEarlierTurn accepts any older matching result. Could we expire it after the next user turn and require a one-time user confirmation tied to that alert before calling del?
There was a problem hiding this comment.
Partly fixed in 8fee0ab: a proposal now only counts in the turn right after it, so it expires once the user moves on; a click-to-confirm in the chat UI is the follow-up noted in the PR description.
|
|
||
| // Schedule is when a report is sent or an alert is checked. | ||
| type Schedule struct { | ||
| Cron string `json:"cron" jsonschema:"Standard 5-field cron expression, for example '0 9 * * 1' for Mondays at 9:00."` |
There was a problem hiding this comment.
I checked 0 9 15 * * against the dialog's schedule helpers. Chat can save the 15th, then getExistingScheduleFormValues shows the 1st and saving a rename moves the schedule. Could ScheduleForm.svelte and its save handlers preserve the original cron when the schedule is untouched, and show the actual schedule for values the picker can't edit? This applies to chat-created reports too.
There was a problem hiding this comment.
Fixed on the tool side in 8fee0ab: the shared checkSchedule only accepts the daily, weekdays, weekly and monthly-on-the-1st crons the dialog can edit, so 0 9 15 * * and hourly crons are rejected (this covers create_report once #10234 picks it up); keeping other crons in ScheduleForm.svelte is a dialog change left for a follow-up.
| if err := protojson.Unmarshal([]byte(argsJSON), q); err != nil || q.Having == nil { | ||
| return "" | ||
| } | ||
| sql, err := metricsview.ExpressionToSQL(metricsview.NewExpressionFromProto(q.Having)) |
There was a problem hiding this comment.
list_alerts returns the same revenue_delta_perc < -0.1 for a week-over-week drop and a year-over-year drop. If a user has both, the analyst can't tell which one they mean from the condition. Could alertCondition include q.ComparisonTimeRange.Expression when present?
There was a problem hiding this comment.
Fixed in 8fee0ab: the condition now ends with , compared with <comparison time range> when there is one.
| Exprs: []*runtimev1.Expression{expressionpb.Identifier(name), expressionpb.Number(value)}, | ||
| }}}) | ||
| } | ||
| return &runtimev1.Expression{Expression: &runtimev1.Expression_Cond{Cond: &runtimev1.Condition{Op: op, Exprs: exprs}}}, comparison, percentOfTotal, nil |
There was a problem hiding this comment.
Could criteriaHaving pick expressionpb.And or expressionpb.Or in the joiner switch and call that helper here? Both construct the exact same protobuf wrapper.
There was a problem hiding this comment.
Fixed in 8fee0ab: the joiner switch picks expressionpb.And or expressionpb.Or.
- Escape the explore name in the alert's open path, and decode it in list_alerts. - Require a dimension for percent-of-total criteria and a threshold from 0 to 100, as the dialog does. - Only accept schedules the dialogs can edit, so saving an edit there doesn't move them. - A delete proposal expires after the user's next message. - list_alerts shows the time range a change is compared with. - Join criteria with expressionpb.And and expressionpb.Or.
|
The tools are much smaller and the saved query matches the dialog's, including the sort and the comparison measures. Accepting only the crons the dialog can show is the right call, so that editing in the UI keeps working. Three changes. Explore names. I asked for the metrics-view-name fallback to be dropped in the first review, and that was a mistake. The analyst has no tool that lists explores, so in a project-level chat the model only knows metrics view names, and Delete confirmation. Slack channels. With the change requested on #10232, please add Smaller point. The Developed in collaboration with Claude Code |
…e delete gate after review - validExplore's not found error lists the explores the user can access, with their metrics views, so the analyst can retry when it only knows a metrics view's name. - delete_alert deletes in one call: its description has the model confirm with the user first, as for create_alert. proposedInPreviousTurn and the confirmed argument are gone. - Recipients has slack_channels, and create_alert rejects Slack recipients when the project has no Slack connector, as the dialog does. - setAICondition no longer calls the parser's schedule check, which dialogCron already covers, so the parser's function is unexported again. - create_alert builds the alert's URL with resourcePageURL, since the admin service no longer returns it.
|
@pjain1 Thanks, done in 241a977 (after merging #10232 in 2b236dc):
|
nishantmonu51
left a comment
There was a problem hiding this comment.
A few things inline.
| // The explore exists, so the error is about something else, like an invalid spec | ||
| return nil, err | ||
| } | ||
| accessible = append(accessible, fmt.Sprintf("%s (metrics view %s)", r.Meta.Name.Name, r.GetExplore().Spec.MetricsView)) |
There was a problem hiding this comment.
If a bad Explore edit is staged, the dashboard can still serve its last valid spec. This fallback reads the draft Spec, so the chat can suggest an Explore with the wrong metrics view or one it can't create an alert for. Can we build the list from ValidSpec, the same spec the create path uses?
| accessible = append(accessible, fmt.Sprintf("%s (metrics view %s)", r.Meta.Name.Name, r.GetExplore().Spec.MetricsView)) | |
| if spec := r.GetExplore().GetState().GetValidSpec(); spec != nil { | |
| accessible = append(accessible, fmt.Sprintf("%s (metrics view %s)", r.Meta.Name.Name, spec.MetricsView)) | |
| } |
There was a problem hiding this comment.
Fixed in 9dbb346: the list is built from each explore's ValidSpec, so an explore without one isn't suggested, and a new runtime test covers it.
| if err != nil { | ||
| return err | ||
| } | ||
| if slices.ContainsFunc(slices.Concat(inst.Connectors, inst.ProjectConnectors), func(c *runtimev1.Connector) bool { return c.Name == "slack" && c.Type == "slack" }) { |
There was a problem hiding this comment.
A slack connector with an empty bot_token passes this check. So does a project with only an unrelated connector.slack.* setting. A Slack-only alert or report then saves successfully but fails to send. Could checkSlackConfigured use Runtime.ConnectorConfig and verify the effective driver and resolved bot_token? That also follows the same connector precedence as delivery.
There was a problem hiding this comment.
Fixed in 9dbb346: checkSlackConfigured now resolves the slack connector with Runtime.ConnectorConfig and requires the slack driver and a non-empty bot_token (a token variable that isn't set fails too), with a test for each case.
| "It can send to the user and other members of the project, by email or to their Slack user, and to Slack channels, and sends to the user by email if you pass no recipients. " | ||
|
|
||
| // confirmBeforeDeleting is the part of the delete tools' descriptions that makes the model confirm with the user. | ||
| const confirmBeforeDeleting = "Before calling this tool, tell the user which one you will delete, by its display name, then end your turn and wait for them to confirm. " |
There was a problem hiding this comment.
Can we update the PR description to match this change? It still says delete_alert returns confirmation_required and waits for a later user message. The first call now deletes the alert, and the confirmation prompt comes from the analyst's instructions.
There was a problem hiding this comment.
Agreed; editing the description is left to Nishant, and I've passed him the replacement lines.
…en after review - validExplore's not-found error lists explores by their valid spec, which the create tools use, so it doesn't suggest one whose last edit failed. - checkSlackConfigured resolves the slack connector like delivery does (Runtime.ConnectorConfig) and requires its bot token, so an empty token, an unset token variable or an unrelated connector.slack. variable no longer passes.
Takes over #10122 with the create-and-delete-only direction from its review. Stacked on #10232.
create_alertcreates a formula alert, saving the dialog'sMetricsViewAggregationquery with its sort and comparison measures, or an AI alert.list_alertsreads the user's own alerts from the runtime catalog: name, display name, explore, a one-line condition and the URL of the alert's page.delete_alertonly deletes the user's own alerts. Its first call returnsconfirmation_required, and a confirmed call only deletes if the user sent a message since. A prompt injection in tool results can't confirm on the user's behalf.Open for review: the delete gate is weaker than a click. A confirm button in the web chat would need a chat card plus a runtime call; that would be a follow-up if we want it.
Checklist: