Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

3.2 KB
Raw Blame
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 { getNotebookTools } from './notebook-tools'
import { getReportTools } from './report-tools'
import { getSchemaTools } from './schema-tools'
import { getOptInTools, 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,
  isExplorerEnabled,
  signal,
}: {
  projectRef: string
  connectionString: string
  authorization?: string
  aiOptInLevel: AiOptInLevel
  accessToken?: string
  baseUrl?: string
  supportMode?: boolean
  // Notebooks haven't shipped yet — they live behind the Explorer feature flag, so the
  // assistant must not advertise list_notebooks/get_notebook until the caller confirms the
  // flag is on for this user.
  isExplorerEnabled?: 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 }),
    // Opt-in levels only apply on platform
    ...(IS_PLATFORM ? getOptInTools({ aiOptInLevel }) : {}),
  }

  // If self-hosted, only add fallback tools
  if (!IS_PLATFORM) {
    tools = {
      ...tools,
      ...getFallbackTools({
        projectRef,
        connectionString,
        authorization,
        aiOptInLevel,
        includeSchemaMetadata: aiOptInLevel !== 'disabled',
      }),
    }
  } else if (accessToken) {
    // If platform, fetch MCP and other platform specific tools. The MCP tools
    // are fetched from the remote MCP server over the network, 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,
        authorization,
        aiOptInLevel,
      }),
      ...getReportTools({ projectRef, authorization, aiOptInLevel }),
      ...(isExplorerEnabled
        ? getNotebookTools({
            projectRef,
            connectionString,
            authorization,
            aiOptInLevel,
          })
        : {}),
      ...(baseUrl ? getIncidentTools({ baseUrl, aiOptInLevel }) : {}),
    }
  }

  // Filter all tools based on the (potentially modified) AI opt-in level
  const toolsWithSupport = supportMode
    ? { ...tools, ...getSupportLifecycleTools({ aiOptInLevel }) }
    : tools
  const filteredTools: ToolSet = filterToolsByOptInLevel(toolsWithSupport, aiOptInLevel)

  return filteredTools
}