mirror of
https://github.com/supabase/supabase.git
synced 2026-10-08 19:05:06 +03: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.
120 lines
4.1 KiB
TypeScript
120 lines
4.1 KiB
TypeScript
import { NotebookText } from 'lucide-react'
|
|
import { useState } from 'react'
|
|
import { Button, cn } from 'ui'
|
|
|
|
import {
|
|
formatNotebookDiffSummary,
|
|
getEntryKey,
|
|
summarizeNotebookDiff,
|
|
} from './AssistantNotebookPreview.utils'
|
|
import { AssistantNotebookPreviewCell } from './AssistantNotebookPreviewCell'
|
|
import {
|
|
ExplorerToolbar,
|
|
ExplorerToolbarActions,
|
|
ExplorerToolbarIcon,
|
|
ExplorerToolbarTitle,
|
|
} from '@/components/interfaces/Explorer/ExplorerToolbar'
|
|
import type { QueryResult } from '@/components/interfaces/Explorer/types'
|
|
import type { NotebookCellDiffEntry } from '@/data/content/notebooks/notebook-operations'
|
|
|
|
export interface AssistantNotebookPreviewProps {
|
|
entries: NotebookCellDiffEntry[]
|
|
mode: 'create' | 'update' | 'run'
|
|
/** The notebook's name, shown in the toolbar. Falls back to a generic label. */
|
|
title?: string
|
|
/** Query results keyed by persisted cell id. */
|
|
results?: Record<string, QueryResult>
|
|
className?: string
|
|
}
|
|
|
|
const VISIBLE_ENTRY_LIMIT = 5
|
|
|
|
const FALLBACK_TITLE = {
|
|
create: 'New notebook',
|
|
update: 'Notebook changes',
|
|
run: 'Notebook run',
|
|
} as const
|
|
|
|
/**
|
|
* Read-only minified notebook for assistant create/update proposals and notebook runs.
|
|
* Composes the same Explorer toolbar and query-result surfaces as notebook tabs; the
|
|
* surrounding `Confirm` card owns the frame. Pure presentational: no data fetching,
|
|
* approval, or notebook-editor state — callers adapt those concerns into entries/results.
|
|
*/
|
|
export const AssistantNotebookPreview = ({
|
|
entries,
|
|
mode,
|
|
title,
|
|
results,
|
|
className,
|
|
}: AssistantNotebookPreviewProps) => {
|
|
const [isShowingAllEntries, setIsShowingAllEntries] = useState(false)
|
|
const [expandedOverrides, setExpandedOverrides] = useState<Record<string, boolean>>({})
|
|
|
|
const summary = summarizeNotebookDiff(entries, mode)
|
|
const visibleEntries = isShowingAllEntries ? entries : entries.slice(0, VISIBLE_ENTRY_LIMIT)
|
|
const hiddenCount = entries.length - visibleEntries.length
|
|
const hasResults = results !== undefined
|
|
|
|
const isExpanded = (entry: NotebookCellDiffEntry) =>
|
|
expandedOverrides[getEntryKey(entry)] === true
|
|
|
|
return (
|
|
<div className={cn('flex w-full min-w-0 max-w-6xl mx-auto flex-col', className)}>
|
|
<ExplorerToolbar aria-label="Notebook toolbar">
|
|
<ExplorerToolbarIcon>
|
|
<NotebookText />
|
|
</ExplorerToolbarIcon>
|
|
<ExplorerToolbarTitle>{title ?? FALLBACK_TITLE[mode]}</ExplorerToolbarTitle>
|
|
<ExplorerToolbarActions>
|
|
<span className="shrink-0 text-sm text-muted-foreground">
|
|
{formatNotebookDiffSummary(summary)}
|
|
</span>
|
|
</ExplorerToolbarActions>
|
|
</ExplorerToolbar>
|
|
<div className="p-2">
|
|
<div
|
|
className={cn(
|
|
hasResults ? 'flex flex-col gap-2' : 'overflow-hidden rounded-md border bg-surface-100'
|
|
)}
|
|
>
|
|
<div className={cn(hasResults ? 'contents' : 'divide-y divide-border')}>
|
|
{visibleEntries.map((entry) => {
|
|
const key = getEntryKey(entry)
|
|
return (
|
|
<div
|
|
key={key}
|
|
className={cn(hasResults && 'overflow-hidden rounded-md border bg-surface-100')}
|
|
>
|
|
<AssistantNotebookPreviewCell
|
|
entry={entry}
|
|
mode={mode}
|
|
result={results?.[key]}
|
|
isExpanded={isExpanded(entry)}
|
|
onExpandedChange={(open) =>
|
|
setExpandedOverrides((prev) => ({ ...prev, [key]: open }))
|
|
}
|
|
/>
|
|
</div>
|
|
)
|
|
})}
|
|
</div>
|
|
{hiddenCount > 0 && (
|
|
<Button
|
|
variant="text"
|
|
size="tiny"
|
|
className={cn(
|
|
'w-full text-foreground-light',
|
|
!hasResults && 'rounded-none border-0 border-t border-default'
|
|
)}
|
|
onClick={() => setIsShowingAllEntries(true)}
|
|
>
|
|
Show {hiddenCount} more cell{hiddenCount === 1 ? '' : 's'}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|