The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
refactor(studio): migrate update_opt_in_level to a tool contract (#52081)
Charis96d51239c65f
diff --git a/apps/studio/components/ui/AIAssistantPanel/Message.Parts.tsx b/apps/studio/components/ui/AIAssistantPanel/Message.Parts.tsxindex fdc32dd960d..a5e4458bd2b 100644--- a/apps/studio/components/ui/AIAssistantPanel/Message.Parts.tsx+++ b/apps/studio/components/ui/AIAssistantPanel/Message.Parts.tsx@@ -31,8 +31,8 @@ import { NotebookRunRenderer } from './NotebookRunRenderer'import { OptInRequest } from './OptInRequest'import { parseSupportRequestMessage, SupportRequestMessage } from './SupportRequestMessage'import { useMinimumDisplayTime } from '@/hooks/misc/useMinimumDisplayTime'-import { storedUpdateOptInLevelInputSchema } from '@/lib/ai/tool-filter'import { contract as deployEdgeFunctionContract } from '@/lib/ai/tools/deploy-edge-function'+import { contract as updateOptInLevelContract } from '@/lib/ai/tools/update-opt-in-level'function MessagePartText({ textPart }: { textPart: TextUIPart }) {const { id, isLoading, readOnly, isUserMessage, state } = useMessageInfoContext()@@ -275,7 +275,7 @@ function MessagePartUpdateOptInLevel({ toolPart }: { toolPart: ToolUIPart }) {const { state, input, output } = toolPartconst { addToolApprovalResponse } = useMessageActionsContext()- const parsedInput = storedUpdateOptInLevelInputSchema.safeParse(input)+ const parsedInput = updateOptInLevelContract.inputSchema.safeParse(input)if (state === 'input-streaming' || !parsedInput.success) return nullconst { confirmState, onApprove, onDeny } = getManualToolApprovalHandlers({diff --git a/apps/studio/components/ui/AIAssistantPanel/OptInRequest.tsx b/apps/studio/components/ui/AIAssistantPanel/OptInRequest.tsxindex 3e8107edf9a..b05b3c6ec77 100644--- a/apps/studio/components/ui/AIAssistantPanel/OptInRequest.tsx+++ b/apps/studio/components/ui/AIAssistantPanel/OptInRequest.tsx@@ -1,6 +1,5 @@import { PermissionAction } from '@supabase/shared-types/out/constants'import { useState } from 'react'-import { z } from 'zod'import { AI_OPT_IN_LEVEL_LABELS } from './AiAssistant.constants'import { AIOptInModal } from './AIOptInModal'@@ -9,12 +8,7 @@ import type { ConfirmFooterApprovalState } from './Confirm.utils'import { NoPermission } from '@/components/ui/NoPermission'import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'import { useOrgAiOptInLevel, type AiOptInLevel } from '@/hooks/misc/useOrgOptedIntoAi'-import { optInLevelSchema } from '@/lib/ai/opt-in-level'--const updateOptInLevelOutputSchema = z.object({- levelAfterReview: optInLevelSchema,- sufficient: z.boolean().optional(),-})+import { contract } from '@/lib/ai/tools/update-opt-in-level'interface OptInRequestProps {/** Omitted when the Assistant is only asking the user to review the setting. */@@ -45,7 +39,7 @@ export const OptInRequest = ({)const requiredLabel = requiredLevel && AI_OPT_IN_LEVEL_LABELS[requiredLevel]- const result = updateOptInLevelOutputSchema.safeParse(output).data+ const result = contract.outputSchema.safeParse(output).data// Saving a lower level still approves the callconst isBelowRequest = confirmState === 'success' && result?.sufficient === falsediff --git a/apps/studio/evals/opt-in-replay.ts b/apps/studio/evals/opt-in-replay.tsindex 179e0ff7ca6..e7b170618a8 100644--- a/apps/studio/evals/opt-in-replay.ts+++ b/apps/studio/evals/opt-in-replay.ts@@ -4,7 +4,7 @@ import type { OptInDecision } from './scorer'import type { Transcript } from './transcript'import { USER_SKIPPED_TOOL_REASON } from '@/components/ui/AIAssistantPanel/Confirm.utils'import type { AiOptInLevel } from '@/hooks/misc/useOrgOptedIntoAi'-import { storedUpdateOptInLevelInputSchema } from '@/lib/ai/tool-filter'+import { contract as updateOptInLevelContract } from '@/lib/ai/tools/update-opt-in-level'const TOOL_PART_TYPE = 'tool-update_opt_in_level'@@ -36,7 +36,7 @@ export function applyOptInDecision(}isResolved = true- const requestedLevel = storedUpdateOptInLevelInputSchema.safeParse(part.input).data+ const requestedLevel = updateOptInLevelContract.inputSchema.safeParse(part.input).data?.requiredLevelconst isApproved = decision !== 'skip'if (!isApproved) deniedToolCalls.push({ toolName: 'update_opt_in_level', input: part.input })diff --git a/apps/studio/lib/ai/README.md b/apps/studio/lib/ai/README.mdindex 4130cc7175c..4f8ec1f6e22 100644--- a/apps/studio/lib/ai/README.md+++ b/apps/studio/lib/ai/README.md@@ -93,7 +93,7 @@ Each contract tool is a pair of files named after the tool, plus an optional out- `<tool-name>.server.ts` exports `createTool`, which builds the executable tool from the contract with `toolFromContract`, or `approvalGatedToolFromContract` when `needsApproval` is true.- `<tool-name>-output.ts` holds output schemas and transformations that outgrow the contract file.-[update-notebook.ts](./tools/update-notebook.ts) and [update-notebook.server.ts](./tools/update-notebook.server.ts) show the split with approval. [execute-sql.ts](./tools/execute-sql.ts) shows an output schema that also accepts an older persisted shape.+[update-notebook.ts](./tools/update-notebook.ts) and [update-notebook.server.ts](./tools/update-notebook.server.ts) show the split with approval. [update-opt-in-level.ts](./tools/update-opt-in-level.ts) shows `createInputSchema`, for a tool whose stored input depends on the request: the SDK stores the parsed input, so the contract's `inputSchema` describes the stored shape and the per-request schema is built from the context. [execute-sql.ts](./tools/execute-sql.ts) shows an output schema that also accepts an older persisted shape.To migrate a tool to a contract:@@ -122,7 +122,7 @@ These checks have different responsibilities:The opt-in levels are `disabled`, `schema`, `schema_and_log`, and `schema_and_log_and_data`. `execute_sql` is available at every level and requires user approval; sharing its result rows with the model requires `schema_and_log_and_data`. History sanitization also considers the level stamped on the original SQL output. Permission to change opt-in settings, user approval to run SQL, and the database's authorization checks are separate from result sharing.-The model can call `update_opt_in_level` with `requiredLevel` to request higher access, or without `requiredLevel` when the user asks to review or change the setting. Every call requires approval through the opt-in request card, even when the organization already has the requested level. If the requested level is already satisfied, the input transform removes `requiredLevel` and treats the call as a review request; `levelWhenAsked` records the organization's level when the call was made.+The model can call `update_opt_in_level` with `requiredLevel` to request higher access, or without `requiredLevel` when the user asks to review or change the setting. Every call requires approval through the opt-in request card, even when the organization already has the requested level. If the requested level is already satisfied, the contract's `createInputSchema` transform removes `requiredLevel` and treats the call as a review request; `levelWhenAsked` records the organization's level when the call was made.On platform, the endpoint resolves organization access and `getTools` includes platform tools when an access token is available. It also registers the opt-in request tool. Self-hosted requests use the `schema` level and fallback tools instead of the platform tool set. Check [getTools](./tools/index.ts) and [generate-v4](../../pages/api/ai/sql/generate-v4.ts) when changing either environment.diff --git a/apps/studio/lib/ai/tool-filter.test.ts b/apps/studio/lib/ai/tool-filter.test.tsindex 6df4273826c..d5ddf565d6e 100644--- a/apps/studio/lib/ai/tool-filter.test.ts+++ b/apps/studio/lib/ai/tool-filter.test.ts@@ -8,6 +8,7 @@ import {TOOL_CATEGORIES,TOOL_CATEGORY_MAP,toolSetValidationSchema,+ withoutApprovalTools,} from './tool-filter'import { contract as createNotebookContract } from './tools/create-notebook'import { contract as deleteNotebookContract } from './tools/delete-notebook'@@ -354,3 +355,20 @@ describe('toolSetValidationSchema', () => {expect(incompleteValidationResult.success).toBe(true) // Should still pass as we allow subsets})})++describe('withoutApprovalTools', () => {+ const makeTool = (needsApproval?: Tool['needsApproval']) =>+ ({ description: 'A tool', execute: vitest.fn(), needsApproval }) as unknown as Tool++ it('keeps tools without approval and drops gated ones', () => {+ const tools: ToolSet = {+ unset: makeTool(undefined),+ off: makeTool(false),+ on: makeTool(true),+ conditional: makeTool(() => false),+ stub: createPrivacyMessageTool(makeTool(true)) as Tool,+ }++ expect(Object.keys(withoutApprovalTools(tools))).toEqual(['unset', 'off'])+ })+})diff --git a/apps/studio/lib/ai/tool-filter.ts b/apps/studio/lib/ai/tool-filter.tsindex ad6ab41a3bd..eb6a292afd1 100644--- a/apps/studio/lib/ai/tool-filter.ts+++ b/apps/studio/lib/ai/tool-filter.ts@@ -1,7 +1,7 @@import type { Tool, ToolSet } from 'ai'import { z } from 'zod'-import { isOptInLevelAtLeast, optInLevelSchema } from './opt-in-level'+import { isOptInLevelAtLeast } from './opt-in-level'import { getToolContract, type ContractToolName } from './tools/registry'import type { AiOptInLevel } from '@/hooks/misc/useOrgOptedIntoAi'@@ -61,20 +61,6 @@ export const toolSetValidationSchema = z.record()export type ToolName = keyof z.infer<typeof toolSetValidationSchema>-export const updateOptInLevelInputSchema = z.object({- requiredLevel: z- .enum(['schema', 'schema_and_log', 'schema_and_log_and_data'])- .optional()- .describe(- 'The lowest opt-in level that unlocks what you need: `schema` for table and column metadata, `schema_and_log` for logs, `schema_and_log_and_data` for query results. Omit it when the user just wants to review or change the setting, for example to lower it.'- ),-})--/** Input as stored on the message. `levelWhenAsked` is the org's level when the call was made. */-export const storedUpdateOptInLevelInputSchema = updateOptInLevelInputSchema.extend({- levelWhenAsked: optInLevelSchema.optional(),-})-/*** Tool categories based on the data they access*/@@ -97,7 +83,6 @@ type ToolCategory = (typeof TOOL_CATEGORIES)[keyof typeof TOOL_CATEGORIES]*/export const TOOL_CATEGORY_MAP: Record<Exclude<ToolName, ContractToolName>, ToolCategory> = {// UI tools - always available- update_opt_in_level: TOOL_CATEGORIES.UI,search_docs: TOOL_CATEGORIES.UI,// Schema tools - MCP@@ -211,3 +196,13 @@ export function filterToolsByOptInLevel(tools: ToolSet, aiOptInLevel: AiOptInLev}))}++/**+ * Drops tools that need approval. A one-shot request can't answer an approval request, so a+ * call to one ends generation without output.+ */+export function withoutApprovalTools(tools: ToolSet): ToolSet {+ return Object.fromEntries(+ Object.entries(tools).filter(([, toolInstance]) => !toolInstance.needsApproval)+ )+}diff --git a/apps/studio/lib/ai/tools/execute-sql.server.ts b/apps/studio/lib/ai/tools/execute-sql.server.tsindex 0b26954341a..5aedfab52de 100644--- a/apps/studio/lib/ai/tools/execute-sql.server.ts+++ b/apps/studio/lib/ai/tools/execute-sql.server.ts@@ -9,7 +9,7 @@ import { DEFAULT_CELL_ROW_LIMIT } from '@/components/interfaces/Explorer/QueryCeimport { executeSql } from '@/data/sql/execute-sql-mutation'export const createTool: ApprovalGatedToolFactory<typeof contract> = (ctx) => {- const { projectRef, connectionString, aiOptInLevel = 'schema' } = ctx+ const { projectRef, connectionString, aiOptInLevel } = ctxconst authHeaders = getAuthHeaders(ctx.authorization, { contentType: true })return approvalGatedToolFromContract(diff --git a/apps/studio/lib/ai/tools/incident-tools.test.ts b/apps/studio/lib/ai/tools/incident-tools.test.tsindex ca5e0498762..215f1b41cca 100644--- a/apps/studio/lib/ai/tools/incident-tools.test.ts+++ b/apps/studio/lib/ai/tools/incident-tools.test.ts@@ -37,21 +37,30 @@ describe('ai/tools/incident-tools', () => {describe('getIncidentTools', () => {it('should return an object with get_active_incidents tool', () => {- const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })+ const tools = getIncidentTools({+ baseUrl: 'https://supabase.com/dashboard',+ aiOptInLevel: 'schema',+ })expect(tools).toBeDefined()expect(tools.get_active_incidents).toBeDefined()})it('should have correct description for get_active_incidents', () => {- const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })+ const tools = getIncidentTools({+ baseUrl: 'https://supabase.com/dashboard',+ aiOptInLevel: 'schema',+ })expect(tools.get_active_incidents.description).toContain('Check for active incidents')expect(tools.get_active_incidents.description).toContain('Supabase service')})it('should have empty input schema', () => {- const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })+ const tools = getIncidentTools({+ baseUrl: 'https://supabase.com/dashboard',+ aiOptInLevel: 'schema',+ })const schema = tools.get_active_incidents.inputSchema// The schema is a Zod object that accepts empty object@@ -64,7 +73,10 @@ describe('ai/tools/incident-tools', () => {const common = await import('common')vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(false)- const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })+ const tools = getIncidentTools({+ baseUrl: 'https://supabase.com/dashboard',+ aiOptInLevel: 'schema',+ })const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)expect(result).toEqual({@@ -80,7 +92,10 @@ describe('ai/tools/incident-tools', () => {mockFetch.mockRejectedValue(new Error('Network error'))- const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })+ const tools = getIncidentTools({+ baseUrl: 'https://supabase.com/dashboard',+ aiOptInLevel: 'schema',+ })await expect((tools.get_active_incidents.execute as any)({}, executeOptions)).rejects.toThrow('Network error')@@ -95,7 +110,10 @@ describe('ai/tools/incident-tools', () => {json: async () => statusPagePayload(),})- const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })+ const tools = getIncidentTools({+ baseUrl: 'https://supabase.com/dashboard',+ aiOptInLevel: 'schema',+ })if (!tools.get_active_incidents.execute) throw new Error('execute is undefined')await tools.get_active_incidents.execute({},@@ -113,7 +131,10 @@ describe('ai/tools/incident-tools', () => {})const abortController = new AbortController()- const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })+ const tools = getIncidentTools({+ baseUrl: 'https://supabase.com/dashboard',+ aiOptInLevel: 'schema',+ })if (!tools.get_active_incidents.execute) throw new Error('execute is undefined')await tools.get_active_incidents.execute({},@@ -162,7 +183,10 @@ describe('ai/tools/incident-tools', () => {json: async () => statusPagePayload(),})- const tools = getIncidentTools({ baseUrl: 'https://example.com/dashboard' })+ const tools = getIncidentTools({+ baseUrl: 'https://example.com/dashboard',+ aiOptInLevel: 'schema',+ })if (!tools.get_active_incidents.execute) throw new Error('execute is undefined')await tools.get_active_incidents.execute({}, executeOptions)@@ -184,7 +208,10 @@ describe('ai/tools/incident-tools', () => {}),})- const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })+ const tools = getIncidentTools({+ baseUrl: 'https://supabase.com/dashboard',+ aiOptInLevel: 'schema',+ })const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)expect((result as any).incidents).toHaveLength(1)@@ -203,7 +230,10 @@ describe('ai/tools/incident-tools', () => {}),})- const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })+ const tools = getIncidentTools({+ baseUrl: 'https://supabase.com/dashboard',+ aiOptInLevel: 'schema',+ })const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)expect((result as any).incidents).toHaveLength(1)@@ -235,7 +265,10 @@ describe('ai/tools/incident-tools', () => {}),})- const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })+ const tools = getIncidentTools({+ baseUrl: 'https://supabase.com/dashboard',+ aiOptInLevel: 'schema',+ })const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)expect((result as any).incidents).toHaveLength(2)@@ -255,7 +288,10 @@ describe('ai/tools/incident-tools', () => {}),})- const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })+ const tools = getIncidentTools({+ baseUrl: 'https://supabase.com/dashboard',+ aiOptInLevel: 'schema',+ })const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)expect((result as any).incidents).toHaveLength(1)@@ -269,7 +305,10 @@ describe('ai/tools/incident-tools', () => {json: async () => statusPagePayload(),})- const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })+ const tools = getIncidentTools({+ baseUrl: 'https://supabase.com/dashboard',+ aiOptInLevel: 'schema',+ })const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)expect(result).toEqual({@@ -284,7 +323,10 @@ describe('ai/tools/incident-tools', () => {json: async () => ({ this_is: 'not a valid status page response' }),})- const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })+ const tools = getIncidentTools({+ baseUrl: 'https://supabase.com/dashboard',+ aiOptInLevel: 'schema',+ })await expect((tools.get_active_incidents.execute as any)({}, executeOptions)).rejects.toThrow('Failed to parse status page response')@@ -296,7 +338,10 @@ describe('ai/tools/incident-tools', () => {status: 500,})- const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })+ const tools = getIncidentTools({+ baseUrl: 'https://supabase.com/dashboard',+ aiOptInLevel: 'schema',+ })await expect((tools.get_active_incidents.execute as any)({}, executeOptions)).rejects.toThrow('Failed to fetch status page: 500')diff --git a/apps/studio/lib/ai/tools/index.test.ts b/apps/studio/lib/ai/tools/index.test.tsindex b221f4c2703..ed0acfcda00 100644--- a/apps/studio/lib/ai/tools/index.test.ts+++ b/apps/studio/lib/ai/tools/index.test.ts@@ -62,6 +62,7 @@ describe('ai/tools getTools', () => {projectRef: BASE_PARAMS.projectRef,connectionString: BASE_PARAMS.connectionString,authorization: BASE_PARAMS.authorization,+ aiOptInLevel: BASE_PARAMS.aiOptInLevel,})})diff --git a/apps/studio/lib/ai/tools/index.ts b/apps/studio/lib/ai/tools/index.tsindex 23d2aff373f..9362705c263 100644--- a/apps/studio/lib/ai/tools/index.ts+++ b/apps/studio/lib/ai/tools/index.ts@@ -53,6 +53,7 @@ export const getTools = async ({projectRef,connectionString,authorization,+ aiOptInLevel,includeSchemaMetadata: aiOptInLevel !== 'disabled',}),}@@ -80,8 +81,9 @@ export const getTools = async ({projectRef,connectionString,authorization,+ aiOptInLevel,}),- ...getReportTools({ projectRef, authorization }),+ ...getReportTools({ projectRef, authorization, aiOptInLevel }),...(isExplorerEnabled? getNotebookTools({projectRef,@@ -90,12 +92,14 @@ export const getTools = async ({aiOptInLevel,}): {}),- ...(baseUrl ? getIncidentTools({ baseUrl }) : {}),+ ...(baseUrl ? getIncidentTools({ baseUrl, aiOptInLevel }) : {}),}}// Filter all tools based on the (potentially modified) AI opt-in level- const toolsWithSupport = supportMode ? { ...tools, ...getSupportLifecycleTools() } : tools+ const toolsWithSupport = supportMode+ ? { ...tools, ...getSupportLifecycleTools({ aiOptInLevel }) }+ : toolsconst filteredTools: ToolSet = filterToolsByOptInLevel(toolsWithSupport, aiOptInLevel)return filteredToolsdiff --git a/apps/studio/lib/ai/tools/mock-tools.test.ts b/apps/studio/lib/ai/tools/mock-tools.test.tsindex f560fb00ce6..2e0568a5ba5 100644--- a/apps/studio/lib/ai/tools/mock-tools.test.ts+++ b/apps/studio/lib/ai/tools/mock-tools.test.ts@@ -124,7 +124,7 @@ describe('ai/tools/mock-tools getMockTools', () => {it('overrides create_notebook needsApproval to false, unlike the real tool', async () => {const mockTools = await getMockTools(undefined)- expect(getNotebookTools().create_notebook.needsApproval).toBe(true)+ expect(getNotebookTools({ aiOptInLevel: 'schema' }).create_notebook.needsApproval).toBe(true)expect(mockTools.create_notebook.needsApproval).toBe(false)})@@ -167,7 +167,7 @@ describe('ai/tools/mock-tools getMockTools', () => {it('overrides update_notebook needsApproval to false, unlike the real tool', async () => {const mockTools = await getMockTools(undefined)- expect(getNotebookTools().update_notebook.needsApproval).toBe(true)+ expect(getNotebookTools({ aiOptInLevel: 'schema' }).update_notebook.needsApproval).toBe(true)expect(mockTools.update_notebook.needsApproval).toBe(false)})@@ -249,7 +249,7 @@ describe('ai/tools/mock-tools getMockTools', () => {it('overrides delete_notebook needsApproval to false, unlike the real tool', async () => {const mockTools = await getMockTools(undefined)- expect(getNotebookTools().delete_notebook.needsApproval).toBe(true)+ expect(getNotebookTools({ aiOptInLevel: 'schema' }).delete_notebook.needsApproval).toBe(true)expect(mockTools.delete_notebook.needsApproval).toBe(false)})diff --git a/apps/studio/lib/ai/tools/mock-tools.ts b/apps/studio/lib/ai/tools/mock-tools.tsindex a8792aef667..28bcd0596ba 100644--- a/apps/studio/lib/ai/tools/mock-tools.ts+++ b/apps/studio/lib/ai/tools/mock-tools.ts@@ -233,7 +233,9 @@ export const MOCK_NOTEBOOKS_DATA: MockNotebook[] = []function createMockedStudioTools() {- const studioTools = getStudioTools()+ // Pinned to `schema` so the model sees the same execute_sql output it always has in evals,+ // whatever level the eval runs at. Evals don't yet use one level for every tool and for replay.+ const studioTools = getStudioTools({ aiOptInLevel: 'schema' })return Object.fromEntries(Object.entries(studioTools).map(([name, baseTool]) => {diff --git a/apps/studio/lib/ai/tools/notebook-tools.test.ts b/apps/studio/lib/ai/tools/notebook-tools.test.tsindex 11c6c5c0fb5..717b0fd3257 100644--- a/apps/studio/lib/ai/tools/notebook-tools.test.ts+++ b/apps/studio/lib/ai/tools/notebook-tools.test.ts@@ -113,7 +113,7 @@ function mockGetNotebook() {describe('ai/tools/notebook-tools', () => {describe('getNotebookTools', () => {it('should return all notebook tools', () => {- const tools = getNotebookTools()+ const tools = getNotebookTools({ aiOptInLevel: 'schema' })expect(Object.keys(tools)).toEqual(['list_databases',@@ -127,7 +127,7 @@ describe('ai/tools/notebook-tools', () => {})it('should not require approval to read notebooks', () => {- const tools = getNotebookTools()+ const tools = getNotebookTools({ aiOptInLevel: 'schema' })expect(tools.list_databases.needsApproval).toBe(false)expect(tools.list_notebooks.needsApproval).toBe(false)@@ -135,7 +135,7 @@ describe('ai/tools/notebook-tools', () => {})it('should require approval to run, create, update, or delete a notebook', () => {- const tools = getNotebookTools()+ const tools = getNotebookTools({ aiOptInLevel: 'schema' })expect(tools.run_notebook.needsApproval).toBe(true)expect(tools.create_notebook.needsApproval).toBe(true)@@ -184,7 +184,7 @@ describe('ai/tools/notebook-tools', () => {]),})- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.list_databases.execute) throw new Error('execute is undefined')const result = await tools.list_databases.execute(@@ -392,7 +392,7 @@ describe('ai/tools/notebook-tools', () => {it('rejects the run when the notebook changed after it was read', async () => {mockGetNotebook()- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.run_notebook.execute) throw new Error('execute is undefined')await expect(@@ -523,6 +523,7 @@ describe('ai/tools/notebook-tools', () => {const tools = getNotebookTools({projectRef: 'test-project',authorization: 'Bearer token',+ aiOptInLevel: 'schema',})if (!tools.list_notebooks.execute) throw new Error('execute is undefined')@@ -568,7 +569,7 @@ describe('ai/tools/notebook-tools', () => {},})- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.list_notebooks.execute) throw new Error('execute is undefined')await tools.list_notebooks.execute(@@ -603,7 +604,7 @@ describe('ai/tools/notebook-tools', () => {} as unknown as GetUserContentByIdResponse),})- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.get_notebook.execute) throw new Error('execute is undefined')const result = await tools.get_notebook.execute(@@ -658,7 +659,7 @@ describe('ai/tools/notebook-tools', () => {}),})- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.get_notebook.execute) throw new Error('execute is undefined')await expect(@@ -676,7 +677,7 @@ describe('ai/tools/notebook-tools', () => {response: () => HttpResponse.json<APIErrorBody>({ message: 'Not found' }, { status: 404 }),})- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.get_notebook.execute) throw new Error('execute is undefined')await expect(@@ -690,7 +691,7 @@ describe('ai/tools/notebook-tools', () => {describe('create_notebook', () => {it('should reject content whose cells carry an agent-supplied id', () => {- const tools = getNotebookTools()+ const tools = getNotebookTools({ aiOptInLevel: 'schema' })const schema = tools.create_notebook.inputSchemaif (!('safeParse' in schema)) throw new Error('inputSchema has no safeParse')@@ -707,7 +708,7 @@ describe('ai/tools/notebook-tools', () => {})it('should accept a valid id-less notebook with all three cell types', () => {- const tools = getNotebookTools()+ const tools = getNotebookTools({ aiOptInLevel: 'schema' })const schema = tools.create_notebook.inputSchemaif (!('safeParse' in schema)) throw new Error('inputSchema has no safeParse')@@ -732,7 +733,7 @@ describe('ai/tools/notebook-tools', () => {},})- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.create_notebook.execute) throw new Error('execute is undefined')await tools.create_notebook.execute(@@ -766,7 +767,7 @@ describe('ai/tools/notebook-tools', () => {},})- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.create_notebook.execute) throw new Error('execute is undefined')const result = await tools.create_notebook.execute(@@ -781,7 +782,7 @@ describe('ai/tools/notebook-tools', () => {it('should throw an assistant-exposable error instead of PUTting when a database_cell carries an unknown database_identifier', async () => {mockDatabases(['test-project', 'test-project-replica-1'])- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.create_notebook.execute) throw new Error('execute is undefined')const execute = tools.create_notebook.execute(@@ -810,7 +811,7 @@ describe('ai/tools/notebook-tools', () => {it('should throw an assistant-exposable error instead of PUTting when a database_cell carries an empty database_identifier', async () => {mockDatabases(['test-project', 'test-project-replica-1'])- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.create_notebook.execute) throw new Error('execute is undefined')const execute = tools.create_notebook.execute(@@ -839,7 +840,7 @@ describe('ai/tools/notebook-tools', () => {mockDatabases(['test-project', 'test-project-replica-1'])mockCreateNotebookPut()- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.create_notebook.execute) throw new Error('execute is undefined')await expect(@@ -866,7 +867,7 @@ describe('ai/tools/notebook-tools', () => {it('should succeed without ever calling the databases endpoint when no cell sets database_identifier', async () => {mockCreateNotebookPut()- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.create_notebook.execute) throw new Error('execute is undefined')// No mock registered for GET /platform/projects/:ref/databases: MSW fails the test@@ -893,7 +894,7 @@ describe('ai/tools/notebook-tools', () => {},})- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.update_notebook.execute) throw new Error('execute is undefined')const result = await tools.update_notebook.execute(@@ -958,7 +959,7 @@ describe('ai/tools/notebook-tools', () => {it('should throw a descriptive, assistant-exposable error instead of PUTting when an operation targets an unknown cell id', async () => {mockGetNotebook()- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.update_notebook.execute) throw new Error('execute is undefined')const execute = tools.update_notebook.execute(@@ -978,7 +979,7 @@ describe('ai/tools/notebook-tools', () => {it('should throw an assistant-exposable error instead of PUTting when the notebook changed since expected_updated_at', async () => {mockGetNotebook()- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.update_notebook.execute) throw new Error('execute is undefined')const execute = tools.update_notebook.execute(@@ -999,7 +1000,7 @@ describe('ai/tools/notebook-tools', () => {mockGetNotebook()mockDatabases(['test-project'])- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.update_notebook.execute) throw new Error('execute is undefined')const execute = tools.update_notebook.execute(@@ -1036,7 +1037,7 @@ describe('ai/tools/notebook-tools', () => {response: () => new HttpResponse(null),})- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.update_notebook.execute) throw new Error('execute is undefined')await expect(@@ -1066,7 +1067,7 @@ describe('ai/tools/notebook-tools', () => {mockGetNotebook()mockDatabases(['test-project'])- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.update_notebook.execute) throw new Error('execute is undefined')const execute = tools.update_notebook.execute(@@ -1103,7 +1104,7 @@ describe('ai/tools/notebook-tools', () => {response: () => new HttpResponse(null),})- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.update_notebook.execute) throw new Error('execute is undefined')await expect(@@ -1165,7 +1166,7 @@ describe('ai/tools/notebook-tools', () => {response: () => new HttpResponse(null),})- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.update_notebook.execute) throw new Error('execute is undefined')// No mock registered for GET /platform/projects/:ref/databases: MSW fails the test@@ -1196,7 +1197,7 @@ describe('ai/tools/notebook-tools', () => {},})- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.delete_notebook.execute) throw new Error('execute is undefined')const result = await tools.delete_notebook.execute(@@ -1217,7 +1218,7 @@ describe('ai/tools/notebook-tools', () => {response: () => HttpResponse.json<APIErrorBody>({ message: 'Not found' }, { status: 404 }),})- const tools = getNotebookTools({ projectRef: 'test-project' })+ const tools = getNotebookTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.delete_notebook.execute) throw new Error('execute is undefined')// No mock registered for DELETE /platform/projects/:ref/content: MSW fails the testdiff --git a/apps/studio/lib/ai/tools/notebook-tools.ts b/apps/studio/lib/ai/tools/notebook-tools.tsindex 6bb60f0d9fc..84825314ab1 100644--- a/apps/studio/lib/ai/tools/notebook-tools.ts+++ b/apps/studio/lib/ai/tools/notebook-tools.ts@@ -7,7 +7,7 @@ import { createTool as createRunNotebookTool } from './run-notebook.server'import type { ToolContext } from './types'import { createTool as createUpdateNotebookTool } from './update-notebook.server'-export const getNotebookTools = (ctx: ToolContext = {}) => {+export const getNotebookTools = (ctx: ToolContext) => {return {list_databases: createListDatabasesTool(ctx),list_notebooks: createListNotebooksTool(ctx),diff --git a/apps/studio/lib/ai/tools/registry.ts b/apps/studio/lib/ai/tools/registry.tsindex d7af98698f5..a429a5e0588 100644--- a/apps/studio/lib/ai/tools/registry.ts+++ b/apps/studio/lib/ai/tools/registry.ts@@ -19,6 +19,7 @@ import { contract as resolveSupportConversation } from './resolve-support-converimport { contract as runNotebook } from './run-notebook'import type { ToolContract, ToolContractInput, ToolContractOutput } from './types'import { contract as updateNotebook } from './update-notebook'+import { contract as updateOptInLevel } from './update-opt-in-level'import type { AiOptInLevel } from '@/hooks/misc/useOrgOptedIntoAi'/**@@ -43,6 +44,7 @@ export const TOOL_CONTRACTS = {resolve_support_conversation: resolveSupportConversation,run_notebook: runNotebook,update_notebook: updateNotebook,+ update_opt_in_level: updateOptInLevel,} as const satisfies Partial<Record<ToolName, ToolContract>>export type ContractToolName = keyof typeof TOOL_CONTRACTSdiff --git a/apps/studio/lib/ai/tools/report-tools.test.ts b/apps/studio/lib/ai/tools/report-tools.test.tsindex d83b4f1613f..2264ca42316 100644--- a/apps/studio/lib/ai/tools/report-tools.test.ts+++ b/apps/studio/lib/ai/tools/report-tools.test.ts@@ -10,13 +10,13 @@ type GetUserContentByIdResponse = components['schemas']['GetUserContentByIdRespodescribe('ai/tools/report-tools', () => {describe('getReportTools', () => {it('should return list_reports and get_report tools', () => {- const tools = getReportTools()+ const tools = getReportTools({ aiOptInLevel: 'schema' })expect(Object.keys(tools)).toEqual(['list_reports', 'get_report'])})it('should not require approval to read reports', () => {- const tools = getReportTools()+ const tools = getReportTools({ aiOptInLevel: 'schema' })expect(tools.list_reports.needsApproval).toBe(false)expect(tools.get_report.needsApproval).toBe(false)@@ -61,7 +61,11 @@ describe('ai/tools/report-tools', () => {},})- const tools = getReportTools({ projectRef: 'test-project', authorization: 'Bearer token' })+ const tools = getReportTools({+ projectRef: 'test-project',+ authorization: 'Bearer token',+ aiOptInLevel: 'schema',+ })if (!tools.list_reports.execute) throw new Error('execute is undefined')const result = await tools.list_reports.execute(@@ -164,7 +168,7 @@ describe('ai/tools/report-tools', () => {},})- const tools = getReportTools({ projectRef: 'test-project' })+ const tools = getReportTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.get_report.execute) throw new Error('execute is undefined')const result = (await tools.get_report.execute(@@ -204,7 +208,7 @@ describe('ai/tools/report-tools', () => {}),})- const tools = getReportTools({ projectRef: 'test-project' })+ const tools = getReportTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.get_report.execute) throw new Error('execute is undefined')await expect(diff --git a/apps/studio/lib/ai/tools/report-tools.ts b/apps/studio/lib/ai/tools/report-tools.tsindex 34fdb27d366..3e2590ebd8d 100644--- a/apps/studio/lib/ai/tools/report-tools.ts+++ b/apps/studio/lib/ai/tools/report-tools.ts@@ -2,7 +2,7 @@ import { createTool as createGetReportTool } from './get-report.server'import { createTool as createListReportsTool } from './list-reports.server'import type { ToolContext } from './types'-export const getReportTools = (ctx: ToolContext = {}) => ({+export const getReportTools = (ctx: ToolContext) => ({list_reports: createListReportsTool(ctx),get_report: createGetReportTool(ctx),})diff --git a/apps/studio/lib/ai/tools/run-notebook.server.ts b/apps/studio/lib/ai/tools/run-notebook.server.tsindex 7b7bdc1fdb3..fb19d39959b 100644--- a/apps/studio/lib/ai/tools/run-notebook.server.ts+++ b/apps/studio/lib/ai/tools/run-notebook.server.ts@@ -24,7 +24,7 @@ function getErrorMessage(error: unknown): string {}export const createTool: ApprovalGatedToolFactory<typeof contract> = (ctx) => {- const { projectRef, connectionString, aiOptInLevel = 'schema' } = ctx+ const { projectRef, connectionString, aiOptInLevel } = ctxconst authHeaders = getAuthHeaders(ctx.authorization)return approvalGatedToolFromContract(diff --git a/apps/studio/lib/ai/tools/schema-tools.test.ts b/apps/studio/lib/ai/tools/schema-tools.test.tsindex ecdbc4af7a2..09ca37c898a 100644--- a/apps/studio/lib/ai/tools/schema-tools.test.ts+++ b/apps/studio/lib/ai/tools/schema-tools.test.ts@@ -31,6 +31,7 @@ describe('ai/tools/schema-tools list_policies', () => {projectRef: 'abcdefghijklmnopqrst',connectionString: 'encrypted',authorization: 'Bearer token',+ aiOptInLevel: 'schema',})if (!tools.list_policies.execute) throw new Error('execute is undefined')@@ -51,6 +52,7 @@ describe('ai/tools/schema-tools list_policies', () => {const tools = getSchemaTools({projectRef: 'abcdefghijklmnopqrst',connectionString: 'encrypted',+ aiOptInLevel: 'schema',})if (!tools.list_policies.execute) throw new Error('execute is undefined')diff --git a/apps/studio/lib/ai/tools/schema-tools.ts b/apps/studio/lib/ai/tools/schema-tools.tsindex 093bd5689a9..e84ccd99394 100644--- a/apps/studio/lib/ai/tools/schema-tools.ts+++ b/apps/studio/lib/ai/tools/schema-tools.ts@@ -1,6 +1,6 @@import { createTool as createListPoliciesTool } from './list-policies.server'import type { ToolContext } from './types'-export const getSchemaTools = (ctx: ToolContext = {}) => ({+export const getSchemaTools = (ctx: ToolContext) => ({list_policies: createListPoliciesTool(ctx),})diff --git a/apps/studio/lib/ai/tools/studio-tools.test.ts b/apps/studio/lib/ai/tools/studio-tools.test.tsindex c035b54ec88..5a0954f93d5 100644--- a/apps/studio/lib/ai/tools/studio-tools.test.ts+++ b/apps/studio/lib/ai/tools/studio-tools.test.ts@@ -1,7 +1,7 @@import { safeSql } from '@supabase/pg-meta'import { beforeEach, describe, expect, it, vi } from 'vitest'-import { createUpdateOptInLevelInputSchema, getOptInTools, getStudioTools } from './studio-tools'+import { getOptInTools, getStudioTools } from './studio-tools'import { executeSql } from '@/data/sql/execute-sql-mutation'import { NO_DATA_PERMISSIONS } from '@/lib/ai/tools/execute-sql-output'@@ -16,35 +16,35 @@ describe('ai/tools/studio-tools', () => {describe('getStudioTools', () => {it('should return an object with tool definitions', () => {- const tools = getStudioTools()+ const tools = getStudioTools({ aiOptInLevel: 'schema' })expect(tools).toBeDefined()expect(typeof tools).toBe('object')})it('should include execute_sql tool', () => {- const tools = getStudioTools()+ const tools = getStudioTools({ aiOptInLevel: 'schema' })expect(tools.execute_sql).toBeDefined()expect(tools.execute_sql.description).toContain('execute a SQL statement')})it('should include deploy_edge_function tool', () => {- const tools = getStudioTools()+ const tools = getStudioTools({ aiOptInLevel: 'schema' })expect(tools.deploy_edge_function).toBeDefined()expect(tools.deploy_edge_function.description).toContain('deploy a Supabase Edge Function')})it('should include rename_chat tool', () => {- const tools = getStudioTools()+ const tools = getStudioTools({ aiOptInLevel: 'schema' })expect(tools.rename_chat).toBeDefined()expect(tools.rename_chat.description).toContain('Rename the current chat session')})it('should have exactly 4 tools', () => {- const tools = getStudioTools()+ const tools = getStudioTools({ aiOptInLevel: 'schema' })const toolNames = Object.keys(tools)expect(toolNames).toHaveLength(4)@@ -55,7 +55,7 @@ describe('ai/tools/studio-tools', () => {})it('should include logs in the load_knowledge schema', () => {- const tools = getStudioTools()+ const tools = getStudioTools({ aiOptInLevel: 'schema' })const schema = tools.load_knowledge.inputSchemaif ('safeParse' in schema) {@@ -68,7 +68,7 @@ describe('ai/tools/studio-tools', () => {})it('should return ClickHouse logs knowledge for load_knowledge logs', async () => {- const tools = getStudioTools()+ const tools = getStudioTools({ aiOptInLevel: 'schema' })if (!tools.load_knowledge.execute) throw new Error('execute is undefined')const result = await tools.load_knowledge.execute(@@ -83,7 +83,7 @@ describe('ai/tools/studio-tools', () => {})it('should have execute_sql with correct input schema fields', () => {- const tools = getStudioTools()+ const tools = getStudioTools({ aiOptInLevel: 'schema' })const executeSqlTool = tools.execute_sql// Check that the tool has an input schema@@ -96,7 +96,7 @@ describe('ai/tools/studio-tools', () => {})it('should have deploy_edge_function with input schema', () => {- const tools = getStudioTools()+ const tools = getStudioTools({ aiOptInLevel: 'schema' })const deployTool = tools.deploy_edge_functionexpect(deployTool.inputSchema).toBeDefined()@@ -107,7 +107,7 @@ describe('ai/tools/studio-tools', () => {})it('should have rename_chat with execute function', async () => {- const tools = getStudioTools()+ const tools = getStudioTools({ aiOptInLevel: 'schema' })const renameTool = tools.rename_chatexpect(renameTool.execute).toBeDefined()@@ -123,7 +123,7 @@ describe('ai/tools/studio-tools', () => {})it('should validate execute_sql input schema correctly', () => {- const tools = getStudioTools()+ const tools = getStudioTools({ aiOptInLevel: 'schema' })const schema = tools.execute_sql.inputSchema// Check if schema is a Zod schema with safeParse@@ -159,7 +159,7 @@ describe('ai/tools/studio-tools', () => {})it('should require approval for read and write SQL queries', () => {- const tools = getStudioTools()+ const tools = getStudioTools({ aiOptInLevel: 'schema' })expect(tools.execute_sql.needsApproval).toBe(true)})@@ -240,7 +240,7 @@ describe('ai/tools/studio-tools', () => {it('should push a row limit into read queries but leave write queries untouched', async () => {vi.mocked(executeSql).mockResolvedValue({ result: [] })- const tools = getStudioTools({ projectRef: 'test-project' })+ const tools = getStudioTools({ projectRef: 'test-project', aiOptInLevel: 'schema' })if (!tools.execute_sql.execute) throw new Error('execute is undefined')const options = { toolCallId: 'test', messages: [], context: {} }const base = { label: 'Query', chartConfig: { view: 'table' as const } }@@ -327,7 +327,7 @@ describe('ai/tools/studio-tools', () => {})it('should validate rename_chat input schema correctly', () => {- const tools = getStudioTools()+ const tools = getStudioTools({ aiOptInLevel: 'schema' })const schema = tools.rename_chat.inputSchema// Check if schema is a Zod schema with safeParse@@ -349,39 +349,7 @@ describe('ai/tools/studio-tools', () => {})describe('ai/tools/studio-tools getOptInTools', () => {- it.each([- ['schema', 'schema_and_log_and_data', false],- ['schema_and_log_and_data', 'schema', true],- ['schema', 'schema', true],- ['schema', undefined, undefined],- ] as const)(- 'level %s after approval, required %s → sufficient %s',- async (level, requiredLevel, sufficient) => {- const { update_opt_in_level } = getOptInTools({ aiOptInLevel: level })- if (!update_opt_in_level.execute) throw new Error('execute is undefined')-- const result = await update_opt_in_level.execute(- { levelWhenAsked: level, requiredLevel },- { toolCallId: 'test', messages: [], context: {} }- )-- expect(update_opt_in_level.needsApproval).toBe(true)- expect(result).toMatchObject({- levelAfterReview: level,- ...(sufficient !== undefined && { sufficient }),- })- // Only an insufficient result carries an instruction for the model- if (sufficient === false) expect(result).toHaveProperty('status')- else expect(result).not.toHaveProperty('status')- }- )-- it.each([- ['schema_and_log_and_data', 'schema', undefined],- ['schema', 'schema', undefined],- ['schema', 'schema_and_log', 'schema_and_log'],- ] as const)('at level %s, requested %s is stored as %s', (level, requested, stored) => {- const parsed = createUpdateOptInLevelInputSchema(level).parse({ requiredLevel: requested })- expect(parsed).toEqual({ levelWhenAsked: level, requiredLevel: stored })+ it('registers update_opt_in_level for the level', () => {+ expect(Object.keys(getOptInTools({ aiOptInLevel: 'schema' }))).toEqual(['update_opt_in_level'])})})diff --git a/apps/studio/lib/ai/tools/studio-tools.ts b/apps/studio/lib/ai/tools/studio-tools.tsindex 41ee0e028dc..016ebf62349 100644--- a/apps/studio/lib/ai/tools/studio-tools.ts+++ b/apps/studio/lib/ai/tools/studio-tools.ts@@ -1,45 +1,19 @@-import { tool } from 'ai'-import { createTool as createDeployEdgeFunctionTool } from './deploy-edge-function.server'import { createTool as createExecuteSqlTool } from './execute-sql.server'import { createTool as createLoadKnowledgeTool } from './load-knowledge.server'import { createTool as createRenameChatTool } from './rename-chat.server'import type { ToolContext } from './types'+import { createTool as createUpdateOptInLevelTool } from './update-opt-in-level.server'import type { AiOptInLevel } from '@/hooks/misc/useOrgOptedIntoAi'-import { isOptInLevelAtLeast } from '@/lib/ai/opt-in-level'-import { updateOptInLevelInputSchema } from '@/lib/ai/tool-filter'-export const getStudioTools = (ctx: ToolContext = {}) => ({+export const getStudioTools = (ctx: ToolContext) => ({execute_sql: createExecuteSqlTool(ctx),deploy_edge_function: createDeployEdgeFunctionTool(ctx),rename_chat: createRenameChatTool(ctx),load_knowledge: createLoadKnowledgeTool(ctx),})-// Rewritten at parse time so the stored input is what the card shows. A level the org already-// has is a review request, and `levelWhenAsked` outlives the user changing the setting.-export const createUpdateOptInLevelInputSchema = (aiOptInLevel: AiOptInLevel) =>- updateOptInLevelInputSchema.transform(({ requiredLevel }) => ({- levelWhenAsked: aiOptInLevel,- ...(requiredLevel && !isOptInLevelAtLeast(aiOptInLevel, requiredLevel) && { requiredLevel }),- }))-/** Registered at every level so an approval that lands on the top level can still execute. */export const getOptInTools = ({ aiOptInLevel }: { aiOptInLevel: AiOptInLevel }) => ({- update_opt_in_level: tool({- description:- 'Asks the user to review or change the organization-wide AI opt-in level. Call it with `requiredLevel` when a tool needs a higher level than the organization has or you need to read query results. Call it without `requiredLevel` whenever the user asks to change the setting, including lowering it. Do not call it again in the same chat after the user skipped it unless they ask.',- inputSchema: createUpdateOptInLevelInputSchema(aiOptInLevel),- needsApproval: true,- execute: async ({ requiredLevel }) => {- const sufficient = !requiredLevel || isOptInLevelAtLeast(aiOptInLevel, requiredLevel)- return {- levelAfterReview: aiOptInLevel,- ...(requiredLevel && { sufficient }),- ...(!sufficient && {- status: `The opt-in level is still below the requested ${requiredLevel}. If you still need that access, call update_opt_in_level again.`,- }),- }- },- }),+ update_opt_in_level: createUpdateOptInLevelTool({ aiOptInLevel }),})diff --git a/apps/studio/lib/ai/tools/support-tools.ts b/apps/studio/lib/ai/tools/support-tools.tsindex 1c8dabe6b3b..19ab0d8c73e 100644--- a/apps/studio/lib/ai/tools/support-tools.ts+++ b/apps/studio/lib/ai/tools/support-tools.ts@@ -2,7 +2,7 @@ import { createTool as createEscalateToHumanTool } from './escalate-to-human.serimport { createTool as createResolveSupportConversationTool } from './resolve-support-conversation.server'import type { ToolContext } from './types'-export const getSupportLifecycleTools = (ctx: ToolContext = {}) => ({+export const getSupportLifecycleTools = (ctx: ToolContext) => ({escalate_to_human: createEscalateToHumanTool(ctx),resolve_support_conversation: createResolveSupportConversationTool(ctx),})diff --git a/apps/studio/lib/ai/tools/tool-from-contract.test.ts b/apps/studio/lib/ai/tools/tool-from-contract.test.tsnew file mode 100644index 00000000000..f2559e546b4--- /dev/null+++ b/apps/studio/lib/ai/tools/tool-from-contract.test.ts@@ -0,0 +1,54 @@+import { describe, expect, it } from 'vitest'+import { z } from 'zod'++import { toolFromContract } from './tool-from-contract'+import type { ToolContract } from './types'++describe('toolFromContract createInputSchema', () => {+ const base = {+ name: 'probe',+ description: 'A probe',+ outputSchema: z.object({}),+ minOptInLevel: null,+ needsApproval: false,+ toModelOutput: () => ({}),+ } as const++ it('parses with createInputSchema when the contract has one', () => {+ const perRequest = z.object({}).transform(() => ({ level: 'schema_and_log' }))+ const probe = {+ ...base,+ inputSchema: z.object({ level: z.string() }),+ createInputSchema: () => perRequest,+ } satisfies ToolContract<{ level: string }, object>++ expect(toolFromContract(probe, { aiOptInLevel: 'schema' }, async () => ({})).inputSchema).toBe(+ perRequest+ )+ })++ it('passes the context to createInputSchema', () => {+ let received: unknown+ const probe = {+ ...base,+ inputSchema: z.object({}),+ createInputSchema: (ctx) => {+ received = ctx+ return z.object({})+ },+ } satisfies ToolContract<object, object>++ const ctx = { projectRef: 'ref', aiOptInLevel: 'schema' } as const+ toolFromContract(probe, ctx, async () => ({}))+ expect(received).toBe(ctx)+ })++ it('parses with inputSchema otherwise', () => {+ const inputSchema = z.object({})+ const probe = { ...base, inputSchema } satisfies ToolContract<object, object>++ expect(toolFromContract(probe, { aiOptInLevel: 'schema' }, async () => ({})).inputSchema).toBe(+ inputSchema+ )+ })+})diff --git a/apps/studio/lib/ai/tools/tool-from-contract.ts b/apps/studio/lib/ai/tools/tool-from-contract.tsindex 104bb1a31f2..5d7038aec16 100644--- a/apps/studio/lib/ai/tools/tool-from-contract.ts+++ b/apps/studio/lib/ai/tools/tool-from-contract.ts@@ -9,7 +9,7 @@ import type { ToolContext, ToolContract } from './types'*/export function toolFromContract<TInput, TOutput>(contract: ToolContract<TInput, TOutput>,- { aiOptInLevel = 'schema' }: ToolContext,+ ctx: ToolContext,execute: (input: TInput, options: ToolExecutionOptions<unknown>) => PromiseLike<TOutput> | TOutput): Tool<TInput, TOutput> {const { toModelOutput } = contract@@ -19,12 +19,12 @@ export function toolFromContract<TInput, TOutput>(// annotation checks the concrete types instead.return {description: contract.description,- inputSchema: contract.inputSchema,+ inputSchema: contract.createInputSchema?.(ctx) ?? contract.inputSchema,needsApproval: contract.needsApproval,execute,toModelOutput: ({ output }: { output: TOutput }) => ({type: 'json' as const,- value: toModelOutput(output, aiOptInLevel),+ value: toModelOutput(output, ctx.aiOptInLevel),}),} as unknown as Tool<TInput, TOutput>}diff --git a/apps/studio/lib/ai/tools/types.ts b/apps/studio/lib/ai/tools/types.tsindex fc897faf9d6..1afb6b2e9e9 100644--- a/apps/studio/lib/ai/tools/types.ts+++ b/apps/studio/lib/ai/tools/types.ts@@ -7,7 +7,7 @@ export type ToolContext = {projectRef?: stringconnectionString?: stringauthorization?: string- aiOptInLevel?: AiOptInLevel+ aiOptInLevel: AiOptInLevel/** Public dashboard URL, used by tools that call Studio's own API routes. */baseUrl?: string}@@ -19,6 +19,12 @@ export type ToolContract<TInput = unknown, TOutput = unknown> = {name: stringdescription: stringinputSchema: z.ZodType<TInput, z.ZodTypeDef, unknown>+ /**+ * For tools whose stored input depends on the request, since the SDK stores the parsed value.+ * It must produce the same type `inputSchema` describes. `inputSchema` stays the shape stored+ * on the message, which is what history and the client parse. Omit it to parse with `inputSchema`.+ */+ createInputSchema?(ctx: ToolContext): z.ZodType<TInput, z.ZodTypeDef, unknown>outputSchema: z.ZodType<TOutput, z.ZodTypeDef, unknown>/** Lowest opt-in level at which the tool runs. `null` means always available. */minOptInLevel: AiOptInLevel | nulldiff --git a/apps/studio/lib/ai/tools/update-opt-in-level.server.ts b/apps/studio/lib/ai/tools/update-opt-in-level.server.tsnew file mode 100644index 00000000000..533f3bed444--- /dev/null+++ b/apps/studio/lib/ai/tools/update-opt-in-level.server.ts@@ -0,0 +1,17 @@+import { toolFromContract } from './tool-from-contract'+import type { ToolFactory } from './types'+import { contract } from './update-opt-in-level'+import { isOptInLevelAtLeast } from '@/lib/ai/opt-in-level'++export const createTool: ToolFactory<typeof contract> = (ctx) =>+ toolFromContract(contract, ctx, async ({ requiredLevel }) => {+ const { aiOptInLevel } = ctx+ const sufficient = !requiredLevel || isOptInLevelAtLeast(aiOptInLevel, requiredLevel)+ return {+ levelAfterReview: aiOptInLevel,+ ...(requiredLevel && { sufficient }),+ ...(!sufficient && {+ status: `The opt-in level is still below the requested ${requiredLevel}. If you still need that access, call update_opt_in_level again.`,+ }),+ }+ })diff --git a/apps/studio/lib/ai/tools/update-opt-in-level.test.ts b/apps/studio/lib/ai/tools/update-opt-in-level.test.tsnew file mode 100644index 00000000000..b264fb5d14a--- /dev/null+++ b/apps/studio/lib/ai/tools/update-opt-in-level.test.ts@@ -0,0 +1,72 @@+import type { ToolUIPart } from 'ai'+import { describe, expect, it } from 'vitest'+import { z } from 'zod'++import { sanitizeContractPart } from './registry'+import { contract } from './update-opt-in-level'+import { createTool } from './update-opt-in-level.server'++const executeOptions = { toolCallId: 'test', messages: [], context: {} }++describe('update_opt_in_level contract', () => {+ it.each([+ ['schema_and_log_and_data', 'schema', undefined],+ ['schema', 'schema', undefined],+ ['schema', 'schema_and_log', 'schema_and_log'],+ ['schema', undefined, undefined],+ ] as const)('at level %s, requested %s is stored as %s', (level, requested, stored) => {+ const parsed = contract+ .createInputSchema({ aiOptInLevel: level })+ .parse({ requiredLevel: requested })+ expect(parsed).toEqual({ levelWhenAsked: level, requiredLevel: stored })+ })++ it('builds the tool with the per-request input schema', () => {+ const tool = createTool({ aiOptInLevel: 'schema_and_log_and_data' })+ expect(tool.inputSchema).not.toBe(contract.inputSchema)+ const parsed = (tool.inputSchema as z.ZodType).parse({ requiredLevel: 'schema' })+ expect(parsed).toEqual({ levelWhenAsked: 'schema_and_log_and_data' })+ })++ it.each([+ ['schema', 'schema_and_log_and_data', false],+ ['schema_and_log_and_data', 'schema', true],+ ['schema', 'schema', true],+ ['schema', undefined, undefined],+ ] as const)(+ 'level %s after approval, stored required %s gives sufficient %s',+ async (level, requiredLevel, sufficient) => {+ const tool = createTool({ aiOptInLevel: level })+ if (!tool.execute) throw new Error('execute is undefined')++ const result = await tool.execute({ levelWhenAsked: 'schema', requiredLevel }, executeOptions)++ expect(tool.needsApproval).toBe(true)+ expect(result).toMatchObject({+ levelAfterReview: level,+ ...(sufficient !== undefined && { sufficient }),+ })+ if (sufficient === false) expect(result).toHaveProperty('status')+ else expect(result).not.toHaveProperty('status')+ }+ )++ describe('sanitizeContractPart', () => {+ const part = {+ type: 'tool-update_opt_in_level',+ toolCallId: 'call-1',+ state: 'output-available',+ input: {},+ output: { levelAfterReview: 'schema_and_log', sufficient: true },+ } as ToolUIPart++ it('round-trips a valid output', () => {+ expect(sanitizeContractPart(part, 'schema')?.output).toEqual(part.output)+ })++ it('drops an invalid output', () => {+ const invalid = { ...part, output: { levelAfterReview: 'everything' } } as ToolUIPart+ expect(sanitizeContractPart(invalid, 'schema')?.output).toBe(undefined)+ })+ })+})diff --git a/apps/studio/lib/ai/tools/update-opt-in-level.ts b/apps/studio/lib/ai/tools/update-opt-in-level.tsnew file mode 100644index 00000000000..cd641ee4043--- /dev/null+++ b/apps/studio/lib/ai/tools/update-opt-in-level.ts@@ -0,0 +1,48 @@+import { z } from 'zod'++import type { ToolContract } from './types'+import { isOptInLevelAtLeast, optInLevelSchema } from '@/lib/ai/opt-in-level'++/** What the model sends. */+const modelInputSchema = z.object({+ requiredLevel: z+ .enum(['schema', 'schema_and_log', 'schema_and_log_and_data'])+ .optional()+ .describe(+ 'The lowest opt-in level that unlocks what you need: `schema` for table and column metadata, `schema_and_log` for logs, `schema_and_log_and_data` for query results. Omit it when the user just wants to review or change the setting, for example to lower it.'+ ),+})++/**+ * Input as stored on the message. `levelWhenAsked` is the org's level when the call was made.+ * Optional because older chats may lack it.+ */+const storedInputSchema = modelInputSchema.extend({+ levelWhenAsked: optInLevelSchema.optional(),+})++type StoredInput = z.infer<typeof storedInputSchema>++const outputSchema = z.object({+ levelAfterReview: optInLevelSchema,+ sufficient: z.boolean().optional(),+ status: z.string().optional(),+})++type Output = z.infer<typeof outputSchema>++export const contract = {+ name: 'update_opt_in_level',+ description:+ 'Asks the user to review or change the organization-wide AI opt-in level. Call it with `requiredLevel` when a tool needs a higher level than the organization has or you need to read query results. Call it without `requiredLevel` whenever the user asks to change the setting, including lowering it. Do not call it again in the same chat after the user skipped it unless they ask.',+ inputSchema: storedInputSchema,+ createInputSchema: ({ aiOptInLevel }) =>+ modelInputSchema.transform(({ requiredLevel }) => ({+ levelWhenAsked: aiOptInLevel,+ ...(requiredLevel && !isOptInLevelAtLeast(aiOptInLevel, requiredLevel) && { requiredLevel }),+ })),+ outputSchema,+ minOptInLevel: null,+ needsApproval: true,+ toModelOutput: (output) => output,+} as const satisfies ToolContract<StoredInput, Output>diff --git a/apps/studio/pages/api/ai/sql/policy.ts b/apps/studio/pages/api/ai/sql/policy.tsindex 19bc5f20f19..92146983264 100644--- a/apps/studio/pages/api/ai/sql/policy.ts+++ b/apps/studio/pages/api/ai/sql/policy.ts@@ -11,6 +11,7 @@ import { isExplorerEnabled } from '@/lib/ai/is-explorer-enabled'import { getModel } from '@/lib/ai/model'import { DEFAULT_COMPLETION_MODEL } from '@/lib/ai/model.utils'import { RLS_PROMPT } from '@/lib/ai/prompts'+import { withoutApprovalTools } from '@/lib/ai/tool-filter'import { getTools } from '@/lib/ai/tools'import { apiWrapper } from '@/lib/api/apiWrapper'import { trustedUserEmail } from '@/lib/server/configcat'@@ -112,7 +113,7 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse, clai// Fires when the response finishes or the connection drops.res.on('close', () => toolsAbortController.abort())try {- const tools = await getTools({+ const allTools = await getTools({projectRef,connectionString,authorization,@@ -122,7 +123,7 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse, claisignal: toolsAbortController.signal,})// One-shot generateText can't answer an approval request- delete tools.update_opt_in_level+ const tools = withoutApprovalTools(allTools)const { output } = await generateText({...modelParams,