dsh-cliproxyapi-pro 1.9.0 → 1.10.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 +23 -0
- package/README.en.md +24 -408
- package/README.md +24 -408
- package/lib/client.js +265 -27
- package/lib/host/accounts.js +178 -0
- package/lib/host/apply/agent-tool.js +406 -0
- package/lib/host/apply/commands.js +122 -0
- package/lib/host/apply/controller.js +121 -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 +50 -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 +210 -0
- package/lib/host/controller.js +464 -0
- package/lib/host/discovery.js +301 -0
- package/lib/host/http-gate.js +144 -0
- package/lib/host/model-info.js +363 -0
- package/lib/host/model-names.js +152 -0
- package/lib/host/models.js +75 -0
- package/lib/host/mount-guard.js +56 -0
- package/lib/host/recommend.js +367 -0
- package/lib/host/routes.js +165 -0
- package/lib/host/runtime.js +471 -0
- package/lib/host/state-store.js +39 -0
- package/lib/host/status-text.js +93 -0
- package/lib/index.js +18 -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
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
// One account runtime: discovery, route synchronization and default-model following.
|
|
2
|
+
|
|
3
|
+
import { effectiveKind } from "../media/index.js";
|
|
4
|
+
import { DEFAULT_API_KEY_ENV, PI_AI_NS, DEFAULT_MODEL_NS } from "./config.js";
|
|
5
|
+
import { modelFamily } from "./models.js";
|
|
6
|
+
import { recommendModels } from "./recommend.js";
|
|
7
|
+
import { nativeProtocol, modelProtocol, applyModelOverride, isChatModel, buildPiAiRoutes, managedRouteKeys } from "./routes.js";
|
|
8
|
+
import { discoverCpa } from "./discovery.js";
|
|
9
|
+
import { withOnlineLimits } from "./model-info.js";
|
|
10
|
+
|
|
11
|
+
export function sleep(ms) {
|
|
12
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function sameJson(a, b) {
|
|
16
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Fields this plugin writes; any stale copy is replaced, never merged. */
|
|
20
|
+
export const MANAGED_ROUTE_FIELDS = ["displayName", "api", "baseURL", "models", "apiKeyEnv", "headers"];
|
|
21
|
+
|
|
22
|
+
export function stripManagedFields(route) {
|
|
23
|
+
const out = { ...route };
|
|
24
|
+
for (const field of MANAGED_ROUTE_FIELDS) delete out[field];
|
|
25
|
+
// An empty dict materialized by the schema is not a user choice.
|
|
26
|
+
if (out.compat !== undefined && sameJson(out.compat, { chatTemplateKwargs: {}, chatTemplateArgs: {} })) {
|
|
27
|
+
delete out.compat;
|
|
28
|
+
}
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function mutateSettingsSafe(settings, ns, ops) {
|
|
33
|
+
let lastError;
|
|
34
|
+
for (let attempt = 0; attempt < 6; attempt += 1) {
|
|
35
|
+
try {
|
|
36
|
+
await settings.mutate(ns, ops);
|
|
37
|
+
return;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
lastError = error;
|
|
40
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
41
|
+
if (msg.includes("HMR transactions cannot be nested")) {
|
|
42
|
+
await sleep(300 * (attempt + 1));
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
throw lastError;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function formatModelView(m, selectedSet, config) {
|
|
52
|
+
const overrides = config?.modelOverrides ?? {};
|
|
53
|
+
const override = overrides[m.id];
|
|
54
|
+
const effective = applyModelOverride(m, override);
|
|
55
|
+
const protocol = config === undefined ? nativeProtocol(m) : modelProtocol(m, config);
|
|
56
|
+
return {
|
|
57
|
+
kind: m.kind ?? m.detectedKind ?? "chat",
|
|
58
|
+
protocol,
|
|
59
|
+
nativeProtocol: nativeProtocol(m),
|
|
60
|
+
...(m.ownedBy === undefined ? {} : { ownedBy: m.ownedBy }),
|
|
61
|
+
id: effective.id,
|
|
62
|
+
name: effective.name ?? effective.id,
|
|
63
|
+
family: modelFamily(effective),
|
|
64
|
+
contextWindow: effective.contextWindow ?? 262144,
|
|
65
|
+
maxTokens: effective.maxTokens ?? 64000,
|
|
66
|
+
image: effective.inputModalities.includes("image"),
|
|
67
|
+
reasoning: effective.reasoning,
|
|
68
|
+
reasoningLevels: effective.reasoningLevels,
|
|
69
|
+
selected: selectedSet.has(effective.id),
|
|
70
|
+
...(override === undefined ? {} : { override }),
|
|
71
|
+
discovered: {
|
|
72
|
+
contextWindow: m.contextWindow ?? 262144,
|
|
73
|
+
maxTokens: m.maxTokens ?? 64000,
|
|
74
|
+
image: m.inputModalities.includes("image"),
|
|
75
|
+
reasoningLevels: m.reasoningLevels,
|
|
76
|
+
// Limits the catalog left out, filled from online model info.
|
|
77
|
+
...(Array.isArray(m.onlineLimits) ? { onlineLimits: m.onlineLimits } : {}),
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export class CpaRuntime {
|
|
83
|
+
constructor(options) {
|
|
84
|
+
this.options = options;
|
|
85
|
+
this.activeProvider = undefined;
|
|
86
|
+
this.activeRoutes = undefined;
|
|
87
|
+
this.running = undefined;
|
|
88
|
+
this.rerun = false;
|
|
89
|
+
this.controller = new AbortController();
|
|
90
|
+
this.allDiscoveredModels = [];
|
|
91
|
+
this.syncedModels = [];
|
|
92
|
+
// Whether the last successful discovery could read the plain `/v1/models`
|
|
93
|
+
// listing, which carries models the enriched catalog omits (image models
|
|
94
|
+
// for some servers). A failed read is retried shortly, not in five minutes.
|
|
95
|
+
this.listingOk = true;
|
|
96
|
+
this.listingRetries = 0;
|
|
97
|
+
this.listingRetryTimer = undefined;
|
|
98
|
+
this.currentStatus = {
|
|
99
|
+
state: "idle",
|
|
100
|
+
modelCount: 0,
|
|
101
|
+
totalDiscovered: 0,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Models with their effective `kind` under the account's current kind
|
|
107
|
+
* overrides (read fresh, so an override applies without a new probe).
|
|
108
|
+
*/
|
|
109
|
+
withKinds(models = this.allDiscoveredModels) {
|
|
110
|
+
let overrides = {};
|
|
111
|
+
try {
|
|
112
|
+
overrides = this.options.kindOverrides?.() ?? {};
|
|
113
|
+
} catch {}
|
|
114
|
+
return models.map((m) => ({ ...withOnlineLimits(m, this.lookupInfo(m)), kind: effectiveKind(m, overrides) }));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Online facts about one catalog model (lib/host/model-info.js), when switched on. */
|
|
118
|
+
lookupInfo(model) {
|
|
119
|
+
try {
|
|
120
|
+
return this.options.lookupInfo?.(model);
|
|
121
|
+
} catch {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Discovered chat models (what the picker, recommendations and seeding use). */
|
|
127
|
+
chatModels(models = this.allDiscoveredModels) {
|
|
128
|
+
return this.withKinds(models).filter(isChatModel);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
status() {
|
|
132
|
+
const syncedSet = new Set(this.syncedModels.map((m) => m.id));
|
|
133
|
+
const config = this.options.config();
|
|
134
|
+
const kinded = this.withKinds();
|
|
135
|
+
const byId = new Map(kinded.map((m) => [m.id, m]));
|
|
136
|
+
return {
|
|
137
|
+
...this.currentStatus,
|
|
138
|
+
listingOk: this.listingOk,
|
|
139
|
+
models: this.syncedModels.map((m) => ({
|
|
140
|
+
...formatModelView(byId.get(m.id) ?? m, syncedSet, config),
|
|
141
|
+
route: m.route,
|
|
142
|
+
})),
|
|
143
|
+
allModels: kinded.map((m) => formatModelView(m, syncedSet, config)),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Run one sync. `probe: true` also reads the catalog of a disabled account
|
|
149
|
+
* so its models can be picked before it is enabled; periodic refreshes of a
|
|
150
|
+
* disabled account stay offline.
|
|
151
|
+
*/
|
|
152
|
+
refresh(options = {}) {
|
|
153
|
+
if (options.probe === true) this.probeRequested = true;
|
|
154
|
+
if (this.running !== undefined) {
|
|
155
|
+
this.rerun = true;
|
|
156
|
+
return this.running;
|
|
157
|
+
}
|
|
158
|
+
this.running = this.runLoop().finally(() => {
|
|
159
|
+
this.running = undefined;
|
|
160
|
+
});
|
|
161
|
+
return this.running;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
dispose() {
|
|
165
|
+
this.controller.abort(new Error("dsh-cliproxyapi disposed"));
|
|
166
|
+
if (this.hmrRetryTimer !== undefined) clearTimeout(this.hmrRetryTimer);
|
|
167
|
+
if (this.listingRetryTimer !== undefined) clearTimeout(this.listingRetryTimer);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Stop syncing and remove every route this account wrote. */
|
|
171
|
+
async teardown(provider) {
|
|
172
|
+
this.dispose();
|
|
173
|
+
try {
|
|
174
|
+
await this.running;
|
|
175
|
+
} catch {}
|
|
176
|
+
await this.releaseRoutes([provider, this.activeProvider]);
|
|
177
|
+
this.activeProvider = undefined;
|
|
178
|
+
this.activeRoutes = undefined;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Unset the managed route keys of the given providers that still exist. */
|
|
182
|
+
async releaseRoutes(providers) {
|
|
183
|
+
const reserved = this.reservedKeys();
|
|
184
|
+
const keys = new Set(
|
|
185
|
+
providers
|
|
186
|
+
.filter((p) => typeof p === "string" && p !== "")
|
|
187
|
+
.flatMap((p) => managedRouteKeys(p))
|
|
188
|
+
);
|
|
189
|
+
const ops = [];
|
|
190
|
+
for (const key of keys) {
|
|
191
|
+
if (reserved.has(key)) continue;
|
|
192
|
+
if (this.options.readExistingPiAiRoute?.(key) !== undefined) {
|
|
193
|
+
ops.push({ op: "unset", path: ["providers", key] });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (ops.length > 0) await mutateSettingsSafe(this.options.settings, PI_AI_NS, ops);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* An account that has never saved a selection gets the catalog's
|
|
201
|
+
* recommendation once, persisted through `options.seedSelection`, so the
|
|
202
|
+
* picker starts with one model per family instead of the whole catalog.
|
|
203
|
+
* @returns the seeded ids, or undefined when a selection already exists.
|
|
204
|
+
*/
|
|
205
|
+
seedSelection(config, models) {
|
|
206
|
+
if (config.selectedModels !== undefined || models.length === 0) return undefined;
|
|
207
|
+
const ids = recommendModels(models, { lookup: (m) => this.lookupInfo(m) });
|
|
208
|
+
try {
|
|
209
|
+
this.options.seedSelection?.(ids);
|
|
210
|
+
} catch (error) {
|
|
211
|
+
this.options.logger?.warn?.(
|
|
212
|
+
`dsh-cliproxyapi: could not save the initial model selection: ${error instanceof Error ? error.message : String(error)}`
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
return ids;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Before a first-run seed only: give online model info a few seconds to
|
|
220
|
+
* arrive so the seeded selection matches what the panel will recommend.
|
|
221
|
+
*/
|
|
222
|
+
async awaitModelInfo(config) {
|
|
223
|
+
if (config.selectedModels !== undefined || this.options.modelInfoReady === undefined) return;
|
|
224
|
+
try {
|
|
225
|
+
await this.options.modelInfoReady();
|
|
226
|
+
} catch {}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Route keys owned by other accounts, from the latest config. */
|
|
230
|
+
reservedKeys() {
|
|
231
|
+
try {
|
|
232
|
+
const keys = this.options.config?.()?.reservedKeys;
|
|
233
|
+
return new Set(Array.isArray(keys) ? keys : []);
|
|
234
|
+
} catch {
|
|
235
|
+
return new Set();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Resolve the key and probe the account's CPA service. */
|
|
240
|
+
async discover(config) {
|
|
241
|
+
const existingRoute = this.options.readExistingPiAiRoute?.(config.provider);
|
|
242
|
+
const effectiveApiKeyEnv =
|
|
243
|
+
config.apiKeyEnv === DEFAULT_API_KEY_ENV &&
|
|
244
|
+
existingRoute?.apiKeyEnv &&
|
|
245
|
+
typeof existingRoute.apiKeyEnv === "string"
|
|
246
|
+
? existingRoute.apiKeyEnv
|
|
247
|
+
: config.apiKeyEnv;
|
|
248
|
+
|
|
249
|
+
const apiKey = await this.options.resolveApiKey(effectiveApiKeyEnv);
|
|
250
|
+
const autoDiscover = config.baseURL === "";
|
|
251
|
+
const fallbackBaseURL =
|
|
252
|
+
autoDiscover && typeof existingRoute?.baseURL === "string" ? existingRoute.baseURL : undefined;
|
|
253
|
+
const environmentBaseURL = autoDiscover ? this.options.environmentBaseURL?.() : undefined;
|
|
254
|
+
|
|
255
|
+
const result = await discoverCpa({
|
|
256
|
+
baseURL: config.baseURL,
|
|
257
|
+
autoDiscover,
|
|
258
|
+
...(fallbackBaseURL === undefined ? {} : { fallbackBaseURL }),
|
|
259
|
+
...(environmentBaseURL === undefined ? {} : { environmentBaseURL }),
|
|
260
|
+
...(apiKey === undefined ? {} : { apiKey }),
|
|
261
|
+
timeoutMs: config.probeTimeoutMs,
|
|
262
|
+
signal: this.controller.signal,
|
|
263
|
+
...(this.options.fetch === undefined ? {} : { fetch: this.options.fetch }),
|
|
264
|
+
});
|
|
265
|
+
return { result, apiKey, effectiveApiKeyEnv };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async runLoop() {
|
|
269
|
+
do {
|
|
270
|
+
this.rerun = false;
|
|
271
|
+
try {
|
|
272
|
+
await this.syncOnce();
|
|
273
|
+
this.hmrRetries = 0;
|
|
274
|
+
} catch (error) {
|
|
275
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
276
|
+
this.currentStatus = {
|
|
277
|
+
...this.currentStatus,
|
|
278
|
+
state: "error",
|
|
279
|
+
error: message,
|
|
280
|
+
};
|
|
281
|
+
this.options.logger?.warn?.(message);
|
|
282
|
+
this.scheduleHmrRetry(message);
|
|
283
|
+
}
|
|
284
|
+
} while (this.rerun && !this.controller.signal.aborted);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* A plugin reload holds DSH's HMR transaction for longer than the in-call
|
|
289
|
+
* backoff; retry the whole sync a few times once that transaction ends.
|
|
290
|
+
*/
|
|
291
|
+
scheduleHmrRetry(message) {
|
|
292
|
+
if (!message.includes("HMR transactions cannot be nested")) return;
|
|
293
|
+
this.hmrRetries = (this.hmrRetries ?? 0) + 1;
|
|
294
|
+
if (this.hmrRetries > 5 || this.controller.signal.aborted) return;
|
|
295
|
+
if (this.hmrRetryTimer !== undefined) clearTimeout(this.hmrRetryTimer);
|
|
296
|
+
const schedule = this.options.setTimeout ?? setTimeout;
|
|
297
|
+
this.hmrRetryTimer = schedule(() => {
|
|
298
|
+
this.hmrRetryTimer = undefined;
|
|
299
|
+
void this.refresh();
|
|
300
|
+
}, 3000 * this.hmrRetries);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* A failed plain listing must not leave image models hidden until the next
|
|
305
|
+
* five-minute refresh. Re-read soon, at most twice per failure streak.
|
|
306
|
+
*/
|
|
307
|
+
scheduleListingRetry() {
|
|
308
|
+
if (this.listingOk !== false) {
|
|
309
|
+
this.listingRetries = 0;
|
|
310
|
+
if (this.listingRetryTimer !== undefined) {
|
|
311
|
+
clearTimeout(this.listingRetryTimer);
|
|
312
|
+
this.listingRetryTimer = undefined;
|
|
313
|
+
}
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (this.listingRetries >= 2 || this.controller.signal.aborted || this.listingRetryTimer !== undefined) return;
|
|
317
|
+
this.listingRetries += 1;
|
|
318
|
+
const schedule = this.options.setTimeout ?? setTimeout;
|
|
319
|
+
this.listingRetryTimer = schedule(() => {
|
|
320
|
+
this.listingRetryTimer = undefined;
|
|
321
|
+
if (!this.controller.signal.aborted) void this.refresh();
|
|
322
|
+
}, 4000);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
async syncOnce() {
|
|
326
|
+
const config = this.options.config();
|
|
327
|
+
const probe = this.probeRequested === true;
|
|
328
|
+
this.probeRequested = false;
|
|
329
|
+
|
|
330
|
+
// An account whose route keys belong to another account must not write
|
|
331
|
+
// or remove them.
|
|
332
|
+
if (config.conflictWith !== undefined) {
|
|
333
|
+
this.syncedModels = [];
|
|
334
|
+
throw new Error(
|
|
335
|
+
`dsh-cliproxyapi: provider route "${config.provider}" overlaps account "${config.conflictWith}"; choose a different Provider Route ID`
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (!config.enabled) {
|
|
340
|
+
// A disabled account keeps no models in the picker.
|
|
341
|
+
await this.releaseRoutes([config.provider, this.activeProvider]);
|
|
342
|
+
this.activeProvider = undefined;
|
|
343
|
+
this.activeRoutes = undefined;
|
|
344
|
+
this.syncedModels = [];
|
|
345
|
+
let endpoint;
|
|
346
|
+
let probeError;
|
|
347
|
+
if (probe) {
|
|
348
|
+
try {
|
|
349
|
+
const { result } = await this.discover(config);
|
|
350
|
+
this.allDiscoveredModels = result.models;
|
|
351
|
+
this.listingOk = result.listingOk !== false;
|
|
352
|
+
await this.awaitModelInfo(config);
|
|
353
|
+
this.seedSelection(config, this.chatModels(result.models));
|
|
354
|
+
endpoint = result.rootURL;
|
|
355
|
+
} catch (error) {
|
|
356
|
+
if (this.controller.signal.aborted) throw error;
|
|
357
|
+
probeError = error instanceof Error ? error.message : String(error);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
this.currentStatus = {
|
|
361
|
+
state: "disabled",
|
|
362
|
+
provider: config.provider,
|
|
363
|
+
displayName: config.displayName,
|
|
364
|
+
...(endpoint === undefined ? {} : { endpoint }),
|
|
365
|
+
modelCount: 0,
|
|
366
|
+
totalDiscovered: this.allDiscoveredModels.length,
|
|
367
|
+
...(probe ? { refreshedAt: new Date().toISOString() } : {}),
|
|
368
|
+
...(probeError === undefined ? {} : { error: probeError }),
|
|
369
|
+
};
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const { error: _prevErr, ...prevStatus } = this.currentStatus;
|
|
374
|
+
this.currentStatus = { ...prevStatus, state: "probing" };
|
|
375
|
+
|
|
376
|
+
const { result, apiKey, effectiveApiKeyEnv } = await this.discover(config);
|
|
377
|
+
// Keep the catalog visible in the panel even if the selection matches nothing.
|
|
378
|
+
this.allDiscoveredModels = result.models;
|
|
379
|
+
this.listingOk = result.listingOk !== false;
|
|
380
|
+
this.scheduleListingRetry();
|
|
381
|
+
await this.awaitModelInfo(config);
|
|
382
|
+
const seeded = this.seedSelection(config, this.chatModels(result.models));
|
|
383
|
+
|
|
384
|
+
const effectiveConfig = {
|
|
385
|
+
...config,
|
|
386
|
+
...(seeded === undefined ? {} : { selectedModels: seeded }),
|
|
387
|
+
apiKeyEnv: effectiveApiKeyEnv,
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
const built = buildPiAiRoutes(
|
|
391
|
+
effectiveConfig,
|
|
392
|
+
{ ...result, models: this.withKinds(result.models) },
|
|
393
|
+
apiKey !== undefined
|
|
394
|
+
);
|
|
395
|
+
this.syncedModels = built.orderedModels;
|
|
396
|
+
|
|
397
|
+
// Each managed route keeps unrelated fields a user set by hand (retry
|
|
398
|
+
// policy, image budgets) and takes this plugin's fields on top. A route
|
|
399
|
+
// key that no longer carries models is removed.
|
|
400
|
+
const reserved = new Set(config.reservedKeys ?? []);
|
|
401
|
+
const ownedKeys = new Set(
|
|
402
|
+
[
|
|
403
|
+
...managedRouteKeys(config.provider),
|
|
404
|
+
...(this.activeProvider !== undefined && this.activeProvider !== config.provider
|
|
405
|
+
? managedRouteKeys(this.activeProvider)
|
|
406
|
+
: []),
|
|
407
|
+
].filter((key) => !reserved.has(key))
|
|
408
|
+
);
|
|
409
|
+
const ops = [];
|
|
410
|
+
const nextRoutes = {};
|
|
411
|
+
for (const key of ownedKeys) {
|
|
412
|
+
const existing = this.options.readExistingPiAiRoute?.(key);
|
|
413
|
+
const wanted = built.routes[key];
|
|
414
|
+
if (wanted === undefined) {
|
|
415
|
+
if (existing !== undefined) ops.push({ op: "unset", path: ["providers", key] });
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
const merged = {
|
|
419
|
+
...(existing && typeof existing === "object" ? stripManagedFields(existing) : {}),
|
|
420
|
+
...wanted,
|
|
421
|
+
};
|
|
422
|
+
nextRoutes[key] = merged;
|
|
423
|
+
if (!sameJson(existing, merged)) {
|
|
424
|
+
ops.push({ op: "set", path: ["providers", key], value: merged });
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
if (ops.length > 0) {
|
|
428
|
+
await mutateSettingsSafe(this.options.settings, PI_AI_NS, ops);
|
|
429
|
+
}
|
|
430
|
+
this.activeProvider = config.provider;
|
|
431
|
+
this.activeRoutes = nextRoutes;
|
|
432
|
+
await this.followDefaultModel(built.orderedModels, ownedKeys);
|
|
433
|
+
|
|
434
|
+
const protocolCounts = {};
|
|
435
|
+
for (const m of built.orderedModels) protocolCounts[m.protocol] = (protocolCounts[m.protocol] ?? 0) + 1;
|
|
436
|
+
this.currentStatus = {
|
|
437
|
+
state: "connected",
|
|
438
|
+
endpoint: result.rootURL,
|
|
439
|
+
provider: config.provider,
|
|
440
|
+
displayName: config.displayName,
|
|
441
|
+
routes: Object.keys(built.routes),
|
|
442
|
+
protocolCounts,
|
|
443
|
+
modelCount: built.orderedModels.length,
|
|
444
|
+
totalDiscovered: result.models.length,
|
|
445
|
+
refreshedAt: new Date().toISOString(),
|
|
446
|
+
};
|
|
447
|
+
this.options.logger?.info?.(
|
|
448
|
+
`dsh-cliproxyapi: connected to ${result.rootURL}; synchronized ${built.orderedModels.length}/${result.models.length} model(s) into ${Object.keys(built.routes).join(", ")}`
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Keep the agent default model reachable when protocol routing moves it to
|
|
454
|
+
* a sibling route (e.g. `cli-proxy` -> `cli-proxy-messages`).
|
|
455
|
+
*/
|
|
456
|
+
async followDefaultModel(models, ownedKeys) {
|
|
457
|
+
const current = this.options.readDefaultModel?.();
|
|
458
|
+
if (current === undefined || !ownedKeys.has(current.provider)) return;
|
|
459
|
+
const target = models.find((m) => m.id === current.model);
|
|
460
|
+
if (target === undefined || target.route === current.provider) return;
|
|
461
|
+
try {
|
|
462
|
+
await mutateSettingsSafe(this.options.settings, DEFAULT_MODEL_NS, [
|
|
463
|
+
{ op: "set", path: ["provider"], value: target.route },
|
|
464
|
+
]);
|
|
465
|
+
} catch (error) {
|
|
466
|
+
this.options.logger?.warn?.(
|
|
467
|
+
`dsh-cliproxyapi: could not move default model to ${target.route}: ${error instanceof Error ? error.message : String(error)}`
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Profile state path resolution and atomic account state persistence.
|
|
2
|
+
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { dirname, join } from "node:path";
|
|
6
|
+
|
|
7
|
+
export function resolveStateFilePath(ctx) {
|
|
8
|
+
try {
|
|
9
|
+
const profileCtx = ctx?.get?.("profileContext");
|
|
10
|
+
if (profileCtx && typeof profileCtx.dir === "string" && profileCtx.dir.trim() !== "") {
|
|
11
|
+
return join(profileCtx.dir.trim(), ".dsh-cliproxyapi", "state.json");
|
|
12
|
+
}
|
|
13
|
+
} catch {}
|
|
14
|
+
const dshHome =
|
|
15
|
+
process.env.DSH_HOME && process.env.DSH_HOME.trim() !== ""
|
|
16
|
+
? process.env.DSH_HOME.trim()
|
|
17
|
+
: join(homedir(), ".dsh");
|
|
18
|
+
return join(dshHome, "dsh-cliproxyapi", "state.json");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function loadPersistedState(filePath) {
|
|
22
|
+
try {
|
|
23
|
+
if (!existsSync(filePath)) return {};
|
|
24
|
+
const raw = JSON.parse(readFileSync(filePath, "utf8"));
|
|
25
|
+
return typeof raw === "object" && raw !== null && !Array.isArray(raw) ? raw : {};
|
|
26
|
+
} catch {
|
|
27
|
+
return {};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function savePersistedState(filePath, state) {
|
|
32
|
+
try {
|
|
33
|
+
const dir = dirname(filePath);
|
|
34
|
+
mkdirSync(dir, { recursive: true });
|
|
35
|
+
const tmp = `${filePath}.${Date.now().toString(36)}.tmp`;
|
|
36
|
+
writeFileSync(tmp, JSON.stringify(state, null, 2), "utf8");
|
|
37
|
+
renameSync(tmp, filePath);
|
|
38
|
+
} catch {}
|
|
39
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Human-readable account discovery and quota status for slash commands.
|
|
2
|
+
|
|
3
|
+
import { DEFAULT_REFRESH_INTERVAL_SECONDS, PROTOCOLS } from "./config.js";
|
|
4
|
+
|
|
5
|
+
export function renderAccountStatus(account) {
|
|
6
|
+
const { config: cfg, credential: keyInfo, status } = account;
|
|
7
|
+
const selectedMode =
|
|
8
|
+
cfg.selectionSaved === false
|
|
9
|
+
? "Selected Filter: not chosen yet (the first successful probe checks the newest model of each family)"
|
|
10
|
+
: Array.isArray(cfg.selectedModels) && cfg.selectedModels.length > 0
|
|
11
|
+
? `Selected Filter: ${cfg.selectedModels.length} model(s) checked`
|
|
12
|
+
: "Selected Filter: All models (no selection filter active)";
|
|
13
|
+
const modelListPreview =
|
|
14
|
+
Array.isArray(status.models) && status.models.length > 0
|
|
15
|
+
? `Active Models in Picker: ${status.models
|
|
16
|
+
.slice(0, 12)
|
|
17
|
+
.map((m) => m.id)
|
|
18
|
+
.join(", ")}${status.models.length > 12 ? ` ... (+${status.models.length - 12} more)` : ""}`
|
|
19
|
+
: undefined;
|
|
20
|
+
const media = (status.allModels ?? []).filter((m) => m.kind === "image" || m.kind === "video");
|
|
21
|
+
const mediaLine =
|
|
22
|
+
media.length > 0
|
|
23
|
+
? `Media Models (not in the picker): ${media.filter((m) => m.kind === "image").length} image, ${media.filter((m) => m.kind === "video").length} video`
|
|
24
|
+
: undefined;
|
|
25
|
+
const counts = status.protocolCounts ?? {};
|
|
26
|
+
const protocolLine =
|
|
27
|
+
cfg.protocolMode === "fixed"
|
|
28
|
+
? `Protocol: ${cfg.protocol} (fixed for every model)`
|
|
29
|
+
: `Protocol: native per model (${PROTOCOLS.filter((p) => counts[p])
|
|
30
|
+
.map((p) => `${p}: ${counts[p]}`)
|
|
31
|
+
.join(", ") || "none synced yet"})`;
|
|
32
|
+
const lines = [
|
|
33
|
+
`Account "${cfg.displayName}" [id: ${account.id}] — ${cfg.enabled ? "enabled" : "disabled"}`,
|
|
34
|
+
`CLIProxyAPI Status: ${status.state}`,
|
|
35
|
+
`Provider Routes: ${(status.state === "connected" && status.routes ? status.routes : [cfg.provider]).join(", ")}`,
|
|
36
|
+
protocolLine,
|
|
37
|
+
`Configured Base URL: ${cfg.baseURL || "(auto-discover / inherited)"}`,
|
|
38
|
+
...(status.endpoint ? [`Active Endpoint: ${status.endpoint}`] : []),
|
|
39
|
+
`API Key Reference: ${keyInfo.ref} (${keyInfo.configured ? "configured" : "not set"})`,
|
|
40
|
+
selectedMode,
|
|
41
|
+
`Synchronized Models: ${status.modelCount}${
|
|
42
|
+
status.totalDiscovered ? ` (of ${status.totalDiscovered} discovered)` : ""
|
|
43
|
+
}`,
|
|
44
|
+
...(modelListPreview ? [modelListPreview] : []),
|
|
45
|
+
...(mediaLine ? [mediaLine] : []),
|
|
46
|
+
...(status.refreshedAt ? [`Last Refreshed: ${status.refreshedAt}`] : []),
|
|
47
|
+
...(status.error ? [`Error: ${status.error}`] : []),
|
|
48
|
+
];
|
|
49
|
+
return lines.join("\n");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** One line per account: management key, quota state and what the picker models map to. Never includes keys. */
|
|
53
|
+
export function renderQuotaText(snapshot, onlyAccountId) {
|
|
54
|
+
const accounts = (snapshot?.accounts ?? []).filter((a) => onlyAccountId === undefined || a.accountId === onlyAccountId);
|
|
55
|
+
if (accounts.length === 0) return "";
|
|
56
|
+
const lines = ["Quota (CPA management API):"];
|
|
57
|
+
for (const a of accounts) {
|
|
58
|
+
if (!a.keyConfigured) {
|
|
59
|
+
lines.push(`- ${a.displayName} [${a.accountId}]: no management key (${a.keyRef})`);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const creds = Array.isArray(a.credentials) ? a.credentials : [];
|
|
63
|
+
const state = a.state + (a.errorCode ? ` (${a.errorCode}${a.errorStatus ? ` ${a.errorStatus}` : ""})` : "") +
|
|
64
|
+
(Number.isInteger(a.authFailures) && a.authFailures > 0
|
|
65
|
+
? `, key rejected ${a.authFailures}/${a.authFailureLimit}${a.authPaused ? ", automatic checks stopped until a new management key is saved" : ""}`
|
|
66
|
+
: "") +
|
|
67
|
+
(a.refreshing ? ", refreshing" : "");
|
|
68
|
+
lines.push(`- ${a.displayName} [${a.accountId}]: ${state}; ${creds.length} credential(s)${a.fetchedAt ? `; fetched ${a.fetchedAt}` : ""}; routes ${(a.routes ?? []).join(", ") || "none"}`);
|
|
69
|
+
for (const m of a.pickerModels ?? []) {
|
|
70
|
+
const ids = a.modelIndex?.[m.id] ?? [];
|
|
71
|
+
const names = ids.map((id) => {
|
|
72
|
+
const c = creds.find((x) => x.id === id);
|
|
73
|
+
if (!c) return id;
|
|
74
|
+
const pct = (c.windows ?? []).map((w) => w.remainingPercent).filter((v) => typeof v === "number");
|
|
75
|
+
return `${c.provider}:${c.label || c.id}${pct.length ? ` ${Math.round(Math.min(...pct))}%` : ""}${c.status === "active" ? "" : ` (${c.status})`}`;
|
|
76
|
+
});
|
|
77
|
+
lines.push(` ${m.route}/${m.id} -> ${names.length ? names.join(", ") : "no credential"} [${a.modelMatch?.[m.id] ?? "none"}]`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return lines.join("\n");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function renderStatusText(payload, onlyAccountId) {
|
|
84
|
+
const accounts = payload.accounts ?? [];
|
|
85
|
+
if (accounts.length === 0) {
|
|
86
|
+
return "No CLIProxyAPI account is configured. Add one in Settings → CLIProxyAPI.";
|
|
87
|
+
}
|
|
88
|
+
const shown =
|
|
89
|
+
onlyAccountId === undefined ? accounts : accounts.filter((a) => a.id === onlyAccountId);
|
|
90
|
+
const enabled = accounts.filter((a) => a.config.enabled).length;
|
|
91
|
+
const header = `CLIProxyAPI accounts: ${accounts.length} (${enabled} enabled); auto refresh every ${payload.global?.refreshIntervalSeconds ?? DEFAULT_REFRESH_INTERVAL_SECONDS}s`;
|
|
92
|
+
return [header, ...shown.map(renderAccountStatus)].join("\n\n");
|
|
93
|
+
}
|