Files
supabase/apps/studio/pages/api/ai/feedback/classify.ts
T
Matt Rossman d29fbf6eb7 feat(assistant): upgrade AI SDK v5 → v6 (#43931)
Upgrades `ai` from v5 to v6 and all related packages.

**Package bumps:**
- `ai`: `5.0.52` → `^6.0.116`
- `@ai-sdk/openai`: `2.0.32` → `^3.0.41`
- `@ai-sdk/react`: `2.0.52` → `^3.0.118`
- `@ai-sdk/provider`: `^2.0.0` → `^3.0.8`
- `@ai-sdk/provider-utils`: `^3.0.0` → `^4.0.19`
- `@ai-sdk/amazon-bedrock`: `^3.0.0` → `^4.0.81`
- `@ai-sdk/mcp`: N/A → `^1.0.25`
- `openai`: bumped to `^4.104.0`
- `braintrust`: `3.0.x` → `^3.4.0`

**Breaking change migrations:**
- `generateObject` removed in v6 — migrated 5 API routes to
`generateText` with `Output.object({ schema })`, returning
`result.output`
- `convertToModelMessages` is now async — added `await`
- MCP import path changed: `experimental_createMCPClient` from `ai` →
`createMCPClient` from `@ai-sdk/mcp`
- `openai()` defaults to Responses API — added `store: false` to
provider options for ZDR org compatibility

**Streaming fix:**
Added `Content-Encoding: none` header to `pipeUIMessageStreamToResponse`
calls. Without it, proxy middleware buffers the entire SSE response
before flushing, causing the full reply to appear at once.

**Zero Data Retention fix:**
In recent AI SDK versions, `openai()` default to Responses API instead
of the legacy chat completions API. This produces a 404 from OpenAI with
message `"Items are not persisted for Zero Data Retention organizations.
Remove this item from your input and try again."` The Responses API is
OpenAI's [recommended
endpoint](https://developers.openai.com/api/docs/guides/migrate-to-responses).

This PR adds `store: false` as mentioned in
https://github.com/vercel/ai/issues/10060 to avoid incompatible
persistence attempts.

**References:**
- https://ai-sdk.dev/docs/migration-guides/migration-guide-6-0
-
https://ai-sdk.dev/docs/troubleshooting/streaming-not-working-when-proxied
- https://github.com/vercel/ai/issues/10060

Closes AI-514
Related AI-509
2026-03-19 09:07:33 -04:00

127 lines
3.8 KiB
TypeScript

import { generateText, Output } 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 { output } = await generateText({
model,
providerOptions,
output: Output.object({
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: output.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