forked from microsoft/agent-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoice_data.py
More file actions
227 lines (198 loc) · 6.96 KB
/
Copy pathinvoice_data.py
File metadata and controls
227 lines (198 loc) · 6.96 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
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
216
217
218
219
220
221
222
223
224
225
226
227
# Copyright (c) Microsoft. All rights reserved.
"""Mock invoice data and tool functions for the A2A server sample.
Provides mock invoice data and query tools for the A2A server sample,
enabling invoice-related queries through the A2A protocol.
"""
import json
import random
from dataclasses import dataclass, field
from datetime import datetime, timedelta, timezone
from typing import Annotated
from agent_framework import tool
from pydantic import Field
@dataclass
class Product:
"""A product line item on an invoice."""
name: str
quantity: int
price_per_unit: float
@property
def total_price(self) -> float:
return self.quantity * self.price_per_unit
def to_dict(self) -> dict:
return {
"name": self.name,
"quantity": self.quantity,
"price_per_unit": self.price_per_unit,
"total_price": self.total_price,
}
@dataclass
class Invoice:
"""An invoice record with products."""
transaction_id: str
invoice_id: str
company_name: str
invoice_date: datetime
products: list[Product] = field(default_factory=list)
@property
def total_invoice_price(self) -> float:
return sum(p.total_price for p in self.products)
def to_dict(self) -> dict:
return {
"transaction_id": self.transaction_id,
"invoice_id": self.invoice_id,
"company_name": self.company_name,
"invoice_date": self.invoice_date.strftime("%Y-%m-%d"),
"products": [p.to_dict() for p in self.products],
"total_invoice_price": self.total_invoice_price,
}
def _random_date_within_last_two_months() -> datetime:
end_date = datetime.now(timezone.utc)
start_date = end_date - timedelta(days=60)
random_days = random.randint(0, 60)
return start_date + timedelta(days=random_days)
def _build_invoices() -> list[Invoice]:
"""Build 10 mock invoices."""
return [
Invoice(
"TICKET-XYZ987",
"INV789",
"Contoso",
_random_date_within_last_two_months(),
[
Product("T-Shirts", 150, 10.00),
Product("Hats", 200, 15.00),
Product("Glasses", 300, 5.00),
],
),
Invoice(
"TICKET-XYZ111",
"INV111",
"XStore",
_random_date_within_last_two_months(),
[
Product("T-Shirts", 2500, 12.00),
Product("Hats", 1500, 8.00),
Product("Glasses", 200, 20.00),
],
),
Invoice(
"TICKET-XYZ222",
"INV222",
"Cymbal Direct",
_random_date_within_last_two_months(),
[
Product("T-Shirts", 1200, 14.00),
Product("Hats", 800, 7.00),
Product("Glasses", 500, 25.00),
],
),
Invoice(
"TICKET-XYZ333",
"INV333",
"Contoso",
_random_date_within_last_two_months(),
[
Product("T-Shirts", 400, 11.00),
Product("Hats", 600, 15.00),
Product("Glasses", 700, 5.00),
],
),
Invoice(
"TICKET-XYZ444",
"INV444",
"XStore",
_random_date_within_last_two_months(),
[
Product("T-Shirts", 800, 10.00),
Product("Hats", 500, 18.00),
Product("Glasses", 300, 22.00),
],
),
Invoice(
"TICKET-XYZ555",
"INV555",
"Cymbal Direct",
_random_date_within_last_two_months(),
[
Product("T-Shirts", 1100, 9.00),
Product("Hats", 900, 12.00),
Product("Glasses", 1200, 15.00),
],
),
Invoice(
"TICKET-XYZ666",
"INV666",
"Contoso",
_random_date_within_last_two_months(),
[
Product("T-Shirts", 2500, 8.00),
Product("Hats", 1200, 10.00),
Product("Glasses", 1000, 6.00),
],
),
Invoice(
"TICKET-XYZ777",
"INV777",
"XStore",
_random_date_within_last_two_months(),
[
Product("T-Shirts", 1900, 13.00),
Product("Hats", 1300, 16.00),
Product("Glasses", 800, 19.00),
],
),
Invoice(
"TICKET-XYZ888",
"INV888",
"Cymbal Direct",
_random_date_within_last_two_months(),
[
Product("T-Shirts", 2200, 11.00),
Product("Hats", 1700, 8.50),
Product("Glasses", 600, 21.00),
],
),
Invoice(
"TICKET-XYZ999",
"INV999",
"Contoso",
_random_date_within_last_two_months(),
[
Product("T-Shirts", 1400, 10.50),
Product("Hats", 1100, 9.00),
Product("Glasses", 950, 12.00),
],
),
]
# Module-level singleton so dates are stable for the lifetime of the server
INVOICES = _build_invoices()
@tool(approval_mode="never_require")
def query_invoices(
company_name: Annotated[str, Field(description="The company name to filter invoices by.")],
start_date: Annotated[str | None, Field(description="Optional start date (YYYY-MM-DD) to filter invoices.")] = None,
end_date: Annotated[str | None, Field(description="Optional end date (YYYY-MM-DD) to filter invoices.")] = None,
) -> str:
"""Retrieves invoices for the specified company and optionally within the specified time range."""
results = [i for i in INVOICES if i.company_name.lower() == company_name.lower()]
if start_date:
start = datetime.strptime(start_date, "%Y-%m-%d").replace(tzinfo=timezone.utc)
results = [i for i in results if i.invoice_date >= start]
if end_date:
end = datetime.strptime(end_date, "%Y-%m-%d").replace(tzinfo=timezone.utc) + timedelta(days=1)
results = [i for i in results if i.invoice_date < end]
return json.dumps([i.to_dict() for i in results], indent=2)
@tool(approval_mode="never_require")
def query_by_transaction_id(
transaction_id: Annotated[str, Field(description="The transaction ID to look up (e.g. TICKET-XYZ987).")],
) -> str:
"""Retrieves invoice using the transaction id."""
results = [i for i in INVOICES if i.transaction_id.lower() == transaction_id.lower()]
return json.dumps([i.to_dict() for i in results], indent=2)
@tool(approval_mode="never_require")
def query_by_invoice_id(
invoice_id: Annotated[str, Field(description="The invoice ID to look up (e.g. INV789).")],
) -> str:
"""Retrieves invoice using the invoice id."""
results = [i for i in INVOICES if i.invoice_id.lower() == invoice_id.lower()]
return json.dumps([i.to_dict() for i in results], indent=2)