dsh-zcode-startplan 0.0.0-stage → 0.4.5
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/CHANGELOG.md +405 -0
- package/LICENSE +674 -0
- package/README.md +404 -2
- package/bin/cli.mjs +450 -0
- package/client.js +716 -0
- package/cordis.patch.yml +32 -0
- package/icon.svg +18 -0
- package/index.js +335 -0
- package/lib/adapter.js +422 -0
- package/lib/anthropic-body.js +469 -0
- package/lib/assemble.js +420 -0
- package/lib/captcha/minter.js +654 -0
- package/lib/captcha/sdk-page.js +20 -0
- package/lib/errors.js +125 -0
- package/lib/local-endpoint.js +352 -0
- package/lib/manage-routes.js +489 -0
- package/lib/pinned-model.js +56 -0
- package/lib/plan-client.js +532 -0
- package/lib/pool.js +478 -0
- package/lib/sse.js +317 -0
- package/lib/stream-translator.js +301 -0
- package/lib/text-heuristics.js +35 -0
- package/lib/upstream-api.js +570 -0
- package/lib/upstream.js +134 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +78 -4
- package/scripts/_cdp-inspect.mjs +54 -0
- package/scripts/asar-list.mjs +66 -0
- package/scripts/bench-toks.mjs +138 -0
- package/scripts/check-compat-rc3.mjs +11 -0
- package/scripts/check-data-dir.mjs +31 -0
- package/scripts/restart-dsh.ps1 +76 -0
- package/scripts/run-tests.mjs +68 -0
- package/scripts/smoke-install.mjs +204 -0
- package/scripts/verify_compat.mjs +64 -0
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# dsh-zcode-startplan bundle patch
|
|
2
|
+
#
|
|
3
|
+
# 安装后这里就是「开关面板」:改完重启 Harness 生效(fiber 一次性 apply)。
|
|
4
|
+
#
|
|
5
|
+
# provider / displayName DSH 里的路由名与展示名(与 llm-pi-ai 的路由不能重名)
|
|
6
|
+
# models 暴露给 DSH 的模型 id(与上游目录求交集)
|
|
7
|
+
# accountProviders true = 每个启用账号一个独立 provider 栏位(可在面板关统一栏)
|
|
8
|
+
# captcha.debugPort 宿主 Electron 的 CDP 端口(DSH 桌面端需以
|
|
9
|
+
# --remote-debugging-port=9229 启动;captcha 用它产出)
|
|
10
|
+
# captcha.mode cdp = 自动产出;manual = 只认人工粘贴(见 CLI)
|
|
11
|
+
# endpoint.enabled true 才启动本地 Anthropic 端点(仅 llm-pi-ai 回落路线需要)
|
|
12
|
+
# statusTool 是否注册 zcode_start_plan_status 工具
|
|
13
|
+
- insert:
|
|
14
|
+
- id: zcode-start-plan
|
|
15
|
+
name: dsh-zcode-startplan
|
|
16
|
+
config:
|
|
17
|
+
provider: zcode-start-plan
|
|
18
|
+
displayName: ZCode Start Plan(赠送免费 plan)
|
|
19
|
+
models:
|
|
20
|
+
- GLM-5.3-Flash
|
|
21
|
+
- GLM-5.3
|
|
22
|
+
accountProviders: true
|
|
23
|
+
dataDir: ''
|
|
24
|
+
modelCacheMs: 600000
|
|
25
|
+
captcha:
|
|
26
|
+
mode: cdp
|
|
27
|
+
debugPort: 9229
|
|
28
|
+
origin: https://zcode.z.ai/
|
|
29
|
+
endpoint:
|
|
30
|
+
enabled: false
|
|
31
|
+
port: 17899
|
|
32
|
+
statusTool: true
|
package/icon.svg
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="ZCode Start Plan">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="g" x1="0" y1="0" x2="1" y2="1">
|
|
4
|
+
<stop offset="0" stop-color="#3B82F6"/>
|
|
5
|
+
<stop offset="1" stop-color="#8B5CF6"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<!-- 底座:代表"通道" -->
|
|
9
|
+
<rect x="4" y="4" width="56" height="56" rx="14" fill="url(#g)"/>
|
|
10
|
+
<!-- Z 字形折线:ZCode 的首字母 -->
|
|
11
|
+
<path d="M19 19 H45 L19 45 H45" fill="none" stroke="#FFFFFF" stroke-width="6"
|
|
12
|
+
stroke-linecap="round" stroke-linejoin="round"/>
|
|
13
|
+
<!-- 右上角的"起点"圆点:Start -->
|
|
14
|
+
<circle cx="47" cy="17" r="5" fill="#FFFFFF" opacity="0.9"/>
|
|
15
|
+
<!-- 左下角的勾:免费额度已领取的状态暗示 -->
|
|
16
|
+
<path d="M15 50 L19 54 L26 47" fill="none" stroke="#FFFFFF" stroke-width="3.5"
|
|
17
|
+
stroke-linecap="round" stroke-linejoin="round" opacity="0.75"/>
|
|
18
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 插件入口:把「ZCode 赠送免费 plan」接进 DSH 的 `llm` 服务。
|
|
3
|
+
*
|
|
4
|
+
* ## 装配(顺序不可乱)
|
|
5
|
+
*
|
|
6
|
+
* ```
|
|
7
|
+
* 解析配置 → 异步建资源(账号库 / 账号池 / captcha 产出器 / plan 客户端 / 可选本地端点)
|
|
8
|
+
* → 同步注册 llm 适配器(路由名 = config.provider,默认 zcode-start-plan)
|
|
9
|
+
* → 同步注册状态工具
|
|
10
|
+
* → 就绪后向 ctx 发出 `llm/adapters-updated`,让模型选择器立刻刷新
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* ## 为什么「注册」必须同步
|
|
14
|
+
*
|
|
15
|
+
* `ctx.effect(fn)` 要求 `fn` 同步返回 disposer;在异步函数里调用 `ctx.effect` 会拿到
|
|
16
|
+
* 一条**未挂到 fiber** 的注册 —— 热重载或禁用插件后路由不会被撤销,表现为
|
|
17
|
+
* 「插件已禁用,模型却还在选择器里且每次调用都失败」。故异步部分只负责**建资源**,
|
|
18
|
+
* 注册统一放在同步入口里做。
|
|
19
|
+
*
|
|
20
|
+
* ## 与其他 llm 插件的关系
|
|
21
|
+
*
|
|
22
|
+
* 只要 `config.provider` 不与 `llm-pi-ai` / `llm-deepseek` 的路由重名就不会冲突。
|
|
23
|
+
* 重名时 `registerAdapter` 抛 `DUPLICATE_ADAPTER`:这里捕获并降级为「只提供状态工具」,
|
|
24
|
+
* 不让整个 fiber 崩掉(其余模型仍可正常服务)。
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { assemble } from './lib/assemble.js'
|
|
28
|
+
import { LOGIN_CHANNELS } from './lib/upstream-api.js'
|
|
29
|
+
|
|
30
|
+
/** 默认模型列表(同时作为 Config 的默认值来源)。 */
|
|
31
|
+
const DEFAULT_MODELS = Object.freeze(['GLM-5.3-Flash', 'GLM-5.3'])
|
|
32
|
+
|
|
33
|
+
/** 插件名(Cordis 用它标识 fiber)。 */
|
|
34
|
+
export const name = 'zcode-start-plan'
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 依赖的服务。
|
|
38
|
+
*
|
|
39
|
+
* ⚠ 宿主 ctx 是沙箱 façade:**只有这里列出的服务才会挂到 ctx 上**,
|
|
40
|
+
* 少写一个,`apply` 里一碰就抛 `cannot get property "X" without inject`,
|
|
41
|
+
* 整个插件静默失效(宿主只往 stderr 打一条 warning,打包态下看不到)。
|
|
42
|
+
*
|
|
43
|
+
* - `llm` 注册 provider 适配器(本插件的主用途)
|
|
44
|
+
* - `tools` 注册 `zcode_start_plan_status` 状态工具
|
|
45
|
+
* - `timer` 每小时清理过期的「账号×模型」限流标记
|
|
46
|
+
* - `webServer` 注册账号池管理路由(Web UI 面板的数据通道;可选增强)
|
|
47
|
+
*
|
|
48
|
+
* ⚠ 图片输入**不**走 inject:cordis 的 inject 是硬依赖(声明的服务缺席时整个
|
|
49
|
+
* fiber 挂起,插件完全不加载)。附件服务经沙箱 façade 的 `ctx.get('attachments')`
|
|
50
|
+
* 探测(宿主允许未声明查询),缺席时图片消息降级为占位文本,主功能不受影响。
|
|
51
|
+
*/
|
|
52
|
+
export const inject = ['llm', 'tools', 'timer', 'webServer']
|
|
53
|
+
|
|
54
|
+
const PROVIDER = 'zcode-start-plan'
|
|
55
|
+
|
|
56
|
+
/** 默认配置(每个字段都能由 profile 的 cordis.patch.yml 覆盖)。 */
|
|
57
|
+
const DEFAULT_CONFIG = Object.freeze({
|
|
58
|
+
/** DSH 里的路由名;模型选择器里显示为 `<displayName>`。 */
|
|
59
|
+
provider: PROVIDER,
|
|
60
|
+
displayName: 'ZCode Start Plan(赠送免费 plan)',
|
|
61
|
+
/** 暴露给 DSH 的模型 id 列表(与上游模型目录求交集;拉不到时直接用这份)。 */
|
|
62
|
+
models: DEFAULT_MODELS,
|
|
63
|
+
/**
|
|
64
|
+
* 为每个启用账号注册一个独立的 provider 栏位(像 workbuddy 一样一栏一个
|
|
65
|
+
* 账号,路由名 `<provider>-acct-<账号id>`、显示名「ZCode Start Plan · 昵称」),
|
|
66
|
+
* 选中即只消耗该账号的额度,不自动换号。关闭后只有统一选号栏位。
|
|
67
|
+
* (0.3.0 的 `pinnedAccounts` 同义更名,旧值仍被接受。)
|
|
68
|
+
*/
|
|
69
|
+
accountProviders: true,
|
|
70
|
+
/** 上游模型目录缓存时长(毫秒)。 */
|
|
71
|
+
modelCacheMs: 10 * 60_000,
|
|
72
|
+
/** 数据目录(空 = ~/.dsh/zcode-start-plan)。 */
|
|
73
|
+
dataDir: '',
|
|
74
|
+
/**
|
|
75
|
+
* 登录渠道:`cn`(智谱国内站 bigmodel.cn)或 `intl`(Z.AI 国际站 z.ai)。
|
|
76
|
+
*
|
|
77
|
+
* 两条渠道**协议完全一致**,只是 `provider` 与授权站点不同:
|
|
78
|
+
* 国内账号用 `cn`,国际账号用 `intl`。账号池可以同时容纳两边登录进来的账号
|
|
79
|
+
* (每条凭据都记着自己的 `login_channel`,推理走同一个 zcode-plan 端点)。
|
|
80
|
+
*
|
|
81
|
+
* 这个配置只影响「在 DSH 里发起登录」的场景;用 CLI 登录时以 `--channel` 为准。
|
|
82
|
+
*/
|
|
83
|
+
channel: 'cn',
|
|
84
|
+
captcha: Object.freeze({
|
|
85
|
+
/** 'cdp' = 用宿主 Electron 产出(默认);'manual' = 由人工粘贴 param(见 CLI)。 */
|
|
86
|
+
mode: 'cdp',
|
|
87
|
+
/** 宿主调试端口(DSH 桌面端需以 --remote-debugging-port 启动)。 */
|
|
88
|
+
debugPort: 9229,
|
|
89
|
+
/** captcha 页面 origin(必须是真实 https origin,否则二次 mint 必失败)。 */
|
|
90
|
+
origin: 'https://zcode.z.ai/',
|
|
91
|
+
}),
|
|
92
|
+
endpoint: Object.freeze({
|
|
93
|
+
/** 是否启动本地 Anthropic 兼容端点(仅 `llm-pi-ai` 回落路线需要)。 */
|
|
94
|
+
enabled: false,
|
|
95
|
+
port: 17_899,
|
|
96
|
+
}),
|
|
97
|
+
/** 是否注册 `zcode_start_plan_status` 工具。 */
|
|
98
|
+
statusTool: true,
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 深合并默认值(只做一层嵌套:够用且不易写错)。
|
|
103
|
+
*
|
|
104
|
+
* ⚠ `models` 只在**未写**(undefined/null)时补默认值;写了但类型不对
|
|
105
|
+
* (例如 `models: "GLM"`)必须原样保留,让 `validateConfig` 报错 ——
|
|
106
|
+
* 若在这里悄悄替换成默认值,非法配置就会被静默接受。
|
|
107
|
+
*/
|
|
108
|
+
export function resolveConfig(raw) {
|
|
109
|
+
const src = raw !== null && typeof raw === 'object' ? raw : {}
|
|
110
|
+
const out = { ...DEFAULT_CONFIG, ...src }
|
|
111
|
+
out.captcha = { ...DEFAULT_CONFIG.captcha, ...(src.captcha ?? {}) }
|
|
112
|
+
out.endpoint = { ...DEFAULT_CONFIG.endpoint, ...(src.endpoint ?? {}) }
|
|
113
|
+
if (src.models === undefined || src.models === null) out.models = [...DEFAULT_CONFIG.models]
|
|
114
|
+
else if (Array.isArray(src.models)) out.models = src.models.length > 0 ? src.models.slice() : [...DEFAULT_CONFIG.models]
|
|
115
|
+
/** 0.3.0 的 `pinnedAccounts` 同义更名:旧名仍被接受,新名优先。 */
|
|
116
|
+
if (src.accountProviders === undefined && typeof src.pinnedAccounts === 'boolean') {
|
|
117
|
+
out.accountProviders = src.pinnedAccounts
|
|
118
|
+
}
|
|
119
|
+
return out
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Config 契约:**零依赖的 schemastery 兼容 schema**。
|
|
124
|
+
*
|
|
125
|
+
* ## 为什么必须"长得像 schemastery"
|
|
126
|
+
*
|
|
127
|
+
* DSH 用 `isNativeConfigSchema(config)` 判定一行能否进配置目录:
|
|
128
|
+
* `Symbol.for('schemastery') === true` + string `type` + object `meta`。
|
|
129
|
+
* 不属于 native 就得到 `status: 'unsupported'`,于是设置页不可编辑、
|
|
130
|
+
* **provider 也进不了模型选择器**(选择器按配置目录解析 provider)。
|
|
131
|
+
*
|
|
132
|
+
* ## 为什么不直接 `import '@deepseek-ai/schemastery'`
|
|
133
|
+
*
|
|
134
|
+
* `plugin_manager` 用**软链**安装 bundle(`link:`),插件的 `index.js` 实际运行在
|
|
135
|
+
* 源码目录,那里解析不到 profile 的 `node_modules` —— 一旦顶层静态 import 就
|
|
136
|
+
* `ERR_MODULE_NOT_FOUND`,模块加载失败,`apply` 永不执行:表现为
|
|
137
|
+
* "行状态 schema、插件列表可见,但工具与模型全都没有"(本仓库真实踩过)。
|
|
138
|
+
*
|
|
139
|
+
* 官方标准也明确:Host-only bundle 不需要运行时依赖。
|
|
140
|
+
* 故这里手写一个**最小兼容对象**:属性名与判据完全一致,求值语义与 schemastery
|
|
141
|
+
* 的 `Schema(...)` 调用一致(返回补全默认值后的配置,非法值抛错)。
|
|
142
|
+
*/
|
|
143
|
+
const SCHEMASTERNY_SYMBOL = Symbol.for('schemastery')
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 构造一个 schemastery 兼容的 object schema。
|
|
147
|
+
*
|
|
148
|
+
* @param {(value: any) => any} validate 校验并补全默认值;非法值应抛错
|
|
149
|
+
* @param {Record<string, any>} fields 字段名 → 该字段的默认值(供文档/投影使用)
|
|
150
|
+
*/
|
|
151
|
+
function makeConfigSchema(validate, fields) {
|
|
152
|
+
const schema = (value) => validate(value ?? {})
|
|
153
|
+
Object.defineProperty(schema, SCHEMASTERNY_SYMBOL, { value: true, enumerable: false })
|
|
154
|
+
Object.defineProperty(schema, 'type', { value: 'object', enumerable: true })
|
|
155
|
+
Object.defineProperty(schema, 'meta', { value: { default: {}, ...fields }, enumerable: true })
|
|
156
|
+
/** Standard Schema 兼容层:让不认识 schemastery 的消费者也能校验。 */
|
|
157
|
+
Object.defineProperty(schema, '~standard', {
|
|
158
|
+
value: {
|
|
159
|
+
version: 1,
|
|
160
|
+
vendor: 'dsh-zcode-startplan',
|
|
161
|
+
validate: (value) => {
|
|
162
|
+
try {
|
|
163
|
+
return { value: validate(value ?? {}) }
|
|
164
|
+
} catch (error) {
|
|
165
|
+
return { issues: [{ message: error instanceof Error ? error.message : String(error) }] }
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
enumerable: false,
|
|
170
|
+
})
|
|
171
|
+
return schema
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** 单个 `channel` 值是否合法(与运行时归一化共用同一张表)。 */
|
|
175
|
+
const isChannelValue = (value) => {
|
|
176
|
+
const raw = String(value ?? '').trim().toLowerCase()
|
|
177
|
+
return raw.length > 0 && Object.values(LOGIN_CHANNELS).some(
|
|
178
|
+
(entry) => entry.provider === raw || entry.aliases.some((alias) => alias.toLowerCase() === raw),
|
|
179
|
+
)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* 校验并补全配置。非法值**抛错**(激活期即失败,不静默降级)。
|
|
184
|
+
*
|
|
185
|
+
* @param {any} value
|
|
186
|
+
*/
|
|
187
|
+
export function validateConfig(value) {
|
|
188
|
+
const src = value === undefined || value === null ? {} : value
|
|
189
|
+
if (typeof src !== 'object' || Array.isArray(src)) throw new Error('config 必须是对象')
|
|
190
|
+
|
|
191
|
+
const out = resolveConfig(src)
|
|
192
|
+
|
|
193
|
+
if (typeof out.provider !== 'string' || out.provider.trim().length === 0) {
|
|
194
|
+
throw new Error('config.provider 必须是非空字符串')
|
|
195
|
+
}
|
|
196
|
+
if (!Array.isArray(out.models) || !out.models.every((id) => typeof id === 'string')) {
|
|
197
|
+
throw new Error('config.models 必须是字符串数组')
|
|
198
|
+
}
|
|
199
|
+
if (typeof out.accountProviders !== 'boolean') {
|
|
200
|
+
throw new Error('config.accountProviders 必须是布尔值')
|
|
201
|
+
}
|
|
202
|
+
if (!Number.isInteger(out.modelCacheMs) || out.modelCacheMs < 0) {
|
|
203
|
+
throw new Error('config.modelCacheMs 必须是非负整数')
|
|
204
|
+
}
|
|
205
|
+
if (!isChannelValue(out.channel)) {
|
|
206
|
+
throw new Error('config.channel 只能是 "cn"(国内智谱)或 "intl"(国际 Z.AI)')
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const { captcha, endpoint } = out
|
|
210
|
+
if (typeof captcha !== 'object' || captcha === null) throw new Error('config.captcha 必须是对象')
|
|
211
|
+
if (captcha.mode !== 'cdp' && captcha.mode !== 'manual') {
|
|
212
|
+
throw new Error('config.captcha.mode 只能是 "cdp" 或 "manual"')
|
|
213
|
+
}
|
|
214
|
+
if (!Number.isInteger(captcha.debugPort) || captcha.debugPort < 1 || captcha.debugPort > 65_535) {
|
|
215
|
+
throw new Error('config.captcha.debugPort 必须是 1-65535 的整数')
|
|
216
|
+
}
|
|
217
|
+
if (typeof captcha.origin !== 'string' || !captcha.origin.startsWith('http')) {
|
|
218
|
+
throw new Error('config.captcha.origin 必须是 http(s) URL')
|
|
219
|
+
}
|
|
220
|
+
if (typeof endpoint !== 'object' || endpoint === null) throw new Error('config.endpoint 必须是对象')
|
|
221
|
+
if (!Number.isInteger(endpoint.port) || endpoint.port < 0 || endpoint.port > 65_535) {
|
|
222
|
+
throw new Error('config.endpoint.port 必须是 0-65535 的整数')
|
|
223
|
+
}
|
|
224
|
+
if (typeof endpoint.enabled !== 'boolean') throw new Error('config.endpoint.enabled 必须是布尔值')
|
|
225
|
+
if (typeof out.statusTool !== 'boolean') throw new Error('config.statusTool 必须是布尔值')
|
|
226
|
+
|
|
227
|
+
return out
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export const Config = makeConfigSchema(validateConfig, {
|
|
231
|
+
provider: PROVIDER,
|
|
232
|
+
displayName: 'ZCode Start Plan(赠送免费 plan)',
|
|
233
|
+
models: [...DEFAULT_MODELS],
|
|
234
|
+
accountProviders: true,
|
|
235
|
+
modelCacheMs: 10 * 60_000,
|
|
236
|
+
dataDir: '',
|
|
237
|
+
channel: 'cn',
|
|
238
|
+
captcha: { mode: 'cdp', debugPort: 9229, origin: 'https://zcode.z.ai/' },
|
|
239
|
+
endpoint: { enabled: false, port: 17_899 },
|
|
240
|
+
statusTool: true,
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Cordis 插件入口。
|
|
245
|
+
*
|
|
246
|
+
* ## ⚠ 宿主的 `ctx` 是**沙箱 façade**,这是本插件最大的坑
|
|
247
|
+
*
|
|
248
|
+
* 宿主只把 `inject` 声明过的服务挂到 ctx 上。实测(一次性诊断插件):
|
|
249
|
+
* `Object.keys(ctx)` 只有 `['fiber']`,未声明的服务访问即抛
|
|
250
|
+
* `cannot get property "tools" without inject`,整个 `apply` 失败 ——
|
|
251
|
+
* 而宿主只把它当一条 warning 打到 stderr(打包态被丢弃),
|
|
252
|
+
* 表现为「插件在列表里、Config 是 schema,但工具和模型全都没有」。
|
|
253
|
+
*
|
|
254
|
+
* 因此:**用到哪个服务就必须在 `inject` 里声明**。本插件用到
|
|
255
|
+
* `llm`(注册适配器)、`tools`(状态工具)、`timer`(定期清理)、
|
|
256
|
+
* `webServer`(账号池管理路由,供插件 Web UI 面板调用)。
|
|
257
|
+
* 后两处都有容错(服务不可用时面板路由跳过 / 图片降级占位文本)。
|
|
258
|
+
* 图片输入的附件服务**刻意不进 inject**(硬依赖会把插件整体挂起),
|
|
259
|
+
* 由 assemble 在建资源时用 `ctx.get('attachments')` 探测。
|
|
260
|
+
*
|
|
261
|
+
* 另外 `ctx.effect` 在沙箱里可用(它不是服务,是 ctx 动词),
|
|
262
|
+
* 而 `ctx.emit` / `ctx.on` / `ctx.setInterval` **不可用** ——
|
|
263
|
+
* 分别改用 `ctx.llm.emitAdaptersUpdated?.()` 与 `ctx.timer.interval`。
|
|
264
|
+
*
|
|
265
|
+
* `webServer`(账号池管理路由)也在 `inject` 里声明了,但注册处做了
|
|
266
|
+
* 容错(`ctx.webServer?.register` 探测 + try/catch 降级):单测假 ctx、
|
|
267
|
+
* 或未来宿主版本缺该服务时,只跳过面板路由,其余功能不受影响。
|
|
268
|
+
*
|
|
269
|
+
* @param {import('@deepseek-ai/cordis').Context} ctx
|
|
270
|
+
* @param {ReturnType<typeof resolveConfig>} rawConfig
|
|
271
|
+
*/
|
|
272
|
+
export function apply(ctx, rawConfig) {
|
|
273
|
+
const config = resolveConfig(rawConfig)
|
|
274
|
+
const log = (message) => {
|
|
275
|
+
try {
|
|
276
|
+
ctx.logger?.info?.(`[zcode-start-plan] ${message}`)
|
|
277
|
+
} catch {
|
|
278
|
+
// logger 不可用时静默(不要让日志把插件弄崩)。
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
const warn = (message) => {
|
|
282
|
+
try {
|
|
283
|
+
ctx.logger?.warn?.(`[zcode-start-plan] ${message}`)
|
|
284
|
+
} catch {
|
|
285
|
+
// 同上。
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const assembled = assemble(ctx, config, { log, warn })
|
|
290
|
+
let adapterRegistered = false
|
|
291
|
+
let disposed = false
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* 异步建资源;就绪后才注册适配器。
|
|
295
|
+
*
|
|
296
|
+
* 官方标准:每个资源都用 `ctx.effect` 注册并交出 cleanup。
|
|
297
|
+
* ① 适配器 / 工具 / 定时器 —— 就绪后在 `ctx.effect` 里注册;
|
|
298
|
+
* ② 账号库 / 端点 / DB 句柄 —— 异步资源,由 `apply` 返回的 cleanup 释放。
|
|
299
|
+
*/
|
|
300
|
+
void assembled
|
|
301
|
+
.init()
|
|
302
|
+
.then(() => {
|
|
303
|
+
if (adapterRegistered || disposed) return
|
|
304
|
+
adapterRegistered = true
|
|
305
|
+
ctx.effect(() => {
|
|
306
|
+
const undo = assembled.register()
|
|
307
|
+
return () => {
|
|
308
|
+
if (typeof undo === 'function') undo()
|
|
309
|
+
}
|
|
310
|
+
})
|
|
311
|
+
/**
|
|
312
|
+
* 拓扑变了:让模型选择器重读注册表。
|
|
313
|
+
* ⚠ 沙箱里没有 `ctx.emit`,改用 `ctx.llm.emitAdaptersUpdated?.()`
|
|
314
|
+
* (`dsh-llm` 自己的发布入口,`workbuddy` 插件也是这么用的)。
|
|
315
|
+
*/
|
|
316
|
+
try {
|
|
317
|
+
ctx.llm.emitAdaptersUpdated?.()
|
|
318
|
+
} catch {
|
|
319
|
+
// 老版本没有这个方法;选择器下次刷新自然会看到。
|
|
320
|
+
}
|
|
321
|
+
})
|
|
322
|
+
.catch((error) => {
|
|
323
|
+
warn(
|
|
324
|
+
`初始化失败(本插件不提供服务,其它模型不受影响):${error instanceof Error ? error.message : String(error)}`,
|
|
325
|
+
)
|
|
326
|
+
})
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* 插件级 cleanup(官方标准:`apply` 返回它)。
|
|
330
|
+
*/
|
|
331
|
+
return async () => {
|
|
332
|
+
disposed = true
|
|
333
|
+
await assembled.dispose()
|
|
334
|
+
}
|
|
335
|
+
}
|