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