Files
supabase/apps/studio/lib/ai/tools/incident-tools.test.ts
Saxon Fletcher c80f8ad78d chore(studio): upgrade AI SDK to v7 (#49167)
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

## What kind of change does this PR introduce?

Chore / dependency upgrade.

## What is the current behavior?

Studio is on AI SDK 6 (`ai` ^6.0.174, `@ai-sdk/react` ^3). Tool
approvals still use the v6 `needsApproval` flag on individual tools.

## What is the new behavior?

Upgrades Studio to AI SDK 7 (`ai` 7.0.59) and the matching `@ai-sdk/*`
packages. Aligns call sites with v7 names (`instructions`,
`isStepCount`, `onEnd`, `ToolExecutionOptions`).

This is the bottom of stack #49171. Later layers add a shared Confirm
card and AssistantQueryCell.

## Additional context

- Stack: #49167 → #49168 → #49169 → #49170
- `needsApproval` on tools is left as-is in this PR so the upgrade can
land independently. A follow-up can move those gates to `streamText({
toolApproval })` and `experimental_toolApprovalSecret`.
- Independent of the notebook preview stack
([#49112](https://github.com/supabase/supabase/pull/49112),
[#49159](https://github.com/supabase/supabase/pull/49159)), which should
merge first before we wrap notebook proposals in Confirm.

## Test plan

- [ ] `pnpm --filter studio test` for `lib/ai/tools/*` and assistant
generate path
- [ ] Assistant chat still streams and tool-approval SQL / Edge Function
still pause for confirm
- [ ] Evals still run with mock tools (`needsApproval: false` overrides)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Improvements**
* Updated AI-powered chat, onboarding, SQL, code completion, and recipe
generation workflows for more reliable responses.
* Streaming responses now better preserve reasoning and source
information where available.
* Improved tool privacy notices while preserving dynamically generated
tool descriptions.
* Refined AI response handling, including step limits and structured
policy results.
* **Bug Fixes**
* Improved compatibility across AI-powered tool interactions and
execution scenarios.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-18 17:04:36 +08:00

227 lines
7.4 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
import { getIncidentTools } from './incident-tools'
// Mock IS_PLATFORM
vi.mock('common', () => ({
IS_PLATFORM: true,
}))
describe('ai/tools/incident-tools', () => {
let mockFetch: ReturnType<typeof vi.fn>
let mockAbortSignal: AbortSignal
beforeEach(() => {
vi.clearAllMocks()
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('getIncidentTools', () => {
it('should return an object with get_active_incidents tool', () => {
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
expect(tools).toBeDefined()
expect(tools.get_active_incidents).toBeDefined()
})
it('should have correct description for get_active_incidents', () => {
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
expect(tools.get_active_incidents.description).toContain('Check for active incidents')
expect(tools.get_active_incidents.description).toContain('Supabase service')
})
it('should have empty input schema', () => {
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
const schema = tools.get_active_incidents.inputSchema
// The schema is a Zod object that accepts empty object
expect(schema).toBeDefined()
expect((schema as any)._def.typeName).toBe('ZodObject')
})
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 = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
const result = await (tools.get_active_incidents.execute as any)({})
expect(result).toEqual({
incidents: [],
message: 'Incident checking is only available on Supabase platform.',
})
expect(mockFetch).not.toHaveBeenCalled()
})
it('should fetch incidents from correct URL', async () => {
const common = await import('common')
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)
mockFetch.mockResolvedValue({
ok: true,
json: async () => [],
})
const tools = getIncidentTools({ baseUrl: 'https://example.com/dashboard' })
if (!tools.get_active_incidents.execute) throw new Error('execute is undefined')
await tools.get_active_incidents.execute(
{},
{ toolCallId: 'test', messages: [], context: {} }
)
expect(mockFetch).toHaveBeenCalledWith(
'https://example.com/dashboard/api/incident-status',
{
signal: expect.any(AbortSignal),
}
)
})
it('should return message when no incidents', async () => {
const common = await import('common')
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)
mockFetch.mockResolvedValue({
ok: true,
json: async () => [],
})
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
const result = await (tools.get_active_incidents.execute as any)({})
expect(result).toEqual({
incidents: [],
message: expect.stringContaining('No active incidents'),
})
})
it('should return incident summaries when incidents exist', async () => {
const common = await import('common')
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)
const mockIncidents = [
{
name: 'Database slowness',
status: 'investigating',
impact: 'minor',
active_since: '2024-01-01T10:00:00Z',
extra_field: 'should be filtered',
},
]
mockFetch.mockResolvedValue({
ok: true,
json: async () => mockIncidents,
})
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
const result = await (tools.get_active_incidents.execute as any)({})
expect((result as any).incidents).toEqual([
{
name: 'Database slowness',
status: 'investigating',
impact: 'minor',
active_since: '2024-01-01T10:00:00Z',
},
])
expect((result as any).message).toContain('1 active incident')
expect((result as any).message).toContain('status.supabase.com')
})
it('should handle multiple incidents', async () => {
const common = await import('common')
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)
const mockIncidents = [
{
name: 'Database issue',
status: 'investigating',
impact: 'major',
active_since: '2024-01-01T10:00:00Z',
},
{
name: 'Storage issue',
status: 'identified',
impact: 'minor',
active_since: '2024-01-01T11:00:00Z',
},
]
mockFetch.mockResolvedValue({
ok: true,
json: async () => mockIncidents,
})
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
const result = await (tools.get_active_incidents.execute as any)({})
expect((result as any).incidents).toHaveLength(2)
expect((result as any).message).toContain('2 active incidents')
})
it('should handle 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)({})
expect(result).toEqual({
incidents: [],
error: 'Unable to check incident status at this time.',
})
})
it('should handle non-ok responses', async () => {
const common = await import('common')
vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true)
mockFetch.mockResolvedValue({
ok: false,
status: 500,
})
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
const result = await (tools.get_active_incidents.execute as any)({})
expect(result).toEqual({
incidents: [],
error: 'Unable to check incident status at this time.',
})
})
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 () => [],
})
const tools = getIncidentTools({ baseUrl: 'https://supabase.com/dashboard' })
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)
})
})
})
})