mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Stacked on supabase/supabase#52398. ## What The chat system prompt's tool guidance now comes from the contracts available to the request, so the prompt never names a tool the request doesn't have. * `ToolContract` and `RemoteToolContract` gain an optional `promptGuidance`: rules for using the tool that its description doesn't cover. * `buildToolsPrompt` generates a `## Tools` section: every available tool by name, sorted, plus a section with the guidance of each tool that has any. Tool descriptions aren't repeated, since the model already gets them in the tool definitions. * `getToolRequest` computes the environment and satisfied requirements once, for both `getTools` and the prompt. * `buildSystemPrompt` assembles the full chat system prompt, used by `generateAssistantResponse` and by the tests. * Rules that named a tool move out of `GENERAL_PROMPT`, `CHAT_PROMPT`, `LIMITATIONS_PROMPT`, `NOTEBOOKS_PROMPT` (deleted), the hardcoded Available Knowledge list, and the opt-in, support-mode, and logs-snippet context messages into the `promptGuidance` of the tool each rule is about. * `SECURITY_PROMPT` is unchanged because it's shared with code completion. ## Prompt changes * Platform: the same guidance, reorganized under the tool it applies to. Small rewording: the two `search_docs` rules are merged, the opt-in rule no longer starts with "if `update_opt_in_level` is available", and two typos are fixed. The tool list also names tools the old prompt didn't mention. * Self-hosted: the prompt lists only `execute_sql`, `load_knowledge`, and `rename_chat`, and no longer mentions platform-only tools like `search_docs`, `query_logs`, notebooks, or the opt-in tool. The self-hosted fallback tools aren't listed until they become contracts in a later PR. ## Prompt caching The tools section varies with the actual provided tools. That adds no cache misses: providers cache tool definitions before the system prompt, so a change to the tool set already invalidates the cached system prompt ([Anthropic](<https://platform.claude.com/docs/en/build-with-claude/prompt-caching#what-invalidates-the-cache>), [Bedrock](<https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html#prompt-caching-converse>), [OpenAI](<https://developers.openai.com/api/docs/guides/prompt-caching>)). ## Tests * For every combination of environment, Explorer, support mode, and base URL, the system prompt and context messages name only tools available to that request. * Outside its generated section, the system prompt names no tools except `execute_sql` in the shared `SECURITY_PROMPT`. * Labeled `run-evals` to check the reworded prompt for regressions. ## Checklist Check all before review: - [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>)
49 lines
3.0 KiB
TypeScript
49 lines
3.0 KiB
TypeScript
import { z } from 'zod'
|
||
|
||
import { executeSqlOutputSchema, toModelExecuteSqlOutput } from './execute-sql-output'
|
||
import type { ToolContract } from './types'
|
||
import { fixSqlBackslashEscapes } from '@/lib/ai/util'
|
||
|
||
const inputSchema = 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 contract = {
|
||
name: 'execute_sql',
|
||
description:
|
||
'Asks the user to execute a SQL statement and return the results. Requires user approval before executing.',
|
||
inputSchema,
|
||
outputSchema: executeSqlOutputSchema,
|
||
minOptInLevel: null,
|
||
availability: {
|
||
environments: new Set(['platform', 'self-hosted']),
|
||
},
|
||
needsApproval: true,
|
||
promptGuidance: `- When the user's request is clear, call \`execute_sql\` immediately—never propose a query and ask "do you want me to run this?" The tool implementation handles user confirmation.
|
||
- Only ask clarifying questions when required information is missing or ambiguous—not as a confirmation step before execution.
|
||
- Do not show the SQL query before execution; the client will display it to the user.
|
||
- Set chartConfig \`view\` to \`chart\` and xAxis/yAxis if the results would be best displayed as a chart e.g. count of items by date
|
||
- On execution error, explain succinctly and attempt to correct if possible, validating each outcome briefly (1–2 lines) after execution.
|
||
- If a user skips execution, acknowledge and suggest alternatives. A skip is a user choice, not a permission or environment error.
|
||
- Use markdown code blocks (\`\`\`sql\`\`\`) for illustrative SQL only if requested by the user or when providing non-executable examples.
|
||
- This tool requires user approval, and tools that need approval must be called one per step: never call it in parallel with another tool that needs approval. Issue one per step and wait for its result before calling the next.
|
||
- After execution, summarize outcomes concisely without duplicating results, as the client will present these.`,
|
||
toModelOutput: toModelExecuteSqlOutput,
|
||
} as const satisfies ToolContract
|