Files
supabase/apps/studio/lib/ai/README.md
Alaister YoungandAlaister Young c5fca0a0d7 [FE-5114] (part 2) – refactor(studio): migrate AI API routes (#54029)
## Problem

Studio's AI API routes under `routes/api/ai/**` still run legacy Next
Pages Router handlers through `compat/next/api.ts`. Part 2 of 7 for
[FE-5114](https://linear.app/supabase/issue/FE-5114/rewrite-api-routes-as-native-tanstack-start-handlers-and-remove-the)
migrates the active AI routes to native TanStack Start (`Request` →
`Response`) handlers and drops 3 unused AI endpoints.

## Solution

**Changed:**

- Migrated 8 active AI routes (`ai/code/complete`,
`ai/feedback/{classify,rate}`, and
`ai/sql/{check-api-key,cron-v2,filter-v1,generate-v4,title-v2}`) to
`createApiHandlers` and `parseJsonBody`
- Replaced Node `pipeUIMessageStreamToResponse` in
`ai/sql/generate-v4.ts` with native `createUIMessageStreamResponse`,
preserving the `x-braintrust-span-id` header
- Replaced `req.on`/`res.on` in `ai/sql/generate-v4.ts` with
`AbortSignal.any([request.signal, done.signal])` and a `TransformStream`
whose `flush`/`cancel` tear down the MCP connection opened in `getTools`
on stream completion, cancellation, or setup error
- Removed the custom malformed-JSON handling in `ai/code/complete.ts` in
favor of the wrapper's `400`
- Updated `lib/api/generate-v4.test.ts` to assert against the real
native SSE `Response` stream instead of mocking
`pipeUIMessageStreamToResponse`, with a shared `mockOpenStream()`
helper, a stream-cancellation abort test, and a Braintrust tracing test
asserting `x-braintrust-span-id` matches the exported root span
- Updated `lib/ai/README.md` to drop references to removed endpoints

**Removed:**

- Removed 3 unused AI routes (`routes/api/ai/onboarding/design.ts`,
`routes/api/ai/sql/parse-client-code.ts`, and
`routes/api/ai/sql/policy.ts`), dropped them from
`lib/hosted-api-allowlist.ts`, and regenerated `routeTree.gen.ts`
- Removed the now-unused `withoutApprovalTools` helper from
`lib/ai/tool-filter.ts` and its test from `lib/ai/tool-filter.test.ts`

**Behavior changes vs master:**

- `ai/code/complete` returns the wrapper's `400` JSON `{ error: {
message: 'Invalid JSON' } }` for malformed bodies
- Unused `ai/onboarding/design`, `ai/sql/parse-client-code`, and
`ai/sql/policy` endpoints have been removed

## Review instructions

1. Run `pnpm --filter studio exec vitest run lib/api/generate-v4.test.ts
lib/api/rate.test.ts lib/ai/tool-filter.test.ts`.
2. Send a prompt in the AI Assistant panel, verify streaming output, and
cancel mid-stream.
3. Trigger SQL title, cron, and filter generation as well as AI code
completion.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-12-01 21:36:38 +08:00

24 KiB

Supabase Assistant code guide

Start here when working on the Assistant in Studio. This guide maps the implementation for contributors and coding agents. Follow Studio's agent instructions and load the skills that apply to your change.

Request flow

flowchart TD
  UI[AssistantChat] --> State[ai-assistant-state: chat transport]
  State --> API[POST /api/ai/sql/generate-v4]
  API --> Access[Resolve organization access and model]
  Access --> Tools[getTools]
  Tools --> Response[generateAssistantResponse]
  Response --> Stream[Stream messages and tool calls to the UI]
  Stream --> Parts[Message.Parts: render text and tool cards]
  Parts --> Approval[User approves a tool when required]
  Approval --> State

The sidebar provider mounts AIAssistant. AssistantChat manages the active conversation UI and supplies project and organization context to ai-assistant-state.

The state module owns the AI SDK chat instances, request transport, chat persistence, and approval continuation. Its transport sends messages to generate-v4, which validates and authenticates the request, resolves access and the model, builds tools, and streams the response.

generateAssistantResponse combines prompts, context, prepared conversation history, and tools for the model. Message.Parts dispatches streamed message parts to their UI renderers. Approval responses travel back through the chat transport so the server can continue tool execution.

Rules to keep

  • Enforce access in code. Resolve organization/project access on the server and keep the restrictive fallback for an unverified pairing. Prompts and UI visibility are not authorization checks.
  • Keep execution approval separate from data sharing. Full data opt-in does not approve AI-generated SQL execution. Promote untrusted SQL only after explicit user approval.
  • Protect results on every model path. Apply output filtering to both live tool results and replayed history. Raising opt-in must not expose SQL results collected under a lower level.
  • Keep the tool allowlist. The registry is the allowlist: register a tool with a contract that declares its minimum opt-in level. Remote MCP tools must have a remote contract to pass; the remote server's read-only setting does not replace it.
  • Keep request context out of the shared system prompt. Put project, chat, clock, and logs context in assistant-context.ts to preserve provider caching.
  • Keep tool contracts safe to import in the browser. Shared contracts contain schemas and pure output transformations; database calls and other server execution belong in the server implementation.

Where to make changes

Chat and rendering

Change Start here
Panel layout, chat selection, and header AIAssistant.tsx, AIAssistantHeader.tsx
Composer, attachments, and sending messages AssistantChat.tsx, AssistantChatForm.tsx
Chat state, persistence, transport, and selected model ai-assistant-state.tsx
Message rendering and tool-specific cards Message.Parts.tsx, Message.Parts.utils.ts
SQL results and charts AssistantQueryCell.tsx, AssistantQueryCell.utils.ts
Approval UI Confirm.tsx, Confirm.utils.ts

Model requests and context

Change Start here
Chat endpoint and request validation generate-v4.ts
Model streaming and response orchestration generate-assistant-response.ts
System instructions and topic knowledge prompts.ts for tool-agnostic rules; tool rules in each contract's promptGuidance, assembled by tools-prompt.ts; the full prompt is built by buildSystemPrompt in generate-assistant-response.utils.ts; load_knowledge topics in knowledge-topics.ts
Project, chat, clock, and logs context assistant-context.ts, assistant-message-metadata.ts
History preparation and output sanitization generate-assistant-response.utils.ts, tool-sanitizer.ts, registry.ts
Model catalog, defaults, and provider configuration model.utils.ts, model.ts, bedrock.ts; entitlement and throttling decisions in generate-v4.ts
Tracing and feedback braintrust-logger.ts, feedback API, rate-message-mutation.ts

Keep per-request context in assistant-context.ts. The shared system prompt is deliberately static for provider caching: it varies only with the tool set, through its generated ## Tools section. That costs no extra cache misses, because providers cache tool definitions before the system prompt, so a change to the tool set already invalidates the cached system prompt (Anthropic, Bedrock, OpenAI).

Tools

Change Start here
Tool assembly and platform/self-hosted selection tools/index.ts
Opt-in filtering and blocked-tool responses tool-filter.ts
Remote MCP connection and tool selection supabase-mcp.ts, mcp-tools.ts
Contract-based tools (see Tool contracts) registry.ts lists them in TOOL_CONTRACTS (and remote MCP tools in REMOTE_TOOL_CONTRACTS), and registry.server.ts maps each to its factory in TOOL_FACTORIES; types.ts, tool-from-contract.ts

Every tool is a contract tool, and registry.ts is the source of truth for which tools exist: TOOL_CONTRACTS for tools Studio implements and REMOTE_TOOL_CONTRACTS for tools the remote MCP server implements. A tool not in either is not on the allowlist.

For a new or changed tool:

  1. Write a contract for the tool.
  2. Register it in the registry, which getTools builds tools from and which is the allowlist (add the factory to TOOL_FACTORIES for a local contract). A contract, local or remote, declares minimum access as minOptInLevel and where it exists as availability.
  3. Decide whether execution needs user approval and what output may reach the model, including on history replay. Read the SQL safety skill before changing SQL execution.
  4. Update Message.Parts.tsx and its helpers if the tool needs a UI renderer.
  5. Update relevant tests, mock tools, and evaluation cases. See local development and validation.

Tool contracts

A contract declares a tool once: name, description, input and output schemas, availability, minOptInLevel, needsApproval, optional promptGuidance, and toModelOutput, which decides what the model may see of the output. availability says which environments (platform, self-hosted) the tool exists in and which request conditions it also needs (requires, such as the Explorer flag or support mode). registry.server.ts builds only the available tools for a request, so getTools has no per-tool branches.

A tool factory that queries the project's database runs the pg-meta query through runPgMetaQuery in tool-sql.server.ts, not executeSql directly. Off platform, executeSql without a fetcher posts to API_URL, which is the relative /api unless NEXT_PUBLIC_SITE_URL is set and so can't resolve on the server; the helper passes executeQuery from lib/api/self-hosted/query.ts as the fetcher there. It accepts only the { sql, zod } a pg-meta builder returns, so the SQL is a SafeSqlFragment and the rows are validated.

promptGuidance holds the rules for using the tool that its description doesn't cover; omit it when the description is enough. buildToolsPrompt generates the system prompt's ## Tools section from the tools the model is given: it lists each one that has a contract, then adds a section with the guidance of each tool that has any. Because it reads the tool set itself, the prompt never mentions a tool the model doesn't have, including remote tools that failed to load. Keep rules that name a tool in its promptGuidance, not in prompts.ts, and name only tools that are available whenever that tool is; a test checks the tool set getTools builds for every combination of environment and requirements. The prompt is cached, so the section may depend only on the tool set, never on per-request data. The registry also applies toModelOutput to persisted outputs when it replays history, so it must be safe to run on its own result.

Each contract tool is a pair of files named after the tool, plus an optional output helper:

  • <tool-name>.ts exports contract. The browser imports it, so it contains only zod schemas and pure functions, with no data-layer or other server imports.
  • <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 and update-notebook.server.ts show the split with approval. 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 shows an output schema that also accepts an older persisted shape.

Remote contracts

A tool the remote MCP server implements has a RemoteToolContract: only name, availability, minOptInLevel, needsApproval, and promptGuidance. The server owns the description, input schema, execution, and output, so there is no .server.ts file and its parts stay dynamic-tool, outside StudioUIMessage. When a local contract has the same name, give them disjoint environments and the same minOptInLevel and needsApproval; the remote file is named <tool-name>.remote.ts, as in list-tables.remote.ts next to the self-hosted list-tables.ts. Register it in REMOTE_TOOL_CONTRACTS, which is also what lets the tool through; mcp-tools.ts applies needsApproval, since MCP tool definitions can't declare it. list-extensions.ts is an example.

Legacy contracts

A tool that no longer exists can still have outputs in saved chats, such as the self-hosted fallback tools (getSchemaTables, getRlsKnowledge, getFunctions, getEdgeFunctionKnowledge) that contracts replaced. A LegacyToolContract keeps those outputs safe: only name, outputSchema, toModelOutput, and outputOnInvalid, so replayed output is validated and gated by opt-in level like any contract tool's. legacy-tools.ts lists them in LEGACY_TOOL_CONTRACTS. Only history replay reads them, through sanitizeContractPart, and nothing registers them as tools. A legacy contract may replay less than was saved: getRlsKnowledge replays only the saved policies, dropping the stale instructions stored with them, and getEdgeFunctionKnowledge replays a pointer to load_knowledge. A legacy name must never match a registry tool. When you remove or rename a tool whose outputs exist in saved chats, add a legacy contract for the old name.

Legacy contracts

A tool that no longer exists can still have outputs in saved chats, such as the self-hosted fallback tools that contracts replace. A LegacyToolContract keeps those outputs safe: only name, outputSchema, toModelOutput, and outputOnInvalid, so replayed output is validated and gated by opt-in level like any contract tool's. legacy-tools.ts lists them in LEGACY_TOOL_CONTRACTS. Only history replay reads them, through sanitizeContractPart, and nothing registers them as tools. When you remove or rename a tool whose outputs exist in saved chats, add a legacy contract for the old name.

Access and data sharing

These checks have different responsibilities:

Responsibility Implementation
Resolve the saved organization opt-in tags for the UI useOrgOptedIntoAi.ts
Resolve server-side access for an organization/project pair ai-details.ts
Check permission to change organization settings useCheckPermissions.ts, used by OptInRequest.tsx and AIOptInModal.tsx
Save organization opt-in settings useAIOptInForm.ts, DataPrivacyForm.tsx
Gate tool availability by opt-in level tool-filter.ts
Approve SQL execution and call the database execute-sql.ts (approval) and execute-sql.server.ts (execution), using execute-sql-mutation.ts
Control which SQL results reach the model toModelOutput in execute-sql-output.ts, applied to live results by the SDK and to history by registry.ts

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 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 the contract tools whose availability includes self-hosted, such as list_tables (a local contract that mirrors the remote MCP tool), list_policies, and list_functions, instead of the remote MCP tools. Check getTools and generate-v4 when changing either environment.

The chat Assistant shares code with other Studio AI features, but those have separate endpoints:

  • routes/api/ai/sql/ contains SQL title, filter, and cron helpers alongside the chat endpoint.
  • routes/api/ai/code/complete.ts handles code completions.
  • data/ai/ contains client queries and mutations for AI helpers and feedback. The chat transport itself lives in ai-assistant-state.tsx.

Local development and validation

Use the Studio setup instructions for the application and backend configuration. From the repository root, pnpm dev:studio starts Studio on port 8082 by default. The chat endpoint currently selects the OpenAI provider, which requires a server-side OPENAI_API_KEY; see model.ts. A self-hosted session exercises the self-hosted contract tools, so use a platform development environment when working on platform-only tools or organization settings.

Tests are colocated with the implementation under lib/ai/, components/ui/AIAssistantPanel/, and state/. For example, from apps/studio:

pnpm exec vitest run lib/ai/tool-filter.test.ts lib/ai/tools/index.availability.test.ts lib/ai/tools/registry.test.ts

Use the Assistant evaluation guide for behavioral datasets, local evaluation commands, CI labels, and scoring. dataset.ts contains scenarios and mock-tools.ts supplies tool behavior for evaluations. The browser test is assistant.spec.ts; read the E2E setup guide before running it.

Update this map when moving entry points or changing the request/tool lifecycle.