dsh-sub2api 0.2.7 → 0.2.9

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 CHANGED
@@ -1,5 +1,29 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.9
4
+
5
+ - Mount the panel routes by **waiting for `webServer`**, not by sampling it once
6
+ during `apply`. The service is registered by a plugin that loads after this
7
+ one, so `ctx.get("webServer")` was undefined at that moment and every
8
+ `/api/sub2api/*` route was silently skipped — the settings page then showed
9
+ only "HTTP 404:响应不是 JSON". Found by installing the plugin into a real
10
+ DSH profile and watching it 404.
11
+ - Same treatment for `llm` and `webServer` on hosts without `ctx.inject`.
12
+
13
+ ## 0.2.8
14
+
15
+ - The panel no longer depends on the provider registration succeeding. A
16
+ rejected route (a second install, an older host) used to abort `apply`, so the
17
+ browser half rendered while every `/api/sub2api/*` route was missing — the
18
+ settings page showed only "HTTP 404:响应不是 JSON". Registration now runs in a
19
+ nested injection with its own try/catch, and `inject` is empty so the panel
20
+ mounts even when the LLM seam is absent.
21
+ - Say what a 404 means: the panel now explains that the host half did not load,
22
+ lists the usual causes, and offers a retry.
23
+ - Log what mounted: `[sub2api] 面板路由已注册:N 条(vX.Y.Z)`, or the reason it
24
+ did not (missing `webServer`, rejected provider).
25
+ - Fall back to `ctx.get` when a host predates `ctx.inject`.
26
+
3
27
  ## 0.2.7
4
28
 
5
29
  - Retry 5xx and 429, not just dropped connections. The openresty in front of the
package/README.md CHANGED
@@ -262,6 +262,21 @@ llm-sub2api:
262
262
 
263
263
  > 报错里的 URL 显示成 `/v1` 是传输层只上报 base 的结果;插件会把它改写成真正失败的那条路径 `/chat/completions`,并带上重试次数。
264
264
 
265
+ ### 设置页只有一行「HTTP 404:响应不是 JSON」
266
+
267
+ 这是**插件后端没有挂载**:浏览器那一半加载了(所以设置页能渲染),但 `/api/sub2api/*` 一条都不存在,DSH 的 Web 服务器对未知路径返回了 HTML。
268
+
269
+ 从 0.2.8 起,面板会直接说清楚,并给出排查步骤。根因通常是:
270
+
271
+ | 原因 | 怎么确认 |
272
+ |---|---|
273
+ | 装上了但没重启 | 重启 DSH 后再打开 |
274
+ | 插件被禁用 | 插件页确认已启用 |
275
+ | 宿主那一半加载失败 | 看 `~/.dsh/logs/startup-*.log` 里 `sub2api` 的报错 |
276
+ | 同一 profile 装了两份 | `dsh plugin list` 里出现两条 |
277
+
278
+ 从 0.2.8 起,宿主会在启动日志里写一行 `[sub2api] 面板路由已注册:N 条(vX.Y.Z)`——**有这行就说明后端活着**,没有就是没挂载,日志里会同时给出原因(缺 `webServer`、provider 注册被拒等)。
279
+
265
280
  ### `the endpoint answered 502 Bad Gateway: <html>...openresty</html>`
266
281
 
267
282
  这是**网关前面的 openresty 返回的**,不是模型的问题:sub2api 的 Go 后端当时不可用(重启中),或者上游账号被判定不可用正在切换。
