mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 23:49:03 +00:00
## Problem `update_opt_in_level` is the last local assistant tool not on the tool contract pattern. Its input schema is built for each request: a transform captures the org's opt-in level, stamps `levelWhenAsked`, and drops `requiredLevel` when the org already has it. The SDK stores that parsed value, and the approval card reads it before the tool runs. A contract has one static `inputSchema`, so this didn't fit. Separately, `pages/api/ai/sql/policy.ts` passes every assistant tool to a one-shot `generateText` and only removes `update_opt_in_level`. Any tool with `needsApproval` (`execute_sql`, `deploy_edge_function`, the notebook mutation and run tools) stops the step loop with no result, so `output` is never set and the route returns a 500. ## Solution - `ToolContract` gains an optional `createInputSchema(ctx)`. `toolFromContract` parses with it when present. `inputSchema` stays the shape stored on the message, which is what history replay and the client parse. - `update_opt_in_level` moves to `update-opt-in-level.ts` and `update-opt-in-level.server.ts`: - The per-request transform becomes `createInputSchema`; the stored shape is `inputSchema`. - The output schema moves out of `OptInRequest.tsx` into the contract; `toModelOutput` returns it unchanged, since it only contains level names. - `minOptInLevel: null` and `needsApproval: true`, as before. It uses plain `toolFromContract`, since it promotes no SQL. - Registered in `TOOL_CONTRACTS`, removed from `TOOL_CATEGORY_MAP`. `getOptInTools` still registers it on platform at every level. - `Message.Parts.tsx`, `OptInRequest.tsx`, and `evals/opt-in-replay.ts` use the contract's schemas; the old schemas in `tool-filter.ts` and `studio-tools.ts` are removed. - `ToolContext.aiOptInLevel` is now required. The `'schema'` defaults in `toolFromContract`, `execute-sql.server.ts`, and `run-notebook.server.ts` are gone, and every caller passes the real level. The eval mocks pin the mocked studio tools to `'schema'` so eval results don't shift. - New `withoutApprovalTools(tools)` in `tool-filter.ts` drops any tool whose `needsApproval` is set (including a function, and privacy-stubbed tools). `policy.ts` uses it instead of deleting one tool. ## Behavior changes - Replayed `update_opt_in_level` outputs are validated against the output schema and dropped if invalid (previously unchecked). - The policy generator no longer offers approval-gated tools to the model. ## Testing - New `update-opt-in-level.test.ts` (transform, per-request schema, execute at a new level, replay) and `tool-from-contract.test.ts` (`createInputSchema` used when present and receives the context). - `withoutApprovalTools` tests in `tool-filter.test.ts`. - `pnpm exec vitest run lib/ai components/ui/AIAssistantPanel evals` passes in `apps/studio`.
73 lines
2.7 KiB
TypeScript
73 lines
2.7 KiB
TypeScript
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)
|
|
})
|
|
})
|
|
})
|