@sokel-dev/plugin-sdk 0.2.0
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.
- package/LICENSE +201 -0
- package/README.md +95 -0
- package/dist/src/auth.d.ts +33 -0
- package/dist/src/auth.js +14 -0
- package/dist/src/auth.js.map +1 -0
- package/dist/src/contract.d.ts +88 -0
- package/dist/src/contract.js +55 -0
- package/dist/src/contract.js.map +1 -0
- package/dist/src/env.d.ts +8 -0
- package/dist/src/env.js +14 -0
- package/dist/src/env.js.map +1 -0
- package/dist/src/events.d.ts +101 -0
- package/dist/src/events.js +180 -0
- package/dist/src/events.js.map +1 -0
- package/dist/src/index.d.ts +29 -0
- package/dist/src/index.js +24 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/nats.d.ts +35 -0
- package/dist/src/nats.js +363 -0
- package/dist/src/nats.js.map +1 -0
- package/dist/src/plugin.d.ts +80 -0
- package/dist/src/plugin.js +188 -0
- package/dist/src/plugin.js.map +1 -0
- package/dist/src/runtime.d.ts +75 -0
- package/dist/src/runtime.js +94 -0
- package/dist/src/runtime.js.map +1 -0
- package/dist/src/webhook.d.ts +38 -0
- package/dist/src/webhook.js +53 -0
- package/dist/src/webhook.js.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -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.
|
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# Sokel Plugin SDK — Node.js / TypeScript
|
|
2
|
+
|
|
3
|
+
用 TypeScript 写 [Sokel](https://github.com/sokel-dev) 插件。契约写在 `sokel.yaml` 里(语言中立),
|
|
4
|
+
`sokel-gen` 把它生成成 TS 接口与类型化的注册口;SDK 负责注册、传输、凭证、文件、心跳与重连。
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
onIssuesList(p, async (ctx, in_) => {
|
|
8
|
+
const issues = await client.listIssues(in_.project, in_.state);
|
|
9
|
+
return { issues, count: issues.length };
|
|
10
|
+
});
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`in_.project` 拼错是编译错误,不是线上的一次失败调用——代码里没有任何 `any`。
|
|
14
|
+
|
|
15
|
+
## 装
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @sokel-dev/plugin-sdk
|
|
19
|
+
go install github.com/sokel-dev/sokel-plugin-sdk/cmd/sokel-gen@latest # 生成器
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
`sokel-gen` 是个单文件二进制(Go 写的),只在**生成时**用到;跑插件时不需要它。
|
|
23
|
+
|
|
24
|
+
## 四步
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
sokel-gen init -lang ts ./my-plugin
|
|
28
|
+
cd my-plugin
|
|
29
|
+
npm install
|
|
30
|
+
sokel-gen generate . # sokel.yaml → src/sokel.gen.ts
|
|
31
|
+
npm run build && npm start
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
1. **声明** —— `sokel.yaml`:操作、事件、凭证、认证方式。格式见 [docs/manifest.md](../docs/manifest.md)。
|
|
35
|
+
2. **生成** —— `sokel-gen generate .` 产出 `src/sokel.gen.ts`:每个操作一对 `XxxIn` / `XxxOut` 接口
|
|
36
|
+
和一个 `onXxx(p, fn)`;每个事件一个 payload 接口和一个 `triggerXxx(ctx, eventId, payload)`。
|
|
37
|
+
3. **实现** —— handler 签名完全具体,返回值可以是对象也可以是 Promise。
|
|
38
|
+
4. **连接** —— `await p.run()`。插件**出站**连平台:无入站端口、无公网 IP、无防火墙洞。
|
|
39
|
+
|
|
40
|
+
## 为什么不是 zod
|
|
41
|
+
|
|
42
|
+
zod schema 是运行时对象:用它声明契约意味着「契约只有跑起来才知道」,而且每种语言都得
|
|
43
|
+
自己解释一遍那套 DSL。声明留在 `sokel.yaml`,TS 这边只要类型——类型在编译期,运行时零开销。
|
|
44
|
+
|
|
45
|
+
## 能力一览
|
|
46
|
+
|
|
47
|
+
| 要做的事 | 怎么写 |
|
|
48
|
+
|---|---|
|
|
49
|
+
| 读凭证 | `ctx.credentialAs<Credential>()` |
|
|
50
|
+
| 取入参文件的字节 | `await ctx.fetch(in_.file)` |
|
|
51
|
+
| 产出文件 | `await ctx.upload(name, mime, bytes)` → 放进出参 |
|
|
52
|
+
| 流式产出 | `out.text(...)` 逐帧给人看,`out.vars({...})` 给下游 |
|
|
53
|
+
| 推事件 | `await triggerMessage(ctx, eventId, {...})` |
|
|
54
|
+
| 常驻事件源 | `p.registerSource(id, label, fn)`,循环里判 `ctx.stopped` |
|
|
55
|
+
| 平台代收 webhook | `p.registerWebhook(fn)`,返回 `ok()` / `text(401, "...")` |
|
|
56
|
+
| 协作式认证 | `p.registerAuth({ start, poll, submit })` |
|
|
57
|
+
| 会话型凭证刷新 | `await ctx.updateCredential({ session: "…" })` |
|
|
58
|
+
| 自报运行态 | `ctx.reportStatus("auth_required", "…")` |
|
|
59
|
+
|
|
60
|
+
## 配置
|
|
61
|
+
|
|
62
|
+
SDK 读 `SOKEL_` 前缀的环境变量:
|
|
63
|
+
|
|
64
|
+
| 变量 | 必填 | 含义 |
|
|
65
|
+
|---|---|---|
|
|
66
|
+
| `SOKEL_ENDPOINT` | 是 | `nats://broker:4222`,或 `https://` 平台地址(经 `/connect-info` 发现 broker) |
|
|
67
|
+
| `SOKEL_TOKEN` | 是 | 接入组 token(`skp_…`),平台据此认「插件 + 工作空间」 |
|
|
68
|
+
| `SOKEL_NATS_TOKEN` | 否 | broker 的传输层鉴权 |
|
|
69
|
+
| `SOKEL_NATS_CA` | 否 | `tls://` broker 的自定义 CA |
|
|
70
|
+
| `SOKEL_INSTANCE_ID` | 否 | 固定副本身份(默认按 token 指纹落盘复用) |
|
|
71
|
+
| `SOKEL_REGION` | 否 | 副本的地域标注 |
|
|
72
|
+
|
|
73
|
+
凭证从不由插件存储:平台随每次调用把解析后的字段下发下来。
|
|
74
|
+
|
|
75
|
+
## 例子
|
|
76
|
+
|
|
77
|
+
[`examples/kitchen-sink`](../examples/kitchen-sink) 覆盖了全部形态——每种字段、文件、流式、
|
|
78
|
+
事件、webhook、协作式认证各一份,Node 与 Python 实现的是同一份声明。
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
cd examples/kitchen-sink/node
|
|
82
|
+
npm install && npm run build
|
|
83
|
+
SOKEL_ENDPOINT=nats://localhost:4222 SOKEL_TOKEN=skp_xxx npm start
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## 开发本 SDK
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
pnpm install
|
|
90
|
+
pnpm test # tsc + node --test
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## License
|
|
94
|
+
|
|
95
|
+
Apache-2.0.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 协作式凭证认证:有些凭证没法让人填——扫码、验证码回填、OAuth 同意页。
|
|
3
|
+
*
|
|
4
|
+
* 面板点「登录」→ start 拿挑战 →(扫码 / 回填)→ 2s 轮询 poll → confirmed。
|
|
5
|
+
*
|
|
6
|
+
* 形态写在 sokel.yaml 的 credential.auth 里(声明式),处理器挂在保留操作 id
|
|
7
|
+
* auth.start / auth.poll / auth.submit 上——**不要**自己注册叫 auth_start 的业务操作:
|
|
8
|
+
* 那三个名字从来不是保留字,任何插件的同名业务操作都会让面板的按钮凭空出现。
|
|
9
|
+
*/
|
|
10
|
+
import type { Ctx } from "./runtime.js";
|
|
11
|
+
export declare const PENDING = "pending";
|
|
12
|
+
export declare const SCANNED = "scanned";
|
|
13
|
+
export declare const CONFIRMED = "confirmed";
|
|
14
|
+
export declare const EXPIRED = "expired";
|
|
15
|
+
/** start 交出的挑战。面板按 kind 渲染:qr 画二维码,input 显示 prompt 与输入框。 */
|
|
16
|
+
export interface AuthChallenge {
|
|
17
|
+
authId?: string;
|
|
18
|
+
kind?: "qr" | "input";
|
|
19
|
+
/** data-uri 形态的二维码图片。 */
|
|
20
|
+
qrImage?: string;
|
|
21
|
+
prompt?: string;
|
|
22
|
+
expiresIn?: number;
|
|
23
|
+
}
|
|
24
|
+
/** poll 的结果。session 只在 confirmed 时带上——中途带出去等于让平台反复覆写凭证行。 */
|
|
25
|
+
export interface AuthState {
|
|
26
|
+
status: string;
|
|
27
|
+
session?: Record<string, unknown>;
|
|
28
|
+
}
|
|
29
|
+
export interface AuthHandlers {
|
|
30
|
+
start?: (ctx: Ctx) => Promise<AuthChallenge> | AuthChallenge;
|
|
31
|
+
poll?: (ctx: Ctx, authId: string) => Promise<AuthState> | AuthState;
|
|
32
|
+
submit?: (ctx: Ctx, authId: string, input: string) => Promise<void> | void;
|
|
33
|
+
}
|
package/dist/src/auth.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 协作式凭证认证:有些凭证没法让人填——扫码、验证码回填、OAuth 同意页。
|
|
3
|
+
*
|
|
4
|
+
* 面板点「登录」→ start 拿挑战 →(扫码 / 回填)→ 2s 轮询 poll → confirmed。
|
|
5
|
+
*
|
|
6
|
+
* 形态写在 sokel.yaml 的 credential.auth 里(声明式),处理器挂在保留操作 id
|
|
7
|
+
* auth.start / auth.poll / auth.submit 上——**不要**自己注册叫 auth_start 的业务操作:
|
|
8
|
+
* 那三个名字从来不是保留字,任何插件的同名业务操作都会让面板的按钮凭空出现。
|
|
9
|
+
*/
|
|
10
|
+
export const PENDING = "pending";
|
|
11
|
+
export const SCANNED = "scanned";
|
|
12
|
+
export const CONFIRMED = "confirmed";
|
|
13
|
+
export const EXPIRED = "expired";
|
|
14
|
+
//# sourceMappingURL=auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/auth.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,MAAM,CAAC,MAAM,OAAO,GAAG,SAAS,CAAC;AACjC,MAAM,CAAC,MAAM,OAAO,GAAG,SAAS,CAAC;AACjC,MAAM,CAAC,MAAM,SAAS,GAAG,WAAW,CAAC;AACrC,MAAM,CAAC,MAAM,OAAO,GAAG,SAAS,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 契约的运行时视图(线协议 §5)。
|
|
3
|
+
*
|
|
4
|
+
* 契约本身是**数据**:`sokel-gen` 从 sokel.yaml 生成一份 CONTRACT 常量,运行时只是查它、上报它。
|
|
5
|
+
* 所以这里不重新定义一套 Field 校验器——那会变成契约的第二份定义,而两份定义迟早会漂。
|
|
6
|
+
*/
|
|
7
|
+
/** 一个入/出参字段(协议 §5 的 Field)。这里只声明形状,不做校验——校验在 sokel-gen。 */
|
|
8
|
+
export interface Field {
|
|
9
|
+
name: string;
|
|
10
|
+
label?: string;
|
|
11
|
+
type: string;
|
|
12
|
+
types?: string[];
|
|
13
|
+
required?: boolean;
|
|
14
|
+
default?: unknown;
|
|
15
|
+
desc?: string;
|
|
16
|
+
options?: Array<string | {
|
|
17
|
+
value: string;
|
|
18
|
+
label?: string;
|
|
19
|
+
}>;
|
|
20
|
+
fields?: Field[];
|
|
21
|
+
valueType?: Field;
|
|
22
|
+
itemType?: string;
|
|
23
|
+
goType?: string;
|
|
24
|
+
opaque?: boolean;
|
|
25
|
+
oneOf?: Array<{
|
|
26
|
+
name: string;
|
|
27
|
+
label?: string;
|
|
28
|
+
type: string;
|
|
29
|
+
fields?: Field[];
|
|
30
|
+
}>;
|
|
31
|
+
}
|
|
32
|
+
export interface OperationSpec {
|
|
33
|
+
id: string;
|
|
34
|
+
label?: string;
|
|
35
|
+
desc?: string;
|
|
36
|
+
stream?: boolean;
|
|
37
|
+
internal?: boolean;
|
|
38
|
+
timeoutSec?: number;
|
|
39
|
+
inputs: Field[];
|
|
40
|
+
outputs: Field[];
|
|
41
|
+
}
|
|
42
|
+
export interface EventSpec {
|
|
43
|
+
id: string;
|
|
44
|
+
label?: string;
|
|
45
|
+
desc?: string;
|
|
46
|
+
fields: Field[];
|
|
47
|
+
}
|
|
48
|
+
export interface AuthFlowSpec {
|
|
49
|
+
kind: "qr" | "input" | "oauth";
|
|
50
|
+
steps?: string[];
|
|
51
|
+
}
|
|
52
|
+
/** 生成物 CONTRACT 的形状。键名与注册载荷(协议 §3)同名,直接上报,不做转换。 */
|
|
53
|
+
export interface ContractData {
|
|
54
|
+
name?: string;
|
|
55
|
+
label?: string;
|
|
56
|
+
desc?: string;
|
|
57
|
+
version?: string;
|
|
58
|
+
operations: OperationSpec[];
|
|
59
|
+
credential_schema?: Field[];
|
|
60
|
+
events?: EventSpec[];
|
|
61
|
+
events_common?: Field[];
|
|
62
|
+
auth_flow?: AuthFlowSpec;
|
|
63
|
+
oauth?: {
|
|
64
|
+
provider: string;
|
|
65
|
+
scopes?: string[];
|
|
66
|
+
};
|
|
67
|
+
capabilities?: Record<string, boolean>;
|
|
68
|
+
doc?: string;
|
|
69
|
+
doc_url?: string;
|
|
70
|
+
}
|
|
71
|
+
/** 保留操作 id(认证流)。带点号,业务 id 产生不出来(业务 id 限定 ^[a-z][a-z0-9_]*$)。 */
|
|
72
|
+
export declare const OP_AUTH_START = "auth.start";
|
|
73
|
+
export declare const OP_AUTH_POLL = "auth.poll";
|
|
74
|
+
export declare const OP_AUTH_SUBMIT = "auth.submit";
|
|
75
|
+
/** 平台代收 webhook 的特殊操作名(复用调用帧,见协议 §7b)。 */
|
|
76
|
+
export declare const OP_WEBHOOK = "__webhook__";
|
|
77
|
+
/** 能力位:注册了 webhook 处理器就是支持,不靠作者手动声明。 */
|
|
78
|
+
export declare const CAP_WEBHOOK = "webhook";
|
|
79
|
+
export declare class Contract {
|
|
80
|
+
readonly data: ContractData;
|
|
81
|
+
constructor(data: ContractData);
|
|
82
|
+
operations(): OperationSpec[];
|
|
83
|
+
operation(id: string): OperationSpec | undefined;
|
|
84
|
+
isStream(id: string): boolean;
|
|
85
|
+
eventIds(): string[];
|
|
86
|
+
/** 契约部分的注册载荷。空值一律省略(协议:新字段一律 optional)。 */
|
|
87
|
+
payload(): Record<string, unknown>;
|
|
88
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 契约的运行时视图(线协议 §5)。
|
|
3
|
+
*
|
|
4
|
+
* 契约本身是**数据**:`sokel-gen` 从 sokel.yaml 生成一份 CONTRACT 常量,运行时只是查它、上报它。
|
|
5
|
+
* 所以这里不重新定义一套 Field 校验器——那会变成契约的第二份定义,而两份定义迟早会漂。
|
|
6
|
+
*/
|
|
7
|
+
/** 保留操作 id(认证流)。带点号,业务 id 产生不出来(业务 id 限定 ^[a-z][a-z0-9_]*$)。 */
|
|
8
|
+
export const OP_AUTH_START = "auth.start";
|
|
9
|
+
export const OP_AUTH_POLL = "auth.poll";
|
|
10
|
+
export const OP_AUTH_SUBMIT = "auth.submit";
|
|
11
|
+
/** 平台代收 webhook 的特殊操作名(复用调用帧,见协议 §7b)。 */
|
|
12
|
+
export const OP_WEBHOOK = "__webhook__";
|
|
13
|
+
/** 能力位:注册了 webhook 处理器就是支持,不靠作者手动声明。 */
|
|
14
|
+
export const CAP_WEBHOOK = "webhook";
|
|
15
|
+
export class Contract {
|
|
16
|
+
data;
|
|
17
|
+
constructor(data) {
|
|
18
|
+
this.data = data;
|
|
19
|
+
}
|
|
20
|
+
operations() {
|
|
21
|
+
return this.data.operations ?? [];
|
|
22
|
+
}
|
|
23
|
+
operation(id) {
|
|
24
|
+
return this.operations().find((op) => op.id === id);
|
|
25
|
+
}
|
|
26
|
+
isStream(id) {
|
|
27
|
+
return this.operation(id)?.stream === true;
|
|
28
|
+
}
|
|
29
|
+
eventIds() {
|
|
30
|
+
return (this.data.events ?? []).map((e) => e.id);
|
|
31
|
+
}
|
|
32
|
+
/** 契约部分的注册载荷。空值一律省略(协议:新字段一律 optional)。 */
|
|
33
|
+
payload() {
|
|
34
|
+
const out = { operations: this.operations() };
|
|
35
|
+
const d = this.data;
|
|
36
|
+
if (d.credential_schema?.length)
|
|
37
|
+
out.credential_schema = d.credential_schema;
|
|
38
|
+
if (d.events?.length)
|
|
39
|
+
out.events = d.events;
|
|
40
|
+
if (d.events_common?.length)
|
|
41
|
+
out.events_common = d.events_common;
|
|
42
|
+
if (d.auth_flow)
|
|
43
|
+
out.auth_flow = d.auth_flow;
|
|
44
|
+
if (d.oauth)
|
|
45
|
+
out.oauth = d.oauth;
|
|
46
|
+
if (d.capabilities && Object.keys(d.capabilities).length)
|
|
47
|
+
out.capabilities = d.capabilities;
|
|
48
|
+
if (d.doc)
|
|
49
|
+
out.doc = d.doc;
|
|
50
|
+
if (d.doc_url)
|
|
51
|
+
out.doc_url = d.doc_url;
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=contract.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contract.js","sourceRoot":"","sources":["../../src/contract.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AA4DH,gEAAgE;AAChE,MAAM,CAAC,MAAM,aAAa,GAAG,YAAY,CAAC;AAC1C,MAAM,CAAC,MAAM,YAAY,GAAG,WAAW,CAAC;AACxC,MAAM,CAAC,MAAM,cAAc,GAAG,aAAa,CAAC;AAE5C,0CAA0C;AAC1C,MAAM,CAAC,MAAM,UAAU,GAAG,aAAa,CAAC;AAExC,wCAAwC;AACxC,MAAM,CAAC,MAAM,WAAW,GAAG,SAAS,CAAC;AAErC,MAAM,OAAO,QAAQ;IACE;IAArB,YAAqB,IAAkB;QAAlB,SAAI,GAAJ,IAAI,CAAc;IAAG,CAAC;IAE3C,UAAU;QACR,OAAO,IAAI,CAAC,IAAI,CAAC,UAAU,IAAI,EAAE,CAAC;IACpC,CAAC;IAED,SAAS,CAAC,EAAU;QAClB,OAAO,IAAI,CAAC,UAAU,EAAE,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;IACtD,CAAC;IAED,QAAQ,CAAC,EAAU;QACjB,OAAO,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IAC7C,CAAC;IAED,QAAQ;QACN,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IACnD,CAAC;IAED,2CAA2C;IAC3C,OAAO;QACL,MAAM,GAAG,GAA4B,EAAE,UAAU,EAAE,IAAI,CAAC,UAAU,EAAE,EAAE,CAAC;QACvE,MAAM,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC;QACpB,IAAI,CAAC,CAAC,iBAAiB,EAAE,MAAM;YAAE,GAAG,CAAC,iBAAiB,GAAG,CAAC,CAAC,iBAAiB,CAAC;QAC7E,IAAI,CAAC,CAAC,MAAM,EAAE,MAAM;YAAE,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC;QAC5C,IAAI,CAAC,CAAC,aAAa,EAAE,MAAM;YAAE,GAAG,CAAC,aAAa,GAAG,CAAC,CAAC,aAAa,CAAC;QACjE,IAAI,CAAC,CAAC,SAAS;YAAE,GAAG,CAAC,SAAS,GAAG,CAAC,CAAC,SAAS,CAAC;QAC7C,IAAI,CAAC,CAAC,KAAK;YAAE,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC;QACjC,IAAI,CAAC,CAAC,YAAY,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,MAAM;YAAE,GAAG,CAAC,YAAY,GAAG,CAAC,CAAC,YAAY,CAAC;QAC5F,IAAI,CAAC,CAAC,GAAG;YAAE,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC;QAC3B,IAAI,CAAC,CAAC,OAAO;YAAE,GAAG,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO,CAAC;QACvC,OAAO,GAAG,CAAC;IACb,CAAC;CACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 插件侧环境变量的统一读法:把 SOKEL_ 前缀收在一处(对齐 Go 侧 pluginenv)。
|
|
3
|
+
*
|
|
4
|
+
* 没有第二个前缀的兼容层——认第二个前缀省下的是一次重新部署,换来的是一个没人敢摘的包袱。
|
|
5
|
+
*/
|
|
6
|
+
/** 读 SOKEL_<name>。name 不带前缀,如 env("TOKEN")。 */
|
|
7
|
+
export declare function env(name: string): string;
|
|
8
|
+
export declare function envOr(name: string, fallback: string): string;
|
package/dist/src/env.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 插件侧环境变量的统一读法:把 SOKEL_ 前缀收在一处(对齐 Go 侧 pluginenv)。
|
|
3
|
+
*
|
|
4
|
+
* 没有第二个前缀的兼容层——认第二个前缀省下的是一次重新部署,换来的是一个没人敢摘的包袱。
|
|
5
|
+
*/
|
|
6
|
+
const PREFIX = "SOKEL_";
|
|
7
|
+
/** 读 SOKEL_<name>。name 不带前缀,如 env("TOKEN")。 */
|
|
8
|
+
export function env(name) {
|
|
9
|
+
return (process.env[PREFIX + name] ?? "").trim();
|
|
10
|
+
}
|
|
11
|
+
export function envOr(name, fallback) {
|
|
12
|
+
return env(name) || fallback;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=env.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.js","sourceRoot":"","sources":["../../src/env.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,MAAM,GAAG,QAAQ,CAAC;AAExB,+CAA+C;AAC/C,MAAM,UAAU,GAAG,CAAC,IAAY;IAC9B,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;AACnD,CAAC;AAED,MAAM,UAAU,KAAK,CAAC,IAAY,EAAE,QAAgB;IAClD,OAAO,GAAG,CAAC,IAAI,CAAC,IAAI,QAAQ,CAAC;AAC/B,CAAC"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 事件源:插件主动把外部事件推给平台起 workflow(协议 §7)。
|
|
3
|
+
*
|
|
4
|
+
* 与操作的区别:操作是 request/reply(平台调插件),事件是 fire-and-forget(插件推平台)。
|
|
5
|
+
*
|
|
6
|
+
* 多 bot 单实例(协议 v1.3):平台每次注册/心跳下发「分配给本副本的凭证子集」,
|
|
7
|
+
* supervisor 按它 reconcile —— 每个凭证一套源实例,凭证被移除就取消,字段变了就重启。
|
|
8
|
+
*/
|
|
9
|
+
import type { FileRuntime, SokelFile } from "./runtime.js";
|
|
10
|
+
export declare const TRIGGER_SUBJECT = "sokel.trigger";
|
|
11
|
+
export declare const CREDENTIAL_UPDATE_SUBJECT = "sokel.credential.update";
|
|
12
|
+
/** 注册回包 credentials 列表项 —— 分配给本副本的一个 bot 身份。 */
|
|
13
|
+
export declare class CredEntry {
|
|
14
|
+
readonly id: string;
|
|
15
|
+
readonly fields: Record<string, string>;
|
|
16
|
+
constructor(id?: string, fields?: Record<string, string>);
|
|
17
|
+
/** 字段的稳定签名:reconcile 据此判定「字段变更 → 重启该源实例」。 */
|
|
18
|
+
sig(): string;
|
|
19
|
+
}
|
|
20
|
+
export interface SourceState {
|
|
21
|
+
source_id: string;
|
|
22
|
+
credential_id?: string;
|
|
23
|
+
status: string;
|
|
24
|
+
error?: string;
|
|
25
|
+
since: string;
|
|
26
|
+
}
|
|
27
|
+
/** 源实例运行态(源 × 凭证)。随注册/心跳上报,面板据此展示每个 bot。 */
|
|
28
|
+
export declare class StateBoard {
|
|
29
|
+
private readonly now;
|
|
30
|
+
private readonly m;
|
|
31
|
+
constructor(now?: () => string);
|
|
32
|
+
set(sourceId: string, credId: string, status: string, error?: string): void;
|
|
33
|
+
/**
|
|
34
|
+
* 只在该实例仍是 running 时改写。
|
|
35
|
+
*
|
|
36
|
+
* 源自报过状态(如 auth_required)之后正常返回,收尾时不该把那句话盖掉——
|
|
37
|
+
* 盖掉之后面板上只剩一个「已退出」,而「为什么退出」正是要看的那一半。
|
|
38
|
+
*/
|
|
39
|
+
setIfRunning(sourceId: string, credId: string, status: string, error?: string): void;
|
|
40
|
+
removeCred(credId: string): void;
|
|
41
|
+
snapshot(): SourceState[];
|
|
42
|
+
}
|
|
43
|
+
export type Publish = (subject: string, data: Uint8Array) => void | Promise<void>;
|
|
44
|
+
/** 常驻事件源 / webhook 的上下文:推事件、读凭证、回写凭证、上传附件、自报状态。 */
|
|
45
|
+
export declare class SourceCtx {
|
|
46
|
+
readonly credential: Record<string, string>;
|
|
47
|
+
readonly credentialId: string;
|
|
48
|
+
readonly sourceId: string;
|
|
49
|
+
/** stopped:该源实例被 reconcile 停止时置真。长轮询循环 while (!ctx.stopped)。 */
|
|
50
|
+
stopped: boolean;
|
|
51
|
+
private readonly token;
|
|
52
|
+
private readonly publish;
|
|
53
|
+
private readonly validEvents;
|
|
54
|
+
private readonly board?;
|
|
55
|
+
private readonly files?;
|
|
56
|
+
constructor(opts: {
|
|
57
|
+
token: string;
|
|
58
|
+
publish: Publish;
|
|
59
|
+
validEvents?: string[];
|
|
60
|
+
credential?: Record<string, string>;
|
|
61
|
+
credentialId?: string;
|
|
62
|
+
sourceId?: string;
|
|
63
|
+
board?: StateBoard;
|
|
64
|
+
files?: FileRuntime;
|
|
65
|
+
});
|
|
66
|
+
/** 凭证按类型化形状读出(与操作侧 Ctx.credentialAs 同义)。 */
|
|
67
|
+
credentialAs<T extends object>(): Partial<T>;
|
|
68
|
+
/**
|
|
69
|
+
* 推一条事件(fire-and-forget)。
|
|
70
|
+
*
|
|
71
|
+
* event 必须是已声明的事件 id —— 拼错在这里当场报错,而不是变成一条平台侧无人认领的
|
|
72
|
+
* 消息(那种失败没有任何症状:插件日志正常,工作流就是不起)。
|
|
73
|
+
* eventId 是幂等键,平台按 (plugin, event, eventId) 去重。
|
|
74
|
+
*/
|
|
75
|
+
trigger(event: string, eventId: string, payload: unknown): Promise<void>;
|
|
76
|
+
/**
|
|
77
|
+
* 把 patch 回写到本实例绑定的平台凭证(会话型凭证运行中刷新用)。
|
|
78
|
+
* 平台是唯一凭证存储方,插件本地从不落地凭证。
|
|
79
|
+
*/
|
|
80
|
+
updateCredential(patch: Record<string, string>): Promise<void>;
|
|
81
|
+
/** 自报运行态(如 session 失效 → auth_required),随心跳上报,面板亮「待登录」。 */
|
|
82
|
+
reportStatus(status: string, msg?: string): void;
|
|
83
|
+
upload(name: string, mime: string, data: Uint8Array): Promise<SokelFile>;
|
|
84
|
+
fetch(f: SokelFile): Promise<Uint8Array>;
|
|
85
|
+
}
|
|
86
|
+
/** 一个常驻事件源。fn 在 SDK 起的任务里跑,内部用 ctx.trigger 推事件。 */
|
|
87
|
+
export interface Source {
|
|
88
|
+
id: string;
|
|
89
|
+
label: string;
|
|
90
|
+
fn: (ctx: SourceCtx) => Promise<void>;
|
|
91
|
+
}
|
|
92
|
+
/** per-credential 源实例监督器:按平台下发的凭证集合起停/重启。 */
|
|
93
|
+
export declare class SourceSupervisor {
|
|
94
|
+
private readonly start;
|
|
95
|
+
private readonly running;
|
|
96
|
+
constructor(start: (c: CredEntry) => () => void);
|
|
97
|
+
reconcile(desired: CredEntry[]): void;
|
|
98
|
+
stopAll(): void;
|
|
99
|
+
}
|
|
100
|
+
/** 空(无凭证插件)→ 一个空凭证裸实例,与有凭证时同一条代码路径。 */
|
|
101
|
+
export declare function desiredSourceCreds(creds: CredEntry[]): CredEntry[];
|