package/lib/client.js CHANGED
@@ -34,7 +34,19 @@ window.__ModuleLoader__.load({
34
34
  try {
35
35
  payload = await response.json();
36
36
  } catch {
37
- throw new ApiError("HTTP " + response.status + ":响应不是 JSON");
37
+ // A 404 here means the host half of the plugin never mounted: the
38
+ // browser bundle loads independently, so the panel renders while every
39
+ // route is missing. Say that instead of "响应不是 JSON".
40
+ if (response.status === 404) {
41
+ throw new ApiError(
42
+ "插件后端没有加载:/api/sub2api 返回 404。请确认插件已启用,然后重启 DSH。",
43
+ "host_missing",
44
+ );
45
+ }
46
+ throw new ApiError(
47
+ "HTTP " + response.status + ":响应不是 JSON(可能是网关或代理返回了错误页)",
48
+ "not_json",
49
+ );
38
50
  }
39
51
  if (!payload || payload.ok !== true) {
40
52
  throw new ApiError(
@@ -968,8 +980,21 @@ window.__ModuleLoader__.load({
968
980
  const onRefresh = useCallback(() => refresh(), [refresh]);
969
981
 
970
982
  if (state === null) {
971
- return h("div", { style: { padding: 18, color: C.muted, fontSize: 13 } },
972
- error ? h(Alert, { tone: "error" }, error) : "加载中…");
983
+ if (error) {
984
+ return h("div", { style: { padding: "4px 0 18px" } },
985
+ h(Card, { title: "Sub2API" },
986
+ h(Alert, { tone: "error", title: "面板无法连接插件后端" }, error),
987
+ h("div", { style: { ...T.hint, marginTop: 12 } },
988
+ "常见原因:插件装上了但宿主那一半没有加载(版本不匹配、依赖缺失、或安装后没有重启)。"),
989
+ h("div", { style: { ...T.hint, marginTop: 6 } },
990
+ "排查:在 DSH 终端执行 ", h("code", { style: T.mono }, "dsh plugin list"),
991
+ " 确认已安装;重启 DSH 后再打开这里。"),
992
+ h("div", { style: { marginTop: 12 } },
993
+ h(Button, { onClick: () => refresh() }, "重试")),
994
+ ),
995
+ );
996
+ }
997
+ return h("div", { style: { padding: 18, color: C.muted, fontSize: 13 } }, "加载中…");
973
998
  }
974
999
 
975
1000
  const unread = announcements.filter((item) => !item.is_read);
package/lib/index.js CHANGED
@@ -25,8 +25,14 @@ export { Config } from "./schema.js";
25
25
  /** Stable Cordis plugin name. */
26
26
  export const name = PLUGIN_NAME;
27
27
 
28
- /** Services required before anything can mount. */
29
- export const inject = ["llm"];
28
+ /**
29
+ * Nothing is a hard requirement.
30
+ *
31
+ * The panel is the part a user needs when something is wrong, so it must mount
32
+ * even when the LLM seam is missing or a provider registration is rejected.
33
+ * The provider route is installed later, once `llm` is actually available.
34
+ */
35
+ export const inject = [];
30
36
 
31
37
  /**
32
38
  * The running build's version, read from the package next to the module.
@@ -112,45 +118,76 @@ export function apply(ctx, rawConfig = {}) {
112
118
  logger,
113
119
  });
114
120
 
115
- const adapter = new Sub2ApiAdapter({
116
- ctx,
117
- catalog,
118
- apiBase: () => cfg().apiBase,
119
- apiKeyEnv: () => cfg().apiKeyEnv,
120
- timeoutMs: () => cfg().requestTimeoutMs,
121
- idleTimeoutMs: () => cfg().streamIdleTimeoutMs,
122
- transportRetries: () => cfg().retries,
123
- logger,
124
- });
121
+ const adapter =
122
+ Sub2ApiAdapter === undefined
123
+ ? undefined
124
+ : new Sub2ApiAdapter({
125
+ ctx,
126
+ catalog,
127
+ apiBase: () => cfg().apiBase,
128
+ apiKeyEnv: () => cfg().apiKeyEnv,
129
+ timeoutMs: () => cfg().requestTimeoutMs,
130
+ idleTimeoutMs: () => cfg().streamIdleTimeoutMs,
131
+ transportRetries: () => cfg().retries,
132
+ logger,
133
+ });
125
134
 
126
135
  const deps = { api, session, catalog, adapter, cfg, logger };
127
136
 
128
- // 1) Provider directory + adapter. The directory entry is what the Models
129
- // page turns into a card for this namespace.
137
+ // 1) Provider directory + adapter, installed once the LLM seam exists.
138
+ //
139
+ // This is deliberately a nested injection rather than a top-level
140
+ // `inject`: a rejected registration (a duplicate route from a second
141
+ // install, an older host without the directory API) must not take the
142
+ // panel down with it. Without the panel the user cannot even see why.
130
143
  /** @type {{replace?: (providers: string[]) => void} | undefined} */
131
144
  let adapterHandle;
132
- ctx.effect(() => {
133
- const providers = ctx.llm.registerConfigurableProviders([
134
- {
135
- provider: PROVIDER,
136
- displayName: "Sub2API",
137
- settingsNs: SETTINGS_NS,
138
- settingsPath: [],
139
- },
140
- ]);
141
- const adapters = ctx.llm.registerAdapter([PROVIDER], adapter);
142
- adapterHandle = adapters;
143
- return () => {
144
- adapterHandle = undefined;
145
- adapters?.();
146
- providers?.();
147
- };
148
- }, "sub2api: provider");
145
+ const mountProvider = (llmCtx) => {
146
+ if (adapter === undefined) {
147
+ logger.warn("[sub2api] adapter 未加载,provider 路由未注册(面板仍可用)");
148
+ return;
149
+ }
150
+ llmCtx.effect(() => {
151
+ try {
152
+ const providers = llmCtx.llm.registerConfigurableProviders([
153
+ {
154
+ provider: PROVIDER,
155
+ displayName: "Sub2API",
156
+ settingsNs: SETTINGS_NS,
157
+ settingsPath: [],
158
+ },
159
+ ]);
160
+ const adapters = llmCtx.llm.registerAdapter([PROVIDER], adapter);
161
+ adapterHandle = adapters;
162
+ return () => {
163
+ adapterHandle = undefined;
164
+ adapters?.();
165
+ providers?.();
166
+ };
167
+ } catch (error) {
168
+ logger.warn(
169
+ `[sub2api] provider 注册失败,面板仍可用:${error instanceof Error ? error.message : error}`,
170
+ );
171
+ return () => {};
172
+ }
173
+ }, "sub2api: provider");
174
+ };
175
+
176
+ // `ctx.inject` is the modern way to wait for a service; older hosts only have
177
+ // `ctx.get`, and a missing method here would abort apply and take the panel
178
+ // down with it.
179
+ if (typeof ctx.inject === "function") {
180
+ ctx.inject(["llm"], mountProvider);
181
+ } else if (optional(ctx, "llm") !== undefined) {
182
+ mountProvider(ctx);
183
+ } else {
184
+ logger.warn("[sub2api] 宿主没有 llm 服务,provider 未注册(面板仍可用)");
185
+ }
149
186
 
150
187
  // 2) Model discovery for the Models page's probe button.
151
- ctx.effect(() => {
152
- if (typeof ctx.llm.registerModelDiscovery !== "function") return () => {};
153
- const dispose = ctx.llm.registerModelDiscovery(SETTINGS_NS, async (request, signal) => {
188
+ const mountDiscovery = (llmCtx) => llmCtx.effect(() => {
189
+ if (typeof llmCtx.llm.registerModelDiscovery !== "function") return () => {};
190
+ const dispose = llmCtx.llm.registerModelDiscovery(SETTINGS_NS, async (request, signal) => {
154
191
  // A draft probe carries its own one-shot key and endpoint; a configured
155
192
  // route falls back to the stored credential and this plugin's apiBase.
156
193
  const key = request?.apiKey ?? (await session.apiKey());
@@ -168,10 +205,20 @@ export function apply(ctx, rawConfig = {}) {
168
205
  return () => dispose?.();
169
206
  }, "sub2api: discovery");
170
207
 
208
+ if (typeof ctx.inject === "function") ctx.inject(["llm"], mountDiscovery);
209
+ else if (optional(ctx, "llm") !== undefined) mountDiscovery(ctx);
210
+
171
211
  // 3) Browser routes.
172
- ctx.effect(() => {
173
- const webServer = optional(ctx, "webServer");
174
- if (webServer === undefined || typeof webServer.register !== "function") return () => {};
212
+ //
213
+ // `webServer` is registered by another plugin, so at this moment it may not
214
+ // exist yet: reading it once here is why a correctly installed plugin could
215
+ // answer 404. `ctx.inject` waits for the service before mounting.
216
+ const mountRoutes = (webCtx) => webCtx.effect(() => {
217
+ const webServer = webCtx.webServer;
218
+ if (webServer === undefined || typeof webServer.register !== "function") {
219
+ logger.warn("[sub2api] 宿主的 webServer 不可用,面板路由未注册(设置页会显示 404)");
220
+ return () => {};
221
+ }
175
222
  const routes = makeRoutes({
176
223
  ...deps,
177
224
  cfg,
@@ -186,9 +233,14 @@ export function apply(ctx, rawConfig = {}) {
186
233
  },
187
234
  });
188
235
  const disposers = routes.map((route) => webServer.register(route));
236
+ logger.info(`[sub2api] 面板路由已注册:${routes.length} 条(v${VERSION})`);
189
237
  return () => disposers.forEach((dispose) => dispose?.());
190
238
  }, "sub2api: routes");
191
239
 
240
+ if (typeof ctx.inject === "function") ctx.inject(["webServer"], mountRoutes);
241
+ else if (optional(ctx, "webServer") !== undefined) mountRoutes(ctx);
242
+ else logger.warn("[sub2api] 宿主没有 webServer 服务,面板路由未注册(设置页会显示 404)");
243
+
192
244
  // 4) Slash command for the terminal surface, where the panel does not exist.
193
245
  ctx.effect(() => {
194
246
  const dispose = applyCommands(ctx, { api, session, catalog, cfg });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-sub2api",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "Sub2API provider for DeepSeek Harness: zero-friction signup, auto-provisioned API key, upstream model sync, balance and recharge",
5
5
  "type": "module",
6
6
  "license": "GPL-3.0-only",