Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix(studio): pass providerOptions to all AI SDK calls (#43031)

Matt Rossman3fcb6cbe2cd4
diff --git a/apps/studio/pages/api/ai/code/complete.ts b/apps/studio/pages/api/ai/code/complete.ts
index f23145983ec..300db283fec 100644
--- a/apps/studio/pages/api/ai/code/complete.ts
+++ b/apps/studio/pages/api/ai/code/complete.ts
@@ -68,6 +68,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
model,
error: modelError,
promptProviderOptions,
+ providerOptions,
} = await getModel({
provider: 'openai',
routingKey: projectRef,
@@ -155,6 +156,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
const { text } = await generateText({
model,
+ providerOptions,
stopWhen: stepCountIs(5),
messages: coreMessages,
tools,
diff --git a/apps/studio/pages/api/ai/feedback/classify.ts b/apps/studio/pages/api/ai/feedback/classify.ts
index ccebe72a43b..bc673a9aa25 100644
--- a/apps/studio/pages/api/ai/feedback/classify.ts
+++ b/apps/studio/pages/api/ai/feedback/classify.ts
@@ -1,8 +1,8 @@
import { generateObject } from 'ai'
-import { NextApiRequest, NextApiResponse } from 'next'
-import { z } from 'zod'
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
@@ -28,7 +28,11 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
}
try {
- const { model, error: modelError } = await getModel({
+ const {
+ model,
+ error: modelError,
+ providerOptions,
+ } = await getModel({
provider: 'openai',
routingKey: 'feedback',
})
@@ -39,6 +43,7 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
const { object } = await generateObject({
model,
+ providerOptions,
schema: z.object({
feedback_category: z.enum(['support', 'feedback', 'unknown']),
}),
diff --git a/apps/studio/pages/api/ai/feedback/rate.ts b/apps/studio/pages/api/ai/feedback/rate.ts
index c1cc9e8a1bf..641f11d469e 100644
--- a/apps/studio/pages/api/ai/feedback/rate.ts
+++ b/apps/studio/pages/api/ai/feedback/rate.ts
@@ -96,7 +96,11 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
})
try {
- const { model, error: modelError } = await getModel({
+ const {
+ model,
+ error: modelError,
+ providerOptions,
+ } = await getModel({
provider: 'openai',
isLimited: true,
routingKey: 'feedback',
@@ -108,6 +112,7 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
const { object } = await generateObject({
model,
+ providerOptions,
schema: rateMessageResponseSchema,
prompt: `
Your job is to look at a Supabase Assistant conversation, which the user has given feedback on, and classify it.
diff --git a/apps/studio/pages/api/ai/sql/cron-v2.ts b/apps/studio/pages/api/ai/sql/cron-v2.ts
index 000106e276c..d9344c9a43f 100644
--- a/apps/studio/pages/api/ai/sql/cron-v2.ts
+++ b/apps/studio/pages/api/ai/sql/cron-v2.ts
@@ -1,10 +1,9 @@
import { generateObject } from 'ai'
import { source } from 'common-tags'
-import { NextApiRequest, NextApiResponse } from 'next'
-import { z } from 'zod'
-
import { getModel } from 'lib/ai/model'
import apiWrapper from 'lib/api/apiWrapper'
+import { NextApiRequest, NextApiResponse } from 'next'
+import { z } from 'zod'
const cronSchema = z.object({
cron_expression: z.string().describe('The generated cron expression.'),
@@ -34,7 +33,11 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
}
try {
- const { model, error: modelError } = await getModel({
+ const {
+ model,
+ error: modelError,
+ providerOptions,
+ } = await getModel({
provider: 'openai',
routingKey: 'cron',
})
@@ -45,6 +48,7 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
const result = await generateObject({
model,
+ providerOptions,
schema: cronSchema,
prompt: source`
You are a cron syntax expert. Your purpose is to convert natural language time descriptions into valid cron expressions for pg_cron.
diff --git a/apps/studio/pages/api/ai/sql/filter-v1.ts b/apps/studio/pages/api/ai/sql/filter-v1.ts
index 64ddbaca735..9dd6cd2b2ad 100644
--- a/apps/studio/pages/api/ai/sql/filter-v1.ts
+++ b/apps/studio/pages/api/ai/sql/filter-v1.ts
@@ -1,7 +1,5 @@
import { generateObject } from 'ai'
import { source } from 'common-tags'
-import { NextApiRequest, NextApiResponse } from 'next'
-
import { getModel } from 'lib/ai/model'
import apiWrapper from 'lib/api/apiWrapper'
import {
@@ -12,6 +10,7 @@ import {
serializeOptions,
validateFilterGroup,
} from 'lib/api/filterHelpers'
+import { NextApiRequest, NextApiResponse } from 'next'
async function handler(req: NextApiRequest, res: NextApiResponse) {
const { method } = req
@@ -36,7 +35,11 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
const { prompt, filterProperties } = parseResult.data
try {
- const { model, error: modelError } = await getModel({
+ const {
+ model,
+ error: modelError,
+ providerOptions,
+ } = await getModel({
provider: 'openai',
routingKey: 'sql',
})
@@ -61,6 +64,7 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
const result = await generateObject({
model,
+ providerOptions,
schema: filterGroupSchema,
prompt: source`
You are an expert Postgres filter builder. Convert the user's request into structured filters.
diff --git a/apps/studio/pages/api/ai/sql/policy.ts b/apps/studio/pages/api/ai/sql/policy.ts
index d403e9b6b35..8bf086cd2d8 100644
--- a/apps/studio/pages/api/ai/sql/policy.ts
+++ b/apps/studio/pages/api/ai/sql/policy.ts
@@ -1,15 +1,14 @@
-import { Output, generateText, stepCountIs } from 'ai'
+import { generateText, Output, stepCountIs } from 'ai'
import { IS_PLATFORM } from 'common'
import { source } from 'common-tags'
-import { NextApiRequest, NextApiResponse } from 'next'
-import { z } from 'zod'
-
import type { AiOptInLevel } from 'hooks/misc/useOrgOptedIntoAi'
import { getModel } from 'lib/ai/model'
import { getOrgAIDetails } from 'lib/ai/org-ai-details'
import { RLS_PROMPT } from 'lib/ai/prompts'
import { getTools } from 'lib/ai/tools'
import apiWrapper from 'lib/api/apiWrapper'
+import { NextApiRequest, NextApiResponse } from 'next'
+import { z } from 'zod'
const policySchema = z.object({
sql: z.string().describe('The generated Postgres CREATE POLICY statement.'),
@@ -91,7 +90,11 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
}
try {
- const { model, error: modelError } = await getModel({
+ const {
+ model,
+ error: modelError,
+ providerOptions,
+ } = await getModel({
provider: 'openai',
routingKey: 'sql-policy',
})
@@ -110,6 +113,7 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
const { experimental_output } = await generateText({
model,
+ providerOptions,
stopWhen: stepCountIs(5),
prompt: source`
You are a Postgres RLS (Row Level Security) expert.
diff --git a/apps/studio/pages/api/ai/sql/title-v2.ts b/apps/studio/pages/api/ai/sql/title-v2.ts
index ed552d1030e..3c3a3cd6848 100644
--- a/apps/studio/pages/api/ai/sql/title-v2.ts
+++ b/apps/studio/pages/api/ai/sql/title-v2.ts
@@ -1,10 +1,9 @@
import { generateObject } from 'ai'
import { source } from 'common-tags'
-import { NextApiRequest, NextApiResponse } from 'next'
-import { z } from 'zod'
-
import { getModel } from 'lib/ai/model'
import apiWrapper from 'lib/api/apiWrapper'
+import { NextApiRequest, NextApiResponse } from 'next'
+import { z } from 'zod'
const titleSchema = z.object({
title: z
@@ -39,7 +38,11 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
}
try {
- const { model, error: modelError } = await getModel({
+ const {
+ model,
+ error: modelError,
+ providerOptions,
+ } = await getModel({
provider: 'openai',
routingKey: 'sql',
})
@@ -50,6 +53,7 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
const result = await generateObject({
model,
+ providerOptions,
schema: titleSchema,
prompt: source`
Generate a short title and summarized description for this Postgres SQL snippet: