dsh-sub2api 0.10.2 → 0.11.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/CHANGELOG.md CHANGED
@@ -1,5 +1,29 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.11.0
4
+
5
+ - **Not being signed in now says so.** A request made without a login used to
6
+ leave the plugin, reach the gateway, and come back as `401 API key is required
7
+ in Authorization header` — a message about a key, for a user whose problem is
8
+ that they never signed in. The adapter refuses before touching the transport
9
+ and answers instead: 还没有登录 dsh-sub2api。打开 设置 → dsh-sub2api,用一个邮箱和密码
10
+ 登录或注册即可。
11
+ - **A reminder on the screen an empty conversation opens on.** While nobody is
12
+ signed in, `conversation.hero.options` shows a single line pointing at
13
+ 设置 → dsh-sub2api. It disappears the moment a login exists.
14
+
15
+ ## 0.10.3
16
+
17
+ - **A request can no longer go out with no key.** When the adapter could not
18
+ reach the host's credential service, the transport treated that as "no key"
19
+ and sent the request without an `Authorization` header; the gateway answered
20
+ `401 API key is required in Authorization header`, which reads as a bad key and
21
+ sends you hunting the wrong problem. The key now also comes from the plugin's
22
+ own session record, and when neither source has one the plugin says so in the
23
+ log instead of failing at the far end.
24
+ - The resolver is a named export so this path is testable: a keyless request and
25
+ a request with a wrong key are indistinguishable from the caller.
26
+
3
27
  ## 0.10.2
4
28
 
5
29
  - **One retry against the current host when the configured one cannot be
