Skip to content

fix: auto-set context to newly created notebook#220

Open
netzwichte1 wants to merge 1 commit intoteng-lin:mainfrom
netzwichte1:fix/create-auto-set-context
Open

fix: auto-set context to newly created notebook#220
netzwichte1 wants to merge 1 commit intoteng-lin:mainfrom
netzwichte1:fix/create-auto-set-context

Conversation

@netzwichte1
Copy link
Copy Markdown

@netzwichte1 netzwichte1 commented Mar 23, 2026

Summary

  • notebooklm create now automatically sets the CLI context to the newly created notebook
  • Previously, after create, subsequent commands like source add would target the previously active notebook — not the one just created
  • Added set_current_notebook() call after notebook creation, matching the behavior of the use command

Problem

notebooklm create "My Research"    # Creates notebook, but context stays on old one
notebooklm source add "https://..."  # ❌ Added to the PREVIOUS notebook!

Fix

notebooklm create "My Research"    # Creates notebook AND sets context
notebooklm source add "https://..."  # ✅ Added to "My Research"

Changes

  • src/notebooklm/cli/notebook.py: Import set_current_notebook and call it after client.notebooks.create() in create_cmd()

Test plan

  • notebooklm create "Test" --json → verify context switches (check with notebooklm status)
  • notebooklm create "Test" (non-JSON) → verify "Context set to new notebook" message
  • notebooklm source add <url> after create → verify source lands in new notebook

Summary by CodeRabbit

Release Notes

  • New Features
    • Notebooks created through the CLI are now automatically set as the current notebook, with a confirmation message displayed to confirm the context switch.

After `notebooklm create`, subsequent commands like `source add` would
target the previously active notebook instead of the newly created one,
because `create_cmd()` did not call `set_current_notebook()`.

This adds a `set_current_notebook()` call after notebook creation,
matching the behavior of the `use` command. This ensures that
`source add`, `ask`, and other context-dependent commands target the
correct notebook without requiring a manual `notebooklm use` step.
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a user experience issue where creating a new notebook via the CLI did not automatically switch the active context to that new notebook. This led to confusion and incorrect operations when subsequent commands were executed. The change ensures that after a notebook is created, the CLI's context is immediately updated, streamlining the workflow and making the tool more intuitive to use.

Highlights

  • Automatic Context Switching: The notebooklm create command now automatically sets the CLI context to the newly created notebook, preventing subsequent commands from targeting an unintended, previously active notebook.
  • Improved User Experience: A set_current_notebook() call was added after notebook creation, aligning its behavior with the use command and providing immediate feedback to the user about the context change.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 23, 2026

📝 Walkthrough

Walkthrough

The create_cmd function in the notebook CLI module now calls set_current_notebook after successfully creating a notebook, automatically setting the new notebook as the current context and displaying a confirmation message to the user.

Changes

Cohort / File(s) Summary
Notebook CLI Enhancement
src/notebooklm/cli/notebook.py
Added set_current_notebook import and integrated automatic context-switching in create_cmd. After notebook creation, the command now computes a formatted created_str from created_at and calls the helper to set the newly created notebook as current, followed by a user-facing confirmation message (non-JSON mode).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 ✨ A notebook springs to life,
Fresh and new, without strife!
Set as current, context flows,
"Now you're focused," the CLI knows.
One small hop, one giant scope! 📔

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: auto-setting the CLI context to a newly created notebook. It directly matches the primary objective and behavior modification in the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a helpful usability improvement by automatically setting the CLI context to a newly created notebook. The implementation is mostly correct, but there is a potential for a runtime error. I've identified a case where accessing nb.is_owner could fail if the API response for creating a notebook doesn't include this attribute. My suggestion is to assume ownership is True for a newly created notebook, which makes the implementation more robust. It's also worth noting that the associated unit tests were not updated to reflect these changes, which is something to address.


# Auto-switch context to the newly created notebook
created_str = nb.created_at.strftime("%Y-%m-%d") if nb.created_at else None
set_current_notebook(nb.id, nb.title, nb.is_owner, created_str)
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.

high

When a notebook is created, the current user is always the owner. It's safer to explicitly pass True for is_owner rather than relying on nb.is_owner. The create API endpoint might not return the is_owner field, which could cause an AttributeError. The existing unit tests for this command also mock a Notebook object without this attribute, suggesting it might not be present.

Suggested change
set_current_notebook(nb.id, nb.title, nb.is_owner, created_str)
set_current_notebook(nb.id, nb.title, True, created_str)

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

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 the current code and only fix it if needed.

Inline comments:
In `@src/notebooklm/cli/notebook.py`:
- Line 102: The console.print call in notebook.py uses an unnecessary f-string:
locate the console.print(f"[dim]Context set to new notebook[/dim]") invocation
(e.g., in the function or block that prints the context change) and remove the
leading "f" so the literal string is a normal string:
console.print("[dim]Context set to new notebook[/dim]"). This eliminates the
unused f-string and resolves the Ruff F541 warning.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9b92e416-1bf6-4cc5-b5b8-7352e8ec9a6c

📥 Commits

Reviewing files that changed from the base of the PR and between ddb4dee and cf09e7d.

📒 Files selected for processing (1)
  • src/notebooklm/cli/notebook.py

return

console.print(f"[green]Created notebook:[/green] {nb.id} - {nb.title}")
console.print(f"[dim]Context set to new notebook[/dim]")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Remove unnecessary f-string prefix.

This string has no placeholders, so the f prefix is extraneous. Ruff flags this as F541.

🔧 Proposed fix
-                console.print(f"[dim]Context set to new notebook[/dim]")
+                console.print("[dim]Context set to new notebook[/dim]")
🧰 Tools
🪛 Ruff (0.15.6)

[error] 102-102: f-string without any placeholders

Remove extraneous f prefix

(F541)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/notebooklm/cli/notebook.py` at line 102, The console.print call in
notebook.py uses an unnecessary f-string: locate the
console.print(f"[dim]Context set to new notebook[/dim]") invocation (e.g., in
the function or block that prints the context change) and remove the leading "f"
so the literal string is a normal string: console.print("[dim]Context set to new
notebook[/dim]"). This eliminates the unused f-string and resolves the Ruff F541
warning.

@teng-lin teng-lin added the bug Something isn't working label May 3, 2026
@teng-lin
Copy link
Copy Markdown
Owner

teng-lin commented May 3, 2026

Behavior is reasonable. Two things blocking merge: (1) ruff format src/ tests/ is reformatting one file in CI — please run it locally and commit the result; (2) please add a CLI test that calls notebook create and asserts the active context is set to the new notebook. After those, this should go in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants