|
| 1 | +import { SQL_FILTERS_LIST } from '@conar/shared/filters/sql' |
| 2 | +import { webSearch } from '@exalabs/ai-sdk' |
| 3 | +import { queryDocs, resolveLibraryId } from '@upstash/context7-tools-ai-sdk' |
| 4 | +import { tool } from 'ai' |
| 5 | +import { type } from 'arktype' |
| 6 | +import { env } from '~/env' |
| 7 | + |
| 8 | +export const tools = { |
| 9 | + columns: tool({ |
| 10 | + description: 'Use this tool if you need to get the list of columns in a table.', |
| 11 | + inputSchema: type({ |
| 12 | + tableAndSchema: type({ |
| 13 | + tableName: 'string', |
| 14 | + schemaName: 'string', |
| 15 | + }), |
| 16 | + }), |
| 17 | + outputSchema: type({ |
| 18 | + isEditable: 'boolean', |
| 19 | + isNullable: 'boolean', |
| 20 | + table: 'string', |
| 21 | + id: 'string', |
| 22 | + type: 'string', |
| 23 | + default: 'string | null', |
| 24 | + }).array(), |
| 25 | + }), |
| 26 | + enums: tool({ |
| 27 | + description: 'Use this tool if you need to get the list of enums in a database', |
| 28 | + inputSchema: type({}), |
| 29 | + outputSchema: type({ |
| 30 | + schema: 'string', |
| 31 | + name: 'string', |
| 32 | + value: 'string', |
| 33 | + }).array(), |
| 34 | + }), |
| 35 | + select: tool({ |
| 36 | + description: [ |
| 37 | + 'Use this tool to select data from the database to improve your response.', |
| 38 | + 'Do not abuse this tool, unless you are 100% sure that the data will help to answer the question.', |
| 39 | + 'Do not select any sensitive data, like password, token, secret, card number, etc.', |
| 40 | + 'Mask sensitive data with asterisks if need to select to answer the question.', |
| 41 | + 'Do not use any tables and schemas that are not provided in the input.', |
| 42 | + 'tableName and schemaName will be concatenated to "schemaName.tableName".', |
| 43 | + 'For tableName use only table without schema prefix.', |
| 44 | + ].join('\n'), |
| 45 | + inputSchema: type({ |
| 46 | + whereConcatOperator: type('"AND" | "OR"').describe('The operator to use to concatenate the where clauses'), |
| 47 | + whereFilters: type({ |
| 48 | + column: 'string', |
| 49 | + operator: type |
| 50 | + .enumerated(...SQL_FILTERS_LIST.map(filter => filter.operator)) |
| 51 | + .describe('The operator to use in the where clause'), |
| 52 | + values: 'string[]', |
| 53 | + }) |
| 54 | + .array() |
| 55 | + .describe('The columns to use in the where clause'), |
| 56 | + select: type('string[] | null').describe('The columns to select. If not provided, all columns will be selected'), |
| 57 | + limit: 'number', |
| 58 | + offset: 'number', |
| 59 | + orderBy: 'Record<string, "ASC" | "DESC"> | null', |
| 60 | + tableAndSchema: type({ |
| 61 | + tableName: 'string', |
| 62 | + schemaName: 'string', |
| 63 | + }).describe('The name of the table and schema to query'), |
| 64 | + }).describe('Input schema for database select query with filters, ordering, and pagination'), |
| 65 | + outputSchema: type('unknown'), |
| 66 | + }), |
| 67 | + ...(env.EXA_API_KEY && { webSearch: webSearch({ apiKey: env.EXA_API_KEY }) }), |
| 68 | + ...(env.CONTEXT7_API_KEY && { |
| 69 | + resolveLibraryId: resolveLibraryId({ apiKey: env.CONTEXT7_API_KEY }), |
| 70 | + queryDocs: queryDocs({ apiKey: env.CONTEXT7_API_KEY }), |
| 71 | + }), |
| 72 | +} |
0 commit comments