Skip to content

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

fix(studio): bypass pg meta route on self-hosted for ai endpoints (#35812)

Alaister Youngc0c3710bde55
diff --git a/apps/studio/data/sql/execute-sql-query.ts b/apps/studio/data/sql/execute-sql-query.ts
index b172ab00514..8e2b1536721 100644
--- a/apps/studio/data/sql/execute-sql-query.ts
+++ b/apps/studio/data/sql/execute-sql-query.ts
@@ -39,7 +39,11 @@ export async function executeSql<T = any>(
| 'isRoleImpersonationEnabled'
>,
signal?: AbortSignal,
- headersInit?: HeadersInit
+ headersInit?: HeadersInit,
+ fetcherOverride?: (
+ sql: string,
+ headers?: HeadersInit
+ ) => Promise<{ data: T } | { error: ResponseError }>
): Promise<{ result: T }> {
if (!projectRef) throw new Error('projectRef is required')
@@ -52,21 +56,37 @@ export async function executeSql<T = any>(
let headers = new Headers(headersInit)
if (connectionString) headers.set('x-connection-encrypted', connectionString)
- let { data, error } = await post('/platform/pg-meta/{ref}/query', {
- signal,
- params: {
- header: { 'x-connection-encrypted': connectionString ?? '' },
- path: { ref: projectRef },
- // @ts-expect-error: This is just a client side thing to identify queries better
- query: {
- key:
- queryKey?.filter((seg) => typeof seg === 'string' || typeof seg === 'number').join('-') ??
- '',
+ let data
+ let error
+
+ if (fetcherOverride) {
+ const result = await fetcherOverride(sql, headers)
+ if ('data' in result) {
+ data = result.data
+ } else {
+ error = result.error
+ }
+ } else {
+ const result = await post('/platform/pg-meta/{ref}/query', {
+ signal,
+ params: {
+ header: { 'x-connection-encrypted': connectionString ?? '' },
+ path: { ref: projectRef },
+ // @ts-expect-error: This is just a client side thing to identify queries better
+ query: {
+ key:
+ queryKey
+ ?.filter((seg) => typeof seg === 'string' || typeof seg === 'number')
+ .join('-') ?? '',
+ },
},
- },
- body: { query: sql },
- headers,
- })
+ body: { query: sql },
+ headers,
+ })
+
+ data = result.data
+ error = result.error
+ }
if (error) {
if (
diff --git a/apps/studio/lib/self-hosted.ts b/apps/studio/lib/self-hosted.ts
new file mode 100644
index 00000000000..a87ca513c38
--- /dev/null
+++ b/apps/studio/lib/self-hosted.ts
@@ -0,0 +1,15 @@
+import { constructHeaders } from 'lib/api/apiHelpers'
+import { post } from 'lib/common/fetch'
+import { PG_META_URL } from 'lib/constants'
+import type { ResponseError } from 'types'
+
+export async function queryPgMetaSelfHosted(sql: string, headersInit?: { [prop: string]: any }) {
+ const headers = constructHeaders(headersInit ?? {})
+ const response = await post(`${PG_META_URL}/query`, { query: sql }, { headers })
+
+ if (response.error) {
+ return { error: response.error as ResponseError }
+ } else {
+ return { data: response.data }
+ }
+}
diff --git a/apps/studio/pages/api/ai/edge-function/complete.ts b/apps/studio/pages/api/ai/edge-function/complete.ts
index 193c3643fd8..4d6096da5f9 100644
--- a/apps/studio/pages/api/ai/edge-function/complete.ts
+++ b/apps/studio/pages/api/ai/edge-function/complete.ts
@@ -1,8 +1,10 @@
import { openai } from '@ai-sdk/openai'
import pgMeta from '@supabase/pg-meta'
import { streamText } from 'ai'
+import { IS_PLATFORM } from 'common'
import { executeSql } from 'data/sql/execute-sql-query'
import apiWrapper from 'lib/api/apiWrapper'
+import { queryPgMetaSelfHosted } from 'lib/self-hosted'
import { NextApiRequest, NextApiResponse } from 'next'
import { getTools } from '../sql/tools'
@@ -65,7 +67,8 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse) {
{
'Content-Type': 'application/json',
...(authorization && { Authorization: authorization }),
- }
+ },
+ IS_PLATFORM ? undefined : queryPgMetaSelfHosted
)
: { result: [] }
diff --git a/apps/studio/pages/api/ai/sql/complete.ts b/apps/studio/pages/api/ai/sql/complete.ts
index 9f51e9f2ff3..569831095e5 100644
--- a/apps/studio/pages/api/ai/sql/complete.ts
+++ b/apps/studio/pages/api/ai/sql/complete.ts
@@ -1,8 +1,10 @@
import { openai } from '@ai-sdk/openai'
import pgMeta from '@supabase/pg-meta'
import { streamText } from 'ai'
+import { IS_PLATFORM } from 'common'
import { executeSql } from 'data/sql/execute-sql-query'
import apiWrapper from 'lib/api/apiWrapper'
+import { queryPgMetaSelfHosted } from 'lib/self-hosted'
import { NextApiRequest, NextApiResponse } from 'next'
import { getTools } from '../sql/tools'
@@ -56,7 +58,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
{
'Content-Type': 'application/json',
...(authorization && { Authorization: authorization }),
- }
+ },
+ IS_PLATFORM ? undefined : queryPgMetaSelfHosted
)
: { result: [] }
diff --git a/apps/studio/pages/api/ai/sql/generate-v3.ts b/apps/studio/pages/api/ai/sql/generate-v3.ts
index cbc45514430..9000ab3d469 100644
--- a/apps/studio/pages/api/ai/sql/generate-v3.ts
+++ b/apps/studio/pages/api/ai/sql/generate-v3.ts
@@ -3,8 +3,10 @@ import pgMeta from '@supabase/pg-meta'
import { streamText } from 'ai'
import { NextApiRequest, NextApiResponse } from 'next'
+import { IS_PLATFORM } from 'common'
import { executeSql } from 'data/sql/execute-sql-query'
import apiWrapper from 'lib/api/apiWrapper'
+import { queryPgMetaSelfHosted } from 'lib/self-hosted'
import { getTools } from './tools'
export const maxDuration = 30
@@ -59,7 +61,8 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse) {
'Content-Type': 'application/json',
...(cookie && { cookie }),
...(authorization && { Authorization: authorization }),
- }
+ },
+ IS_PLATFORM ? undefined : queryPgMetaSelfHosted
)
: { result: [] }
diff --git a/apps/studio/pages/api/ai/sql/tools.ts b/apps/studio/pages/api/ai/sql/tools.ts
index 77fbf78abda..54ea64f33a7 100644
--- a/apps/studio/pages/api/ai/sql/tools.ts
+++ b/apps/studio/pages/api/ai/sql/tools.ts
@@ -3,10 +3,12 @@ import { stripIndent } from 'common-tags'
import { z } from 'zod'
// import { processSql, renderSupabaseJs } from '@supabase/sql-to-rest'
+import { IS_PLATFORM } from 'common'
import { getDatabaseFunctions } from 'data/database-functions/database-functions-query'
import { getDatabasePolicies } from 'data/database-policies/database-policies-query'
import { getEntityDefinitionsSql } from 'data/database/entity-definitions-query'
import { executeSql } from 'data/sql/execute-sql-query'
+import { queryPgMetaSelfHosted } from 'lib/self-hosted'
export const getTools = ({
projectRef,
@@ -43,7 +45,8 @@ export const getTools = ({
sql: getEntityDefinitionsSql({ schemas }),
},
undefined,
- headers
+ headers,
+ IS_PLATFORM ? undefined : queryPgMetaSelfHosted
)
: { result: [] }