Files
supabase/apps/studio/lib/ai/tools/tool-from-contract.test.ts
Charis 51b7cd6a4b refactor(studio): declare Assistant tool availability on contracts (#52396)
## 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)
2026-10-27 18:06:55 +00:00

56 lines
1.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { z } from 'zod'
import { toolFromContract } from './tool-from-contract'
import type { ToolContract } from './types'
describe('toolFromContract createInputSchema', () => {
const base = {
name: 'probe',
description: 'A probe',
outputSchema: z.object({}),
minOptInLevel: null,
availability: { environments: new Set(['platform', 'self-hosted'] as const) },
needsApproval: false,
toModelOutput: () => ({}),
} as const
it('parses with createInputSchema when the contract has one', () => {
const perRequest = z.object({}).transform(() => ({ level: 'schema_and_log' }))
const probe = {
...base,
inputSchema: z.object({ level: z.string() }),
createInputSchema: () => perRequest,
} satisfies ToolContract<{ level: string }, object>
expect(toolFromContract(probe, { aiOptInLevel: 'schema' }, async () => ({})).inputSchema).toBe(
perRequest
)
})
it('passes the context to createInputSchema', () => {
let received: unknown
const probe = {
...base,
inputSchema: z.object({}),
createInputSchema: (ctx) => {
received = ctx
return z.object({})
},
} satisfies ToolContract<object, object>
const ctx = { projectRef: 'ref', aiOptInLevel: 'schema' } as const
toolFromContract(probe, ctx, async () => ({}))
expect(received).toBe(ctx)
})
it('parses with inputSchema otherwise', () => {
const inputSchema = z.object({})
const probe = { ...base, inputSchema } satisfies ToolContract<object, object>
expect(toolFromContract(probe, { aiOptInLevel: 'schema' }, async () => ({})).inputSchema).toBe(
inputSchema
)
})
})