Spaces:
Sleeping

File size: 5,201 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
"""Notes service β€” shared business logic for note CRUD operations."""

from typing import TypedDict, Optional

from ..core.client import NotebookLMClient
from .errors import ValidationError, ServiceError, NotFoundError


class NoteInfo(TypedDict):
    """Note details."""
    id: str
    title: str
    preview: str


class NoteListResult(TypedDict):
    """Result of listing notes."""
    notebook_id: str
    notes: list[NoteInfo]
    count: int


class NoteCreateResult(TypedDict):
    """Result of creating a note."""
    note_id: str
    title: str
    content_preview: str
    message: str


class NoteUpdateResult(TypedDict):
    """Result of updating a note."""
    note_id: str
    updated: bool
    message: str


class NoteDeleteResult(TypedDict):
    """Result of deleting a note."""
    note_id: str
    message: str


def list_notes(client: NotebookLMClient, notebook_id: str) -> NoteListResult:
    """List all notes in a notebook.

    Args:
        client: Authenticated NotebookLM client
        notebook_id: Notebook UUID

    Returns:
        NoteListResult with note list and count

    Raises:
        ServiceError: If the API call fails
    """
    try:
        notes = client.list_notes(notebook_id)
        return {
            "notebook_id": notebook_id,
            "notes": notes,
            "count": len(notes),
        }
    except Exception as e:
        raise ServiceError(f"Failed to list notes: {e}")


def create_note(
    client: NotebookLMClient,
    notebook_id: str,
    content: str,
    title: Optional[str] = None,
) -> NoteCreateResult:
    """Create a new note in a notebook.

    Args:
        client: Authenticated NotebookLM client
        notebook_id: Notebook UUID
        content: Note content (required)
        title: Note title (defaults to "New Note")

    Returns:
        NoteCreateResult with note ID and preview

    Raises:
        ValidationError: If content is empty
        ServiceError: If creation fails
    """
    if not content or not content.strip():
        raise ValidationError(
            "Content is required to create a note.",
            user_message="Note content cannot be empty.",
        )

    effective_title = title or "New Note"

    try:
        result = client.create_note(notebook_id, content, effective_title)
    except Exception as e:
        raise ServiceError(f"Failed to create note: {e}")

    if result and result.get("id"):
        preview = content[:100] + ("..." if len(content) > 100 else "")
        return {
            "note_id": result["id"],
            "title": result.get("title", effective_title),
            "content_preview": preview,
            "message": f"Note '{result.get('title', effective_title)}' created.",
        }

    raise ServiceError(
        "Note creation returned no ID",
        user_message="Failed to create note β€” no confirmation from API.",
    )


def update_note(
    client: NotebookLMClient,
    notebook_id: str,
    note_id: str,
    content: Optional[str] = None,
    title: Optional[str] = None,
) -> NoteUpdateResult:
    """Update a note's content and/or title.

    Args:
        client: Authenticated NotebookLM client
        notebook_id: Notebook UUID
        note_id: Note UUID
        content: New content (optional)
        title: New title (optional)

    Returns:
        NoteUpdateResult

    Raises:
        ValidationError: If neither content nor title is provided
        ServiceError: If update fails
    """
    if content is None and title is None:
        raise ValidationError(
            "Must provide content or title to update.",
            user_message="Provide at least --content or --title to update.",
        )

    try:
        result = client.update_note(note_id, content, title, notebook_id)
    except Exception as e:
        raise ServiceError(f"Failed to update note: {e}")

    if result:
        parts = []
        if title:
            parts.append(f"title β†’ '{title}'")
        if content:
            parts.append(f"content ({len(content)} chars)")
        detail = ", ".join(parts) if parts else "updated"
        return {
            "note_id": note_id,
            "updated": True,
            "message": f"Note updated: {detail}.",
        }

    raise ServiceError(
        "Note update returned falsy result",
        user_message="Failed to update note β€” no confirmation from API.",
    )


def delete_note(
    client: NotebookLMClient,
    notebook_id: str,
    note_id: str,
) -> NoteDeleteResult:
    """Delete a note permanently.

    Args:
        client: Authenticated NotebookLM client
        notebook_id: Notebook UUID
        note_id: Note UUID

    Returns:
        NoteDeleteResult

    Raises:
        ServiceError: If deletion fails
    """
    try:
        result = client.delete_note(note_id, notebook_id)
    except Exception as e:
        raise ServiceError(f"Failed to delete note: {e}")

    if result:
        return {
            "note_id": note_id,
            "message": f"Note {note_id} deleted permanently.",
        }

    raise ServiceError(
        "Note deletion returned falsy result",
        user_message="Failed to delete note β€” no confirmation from API.",
    )