Files
supabase/apps/studio/lib/ai/assistant-context.test.ts
Charis 6aefa45d2b refactor(studio): build the Assistant tool prompt from contracts (#52422)
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>)
2026-10-30 15:42:18 -04:00

81 lines
2.8 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { buildAssistantContextMessages, NO_SCHEMA_ACCESS_MESSAGE } from '@/lib/ai/assistant-context'
const SCHEMAS = 'The available database schema names are: ["public"]'
describe('buildAssistantContextMessages', () => {
it('describes the project when there is project context', () => {
const messages = buildAssistantContextMessages({
projectRef: 'abcdefghijklmnopqrst',
chatName: 'Slow queries',
schemasString: SCHEMAS,
now: new Date('2026-08-20T06:53:00.000Z'),
})
expect(messages).toHaveLength(1)
expect(messages[0].role).toBe('assistant')
expect(messages[0].content).toContain('abcdefghijklmnopqrst')
expect(messages[0].content).toContain(SCHEMAS)
expect(messages[0].content).toContain('Slow queries')
expect(messages[0].content).toContain('2026-08-20T06:53:00.000Z')
expect(messages[0].content).toContain('iso_timestamp_start')
})
it('omits the project message when there is nothing to say', () => {
const messages = buildAssistantContextMessages({
schemasString: NO_SCHEMA_ACCESS_MESSAGE,
})
expect(messages).toEqual([])
})
it('tells the model the current opt-in level so it does not ask for access it has', () => {
const messages = buildAssistantContextMessages({
schemasString: NO_SCHEMA_ACCESS_MESSAGE,
aiOptInLevel: 'schema_and_log_and_data',
})
expect(messages).toHaveLength(1)
expect(messages[0].content).toContain('schema_and_log_and_data')
})
it('adds the support instructions in support mode', () => {
const messages = buildAssistantContextMessages({
schemasString: NO_SCHEMA_ACCESS_MESSAGE,
supportMode: true,
})
expect(messages).toHaveLength(1)
expect(messages[0].content).toContain('active support chat')
})
it('adds ClickHouse instructions when the conversation attached a logs query', () => {
const messages = buildAssistantContextMessages({
projectRef: 'abcdefghijklmnopqrst',
schemasString: SCHEMAS,
includesLogsSnippets: true,
})
expect(messages).toHaveLength(2)
const logsContext = messages[1].content
// The dialect rules, so it doesn't answer in Postgres...
expect(logsContext).toContain('ClickHouse')
// ...and the table reference, so it doesn't invent BigQuery-style unnests.
expect(logsContext).toContain('log_attributes')
expect(logsContext).toContain("where source = 'edge_logs'")
expect(logsContext).not.toContain('cannot run')
})
it('adds nothing extra for a database-only conversation', () => {
const messages = buildAssistantContextMessages({
projectRef: 'abcdefghijklmnopqrst',
schemasString: SCHEMAS,
includesLogsSnippets: false,
})
expect(messages).toHaveLength(1)
expect(messages[0].content).not.toContain('ClickHouse')
})
})