useOrgOptedIntoAi.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { z } from 'zod'
  2. import { subscriptionHasHipaaAddon } from '@/components/interfaces/Billing/Subscription/Subscription.utils'
  3. import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query'
  4. import { useOrgSubscriptionQuery } from '@/data/subscriptions/org-subscription-query'
  5. import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
  6. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  7. import { IS_PLATFORM, OPT_IN_TAGS } from '@/lib/constants'
  8. export const aiOptInLevelSchema = z.enum([
  9. 'disabled',
  10. 'schema',
  11. 'schema_and_log',
  12. 'schema_and_log_and_data',
  13. ])
  14. export type AiOptInLevel = z.infer<typeof aiOptInLevelSchema>
  15. export const getAiOptInLevel = (tags: string[] | undefined): AiOptInLevel => {
  16. const hasSql = tags?.includes(OPT_IN_TAGS.AI_SQL)
  17. const hasData = tags?.includes(OPT_IN_TAGS.AI_DATA)
  18. const hasLog = tags?.includes(OPT_IN_TAGS.AI_LOG)
  19. if (hasData) {
  20. return 'schema_and_log_and_data'
  21. } else if (hasLog) {
  22. return 'schema_and_log'
  23. } else if (hasSql) {
  24. return 'schema'
  25. } else {
  26. return 'disabled'
  27. }
  28. }
  29. /**
  30. * Determines if the organization has opted into *any* level of AI features (schema or schema_and_log or schema_and_log_and_data).
  31. * This is primarily for backward compatibility.
  32. * @returns boolean (true if opted into schema or schema_and_log or schema_and_log_and_data, false otherwise)
  33. */
  34. export function useOrgOptedIntoAi(): boolean {
  35. const { aiOptInLevel } = useOrgAiOptInLevel()
  36. return !IS_PLATFORM || aiOptInLevel !== 'disabled'
  37. }
  38. /**
  39. * Determines the organization's specific AI opt-in level and whether schema metadata should be included.
  40. * @returns Object with aiOptInLevel and includeSchemaMetadata
  41. */
  42. export function useOrgAiOptInLevel(): {
  43. aiOptInLevel: AiOptInLevel
  44. includeSchemaMetadata: boolean
  45. isHipaaProjectDisallowed: boolean
  46. } {
  47. const { data: selectedProject } = useSelectedProjectQuery()
  48. const { data: selectedOrganization } = useSelectedOrganizationQuery()
  49. // [Joshen] Default to disabled until migration to clean up existing opt in tags are completed
  50. // Once toggled on, then we can default to their set opt in level and clean up feature flag
  51. const optInTags = selectedOrganization?.opt_in_tags
  52. const level = getAiOptInLevel(optInTags)
  53. const isOptedIntoAI = level !== 'disabled'
  54. const { data: subscription } = useOrgSubscriptionQuery({ orgSlug: selectedOrganization?.slug })
  55. const hasHipaaAddon = subscriptionHasHipaaAddon(subscription)
  56. const { data: projectSettings } = useProjectSettingsV2Query({ projectRef: selectedProject?.ref })
  57. const isProjectSensitive = !!projectSettings?.is_sensitive
  58. const preventProjectFromUsingAI = hasHipaaAddon && isProjectSensitive
  59. // [Joshen] For CLI / self-host, we'd default to 'schema' as opt in level
  60. const aiOptInLevel = !IS_PLATFORM
  61. ? 'schema'
  62. : (isOptedIntoAI && !selectedProject) || (isOptedIntoAI && !preventProjectFromUsingAI)
  63. ? level
  64. : 'disabled'
  65. const includeSchemaMetadata = !IS_PLATFORM || aiOptInLevel !== 'disabled'
  66. return {
  67. aiOptInLevel,
  68. includeSchemaMetadata,
  69. isHipaaProjectDisallowed: preventProjectFromUsingAI,
  70. }
  71. }