pal-runtime 0.1.0__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.
- pal_runtime-0.1.0/LICENSE +202 -0
- pal_runtime-0.1.0/PKG-INFO +373 -0
- pal_runtime-0.1.0/README.md +349 -0
- pal_runtime-0.1.0/pal/__init__.py +159 -0
- pal_runtime-0.1.0/pal/agent.py +234 -0
- pal_runtime-0.1.0/pal/client.py +2246 -0
- pal_runtime-0.1.0/pal/errors.py +62 -0
- pal_runtime-0.1.0/pal/integrations/__init__.py +5 -0
- pal_runtime-0.1.0/pal/integrations/deepagents_sandbox.py +169 -0
- pal_runtime-0.1.0/pal/integrations/runtime_client.py +116 -0
- pal_runtime-0.1.0/pal/runtime/__init__.py +25 -0
- pal_runtime-0.1.0/pal/runtime/context.py +127 -0
- pal_runtime-0.1.0/pal/runtime/llm_client.py +203 -0
- pal_runtime-0.1.0/pal/runtime/memory_client.py +128 -0
- pal_runtime-0.1.0/pal/runtime/runner.py +149 -0
- pal_runtime-0.1.0/pal/runtime/runtime.py +53 -0
- pal_runtime-0.1.0/pal/runtime/step_runtime.py +146 -0
- pal_runtime-0.1.0/pal/runtime/tool_client.py +109 -0
- pal_runtime-0.1.0/pal/step.py +209 -0
- pal_runtime-0.1.0/pal/tools.py +109 -0
- pal_runtime-0.1.0/pal/types.py +786 -0
- pal_runtime-0.1.0/pal_runtime.egg-info/PKG-INFO +373 -0
- pal_runtime-0.1.0/pal_runtime.egg-info/SOURCES.txt +35 -0
- pal_runtime-0.1.0/pal_runtime.egg-info/dependency_links.txt +1 -0
- pal_runtime-0.1.0/pal_runtime.egg-info/entry_points.txt +2 -0
- pal_runtime-0.1.0/pal_runtime.egg-info/requires.txt +14 -0
- pal_runtime-0.1.0/pal_runtime.egg-info/top_level.txt +1 -0
- pal_runtime-0.1.0/pyproject.toml +46 -0
- pal_runtime-0.1.0/setup.cfg +4 -0
- pal_runtime-0.1.0/tests/test_a2a.py +50 -0
- pal_runtime-0.1.0/tests/test_agent.py +192 -0
- pal_runtime-0.1.0/tests/test_client.py +1041 -0
- pal_runtime-0.1.0/tests/test_deepagents_sandbox.py +185 -0
- pal_runtime-0.1.0/tests/test_egress_inject_types.py +33 -0
- pal_runtime-0.1.0/tests/test_retry.py +112 -0
- pal_runtime-0.1.0/tests/test_runtime.py +152 -0
- pal_runtime-0.1.0/tests/test_step.py +191 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pal-runtime
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python SDK for PAL (Personalized Agentic Layer) — the PAL Runtime
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
Project-URL: Homepage, https://github.com/dshakes/pal
|
|
7
|
+
Project-URL: Source, https://github.com/dshakes/pal/tree/master/packages/sdk-python
|
|
8
|
+
Requires-Python: >=3.11
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: httpx>=0.27
|
|
12
|
+
Requires-Dist: pydantic>=2.0
|
|
13
|
+
Requires-Dist: grpcio>=1.62
|
|
14
|
+
Requires-Dist: protobuf>=5.0
|
|
15
|
+
Requires-Dist: opentelemetry-api>=1.20
|
|
16
|
+
Provides-Extra: dev
|
|
17
|
+
Requires-Dist: pytest; extra == "dev"
|
|
18
|
+
Requires-Dist: pytest-asyncio; extra == "dev"
|
|
19
|
+
Requires-Dist: ruff; extra == "dev"
|
|
20
|
+
Requires-Dist: mypy; extra == "dev"
|
|
21
|
+
Provides-Extra: deepagents
|
|
22
|
+
Requires-Dist: deepagents>=0.6; extra == "deepagents"
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# pal-runtime (Python)
|
|
26
|
+
|
|
27
|
+
The official Python SDK for [PAL](https://github.com/dshakes/pal) — the
|
|
28
|
+
open-source runtime for production AI agents with VPC deployment, pre-run cost
|
|
29
|
+
forecasts, policy budgets, and eval-in-CI.
|
|
30
|
+
|
|
31
|
+
Python 3.11+. Async-first (httpx + asyncio). Full parity with the TypeScript SDK's
|
|
32
|
+
HTTP surface.
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Install
|
|
37
|
+
|
|
38
|
+
Registry packages are not published yet. The distribution is named `pal-runtime`; do not
|
|
39
|
+
`pip install pal-sdk` (that PyPI name belongs to an unrelated project). Install from the repo:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install "path/to/pal/packages/sdk-python" # add [deepagents] for the extra below
|
|
43
|
+
# or in CI:
|
|
44
|
+
pip install "git+https://github.com/dshakes/pal.git#subdirectory=packages/sdk-python"
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The import package is `pal` (`from pal import PalClient`); the CLI entry point is `pal-runner`.
|
|
48
|
+
|
|
49
|
+
Dependencies pulled in automatically: `httpx>=0.27`, `pydantic>=2.0`, `grpcio>=1.62`,
|
|
50
|
+
`protobuf>=5.0`, `opentelemetry-api>=1.20`.
|
|
51
|
+
|
|
52
|
+
Optional extras:
|
|
53
|
+
|
|
54
|
+
| Extra | Pulls in | For |
|
|
55
|
+
| ------------ | ------------------ | ---------------------------------------------------------- |
|
|
56
|
+
| `deepagents` | `deepagents>=0.6` | Run a LangChain deepagents agent on a PAL microVM — see [deepagents integration](#deepagents-integration) |
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Quick start
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
import asyncio
|
|
64
|
+
import os
|
|
65
|
+
from pal import PalClient
|
|
66
|
+
|
|
67
|
+
async def main():
|
|
68
|
+
async with PalClient(
|
|
69
|
+
api_key=os.environ["PAL_API_KEY"],
|
|
70
|
+
# base_url defaults to PAL_API_URL, else http://localhost:8080
|
|
71
|
+
# or set PAL_API_URL for a self-hosted instance
|
|
72
|
+
) as client:
|
|
73
|
+
|
|
74
|
+
# 1. Create an agent
|
|
75
|
+
agent = await client.agents.create(name="triage", description="Classifies support emails")
|
|
76
|
+
|
|
77
|
+
# 2. Run it
|
|
78
|
+
run = await client.runs.create(agent="triage", input={"email": "my invoice is wrong"})
|
|
79
|
+
print(run.status, run.output)
|
|
80
|
+
|
|
81
|
+
# 3. Stream events in real time
|
|
82
|
+
async for event in client.runs.events(run.id):
|
|
83
|
+
print(event.kind, event.data)
|
|
84
|
+
|
|
85
|
+
asyncio.run(main())
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Client constructor
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
PalClient(
|
|
94
|
+
base_url: str | None = None, # default: PAL_API_URL or http://localhost:8080
|
|
95
|
+
api_key: str | None = None, # default: PAL_API_KEY
|
|
96
|
+
*,
|
|
97
|
+
timeout: float = 30.0, # per-request timeout in seconds
|
|
98
|
+
route_strategy: str | None = None, # default: PAL_ROUTE_STRATEGY or "auto"
|
|
99
|
+
)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Use as a context manager (`async with PalClient(...) as client`) to ensure the
|
|
103
|
+
underlying httpx connection pool is closed. Call `await client.close()` manually if
|
|
104
|
+
not using a context manager.
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## Env vars
|
|
109
|
+
|
|
110
|
+
| Var | Purpose |
|
|
111
|
+
|---|---|
|
|
112
|
+
| `PAL_API_URL` | Base URL of the control-plane API. Default `http://localhost:8080`; set it to your control plane |
|
|
113
|
+
| `PAL_API_KEY` | Bearer token issued by `POST /auth/login` or the API-keys settings page |
|
|
114
|
+
| `PAL_ROUTE_STRATEGY` | One of `auto`, `cheap`, `best`, `fast`. Sent as `X-Pal-Route-Strategy`. Default `auto` |
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## API reference
|
|
119
|
+
|
|
120
|
+
### Agents — `client.agents`
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
# Create
|
|
124
|
+
agent: AgentInfo = await client.agents.create(name="my-agent", description="...", labels={})
|
|
125
|
+
|
|
126
|
+
# Get
|
|
127
|
+
agent: AgentInfo = await client.agents.get("my-agent")
|
|
128
|
+
|
|
129
|
+
# List
|
|
130
|
+
resp: AgentListResponse = await client.agents.list(page_size=50, page_token=None)
|
|
131
|
+
# resp.agents: list[AgentInfo]
|
|
132
|
+
# resp.next_page_token: str | None
|
|
133
|
+
|
|
134
|
+
# Delete
|
|
135
|
+
await client.agents.delete("my-agent")
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Runs — `client.runs`
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
# Create (returns Run)
|
|
142
|
+
run: Run = await client.runs.create(
|
|
143
|
+
agent="triage",
|
|
144
|
+
input={"email": "..."},
|
|
145
|
+
labels={},
|
|
146
|
+
idempotency_key=None, # optional; derived from (run_id, step_id, attempt) in the engine
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
# Create with streaming (returns async iterator of StreamEvent)
|
|
150
|
+
stream = await client.runs.create(agent="triage", input={...}, stream=True)
|
|
151
|
+
async for event in stream:
|
|
152
|
+
print(event.kind, event.data)
|
|
153
|
+
|
|
154
|
+
# Get, list, cancel
|
|
155
|
+
run: Run = await client.runs.get(run_id)
|
|
156
|
+
resp: RunListResponse = await client.runs.list(agent="triage", status="completed", page_size=50)
|
|
157
|
+
run: Run = await client.runs.cancel(run_id, reason="user cancelled")
|
|
158
|
+
|
|
159
|
+
# Stream events from a completed or live run
|
|
160
|
+
async for event in client.runs.events(run_id, from_seq=0, live=True):
|
|
161
|
+
print(event.kind, event.data)
|
|
162
|
+
|
|
163
|
+
# Send a signal to a paused run (workflow approval / step_signal)
|
|
164
|
+
await client.runs.signal(run_id, name="approved", value={"comment": "lgtm"})
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Sessions — `client.sessions`
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
# Create an interactive multi-turn session
|
|
171
|
+
session: Session = await client.sessions.create(agent="my-agent", metadata={})
|
|
172
|
+
|
|
173
|
+
# Send a message and get the synchronous response
|
|
174
|
+
msg: SessionMessage = await client.sessions.send_message(session_id, content="hello", role="user")
|
|
175
|
+
|
|
176
|
+
# Stream session events (SSE)
|
|
177
|
+
async for event in client.sessions.stream_events(session_id, from_seq=0):
|
|
178
|
+
print(event)
|
|
179
|
+
|
|
180
|
+
# Get, list, close
|
|
181
|
+
session = await client.sessions.get(session_id)
|
|
182
|
+
resp: SessionListResponse = await client.sessions.list(agent="my-agent", status="active")
|
|
183
|
+
await client.sessions.close(session_id)
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Connectors — `client.connectors`
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
# List installed connectors
|
|
190
|
+
resp: ConnectorListResponse = await client.connectors.list()
|
|
191
|
+
|
|
192
|
+
# Execute a connector action (e.g. send a Slack message)
|
|
193
|
+
result: ConnectorResult = await client.connectors.execute(
|
|
194
|
+
"slack",
|
|
195
|
+
"send_message",
|
|
196
|
+
params={"channel": "#general", "text": "Hello from PAL!"},
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
# List available actions for a connector
|
|
200
|
+
actions: list[dict] = await client.connectors.list_actions("slack")
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
|
|
205
|
+
## Building an agent (decorator style)
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
from pal import agent, step, tool
|
|
209
|
+
from pal.types import AgentContext
|
|
210
|
+
|
|
211
|
+
@agent("researcher", model="reasoning-large", description="Deep research agent")
|
|
212
|
+
async def researcher(input: dict, ctx: AgentContext) -> dict:
|
|
213
|
+
# step() is durable: result is journalled and replayed on restart
|
|
214
|
+
results = await step(
|
|
215
|
+
"search",
|
|
216
|
+
lambda: ctx.tools.web.search(input["query"]),
|
|
217
|
+
)
|
|
218
|
+
# parallel fan-out
|
|
219
|
+
summaries = await step_map(
|
|
220
|
+
"summarize",
|
|
221
|
+
results[:5],
|
|
222
|
+
lambda item, i: ctx.llm.complete(prompt=f"Summarize: {item}"),
|
|
223
|
+
concurrency=3,
|
|
224
|
+
)
|
|
225
|
+
return {"summaries": summaries}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Step primitives
|
|
229
|
+
|
|
230
|
+
| Function | Behaviour |
|
|
231
|
+
|---|---|
|
|
232
|
+
| `await step(name, fn, *, retry, timeout)` | Single durable step. `fn` is an async callable. |
|
|
233
|
+
| `await step_map(name, items, fn, *, concurrency, retry, timeout)` | Fan-out over a list; each item gets its own journal entry `name[i]` |
|
|
234
|
+
| `await step_race(name, fns, *, retry, timeout)` | Run multiple callables concurrently; return the first result, cancel the rest |
|
|
235
|
+
| `await step_sleep(name, duration)` | Durable sleep. Duration strings: `"30s"`, `"5m"`, `"2h"` |
|
|
236
|
+
| `await step_signal(name, *, timeout)` | Wait for an external signal. Only functional in the PAL production runtime |
|
|
237
|
+
|
|
238
|
+
In local / dev mode all step primitives execute the function directly (no
|
|
239
|
+
journalling). The production runtime installs a journal-aware runner via
|
|
240
|
+
`set_step_runtime()`.
|
|
241
|
+
|
|
242
|
+
### Built-in tools
|
|
243
|
+
|
|
244
|
+
```python
|
|
245
|
+
from pal.tools import tool # singleton registry
|
|
246
|
+
|
|
247
|
+
@agent("worker", tools=[tool.web, tool.python])
|
|
248
|
+
async def worker(input: dict, ctx: AgentContext) -> dict:
|
|
249
|
+
html = await ctx.tools.web.fetch("https://example.com")
|
|
250
|
+
...
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Available: `tool.web`, `tool.python`, `tool.fs`, `tool.browser`. `tool.all()` returns
|
|
254
|
+
all four.
|
|
255
|
+
|
|
256
|
+
### Class-based pattern
|
|
257
|
+
|
|
258
|
+
```python
|
|
259
|
+
from pal.agent import PalAgent
|
|
260
|
+
from pal.types import AgentContext
|
|
261
|
+
|
|
262
|
+
class MyAgent(PalAgent):
|
|
263
|
+
name = "my-agent"
|
|
264
|
+
model = "auto"
|
|
265
|
+
|
|
266
|
+
async def run(self, input: dict, ctx: AgentContext) -> dict:
|
|
267
|
+
...
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
---
|
|
271
|
+
|
|
272
|
+
## Errors
|
|
273
|
+
|
|
274
|
+
All errors inherit from `PalError`.
|
|
275
|
+
|
|
276
|
+
| Exception | Raised when |
|
|
277
|
+
|---|---|
|
|
278
|
+
| `PalApiError(status_code, body)` | HTTP response is non-2xx |
|
|
279
|
+
| `PalStepError(name, reason)` | Step execution fails |
|
|
280
|
+
| `PalLlmError` | LLM call fails (budget, provider, timeout) |
|
|
281
|
+
| `PalTimeoutError(name, duration)` | `step()` / `step_sleep()` timeout exceeded |
|
|
282
|
+
| `PalValidationError` | Invalid agent name or config |
|
|
283
|
+
|
|
284
|
+
---
|
|
285
|
+
|
|
286
|
+
## Running an agent locally
|
|
287
|
+
|
|
288
|
+
```bash
|
|
289
|
+
python -m pal.runtime.runner --agent hello-world --input '{"name": "World"}'
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
The `pal-runner` entry point (installed by `pip install`) does the same:
|
|
293
|
+
|
|
294
|
+
```bash
|
|
295
|
+
pal-runner --agent hello-world --input '{"name": "World"}'
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
300
|
+
## deepagents integration
|
|
301
|
+
|
|
302
|
+
`pip install "path/to/pal/packages/sdk-python[deepagents]"` adds a
|
|
303
|
+
[deepagents](https://github.com/langchain-ai/deepagents) sandbox backend that runs
|
|
304
|
+
the agent's shell and filesystem inside a PAL microVM instead of on the host.
|
|
305
|
+
Egress allowlist, secret vending, and per-tenant quota are enforced by the runtime,
|
|
306
|
+
not by the prompt — untrusted agent code never touches a bare shell (invariant #5).
|
|
307
|
+
|
|
308
|
+
```python
|
|
309
|
+
from deepagents import create_deep_agent
|
|
310
|
+
from pal.integrations.deepagents_sandbox import PalSandbox
|
|
311
|
+
|
|
312
|
+
with PalSandbox.create(image_digest="sha256:...") as sandbox:
|
|
313
|
+
agent = create_deep_agent(backend=sandbox)
|
|
314
|
+
agent.invoke({"messages": [{"role": "user", "content": "run the tests"}]})
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
`PalSandbox` implements deepagents' `SandboxBackendProtocol`. deepagents'
|
|
318
|
+
`BaseSandbox` derives the full filesystem toolset (`ls`/`read`/`write`/`edit`/
|
|
319
|
+
`glob`/`grep`) from three primitives, so the backend is just those three mapped
|
|
320
|
+
onto the runtime REST surface:
|
|
321
|
+
|
|
322
|
+
| Backend method | PAL endpoint |
|
|
323
|
+
| -------------------------------- | ----------------------------------------- |
|
|
324
|
+
| `execute()` | `POST /v1/runtime/vms/{id}/exec` |
|
|
325
|
+
| `upload_files()`/`download_files()` | base64 over the same exec channel |
|
|
326
|
+
| `create()` / `close()` | `POST /v1/runtime/schedule` / `DELETE /v1/runtime/vms/{id}` |
|
|
327
|
+
|
|
328
|
+
Constructors:
|
|
329
|
+
|
|
330
|
+
- `PalSandbox.create(image_digest=..., **spec)` — schedules a fresh microVM and
|
|
331
|
+
polls until it is `running`. Extra kwargs pass through to `/v1/runtime/schedule`
|
|
332
|
+
(`limits`, `egressRules`, `secrets`, `labels`, ...). Terminates the VM on exit.
|
|
333
|
+
- `PalSandbox.connect(vm_id)` — attaches to an already-running VM; does **not**
|
|
334
|
+
terminate it on close.
|
|
335
|
+
|
|
336
|
+
Composes with deepagents' own `CompositeBackend` so state can outlive the VM —
|
|
337
|
+
mount a store at `/memories/` while everything else executes in the sandbox. See
|
|
338
|
+
[`examples/deepagents_sandbox_agent.py`](examples/deepagents_sandbox_agent.py) for a
|
|
339
|
+
runnable version that also wires `RubricMiddleware` for in-loop self-evaluation.
|
|
340
|
+
|
|
341
|
+
Reads `PAL_API_URL` / `PAL_API_KEY` like the main client; pass `base_url` /
|
|
342
|
+
`api_key` explicitly to override.
|
|
343
|
+
|
|
344
|
+
`create()` defaults the entrypoint to `sleep infinity`. A sandbox is a long-lived
|
|
345
|
+
exec target, so an image whose own entrypoint runs to completion (`python:3.11-slim`
|
|
346
|
+
just starts a REPL and exits) would be reaped by the runtime before the agent could
|
|
347
|
+
exec into it. Pass an explicit `command` to override.
|
|
348
|
+
|
|
349
|
+
**Verified against a real VM.** The full path — `PalSandbox` → control-plane
|
|
350
|
+
→ runtime-scheduler → runtime-manager → real container — is exercised end to end
|
|
351
|
+
on the `docker` backend, covering `execute`, binary upload/download roundtrip, and
|
|
352
|
+
the BaseSandbox-derived `write`/`read`/`ls`/`edit`/`grep` the agent actually calls.
|
|
353
|
+
The Go-side regression test is
|
|
354
|
+
`services/control-plane/internal/handlers/runtime_exec_e2e_test.go` (opt-in via
|
|
355
|
+
`PAL_RUNTIME_E2E=1`). The **Firecracker** path specifically is not verifiable
|
|
356
|
+
on macOS (no `/dev/kvm`) — that is covered by the `microvm · integration` CI job on
|
|
357
|
+
a KVM runner. Exec routing differs between them: the manager sends Firecracker VMs
|
|
358
|
+
to the in-guest harness and everything else through the backend.
|
|
359
|
+
|
|
360
|
+
**Known ceilings.** File transfer is base64-inline over the exec channel — fine for
|
|
361
|
+
agent-sized files, but a large binary should move via object storage. A microVM
|
|
362
|
+
image digest is required; the sandbox does not build one for you.
|
|
363
|
+
|
|
364
|
+
---
|
|
365
|
+
|
|
366
|
+
## Parity note
|
|
367
|
+
|
|
368
|
+
The Python SDK's management surface (`agents`, `runs`, `sessions`, `connectors`,
|
|
369
|
+
`budgets`, `evals`, `experiments`, `marketplace`, `mcp`, `receipts`, `feedback`,
|
|
370
|
+
`rehearsals`, plus `debug` and `support`) is at parity with the TypeScript SDK's HTTP surface — all typed,
|
|
371
|
+
none require raw `client._request(...)` calls. The agent definition layer
|
|
372
|
+
(`@agent`, `step`, `tool`) mirrors the TypeScript surface except that `zod`
|
|
373
|
+
schema validation is replaced by plain Python type hints and pydantic models.
|