mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 09:55:06 +03:00
## Problem
- Assistant responses were capped at 120 seconds and 10 steps, which is
too short for longer reasoning or multi-step tool work.
- When the hosting platform ended a request at that limit, the
connection just dropped. The user got no explanation, and "Thinking…"
and tool rows kept spinning.
- Studio's own tools ignored the request's abort signal, so a stop,
disconnect or deadline couldn't cancel their in-flight requests.
- Aborted responses never closed their Braintrust span. Under TanStack
Start, the remote MCP client was only released on `res.on('close')`,
which the adapter never emits.
## Solution
Uses AI SDK options instead of custom stream handling:
- `maxDuration` goes to 300s and the step limit to 20. `streamText({
timeout: { totalMs } })` stops the response at 270s, leaving time to
finish the stream before the platform cutoff.
- `toUIMessageStream({ messageMetadata })` marks an aborted response
`timedOut: true`. `Chat` ignores `abort` chunks, so the client reads
this flag instead and shows a timeout alert with Retry. The flag is
saved with the message, so the alert survives a reload.
- `toUIMessageStream({ onEnd })` aborts the request whenever the stream
ends, releasing the MCP client on both runtimes. `streamText({ onAbort
})` ends the Braintrust span.
- Studio tools pass the SDK's `abortSignal` to their fetches. MCP tools
already did.
- Reasoning and server-tool rows that never finished show "Response
interrupted" instead of a spinner or "Ran X ✓".
There's no per-tool timeout. Approved SQL and migrations can
legitimately run longer, and aborting the HTTP request doesn't stop the
query in Postgres.
## Review instructions
1. Run the unit tests: `cd apps/studio && pnpm vitest run
lib/api/generate-v4.test.ts lib/ai components/ui/AIAssistantPanel`
2. To see a timeout without waiting 4.5 minutes, temporarily set
`ASSISTANT_TIMEOUT_MS` in `apps/studio/lib/ai/assistant-timeout.ts` to
`15_000` and run `pnpm dev:studio`.
3. Ask the Assistant something that needs several tool calls or long
reasoning, for example "Audit my schema for missing indexes and RLS
gaps, then write the fixes."
4. After 15 seconds, check that:
- the response stops and a "Assistant response timed out" alert appears
with Retry
- any in-progress reasoning or tool row shows "Response interrupted"
instead of spinning
- Retry starts a new response
- reloading the page still shows the alert on that chat
5. Stop a response with the Stop button before the deadline. It should
stop without the timeout alert.
6. With the default 270s, confirm that a normal response completes as
before.
## Checklist
Check all before review:
- [ ] 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 references
[WORD_LIST](https://github.com/supabase/supabase/blob/master/apps/docs/WORD_LIST.md)
and the docs
[CONTRIBUTING](https://github.com/supabase/supabase/blob/master/apps/docs/CONTRIBUTING.md)
guide
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Improvements**
* AI assistant responses can now run for up to five minutes, supporting
longer requests.
* When a response times out, the assistant displays a message suggesting
you retry or ask for a smaller change.
* Incomplete responses now show a “Response interrupted” notice, and
loading indicators stop when generation ends.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
132 lines
4.9 KiB
TypeScript
132 lines
4.9 KiB
TypeScript
import { acceptUntrustedSql, untrustedSql } from '@supabase/pg-meta'
|
|
import { tool } from 'ai'
|
|
import { z } from 'zod'
|
|
|
|
import { deployEdgeFunction } from '@/data/edge-functions/edge-functions-deploy-mutation'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import type { AiOptInLevel } from '@/hooks/misc/useOrgOptedIntoAi'
|
|
import {
|
|
EDGE_FUNCTION_PROMPT,
|
|
LOGS_PROMPT,
|
|
PG_BEST_PRACTICES,
|
|
REALTIME_PROMPT,
|
|
RLS_PROMPT,
|
|
STORAGE_PROMPT,
|
|
} from '@/lib/ai/prompts'
|
|
import { NO_DATA_PERMISSIONS } from '@/lib/ai/tools/tool-sanitizer'
|
|
import { fixSqlBackslashEscapes } from '@/lib/ai/util'
|
|
|
|
const KNOWLEDGE = {
|
|
pg_best_practices: PG_BEST_PRACTICES,
|
|
rls: RLS_PROMPT,
|
|
storage: STORAGE_PROMPT,
|
|
edge_functions: EDGE_FUNCTION_PROMPT,
|
|
realtime: REALTIME_PROMPT,
|
|
logs: LOGS_PROMPT,
|
|
} as const
|
|
|
|
type KnowledgeName = keyof typeof KNOWLEDGE
|
|
|
|
export const executeSqlInputSchema = z.object({
|
|
// Transform at parse time so the corrected SQL is what gets stored in
|
|
// toolCall.input — ensuring evals and logs reflect what actually runs.
|
|
sql: z.string().describe('The SQL statement to execute.').transform(fixSqlBackslashEscapes),
|
|
label: z.string().describe('A short 2-4 word label for the SQL statement.'),
|
|
chartConfig: z
|
|
.object({
|
|
view: z.enum(['table', 'chart']).describe('How to render the results after execution'),
|
|
xAxis: z.string().optional().describe('The column to use for the x-axis of the chart.'),
|
|
yAxis: z.string().optional().describe('The column to use for the y-axis of the chart.'),
|
|
})
|
|
.describe('Chart configuration for rendering the results'),
|
|
isWriteQuery: z
|
|
.boolean()
|
|
.default(false)
|
|
.describe(
|
|
'Whether the SQL statement performs a write operation or has side effects. Set true for INSERT/UPDATE/DELETE/DDL and for SELECT statements that call side-effecting functions, such as select cron.schedule(...), cron.unschedule(...), or functions that create, modify, schedule, enqueue, notify, or trigger work.'
|
|
),
|
|
})
|
|
|
|
export const loadKnowledgeInputSchema = z.object({
|
|
name: z
|
|
.enum(Object.keys(KNOWLEDGE) as [KnowledgeName, ...KnowledgeName[]])
|
|
.describe('The knowledge to load'),
|
|
})
|
|
|
|
export type StudioToolsContext = {
|
|
projectRef?: string
|
|
connectionString?: string
|
|
authorization?: string
|
|
aiOptInLevel?: AiOptInLevel
|
|
}
|
|
|
|
export const getStudioTools = (ctx: StudioToolsContext = {}) => {
|
|
const { projectRef, connectionString, authorization, aiOptInLevel = 'schema' } = ctx
|
|
const authHeaders = authorization
|
|
? { 'Content-Type': 'application/json', Authorization: authorization }
|
|
: undefined
|
|
|
|
return {
|
|
execute_sql: tool({
|
|
description:
|
|
'Asks the user to execute a SQL statement and return the results. Requires user approval before executing.',
|
|
inputSchema: executeSqlInputSchema,
|
|
needsApproval: true,
|
|
execute: async ({ sql }, { abortSignal }) => {
|
|
// The `needsApproval: true` gate on this tool means the user has
|
|
// explicitly approved this AI-generated SQL before execute runs —
|
|
// that approval is the user gesture that promotes untrusted to safe.
|
|
const { result } = await executeSql(
|
|
{ projectRef, connectionString, sql: acceptUntrustedSql(untrustedSql(sql)) },
|
|
abortSignal,
|
|
authHeaders
|
|
)
|
|
return result
|
|
},
|
|
toModelOutput: ({ output }) => {
|
|
return aiOptInLevel === 'schema_and_log_and_data'
|
|
? { type: 'json', value: output }
|
|
: { type: 'text', value: NO_DATA_PERMISSIONS }
|
|
},
|
|
}),
|
|
deploy_edge_function: tool({
|
|
description:
|
|
'Asks the user to deploy a Supabase Edge Function from provided code. Requires user approval before deploying.',
|
|
inputSchema: z.object({
|
|
name: z.string().describe('The URL-friendly name/slug of the Edge Function.'),
|
|
code: z.string().describe('The TypeScript code for the Edge Function.'),
|
|
}),
|
|
needsApproval: true,
|
|
execute: async ({ name, code }) => {
|
|
await deployEdgeFunction({
|
|
projectRef: projectRef ?? '',
|
|
slug: name,
|
|
metadata: {
|
|
entrypoint_path: 'index.ts',
|
|
name,
|
|
verify_jwt: true,
|
|
},
|
|
files: [{ name: 'index.ts', content: code }],
|
|
authorization,
|
|
})
|
|
return { success: true }
|
|
},
|
|
}),
|
|
rename_chat: tool({
|
|
description: `Rename the current chat session when the current chat name doesn't describe the conversation topic.`,
|
|
inputSchema: z.object({
|
|
newName: z.string().describe('The new name for the chat session. Five words or less.'),
|
|
}),
|
|
execute: async () => {
|
|
return { status: 'Chat request sent to client' }
|
|
},
|
|
}),
|
|
load_knowledge: tool({
|
|
description:
|
|
'Load detailed knowledge about a Supabase topic before answering questions about it.',
|
|
inputSchema: loadKnowledgeInputSchema,
|
|
execute: ({ name }) => KNOWLEDGE[name],
|
|
}),
|
|
}
|
|
}
|