dsh-sub2api 0.1.5 → 0.1.7
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 +24 -0
- package/README.md +28 -0
- package/lib/client.js +116 -34
- package/lib/config.js +3 -1
- package/lib/constants.js +23 -0
- package/lib/index.js +37 -1
- package/lib/routes.js +17 -1
- package/lib/schema.js +2 -1
- package/lib/selection.js +66 -0
- package/lib/sub2api/catalog.js +51 -6
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.7
|
|
4
|
+
|
|
5
|
+
- Always report a context window. The host refuses to switch to a model it
|
|
6
|
+
cannot size ("no context capacity for <route>; configure contextWindow on
|
|
7
|
+
that adapter model"), and `/v1/models` usually omits `context_length` because
|
|
8
|
+
it is the upstream's response passed through. The adapter now resolves one
|
|
9
|
+
through config → upstream → measured table → `defaultContextWindow` (128000),
|
|
10
|
+
and the panel labels where the figure came from.
|
|
11
|
+
- `deepseek-flash` / `deepseek-v4.1-flash` are pinned to 1,048,576 from the
|
|
12
|
+
measurement on this gateway.
|
|
13
|
+
|
|
14
|
+
## 0.1.6
|
|
15
|
+
|
|
16
|
+
- Make the chat model picker see a fresh catalog. It rebuilds itself from
|
|
17
|
+
`llm/adapters-updated`, and the plugin never published one, so a session
|
|
18
|
+
opened before the first sync kept an empty provider until the page reloaded.
|
|
19
|
+
Refreshing the catalog now swaps the adapter route set, which is the
|
|
20
|
+
documented way to announce the change.
|
|
21
|
+
- Add a model allowlist: the panel's 模型 card lists every upstream model with
|
|
22
|
+
checkboxes, stores the choice in `$DSH_HOME/sub2api-selection.json`, and
|
|
23
|
+
`listModels` honours it, so the picker shows exactly what was chosen. All
|
|
24
|
+
boxes ticked means no restriction; the `visibleModels` config still wins when
|
|
25
|
+
an operator sets it.
|
|
26
|
+
|
|
3
27
|
## 0.1.5
|
|
4
28
|
|
|
5
29
|
- Published to npm as `dsh-sub2api`, which is what DSH Desktop's community
|
package/README.md
CHANGED
|
@@ -124,6 +124,34 @@ dsh plugin --profile desktop add dsh-sub2api
|
|
|
124
124
|
- 日志与错误信息不包含 token、密钥或密码。
|
|
125
125
|
- 支付链接里的 `token` 是 sub2api 的访问令牌,只在用户点「去支付」时生成,不写日志、不落盘。
|
|
126
126
|
|
|
127
|
+
## 上下文窗口
|
|
128
|
+
|
|
129
|
+
DSH 在切换模型前要算上下文容量,拿不到就直接拒绝:
|
|
130
|
+
|
|
131
|
+
```
|
|
132
|
+
Cannot switch to sub2api/...: no context capacity; configure contextWindow on that adapter model
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
`/v1/models` 透传的是上游响应,多数上游**不带 `context_length`**,所以插件自己补一个,回退链是:
|
|
136
|
+
|
|
137
|
+
1. 配置里的 `contextWindow`(对所有模型生效)
|
|
138
|
+
2. 上游声明的 `context_length` / `context_window`
|
|
139
|
+
3. 实测表(目前只有 `deepseek-flash` / `deepseek-v4.1-flash` = 1,048,576,来自本站实测)
|
|
140
|
+
4. `defaultContextWindow`,默认 **128000**
|
|
141
|
+
|
|
142
|
+
面板的模型列表里会标出容量来源:`128k · 默认` 表示是兜底值,`1049k · 实测` 表示来自实测表,没有标记表示上游自己声明的。
|
|
143
|
+
|
|
144
|
+
> 兜底值只是让对话能跑起来。如果某个模型实际容量更大,把它写进配置的 `contextWindow`,或者告诉我加进实测表——填大了上游会 400,填小了会提前压缩。
|
|
145
|
+
|
|
146
|
+
## 选择对话里显示哪些模型
|
|
147
|
+
|
|
148
|
+
上游一个中转站往往有几十个模型,全部塞进选择器既难找也容易选错。面板的「模型 → 选择模型」里可以勾选要用的那几个:
|
|
149
|
+
|
|
150
|
+
- 勾选结果存在 `$DSH_HOME/sub2api-selection.json`(0600),不写进设置文档
|
|
151
|
+
- **全部勾选等于不做限制**,等同于没设置
|
|
152
|
+
- 通过 `groupId` 之外的方式固定的话,配置里的 `visibleModels` 优先级更高(适合运营方统一指定)
|
|
153
|
+
- 保存后会立刻通知宿主刷新选择器,不用重开会话
|
|
154
|
+
|
|
127
155
|
## 密钥建在哪个分组
|
|
128
156
|
|
|
129
157
|
网关会拒绝**没有分组的密钥**(`API key is not assigned to a group`),所以自动选择必须有确定结果:
|
package/lib/client.js
CHANGED
|
@@ -499,11 +499,47 @@ window.__ModuleLoader__.load({
|
|
|
499
499
|
|
|
500
500
|
// ------------------------------------------------------------ models
|
|
501
501
|
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
502
|
+
/**
|
|
503
|
+
* Show the capacity the host will actually use, and say where it came from:
|
|
504
|
+
* a defaulted figure is the difference between "chat works" and "the model
|
|
505
|
+
* cannot be switched to".
|
|
506
|
+
*/
|
|
507
|
+
function contextBadge(model) {
|
|
508
|
+
if (!model || !model.contextWindow) return null;
|
|
509
|
+
const k = Math.round(Number(model.contextWindow) / 1000);
|
|
510
|
+
const source = model.contextSource;
|
|
511
|
+
const label = source === "default" ? "默认" : source === "known" ? "实测" : null;
|
|
512
|
+
return h("span", {
|
|
513
|
+
style: {
|
|
514
|
+
fontSize: 11.5,
|
|
515
|
+
color: source === "default" ? C.warn : C.muted,
|
|
516
|
+
whiteSpace: "nowrap",
|
|
517
|
+
},
|
|
518
|
+
}, k + "k" + (label ? " · " + label : ""));
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
function ModelsCard({ state, busy, onRefreshModels, onReload, onGoRecharge }) {
|
|
522
|
+
const models = state.models || { models: [], all: [], allowed: [], fetchedAt: 0, stale: false, error: null };
|
|
523
|
+
const shown = models.models || [];
|
|
524
|
+
const everything = models.all || [];
|
|
525
|
+
const allowed = models.allowed || [];
|
|
526
|
+
const showingAll = allowed.length === 0;
|
|
527
|
+
|
|
505
528
|
const [open, setOpen] = useState(false);
|
|
506
529
|
const [error, setError] = useState("");
|
|
530
|
+
const [draft, setDraft] = useState(null); // null = untouched
|
|
531
|
+
const [saving, setSaving] = useState(false);
|
|
532
|
+
const [saved, setSaved] = useState(false);
|
|
533
|
+
|
|
534
|
+
const checked = draft ?? (showingAll ? everything.map((m) => m.id) : allowed);
|
|
535
|
+
const isOn = (id) => checked.includes(id);
|
|
536
|
+
const dirty = draft !== null;
|
|
537
|
+
|
|
538
|
+
const toggle = (id) => {
|
|
539
|
+
const next = isOn(id) ? checked.filter((x) => x !== id) : [...checked, id];
|
|
540
|
+
setDraft(next);
|
|
541
|
+
setSaved(false);
|
|
542
|
+
};
|
|
507
543
|
|
|
508
544
|
const refresh = async () => {
|
|
509
545
|
setError("");
|
|
@@ -515,13 +551,34 @@ window.__ModuleLoader__.load({
|
|
|
515
551
|
}
|
|
516
552
|
};
|
|
517
553
|
|
|
554
|
+
const save = async () => {
|
|
555
|
+
setSaving(true);
|
|
556
|
+
setError("");
|
|
557
|
+
setSaved(false);
|
|
558
|
+
try {
|
|
559
|
+
// Every model selected is the same as no restriction.
|
|
560
|
+
const ids = checked.length === everything.length ? [] : checked;
|
|
561
|
+
await call("/models/visible", { method: "POST", body: { ids } });
|
|
562
|
+
// Saving only changes the allowlist, so re-read the panel state
|
|
563
|
+
// instead of asking the upstream for its catalog again.
|
|
564
|
+
await onReload();
|
|
565
|
+
setDraft(null);
|
|
566
|
+
setSaved(true);
|
|
567
|
+
setTimeout(() => setSaved(false), 2000);
|
|
568
|
+
} catch (e) {
|
|
569
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
570
|
+
} finally {
|
|
571
|
+
setSaving(false);
|
|
572
|
+
}
|
|
573
|
+
};
|
|
574
|
+
|
|
518
575
|
const reason = error || models.error?.message || "";
|
|
519
576
|
|
|
520
577
|
return h(Card, {
|
|
521
578
|
title: "模型",
|
|
522
|
-
meta:
|
|
523
|
-
?
|
|
524
|
-
: "0 个
|
|
579
|
+
meta: everything.length > 0
|
|
580
|
+
? `对话里显示 ${showingAll && !dirty ? everything.length : checked.length} / ${everything.length}`
|
|
581
|
+
: "0 个",
|
|
525
582
|
},
|
|
526
583
|
models.needsRecharge
|
|
527
584
|
? h("div", { style: { marginBottom: 12 } },
|
|
@@ -541,43 +598,67 @@ window.__ModuleLoader__.load({
|
|
|
541
598
|
? h("div", { style: { marginBottom: 12 } }, h(Alert, { tone: "error", title: "同步失败" }, reason))
|
|
542
599
|
: null,
|
|
543
600
|
|
|
544
|
-
h("div", { style: T.row },
|
|
601
|
+
h("div", { style: { ...T.row, marginBottom: everything.length > 0 ? 12 : 0 } },
|
|
545
602
|
h(Button, { onClick: refresh, disabled: busy || !state.loggedIn }, busy ? "同步中…" : "刷新模型"),
|
|
546
|
-
h(Button, { onClick: () => setOpen((v) => !v), disabled:
|
|
547
|
-
open ? "
|
|
548
|
-
models.stale &&
|
|
603
|
+
h(Button, { onClick: () => setOpen((v) => !v), disabled: everything.length === 0 },
|
|
604
|
+
open ? "收起选择" : "选择模型"),
|
|
605
|
+
models.stale && everything.length > 0 ? h("span", { style: T.hint }, "缓存已过期") : null,
|
|
549
606
|
),
|
|
550
607
|
|
|
551
|
-
open
|
|
552
|
-
? h("div",
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
608
|
+
open && everything.length > 0
|
|
609
|
+
? h("div", null,
|
|
610
|
+
h("div", {
|
|
611
|
+
style: {
|
|
612
|
+
maxHeight: 260,
|
|
613
|
+
overflow: "auto",
|
|
614
|
+
border: "0.5px solid " + C.line,
|
|
615
|
+
borderRadius: 12,
|
|
616
|
+
},
|
|
559
617
|
},
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
618
|
+
everything.map((model) =>
|
|
619
|
+
h("label", {
|
|
620
|
+
key: model.id,
|
|
621
|
+
style: {
|
|
622
|
+
display: "flex",
|
|
623
|
+
alignItems: "center",
|
|
624
|
+
gap: 8,
|
|
625
|
+
padding: "7px 12px",
|
|
626
|
+
borderTop: "0.5px solid " + C.line,
|
|
627
|
+
cursor: "pointer",
|
|
628
|
+
fontSize: 13,
|
|
629
|
+
},
|
|
570
630
|
},
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
631
|
+
h("input", {
|
|
632
|
+
type: "checkbox",
|
|
633
|
+
checked: isOn(model.id),
|
|
634
|
+
onChange: () => toggle(model.id),
|
|
635
|
+
}),
|
|
636
|
+
h("code", { style: { ...T.mono, flex: "1 1 auto", minWidth: 0, overflow: "hidden", textOverflow: "ellipsis" } }, model.id),
|
|
637
|
+
contextBadge(shown.find((m) => m.id === model.id) ?? model),
|
|
638
|
+
shown.some((m) => m.id === model.id && m.vision) ? h("span", { style: T.hint }, "图片") : null,
|
|
639
|
+
),
|
|
640
|
+
)),
|
|
641
|
+
h("div", { style: { ...T.row, marginTop: 10 } },
|
|
642
|
+
h(Button, { size: "sm", onClick: () => { setDraft(everything.map((m) => m.id)); setSaved(false); } }, "全选"),
|
|
643
|
+
h(Button, { size: "sm", onClick: () => { setDraft([]); setSaved(false); } }, "全不选"),
|
|
644
|
+
h(Button, { size: "sm", onClick: () => { setDraft(null); setSaved(false); } }, "还原"),
|
|
645
|
+
h("div", { style: { marginLeft: "auto", display: "flex", gap: 8, alignItems: "center" } },
|
|
646
|
+
saved ? h("span", { style: { ...T.hint, color: C.ok } }, "已保存") : null,
|
|
647
|
+
h(Button, {
|
|
648
|
+
variant: "primary",
|
|
649
|
+
size: "sm",
|
|
650
|
+
onClick: save,
|
|
651
|
+
disabled: !dirty || saving,
|
|
652
|
+
}, saving ? "保存中…" : "保存选择"),
|
|
575
653
|
),
|
|
576
|
-
)
|
|
654
|
+
),
|
|
655
|
+
)
|
|
577
656
|
: null,
|
|
578
657
|
|
|
579
658
|
h("div", { style: { ...T.hint, marginTop: 10 } },
|
|
580
|
-
|
|
659
|
+
everything.length === 0
|
|
660
|
+
? "目录来自上游 /v1/models,登录后自动同步。"
|
|
661
|
+
: "勾选的模型会出现在对话的模型选择器里;全部勾选等于不做限制。"),
|
|
581
662
|
);
|
|
582
663
|
}
|
|
583
664
|
|
|
@@ -668,6 +749,7 @@ window.__ModuleLoader__.load({
|
|
|
668
749
|
state,
|
|
669
750
|
busy,
|
|
670
751
|
onRefreshModels,
|
|
752
|
+
onReload: refresh,
|
|
671
753
|
onGoRecharge: () => {
|
|
672
754
|
const target = document.getElementById("sub2api-recharge");
|
|
673
755
|
if (target && typeof target.scrollIntoView === "function") {
|
package/lib/config.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
import { join } from "node:path";
|
|
12
12
|
import { homedir } from "node:os";
|
|
13
|
-
import { API_KEY_REF, KEY_NAME, MODELS_CACHE_FILE } from "./constants.js";
|
|
13
|
+
import { API_KEY_REF, KEY_NAME, MODELS_CACHE_FILE, SELECTION_FILE } from "./constants.js";
|
|
14
14
|
import { normalizeBaseUrl } from "./util.js";
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -64,9 +64,11 @@ export function resolveConfig(config) {
|
|
|
64
64
|
modelsCachePath: String(
|
|
65
65
|
field(config, "modelsCachePath", join(dshHome, MODELS_CACHE_FILE)),
|
|
66
66
|
),
|
|
67
|
+
selectionPath: String(field(config, "selectionPath", join(dshHome, SELECTION_FILE))),
|
|
67
68
|
showSidebarQuota: field(config, "showSidebarQuota", false) === true,
|
|
68
69
|
visibleModels: toStringArray(field(config, "visibleModels", [])),
|
|
69
70
|
contextWindow: toPositiveInt(field(config, "contextWindow", undefined)),
|
|
71
|
+
defaultContextWindow: toPositiveInt(field(config, "defaultContextWindow", undefined)),
|
|
70
72
|
autoProvision: field(config, "autoProvision", true) !== false,
|
|
71
73
|
announceToAgent: field(config, "announceToAgent", true) !== false,
|
|
72
74
|
};
|
package/lib/constants.js
CHANGED
|
@@ -31,6 +31,9 @@ export const ROUTE_PREFIX = "/api/sub2api";
|
|
|
31
31
|
/** Default model catalog cache file name under $DSH_HOME. */
|
|
32
32
|
export const MODELS_CACHE_FILE = "sub2api-models.json";
|
|
33
33
|
|
|
34
|
+
/** Where the panel keeps the model allowlist (an empty list means "show all"). */
|
|
35
|
+
export const SELECTION_FILE = "sub2api-selection.json";
|
|
36
|
+
|
|
34
37
|
/** How long a discovered catalog stays fresh before a background refresh. */
|
|
35
38
|
export const CATALOG_TTL_MS = 10 * 60 * 1000;
|
|
36
39
|
|
|
@@ -40,6 +43,26 @@ export const TOKEN_REFRESH_LEAD_MS = 60 * 1000;
|
|
|
40
43
|
/** Default page size when listing API keys. */
|
|
41
44
|
export const KEY_PAGE_SIZE = 100;
|
|
42
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Context window used when neither the endpoint nor the configuration says
|
|
48
|
+
* anything. DSH cannot compact a conversation without a capacity figure and
|
|
49
|
+
* refuses to switch to such a model, so "unknown" is not an option the adapter
|
|
50
|
+
* can afford to return.
|
|
51
|
+
*/
|
|
52
|
+
export const DEFAULT_CONTEXT_WINDOW = 128000;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Context windows measured against this gateway, keyed by model id fragment.
|
|
56
|
+
* Only families with direct evidence belong here: a figure larger than the real
|
|
57
|
+
* limit turns every long request into a 400, and a smaller one compacts early.
|
|
58
|
+
*/
|
|
59
|
+
export const KNOWN_CONTEXT = [
|
|
60
|
+
// Measured on sub.pscly.cc: 897,791 tokens accepted, 1,050,041 rejected with
|
|
61
|
+
// "maximum context length is 1048576 tokens".
|
|
62
|
+
{ match: "deepseek-v4.1-flash", contextWindow: 1048576 },
|
|
63
|
+
{ match: "deepseek-flash", contextWindow: 1048576 },
|
|
64
|
+
];
|
|
65
|
+
|
|
43
66
|
/** Model id fragments that mark an image-capable model when the endpoint says nothing. */
|
|
44
67
|
export const VISION_HINTS = [
|
|
45
68
|
"gpt-4o",
|
package/lib/index.js
CHANGED
|
@@ -15,6 +15,7 @@ import { Sub2ApiAdapter } from "./adapter.js";
|
|
|
15
15
|
import { makeRoutes } from "./routes.js";
|
|
16
16
|
import { Sub2ApiApi } from "./sub2api/api.js";
|
|
17
17
|
import { ModelCatalog } from "./sub2api/catalog.js";
|
|
18
|
+
import { SelectionStore } from "./selection.js";
|
|
18
19
|
import { SessionStore } from "./sub2api/session.js";
|
|
19
20
|
|
|
20
21
|
export { Config } from "./schema.js";
|
|
@@ -56,11 +57,36 @@ export function apply(ctx, rawConfig = {}) {
|
|
|
56
57
|
apiKeyEnv: () => cfg().apiKeyEnv,
|
|
57
58
|
});
|
|
58
59
|
|
|
60
|
+
/** Panel-side model allowlist, kept beside the catalog cache. */
|
|
61
|
+
const selection = new SelectionStore({
|
|
62
|
+
path: () => cfg().selectionPath,
|
|
63
|
+
logger,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Tell the host the catalog changed.
|
|
68
|
+
*
|
|
69
|
+
* The model picker rebuilds itself from `llm/adapters-updated` (and the
|
|
70
|
+
* credential events), not from our HTTP routes. Without this a session that
|
|
71
|
+
* opened before the first sync keeps showing an empty provider until the page
|
|
72
|
+
* is reloaded. `replace()` on the registration handle is the documented way
|
|
73
|
+
* to publish that event.
|
|
74
|
+
*/
|
|
75
|
+
const announceCatalog = () => {
|
|
76
|
+
try {
|
|
77
|
+
adapterHandle?.replace?.([PROVIDER]);
|
|
78
|
+
} catch {
|
|
79
|
+
// A disposed registration is not an error worth failing a refresh over.
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
59
83
|
const catalog = new ModelCatalog({
|
|
60
84
|
api,
|
|
61
85
|
cachePath: () => cfg().modelsCachePath,
|
|
62
86
|
visibleModels: () => cfg().visibleModels,
|
|
87
|
+
selection: () => selection.current(),
|
|
63
88
|
contextWindow: () => cfg().contextWindow,
|
|
89
|
+
defaultContextWindow: () => cfg().defaultContextWindow,
|
|
64
90
|
apiKey: () => session.apiKey(),
|
|
65
91
|
logger,
|
|
66
92
|
});
|
|
@@ -79,6 +105,8 @@ export function apply(ctx, rawConfig = {}) {
|
|
|
79
105
|
|
|
80
106
|
// 1) Provider directory + adapter. The directory entry is what the Models
|
|
81
107
|
// page turns into a card for this namespace.
|
|
108
|
+
/** @type {{replace?: (providers: string[]) => void} | undefined} */
|
|
109
|
+
let adapterHandle;
|
|
82
110
|
ctx.effect(() => {
|
|
83
111
|
const providers = ctx.llm.registerConfigurableProviders([
|
|
84
112
|
{
|
|
@@ -89,7 +117,9 @@ export function apply(ctx, rawConfig = {}) {
|
|
|
89
117
|
},
|
|
90
118
|
]);
|
|
91
119
|
const adapters = ctx.llm.registerAdapter([PROVIDER], adapter);
|
|
120
|
+
adapterHandle = adapters;
|
|
92
121
|
return () => {
|
|
122
|
+
adapterHandle = undefined;
|
|
93
123
|
adapters?.();
|
|
94
124
|
providers?.();
|
|
95
125
|
};
|
|
@@ -123,8 +153,14 @@ export function apply(ctx, rawConfig = {}) {
|
|
|
123
153
|
const routes = makeRoutes({
|
|
124
154
|
...deps,
|
|
125
155
|
cfg,
|
|
156
|
+
selection,
|
|
157
|
+
announceCatalog,
|
|
126
158
|
hostname: () => "dsh",
|
|
127
|
-
refreshCatalog: (key) =>
|
|
159
|
+
refreshCatalog: async (key) => {
|
|
160
|
+
const count = await catalog.refresh(key);
|
|
161
|
+
announceCatalog();
|
|
162
|
+
return count;
|
|
163
|
+
},
|
|
128
164
|
});
|
|
129
165
|
const disposers = routes.map((route) => webServer.register(route));
|
|
130
166
|
return () => disposers.forEach((dispose) => dispose?.());
|
package/lib/routes.js
CHANGED
|
@@ -37,7 +37,7 @@ import {
|
|
|
37
37
|
* @returns {Array<{kind: "exact", path: string, handler: Function}>}
|
|
38
38
|
*/
|
|
39
39
|
export function makeRoutes(deps) {
|
|
40
|
-
const { api, session, catalog, cfg, logger } = deps;
|
|
40
|
+
const { api, session, catalog, cfg, logger, selection, announceCatalog } = deps;
|
|
41
41
|
|
|
42
42
|
/** Orders created by this process, keyed by the pay centre's order number. */
|
|
43
43
|
const orders = new Map();
|
|
@@ -215,9 +215,25 @@ export function makeRoutes(deps) {
|
|
|
215
215
|
status: failure instanceof Sub2ApiError ? failure.status : undefined,
|
|
216
216
|
};
|
|
217
217
|
}
|
|
218
|
+
if (error === undefined) announceCatalog?.();
|
|
218
219
|
return { ok: error === undefined, count, error: error ?? null, models: catalog.snapshot() };
|
|
219
220
|
}),
|
|
220
221
|
|
|
222
|
+
/** Save which models the picker offers. An empty list restores "show all". */
|
|
223
|
+
defineRoute("POST", `${ROUTE_PREFIX}/models/visible`, async (body) => {
|
|
224
|
+
const ids = Array.isArray(body?.ids) ? body.ids.map((id) => String(id)) : [];
|
|
225
|
+
const known = new Set(catalog.snapshot().all.map((model) => model.id));
|
|
226
|
+
const unknown = ids.filter((id) => !known.has(id));
|
|
227
|
+
if (unknown.length > 0) {
|
|
228
|
+
throw new Sub2ApiError(`这些模型不在当前目录里:${unknown.slice(0, 3).join("、")}`, {
|
|
229
|
+
code: "unknown_model",
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
await selection.save(ids);
|
|
233
|
+
announceCatalog?.();
|
|
234
|
+
return { ok: true, ids, models: catalog.snapshot() };
|
|
235
|
+
}),
|
|
236
|
+
|
|
221
237
|
/** Recharge level 1: a signed-in URL the browser opens in a new tab. */
|
|
222
238
|
defineRoute("POST", `${ROUTE_PREFIX}/recharge/link`, async (body) => {
|
|
223
239
|
const stored = await session.read();
|
package/lib/schema.js
CHANGED
|
@@ -30,7 +30,8 @@ export const Config = z.object({
|
|
|
30
30
|
streamIdleTimeoutMs: count("流式请求空闲超时(毫秒)").default(300000),
|
|
31
31
|
modelsCachePath: text("", "模型目录缓存路径;留空使用 $DSH_HOME/sub2api-models.json"),
|
|
32
32
|
visibleModels: z.array(z.string()).description("只在这些模型 id 中显示;留空显示全部").volatile(),
|
|
33
|
-
contextWindow: count("
|
|
33
|
+
contextWindow: count("强制覆盖所有模型的上下文窗口;留空则逐模型解析"),
|
|
34
|
+
defaultContextWindow: count("上游没有声明上下文窗口时使用的兜底值(默认 128000)"),
|
|
34
35
|
showSidebarQuota: z.boolean().default(false).description("在侧边栏显示余额卡片").volatile(),
|
|
35
36
|
autoProvision: z.boolean().default(true).description("登录后自动创建并保存密钥").volatile(),
|
|
36
37
|
announceToAgent: z.boolean().default(true).description("向 agent 注入插件说明").volatile(),
|
package/lib/selection.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which models the user wants in the picker.
|
|
3
|
+
*
|
|
4
|
+
* Configuration can pin a list, but typing model ids into a YAML file is not a
|
|
5
|
+
* reasonable way to choose from forty of them, so the panel writes this small
|
|
6
|
+
* sidecar instead. It is deliberately separate from the settings document: the
|
|
7
|
+
* settings service projects a schema, and a growing list of upstream ids is not
|
|
8
|
+
* a schema.
|
|
9
|
+
* @module dsh-sub2api/selection
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
13
|
+
import { dirname } from "node:path";
|
|
14
|
+
|
|
15
|
+
export class SelectionStore {
|
|
16
|
+
/**
|
|
17
|
+
* @param {object} deps
|
|
18
|
+
* @param {() => string} deps.path file to keep the selection in
|
|
19
|
+
* @param {{warn: Function}} [deps.logger]
|
|
20
|
+
*/
|
|
21
|
+
constructor(deps) {
|
|
22
|
+
this.deps = deps;
|
|
23
|
+
this.logger = deps.logger ?? { warn() {} };
|
|
24
|
+
/** @type {string[] | undefined} */
|
|
25
|
+
this.ids = undefined;
|
|
26
|
+
this.loaded = false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async load() {
|
|
30
|
+
if (this.loaded) return this.current();
|
|
31
|
+
this.loaded = true;
|
|
32
|
+
try {
|
|
33
|
+
const text = await readFile(this.deps.path(), "utf8");
|
|
34
|
+
const parsed = JSON.parse(text);
|
|
35
|
+
if (Array.isArray(parsed?.ids)) {
|
|
36
|
+
this.ids = parsed.ids.filter((id) => typeof id === "string" && id.trim() !== "");
|
|
37
|
+
}
|
|
38
|
+
} catch {
|
|
39
|
+
// No file yet: an empty selection means "show everything".
|
|
40
|
+
}
|
|
41
|
+
return this.current();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The stored ids, synchronously. Empty means no restriction. */
|
|
45
|
+
current() {
|
|
46
|
+
return Array.isArray(this.ids) ? this.ids : [];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {string[]} ids model ids to keep; an empty list restores "show all"
|
|
51
|
+
* @returns {Promise<string[]>}
|
|
52
|
+
*/
|
|
53
|
+
async save(ids) {
|
|
54
|
+
const clean = [...new Set((Array.isArray(ids) ? ids : []).map((id) => String(id).trim()))]
|
|
55
|
+
.filter((id) => id !== "");
|
|
56
|
+
this.ids = clean;
|
|
57
|
+
this.loaded = true;
|
|
58
|
+
try {
|
|
59
|
+
await mkdir(dirname(this.deps.path()), { recursive: true });
|
|
60
|
+
await writeFile(this.deps.path(), JSON.stringify({ ids: clean }, null, 0), { mode: 0o600 });
|
|
61
|
+
} catch (error) {
|
|
62
|
+
this.logger.warn?.(`[sub2api] 模型选择保存失败:${error instanceof Error ? error.message : error}`);
|
|
63
|
+
}
|
|
64
|
+
return clean;
|
|
65
|
+
}
|
|
66
|
+
}
|
package/lib/sub2api/catalog.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
12
12
|
import { dirname } from "node:path";
|
|
13
|
-
import { CATALOG_TTL_MS, VISION_HINTS } from "../constants.js";
|
|
13
|
+
import { CATALOG_TTL_MS, DEFAULT_CONTEXT_WINDOW, KNOWN_CONTEXT, VISION_HINTS } from "../constants.js";
|
|
14
14
|
import { Sub2ApiError } from "../util.js";
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -138,15 +138,46 @@ export class ModelCatalog {
|
|
|
138
138
|
}
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
-
/**
|
|
141
|
+
/**
|
|
142
|
+
* The allowlist in force: the configuration wins when it names anything,
|
|
143
|
+
* otherwise the panel's stored selection applies. Empty means "show all".
|
|
144
|
+
* @returns {string[]}
|
|
145
|
+
*/
|
|
146
|
+
allowed() {
|
|
147
|
+
const configured = this.deps.visibleModels() ?? [];
|
|
148
|
+
if (configured.length > 0) return configured;
|
|
149
|
+
return this.deps.selection?.() ?? [];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Models as the picker should see them: allowlist applied and a context
|
|
154
|
+
* window always resolved.
|
|
155
|
+
*
|
|
156
|
+
* A model with no capacity figure is unusable here: the host's compaction
|
|
157
|
+
* refuses to switch to it ("no context capacity for <route>"), so the fall
|
|
158
|
+
* back chain is config → upstream → measured table → conservative default,
|
|
159
|
+
* and `contextSource` records which one answered.
|
|
160
|
+
*/
|
|
142
161
|
visible() {
|
|
143
|
-
const allow = this.
|
|
162
|
+
const allow = this.allowed();
|
|
144
163
|
const forcedContext = this.deps.contextWindow();
|
|
164
|
+
const fallback = this.deps.defaultContextWindow?.() ?? DEFAULT_CONTEXT_WINDOW;
|
|
165
|
+
|
|
145
166
|
return this.models
|
|
146
167
|
.filter((model) => allow.length === 0 || allow.includes(model.id))
|
|
147
|
-
.map((model) =>
|
|
148
|
-
forcedContext
|
|
149
|
-
|
|
168
|
+
.map((model) => {
|
|
169
|
+
if (forcedContext !== undefined) {
|
|
170
|
+
return { ...model, contextWindow: forcedContext, contextSource: "config" };
|
|
171
|
+
}
|
|
172
|
+
if (model.contextWindow !== undefined) {
|
|
173
|
+
return { ...model, contextSource: "upstream" };
|
|
174
|
+
}
|
|
175
|
+
const known = knownContext(model.id);
|
|
176
|
+
if (known !== undefined) {
|
|
177
|
+
return { ...model, contextWindow: known, contextSource: "known" };
|
|
178
|
+
}
|
|
179
|
+
return { ...model, contextWindow: fallback, contextSource: "default" };
|
|
180
|
+
});
|
|
150
181
|
}
|
|
151
182
|
|
|
152
183
|
/**
|
|
@@ -186,10 +217,13 @@ export class ModelCatalog {
|
|
|
186
217
|
stale: this.stale,
|
|
187
218
|
error: this.lastError ?? null,
|
|
188
219
|
needsRecharge: this.lastError?.code === "INSUFFICIENT_BALANCE",
|
|
220
|
+
allowed: this.allowed(),
|
|
221
|
+
all: this.models.map((model) => ({ id: model.id, name: model.name })),
|
|
189
222
|
models: this.visible().map((model) => ({
|
|
190
223
|
id: model.id,
|
|
191
224
|
name: model.name,
|
|
192
225
|
contextWindow: model.contextWindow ?? null,
|
|
226
|
+
contextSource: model.contextSource ?? null,
|
|
193
227
|
maxTokens: model.maxTokens ?? null,
|
|
194
228
|
vision: model.vision === true,
|
|
195
229
|
ownedBy: model.ownedBy ?? null,
|
|
@@ -279,6 +313,17 @@ function normalizeOne(raw, id) {
|
|
|
279
313
|
return record;
|
|
280
314
|
}
|
|
281
315
|
|
|
316
|
+
/**
|
|
317
|
+
* A measured context window for a known family, when the endpoint omits one.
|
|
318
|
+
* @param {string} id
|
|
319
|
+
* @returns {number | undefined}
|
|
320
|
+
*/
|
|
321
|
+
export function knownContext(id) {
|
|
322
|
+
const lower = String(id).toLowerCase();
|
|
323
|
+
const hit = KNOWN_CONTEXT.find((entry) => lower.includes(entry.match));
|
|
324
|
+
return hit?.contextWindow;
|
|
325
|
+
}
|
|
326
|
+
|
|
282
327
|
/** A model id is the only reliable label; strip a provider prefix for display. */
|
|
283
328
|
function displayName(id) {
|
|
284
329
|
const tail = id.includes("/") ? id.slice(id.lastIndexOf("/") + 1) : id;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-sub2api",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
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",
|