package/lib/adapter.js CHANGED
@@ -60,6 +60,7 @@ export class Sub2ApiAdapter extends LlmAdapter {
60
60
  * @param {import("./sub2api/catalog.js").ModelCatalog} deps.catalog
61
61
  * @param {() => string} deps.apiBase
62
62
  * @param {() => string} deps.apiKeyEnv credential reference holding the key
63
+ * @param {() => {apiKey: () => Promise<string | undefined>}} [deps.session] fallback key source
63
64
  * @param {() => number} deps.timeoutMs
64
65
  * @param {() => number} deps.idleTimeoutMs
65
66
  * @param {{warn: Function}} [deps.logger]
@@ -110,7 +111,7 @@ export class Sub2ApiAdapter extends LlmAdapter {
110
111
  this.service = new OpenAiLlmService({
111
112
  config,
112
113
  provider: PROVIDER,
113
- credentials: () => optionalService(deps.ctx, "credentials"),
114
+ credentials: () => resolveCredentials(deps),
114
115
  images: () => optionalService(deps.ctx, "attachments"),
115
116
  });
116
117
  return this.service;
@@ -155,6 +156,18 @@ export class Sub2ApiAdapter extends LlmAdapter {
155
156
  }
156
157
 
157
158
  async *stream(options) {
159
+ // Refuse before touching the transport. A request with no key leaves here,
160
+ // comes back as "401 API key is required in Authorization header", and sends
161
+ // the user looking for a broken key when the real answer is "you are not
162
+ // signed in". Checking here turns that into a sentence they can act on.
163
+ const key = await this.deps.session?.()?.apiKey?.().catch(() => undefined);
164
+ if (typeof key !== "string" || key === "") {
165
+ throw new Sub2ApiError(
166
+ "还没有登录 dsh-sub2api。打开 设置 → dsh-sub2api,用一个邮箱和密码登录或注册即可。",
167
+ { code: "not_logged_in" },
168
+ );
169
+ }
170
+
158
171
  const service = await this.#service();
159
172
  const route = service.routes()[0];
160
173
  // An empty `models` array still yields a route object; without this check
@@ -392,6 +405,42 @@ export function selectedEffort(model, id) {
392
405
  }
393
406
 
394
407
  /** Read an optional host service without letting an absent one abort the call. */
408
+ /**
409
+ * The credential resolver handed to the transport.
410
+ *
411
+ * The host's `credentials` service is the right source, but it is not a hard
412
+ * requirement: the same key also lives in this plugin's own session record. The
413
+ * transport treats a missing resolver as "no key" and sends the request without
414
+ * an `Authorization` header, which the gateway answers with a 401 that reads
415
+ * exactly like a bad key — so falling back to the stored record, and saying so
416
+ * when neither source has anything, is what keeps that failure diagnosable.
417
+ *
418
+ * Exported because this path cannot be exercised from the outside: a request
419
+ * with no key and a request with a wrong key look identical to a caller.
420
+ * @param {object} deps adapter dependencies
421
+ * @returns {any} a resolver, or undefined when no key source exists
422
+ */
423
+ export function resolveCredentials(deps) {
424
+ const service = optionalService(deps.ctx, "credentials");
425
+ if (service !== undefined) return service;
426
+
427
+ const stored = deps.session?.();
428
+ if (stored !== undefined) {
429
+ return {
430
+ resolve: async () => {
431
+ const value = await stored.apiKey().catch(() => undefined);
432
+ return typeof value === "string" && value !== "" ? { value } : undefined;
433
+ },
434
+ set: async () => {},
435
+ };
436
+ }
437
+
438
+ deps.logger?.warn?.(
439
+ "[sub2api] 取不到凭据:ctx.get('credentials') 为空且会话里没有密钥,请求会不带密钥发出",
440
+ );
441
+ return undefined;
442
+ }
443
+
395
444
  function optionalService(ctx, name) {
396
445
  try {
397
446
  return ctx?.get?.(name);
package/lib/client.js CHANGED
@@ -1550,6 +1550,42 @@ window.__ModuleLoader__.load({
1550
1550
  return h("div", { "data-sub2api": "" }, h(Boundary, null, h(SectionInner, null)));
1551
1551
  }
1552
1552
 
1553
+ // ------------------------------------------------------ login nudge
1554
+
1555
+ /**
1556
+ * The line shown on the empty conversation screen while nobody is signed in.
1557
+ *
1558
+ * A user who installs the plugin and starts typing gets a 401; saying why on
1559
+ * the screen they are already looking at is the difference between "this
1560
+ * thing is broken" and "there is one step left". Renders nothing once a
1561
+ * login exists, so it never becomes furniture.
1562
+ */
1563
+ function LoginNudge() {
1564
+ const { state } = useStatus();
1565
+ if (state === null || state.loggedIn === true) return null;
1566
+
1567
+ return h("button", {
1568
+ type: "button",
1569
+ "data-sub2api": "",
1570
+ onClick: () => toastStore.push("打开 设置 → dsh-sub2api 登录或注册", "ok"),
1571
+ style: {
1572
+ display: "inline-flex",
1573
+ alignItems: "center",
1574
+ gap: 8,
1575
+ padding: "8px 14px",
1576
+ borderRadius: 999,
1577
+ border: "0.5px solid " + C.line,
1578
+ background: C.surface,
1579
+ color: C.second,
1580
+ fontSize: 12.5,
1581
+ cursor: "pointer",
1582
+ maxWidth: "100%",
1583
+ },
1584
+ },
1585
+ h("span", { style: { width: 7, height: 7, borderRadius: 999, background: C.warn, flex: "0 0 7px" } }),
1586
+ h("span", null, "还没登录 dsh-sub2api —— 去 设置 → dsh-sub2api 登录后即可使用这里的模型"));
1587
+ }
1588
+
1553
1589
  // ------------------------------------------------------------ badge
1554
1590
 
1555
1591
  /**
@@ -1689,6 +1725,18 @@ window.__ModuleLoader__.load({
1689
1725
  } catch {
1690
1726
  // The sidebar slot is optional.
1691
1727
  }
1728
+ try {
1729
+ // The hero area is what an empty conversation opens on, so the reminder
1730
+ // is already in view when someone starts typing without a login.
1731
+ ctx.slots.inject("conversation.hero.options", () =>
1732
+ ctx.slots.register(
1733
+ { name: "conversation.hero.options", id: "sub2api-login" },
1734
+ LoginNudge,
1735
+ ),
1736
+ );
1737
+ } catch {
1738
+ // Older hosts without the hero slot simply do not show the reminder.
1739
+ }
1692
1740
  try {
1693
1741
  ctx.slots.inject("conversation.input.right", () =>
1694
1742
  ctx.slots.register(
@@ -1705,6 +1753,7 @@ window.__ModuleLoader__.load({
1705
1753
  exports.inject = inject;
1706
1754
  exports.Section = Section;
1707
1755
  exports.BalanceBadge = BalanceBadge;
1756
+ exports.LoginNudge = LoginNudge;
1708
1757
  // Exposed for the load test: pure read/write of the remembered layout.
1709
1758
  exports.__sections = { readOpenSections, DEFAULT_OPEN, OPEN_KEY };
1710
1759
  return module.exports;
package/lib/index.js CHANGED
@@ -127,6 +127,9 @@ export function apply(ctx, rawConfig = {}) {
127
127
  catalog,
128
128
  apiBase: () => cfg().apiBase,
129
129
  apiKeyEnv: () => cfg().apiKeyEnv,
130
+ // The transport prefers the host's credential service; this is the
131
+ // fallback when that service is not reachable from the adapter.
132
+ session: () => session,
130
133
  timeoutMs: () => cfg().requestTimeoutMs,
131
134
  idleTimeoutMs: () => cfg().streamIdleTimeoutMs,
132
135
  transportRetries: () => cfg().retries,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-sub2api",
3
- "version": "0.10.2",
3
+ "version": "0.11.0",
4
4
  "description": "Official Psapi provider for DeepSeek Harness: email sign-in or sign-up, automatic key and model provisioning, one panel for balance and recharge",
5
5
  "type": "module",
6
6
  "license": "GPL-3.0-only",