mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Stacked on supabase/supabase#52422. ## What The self-hosted Assistant tools become contracts, so every tool the Assistant can call now comes from the registry and the allowlist is derived from it. * The fallback tools (`getSchemaTables`, `getRlsKnowledge`, `getFunctions`, `getEdgeFunctionKnowledge`) are deleted. Self-hosted now gets the shared tools plus: * `list_tables`: a local contract with the same input as the MCP tool and a similar output, built from pg-meta. It shares its prompt guidance with the remote contract, which moves to `list-tables.remote.ts`. On platform the remote contract is still used. * `list_functions`: self-hosted only, filtered by schema in SQL. * `list_policies`: now available in both environments. * `runPgMetaQuery` (`tool-sql.server.ts`) runs pg-meta-built SQL and validates the rows with the builder's zod schema. Off platform it calls `executeQuery` directly, because the default fetcher goes through a relative API URL that doesn't resolve server-side. * The allowlist comes from the registry: * `ToolName` and the tool set validation schema are built from the contract keys. The tool category map is removed. * `mcp-tools.ts` keeps only tools with a remote contract, before opt-in filtering, so `UI_EXECUTED_TOOLS` is gone. `EXPECTED_MCP_TOOLS` is derived from the contracts and type-checked against the MCP package. * Saved chats: the legacy contracts now trim replayed outputs. `getRlsKnowledge` replays only the saved policy list, without the stale RLS instructions. `getEdgeFunctionKnowledge` replays a pointer to `load_knowledge`. * The logs reference text no longer names `query_logs`. ## Behavior changes * `list_policies` on platform validates rows before formatting, so a row that fails pg-meta's schema now errors the tool call instead of returning text. * `list_tables` on self-hosted doesn't include the MCP tool's optional `advisory` field. * `list_functions` with an empty `schemas` list returns every non-system schema. * On platform, if the MCP tools fail to load, `list_tables` is dropped from the tool set like the other remote tools. ## Tests * Unit tests for `runPgMetaQuery`, the local `list_tables` and `list_functions`, and the output mapping. The SQL boundary is mocked; not yet run against a live self-hosted database. * Legacy contract tests cover trimming, the empty and missing policy cases, and that replaying an already-trimmed output leaves it unchanged. ## 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>)
220 lines
8.2 KiB
TypeScript
220 lines
8.2 KiB
TypeScript
import {
|
|
getToolName,
|
|
isToolUIPart,
|
|
type DynamicToolUIPart,
|
|
type ReasoningUIPart,
|
|
type ToolUIPart,
|
|
type UIMessage,
|
|
} from 'ai'
|
|
import isEqual from 'lodash/isEqual'
|
|
|
|
import { KNOWLEDGE_TOPIC_LABELS, type KnowledgeName } from '@/lib/ai/tools/knowledge-topics'
|
|
|
|
type MessagePart = UIMessage['parts'][number]
|
|
type ToolPart = ToolUIPart | DynamicToolUIPart
|
|
/** A one-line row that folds into a tool group: reasoning, or a lookup tool call. */
|
|
export type CompactPart = ReasoningUIPart | ToolPart
|
|
|
|
export function areMessagePartsEqual(previous: MessagePart, next: MessagePart): boolean {
|
|
if (previous === next) return true
|
|
if (previous.type !== next.type) return false
|
|
|
|
if (
|
|
isToolUIPart(previous) &&
|
|
isToolUIPart(next) &&
|
|
previous.state === 'output-available' &&
|
|
next.state === 'output-available' &&
|
|
!previous.preliminary &&
|
|
!next.preliminary
|
|
) {
|
|
// Final output is fixed for a tool call, but the SDK clones it on every text update.
|
|
// Keep checking identity, input, approval and metadata without walking result rows.
|
|
const { output: _previousOutput, ...previousFields } = previous
|
|
const { output: _nextOutput, ...nextFields } = next
|
|
return isEqual(previousFields, nextFields)
|
|
}
|
|
|
|
// Preliminary output and live text/reasoning can still change without a state transition.
|
|
return isEqual(previous, next)
|
|
}
|
|
|
|
type ToolLabels = {
|
|
running: string
|
|
done: string
|
|
/** What the call looks at, from its input, e.g. "in public" */
|
|
detail?: (input: unknown) => string | undefined
|
|
}
|
|
|
|
const getField = (input: unknown, key: string): unknown =>
|
|
typeof input === 'object' && input !== null ? Reflect.get(input, key) : undefined
|
|
|
|
function inSchemas(input: unknown) {
|
|
const schemas = getField(input, 'schemas')
|
|
return Array.isArray(schemas) && schemas.length > 0 ? `in ${schemas.join(', ')}` : undefined
|
|
}
|
|
|
|
function forDocsQuery(input: unknown) {
|
|
// e.g. { searchDocs(query: "row level security", limit: 5) { ... } }
|
|
const query = String(getField(input, 'graphql_query')).match(
|
|
/searchDocs\s*\(\s*query\s*:\s*"((?:[^"\\]|\\.)+)"/
|
|
)?.[1]
|
|
return query ? `for "${query}"` : undefined
|
|
}
|
|
|
|
function onKnowledgeTopic(input: unknown) {
|
|
const name = String(getField(input, 'name'))
|
|
return Object.hasOwn(KNOWLEDGE_TOPIC_LABELS, name)
|
|
? `on ${KNOWLEDGE_TOPIC_LABELS[name as KnowledgeName]}`
|
|
: undefined
|
|
}
|
|
|
|
function forAdvisorType(input: unknown) {
|
|
const type = getField(input, 'type')
|
|
return type === 'security' || type === 'performance' ? `for ${type} issues` : undefined
|
|
}
|
|
|
|
const TOOL_LABELS: Record<string, ToolLabels> = {
|
|
search_docs: { running: 'Searching docs', done: 'Searched docs', detail: forDocsQuery },
|
|
load_knowledge: { running: 'Reading up', done: 'Read up', detail: onKnowledgeTopic },
|
|
get_active_incidents: { running: 'Checking Supabase status', done: 'Checked Supabase status' },
|
|
list_policies: { running: 'Checking policies', done: 'Checked policies', detail: inSchemas },
|
|
list_tables: { running: 'Listing tables', done: 'Listed tables', detail: inSchemas },
|
|
list_functions: {
|
|
running: 'Listing database functions',
|
|
done: 'Listed database functions',
|
|
detail: inSchemas,
|
|
},
|
|
list_extensions: { running: 'Listing extensions', done: 'Listed extensions' },
|
|
list_edge_functions: { running: 'Listing Edge Functions', done: 'Listed Edge Functions' },
|
|
list_branches: { running: 'Listing branches', done: 'Listed branches' },
|
|
get_advisors: { running: 'Checking advisors', done: 'Checked advisors', detail: forAdvisorType },
|
|
list_reports: { running: 'Listing reports', done: 'Listed reports' },
|
|
get_report: { running: 'Reading report', done: 'Read report' },
|
|
list_databases: { running: 'Listing databases', done: 'Listed databases' },
|
|
list_notebooks: { running: 'Listing notebooks', done: 'Listed notebooks' },
|
|
get_notebook: { running: 'Reading notebook', done: 'Read notebook' },
|
|
// Removed self-hosted fallback tools, kept for their calls in saved chats
|
|
getSchemaTables: { running: 'Listing tables', done: 'Listed tables', detail: inSchemas },
|
|
getRlsKnowledge: { running: 'Checking policies', done: 'Checked policies', detail: inSchemas },
|
|
getFunctions: {
|
|
running: 'Listing database functions',
|
|
done: 'Listed database functions',
|
|
detail: inSchemas,
|
|
},
|
|
getEdgeFunctionKnowledge: {
|
|
running: 'Reading up',
|
|
done: 'Read up',
|
|
detail: () => 'on Edge Functions',
|
|
},
|
|
}
|
|
|
|
function getToolLabels(toolName: string): ToolLabels {
|
|
if (Object.hasOwn(TOOL_LABELS, toolName)) return TOOL_LABELS[toolName]
|
|
const name = toolName.replaceAll('_', ' ')
|
|
return { running: `Running ${name}`, done: `Ran ${name}` }
|
|
}
|
|
|
|
const BLOCK_TOOLS = new Set([
|
|
'execute_sql',
|
|
'query_logs',
|
|
'deploy_edge_function',
|
|
'create_notebook',
|
|
'update_notebook',
|
|
'delete_notebook',
|
|
'run_notebook',
|
|
'update_opt_in_level',
|
|
])
|
|
|
|
/** Lookups get a compact row. Studio's own tools need a label; any MCP tool gets a generic one. */
|
|
export function isCompactToolCall(part: ToolPart): boolean {
|
|
const toolName = getToolName(part)
|
|
return part.type === 'dynamic-tool'
|
|
? toolName !== 'query_logs'
|
|
: Object.hasOwn(TOOL_LABELS, toolName)
|
|
}
|
|
|
|
const isBlockPart = (part: MessagePart) =>
|
|
part.type === 'text'
|
|
? part.text.trim().length > 0
|
|
: isToolUIPart(part) && BLOCK_TOOLS.has(getToolName(part))
|
|
|
|
export type MessagePartItem =
|
|
| { type: 'part'; part: MessagePart; partIndex: number }
|
|
| { type: 'tool-group'; parts: CompactPart[]; groupIndex: number }
|
|
|
|
/**
|
|
* Folds each run of consecutive compact parts into a tool group. Parts that render nothing
|
|
* (step markers, empty text, tools without UI) are dropped so they don't split a run.
|
|
*/
|
|
export function groupMessageParts(parts: MessagePart[]): MessagePartItem[] {
|
|
const items: MessagePartItem[] = []
|
|
let groupCount = 0
|
|
|
|
parts.forEach((part, partIndex) => {
|
|
if (part.type === 'reasoning' || (isToolUIPart(part) && isCompactToolCall(part))) {
|
|
const lastItem = items.at(-1)
|
|
if (lastItem?.type === 'tool-group') lastItem.parts.push(part)
|
|
else items.push({ type: 'tool-group', parts: [part], groupIndex: groupCount++ })
|
|
} else if (isBlockPart(part)) {
|
|
items.push({ type: 'part', part, partIndex })
|
|
}
|
|
})
|
|
|
|
return items
|
|
}
|
|
|
|
export const INTERRUPTED_LABEL = 'Response interrupted'
|
|
|
|
/** `running` covers any unfinished part. Once its message stops, it renders as interrupted. */
|
|
export type CompactPartStatus = 'running' | 'done' | 'failed'
|
|
|
|
export function getCompactPartStatus(part: CompactPart): CompactPartStatus {
|
|
if (part.type === 'reasoning') return part.state === 'streaming' ? 'running' : 'done'
|
|
if (part.state === 'output-available') return 'done'
|
|
if (part.state === 'output-error' || part.state === 'output-denied') return 'failed'
|
|
return 'running'
|
|
}
|
|
|
|
export const isRunningToolCall = (part: CompactPart) =>
|
|
part.type !== 'reasoning' && getCompactPartStatus(part) === 'running'
|
|
|
|
export function getCompactPartLabel(
|
|
part: CompactPart,
|
|
status = getCompactPartStatus(part)
|
|
): string {
|
|
if (part.type === 'reasoning') return status === 'running' ? 'Thinking...' : 'Reasoned'
|
|
|
|
const labels = getToolLabels(getToolName(part))
|
|
const detail = labels.detail?.(part.input)
|
|
const target = detail ? ` ${detail}` : ''
|
|
if (status === 'running') return `${labels.running}${target}...`
|
|
if (status === 'failed') return `${labels.running}${target} failed`
|
|
return `${labels.done}${target}`
|
|
}
|
|
|
|
const lowerFirst = (text: string) => text.charAt(0).toLowerCase() + text.slice(1)
|
|
|
|
/** A finished group's header: what its tool calls did, e.g. "Searched docs and checked policies". */
|
|
export function getToolGroupSummary(parts: CompactPart[]): string {
|
|
const actions = new Set(
|
|
parts.flatMap((part) =>
|
|
part.type !== 'reasoning' && getCompactPartStatus(part) === 'done'
|
|
? [getToolLabels(getToolName(part)).done]
|
|
: []
|
|
)
|
|
)
|
|
const [first, second, ...rest] = actions
|
|
|
|
if (!first) {
|
|
// Nothing finished: the group only reasoned, failed or was cut off
|
|
const lastPart = parts.at(-1)
|
|
if (!lastPart) return 'Reasoned'
|
|
return getCompactPartStatus(lastPart) === 'running'
|
|
? INTERRUPTED_LABEL
|
|
: getCompactPartLabel(lastPart)
|
|
}
|
|
if (!second) return first
|
|
if (rest.length === 0) return `${first} and ${lowerFirst(second)}`
|
|
return `${first}, ${lowerFirst(second)}, and ${rest.length} more`
|
|
}
|