@goodandready/dsh-ollama-cloud-provider 0.1.37 → 0.1.39

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.
Files changed (62) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +13 -6
  3. package/README.ru.md +13 -6
  4. package/README.zh.md +13 -6
  5. package/lib/account-rules.js +26 -0
  6. package/lib/adapter.d.ts +92 -0
  7. package/lib/adapter.js +566 -0
  8. package/lib/analytics.d.ts +81 -0
  9. package/lib/analytics.js +314 -0
  10. package/lib/attempt.js +29 -0
  11. package/lib/best-effort.js +20 -0
  12. package/lib/catalog-cache.js +81 -0
  13. package/lib/catalog.d.ts +10 -0
  14. package/lib/catalog.js +309 -0
  15. package/lib/commands.d.ts +14 -0
  16. package/lib/commands.js +228 -0
  17. package/lib/config.d.ts +173 -0
  18. package/lib/config.js +280 -0
  19. package/lib/context-override.js +38 -0
  20. package/lib/credentials.d.ts +23 -0
  21. package/lib/credentials.js +105 -0
  22. package/lib/discovery.js +108 -0
  23. package/lib/endpoints.js +58 -0
  24. package/lib/errors.d.ts +9 -0
  25. package/lib/errors.js +72 -0
  26. package/lib/files.js +13 -0
  27. package/lib/images.js +78 -0
  28. package/lib/index.d.ts +10 -521
  29. package/lib/index.js +20 -5921
  30. package/lib/local-host.js +47 -0
  31. package/lib/messages.js +263 -0
  32. package/lib/models.generated.d.ts +26 -0
  33. package/lib/models.generated.js +499 -0
  34. package/lib/plugin-updater.js +320 -0
  35. package/lib/pricing.js +78 -0
  36. package/lib/provider-ids.d.ts +4 -0
  37. package/lib/provider-ids.js +9 -0
  38. package/lib/quota-model.js +66 -0
  39. package/lib/reasoning.js +71 -0
  40. package/lib/request-log.d.ts +38 -0
  41. package/lib/request-log.js +203 -0
  42. package/lib/retry.js +24 -0
  43. package/lib/route-guard.js +25 -0
  44. package/lib/routes-public.js +59 -0
  45. package/lib/routes-values.js +129 -0
  46. package/lib/routes.d.ts +28 -0
  47. package/lib/routes.js +733 -0
  48. package/lib/routing.js +20 -0
  49. package/lib/sanitize.js +43 -0
  50. package/lib/status.js +60 -0
  51. package/lib/tool-schema.js +109 -0
  52. package/lib/tools.js +38 -0
  53. package/lib/transports/native.js +156 -0
  54. package/lib/transports/ndjson.js +73 -0
  55. package/lib/transports/openai-chat.js +278 -0
  56. package/lib/transports/sse.js +57 -0
  57. package/lib/transports/tool-call-id.js +17 -0
  58. package/lib/tui-settings.js +246 -0
  59. package/lib/usage.d.ts +69 -0
  60. package/lib/usage.js +277 -0
  61. package/lib/web.js +143 -0
  62. package/package.json +6 -5
