spredr 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.
- spredr-0.1.0/.gitignore +4 -0
- spredr-0.1.0/LICENSE +33 -0
- spredr-0.1.0/PKG-INFO +237 -0
- spredr-0.1.0/README.md +188 -0
- spredr-0.1.0/pyproject.toml +27 -0
- spredr-0.1.0/src/spredr/__init__.py +36 -0
- spredr-0.1.0/src/spredr/_http.py +191 -0
- spredr-0.1.0/src/spredr/_json.py +60 -0
- spredr-0.1.0/src/spredr/_sign.py +91 -0
- spredr-0.1.0/src/spredr/_tag.py +41 -0
- spredr-0.1.0/src/spredr/_usage.py +45 -0
- spredr-0.1.0/src/spredr/agent.py +269 -0
- spredr-0.1.0/src/spredr/caller.py +44 -0
- spredr-0.1.0/src/spredr/context.py +282 -0
- spredr-0.1.0/src/spredr/errors.py +78 -0
- spredr-0.1.0/src/spredr/owner.py +179 -0
- spredr-0.1.0/src/spredr/py.typed +0 -0
- spredr-0.1.0/src/spredr/tasks.py +165 -0
- spredr-0.1.0/tests/contract.json +65 -0
- spredr-0.1.0/tests/fake_ai.py +77 -0
- spredr-0.1.0/tests/test_contract.py +60 -0
- spredr-0.1.0/tests/test_more.py +272 -0
- spredr-0.1.0/tests/test_sdk.py +192 -0
spredr-0.1.0/.gitignore
ADDED
spredr-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
spredr SDK License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Candhrly, LLC. All rights reserved.
|
|
4
|
+
|
|
5
|
+
This license covers @spredr/sdk («the SDK»): the code in this package and in its repository. Candhrly, LLC («we»)
|
|
6
|
+
owns the SDK. It is not open source.
|
|
7
|
+
|
|
8
|
+
1. What you may do. We grant you a worldwide, non-exclusive, royalty-free license to install, run and copy the SDK,
|
|
9
|
+
and to adapt it as far as your own software needs, only to use spredr's services (spredr.cloud). You may
|
|
10
|
+
distribute the SDK only as part of your own software that uses spredr, with this license and the copyright notice
|
|
11
|
+
kept.
|
|
12
|
+
|
|
13
|
+
2. What you may not do. You may not:
|
|
14
|
+
a. distribute the SDK on its own, or publish a modified version or a fork of it;
|
|
15
|
+
b. use the SDK, or any part of it, to reach a service other than spredr, or to build a service that competes with
|
|
16
|
+
spredr;
|
|
17
|
+
c. remove or change this license, the copyright notice or the trademark notice.
|
|
18
|
+
|
|
19
|
+
3. Ownership and trademarks. We keep every right we do not grant here. The spredr name and logo are trademarks of
|
|
20
|
+
Candhrly, LLC and are not licensed by this file (see TRADEMARKS.md).
|
|
21
|
+
|
|
22
|
+
4. Ending. This license ends at once if you break its terms. When it ends, you stop using and distributing the SDK.
|
|
23
|
+
|
|
24
|
+
5. Future versions. We may publish later versions of the SDK under an open-source license. A version keeps the
|
|
25
|
+
license it was published with.
|
|
26
|
+
|
|
27
|
+
6. No warranty. THE SDK IS PROVIDED «AS IS», WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
|
28
|
+
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
29
|
+
|
|
30
|
+
7. Limit of liability. IN NO EVENT SHALL CANDHRLY, LLC BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|
31
|
+
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SDK OR ITS USE.
|
|
32
|
+
|
|
33
|
+
Your use of spredr's services is also governed by spredr's terms of service.
|
spredr-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: spredr
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Build AI agents as ordinary services and run them on spredr: tasks, questions before risky actions, memory per customer, lessons and traces.
|
|
5
|
+
Project-URL: Homepage, https://spredr.cloud
|
|
6
|
+
Project-URL: Documentation, https://spredr.cloud/docs
|
|
7
|
+
Author: Candhrly, LLC
|
|
8
|
+
License: spredr SDK License
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2026 Candhrly, LLC. All rights reserved.
|
|
11
|
+
|
|
12
|
+
This license covers @spredr/sdk («the SDK»): the code in this package and in its repository. Candhrly, LLC («we»)
|
|
13
|
+
owns the SDK. It is not open source.
|
|
14
|
+
|
|
15
|
+
1. What you may do. We grant you a worldwide, non-exclusive, royalty-free license to install, run and copy the SDK,
|
|
16
|
+
and to adapt it as far as your own software needs, only to use spredr's services (spredr.cloud). You may
|
|
17
|
+
distribute the SDK only as part of your own software that uses spredr, with this license and the copyright notice
|
|
18
|
+
kept.
|
|
19
|
+
|
|
20
|
+
2. What you may not do. You may not:
|
|
21
|
+
a. distribute the SDK on its own, or publish a modified version or a fork of it;
|
|
22
|
+
b. use the SDK, or any part of it, to reach a service other than spredr, or to build a service that competes with
|
|
23
|
+
spredr;
|
|
24
|
+
c. remove or change this license, the copyright notice or the trademark notice.
|
|
25
|
+
|
|
26
|
+
3. Ownership and trademarks. We keep every right we do not grant here. The spredr name and logo are trademarks of
|
|
27
|
+
Candhrly, LLC and are not licensed by this file (see TRADEMARKS.md).
|
|
28
|
+
|
|
29
|
+
4. Ending. This license ends at once if you break its terms. When it ends, you stop using and distributing the SDK.
|
|
30
|
+
|
|
31
|
+
5. Future versions. We may publish later versions of the SDK under an open-source license. A version keeps the
|
|
32
|
+
license it was published with.
|
|
33
|
+
|
|
34
|
+
6. No warranty. THE SDK IS PROVIDED «AS IS», WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
|
35
|
+
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
36
|
+
|
|
37
|
+
7. Limit of liability. IN NO EVENT SHALL CANDHRLY, LLC BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|
38
|
+
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SDK OR ITS USE.
|
|
39
|
+
|
|
40
|
+
Your use of spredr's services is also governed by spredr's terms of service.
|
|
41
|
+
License-File: LICENSE
|
|
42
|
+
Keywords: agents,ai,hosting,llm,spredr
|
|
43
|
+
Classifier: Framework :: AsyncIO
|
|
44
|
+
Classifier: Programming Language :: Python :: 3
|
|
45
|
+
Classifier: Typing :: Typed
|
|
46
|
+
Requires-Python: >=3.10
|
|
47
|
+
Requires-Dist: cryptography>=42
|
|
48
|
+
Description-Content-Type: text/markdown
|
|
49
|
+
|
|
50
|
+
# spredr for Python
|
|
51
|
+
|
|
52
|
+
Build an AI agent as an ordinary Python service and run it on [spredr](https://spredr.cloud). Your code says what the
|
|
53
|
+
agent does and calls the model you choose (Claude, OpenAI, Gemini or your own) with your own key. spredr runs it and
|
|
54
|
+
adds what an agent needs to be trusted with real work:
|
|
55
|
+
|
|
56
|
+
- it hands the agent **tasks**, every user taking turns, and keeps a journal so a task that comes back never does a
|
|
57
|
+
step twice;
|
|
58
|
+
- it **holds a risky action** (a refund, an email, a booking) until a person says yes;
|
|
59
|
+
- it keeps a **memory per customer**, and turns your corrections into lessons;
|
|
60
|
+
- it shows **every step**, with its tokens and cost.
|
|
61
|
+
|
|
62
|
+
This is the same SDK as [`@spredr/sdk`](https://www.npmjs.com/package/@spredr/sdk) for Node, with Python's names. Both
|
|
63
|
+
run one shared contract (every call and the exact request it makes to ai), so they do the same thing.
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
pip install spredr
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Python 3.10 or newer. One dependency, `cryptography`, for the ed25519 signatures. The methods are `async`.
|
|
70
|
+
|
|
71
|
+
| You are | You use | You can |
|
|
72
|
+
|---|---|---|
|
|
73
|
+
| **The agent** | `Agent` | take tasks, ask a person before a risky action, remember, learn, report each step |
|
|
74
|
+
| **Your backend** | `Caller` | give the agent tasks, follow them, answer its questions, receive callbacks |
|
|
75
|
+
| **You** | `Spredr` | register agents, link backends, schedule work, read memory, approve lessons, set limits |
|
|
76
|
+
|
|
77
|
+
## The agent
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from anthropic import AsyncAnthropic
|
|
81
|
+
from spredr import Agent
|
|
82
|
+
|
|
83
|
+
claude = AsyncAnthropic()
|
|
84
|
+
agent = Agent(description="Answers the shop's customers", actions=[{"name": "refund"}])
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@agent.on("reply")
|
|
88
|
+
async def reply(input, ctx):
|
|
89
|
+
order = await ctx.track("tool", "orders.find", lambda: find_order(input["order"]))
|
|
90
|
+
if input.get("refund"):
|
|
91
|
+
answer = await ctx.ask("refund", f"Refund {order['total']}?", data={"order": order["id"]})
|
|
92
|
+
if not answer.yes:
|
|
93
|
+
return {"text": "The team will look at it."}
|
|
94
|
+
message = await ctx.model(lambda: claude.messages.create(
|
|
95
|
+
model="claude-haiku-5-5",
|
|
96
|
+
max_tokens=400,
|
|
97
|
+
system="\n".join(ctx.lessons),
|
|
98
|
+
messages=[{"role": "user", "content": input["text"]}],
|
|
99
|
+
))
|
|
100
|
+
return {"text": message.content[0].text}
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
agent.run() # listens on $PORT, answers /health, says hello to ai
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
`Agent()` reads `SPREDR_AGENT_ID`, `SPREDR_AGENT_KEY`, `SPREDR_AI_URL` and `SPREDR_AI_KEY`, which spredr writes into
|
|
107
|
+
the service when you register it as an agent. What it declares (`description`, `tasks`, `actions`, `tools`,
|
|
108
|
+
`concurrency`, `timeout_seconds`, `judge`, `sleep_after_minutes`) is what its owner sees in the panel.
|
|
109
|
+
|
|
110
|
+
A handler takes `(input, ctx)` and may be sync or async. With `schema=` (a pydantic model, or any function that returns
|
|
111
|
+
the value or raises), the input is checked first, and a task whose input does not match fails saying why:
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
@agent.on("refund", schema=RefundRequest)
|
|
115
|
+
async def refund(request: RefundRequest, ctx): ...
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### `ctx`, what a handler gets
|
|
119
|
+
|
|
120
|
+
What a task already did is never done twice: when it comes back (after a question, after a crash), every `ask`,
|
|
121
|
+
`track`, `model`, `later`, `delegate` and `memory.remember` gives back what it gave the first time, without doing it.
|
|
122
|
+
|
|
123
|
+
| | |
|
|
124
|
+
|---|---|
|
|
125
|
+
| `ctx.task`, `ctx.tenant`, `ctx.thread` | The task, whom it works for (its memory, freedom and caps are that tenant's) and its conversation. |
|
|
126
|
+
| `ctx.lessons` | The lessons you approved. Put them in your prompt. |
|
|
127
|
+
| `ctx.trial` | A trial replays an old task with new lessons: do nothing real. |
|
|
128
|
+
| `await ctx.ask(action, question, options=None, data=None)` | Asks before acting. The task stops here and comes back with the answer: `answer.yes`, `answer.value`, `answer.note`. |
|
|
129
|
+
| `await ctx.track(kind, name, work, usage=None)` | Runs `work` (a function, sync or async), times it and reports it as a step. Put side effects here. |
|
|
130
|
+
| `await ctx.model(work, name=None)` | A model call, reported with its model and tokens (read from Anthropic's and OpenAI's answers). |
|
|
131
|
+
| `await ctx.later(kind, input, at, key=None)` | A task for later (a reminder, a follow-up), for the same tenant and thread. |
|
|
132
|
+
| `await ctx.delegate(agent, kind, input=None, title=None, key=None)` | Hands a part to another of your agents and waits for its result (or a `PartError`). |
|
|
133
|
+
| `await ctx.show(block)` | Shows a block in the task's page: a record, a table, an image, findings, an answer with sources. |
|
|
134
|
+
| `ctx.stream(text)`, `await ctx.flush()` | Pieces of a live answer, for whoever watches the task. |
|
|
135
|
+
| `await ctx.step(kind, name, **fields)`, `await ctx.progress(data)` | Report a step yourself, or say how it is going. |
|
|
136
|
+
| `ctx.files`, `await ctx.file(id_or_name)` | The files its session holds, and one of them as `data`, `type`, `name`. |
|
|
137
|
+
| `await ctx.memory.remember(scope, text, key=None, data=None)` | Keeps a fact for this tenant. `recall(scope=, q=, limit=)` and `forget(fact_id)` too. |
|
|
138
|
+
|
|
139
|
+
Lines logged with `logging` while a task works start with its id (`[tsk_…] …`); `spredr.current_task()` gives it to a
|
|
140
|
+
logger of your own.
|
|
141
|
+
|
|
142
|
+
### Inside a server you already run
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
from fastapi import FastAPI
|
|
146
|
+
|
|
147
|
+
app = FastAPI(on_startup=[agent.start], on_shutdown=[agent.close])
|
|
148
|
+
app.mount("/", agent.asgi())
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
With any other framework, route `POST agent.path` to `await agent.handle(method, path, headers, body)` with the raw body
|
|
152
|
+
exactly as it arrived (the signature is over those bytes) and send back the `Answer(status, headers, body)` it returns.
|
|
153
|
+
`agent.close()` lets running tasks finish before it stops (`shutdown_grace`, 25 s by default).
|
|
154
|
+
|
|
155
|
+
### Learning
|
|
156
|
+
|
|
157
|
+
`await agent.feedback(after)` reads what apps said about its tasks; `await agent.propose(text, why, sources)` proposes a
|
|
158
|
+
lesson to its owner.
|
|
159
|
+
|
|
160
|
+
## Your backend
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
from spredr import Caller
|
|
164
|
+
|
|
165
|
+
agent = Caller() # reads the variables spredr writes into a linked service
|
|
166
|
+
shop = agent.for_tenant(shop_id)
|
|
167
|
+
|
|
168
|
+
answer = await shop.result("reply", {"text": text}, thread=f"chat:{customer_id}", key=message_id)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
| | |
|
|
172
|
+
|---|---|
|
|
173
|
+
| `ask(kind, input, key=, wait=, at=, thread=, tenant=, title=)` | Asks for a task. With `key`, asking again is the same task: safe to retry. |
|
|
174
|
+
| `run(...)`, `result(...)` | Asks and waits; `result` gives the result itself, or raises a `TaskError` carrying the task. |
|
|
175
|
+
| `get(id, wait=0)`, `settle(id)`, `list(status=, limit=, before=, thread=, tenant=)` | Read tasks. |
|
|
176
|
+
| `events(id, after, wait)`, `steps(id)` | What happened inside a task; `steps` reads every page. |
|
|
177
|
+
| `stream(id)`, `live(kind, input, ...)` | A task as it happens: `delta`, `event`, and last `end` with the task. |
|
|
178
|
+
| `cancel(id)`, `pauses()`, `answer(pause_id, {"yes": True})`, `feedback(id, "good" \| "bad", correction=, note=)` | Act on tasks. |
|
|
179
|
+
| `attach(thread, name, data, type=)`, `files(thread)`, `detach(thread, name)` | Files a conversation leaves for the agent to read. |
|
|
180
|
+
| `for_tenant(tenant)` | The same calls for one tenant, plus `usage()`, `set_limits(...)`, `forget()`, `freedom()`, `set_freedom()`, `lessons()`, `teach()`, `decide()`. |
|
|
181
|
+
| `callback(method, path, headers, body)` | A callback from ai, checked (needs `SPREDR_AI_KEY`): `event` and `task`, or `None`. |
|
|
182
|
+
|
|
183
|
+
## You
|
|
184
|
+
|
|
185
|
+
```python
|
|
186
|
+
from spredr import Spredr
|
|
187
|
+
|
|
188
|
+
owner = Spredr() # SPREDR_TOKEN, an API token from the panel
|
|
189
|
+
shop = owner.agent("shop")
|
|
190
|
+
|
|
191
|
+
await shop.schedule("daily-report", "daily", "0 8 * * *", tz="America/Bogota")
|
|
192
|
+
for pause in await shop.tasks.pauses():
|
|
193
|
+
await shop.tasks.answer(pause.id, {"yes": True})
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
`Spredr`: `agents()`, `register(name, service)`, `keep_token(token)`, `inbox(hours)`, `agent(name)`.
|
|
197
|
+
|
|
198
|
+
`owner.agent(name)`: `info`, `summary`, `threads`, `tenants`, `set_tenant_limits`, `export`, `export_count`,
|
|
199
|
+
`set_limits`, `set_traces`, `traces`, `stop_traces`, `remove`, `rotate_key`, `add_caller`, `callers`, `remove_caller`,
|
|
200
|
+
`schedule`, `hook`, `stored_files`, `download_file`, `delete_file`, `memory_use`, `memory`, `edit_fact`, `forget`,
|
|
201
|
+
`freedom`, `set_freedom`, `lessons`, `write_lesson`, `decide`, `versions`, `release`, `version`, and `.tasks` with
|
|
202
|
+
everything a backend can do.
|
|
203
|
+
|
|
204
|
+
## The same as in Node, the Python way
|
|
205
|
+
|
|
206
|
+
| Node | Python |
|
|
207
|
+
|---|---|
|
|
208
|
+
| `agent.on("reply", fn)` | `@agent.on("reply")` |
|
|
209
|
+
| `await agent.listen()` | `agent.run()` · `agent.asgi()` |
|
|
210
|
+
| `caller.for(tenant)` | `caller.for_tenant(tenant)` |
|
|
211
|
+
| `shop.setLimits({ spendPerDayUsd: 8 })` | `shop.set_limits(spend_per_day_usd=8)` |
|
|
212
|
+
| `tasks.ask(kind, input, { wait: 30 })` | `tasks.ask(kind, input, wait=30)` |
|
|
213
|
+
| `task.createdAt` | `task.created_at` or `task["createdAt"]` |
|
|
214
|
+
| `agent.on(kind, zodSchema, fn)` | `@agent.on(kind, schema=PydanticModel)` |
|
|
215
|
+
| `for await (const e of tasks.stream(id))` | `async for e in tasks.stream(id)` |
|
|
216
|
+
|
|
217
|
+
What ai answers comes back as it sends it: a `dict` you can also read by attribute, in either spelling.
|
|
218
|
+
|
|
219
|
+
## Errors
|
|
220
|
+
|
|
221
|
+
`SpredrError` (with `status` and `trace`) and, by status: `BadRequestError`, `AuthenticationError`,
|
|
222
|
+
`PermissionDeniedError`, `NotFoundError`, `ConflictError`, `RateLimitError` (with `retry_after`),
|
|
223
|
+
`InternalServerError`, `ConnectionError`, `TimeoutError`. A busy ai that says when to come back (429 or 503 with
|
|
224
|
+
Retry-After) is asked again then; what can be sent twice is retried with backoff. `TaskError` carries a task that did
|
|
225
|
+
not end done; `PartError` a part that failed.
|
|
226
|
+
|
|
227
|
+
## Tests
|
|
228
|
+
|
|
229
|
+
```sh
|
|
230
|
+
PYTHONPATH=src:tests python -m unittest discover tests
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
The contract in `tests/contract.json` is the Node SDK's own (`sdk/test/contract.json`): both SDKs run it.
|
|
234
|
+
|
|
235
|
+
## License
|
|
236
|
+
|
|
237
|
+
See [LICENSE](LICENSE).
|
spredr-0.1.0/README.md
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# spredr for Python
|
|
2
|
+
|
|
3
|
+
Build an AI agent as an ordinary Python service and run it on [spredr](https://spredr.cloud). Your code says what the
|
|
4
|
+
agent does and calls the model you choose (Claude, OpenAI, Gemini or your own) with your own key. spredr runs it and
|
|
5
|
+
adds what an agent needs to be trusted with real work:
|
|
6
|
+
|
|
7
|
+
- it hands the agent **tasks**, every user taking turns, and keeps a journal so a task that comes back never does a
|
|
8
|
+
step twice;
|
|
9
|
+
- it **holds a risky action** (a refund, an email, a booking) until a person says yes;
|
|
10
|
+
- it keeps a **memory per customer**, and turns your corrections into lessons;
|
|
11
|
+
- it shows **every step**, with its tokens and cost.
|
|
12
|
+
|
|
13
|
+
This is the same SDK as [`@spredr/sdk`](https://www.npmjs.com/package/@spredr/sdk) for Node, with Python's names. Both
|
|
14
|
+
run one shared contract (every call and the exact request it makes to ai), so they do the same thing.
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
pip install spredr
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Python 3.10 or newer. One dependency, `cryptography`, for the ed25519 signatures. The methods are `async`.
|
|
21
|
+
|
|
22
|
+
| You are | You use | You can |
|
|
23
|
+
|---|---|---|
|
|
24
|
+
| **The agent** | `Agent` | take tasks, ask a person before a risky action, remember, learn, report each step |
|
|
25
|
+
| **Your backend** | `Caller` | give the agent tasks, follow them, answer its questions, receive callbacks |
|
|
26
|
+
| **You** | `Spredr` | register agents, link backends, schedule work, read memory, approve lessons, set limits |
|
|
27
|
+
|
|
28
|
+
## The agent
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from anthropic import AsyncAnthropic
|
|
32
|
+
from spredr import Agent
|
|
33
|
+
|
|
34
|
+
claude = AsyncAnthropic()
|
|
35
|
+
agent = Agent(description="Answers the shop's customers", actions=[{"name": "refund"}])
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@agent.on("reply")
|
|
39
|
+
async def reply(input, ctx):
|
|
40
|
+
order = await ctx.track("tool", "orders.find", lambda: find_order(input["order"]))
|
|
41
|
+
if input.get("refund"):
|
|
42
|
+
answer = await ctx.ask("refund", f"Refund {order['total']}?", data={"order": order["id"]})
|
|
43
|
+
if not answer.yes:
|
|
44
|
+
return {"text": "The team will look at it."}
|
|
45
|
+
message = await ctx.model(lambda: claude.messages.create(
|
|
46
|
+
model="claude-haiku-5-5",
|
|
47
|
+
max_tokens=400,
|
|
48
|
+
system="\n".join(ctx.lessons),
|
|
49
|
+
messages=[{"role": "user", "content": input["text"]}],
|
|
50
|
+
))
|
|
51
|
+
return {"text": message.content[0].text}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
agent.run() # listens on $PORT, answers /health, says hello to ai
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`Agent()` reads `SPREDR_AGENT_ID`, `SPREDR_AGENT_KEY`, `SPREDR_AI_URL` and `SPREDR_AI_KEY`, which spredr writes into
|
|
58
|
+
the service when you register it as an agent. What it declares (`description`, `tasks`, `actions`, `tools`,
|
|
59
|
+
`concurrency`, `timeout_seconds`, `judge`, `sleep_after_minutes`) is what its owner sees in the panel.
|
|
60
|
+
|
|
61
|
+
A handler takes `(input, ctx)` and may be sync or async. With `schema=` (a pydantic model, or any function that returns
|
|
62
|
+
the value or raises), the input is checked first, and a task whose input does not match fails saying why:
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
@agent.on("refund", schema=RefundRequest)
|
|
66
|
+
async def refund(request: RefundRequest, ctx): ...
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### `ctx`, what a handler gets
|
|
70
|
+
|
|
71
|
+
What a task already did is never done twice: when it comes back (after a question, after a crash), every `ask`,
|
|
72
|
+
`track`, `model`, `later`, `delegate` and `memory.remember` gives back what it gave the first time, without doing it.
|
|
73
|
+
|
|
74
|
+
| | |
|
|
75
|
+
|---|---|
|
|
76
|
+
| `ctx.task`, `ctx.tenant`, `ctx.thread` | The task, whom it works for (its memory, freedom and caps are that tenant's) and its conversation. |
|
|
77
|
+
| `ctx.lessons` | The lessons you approved. Put them in your prompt. |
|
|
78
|
+
| `ctx.trial` | A trial replays an old task with new lessons: do nothing real. |
|
|
79
|
+
| `await ctx.ask(action, question, options=None, data=None)` | Asks before acting. The task stops here and comes back with the answer: `answer.yes`, `answer.value`, `answer.note`. |
|
|
80
|
+
| `await ctx.track(kind, name, work, usage=None)` | Runs `work` (a function, sync or async), times it and reports it as a step. Put side effects here. |
|
|
81
|
+
| `await ctx.model(work, name=None)` | A model call, reported with its model and tokens (read from Anthropic's and OpenAI's answers). |
|
|
82
|
+
| `await ctx.later(kind, input, at, key=None)` | A task for later (a reminder, a follow-up), for the same tenant and thread. |
|
|
83
|
+
| `await ctx.delegate(agent, kind, input=None, title=None, key=None)` | Hands a part to another of your agents and waits for its result (or a `PartError`). |
|
|
84
|
+
| `await ctx.show(block)` | Shows a block in the task's page: a record, a table, an image, findings, an answer with sources. |
|
|
85
|
+
| `ctx.stream(text)`, `await ctx.flush()` | Pieces of a live answer, for whoever watches the task. |
|
|
86
|
+
| `await ctx.step(kind, name, **fields)`, `await ctx.progress(data)` | Report a step yourself, or say how it is going. |
|
|
87
|
+
| `ctx.files`, `await ctx.file(id_or_name)` | The files its session holds, and one of them as `data`, `type`, `name`. |
|
|
88
|
+
| `await ctx.memory.remember(scope, text, key=None, data=None)` | Keeps a fact for this tenant. `recall(scope=, q=, limit=)` and `forget(fact_id)` too. |
|
|
89
|
+
|
|
90
|
+
Lines logged with `logging` while a task works start with its id (`[tsk_…] …`); `spredr.current_task()` gives it to a
|
|
91
|
+
logger of your own.
|
|
92
|
+
|
|
93
|
+
### Inside a server you already run
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from fastapi import FastAPI
|
|
97
|
+
|
|
98
|
+
app = FastAPI(on_startup=[agent.start], on_shutdown=[agent.close])
|
|
99
|
+
app.mount("/", agent.asgi())
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
With any other framework, route `POST agent.path` to `await agent.handle(method, path, headers, body)` with the raw body
|
|
103
|
+
exactly as it arrived (the signature is over those bytes) and send back the `Answer(status, headers, body)` it returns.
|
|
104
|
+
`agent.close()` lets running tasks finish before it stops (`shutdown_grace`, 25 s by default).
|
|
105
|
+
|
|
106
|
+
### Learning
|
|
107
|
+
|
|
108
|
+
`await agent.feedback(after)` reads what apps said about its tasks; `await agent.propose(text, why, sources)` proposes a
|
|
109
|
+
lesson to its owner.
|
|
110
|
+
|
|
111
|
+
## Your backend
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
from spredr import Caller
|
|
115
|
+
|
|
116
|
+
agent = Caller() # reads the variables spredr writes into a linked service
|
|
117
|
+
shop = agent.for_tenant(shop_id)
|
|
118
|
+
|
|
119
|
+
answer = await shop.result("reply", {"text": text}, thread=f"chat:{customer_id}", key=message_id)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
| | |
|
|
123
|
+
|---|---|
|
|
124
|
+
| `ask(kind, input, key=, wait=, at=, thread=, tenant=, title=)` | Asks for a task. With `key`, asking again is the same task: safe to retry. |
|
|
125
|
+
| `run(...)`, `result(...)` | Asks and waits; `result` gives the result itself, or raises a `TaskError` carrying the task. |
|
|
126
|
+
| `get(id, wait=0)`, `settle(id)`, `list(status=, limit=, before=, thread=, tenant=)` | Read tasks. |
|
|
127
|
+
| `events(id, after, wait)`, `steps(id)` | What happened inside a task; `steps` reads every page. |
|
|
128
|
+
| `stream(id)`, `live(kind, input, ...)` | A task as it happens: `delta`, `event`, and last `end` with the task. |
|
|
129
|
+
| `cancel(id)`, `pauses()`, `answer(pause_id, {"yes": True})`, `feedback(id, "good" \| "bad", correction=, note=)` | Act on tasks. |
|
|
130
|
+
| `attach(thread, name, data, type=)`, `files(thread)`, `detach(thread, name)` | Files a conversation leaves for the agent to read. |
|
|
131
|
+
| `for_tenant(tenant)` | The same calls for one tenant, plus `usage()`, `set_limits(...)`, `forget()`, `freedom()`, `set_freedom()`, `lessons()`, `teach()`, `decide()`. |
|
|
132
|
+
| `callback(method, path, headers, body)` | A callback from ai, checked (needs `SPREDR_AI_KEY`): `event` and `task`, or `None`. |
|
|
133
|
+
|
|
134
|
+
## You
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
from spredr import Spredr
|
|
138
|
+
|
|
139
|
+
owner = Spredr() # SPREDR_TOKEN, an API token from the panel
|
|
140
|
+
shop = owner.agent("shop")
|
|
141
|
+
|
|
142
|
+
await shop.schedule("daily-report", "daily", "0 8 * * *", tz="America/Bogota")
|
|
143
|
+
for pause in await shop.tasks.pauses():
|
|
144
|
+
await shop.tasks.answer(pause.id, {"yes": True})
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
`Spredr`: `agents()`, `register(name, service)`, `keep_token(token)`, `inbox(hours)`, `agent(name)`.
|
|
148
|
+
|
|
149
|
+
`owner.agent(name)`: `info`, `summary`, `threads`, `tenants`, `set_tenant_limits`, `export`, `export_count`,
|
|
150
|
+
`set_limits`, `set_traces`, `traces`, `stop_traces`, `remove`, `rotate_key`, `add_caller`, `callers`, `remove_caller`,
|
|
151
|
+
`schedule`, `hook`, `stored_files`, `download_file`, `delete_file`, `memory_use`, `memory`, `edit_fact`, `forget`,
|
|
152
|
+
`freedom`, `set_freedom`, `lessons`, `write_lesson`, `decide`, `versions`, `release`, `version`, and `.tasks` with
|
|
153
|
+
everything a backend can do.
|
|
154
|
+
|
|
155
|
+
## The same as in Node, the Python way
|
|
156
|
+
|
|
157
|
+
| Node | Python |
|
|
158
|
+
|---|---|
|
|
159
|
+
| `agent.on("reply", fn)` | `@agent.on("reply")` |
|
|
160
|
+
| `await agent.listen()` | `agent.run()` · `agent.asgi()` |
|
|
161
|
+
| `caller.for(tenant)` | `caller.for_tenant(tenant)` |
|
|
162
|
+
| `shop.setLimits({ spendPerDayUsd: 8 })` | `shop.set_limits(spend_per_day_usd=8)` |
|
|
163
|
+
| `tasks.ask(kind, input, { wait: 30 })` | `tasks.ask(kind, input, wait=30)` |
|
|
164
|
+
| `task.createdAt` | `task.created_at` or `task["createdAt"]` |
|
|
165
|
+
| `agent.on(kind, zodSchema, fn)` | `@agent.on(kind, schema=PydanticModel)` |
|
|
166
|
+
| `for await (const e of tasks.stream(id))` | `async for e in tasks.stream(id)` |
|
|
167
|
+
|
|
168
|
+
What ai answers comes back as it sends it: a `dict` you can also read by attribute, in either spelling.
|
|
169
|
+
|
|
170
|
+
## Errors
|
|
171
|
+
|
|
172
|
+
`SpredrError` (with `status` and `trace`) and, by status: `BadRequestError`, `AuthenticationError`,
|
|
173
|
+
`PermissionDeniedError`, `NotFoundError`, `ConflictError`, `RateLimitError` (with `retry_after`),
|
|
174
|
+
`InternalServerError`, `ConnectionError`, `TimeoutError`. A busy ai that says when to come back (429 or 503 with
|
|
175
|
+
Retry-After) is asked again then; what can be sent twice is retried with backoff. `TaskError` carries a task that did
|
|
176
|
+
not end done; `PartError` a part that failed.
|
|
177
|
+
|
|
178
|
+
## Tests
|
|
179
|
+
|
|
180
|
+
```sh
|
|
181
|
+
PYTHONPATH=src:tests python -m unittest discover tests
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
The contract in `tests/contract.json` is the Node SDK's own (`sdk/test/contract.json`): both SDKs run it.
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.25"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "spredr"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Build AI agents as ordinary services and run them on spredr: tasks, questions before risky actions, memory per customer, lessons and traces."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [{ name = "Candhrly, LLC" }]
|
|
13
|
+
keywords = ["ai", "agents", "llm", "spredr", "hosting"]
|
|
14
|
+
# one dependency: ed25519 signatures (Python's standard library has none), in constant time
|
|
15
|
+
dependencies = ["cryptography>=42"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Framework :: AsyncIO",
|
|
19
|
+
"Typing :: Typed",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.urls]
|
|
23
|
+
Homepage = "https://spredr.cloud"
|
|
24
|
+
Documentation = "https://spredr.cloud/docs"
|
|
25
|
+
|
|
26
|
+
[tool.hatch.build.targets.wheel]
|
|
27
|
+
packages = ["src/spredr"]
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""spredr: build an AI agent as an ordinary service and run it on spredr.
|
|
2
|
+
|
|
3
|
+
Agent inside the agent: take tasks, ask before risky actions, remember, report each step
|
|
4
|
+
Caller in your backend: give the agent tasks, follow them, answer its questions
|
|
5
|
+
Spredr you, with your token: register agents, link backends, schedule work, set limits
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from ._json import Record
|
|
9
|
+
from ._sign import ai_verifier, new_keys
|
|
10
|
+
from ._tag import current_task
|
|
11
|
+
from .agent import Agent, Answer
|
|
12
|
+
from .caller import Caller
|
|
13
|
+
from .context import Context, Memory
|
|
14
|
+
from .errors import (
|
|
15
|
+
AuthenticationError,
|
|
16
|
+
BadRequestError,
|
|
17
|
+
ConflictError,
|
|
18
|
+
ConnectionError,
|
|
19
|
+
InternalServerError,
|
|
20
|
+
NotFoundError,
|
|
21
|
+
PartError,
|
|
22
|
+
PermissionDeniedError,
|
|
23
|
+
RateLimitError,
|
|
24
|
+
SpredrError,
|
|
25
|
+
TaskError,
|
|
26
|
+
TimeoutError,
|
|
27
|
+
)
|
|
28
|
+
from .owner import DEFAULT_AI_URL, OwnedAgent, Spredr
|
|
29
|
+
from .tasks import Tasks, Tenant
|
|
30
|
+
|
|
31
|
+
__version__ = "0.1.0"
|
|
32
|
+
__all__ = [
|
|
33
|
+
"Agent", "Answer", "AuthenticationError", "BadRequestError", "Caller", "ConflictError", "ConnectionError", "Context", "DEFAULT_AI_URL",
|
|
34
|
+
"InternalServerError", "Memory", "NotFoundError", "OwnedAgent", "PartError", "PermissionDeniedError", "RateLimitError", "Record",
|
|
35
|
+
"Spredr", "SpredrError", "TaskError", "Tasks", "Tenant", "TimeoutError", "ai_verifier", "current_task", "new_keys",
|
|
36
|
+
]
|