mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +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`.
74 lines
2.7 KiB
TypeScript
74 lines
2.7 KiB
TypeScript
import type { UIMessage } from 'ai'
|
|
|
|
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 { contract as updateOptInLevelContract } from '@/lib/ai/tools/update-opt-in-level'
|
|
|
|
const TOOL_PART_TYPE = 'tool-update_opt_in_level'
|
|
|
|
/**
|
|
* Answers the pending `update_opt_in_level` approval the way the card would.
|
|
* Returns the history to resume with and the level the next request runs at, or null
|
|
* when the model never asked.
|
|
*
|
|
* accept → approved, level becomes the requested one
|
|
* skip → denied with the Skip reason, level unchanged
|
|
* { chooses: L } → approved after the user saved L, whatever was requested
|
|
*/
|
|
export function applyOptInDecision(
|
|
message: UIMessage,
|
|
decision: OptInDecision,
|
|
currentLevel: AiOptInLevel
|
|
): {
|
|
message: UIMessage
|
|
level: AiOptInLevel
|
|
deniedToolCalls: Array<{ toolName: string; input: unknown }>
|
|
} | null {
|
|
let level = currentLevel
|
|
let isResolved = false
|
|
const deniedToolCalls: Array<{ toolName: string; input: unknown }> = []
|
|
|
|
const parts = message.parts.map((part): UIMessage['parts'][number] => {
|
|
if (part.type !== TOOL_PART_TYPE || part.state !== 'approval-requested' || !part.approval) {
|
|
return part
|
|
}
|
|
|
|
isResolved = true
|
|
const requestedLevel = updateOptInLevelContract.inputSchema.safeParse(part.input).data
|
|
?.requiredLevel
|
|
const isApproved = decision !== 'skip'
|
|
if (!isApproved) deniedToolCalls.push({ toolName: 'update_opt_in_level', input: part.input })
|
|
if (decision === 'accept') level = requestedLevel ?? currentLevel
|
|
if (typeof decision === 'object') level = decision.chooses
|
|
|
|
return {
|
|
type: TOOL_PART_TYPE,
|
|
toolCallId: part.toolCallId,
|
|
state: 'approval-responded' as const,
|
|
input: part.input,
|
|
approval: {
|
|
id: part.approval.id,
|
|
approved: isApproved,
|
|
...(!isApproved && { reason: USER_SKIPPED_TOOL_REASON }),
|
|
},
|
|
}
|
|
})
|
|
|
|
return isResolved ? { message: { ...message, parts }, level, deniedToolCalls } : null
|
|
}
|
|
|
|
function describeDecision(decision: OptInDecision) {
|
|
if (decision === 'accept') return 'accepted the request'
|
|
if (decision === 'skip') return 'skipped the request'
|
|
return `saved the ${decision.chooses} level`
|
|
}
|
|
|
|
/** Request 1 plus the simulated answer, as the context a judge sees for request 2. */
|
|
export function describePriorTurn(first: Transcript, decision: OptInDecision) {
|
|
return [first.lastAssistantTurnWithToolInputs, `[user ${describeDecision(decision)}]`]
|
|
.filter((part) => part !== null)
|
|
.join('\n')
|
|
}
|