index.ts65 lines · main
| 1 | import { ToolSet } from 'ai' |
| 2 | import { IS_PLATFORM } from 'common' |
| 3 | |
| 4 | import { filterToolsByOptInLevel } from '../tool-filter' |
| 5 | import { getFallbackTools } from './fallback-tools' |
| 6 | import { getIncidentTools } from './incident-tools' |
| 7 | import { getMcpTools } from './mcp-tools' |
| 8 | import { getSchemaTools } from './schema-tools' |
| 9 | import { getStudioTools } from './studio-tools' |
| 10 | import { AiOptInLevel } from '@/hooks/misc/useOrgOptedIntoAi' |
| 11 | |
| 12 | export const getTools = async ({ |
| 13 | projectRef, |
| 14 | connectionString, |
| 15 | authorization, |
| 16 | aiOptInLevel, |
| 17 | accessToken, |
| 18 | baseUrl, |
| 19 | }: { |
| 20 | projectRef: string |
| 21 | connectionString: string |
| 22 | authorization?: string |
| 23 | aiOptInLevel: AiOptInLevel |
| 24 | accessToken?: string |
| 25 | baseUrl?: string |
| 26 | }) => { |
| 27 | // Always include studio tools |
| 28 | let tools: ToolSet = getStudioTools({ projectRef, connectionString, authorization, aiOptInLevel }) |
| 29 | |
| 30 | // If self-hosted, only add fallback tools |
| 31 | if (!IS_PLATFORM) { |
| 32 | tools = { |
| 33 | ...tools, |
| 34 | ...getFallbackTools({ |
| 35 | projectRef, |
| 36 | connectionString, |
| 37 | authorization, |
| 38 | includeSchemaMetadata: aiOptInLevel !== 'disabled', |
| 39 | }), |
| 40 | } |
| 41 | } else if (accessToken) { |
| 42 | // If platform, fetch MCP and other platform specific tools |
| 43 | const mcpTools = await getMcpTools({ |
| 44 | accessToken, |
| 45 | projectRef, |
| 46 | aiOptInLevel, |
| 47 | }) |
| 48 | |
| 49 | tools = { |
| 50 | ...tools, |
| 51 | ...mcpTools, |
| 52 | ...getSchemaTools({ |
| 53 | projectRef, |
| 54 | connectionString, |
| 55 | authorization, |
| 56 | }), |
| 57 | ...(baseUrl ? getIncidentTools({ baseUrl }) : {}), |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Filter all tools based on the (potentially modified) AI opt-in level |
| 62 | const filteredTools: ToolSet = filterToolsByOptInLevel(tools, aiOptInLevel) |
| 63 | |
| 64 | return filteredTools |
| 65 | } |