package/lib/adapter.js ADDED
@@ -0,0 +1,566 @@
1
+ import { matchModelAccount, orderKeysForModel } from "./account-rules.js";
2
+ import { bestEffortFailed } from "./best-effort.js";
3
+ import { createAttemptBudget } from "./attempt.js";
4
+ import { describeModel, effectiveMaxTokens, effectiveModel, refreshCatalog, sortForPicker } from "./catalog.js";
5
+ import { classifyResponseError } from "./errors.js";
6
+ import { effectiveNumCtx } from "./context-override.js";
7
+ import { hasImages, resolveImages } from "./images.js";
8
+ import { buildChatBody, buildNativeBody } from "./messages.js";
9
+ import { isRetryableStatus, retryDelay, retryPolicy } from "./retry.js";
10
+ import { redactText } from "./sanitize.js";
11
+ import "./provider-ids.js";
12
+ import { recordRequest } from "./request-log.js";
13
+ import { endpointFor } from "./routing.js";
14
+ import { limitTools } from "./tools.js";
15
+ import { parseNativeStream } from "./transports/native.js";
16
+ import { parseChatStream } from "./transports/openai-chat.js";
17
+ import { createHash, randomUUID } from "node:crypto";
18
+ import { readFileSync } from "node:fs";
19
+ import { mkdir, rename, rm, writeFile } from "node:fs/promises";
20
+ import { homedir } from "node:os";
21
+ import { basename, dirname, join } from "node:path";
22
+ import { LlmAdapter, LlmError, ReasoningEffortId, attributionHeaders, resolveRetryPolicy } from "@deepseek-ai/dsh-llm";
23
+ // src/adapter.ts
24
+ function storagePath(file) {
25
+ const home = process.env.DSH_HOME?.trim() || join(homedir(), ".dsh");
26
+ return join(home, "storages", "dsh-ollama", file);
27
+ }
28
+ function effortName(id) {
29
+ if (id === "off" || id === "none") return "Off";
30
+ return id.charAt(0).toUpperCase() + id.slice(1);
31
+ }
32
+ async function atomicWriteFile(filePath, data) {
33
+ const dir = dirname(filePath);
34
+ await mkdir(dir, { recursive: true });
35
+ const tmp = join(dir, `.${basename(filePath)}.${randomUUID()}.tmp`);
36
+ try {
37
+ await writeFile(tmp, data, "utf8");
38
+ await rename(tmp, filePath);
39
+ } catch (error) {
40
+ await rm(tmp, { force: true }).catch((err) => bestEffortFailed("atomicWriteFile:rm-tmp", err));
41
+ throw error;
42
+ }
43
+ }
44
+ var OllamaCloudAdapter = class extends LlmAdapter {
45
+ deps;
46
+ fetchImpl;
47
+ cooldownPath;
48
+ stickyPath;
49
+ cooldownWrites = Promise.resolve();
50
+ stickyWrites = Promise.resolve();
51
+ /** key fingerprint → cooldown deadline and the rejection that caused it. */
52
+ cooldowns = /* @__PURE__ */ new Map();
53
+ /** key fingerprint + model → short preference mark for model-scoped rejections. */
54
+ modelCooldowns = /* @__PURE__ */ new Map();
55
+ sticky = /* @__PURE__ */ new Map();
56
+ routing = [];
57
+ warnedTools = false;
58
+ constructor(deps) {
59
+ super();
60
+ this.deps = deps;
61
+ this.fetchImpl = deps.fetchImpl ?? fetch;
62
+ this.cooldownPath = deps.cooldownPath ?? storagePath("cooldowns.json");
63
+ this.stickyPath = deps.stickyPath ?? storagePath("sticky.json");
64
+ this.loadCooldowns();
65
+ this.loadSticky();
66
+ }
67
+ providerInfo(provider) {
68
+ return {
69
+ id: provider,
70
+ name: "Ollama Cloud"
71
+ };
72
+ }
73
+ providerRetryPolicy(_provider) {
74
+ return resolveRetryPolicy({
75
+ mode: "normal",
76
+ maxRetries: 5,
77
+ retryableCodes: [
78
+ "EMPTY_RESPONSE",
79
+ "RATE_LIMIT",
80
+ "SERVER",
81
+ "TIMEOUT",
82
+ "TRANSPORT"
83
+ ],
84
+ backoff: {
85
+ initialDelayMs: 500,
86
+ maxDelayMs: 15e3,
87
+ jitterRatio: .1
88
+ }
89
+ }, "llm-ollama-cloud: retryPolicy");
90
+ }
91
+ async listModels(provider, opts) {
92
+ const config = this.deps.config();
93
+ const discovered = await refreshCatalog(config, this.fetchImpl);
94
+ const ids = [.../* @__PURE__ */ new Set([
95
+ ...discovered,
96
+ ...config.manualModels.map((model) => model.id),
97
+ ...Object.keys(config.modelOverrides)
98
+ ])];
99
+ return sortForPicker(ids, config, opts?.unfiltered === true).map((id) => {
100
+ const model = effectiveModel(id, config);
101
+ return {
102
+ provider,
103
+ id,
104
+ name: model.name === "" ? id : model.name,
105
+ description: describeModel(model, config),
106
+ inputModalities: model.input
107
+ };
108
+ });
109
+ }
110
+ /** Recent routing decisions, newest first; no keys or prompt content. */
111
+ routingTrace() {
112
+ return [...this.routing];
113
+ }
114
+ async resolveModel(provider, model, signal) {
115
+ signal?.throwIfAborted();
116
+ const config = this.deps.config();
117
+ const meta = effectiveModel(model, config);
118
+ const maxTokens = effectiveMaxTokens(meta, config);
119
+ const efforts = meta.reasoning;
120
+ const defaultEffort = meta.defaultReasoning;
121
+ return {
122
+ provider,
123
+ id: model,
124
+ name: meta.name === "" ? model : meta.name,
125
+ description: describeModel(meta, config),
126
+ inputModalities: meta.input,
127
+ context: { contextWindow: meta.contextWindow },
128
+ ...maxTokens === void 0 ? {} : { defaultMaxTokens: maxTokens },
129
+ ...efforts.length === 0 ? {} : { reasoning: {
130
+ efforts: efforts.map((effort) => ({
131
+ id: ReasoningEffortId(effort),
132
+ name: offLabel(effort, meta)
133
+ })),
134
+ ...defaultEffort !== void 0 && efforts.includes(defaultEffort) ? { defaultEffort: ReasoningEffortId(defaultEffort) } : {}
135
+ } }
136
+ };
137
+ }
138
+ stream(options) {
139
+ return this.run(options);
140
+ }
141
+ async *run(options) {
142
+ const record = this.deps.recordRequest ?? ((provider, model, purpose, account, reason, outcome) => recordRequest(provider, model, outcome?.at ?? Date.now(), purpose, account, reason, outcome));
143
+ const config = this.deps.config();
144
+ const runStartedAt = Date.now();
145
+ const model = effectiveModel(options.model, config);
146
+ const rawKeys = orderKeysForModel(this.deps.resolveKeys !== void 0 ? await this.deps.resolveKeys(options.provider) : [await this.deps.resolveKey(options.provider)], options.model, config.modelAccountRules);
147
+ const rule = matchModelAccount(options.model, config.modelAccountRules);
148
+ let keys = rawKeys;
149
+ let reason = rule === void 0 ? "default" : "rule";
150
+ if (config.activeAccount !== "") reason = "pinned";
151
+ else if (config.stickySession && rule === void 0) {
152
+ const previous = this.sticky.get(options.sessionId === void 0 ? "" : String(options.sessionId));
153
+ if (previous !== void 0) {
154
+ const preferred = rawKeys.find((entry) => this.keyId(entry.key) === previous.fingerprint);
155
+ if (preferred !== void 0) {
156
+ keys = [preferred, ...rawKeys.filter((entry) => entry !== preferred)];
157
+ reason = "sticky";
158
+ }
159
+ }
160
+ }
161
+ const availableKeys = keys.filter((entry) => !this.coolingDown(entry.key));
162
+ const preferredKeys = availableKeys.filter((entry) => !this.modelCoolingDown(entry.key, options.model));
163
+ keys = preferredKeys.length > 0 ? preferredKeys : availableKeys;
164
+ const url = endpointFor(model.transport, config);
165
+ let images;
166
+ if (hasImages(options)) {
167
+ const store = this.deps.resolveAttachments?.();
168
+ if (store === void 0) throw new LlmError("llm-ollama-cloud: image input requires the durable attachment service", "UNSUPPORTED_CONTENT");
169
+ images = await resolveImages(options, store, options.signal);
170
+ }
171
+ let bodyOptions = options;
172
+ if (options.tools !== void 0) {
173
+ const limited = limitTools(options.tools) ?? [];
174
+ if (limited.length < options.tools.length && !this.warnedTools) {
175
+ this.warnedTools = true;
176
+ this.deps.warn?.(`llm-ollama-cloud: provider accepts at most 128 tools; ${options.tools.length - limited.length} tool declaration(s) omitted`);
177
+ }
178
+ bodyOptions = {
179
+ ...options,
180
+ tools: limited
181
+ };
182
+ }
183
+ const body = model.transport === "openai-chat" ? buildChatBody(bodyOptions, model, config, images) : buildNativeBody(bodyOptions, model, config, images, effectiveNumCtx(config, options.sessionId));
184
+ let lastError;
185
+ let lastAccount = "default";
186
+ let loggedAny = false;
187
+ for (const [index, entry] of keys.entries()) {
188
+ if (this.coolingDown(entry.key)) continue;
189
+ lastAccount = entry.label ?? "default";
190
+ const attemptReason = index === 0 ? reason : "rotation";
191
+ const attemptStartedAt = Date.now();
192
+ let started = false;
193
+ let responseFailure = false;
194
+ let firstChunkAt;
195
+ let logged = false;
196
+ const logAttempt = (ok, errorCode) => {
197
+ if (logged) return;
198
+ logged = true;
199
+ loggedAny = true;
200
+ record(options.provider, options.model, options.purpose, entry.label ?? "default", attemptReason, {
201
+ at: attemptStartedAt,
202
+ ok,
203
+ latencyMs: Math.max(0, Date.now() - attemptStartedAt),
204
+ ...firstChunkAt === void 0 ? {} : { ttftMs: Math.max(0, firstChunkAt - attemptStartedAt) },
205
+ ...errorCode === void 0 ? {} : { error: errorCode }
206
+ });
207
+ };
208
+ const budget = createAttemptBudget(config.attemptTimeoutMs, options.signal);
209
+ try {
210
+ const response = await this.send(url, entry.key, model, body, config, budget.signal);
211
+ if (!response.ok) {
212
+ const text = await response.text().catch(() => "");
213
+ const error = classifyResponseError(response.status, text, response.headers);
214
+ lastError = error;
215
+ logAttempt(false, error.code);
216
+ const canRotate = index < keys.length - 1;
217
+ if (this.modelScoped(error)) {
218
+ this.markModelCooldown(entry.key, options.model, error.code === "UNSUPPORTED_MODEL" ? "model-unsupported" : "model-quota");
219
+ responseFailure = true;
220
+ if (canRotate) continue;
221
+ throw error;
222
+ }
223
+ const retryable = this.isTransient(error) || error.code === "INVALID_CREDENTIAL";
224
+ const credentialOrQuota = error.code === "INVALID_CREDENTIAL" || error.code === "RATE_LIMIT" || error.code === "QUOTA";
225
+ if (retryable) {
226
+ if (canRotate || credentialOrQuota) this.markCooldown(entry.key, response.status, response.headers, error.code);
227
+ responseFailure = true;
228
+ if (canRotate) continue;
229
+ }
230
+ throw error;
231
+ }
232
+ this.rememberSticky(options, entry);
233
+ this.pushTrace({
234
+ at: Date.now(),
235
+ provider: options.provider,
236
+ model: options.model,
237
+ account: entry.label ?? "default",
238
+ reason: attemptReason,
239
+ fallback: false,
240
+ ok: true
241
+ });
242
+ const chunks = model.transport === "openai-chat" ? parseChatStream(response, config.streamIdleTimeoutMs, budget.signal) : parseNativeStream(response, config.streamIdleTimeoutMs, budget.signal);
243
+ for await (const chunk of chunks) {
244
+ if (firstChunkAt === void 0) firstChunkAt = Date.now();
245
+ started = true;
246
+ yield chunk;
247
+ }
248
+ logAttempt(true);
249
+ return;
250
+ } catch (error) {
251
+ const classified = budget.timedOut ? new LlmError(`Ollama Cloud attempt exceeded ${config.attemptTimeoutMs}ms`, "TIMEOUT", { cause: error }) : error instanceof LlmError ? error : new LlmError(error instanceof Error ? redactText(error.message) : "Ollama Cloud request failed", "TRANSPORT", { cause: error });
252
+ logAttempt(false, classified.code);
253
+ if (started || options.signal?.aborted === true) throw budget.timedOut ? classified : error;
254
+ lastError = classified;
255
+ const transient = this.isTransient(classified);
256
+ const canRotate = index < keys.length - 1 && (transient || this.modelScoped(classified));
257
+ const credentialOrQuota = classified.code === "INVALID_CREDENTIAL" || classified.code === "RATE_LIMIT" || classified.code === "QUOTA";
258
+ if (this.modelScoped(classified)) {
259
+ this.markModelCooldown(entry.key, options.model, classified.code === "UNSUPPORTED_MODEL" ? "model-unsupported" : "model-quota");
260
+ if (!canRotate) throw classified;
261
+ continue;
262
+ }
263
+ if (!responseFailure && (canRotate || credentialOrQuota)) this.markCooldown(entry.key, 0);
264
+ if (!canRotate) throw classified;
265
+ } finally {
266
+ budget.dispose();
267
+ }
268
+ }
269
+ const cooldownEntries = rawKeys.map((entry) => this.cooldowns.get(this.keyId(entry.key))).filter((entry) => entry !== void 0 && entry.until > Date.now());
270
+ let finalError = lastError;
271
+ if (finalError === void 0) {
272
+ if (cooldownEntries.length > 0) {
273
+ const minUntil = Math.min(...cooldownEntries.map((e) => e.until));
274
+ const remainingMinutes = Math.max(1, Math.ceil((minUntil - Date.now()) / 6e4));
275
+ const reasons = new Set(cooldownEntries.map((e) => e.reason));
276
+ if (reasons.has("invalid-credential") && reasons.size === 1) finalError = new LlmError(`llm-ollama-cloud: every configured account is cooling down due to invalid credentials (retry in ${remainingMinutes}m)`, "INVALID_CREDENTIAL");
277
+ else if (reasons.has("quota") && !reasons.has("rate-limit")) finalError = new LlmError(`llm-ollama-cloud: every configured account is cooling down due to quota limits (retry in ${remainingMinutes}m)`, "QUOTA");
278
+ else finalError = new LlmError(`llm-ollama-cloud: every configured account is cooling down after provider rejections (retry in ${remainingMinutes}m)`, "RATE_LIMIT");
279
+ } else finalError = new LlmError("llm-ollama-cloud: every configured account is cooling down after provider rejections", "RATE_LIMIT");
280
+ }
281
+ if (!loggedAny) record(options.provider, options.model, options.purpose, lastAccount, reason, {
282
+ at: runStartedAt,
283
+ ok: false,
284
+ latencyMs: 0,
285
+ error: finalError.code
286
+ });
287
+ this.pushTrace({
288
+ at: Date.now(),
289
+ provider: options.provider,
290
+ model: options.model,
291
+ account: lastAccount,
292
+ reason,
293
+ fallback: false,
294
+ ok: false,
295
+ error: finalError.code
296
+ });
297
+ throw finalError;
298
+ }
299
+ rememberSticky(options, entry) {
300
+ if (!this.deps.config().stickySession) return;
301
+ if (options.sessionId === void 0) return;
302
+ const sessionId = String(options.sessionId);
303
+ if (!this.sticky.has(sessionId) && this.sticky.size >= 500) {
304
+ const oldest = this.sticky.keys().next().value;
305
+ if (oldest !== void 0) this.sticky.delete(oldest);
306
+ }
307
+ this.sticky.set(sessionId, {
308
+ fingerprint: this.keyId(entry.key),
309
+ label: entry.label ?? "default",
310
+ provider: options.provider,
311
+ model: options.model,
312
+ at: Date.now()
313
+ });
314
+ this.persistSticky();
315
+ }
316
+ /** Await pending sticky persistence writes (tests and shutdown). */
317
+ async flushSticky() {
318
+ await this.stickyWrites;
319
+ }
320
+ loadSticky() {
321
+ try {
322
+ const entries = JSON.parse(readFileSync(this.stickyPath, "utf8")).sticky;
323
+ if (typeof entries !== "object" || entries === null || Array.isArray(entries)) return;
324
+ for (const [sessionId, value] of Object.entries(entries)) {
325
+ if (typeof value !== "object" || value === null || Array.isArray(value)) continue;
326
+ const record = value;
327
+ const fingerprint = record.fingerprint;
328
+ const label = record.label;
329
+ const provider = record.provider;
330
+ const model = record.model;
331
+ const at = record.at;
332
+ if (typeof fingerprint !== "string" || !/^[0-9a-f]{16}$/.test(fingerprint)) continue;
333
+ if (typeof label !== "string" || label === "") continue;
334
+ if (typeof provider !== "string" || provider === "") continue;
335
+ if (typeof model !== "string" || model === "") continue;
336
+ if (typeof at !== "number" || !Number.isFinite(at)) continue;
337
+ this.sticky.set(sessionId, {
338
+ fingerprint,
339
+ label,
340
+ provider,
341
+ model,
342
+ at
343
+ });
344
+ }
345
+ this.pruneSticky();
346
+ } catch (error) {
347
+ bestEffortFailed("sticky state is unavailable", error);
348
+ }
349
+ }
350
+ pruneSticky() {
351
+ const cutoff = Date.now() - 12096e5;
352
+ for (const [sessionId, entry] of this.sticky) if (entry.at < cutoff) this.sticky.delete(sessionId);
353
+ const overflow = this.sticky.size - 500;
354
+ if (overflow <= 0) return;
355
+ const oldest = [...this.sticky.entries()].sort((a, b) => a[1].at - b[1].at).slice(0, overflow);
356
+ for (const [sessionId] of oldest) this.sticky.delete(sessionId);
357
+ }
358
+ persistSticky() {
359
+ this.pruneSticky();
360
+ const payload = JSON.stringify({ sticky: Object.fromEntries(this.sticky) });
361
+ this.stickyWrites = this.stickyWrites.then(async () => {
362
+ await atomicWriteFile(this.stickyPath, payload);
363
+ }).catch((err) => bestEffortFailed("persistSticky", err));
364
+ }
365
+ pushTrace(entry) {
366
+ this.routing.unshift(entry);
367
+ if (this.routing.length > 20) this.routing.length = 20;
368
+ }
369
+ async send(url, key, model, body, config, signal) {
370
+ const headers = {
371
+ ...attributionHeaders(),
372
+ ...key === "" ? {} : { authorization: `Bearer ${key}` },
373
+ "content-type": "application/json",
374
+ accept: model.transport === "openai-chat" ? "text/event-stream" : "application/x-ndjson",
375
+ "user-agent": config.userAgent
376
+ };
377
+ const policy = retryPolicy(config);
378
+ const wait = async (attempt) => {
379
+ await new Promise((resolve) => setTimeout(resolve, retryDelay(attempt, policy)));
380
+ signal?.throwIfAborted();
381
+ };
382
+ for (let attempt = 0;; attempt += 1) {
383
+ const controller = new AbortController();
384
+ let timedOut = false;
385
+ const onAbort = () => controller.abort(signal?.reason);
386
+ signal?.addEventListener("abort", onAbort, { once: true });
387
+ const timer = setTimeout(() => {
388
+ timedOut = true;
389
+ controller.abort();
390
+ }, config.requestTimeoutMs);
391
+ timer.unref?.();
392
+ try {
393
+ const response = await this.fetchImpl(url, {
394
+ method: "POST",
395
+ headers,
396
+ body: JSON.stringify(body),
397
+ signal: controller.signal
398
+ });
399
+ if (attempt < policy.maxRetries && isRetryableStatus(response.status)) {
400
+ await response.body?.cancel();
401
+ await wait(attempt);
402
+ continue;
403
+ }
404
+ return response;
405
+ } catch (error) {
406
+ if (signal?.aborted === true) throw signal.reason ?? error;
407
+ if (timedOut) {
408
+ const timeout = new LlmError(`Ollama Cloud did not return response headers within ${config.requestTimeoutMs}ms`, "TIMEOUT", { cause: error });
409
+ if (attempt < policy.maxRetries) {
410
+ await wait(attempt);
411
+ continue;
412
+ }
413
+ throw timeout;
414
+ }
415
+ if (attempt < policy.maxRetries) {
416
+ await wait(attempt);
417
+ continue;
418
+ }
419
+ throw error;
420
+ } finally {
421
+ clearTimeout(timer);
422
+ signal?.removeEventListener("abort", onAbort);
423
+ }
424
+ }
425
+ }
426
+ keyId(key) {
427
+ return createHash("sha256").update(key).digest("hex").slice(0, 16);
428
+ }
429
+ coolingDown(key) {
430
+ const id = this.keyId(key);
431
+ const entry = this.cooldowns.get(id);
432
+ if (entry === void 0) return false;
433
+ if (entry.until <= Date.now()) {
434
+ this.cooldowns.delete(id);
435
+ return false;
436
+ }
437
+ return true;
438
+ }
439
+ modelKey(key, model) {
440
+ return `${this.keyId(key)}:${model}`;
441
+ }
442
+ modelCoolingDown(key, model) {
443
+ const id = this.modelKey(key, model);
444
+ const entry = this.modelCooldowns.get(id);
445
+ if (entry === void 0) return false;
446
+ if (entry.until <= Date.now()) {
447
+ this.modelCooldowns.delete(id);
448
+ return false;
449
+ }
450
+ return true;
451
+ }
452
+ markModelCooldown(key, model, reason) {
453
+ const now = Date.now();
454
+ for (const [id, entry] of this.modelCooldowns) if (entry.until <= now) this.modelCooldowns.delete(id);
455
+ this.modelCooldowns.set(this.modelKey(key, model), {
456
+ until: now + 3e5,
457
+ reason
458
+ });
459
+ const overflow = this.modelCooldowns.size - 200;
460
+ if (overflow <= 0) return;
461
+ const oldest = [...this.modelCooldowns.entries()].sort((a, b) => a[1].until - b[1].until).slice(0, overflow);
462
+ for (const [id] of oldest) this.modelCooldowns.delete(id);
463
+ }
464
+ /**
465
+ * A model-scoped rejection says nothing about the key's other models:
466
+ * `:free` models and other paid models may still work on the same account.
467
+ * Only account-wide quota, credential and rate-limit responses cool the key.
468
+ */
469
+ modelScoped(error) {
470
+ if (error.code === "UNSUPPORTED_MODEL") return true;
471
+ if (error.code !== "QUOTA") return false;
472
+ const detail = error.message.toLowerCase();
473
+ return detail.includes("model") && (detail.includes("credit") || detail.includes("quota") || detail.includes("allowance") || detail.includes("limit"));
474
+ }
475
+ /** Current cooldown for one resolved key, if any. */
476
+ accountHealth(key) {
477
+ const id = this.keyId(key);
478
+ const entry = this.cooldowns.get(id);
479
+ if (entry === void 0) return void 0;
480
+ if (entry.until <= Date.now()) {
481
+ this.cooldowns.delete(id);
482
+ return;
483
+ }
484
+ return {
485
+ coolingUntil: entry.until,
486
+ reason: entry.reason
487
+ };
488
+ }
489
+ /** Await pending cooldown persistence writes (tests and shutdown). */
490
+ async flushCooldowns() {
491
+ await this.cooldownWrites;
492
+ }
493
+ /** Manually clear all active account and model cooldowns. */
494
+ clearCooldowns() {
495
+ this.cooldowns.clear();
496
+ this.modelCooldowns.clear();
497
+ this.persistCooldowns();
498
+ }
499
+ loadCooldowns() {
500
+ try {
501
+ const entries = JSON.parse(readFileSync(this.cooldownPath, "utf8")).cooldowns;
502
+ if (typeof entries !== "object" || entries === null || Array.isArray(entries)) return;
503
+ for (const [id, value] of Object.entries(entries)) {
504
+ if (typeof value !== "object" || value === null || Array.isArray(value)) continue;
505
+ const until = value.until;
506
+ const reason = value.reason;
507
+ if (typeof until !== "number" || !Number.isFinite(until) || until <= Date.now()) continue;
508
+ if (typeof reason !== "string" || reason === "") continue;
509
+ this.cooldowns.set(id, {
510
+ until,
511
+ reason
512
+ });
513
+ }
514
+ this.pruneCooldowns();
515
+ } catch (error) {
516
+ bestEffortFailed("cooldown state is unavailable", error);
517
+ }
518
+ }
519
+ pruneCooldowns() {
520
+ const now = Date.now();
521
+ for (const [id, entry] of this.cooldowns) if (entry.until <= now) this.cooldowns.delete(id);
522
+ const overflow = this.cooldowns.size - 50;
523
+ if (overflow <= 0) return;
524
+ const oldest = [...this.cooldowns.entries()].sort((a, b) => a[1].until - b[1].until).slice(0, overflow);
525
+ for (const [id] of oldest) this.cooldowns.delete(id);
526
+ }
527
+ persistCooldowns() {
528
+ this.pruneCooldowns();
529
+ const payload = JSON.stringify({ cooldowns: Object.fromEntries(this.cooldowns) });
530
+ this.cooldownWrites = this.cooldownWrites.then(async () => {
531
+ await atomicWriteFile(this.cooldownPath, payload);
532
+ }).catch((err) => bestEffortFailed("persistCooldowns", err));
533
+ }
534
+ markCooldown(key, status, headers, code) {
535
+ let delayMs = 3e4;
536
+ let reason = "transport";
537
+ if (code === "QUOTA" || status === 402) {
538
+ const seconds = Number(headers?.get("retry-after") ?? NaN);
539
+ delayMs = Number.isFinite(seconds) && seconds > 0 ? seconds * 1e3 : 6e5;
540
+ reason = "quota";
541
+ } else if (status === 401 || status === 403) {
542
+ delayMs = 3e5;
543
+ reason = "invalid-credential";
544
+ } else if (status === 429) {
545
+ const seconds = Number(headers?.get("retry-after") ?? NaN);
546
+ delayMs = Number.isFinite(seconds) && seconds > 0 ? seconds * 1e3 : 6e4;
547
+ reason = "rate-limit";
548
+ }
549
+ this.cooldowns.set(this.keyId(key), {
550
+ until: Date.now() + delayMs,
551
+ reason
552
+ });
553
+ this.persistCooldowns();
554
+ }
555
+ isTransient(error) {
556
+ return error.code === "RATE_LIMIT" || error.code === "QUOTA" || error.code === "SERVER" || error.code === "TIMEOUT" || error.code === "TRANSPORT" || error.code === "EMPTY_RESPONSE";
557
+ }
558
+ };
559
+ function offLabel(id, meta) {
560
+ if (id === "off" || id === "none") return "Off";
561
+ if (id === "high" && meta.reasoningToggle) return "On";
562
+ return effortName(id);
563
+ }
564
+ export { OllamaCloudAdapter };
565
+
566
+ //# sourceMappingURL=adapter.js.map
@@ -0,0 +1,81 @@
1
+ import "./config.js";
2
+ interface ModelUsageRow {
3
+ provider: string;
4
+ model: string;
5
+ sessions: number;
6
+ turns: number;
7
+ steps: number;
8
+ inputTokens: number;
9
+ outputTokens: number;
10
+ cacheReadTokens: number;
11
+ cacheWriteTokens: number;
12
+ cost: number;
13
+ priced: boolean;
14
+ partial: boolean;
15
+ free: boolean;
16
+ llmMs: number;
17
+ ttftMs: number;
18
+ ttftSteps: number;
19
+ decodeMs: number;
20
+ }
21
+ interface AnalyticsSummary {
22
+ sessions: number;
23
+ requests: number;
24
+ tokens: number;
25
+ inputTokens: number;
26
+ outputTokens: number;
27
+ reasoningTokens: number | null;
28
+ cacheReadTokens: number;
29
+ cacheWriteTokens: number;
30
+ cost: number;
31
+ priced: boolean;
32
+ partial: boolean;
33
+ free: boolean;
34
+ avgTtftMs: number | null;
35
+ avgLatencyMs: number | null;
36
+ errors: number | null;
37
+ successRate: number | null;
38
+ }
39
+ interface DayUsageRow {
40
+ date: string;
41
+ sessions: number;
42
+ requests: number;
43
+ tokens: number;
44
+ cost: number;
45
+ priced: boolean;
46
+ partial: boolean;
47
+ free: boolean;
48
+ avgTtftMs: number | null;
49
+ }
50
+ interface SessionUsageRow {
51
+ id: string;
52
+ provider: string;
53
+ model: string;
54
+ at: number;
55
+ requests: number;
56
+ tokens: number;
57
+ inputTokens: number;
58
+ outputTokens: number;
59
+ cacheReadTokens: number;
60
+ cacheWriteTokens: number;
61
+ cost: number;
62
+ priced: boolean;
63
+ partial: boolean;
64
+ free: boolean;
65
+ avgTtftMs: number | null;
66
+ avgLatencyMs: number | null;
67
+ }
68
+ interface AnalyticsReport {
69
+ since: number;
70
+ scannedSessions: number;
71
+ matchedSessions: number;
72
+ models: ModelUsageRow[];
73
+ summary: AnalyticsSummary;
74
+ days: DayUsageRow[];
75
+ sessions: SessionUsageRow[];
76
+ /** The projection directory held more files than the bounded scan. */
77
+ truncated?: boolean;
78
+ totalFiles?: number;
79
+ }
80
+ export { AnalyticsReport, AnalyticsSummary, DayUsageRow, ModelUsageRow, SessionUsageRow };
81
+ //# sourceMappingURL=analytics.d.ts.map