Repository navigation
Expand file tree
/
Copy pathnotes.py
More file actions
84 lines (68 loc) · 1.88 KB
/
Copy pathnotes.py
File metadata and controls
84 lines (68 loc) · 1.88 KB
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
from dataclasses import dataclass
from datetime import datetime
from uuid import UUID
from autogen.beta import Toolkit, tool
from dishka_ag2 import FromDishka, inject
from ag2_example.domain.entities import Note, NoteId
from ag2_example.usecases import (
CreateNoteRequest,
CreateNoteUseCase,
DeleteNoteRequest,
DeleteNoteUseCase,
ListNotesRequest,
ListNotesUseCase,
)
@dataclass(slots=True, frozen=True)
class NoteToolResult:
id: UUID
title: str
body: str
created_at: datetime
@classmethod
def from_entity(cls, note: Note) -> "NoteToolResult":
return cls(
id=note.id,
title=note.title,
body=note.body,
created_at=note.created_at,
)
@tool
@inject
async def list_notes(
uc: FromDishka[ListNotesUseCase],
limit: int = 20,
) -> list[NoteToolResult]:
"""List notes stored in the database.
Args:
limit: maximum number of notes to return (newest first).
"""
response = await uc.execute(ListNotesRequest(limit=limit))
return [NoteToolResult.from_entity(n) for n in response.notes]
@tool
@inject
async def create_note(
uc: FromDishka[CreateNoteUseCase],
title: str,
body: str = "",
) -> NoteToolResult:
"""Create a new note in the database.
Args:
title: short title of the note.
body: full text body (optional).
"""
response = await uc.execute(CreateNoteRequest(title=title, body=body))
return NoteToolResult.from_entity(response.note)
@tool
@inject
async def delete_note(
uc: FromDishka[DeleteNoteUseCase],
note_id: str,
) -> str:
"""Delete a note by its UUID.
Args:
note_id: UUID of the note to delete.
"""
await uc.execute(DeleteNoteRequest(note_id=NoteId(UUID(note_id))))
return "deleted"
def notes_toolkit() -> Toolkit:
return Toolkit(list_notes, create_note, delete_note)