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
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// Native wire protocols, model overrides and managed llm-pi-ai route profiles.
|
|
2
|
+
|
|
3
|
+
import { REASONING_LEVELS, PROTOCOLS, PROTOCOL_ROUTES } from "./config.js";
|
|
4
|
+
import { filterModels, sortModelsNewestFirst } from "./models.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Pick the wire protocol that reaches a model with the fewest translations.
|
|
8
|
+
*
|
|
9
|
+
* CLIProxyAPI serves every model on OpenAI Chat, OpenAI Responses and
|
|
10
|
+
* Anthropic Messages and translates between them and the upstream channel.
|
|
11
|
+
* Speaking the upstream's own protocol skips that translation:
|
|
12
|
+
* - Claude family -> Anthropic Messages (`/v1/messages`)
|
|
13
|
+
* - OpenAI / Codex / Grok -> OpenAI Responses (`/v1/responses`)
|
|
14
|
+
* - everything else, including Gemini -> OpenAI Chat Completions; DSH
|
|
15
|
+
* cannot speak Gemini generateContent on a custom route.
|
|
16
|
+
* `owned_by` from the CPA listing names the family; the id is the fallback.
|
|
17
|
+
* @param model - parsed catalog model.
|
|
18
|
+
* @returns one of {@link PROTOCOLS}.
|
|
19
|
+
*/
|
|
20
|
+
export function nativeProtocol(model) {
|
|
21
|
+
const owner = String(model.ownedBy ?? "").toLowerCase();
|
|
22
|
+
const bare = String(model.id ?? "").toLowerCase().split("/").pop() ?? "";
|
|
23
|
+
if (owner === "anthropic" || /^claude/.test(bare)) return "anthropic-messages";
|
|
24
|
+
if (owner === "openai" || owner === "xai") return "openai-responses";
|
|
25
|
+
if (/^(gpt|o\d|codex|grok)/.test(bare) && !/^gpt-oss/.test(bare)) return "openai-responses";
|
|
26
|
+
return "openai-completions";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The protocol a model is synchronized with under the given config. */
|
|
30
|
+
export function modelProtocol(model, config) {
|
|
31
|
+
const override = config.modelOverrides?.[model.id]?.protocol;
|
|
32
|
+
if (override !== undefined) return override;
|
|
33
|
+
return config.protocolMode === "fixed" ? config.protocol : nativeProtocol(model);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The DSH route key carrying one protocol. */
|
|
37
|
+
export function routeKeyFor(provider, protocol) {
|
|
38
|
+
return `${provider}${PROTOCOL_ROUTES[protocol].suffix}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function routeBaseURL(rootURL, protocol) {
|
|
42
|
+
return protocol === "anthropic-messages" ? rootURL : `${rootURL}/v1`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function supportedReasoning(model) {
|
|
46
|
+
if (!model.reasoning) return undefined;
|
|
47
|
+
const declared = new Set(model.reasoningLevels);
|
|
48
|
+
const levels =
|
|
49
|
+
declared.size === 0
|
|
50
|
+
? REASONING_LEVELS
|
|
51
|
+
: REASONING_LEVELS.filter((level) => declared.has(level));
|
|
52
|
+
const result = {};
|
|
53
|
+
for (const level of levels) {
|
|
54
|
+
result[level] = level === "off" ? null : model.reasoningWire?.[level] ?? level;
|
|
55
|
+
}
|
|
56
|
+
return Object.keys(result).length === 0 ? undefined : result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Claude models that take adaptive thinking with an `effort` value rather
|
|
61
|
+
* than a token budget: Opus/Sonnet 4.6+, Haiku 5+, Fable and every 5.x.
|
|
62
|
+
* Older Claude models keep budget-based thinking.
|
|
63
|
+
*/
|
|
64
|
+
export function claudeUsesAdaptiveThinking(model) {
|
|
65
|
+
const bare = String(model.id).toLowerCase().split("/").pop() ?? "";
|
|
66
|
+
if (/^claude-fable/.test(bare)) return true;
|
|
67
|
+
const match = bare.match(/^claude-(?:opus|sonnet|haiku)-(\d+)(?:-(\d+))?/);
|
|
68
|
+
if (match === null) return /^claude-\d+-fable/.test(bare);
|
|
69
|
+
const major = Number(match[1]);
|
|
70
|
+
const minor = match[2] !== undefined && match[2].length <= 2 ? Number(match[2]) : 0;
|
|
71
|
+
return major > 4 || (major === 4 && minor >= 6);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function applyModelOverride(model, override) {
|
|
75
|
+
if (override === undefined) return model;
|
|
76
|
+
const next = { ...model };
|
|
77
|
+
if (override.contextWindow !== undefined) next.contextWindow = override.contextWindow;
|
|
78
|
+
if (override.maxTokens !== undefined) next.maxTokens = override.maxTokens;
|
|
79
|
+
if (override.image === true || override.image === false) {
|
|
80
|
+
const rest = model.inputModalities.filter((item) => item !== "image");
|
|
81
|
+
next.inputModalities = override.image ? [...new Set(["text", "image", ...rest])] : rest.length > 0 ? rest : ["text"];
|
|
82
|
+
}
|
|
83
|
+
if (Array.isArray(override.reasoningLevels)) {
|
|
84
|
+
next.reasoningLevels = [...override.reasoningLevels];
|
|
85
|
+
next.reasoning = override.reasoningLevels.some((level) => level !== "off");
|
|
86
|
+
}
|
|
87
|
+
return next;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function protocolCompat(model, protocol, hasReasoning) {
|
|
91
|
+
if (protocol === "openai-completions") {
|
|
92
|
+
return hasReasoning ? { supportsReasoningEffort: true, thinkingFormat: "openai" } : undefined;
|
|
93
|
+
}
|
|
94
|
+
if (protocol === "anthropic-messages") {
|
|
95
|
+
return hasReasoning && claudeUsesAdaptiveThinking(model)
|
|
96
|
+
? { forceAdaptiveThinking: true }
|
|
97
|
+
: undefined;
|
|
98
|
+
}
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function toPiAiModelProfile(model, protocol) {
|
|
103
|
+
const reasoningEfforts = supportedReasoning(model);
|
|
104
|
+
const compat = protocolCompat(model, protocol, reasoningEfforts !== undefined);
|
|
105
|
+
return {
|
|
106
|
+
id: model.id,
|
|
107
|
+
...(model.name === undefined ? {} : { name: model.name }),
|
|
108
|
+
contextWindow: model.contextWindow ?? 262144,
|
|
109
|
+
maxTokens: model.maxTokens ?? 64000,
|
|
110
|
+
input: model.inputModalities.includes("image") ? ["text", "image"] : ["text"],
|
|
111
|
+
...(reasoningEfforts === undefined ? {} : { reasoningEfforts }),
|
|
112
|
+
...(compat === undefined ? {} : { compat }),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Build one llm-pi-ai route per protocol the selected models need.
|
|
118
|
+
* The base route key (e.g. `cli-proxy`) always carries OpenAI Chat so that
|
|
119
|
+
* existing sessions and the default-model setting keep resolving; Responses
|
|
120
|
+
* and Messages models get `<key>-responses` / `<key>-messages` siblings.
|
|
121
|
+
* @returns `{ allModels, orderedModels, routes }` where `routes` maps each
|
|
122
|
+
* route key to its profile, in protocol order, and every ordered model
|
|
123
|
+
* carries its `protocol` and `route`.
|
|
124
|
+
*/
|
|
125
|
+
/** Whether a model belongs in chat routes: `kind` (with overrides) or detection says chat. */
|
|
126
|
+
export function isChatModel(model) {
|
|
127
|
+
return (model.kind ?? model.detectedKind ?? "chat") === "chat";
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function buildPiAiRoutes(config, discovery, hasApiKey) {
|
|
131
|
+
const allOrdered = sortModelsNewestFirst(discovery.models);
|
|
132
|
+
// Image / video models are served by CPA's media endpoints, never by chat routes.
|
|
133
|
+
const chatOrdered = allOrdered.filter(isChatModel);
|
|
134
|
+
const filtered = filterModels(chatOrdered, config).map((model) => {
|
|
135
|
+
const effective = applyModelOverride(model, config.modelOverrides?.[model.id]);
|
|
136
|
+
const protocol = modelProtocol(model, config);
|
|
137
|
+
return { ...effective, protocol, route: routeKeyFor(config.provider, protocol) };
|
|
138
|
+
});
|
|
139
|
+
if (filtered.length === 0) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
`dsh-cliproxyapi: none of the selected models matched the ${chatOrdered.length} chat model(s) offered by ${discovery.rootURL}. Select models in Settings → CLIProxyAPI → Models.`
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
const auth = hasApiKey
|
|
145
|
+
? { apiKeyEnv: config.apiKeyEnv }
|
|
146
|
+
: { headers: { Authorization: "Bearer cpa-local" } };
|
|
147
|
+
const routes = {};
|
|
148
|
+
for (const protocol of PROTOCOLS) {
|
|
149
|
+
const members = filtered.filter((m) => m.protocol === protocol);
|
|
150
|
+
if (members.length === 0) continue;
|
|
151
|
+
routes[routeKeyFor(config.provider, protocol)] = {
|
|
152
|
+
displayName: `${config.displayName}${PROTOCOL_ROUTES[protocol].label}`,
|
|
153
|
+
api: protocol,
|
|
154
|
+
baseURL: routeBaseURL(discovery.rootURL, protocol),
|
|
155
|
+
models: members.map((m) => toPiAiModelProfile(m, protocol)),
|
|
156
|
+
...auth,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
return { allModels: allOrdered, orderedModels: filtered, routes };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Every route key this plugin may own under a provider prefix. */
|
|
163
|
+
export function managedRouteKeys(provider) {
|
|
164
|
+
return PROTOCOLS.map((protocol) => routeKeyFor(provider, protocol));
|
|
165
|
+
}
|
|
@@ -0,0 +1,445 @@
|
|
|
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, recommendModels } from "./models.js";
|
|
6
|
+
import { nativeProtocol, modelProtocol, applyModelOverride, isChatModel, buildPiAiRoutes, managedRouteKeys } from "./routes.js";
|
|
7
|
+
import { discoverCpa } from "./discovery.js";
|
|
8
|
+
|
|
9
|
+
export function sleep(ms) {
|
|
10
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function sameJson(a, b) {
|
|
14
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Fields this plugin writes; any stale copy is replaced, never merged. */
|
|
18
|
+
export const MANAGED_ROUTE_FIELDS = ["displayName", "api", "baseURL", "models", "apiKeyEnv", "headers"];
|
|
19
|
+
|
|
20
|
+
export function stripManagedFields(route) {
|
|
21
|
+
const out = { ...route };
|
|
22
|
+
for (const field of MANAGED_ROUTE_FIELDS) delete out[field];
|
|
23
|
+
// An empty dict materialized by the schema is not a user choice.
|
|
24
|
+
if (out.compat !== undefined && sameJson(out.compat, { chatTemplateKwargs: {}, chatTemplateArgs: {} })) {
|
|
25
|
+
delete out.compat;
|
|
26
|
+
}
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function mutateSettingsSafe(settings, ns, ops) {
|
|
31
|
+
let lastError;
|
|
32
|
+
for (let attempt = 0; attempt < 6; attempt += 1) {
|
|
33
|
+
try {
|
|
34
|
+
await settings.mutate(ns, ops);
|
|
35
|
+
return;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
lastError = error;
|
|
38
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
39
|
+
if (msg.includes("HMR transactions cannot be nested")) {
|
|
40
|
+
await sleep(300 * (attempt + 1));
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
throw lastError;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function formatModelView(m, selectedSet, config) {
|
|
50
|
+
const overrides = config?.modelOverrides ?? {};
|
|
51
|
+
const override = overrides[m.id];
|
|
52
|
+
const effective = applyModelOverride(m, override);
|
|
53
|
+
const protocol = config === undefined ? nativeProtocol(m) : modelProtocol(m, config);
|
|
54
|
+
return {
|
|
55
|
+
kind: m.kind ?? m.detectedKind ?? "chat",
|
|
56
|
+
protocol,
|
|
57
|
+
nativeProtocol: nativeProtocol(m),
|
|
58
|
+
...(m.ownedBy === undefined ? {} : { ownedBy: m.ownedBy }),
|
|
59
|
+
id: effective.id,
|
|
60
|
+
name: effective.name ?? effective.id,
|
|
61
|
+
family: modelFamily(effective),
|
|
62
|
+
contextWindow: effective.contextWindow ?? 262144,
|
|
63
|
+
maxTokens: effective.maxTokens ?? 64000,
|
|
64
|
+
image: effective.inputModalities.includes("image"),
|
|
65
|
+
reasoning: effective.reasoning,
|
|
66
|
+
reasoningLevels: effective.reasoningLevels,
|
|
67
|
+
selected: selectedSet.has(effective.id),
|
|
68
|
+
...(override === undefined ? {} : { override }),
|
|
69
|
+
discovered: {
|
|
70
|
+
contextWindow: m.contextWindow ?? 262144,
|
|
71
|
+
maxTokens: m.maxTokens ?? 64000,
|
|
72
|
+
image: m.inputModalities.includes("image"),
|
|
73
|
+
reasoningLevels: m.reasoningLevels,
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export class CpaRuntime {
|
|
79
|
+
constructor(options) {
|
|
80
|
+
this.options = options;
|
|
81
|
+
this.activeProvider = undefined;
|
|
82
|
+
this.activeRoutes = undefined;
|
|
83
|
+
this.running = undefined;
|
|
84
|
+
this.rerun = false;
|
|
85
|
+
this.controller = new AbortController();
|
|
86
|
+
this.allDiscoveredModels = [];
|
|
87
|
+
this.syncedModels = [];
|
|
88
|
+
// Whether the last successful discovery could read the plain `/v1/models`
|
|
89
|
+
// listing, which carries models the enriched catalog omits (image models
|
|
90
|
+
// for some servers). A failed read is retried shortly, not in five minutes.
|
|
91
|
+
this.listingOk = true;
|
|
92
|
+
this.listingRetries = 0;
|
|
93
|
+
this.listingRetryTimer = undefined;
|
|
94
|
+
this.currentStatus = {
|
|
95
|
+
state: "idle",
|
|
96
|
+
modelCount: 0,
|
|
97
|
+
totalDiscovered: 0,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Models with their effective `kind` under the account's current kind
|
|
103
|
+
* overrides (read fresh, so an override applies without a new probe).
|
|
104
|
+
*/
|
|
105
|
+
withKinds(models = this.allDiscoveredModels) {
|
|
106
|
+
let overrides = {};
|
|
107
|
+
try {
|
|
108
|
+
overrides = this.options.kindOverrides?.() ?? {};
|
|
109
|
+
} catch {}
|
|
110
|
+
return models.map((m) => ({ ...m, kind: effectiveKind(m, overrides) }));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Discovered chat models (what the picker, recommendations and seeding use). */
|
|
114
|
+
chatModels(models = this.allDiscoveredModels) {
|
|
115
|
+
return this.withKinds(models).filter(isChatModel);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
status() {
|
|
119
|
+
const syncedSet = new Set(this.syncedModels.map((m) => m.id));
|
|
120
|
+
const config = this.options.config();
|
|
121
|
+
const kinded = this.withKinds();
|
|
122
|
+
const byId = new Map(kinded.map((m) => [m.id, m]));
|
|
123
|
+
return {
|
|
124
|
+
...this.currentStatus,
|
|
125
|
+
listingOk: this.listingOk,
|
|
126
|
+
models: this.syncedModels.map((m) => ({
|
|
127
|
+
...formatModelView(byId.get(m.id) ?? m, syncedSet, config),
|
|
128
|
+
route: m.route,
|
|
129
|
+
})),
|
|
130
|
+
allModels: kinded.map((m) => formatModelView(m, syncedSet, config)),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Run one sync. `probe: true` also reads the catalog of a disabled account
|
|
136
|
+
* so its models can be picked before it is enabled; periodic refreshes of a
|
|
137
|
+
* disabled account stay offline.
|
|
138
|
+
*/
|
|
139
|
+
refresh(options = {}) {
|
|
140
|
+
if (options.probe === true) this.probeRequested = true;
|
|
141
|
+
if (this.running !== undefined) {
|
|
142
|
+
this.rerun = true;
|
|
143
|
+
return this.running;
|
|
144
|
+
}
|
|
145
|
+
this.running = this.runLoop().finally(() => {
|
|
146
|
+
this.running = undefined;
|
|
147
|
+
});
|
|
148
|
+
return this.running;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
dispose() {
|
|
152
|
+
this.controller.abort(new Error("dsh-cliproxyapi disposed"));
|
|
153
|
+
if (this.hmrRetryTimer !== undefined) clearTimeout(this.hmrRetryTimer);
|
|
154
|
+
if (this.listingRetryTimer !== undefined) clearTimeout(this.listingRetryTimer);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Stop syncing and remove every route this account wrote. */
|
|
158
|
+
async teardown(provider) {
|
|
159
|
+
this.dispose();
|
|
160
|
+
try {
|
|
161
|
+
await this.running;
|
|
162
|
+
} catch {}
|
|
163
|
+
await this.releaseRoutes([provider, this.activeProvider]);
|
|
164
|
+
this.activeProvider = undefined;
|
|
165
|
+
this.activeRoutes = undefined;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Unset the managed route keys of the given providers that still exist. */
|
|
169
|
+
async releaseRoutes(providers) {
|
|
170
|
+
const reserved = this.reservedKeys();
|
|
171
|
+
const keys = new Set(
|
|
172
|
+
providers
|
|
173
|
+
.filter((p) => typeof p === "string" && p !== "")
|
|
174
|
+
.flatMap((p) => managedRouteKeys(p))
|
|
175
|
+
);
|
|
176
|
+
const ops = [];
|
|
177
|
+
for (const key of keys) {
|
|
178
|
+
if (reserved.has(key)) continue;
|
|
179
|
+
if (this.options.readExistingPiAiRoute?.(key) !== undefined) {
|
|
180
|
+
ops.push({ op: "unset", path: ["providers", key] });
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (ops.length > 0) await mutateSettingsSafe(this.options.settings, PI_AI_NS, ops);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* An account that has never saved a selection gets the catalog's
|
|
188
|
+
* recommendation once, persisted through `options.seedSelection`, so the
|
|
189
|
+
* picker starts with one model per family instead of the whole catalog.
|
|
190
|
+
* @returns the seeded ids, or undefined when a selection already exists.
|
|
191
|
+
*/
|
|
192
|
+
seedSelection(config, models) {
|
|
193
|
+
if (config.selectedModels !== undefined || models.length === 0) return undefined;
|
|
194
|
+
const ids = recommendModels(models);
|
|
195
|
+
try {
|
|
196
|
+
this.options.seedSelection?.(ids);
|
|
197
|
+
} catch (error) {
|
|
198
|
+
this.options.logger?.warn?.(
|
|
199
|
+
`dsh-cliproxyapi: could not save the initial model selection: ${error instanceof Error ? error.message : String(error)}`
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
return ids;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Route keys owned by other accounts, from the latest config. */
|
|
206
|
+
reservedKeys() {
|
|
207
|
+
try {
|
|
208
|
+
const keys = this.options.config?.()?.reservedKeys;
|
|
209
|
+
return new Set(Array.isArray(keys) ? keys : []);
|
|
210
|
+
} catch {
|
|
211
|
+
return new Set();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Resolve the key and probe the account's CPA service. */
|
|
216
|
+
async discover(config) {
|
|
217
|
+
const existingRoute = this.options.readExistingPiAiRoute?.(config.provider);
|
|
218
|
+
const effectiveApiKeyEnv =
|
|
219
|
+
config.apiKeyEnv === DEFAULT_API_KEY_ENV &&
|
|
220
|
+
existingRoute?.apiKeyEnv &&
|
|
221
|
+
typeof existingRoute.apiKeyEnv === "string"
|
|
222
|
+
? existingRoute.apiKeyEnv
|
|
223
|
+
: config.apiKeyEnv;
|
|
224
|
+
|
|
225
|
+
const apiKey = await this.options.resolveApiKey(effectiveApiKeyEnv);
|
|
226
|
+
const autoDiscover = config.baseURL === "";
|
|
227
|
+
const fallbackBaseURL =
|
|
228
|
+
autoDiscover && typeof existingRoute?.baseURL === "string" ? existingRoute.baseURL : undefined;
|
|
229
|
+
const environmentBaseURL = autoDiscover ? this.options.environmentBaseURL?.() : undefined;
|
|
230
|
+
|
|
231
|
+
const result = await discoverCpa({
|
|
232
|
+
baseURL: config.baseURL,
|
|
233
|
+
autoDiscover,
|
|
234
|
+
...(fallbackBaseURL === undefined ? {} : { fallbackBaseURL }),
|
|
235
|
+
...(environmentBaseURL === undefined ? {} : { environmentBaseURL }),
|
|
236
|
+
...(apiKey === undefined ? {} : { apiKey }),
|
|
237
|
+
timeoutMs: config.probeTimeoutMs,
|
|
238
|
+
signal: this.controller.signal,
|
|
239
|
+
...(this.options.fetch === undefined ? {} : { fetch: this.options.fetch }),
|
|
240
|
+
});
|
|
241
|
+
return { result, apiKey, effectiveApiKeyEnv };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async runLoop() {
|
|
245
|
+
do {
|
|
246
|
+
this.rerun = false;
|
|
247
|
+
try {
|
|
248
|
+
await this.syncOnce();
|
|
249
|
+
this.hmrRetries = 0;
|
|
250
|
+
} catch (error) {
|
|
251
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
252
|
+
this.currentStatus = {
|
|
253
|
+
...this.currentStatus,
|
|
254
|
+
state: "error",
|
|
255
|
+
error: message,
|
|
256
|
+
};
|
|
257
|
+
this.options.logger?.warn?.(message);
|
|
258
|
+
this.scheduleHmrRetry(message);
|
|
259
|
+
}
|
|
260
|
+
} while (this.rerun && !this.controller.signal.aborted);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* A plugin reload holds DSH's HMR transaction for longer than the in-call
|
|
265
|
+
* backoff; retry the whole sync a few times once that transaction ends.
|
|
266
|
+
*/
|
|
267
|
+
scheduleHmrRetry(message) {
|
|
268
|
+
if (!message.includes("HMR transactions cannot be nested")) return;
|
|
269
|
+
this.hmrRetries = (this.hmrRetries ?? 0) + 1;
|
|
270
|
+
if (this.hmrRetries > 5 || this.controller.signal.aborted) return;
|
|
271
|
+
if (this.hmrRetryTimer !== undefined) clearTimeout(this.hmrRetryTimer);
|
|
272
|
+
const schedule = this.options.setTimeout ?? setTimeout;
|
|
273
|
+
this.hmrRetryTimer = schedule(() => {
|
|
274
|
+
this.hmrRetryTimer = undefined;
|
|
275
|
+
void this.refresh();
|
|
276
|
+
}, 3000 * this.hmrRetries);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* A failed plain listing must not leave image models hidden until the next
|
|
281
|
+
* five-minute refresh. Re-read soon, at most twice per failure streak.
|
|
282
|
+
*/
|
|
283
|
+
scheduleListingRetry() {
|
|
284
|
+
if (this.listingOk !== false) {
|
|
285
|
+
this.listingRetries = 0;
|
|
286
|
+
if (this.listingRetryTimer !== undefined) {
|
|
287
|
+
clearTimeout(this.listingRetryTimer);
|
|
288
|
+
this.listingRetryTimer = undefined;
|
|
289
|
+
}
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
if (this.listingRetries >= 2 || this.controller.signal.aborted || this.listingRetryTimer !== undefined) return;
|
|
293
|
+
this.listingRetries += 1;
|
|
294
|
+
const schedule = this.options.setTimeout ?? setTimeout;
|
|
295
|
+
this.listingRetryTimer = schedule(() => {
|
|
296
|
+
this.listingRetryTimer = undefined;
|
|
297
|
+
if (!this.controller.signal.aborted) void this.refresh();
|
|
298
|
+
}, 4000);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
async syncOnce() {
|
|
302
|
+
const config = this.options.config();
|
|
303
|
+
const probe = this.probeRequested === true;
|
|
304
|
+
this.probeRequested = false;
|
|
305
|
+
|
|
306
|
+
// An account whose route keys belong to another account must not write
|
|
307
|
+
// or remove them.
|
|
308
|
+
if (config.conflictWith !== undefined) {
|
|
309
|
+
this.syncedModels = [];
|
|
310
|
+
throw new Error(
|
|
311
|
+
`dsh-cliproxyapi: provider route "${config.provider}" overlaps account "${config.conflictWith}"; choose a different Provider Route ID`
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (!config.enabled) {
|
|
316
|
+
// A disabled account keeps no models in the picker.
|
|
317
|
+
await this.releaseRoutes([config.provider, this.activeProvider]);
|
|
318
|
+
this.activeProvider = undefined;
|
|
319
|
+
this.activeRoutes = undefined;
|
|
320
|
+
this.syncedModels = [];
|
|
321
|
+
let endpoint;
|
|
322
|
+
let probeError;
|
|
323
|
+
if (probe) {
|
|
324
|
+
try {
|
|
325
|
+
const { result } = await this.discover(config);
|
|
326
|
+
this.allDiscoveredModels = result.models;
|
|
327
|
+
this.listingOk = result.listingOk !== false;
|
|
328
|
+
this.seedSelection(config, this.chatModels(result.models));
|
|
329
|
+
endpoint = result.rootURL;
|
|
330
|
+
} catch (error) {
|
|
331
|
+
if (this.controller.signal.aborted) throw error;
|
|
332
|
+
probeError = error instanceof Error ? error.message : String(error);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
this.currentStatus = {
|
|
336
|
+
state: "disabled",
|
|
337
|
+
provider: config.provider,
|
|
338
|
+
displayName: config.displayName,
|
|
339
|
+
...(endpoint === undefined ? {} : { endpoint }),
|
|
340
|
+
modelCount: 0,
|
|
341
|
+
totalDiscovered: this.allDiscoveredModels.length,
|
|
342
|
+
...(probe ? { refreshedAt: new Date().toISOString() } : {}),
|
|
343
|
+
...(probeError === undefined ? {} : { error: probeError }),
|
|
344
|
+
};
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const { error: _prevErr, ...prevStatus } = this.currentStatus;
|
|
349
|
+
this.currentStatus = { ...prevStatus, state: "probing" };
|
|
350
|
+
|
|
351
|
+
const { result, apiKey, effectiveApiKeyEnv } = await this.discover(config);
|
|
352
|
+
// Keep the catalog visible in the panel even if the selection matches nothing.
|
|
353
|
+
this.allDiscoveredModels = result.models;
|
|
354
|
+
this.listingOk = result.listingOk !== false;
|
|
355
|
+
this.scheduleListingRetry();
|
|
356
|
+
const seeded = this.seedSelection(config, this.chatModels(result.models));
|
|
357
|
+
|
|
358
|
+
const effectiveConfig = {
|
|
359
|
+
...config,
|
|
360
|
+
...(seeded === undefined ? {} : { selectedModels: seeded }),
|
|
361
|
+
apiKeyEnv: effectiveApiKeyEnv,
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const built = buildPiAiRoutes(
|
|
365
|
+
effectiveConfig,
|
|
366
|
+
{ ...result, models: this.withKinds(result.models) },
|
|
367
|
+
apiKey !== undefined
|
|
368
|
+
);
|
|
369
|
+
this.syncedModels = built.orderedModels;
|
|
370
|
+
|
|
371
|
+
// Each managed route keeps unrelated fields a user set by hand (retry
|
|
372
|
+
// policy, image budgets) and takes this plugin's fields on top. A route
|
|
373
|
+
// key that no longer carries models is removed.
|
|
374
|
+
const reserved = new Set(config.reservedKeys ?? []);
|
|
375
|
+
const ownedKeys = new Set(
|
|
376
|
+
[
|
|
377
|
+
...managedRouteKeys(config.provider),
|
|
378
|
+
...(this.activeProvider !== undefined && this.activeProvider !== config.provider
|
|
379
|
+
? managedRouteKeys(this.activeProvider)
|
|
380
|
+
: []),
|
|
381
|
+
].filter((key) => !reserved.has(key))
|
|
382
|
+
);
|
|
383
|
+
const ops = [];
|
|
384
|
+
const nextRoutes = {};
|
|
385
|
+
for (const key of ownedKeys) {
|
|
386
|
+
const existing = this.options.readExistingPiAiRoute?.(key);
|
|
387
|
+
const wanted = built.routes[key];
|
|
388
|
+
if (wanted === undefined) {
|
|
389
|
+
if (existing !== undefined) ops.push({ op: "unset", path: ["providers", key] });
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
const merged = {
|
|
393
|
+
...(existing && typeof existing === "object" ? stripManagedFields(existing) : {}),
|
|
394
|
+
...wanted,
|
|
395
|
+
};
|
|
396
|
+
nextRoutes[key] = merged;
|
|
397
|
+
if (!sameJson(existing, merged)) {
|
|
398
|
+
ops.push({ op: "set", path: ["providers", key], value: merged });
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
if (ops.length > 0) {
|
|
402
|
+
await mutateSettingsSafe(this.options.settings, PI_AI_NS, ops);
|
|
403
|
+
}
|
|
404
|
+
this.activeProvider = config.provider;
|
|
405
|
+
this.activeRoutes = nextRoutes;
|
|
406
|
+
await this.followDefaultModel(built.orderedModels, ownedKeys);
|
|
407
|
+
|
|
408
|
+
const protocolCounts = {};
|
|
409
|
+
for (const m of built.orderedModels) protocolCounts[m.protocol] = (protocolCounts[m.protocol] ?? 0) + 1;
|
|
410
|
+
this.currentStatus = {
|
|
411
|
+
state: "connected",
|
|
412
|
+
endpoint: result.rootURL,
|
|
413
|
+
provider: config.provider,
|
|
414
|
+
displayName: config.displayName,
|
|
415
|
+
routes: Object.keys(built.routes),
|
|
416
|
+
protocolCounts,
|
|
417
|
+
modelCount: built.orderedModels.length,
|
|
418
|
+
totalDiscovered: result.models.length,
|
|
419
|
+
refreshedAt: new Date().toISOString(),
|
|
420
|
+
};
|
|
421
|
+
this.options.logger?.info?.(
|
|
422
|
+
`dsh-cliproxyapi: connected to ${result.rootURL}; synchronized ${built.orderedModels.length}/${result.models.length} model(s) into ${Object.keys(built.routes).join(", ")}`
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Keep the agent default model reachable when protocol routing moves it to
|
|
428
|
+
* a sibling route (e.g. `cli-proxy` -> `cli-proxy-messages`).
|
|
429
|
+
*/
|
|
430
|
+
async followDefaultModel(models, ownedKeys) {
|
|
431
|
+
const current = this.options.readDefaultModel?.();
|
|
432
|
+
if (current === undefined || !ownedKeys.has(current.provider)) return;
|
|
433
|
+
const target = models.find((m) => m.id === current.model);
|
|
434
|
+
if (target === undefined || target.route === current.provider) return;
|
|
435
|
+
try {
|
|
436
|
+
await mutateSettingsSafe(this.options.settings, DEFAULT_MODEL_NS, [
|
|
437
|
+
{ op: "set", path: ["provider"], value: target.route },
|
|
438
|
+
]);
|
|
439
|
+
} catch (error) {
|
|
440
|
+
this.options.logger?.warn?.(
|
|
441
|
+
`dsh-cliproxyapi: could not move default model to ${target.route}: ${error instanceof Error ? error.message : String(error)}`
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
@@ -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
|
+
}
|