sokel-plugin-sdk 0.2.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.
- sokel_plugin_sdk-0.2.0/.gitignore +25 -0
- sokel_plugin_sdk-0.2.0/LICENSE +201 -0
- sokel_plugin_sdk-0.2.0/PKG-INFO +115 -0
- sokel_plugin_sdk-0.2.0/README.md +91 -0
- sokel_plugin_sdk-0.2.0/pyproject.toml +39 -0
- sokel_plugin_sdk-0.2.0/sokel/__init__.py +48 -0
- sokel_plugin_sdk-0.2.0/sokel/auth.py +41 -0
- sokel_plugin_sdk-0.2.0/sokel/contract.py +86 -0
- sokel_plugin_sdk-0.2.0/sokel/env.py +19 -0
- sokel_plugin_sdk-0.2.0/sokel/events.py +195 -0
- sokel_plugin_sdk-0.2.0/sokel/nats_transport.py +417 -0
- sokel_plugin_sdk-0.2.0/sokel/plugin.py +276 -0
- sokel_plugin_sdk-0.2.0/sokel/runtime.py +131 -0
- sokel_plugin_sdk-0.2.0/sokel/webhook.py +68 -0
- sokel_plugin_sdk-0.2.0/tests/conftest.py +32 -0
- sokel_plugin_sdk-0.2.0/tests/test_auth.py +64 -0
- sokel_plugin_sdk-0.2.0/tests/test_dispatch.py +96 -0
- sokel_plugin_sdk-0.2.0/tests/test_events.py +107 -0
- sokel_plugin_sdk-0.2.0/tests/test_files.py +67 -0
- sokel_plugin_sdk-0.2.0/tests/test_register_payload.py +46 -0
- sokel_plugin_sdk-0.2.0/tests/test_webhook.py +71 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# 编译产物。Go 的二进制没有后缀,`go build ./...` 会按目录名把它们丢在 cwd——
|
|
2
|
+
# 已经因此把一个 4.3MB 的 sokel-gen 误提交过一次,所以逐个挡住。
|
|
3
|
+
/sokel-gen
|
|
4
|
+
/examples/*/sysinfo
|
|
5
|
+
/examples/**/*.test
|
|
6
|
+
*.test
|
|
7
|
+
*.exe
|
|
8
|
+
|
|
9
|
+
# 本地实例身份(sokel 首次运行落盘,机器相关)
|
|
10
|
+
.sokel-instance-id*
|
|
11
|
+
|
|
12
|
+
# 编辑器 / 系统
|
|
13
|
+
.DS_Store
|
|
14
|
+
.idea/
|
|
15
|
+
.vscode/
|
|
16
|
+
|
|
17
|
+
# Python SDK
|
|
18
|
+
.venv/
|
|
19
|
+
__pycache__/
|
|
20
|
+
*.egg-info/
|
|
21
|
+
.pytest_cache/
|
|
22
|
+
|
|
23
|
+
# Node SDK
|
|
24
|
+
node_modules/
|
|
25
|
+
dist/
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 The Sokel Authors
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: sokel-plugin-sdk
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Sokel plugin SDK for Python — declare the contract in sokel.yaml, implement typed handlers.
|
|
5
|
+
Project-URL: Homepage, https://github.com/sokel-dev/sokel-plugin-sdk
|
|
6
|
+
Project-URL: Source, https://github.com/sokel-dev/sokel-plugin-sdk/tree/main/sdk-python
|
|
7
|
+
Project-URL: Issues, https://github.com/sokel-dev/sokel-plugin-sdk/issues
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: automation,nats,plugin,sdk,sokel,workflow
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Python: >=3.9
|
|
18
|
+
Requires-Dist: nats-py>=2.6.0
|
|
19
|
+
Requires-Dist: pydantic>=2.5.0
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
22
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# Sokel Plugin SDK — Python
|
|
26
|
+
|
|
27
|
+
用 Python 写 [Sokel](https://github.com/sokel-dev) 插件。契约写在 `sokel.yaml` 里(语言中立),
|
|
28
|
+
`sokel-gen` 把它生成成 pydantic 模型与类型化的注册口;SDK 负责注册、传输、凭证、文件、心跳与重连。
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
async def issues_list(ctx: Ctx, in_: IssuesListIn) -> IssuesListOut:
|
|
32
|
+
issues = await client.list_issues(in_.project, in_.state)
|
|
33
|
+
return IssuesListOut(issues=issues, count=len(issues))
|
|
34
|
+
|
|
35
|
+
on_issues_list(p, issues_list)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`in_.project` 拼错是 IDE 里的红线,不是线上的一次失败调用——代码里没有任何 `dict["key"]`。
|
|
39
|
+
|
|
40
|
+
## 装
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install sokel-plugin-sdk
|
|
44
|
+
go install github.com/sokel-dev/sokel-plugin-sdk/cmd/sokel-gen@latest # 生成器
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`sokel-gen` 是个单文件二进制(Go 写的),只在**生成时**用到;跑插件时不需要它。
|
|
48
|
+
|
|
49
|
+
## 四步
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
sokel-gen init -lang python ./my-plugin
|
|
53
|
+
cd my-plugin
|
|
54
|
+
pip install -r requirements.txt
|
|
55
|
+
sokel-gen generate . # sokel.yaml → sokel_gen.py
|
|
56
|
+
python main.py
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
1. **声明** —— `sokel.yaml`:操作、事件、凭证、认证方式。格式见 [docs/manifest.md](../docs/manifest.md)。
|
|
60
|
+
2. **生成** —— `sokel-gen generate .` 产出 `sokel_gen.py`:每个操作一对 `XxxIn` / `XxxOut` 模型
|
|
61
|
+
和一个 `on_xxx(p, fn)`;每个事件一个 payload 模型和一个 `trigger_xxx(ctx, event_id, payload)`。
|
|
62
|
+
3. **实现** —— handler 签名完全具体,可以是 `async def` 也可以是普通函数。
|
|
63
|
+
4. **连接** —— `asyncio.run(p.run())`。插件**出站**连平台:无入站端口、无公网 IP、无防火墙洞。
|
|
64
|
+
|
|
65
|
+
## 能力一览
|
|
66
|
+
|
|
67
|
+
| 要做的事 | 怎么写 |
|
|
68
|
+
|---|---|
|
|
69
|
+
| 读凭证 | `credential(ctx)` → 生成的 `Credential` 模型 |
|
|
70
|
+
| 取入参文件的字节 | `await ctx.fetch(in_.file)` |
|
|
71
|
+
| 产出文件 | `await ctx.upload(name, mime, data)` → 放进出参 |
|
|
72
|
+
| 流式产出 | `out.text(...)` 逐帧给人看,`out.vars(Out(...))` 给下游 |
|
|
73
|
+
| 推事件 | `await trigger_message(ctx, event_id, MessageEvent(...))` |
|
|
74
|
+
| 常驻事件源 | `p.register_source(id, label, fn)`,循环里判 `ctx.stopping.is_set()` |
|
|
75
|
+
| 平台代收 webhook | `p.register_webhook(fn)`,返回 `ok()` / `text(401, "...")` |
|
|
76
|
+
| 协作式认证 | `p.register_auth(start=…, poll=…, submit=…)` |
|
|
77
|
+
| 会话型凭证刷新 | `await ctx.update_credential({"session": "…"})` |
|
|
78
|
+
| 自报运行态 | `ctx.report_status("auth_required", "…")` |
|
|
79
|
+
|
|
80
|
+
## 配置
|
|
81
|
+
|
|
82
|
+
SDK 读 `SOKEL_` 前缀的环境变量:
|
|
83
|
+
|
|
84
|
+
| 变量 | 必填 | 含义 |
|
|
85
|
+
|---|---|---|
|
|
86
|
+
| `SOKEL_ENDPOINT` | 是 | `nats://broker:4222`,或 `https://` 平台地址(经 `/connect-info` 发现 broker) |
|
|
87
|
+
| `SOKEL_TOKEN` | 是 | 接入组 token(`skp_…`),平台据此认「插件 + 工作空间」 |
|
|
88
|
+
| `SOKEL_NATS_TOKEN` | 否 | broker 的传输层鉴权 |
|
|
89
|
+
| `SOKEL_NATS_CA` | 否 | `tls://` broker 的自定义 CA |
|
|
90
|
+
| `SOKEL_INSTANCE_ID` | 否 | 固定副本身份(默认按 token 指纹落盘复用) |
|
|
91
|
+
| `SOKEL_REGION` | 否 | 副本的地域标注 |
|
|
92
|
+
|
|
93
|
+
凭证从不由插件存储:平台随每次调用把解析后的字段下发下来。
|
|
94
|
+
|
|
95
|
+
## 例子
|
|
96
|
+
|
|
97
|
+
[`examples/kitchen-sink`](../examples/kitchen-sink) 覆盖了全部形态——每种字段、文件、流式、
|
|
98
|
+
事件、webhook、协作式认证各一份,Python 与 Node 实现的是同一份声明。
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
cd examples/kitchen-sink/python
|
|
102
|
+
pip install -r requirements.txt
|
|
103
|
+
SOKEL_ENDPOINT=nats://localhost:4222 SOKEL_TOKEN=skp_xxx python main.py
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## 开发本 SDK
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
uv venv && uv pip install -e '.[dev]'
|
|
110
|
+
python -m pytest -q
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
Apache-2.0.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# Sokel Plugin SDK — Python
|
|
2
|
+
|
|
3
|
+
用 Python 写 [Sokel](https://github.com/sokel-dev) 插件。契约写在 `sokel.yaml` 里(语言中立),
|
|
4
|
+
`sokel-gen` 把它生成成 pydantic 模型与类型化的注册口;SDK 负责注册、传输、凭证、文件、心跳与重连。
|
|
5
|
+
|
|
6
|
+
```python
|
|
7
|
+
async def issues_list(ctx: Ctx, in_: IssuesListIn) -> IssuesListOut:
|
|
8
|
+
issues = await client.list_issues(in_.project, in_.state)
|
|
9
|
+
return IssuesListOut(issues=issues, count=len(issues))
|
|
10
|
+
|
|
11
|
+
on_issues_list(p, issues_list)
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`in_.project` 拼错是 IDE 里的红线,不是线上的一次失败调用——代码里没有任何 `dict["key"]`。
|
|
15
|
+
|
|
16
|
+
## 装
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pip install sokel-plugin-sdk
|
|
20
|
+
go install github.com/sokel-dev/sokel-plugin-sdk/cmd/sokel-gen@latest # 生成器
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`sokel-gen` 是个单文件二进制(Go 写的),只在**生成时**用到;跑插件时不需要它。
|
|
24
|
+
|
|
25
|
+
## 四步
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
sokel-gen init -lang python ./my-plugin
|
|
29
|
+
cd my-plugin
|
|
30
|
+
pip install -r requirements.txt
|
|
31
|
+
sokel-gen generate . # sokel.yaml → sokel_gen.py
|
|
32
|
+
python main.py
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
1. **声明** —— `sokel.yaml`:操作、事件、凭证、认证方式。格式见 [docs/manifest.md](../docs/manifest.md)。
|
|
36
|
+
2. **生成** —— `sokel-gen generate .` 产出 `sokel_gen.py`:每个操作一对 `XxxIn` / `XxxOut` 模型
|
|
37
|
+
和一个 `on_xxx(p, fn)`;每个事件一个 payload 模型和一个 `trigger_xxx(ctx, event_id, payload)`。
|
|
38
|
+
3. **实现** —— handler 签名完全具体,可以是 `async def` 也可以是普通函数。
|
|
39
|
+
4. **连接** —— `asyncio.run(p.run())`。插件**出站**连平台:无入站端口、无公网 IP、无防火墙洞。
|
|
40
|
+
|
|
41
|
+
## 能力一览
|
|
42
|
+
|
|
43
|
+
| 要做的事 | 怎么写 |
|
|
44
|
+
|---|---|
|
|
45
|
+
| 读凭证 | `credential(ctx)` → 生成的 `Credential` 模型 |
|
|
46
|
+
| 取入参文件的字节 | `await ctx.fetch(in_.file)` |
|
|
47
|
+
| 产出文件 | `await ctx.upload(name, mime, data)` → 放进出参 |
|
|
48
|
+
| 流式产出 | `out.text(...)` 逐帧给人看,`out.vars(Out(...))` 给下游 |
|
|
49
|
+
| 推事件 | `await trigger_message(ctx, event_id, MessageEvent(...))` |
|
|
50
|
+
| 常驻事件源 | `p.register_source(id, label, fn)`,循环里判 `ctx.stopping.is_set()` |
|
|
51
|
+
| 平台代收 webhook | `p.register_webhook(fn)`,返回 `ok()` / `text(401, "...")` |
|
|
52
|
+
| 协作式认证 | `p.register_auth(start=…, poll=…, submit=…)` |
|
|
53
|
+
| 会话型凭证刷新 | `await ctx.update_credential({"session": "…"})` |
|
|
54
|
+
| 自报运行态 | `ctx.report_status("auth_required", "…")` |
|
|
55
|
+
|
|
56
|
+
## 配置
|
|
57
|
+
|
|
58
|
+
SDK 读 `SOKEL_` 前缀的环境变量:
|
|
59
|
+
|
|
60
|
+
| 变量 | 必填 | 含义 |
|
|
61
|
+
|---|---|---|
|
|
62
|
+
| `SOKEL_ENDPOINT` | 是 | `nats://broker:4222`,或 `https://` 平台地址(经 `/connect-info` 发现 broker) |
|
|
63
|
+
| `SOKEL_TOKEN` | 是 | 接入组 token(`skp_…`),平台据此认「插件 + 工作空间」 |
|
|
64
|
+
| `SOKEL_NATS_TOKEN` | 否 | broker 的传输层鉴权 |
|
|
65
|
+
| `SOKEL_NATS_CA` | 否 | `tls://` broker 的自定义 CA |
|
|
66
|
+
| `SOKEL_INSTANCE_ID` | 否 | 固定副本身份(默认按 token 指纹落盘复用) |
|
|
67
|
+
| `SOKEL_REGION` | 否 | 副本的地域标注 |
|
|
68
|
+
|
|
69
|
+
凭证从不由插件存储:平台随每次调用把解析后的字段下发下来。
|
|
70
|
+
|
|
71
|
+
## 例子
|
|
72
|
+
|
|
73
|
+
[`examples/kitchen-sink`](../examples/kitchen-sink) 覆盖了全部形态——每种字段、文件、流式、
|
|
74
|
+
事件、webhook、协作式认证各一份,Python 与 Node 实现的是同一份声明。
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
cd examples/kitchen-sink/python
|
|
78
|
+
pip install -r requirements.txt
|
|
79
|
+
SOKEL_ENDPOINT=nats://localhost:4222 SOKEL_TOKEN=skp_xxx python main.py
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## 开发本 SDK
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
uv venv && uv pip install -e '.[dev]'
|
|
86
|
+
python -m pytest -q
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
Apache-2.0.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "sokel-plugin-sdk"
|
|
3
|
+
version = "0.2.0"
|
|
4
|
+
description = "Sokel plugin SDK for Python — declare the contract in sokel.yaml, implement typed handlers."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.9"
|
|
7
|
+
license = { text = "Apache-2.0" }
|
|
8
|
+
keywords = ["sokel", "plugin", "sdk", "nats", "workflow", "automation"]
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Development Status :: 4 - Beta",
|
|
11
|
+
"Intended Audience :: Developers",
|
|
12
|
+
"License :: OSI Approved :: Apache Software License",
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Topic :: Software Development :: Libraries :: Application Frameworks",
|
|
15
|
+
"Typing :: Typed",
|
|
16
|
+
]
|
|
17
|
+
dependencies = [
|
|
18
|
+
"nats-py>=2.6.0",
|
|
19
|
+
"pydantic>=2.5.0",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.urls]
|
|
23
|
+
Homepage = "https://github.com/sokel-dev/sokel-plugin-sdk"
|
|
24
|
+
Source = "https://github.com/sokel-dev/sokel-plugin-sdk/tree/main/sdk-python"
|
|
25
|
+
Issues = "https://github.com/sokel-dev/sokel-plugin-sdk/issues"
|
|
26
|
+
|
|
27
|
+
[project.optional-dependencies]
|
|
28
|
+
dev = ["pytest>=8.0", "pytest-asyncio>=0.23"]
|
|
29
|
+
|
|
30
|
+
[build-system]
|
|
31
|
+
requires = ["hatchling"]
|
|
32
|
+
build-backend = "hatchling.build"
|
|
33
|
+
|
|
34
|
+
[tool.hatch.build.targets.wheel]
|
|
35
|
+
packages = ["sokel"]
|
|
36
|
+
|
|
37
|
+
[tool.pytest.ini_options]
|
|
38
|
+
asyncio_mode = "auto"
|
|
39
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""Sokel plugin SDK for Python.
|
|
2
|
+
|
|
3
|
+
契约在 sokel.yaml 里声明(语言中立),`sokel-gen generate` 生成类型化的模型与注册口;
|
|
4
|
+
本包提供运行时:注册握手、心跳、调用分发、文件分块、事件触发、webhook、协作式认证。
|
|
5
|
+
|
|
6
|
+
from sokel import Plugin
|
|
7
|
+
from sokel_gen import CONTRACT, on_issues_list, IssuesListIn, IssuesListOut
|
|
8
|
+
|
|
9
|
+
p = Plugin(CONTRACT, name="gitlab")
|
|
10
|
+
|
|
11
|
+
async def handle(ctx, in_: IssuesListIn) -> IssuesListOut:
|
|
12
|
+
...
|
|
13
|
+
|
|
14
|
+
on_issues_list(p, handle)
|
|
15
|
+
asyncio.run(p.run())
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from .auth import AuthChallenge, AuthState
|
|
19
|
+
from .contract import Contract
|
|
20
|
+
# 注意导出名不能叫 env —— 那会遮住 sokel.env 这个子模块,包内的 `from . import env`
|
|
21
|
+
# 会拿到函数而不是模块(第一次跑示例就撞上了,报的还是个莫名其妙的 AttributeError)。
|
|
22
|
+
from .env import get as getenv, get_or as getenv_or
|
|
23
|
+
from .events import CredEntry, Source, SourceCtx, StateBoard
|
|
24
|
+
from .plugin import Plugin
|
|
25
|
+
from .runtime import Ctx, Emitter, File
|
|
26
|
+
from .webhook import WebhookRequest, WebhookResponse, ok, text
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"AuthChallenge",
|
|
30
|
+
"AuthState",
|
|
31
|
+
"Contract",
|
|
32
|
+
"CredEntry",
|
|
33
|
+
"Ctx",
|
|
34
|
+
"Emitter",
|
|
35
|
+
"File",
|
|
36
|
+
"Plugin",
|
|
37
|
+
"Source",
|
|
38
|
+
"SourceCtx",
|
|
39
|
+
"StateBoard",
|
|
40
|
+
"WebhookRequest",
|
|
41
|
+
"WebhookResponse",
|
|
42
|
+
"getenv",
|
|
43
|
+
"getenv_or",
|
|
44
|
+
"ok",
|
|
45
|
+
"text",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""协作式凭证认证:有些凭证没法让人填——扫码、验证码回填、OAuth 同意页。
|
|
2
|
+
|
|
3
|
+
面板点「登录」→ start 拿挑战 →(扫码 / 回填)→ 2s 轮询 poll → confirmed。
|
|
4
|
+
|
|
5
|
+
形态写在 sokel.yaml 的 credential.auth 里(声明式),处理器挂在保留操作 id
|
|
6
|
+
auth.start / auth.poll / auth.submit 上——**不要**自己注册叫 auth_start 的业务操作:
|
|
7
|
+
那三个名字从来不是保留字,任何插件的同名业务操作都会让面板的按钮凭空出现。
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from typing import Any, Dict, Optional
|
|
13
|
+
|
|
14
|
+
from pydantic import BaseModel
|
|
15
|
+
|
|
16
|
+
KIND_QR = "qr"
|
|
17
|
+
KIND_INPUT = "input"
|
|
18
|
+
KIND_OAUTH = "oauth"
|
|
19
|
+
|
|
20
|
+
# 状态常量:拼错字符串不会报错,只会让面板一直转圈
|
|
21
|
+
PENDING = "pending"
|
|
22
|
+
SCANNED = "scanned"
|
|
23
|
+
CONFIRMED = "confirmed"
|
|
24
|
+
EXPIRED = "expired"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class AuthChallenge(BaseModel):
|
|
28
|
+
"""start 交出的挑战。面板按 kind 渲染:qr 画二维码,input 显示 prompt 与输入框。"""
|
|
29
|
+
|
|
30
|
+
auth_id: str = ""
|
|
31
|
+
kind: str = ""
|
|
32
|
+
qr_image: str = "" # data-uri
|
|
33
|
+
prompt: str = ""
|
|
34
|
+
expires_in: int = 0
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class AuthState(BaseModel):
|
|
38
|
+
"""poll 的结果。session 只在 confirmed 时带上——中途带出去等于让平台反复覆写凭证行。"""
|
|
39
|
+
|
|
40
|
+
status: str = PENDING
|
|
41
|
+
session: Optional[Dict[str, Any]] = None
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""契约的运行时视图(线协议 §5)。
|
|
2
|
+
|
|
3
|
+
契约本身是**数据**:`sokel-gen` 从 sokel.yaml 生成一份 CONTRACT 字典,运行时只是查它、上报它。
|
|
4
|
+
所以这里不重新定义一套 Field 类——那会变成契约的第二份定义,而两份定义迟早会漂
|
|
5
|
+
(Go 侧栽过一次:SDK 的 Field 是全量的、平台那份只有四个键,SDK 声明了的东西平台看不见)。
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Any, Dict, List, Optional
|
|
11
|
+
|
|
12
|
+
# 契约字典的键(与线协议 §3 的注册载荷同名,直接上报,不做转换)
|
|
13
|
+
KEY_OPERATIONS = "operations"
|
|
14
|
+
KEY_CREDENTIAL = "credential_schema"
|
|
15
|
+
KEY_EVENTS = "events"
|
|
16
|
+
KEY_EVENTS_COMMON = "events_common"
|
|
17
|
+
KEY_AUTH_FLOW = "auth_flow"
|
|
18
|
+
KEY_OAUTH = "oauth"
|
|
19
|
+
KEY_CAPABILITIES = "capabilities"
|
|
20
|
+
KEY_DOC = "doc"
|
|
21
|
+
KEY_DOC_URL = "doc_url"
|
|
22
|
+
|
|
23
|
+
# 保留操作 id(认证流)。带点号,业务 id 产生不出来(业务 id 限定 ^[a-z][a-z0-9_]*$)。
|
|
24
|
+
OP_AUTH_START = "auth.start"
|
|
25
|
+
OP_AUTH_POLL = "auth.poll"
|
|
26
|
+
OP_AUTH_SUBMIT = "auth.submit"
|
|
27
|
+
|
|
28
|
+
# 平台代收 webhook 的特殊操作名(复用调用帧,见协议 §7b)
|
|
29
|
+
OP_WEBHOOK = "__webhook__"
|
|
30
|
+
|
|
31
|
+
# 能力位:注册了 webhook 处理器就是支持,不靠作者手动声明
|
|
32
|
+
CAP_WEBHOOK = "webhook"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class Contract:
|
|
36
|
+
"""一份插件契约。构造参数就是生成物 CONTRACT 字典。"""
|
|
37
|
+
|
|
38
|
+
def __init__(self, data: Optional[Dict[str, Any]] = None) -> None:
|
|
39
|
+
self.data: Dict[str, Any] = dict(data or {})
|
|
40
|
+
|
|
41
|
+
# —— 查 ——
|
|
42
|
+
|
|
43
|
+
def operations(self) -> List[Dict[str, Any]]:
|
|
44
|
+
return list(self.data.get(KEY_OPERATIONS) or [])
|
|
45
|
+
|
|
46
|
+
def operation(self, op_id: str) -> Optional[Dict[str, Any]]:
|
|
47
|
+
for op in self.operations():
|
|
48
|
+
if op.get("id") == op_id:
|
|
49
|
+
return op
|
|
50
|
+
return None
|
|
51
|
+
|
|
52
|
+
def is_stream(self, op_id: str) -> bool:
|
|
53
|
+
op = self.operation(op_id)
|
|
54
|
+
return bool(op and op.get("stream"))
|
|
55
|
+
|
|
56
|
+
def event_ids(self) -> List[str]:
|
|
57
|
+
return [e.get("id", "") for e in (self.data.get(KEY_EVENTS) or [])]
|
|
58
|
+
|
|
59
|
+
def get(self, key: str, default: Any = None) -> Any:
|
|
60
|
+
return self.data.get(key, default)
|
|
61
|
+
|
|
62
|
+
def set(self, key: str, value: Any) -> None:
|
|
63
|
+
self.data[key] = value
|
|
64
|
+
|
|
65
|
+
# —— 上报 ——
|
|
66
|
+
|
|
67
|
+
def payload(self) -> Dict[str, Any]:
|
|
68
|
+
"""契约部分的注册载荷。空值一律省略(协议:新字段一律 optional)。"""
|
|
69
|
+
out: Dict[str, Any] = {}
|
|
70
|
+
for key in (
|
|
71
|
+
KEY_OPERATIONS,
|
|
72
|
+
KEY_CREDENTIAL,
|
|
73
|
+
KEY_EVENTS,
|
|
74
|
+
KEY_EVENTS_COMMON,
|
|
75
|
+
KEY_AUTH_FLOW,
|
|
76
|
+
KEY_OAUTH,
|
|
77
|
+
KEY_CAPABILITIES,
|
|
78
|
+
KEY_DOC,
|
|
79
|
+
KEY_DOC_URL,
|
|
80
|
+
):
|
|
81
|
+
v = self.data.get(key)
|
|
82
|
+
if v:
|
|
83
|
+
out[key] = v
|
|
84
|
+
# operations 必须在(哪怕空):平台侧读不到这个键会当成「契约锁定」而不是「没有操作」
|
|
85
|
+
out.setdefault(KEY_OPERATIONS, [])
|
|
86
|
+
return out
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""插件侧环境变量的统一读法:把 SOKEL_ 前缀收在一处(对齐 Go 侧 pluginenv)。
|
|
2
|
+
|
|
3
|
+
没有第二个前缀的兼容层——认第二个前缀省下的是一次重新部署,换来的是一个没人敢摘的包袱。
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
|
|
10
|
+
PREFIX = "SOKEL_"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def get(name: str) -> str:
|
|
14
|
+
"""读 SOKEL_<name>。name 不带前缀,如 get("TOKEN")。"""
|
|
15
|
+
return (os.environ.get(PREFIX + name) or "").strip()
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def get_or(name: str, default: str) -> str:
|
|
19
|
+
return get(name) or default
|