@lingda-ai/dsh-plugin-hublink 0.0.0-stage → 0.1.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 +202 -0
- package/README.md +105 -2
- package/bin/hublink.js +240 -0
- package/cordis.patch.yml +15 -0
- package/docs/ops.md +56 -0
- package/lib/collect.js +157 -0
- package/lib/config.js +98 -0
- package/lib/credentials.js +72 -0
- package/lib/flush.js +79 -0
- package/lib/http.js +97 -0
- package/lib/index.js +157 -0
- package/lib/outbox.js +89 -0
- package/lib/plan.js +90 -0
- package/lib/policy.js +73 -0
- package/lib/redact.js +146 -0
- package/package.json +71 -4
package/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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,106 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @lingda-ai/dsh-plugin-hublink
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
灵港(LingPort)采集端:把本机 DeepSeek Harness 的用量上报到 hub。零运行时依赖。
|
|
4
|
+
|
|
5
|
+
## 它做什么
|
|
6
|
+
|
|
7
|
+
三件事,且只有三件:
|
|
8
|
+
|
|
9
|
+
1. **采**:挂载官方 `ctx.sessionTelemetry` 后端(`sharing: "full"`),每条 canonical 事件变一条记录;没有该 seam 的宿主降级为读 `ledger` 的会话级快照。
|
|
10
|
+
2. **存**:append-only outbox(`~/.dsh/telemetry/hublink-outbox.jsonl`),断电不丢,坏行跳过不拖死队列。
|
|
11
|
+
3. **送**:批量(≤200 条 / ≤1MB)、幂等(`eventId` 写盘时固定,重发不变)、失败分类(401 不重试不丢数、413 拆批、400 丢批记账、429 按 Retry-After 退避)。
|
|
12
|
+
|
|
13
|
+
**它刻意不做**:面板、图表、额度拦截。拦截必须在 LLM 网关,插件侧拦不住;面板由 hub 控制台做,重复做只会出现"两个金额"。
|
|
14
|
+
|
|
15
|
+
## 三条红线
|
|
16
|
+
|
|
17
|
+
| 红线 | 实现 |
|
|
18
|
+
| --- | --- |
|
|
19
|
+
| 不传对话内容 | 白名单投影,只输出 model / tokens / cost / tool / configVersion / plugin;消息类事件直接不采 |
|
|
20
|
+
| 金额未知不写 0 | 拿不到金额就不写 `cost` 字段;写 0 会被 hub 算成"已知 0 元",比缺数据更坏 |
|
|
21
|
+
| 密钥不进备份 | 只落 `~/.dsh/credentials/hublink.json`,目录 0700 / 文件 0600;与 spool 同目录直接拒绝启动 |
|
|
22
|
+
|
|
23
|
+
## 安装
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
# 与空港套件其它插件同一方式
|
|
27
|
+
dsh plugin add @lingda-ai/dsh-plugin-hublink
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
或离线安装(无网络环境):
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pnpm add @lingda-ai/dsh-plugin-hublink --offline
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## 纳管(一次性)
|
|
37
|
+
|
|
38
|
+
在 hub 控制台「机器」页签发令牌(10 分钟、单次、绑指纹),然后:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
hublink enroll --hub http://10.0.0.8:3000/extension/agentops/api/openapi --token lda_enr_xxx
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
成功后会写入凭证文件并打印 `machineId` / `keyId`。**密钥明文只出现这一次**,hub 侧只存哈希。
|
|
45
|
+
|
|
46
|
+
## 配置(`~/.dsh/settings.json`)
|
|
47
|
+
|
|
48
|
+
```json
|
|
49
|
+
{
|
|
50
|
+
"hublink": {
|
|
51
|
+
"hubUrl": "http://10.0.0.8:3000/extension/agentops/api/openapi",
|
|
52
|
+
"machineId": "build-01-a1b2c3",
|
|
53
|
+
"flushIntervalSeconds": 60,
|
|
54
|
+
"policyIntervalSeconds": 300,
|
|
55
|
+
"batchSize": 100
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
`hubUrl` 填到 `.../api/openapi` 为止,插件自己拼 `/ingest`、`/policy`、`/enroll`。
|
|
61
|
+
|
|
62
|
+
## CLI
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
hublink status # 机器、密钥指纹、待发条数、策略版本
|
|
66
|
+
hublink flush # 立即投一批(键盘前的排障手段)
|
|
67
|
+
hublink doctor # 五项自检:配置 / 凭证权限 / 已纳管 / spool 可写 / hub 可达
|
|
68
|
+
hublink logout # 只删本地凭证,hub 侧记录需在控制台吊销
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## 故障处理对照
|
|
72
|
+
|
|
73
|
+
| 现象 | 含义 | 处置 |
|
|
74
|
+
| --- | --- | --- |
|
|
75
|
+
| `401 invalid_key` | 密钥被吊销或无效 | 数据留在 outbox;控制台重新签发令牌 → `enroll` → 继续投,不丢 |
|
|
76
|
+
| `413 batch_too_large` | 批次超限 | 自动拆半重投(本地也会按字节预切) |
|
|
77
|
+
| `400 unsupported_schema` | 版本不匹配 | 升级插件或 hub,不要改 body 硬凑 |
|
|
78
|
+
| `429` | 上报过频(服务端 60 req/min) | 按 `Retry-After` 退避,不并发堆请求 |
|
|
79
|
+
| `pending` 持续增长 | hub 不可达 | 正常:本地继续攒,恢复后一次补齐;超过 7 天看 `doctor` |
|
|
80
|
+
|
|
81
|
+
## 与 hub 的契约
|
|
82
|
+
|
|
83
|
+
- `POST {hubUrl}/ingest` — `X-API-Key`,202 返回 `{accepted, duplicated, rejected}`
|
|
84
|
+
- `GET {hubUrl}/policy` — `If-None-Match`,304 复用缓存
|
|
85
|
+
- `POST {hubUrl}/enroll` — 一次性令牌换密钥,201 返回 `{machineId, key, keyId}`
|
|
86
|
+
|
|
87
|
+
幂等键在服务端是 `(tenant, machine, session, step_seq)`;客户端只保证同一批重发时 `eventId` 不变。
|
|
88
|
+
|
|
89
|
+
## 已知边界(0.1.0)
|
|
90
|
+
|
|
91
|
+
- `sessionTelemetry` 的注册方法名在宿主上是 `register` / `addBackend` / `use` 三者之一,插件三个都试;当前版本是按源码推断,**未在真机 DSH 0.1.7-rc.2 上验证**。降级路径(ledger)已验证。
|
|
92
|
+
- 官方 seam 默认是"完整事件镜像"(assistant/message 带全文),所以脱敏规则是前置条件而非可选项;没有规则时本插件不启动采集。
|
|
93
|
+
- 未接网关熔断:`executor=gateway` 时插件仍只提示,真正阻断在 LLM 网关(P1)。
|
|
94
|
+
|
|
95
|
+
## 本地自测
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
pnpm test # 37 项:脱敏/outbox/批规划/单位/e2e
|
|
99
|
+
node scripts/mock-hub.mjs # 起一个行为一致的 mock hub,打印一次性令牌
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
e2e 用真 HTTP + 真文件跑完整链路:纳管 → 采集 → 投递 → 重投(全 duplicated)→ 策略 304 → 吊销 401 不丢数 → 重新纳管续投。
|
|
103
|
+
|
|
104
|
+
## 许可
|
|
105
|
+
|
|
106
|
+
Apache-2.0。灵搭(LingDa)自有产品,署名与收款主体为灵搭。
|
package/bin/hublink.js
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* hublink CLI
|
|
4
|
+
*
|
|
5
|
+
* 为什么要有 CLI:纳管、自检、排障都在"还没有面板"的时候发生。
|
|
6
|
+
* 一旦 hub 连不上,用户第一反应是开终端,而不是看 UI。
|
|
7
|
+
*/
|
|
8
|
+
import { createHash } from "node:crypto"
|
|
9
|
+
import { existsSync, readFileSync } from "node:fs"
|
|
10
|
+
import { arch, hostname, platform } from "node:os"
|
|
11
|
+
import { homedir } from "node:os"
|
|
12
|
+
import { join } from "node:path"
|
|
13
|
+
import { parseArgs } from "node:util"
|
|
14
|
+
|
|
15
|
+
import { expandHome, normalizeConfig } from "../lib/config.js"
|
|
16
|
+
import { checkPermissions, deleteCredentials, loadCredentials, maskCredentials, saveCredentials } from "../lib/credentials.js"
|
|
17
|
+
import { flushOnce } from "../lib/flush.js"
|
|
18
|
+
import { createClient } from "../lib/http.js"
|
|
19
|
+
import { createOutbox } from "../lib/outbox.js"
|
|
20
|
+
import { readCache } from "../lib/policy.js"
|
|
21
|
+
|
|
22
|
+
const USAGE = `hublink 0.1.0 — 灵港(LingPort)采集端
|
|
23
|
+
|
|
24
|
+
hublink enroll --hub <url> --token <令牌> [--machine-id <id>] [--config <path>]
|
|
25
|
+
hublink status [--config <path>]
|
|
26
|
+
hublink flush [--config <path>] [--verbose]
|
|
27
|
+
hublink doctor [--config <path>]
|
|
28
|
+
|
|
29
|
+
配置文件:默认 ~/.dsh/settings.json 里的 "hublink" 段;也可 --config 直接指一个 JSON。
|
|
30
|
+
`
|
|
31
|
+
|
|
32
|
+
function resolveSettingsPath(configPath) {
|
|
33
|
+
return configPath ? expandHome(configPath, homedir()) : join(homedir(), ".dsh", "settings.json")
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function loadSettings(configPath) {
|
|
37
|
+
const path = resolveSettingsPath(configPath)
|
|
38
|
+
if (!existsSync(path)) return { path, config: {} }
|
|
39
|
+
const raw = JSON.parse(readFileSync(path, "utf8"))
|
|
40
|
+
return { path, config: raw.hublink ?? raw["@lingda-ai/dsh-plugin-hublink"] ?? {} }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function requireConfig(configPath, extra = {}) {
|
|
44
|
+
const { config } = loadSettings(configPath)
|
|
45
|
+
const normalized = normalizeConfig({ ...config, ...extra })
|
|
46
|
+
if (!normalized.ok) {
|
|
47
|
+
console.error(`配置不完整:\n - ${normalized.errors.join("\n - ")}`)
|
|
48
|
+
process.exit(2)
|
|
49
|
+
}
|
|
50
|
+
return normalized.config
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 稳定的机器标识:主机名 + 主机名短哈希(换主机名才会变,换网卡不会) */
|
|
54
|
+
function defaultMachineId() {
|
|
55
|
+
const base = hostname().toLowerCase().replace(/[^a-z0-9._-]/g, "-").slice(0, 40) || "machine"
|
|
56
|
+
const suffix = createHash("sha256").update(hostname()).digest("hex").slice(0, 6)
|
|
57
|
+
return `${base}-${suffix}`
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function fingerprint(machineId) {
|
|
61
|
+
return createHash("sha256").update(`${machineId}|${platform()}|${arch()}`).digest("hex")
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function cmdEnroll(args) {
|
|
65
|
+
const { values } = parseArgs({
|
|
66
|
+
args,
|
|
67
|
+
options: {
|
|
68
|
+
hub: { type: "string" },
|
|
69
|
+
token: { type: "string" },
|
|
70
|
+
"machine-id": { type: "string" },
|
|
71
|
+
config: { type: "string" },
|
|
72
|
+
},
|
|
73
|
+
})
|
|
74
|
+
const machineId = values["machine-id"] ?? defaultMachineId()
|
|
75
|
+
const { config: fileConfig } = loadSettings(values.config)
|
|
76
|
+
// --hub 优先;没给就用设置文件里的 hubUrl(避免重复配置同一个值)
|
|
77
|
+
const hubUrl = values.hub ?? fileConfig.hubUrl
|
|
78
|
+
if (!hubUrl) {
|
|
79
|
+
console.error("需要 --hub(或先在设置文件里写 hublink.hubUrl)")
|
|
80
|
+
process.exit(2)
|
|
81
|
+
}
|
|
82
|
+
if (!values.token) {
|
|
83
|
+
console.error("需要 --token:令牌在 hub 控制台「机器」页签发,10 分钟有效、单次使用")
|
|
84
|
+
process.exit(2)
|
|
85
|
+
}
|
|
86
|
+
const normalized = normalizeConfig({ ...fileConfig, hubUrl, machineId })
|
|
87
|
+
if (!normalized.ok) {
|
|
88
|
+
console.error(`配置不完整:\n - ${normalized.errors.join("\n - ")}`)
|
|
89
|
+
process.exit(2)
|
|
90
|
+
}
|
|
91
|
+
const config = normalized.config
|
|
92
|
+
|
|
93
|
+
const client = createClient({ hubUrl: config.hubUrl, timeoutMs: config.requestTimeoutMs })
|
|
94
|
+
const result = await client.enroll({
|
|
95
|
+
token: values.token,
|
|
96
|
+
machineId,
|
|
97
|
+
fingerprint: fingerprint(machineId),
|
|
98
|
+
hostname: hostname(),
|
|
99
|
+
os: platform(),
|
|
100
|
+
arch: arch(),
|
|
101
|
+
})
|
|
102
|
+
if (result.status !== 201 || !result.json?.key) {
|
|
103
|
+
console.error(`纳管失败:HTTP ${result.status} ${result.json?.message ?? result.json?.code ?? ""}`)
|
|
104
|
+
process.exit(1)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
saveCredentials(config.credentialsPath, {
|
|
108
|
+
hubUrl: config.hubUrl,
|
|
109
|
+
tenantId: result.json.tenantId ?? config.tenantId ?? null,
|
|
110
|
+
machineId,
|
|
111
|
+
keyId: result.json.keyId,
|
|
112
|
+
key: result.json.key,
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
console.log("纳管成功。")
|
|
116
|
+
console.log(` machineId : ${machineId}`)
|
|
117
|
+
console.log(` keyId : ${result.json.keyId}`)
|
|
118
|
+
console.log(` 凭证文件 : ${config.credentialsPath}(0600)`)
|
|
119
|
+
console.log("密钥明文只出现这一次;hub 侧只存哈希。丢了就重新签发令牌,不换密钥不重启。")
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function cmdStatus(args) {
|
|
123
|
+
const { values } = parseArgs({ args, options: { config: { type: "string" }, json: { type: "boolean" } } })
|
|
124
|
+
const config = requireConfig(values.config)
|
|
125
|
+
const credentials = loadCredentials(config.credentialsPath)
|
|
126
|
+
const outbox = createOutbox(config.spoolPath)
|
|
127
|
+
const cache = readCache(config.policyCachePath)
|
|
128
|
+
const report = {
|
|
129
|
+
config: {
|
|
130
|
+
hubUrl: config.hubUrl,
|
|
131
|
+
batchSize: config.batchSize,
|
|
132
|
+
flushIntervalSeconds: config.flushIntervalSeconds,
|
|
133
|
+
spoolPath: config.spoolPath,
|
|
134
|
+
},
|
|
135
|
+
credentials: maskCredentials(credentials),
|
|
136
|
+
outbox: outbox.stats(),
|
|
137
|
+
policy: cache ? { version: cache.policy?.version, etag: cache.etag, fetchedAt: cache.fetchedAt } : null,
|
|
138
|
+
}
|
|
139
|
+
if (values.json) {
|
|
140
|
+
console.log(JSON.stringify(report, null, 2))
|
|
141
|
+
return
|
|
142
|
+
}
|
|
143
|
+
console.log(`hub : ${config.hubUrl}`)
|
|
144
|
+
console.log(`机器 : ${report.credentials.machineId ?? "未纳管"}`)
|
|
145
|
+
console.log(`密钥 : ${report.credentials.keyHint ?? "—"}(keyId ${report.credentials.keyId ?? "—"})`)
|
|
146
|
+
console.log(`待发送 : ${report.outbox.pending} 条 / ${report.outbox.bytes} 字节`)
|
|
147
|
+
if (report.outbox.oldestSeconds !== null) console.log(`最老一条 : ${report.outbox.oldestSeconds}s 前`)
|
|
148
|
+
if (report.outbox.damaged > 0) console.log(`损坏行 : ${report.outbox.damaged}(已跳过,不影响其余投递)`)
|
|
149
|
+
console.log(`策略 : ${report.policy ? `v${report.policy.version}(${report.policy.etag})` : "尚未拉取"}`)
|
|
150
|
+
if (!credentials) console.log("\n未纳管:先跑 hublink enroll --hub <url> --token <令牌>")
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async function cmdFlush(args) {
|
|
154
|
+
const { values } = parseArgs({ args, options: { config: { type: "string" }, verbose: { type: "boolean" } } })
|
|
155
|
+
const config = requireConfig(values.config)
|
|
156
|
+
const credentials = loadCredentials(config.credentialsPath)
|
|
157
|
+
if (!credentials) {
|
|
158
|
+
console.error("未纳管,无法投递。先跑 hublink enroll")
|
|
159
|
+
process.exit(1)
|
|
160
|
+
}
|
|
161
|
+
const outbox = createOutbox(config.spoolPath)
|
|
162
|
+
const client = createClient({ hubUrl: config.hubUrl, key: credentials.key, timeoutMs: config.requestTimeoutMs })
|
|
163
|
+
const log = values.verbose ? (m) => console.log(` · ${m}`) : () => {}
|
|
164
|
+
const result = await flushOnce({ outbox, client, batchSize: config.batchSize, logger: log, sleep: (ms) => new Promise((r) => setTimeout(r, ms)) })
|
|
165
|
+
console.log(JSON.stringify(result, null, 2))
|
|
166
|
+
if (result.needsReenroll) {
|
|
167
|
+
console.error("密钥已被吊销/失效:数据仍在 outbox,重新纳管后继续投递,不会丢。")
|
|
168
|
+
process.exit(1)
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function cmdDoctor(args) {
|
|
173
|
+
const { values } = parseArgs({ args, options: { config: { type: "string" } } })
|
|
174
|
+
const checks = []
|
|
175
|
+
let config
|
|
176
|
+
try {
|
|
177
|
+
config = requireConfig(values.config)
|
|
178
|
+
checks.push(["配置", true, `hub=${config.hubUrl} batch=${config.batchSize}`])
|
|
179
|
+
} catch {
|
|
180
|
+
checks.push(["配置", false, "见上方错误"])
|
|
181
|
+
printChecks(checks)
|
|
182
|
+
process.exit(1)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const perms = checkPermissions(config.credentialsPath)
|
|
186
|
+
checks.push(["凭证权限", perms.ok, perms.ok ? "0600" : perms.reason])
|
|
187
|
+
|
|
188
|
+
const credentials = loadCredentials(config.credentialsPath)
|
|
189
|
+
checks.push(["已纳管", Boolean(credentials), credentials ? `keyId=${credentials.keyId}` : "需要 enroll"])
|
|
190
|
+
|
|
191
|
+
try {
|
|
192
|
+
const outbox = createOutbox(config.spoolPath)
|
|
193
|
+
outbox.append([])
|
|
194
|
+
checks.push(["spool 可写", true, config.spoolPath])
|
|
195
|
+
} catch (error) {
|
|
196
|
+
checks.push(["spool 可写", false, String(error?.message ?? error)])
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (credentials) {
|
|
200
|
+
try {
|
|
201
|
+
const client = createClient({ hubUrl: config.hubUrl, key: credentials.key, timeoutMs: 5000 })
|
|
202
|
+
const result = await client.policy(null)
|
|
203
|
+
checks.push(["hub 可达", result.status === 200 || result.status === 304, `HTTP ${result.status}`])
|
|
204
|
+
} catch (error) {
|
|
205
|
+
checks.push(["hub 可达", false, String(error?.message ?? error)])
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
printChecks(checks)
|
|
210
|
+
if (checks.some(([, ok]) => !ok)) process.exit(1)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function printChecks(checks) {
|
|
214
|
+
for (const [label, ok, detail] of checks) {
|
|
215
|
+
console.log(`${ok ? "✓" : "✗"} ${label.padEnd(10, " ")} ${detail}`)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const [, , command, ...rest] = process.argv
|
|
220
|
+
const commands = { enroll: cmdEnroll, status: cmdStatus, flush: cmdFlush, doctor: cmdDoctor, "logout": cmdLogout }
|
|
221
|
+
|
|
222
|
+
async function cmdLogout(args) {
|
|
223
|
+
const { values } = parseArgs({ args, options: { config: { type: "string" } } })
|
|
224
|
+
const config = requireConfig(values.config)
|
|
225
|
+
deleteCredentials(config.credentialsPath)
|
|
226
|
+
console.log("本地凭证已删除;hub 侧记录仍在,需要时在控制台吊销。")
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (!command || command === "--help" || command === "-h") {
|
|
230
|
+
console.log(USAGE)
|
|
231
|
+
process.exit(0)
|
|
232
|
+
}
|
|
233
|
+
if (!commands[command]) {
|
|
234
|
+
console.error(`未知命令:${command}\n\n${USAGE}`)
|
|
235
|
+
process.exit(2)
|
|
236
|
+
}
|
|
237
|
+
commands[command](rest).catch((error) => {
|
|
238
|
+
console.error(`执行失败:${String(error?.message ?? error)}`)
|
|
239
|
+
process.exit(1)
|
|
240
|
+
})
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @lingda-ai/dsh-plugin-hublink —— 灵港采集端
|
|
2
|
+
#
|
|
3
|
+
# 一个 insert 行即可挂载。插件会把用量写进本地 outbox 并批量投递到 hub;
|
|
4
|
+
# 没有纳管(缺凭证)时只记录、不发送,不影响宿主启动。
|
|
5
|
+
#
|
|
6
|
+
# 配置写在 ~/.dsh/settings.json:
|
|
7
|
+
# {
|
|
8
|
+
# "hublink": {
|
|
9
|
+
# "hubUrl": "http://10.0.0.8:3000/extension/agentops/api/openapi",
|
|
10
|
+
# "machineId": "build-01-a1b2c3"
|
|
11
|
+
# }
|
|
12
|
+
# }
|
|
13
|
+
- insert:
|
|
14
|
+
- id: lingda-hublink
|
|
15
|
+
name: '@lingda-ai/dsh-plugin-hublink'
|
package/docs/ops.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# 运维手册(hublink 0.1.0)
|
|
2
|
+
|
|
3
|
+
## 1. 数据落在哪
|
|
4
|
+
|
|
5
|
+
| 路径 | 内容 | 备份策略 |
|
|
6
|
+
| --- | --- | --- |
|
|
7
|
+
| `~/.dsh/credentials/hublink.json` | 机器密钥(0600) | **不备份、不 rsync、不进 git** |
|
|
8
|
+
| `~/.dsh/telemetry/hublink-outbox.jsonl` | 待发送事件(append-only) | 可备份,恢复后会自动补投 |
|
|
9
|
+
| `~/.dsh/telemetry/hublink-policy.json` | 上次拉到的策略 + etag | 可丢,下次轮询重建 |
|
|
10
|
+
|
|
11
|
+
凭证与 spool 同目录时插件拒绝启动——这是为了避免"备份顺手把密钥带走"。
|
|
12
|
+
|
|
13
|
+
## 2. 日常检查(三条命令)
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
hublink status # pending 是否在增长、策略版本是否更新
|
|
17
|
+
hublink doctor # 五项自检
|
|
18
|
+
hublink flush # 立刻推一次,确认链路通
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
判据:
|
|
22
|
+
- `pending` 稳态应小于 1 个 flush 周期的产量(默认 60s)。
|
|
23
|
+
- 持续增长但 `doctor` 的 hub 可达是 ✓ → 看 hub 侧是否 429/413。
|
|
24
|
+
- `oldestSeconds > 86400` → 说明一天没送出去,先查网络,再看 hub 日志里的 `invalid_key`。
|
|
25
|
+
|
|
26
|
+
## 3. 吊销与换机
|
|
27
|
+
|
|
28
|
+
1. 控制台「机器」页吊销 → 该机所有 `/ingest`、`/policy` 立刻 401。
|
|
29
|
+
2. 机器侧数据**不会丢**:`flush` 返回 `needsReenroll: true`,事件留在 outbox。
|
|
30
|
+
3. 控制台重新签发令牌 → `hublink enroll --token ...` → `hublink flush` 补齐。
|
|
31
|
+
|
|
32
|
+
不要做的两件事:
|
|
33
|
+
- 不要手工删 outbox"清干净"——那等于承认少报。
|
|
34
|
+
- 不要把凭证文件复制到另一台机器"图省事"——指纹绑定会失效,且两台机器共用一个 key 会让归因错乱。
|
|
35
|
+
|
|
36
|
+
## 4. 采集口径
|
|
37
|
+
|
|
38
|
+
| 字段 | 来源 | 缺失时 |
|
|
39
|
+
| --- | --- | --- |
|
|
40
|
+
| `tokens.in/out/cacheRead` | 上游记录 | 记 0(token 数是可加量,0 不会污染金额) |
|
|
41
|
+
| `cost.amount` | 上游(ledger 算好的) | **不写该字段**,hub 记 `cost_unknown` |
|
|
42
|
+
| `attribution.tool/plugin/configVersion` | 上游 | 留空 |
|
|
43
|
+
|
|
44
|
+
金额只由 hub 算一次,插件不重算价表——两套价表跑一段时间必然对账不一致。
|
|
45
|
+
|
|
46
|
+
## 5. 升级与回滚
|
|
47
|
+
|
|
48
|
+
- 升级:`dsh plugin add @lingda-ai/dsh-plugin-hublink@<新版>`,重启后自动加载。
|
|
49
|
+
- 回滚:装回旧版本即可;outbox 格式带 `eventId`,跨版本兼容。
|
|
50
|
+
- 跨大版本(0.x → 1.x)会改 outbox 结构时,README 会写明迁移命令。
|
|
51
|
+
|
|
52
|
+
## 6. 已知未验证项
|
|
53
|
+
|
|
54
|
+
`ctx.sessionTelemetry` 的注册方法名(`register` / `addBackend` / `use`)在宿主 0.1.7-rc.2 上未逐一验证。
|
|
55
|
+
插件三个都试,失败会打印一行日志并降级到 `ledger` 会话级补采。真机首次启动后看日志里有没有
|
|
56
|
+
`已挂载 sessionTelemetry 后端` 即可确认。
|