desp-sdk 0.5.7__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.
- desp_sdk-0.5.7/.gitignore +32 -0
- desp_sdk-0.5.7/LICENSE +202 -0
- desp_sdk-0.5.7/PKG-INFO +40 -0
- desp_sdk-0.5.7/README.md +18 -0
- desp_sdk-0.5.7/pyproject.toml +62 -0
- desp_sdk-0.5.7/src/_desp_sdk_usage_boot.py +205 -0
- desp_sdk-0.5.7/src/desp_sdk/__init__.py +57 -0
- desp_sdk-0.5.7/src/desp_sdk/_config.py +43 -0
- desp_sdk-0.5.7/src/desp_sdk/_errors.py +14 -0
- desp_sdk-0.5.7/src/desp_sdk/_http.py +277 -0
- desp_sdk-0.5.7/src/desp_sdk/client.py +203 -0
- desp_sdk-0.5.7/src/desp_sdk/health.py +23 -0
- desp_sdk-0.5.7/src/desp_sdk/langgraph_adapter.py +61 -0
- desp_sdk-0.5.7/src/desp_sdk/models.py +60 -0
- desp_sdk-0.5.7/src/desp_sdk/py.typed +0 -0
- desp_sdk-0.5.7/src/desp_sdk/serialization.py +23 -0
- desp_sdk-0.5.7/src/desp_sdk/trigger_plans.py +84 -0
- desp_sdk-0.5.7/src/desp_sdk/usage/__init__.py +11 -0
- desp_sdk-0.5.7/src/desp_sdk/usage/_activate.py +64 -0
- desp_sdk-0.5.7/src/desp_sdk/usage/_decoder.py +135 -0
- desp_sdk-0.5.7/src/desp_sdk/usage/_httpx_patch.py +522 -0
- desp_sdk-0.5.7/src/desp_sdk/usage/_log.py +26 -0
- desp_sdk-0.5.7/src/desp_sdk/usage/_models.py +73 -0
- desp_sdk-0.5.7/src/desp_sdk/usage/_parsers.py +537 -0
- desp_sdk-0.5.7/src/desp_sdk/usage/_recorder.py +385 -0
- desp_sdk-0.5.7/src/desp_sdk_usage.pth +1 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Byte-compiled / optimized files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# Distribution / packaging
|
|
7
|
+
build/
|
|
8
|
+
dist/
|
|
9
|
+
*.egg-info/
|
|
10
|
+
|
|
11
|
+
# Unit test / coverage reports
|
|
12
|
+
.pytest_cache/
|
|
13
|
+
.coverage
|
|
14
|
+
.coverage.*
|
|
15
|
+
htmlcov/
|
|
16
|
+
|
|
17
|
+
# Environments
|
|
18
|
+
.venv/
|
|
19
|
+
.env
|
|
20
|
+
|
|
21
|
+
# Editors
|
|
22
|
+
.vscode/
|
|
23
|
+
.idea/
|
|
24
|
+
|
|
25
|
+
# Mac files
|
|
26
|
+
.DS_Store
|
|
27
|
+
|
|
28
|
+
# Claude files
|
|
29
|
+
CLAUDE.md
|
|
30
|
+
|
|
31
|
+
# Documentation files
|
|
32
|
+
docs/
|
desp_sdk-0.5.7/LICENSE
ADDED
|
@@ -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.
|
desp_sdk-0.5.7/PKG-INFO
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: desp-sdk
|
|
3
|
+
Version: 0.5.7
|
|
4
|
+
Summary: Python SDK for reporting Deltas to DESP and requesting Trigger Plans.
|
|
5
|
+
Project-URL: Documentation, https://documentation.aibydna.com/desp/introduction
|
|
6
|
+
Author-email: AI by DNA <contact@aibydna.com>
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: agents,deltas,desp,sdk,trigger-plans
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Framework :: AsyncIO
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Python: >=3.12
|
|
20
|
+
Requires-Dist: httpx>=0.27
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# desp-sdk
|
|
24
|
+
|
|
25
|
+
Client SDK for the connected-system side of the DESP API. It covers the routes
|
|
26
|
+
a connected system can reach: reporting Deltas (`POST /v1/connected/deltas`)
|
|
27
|
+
and requesting Trigger Plans (`POST /v1/connected/trigger-plans`). Managing
|
|
28
|
+
environments, systems, contracts and API keys is not part of this SDK.
|
|
29
|
+
|
|
30
|
+
## Install
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install desp-sdk # or: uv add desp-sdk
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Requires Python 3.12+. Import it as `desp_sdk`.
|
|
37
|
+
|
|
38
|
+
### Documentation
|
|
39
|
+
|
|
40
|
+
See the [DESP Reference](https://documentation.aibydna.com/desp/introduction) for more details.
|
desp_sdk-0.5.7/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# desp-sdk
|
|
2
|
+
|
|
3
|
+
Client SDK for the connected-system side of the DESP API. It covers the routes
|
|
4
|
+
a connected system can reach: reporting Deltas (`POST /v1/connected/deltas`)
|
|
5
|
+
and requesting Trigger Plans (`POST /v1/connected/trigger-plans`). Managing
|
|
6
|
+
environments, systems, contracts and API keys is not part of this SDK.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pip install desp-sdk # or: uv add desp-sdk
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Requires Python 3.12+. Import it as `desp_sdk`.
|
|
15
|
+
|
|
16
|
+
### Documentation
|
|
17
|
+
|
|
18
|
+
See the [DESP Reference](https://documentation.aibydna.com/desp/introduction) for more details.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "desp-sdk"
|
|
3
|
+
version = "0.5.7"
|
|
4
|
+
readme = "README.md"
|
|
5
|
+
description = "Python SDK for reporting Deltas to DESP and requesting Trigger Plans."
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
license = "Apache-2.0"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
authors = [{ name = "AI by DNA", email = "contact@aibydna.com" }]
|
|
10
|
+
keywords = ["desp", "sdk", "deltas", "trigger-plans", "agents"]
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 4 - Beta",
|
|
13
|
+
"Intended Audience :: Developers",
|
|
14
|
+
"Operating System :: OS Independent",
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
17
|
+
"Programming Language :: Python :: 3.12",
|
|
18
|
+
"Programming Language :: Python :: 3.13",
|
|
19
|
+
"Framework :: AsyncIO",
|
|
20
|
+
"Typing :: Typed",
|
|
21
|
+
]
|
|
22
|
+
dependencies = [
|
|
23
|
+
"httpx>=0.27",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
Documentation = "https://documentation.aibydna.com/desp/introduction"
|
|
28
|
+
|
|
29
|
+
[build-system]
|
|
30
|
+
requires = ["hatchling"]
|
|
31
|
+
build-backend = "hatchling.build"
|
|
32
|
+
|
|
33
|
+
# Ship only what builds the package; README is the PyPI page.
|
|
34
|
+
[tool.hatch.build.targets.sdist]
|
|
35
|
+
include = ["src", "pyproject.toml", "README.md", "LICENSE"]
|
|
36
|
+
|
|
37
|
+
[tool.hatch.build.targets.wheel]
|
|
38
|
+
packages = ["src/desp_sdk"]
|
|
39
|
+
|
|
40
|
+
# LLM usage capture: the .pth runs the boot module at interpreter start.
|
|
41
|
+
[tool.hatch.build.targets.wheel.force-include]
|
|
42
|
+
"src/_desp_sdk_usage_boot.py" = "_desp_sdk_usage_boot.py"
|
|
43
|
+
"src/desp_sdk_usage.pth" = "desp_sdk_usage.pth"
|
|
44
|
+
|
|
45
|
+
[dependency-groups]
|
|
46
|
+
dev = [
|
|
47
|
+
"httpx2>=2.13.1",
|
|
48
|
+
"mypy>=2.4.0",
|
|
49
|
+
"pytest>=9.1.1",
|
|
50
|
+
"ruff>=0.16.10",
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
[tool.ruff]
|
|
54
|
+
target-version = "py312"
|
|
55
|
+
src = ["src"]
|
|
56
|
+
|
|
57
|
+
[tool.mypy]
|
|
58
|
+
strict = true
|
|
59
|
+
files = ["src", "tests"]
|
|
60
|
+
|
|
61
|
+
[tool.pytest.ini_options]
|
|
62
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Interpreter-start bootstrap for LLM usage capture, run by desp_sdk_usage.pth.
|
|
3
|
+
|
|
4
|
+
Lives outside desp_sdk: importing desp_sdk loads httpx (~90 ms), and the
|
|
5
|
+
.pth runs in every interpreter of the environment. This module costs nothing
|
|
6
|
+
when capture is off, and defers the rest until the app imports httpx or httpx2:
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import sys
|
|
12
|
+
from os import environ
|
|
13
|
+
|
|
14
|
+
# Type-only imports: loading typing / collections at runtime costs ~3-8 ms in
|
|
15
|
+
# every interpreter. mypy treats any name TYPE_CHECKING as true.
|
|
16
|
+
TYPE_CHECKING = False
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from collections.abc import Callable, Sequence
|
|
19
|
+
from importlib.machinery import ModuleSpec
|
|
20
|
+
from types import ModuleType
|
|
21
|
+
|
|
22
|
+
# Mirrors desp_sdk._config; importing it here would load desp_sdk.
|
|
23
|
+
_API_KEY_ENV = "DESP_API_KEY"
|
|
24
|
+
_LLM_USAGE_ENV = "DESP_LLM_USAGE"
|
|
25
|
+
_OFF = "off"
|
|
26
|
+
|
|
27
|
+
# Mirrors desp_sdk.usage._httpx_patch.TARGETS. httpx2: what openai>=3 sends through.
|
|
28
|
+
_TARGETS = ("httpx", "httpx2")
|
|
29
|
+
|
|
30
|
+
# Importing the SDK first loads httpx from inside desp_sdk._http, where
|
|
31
|
+
# activating would hit its partially initialized modules; activate after it.
|
|
32
|
+
_SDK = "desp_sdk"
|
|
33
|
+
_HOOKED = (*_TARGETS, _SDK)
|
|
34
|
+
|
|
35
|
+
# Loading the SDK imports httpx, which re-enters _activate(); see there.
|
|
36
|
+
_busy = False
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _enabled() -> bool:
|
|
40
|
+
"""
|
|
41
|
+
Cheap pre-check of the config; activate() re-checks it.
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
True when the key is set and the kill switch is not.
|
|
45
|
+
"""
|
|
46
|
+
if environ.get(_LLM_USAGE_ENV, "").strip().lower() == _OFF:
|
|
47
|
+
return False
|
|
48
|
+
return bool(environ.get(_API_KEY_ENV))
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _activate() -> None:
|
|
52
|
+
"""
|
|
53
|
+
Load the SDK and start capture. Never raises: the app's import must not break.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
None.
|
|
57
|
+
"""
|
|
58
|
+
global _busy
|
|
59
|
+
|
|
60
|
+
# Re-entry while the SDK loads (it imports httpx): skip; the outer call
|
|
61
|
+
# patches every target loaded by then.
|
|
62
|
+
if _busy:
|
|
63
|
+
return
|
|
64
|
+
|
|
65
|
+
_busy = True
|
|
66
|
+
try:
|
|
67
|
+
from desp_sdk.usage._activate import activate
|
|
68
|
+
|
|
69
|
+
activate()
|
|
70
|
+
except Exception: # noqa: BLE001, S110 - the app's import must not break; no logging set up yet
|
|
71
|
+
pass
|
|
72
|
+
finally:
|
|
73
|
+
_busy = False
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _exec_busy(exec_module: Callable[[ModuleType], None], module: ModuleType) -> None:
|
|
77
|
+
"""
|
|
78
|
+
Execute a module with _activate() deferred, as while the SDK loads.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
exec_module: The loader's original exec_module.
|
|
82
|
+
module: The module to execute.
|
|
83
|
+
Returns:
|
|
84
|
+
None.
|
|
85
|
+
"""
|
|
86
|
+
global _busy
|
|
87
|
+
|
|
88
|
+
outer = _busy
|
|
89
|
+
_busy = True
|
|
90
|
+
try:
|
|
91
|
+
exec_module(module)
|
|
92
|
+
finally:
|
|
93
|
+
_busy = outer
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _all_loaded() -> bool:
|
|
97
|
+
"""
|
|
98
|
+
Whether every target package is imported.
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
True when none is left to wait for.
|
|
102
|
+
"""
|
|
103
|
+
return all(name in sys.modules for name in _TARGETS)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class _HttpxHook:
|
|
107
|
+
"""
|
|
108
|
+
Import hook: lets the normal finders locate each target (httpx, httpx2),
|
|
109
|
+
then runs _activate() right after its module code has executed. Leaves
|
|
110
|
+
sys.meta_path once every target is loaded.
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
def __init__(self) -> None:
|
|
114
|
+
"""
|
|
115
|
+
Start with no lookup in progress.
|
|
116
|
+
|
|
117
|
+
Returns:
|
|
118
|
+
None.
|
|
119
|
+
"""
|
|
120
|
+
self._resolving: set[str] = set()
|
|
121
|
+
|
|
122
|
+
def find_spec(
|
|
123
|
+
self,
|
|
124
|
+
fullname: str,
|
|
125
|
+
path: Sequence[str] | None,
|
|
126
|
+
target: ModuleType | None = None,
|
|
127
|
+
) -> ModuleSpec | None:
|
|
128
|
+
"""
|
|
129
|
+
Intercept target and SDK imports only.
|
|
130
|
+
|
|
131
|
+
Args:
|
|
132
|
+
fullname: The module being imported.
|
|
133
|
+
path: The parent package's path (None for top-level).
|
|
134
|
+
target: The module being reloaded, if any.
|
|
135
|
+
Returns:
|
|
136
|
+
httpx's real spec with activation chained after its execution,
|
|
137
|
+
or None to let other finders handle the import.
|
|
138
|
+
"""
|
|
139
|
+
if fullname not in _HOOKED or fullname in self._resolving:
|
|
140
|
+
return None
|
|
141
|
+
|
|
142
|
+
# Mark the lookup, so the find_spec below does not recurse into us.
|
|
143
|
+
from importlib.util import find_spec
|
|
144
|
+
|
|
145
|
+
self._resolving.add(fullname)
|
|
146
|
+
try:
|
|
147
|
+
spec = find_spec(fullname)
|
|
148
|
+
finally:
|
|
149
|
+
self._resolving.discard(fullname)
|
|
150
|
+
|
|
151
|
+
loader = spec.loader if spec else None
|
|
152
|
+
exec_module = getattr(loader, "exec_module", None)
|
|
153
|
+
if spec is None or exec_module is None:
|
|
154
|
+
return spec
|
|
155
|
+
|
|
156
|
+
def exec_then_activate(module: ModuleType) -> None:
|
|
157
|
+
if fullname == _SDK:
|
|
158
|
+
_exec_busy(exec_module, module)
|
|
159
|
+
else:
|
|
160
|
+
exec_module(module)
|
|
161
|
+
_activate()
|
|
162
|
+
self._retire()
|
|
163
|
+
|
|
164
|
+
try:
|
|
165
|
+
loader.exec_module = exec_then_activate # type: ignore[union-attr, method-assign]
|
|
166
|
+
except AttributeError:
|
|
167
|
+
pass
|
|
168
|
+
return spec
|
|
169
|
+
|
|
170
|
+
def _retire(self) -> None:
|
|
171
|
+
"""
|
|
172
|
+
Leave sys.meta_path once no target is left to wait for.
|
|
173
|
+
|
|
174
|
+
Returns:
|
|
175
|
+
None.
|
|
176
|
+
"""
|
|
177
|
+
if not _all_loaded():
|
|
178
|
+
return
|
|
179
|
+
|
|
180
|
+
try:
|
|
181
|
+
sys.meta_path.remove(self)
|
|
182
|
+
except ValueError:
|
|
183
|
+
pass
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def _boot() -> None:
|
|
187
|
+
"""
|
|
188
|
+
Arm capture: now for targets already loaded, else on their first import.
|
|
189
|
+
|
|
190
|
+
Returns:
|
|
191
|
+
None.
|
|
192
|
+
"""
|
|
193
|
+
if not _enabled():
|
|
194
|
+
return
|
|
195
|
+
|
|
196
|
+
if any(name in sys.modules for name in _TARGETS):
|
|
197
|
+
_activate()
|
|
198
|
+
|
|
199
|
+
if _all_loaded():
|
|
200
|
+
return
|
|
201
|
+
|
|
202
|
+
sys.meta_path.insert(0, _HttpxHook())
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
_boot()
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
4
|
+
|
|
5
|
+
from ._config import DespConfigError
|
|
6
|
+
from ._errors import DespError
|
|
7
|
+
from ._http import (
|
|
8
|
+
DEFAULT_TIMEOUT_SECONDS,
|
|
9
|
+
DespApiError,
|
|
10
|
+
DespConnectionError,
|
|
11
|
+
DespResponseError,
|
|
12
|
+
DespTimeoutError,
|
|
13
|
+
)
|
|
14
|
+
from .client import DespClient, emit_delta, emit_delta_strict
|
|
15
|
+
from .health import get_health
|
|
16
|
+
from .langgraph_adapter import wrap_node
|
|
17
|
+
from .models import (
|
|
18
|
+
DeltaAck,
|
|
19
|
+
DeltaCreate,
|
|
20
|
+
SystemDecision,
|
|
21
|
+
TriggerPlanCreate,
|
|
22
|
+
TriggerPlanDecision,
|
|
23
|
+
TriggerPlanMode,
|
|
24
|
+
TriggerPlanOut,
|
|
25
|
+
)
|
|
26
|
+
from .serialization import to_jsonable
|
|
27
|
+
from .trigger_plans import request_trigger_plan
|
|
28
|
+
|
|
29
|
+
try:
|
|
30
|
+
__version__ = version("desp-sdk")
|
|
31
|
+
except PackageNotFoundError:
|
|
32
|
+
__version__ = "0.0.0"
|
|
33
|
+
|
|
34
|
+
__all__ = [
|
|
35
|
+
"DEFAULT_TIMEOUT_SECONDS",
|
|
36
|
+
"DeltaAck",
|
|
37
|
+
"DeltaCreate",
|
|
38
|
+
"DespApiError",
|
|
39
|
+
"DespClient",
|
|
40
|
+
"DespConfigError",
|
|
41
|
+
"DespConnectionError",
|
|
42
|
+
"DespError",
|
|
43
|
+
"DespResponseError",
|
|
44
|
+
"DespTimeoutError",
|
|
45
|
+
"SystemDecision",
|
|
46
|
+
"TriggerPlanCreate",
|
|
47
|
+
"TriggerPlanDecision",
|
|
48
|
+
"TriggerPlanMode",
|
|
49
|
+
"TriggerPlanOut",
|
|
50
|
+
"__version__",
|
|
51
|
+
"emit_delta",
|
|
52
|
+
"emit_delta_strict",
|
|
53
|
+
"get_health",
|
|
54
|
+
"request_trigger_plan",
|
|
55
|
+
"to_jsonable",
|
|
56
|
+
"wrap_node",
|
|
57
|
+
]
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from ._errors import DespError
|
|
4
|
+
|
|
5
|
+
BASE_URL_ENV = "DESP_BASE_URL"
|
|
6
|
+
DEFAULT_BASE_URL = "https://aether-server-production.up.railway.app"
|
|
7
|
+
API_KEY_ENV = "DESP_API_KEY"
|
|
8
|
+
LLM_USAGE_ENV = "DESP_LLM_USAGE"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class DespConfigError(DespError, ValueError):
|
|
12
|
+
"""Raised when the API key cannot be resolved."""
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def resolve_base_url(base_url: str | None) -> str:
|
|
16
|
+
"""
|
|
17
|
+
Resolve the base URL: argument, then DESP_BASE_URL, then production.
|
|
18
|
+
The overrides exist for local and staging deployments.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
base_url: The base URL to resolve.
|
|
22
|
+
Returns:
|
|
23
|
+
The resolved base URL.
|
|
24
|
+
"""
|
|
25
|
+
value = base_url or os.environ.get(BASE_URL_ENV) or DEFAULT_BASE_URL
|
|
26
|
+
return value.rstrip("/")
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def resolve_api_key(api_key: str | None) -> str:
|
|
30
|
+
"""
|
|
31
|
+
Resolve the API key from the environment or the provided API key.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
api_key: The API key to resolve.
|
|
35
|
+
Returns:
|
|
36
|
+
The resolved API key.
|
|
37
|
+
"""
|
|
38
|
+
value = api_key or os.environ.get(API_KEY_ENV)
|
|
39
|
+
if not value:
|
|
40
|
+
raise DespConfigError(
|
|
41
|
+
f"No DESP API key: pass api_key=... or set {API_KEY_ENV}."
|
|
42
|
+
)
|
|
43
|
+
return value
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class DespError(Exception):
|
|
5
|
+
"""
|
|
6
|
+
Base for every SDK error. Catch it to handle all SDK failures at once:
|
|
7
|
+
|
|
8
|
+
DespError
|
|
9
|
+
├── DespConfigError missing base URL / API key
|
|
10
|
+
├── DespApiError non-2xx response (3xx / 4xx / 5xx)
|
|
11
|
+
├── DespTimeoutError no answer within the timeout
|
|
12
|
+
├── DespConnectionError refused, DNS, TLS, protocol
|
|
13
|
+
└── DespResponseError 2xx body is not the expected JSON shape
|
|
14
|
+
"""
|