Spaces:
Sleeping
Sleeping
File size: 8,030 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | """Notebook CLI commands."""
from typing import Optional
import typer
from rich.console import Console
from notebooklm_tools.core.alias import get_alias_manager
from notebooklm_tools.core.exceptions import NLMError
from notebooklm_tools.cli.formatters import detect_output_format, get_formatter
from notebooklm_tools.cli.utils import get_client
from notebooklm_tools.services import (
notebooks as notebooks_service,
chat as chat_service,
ServiceError,
)
console = Console()
app = typer.Typer(
help="Manage notebooks",
rich_markup_mode="rich",
no_args_is_help=True,
)
@app.command("list")
def list_notebooks(
full: bool = typer.Option(False, "--full", "-a", help="Show all columns"),
json_output: bool = typer.Option(False, "--json", "-j", help="Output as JSON"),
quiet: bool = typer.Option(False, "--quiet", "-q", help="Output IDs only"),
title: bool = typer.Option(False, "--title", "-t", help="Show ID: Title format"),
profile: Optional[str] = typer.Option(None, "--profile", "-p", help="Profile to use"),
) -> None:
"""List all notebooks."""
try:
with get_client(profile) as client:
notebooks = client.list_notebooks()
fmt = detect_output_format(json_output, quiet, title)
formatter = get_formatter(fmt, console)
formatter.format_notebooks(notebooks, full=full, title_only=title)
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_notebook(
title: str = typer.Argument("", help="Notebook title"),
profile: Optional[str] = typer.Option(None, "--profile", "-p", help="Profile to use"),
) -> None:
"""Create a new notebook."""
try:
with get_client(profile) as client:
result = notebooks_service.create_notebook(client, title)
console.print(f"[green]✓[/green] {result['message']}")
console.print(f" ID: {result['notebook_id']}")
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("get")
def get_notebook(
notebook_id: str = typer.Argument(..., help="Notebook ID"),
json_output: bool = typer.Option(False, "--json", "-j", help="Output as JSON"),
profile: Optional[str] = typer.Option(None, "--profile", "-p", help="Profile to use"),
) -> None:
"""Get notebook details."""
try:
notebook_id = get_alias_manager().resolve(notebook_id)
with get_client(profile) as client:
result = notebooks_service.get_notebook(client, notebook_id)
fmt = detect_output_format(json_output)
formatter = get_formatter(fmt, console)
formatter.format_item(result, title="Notebook Details")
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("describe")
def describe_notebook(
notebook_id: str = typer.Argument(..., help="Notebook ID"),
json_output: bool = typer.Option(False, "--json", "-j", help="Output as JSON"),
profile: Optional[str] = typer.Option(None, "--profile", "-p", help="Profile to use"),
) -> None:
"""Get AI-generated notebook summary with suggested topics."""
try:
notebook_id = get_alias_manager().resolve(notebook_id)
with get_client(profile) as client:
result = notebooks_service.describe_notebook(client, notebook_id)
fmt = detect_output_format(json_output)
formatter = get_formatter(fmt, console)
formatter.format_item(result, title="Notebook Summary")
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("rename")
def rename_notebook(
notebook_id: str = typer.Argument(..., help="Notebook ID"),
new_title: str = typer.Argument(..., help="New title"),
profile: Optional[str] = typer.Option(None, "--profile", "-p", help="Profile to use"),
) -> None:
"""Rename a notebook."""
try:
notebook_id = get_alias_manager().resolve(notebook_id)
with get_client(profile) as client:
result = notebooks_service.rename_notebook(client, notebook_id, new_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_notebook(
notebook_id: str = typer.Argument(..., help="Notebook ID"),
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 notebook permanently."""
notebook_id = get_alias_manager().resolve(notebook_id)
if not confirm:
typer.confirm(
f"Are you sure you want to delete notebook {notebook_id}?",
abort=True,
)
try:
with get_client(profile) as client:
result = notebooks_service.delete_notebook(client, notebook_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)
@app.command("query")
def query_notebook(
notebook_id: str = typer.Argument(..., help="Notebook ID"),
question: str = typer.Argument(..., help="Question to ask"),
json_output: bool = typer.Option(False, "--json", "-j", help="Output as JSON"),
conversation_id: Optional[str] = typer.Option(
None, "--conversation-id", "-c",
help="Conversation ID for follow-up questions",
),
source_ids: Optional[str] = typer.Option(
None, "--source-ids", "-s",
help="Comma-separated source IDs to query (default: all)",
),
profile: Optional[str] = typer.Option(None, "--profile", "-p", help="Profile to use"),
) -> None:
"""Chat with notebook sources."""
try:
sources = source_ids.split(",") if source_ids else None
notebook_id = get_alias_manager().resolve(notebook_id)
with get_client(profile) as client:
result = chat_service.query(
client, notebook_id, question,
source_ids=sources,
conversation_id=conversation_id,
)
fmt = detect_output_format(json_output)
formatter = get_formatter(fmt, console)
formatter.format_item(result, title="Query Response")
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)
|