Files
supabase/apps/studio/lib/ai/tools/list-notebooks.server.ts
Charis 2d79674ab3 refactor: migrate list_databases, list_notebooks, and get_notebook to tool contracts (#51937)
## Problem

The read-only notebook tools (`list_databases`, `list_notebooks`,
`get_notebook`) are still defined inline with `tool({...})`. The other
AI tools have moved to the tool contract pattern, so these three are
inconsistent with the rest.

## Solution

Migrate the three tools to the tool contract pattern: a contract file
plus a `.server.ts` implementation, registered in `TOOL_CONTRACTS`. They
are removed from `TOOL_CATEGORY_MAP` and kept in the allowlist.

Behavior is unchanged:

- `minOptInLevel: 'schema'`, same as before.
- `needsApproval: false`.
- `toModelOutput` returns the output unchanged, so the model sees the
same data at every opt-in level.

Stored-shape changes for replayed outputs:

- Replayed outputs are now validated against the output schema and
dropped if invalid. Previously they passed through unchecked.
- Zod strips unknown keys, so fields outside the schema no longer
replay.
- `status`, `region`, and `visibility` use plain `z.string()`, so new
enum values do not break replay.

## Review instructions

1. Run `pnpm exec vitest run lib/ai` in `apps/studio`. It should pass
(323 tests).
2. In `apps/studio/lib/ai/tools/registry.test.ts`, check the new
coverage: `toModelOutput` at all four opt-in levels, for both live and
replayed outputs, and invalid replayed outputs being dropped.
3. Confirm the three tools still appear for the schema opt-in level in
`tool-filter.test.ts` and `notebook-tools.test.ts`.

## Checklist

Check all before review:

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] 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-19 10:29:27 -04:00

32 lines
1.1 KiB
TypeScript

import { getAuthHeaders } from './auth-headers'
import { contract } from './list-notebooks'
import { toolFromContract } from './tool-from-contract'
import type { ToolFactory } from './types'
import { getContent } from '@/data/content/content-infinite-query'
import type { Notebooks } from '@/types'
export const createTool: ToolFactory<typeof contract> = (ctx) => {
const { projectRef } = ctx
const authHeaders = getAuthHeaders(ctx.authorization)
return toolFromContract(contract, ctx, async ({ cursor, limit, sort_by }, { abortSignal }) => {
const { content, cursor: nextCursor } = await getContent(
{ projectRef, type: 'notebook', limit, cursor, sort: sort_by },
abortSignal,
authHeaders
)
return {
notebooks: content.map((notebook) => ({
id: notebook.id,
name: notebook.name,
description: notebook.description,
visibility: notebook.visibility,
updated_at: notebook.updated_at,
cell_count: (notebook.content as Notebooks.Content).cells.length,
})),
cursor: nextCursor,
}
})
}