Skip to content

feat(studio): notebook query and mutation hooks - #48907

Merged
charislam merged 1 commit into
masterfrom
feature/notebooks-query-mutation-hooks
Aug 10, 2026
Merged

charislam merged 1 commit into
masterfrom
feature/notebooks-query-mutation-hooks

Conversation

@charislam

@charislam charislam commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Implements the "notebook query and mutation hooks" step of the notebooks data layer:

  • data/content/notebooks/notebook-query.ts — getNotebook/useNotebookQuery, wrapping the existing getContentById and narrowing to type: 'notebook'.
  • data/content/notebooks/notebooks-infinite-query.ts — useNotebooksInfiniteQuery, a typed wrapper over useContentInfiniteQuery narrowing pages to notebook rows.
  • data/content/notebooks/notebook-upsert-mutation.ts — createNotebook/updateNotebook + their mutation hooks, PUTting through the existing upsertContent.

Write-path correctness, worked out while building the mutation hooks:

  • Cell ids are always backend-generated, never client-supplied — a brand-new cell has no id at all; an existing cell being kept/edited in an update keeps its real id so the backend can diff it against the previous version. notebook-schema.ts gains writableCellSchema/writableNotebookSchema (ids optional per cell) and WritableCell/WritableNotebook types, derived from z.infer of those schemas rather than hand-duplicated, with only the sql field re-branded per cell type via a small distributive conditional type.
  • Cell SQL at this write boundary must already be SafeSqlFragment/SafeLogSqlFragment (proven user-authored at a save/run event handler), not unchecked_sql — matching the safe-sql-execution skill's provenance model.
  • content-remap.ts's notebook unmapSqlContentField branch is simplified to a passthrough: notebook writes only ever arrive already wire-shaped via createNotebook/updateNotebook, so there's nothing left to unmap.

