Skip to content

refactor(sql-editor): extract AI/diff + shortcuts hooks (decompose 5/6) - #47935

Merged
charislam merged 1 commit into
masterfrom
charislam/sql-editor-decompose-5-ai-shortcuts
Jul 14, 2026
Merged

charislam merged 1 commit into
masterfrom
charislam/sql-editor-decompose-5-ai-shortcuts

Conversation

@charislam

@charislam charislam commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

What

Decompose step 5 of 6 for SQLEditor.tsx. Extracts the Assistant / diff cluster and the keyboard-shortcut wiring out of the SQLEditorContent monolith into two focused, individually-testable hooks:

  • useSqlEditorAi — SQL completion (complete), the ask-AI prompt flow (handlePrompt), accept/discard diff handlers, onDebug / buildDebugPrompt helpers, handleDiffEditorMount, and the fragile diff lifecycle effects (one-shot diff-request drain, diff-editor value sync, ask-AI widget visibility).
  • useSqlEditorShortcuts — the registered shortcuts (focus editor, new snippet, format, explain) plus the accept/discard/escape keydown handling.

SQLEditorContent now composes these hooks alongside the execution/explain hooks landed in decompose 4.

Behavior-preserving

This is a pure extraction. The moved function bodies, effect logic, dependency arrays, and JSX are unchanged from the previous monolith (verified via git diff against the pre-decomposition source). In particular:

  • useEffectEvent is preserved for drainDiffRequest / resetDiff.
  • editorMountCount remains single-owner (passed into the AI hook to drive the one-shot drain).
  • The untrusted→safe SQL promotion (acceptUntrustedSql) continues to happen in the run/explain gesture and warning-modal handlers in SQLEditorContent, as close to the explicit user action as possible.

The Phase-1 characterization suite (SQLEditor.test.tsx, 11 tests) remains green.

Stack

