podstack 1.8.0__tar.gz → 1.9.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.
- {podstack-1.8.0 → podstack-1.9.0}/AGENTS.md +7 -3
- {podstack-1.8.0 → podstack-1.9.0}/PKG-INFO +14 -8
- {podstack-1.8.0 → podstack-1.9.0}/README.md +12 -7
- {podstack-1.8.0 → podstack-1.9.0}/docs/audit.md +1 -1
- podstack-1.9.0/docs/functions.md +117 -0
- {podstack-1.8.0 → podstack-1.9.0}/examples/02_functions.py +4 -4
- {podstack-1.8.0 → podstack-1.9.0}/pyproject.toml +2 -1
- {podstack-1.8.0 → podstack-1.9.0}/src/podstack/_version.py +1 -1
- {podstack-1.8.0 → podstack-1.9.0}/src/podstack/async_client.py +119 -26
- {podstack-1.8.0 → podstack-1.9.0}/src/podstack/client.py +142 -44
- {podstack-1.8.0 → podstack-1.9.0}/src/podstack/config.py +13 -9
- {podstack-1.8.0 → podstack-1.9.0}/src/podstack/exceptions.py +2 -2
- podstack-1.9.0/src/podstack/function_codec.py +178 -0
- {podstack-1.8.0 → podstack-1.9.0}/src/podstack/functions.py +87 -44
- {podstack-1.8.0 → podstack-1.9.0}/src/podstack/mcp.py +2 -1
- {podstack-1.8.0 → podstack-1.9.0}/src/podstack/models.py +66 -18
- {podstack-1.8.0 → podstack-1.9.0}/src/podstack/transport.py +112 -4
- {podstack-1.8.0 → podstack-1.9.0}/src/podstack.egg-info/SOURCES.txt +1 -0
- {podstack-1.8.0 → podstack-1.9.0}/tests/test_client.py +93 -3
- {podstack-1.8.0 → podstack-1.9.0}/tests/test_config_and_errors.py +10 -4
- podstack-1.9.0/tests/test_functions.py +378 -0
- podstack-1.8.0/docs/functions.md +0 -65
- podstack-1.8.0/tests/test_functions.py +0 -132
- {podstack-1.8.0 → podstack-1.9.0}/LICENSE +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/MANIFEST.in +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/examples/01_quickstart.py +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/examples/03_instances.py +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/examples/04_jobs.py +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/examples/05_endpoints.py +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/examples/06_inference.py +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/examples/07_balance.py +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/examples/08_tools.py +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/examples/09_mcp.py +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/examples/README.md +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/llms.txt +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/setup.cfg +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/skills/podstack/SKILL.md +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/src/podstack/__init__.py +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/src/podstack/py.typed +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/src/podstack/ssh.py +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/src/podstack/tools.py +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/tests/conftest.py +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/tests/test_ssh_async.py +0 -0
- {podstack-1.8.0 → podstack-1.9.0}/tests/test_tools_mcp.py +0 -0
|
@@ -8,7 +8,7 @@ Use this package to call `https://podstack.ai/api/v1`. Do not use
|
|
|
8
8
|
`Podstack()` reads the key in this order: the `api_key` argument,
|
|
9
9
|
`PODSTACK_API_KEY`, then `~/.config/podstack/credentials.json`
|
|
10
10
|
(`XDG_CONFIG_HOME` is honored). The same file is what the Podstack CLI writes.
|
|
11
|
-
|
|
11
|
+
The server accepts `ps_live_` keys. A `psk_` string is still redacted if it appears in an error, and the API does not accept it. Do not print keys.
|
|
12
12
|
`repr` redacts them, and the MCP server redacts them in tool output.
|
|
13
13
|
|
|
14
14
|
Override the base with `base_url=` or `PODSTACK_API_BASE`.
|
|
@@ -36,7 +36,9 @@ GPU types, VRAM, regions, and prices come from `gpus.list()`, `rates.list()`,
|
|
|
36
36
|
`NotAvailableYet`.
|
|
37
37
|
|
|
38
38
|
`NotAvailableYet` is for a route the platform does not implement: function
|
|
39
|
-
logs
|
|
39
|
+
logs when `GET /functions/{id}/logs` returns 404 or 405, and wallet when the
|
|
40
|
+
route is 404. A 404 from `GET /functions/{id}/runs/{run_id}` is a missing run
|
|
41
|
+
(`NotFoundError`).
|
|
40
42
|
|
|
41
43
|
There is no instance stop or start, no instance exec HTTP API, and no dataset
|
|
42
44
|
upload. `exec`, `upload`, and `download` shell out to OpenSSH using the SSH
|
|
@@ -46,7 +48,9 @@ command on the instance. Fine-tuning takes an existing `training_file` id.
|
|
|
46
48
|
|
|
47
49
|
Create functions with `@podstack.function`, not with the CLI and not with an
|
|
48
50
|
HTTP-only helper that uploads a notebook. The contract is `docs/functions.md`.
|
|
49
|
-
|
|
51
|
+
`deploy` registers the name, uploads a cloudpickle version, and activates it.
|
|
52
|
+
Invoke uses `POST /functions/{id}/runs`. The function must be a top-level
|
|
53
|
+
function in a file. Arguments must be JSON.
|
|
50
54
|
|
|
51
55
|
## Tools and MCP
|
|
52
56
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: podstack
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.9.0
|
|
4
4
|
Summary: Python SDK for the Podstack platform API
|
|
5
5
|
Author-email: Podstack <support@podstack.ai>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -21,6 +21,7 @@ Classifier: Typing :: Typed
|
|
|
21
21
|
Requires-Python: >=3.9
|
|
22
22
|
Description-Content-Type: text/markdown
|
|
23
23
|
License-File: LICENSE
|
|
24
|
+
Requires-Dist: cloudpickle<4,>=3.0
|
|
24
25
|
Requires-Dist: httpx>=0.24
|
|
25
26
|
Provides-Extra: dev
|
|
26
27
|
Requires-Dist: build>=1.2; extra == "dev"
|
|
@@ -40,8 +41,9 @@ pip install podstack
|
|
|
40
41
|
```
|
|
41
42
|
|
|
42
43
|
The client talks to `https://podstack.ai/api/v1`. Set `PODSTACK_API_KEY` to a
|
|
43
|
-
`ps_live_` key
|
|
44
|
-
|
|
44
|
+
`ps_live_` key, or run `podstack auth login` so the CLI credentials file is
|
|
45
|
+
present. The API does not accept a `psk_` key. `api.podstack.ai` does not resolve.
|
|
46
|
+
Public reads such as `client.job_gpus.list()` work with no key.
|
|
45
47
|
|
|
46
48
|
```python
|
|
47
49
|
from podstack import Podstack
|
|
@@ -78,7 +80,7 @@ Creating a function is this package. The CLI only lists functions.
|
|
|
78
80
|
```python
|
|
79
81
|
import podstack
|
|
80
82
|
|
|
81
|
-
@podstack.function(gpu="
|
|
83
|
+
@podstack.function(gpu="a100_80gb", requirements=["torch"])
|
|
82
84
|
def device_name():
|
|
83
85
|
import torch
|
|
84
86
|
return torch.cuda.get_device_name(0)
|
|
@@ -100,11 +102,15 @@ and they report that the platform does not keep a stopped machine.
|
|
|
100
102
|
|
|
101
103
|
See `examples/` and `AGENTS.md`.
|
|
102
104
|
|
|
103
|
-
## Version 1.
|
|
105
|
+
## Version 1.9.0
|
|
104
106
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
107
|
+
`deploy` registers a function by name, uploads a cloudpickle version, and
|
|
108
|
+
activates it. `invoke` starts `POST /functions/{id}/runs` and unpickles the
|
|
109
|
+
result. See `CHANGELOG.md`.
|
|
110
|
+
|
|
111
|
+
1.8.0 removed the previous notebook SDK. There are no deprecated shims for
|
|
112
|
+
routes the platform does not serve. The removed modules are listed in
|
|
113
|
+
`docs/audit.md`.
|
|
108
114
|
|
|
109
115
|
Async code uses `AsyncPodstack`. The method names match the sync client.
|
|
110
116
|
|
|
@@ -7,8 +7,9 @@ pip install podstack
|
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
The client talks to `https://podstack.ai/api/v1`. Set `PODSTACK_API_KEY` to a
|
|
10
|
-
`ps_live_` key
|
|
11
|
-
|
|
10
|
+
`ps_live_` key, or run `podstack auth login` so the CLI credentials file is
|
|
11
|
+
present. The API does not accept a `psk_` key. `api.podstack.ai` does not resolve.
|
|
12
|
+
Public reads such as `client.job_gpus.list()` work with no key.
|
|
12
13
|
|
|
13
14
|
```python
|
|
14
15
|
from podstack import Podstack
|
|
@@ -45,7 +46,7 @@ Creating a function is this package. The CLI only lists functions.
|
|
|
45
46
|
```python
|
|
46
47
|
import podstack
|
|
47
48
|
|
|
48
|
-
@podstack.function(gpu="
|
|
49
|
+
@podstack.function(gpu="a100_80gb", requirements=["torch"])
|
|
49
50
|
def device_name():
|
|
50
51
|
import torch
|
|
51
52
|
return torch.cuda.get_device_name(0)
|
|
@@ -67,11 +68,15 @@ and they report that the platform does not keep a stopped machine.
|
|
|
67
68
|
|
|
68
69
|
See `examples/` and `AGENTS.md`.
|
|
69
70
|
|
|
70
|
-
## Version 1.
|
|
71
|
+
## Version 1.9.0
|
|
71
72
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
`deploy` registers a function by name, uploads a cloudpickle version, and
|
|
74
|
+
activates it. `invoke` starts `POST /functions/{id}/runs` and unpickles the
|
|
75
|
+
result. See `CHANGELOG.md`.
|
|
76
|
+
|
|
77
|
+
1.8.0 removed the previous notebook SDK. There are no deprecated shims for
|
|
78
|
+
routes the platform does not serve. The removed modules are listed in
|
|
79
|
+
`docs/audit.md`.
|
|
75
80
|
|
|
76
81
|
Async code uses `AsyncPodstack`. The method names match the sync client.
|
|
77
82
|
|
|
@@ -49,6 +49,6 @@ method and then fails.
|
|
|
49
49
|
These are explicit errors, not silent success:
|
|
50
50
|
|
|
51
51
|
- `functions.logs` when `GET /functions/{id}/logs` returns 404 or 405.
|
|
52
|
-
- `functions.call`
|
|
52
|
+
- `functions.call` reads `GET /functions/{id}/runs/{run_id}`. A 404 is `NotFoundError` (the run is missing).
|
|
53
53
|
- `wallet.balance` and `wallet.transactions` when those routes return 404.
|
|
54
54
|
- MCP `stop_instance` and `start_instance`. They do not send HTTP.
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# Functions API
|
|
2
|
+
|
|
3
|
+
The CLI lists functions with `GET /functions`. This package creates one,
|
|
4
|
+
uploads a cloudpickle version, and activates it. The first confirmed
|
|
5
|
+
`invoke` deploys when the function has not been deployed yet, then starts a
|
|
6
|
+
run.
|
|
7
|
+
|
|
8
|
+
`gpu` is an id from `GET /gpu-types` or `GET /jobs/gpu-types`. Job runs use a
|
|
9
|
+
job GPU type such as `a100_80gb` or `l40s`. The SDK does not check the id
|
|
10
|
+
against a built-in list. `image` may be empty. `requirements` is a list of
|
|
11
|
+
pip specifiers, or the decorator accepts a path to `requirements.txt`.
|
|
12
|
+
|
|
13
|
+
The name starts with a letter and uses letters, numbers, hyphens, and
|
|
14
|
+
underscores, at most 51 characters. The function must be a top-level function
|
|
15
|
+
in a file. Arguments must be JSON.
|
|
16
|
+
|
|
17
|
+
## Deploy
|
|
18
|
+
|
|
19
|
+
Deploy is three calls. A second deploy repeats them, so a code change is
|
|
20
|
+
shipped. An identical bundle can come back without an upload URL; the SDK
|
|
21
|
+
still activates that version.
|
|
22
|
+
|
|
23
|
+
`POST /functions`
|
|
24
|
+
|
|
25
|
+
```json
|
|
26
|
+
{"name": "device_name"}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`POST /functions/{id}/versions`
|
|
30
|
+
|
|
31
|
+
```json
|
|
32
|
+
{
|
|
33
|
+
"gpu": "a100_80gb",
|
|
34
|
+
"pip": ["torch"],
|
|
35
|
+
"image": "custom:latest",
|
|
36
|
+
"python_version": "3.11",
|
|
37
|
+
"cloudpickle_version": "3.1.1",
|
|
38
|
+
"timeout": 3600,
|
|
39
|
+
"bundle_bytes": 120,
|
|
40
|
+
"bundle_sha256": "...",
|
|
41
|
+
"content_hash": "...",
|
|
42
|
+
"gpu_count": 1,
|
|
43
|
+
"min_vram_gb": 0,
|
|
44
|
+
"regions": [],
|
|
45
|
+
"idle_timeout": 300,
|
|
46
|
+
"max_concurrency": 1
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`pip` is the requirements list. `image` is sent only when it is non-empty.
|
|
51
|
+
`content_hash` is the SHA-256 of that JSON with the keys sorted, computed
|
|
52
|
+
before `content_hash` itself is added.
|
|
53
|
+
|
|
54
|
+
When the response includes `upload.url`, the SDK `PUT`s the raw cloudpickle
|
|
55
|
+
bytes to that URL with `Content-Type: application/octet-stream` and no API
|
|
56
|
+
key. It then confirms the version:
|
|
57
|
+
|
|
58
|
+
`POST /functions/{id}/versions/{version_id}/activate`
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`deploy(dry_run=True)` returns a `DryRun` for the version route and does not
|
|
65
|
+
pickle, build a client, or send HTTP.
|
|
66
|
+
|
|
67
|
+
## Invoke
|
|
68
|
+
|
|
69
|
+
`POST /functions/{id}/runs`
|
|
70
|
+
|
|
71
|
+
```json
|
|
72
|
+
{"input": "<base64 cloudpickle of (args, kwargs)>", "version_id": "ver_..."}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
`version_id` is omitted until a deploy has stored one. `args` and `kwargs`
|
|
76
|
+
must be JSON. The call body is a pickle, limited to 4 MB before base64.
|
|
77
|
+
|
|
78
|
+
`wait=true` (the default) polls `GET /functions/{id}/runs/{run_id}?wait=25&include_result=true`
|
|
79
|
+
until `status` is `succeeded`, `failed`, `timed_out`, or `cancelled`, or the
|
|
80
|
+
function timeout elapses. `wait=false` returns the accepted run. Poll later
|
|
81
|
+
with `functions.call`, which reads the same run route. A 404 on that route is
|
|
82
|
+
`NotFoundError`.
|
|
83
|
+
|
|
84
|
+
The result object is `{result, status, call_id, error}`. `call_id` is the run
|
|
85
|
+
id. A base64 cloudpickle `result` is unpickled. A JSON value is left as-is.
|
|
86
|
+
`error` may be a string or `{"message", "type", "traceback"}`; the message is
|
|
87
|
+
kept.
|
|
88
|
+
|
|
89
|
+
Name or id both work in the path. After deploy, the decorator uses the id.
|
|
90
|
+
|
|
91
|
+
## Logs, get, and delete
|
|
92
|
+
|
|
93
|
+
`GET /functions/{id}` reads one function. `GET /functions/{id}/logs` returns
|
|
94
|
+
logs as text. A 404 or 405 raises `NotAvailableYet`. `DELETE /functions/{id}`
|
|
95
|
+
removes the function and requires `confirm=True`. Pass the name or the id.
|
|
96
|
+
|
|
97
|
+
## Decorator
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
import podstack
|
|
101
|
+
|
|
102
|
+
@podstack.function(gpu="a100_80gb", requirements=["torch"])
|
|
103
|
+
def device_name():
|
|
104
|
+
import torch
|
|
105
|
+
return torch.cuda.get_device_name(0)
|
|
106
|
+
|
|
107
|
+
device_name.local() # this process, not billed
|
|
108
|
+
device_name.deploy(dry_run=True) # preview, not sent
|
|
109
|
+
device_name.deploy(confirm=True) # create, upload, activate
|
|
110
|
+
device_name.invoke(confirm=True) # deploy if needed, then run
|
|
111
|
+
device_name.get()
|
|
112
|
+
device_name.logs()
|
|
113
|
+
await device_name.ainvoke(confirm=True)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
`device_name()` raises `ConfirmationRequired` unless the decorator was built
|
|
117
|
+
with `confirm=True`.
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"""Deploy and invoke a GPU function.
|
|
2
2
|
|
|
3
|
-
The decorator
|
|
4
|
-
The first invoke deploys, then
|
|
3
|
+
The decorator registers the function, uploads a cloudpickle version, and
|
|
4
|
+
activates it. The first invoke deploys, then starts POST /functions/{id}/runs.
|
|
5
5
|
|
|
6
6
|
PODSTACK_API_KEY=ps_live_... python examples/02_functions.py
|
|
7
7
|
PODSTACK_API_KEY=ps_live_... PODSTACK_CONFIRM=1 python examples/02_functions.py
|
|
8
8
|
|
|
9
9
|
PODSTACK_CONFIRM=1 sends confirm=True. Without it, the script prints a dry run.
|
|
10
|
-
Pick PODSTACK_GPU from client.
|
|
10
|
+
Pick PODSTACK_GPU from client.job_gpus.list(). The default below is only a placeholder.
|
|
11
11
|
"""
|
|
12
12
|
|
|
13
13
|
import asyncio
|
|
@@ -15,7 +15,7 @@ import os
|
|
|
15
15
|
|
|
16
16
|
import podstack
|
|
17
17
|
|
|
18
|
-
GPU = os.environ.get("PODSTACK_GPU", "
|
|
18
|
+
GPU = os.environ.get("PODSTACK_GPU", "a100_80gb")
|
|
19
19
|
SEND = os.environ.get("PODSTACK_CONFIRM") == "1"
|
|
20
20
|
|
|
21
21
|
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "podstack"
|
|
7
|
-
version = "1.
|
|
7
|
+
version = "1.9.0"
|
|
8
8
|
description = "Python SDK for the Podstack platform API"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "MIT"
|
|
@@ -27,6 +27,7 @@ classifiers = [
|
|
|
27
27
|
"Typing :: Typed",
|
|
28
28
|
]
|
|
29
29
|
dependencies = [
|
|
30
|
+
"cloudpickle>=3.0,<4",
|
|
30
31
|
"httpx>=0.24",
|
|
31
32
|
]
|
|
32
33
|
|
|
@@ -8,6 +8,7 @@ so they do not block the event loop. Billable methods still require
|
|
|
8
8
|
from __future__ import annotations
|
|
9
9
|
|
|
10
10
|
import asyncio
|
|
11
|
+
import time
|
|
11
12
|
from typing import Any, AsyncIterator
|
|
12
13
|
from urllib.parse import quote
|
|
13
14
|
|
|
@@ -24,6 +25,18 @@ from .client import (
|
|
|
24
25
|
from .client import _omit as omit
|
|
25
26
|
from .config import resolve_config
|
|
26
27
|
from .exceptions import APIError, NotAvailableYet, NotFoundError, ValidationError
|
|
28
|
+
from .function_codec import (
|
|
29
|
+
bundle_function,
|
|
30
|
+
callable_for_deploy,
|
|
31
|
+
check_function_name,
|
|
32
|
+
deploy_preview,
|
|
33
|
+
encode_call,
|
|
34
|
+
poll_wait_seconds,
|
|
35
|
+
run_finished,
|
|
36
|
+
run_id,
|
|
37
|
+
version_spec,
|
|
38
|
+
version_upload,
|
|
39
|
+
)
|
|
27
40
|
from .models import (
|
|
28
41
|
ChatChunk,
|
|
29
42
|
ChatCompletion,
|
|
@@ -467,42 +480,94 @@ class AsyncFunctions:
|
|
|
467
480
|
self,
|
|
468
481
|
*,
|
|
469
482
|
name: str,
|
|
470
|
-
source: str,
|
|
471
|
-
entrypoint: str,
|
|
472
483
|
gpu: str,
|
|
473
484
|
image: str = "",
|
|
474
485
|
requirements: _StringList | None = None,
|
|
475
486
|
timeout_seconds: int = 3600,
|
|
476
487
|
confirm: bool = False,
|
|
477
488
|
dry_run: bool = False,
|
|
489
|
+
func: Any = None,
|
|
490
|
+
source: str = "",
|
|
491
|
+
entrypoint: str = "",
|
|
478
492
|
) -> CloudFunction | DryRun:
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
"gpu"
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
"source": source,
|
|
485
|
-
"entrypoint": entrypoint,
|
|
486
|
-
"timeout_seconds": timeout_seconds,
|
|
487
|
-
}
|
|
493
|
+
check_function_name(name)
|
|
494
|
+
if not gpu or not str(gpu).strip():
|
|
495
|
+
raise ValidationError("gpu is required", field="gpu")
|
|
496
|
+
specs = list(requirements or [])
|
|
497
|
+
path, body = deploy_preview(name, gpu, image, specs, timeout_seconds)
|
|
488
498
|
preview = gate(
|
|
489
499
|
"deploy function",
|
|
490
500
|
method="POST",
|
|
491
|
-
path=
|
|
501
|
+
path=path,
|
|
492
502
|
body=body,
|
|
493
503
|
confirm=confirm,
|
|
494
504
|
dry_run=dry_run,
|
|
495
505
|
)
|
|
496
506
|
if preview is not None:
|
|
497
507
|
return preview
|
|
498
|
-
|
|
508
|
+
target = callable_for_deploy(func, source, entrypoint)
|
|
509
|
+
bundle = bundle_function(target)
|
|
510
|
+
fn = await self._create_function(name)
|
|
511
|
+
spec = version_spec(
|
|
512
|
+
bundle,
|
|
513
|
+
gpu=gpu,
|
|
514
|
+
image=image,
|
|
515
|
+
requirements=specs,
|
|
516
|
+
timeout=timeout_seconds,
|
|
517
|
+
)
|
|
518
|
+
created = await self._t.request(
|
|
519
|
+
"POST",
|
|
520
|
+
f"/functions/{quote(fn.id, safe='')}/versions",
|
|
521
|
+
json_body=spec,
|
|
522
|
+
)
|
|
523
|
+
version = created if isinstance(created, dict) else {}
|
|
524
|
+
version_id, upload_url, upload_method = version_upload(version)
|
|
525
|
+
if not version_id:
|
|
526
|
+
raise APIError(
|
|
527
|
+
"The function version response did not include an id",
|
|
528
|
+
status_code=200,
|
|
529
|
+
code="invalid_response",
|
|
530
|
+
)
|
|
531
|
+
if upload_url:
|
|
532
|
+
await self._t.upload(upload_url, bundle, method=upload_method)
|
|
533
|
+
await self._t.request(
|
|
534
|
+
"POST",
|
|
535
|
+
f"/functions/{quote(fn.id, safe='')}/versions/{quote(version_id, safe='')}/activate",
|
|
536
|
+
json_body={},
|
|
537
|
+
)
|
|
538
|
+
fn.current_version_id = version_id
|
|
539
|
+
if not fn.gpu:
|
|
540
|
+
fn.gpu = gpu
|
|
541
|
+
return fn
|
|
542
|
+
|
|
543
|
+
async def _create_function(self, name: str) -> CloudFunction:
|
|
544
|
+
try:
|
|
545
|
+
raw = await self._t.request("POST", "/functions", json_body={"name": name})
|
|
546
|
+
except APIError as exc:
|
|
547
|
+
if exc.status_code != 409:
|
|
548
|
+
raise
|
|
549
|
+
return await self._find_function(name)
|
|
499
550
|
fn = CloudFunction.from_dict(raw if isinstance(raw, dict) else {})
|
|
500
551
|
if not fn.name:
|
|
501
552
|
fn.name = name
|
|
502
|
-
if not fn.
|
|
503
|
-
|
|
553
|
+
if not fn.id:
|
|
554
|
+
return await self._find_function(name)
|
|
504
555
|
return fn
|
|
505
556
|
|
|
557
|
+
async def _find_function(self, name: str) -> CloudFunction:
|
|
558
|
+
try:
|
|
559
|
+
found = await self.get(name)
|
|
560
|
+
except NotFoundError:
|
|
561
|
+
found = None
|
|
562
|
+
if found is not None and found.id:
|
|
563
|
+
if not found.name:
|
|
564
|
+
found.name = name
|
|
565
|
+
return found
|
|
566
|
+
for row in await self.list():
|
|
567
|
+
if row.name == name and row.id:
|
|
568
|
+
return row
|
|
569
|
+
raise NotFoundError(f"Function {name} was not created")
|
|
570
|
+
|
|
506
571
|
async def invoke(
|
|
507
572
|
self,
|
|
508
573
|
name: str,
|
|
@@ -512,9 +577,13 @@ class AsyncFunctions:
|
|
|
512
577
|
wait: bool = True,
|
|
513
578
|
confirm: bool = False,
|
|
514
579
|
dry_run: bool = False,
|
|
580
|
+
version_id: str = "",
|
|
581
|
+
timeout_seconds: int = 3600,
|
|
515
582
|
) -> FunctionResult | DryRun:
|
|
516
|
-
path = f"/functions/{quote(name, safe='')}/
|
|
517
|
-
body = {"
|
|
583
|
+
path = f"/functions/{quote(name, safe='')}/runs"
|
|
584
|
+
body: dict[str, Any] = {"input": encode_call(list(args or []), dict(kwargs or {}))}
|
|
585
|
+
if version_id:
|
|
586
|
+
body["version_id"] = version_id
|
|
518
587
|
preview = gate(
|
|
519
588
|
"invoke function",
|
|
520
589
|
method="POST",
|
|
@@ -526,17 +595,41 @@ class AsyncFunctions:
|
|
|
526
595
|
if preview is not None:
|
|
527
596
|
return preview
|
|
528
597
|
raw = await self._t.request("POST", path, json_body=body)
|
|
529
|
-
|
|
598
|
+
run = raw if isinstance(raw, dict) else {}
|
|
599
|
+
if not wait:
|
|
600
|
+
return FunctionResult.from_dict(run)
|
|
601
|
+
return await self._wait_for_run(name, run, timeout_seconds)
|
|
602
|
+
|
|
603
|
+
async def _wait_for_run(self, name: str, run: dict[str, Any], timeout_seconds: int) -> FunctionResult:
|
|
604
|
+
if run_finished(run) or not run_id(run):
|
|
605
|
+
return FunctionResult.from_dict(run)
|
|
606
|
+
call_id = run_id(run)
|
|
607
|
+
deadline = time.monotonic() + max(int(timeout_seconds or 1), 1)
|
|
608
|
+
latest = run
|
|
609
|
+
while not run_finished(latest):
|
|
610
|
+
remaining = deadline - time.monotonic()
|
|
611
|
+
wait_for = poll_wait_seconds(remaining)
|
|
612
|
+
if wait_for <= 0:
|
|
613
|
+
break
|
|
614
|
+
started = time.monotonic()
|
|
615
|
+
polled = await self._t.request(
|
|
616
|
+
"GET",
|
|
617
|
+
f"/functions/{quote(name, safe='')}/runs/{quote(call_id, safe='')}",
|
|
618
|
+
query={"wait": wait_for, "include_result": "true"},
|
|
619
|
+
)
|
|
620
|
+
if isinstance(polled, dict):
|
|
621
|
+
latest = polled
|
|
622
|
+
if run_finished(latest):
|
|
623
|
+
break
|
|
624
|
+
if time.monotonic() - started < 0.25:
|
|
625
|
+
pause = min(1.0, max(0.0, deadline - time.monotonic()))
|
|
626
|
+
if pause > 0:
|
|
627
|
+
await asyncio.sleep(pause)
|
|
628
|
+
return FunctionResult.from_dict(latest)
|
|
530
629
|
|
|
531
630
|
async def call(self, name: str, call_id: str) -> FunctionResult:
|
|
532
|
-
path = f"/functions/{quote(name, safe='')}/
|
|
533
|
-
|
|
534
|
-
raw = await self._t.request("GET", path)
|
|
535
|
-
except NotFoundError as exc:
|
|
536
|
-
raise NotAvailableYet(
|
|
537
|
-
"async function calls",
|
|
538
|
-
"GET /functions/{name}/calls/{call_id} is not on this API",
|
|
539
|
-
) from exc
|
|
631
|
+
path = f"/functions/{quote(name, safe='')}/runs/{quote(call_id, safe='')}"
|
|
632
|
+
raw = await self._t.request("GET", path, query={"include_result": "true"})
|
|
540
633
|
return FunctionResult.from_dict(raw if isinstance(raw, dict) else {})
|
|
541
634
|
|
|
542
635
|
async def logs(self, function_id: str) -> str:
|