Files
supabase/apps/studio/pages/api/ai/sql/cron.ts
T
Terry SuttonandJoshen Lim 95219d3e2a Chore/cron nl (#30345)
* Add NL input

* Add chart to sidebar

* Better loading animation

* Redo api route to not use edge

* Remove edge stuff

* Allow sheet to close

* Cleanup

* Adjust loading state

* Add timezone

* CR comments

* Replace query with useCompletion

* Set the form value

* Tiny clean up

* Tinyyyyy tweaks

* Fix missing syntax

* Smol refactor

* Fixy

* Remove console log

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2024-11-12 11:55:01 -03:30

60 lines
1.6 KiB
TypeScript

import { ContextLengthError } from 'ai-commands'
import { generateCron } from 'ai-commands/edge'
import apiWrapper from 'lib/api/apiWrapper'
import { NextApiRequest, NextApiResponse } from 'next'
import OpenAI from 'openai'
const openAiKey = process.env.OPENAI_API_KEY
const openai = new OpenAI({ apiKey: openAiKey })
async function handler(req: NextApiRequest, res: NextApiResponse) {
if (!openAiKey) {
return res.status(500).json({
error: 'No OPENAI_API_KEY set. Create this environment variable to use AI features.',
})
}
const { method } = req
switch (method) {
case 'POST':
return handlePost(req, res)
default:
res.setHeader('Allow', ['POST'])
res.status(405).json({ data: null, error: { message: `Method ${method} Not Allowed` } })
}
}
export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
const {
body: { prompt },
} = req
try {
const result = await generateCron(openai, prompt)
return res.json(result)
} catch (error) {
if (error instanceof Error) {
console.error(`AI cron generation failed: ${error.message}`)
if (error instanceof ContextLengthError) {
return res.status(400).json({
error:
'Your cron prompt is too large for Supabase AI to ingest. Try splitting it into smaller prompts.',
})
}
} else {
console.log(`Unknown error: ${error}`)
}
return res.status(500).json({
error: 'There was an unknown error generating the cron syntax. Please try again.',
})
}
}
const wrapper = (req: NextApiRequest, res: NextApiResponse) => apiWrapper(req, res, handler)
export default wrapper