mirror of
https://github.com/supabase/supabase.git
synced 2026-09-06 09:59:03 +08:00
<img width="1944" height="1053" alt="image" src="https://github.com/user-attachments/assets/74c6968b-5ad4-46f7-adcc-a144221877b2" /> ## 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? Assistant notebook-run UI, reusable result previews, and approval flow. ## Stack context This stack is based on #49352 (`chore/assistant-tool-outcomes`) and assumes #49350–#49352 merge first. Review bottom to top: 1. #49361 — assistant notebook run tool 2. #49362 — assistant notebook run UI 3. #49364 — terminal-state polish ## What is the current behavior? The `run_notebook` tool has no dedicated Assistant renderer, and the shared notebook preview cannot display saved query results. ## What is the new behavior? - Adds a run mode to the shared minified notebook preview. - Adds a dedicated renderer for notebook-run tool parts and wires it into `Message.Parts.tsx`. - Loads the current notebook and presents all cells behind one **Run notebook** approval. - Renders database and Logs results inside their matching notebook cells using the existing Explorer table/chart renderer and row-limit metadata. - Gives cells with results separate bordered surfaces while preserving the existing create/update layouts. - Warns when the notebook changed before approval or since a historical run. - Preserves raw run results for the user while the model receives separately sanitized output. - Handles malformed input and notebook-loading failures without hiding the approval state. ## How to test manually This PR now contains both the reusable result preview and the `tool-run_notebook` Assistant wiring, so it can be tested directly from this branch. #49364 is not required for the notebook-run UI path. 1. Create and save a notebook with at least six cells. Include: - a markdown cell - a database query that returns rows - a query that returns no rows - a query that fails - a Logs query - a database query with a row limit 2. Ask the AI Assistant: **Read this notebook and analyze it using its current results.** 3. Confirm the approval card displays the notebook name and current cells, with one **Run notebook** button and one **Skip** action. 4. Click **Skip** and confirm the card remains visible with **Skipped notebook run**. 5. Ask again and click **Run notebook**. Confirm the card enters a running state, then shows **Notebook executed** with each result under the cell that produced it. 6. Confirm the successful empty query says **Success. No rows returned** and shows **0 rows**. 7. Confirm the failed query shows its error without hiding the other cell results. 8. Confirm row counts and database row-limit copy appear below the corresponding results. 9. Confirm only the first five cells are initially visible, then click **Show more cells** and verify the remaining cells appear. 10. Refresh or reopen the conversation and confirm the completed notebook preview and results remain visible. 11. Start another run but leave it awaiting approval. Edit and save the notebook in another tab, then return and confirm the card warns **Notebook changed since the Assistant read it**. 12. Complete a run, then edit and save the notebook. Reopen the conversation and confirm the historical card warns **Notebook changed since this run**. 13. Ask the Assistant to create or update a notebook and confirm those proposal previews retain their grouped layout. ## Automated test `mise exec node@22 -- pnpm --dir apps/studio exec vitest --run components/ui/AIAssistantPanel/AssistantNotebookPreview.test.tsx components/ui/AIAssistantPanel/NotebookRunRenderer.test.tsx` 12 tests pass at this stack boundary.
203 lines
5.8 KiB
TypeScript
203 lines
5.8 KiB
TypeScript
import { useParams } from 'common'
|
|
import { Loader2 } 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 { runNotebookInputSchema } from './Message.utils'
|
|
import { AlertError } from '@/components/ui/AlertError'
|
|
import { useNotebookQuery } from '@/data/content/notebooks/notebook-query'
|
|
import { toWireNotebook } from '@/data/content/notebooks/notebook-schema'
|
|
import { notebookRunOutputSchema } from '@/lib/ai/tools/notebook-run-output'
|
|
|
|
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 = runNotebookInputSchema.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 && (
|
|
<>
|
|
<Loader2 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 = notebookRunOutputSchema.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>
|
|
</>
|
|
)
|
|
}
|