Spaces:
Sleeping
Sleeping
File size: 5,733 Bytes
7c87115 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | """Note CLI commands."""
from typing import Optional
import typer
from rich.console import Console
from rich.table import Table
from notebooklm_tools.core.alias import get_alias_manager
from notebooklm_tools.core.exceptions import NLMError
from notebooklm_tools.cli.utils import get_client
from notebooklm_tools.services import notes as notes_service, ServiceError
console = Console()
app = typer.Typer(
help="Manage notes",
rich_markup_mode="rich",
no_args_is_help=True,
)
@app.command("list")
def list_notes(
notebook_id: str = typer.Argument(..., help="Notebook ID"),
json_output: bool = typer.Option(False, "--json", "-j", help="Output as JSON"),
quiet: bool = typer.Option(False, "--quiet", "-q", help="Output IDs only"),
profile: Optional[str] = typer.Option(None, "--profile", "-p", help="Profile to use"),
) -> None:
"""List all notes in a notebook."""
try:
notebook_id = get_alias_manager().resolve(notebook_id)
with get_client(profile) as client:
result = notes_service.list_notes(client, notebook_id)
notes = result["notes"]
if quiet:
for note in notes:
console.print(note['id'])
elif json_output:
import json
console.print(json.dumps(result, indent=2))
else:
if not notes:
console.print(f"[dim]No notes found in notebook {notebook_id}[/dim]")
return
table = Table(title=f"Notes in Notebook {notebook_id}")
table.add_column("ID", style="cyan", no_wrap=True)
table.add_column("Title", style="green")
table.add_column("Preview", style="white")
for note in notes:
preview = note.get('preview', '')[:100]
if len(preview) == 100:
preview += "..."
table.add_row(note['id'][:8] + "...", note.get('title', 'Untitled'), preview)
console.print(table)
console.print(f"\n[dim]Total: {result['count']} note(s)[/dim]")
except ServiceError as e:
console.print(f"[red]Error:[/red] {e.user_message}")
raise typer.Exit(1)
except NLMError as e:
console.print(f"[red]Error:[/red] {e.message}")
if e.hint:
console.print(f"\n[dim]Hint: {e.hint}[/dim]")
raise typer.Exit(1)
@app.command("create")
def create_note(
notebook_id: str = typer.Argument(..., help="Notebook ID"),
content: str = typer.Option(..., "--content", "-c", help="Note content"),
title: str = typer.Option("New Note", "--title", "-t", help="Note title"),
profile: Optional[str] = typer.Option(None, "--profile", "-p", help="Profile to use"),
) -> None:
"""Create a new note in a notebook."""
try:
notebook_id = get_alias_manager().resolve(notebook_id)
with get_client(profile) as client:
result = notes_service.create_note(client, notebook_id, content, title)
console.print(f"[green]✓[/green] Note created: {result['note_id']}")
console.print(f" Title: {result['title']}")
console.print(f" Preview: {result['content_preview']}")
except ServiceError as e:
console.print(f"[red]Error:[/red] {e.user_message}")
raise typer.Exit(1)
except NLMError as e:
console.print(f"[red]Error:[/red] {e.message}")
if e.hint:
console.print(f"\n[dim]Hint: {e.hint}[/dim]")
raise typer.Exit(1)
@app.command("update")
def update_note(
notebook_id: str = typer.Argument(..., help="Notebook ID"),
note_id: str = typer.Argument(..., help="Note ID"),
content: Optional[str] = typer.Option(None, "--content", "-c", help="New content"),
title: Optional[str] = typer.Option(None, "--title", "-t", help="New title"),
profile: Optional[str] = typer.Option(None, "--profile", "-p", help="Profile to use"),
) -> None:
"""Update a note's content or title."""
try:
notebook_id = get_alias_manager().resolve(notebook_id)
with get_client(profile) as client:
result = notes_service.update_note(client, notebook_id, note_id, content, title)
console.print(f"[green]✓[/green] {result['message']}")
except ServiceError as e:
console.print(f"[red]Error:[/red] {e.user_message}")
raise typer.Exit(1)
except NLMError as e:
console.print(f"[red]Error:[/red] {e.message}")
if e.hint:
console.print(f"\n[dim]Hint: {e.hint}[/dim]")
raise typer.Exit(1)
@app.command("delete")
def delete_note(
notebook_id: str = typer.Argument(..., help="Notebook ID"),
note_id: str = typer.Argument(..., help="Note ID to delete"),
confirm: bool = typer.Option(False, "--confirm", "-y", help="Skip confirmation"),
profile: Optional[str] = typer.Option(None, "--profile", "-p", help="Profile to use"),
) -> None:
"""Delete a note permanently."""
if not confirm:
confirmed = typer.confirm(f"Delete note {note_id}? This action is IRREVERSIBLE.")
if not confirmed:
console.print("[dim]Cancelled[/dim]")
raise typer.Exit(0)
try:
notebook_id = get_alias_manager().resolve(notebook_id)
with get_client(profile) as client:
result = notes_service.delete_note(client, notebook_id, note_id)
console.print(f"[green]✓[/green] {result['message']}")
except ServiceError as e:
console.print(f"[red]Error:[/red] {e.user_message}")
raise typer.Exit(1)
except NLMError as e:
console.print(f"[red]Error:[/red] {e.message}")
if e.hint:
console.print(f"\n[dim]Hint: {e.hint}[/dim]")
raise typer.Exit(1)
|