@goodandready/dsh-ollama-cloud-provider 0.1.36 → 0.1.38
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 +13 -0
- package/README.md +5 -4
- package/README.ru.md +5 -4
- package/README.zh.md +5 -4
- package/lib/account-rules.js +26 -0
- package/lib/adapter.d.ts +92 -0
- package/lib/adapter.js +566 -0
- package/lib/analytics.d.ts +81 -0
- package/lib/analytics.js +314 -0
- package/lib/attempt.js +29 -0
- package/lib/best-effort.js +20 -0
- package/lib/catalog-cache.js +81 -0
- package/lib/catalog.d.ts +10 -0
- package/lib/catalog.js +309 -0
- package/lib/commands.d.ts +14 -0
- package/lib/commands.js +228 -0
- package/lib/config.d.ts +173 -0
- package/lib/config.js +279 -0
- package/lib/context-override.js +38 -0
- package/lib/credentials.d.ts +23 -0
- package/lib/credentials.js +105 -0
- package/lib/discovery.js +108 -0
- package/lib/errors.d.ts +9 -0
- package/lib/errors.js +72 -0
- package/lib/files.js +13 -0
- package/lib/images.js +78 -0
- package/lib/index.d.ts +10 -521
- package/lib/index.js +20 -5909
- package/lib/local-host.js +38 -0
- package/lib/messages.js +263 -0
- package/lib/models.generated.d.ts +26 -0
- package/lib/models.generated.js +499 -0
- package/lib/plugin-updater.js +293 -0
- package/lib/pricing.js +78 -0
- package/lib/provider-ids.d.ts +4 -0
- package/lib/provider-ids.js +9 -0
- package/lib/quota-model.js +66 -0
- package/lib/reasoning.js +71 -0
- package/lib/request-log.d.ts +38 -0
- package/lib/request-log.js +194 -0
- package/lib/retry.js +24 -0
- package/lib/routes-public.js +59 -0
- package/lib/routes-values.js +129 -0
- package/lib/routes.d.ts +28 -0
- package/lib/routes.js +677 -0
- package/lib/routing.js +20 -0
- package/lib/sanitize.js +43 -0
- package/lib/status.js +42 -0
- package/lib/tool-schema.js +109 -0
- package/lib/tools.js +38 -0
- package/lib/transports/native.js +156 -0
- package/lib/transports/ndjson.js +73 -0
- package/lib/transports/openai-chat.js +278 -0
- package/lib/transports/sse.js +57 -0
- package/lib/transports/tool-call-id.js +17 -0
- package/lib/tui-settings.js +246 -0
- package/lib/usage.d.ts +69 -0
- package/lib/usage.js +277 -0
- package/lib/web.js +143 -0
- package/package.json +6 -5
package/lib/config.d.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import z from "@deepseek-ai/schemastery";
|
|
2
|
+
type TransportId = 'native' | 'openai-chat';
|
|
3
|
+
interface AccountConfig {
|
|
4
|
+
label?: string;
|
|
5
|
+
apiKeyEnv?: string;
|
|
6
|
+
apiKey?: string;
|
|
7
|
+
}
|
|
8
|
+
interface ModelAccountRule {
|
|
9
|
+
pattern: string;
|
|
10
|
+
account: string;
|
|
11
|
+
}
|
|
12
|
+
interface ModelPriceOverride {
|
|
13
|
+
input?: number;
|
|
14
|
+
output?: number;
|
|
15
|
+
cacheRead?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Plugin configuration. Every field except the literal `apiKey` is volatile:
|
|
19
|
+
* the harness projects settings forms from schema fields carrying that marker.
|
|
20
|
+
*/
|
|
21
|
+
interface ModelOverride {
|
|
22
|
+
contextWindow?: number;
|
|
23
|
+
image?: boolean;
|
|
24
|
+
thinking?: boolean;
|
|
25
|
+
reasoningEfforts?: string[];
|
|
26
|
+
defaultEffort?: string;
|
|
27
|
+
enabled?: boolean;
|
|
28
|
+
}
|
|
29
|
+
interface ManualModel {
|
|
30
|
+
id: string;
|
|
31
|
+
enabled?: boolean;
|
|
32
|
+
name?: string;
|
|
33
|
+
contextWindow?: number;
|
|
34
|
+
maxTokens?: number;
|
|
35
|
+
image?: boolean;
|
|
36
|
+
thinking?: boolean;
|
|
37
|
+
reasoningEfforts?: string[];
|
|
38
|
+
defaultReasoning?: string;
|
|
39
|
+
tools?: boolean;
|
|
40
|
+
}
|
|
41
|
+
interface Config {
|
|
42
|
+
/** Credential reference resolved per request; defaults to `OLLAMA_API_KEY`. */
|
|
43
|
+
apiKeyEnv?: string;
|
|
44
|
+
/** Literal key for composition config only; stripped from settings reads. */
|
|
45
|
+
apiKey?: string;
|
|
46
|
+
/** Native API root; defaults to `https://ollama.com`. */
|
|
47
|
+
apiBase?: string;
|
|
48
|
+
/** OpenAI-compatible root; defaults to `https://ollama.com/v1`. */
|
|
49
|
+
openAiBase?: string;
|
|
50
|
+
/** Cloud usage endpoint. */
|
|
51
|
+
usageUrl?: string;
|
|
52
|
+
/** Cloud balance endpoint. */
|
|
53
|
+
balanceUrl?: string;
|
|
54
|
+
/** Default wire protocol for chat. */
|
|
55
|
+
transport?: TransportId;
|
|
56
|
+
/** Per-model transport overrides; wins over `transport`. */
|
|
57
|
+
modelTransports?: Record<string, TransportId>;
|
|
58
|
+
/** Milliseconds to wait for response headers; defaults to 300s. */
|
|
59
|
+
requestTimeoutMs?: number;
|
|
60
|
+
/** Milliseconds a stream may stall; defaults to 300s. */
|
|
61
|
+
streamIdleTimeoutMs?: number;
|
|
62
|
+
/** Whole-attempt budget; 0 disables the independent budget. */
|
|
63
|
+
attemptTimeoutMs?: number;
|
|
64
|
+
/** Pre-response retry count; 0 disables internal retries. */
|
|
65
|
+
retryMaxRetries?: number;
|
|
66
|
+
retryBaseDelayMs?: number;
|
|
67
|
+
retryMaxDelayMs?: number;
|
|
68
|
+
/** Jitter fraction, 0..1. */
|
|
69
|
+
retryJitter?: number;
|
|
70
|
+
/** Visible-model allowlist; empty with `showAllModels:false` means none. */
|
|
71
|
+
visibleModels?: string[];
|
|
72
|
+
/** Explicit all-models switch; absent means derive from `visibleModels`. */
|
|
73
|
+
showAllModels?: boolean;
|
|
74
|
+
/** Per-model visibility overrides; wins over `visibleModels`. */
|
|
75
|
+
modelVisibility?: Record<string, boolean>;
|
|
76
|
+
/** Manual catalog entries and overrides; merged over live discovery. */
|
|
77
|
+
manualModels?: ManualModel[];
|
|
78
|
+
/** Per-model capability/thinking overrides; wins over manual entries. */
|
|
79
|
+
modelOverrides?: Record<string, ModelOverride>;
|
|
80
|
+
/** Stable picker order; ids not listed follow by name. */
|
|
81
|
+
modelOrder?: string[];
|
|
82
|
+
/** Hide models the cloud marks unsupported in the API; defaults to true. */
|
|
83
|
+
filterModelsByPlan?: boolean;
|
|
84
|
+
/** Global context-window override. */
|
|
85
|
+
contextWindow?: number;
|
|
86
|
+
/** Per-model context-window overrides. */
|
|
87
|
+
contextWindowOverrides?: Record<string, number>;
|
|
88
|
+
/** Per-turn output cap; `0` omits `num_predict`/`max_tokens`. */
|
|
89
|
+
maxOutputTokens?: number;
|
|
90
|
+
/** Optional Ollama native generation defaults. */
|
|
91
|
+
temperature?: number;
|
|
92
|
+
topK?: number;
|
|
93
|
+
topP?: number;
|
|
94
|
+
minP?: number;
|
|
95
|
+
seed?: number;
|
|
96
|
+
repeatPenalty?: number;
|
|
97
|
+
repeatLastN?: number;
|
|
98
|
+
numCtx?: number;
|
|
99
|
+
keepAlive?: string;
|
|
100
|
+
stop?: string[];
|
|
101
|
+
/** Refresh the model catalog at mount and on an interval. */
|
|
102
|
+
autoRefresh?: boolean;
|
|
103
|
+
/** Catalog refresh interval in minutes; 0 = mount only. */
|
|
104
|
+
refreshMinutes?: number;
|
|
105
|
+
/** Extra accounts for rotation. */
|
|
106
|
+
accounts?: AccountConfig[];
|
|
107
|
+
/** Preferred account per model glob; others remain fallbacks. */
|
|
108
|
+
modelAccountRules?: ModelAccountRule[];
|
|
109
|
+
/** Prefer the last successful account for a conversation. */
|
|
110
|
+
stickySession?: boolean;
|
|
111
|
+
/** Pinned account label; empty means automatic. */
|
|
112
|
+
activeAccount?: string;
|
|
113
|
+
/** Show the quota card in the sidebar. */
|
|
114
|
+
showSidebarQuota?: boolean;
|
|
115
|
+
/** Optional plan label shown on the sidebar card, for example PRO. */
|
|
116
|
+
planLabel?: string;
|
|
117
|
+
/** Per-model price overrides in USD per 1M tokens. */
|
|
118
|
+
priceOverrides?: Record<string, ModelPriceOverride>;
|
|
119
|
+
/** Product user agent sent to Ollama Cloud. */
|
|
120
|
+
userAgent?: string;
|
|
121
|
+
}
|
|
122
|
+
declare const Config: z<Config>;
|
|
123
|
+
interface ResolvedConfig {
|
|
124
|
+
apiKeyEnv: string;
|
|
125
|
+
apiKey?: string;
|
|
126
|
+
apiBase: string;
|
|
127
|
+
openAiBase: string;
|
|
128
|
+
usageUrl: string;
|
|
129
|
+
balanceUrl: string;
|
|
130
|
+
transport: TransportId;
|
|
131
|
+
modelTransports: Readonly<Record<string, TransportId>>;
|
|
132
|
+
requestTimeoutMs: number;
|
|
133
|
+
streamIdleTimeoutMs: number;
|
|
134
|
+
attemptTimeoutMs: number;
|
|
135
|
+
retryMaxRetries: number;
|
|
136
|
+
retryBaseDelayMs: number;
|
|
137
|
+
retryMaxDelayMs: number;
|
|
138
|
+
retryJitter: number;
|
|
139
|
+
visibleModels: readonly string[];
|
|
140
|
+
showAllModels: boolean;
|
|
141
|
+
modelVisibility: Readonly<Record<string, boolean>>;
|
|
142
|
+
manualModels: readonly ManualModel[];
|
|
143
|
+
modelOverrides: Readonly<Record<string, ModelOverride>>;
|
|
144
|
+
modelOrder: readonly string[];
|
|
145
|
+
filterModelsByPlan: boolean;
|
|
146
|
+
contextWindow?: number;
|
|
147
|
+
contextWindowOverrides: Readonly<Record<string, number>>;
|
|
148
|
+
maxOutputTokens?: number;
|
|
149
|
+
temperature?: number;
|
|
150
|
+
topK?: number;
|
|
151
|
+
topP?: number;
|
|
152
|
+
minP?: number;
|
|
153
|
+
seed?: number;
|
|
154
|
+
repeatPenalty?: number;
|
|
155
|
+
repeatLastN?: number;
|
|
156
|
+
numCtx?: number;
|
|
157
|
+
keepAlive?: string;
|
|
158
|
+
stop?: readonly string[];
|
|
159
|
+
autoRefresh: boolean;
|
|
160
|
+
refreshMinutes: number;
|
|
161
|
+
accounts: readonly AccountConfig[];
|
|
162
|
+
modelAccountRules: readonly ModelAccountRule[];
|
|
163
|
+
stickySession: boolean;
|
|
164
|
+
activeAccount: string;
|
|
165
|
+
showSidebarQuota: boolean;
|
|
166
|
+
planLabel: string;
|
|
167
|
+
priceOverrides: Readonly<Record<string, ModelPriceOverride>>;
|
|
168
|
+
userAgent: string;
|
|
169
|
+
}
|
|
170
|
+
/** Normalize a raw config (possibly bypassing schemastery) into request facts. */
|
|
171
|
+
declare function resolveConfig(config: Config): ResolvedConfig;
|
|
172
|
+
export { AccountConfig, Config, ManualModel, ModelAccountRule, ModelOverride, ModelPriceOverride, ResolvedConfig, TransportId, resolveConfig };
|
|
173
|
+
//# sourceMappingURL=config.d.ts.map
|
package/lib/config.js
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import z from "@deepseek-ai/schemastery";
|
|
2
|
+
// src/config.ts
|
|
3
|
+
const NS = "llm-ollama-cloud";
|
|
4
|
+
const DEFAULT_API_KEY_ENV = "OLLAMA_API_KEY";
|
|
5
|
+
const DEFAULT_API_BASE = "https://ollama.com";
|
|
6
|
+
const DEFAULT_OPENAI_BASE = "https://ollama.com/v1";
|
|
7
|
+
const DEFAULT_USAGE_URL = "https://ollama.com/api/usage";
|
|
8
|
+
const DEFAULT_BALANCE_URL = "https://ollama.com/api/balance";
|
|
9
|
+
const DEFAULT_USER_AGENT = "dsh-ollama-cloud-provider/0.1.38";
|
|
10
|
+
const DEFAULT_CONTEXT_WINDOW = 131072;
|
|
11
|
+
const DEFAULT_MAX_TOKENS = 32768;
|
|
12
|
+
const DEFAULT_GENERATE_MAX_TOKENS = 131072;
|
|
13
|
+
function markVolatile(schema) {
|
|
14
|
+
const mark = schema.volatile;
|
|
15
|
+
return typeof mark === "function" ? mark.call(schema) : schema;
|
|
16
|
+
}
|
|
17
|
+
const Config = z.object({
|
|
18
|
+
apiKeyEnv: markVolatile(z.string().role("credential-ref").default(DEFAULT_API_KEY_ENV)),
|
|
19
|
+
apiKey: z.string().role("secret"),
|
|
20
|
+
apiBase: markVolatile(z.string().default(DEFAULT_API_BASE)),
|
|
21
|
+
openAiBase: markVolatile(z.string().default(DEFAULT_OPENAI_BASE)),
|
|
22
|
+
usageUrl: markVolatile(z.string().default(DEFAULT_USAGE_URL)),
|
|
23
|
+
balanceUrl: markVolatile(z.string().default(DEFAULT_BALANCE_URL)),
|
|
24
|
+
transport: markVolatile(z.union(["native", "openai-chat"]).default("native")),
|
|
25
|
+
modelTransports: markVolatile(z.dict(z.union(["native", "openai-chat"])).default({})),
|
|
26
|
+
requestTimeoutMs: markVolatile(z.number().min(1).max(2147483647).default(3e5)),
|
|
27
|
+
streamIdleTimeoutMs: markVolatile(z.number().min(1).max(2147483647).default(3e5)),
|
|
28
|
+
attemptTimeoutMs: markVolatile(z.number().min(0).max(36e5).step(1).default(0)),
|
|
29
|
+
retryMaxRetries: markVolatile(z.number().min(0).max(10).step(1).default(0)),
|
|
30
|
+
retryBaseDelayMs: markVolatile(z.number().min(0).max(6e4).step(1).default(250)),
|
|
31
|
+
retryMaxDelayMs: markVolatile(z.number().min(0).max(12e4).step(1).default(2e3)),
|
|
32
|
+
retryJitter: markVolatile(z.number().min(0).max(1).default(.2)),
|
|
33
|
+
visibleModels: markVolatile(z.array(z.string()).default([])),
|
|
34
|
+
showAllModels: markVolatile(z.boolean()),
|
|
35
|
+
modelVisibility: markVolatile(z.dict(z.boolean()).default({})),
|
|
36
|
+
manualModels: markVolatile(z.array(z.object({
|
|
37
|
+
id: z.string(),
|
|
38
|
+
enabled: z.boolean().default(true),
|
|
39
|
+
name: z.string(),
|
|
40
|
+
contextWindow: z.number().min(1).max(1e7).step(1),
|
|
41
|
+
maxTokens: z.number().min(1).max(1e7).step(1),
|
|
42
|
+
image: z.boolean(),
|
|
43
|
+
thinking: z.boolean(),
|
|
44
|
+
reasoningEfforts: z.array(z.string()),
|
|
45
|
+
defaultReasoning: z.string(),
|
|
46
|
+
tools: z.boolean()
|
|
47
|
+
})).default([])),
|
|
48
|
+
modelOverrides: markVolatile(z.dict(z.object({
|
|
49
|
+
contextWindow: z.number().min(1).max(1e7).step(1),
|
|
50
|
+
image: z.boolean(),
|
|
51
|
+
thinking: z.boolean(),
|
|
52
|
+
reasoningEfforts: z.array(z.string()),
|
|
53
|
+
defaultEffort: z.string(),
|
|
54
|
+
enabled: z.boolean()
|
|
55
|
+
})).default({})),
|
|
56
|
+
modelOrder: markVolatile(z.array(z.string()).default([])),
|
|
57
|
+
filterModelsByPlan: markVolatile(z.boolean().default(true)),
|
|
58
|
+
contextWindow: markVolatile(z.number().min(1).max(1e7).step(1)),
|
|
59
|
+
contextWindowOverrides: markVolatile(z.dict(z.number().min(1).max(1e7).step(1)).default({})),
|
|
60
|
+
maxOutputTokens: markVolatile(z.number().min(0).max(1e7).step(1)),
|
|
61
|
+
temperature: markVolatile(z.number().min(0).max(2)),
|
|
62
|
+
topK: markVolatile(z.number().min(1).max(1e6).step(1)),
|
|
63
|
+
topP: markVolatile(z.number().min(0).max(1)),
|
|
64
|
+
minP: markVolatile(z.number().min(0).max(1)),
|
|
65
|
+
seed: markVolatile(z.number().min(0).max(4294967295).step(1)),
|
|
66
|
+
repeatPenalty: markVolatile(z.number().min(0).max(2)),
|
|
67
|
+
repeatLastN: markVolatile(z.number().min(-1).max(1e6).step(1)),
|
|
68
|
+
numCtx: markVolatile(z.number().min(1).max(1e7).step(1)),
|
|
69
|
+
keepAlive: markVolatile(z.string()),
|
|
70
|
+
stop: markVolatile(z.array(z.string())),
|
|
71
|
+
autoRefresh: markVolatile(z.boolean().default(true)),
|
|
72
|
+
refreshMinutes: markVolatile(z.number().min(0).max(10080).step(1).default(1440)),
|
|
73
|
+
accounts: markVolatile(z.array(z.object({
|
|
74
|
+
label: z.string(),
|
|
75
|
+
apiKeyEnv: z.string().role("credential-ref"),
|
|
76
|
+
apiKey: z.string().role("secret")
|
|
77
|
+
})).default([])),
|
|
78
|
+
modelAccountRules: markVolatile(z.array(z.object({
|
|
79
|
+
pattern: z.string(),
|
|
80
|
+
account: z.string()
|
|
81
|
+
})).default([])),
|
|
82
|
+
stickySession: markVolatile(z.boolean().default(true)),
|
|
83
|
+
activeAccount: markVolatile(z.string().default("")),
|
|
84
|
+
showSidebarQuota: markVolatile(z.boolean().default(false)),
|
|
85
|
+
planLabel: markVolatile(z.string().default("")),
|
|
86
|
+
priceOverrides: markVolatile(z.dict(z.object({
|
|
87
|
+
input: z.number().min(0),
|
|
88
|
+
output: z.number().min(0),
|
|
89
|
+
cacheRead: z.number().min(0)
|
|
90
|
+
})).default({})),
|
|
91
|
+
userAgent: markVolatile(z.string().default(DEFAULT_USER_AGENT))
|
|
92
|
+
});
|
|
93
|
+
/** Read each volatile field through its live reference, if the loader installed one. */
|
|
94
|
+
function unwrapVolatileConfig(config) {
|
|
95
|
+
const source = config;
|
|
96
|
+
const out = {};
|
|
97
|
+
for (const key of Object.keys(source)) {
|
|
98
|
+
const value = source[key];
|
|
99
|
+
out[key] = value !== null && typeof value === "object" && typeof value.get === "function" ? value.get() : value;
|
|
100
|
+
}
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
function positiveInt(value, fallback) {
|
|
104
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.trunc(value) : fallback;
|
|
105
|
+
}
|
|
106
|
+
function numberInRange(value, min, max) {
|
|
107
|
+
return typeof value === "number" && Number.isFinite(value) && value >= min && value <= max ? value : void 0;
|
|
108
|
+
}
|
|
109
|
+
function integerInRange(value, min, max) {
|
|
110
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < min || value > max) return void 0;
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
function nonEmptyStrings(value) {
|
|
114
|
+
if (!Array.isArray(value)) return void 0;
|
|
115
|
+
const out = value.filter((entry) => typeof entry === "string" && entry.trim() !== "");
|
|
116
|
+
return out.length === 0 ? void 0 : out;
|
|
117
|
+
}
|
|
118
|
+
function modelOverrideRecord(value) {
|
|
119
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return {};
|
|
120
|
+
const out = {};
|
|
121
|
+
for (const [id, entry] of Object.entries(value)) {
|
|
122
|
+
if (id === "" || id.includes("..") || typeof entry !== "object" || entry === null || Array.isArray(entry)) continue;
|
|
123
|
+
const record = entry;
|
|
124
|
+
const contextWindow = integerInRange(record.contextWindow, 1, 1e7);
|
|
125
|
+
const efforts = Array.isArray(record.reasoningEfforts) ? record.reasoningEfforts.filter((item) => typeof item === "string" && item.trim() !== "") : void 0;
|
|
126
|
+
const thinking = typeof record.thinking === "boolean" ? record.thinking : void 0;
|
|
127
|
+
const normalizedEfforts = thinking === true && (efforts === void 0 || efforts.length === 0) ? ["off", "high"] : thinking === false ? [] : efforts;
|
|
128
|
+
const defaultEffort = typeof record.defaultEffort === "string" && record.defaultEffort.trim() !== "" ? record.defaultEffort.trim() : void 0;
|
|
129
|
+
if (contextWindow === void 0 && typeof record.image !== "boolean" && thinking === void 0 && efforts === void 0 && defaultEffort === void 0 && typeof record.enabled !== "boolean") continue;
|
|
130
|
+
out[id] = {
|
|
131
|
+
...contextWindow === void 0 ? {} : { contextWindow },
|
|
132
|
+
...typeof record.image === "boolean" ? { image: record.image } : {},
|
|
133
|
+
...thinking === void 0 ? {} : { thinking },
|
|
134
|
+
...normalizedEfforts === void 0 ? {} : { reasoningEfforts: normalizedEfforts },
|
|
135
|
+
...defaultEffort === void 0 ? {} : { defaultEffort },
|
|
136
|
+
...typeof record.enabled === "boolean" ? { enabled: record.enabled } : {}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
function manualModelList(value) {
|
|
142
|
+
if (!Array.isArray(value)) return [];
|
|
143
|
+
const out = [];
|
|
144
|
+
for (const entry of value) {
|
|
145
|
+
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) continue;
|
|
146
|
+
const record = entry;
|
|
147
|
+
const id = typeof record.id === "string" ? record.id.trim() : "";
|
|
148
|
+
if (id === "" || id.includes("..") || !/^[A-Za-z0-9][A-Za-z0-9._:/-]*$/.test(id)) continue;
|
|
149
|
+
const name = typeof record.name === "string" ? record.name.trim() : "";
|
|
150
|
+
const contextWindow = integerInRange(record.contextWindow, 1, 1e7);
|
|
151
|
+
const maxTokens = integerInRange(record.maxTokens, 1, 1e7);
|
|
152
|
+
const efforts = Array.isArray(record.reasoningEfforts) ? record.reasoningEfforts.filter((item) => typeof item === "string" && item.trim() !== "") : void 0;
|
|
153
|
+
const thinking = typeof record.thinking === "boolean" ? record.thinking : void 0;
|
|
154
|
+
const normalizedEfforts = thinking === true && (efforts === void 0 || efforts.length === 0) ? ["off", "high"] : thinking === false ? [] : efforts;
|
|
155
|
+
const defaultReasoning = typeof record.defaultReasoning === "string" && record.defaultReasoning.trim() !== "" ? record.defaultReasoning.trim() : void 0;
|
|
156
|
+
out.push({
|
|
157
|
+
id,
|
|
158
|
+
enabled: record.enabled !== false,
|
|
159
|
+
...name === "" ? {} : { name },
|
|
160
|
+
...contextWindow === void 0 ? {} : { contextWindow },
|
|
161
|
+
...maxTokens === void 0 ? {} : { maxTokens },
|
|
162
|
+
...typeof record.image === "boolean" ? { image: record.image } : {},
|
|
163
|
+
...thinking === void 0 ? {} : { thinking },
|
|
164
|
+
...normalizedEfforts === void 0 ? {} : { reasoningEfforts: normalizedEfforts },
|
|
165
|
+
...defaultReasoning === void 0 ? {} : { defaultReasoning },
|
|
166
|
+
...typeof record.tools === "boolean" ? { tools: record.tools } : {}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
return out;
|
|
170
|
+
}
|
|
171
|
+
function recordOf(value) {
|
|
172
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : {};
|
|
173
|
+
}
|
|
174
|
+
function numberRecord(value) {
|
|
175
|
+
const out = {};
|
|
176
|
+
for (const [key, entry] of Object.entries(recordOf(value))) if (typeof entry === "number" && Number.isInteger(entry) && entry > 0) out[key] = entry;
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
function booleanRecord(value) {
|
|
180
|
+
const out = {};
|
|
181
|
+
for (const [key, entry] of Object.entries(recordOf(value))) if (typeof entry === "boolean") out[key] = entry;
|
|
182
|
+
return out;
|
|
183
|
+
}
|
|
184
|
+
function priceRecord(value) {
|
|
185
|
+
const out = {};
|
|
186
|
+
for (const [key, entry] of Object.entries(recordOf(value))) {
|
|
187
|
+
const raw = recordOf(entry);
|
|
188
|
+
const price = {};
|
|
189
|
+
if (typeof raw.input === "number" && raw.input >= 0) price.input = raw.input;
|
|
190
|
+
if (typeof raw.output === "number" && raw.output >= 0) price.output = raw.output;
|
|
191
|
+
if (typeof raw.cacheRead === "number" && raw.cacheRead >= 0) price.cacheRead = raw.cacheRead;
|
|
192
|
+
if (Object.keys(price).length > 0) out[key] = price;
|
|
193
|
+
}
|
|
194
|
+
return out;
|
|
195
|
+
}
|
|
196
|
+
const TRANSPORTS = /* @__PURE__ */ new Set(["native", "openai-chat"]);
|
|
197
|
+
function transportRecord(value) {
|
|
198
|
+
const out = {};
|
|
199
|
+
for (const [key, entry] of Object.entries(recordOf(value))) if (typeof entry === "string" && TRANSPORTS.has(entry)) out[key] = entry;
|
|
200
|
+
return out;
|
|
201
|
+
}
|
|
202
|
+
function ruleList(value) {
|
|
203
|
+
if (!Array.isArray(value)) return [];
|
|
204
|
+
const out = [];
|
|
205
|
+
for (const entry of value) {
|
|
206
|
+
const raw = recordOf(entry);
|
|
207
|
+
const pattern = typeof raw.pattern === "string" ? raw.pattern.trim() : "";
|
|
208
|
+
const account = typeof raw.account === "string" ? raw.account.trim() : "";
|
|
209
|
+
if (pattern !== "" && account !== "") out.push({
|
|
210
|
+
pattern,
|
|
211
|
+
account
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
return out;
|
|
215
|
+
}
|
|
216
|
+
/** Normalize a raw config (possibly bypassing schemastery) into request facts. */
|
|
217
|
+
function resolveConfig(config) {
|
|
218
|
+
const apiKey = typeof config.apiKey === "string" && config.apiKey.trim() !== "" ? config.apiKey.trim() : void 0;
|
|
219
|
+
const visibleModels = Array.isArray(config.visibleModels) ? config.visibleModels.filter((id) => typeof id === "string" && id !== "") : [];
|
|
220
|
+
const temperature = numberInRange(config.temperature, 0, 2);
|
|
221
|
+
const topK = integerInRange(config.topK, 1, 1e6);
|
|
222
|
+
const topP = numberInRange(config.topP, 0, 1);
|
|
223
|
+
const minP = numberInRange(config.minP, 0, 1);
|
|
224
|
+
const seed = integerInRange(config.seed, 0, 4294967295);
|
|
225
|
+
const repeatPenalty = numberInRange(config.repeatPenalty, 0, 2);
|
|
226
|
+
const repeatLastN = integerInRange(config.repeatLastN, -1, 1e6);
|
|
227
|
+
const numCtx = integerInRange(config.numCtx, 1, 1e7);
|
|
228
|
+
const stop = nonEmptyStrings(config.stop);
|
|
229
|
+
return {
|
|
230
|
+
apiKeyEnv: typeof config.apiKeyEnv === "string" && config.apiKeyEnv.trim() !== "" ? config.apiKeyEnv.trim() : DEFAULT_API_KEY_ENV,
|
|
231
|
+
...apiKey === void 0 ? {} : { apiKey },
|
|
232
|
+
apiBase: typeof config.apiBase === "string" && config.apiBase !== "" ? config.apiBase : DEFAULT_API_BASE,
|
|
233
|
+
openAiBase: typeof config.openAiBase === "string" && config.openAiBase !== "" ? config.openAiBase : DEFAULT_OPENAI_BASE,
|
|
234
|
+
usageUrl: typeof config.usageUrl === "string" && config.usageUrl !== "" ? config.usageUrl : DEFAULT_USAGE_URL,
|
|
235
|
+
balanceUrl: typeof config.balanceUrl === "string" && config.balanceUrl !== "" ? config.balanceUrl : DEFAULT_BALANCE_URL,
|
|
236
|
+
transport: config.transport === "openai-chat" ? "openai-chat" : "native",
|
|
237
|
+
modelTransports: transportRecord(config.modelTransports),
|
|
238
|
+
requestTimeoutMs: positiveInt(config.requestTimeoutMs, 3e5),
|
|
239
|
+
streamIdleTimeoutMs: positiveInt(config.streamIdleTimeoutMs, 3e5),
|
|
240
|
+
attemptTimeoutMs: typeof config.attemptTimeoutMs === "number" && Number.isInteger(config.attemptTimeoutMs) && config.attemptTimeoutMs >= 0 && config.attemptTimeoutMs <= 36e5 ? config.attemptTimeoutMs : 0,
|
|
241
|
+
retryMaxRetries: typeof config.retryMaxRetries === "number" && Number.isInteger(config.retryMaxRetries) && config.retryMaxRetries >= 0 && config.retryMaxRetries <= 10 ? config.retryMaxRetries : 0,
|
|
242
|
+
retryBaseDelayMs: positiveInt(config.retryBaseDelayMs, 250),
|
|
243
|
+
retryMaxDelayMs: positiveInt(config.retryMaxDelayMs, 2e3),
|
|
244
|
+
retryJitter: typeof config.retryJitter === "number" && config.retryJitter >= 0 && config.retryJitter <= 1 ? config.retryJitter : .2,
|
|
245
|
+
visibleModels,
|
|
246
|
+
showAllModels: config.showAllModels === true ? true : config.showAllModels === false ? false : visibleModels.length === 0,
|
|
247
|
+
modelVisibility: booleanRecord(config.modelVisibility),
|
|
248
|
+
manualModels: manualModelList(config.manualModels),
|
|
249
|
+
modelOverrides: modelOverrideRecord(config.modelOverrides),
|
|
250
|
+
modelOrder: Array.isArray(config.modelOrder) ? config.modelOrder.filter((id) => typeof id === "string" && id !== "") : [],
|
|
251
|
+
filterModelsByPlan: config.filterModelsByPlan !== false,
|
|
252
|
+
...typeof config.contextWindow === "number" && Number.isInteger(config.contextWindow) && config.contextWindow > 0 ? { contextWindow: config.contextWindow } : {},
|
|
253
|
+
contextWindowOverrides: numberRecord(config.contextWindowOverrides),
|
|
254
|
+
...typeof config.maxOutputTokens === "number" && Number.isInteger(config.maxOutputTokens) && config.maxOutputTokens >= 0 ? { maxOutputTokens: config.maxOutputTokens } : {},
|
|
255
|
+
...temperature === void 0 ? {} : { temperature },
|
|
256
|
+
...topK === void 0 ? {} : { topK },
|
|
257
|
+
...topP === void 0 ? {} : { topP },
|
|
258
|
+
...minP === void 0 ? {} : { minP },
|
|
259
|
+
...seed === void 0 ? {} : { seed },
|
|
260
|
+
...repeatPenalty === void 0 ? {} : { repeatPenalty },
|
|
261
|
+
...repeatLastN === void 0 ? {} : { repeatLastN },
|
|
262
|
+
...numCtx === void 0 ? {} : { numCtx },
|
|
263
|
+
...typeof config.keepAlive === "string" && config.keepAlive.trim() !== "" ? { keepAlive: config.keepAlive.trim() } : {},
|
|
264
|
+
...stop === void 0 ? {} : { stop },
|
|
265
|
+
autoRefresh: config.autoRefresh !== false,
|
|
266
|
+
refreshMinutes: typeof config.refreshMinutes === "number" && Number.isInteger(config.refreshMinutes) && config.refreshMinutes >= 0 && config.refreshMinutes <= 10080 ? config.refreshMinutes : 1440,
|
|
267
|
+
accounts: Array.isArray(config.accounts) ? config.accounts : [],
|
|
268
|
+
modelAccountRules: ruleList(config.modelAccountRules),
|
|
269
|
+
stickySession: config.stickySession !== false,
|
|
270
|
+
activeAccount: typeof config.activeAccount === "string" ? config.activeAccount : "",
|
|
271
|
+
showSidebarQuota: config.showSidebarQuota === true,
|
|
272
|
+
planLabel: typeof config.planLabel === "string" ? config.planLabel.trim() : "",
|
|
273
|
+
priceOverrides: priceRecord(config.priceOverrides),
|
|
274
|
+
userAgent: typeof config.userAgent === "string" && config.userAgent !== "" ? config.userAgent : DEFAULT_USER_AGENT
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
export { Config, DEFAULT_API_BASE, DEFAULT_API_KEY_ENV, DEFAULT_BALANCE_URL, DEFAULT_CONTEXT_WINDOW, DEFAULT_GENERATE_MAX_TOKENS, DEFAULT_MAX_TOKENS, DEFAULT_OPENAI_BASE, DEFAULT_USAGE_URL, DEFAULT_USER_AGENT, NS, resolveConfig, unwrapVolatileConfig };
|
|
278
|
+
|
|
279
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// src/context-override.ts
|
|
2
|
+
const overrides = /* @__PURE__ */ new Map();
|
|
3
|
+
function sessionKey(sessionId) {
|
|
4
|
+
return sessionId === void 0 || sessionId === null ? "" : String(sessionId);
|
|
5
|
+
}
|
|
6
|
+
function setContextOverride(sessionId, value) {
|
|
7
|
+
overrides.set(sessionKey(sessionId), value);
|
|
8
|
+
}
|
|
9
|
+
function clearContextOverride(sessionId) {
|
|
10
|
+
overrides.delete(sessionKey(sessionId));
|
|
11
|
+
}
|
|
12
|
+
function contextOverrideFor(sessionId) {
|
|
13
|
+
return overrides.get(sessionKey(sessionId));
|
|
14
|
+
}
|
|
15
|
+
/** Session value wins; `follow` and an unset value use the configured num_ctx. */
|
|
16
|
+
function effectiveNumCtx(config, sessionId) {
|
|
17
|
+
const override = contextOverrideFor(sessionId);
|
|
18
|
+
return typeof override === "number" ? override : config.numCtx;
|
|
19
|
+
}
|
|
20
|
+
function parseContextValue(raw) {
|
|
21
|
+
const value = raw.trim().toLowerCase();
|
|
22
|
+
if (value === "" || value === "status") return void 0;
|
|
23
|
+
if (value === "follow") return "follow";
|
|
24
|
+
if (value === "reset") return "reset";
|
|
25
|
+
const match = /^(\d+)(k|m)?$/.exec(value);
|
|
26
|
+
if (match === null) return void 0;
|
|
27
|
+
const number = Number(match[1]) * (match[2] === "k" ? 1024 : match[2] === "m" ? 1048576 : 1);
|
|
28
|
+
if (!Number.isInteger(number) || number < 1024 || number > 1e7) return void 0;
|
|
29
|
+
return number;
|
|
30
|
+
}
|
|
31
|
+
function describeContext(config, sessionId) {
|
|
32
|
+
const override = contextOverrideFor(sessionId);
|
|
33
|
+
const effective = effectiveNumCtx(config, sessionId);
|
|
34
|
+
return `Context: session=${override === void 0 ? "unset" : override === "follow" ? "follow" : `${override} tokens`}, effective=${effective === void 0 ? "model default" : `${effective} tokens`}`;
|
|
35
|
+
}
|
|
36
|
+
export { clearContextOverride, contextOverrideFor, describeContext, effectiveNumCtx, parseContextValue, setContextOverride };
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=context-override.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ResolvedConfig } from "./config.js";
|
|
2
|
+
import { Context } from "@deepseek-ai/cordis";
|
|
3
|
+
interface ResolvedKey {
|
|
4
|
+
readonly key: string;
|
|
5
|
+
readonly source: string;
|
|
6
|
+
/** Slot label (`default` or the configured account name). */
|
|
7
|
+
readonly label?: string;
|
|
8
|
+
}
|
|
9
|
+
type KeyResolver = (provider: string) => Promise<ResolvedKey>;
|
|
10
|
+
interface KeyPool {
|
|
11
|
+
resolve(provider: string): Promise<ResolvedKey>;
|
|
12
|
+
resolveAll(provider: string): Promise<readonly ResolvedKey[]>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Resolve every configured account for one request. The default account comes
|
|
16
|
+
* first unless `activeAccount` pins another slot by label or credential name.
|
|
17
|
+
* Keys are deduplicated; a slot without a resolvable value is skipped.
|
|
18
|
+
*/
|
|
19
|
+
declare function createKeyPool(ctx: Context, config: () => ResolvedConfig): KeyPool;
|
|
20
|
+
/** Backwards-compatible single-key facade over the pool. */
|
|
21
|
+
declare function createKeyResolver(ctx: Context, config: () => ResolvedConfig): KeyResolver;
|
|
22
|
+
export { KeyPool, KeyResolver, ResolvedKey, createKeyPool, createKeyResolver };
|
|
23
|
+
//# sourceMappingURL=credentials.d.ts.map
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { bestEffortFailed } from "./best-effort.js";
|
|
2
|
+
import { LlmError } from "@deepseek-ai/dsh-llm";
|
|
3
|
+
import { credentialRef } from "@deepseek-ai/dsh-credentials";
|
|
4
|
+
import { launchEnvironmentOf } from "@deepseek-ai/dsh-launch-environment";
|
|
5
|
+
// src/credentials.ts
|
|
6
|
+
async function resolveRef(ctx, refName) {
|
|
7
|
+
const credentials = ctx.get("credentials");
|
|
8
|
+
if (credentials !== void 0) try {
|
|
9
|
+
const hit = await credentials.resolve(credentialRef(refName));
|
|
10
|
+
if (hit !== void 0 && hit.value.trim() !== "") return {
|
|
11
|
+
key: hit.value.trim(),
|
|
12
|
+
source: hit.source ?? refName
|
|
13
|
+
};
|
|
14
|
+
} catch (error) {
|
|
15
|
+
bestEffortFailed("credential reference is invalid", error);
|
|
16
|
+
}
|
|
17
|
+
const ambient = launchEnvironmentOf(ctx).get(refName);
|
|
18
|
+
if (ambient !== void 0 && ambient.value.trim() !== "") return {
|
|
19
|
+
key: ambient.value.trim(),
|
|
20
|
+
source: ambient.source
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function slotsOf(config) {
|
|
24
|
+
const slots = [{
|
|
25
|
+
label: "default",
|
|
26
|
+
...config.apiKey === void 0 ? {} : { literal: config.apiKey },
|
|
27
|
+
refName: config.apiKeyEnv
|
|
28
|
+
}];
|
|
29
|
+
for (const [index, account] of config.accounts.entries()) {
|
|
30
|
+
const label = typeof account.label === "string" && account.label.trim() !== "" ? account.label.trim() : `account-${index + 2}`;
|
|
31
|
+
const literal = typeof account.apiKey === "string" && account.apiKey.trim() !== "" ? account.apiKey.trim() : void 0;
|
|
32
|
+
const refName = typeof account.apiKeyEnv === "string" && account.apiKeyEnv.trim() !== "" ? account.apiKeyEnv.trim() : void 0;
|
|
33
|
+
if (literal === void 0 && refName === void 0) continue;
|
|
34
|
+
slots.push({
|
|
35
|
+
label,
|
|
36
|
+
...literal === void 0 ? {} : { literal },
|
|
37
|
+
...refName === void 0 ? {} : { refName }
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return slots;
|
|
41
|
+
}
|
|
42
|
+
async function resolveSlot(ctx, slot) {
|
|
43
|
+
if (slot.literal !== void 0) return {
|
|
44
|
+
key: slot.literal,
|
|
45
|
+
source: `${slot.label}:config.apiKey`,
|
|
46
|
+
label: slot.label
|
|
47
|
+
};
|
|
48
|
+
if (slot.refName !== void 0) {
|
|
49
|
+
const resolved = await resolveRef(ctx, slot.refName);
|
|
50
|
+
if (resolved !== void 0) return {
|
|
51
|
+
...resolved,
|
|
52
|
+
label: slot.label
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Resolve every configured account for one request. The default account comes
|
|
58
|
+
* first unless `activeAccount` pins another slot by label or credential name.
|
|
59
|
+
* Keys are deduplicated; a slot without a resolvable value is skipped.
|
|
60
|
+
*/
|
|
61
|
+
function createKeyPool(ctx, config) {
|
|
62
|
+
const all = async () => {
|
|
63
|
+
const current = config();
|
|
64
|
+
const pinned = current.activeAccount.trim();
|
|
65
|
+
const slots = slotsOf(current);
|
|
66
|
+
if (pinned !== "") {
|
|
67
|
+
const index = slots.findIndex((slot) => slot.label === pinned || slot.refName === pinned);
|
|
68
|
+
if (index > 0) {
|
|
69
|
+
const [slot] = slots.splice(index, 1);
|
|
70
|
+
if (slot !== void 0) slots.unshift(slot);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const resolved = [];
|
|
74
|
+
const seen = /* @__PURE__ */ new Set();
|
|
75
|
+
for (const slot of slots) {
|
|
76
|
+
const key = await resolveSlot(ctx, slot);
|
|
77
|
+
if (key === void 0 || seen.has(key.key)) continue;
|
|
78
|
+
seen.add(key.key);
|
|
79
|
+
resolved.push(key);
|
|
80
|
+
}
|
|
81
|
+
return resolved;
|
|
82
|
+
};
|
|
83
|
+
return {
|
|
84
|
+
async resolveAll() {
|
|
85
|
+
const keys = await all();
|
|
86
|
+
if (keys.length === 0) throw missingKey(config());
|
|
87
|
+
return keys;
|
|
88
|
+
},
|
|
89
|
+
async resolve() {
|
|
90
|
+
const first = (await all())[0];
|
|
91
|
+
if (first === void 0) throw missingKey(config());
|
|
92
|
+
return first;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function missingKey(config) {
|
|
97
|
+
return new LlmError(`llm-ollama-cloud: no API key. Store ${config.apiKeyEnv} through the credentials service, export it in the launching environment, or set config.apiKey in the composition. Create a key at https://ollama.com/settings/keys.`, "MISSING_CREDENTIAL");
|
|
98
|
+
}
|
|
99
|
+
/** Backwards-compatible single-key facade over the pool. */
|
|
100
|
+
function createKeyResolver(ctx, config) {
|
|
101
|
+
return createKeyPool(ctx, config).resolve;
|
|
102
|
+
}
|
|
103
|
+
export { createKeyPool, createKeyResolver };
|
|
104
|
+
|
|
105
|
+
//# sourceMappingURL=credentials.js.map
|