parse-client-code.ts85 lines · main
| 1 | import { generateText, Output } from 'ai' |
| 2 | import { source } from 'common-tags' |
| 3 | import { NextApiRequest, NextApiResponse } from 'next' |
| 4 | import { z } from 'zod' |
| 5 | |
| 6 | import { getModel } from '@/lib/ai/model' |
| 7 | import { DEFAULT_COMPLETION_MODEL } from '@/lib/ai/model.utils' |
| 8 | import apiWrapper from '@/lib/api/apiWrapper' |
| 9 | |
| 10 | const codeSchema = z.object({ |
| 11 | sql: z |
| 12 | .string() |
| 13 | .nullable() |
| 14 | .describe( |
| 15 | 'The converted SQL query from the provided client library code. Return null if the code is invalid' |
| 16 | ), |
| 17 | valid: z.boolean().describe('Whether the provided client library code is valid.'), |
| 18 | }) |
| 19 | |
| 20 | async function handler(req: NextApiRequest, res: NextApiResponse) { |
| 21 | const { method } = req |
| 22 | |
| 23 | switch (method) { |
| 24 | case 'POST': |
| 25 | return handlePost(req, res) |
| 26 | default: |
| 27 | res.setHeader('Allow', ['POST']) |
| 28 | res.status(405).json({ data: null, error: { message: `Method ${method} Not Allowed` } }) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | export async function handlePost(req: NextApiRequest, res: NextApiResponse) { |
| 33 | const { |
| 34 | body: { code }, |
| 35 | } = req |
| 36 | |
| 37 | if (!code) return res.status(400).json({ error: 'Code is required' }) |
| 38 | |
| 39 | try { |
| 40 | const { modelParams, error: modelError } = await getModel({ |
| 41 | provider: 'openai', |
| 42 | modelEntry: DEFAULT_COMPLETION_MODEL, |
| 43 | }) |
| 44 | |
| 45 | if (modelError) { |
| 46 | return res.status(500).json({ error: modelError.message }) |
| 47 | } |
| 48 | |
| 49 | const result = await generateText({ |
| 50 | ...modelParams, |
| 51 | output: Output.object({ schema: codeSchema }), |
| 52 | prompt: source` |
| 53 | Convert the follow Briven client library code into SQL. The response should only be in JSON with the structure: { sql: string, valid: boolean } |
| 54 | If the client library code does not look valid, return { sql: null, valid: false }. Otherwise return valid as true and sql as the converted SQL query |
| 55 | |
| 56 | ${code} |
| 57 | `, |
| 58 | }) |
| 59 | |
| 60 | return res.json(result.output) |
| 61 | } catch (error) { |
| 62 | if (error instanceof Error) { |
| 63 | console.error(`Code parsing to SQL failed: ${error.message}`) |
| 64 | |
| 65 | // Check for context length error |
| 66 | if (error.message.includes('context_length') || error.message.includes('too long')) { |
| 67 | return res.status(400).json({ |
| 68 | error: |
| 69 | 'The provided code snippet is too large for Briven Assistant to ingest. Try splitting it into smaller queries.', |
| 70 | }) |
| 71 | } |
| 72 | } else { |
| 73 | console.log(`Unknown error: ${error}`) |
| 74 | } |
| 75 | |
| 76 | return res.status(500).json({ |
| 77 | error: 'There was an unknown error parsing the client library code. Please try again.', |
| 78 | }) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | const wrapper = (req: NextApiRequest, res: NextApiResponse) => |
| 83 | apiWrapper(req, res, handler, { withAuth: true }) |
| 84 | |
| 85 | export default wrapper |