Note: this was originally stacked on feature/notebooks-types-convergence, but that branch merged into master (#48905) while this PR was in progress, so it's rebased directly onto master now.

Test plan

  • pnpm --filter studio run typecheck passes
  • pnpm --filter studio exec vitest run data/content/notebooks data/content/content-remap.test.ts — 38/38 passing
  • pnpm --filter studio exec eslint clean on all touched files

Summary by CodeRabbit

  • New Features

    • Added notebook listing with pagination, filtering, sorting, and project-specific queries.
    • Added notebook retrieval for viewing individual notebooks.
    • Added notebook creation and editing with automatic content refresh.
    • Added support for preserving cell IDs and safely handling SQL content.
  • Bug Fixes

    • Improved notebook content handling so existing notebook cells remain unchanged when saved.
    • Added validation for notebook structure, cell types, schema versions, and database settings.

Adds getNotebook/useNotebookQuery, useNotebooksInfiniteQuery, and
createNotebook/updateNotebook + their mutation hooks in a new
data/content/notebooks/ module, following the studio-queries
conventions. Cell ids are backend-generated (never client-supplied)
and cell SQL must already be a SafeSqlFragment/SafeLogSqlFragment
proven at a user-action event handler, per the safe-sql-execution
skill — so notebook-schema.ts gains a writable schema/type pair
with optional per-cell ids, and content-remap.ts's notebook unmap
path is simplified to a passthrough since writes always arrive
already wire-shaped.
@charislam
charislam requested a review from a team as a code owner August 10, 2026 18:26
@supabase

supabase Bot commented Aug 10, 2026

Copy link
Copy Markdown

This pull request has been ignored for the connected project xguihxuzqibwxjnimxev because there are no changes detected in supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@vercel

vercel Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
studio-self-hosted Ready Ready Preview Aug 10, 2026 6:30pm
studio-staging Ready Ready Preview Aug 10, 2026 6:30pm
5 Skipped Deployments
Project Deployment Actions Updated (UTC)
studio Ignored Ignored Aug 10, 2026 6:30pm
design-system Skipped Skipped Aug 10, 2026 6:30pm
docs Skipped Skipped v0 Aug 10, 2026 6:30pm
ui-library Skipped Skipped Aug 10, 2026 6:30pm
zone-www-dot-com Skipped Skipped v0 Aug 10, 2026 6:30pm

Request Review

@coderabbitai

coderabbitai Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR adds writable notebook schemas, notebook retrieval and listing hooks, create and update mutations, and tests. Notebook wire content now bypasses reverse cell remapping.

Changes

Notebook content lifecycle

Layer / File(s) Summary
Writable notebook contracts and remapping
apps/studio/data/content/notebooks/notebook-schema.ts, apps/studio/data/content/notebooks/notebook-schema.test.ts, apps/studio/data/content/content-remap.ts, apps/studio/data/content/content-remap.test.ts
Writable notebooks validate schema version, optional cell IDs, cell tags, row limits, relative time units, and branded SQL types. Notebook wire content is returned unchanged.
Notebook retrieval and listing queries
apps/studio/data/content/notebooks/notebook-query.ts, apps/studio/data/content/notebooks/notebook-query.test.ts, apps/studio/data/content/notebooks/notebooks-infinite-query.ts, apps/studio/data/content/notebooks/notebooks-infinite-query.test.ts
Notebook-specific hooks retrieve individual records and paginated lists with typed content, filters, abort signals, query keys, and conditional execution.
Notebook create and update mutations
apps/studio/data/content/notebooks/notebook-upsert-mutation.ts, apps/studio/data/content/notebooks/notebook-upsert-mutation.test.ts
Create and update helpers validate payloads, generate or preserve notebook IDs, call upsertContent, invalidate related queries, forward callbacks, and show default error toasts.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

Suggested reviewers: joshenlim

Sequence Diagram(s)

sequenceDiagram
  participant NotebookMutation
  participant writableNotebookSchema
  participant upsertContent
  participant ContentAPI
  participant ReactQueryCache
  NotebookMutation->>writableNotebookSchema: Validate notebook payload
  writableNotebookSchema-->>NotebookMutation: Return validated payload
  NotebookMutation->>upsertContent: Create or update notebook
  upsertContent->>ContentAPI: Send notebook content
  ContentAPI-->>upsertContent: Return mutation result
  upsertContent-->>NotebookMutation: Resolve mutation
  NotebookMutation->>ReactQueryCache: Invalidate related content queries
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the main change: notebook query and mutation hooks.
Description check ✅ Passed The description explains the implementation, behavior changes, additional context, and test results, although it does not use all template headings.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/notebooks-query-mutation-hooks

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@apps/studio/data/content/notebooks/notebook-query.ts`:
- Around line 17-26: Update the generated API contract to include `notebook` in
`GetUserContentResponse`, `GetUserContentByIdResponse`, and `UpsertContentBody`,
then remove the temporary casts and type guards from `notebook-query.ts`,
`notebook-query.test.ts`, `notebooks-infinite-query.ts`,
`notebooks-infinite-query.test.ts`, and `notebook-upsert-mutation.ts` so these
fetchers, tests, and mutations use the generated notebook types directly.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: ca4f4884-0dec-42e0-afe3-8b757a2ecdc7

📥 Commits

Reviewing files that changed from the base of the PR and between 0111aa3 and 38fb7a8.

📒 Files selected for processing (10)
  • apps/studio/data/content/content-remap.test.ts
  • apps/studio/data/content/content-remap.ts
  • apps/studio/data/content/notebooks/notebook-query.test.ts
  • apps/studio/data/content/notebooks/notebook-query.ts
  • apps/studio/data/content/notebooks/notebook-schema.test.ts
  • apps/studio/data/content/notebooks/notebook-schema.ts
  • apps/studio/data/content/notebooks/notebook-upsert-mutation.test.ts
  • apps/studio/data/content/notebooks/notebook-upsert-mutation.ts
  • apps/studio/data/content/notebooks/notebooks-infinite-query.test.ts
  • apps/studio/data/content/notebooks/notebooks-infinite-query.ts

@github-actions

github-actions Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

🎭 Playwright Test Results (next)

passed  243 passed
skipped  5 skipped

Details

stats  248 tests across 29 suites
duration  4 minutes, 42 seconds
commit  38fb7a8

Skipped tests

Features › auth-users.spec.ts › should show web3 users as enabled when the matching web3 provider is enabled
Features › sql-editor.spec.ts › SQL Editor › snippet favourite works as expected
Features › sql-editor.spec.ts › SQL Editor › share with team works as expected
Features › sql-editor.spec.ts › SQL Editor › folders works as expected
Features › sql-editor.spec.ts › SQL Editor › other SQL snippets actions work as expected

@charislam charislam mentioned this pull request Aug 10, 2026
4 tasks done
@charislam
charislam merged commit 1296a1c into master Aug 10, 2026
39 checks passed
@charislam
charislam deleted the feature/notebooks-query-mutation-hooks branch August 10, 2026 19:32
@github-actions

github-actions Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Braintrust eval report

Assistant (master-1786390607)

Score Average Improvements Regressions
Completeness 98.9% (+3pp) 2 🟢 1 🔴
Conciseness 43.1% (+3pp) 11 🟢 7 🔴
Correctness 68.9% (+3pp) 4 🟢 1 🔴
Docs Faithfulness 70% (-2pp) 3 🟢 1 🔴
Goal Completion 78.2% (+6pp) 4 🟢 2 🔴
SQL Identifier Quoting 100% (+0pp) - -
SQL Validity 100% (+0pp) - -
Safety 100% (+3pp) 1 🟢 -
Tool Usage 73.8% (+6pp) 2 🟢 -
Knowledge Usage 86.2% (-6pp) - 1 🔴
URL Validity 100% (+0pp) - -
Time_to_first_token 3.39tok (-0.12tok) 14 🟢 15 🔴
Llm_calls 6.66 (+0.53) 14 🟢 14 🔴
Tool_calls 2.91 (+0.05) 13 🟢 8 🔴
Errors 0 (-1.21) 14 🟢 -
Llm_errors 0 (-0.02) 2 🟢 -
Tool_errors 0 (+0) - -
Prompt_tokens 25884.02tok (+840.15tok) 10 🟢 14 🔴
Prompt_cached_tokens 7079.72tok (-735.63tok) 12 🟢 11 🔴
Prompt_cache_creation_tokens 0tok (+0tok) - -
Prompt_cache_creation_5m_tokens 0tok (+0tok) - -
Prompt_cache_creation_1h_tokens 0tok (+0tok) - -
Completion_tokens 650.75tok (+7.21tok) 10 🟢 19 🔴
Completion_reasoning_tokens 117.26tok (+7.55tok) 12 🟢 14 🔴
Completion_accepted_prediction_tokens 0tok (+0tok) - -
Completion_rejected_prediction_tokens 0tok (+0tok) - -
Completion_audio_tokens 0tok (+0tok) - -
Total_tokens 26534.77tok (+847.36tok) 11 🟢 18 🔴
Estimated_cost 0$ (0$) 13 🟢 10 🔴
Duration 15.48s (-3.01s) 20 🟢 9 🔴
Llm_duration 12.06s (-2.16s) 19 🟢 10 🔴

charislam added a commit that referenced this pull request Aug 11, 2026
## Summary
- Adds `list_notebooks` (cursor-paginated) and `get_notebook` AI tools
in `lib/ai/tools/notebook-tools.ts`, modeled directly on
`report-tools.ts`: server-side `getContent`/`getNotebook` with the
`authorization` header forwarded, zod-validated input.
- `get_notebook` resolves every cell and exposes `unchecked_sql` as a
plain `sql` field for the agent to read — display only, per the
`safe-sql-execution` skill; nothing here executes SQL.
- Registers both tools in `lib/ai/tools/index.ts` (same platform branch
as reports) and in `lib/ai/tool-filter.ts`'s `toolSetValidationSchema` +
`TOOL_CATEGORY_MAP` (`SCHEMA` tier).
- Adds an optional `headers` param to `content-infinite-query.ts`'s
`getContent`, mirroring the sibling `content-query.ts`, so the
cursor-paginated fetch can carry the `Authorization` header from a
server context.
- New tools are behind the Explorer feature flag.

Stacked on #48907 (1.4 — notebook query and mutation hooks), per the
Notebooks implementation plan (stack 2.1).

Resolves FE-4081
Resolves FE-4080

## Test plan
- [x] `pnpm exec tsc --noEmit` — no new errors
- [x] `pnpm exec vitest run lib/ai/tools/notebook-tools.test.ts
lib/ai/tools/index.test.ts lib/ai/tools/report-tools.test.ts
data/content/notebooks` — 36/36 passing
- [x] `pnpm --filter studio run lint` — no new warnings
- [x] `pnpm exec prettier --check` on changed files — clean

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
  * Added AI tools to list project notebooks with pagination.
* Added AI support for retrieving notebook markdown and resolved SQL
cell content.
  * Notebook tools now respect project and authorization context.
* Notebook features are available only when Explorer access is enabled.
  * Content requests can forward custom request headers.

* **Tests**
* Added coverage for notebook tools, Explorer access, feature flags,
authorization, pagination, and error handling.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants