agentic-platform-sdk 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.
- agentic_platform_sdk/__init__.py +9 -0
- agentic_platform_sdk/_a2a.py +133 -0
- agentic_platform_sdk/_client.py +152 -0
- agentic_platform_sdk/_contract.py +72 -0
- agentic_platform_sdk/_generated/__init__.py +0 -0
- agentic_platform_sdk/_generated/_base.py +20 -0
- agentic_platform_sdk/_generated/models.py +698 -0
- agentic_platform_sdk/_paging.py +117 -0
- agentic_platform_sdk/_spec/call_areas.json +13 -0
- agentic_platform_sdk/_spec/chat-events.schema.json +371 -0
- agentic_platform_sdk/_spec/errors.json +2365 -0
- agentic_platform_sdk/_spec/operations.json +230 -0
- agentic_platform_sdk/_transport.py +630 -0
- agentic_platform_sdk/_util.py +133 -0
- agentic_platform_sdk/agents.py +793 -0
- agentic_platform_sdk/approvals.py +66 -0
- agentic_platform_sdk/auth.py +336 -0
- agentic_platform_sdk/chat.py +599 -0
- agentic_platform_sdk/datasets.py +69 -0
- agentic_platform_sdk/errors.py +204 -0
- agentic_platform_sdk/kb.py +288 -0
- agentic_platform_sdk/llm.py +40 -0
- agentic_platform_sdk/me.py +28 -0
- agentic_platform_sdk/models.py +91 -0
- agentic_platform_sdk/ontology.py +209 -0
- agentic_platform_sdk/py.typed +0 -0
- agentic_platform_sdk/testing/__init__.py +19 -0
- agentic_platform_sdk/testing/conformance.py +803 -0
- agentic_platform_sdk/testing/fake.py +902 -0
- agentic_platform_sdk/testing/standin.py +165 -0
- agentic_platform_sdk/toolchains.py +137 -0
- agentic_platform_sdk/tools/__init__.py +70 -0
- agentic_platform_sdk/tools/_jcs.py +218 -0
- agentic_platform_sdk/tools/asgi.py +228 -0
- agentic_platform_sdk/tools/mcp.py +751 -0
- agentic_platform_sdk/tools/verify.py +379 -0
- agentic_platform_sdk/vision.py +300 -0
- agentic_platform_sdk-0.1.0.dist-info/METADATA +226 -0
- agentic_platform_sdk-0.1.0.dist-info/RECORD +41 -0
- agentic_platform_sdk-0.1.0.dist-info/WHEEL +4 -0
- agentic_platform_sdk-0.1.0.dist-info/licenses/LICENSE +202 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""Helpers the area modules share: request bodies, idempotency keys and file uploads."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import contextlib
|
|
6
|
+
import os
|
|
7
|
+
import uuid
|
|
8
|
+
from collections.abc import Iterable, Iterator, Mapping
|
|
9
|
+
from datetime import UTC, datetime
|
|
10
|
+
from typing import Any, BinaryIO, TypeVar
|
|
11
|
+
|
|
12
|
+
import httpx
|
|
13
|
+
from pydantic import BaseModel, ValidationError
|
|
14
|
+
from pydantic_core import to_jsonable_python
|
|
15
|
+
|
|
16
|
+
from .errors import AREA_EXTENSION, error_from_unparsed_body
|
|
17
|
+
|
|
18
|
+
M = TypeVar("M", bound=BaseModel)
|
|
19
|
+
|
|
20
|
+
# A path (str or PathLike), the bytes themselves, or a binary stream opened by the caller.
|
|
21
|
+
FileSource = str | os.PathLike[str] | bytes | bytearray | memoryview | BinaryIO
|
|
22
|
+
|
|
23
|
+
_ANONYMOUS_UPLOAD = "an upload from bytes or a stream needs a filename"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def jsonable(value: Any) -> Any:
|
|
27
|
+
"""`value` as plain JSON data: models, UUIDs, enums and datetimes become their wire form."""
|
|
28
|
+
if isinstance(value, BaseModel):
|
|
29
|
+
return value.model_dump(mode="json", exclude_unset=True)
|
|
30
|
+
if isinstance(value, Mapping):
|
|
31
|
+
return {key: jsonable(item) for key, item in value.items()}
|
|
32
|
+
if isinstance(value, list | tuple):
|
|
33
|
+
return [jsonable(item) for item in value]
|
|
34
|
+
if isinstance(value, datetime) and value.tzinfo is None:
|
|
35
|
+
value = value.replace(
|
|
36
|
+
tzinfo=UTC
|
|
37
|
+
) # a naive datetime means UTC; the wire always carries an offset
|
|
38
|
+
return to_jsonable_python(value)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def parse(model: type[M], response: httpx.Response) -> M:
|
|
42
|
+
"""The response body as `model`; fields the model does not know are kept.
|
|
43
|
+
|
|
44
|
+
A body that is not JSON, or not `model`, raises ``Unavailable`` (``<area>.unavailable``) with
|
|
45
|
+
the pydantic error as its ``__cause__``: the platform answered, but not with what it should.
|
|
46
|
+
"""
|
|
47
|
+
try:
|
|
48
|
+
return model.model_validate_json(response.content)
|
|
49
|
+
except ValidationError as exc: # invalid JSON is a ValidationError too (json_invalid)
|
|
50
|
+
area = response.extensions.get(AREA_EXTENSION)
|
|
51
|
+
raise error_from_unparsed_body(response.status_code, model.__name__, area, exc) from exc
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def body_of(fields: Mapping[str, Any]) -> dict[str, Any]:
|
|
55
|
+
return jsonable(dict(fields))
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def idempotency(key: str | bool | None) -> str | None:
|
|
59
|
+
"""The Idempotency-Key to send: the caller's own, a fresh uuid4, or None for ``False``."""
|
|
60
|
+
if key is False:
|
|
61
|
+
return None
|
|
62
|
+
if key is None or key is True:
|
|
63
|
+
return str(uuid.uuid4())
|
|
64
|
+
return key
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _is_bytes(source: Any) -> bool:
|
|
68
|
+
return isinstance(source, bytes | bytearray | memoryview)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _name_of(source: Any, filename: str | None) -> str:
|
|
72
|
+
if filename:
|
|
73
|
+
return filename
|
|
74
|
+
if isinstance(source, str | os.PathLike):
|
|
75
|
+
return os.path.basename(os.fspath(source))
|
|
76
|
+
name = getattr(source, "name", None)
|
|
77
|
+
if isinstance(name, str) and name and not name.startswith("<"):
|
|
78
|
+
return os.path.basename(name)
|
|
79
|
+
raise ValueError(_ANONYMOUS_UPLOAD)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@contextlib.contextmanager
|
|
83
|
+
def upload_part(source: FileSource, filename: str | None = None) -> Iterator[tuple[str, Any]]:
|
|
84
|
+
"""The ``(filename, content)`` pair for a multipart part.
|
|
85
|
+
|
|
86
|
+
A path is opened here and closed on exit; bytes are sent as they are; a stream is read by httpx
|
|
87
|
+
in chunks and is left open for its owner to close.
|
|
88
|
+
"""
|
|
89
|
+
name = _name_of(source, filename) if not _is_bytes(source) else filename
|
|
90
|
+
if _is_bytes(source):
|
|
91
|
+
if not name:
|
|
92
|
+
raise ValueError(_ANONYMOUS_UPLOAD)
|
|
93
|
+
yield name, bytes(source)
|
|
94
|
+
elif isinstance(source, str | os.PathLike):
|
|
95
|
+
with open(source, "rb") as handle:
|
|
96
|
+
yield name, handle
|
|
97
|
+
else:
|
|
98
|
+
if not hasattr(source, "read"):
|
|
99
|
+
raise TypeError("a file must be a path, bytes or a binary stream")
|
|
100
|
+
yield name, source
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@contextlib.contextmanager
|
|
104
|
+
def upload_parts(
|
|
105
|
+
files: Mapping[str, FileSource] | Iterable[FileSource],
|
|
106
|
+
) -> Iterator[list[tuple[str, tuple[str, Any]]]]:
|
|
107
|
+
"""Several ``files`` parts. A mapping is ``{filename: source}``; an iterable of paths or
|
|
108
|
+
named streams takes each filename from the source."""
|
|
109
|
+
pairs = (
|
|
110
|
+
list(files.items()) if isinstance(files, Mapping) else [(None, source) for source in files]
|
|
111
|
+
)
|
|
112
|
+
with contextlib.ExitStack() as stack:
|
|
113
|
+
parts = [
|
|
114
|
+
("files", stack.enter_context(upload_part(source, name))) for name, source in pairs
|
|
115
|
+
]
|
|
116
|
+
yield parts
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def text_part(value: str) -> tuple[None, str]:
|
|
120
|
+
"""A multipart form field (no filename)."""
|
|
121
|
+
return (None, value)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
__all__ = [
|
|
125
|
+
"FileSource",
|
|
126
|
+
"body_of",
|
|
127
|
+
"idempotency",
|
|
128
|
+
"jsonable",
|
|
129
|
+
"parse",
|
|
130
|
+
"text_part",
|
|
131
|
+
"upload_part",
|
|
132
|
+
"upload_parts",
|
|
133
|
+
]
|