mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
## Problem The AI Assistant follow-up on support requests (behind the `supportAssistantFollowUp` flag) told users with service restrictions to ask Support for an exception. In almost every restriction ticket we reviewed during the production test, the Assistant suggested this, and Support then declined. ## Solution Rather than a rule for each ticket type, this adds one general rule to the server-side support-mode context and removes the "a human will reply" framing. `escalate_to_human` is unchanged, so users who ask for a person can still be escalated. ## Review instructions Read the diff to `assistant-context.ts` and `SupportAssistant.utils.ts`, and check that the rule is general and doesn't name a particular ticket type. ## Checklist Check all before review: - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] 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)
97 lines
4.3 KiB
TypeScript
97 lines
4.3 KiB
TypeScript
import type { AiOptInLevel } from '@/hooks/misc/useOrgOptedIntoAi'
|
|
import {
|
|
buildClickhouseLogsSchemaSection,
|
|
CLICKHOUSE_LOGS_COMPLETION_INSTRUCTIONS,
|
|
} from '@/lib/ai/clickhouse-logs'
|
|
|
|
/**
|
|
* Stands in for the schema list when the org hasn't opted into sharing schemas.
|
|
* Doubles as a sentinel — "no project context worth sending" is decided by
|
|
* comparing against this exact sentence — so the producer and the comparison have
|
|
* to agree on it, hence one exported constant instead of copies per call site.
|
|
*/
|
|
export const NO_SCHEMA_ACCESS_MESSAGE = "You don't have access to any schemas."
|
|
|
|
/**
|
|
* A request-scoped context message, sent as an assistant turn ahead of the
|
|
* conversation. Deliberately NOT part of the system prompt: the system prompt is
|
|
* static so Bedrock can cache it, and anything derived from the current project,
|
|
* chat, or open editor tab would break that cache.
|
|
*/
|
|
export type AssistantContextMessage = { role: 'assistant'; content: string }
|
|
|
|
/**
|
|
* Tells the model that the snippet in the SQL editor targets the logs backend, so
|
|
* SQL it writes for that snippet comes back as ClickHouse for the `logs` table
|
|
* rather than Postgres. Carries the same dialect rules and table reference the
|
|
* inline editor completions use, so there's one description of the logs schema.
|
|
*/
|
|
function buildLogsSnippetContext(): string {
|
|
return [
|
|
"Some SQL snippets are marked with the dialect 'clickhouse', which means they query the Supabase logs backend, not the Postgres database. Any SQL you write, edit, or debug for that snippet must be ClickHouse SQL against the logs table described below — the database schema and the Postgres tools don't apply to it. Postgres SQL is still the right answer for anything else the user asks about their database, or for non-ClickHouse marked queries.",
|
|
CLICKHOUSE_LOGS_COMPLETION_INSTRUCTIONS.trim(),
|
|
buildClickhouseLogsSchemaSection().trim(),
|
|
].join('\n\n')
|
|
}
|
|
|
|
/**
|
|
* Assemble the context messages that precede the conversation: what project and
|
|
* chat this is, whether it's a support chat, and which backend the open SQL editor
|
|
* snippet targets. Pure and per-request — see {@link AssistantContextMessage} for
|
|
* why none of this belongs in the system prompt.
|
|
*/
|
|
export function buildAssistantContextMessages({
|
|
projectRef,
|
|
chatName,
|
|
schemasString,
|
|
supportMode,
|
|
includesLogsSnippets,
|
|
aiOptInLevel,
|
|
now = new Date(),
|
|
}: {
|
|
projectRef?: string
|
|
chatName?: string
|
|
schemasString: string
|
|
supportMode?: boolean
|
|
/** Whether any user message in the conversation attached a logs (ClickHouse) query. */
|
|
includesLogsSnippets?: boolean
|
|
/** The organization's current opt-in level, so the model doesn't ask for access it already has. */
|
|
aiOptInLevel?: AiOptInLevel
|
|
/** Injected so tests can pin the clock. Lives here, not the system prompt, so Bedrock can cache the system prompt. */
|
|
now?: Date
|
|
}): AssistantContextMessage[] {
|
|
const messages: AssistantContextMessage[] = []
|
|
|
|
const hasProjectContext = !!projectRef || !!chatName || schemasString !== NO_SCHEMA_ACCESS_MESSAGE
|
|
if (hasProjectContext) {
|
|
messages.push({
|
|
role: 'assistant',
|
|
content: `The user's current project is ${projectRef || 'unknown'}. Their available schemas are: ${schemasString}. The current chat name is: ${chatName || 'unnamed'}. The current time is ${now.toISOString()} (UTC). Use this clock when converting relative ranges such as "last hour" into iso_timestamp_start and iso_timestamp_end.`,
|
|
})
|
|
}
|
|
|
|
if (aiOptInLevel) {
|
|
messages.push({
|
|
role: 'assistant',
|
|
content: `The organization's AI opt-in level is ${aiOptInLevel}.`,
|
|
})
|
|
}
|
|
|
|
if (supportMode) {
|
|
messages.push({
|
|
role: 'assistant',
|
|
content: [
|
|
'This is an active support chat. The user has submitted a support request. Keep guidance practical and concise.',
|
|
'Do not promise or imply that a human will reply, or say when.',
|
|
'Do not offer, promise or suggest actions that only Supabase staff can take, such as exceptions, refunds, credits, limit increases, extensions or manually lifting a restriction. Only recommend steps the user can take themselves, as confirmed by the documentation or your tools.',
|
|
].join('\n'),
|
|
})
|
|
}
|
|
|
|
if (includesLogsSnippets) {
|
|
messages.push({ role: 'assistant', content: buildLogsSnippetContext() })
|
|
}
|
|
|
|
return messages
|
|
}
|