aice-agent-platform 0.2.1__tar.gz
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.
- aice_agent_platform-0.2.1/.gitignore +28 -0
- aice_agent_platform-0.2.1/CHANGELOG.md +50 -0
- aice_agent_platform-0.2.1/PKG-INFO +87 -0
- aice_agent_platform-0.2.1/README.md +67 -0
- aice_agent_platform-0.2.1/pyproject.toml +37 -0
- aice_agent_platform-0.2.1/src/aice_agent_platform/__init__.py +7 -0
- aice_agent_platform-0.2.1/src/aice_agent_platform/client.py +283 -0
- aice_agent_platform-0.2.1/src/aice_agent_platform/py.typed +0 -0
- aice_agent_platform-0.2.1/tests/test_agent_platform_client.py +150 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Python bytecode / caches
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.so
|
|
6
|
+
.Python
|
|
7
|
+
.pytest_cache/
|
|
8
|
+
.mypy_cache/
|
|
9
|
+
.ruff_cache/
|
|
10
|
+
|
|
11
|
+
# Virtualenvs & editors
|
|
12
|
+
.venv/
|
|
13
|
+
.env
|
|
14
|
+
.env.*
|
|
15
|
+
!.env.example
|
|
16
|
+
.vscode/
|
|
17
|
+
.idea/
|
|
18
|
+
.DS_Store
|
|
19
|
+
|
|
20
|
+
# Build artifacts
|
|
21
|
+
build/
|
|
22
|
+
dist/
|
|
23
|
+
*.egg-info/
|
|
24
|
+
node_modules/
|
|
25
|
+
*.tsbuildinfo
|
|
26
|
+
|
|
27
|
+
# uv / pnpm lockfiles tracked per-package don't ignore them.
|
|
28
|
+
_skills
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Changelog aice-agent-platform
|
|
2
|
+
|
|
3
|
+
## 0.2.1
|
|
4
|
+
|
|
5
|
+
### Changed
|
|
6
|
+
- Dependency floor on `aice-core` bumped to `>=0.3.0,<0.4` (core 0.3.0
|
|
7
|
+
renamed `PermissionError` → `AiceForbiddenError` not referenced by
|
|
8
|
+
this package, floor bumped purely to avoid a cross-package dependency
|
|
9
|
+
conflict if installed alongside `aice-sandbox`).
|
|
10
|
+
|
|
11
|
+
## 0.2.0
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
- Composed the official `langgraph-sdk` package directly into
|
|
15
|
+
`AgentPlatformClient`/`AsyncAgentPlatformClient` as `threads`, `runs`,
|
|
16
|
+
`assistants`, and `crons` the native LangGraph surface, including
|
|
17
|
+
streaming run execution (`client.runs.stream(...)`). This was previously
|
|
18
|
+
entirely missing: the SDK only covered AICE-specific management routes
|
|
19
|
+
(`agents`, `skills`, `usage`, `suggestions`). Backend deployment already
|
|
20
|
+
speaks the LangGraph Platform protocol; this composes the existing
|
|
21
|
+
`langgraph-sdk` client rather than re-implementing it, per
|
|
22
|
+
`docs/adding-a-service.md`'s vendor-composition guidance.
|
|
23
|
+
- New dependency: `langgraph-sdk>=0.1.60,<0.2`.
|
|
24
|
+
|
|
25
|
+
### Changed
|
|
26
|
+
- `close()`/`aclose()` now also attempt to close the composed
|
|
27
|
+
`langgraph-sdk` client's underlying HTTP client, defensively (no-op if
|
|
28
|
+
the installed version doesn't expose a close method).
|
|
29
|
+
|
|
30
|
+
### Important behavioral note
|
|
31
|
+
- The new `threads`/`runs`/`assistants`/`crons` surface does **not** share
|
|
32
|
+
`aice-core`'s error hierarchy, retry policy, or idempotency-key handling
|
|
33
|
+
it raises and retries exactly as `langgraph-sdk` itself does. Only
|
|
34
|
+
`agents`/`skills`/`usage`/`suggestions` go through `aice-core`'s shared
|
|
35
|
+
transport. See the module docstring in `client.py` for the full
|
|
36
|
+
rationale; this is a deliberate composition trade-off (keeping the
|
|
37
|
+
vendor surface upgradeable via `pip install -U langgraph-sdk`), not an
|
|
38
|
+
oversight.
|
|
39
|
+
|
|
40
|
+
## 0.1.1
|
|
41
|
+
|
|
42
|
+
### Changed
|
|
43
|
+
- Dependency floor on `aice-core` bumped to `>=0.2.0,<0.3` to pick up the
|
|
44
|
+
safer default idempotency-key retry behavior. No other code changes.
|
|
45
|
+
|
|
46
|
+
### Fixed
|
|
47
|
+
- (Superseded by 0.2.0 above, noted here for the record) The package's own
|
|
48
|
+
`README.md` had a bug unrelated to versioning: its example called
|
|
49
|
+
`client.suggestions.create(thread_id=...)`, but the actual method is
|
|
50
|
+
`create_for_thread(thread_id)`. Fixed in the 0.2.0 README rewrite.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: aice-agent-platform
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: Python SDK for the AICE Agent Platform (config-driven LangGraph agent runtime)
|
|
5
|
+
Author-email: Adanian Labs <engineering@adanianlabs.io>
|
|
6
|
+
License: MIT
|
|
7
|
+
Classifier: Development Status :: 4 - Beta
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Typing :: Typed
|
|
12
|
+
Requires-Python: >=3.11
|
|
13
|
+
Requires-Dist: aice-core<0.4,>=0.3.0
|
|
14
|
+
Requires-Dist: langgraph-sdk<0.2,>=0.1.60
|
|
15
|
+
Provides-Extra: dev
|
|
16
|
+
Requires-Dist: pytest-asyncio>=0.24; extra == 'dev'
|
|
17
|
+
Requires-Dist: pytest>=8.3; extra == 'dev'
|
|
18
|
+
Requires-Dist: respx>=0.21; extra == 'dev'
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# aice-agent-platform
|
|
22
|
+
|
|
23
|
+
Python SDK for the [Agent Platform](../../../aice_packages/agent_platform/) the
|
|
24
|
+
config-driven LangGraph agent runtime exposed over Aegra's Agent Protocol.
|
|
25
|
+
|
|
26
|
+
This SDK is a composition of two surfaces:
|
|
27
|
+
|
|
28
|
+
1. **AICE-specific management routes** (`agents`, `skills`, `usage`,
|
|
29
|
+
`suggestions`) go through `aice-core`'s shared transport, so they get
|
|
30
|
+
the usual typed `AiceError` hierarchy, retries, and idempotency-key
|
|
31
|
+
handling.
|
|
32
|
+
2. **The native LangGraph surface** (`threads`, `runs`, `assistants`,
|
|
33
|
+
`crons`) this service is a LangGraph Platform-compatible deployment,
|
|
34
|
+
so this SDK composes the official
|
|
35
|
+
[`langgraph-sdk`](https://reference.langchain.com/python/langgraph-sdk)
|
|
36
|
+
directly rather than re-implementing it. This is where streaming/run
|
|
37
|
+
execution lives.
|
|
38
|
+
|
|
39
|
+
**These two surfaces do not share error types or retry behavior.**
|
|
40
|
+
`client.threads`/`client.runs`/`client.assistants`/`client.crons` raise
|
|
41
|
+
whatever `langgraph-sdk` itself raises, with its own retry policy never
|
|
42
|
+
`aice_core.AiceError`. `client.agents`/`client.skills`/`client.usage`/
|
|
43
|
+
`client.suggestions` behave like every other AICE SDK. See the docstring at
|
|
44
|
+
the top of `client.py` for the full rationale.
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
from aice_agent_platform import AgentPlatformClient
|
|
48
|
+
|
|
49
|
+
client = AgentPlatformClient(base_url="https://agents.example.com", api_key="sk-…")
|
|
50
|
+
|
|
51
|
+
# ── AICE-specific management routes ────────────────────────────────────
|
|
52
|
+
|
|
53
|
+
# List available agents
|
|
54
|
+
agents = client.agents.list()
|
|
55
|
+
|
|
56
|
+
# Publish a saved agent definition so it's runnable
|
|
57
|
+
client.agents.publish("ag_123")
|
|
58
|
+
|
|
59
|
+
# Discover skills
|
|
60
|
+
skills = client.skills.list()
|
|
61
|
+
|
|
62
|
+
# Fetch usage counters for the current project
|
|
63
|
+
usage = client.usage.get()
|
|
64
|
+
|
|
65
|
+
# Trigger the post-turn suggestion engine for a thread
|
|
66
|
+
suggestions = client.suggestions.create_for_thread("th_123")
|
|
67
|
+
|
|
68
|
+
# ── Native LangGraph surface (streaming, threads, runs) ────────────────
|
|
69
|
+
|
|
70
|
+
thread = client.threads.create()
|
|
71
|
+
|
|
72
|
+
for chunk in client.runs.stream(
|
|
73
|
+
thread["thread_id"],
|
|
74
|
+
assistant_id="agent",
|
|
75
|
+
input={"messages": [{"role": "user", "content": "hi"}]},
|
|
76
|
+
):
|
|
77
|
+
print(chunk.event, chunk.data)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Async variant mirrors this with `AsyncAgentPlatformClient` and `await`/
|
|
81
|
+
`async for`.
|
|
82
|
+
|
|
83
|
+
See the upstream service's own
|
|
84
|
+
[docs/API.md](../../../aice_packages/agent_platform/docs/API.md) for the
|
|
85
|
+
AICE-specific endpoint spec this SDK tracks, and the
|
|
86
|
+
[LangGraph SDK reference](https://reference.langchain.com/python/langgraph-sdk)
|
|
87
|
+
for the native `threads`/`runs`/`assistants`/`crons` surface.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# aice-agent-platform
|
|
2
|
+
|
|
3
|
+
Python SDK for the [Agent Platform](../../../aice_packages/agent_platform/) the
|
|
4
|
+
config-driven LangGraph agent runtime exposed over Aegra's Agent Protocol.
|
|
5
|
+
|
|
6
|
+
This SDK is a composition of two surfaces:
|
|
7
|
+
|
|
8
|
+
1. **AICE-specific management routes** (`agents`, `skills`, `usage`,
|
|
9
|
+
`suggestions`) go through `aice-core`'s shared transport, so they get
|
|
10
|
+
the usual typed `AiceError` hierarchy, retries, and idempotency-key
|
|
11
|
+
handling.
|
|
12
|
+
2. **The native LangGraph surface** (`threads`, `runs`, `assistants`,
|
|
13
|
+
`crons`) this service is a LangGraph Platform-compatible deployment,
|
|
14
|
+
so this SDK composes the official
|
|
15
|
+
[`langgraph-sdk`](https://reference.langchain.com/python/langgraph-sdk)
|
|
16
|
+
directly rather than re-implementing it. This is where streaming/run
|
|
17
|
+
execution lives.
|
|
18
|
+
|
|
19
|
+
**These two surfaces do not share error types or retry behavior.**
|
|
20
|
+
`client.threads`/`client.runs`/`client.assistants`/`client.crons` raise
|
|
21
|
+
whatever `langgraph-sdk` itself raises, with its own retry policy never
|
|
22
|
+
`aice_core.AiceError`. `client.agents`/`client.skills`/`client.usage`/
|
|
23
|
+
`client.suggestions` behave like every other AICE SDK. See the docstring at
|
|
24
|
+
the top of `client.py` for the full rationale.
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from aice_agent_platform import AgentPlatformClient
|
|
28
|
+
|
|
29
|
+
client = AgentPlatformClient(base_url="https://agents.example.com", api_key="sk-…")
|
|
30
|
+
|
|
31
|
+
# ── AICE-specific management routes ────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
# List available agents
|
|
34
|
+
agents = client.agents.list()
|
|
35
|
+
|
|
36
|
+
# Publish a saved agent definition so it's runnable
|
|
37
|
+
client.agents.publish("ag_123")
|
|
38
|
+
|
|
39
|
+
# Discover skills
|
|
40
|
+
skills = client.skills.list()
|
|
41
|
+
|
|
42
|
+
# Fetch usage counters for the current project
|
|
43
|
+
usage = client.usage.get()
|
|
44
|
+
|
|
45
|
+
# Trigger the post-turn suggestion engine for a thread
|
|
46
|
+
suggestions = client.suggestions.create_for_thread("th_123")
|
|
47
|
+
|
|
48
|
+
# ── Native LangGraph surface (streaming, threads, runs) ────────────────
|
|
49
|
+
|
|
50
|
+
thread = client.threads.create()
|
|
51
|
+
|
|
52
|
+
for chunk in client.runs.stream(
|
|
53
|
+
thread["thread_id"],
|
|
54
|
+
assistant_id="agent",
|
|
55
|
+
input={"messages": [{"role": "user", "content": "hi"}]},
|
|
56
|
+
):
|
|
57
|
+
print(chunk.event, chunk.data)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Async variant mirrors this with `AsyncAgentPlatformClient` and `await`/
|
|
61
|
+
`async for`.
|
|
62
|
+
|
|
63
|
+
See the upstream service's own
|
|
64
|
+
[docs/API.md](../../../aice_packages/agent_platform/docs/API.md) for the
|
|
65
|
+
AICE-specific endpoint spec this SDK tracks, and the
|
|
66
|
+
[LangGraph SDK reference](https://reference.langchain.com/python/langgraph-sdk)
|
|
67
|
+
for the native `threads`/`runs`/`assistants`/`crons` surface.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "aice-agent-platform"
|
|
7
|
+
version = "0.2.1"
|
|
8
|
+
description = "Python SDK for the AICE Agent Platform (config-driven LangGraph agent runtime)"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "Adanian Labs", email = "engineering@adanianlabs.io" }]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 4 - Beta",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Typing :: Typed",
|
|
19
|
+
]
|
|
20
|
+
dependencies = [
|
|
21
|
+
"aice-core>=0.3.0,<0.4",
|
|
22
|
+
# Native LangGraph surface (threads/runs/assistants/crons) is composed
|
|
23
|
+
# in directly see the module docstring in client.py for why this is a
|
|
24
|
+
# dependency rather than a re-implementation. Floor picked for per-call
|
|
25
|
+
# header support; bump alongside upstream releases as normal.
|
|
26
|
+
"langgraph-sdk>=0.1.60,<0.2",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
dev = ["pytest>=8.3", "pytest-asyncio>=0.24", "respx>=0.21"]
|
|
31
|
+
|
|
32
|
+
[tool.hatch.build.targets.wheel]
|
|
33
|
+
packages = ["src/aice_agent_platform"]
|
|
34
|
+
|
|
35
|
+
[tool.pytest.ini_options]
|
|
36
|
+
asyncio_mode = "auto"
|
|
37
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
"""Sync and async clients for the AICE Agent Platform.
|
|
2
|
+
|
|
3
|
+
This service is deployed as a LangGraph Platform-compatible server (custom
|
|
4
|
+
auth validates AICE's own bearer tokens instead of LangSmith's scheme), so
|
|
5
|
+
this client is a **composition** of two things rather than one:
|
|
6
|
+
|
|
7
|
+
1. The native LangGraph surface ``threads``, ``runs``, ``assistants``,
|
|
8
|
+
``crons`` exposed unmodified via the vendor ``langgraph-sdk`` package
|
|
9
|
+
(``get_sync_client``/``get_client``). This is where streaming/run
|
|
10
|
+
execution lives: ``client.runs.stream(...)``,
|
|
11
|
+
``client.threads.create(...)``, etc. See the `LangGraph SDK reference
|
|
12
|
+
<https://reference.langchain.com/python/langgraph-sdk>`_ for the full
|
|
13
|
+
surface. We pass our own bearer token through as a header and disable
|
|
14
|
+
the vendor SDK's own API-key handling (``api_key=None``) so it doesn't
|
|
15
|
+
also try to read ``LANGSMITH_API_KEY``/``LANGGRAPH_API_KEY`` from the
|
|
16
|
+
environment.
|
|
17
|
+
2. AICE-specific management routes ``agents``, ``skills``, ``usage``,
|
|
18
|
+
``suggestions`` that don't exist in LangGraph's own API. These are
|
|
19
|
+
custom siblings routed through ``aice_core``'s shared transport
|
|
20
|
+
(retries, typed errors, idempotency keys), exactly like every other
|
|
21
|
+
AICE service.
|
|
22
|
+
|
|
23
|
+
**Important asymmetry to be aware of:** (1) and (2) do NOT share error
|
|
24
|
+
handling or retry behavior. Calls through ``client.threads``/``client.runs``/
|
|
25
|
+
``client.assistants``/``client.crons`` raise whatever ``langgraph-sdk``
|
|
26
|
+
itself raises (its own HTTP error types, its own retry policy via
|
|
27
|
+
``callerOptions``) they never raise ``aice_core.AiceError`` subclasses and
|
|
28
|
+
never pass through the idempotency-key handling documented in
|
|
29
|
+
``docs/conventions.md``. Calls through ``client.agents``/``client.skills``/
|
|
30
|
+
``client.usage``/``client.suggestions`` behave like every other AICE SDK
|
|
31
|
+
(typed ``AiceError`` hierarchy, ``aice_core``'s retry/idempotency rules).
|
|
32
|
+
Don't assume uniform behavior across the two if you need a
|
|
33
|
+
``langgraph-sdk`` failure to look like an ``AiceError`` for your own error
|
|
34
|
+
handling, catch and translate at the call site (see ``aice_sandbox`` for the
|
|
35
|
+
translation-layer pattern if this becomes common enough to warrant one).
|
|
36
|
+
|
|
37
|
+
Custom (AICE-specific) routes exposed by the upstream service (see
|
|
38
|
+
``agent_platform/app/api/http_app.py``):
|
|
39
|
+
|
|
40
|
+
* ``GET /agents`` list configured agents
|
|
41
|
+
* ``POST /agents`` create an agent definition
|
|
42
|
+
* ``POST /agents/{agent_id}/publish`` publish an agent
|
|
43
|
+
* ``GET /skills`` list skills
|
|
44
|
+
* ``POST /skills`` create a skill
|
|
45
|
+
* ``POST /skills/{skill_id}/publish`` publish a skill
|
|
46
|
+
* ``GET /usage`` usage counters for the current project
|
|
47
|
+
* ``GET /suggestions/starters`` starter prompts
|
|
48
|
+
* ``POST /threads/{thread_id}/postturn`` generate post-turn suggestions
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
from __future__ import annotations
|
|
52
|
+
|
|
53
|
+
from typing import Any
|
|
54
|
+
|
|
55
|
+
from langgraph_sdk import get_client, get_sync_client
|
|
56
|
+
|
|
57
|
+
from aice_core import AsyncTransport, AuthContext, SyncTransport
|
|
58
|
+
|
|
59
|
+
_SERVICE_NAME = "agent-platform"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class _AgentsResource:
|
|
63
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
64
|
+
self._t = transport
|
|
65
|
+
|
|
66
|
+
def list(self) -> list[dict[str, Any]]:
|
|
67
|
+
return self._t.request("GET", "/agents")
|
|
68
|
+
|
|
69
|
+
def create(self, spec: dict[str, Any]) -> dict[str, Any]:
|
|
70
|
+
return self._t.request("POST", "/agents", json=spec)
|
|
71
|
+
|
|
72
|
+
def publish(self, agent_id: str) -> dict[str, Any]:
|
|
73
|
+
return self._t.request("POST", f"/agents/{agent_id}/publish")
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class _AsyncAgentsResource:
|
|
77
|
+
def __init__(self, transport: AsyncTransport) -> None:
|
|
78
|
+
self._t = transport
|
|
79
|
+
|
|
80
|
+
async def list(self) -> list[dict[str, Any]]:
|
|
81
|
+
return await self._t.request("GET", "/agents")
|
|
82
|
+
|
|
83
|
+
async def create(self, spec: dict[str, Any]) -> dict[str, Any]:
|
|
84
|
+
return await self._t.request("POST", "/agents", json=spec)
|
|
85
|
+
|
|
86
|
+
async def publish(self, agent_id: str) -> dict[str, Any]:
|
|
87
|
+
return await self._t.request("POST", f"/agents/{agent_id}/publish")
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class _SkillsResource:
|
|
91
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
92
|
+
self._t = transport
|
|
93
|
+
|
|
94
|
+
def list(self) -> list[dict[str, Any]]:
|
|
95
|
+
return self._t.request("GET", "/skills")
|
|
96
|
+
|
|
97
|
+
def create(self, spec: dict[str, Any]) -> dict[str, Any]:
|
|
98
|
+
return self._t.request("POST", "/skills", json=spec)
|
|
99
|
+
|
|
100
|
+
def publish(self, skill_id: str) -> dict[str, Any]:
|
|
101
|
+
return self._t.request("POST", f"/skills/{skill_id}/publish")
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class _AsyncSkillsResource:
|
|
105
|
+
def __init__(self, transport: AsyncTransport) -> None:
|
|
106
|
+
self._t = transport
|
|
107
|
+
|
|
108
|
+
async def list(self) -> list[dict[str, Any]]:
|
|
109
|
+
return await self._t.request("GET", "/skills")
|
|
110
|
+
|
|
111
|
+
async def create(self, spec: dict[str, Any]) -> dict[str, Any]:
|
|
112
|
+
return await self._t.request("POST", "/skills", json=spec)
|
|
113
|
+
|
|
114
|
+
async def publish(self, skill_id: str) -> dict[str, Any]:
|
|
115
|
+
return await self._t.request("POST", f"/skills/{skill_id}/publish")
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class _UsageResource:
|
|
119
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
120
|
+
self._t = transport
|
|
121
|
+
|
|
122
|
+
def get(self) -> dict[str, Any]:
|
|
123
|
+
return self._t.request("GET", "/usage")
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class _AsyncUsageResource:
|
|
127
|
+
def __init__(self, transport: AsyncTransport) -> None:
|
|
128
|
+
self._t = transport
|
|
129
|
+
|
|
130
|
+
async def get(self) -> dict[str, Any]:
|
|
131
|
+
return await self._t.request("GET", "/usage")
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class _SuggestionsResource:
|
|
135
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
136
|
+
self._t = transport
|
|
137
|
+
|
|
138
|
+
def starters(self) -> list[dict[str, Any]]:
|
|
139
|
+
return self._t.request("GET", "/suggestions/starters")
|
|
140
|
+
|
|
141
|
+
def create_for_thread(self, thread_id: str) -> dict[str, Any]:
|
|
142
|
+
return self._t.request("POST", f"/threads/{thread_id}/postturn")
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
class _AsyncSuggestionsResource:
|
|
146
|
+
def __init__(self, transport: AsyncTransport) -> None:
|
|
147
|
+
self._t = transport
|
|
148
|
+
|
|
149
|
+
async def starters(self) -> list[dict[str, Any]]:
|
|
150
|
+
return await self._t.request("GET", "/suggestions/starters")
|
|
151
|
+
|
|
152
|
+
async def create_for_thread(self, thread_id: str) -> dict[str, Any]:
|
|
153
|
+
return await self._t.request("POST", f"/threads/{thread_id}/postturn")
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
class AgentPlatformClient:
|
|
157
|
+
"""Sync client for the Agent Platform.
|
|
158
|
+
|
|
159
|
+
Usage custom AICE management routes::
|
|
160
|
+
|
|
161
|
+
client = AgentPlatformClient(base_url="…", api_key="sk-…")
|
|
162
|
+
agents = client.agents.list()
|
|
163
|
+
|
|
164
|
+
Usage native LangGraph surface (streaming, threads, runs)::
|
|
165
|
+
|
|
166
|
+
thread = client.threads.create()
|
|
167
|
+
for chunk in client.runs.stream(
|
|
168
|
+
thread["thread_id"], assistant_id="agent", input={"messages": [...]}
|
|
169
|
+
):
|
|
170
|
+
print(chunk.event, chunk.data)
|
|
171
|
+
|
|
172
|
+
See the module docstring for the important behavioral split between
|
|
173
|
+
these two surfaces (error types, retries, idempotency).
|
|
174
|
+
"""
|
|
175
|
+
|
|
176
|
+
def __init__(
|
|
177
|
+
self,
|
|
178
|
+
*,
|
|
179
|
+
base_url: str,
|
|
180
|
+
api_key: str,
|
|
181
|
+
timeout: float = 30.0,
|
|
182
|
+
max_retries: int = 3,
|
|
183
|
+
user_id: str | None = None,
|
|
184
|
+
user_role: str | None = None,
|
|
185
|
+
) -> None:
|
|
186
|
+
auth = AuthContext(api_key=api_key, user_id=user_id, user_role=user_role)
|
|
187
|
+
auth.require_scope("agent-platform")
|
|
188
|
+
self._t = SyncTransport(
|
|
189
|
+
base_url=base_url,
|
|
190
|
+
auth=auth,
|
|
191
|
+
service=_SERVICE_NAME,
|
|
192
|
+
timeout=timeout,
|
|
193
|
+
max_retries=max_retries,
|
|
194
|
+
)
|
|
195
|
+
self.agents = _AgentsResource(self._t)
|
|
196
|
+
self.skills = _SkillsResource(self._t)
|
|
197
|
+
self.usage = _UsageResource(self._t)
|
|
198
|
+
self.suggestions = _SuggestionsResource(self._t)
|
|
199
|
+
|
|
200
|
+
# Native LangGraph surface composed, not forked. api_key=None so
|
|
201
|
+
# langgraph-sdk doesn't also try LANGSMITH_API_KEY/LANGGRAPH_API_KEY
|
|
202
|
+
# from the environment; our bearer token goes through `headers`
|
|
203
|
+
# instead, matching how the rest of this SDK authenticates.
|
|
204
|
+
self._lg = get_sync_client(
|
|
205
|
+
url=base_url,
|
|
206
|
+
api_key=None,
|
|
207
|
+
headers=auth.as_headers(),
|
|
208
|
+
timeout=timeout,
|
|
209
|
+
)
|
|
210
|
+
self.threads = self._lg.threads
|
|
211
|
+
self.runs = self._lg.runs
|
|
212
|
+
self.assistants = self._lg.assistants
|
|
213
|
+
self.crons = self._lg.crons
|
|
214
|
+
|
|
215
|
+
def close(self) -> None:
|
|
216
|
+
self._t.close()
|
|
217
|
+
# Defensive: langgraph-sdk's sync client manages its own HTTP
|
|
218
|
+
# client lifecycle separately from aice_core's transport. Close it
|
|
219
|
+
# if the installed version exposes a close method; harmless no-op
|
|
220
|
+
# otherwise.
|
|
221
|
+
close_fn = getattr(self._lg, "close", None)
|
|
222
|
+
if callable(close_fn):
|
|
223
|
+
close_fn()
|
|
224
|
+
|
|
225
|
+
def __enter__(self) -> "AgentPlatformClient":
|
|
226
|
+
return self
|
|
227
|
+
|
|
228
|
+
def __exit__(self, *exc: Any) -> None:
|
|
229
|
+
self.close()
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
class AsyncAgentPlatformClient:
|
|
233
|
+
"""Async client for the Agent Platform. See ``AgentPlatformClient`` for
|
|
234
|
+
the sync/streaming usage split this mirrors it with ``await``."""
|
|
235
|
+
|
|
236
|
+
def __init__(
|
|
237
|
+
self,
|
|
238
|
+
*,
|
|
239
|
+
base_url: str,
|
|
240
|
+
api_key: str,
|
|
241
|
+
timeout: float = 30.0,
|
|
242
|
+
max_retries: int = 3,
|
|
243
|
+
user_id: str | None = None,
|
|
244
|
+
user_role: str | None = None,
|
|
245
|
+
) -> None:
|
|
246
|
+
auth = AuthContext(api_key=api_key, user_id=user_id, user_role=user_role)
|
|
247
|
+
auth.require_scope("agent-platform")
|
|
248
|
+
self._t = AsyncTransport(
|
|
249
|
+
base_url=base_url,
|
|
250
|
+
auth=auth,
|
|
251
|
+
service=_SERVICE_NAME,
|
|
252
|
+
timeout=timeout,
|
|
253
|
+
max_retries=max_retries,
|
|
254
|
+
)
|
|
255
|
+
self.agents = _AsyncAgentsResource(self._t)
|
|
256
|
+
self.skills = _AsyncSkillsResource(self._t)
|
|
257
|
+
self.usage = _AsyncUsageResource(self._t)
|
|
258
|
+
self.suggestions = _AsyncSuggestionsResource(self._t)
|
|
259
|
+
|
|
260
|
+
self._lg = get_client(
|
|
261
|
+
url=base_url,
|
|
262
|
+
api_key=None,
|
|
263
|
+
headers=auth.as_headers(),
|
|
264
|
+
timeout=timeout,
|
|
265
|
+
)
|
|
266
|
+
self.threads = self._lg.threads
|
|
267
|
+
self.runs = self._lg.runs
|
|
268
|
+
self.assistants = self._lg.assistants
|
|
269
|
+
self.crons = self._lg.crons
|
|
270
|
+
|
|
271
|
+
async def aclose(self) -> None:
|
|
272
|
+
await self._t.aclose()
|
|
273
|
+
close_fn = getattr(self._lg, "aclose", None) or getattr(self._lg, "close", None)
|
|
274
|
+
if callable(close_fn):
|
|
275
|
+
result = close_fn()
|
|
276
|
+
if hasattr(result, "__await__"):
|
|
277
|
+
await result
|
|
278
|
+
|
|
279
|
+
async def __aenter__(self) -> "AsyncAgentPlatformClient":
|
|
280
|
+
return self
|
|
281
|
+
|
|
282
|
+
async def __aexit__(self, *exc: Any) -> None:
|
|
283
|
+
await self.aclose()
|
|
File without changes
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"""Tests for the Agent Platform client.
|
|
2
|
+
|
|
3
|
+
Covers two things:
|
|
4
|
+
|
|
5
|
+
1. The AICE-specific management routes (agents/skills/usage/suggestions),
|
|
6
|
+
which go through aice_core's transport like every other service —
|
|
7
|
+
tested with respx the same way as the other services.
|
|
8
|
+
2. The langgraph-sdk composition wiring itself: that ``get_sync_client``/
|
|
9
|
+
``get_client`` are called with the right ``url``/``headers``/``api_key``,
|
|
10
|
+
and that ``threads``/``runs``/``assistants``/``crons`` are exposed from
|
|
11
|
+
whatever they return. This is mocked at the ``get_sync_client``/
|
|
12
|
+
``get_client`` level (not a real LangGraph server) since verifying the
|
|
13
|
+
composition wiring is the point the vendor SDK's own behavior is the
|
|
14
|
+
vendor SDK's own test suite's job, not ours.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
from types import SimpleNamespace
|
|
20
|
+
from unittest.mock import MagicMock, patch
|
|
21
|
+
|
|
22
|
+
import httpx
|
|
23
|
+
import pytest
|
|
24
|
+
import respx
|
|
25
|
+
from aice_core import AuthenticationError
|
|
26
|
+
|
|
27
|
+
from aice_agent_platform import AgentPlatformClient, AsyncAgentPlatformClient
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _fake_langgraph_client() -> SimpleNamespace:
|
|
31
|
+
"""A stand-in for langgraph_sdk's LangGraphClient/SyncLangGraphClient —
|
|
32
|
+
just needs the four attributes AgentPlatformClient reads off it."""
|
|
33
|
+
return SimpleNamespace(
|
|
34
|
+
threads=MagicMock(name="threads"),
|
|
35
|
+
runs=MagicMock(name="runs"),
|
|
36
|
+
assistants=MagicMock(name="assistants"),
|
|
37
|
+
crons=MagicMock(name="crons"),
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class TestLangGraphComposition:
|
|
42
|
+
def test_get_sync_client_called_with_our_auth_and_no_own_api_key(self) -> None:
|
|
43
|
+
fake = _fake_langgraph_client()
|
|
44
|
+
with patch(
|
|
45
|
+
"aice_agent_platform.client.get_sync_client", return_value=fake
|
|
46
|
+
) as mock_get_sync_client:
|
|
47
|
+
client = AgentPlatformClient(
|
|
48
|
+
base_url="https://api.test", api_key="sk-test", timeout=15.0
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
mock_get_sync_client.assert_called_once()
|
|
52
|
+
_, kwargs = mock_get_sync_client.call_args
|
|
53
|
+
assert kwargs["url"] == "https://api.test"
|
|
54
|
+
# Our own bearer token goes through `headers`; the vendor
|
|
55
|
+
# client's own api_key handling is explicitly disabled so it
|
|
56
|
+
# doesn't also try LANGSMITH_API_KEY/LANGGRAPH_API_KEY.
|
|
57
|
+
assert kwargs["api_key"] is None
|
|
58
|
+
assert kwargs["headers"].get("Authorization") == "Bearer sk-test"
|
|
59
|
+
assert kwargs["timeout"] == 15.0
|
|
60
|
+
|
|
61
|
+
# The four native resources are exposed straight from whatever
|
|
62
|
+
# get_sync_client returned no wrapping, no overriding.
|
|
63
|
+
assert client.threads is fake.threads
|
|
64
|
+
assert client.runs is fake.runs
|
|
65
|
+
assert client.assistants is fake.assistants
|
|
66
|
+
assert client.crons is fake.crons
|
|
67
|
+
|
|
68
|
+
def test_async_client_uses_get_client(self) -> None:
|
|
69
|
+
fake = _fake_langgraph_client()
|
|
70
|
+
with patch("aice_agent_platform.client.get_client", return_value=fake) as mock_get_client:
|
|
71
|
+
client = AsyncAgentPlatformClient(base_url="https://api.test", api_key="sk-test")
|
|
72
|
+
mock_get_client.assert_called_once()
|
|
73
|
+
assert client.threads is fake.threads
|
|
74
|
+
assert client.runs is fake.runs
|
|
75
|
+
|
|
76
|
+
def test_close_also_closes_langgraph_client_if_supported(self) -> None:
|
|
77
|
+
fake = _fake_langgraph_client()
|
|
78
|
+
fake.close = MagicMock()
|
|
79
|
+
with patch("aice_agent_platform.client.get_sync_client", return_value=fake):
|
|
80
|
+
client = AgentPlatformClient(base_url="https://api.test", api_key="sk-test")
|
|
81
|
+
client.close()
|
|
82
|
+
fake.close.assert_called_once()
|
|
83
|
+
|
|
84
|
+
def test_close_is_a_noop_if_langgraph_client_has_no_close(self) -> None:
|
|
85
|
+
# SimpleNamespace has no `close` attribute this must not raise.
|
|
86
|
+
fake = _fake_langgraph_client()
|
|
87
|
+
with patch("aice_agent_platform.client.get_sync_client", return_value=fake):
|
|
88
|
+
client = AgentPlatformClient(base_url="https://api.test", api_key="sk-test")
|
|
89
|
+
client.close() # should not raise
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class TestCustomManagementRoutes:
|
|
93
|
+
"""The AICE-specific surface routed through aice_core's transport,
|
|
94
|
+
so it gets typed errors/retries/idempotency like every other service."""
|
|
95
|
+
|
|
96
|
+
def _client(self) -> AgentPlatformClient:
|
|
97
|
+
with patch(
|
|
98
|
+
"aice_agent_platform.client.get_sync_client",
|
|
99
|
+
return_value=_fake_langgraph_client(),
|
|
100
|
+
):
|
|
101
|
+
return AgentPlatformClient(base_url="https://api.test", api_key="sk-test")
|
|
102
|
+
|
|
103
|
+
def test_agents_list(self) -> None:
|
|
104
|
+
with respx.mock:
|
|
105
|
+
respx.get("https://api.test/agents").mock(
|
|
106
|
+
return_value=httpx.Response(200, json=[{"id": "ag_1"}])
|
|
107
|
+
)
|
|
108
|
+
c = self._client()
|
|
109
|
+
assert c.agents.list() == [{"id": "ag_1"}]
|
|
110
|
+
|
|
111
|
+
def test_agents_publish(self) -> None:
|
|
112
|
+
with respx.mock:
|
|
113
|
+
respx.post("https://api.test/agents/ag_1/publish").mock(
|
|
114
|
+
return_value=httpx.Response(200, json={"status": "published"})
|
|
115
|
+
)
|
|
116
|
+
c = self._client()
|
|
117
|
+
assert c.agents.publish("ag_1") == {"status": "published"}
|
|
118
|
+
|
|
119
|
+
def test_skills_create(self) -> None:
|
|
120
|
+
with respx.mock:
|
|
121
|
+
respx.post("https://api.test/skills").mock(
|
|
122
|
+
return_value=httpx.Response(201, json={"id": "sk_1"})
|
|
123
|
+
)
|
|
124
|
+
c = self._client()
|
|
125
|
+
assert c.skills.create({"name": "search"}) == {"id": "sk_1"}
|
|
126
|
+
|
|
127
|
+
def test_usage_get(self) -> None:
|
|
128
|
+
with respx.mock:
|
|
129
|
+
respx.get("https://api.test/usage").mock(
|
|
130
|
+
return_value=httpx.Response(200, json={"tokens": 100})
|
|
131
|
+
)
|
|
132
|
+
c = self._client()
|
|
133
|
+
assert c.usage.get() == {"tokens": 100}
|
|
134
|
+
|
|
135
|
+
def test_suggestions_create_for_thread(self) -> None:
|
|
136
|
+
with respx.mock:
|
|
137
|
+
respx.post("https://api.test/threads/th_1/postturn").mock(
|
|
138
|
+
return_value=httpx.Response(200, json={"suggestions": []})
|
|
139
|
+
)
|
|
140
|
+
c = self._client()
|
|
141
|
+
assert c.suggestions.create_for_thread("th_1") == {"suggestions": []}
|
|
142
|
+
|
|
143
|
+
def test_auth_error_maps_to_typed_exception(self) -> None:
|
|
144
|
+
with respx.mock:
|
|
145
|
+
respx.get("https://api.test/agents").mock(
|
|
146
|
+
return_value=httpx.Response(401, json={"message": "bad token"})
|
|
147
|
+
)
|
|
148
|
+
c = self._client()
|
|
149
|
+
with pytest.raises(AuthenticationError):
|
|
150
|
+
c.agents.list()
|