Files
supabase/apps/studio/lib/ai/tools/get-report.server.ts
Charis e21b69c602 refactor(studio): migrate list_policies, list_reports, get_report, get_active_incidents, escalate_to_human, and resolve_support_conversation to tool contracts (#52015)
## Problem

Six assistant tools are still defined inline with `tool({...})`, unlike
the others that moved to the tool contract pattern.

## Solution

Migrate them to a contract file plus a `.server.ts` implementation,
registered in `TOOL_CONTRACTS`, removed from `TOOL_CATEGORY_MAP`, and
kept in the allowlist. The `*-tools.ts` modules remain as thin wrappers.
`ToolContext` gains an optional `baseUrl`, used by
`get_active_incidents`.

- `list_policies`, `list_reports`, `get_report`: `minOptInLevel:
'schema'`, output unchanged for the model at every level.
- `get_active_incidents`, `escalate_to_human`,
`resolve_support_conversation`: `minOptInLevel: null` (always
available), same as before.
- `needsApproval: false` for all.

Stored-shape changes for replayed outputs:

- Replayed outputs are now validated against the output schema and
dropped if invalid (previously unchecked).
- Zod strips unknown keys, so fields outside the schema no longer
replay.
- Enum-like fields use plain `z.string()` so new values don't break
replay.

Not migrated yet: `update_opt_in_level` (input schema is built per
request from the org's level), remote MCP tools, and self-hosted
fallback tools.
2026-10-20 14:18:27 -04:00

50 lines
1.5 KiB
TypeScript

import { getAuthHeaders } from './auth-headers'
import { contract } from './get-report'
import { toolFromContract } from './tool-from-contract'
import type { ToolFactory } from './types'
import { getContentById } from '@/data/content/content-id-query'
import type { Dashboards, SqlSnippets } from '@/types'
export const createTool: ToolFactory<typeof contract> = (ctx) => {
const { projectRef } = ctx
const authHeaders = getAuthHeaders(ctx.authorization)
return toolFromContract(contract, ctx, async ({ id }, { abortSignal }) => {
const report = await getContentById({ projectRef, id }, abortSignal, authHeaders)
if (report.type !== 'report') {
throw new Error(`Content ${id} is not a report (type: ${report.type})`)
}
const content = report.content as Dashboards.Content
const layout = await Promise.all(
content.layout.map(async (chart) => {
if (!chart.attribute.startsWith('snippet_')) return chart
const snippet = await getContentById(
{ projectRef, id: chart.id },
abortSignal,
authHeaders
).catch(() => null)
const sql =
snippet?.type === 'sql'
? (snippet.content as SqlSnippets.Content).unchecked_sql
: undefined
return { ...chart, sql }
})
)
return {
id: report.id,
name: report.name,
description: report.description,
visibility: report.visibility,
period_start: content.period_start,
period_end: content.period_end,
interval: content.interval,
layout,
}
})
}