schema-tools.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { tool } from 'ai'
  2. import { z } from 'zod'
  3. import { getDatabasePolicies } from '@/data/database-policies/database-policies-query'
  4. export const getSchemaTools = ({
  5. projectRef,
  6. connectionString,
  7. authorization,
  8. }: {
  9. projectRef: string
  10. connectionString: string
  11. authorization?: string
  12. }) => ({
  13. list_policies: tool({
  14. description: 'Get existing RLS policies for a given schema',
  15. inputSchema: z.object({
  16. schemas: z.array(z.string()).describe('The schema names to get the policies for'),
  17. }),
  18. execute: async ({ schemas }) => {
  19. const data = await getDatabasePolicies(
  20. {
  21. projectRef,
  22. connectionString,
  23. schema: schemas?.join(','),
  24. },
  25. undefined,
  26. {
  27. 'Content-Type': 'application/json',
  28. ...(authorization && { Authorization: authorization }),
  29. }
  30. )
  31. const formattedPolicies = data
  32. .map(
  33. (policy) => `
  34. Policy Name: "${policy.name}"
  35. Action: ${policy.action}
  36. Roles: ${policy.roles.join(', ')}
  37. Command: ${policy.command}
  38. Definition: ${policy.definition}
  39. ${policy.check ? `Check: ${policy.check}` : ''}
  40. `
  41. )
  42. .join('\n')
  43. return formattedPolicies
  44. },
  45. }),
  46. })