mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 00:19:08 +00:00
## What
Assistant tool contracts now declare where they're available, and
`getTools` builds tools from the registry.
- `ToolContract` and `RemoteToolContract` gain `availability: {
environments, requires? }`, both read-only sets. `environments` is
`platform` and/or `self-hosted`; `requires` is any of `explorer`,
`supportMode`, `baseUrl`.
- New `tools/registry.server.ts` maps every contract to its factory in
`TOOL_FACTORIES`; its type makes a missing or mismatched factory a
compile error. `buildLocalTools` builds only the tools available for a
request.
- `getTools` no longer branches per tool module. Local contract tools
take precedence over remote MCP tools with the same name. The
self-hosted fallback tools are unchanged for now.
- Deleted the per-module getters (`studio-tools.ts`, `schema-tools.ts`,
`report-tools.ts`, `notebook-tools.ts`, `incident-tools.ts`,
`support-tools.ts`); their tests now call the factories directly.
## Behavior changes
- Self-hosted no longer offers `deploy_edge_function`. Its server half
calls `/v1/projects/{ref}/functions/deploy`, which self-hosted Studio
doesn't serve.
- Self-hosted no longer offers the support tools (`escalate_to_human`,
`resolve_support_conversation`) in support mode.
- Platform offers `list_databases` without the Explorer flag.
Everything else produces the same tool set as before.
## Tests
- `index.availability.test.ts` asserts the exact tool names for every
combination of environment, Explorer, support mode, and `baseUrl`.
- `registry.test.ts` checks registry invariants: keys match contract
names, every contract has a factory, and a name in both the local and
remote maps has disjoint environments and matching gates.
This is the first step in moving the self-hosted Assistant tools to
contracts.
## Checklist
Check all before review:
- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
324 lines
11 KiB
TypeScript
324 lines
11 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { createTool as createGetActiveIncidentsTool } from './get-active-incidents.server'
|
|
import type { ToolContext } from './types'
|
|
|
|
// Mock IS_PLATFORM
|
|
vi.mock('common', () => ({
|
|
IS_PLATFORM: true,
|
|
}))
|
|
|
|
const executeOptions = { toolCallId: 'test', messages: [], context: {} }
|
|
|
|
function statusPagePayload(overrides: Record<string, unknown> = {}) {
|
|
return {
|
|
page_title: 'Supabase',
|
|
page_url: 'https://status.supabase.com',
|
|
ongoing_incidents: [],
|
|
in_progress_maintenances: [],
|
|
scheduled_maintenances: [],
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
const createIncidentTools = (ctx: ToolContext) => ({
|
|
get_active_incidents: createGetActiveIncidentsTool(ctx),
|
|
})
|
|
|
|
describe('ai/tools/get-active-incidents', () => {
|
|
let mockFetch: ReturnType<typeof vi.fn>
|
|
let mockAbortSignal: AbortSignal
|
|
|
|
beforeEach(() => {
|
|
mockFetch = vi.fn()
|
|
global.fetch = mockFetch as typeof fetch
|
|
|
|
// Mock AbortSignal.timeout
|
|
mockAbortSignal = new AbortController().signal
|
|
if (!AbortSignal.timeout) {
|
|
AbortSignal.timeout = vi.fn(() => mockAbortSignal) as any
|
|
}
|
|
})
|
|
|
|
describe('incident tools', () => {
|
|
describe('execute function', () => {
|
|
it('should return empty incidents when not on platform', async () => {
|
|
const common = await import('common')
|
|
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(false)
|
|
|
|
const tools = createIncidentTools({
|
|
baseUrl: 'https://supabase.com/dashboard',
|
|
aiOptInLevel: 'schema',
|
|
})
|
|
const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)
|
|
|
|
expect(result).toEqual({
|
|
incidents: [],
|
|
message: 'Incident checking is only available on Supabase platform.',
|
|
})
|
|
expect(mockFetch).not.toHaveBeenCalled()
|
|
})
|
|
|
|
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 = createIncidentTools({
|
|
baseUrl: 'https://supabase.com/dashboard',
|
|
aiOptInLevel: 'schema',
|
|
})
|
|
await expect(
|
|
(tools.get_active_incidents.execute as any)({}, executeOptions)
|
|
).rejects.toThrow('Network error')
|
|
})
|
|
|
|
it('should use timeout signal', async () => {
|
|
const common = await import('common')
|
|
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)
|
|
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: async () => statusPagePayload(),
|
|
})
|
|
|
|
const tools = createIncidentTools({
|
|
baseUrl: 'https://supabase.com/dashboard',
|
|
aiOptInLevel: 'schema',
|
|
})
|
|
if (!tools.get_active_incidents.execute) throw new Error('execute is undefined')
|
|
await tools.get_active_incidents.execute(
|
|
{},
|
|
{ toolCallId: 'test', messages: [], context: {} }
|
|
)
|
|
|
|
const callArgs = mockFetch.mock.calls[0]
|
|
expect(callArgs[1].signal).toBeInstanceOf(AbortSignal)
|
|
})
|
|
|
|
it('cancels the request when the Assistant request is aborted', async () => {
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: async () => statusPagePayload(),
|
|
})
|
|
const abortController = new AbortController()
|
|
|
|
const tools = createIncidentTools({
|
|
baseUrl: 'https://supabase.com/dashboard',
|
|
aiOptInLevel: 'schema',
|
|
})
|
|
if (!tools.get_active_incidents.execute) throw new Error('execute is undefined')
|
|
await tools.get_active_incidents.execute(
|
|
{},
|
|
{ ...executeOptions, abortSignal: abortController.signal }
|
|
)
|
|
|
|
const { signal } = mockFetch.mock.calls[0][1]
|
|
expect(signal.aborted).toBe(false)
|
|
abortController.abort()
|
|
expect(signal.aborted).toBe(true)
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('status page response', () => {
|
|
beforeEach(async () => {
|
|
const common = await import('common')
|
|
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)
|
|
})
|
|
|
|
const baseIncident = {
|
|
id: 'incident-1',
|
|
name: 'Elevated error rates',
|
|
url: 'https://status.supabase.com/incidents/incident-1',
|
|
last_update_at: '2026-01-01T00:00:00Z',
|
|
last_update_message: 'We are investigating.',
|
|
affected_components: [],
|
|
status: 'investigating',
|
|
current_worst_impact: 'partial_outage',
|
|
}
|
|
|
|
const baseMaintenance = {
|
|
id: 'maintenance-1',
|
|
name: 'Scheduled database maintenance',
|
|
url: 'https://status.supabase.com/maintenances/maintenance-1',
|
|
last_update_at: '2026-01-01T00:00:00Z',
|
|
last_update_message: null,
|
|
affected_components: [],
|
|
status: 'maintenance_in_progress',
|
|
started_at: '2026-01-01T00:00:00Z',
|
|
}
|
|
|
|
it('fetches from the status page endpoint', async () => {
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: async () => statusPagePayload(),
|
|
})
|
|
|
|
const tools = createIncidentTools({
|
|
baseUrl: 'https://example.com/dashboard',
|
|
aiOptInLevel: 'schema',
|
|
})
|
|
if (!tools.get_active_incidents.execute) throw new Error('execute is undefined')
|
|
await tools.get_active_incidents.execute({}, executeOptions)
|
|
|
|
expect(mockFetch).toHaveBeenCalledWith('https://example.com/dashboard/api/status-page', {
|
|
signal: expect.any(AbortSignal),
|
|
})
|
|
})
|
|
|
|
it('drops incidents and maintenances where visible is false', async () => {
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: async () =>
|
|
statusPagePayload({
|
|
ongoing_incidents: [
|
|
{ ...baseIncident, visible: false, show_banner: false },
|
|
{ ...baseIncident, id: 'incident-2', visible: true, show_banner: true },
|
|
],
|
|
in_progress_maintenances: [{ ...baseMaintenance, visible: false, show_banner: false }],
|
|
}),
|
|
})
|
|
|
|
const tools = createIncidentTools({
|
|
baseUrl: 'https://supabase.com/dashboard',
|
|
aiOptInLevel: 'schema',
|
|
})
|
|
const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)
|
|
|
|
expect((result as any).incidents).toHaveLength(1)
|
|
expect((result as any).incidents[0]).toMatchObject({
|
|
kind: 'incident',
|
|
name: 'Elevated error rates',
|
|
})
|
|
})
|
|
|
|
it('includes items with show_banner set to false', async () => {
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: async () =>
|
|
statusPagePayload({
|
|
ongoing_incidents: [{ ...baseIncident, visible: true, show_banner: false }],
|
|
}),
|
|
})
|
|
|
|
const tools = createIncidentTools({
|
|
baseUrl: 'https://supabase.com/dashboard',
|
|
aiOptInLevel: 'schema',
|
|
})
|
|
const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)
|
|
|
|
expect((result as any).incidents).toHaveLength(1)
|
|
})
|
|
|
|
it('combines ongoing incidents and in-progress maintenances, excluding scheduled maintenances', async () => {
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: async () =>
|
|
statusPagePayload({
|
|
ongoing_incidents: [{ ...baseIncident, visible: true, show_banner: true }],
|
|
in_progress_maintenances: [{ ...baseMaintenance, visible: true, show_banner: true }],
|
|
scheduled_maintenances: [
|
|
{
|
|
id: 'scheduled-1',
|
|
name: 'Upcoming maintenance',
|
|
url: 'https://status.supabase.com/maintenances/scheduled-1',
|
|
last_update_at: '2026-01-01T00:00:00Z',
|
|
last_update_message: null,
|
|
affected_components: [],
|
|
status: 'maintenance_scheduled',
|
|
starts_at: '2026-02-01T00:00:00Z',
|
|
ends_at: '2026-02-01T01:00:00Z',
|
|
visible: true,
|
|
show_banner: true,
|
|
banner_lead_days: 1,
|
|
},
|
|
],
|
|
}),
|
|
})
|
|
|
|
const tools = createIncidentTools({
|
|
baseUrl: 'https://supabase.com/dashboard',
|
|
aiOptInLevel: 'schema',
|
|
})
|
|
const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)
|
|
|
|
expect((result as any).incidents).toHaveLength(2)
|
|
expect((result as any).incidents.map((incident: any) => incident.kind).sort()).toEqual([
|
|
'incident',
|
|
'maintenance',
|
|
])
|
|
expect((result as any).message).toContain('status.supabase.com')
|
|
})
|
|
|
|
it('describes maintenance-only results as maintenance, not as an incident', async () => {
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: async () =>
|
|
statusPagePayload({
|
|
in_progress_maintenances: [{ ...baseMaintenance, visible: true, show_banner: true }],
|
|
}),
|
|
})
|
|
|
|
const tools = createIncidentTools({
|
|
baseUrl: 'https://supabase.com/dashboard',
|
|
aiOptInLevel: 'schema',
|
|
})
|
|
const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)
|
|
|
|
expect((result as any).incidents).toHaveLength(1)
|
|
expect((result as any).message).toContain('1 in-progress maintenance')
|
|
expect((result as any).message).not.toContain('active incident')
|
|
})
|
|
|
|
it('returns the no-active-incidents message when nothing is visible', async () => {
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: async () => statusPagePayload(),
|
|
})
|
|
|
|
const tools = createIncidentTools({
|
|
baseUrl: 'https://supabase.com/dashboard',
|
|
aiOptInLevel: 'schema',
|
|
})
|
|
const result = await (tools.get_active_incidents.execute as any)({}, executeOptions)
|
|
|
|
expect(result).toEqual({
|
|
incidents: [],
|
|
message: expect.stringContaining('No active incidents'),
|
|
})
|
|
})
|
|
|
|
it('throws when the response fails to parse', async () => {
|
|
mockFetch.mockResolvedValue({
|
|
ok: true,
|
|
json: async () => ({ this_is: 'not a valid status page response' }),
|
|
})
|
|
|
|
const tools = createIncidentTools({
|
|
baseUrl: 'https://supabase.com/dashboard',
|
|
aiOptInLevel: 'schema',
|
|
})
|
|
await expect((tools.get_active_incidents.execute as any)({}, executeOptions)).rejects.toThrow(
|
|
'Failed to parse status page response'
|
|
)
|
|
})
|
|
|
|
it('throws on a non-ok response with the status code', async () => {
|
|
mockFetch.mockResolvedValue({
|
|
ok: false,
|
|
status: 500,
|
|
})
|
|
|
|
const tools = createIncidentTools({
|
|
baseUrl: 'https://supabase.com/dashboard',
|
|
aiOptInLevel: 'schema',
|
|
})
|
|
await expect((tools.get_active_incidents.execute as any)({}, executeOptions)).rejects.toThrow(
|
|
'Failed to fetch status page: 500'
|
|
)
|
|
})
|
|
})
|
|
})
|