index.ts 1.7 KB

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