Part of the SQLEditor decomposition stack (1/6 … 6/6). Builds on decompose 4 (execution + explain hooks, #47923). Next: PR6 splits the JSX into panes + final cleanup.

Summary by CodeRabbit

  • New Features

    • Improved SQL editor AI assistance, including completion prompts, debugging support, and diff review controls.
    • Added keyboard shortcuts for accepting or discarding AI-generated SQL changes.
    • Added shortcuts for focusing the editor, creating snippets, formatting queries, and explaining SQL.
  • Bug Fixes

    • Prevented SQL execution while reviewing AI-generated differences.
    • Improved handling of AI diff state during editor loading and interaction.

@supabase

supabase Bot commented Jul 14, 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 ↗︎.

@coderabbitai

coderabbitai Bot commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The SQL editor’s AI completion, diff handling, debug prompts, shortcuts, and lifecycle effects move into dedicated hooks. The editor consumes the new hook APIs, and diff-mode test coverage is updated for mount-time request draining.

Changes

SQL editor AI integration

Layer / File(s) Summary
AI completion and diff lifecycle
apps/studio/components/interfaces/SQLEditor/useSqlEditorAi.ts
Centralizes completion requests, debug prompt creation, AI diff acceptance and dismissal, pending diff handling, editor synchronization, and widget visibility.
Editor and AI keyboard shortcuts
apps/studio/components/interfaces/SQLEditor/useSqlEditorShortcuts.ts
Registers editor shortcuts and handles keyboard acceptance, dismissal, prompt reset, and editor refocusing.
SQLEditor integration and validation
apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx, apps/studio/tests/components/SQLEditor/SQLEditor.test.tsx
Replaces inline AI wiring with hook APIs, delegates diff-editor mounting, removes local lifecycle effects, and updates diff-mode run-button assertions.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

Suggested reviewers: awaseem, myerekapan, jordienr

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: extracting AI/diff and shortcut hooks from SQLEditor.
Description check ✅ Passed The description covers the change, behavior preservation, and context, but it doesn't follow the required template or include issue links.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch charislam/sql-editor-decompose-5-ai-shortcuts

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.

@vercel

vercel Bot commented Jul 14, 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, Comment Jul 14, 2026 5:29pm
studio-staging Ready Ready Preview, Comment Jul 14, 2026 5:29pm
5 Skipped Deployments
Project Deployment Actions Updated (UTC)
studio Ignored Ignored Jul 14, 2026 5:29pm
design-system Skipped Skipped Jul 14, 2026 5:29pm
docs Skipped Skipped Jul 14, 2026 5:29pm
ui-library Skipped Skipped Jul 14, 2026 5:29pm
zone-www-dot-com Skipped Skipped Jul 14, 2026 5:29pm

Request Review

Extract the Assistant/diff cluster into useSqlEditorAi (SQL completion, the
ask-AI prompt flow, accept/discard diff handlers, debug-prompt helpers, and
the diff lifecycle effects) and the keyboard/shortcut wiring into
useSqlEditorShortcuts. SQLEditorContent now composes these hooks.

Behavior-preserving: the extracted function bodies, effect logic, dependency
arrays, and JSX are unchanged from the previous monolith. The untrusted->safe
SQL promotion (acceptUntrustedSql) continues to happen in the run/explain
gesture and warning-modal handlers, as close to the user action as possible.

The Phase-1 characterization suite remains green.
@charislam
charislam force-pushed the charislam/sql-editor-decompose-5-ai-shortcuts branch from 441b7ca to 3b4d22b Compare July 14, 2026 17:25
@charislam charislam changed the title Charislam/sql editor decompose 5 ai shortcuts refactor(sql-editor): extract AI/diff + shortcuts hooks (decompose 5/6) Jul 14, 2026
@charislam
charislam marked this pull request as ready for review July 14, 2026 17:27
@charislam
charislam requested a review from a team as a code owner July 14, 2026 17:27
@github-actions

github-actions Bot commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

🎭 Playwright Test Results (next)

passed  238 passed
skipped  5 skipped

Details

stats  243 tests across 28 suites
duration  4 minutes, 42 seconds
commit  3b4d22b

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

@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/components/interfaces/SQLEditor/useSqlEditorAi.ts`:
- Around line 77-98: Update handleNewQuery and its caller acceptAiHandler so
new-snippet creation failures propagate or return an explicit unsuccessful
result; do not resolve successfully after missing ref, profile, project, or
caught creation errors. Only record success, reset state, and close the diff
after confirmed creation, including the corresponding flow in the additionally
affected handler.
🪄 Autofix (Beta)

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

Run ID: 5af824c1-afcc-41c7-afeb-91835f740a5a

📥 Commits

Reviewing files that changed from the base of the PR and between 05d2f38 and 3b4d22b.

📒 Files selected for processing (4)
  • apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx
  • apps/studio/components/interfaces/SQLEditor/useSqlEditorAi.ts
  • apps/studio/components/interfaces/SQLEditor/useSqlEditorShortcuts.ts
  • apps/studio/tests/components/SQLEditor/SQLEditor.test.tsx

@charislam
charislam merged commit 52a25c2 into master Jul 14, 2026
40 checks passed
@charislam
charislam deleted the charislam/sql-editor-decompose-5-ai-shortcuts branch July 14, 2026 19:14
@github-actions

github-actions Bot commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Braintrust eval report

Assistant (master-1784056620)

Score Average Improvements Regressions
Knowledge Usage 91.9% (-3pp) - 1 🔴
SQL Identifier Quoting 100% (+0pp) - -
SQL Validity 100% (+0pp) - -
Tool Usage 70% (+0pp) - -
Time_to_first_token 3.27tok (-0.11tok) 14 🟢 15 🔴
Llm_calls 2.91 (-0.21) 12 🟢 13 🔴
Tool_calls 3.07 (-0.29) 7 🟢 14 🔴
Errors 5.39 (+0.07) 4 🟢 7 🔴
Llm_errors 0 (+0) - -
Tool_errors 0 (+0) - -
Prompt_tokens 25434.06tok (+3267.08tok) 9 🟢 16 🔴
Prompt_cached_tokens 7309.24tok (-1600.74tok) 4 🟢 19 🔴
Prompt_cache_creation_tokens 0tok (+0tok) - -
Prompt_cache_creation_5m_tokens 0tok (+0tok) - -
Prompt_cache_creation_1h_tokens 0tok (+0tok) - -
Completion_tokens 604.32tok (-10.79tok) 16 🟢 13 🔴
Completion_reasoning_tokens 95.41tok (-5.9tok) 13 🟢 13 🔴
Total_tokens 26038.38tok (+3256.29tok) 11 🟢 18 🔴
Estimated_cost 0$ (+0$) 7 🟢 18 🔴
Duration 18.4s (-0.02s) 14 🟢 15 🔴
Llm_duration 11.96s (-0.89s) 19 🟢 10 🔴

charislam added a commit that referenced this pull request Jul 15, 2026
…onal components (decompose 6/6) (#47938)

## What

Final step (**6 of 6**) of the `SQLEditor.tsx` decomposition. Splits the
two `ResizablePanel` bodies out of the `SQLEditorContent` monolith into
presentational sub-components:

- **`SQLEditorPane`** — the editor panel: loading state, `DiffEditor` +
diff ask-AI widget, `MonacoEditor` + ask-AI widget. Reads the Monaco
refs (`editorRef`/`monacoRef`/`diffEditorRef`) from `SQLEditorContext`,
and receives the `diff`/`prompt`/`ai` controllers plus reactive state as
props.
- **`SQLEditorResults`** — the results panel: loading state +
`UtilityPanel`.

`SQLEditorContent` is now a composition root: shared-ref provider, hook
composition, and the run-query warning modal.

## Result

`SQLEditor.tsx` goes from the original **1056-line** monolith down to
**259 lines** (≈75% reduction). The remaining size over a bare ~140-line
root is the run-query warning modal, kept inline **deliberately**: its
handlers hold the `acceptUntrustedSql` promotion, which must stay at the
explicit user-action boundary in the root rather than moving into a
presentational pane.

## Behavior-preserving

The moved JSX is byte-identical aside from prop threading. No logic,
effects, dependency arrays, or `eslint-disable`s changed. The two
**render-time ref reads** — the editor placeholder (`!promptState.isOpen
&& !editorRef.current?.getValue()`) and the ask-AI widget gate
(`editorRef.current && promptState.isOpen && !isDiffOpen`) — are
preserved verbatim in `SQLEditorPane`, which re-renders whenever the
`prompt`/`diff` props change, keeping those reads fresh (the guardrail
from the plan).

Verification (all green):
- `SQLEditor.test.tsx` characterization suite — 11/11 pass
- `tsc --noEmit` — no new errors
- eslint — clean (no new ratchet entries)
- prettier — clean

## Stack

Builds on decompose 5 (#47935). This is the last PR in the series — the
decomposition is complete after this merges.


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

* **New Features**
* Added a resizable SQL editor layout with run warnings, query results,
and an Explain view.
* Introduced centralized SQL editor controllers and expanded AI-assisted
prompt/diff workflows.
  * Added keyboard support for running and Explain analysis.
* **Bug Fixes**
  * Restored editor focus after accepting or discarding AI changes.
  * Improved handling and validation of SQL Explain actions.
  * Preserved editor scroll position when switching snippets.
* **Refactor**
* Streamlined the SQLEditor into a composed layout and improved
memoization to reduce unnecessary re-renders.
<!-- 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