dsh-cliproxyapi-pro 1.9.0 → 1.9.1
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 +15 -0
- package/README.en.md +23 -407
- package/README.md +23 -407
- package/lib/client.js +46 -20
- package/lib/host/accounts.js +176 -0
- package/lib/host/apply/agent-tool.js +406 -0
- package/lib/host/apply/commands.js +122 -0
- package/lib/host/apply/controller.js +105 -0
- package/lib/host/apply/http-routes.js +273 -0
- package/lib/host/apply/image-jobs.js +171 -0
- package/lib/host/apply/index.js +41 -0
- package/lib/host/apply/interpret.js +72 -0
- package/lib/host/apply/lifecycle.js +34 -0
- package/lib/host/apply/media-failures.js +68 -0
- package/lib/host/apply/media-service.js +202 -0
- package/lib/host/apply/quota-service.js +42 -0
- package/lib/host/apply/references.js +43 -0
- package/lib/host/apply/session-status.js +104 -0
- package/lib/host/apply/state.js +78 -0
- package/lib/host/apply/updates.js +55 -0
- package/lib/host/apply/video-command.js +152 -0
- package/lib/host/apply/video-edit.js +253 -0
- package/lib/host/apply/video-jobs.js +206 -0
- package/lib/host/apply-utils.js +39 -0
- package/lib/host/config.js +205 -0
- package/lib/host/controller.js +435 -0
- package/lib/host/discovery.js +295 -0
- package/lib/host/http-gate.js +144 -0
- package/lib/host/models.js +102 -0
- package/lib/host/mount-guard.js +56 -0
- package/lib/host/routes.js +165 -0
- package/lib/host/runtime.js +445 -0
- package/lib/host/state-store.js +39 -0
- package/lib/host/status-text.js +93 -0
- package/lib/index.js +17 -4417
- package/lib/media/agent-tool.js +255 -0
- package/lib/media/choice.js +83 -0
- package/lib/media/classify.js +65 -0
- package/lib/media/codec/fit.js +76 -0
- package/lib/media/codec/jpeg.js +158 -0
- package/lib/media/codec/png.js +102 -0
- package/lib/media/commands/parsing.js +269 -0
- package/lib/media/constants.js +46 -0
- package/lib/media/diagnose.js +134 -0
- package/lib/media/images.js +165 -0
- package/lib/media/index.js +29 -0
- package/lib/media/interpret.js +126 -0
- package/lib/media/job-store.js +99 -0
- package/lib/media/mp4.js +44 -0
- package/lib/media/output-files.js +59 -0
- package/lib/media/session-store.js +293 -0
- package/lib/media/settings.js +255 -0
- package/lib/media/util.js +45 -0
- package/lib/media/video.js +326 -0
- package/lib/quota/auth-state.js +33 -0
- package/lib/quota/constants.js +86 -0
- package/lib/quota/controller.js +467 -0
- package/lib/quota/helpers.js +325 -0
- package/lib/quota/index.js +19 -0
- package/lib/quota/management.js +216 -0
- package/lib/quota/matching.js +143 -0
- package/lib/quota/parsers.js +352 -0
- package/lib/quota/probes/billing.js +80 -0
- package/lib/quota/probes/google.js +120 -0
- package/lib/quota/probes/oauth.js +27 -0
- package/lib/quota/probes/plugin.js +18 -0
- package/lib/updater/constants.js +23 -0
- package/lib/updater/errors.js +8 -0
- package/lib/updater/index.js +11 -0
- package/lib/updater/registry.js +83 -0
- package/lib/updater/settings.js +45 -0
- package/lib/updater/store.js +23 -0
- package/lib/updater/updater.js +148 -0
- package/lib/updater/verify.js +110 -0
- package/lib/updater/version.js +31 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +6 -9
- package/lib/media.js +0 -2542
- package/lib/quota.js +0 -1748
- package/lib/updater.js +0 -443
package/lib/client.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
window.__ModuleLoader__.load({
|
|
2
2
|
id: "dsh-cliproxyapi-pro",
|
|
3
3
|
factory: (require) => {
|
|
4
|
+
// GENERATED from src/client by scripts/build-client.mjs - edit the sources, then run npm run build:client
|
|
4
5
|
var module = { exports: {} };
|
|
5
6
|
var exports = module.exports;
|
|
6
7
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
@@ -267,6 +268,9 @@ window.__ModuleLoader__.load({
|
|
|
267
268
|
qKeyStatusSet: "已配置",
|
|
268
269
|
qKeyStatusUnset: "未配置",
|
|
269
270
|
qDataLabel: "额度数据",
|
|
271
|
+
qRefreshPolicy: "默认每 30 分钟自动刷新。账号刷新失败会暂停整个账号;单个凭证额度查询失败只暂停该凭证。手动刷新账号或保存管理密钥可重试。暂停仅保存在插件内存中,插件重启后清除,刷新页面不会清除。",
|
|
272
|
+
qAccountPaused: "此账号自动刷新已暂停。请手动刷新账号或保存管理密钥以重试;暂停保存在插件内存中,插件重启后清除,刷新页面不会清除。",
|
|
273
|
+
qCredentialPaused: "此凭证自动刷新已暂停,其他凭证不受影响。请手动刷新账号以重试;暂停保存在插件内存中,插件重启后清除,刷新页面不会清除。",
|
|
270
274
|
qTotalCreds: "{n} 个凭证",
|
|
271
275
|
qKeySaved: "管理密钥已保存",
|
|
272
276
|
qKeyCleared: "管理密钥已清除",
|
|
@@ -769,6 +773,9 @@ window.__ModuleLoader__.load({
|
|
|
769
773
|
qKeyStatusSet: "Set",
|
|
770
774
|
qKeyStatusUnset: "Not set",
|
|
771
775
|
qDataLabel: "Quota data",
|
|
776
|
+
qRefreshPolicy: "Automatic refresh defaults to every 30 minutes. An account refresh failure pauses the whole account; a credential quota failure pauses only that credential. Refresh the account manually or save its management key to retry. Pauses live only in plugin memory until plugin restart; reloading the page does not clear them.",
|
|
777
|
+
qAccountPaused: "Automatic refresh is paused for this account. Refresh the account manually or save its management key to retry. The pause lives in plugin memory until plugin restart; reloading the page does not clear it.",
|
|
778
|
+
qCredentialPaused: "Automatic refresh is paused for this credential; other credentials are unaffected. Refresh the account manually to retry. The pause lives in plugin memory until plugin restart; reloading the page does not clear it.",
|
|
772
779
|
qTotalCreds: "{n} credentials",
|
|
773
780
|
qKeySaved: "Management key saved",
|
|
774
781
|
qKeyCleared: "Management key cleared",
|
|
@@ -5422,6 +5429,7 @@ select.dcpa-q-input.dcpa-media-kind { flex: none; width: auto; height: 28px; fon
|
|
|
5422
5429
|
let timer = null;
|
|
5423
5430
|
let disposed = false;
|
|
5424
5431
|
let seq = 0;
|
|
5432
|
+
let retryDelay = 0;
|
|
5425
5433
|
const inflight = new Set();
|
|
5426
5434
|
|
|
5427
5435
|
const emit = (patch) => {
|
|
@@ -5452,7 +5460,8 @@ select.dcpa-q-input.dcpa-media-kind { flex: none; width: auto; height: 28px; fon
|
|
|
5452
5460
|
if (timer !== null) clearT(timer);
|
|
5453
5461
|
timer = null;
|
|
5454
5462
|
if (disposed || listeners.size === 0) return;
|
|
5455
|
-
|
|
5463
|
+
// A stale "refreshing" snapshot must not turn failed status reads into a 4 s loop.
|
|
5464
|
+
const delay = retryDelay || (state.data?.refreshing ? QUOTA_BUSY_POLL_MS : QUOTA_POLL_MS);
|
|
5456
5465
|
timer = setT(() => {
|
|
5457
5466
|
timer = null;
|
|
5458
5467
|
void load().finally(schedule);
|
|
@@ -5460,18 +5469,26 @@ select.dcpa-q-input.dcpa-media-kind { flex: none; width: auto; height: 28px; fon
|
|
|
5460
5469
|
};
|
|
5461
5470
|
|
|
5462
5471
|
const load = async () => {
|
|
5472
|
+
if (disposed) return;
|
|
5463
5473
|
const mine = ++seq;
|
|
5464
5474
|
emit({ loading: true });
|
|
5465
5475
|
try {
|
|
5466
5476
|
const body = await request(API.quotaStatus);
|
|
5467
|
-
if (mine === seq)
|
|
5477
|
+
if (!disposed && mine === seq) {
|
|
5478
|
+
retryDelay = 0;
|
|
5479
|
+
emit({ data: body.quota ?? null, error: null, loading: false, loadedAt: Date.now() });
|
|
5480
|
+
}
|
|
5468
5481
|
} catch (error) {
|
|
5469
5482
|
if (disposed) return;
|
|
5470
|
-
if (mine === seq)
|
|
5483
|
+
if (mine === seq) {
|
|
5484
|
+
retryDelay = retryDelay ? Math.min(retryDelay * 2, 300 * 1000) : QUOTA_POLL_MS;
|
|
5485
|
+
emit({ error: error instanceof Error ? error.message : String(error), loading: false });
|
|
5486
|
+
}
|
|
5471
5487
|
}
|
|
5472
5488
|
};
|
|
5473
5489
|
|
|
5474
5490
|
const mutate = async (kind, url, payload) => {
|
|
5491
|
+
if (disposed) return { ok: false };
|
|
5475
5492
|
const mine = ++seq;
|
|
5476
5493
|
emit({ busy: kind, busyAccount: payload?.accountId ?? null, error: null });
|
|
5477
5494
|
try {
|
|
@@ -5480,7 +5497,10 @@ select.dcpa-q-input.dcpa-media-kind { flex: none; width: auto; height: 28px; fon
|
|
|
5480
5497
|
headers: { "content-type": "application/json" },
|
|
5481
5498
|
body: JSON.stringify(payload ?? {}),
|
|
5482
5499
|
});
|
|
5483
|
-
if (mine === seq)
|
|
5500
|
+
if (!disposed && mine === seq) {
|
|
5501
|
+
retryDelay = 0;
|
|
5502
|
+
emit({ data: body.quota ?? state.data, error: null, loading: false, loadedAt: Date.now() });
|
|
5503
|
+
}
|
|
5484
5504
|
emit({ busy: null, busyAccount: null });
|
|
5485
5505
|
schedule();
|
|
5486
5506
|
return { ok: true };
|
|
@@ -5494,6 +5514,7 @@ select.dcpa-q-input.dcpa-media-kind { flex: none; width: auto; height: 28px; fon
|
|
|
5494
5514
|
return {
|
|
5495
5515
|
getSnapshot: () => state,
|
|
5496
5516
|
subscribe(fn) {
|
|
5517
|
+
if (disposed) return () => {};
|
|
5497
5518
|
listeners.add(fn);
|
|
5498
5519
|
if (listeners.size === 1) {
|
|
5499
5520
|
if (state.data === null || Date.now() - state.loadedAt > QUOTA_POLL_MS) void load().finally(schedule);
|
|
@@ -5705,20 +5726,21 @@ select.dcpa-q-input.dcpa-media-kind { flex: none; width: auto; height: 28px; fon
|
|
|
5705
5726
|
}
|
|
5706
5727
|
|
|
5707
5728
|
function CredentialWindows({ t, cred, windows, now }) {
|
|
5708
|
-
|
|
5709
|
-
|
|
5710
|
-
"div",
|
|
5711
|
-
{ className: "dcpa-q-meters" },
|
|
5712
|
-
windows.map((w, i) => h(QuotaMeter, { key: w.id ?? i, t, w, now }))
|
|
5713
|
-
);
|
|
5714
|
-
}
|
|
5729
|
+
const failed = cred.quotaState === "error";
|
|
5730
|
+
const paused = cred.autoRefreshPaused === true;
|
|
5715
5731
|
const lines = [
|
|
5716
|
-
|
|
5717
|
-
? h("p", { key: "q", className: "dcpa-q-note", "data-tone": "error" },
|
|
5718
|
-
|
|
5719
|
-
|
|
5732
|
+
failed || paused
|
|
5733
|
+
? h("p", { key: "q", className: "dcpa-q-note", "data-tone": "error" },
|
|
5734
|
+
failed ? t("qQuotaError") : null,
|
|
5735
|
+
paused ? `${failed ? " · " : ""}${t("qCredentialPaused")}` : null)
|
|
5736
|
+
: windows.length === 0
|
|
5737
|
+
? h("p", { key: "q", className: "dcpa-q-note" }, t(cred.quotaState === "unsupported" ? "qQuotaUnsupported" : "qQuotaNone"))
|
|
5738
|
+
: null,
|
|
5739
|
+
windows.length === 0 && cred.recent ? h("p", { key: "r", className: "dcpa-q-note" }, fmt(t, "qRecent", cred.recent)) : null,
|
|
5720
5740
|
];
|
|
5721
|
-
return h("div", { className: "dcpa-q-meters" },
|
|
5741
|
+
return h("div", { className: "dcpa-q-meters" },
|
|
5742
|
+
windows.map((w, i) => h(QuotaMeter, { key: w.id ?? i, t, w, now })),
|
|
5743
|
+
lines.some(Boolean) ? h("div", null, lines) : null);
|
|
5722
5744
|
}
|
|
5723
5745
|
|
|
5724
5746
|
function CredentialRow({ t, cred, now, pickerIds }) {
|
|
@@ -6003,7 +6025,7 @@ select.dcpa-q-input.dcpa-media-kind { flex: none; width: auto; height: 28px; fon
|
|
|
6003
6025
|
h(
|
|
6004
6026
|
"div",
|
|
6005
6027
|
{ className: "dcpa-q-row-main" },
|
|
6006
|
-
h("div", { className: "dcpa-q-title" }, t("qDataLabel")),
|
|
6028
|
+
h("div", { className: "dcpa-q-title" }, t("qDataLabel"), h(InfoTip, { t, text: t("qRefreshPolicy") })),
|
|
6007
6029
|
h(
|
|
6008
6030
|
"div",
|
|
6009
6031
|
{ className: "dcpa-q-sub" },
|
|
@@ -6012,7 +6034,8 @@ select.dcpa-q-input.dcpa-media-kind { flex: none; width: auto; height: 28px; fon
|
|
|
6012
6034
|
: account.fetchedAt
|
|
6013
6035
|
? fmt(t, "qUpdated", { time: agoText(t, account.fetchedAt, now) })
|
|
6014
6036
|
: t("qNeverUpdated"),
|
|
6015
|
-
account.enabled === false ? ` · ${t("qAccountDisabled")}` : ""
|
|
6037
|
+
account.enabled === false ? ` · ${t("qAccountDisabled")}` : "",
|
|
6038
|
+
account.autoRefreshPaused === true ? ` · ${t("qAccountPaused")}` : ""
|
|
6016
6039
|
)
|
|
6017
6040
|
),
|
|
6018
6041
|
h(
|
|
@@ -6203,7 +6226,9 @@ select.dcpa-q-input.dcpa-media-kind { flex: none; width: auto; height: 28px; fon
|
|
|
6203
6226
|
h(
|
|
6204
6227
|
"div",
|
|
6205
6228
|
{ className: "dcpa-q-pop-foot" },
|
|
6206
|
-
h("span", null,
|
|
6229
|
+
h("span", null,
|
|
6230
|
+
account.fetchedAt ? fmt(t, "qUpdated", { time: agoText(t, account.fetchedAt, now) }) : t("qPeekFooter"),
|
|
6231
|
+
account.autoRefreshPaused === true ? ` · ${t("qAccountPaused")}` : ""),
|
|
6207
6232
|
h(
|
|
6208
6233
|
"button",
|
|
6209
6234
|
{
|
|
@@ -6644,7 +6669,7 @@ select.dcpa-q-input.dcpa-media-kind { flex: none; width: auto; height: 28px; fon
|
|
|
6644
6669
|
|
|
6645
6670
|
// #region generate_media tool rows (tool.call.toolview, key "generate_media")
|
|
6646
6671
|
|
|
6647
|
-
/** The run id the host derives from a tool call id (mirrors lib/media.js
|
|
6672
|
+
/** The run id the host derives from a tool call id (mirrors toolRunId in lib/media/agent-tool.js). */
|
|
6648
6673
|
function toolRunId(callId) {
|
|
6649
6674
|
const raw = String(callId ?? "").replace(/[^A-Za-z0-9._:-]/g, "_").replace(/^[^A-Za-z0-9]+/, "").slice(0, 119);
|
|
6650
6675
|
return /^[A-Za-z0-9][A-Za-z0-9._:-]{0,119}$/.test(raw) ? raw : null;
|
|
@@ -7475,6 +7500,7 @@ select.dcpa-q-input.dcpa-media-kind { flex: none; width: auto; height: 28px; fon
|
|
|
7475
7500
|
);
|
|
7476
7501
|
}
|
|
7477
7502
|
// #endregion
|
|
7503
|
+
|
|
7478
7504
|
const inject = ["slots", "locale"];
|
|
7479
7505
|
|
|
7480
7506
|
function apply(ctx) {
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// Persisted account normalization, migration, conflicts and account patches.
|
|
2
|
+
|
|
3
|
+
import { DEFAULT_ACCOUNT_ID, DEFAULT_PROVIDER, DEFAULT_DISPLAY_NAME, DEFAULT_API_KEY_ENV, STATE_VERSION, ACCOUNT_FIELDS, GLOBAL_FIELDS, normalizeStringList, normalizeModelOverrides, resolveConfig, normalizeAccountId } from "./config.js";
|
|
4
|
+
import { managedRouteKeys } from "./routes.js";
|
|
5
|
+
|
|
6
|
+
/** The persisted shape of one account: its settings without the globals. */
|
|
7
|
+
export function pickAccount(resolved) {
|
|
8
|
+
return {
|
|
9
|
+
id: resolved.id,
|
|
10
|
+
enabled: resolved.enabled,
|
|
11
|
+
provider: resolved.provider,
|
|
12
|
+
displayName: resolved.displayName,
|
|
13
|
+
baseURL: resolved.baseURL,
|
|
14
|
+
apiKeyEnv: resolved.apiKeyEnv,
|
|
15
|
+
protocol: resolved.protocol,
|
|
16
|
+
protocolMode: resolved.protocolMode,
|
|
17
|
+
includePrefix: resolved.includePrefix,
|
|
18
|
+
excludePrefix: resolved.excludePrefix,
|
|
19
|
+
...(resolved.selectedModels === undefined ? {} : { selectedModels: resolved.selectedModels }),
|
|
20
|
+
modelOverrides: resolved.modelOverrides,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function pickFields(source, fields) {
|
|
25
|
+
const out = {};
|
|
26
|
+
if (typeof source !== "object" || source === null) return out;
|
|
27
|
+
for (const field of fields) if (source[field] !== undefined) out[field] = source[field];
|
|
28
|
+
return out;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function plainObject(value) {
|
|
32
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Normalize the state file into `{ version, refreshIntervalSeconds,
|
|
37
|
+
* probeTimeoutMs, accounts[] }`. A legacy single-account file (flat fields,
|
|
38
|
+
* no `accounts` array) becomes one account with id `default`, so its
|
|
39
|
+
* provider route, key reference and model selection carry over unchanged.
|
|
40
|
+
* Invalid or duplicate account ids are replaced with generated ones.
|
|
41
|
+
*/
|
|
42
|
+
export function normalizeState(raw = {}) {
|
|
43
|
+
const source = plainObject(raw) ?? {};
|
|
44
|
+
const globals = resolveConfig(pickFields(source, GLOBAL_FIELDS));
|
|
45
|
+
const rawAccounts = Array.isArray(source.accounts)
|
|
46
|
+
? source.accounts
|
|
47
|
+
: [{ ...pickFields(source, ACCOUNT_FIELDS), id: DEFAULT_ACCOUNT_ID }];
|
|
48
|
+
const accounts = [];
|
|
49
|
+
const ids = new Set();
|
|
50
|
+
for (const entry of rawAccounts) {
|
|
51
|
+
if (plainObject(entry) === undefined) continue;
|
|
52
|
+
let id = normalizeAccountId(entry.id);
|
|
53
|
+
if (id === undefined || ids.has(id)) {
|
|
54
|
+
let n = accounts.length + 1;
|
|
55
|
+
while (ids.has(`account-${n}`)) n += 1;
|
|
56
|
+
id = `account-${n}`;
|
|
57
|
+
}
|
|
58
|
+
ids.add(id);
|
|
59
|
+
accounts.push(pickAccount(resolveConfig({ ...pickFields(entry, ACCOUNT_FIELDS), id })));
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
version: STATE_VERSION,
|
|
63
|
+
refreshIntervalSeconds: globals.refreshIntervalSeconds,
|
|
64
|
+
probeTimeoutMs: globals.probeTimeoutMs,
|
|
65
|
+
accounts,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Overlay the host-supplied plugin config on the persisted state. A config
|
|
71
|
+
* with `accounts` replaces the account list; flat legacy fields apply to the
|
|
72
|
+
* first account, as they did when only one account existed.
|
|
73
|
+
*/
|
|
74
|
+
export function mergeHostConfig(persisted, config) {
|
|
75
|
+
const host = plainObject(config) ?? {};
|
|
76
|
+
const base = plainObject(persisted) ?? {};
|
|
77
|
+
if (!Array.isArray(base.accounts) || Array.isArray(host.accounts)) return { ...base, ...host };
|
|
78
|
+
const flat = pickFields(host, ACCOUNT_FIELDS);
|
|
79
|
+
const accounts = [...base.accounts];
|
|
80
|
+
if (Object.keys(flat).length > 0 && accounts.length > 0) accounts[0] = { ...accounts[0], ...flat };
|
|
81
|
+
return { ...base, ...pickFields(host, GLOBAL_FIELDS), accounts };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Accounts whose managed route keys overlap an earlier account's. Each one
|
|
86
|
+
* maps to the id of the account that already owns the keys.
|
|
87
|
+
*/
|
|
88
|
+
export function accountConflicts(accounts) {
|
|
89
|
+
const owners = new Map();
|
|
90
|
+
const conflicts = new Map();
|
|
91
|
+
for (const account of accounts) {
|
|
92
|
+
const keys = managedRouteKeys(account.provider);
|
|
93
|
+
const clash = keys.map((key) => owners.get(key)).find((owner) => owner !== undefined);
|
|
94
|
+
if (clash !== undefined) {
|
|
95
|
+
conflicts.set(account.id, clash);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
for (const key of keys) owners.set(key, account.id);
|
|
99
|
+
}
|
|
100
|
+
return conflicts;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function assertNoConflicts(accounts) {
|
|
104
|
+
const conflicts = accountConflicts(accounts);
|
|
105
|
+
for (const [id, ownerId] of conflicts) {
|
|
106
|
+
const account = accounts.find((a) => a.id === id);
|
|
107
|
+
const owner = accounts.find((a) => a.id === ownerId);
|
|
108
|
+
throw new Error(
|
|
109
|
+
`dsh-cliproxyapi: provider route "${account?.provider}" of account "${account?.displayName ?? id}" overlaps the routes of account "${owner?.displayName ?? ownerId}". Every account needs its own Provider Route ID.`
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Defaults for a newly added account: the first free `cli-proxy-N` route,
|
|
116
|
+
* its own credential reference, and disabled until the user fills it in.
|
|
117
|
+
* When no account owns `cli-proxy`, the new account takes the classic names.
|
|
118
|
+
*/
|
|
119
|
+
export function newAccountDefaults(accounts) {
|
|
120
|
+
const usedIds = new Set(accounts.map((a) => a.id));
|
|
121
|
+
const usedKeys = new Set(accounts.flatMap((a) => managedRouteKeys(a.provider)));
|
|
122
|
+
const usedRefs = new Set(accounts.map((a) => a.apiKeyEnv));
|
|
123
|
+
const free = (provider) => managedRouteKeys(provider).every((key) => !usedKeys.has(key));
|
|
124
|
+
const base = {
|
|
125
|
+
enabled: false,
|
|
126
|
+
baseURL: "",
|
|
127
|
+
protocol: "openai-completions",
|
|
128
|
+
protocolMode: "auto",
|
|
129
|
+
includePrefix: "",
|
|
130
|
+
excludePrefix: "",
|
|
131
|
+
// No selectedModels: the first successful probe seeds recommendModels().
|
|
132
|
+
modelOverrides: {},
|
|
133
|
+
};
|
|
134
|
+
if (!usedIds.has(DEFAULT_ACCOUNT_ID) && free(DEFAULT_PROVIDER) && !usedRefs.has(DEFAULT_API_KEY_ENV)) {
|
|
135
|
+
return {
|
|
136
|
+
...base,
|
|
137
|
+
id: DEFAULT_ACCOUNT_ID,
|
|
138
|
+
provider: DEFAULT_PROVIDER,
|
|
139
|
+
displayName: DEFAULT_DISPLAY_NAME,
|
|
140
|
+
apiKeyEnv: DEFAULT_API_KEY_ENV,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
let n = Math.max(2, accounts.length + 1);
|
|
144
|
+
while (
|
|
145
|
+
usedIds.has(`account-${n}`) ||
|
|
146
|
+
!free(`${DEFAULT_PROVIDER}-${n}`) ||
|
|
147
|
+
usedRefs.has(`${DEFAULT_API_KEY_ENV}_${n}`)
|
|
148
|
+
) {
|
|
149
|
+
n += 1;
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
...base,
|
|
153
|
+
id: `account-${n}`,
|
|
154
|
+
provider: `${DEFAULT_PROVIDER}-${n}`,
|
|
155
|
+
displayName: `${DEFAULT_DISPLAY_NAME} ${n}`,
|
|
156
|
+
apiKeyEnv: `${DEFAULT_API_KEY_ENV}_${n}`,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Apply an account patch from the UI. `selectedModels` and
|
|
162
|
+
* `modelOverrides` replace the stored value when present; the id never changes.
|
|
163
|
+
*/
|
|
164
|
+
export function patchAccount(account, patch) {
|
|
165
|
+
const fields = pickFields(patch, ACCOUNT_FIELDS);
|
|
166
|
+
const next = { ...account, ...fields, id: account.id };
|
|
167
|
+
if (patch !== null && typeof patch === "object" && "selectedModels" in patch) {
|
|
168
|
+
next.selectedModels = Array.isArray(patch.selectedModels)
|
|
169
|
+
? normalizeStringList(patch.selectedModels) ?? []
|
|
170
|
+
: [];
|
|
171
|
+
}
|
|
172
|
+
if (patch !== null && typeof patch === "object" && "modelOverrides" in patch) {
|
|
173
|
+
next.modelOverrides = normalizeModelOverrides(patch.modelOverrides);
|
|
174
|
+
}
|
|
175
|
+
return pickAccount(resolveConfig(next));
|
|
176
|
+
}
|