Files
Matt Rossman 3fcb6cbe2c fix(studio): pass providerOptions to all AI SDK calls (#43031)
`reasoningEffort: 'minimal'` was
[configured](https://github.com/supabase/supabase/blob/d5cc70560d/apps/studio/lib/ai/model.utils.ts#L55-L59)
in the provider registry but `getModel()` returns it as a separate value
that callers must destructure and forward — and 7 of 8 endpoints weren't
doing so. This meant `gpt-5-mini` (a reasoning model) was running at
default reasoning effort for every call.

This PR destructures `providerOptions` from `getModel()` and passes it
to `generateObject`/`generateText` in all affected endpoints.

## Benchmark (local, median of 5 runs)

| Endpoint | Before (s) | After (s) | Speedup |
|----------|-----------|----------|---------|
| title-v2 | 7.0 | 1.9 | 3.7x |
| cron-v2 | 2.3 | 0.9 | 2.6x |
| filter-v1 | 5.8 | 2.2 | 2.6x |
| feedback/classify | 3.5 | 0.9 | 3.9x |
| feedback/rate | 2.9 | 0.9 | 3.2x |

`code/complete` and `policy` also received the fix but aren't
benchmarked here as they require a live DB connection and use multi-step
tool calls (separate latency concern tracked in AI-419).

To test the SQL naming, visit the SQL Editor in sidebar, add some SQL
like:

```sql
create table todos (                                                                                                 
  id serial primary key,                                                                                             
  task text not null,                                                                                                
  completed boolean default false
);
```

Right click on the snippet, "Rename" and "Rename with Supabase AI"

Closes AI-443
2026-02-20 11:36:06 -05:00

125 lines
3.8 KiB
TypeScript

import { generateObject } from 'ai'
import { getModel } from 'lib/ai/model'
import apiWrapper from 'lib/api/apiWrapper'
import { NextApiRequest, NextApiResponse } from 'next'
import { z } from 'zod'
async function handler(req: NextApiRequest, res: NextApiResponse) {
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
if (!prompt) {
return res.status(400).json({
error: 'Prompt is required',
})
}
try {
const {
model,
error: modelError,
providerOptions,
} = await getModel({
provider: 'openai',
routingKey: 'feedback',
})
if (modelError) {
return res.status(500).json({ error: modelError.message })
}
const { object } = await generateObject({
model,
providerOptions,
schema: z.object({
feedback_category: z.enum(['support', 'feedback', 'unknown']),
}),
temperature: 0,
prompt: `
Classify the following feedback as ONE of: support, feedback, unknown.
- support: bug reports, help requests, or issues
- feedback: feature requests or suggestions
- unknown: unclear or unrelated
If you can't determine support or feedback, always output "unknown".
Only output a JSON object in this format: { "feedback_category": "support|feedback|unknown" }
Examples:
Feedback: "Whenever I try to invite a team member, the invite email doesn't get sent."
Response: { "feedback_category": "support" }
Feedback: "I have reached the storage limit for my project and my plan. I cannot understand how I can expand the storage space in my project."
Response: { "feedback_category": "support" }
Feedback: "Please delete the project x in my account"
Response: { "feedback_category": "support" }
Feedback: "My billing page is broken"
Response: { "feedback_category": "support" }
Feedback: "I accidentally deleted my database—can it be recovered?"
Response: { "feedback_category": "support" }
Feedback: "My login tokens are expiring too quickly, even though I didn't change any settings."
Response: { "feedback_category": "support" }
Feedback: "Can you add more integrations?"
Response: { "feedback_category": "feedback" }
Feedback: "I'm getting charged for a project I thought I deleted. Can you help me stop billing?"
Response: { "feedback_category": "support" }
Feedback: "Could you support OAuth login for more providers like Apple or LinkedIn?"
Response: { "feedback_category": "feedback" }
Feedback: "It's unclear in the docs how to set up row-level security with multiple roles."
Response: { "feedback_category": "feedback" }
Feedback: "I am trying to pause my Pro project"
Response: { "feedback_category": "feedback" }
Feedback: "${prompt}"
Response:
`,
})
return res.json({ feedback_category: object.feedback_category })
} catch (error) {
if (error instanceof Error) {
console.error(`Classifying this feedback failed`)
// Check for context length error
if (error.message.includes('context_length') || error.message.includes('too long')) {
return res.status(400).json({
error: 'This prompt is too large to ingest',
})
}
} else {
console.error(`Unknown error: ${error}`)
}
return res.status(500).json({
error: 'There was an unknown error generating the feedback category.',
})
}
}
const wrapper = (req: NextApiRequest, res: NextApiResponse) =>
apiWrapper(req, res, handler, { withAuth: true })
export default wrapper