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.
147 lines
5.6 KiB
TypeScript
147 lines
5.6 KiB
TypeScript
import { screen } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
import { AssistantNotebookPreview } from './AssistantNotebookPreview'
|
|
import type { NotebookCellDiffEntry } from '@/data/content/notebooks/notebook-operations'
|
|
import type { AgentCell, CellWire } from '@/data/content/notebooks/notebook-schema'
|
|
import { customRender as render } from '@/tests/lib/custom-render'
|
|
|
|
const wireMarkdownCell = (id: string, text: string): CellWire => ({
|
|
_tag: 'markdown_cell',
|
|
_id: id,
|
|
text,
|
|
})
|
|
|
|
const agentMarkdownCell = (text: string): AgentCell => ({ _tag: 'markdown_cell', text })
|
|
|
|
const wireDatabaseCell = (id: string, database_identifier?: string): CellWire => ({
|
|
_tag: 'database_cell',
|
|
_id: id,
|
|
sql: 'select 1',
|
|
row_limit: 100,
|
|
database_identifier,
|
|
})
|
|
|
|
const agentDatabaseCell = (database_identifier?: string): AgentCell => ({
|
|
_tag: 'database_cell',
|
|
sql: 'select 1',
|
|
row_limit: 100,
|
|
database_identifier,
|
|
})
|
|
|
|
describe('AssistantNotebookPreview', () => {
|
|
// The whole safety argument for this feature reduces to this: agent-authored markdown text
|
|
// is rendered as literal source (via CodeBlock), never interpreted into real DOM nodes. A
|
|
// future refactor that swaps in <Markdown> would break this silently.
|
|
it('never renders agent-authored cell content as real img/link/src DOM nodes', async () => {
|
|
const user = userEvent.setup()
|
|
const adversarialText =
|
|
' [y](https://evil.example/) <img src=x onerror=1>'
|
|
const entries: NotebookCellDiffEntry[] = [
|
|
{ _tag: 'added', cell: agentMarkdownCell(adversarialText), operationIndex: 0 },
|
|
]
|
|
|
|
const { container } = render(<AssistantNotebookPreview entries={entries} mode="update" />)
|
|
await user.click(screen.getByRole('button', { name: 'Added Markdown cell' }))
|
|
|
|
expect(container.querySelectorAll('img')).toHaveLength(0)
|
|
expect(container.querySelectorAll('[href]')).toHaveLength(0)
|
|
expect(container.querySelectorAll('[src]')).toHaveLength(0)
|
|
})
|
|
|
|
it('renders the create-mode header summary', () => {
|
|
const entries: NotebookCellDiffEntry[] = [
|
|
{ _tag: 'unchanged', cell: wireMarkdownCell('a', 'one') },
|
|
{ _tag: 'unchanged', cell: wireMarkdownCell('b', 'two') },
|
|
]
|
|
|
|
const { container } = render(<AssistantNotebookPreview entries={entries} mode="create" />)
|
|
|
|
expect(screen.getByRole('toolbar', { name: 'Notebook toolbar' })).toBeInTheDocument()
|
|
expect(screen.getByText('2 cells')).toBeInTheDocument()
|
|
expect(screen.getByText('New notebook')).toBeInTheDocument()
|
|
expect(container.firstElementChild).toHaveClass('max-w-6xl')
|
|
})
|
|
|
|
it('surfaces a metadata-only change on a replaced cell even when the sql is unchanged', () => {
|
|
const entries: NotebookCellDiffEntry[] = [
|
|
{
|
|
_tag: 'replaced',
|
|
before: wireDatabaseCell('cell-1', 'primary'),
|
|
after: agentDatabaseCell('replica-3'),
|
|
operationIndex: 0,
|
|
},
|
|
]
|
|
|
|
render(<AssistantNotebookPreview entries={entries} mode="update" />)
|
|
|
|
expect(screen.getByText('Database: primary → Database: replica-3')).toBeInTheDocument()
|
|
expect(
|
|
screen.getByRole('button', { name: 'Replaced Query: Untitled query' })
|
|
).toHaveTextContent('Database: primary → Database: replica-3')
|
|
})
|
|
|
|
it('hides entries past the limit behind a "Show N more" button', async () => {
|
|
const entries: NotebookCellDiffEntry[] = Array.from({ length: 7 }, (_, index) => ({
|
|
_tag: 'unchanged' as const,
|
|
cell: wireMarkdownCell(`cell-${index}`, `text-${index}`),
|
|
}))
|
|
|
|
render(<AssistantNotebookPreview entries={entries} mode="create" />)
|
|
|
|
expect(screen.getByText('Show 2 more cells')).toBeInTheDocument()
|
|
})
|
|
|
|
it('renders run results inside their matching minified notebook cell', () => {
|
|
const entries: NotebookCellDiffEntry[] = [
|
|
{ _tag: 'unchanged', cell: wireDatabaseCell('cell-1') },
|
|
]
|
|
|
|
const { container } = render(
|
|
<AssistantNotebookPreview
|
|
entries={entries}
|
|
mode="run"
|
|
title="Signup funnel"
|
|
results={{ 'cell-1': { rows: [] } }}
|
|
/>
|
|
)
|
|
|
|
expect(screen.getByText('Signup funnel')).toBeInTheDocument()
|
|
expect(screen.getByText('1 cell')).toBeInTheDocument()
|
|
expect(screen.getByText('Success. No rows returned')).toBeInTheDocument()
|
|
expect(screen.getByText('0 rows')).toBeInTheDocument()
|
|
expect(container.querySelector('[data-slot="explorer-query-results"]')).toBeInTheDocument()
|
|
})
|
|
|
|
it('bases result rendering and layout on supplied results rather than preview mode', () => {
|
|
const entries: NotebookCellDiffEntry[] = [
|
|
{ _tag: 'unchanged', cell: wireDatabaseCell('cell-1') },
|
|
]
|
|
|
|
const { container } = render(
|
|
<AssistantNotebookPreview
|
|
entries={entries}
|
|
mode="create"
|
|
results={{ 'cell-1': { rows: [{ value: 1 }] } }}
|
|
/>
|
|
)
|
|
|
|
expect(screen.getByText('1 row')).toBeInTheDocument()
|
|
expect(container.querySelector('[data-slot="explorer-query-results"]')).toBeInTheDocument()
|
|
expect(container.querySelector('.flex.flex-col.gap-2')).toBeInTheDocument()
|
|
expect(container.querySelector('.divide-y.divide-border')).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('keeps a run preview without results in the grouped cell layout', () => {
|
|
const entries: NotebookCellDiffEntry[] = [
|
|
{ _tag: 'unchanged', cell: wireDatabaseCell('cell-1') },
|
|
]
|
|
|
|
const { container } = render(<AssistantNotebookPreview entries={entries} mode="run" />)
|
|
|
|
expect(container.querySelector('[data-slot="explorer-query-results"]')).not.toBeInTheDocument()
|
|
expect(container.querySelector('.divide-y.divide-border')).toBeInTheDocument()
|
|
})
|
|
})
|