The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
fix(studio): assistant get_active_incidents url in prod (#51047)
Matt Rossman87681812a0b4
diff --git a/apps/studio/lib/ai/tools/incident-tools.test.ts b/apps/studio/lib/ai/tools/incident-tools.test.tsindex c2e0ccee842..f62d3c7a0af 100644--- a/apps/studio/lib/ai/tools/incident-tools.test.ts+++ b/apps/studio/lib/ai/tools/incident-tools.test.ts@@ -170,22 +170,19 @@ describe('ai/tools/incident-tools', () => {expect((result as any).message).toContain('2 active incidents')})- it('should handle fetch errors', async () => {+ it('throws on fetch errors', async () => {const common = await import('common')vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)mockFetch.mockRejectedValue(new Error('Network error'))const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })- const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)-- expect(result).toEqual({- incidents: [],- error: 'Unable to check incident status at this time.',- })+ await expect(+ (tools.get_active_incidents.execute as any)({}, executeOptions)+ ).rejects.toThrow('Network error')})- it('should handle non-ok responses', async () => {+ it('throws on non-ok responses with the status code', async () => {const common = await import('common')vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)@@ -195,12 +192,9 @@ describe('ai/tools/incident-tools', () => {})const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })- const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)-- expect(result).toEqual({- incidents: [],- error: 'Unable to check incident status at this time.',- })+ await expect(+ (tools.get_active_incidents.execute as any)({}, executeOptions)+ ).rejects.toThrow('Failed to fetch incident status: 500')})it('should use timeout signal', async () => {@@ -424,7 +418,7 @@ describe('ai/tools/incident-tools', () => {})})- it('returns an error when the response fails to parse', async () => {+ it('throws when the response fails to parse', async () => {mockFetch.mockResolvedValue({ok: true,json: async () => ({ this_is: 'not a valid status page response' }),@@ -434,15 +428,12 @@ describe('ai/tools/incident-tools', () => {baseUrl: 'https://supabase.com/dashboard',useStatusPageWidget: true,})- const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)-- expect(result).toEqual({- incidents: [],- error: 'Unable to check incident status at this time.',- })+ await expect((tools.get_active_incidents.execute as any)({}, executeOptions)).rejects.toThrow(+ 'Failed to parse status page response'+ )})- it('returns an error on a non-ok response', async () => {+ it('throws on a non-ok response with the status code', async () => {mockFetch.mockResolvedValue({ok: false,status: 500,@@ -452,12 +443,9 @@ describe('ai/tools/incident-tools', () => {baseUrl: 'https://supabase.com/dashboard',useStatusPageWidget: true,})- const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)-- expect(result).toEqual({- incidents: [],- error: 'Unable to check incident status at this time.',- })+ await expect((tools.get_active_incidents.execute as any)({}, executeOptions)).rejects.toThrow(+ 'Failed to fetch status page: 500'+ )})})})diff --git a/apps/studio/lib/ai/tools/incident-tools.ts b/apps/studio/lib/ai/tools/incident-tools.tsindex ffb8e0e0acb..d9e7a9cffbb 100644--- a/apps/studio/lib/ai/tools/incident-tools.ts+++ b/apps/studio/lib/ai/tools/incident-tools.ts@@ -8,10 +8,6 @@ import {type AffectedComponent,} from '@/lib/status-page/status-page.schema'-const INCIDENT_STATUS_ERROR = {- incidents: [],- error: 'Unable to check incident status at this time.',-}const NO_ACTIVE_INCIDENTS_MESSAGE = 'No active incidents.'const DEFAULT_STATUS_PAGE_URL = 'https://status.supabase.com'@@ -74,108 +70,95 @@ export const getIncidentTools = ({}if (useStatusPageWidget) {- try {- const response = await fetch(`${baseUrl}/api/status-page`, {- signal: timeoutSignal(abortSignal),- })-- if (!response.ok) {- console.warn('Failed to fetch status page:', response.status)- return INCIDENT_STATUS_ERROR- }-- const json = await response.json()- const parseResult = StatusPageResponseSchema.safeParse(json)-- if (!parseResult.success) {- console.warn('Failed to parse status page response:', parseResult.error)- return INCIDENT_STATUS_ERROR- }-- const { data } = parseResult-- const incidents = data.ongoing_incidents- .filter((incident) => incident.visible)- .map((incident) => ({- kind: 'incident' as const,- name: incident.name,- status: incident.status,- impact: incident.current_worst_impact,- affected_components: formatAffectedComponents(incident.affected_components),- last_update_message: incident.last_update_message ?? null,- url: incident.url,- }))-- const maintenances = data.in_progress_maintenances- .filter((maintenance) => maintenance.visible)- .map((maintenance) => ({- kind: 'maintenance' as const,- name: maintenance.name,- status: maintenance.status,- affected_components: formatAffectedComponents(maintenance.affected_components),- last_update_message: maintenance.last_update_message ?? null,- url: maintenance.url,- started_at: maintenance.started_at,- }))-- const combined = [...incidents, ...maintenances]-- if (combined.length === 0) {- return {- incidents: [],- message: NO_ACTIVE_INCIDENTS_MESSAGE,- }- }-- const statusPageUrl = data.page_url || DEFAULT_STATUS_PAGE_URL-- return {- incidents: combined,- message: buildActiveIncidentsMessage(- incidents.length,- maintenances.length,- statusPageUrl- ),- }- } catch (error) {- console.warn('Failed to fetch status page:', error)- return INCIDENT_STATUS_ERROR- }- }-- try {- const response = await fetch(`${baseUrl}/api/incident-status`, {+ const response = await fetch(`${baseUrl}/api/status-page`, {signal: timeoutSignal(abortSignal),})if (!response.ok) {- console.warn('Failed to fetch incident status:', response.status)- return INCIDENT_STATUS_ERROR+ throw new Error(`Failed to fetch status page: ${response.status}`)}- const incidents: IncidentInfo[] = await response.json()+ const json = await response.json()+ const parseResult = StatusPageResponseSchema.safeParse(json)- if (incidents.length === 0) {+ if (!parseResult.success) {+ throw new Error(`Failed to parse status page response: ${parseResult.error.message}`)+ }++ const { data } = parseResult++ const incidents = data.ongoing_incidents+ .filter((incident) => incident.visible)+ .map((incident) => ({+ kind: 'incident' as const,+ name: incident.name,+ status: incident.status,+ impact: incident.current_worst_impact,+ affected_components: formatAffectedComponents(incident.affected_components),+ last_update_message: incident.last_update_message ?? null,+ url: incident.url,+ }))++ const maintenances = data.in_progress_maintenances+ .filter((maintenance) => maintenance.visible)+ .map((maintenance) => ({+ kind: 'maintenance' as const,+ name: maintenance.name,+ status: maintenance.status,+ affected_components: formatAffectedComponents(maintenance.affected_components),+ last_update_message: maintenance.last_update_message ?? null,+ url: maintenance.url,+ started_at: maintenance.started_at,+ }))++ const combined = [...incidents, ...maintenances]++ if (combined.length === 0) {return {incidents: [],message: NO_ACTIVE_INCIDENTS_MESSAGE,}}- const incidentSummaries = incidents.map((incident) => ({- name: incident.name,- status: incident.status,- impact: incident.impact,- active_since: incident.active_since,- }))+ const statusPageUrl = data.page_url || DEFAULT_STATUS_PAGE_URL++ return {+ incidents: combined,+ message: buildActiveIncidentsMessage(+ incidents.length,+ maintenances.length,+ statusPageUrl+ ),+ }+ }++ const response = await fetch(`${baseUrl}/api/incident-status`, {+ signal: timeoutSignal(abortSignal),+ })+ if (!response.ok) {+ throw new Error(`Failed to fetch incident status: ${response.status}`)+ }++ const incidents: IncidentInfo[] = await response.json()++ if (incidents.length === 0) {return {- incidents: incidentSummaries,- message: buildActiveIncidentsMessage(incidents.length, 0, DEFAULT_STATUS_PAGE_URL),+ incidents: [],+ message: NO_ACTIVE_INCIDENTS_MESSAGE,}- } catch (error) {- console.warn('Failed to fetch incident status:', error)- return INCIDENT_STATUS_ERROR+ }++ const incidentSummaries = incidents.map((incident) => ({+ name: incident.name,+ status: incident.status,+ impact: incident.impact,+ active_since: incident.active_since,+ }))++ return {+ incidents: incidentSummaries,+ message: buildActiveIncidentsMessage(incidents.length, 0, DEFAULT_STATUS_PAGE_URL),}},}),diff --git a/apps/studio/lib/helpers.test.ts b/apps/studio/lib/helpers.test.tsindex cd867453c81..c800524d7c2 100644--- a/apps/studio/lib/helpers.test.ts+++ b/apps/studio/lib/helpers.test.ts@@ -10,6 +10,7 @@ import {formatBytes,formatCurrency,formatRestoreWindow,+ getBasePathURL,getDatabaseMajorVersion,getDistanceLatLonKM,getSemanticVersion,@@ -107,6 +108,33 @@ describe('getURL', () => {})})+describe('getBasePathURL', () => {+ afterEach(() => {+ vi.unstubAllEnvs()+ })++ it('appends the base path to the site URL', () => {+ vi.stubEnv('NEXT_PUBLIC_SITE_URL', 'https://supabase.com')+ expect(getBasePathURL('/dashboard')).toEqual('https://supabase.com/dashboard')+ })++ it('does not double the base path on the fallback URL', () => {+ vi.stubEnv('NEXT_PUBLIC_SITE_URL', '')+ vi.stubEnv('NEXT_PUBLIC_VERCEL_BRANCH_URL', '')+ expect(getBasePathURL('/dashboard')).toEqual('https://supabase.com/dashboard')+ })++ it('strips a trailing slash before appending the base path', () => {+ vi.stubEnv('NEXT_PUBLIC_SITE_URL', 'https://supabase.com/')+ expect(getBasePathURL('/dashboard')).toEqual('https://supabase.com/dashboard')+ })++ it('returns the site URL when there is no base path', () => {+ vi.stubEnv('NEXT_PUBLIC_SITE_URL', 'http://localhost:8082')+ expect(getBasePathURL('')).toEqual('http://localhost:8082')+ })+})+describe('makeRandomString', () => {it('should return a random string of the given length', () => {const result = makeRandomString(10)diff --git a/apps/studio/lib/helpers.ts b/apps/studio/lib/helpers.tsindex 2206be48975..bf6c58c60b7 100644--- a/apps/studio/lib/helpers.ts+++ b/apps/studio/lib/helpers.ts@@ -2,6 +2,7 @@ import { UIEvent } from 'react'import { v4 as _uuidV4 } from 'uuid'import type { TablesData } from '../data/tables/tables-query'+import { BASE_PATH } from './constants'export const uuidv4 = () => {return _uuidV4()@@ -73,6 +74,16 @@ export const getURL = () => {return url.includes('http') ? url : `https://${url}`}+/**+ * Adds the base path to `getURL()` when it's missing.+ * `https://supabase.com` → `https://supabase.com/dashboard`+ * `https://supabase.com/dashboard` → unchanged+ */+export const getBasePathURL = (basePath = BASE_PATH) => {+ const url = getURL().replace(/\/$/, '')+ return url.endsWith(basePath) ? url : `${url}${basePath}`+}+/*** Generates a random string using alpha characters*/diff --git a/apps/studio/pages/api/ai/sql/generate-v4.ts b/apps/studio/pages/api/ai/sql/generate-v4.tsindex f0e4fe3998e..67209870b15 100644--- a/apps/studio/pages/api/ai/sql/generate-v4.ts+++ b/apps/studio/pages/api/ai/sql/generate-v4.ts@@ -30,7 +30,7 @@ import { getTools } from '@/lib/ai/tools'import { encodeNotebookToolError } from '@/lib/ai/tools/notebook-tools'import { apiWrapper } from '@/lib/api/apiWrapper'import { executeQuery } from '@/lib/api/self-hosted/query'-import { getURL } from '@/lib/helpers'+import { getBasePathURL } from '@/lib/helpers'import { isServerFlagEnabled, trustedUserEmail } from '@/lib/server/configcat'export const maxDuration = 300@@ -192,7 +192,7 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse, claims?: Jwauthorization,aiOptInLevel,accessToken,- baseUrl: getURL(),+ baseUrl: getBasePathURL(),supportMode,isExplorerEnabled: explorerEnabled,useStatusPageWidget,