Files
supabase/apps/studio/lib/ai/tools/index.ts
Joshen Lim 4901f081e5 Migrate remaining requests to pg-meta API to use query endpoint (#47758)
## Context

Migrates the remaining API requests to the pg-meta endpoint to use the
query endpoint directly with the SQL from the pg-meta package. This
touches the following:
- policies
- publications
- triggers
- views
- materialized views
- types

## To test
Just need to verify that we're still fetching the data correctly on
these pages
- Database policies
- Database publications
- Database triggers
- Database tables (views + materialized views)
- Database types

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Bug Fixes**
* Improved and stabilized loading of database metadata (views, triggers,
RLS policies, publications, materialized views, and enum types),
including more reliable schema-scoped filtering.
* Updated policy loading behavior and related UI queries to consistently
use schema arrays, improving cache correctness and consistency.
* **Tests**
* Updated end-to-end test synchronization to wait for the correct
metadata responses using more specific request identifiers.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-07-09 17:09:03 +08:00

83 lines
2.4 KiB
TypeScript

import { ToolSet } from 'ai'
import { IS_PLATFORM } from 'common'
import { filterToolsByOptInLevel } from '../tool-filter'
import { getFallbackTools } from './fallback-tools'
import { getIncidentTools } from './incident-tools'
import { getMcpTools } from './mcp-tools'
import { getSchemaTools } from './schema-tools'
import { getStudioTools } from './studio-tools'
import { getSupportLifecycleTools } from './support-tools'
import { AiOptInLevel } from '@/hooks/misc/useOrgOptedIntoAi'
export const getTools = async ({
projectRef,
connectionString,
authorization,
aiOptInLevel,
accessToken,
baseUrl,
supportMode,
signal,
}: {
projectRef: string
connectionString: string
authorization?: string
aiOptInLevel: AiOptInLevel
accessToken?: string
baseUrl?: string
supportMode?: boolean
// Required: tools fetched from the remote MCP server hold an HTTP connection
// that is closed when this signal aborts (i.e. when the request ends).
signal: AbortSignal
}) => {
// Always include studio tools
let tools: ToolSet = getStudioTools({ projectRef, connectionString, authorization, aiOptInLevel })
// If self-hosted, only add fallback tools
if (!IS_PLATFORM) {
tools = {
...tools,
...getFallbackTools({
projectRef,
connectionString,
authorization,
includeSchemaMetadata: aiOptInLevel !== 'disabled',
}),
}
} else if (accessToken) {
// If platform, fetch MCP and other platform specific tools. The MCP tools
// may be fetched from the remote MCP server over the network (see
// `USE_REMOTE_MCP`), so a failure there (outage, timeout, auth) should
// degrade gracefully to the remaining tools rather than break the entire
// assistant.
let mcpTools: ToolSet = {}
try {
mcpTools = await getMcpTools({
accessToken,
projectRef,
aiOptInLevel,
signal,
})
} catch (error) {
console.error('Failed to fetch MCP tools:', error)
}
tools = {
...tools,
...mcpTools,
...getSchemaTools({
projectRef,
connectionString,
}),
...(baseUrl ? getIncidentTools({ baseUrl }) : {}),
}
}
// Filter all tools based on the (potentially modified) AI opt-in level
const toolsWithSupport = supportMode ? { ...tools, ...getSupportLifecycleTools() } : tools
const filteredTools: ToolSet = filterToolsByOptInLevel(toolsWithSupport, aiOptInLevel)
return filteredTools
}