agentboxd 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
agentboxd/_requests.py ADDED
@@ -0,0 +1,1364 @@
1
+ """Transport-independent request descriptions shared by the sync and async clients."""
2
+
3
+ from collections.abc import Mapping, Sequence
4
+ from dataclasses import dataclass, field
5
+ from datetime import datetime, timezone
6
+ from typing import Any, Literal, Optional, Union, cast
7
+ from urllib.parse import quote
8
+
9
+ from ._types import (
10
+ AttachmentInput,
11
+ Direction,
12
+ ListDirection,
13
+ ListKind,
14
+ MessageChannel,
15
+ MessageType,
16
+ MetadataValue,
17
+ MetricsBucket,
18
+ WebhookEventType,
19
+ WebhookPayload,
20
+ )
21
+
22
+ __all__ = ["NOT_GIVEN", "NotGiven", "Request"]
23
+
24
+
25
+ class NotGiven:
26
+ """Sentinel for "argument not passed", used where ``None`` is a meaningful value."""
27
+
28
+ _instance: Optional["NotGiven"] = None
29
+
30
+ def __new__(cls) -> "NotGiven":
31
+ if cls._instance is None:
32
+ cls._instance = super().__new__(cls)
33
+ return cls._instance
34
+
35
+ def __bool__(self) -> bool:
36
+ return False
37
+
38
+ def __repr__(self) -> str:
39
+ return "NOT_GIVEN"
40
+
41
+
42
+ NOT_GIVEN = NotGiven()
43
+
44
+ _NO_BODY: Any = NOT_GIVEN
45
+
46
+ Recipients = Union[str, Sequence[str]]
47
+ QueryValue = Union[str, int, float, bool, None]
48
+ MetadataPatch = Mapping[str, MetadataValue]
49
+ MetadataFilter = Mapping[str, Union[str, int, float, bool]]
50
+ Nullable = Union[str, None, NotGiven]
51
+
52
+
53
+ @dataclass(frozen=True)
54
+ class Request:
55
+ method: str
56
+ path: str
57
+ params: dict[str, str] = field(default_factory=dict)
58
+ json: Any = _NO_BODY
59
+ idempotency_key: Optional[str] = None
60
+ wait_seconds: Optional[float] = None
61
+ """Server-side long-poll duration; the HTTP read timeout is extended to cover it."""
62
+
63
+ @property
64
+ def has_body(self) -> bool:
65
+ return not isinstance(self.json, NotGiven)
66
+
67
+
68
+ def _seg(value: str) -> str:
69
+ """One path segment: "/" is escaped, and "." and ".." are refused (URLs resolve them to another route)."""
70
+ text = str(value)
71
+ if text in (".", ".."):
72
+ raise ValueError(f"{text!r} is not a valid id")
73
+ return quote(text, safe="")
74
+
75
+
76
+ def _query(values: Mapping[str, QueryValue]) -> dict[str, str]:
77
+ out: dict[str, str] = {}
78
+ for key, value in values.items():
79
+ if value is None:
80
+ continue
81
+ if isinstance(value, bool):
82
+ out[key] = "true" if value else "false"
83
+ else:
84
+ out[key] = str(value)
85
+ return out
86
+
87
+
88
+ def _body(values: Mapping[str, Any]) -> dict[str, Any]:
89
+ """Drop arguments that were not given (``None`` or ``NOT_GIVEN``)."""
90
+ return {k: v for k, v in values.items() if v is not None and not isinstance(v, NotGiven)}
91
+
92
+
93
+ def _recipients(value: Optional[Recipients]) -> Union[str, list[str], None]:
94
+ if value is None or isinstance(value, str):
95
+ return value
96
+ return list(value)
97
+
98
+
99
+ def _list(value: Optional[Sequence[Any]]) -> Optional[list[Any]]:
100
+ return None if value is None else list(value)
101
+
102
+
103
+ def _metadata(value: Optional[MetadataPatch]) -> Optional[dict[str, MetadataValue]]:
104
+ return None if value is None else dict(value)
105
+
106
+
107
+ def _metadata_query(value: Optional[MetadataFilter]) -> dict[str, str]:
108
+ """``{"plan": "pro"}`` -> ``{"metadata.plan": "pro"}`` (exact match; booleans as ``true``/``false``)."""
109
+ return _query({f"metadata.{k}": v for k, v in (value or {}).items()})
110
+
111
+
112
+ def _patch(values: Mapping[str, Any], nullable: Sequence[str] = ()) -> dict[str, Any]:
113
+ """Like ``_body``, but keys in ``nullable`` are sent as JSON null when passed as ``None``
114
+ (they default to ``NOT_GIVEN``)."""
115
+ body = _body(values)
116
+ for key in nullable:
117
+ if key in values and values[key] is None:
118
+ body[key] = None
119
+ return body
120
+
121
+
122
+ # ---------- inboxes ----------
123
+
124
+
125
+ def inbox_create(
126
+ username: Optional[str],
127
+ display_name: Optional[str],
128
+ client_id: Optional[str],
129
+ metadata: Optional[MetadataPatch] = None,
130
+ domain: Optional[str] = None,
131
+ ttl_seconds: Optional[int] = None,
132
+ card: Optional[Mapping[str, Any]] = None,
133
+ ) -> Request:
134
+ body = _body(
135
+ {
136
+ "username": username,
137
+ "display_name": display_name,
138
+ "client_id": client_id,
139
+ "metadata": _metadata(metadata),
140
+ "domain": domain,
141
+ "ttl_seconds": ttl_seconds,
142
+ "card": dict(card) if card is not None else None,
143
+ }
144
+ )
145
+ return Request("POST", "/v1/inboxes", json=body)
146
+
147
+
148
+ def inbox_list(
149
+ cursor: Optional[str],
150
+ limit: Optional[int],
151
+ metadata: Optional[MetadataFilter] = None,
152
+ include_temporary: Optional[bool] = None,
153
+ temporary: Optional[bool] = None,
154
+ ) -> Request:
155
+ params = {
156
+ **_query(
157
+ {"cursor": cursor, "limit": limit, "include_temporary": include_temporary, "temporary": temporary}
158
+ ),
159
+ **_metadata_query(metadata),
160
+ }
161
+ return Request("GET", "/v1/inboxes", params=params)
162
+
163
+
164
+ def inbox_update(
165
+ inbox_id: str,
166
+ display_name: Nullable,
167
+ metadata: Optional[MetadataPatch],
168
+ ttl_seconds: Optional[int] = None,
169
+ promotions_filter: Union[bool, NotGiven, None] = NOT_GIVEN,
170
+ ) -> Request:
171
+ body = _patch(
172
+ {
173
+ "display_name": display_name,
174
+ "metadata": _metadata(metadata),
175
+ "ttl_seconds": ttl_seconds,
176
+ "promotions_filter": promotions_filter,
177
+ },
178
+ nullable=("display_name", "promotions_filter"),
179
+ )
180
+ return Request("PATCH", f"/v1/inboxes/{_seg(inbox_id)}", json=body)
181
+
182
+
183
+ def inbox_get(inbox_id: str) -> Request:
184
+ return Request("GET", f"/v1/inboxes/{_seg(inbox_id)}")
185
+
186
+
187
+ def inbox_delete(inbox_id: str) -> Request:
188
+ return Request("DELETE", f"/v1/inboxes/{_seg(inbox_id)}")
189
+
190
+
191
+ def inbox_pause(inbox_id: str, reason: Optional[str] = None) -> Request:
192
+ return Request("POST", f"/v1/inboxes/{_seg(inbox_id)}/pause", json=_body({"reason": reason}))
193
+
194
+
195
+ def inbox_resume(inbox_id: str) -> Request:
196
+ return Request("POST", f"/v1/inboxes/{_seg(inbox_id)}/resume")
197
+
198
+
199
+ # ---------- identity-only agents ----------
200
+
201
+
202
+ def identity_create(
203
+ username: Optional[str],
204
+ display_name: Optional[str],
205
+ client_id: Optional[str],
206
+ metadata: Optional[MetadataPatch] = None,
207
+ card: Optional[Mapping[str, Any]] = None,
208
+ ) -> Request:
209
+ body = _body(
210
+ {
211
+ "username": username,
212
+ "display_name": display_name,
213
+ "client_id": client_id,
214
+ "metadata": _metadata(metadata),
215
+ "card": dict(card) if card is not None else None,
216
+ }
217
+ )
218
+ return Request("POST", "/v1/identities", json=body)
219
+
220
+
221
+ def identity_list(
222
+ cursor: Optional[str], limit: Optional[int], metadata: Optional[MetadataFilter] = None
223
+ ) -> Request:
224
+ params = {**_query({"cursor": cursor, "limit": limit}), **_metadata_query(metadata)}
225
+ return Request("GET", "/v1/identities", params=params)
226
+
227
+
228
+ def identity_get(identity_id: str) -> Request:
229
+ return Request("GET", f"/v1/identities/{_seg(identity_id)}")
230
+
231
+
232
+ def identity_update(identity_id: str, display_name: Nullable, metadata: Optional[MetadataPatch]) -> Request:
233
+ body = _patch({"display_name": display_name, "metadata": _metadata(metadata)}, nullable=("display_name",))
234
+ return Request("PATCH", f"/v1/identities/{_seg(identity_id)}", json=body)
235
+
236
+
237
+ def identity_delete(identity_id: str) -> Request:
238
+ return Request("DELETE", f"/v1/identities/{_seg(identity_id)}")
239
+
240
+
241
+ # ---------- agent cards and the directory (aSIM) ----------
242
+
243
+
244
+ def agent_get(inbox_id: str) -> Request:
245
+ return Request("GET", f"/v1/inboxes/{_seg(inbox_id)}/agent")
246
+
247
+
248
+ def agent_update(inbox_id: str, card: Mapping[str, Any]) -> Request:
249
+ return Request("PATCH", f"/v1/inboxes/{_seg(inbox_id)}/agent", json=dict(card))
250
+
251
+
252
+ def agent_delete(inbox_id: str) -> Request:
253
+ return Request("DELETE", f"/v1/inboxes/{_seg(inbox_id)}/agent")
254
+
255
+
256
+ def agent_revoke(inbox_id: str, reason: Optional[str]) -> Request:
257
+ return Request("POST", f"/v1/inboxes/{_seg(inbox_id)}/agent/revoke", json=_body({"reason": reason}))
258
+
259
+
260
+ def agent_restore(inbox_id: str) -> Request:
261
+ return Request("POST", f"/v1/inboxes/{_seg(inbox_id)}/agent/restore")
262
+
263
+
264
+ def agent_oasf(inbox_id: str) -> Request:
265
+ return Request("GET", f"/v1/inboxes/{_seg(inbox_id)}/agent/oasf")
266
+
267
+
268
+ def agent_keys_list(inbox_id: str) -> Request:
269
+ return Request("GET", f"/v1/inboxes/{_seg(inbox_id)}/agent/keys")
270
+
271
+
272
+ def agent_keys_register(inbox_id: str, public_jwk: Mapping[str, Any], proof: str) -> Request:
273
+ return Request(
274
+ "POST",
275
+ f"/v1/inboxes/{_seg(inbox_id)}/agent/keys",
276
+ json={"public_jwk": dict(public_jwk), "proof": proof},
277
+ )
278
+
279
+
280
+ def agent_keys_retire(inbox_id: str, kid: str) -> Request:
281
+ return Request("POST", f"/v1/inboxes/{_seg(inbox_id)}/agent/keys/{_seg(kid)}/retire")
282
+
283
+
284
+ def agent_keys_revoke(inbox_id: str, kid: str, reason: Optional[str], since: Optional[str]) -> Request:
285
+ return Request(
286
+ "POST",
287
+ f"/v1/inboxes/{_seg(inbox_id)}/agent/keys/{_seg(kid)}/revoke",
288
+ json=_body({"reason": reason, "since": since}),
289
+ )
290
+
291
+
292
+ def directory_resolve(address: Optional[str] = None, handle: Optional[str] = None) -> Request:
293
+ if (address is None) == (handle is None):
294
+ raise ValueError("pass exactly one of address or handle")
295
+ params = {"address": address} if address is not None else {"handle": cast(str, handle)}
296
+ return Request("GET", "/v1/directory/resolve", params=params)
297
+
298
+
299
+ def directory_handle_get() -> Request:
300
+ return Request("GET", "/v1/directory/handle")
301
+
302
+
303
+ def directory_handle_set(handle: str) -> Request:
304
+ return Request("PUT", "/v1/directory/handle", json={"handle": handle})
305
+
306
+
307
+ def directory_handle_release() -> Request:
308
+ return Request("DELETE", "/v1/directory/handle")
309
+
310
+
311
+ def public_agents_search(
312
+ q: Optional[str],
313
+ capability: Optional[str],
314
+ type: Optional[MessageType],
315
+ limit: Optional[int],
316
+ cursor: Optional[str],
317
+ ) -> Request:
318
+ params = _query({"q": q, "capability": capability, "type": type, "limit": limit, "cursor": cursor})
319
+ return Request("GET", "/v1/public/agents", params=params)
320
+
321
+
322
+ def public_agent(address: str, document: str = "") -> Request:
323
+ suffix = f"/{document}" if document else ""
324
+ return Request("GET", f"/v1/public/agents/{_seg(address)}{suffix}")
325
+
326
+
327
+ def public_handle(workspace: str, agent: str) -> Request:
328
+ return Request("GET", f"/v1/public/handles/{_seg(workspace.lstrip('@'))}/{_seg(agent)}")
329
+
330
+
331
+ def directory_verify(signature: Optional[str], address: Optional[str]) -> Request:
332
+ if (signature is None) == (address is None):
333
+ raise ValueError("pass exactly one of signature or address")
334
+ return Request("POST", "/v1/directory/verify", json=_body({"signature": signature, "address": address}))
335
+
336
+
337
+ def directory_search(
338
+ q: Optional[str],
339
+ capability: Optional[str],
340
+ type: Optional[MessageType],
341
+ limit: Optional[int],
342
+ cursor: Optional[str],
343
+ scope: Optional[str] = None,
344
+ ) -> Request:
345
+ params = _query(
346
+ {"q": q, "capability": capability, "type": type, "limit": limit, "cursor": cursor, "scope": scope}
347
+ )
348
+ return Request("GET", "/v1/directory/search", params=params)
349
+
350
+
351
+ def directory_report(address: str, reason: str, details: Optional[str], message_id: Optional[str]) -> Request:
352
+ body = _body({"address": address, "reason": reason, "details": details, "message_id": message_id})
353
+ return Request("POST", "/v1/directory/reports", json=body)
354
+
355
+
356
+ # ---------- messages ----------
357
+
358
+
359
+ def message_send(
360
+ inbox_id: str,
361
+ to: Recipients,
362
+ subject: str,
363
+ text: Optional[str],
364
+ html: Optional[str],
365
+ cc: Optional[Recipients],
366
+ bcc: Optional[Recipients],
367
+ attachments: Optional[Sequence[AttachmentInput]],
368
+ labels: Optional[Sequence[str]],
369
+ idempotency_key: Optional[str],
370
+ data: Any = None,
371
+ type: Optional[MessageType] = None,
372
+ agent_signature: Optional[str] = None,
373
+ ) -> Request:
374
+ body = _body(
375
+ {
376
+ "to": _recipients(to),
377
+ "cc": _recipients(cc),
378
+ "bcc": _recipients(bcc),
379
+ "subject": subject,
380
+ "text": text,
381
+ "html": html,
382
+ "attachments": _list(attachments),
383
+ "labels": _list(labels),
384
+ "data": data,
385
+ "type": type,
386
+ "agent_signature": agent_signature,
387
+ }
388
+ )
389
+ return Request(
390
+ "POST", f"/v1/inboxes/{_seg(inbox_id)}/messages/send", json=body, idempotency_key=idempotency_key
391
+ )
392
+
393
+
394
+ def message_reply(
395
+ inbox_id: str,
396
+ message_id: str,
397
+ text: Optional[str],
398
+ html: Optional[str],
399
+ attachments: Optional[Sequence[AttachmentInput]],
400
+ reply_all: Optional[bool],
401
+ labels: Optional[Sequence[str]],
402
+ idempotency_key: Optional[str],
403
+ data: Any = None,
404
+ type: Optional[MessageType] = None,
405
+ agent_signature: Optional[str] = None,
406
+ ) -> Request:
407
+ body = _body(
408
+ {
409
+ "text": text,
410
+ "html": html,
411
+ "attachments": _list(attachments),
412
+ "reply_all": reply_all,
413
+ "labels": _list(labels),
414
+ "data": data,
415
+ "type": type,
416
+ "agent_signature": agent_signature,
417
+ }
418
+ )
419
+ return Request(
420
+ "POST",
421
+ f"/v1/inboxes/{_seg(inbox_id)}/messages/{_seg(message_id)}/reply",
422
+ json=body,
423
+ idempotency_key=idempotency_key,
424
+ )
425
+
426
+
427
+ def message_list(
428
+ inbox_id: str,
429
+ labels: Optional[Sequence[str]],
430
+ is_read: Optional[bool],
431
+ direction: Optional[Direction],
432
+ cursor: Optional[str],
433
+ limit: Optional[int],
434
+ include_blocked: Optional[bool] = None,
435
+ channel: Optional[MessageChannel] = None,
436
+ type: Optional[MessageType] = None,
437
+ screening: Optional[str] = None,
438
+ include_unscreened: Optional[bool] = None,
439
+ include_held: Optional[bool] = None,
440
+ promotions: Optional[Literal["include", "exclude", "only"]] = None,
441
+ status: Optional[str] = None,
442
+ ) -> Request:
443
+ params = _query(
444
+ {
445
+ "labels": None if labels is None else ",".join(labels),
446
+ "is_read": is_read,
447
+ "direction": direction,
448
+ "include_blocked": include_blocked,
449
+ "promotions": promotions,
450
+ "channel": channel,
451
+ "type": type,
452
+ "screening": screening,
453
+ "status": status,
454
+ "include_unscreened": include_unscreened,
455
+ "include_held": include_held,
456
+ "cursor": cursor,
457
+ "limit": limit,
458
+ }
459
+ )
460
+ return Request("GET", f"/v1/inboxes/{_seg(inbox_id)}/messages", params=params)
461
+
462
+
463
+ def message_get(
464
+ message_id: str, include_unscreened: Optional[bool] = None, include_held: Optional[bool] = None
465
+ ) -> Request:
466
+ params = _query({"include_unscreened": include_unscreened, "include_held": include_held})
467
+ return Request("GET", f"/v1/messages/{_seg(message_id)}", params=params)
468
+
469
+
470
+ def message_update(
471
+ message_id: str,
472
+ add_labels: Optional[Sequence[str]],
473
+ remove_labels: Optional[Sequence[str]],
474
+ is_read: Optional[bool],
475
+ ) -> Request:
476
+ body = _body({"add_labels": _list(add_labels), "remove_labels": _list(remove_labels), "is_read": is_read})
477
+ return Request("PATCH", f"/v1/messages/{_seg(message_id)}", json=body)
478
+
479
+
480
+ def message_release(message_id: str) -> Request:
481
+ return Request("POST", f"/v1/messages/{_seg(message_id)}/release")
482
+
483
+
484
+ def message_draft_reply(message_id: str, instructions: Optional[str], save: Optional[bool] = None) -> Request:
485
+ return Request(
486
+ "POST",
487
+ f"/v1/messages/{_seg(message_id)}/draft-reply",
488
+ json=_body({"instructions": instructions, "save": save}),
489
+ )
490
+
491
+
492
+ def message_attachment_text(
493
+ message_id: str, attachment_id: str, offset: Optional[int] = None, max_chars: Optional[int] = None
494
+ ) -> Request:
495
+ params = _query({"offset": offset, "max_chars": max_chars})
496
+ return Request(
497
+ "GET", f"/v1/messages/{_seg(message_id)}/attachments/{_seg(attachment_id)}/text", params=params
498
+ )
499
+
500
+
501
+ def message_extract_attachment(
502
+ message_id: str,
503
+ attachment_id: str,
504
+ schema: Union[str, Mapping[str, Any]],
505
+ instructions: Optional[str] = None,
506
+ ) -> Request:
507
+ return Request(
508
+ "POST",
509
+ f"/v1/messages/{_seg(message_id)}/attachments/{_seg(attachment_id)}/extract",
510
+ json=_body(
511
+ {"schema": schema if isinstance(schema, str) else dict(schema), "instructions": instructions}
512
+ ),
513
+ )
514
+
515
+
516
+ # ---------- drafts ----------
517
+
518
+ SendAt = Union[str, datetime]
519
+ """An ISO 8601 string or a timezone-aware ``datetime`` (naive datetimes are treated as UTC)."""
520
+ NullableSendAt = Union[str, datetime, None, NotGiven]
521
+ NullableText = Union[str, None, NotGiven]
522
+ DraftStatuses = Union[str, Sequence[str]]
523
+
524
+
525
+ def _iso(value: SendAt) -> str:
526
+ if isinstance(value, datetime):
527
+ if value.tzinfo is None:
528
+ value = value.replace(tzinfo=timezone.utc)
529
+ return value.isoformat().replace("+00:00", "Z")
530
+ return value
531
+
532
+
533
+ def _statuses(value: Optional[DraftStatuses]) -> Optional[str]:
534
+ if value is None or isinstance(value, str):
535
+ return value
536
+ return ",".join(value)
537
+
538
+
539
+ def _draft_path(inbox_id: str, draft_id: str) -> str:
540
+ return f"/v1/inboxes/{_seg(inbox_id)}/drafts/{_seg(draft_id)}"
541
+
542
+
543
+ def draft_create(
544
+ inbox_id: str,
545
+ to: Optional[Recipients] = None,
546
+ subject: Optional[str] = None,
547
+ text: Optional[str] = None,
548
+ html: Optional[str] = None,
549
+ cc: Optional[Recipients] = None,
550
+ bcc: Optional[Recipients] = None,
551
+ attachments: Optional[Sequence[AttachmentInput]] = None,
552
+ labels: Optional[Sequence[str]] = None,
553
+ metadata: Optional[MetadataPatch] = None,
554
+ reply_to_message_id: Optional[str] = None,
555
+ thread_id: Optional[str] = None,
556
+ reply_all: Optional[bool] = None,
557
+ send_at: Optional[SendAt] = None,
558
+ idempotency_key: Optional[str] = None,
559
+ data: Any = None,
560
+ type: Optional[MessageType] = None,
561
+ ) -> Request:
562
+ body = _body(
563
+ {
564
+ "to": _recipients(to),
565
+ "cc": _recipients(cc),
566
+ "bcc": _recipients(bcc),
567
+ "subject": subject,
568
+ "text": text,
569
+ "html": html,
570
+ "attachments": _list(attachments),
571
+ "labels": _list(labels),
572
+ "metadata": _metadata(metadata),
573
+ "reply_to_message_id": reply_to_message_id,
574
+ "thread_id": thread_id,
575
+ "reply_all": reply_all,
576
+ "send_at": None if send_at is None else _iso(send_at),
577
+ "data": data,
578
+ "type": type,
579
+ }
580
+ )
581
+ return Request("POST", f"/v1/inboxes/{_seg(inbox_id)}/drafts", json=body, idempotency_key=idempotency_key)
582
+
583
+
584
+ def draft_list(
585
+ inbox_id: str,
586
+ status: Optional[DraftStatuses],
587
+ thread_id: Optional[str],
588
+ cursor: Optional[str],
589
+ limit: Optional[int],
590
+ ) -> Request:
591
+ params = _query({"status": _statuses(status), "thread_id": thread_id, "cursor": cursor, "limit": limit})
592
+ return Request("GET", f"/v1/inboxes/{_seg(inbox_id)}/drafts", params=params)
593
+
594
+
595
+ def draft_list_all(
596
+ inbox_id: Optional[str],
597
+ status: Optional[DraftStatuses],
598
+ thread_id: Optional[str],
599
+ cursor: Optional[str],
600
+ limit: Optional[int],
601
+ ) -> Request:
602
+ """``GET /v1/drafts`` filtered by ``inbox_id`` as a query parameter."""
603
+ params = _query(
604
+ {
605
+ "inbox_id": inbox_id,
606
+ "status": _statuses(status),
607
+ "thread_id": thread_id,
608
+ "cursor": cursor,
609
+ "limit": limit,
610
+ }
611
+ )
612
+ return Request("GET", "/v1/drafts", params=params)
613
+
614
+
615
+ def draft_get(inbox_id: str, draft_id: str) -> Request:
616
+ return Request("GET", _draft_path(inbox_id, draft_id))
617
+
618
+
619
+ def draft_update(
620
+ inbox_id: str,
621
+ draft_id: str,
622
+ to: Optional[Recipients] = None,
623
+ subject: NullableText = NOT_GIVEN,
624
+ text: NullableText = NOT_GIVEN,
625
+ html: NullableText = NOT_GIVEN,
626
+ cc: Optional[Recipients] = None,
627
+ bcc: Optional[Recipients] = None,
628
+ attachments: Optional[Sequence[Mapping[str, Any]]] = None,
629
+ labels: Optional[Sequence[str]] = None,
630
+ metadata: Optional[MetadataPatch] = None,
631
+ send_at: NullableSendAt = NOT_GIVEN,
632
+ data: Any = NOT_GIVEN,
633
+ type: Optional[MessageType] = None,
634
+ ) -> Request:
635
+ body = _patch(
636
+ {
637
+ "to": _recipients(to),
638
+ "cc": _recipients(cc),
639
+ "bcc": _recipients(bcc),
640
+ "subject": subject,
641
+ "text": text,
642
+ "html": html,
643
+ "attachments": None if attachments is None else [dict(a) for a in attachments],
644
+ "labels": _list(labels),
645
+ "metadata": _metadata(metadata),
646
+ "send_at": _iso(send_at) if isinstance(send_at, (str, datetime)) else send_at,
647
+ "data": data,
648
+ "type": type,
649
+ },
650
+ nullable=("subject", "text", "html", "send_at", "data"),
651
+ )
652
+ return Request("PATCH", _draft_path(inbox_id, draft_id), json=body)
653
+
654
+
655
+ def draft_delete(inbox_id: str, draft_id: str) -> Request:
656
+ return Request("DELETE", _draft_path(inbox_id, draft_id))
657
+
658
+
659
+ def draft_send(inbox_id: str, draft_id: str, idempotency_key: Optional[str] = None) -> Request:
660
+ return Request("POST", f"{_draft_path(inbox_id, draft_id)}/send", idempotency_key=idempotency_key)
661
+
662
+
663
+ def draft_schedule(inbox_id: str, draft_id: str, send_at: SendAt) -> Request:
664
+ return Request("POST", f"{_draft_path(inbox_id, draft_id)}/schedule", json={"send_at": _iso(send_at)})
665
+
666
+
667
+ def draft_cancel(inbox_id: str, draft_id: str) -> Request:
668
+ return Request("POST", f"{_draft_path(inbox_id, draft_id)}/cancel")
669
+
670
+
671
+ def message_wait(
672
+ inbox_id: str,
673
+ timeout: float,
674
+ since: Optional[str],
675
+ from_: Optional[str],
676
+ subject: Optional[str],
677
+ direction: Optional[Direction],
678
+ channel: Optional[MessageChannel] = None,
679
+ type: Optional[MessageType] = None,
680
+ include_unscreened: Optional[bool] = None,
681
+ include_held: Optional[bool] = None,
682
+ ) -> Request:
683
+ params = _query(
684
+ {
685
+ "timeout": _num(timeout),
686
+ "since": since,
687
+ "from": from_,
688
+ "subject": subject,
689
+ "direction": direction,
690
+ "channel": channel,
691
+ "type": type,
692
+ "include_unscreened": include_unscreened,
693
+ "include_held": include_held,
694
+ }
695
+ )
696
+ return Request("GET", f"/v1/inboxes/{_seg(inbox_id)}/messages/wait", params=params, wait_seconds=timeout)
697
+
698
+
699
+ def message_claim(
700
+ inbox_id: str,
701
+ limit: Optional[int],
702
+ lease_seconds: Optional[int],
703
+ consumer: Optional[str],
704
+ wait: Optional[float],
705
+ enriched: Optional[bool],
706
+ since: Optional[str],
707
+ type: Optional[MessageType] = None,
708
+ channel: Optional[MessageChannel] = None,
709
+ analysis: Optional[str] = None,
710
+ include_unscreened: Optional[bool] = None,
711
+ include_held: Optional[bool] = None,
712
+ ) -> Request:
713
+ body = _body(
714
+ {
715
+ "limit": limit,
716
+ "lease_seconds": lease_seconds,
717
+ "consumer": consumer,
718
+ "wait": None if wait is None else _num(wait),
719
+ "enriched": enriched,
720
+ "since": since,
721
+ "type": type,
722
+ "channel": channel,
723
+ "analysis": analysis,
724
+ "include_unscreened": include_unscreened,
725
+ "include_held": include_held,
726
+ }
727
+ )
728
+ return Request(
729
+ "POST", f"/v1/inboxes/{_seg(inbox_id)}/messages/claim", json=body, wait_seconds=wait if wait else None
730
+ )
731
+
732
+
733
+ def message_ack(message_id: str, lease_id: str, mark_read: Optional[bool]) -> Request:
734
+ body = _body({"lease_id": lease_id, "mark_read": mark_read})
735
+ return Request("POST", f"/v1/messages/{_seg(message_id)}/ack", json=body)
736
+
737
+
738
+ def message_nack(message_id: str, lease_id: str, delay_seconds: Optional[int]) -> Request:
739
+ body = _body({"lease_id": lease_id, "delay_seconds": delay_seconds})
740
+ return Request("POST", f"/v1/messages/{_seg(message_id)}/nack", json=body)
741
+
742
+
743
+ def message_extend(message_id: str, lease_id: str, lease_seconds: Optional[int]) -> Request:
744
+ body = _body({"lease_id": lease_id, "lease_seconds": lease_seconds})
745
+ return Request("POST", f"/v1/messages/{_seg(message_id)}/extend", json=body)
746
+
747
+
748
+ def message_wait_for_verification(
749
+ inbox_id: str,
750
+ timeout: float,
751
+ since: Optional[str],
752
+ from_: Optional[str],
753
+ include_unauthenticated: Optional[bool] = None,
754
+ ) -> Request:
755
+ params = _query(
756
+ {
757
+ "timeout": _num(timeout),
758
+ "since": since,
759
+ "from": from_,
760
+ "include_unauthenticated": include_unauthenticated,
761
+ }
762
+ )
763
+ return Request("GET", f"/v1/inboxes/{_seg(inbox_id)}/verification", params=params, wait_seconds=timeout)
764
+
765
+
766
+ def _num(value: float) -> Union[int, float]:
767
+ return int(value) if float(value).is_integer() else value
768
+
769
+
770
+ # ---------- threads & search ----------
771
+
772
+
773
+ def _thread_filters(
774
+ cursor: Optional[str],
775
+ limit: Optional[int],
776
+ labels: Optional[Sequence[str]] = None,
777
+ before: Optional[str] = None,
778
+ after: Optional[str] = None,
779
+ participant: Optional[str] = None,
780
+ subject: Optional[str] = None,
781
+ ascending: Optional[bool] = None,
782
+ inbox_id: Optional[str] = None,
783
+ ) -> dict[str, str]:
784
+ return _query(
785
+ {
786
+ "cursor": cursor,
787
+ "limit": limit,
788
+ "labels": ",".join(labels) if labels else None,
789
+ "before": before,
790
+ "after": after,
791
+ "participant": participant,
792
+ "subject": subject,
793
+ "ascending": True if ascending else None,
794
+ "inbox_id": inbox_id,
795
+ }
796
+ )
797
+
798
+
799
+ def thread_list(inbox_id: str, cursor: Optional[str], limit: Optional[int], **filters: Any) -> Request:
800
+ params = _thread_filters(cursor, limit, **filters)
801
+ return Request("GET", f"/v1/inboxes/{_seg(inbox_id)}/threads", params=params)
802
+
803
+
804
+ def thread_list_all(cursor: Optional[str], limit: Optional[int], **filters: Any) -> Request:
805
+ return Request("GET", "/v1/threads", params=_thread_filters(cursor, limit, **filters))
806
+
807
+
808
+ def thread_delete(thread_id: str) -> Request:
809
+ return Request("DELETE", f"/v1/threads/{_seg(thread_id)}")
810
+
811
+
812
+ def message_forward(
813
+ inbox_id: str,
814
+ message_id: str,
815
+ to: Recipients,
816
+ cc: Optional[Recipients],
817
+ bcc: Optional[Recipients],
818
+ text: Optional[str],
819
+ html: Optional[str],
820
+ include_attachments: Optional[bool],
821
+ labels: Optional[Sequence[str]],
822
+ idempotency_key: Optional[str],
823
+ ) -> Request:
824
+ body = _body(
825
+ {
826
+ "to": _recipients(to),
827
+ "cc": _recipients(cc),
828
+ "bcc": _recipients(bcc),
829
+ "text": text,
830
+ "html": html,
831
+ "include_attachments": include_attachments,
832
+ "labels": _list(labels),
833
+ }
834
+ )
835
+ return Request(
836
+ "POST",
837
+ f"/v1/inboxes/{_seg(inbox_id)}/messages/{_seg(message_id)}/forward",
838
+ json=body,
839
+ idempotency_key=idempotency_key,
840
+ )
841
+
842
+
843
+ def messages_batch_get(
844
+ ids: Sequence[str], include_unscreened: Optional[bool] = None, include_held: Optional[bool] = None
845
+ ) -> Request:
846
+ body = _body({"ids": list(ids), "include_unscreened": include_unscreened, "include_held": include_held})
847
+ return Request("POST", "/v1/messages/batch-get", json=body)
848
+
849
+
850
+ def messages_batch_update(
851
+ ids: Sequence[str],
852
+ add_labels: Optional[Sequence[str]],
853
+ remove_labels: Optional[Sequence[str]],
854
+ is_read: Optional[bool],
855
+ ) -> Request:
856
+ body = _body({"add_labels": _list(add_labels), "remove_labels": _list(remove_labels), "is_read": is_read})
857
+ return Request("POST", "/v1/messages/batch-update", json={"ids": list(ids), **body})
858
+
859
+
860
+ def thread_get(thread_id: str) -> Request:
861
+ return Request("GET", f"/v1/threads/{_seg(thread_id)}")
862
+
863
+
864
+ def thread_update(
865
+ thread_id: str,
866
+ metadata: Optional[MetadataPatch],
867
+ add_labels: Optional[Sequence[str]],
868
+ remove_labels: Optional[Sequence[str]],
869
+ ) -> Request:
870
+ body = _body(
871
+ {
872
+ "metadata": _metadata(metadata),
873
+ "add_labels": _list(add_labels),
874
+ "remove_labels": _list(remove_labels),
875
+ }
876
+ )
877
+ return Request("PATCH", f"/v1/threads/{_seg(thread_id)}", json=body)
878
+
879
+
880
+ # ---------- contacts ----------
881
+
882
+
883
+ def _kinds(kind: Union[str, Sequence[str], None]) -> Optional[str]:
884
+ if kind is None or isinstance(kind, str):
885
+ return kind
886
+ return ",".join(kind)
887
+
888
+
889
+ def contact_list(
890
+ q: Optional[str],
891
+ label: Optional[str],
892
+ metadata: Optional[MetadataFilter],
893
+ cursor: Optional[str],
894
+ limit: Optional[int],
895
+ kind: Union[str, Sequence[str], None] = None,
896
+ domain: Optional[str] = None,
897
+ ) -> Request:
898
+ params = {
899
+ **_query(
900
+ {"q": q, "label": label, "kind": _kinds(kind), "domain": domain, "cursor": cursor, "limit": limit}
901
+ ),
902
+ **_metadata_query(metadata),
903
+ }
904
+ return Request("GET", "/v1/contacts", params=params)
905
+
906
+
907
+ def contact_domains(kind: Union[str, Sequence[str], None], limit: Optional[int]) -> Request:
908
+ return Request("GET", "/v1/contacts/domains", params=_query({"kind": _kinds(kind), "limit": limit}))
909
+
910
+
911
+ def contact_get(contact_id: str) -> Request:
912
+ return Request("GET", f"/v1/contacts/{_seg(contact_id)}")
913
+
914
+
915
+ def contact_by_address(address: str) -> Request:
916
+ return Request("GET", f"/v1/contacts/by-address/{_seg(address)}")
917
+
918
+
919
+ def contact_update(
920
+ contact_id: str,
921
+ name: Nullable,
922
+ notes: Nullable,
923
+ metadata: Optional[MetadataPatch],
924
+ add_labels: Optional[Sequence[str]],
925
+ remove_labels: Optional[Sequence[str]],
926
+ kind: Optional[str] = None,
927
+ ) -> Request:
928
+ body = _patch(
929
+ {
930
+ "name": name,
931
+ "notes": notes,
932
+ "metadata": _metadata(metadata),
933
+ "add_labels": _list(add_labels),
934
+ "remove_labels": _list(remove_labels),
935
+ "kind": kind,
936
+ },
937
+ nullable=("name", "notes"),
938
+ )
939
+ return Request("PATCH", f"/v1/contacts/{_seg(contact_id)}", json=body)
940
+
941
+
942
+ def contact_delete(contact_id: str, forget: bool = False) -> Request:
943
+ return Request("DELETE", f"/v1/contacts/{_seg(contact_id)}", params={"forget": "true"} if forget else {})
944
+
945
+
946
+ def contact_delete_many(
947
+ kind: Optional[Sequence[str]], domain: Optional[str], forget: bool, dry_run: bool
948
+ ) -> Request:
949
+ body: dict[str, Any] = {}
950
+ if kind is not None:
951
+ body["kind"] = list(kind)
952
+ if domain is not None:
953
+ body["domain"] = domain
954
+ if forget:
955
+ body["forget"] = True
956
+ if dry_run:
957
+ body["dry_run"] = True
958
+ return Request("POST", "/v1/contacts/delete", json=body)
959
+
960
+
961
+ # ---------- knowledge ----------
962
+
963
+
964
+ def knowledge_list(inbox_id: Optional[str], cursor: Optional[str], limit: Optional[int]) -> Request:
965
+ return Request(
966
+ "GET", "/v1/knowledge", params=_query({"inbox_id": inbox_id, "cursor": cursor, "limit": limit})
967
+ )
968
+
969
+
970
+ def knowledge_create(title: str, body: str, inbox_id: Optional[str]) -> Request:
971
+ return Request("POST", "/v1/knowledge", json=_body({"title": title, "body": body, "inbox_id": inbox_id}))
972
+
973
+
974
+ def knowledge_get(doc_id: str) -> Request:
975
+ return Request("GET", f"/v1/knowledge/{_seg(doc_id)}")
976
+
977
+
978
+ def knowledge_update(doc_id: str, title: Optional[str], body: Optional[str], inbox_id: Nullable) -> Request:
979
+ payload = _patch({"title": title, "body": body, "inbox_id": inbox_id}, nullable=("inbox_id",))
980
+ return Request("PATCH", f"/v1/knowledge/{_seg(doc_id)}", json=payload)
981
+
982
+
983
+ def knowledge_delete(doc_id: str) -> Request:
984
+ return Request("DELETE", f"/v1/knowledge/{_seg(doc_id)}")
985
+
986
+
987
+ def knowledge_search(q: str, inbox_id: Optional[str], limit: Optional[int]) -> Request:
988
+ return Request(
989
+ "GET", "/v1/knowledge/search", params=_query({"q": q, "inbox_id": inbox_id, "limit": limit})
990
+ )
991
+
992
+
993
+ def search(
994
+ q: str,
995
+ inbox_id: Optional[str],
996
+ cursor: Optional[str],
997
+ limit: Optional[int],
998
+ channel: Optional[MessageChannel] = None,
999
+ type: Optional[MessageType] = None,
1000
+ ) -> Request:
1001
+ params = _query(
1002
+ {"q": q, "inbox_id": inbox_id, "cursor": cursor, "limit": limit, "channel": channel, "type": type}
1003
+ )
1004
+ return Request("GET", "/v1/search", params=params)
1005
+
1006
+
1007
+ # ---------- webhooks ----------
1008
+
1009
+ InboxIds = Union[Sequence[str], NotGiven, None]
1010
+
1011
+
1012
+ def _inbox_ids(value: InboxIds) -> Union[list[str], NotGiven, None]:
1013
+ if value is None or isinstance(value, NotGiven):
1014
+ return value
1015
+ return list(value)
1016
+
1017
+
1018
+ def _body_keep_null(values: Mapping[str, Any], nullable: str) -> dict[str, Any]:
1019
+ body = _body(values)
1020
+ if values.get(nullable, NOT_GIVEN) is None:
1021
+ body[nullable] = None
1022
+ return body
1023
+
1024
+
1025
+ def webhook_create(
1026
+ url: str,
1027
+ events: Optional[Sequence[WebhookEventType]],
1028
+ inbox_ids: InboxIds,
1029
+ secret: Optional[str],
1030
+ payload: Optional[WebhookPayload] = None,
1031
+ headers: Optional[Mapping[str, str]] = None,
1032
+ ) -> Request:
1033
+ body = _body_keep_null(
1034
+ {
1035
+ "url": url,
1036
+ "events": _list(events),
1037
+ "inbox_ids": _inbox_ids(inbox_ids),
1038
+ "secret": secret,
1039
+ "payload": payload,
1040
+ "headers": dict(headers) if headers is not None else None,
1041
+ },
1042
+ "inbox_ids",
1043
+ )
1044
+ return Request("POST", "/v1/webhooks", json=body)
1045
+
1046
+
1047
+ def webhook_list() -> Request:
1048
+ return Request("GET", "/v1/webhooks")
1049
+
1050
+
1051
+ def webhook_get(webhook_id: str) -> Request:
1052
+ return Request("GET", f"/v1/webhooks/{_seg(webhook_id)}")
1053
+
1054
+
1055
+ def webhook_update(
1056
+ webhook_id: str,
1057
+ url: Optional[str],
1058
+ events: Optional[Sequence[WebhookEventType]],
1059
+ inbox_ids: InboxIds,
1060
+ enabled: Optional[bool],
1061
+ payload: Optional[WebhookPayload] = None,
1062
+ headers: Optional[Mapping[str, str]] = None,
1063
+ ) -> Request:
1064
+ body = _body_keep_null(
1065
+ {
1066
+ "url": url,
1067
+ "events": _list(events),
1068
+ "inbox_ids": _inbox_ids(inbox_ids),
1069
+ "enabled": enabled,
1070
+ "payload": payload,
1071
+ "headers": dict(headers) if headers is not None else None,
1072
+ },
1073
+ "inbox_ids",
1074
+ )
1075
+ return Request("PATCH", f"/v1/webhooks/{_seg(webhook_id)}", json=body)
1076
+
1077
+
1078
+ def webhook_delete(webhook_id: str) -> Request:
1079
+ return Request("DELETE", f"/v1/webhooks/{_seg(webhook_id)}")
1080
+
1081
+
1082
+ def webhook_test(webhook_id: str) -> Request:
1083
+ return Request("POST", f"/v1/webhooks/{_seg(webhook_id)}/test")
1084
+
1085
+
1086
+ def webhook_rotate_secret(webhook_id: str, secret: Optional[str]) -> Request:
1087
+ return Request("POST", f"/v1/webhooks/{_seg(webhook_id)}/rotate-secret", json=_body({"secret": secret}))
1088
+
1089
+
1090
+ # ---------- custom domains ----------
1091
+
1092
+
1093
+ def domain_create(domain: str, receiving: Optional[bool]) -> Request:
1094
+ return Request("POST", "/v1/domains", json=_body({"domain": domain, "receiving": receiving}))
1095
+
1096
+
1097
+ def domain_list() -> Request:
1098
+ return Request("GET", "/v1/domains")
1099
+
1100
+
1101
+ def domain_get(domain_id: str) -> Request:
1102
+ return Request("GET", f"/v1/domains/{_seg(domain_id)}")
1103
+
1104
+
1105
+ def domain_zone_file(domain_id: str) -> Request:
1106
+ return Request("GET", f"/v1/domains/{_seg(domain_id)}/zone-file")
1107
+
1108
+
1109
+ def domain_verify(domain_id: str) -> Request:
1110
+ return Request("POST", f"/v1/domains/{_seg(domain_id)}/verify")
1111
+
1112
+
1113
+ def domain_update(domain_id: str, receiving: Optional[bool]) -> Request:
1114
+ return Request("PATCH", f"/v1/domains/{_seg(domain_id)}", json=_body({"receiving": receiving}))
1115
+
1116
+
1117
+ def domain_dkim_rotate(domain_id: str) -> Request:
1118
+ return Request("POST", f"/v1/domains/{_seg(domain_id)}/dkim/rotate")
1119
+
1120
+
1121
+ def domain_dkim_activate(domain_id: str) -> Request:
1122
+ return Request("POST", f"/v1/domains/{_seg(domain_id)}/dkim/activate")
1123
+
1124
+
1125
+ def domain_delete(domain_id: str, force: bool) -> Request:
1126
+ params = _query({"force": True}) if force else {}
1127
+ return Request("DELETE", f"/v1/domains/{_seg(domain_id)}", params=params)
1128
+
1129
+
1130
+ def webhook_events() -> Request:
1131
+ return Request("GET", "/v1/webhooks/events")
1132
+
1133
+
1134
+ # ---------- allow / block lists ----------
1135
+
1136
+
1137
+ def list_entries(
1138
+ inbox_id: Optional[str], direction: Optional[ListDirection], kind: Optional[ListKind]
1139
+ ) -> Request:
1140
+ params = _query({"inbox_id": inbox_id, "direction": direction, "kind": kind})
1141
+ return Request("GET", "/v1/lists", params=params)
1142
+
1143
+
1144
+ def list_create(direction: ListDirection, kind: ListKind, pattern: str, inbox_id: Optional[str]) -> Request:
1145
+ body = _body({"direction": direction, "kind": kind, "pattern": pattern, "inbox_id": inbox_id})
1146
+ return Request("POST", "/v1/lists", json=body)
1147
+
1148
+
1149
+ def list_delete(entry_id: str) -> Request:
1150
+ return Request("DELETE", f"/v1/lists/{_seg(entry_id)}")
1151
+
1152
+
1153
+ # ---------- metrics ----------
1154
+
1155
+
1156
+ def deliverability() -> Request:
1157
+ return Request("GET", "/v1/deliverability")
1158
+
1159
+
1160
+ def metrics(
1161
+ from_: Optional[str],
1162
+ to: Optional[str],
1163
+ tz: Optional[str],
1164
+ inbox_id: Optional[str],
1165
+ bucket: Optional[MetricsBucket],
1166
+ ) -> Request:
1167
+ params = _query({"from": from_, "to": to, "tz": tz, "inbox_id": inbox_id, "bucket": bucket})
1168
+ return Request("GET", "/v1/metrics", params=params)
1169
+
1170
+
1171
+ # ---------- realtime stream ----------
1172
+
1173
+
1174
+ def stream_token() -> Request:
1175
+ return Request("POST", "/v1/stream/token")
1176
+
1177
+
1178
+ # ---------- agent self-signup ----------
1179
+
1180
+
1181
+ def signup_challenge() -> Request:
1182
+ return Request("GET", "/v1/signup/challenge")
1183
+
1184
+
1185
+ def signup_create(
1186
+ challenge: str,
1187
+ solution: str,
1188
+ agent_name: Optional[str],
1189
+ owner_email: Optional[str],
1190
+ kind: Optional[str] = None,
1191
+ ) -> Request:
1192
+ return Request(
1193
+ "POST",
1194
+ "/v1/signup",
1195
+ json=_body(
1196
+ {
1197
+ "challenge": challenge,
1198
+ "solution": solution,
1199
+ "agent_name": agent_name,
1200
+ "owner_email": owner_email,
1201
+ "kind": kind,
1202
+ }
1203
+ ),
1204
+ )
1205
+
1206
+
1207
+ def account_get() -> Request:
1208
+ return Request("GET", "/v1/account")
1209
+
1210
+
1211
+ # ---------- trust layer: emergency stop and human on call ----------
1212
+
1213
+
1214
+ def emergency_stop(reason: Optional[str]) -> Request:
1215
+ return Request("POST", "/v1/emergency-stop", json=_body({"reason": reason}))
1216
+
1217
+
1218
+ def forwarding_list(inbox_id: str) -> Request:
1219
+ return Request("GET", f"/v1/inboxes/{_seg(inbox_id)}/forwarding")
1220
+
1221
+
1222
+ def forwarding_add(inbox_id: str, address: str, skip_flagged: Optional[bool]) -> Request:
1223
+ return Request(
1224
+ "POST",
1225
+ f"/v1/inboxes/{_seg(inbox_id)}/forwarding",
1226
+ json=_body({"address": address, "skip_flagged": skip_flagged}),
1227
+ )
1228
+
1229
+
1230
+ def forwarding_resend(inbox_id: str, forward_id: str) -> Request:
1231
+ return Request("POST", f"/v1/inboxes/{_seg(inbox_id)}/forwarding/{_seg(forward_id)}/resend")
1232
+
1233
+
1234
+ def forwarding_remove(inbox_id: str, forward_id: str) -> Request:
1235
+ return Request("DELETE", f"/v1/inboxes/{_seg(inbox_id)}/forwarding/{_seg(forward_id)}")
1236
+
1237
+
1238
+ def escalation_get() -> Request:
1239
+ return Request("GET", "/v1/escalation")
1240
+
1241
+
1242
+ def _given(values: Mapping[str, Any]) -> dict[str, Any]:
1243
+ """Keep every argument that was given, ``None`` included (``quiet_hours=None`` clears them)."""
1244
+ return {k: v for k, v in values.items() if not isinstance(v, NotGiven)}
1245
+
1246
+
1247
+ def escalation_update(body: Mapping[str, Any]) -> Request:
1248
+ return Request("PUT", "/v1/escalation", json=_given(body))
1249
+
1250
+
1251
+ def inbox_escalation_get(inbox_id: str) -> Request:
1252
+ return Request("GET", f"/v1/inboxes/{_seg(inbox_id)}/escalation")
1253
+
1254
+
1255
+ def inbox_escalation_update(inbox_id: str, body: Mapping[str, Any]) -> Request:
1256
+ return Request("PUT", f"/v1/inboxes/{_seg(inbox_id)}/escalation", json=_given(body))
1257
+
1258
+
1259
+ def signup_claim(email: str) -> Request:
1260
+ return Request("POST", "/v1/signup/claim", json={"email": email})
1261
+
1262
+
1263
+ def signup_add_agent(kind: Optional[str] = None) -> Request:
1264
+ return Request("POST", "/v1/signup/agents", json={"kind": kind} if kind else {})
1265
+
1266
+
1267
+ # ---------- agent identity (Sign in with Agentboxd) ----------
1268
+
1269
+ IdentityScopes = Union[str, Sequence[str]]
1270
+
1271
+
1272
+ def _scope(value: Optional[IdentityScopes]) -> Optional[str]:
1273
+ if value is None or isinstance(value, str):
1274
+ return value
1275
+ return " ".join(value)
1276
+
1277
+
1278
+ def identity_token(
1279
+ inbox_id: Optional[str],
1280
+ audience: Optional[str],
1281
+ nonce: Optional[str],
1282
+ scope: Optional[IdentityScopes],
1283
+ expires_in: Optional[int],
1284
+ identity_id: Optional[str] = None,
1285
+ ) -> Request:
1286
+ """``inbox_id`` for an inbox or ``identity_id`` for an identity-only agent (the same endpoint)."""
1287
+ if (inbox_id is None) == (identity_id is None):
1288
+ raise TypeError("pass exactly one of inbox_id or identity_id")
1289
+ if not audience:
1290
+ raise TypeError("audience is required (the relying party's client_id)")
1291
+ agent_id = cast(str, identity_id if identity_id is not None else inbox_id)
1292
+ body = _body({"audience": audience, "nonce": nonce, "scope": _scope(scope), "expires_in": expires_in})
1293
+ return Request("POST", f"/v1/inboxes/{_seg(agent_id)}/identity-token", json=body)
1294
+
1295
+
1296
+ def identity_client_list() -> Request:
1297
+ return Request("GET", "/v1/identity/clients")
1298
+
1299
+
1300
+ def identity_client_create(
1301
+ name: str,
1302
+ type: str,
1303
+ redirect_uris: Optional[Sequence[str]],
1304
+ allowed_scopes: Optional[Sequence[str]],
1305
+ subject_type: Optional[str],
1306
+ homepage_url: Optional[str],
1307
+ ) -> Request:
1308
+ body = _body(
1309
+ {
1310
+ "name": name,
1311
+ "type": type,
1312
+ "redirect_uris": _list(redirect_uris),
1313
+ "allowed_scopes": _list(allowed_scopes),
1314
+ "subject_type": subject_type,
1315
+ "homepage_url": homepage_url,
1316
+ }
1317
+ )
1318
+ return Request("POST", "/v1/identity/clients", json=body)
1319
+
1320
+
1321
+ def identity_client_get(client_id: str) -> Request:
1322
+ return Request("GET", f"/v1/identity/clients/{_seg(client_id)}")
1323
+
1324
+
1325
+ def identity_client_update(
1326
+ client_id: str,
1327
+ name: Optional[str],
1328
+ redirect_uris: Optional[Sequence[str]],
1329
+ allowed_scopes: Optional[Sequence[str]],
1330
+ homepage_url: Nullable,
1331
+ enabled: Optional[bool],
1332
+ ) -> Request:
1333
+ body = _patch(
1334
+ {
1335
+ "name": name,
1336
+ "redirect_uris": _list(redirect_uris),
1337
+ "allowed_scopes": _list(allowed_scopes),
1338
+ "homepage_url": homepage_url,
1339
+ "enabled": enabled,
1340
+ },
1341
+ nullable=("homepage_url",),
1342
+ )
1343
+ return Request("PATCH", f"/v1/identity/clients/{_seg(client_id)}", json=body)
1344
+
1345
+
1346
+ def identity_client_delete(client_id: str) -> Request:
1347
+ return Request("DELETE", f"/v1/identity/clients/{_seg(client_id)}")
1348
+
1349
+
1350
+ def identity_client_rotate_secret(client_id: str) -> Request:
1351
+ return Request("POST", f"/v1/identity/clients/{_seg(client_id)}/secret")
1352
+
1353
+
1354
+ def inbox_identity_get(inbox_id: str) -> Request:
1355
+ return Request("GET", f"/v1/inboxes/{_seg(inbox_id)}/identity")
1356
+
1357
+
1358
+ def inbox_identity_update(inbox_id: str, enabled: bool) -> Request:
1359
+ return Request("PATCH", f"/v1/inboxes/{_seg(inbox_id)}/identity", json={"enabled": enabled})
1360
+
1361
+
1362
+ def inbox_identity_sign_ins(inbox_id: str, cursor: Optional[str], limit: Optional[int]) -> Request:
1363
+ params = _query({"cursor": cursor, "limit": limit})
1364
+ return Request("GET", f"/v1/inboxes/{_seg(inbox_id)}/identity/sign-ins", params=params)