Files
supabase/apps/studio/components/ui/AIAssistantPanel/NotebookRunRenderer.tsx
Charis 9ae8432dc7 refactor(studio): migrate create, delete, and run notebook tools to tool contracts (#51754)
## Summary

Migrates the create, delete, and run notebook assistant tools to the
tool contract + server pattern used by `update_notebook`. Each tool now
defines its schema once in a shared contract, with server-only execution
split into a `.server.ts` module. This removes duplicated client-side
schemas and gives the three tools one shared notebook mutation output
schema.

## Changes

- Add `create-notebook`, `delete-notebook`, and `run-notebook` tool
contracts, each with a `.server.ts` implementation.
- Remove the duplicated client-side schemas for these tools from
`notebook-tools.ts` and the registry.
- Add `notebook-mutation-output.ts` as the single output schema shared
by the notebook mutation tools.
- Add `lib/ai/opt-in-level.ts` with the opt-in level helpers, extracted
from the existing module to break an import cycle.
- Add optional `ToolContract.outputOnInvalid` so `run_notebook` history
replay can reuse a stored output when input fails validation.
- Update the AI assistant panel renderers (proposal, run, opt-in modal
and request) and the tool filter, sanitizer, and cache invalidation to
use the new contracts.

## Deviations worth reviewing

- **`outputOnInvalid` on `ToolContract`.** This is a new optional hook.
It exists so `run_notebook` history replay can return a valid output
when stored input no longer passes validation. Reviewers should confirm
this is the right level for the hook rather than handling it inside the
tool.
- **`opt-in-level.ts` extraction.** The helpers were moved into their
own module to break a circular import between the tool modules and the
opt-in components. This adds one file, but no behavior change is
intended.
- **`create-notebook` contract imports the data-layer
`notebook-schema`.** This matches `update-notebook`. It should be moved
behind the contract before the planned ESLint rule step, which restricts
contract modules from importing data-layer code.

## Testing

- `vitest` for `lib/ai`, `components/ui/AIAssistantPanel`, and `state`:
920 tests pass across 71 files.
- `tsc`: one error remains in `packages/ui-patterns`
(`InstructionBlocks.tsx`), which is unrelated to this change. I have not
verified whether it also fails on the base branch.
- ESLint reports no errors on the changed files (9 `no-explicit-any`
warnings in `tool-filter.ts` and its test); Prettier is clean.
- Not run: knip and the lint ratchet.
2026-10-15 11:16:58 -04:00

202 lines
5.8 KiB
TypeScript

import { useParams } from 'common'
import { Loader2Icon } from 'lucide-react'
import { Button, cn } from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
import { CodeBlock } from 'ui-patterns/CodeBlock'
import { AssistantNotebookPreview } from './AssistantNotebookPreview'
import { toAssistantQueryResult } from './AssistantQueryCell.utils'
import { Confirm } from './Confirm'
import type { ConfirmFooterApprovalState } from './Confirm.utils'
import { AlertError } from '@/components/ui/AlertError'
import { useNotebookQuery } from '@/data/content/notebooks/notebook-query'
import { toWireNotebook } from '@/data/content/notebooks/notebook-schema'
import { contract as runNotebookContract } from '@/lib/ai/tools/run-notebook'
export type NotebookRunState =
| 'input-available'
| 'approval-requested'
| 'approval-responded'
| 'output-denied'
| 'output-available'
| 'output-error'
export interface NotebookRunRendererProps {
state: NotebookRunState
input: unknown
output: unknown
confirmState?: ConfirmFooterApprovalState
onApprove?: () => void
onDeny?: () => void
}
export const NotebookRunRenderer = ({
state,
input,
output,
confirmState,
onApprove,
onDeny,
}: NotebookRunRendererProps) => {
const { ref } = useParams()
const parsedInput = runNotebookContract.inputSchema.safeParse(input)
const {
data: notebook,
isLoading,
isError,
error,
} = useNotebookQuery(
{ projectRef: ref, id: parsedInput.success ? parsedInput.data.id : undefined },
{ enabled: parsedInput.success }
)
if (!parsedInput.success) {
return (
<Confirm
className="my-4"
state={confirmState}
message="Assistant wants to run a notebook"
denyOnly
onCancel={onDeny}
>
<div className="flex flex-col gap-2 p-3">
<Admonition
type="warning"
title="Couldn't render this notebook run"
description="The assistant's input didn't match the expected shape. You can review the raw input below."
/>
<CodeBlock
language="json"
value={JSON.stringify(input, null, 2)}
hideLineNumbers
className="text-xs"
wrapperClassName="max-h-56"
/>
</div>
</Confirm>
)
}
const loadingStatus = (
<div
aria-live="polite"
className={cn(
isLoading
? 'my-4 rounded-lg border bg-surface-75 heading-meta h-9 px-3 text-foreground-light flex items-center gap-2'
: 'sr-only'
)}
>
{isLoading && (
<>
<Loader2Icon className="w-4 h-4 animate-spin motion-reduce:animate-none" />
Loading notebook...
</>
)}
</div>
)
if (isLoading) {
return <>{loadingStatus}</>
}
if (isError || !notebook) {
const loadError = <AlertError error={error} subject="Failed to load notebook" />
if (confirmState === 'approval-requested') {
return (
<>
{loadingStatus}
<Confirm
className="my-4"
state={confirmState}
message="Assistant wants to run a notebook"
denyOnly
onCancel={onDeny}
>
<div className="p-3">{loadError}</div>
</Confirm>
</>
)
}
return (
<>
{loadingStatus}
<div className="my-4 flex flex-col gap-2">
{loadError}
{confirmState !== undefined && (
<Button variant="outline" size="tiny" className="w-fit" disabled onClick={onDeny}>
Skip
</Button>
)}
</div>
</>
)
}
const entries = toWireNotebook(notebook.content).cells.map((cell) => ({
_tag: 'unchanged' as const,
cell,
}))
const parsedOutput = runNotebookContract.outputSchema.safeParse(output)
const isHistoricalRun = state === 'output-available' || state === 'output-error'
const referencedUpdatedAt =
state === 'output-available' && parsedOutput.success
? parsedOutput.data.updated_at
: parsedInput.data.expected_updated_at
const hasNotebookChanged = notebook.updated_at !== referencedUpdatedAt
const results = parsedOutput.success
? Object.fromEntries(
parsedOutput.data.cells.map((cell) => [
cell.cell_id,
cell.status === 'error'
? { error: { message: cell.error?.message ?? 'Failed to run query' } }
: (toAssistantQueryResult(cell.rows ?? []) ?? { rows: [] }),
])
)
: undefined
return (
<>
{loadingStatus}
<Confirm
className="my-4"
state={confirmState}
message={`Assistant wants to run "${notebook.name}"`}
cancelLabel="Skip"
confirmLabel="Run notebook"
confirmLabelLoading="Running..."
successMessage="Notebook executed"
errorMessage="Failed to run notebook"
deniedMessage="Skipped notebook run"
onCancel={onDeny}
onConfirm={onApprove}
>
{hasNotebookChanged && (
<div className="px-2 pt-2">
<Admonition
type="warning"
title={
isHistoricalRun
? 'Notebook changed since this run'
: 'Notebook changed since the Assistant read it'
}
description={
isHistoricalRun
? 'This preview shows the current notebook. Its cells may not match the saved results from this run.'
: 'Review the current cells below. The run will be rejected until the Assistant reads the latest notebook version.'
}
/>
</div>
)}
<AssistantNotebookPreview
entries={entries}
mode="run"
title={notebook.name}
results={state === 'output-available' ? results : undefined}
/>
</Confirm>
</>
)
}