@goodandready/dsh-ollama-cloud-provider 0.1.37 → 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 +9 -0
- package/README.md +4 -4
- package/README.ru.md +4 -4
- package/README.zh.md +4 -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 -5921
- 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 +5 -4
package/lib/analytics.js
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { bestEffortFailed } from "./best-effort.js";
|
|
2
|
+
import { estimateCost } from "./pricing.js";
|
|
3
|
+
import { PLUGIN_AND_LEGACY_PROVIDERS } from "./provider-ids.js";
|
|
4
|
+
import { readFile, readdir, stat } from "node:fs/promises";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
// src/analytics.ts
|
|
8
|
+
const DAY_MS = 864e5;
|
|
9
|
+
const CACHE_TTL_MS = 6e4;
|
|
10
|
+
const MAX_FILES = 5e3;
|
|
11
|
+
function count(value) {
|
|
12
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
|
|
13
|
+
}
|
|
14
|
+
function obj(value) {
|
|
15
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
|
|
16
|
+
}
|
|
17
|
+
/** Read the numeric metadata out of one projection cache file. */
|
|
18
|
+
function metadataOf(file) {
|
|
19
|
+
const record = file.record;
|
|
20
|
+
const rows = record?.rows;
|
|
21
|
+
if (record === void 0 || rows === void 0) return void 0;
|
|
22
|
+
const usageRow = obj(rows.tokenUsage?.val);
|
|
23
|
+
const usage = usageRow === void 0 ? void 0 : obj(usageRow.totals);
|
|
24
|
+
const selection = obj(rows.modelSelection?.val);
|
|
25
|
+
const lastUsed = selection === void 0 ? void 0 : obj(selection.lastUsed);
|
|
26
|
+
const stats = obj(rows.sessionStats?.val);
|
|
27
|
+
const list = obj(rows.sessionListMetadata?.val);
|
|
28
|
+
const lastPromptAt = typeof list?.lastPromptAt === "number" ? list.lastPromptAt : void 0;
|
|
29
|
+
const createdAt = record.identity?.createdAt;
|
|
30
|
+
return {
|
|
31
|
+
...createdAt === void 0 ? {} : { createdAt },
|
|
32
|
+
...lastPromptAt === void 0 ? {} : { lastPromptAt },
|
|
33
|
+
...usage === void 0 ? {} : { usage: {
|
|
34
|
+
uncachedInputTokens: count(usage.uncachedInputTokens),
|
|
35
|
+
outputTokens: count(usage.outputTokens),
|
|
36
|
+
cacheReadTokens: count(usage.cacheReadTokens),
|
|
37
|
+
cacheWriteTokens: count(usage.cacheWriteTokens)
|
|
38
|
+
} },
|
|
39
|
+
...lastUsed === void 0 ? {} : { lastUsed: {
|
|
40
|
+
...typeof lastUsed.provider === "string" ? { provider: lastUsed.provider } : {},
|
|
41
|
+
...typeof lastUsed.model === "string" ? { model: lastUsed.model } : {}
|
|
42
|
+
} },
|
|
43
|
+
...stats === void 0 ? {} : { stats: {
|
|
44
|
+
turns: count(stats.turns),
|
|
45
|
+
steps: count(stats.steps),
|
|
46
|
+
llmMs: count(stats.llmMs),
|
|
47
|
+
ttftMs: count(stats.ttftMs),
|
|
48
|
+
ttftSteps: count(stats.ttftSteps),
|
|
49
|
+
decodeMs: count(stats.decodeMs)
|
|
50
|
+
} }
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function localDay(at) {
|
|
54
|
+
const date = new Date(at);
|
|
55
|
+
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Aggregate local session metadata by model, day and session. Only Ollama Cloud
|
|
59
|
+
* routes are counted, and only numeric metadata is read — titles and message
|
|
60
|
+
* content never leave the projection cache. A session that switched models is
|
|
61
|
+
* attributed to the model that served its last request, so the split is
|
|
62
|
+
* approximate. Day grouping uses the session's last activity.
|
|
63
|
+
*/
|
|
64
|
+
function aggregateSessions(sessions, days, now = Date.now(), priceOverrides = {}) {
|
|
65
|
+
const since = now - days * DAY_MS;
|
|
66
|
+
const models = /* @__PURE__ */ new Map();
|
|
67
|
+
const daysMap = /* @__PURE__ */ new Map();
|
|
68
|
+
const sessionRows = [];
|
|
69
|
+
let matched = 0;
|
|
70
|
+
const total = {
|
|
71
|
+
sessions: 0,
|
|
72
|
+
requests: 0,
|
|
73
|
+
inputTokens: 0,
|
|
74
|
+
outputTokens: 0,
|
|
75
|
+
cacheReadTokens: 0,
|
|
76
|
+
cacheWriteTokens: 0,
|
|
77
|
+
cost: 0,
|
|
78
|
+
priced: false,
|
|
79
|
+
partial: false,
|
|
80
|
+
free: true,
|
|
81
|
+
llmMs: 0,
|
|
82
|
+
ttftMs: 0,
|
|
83
|
+
ttftSteps: 0
|
|
84
|
+
};
|
|
85
|
+
for (const session of sessions) {
|
|
86
|
+
const provider = session.lastUsed?.provider;
|
|
87
|
+
const model = session.lastUsed?.model;
|
|
88
|
+
if (typeof provider !== "string" || !PLUGIN_AND_LEGACY_PROVIDERS.has(provider) || model === void 0 || model === "") continue;
|
|
89
|
+
const usage = session.usage;
|
|
90
|
+
if (usage === void 0) continue;
|
|
91
|
+
const input = count(usage.uncachedInputTokens);
|
|
92
|
+
const output = count(usage.outputTokens);
|
|
93
|
+
const cacheRead = count(usage.cacheReadTokens);
|
|
94
|
+
const cacheWrite = count(usage.cacheWriteTokens);
|
|
95
|
+
if (input + output + cacheRead + cacheWrite === 0) continue;
|
|
96
|
+
const at = session.lastPromptAt ?? session.createdAt ?? 0;
|
|
97
|
+
if (at < since) continue;
|
|
98
|
+
matched += 1;
|
|
99
|
+
const estimate = estimateCost({
|
|
100
|
+
model,
|
|
101
|
+
inputTokens: input,
|
|
102
|
+
outputTokens: output,
|
|
103
|
+
cacheReadTokens: cacheRead,
|
|
104
|
+
priceOverrides,
|
|
105
|
+
at
|
|
106
|
+
});
|
|
107
|
+
const priced = estimate.priced;
|
|
108
|
+
const free = estimate.free;
|
|
109
|
+
const partial = cacheWrite > 0 || !priced;
|
|
110
|
+
const cost = estimate.usd;
|
|
111
|
+
const key = `${provider}/${model}`;
|
|
112
|
+
let row = models.get(key);
|
|
113
|
+
if (row === void 0) {
|
|
114
|
+
row = {
|
|
115
|
+
provider,
|
|
116
|
+
model,
|
|
117
|
+
sessions: 0,
|
|
118
|
+
turns: 0,
|
|
119
|
+
steps: 0,
|
|
120
|
+
inputTokens: 0,
|
|
121
|
+
outputTokens: 0,
|
|
122
|
+
cacheReadTokens: 0,
|
|
123
|
+
cacheWriteTokens: 0,
|
|
124
|
+
cost: 0,
|
|
125
|
+
priced,
|
|
126
|
+
partial,
|
|
127
|
+
free,
|
|
128
|
+
llmMs: 0,
|
|
129
|
+
ttftMs: 0,
|
|
130
|
+
ttftSteps: 0,
|
|
131
|
+
decodeMs: 0
|
|
132
|
+
};
|
|
133
|
+
models.set(key, row);
|
|
134
|
+
}
|
|
135
|
+
const steps = session.stats?.steps ?? 0;
|
|
136
|
+
const ttftSteps = session.stats?.ttftSteps ?? 0;
|
|
137
|
+
row.sessions += 1;
|
|
138
|
+
row.turns += session.stats?.turns ?? 0;
|
|
139
|
+
row.steps += steps;
|
|
140
|
+
row.inputTokens += input;
|
|
141
|
+
row.outputTokens += output;
|
|
142
|
+
row.cacheReadTokens += cacheRead;
|
|
143
|
+
row.cacheWriteTokens += cacheWrite;
|
|
144
|
+
row.llmMs += session.stats?.llmMs ?? 0;
|
|
145
|
+
row.ttftMs += session.stats?.ttftMs ?? 0;
|
|
146
|
+
row.ttftSteps += ttftSteps;
|
|
147
|
+
row.decodeMs += session.stats?.decodeMs ?? 0;
|
|
148
|
+
row.partial = row.partial || partial;
|
|
149
|
+
if (priced && !free) row.cost += cost;
|
|
150
|
+
sessionRows.push({
|
|
151
|
+
id: session.id ?? "",
|
|
152
|
+
provider,
|
|
153
|
+
model,
|
|
154
|
+
at,
|
|
155
|
+
requests: steps,
|
|
156
|
+
tokens: input + output,
|
|
157
|
+
inputTokens: input,
|
|
158
|
+
outputTokens: output,
|
|
159
|
+
cacheReadTokens: cacheRead,
|
|
160
|
+
cacheWriteTokens: cacheWrite,
|
|
161
|
+
cost,
|
|
162
|
+
priced,
|
|
163
|
+
partial,
|
|
164
|
+
free,
|
|
165
|
+
avgTtftMs: ttftSteps > 0 ? (session.stats?.ttftMs ?? 0) / ttftSteps : null,
|
|
166
|
+
avgLatencyMs: steps > 0 ? (session.stats?.llmMs ?? 0) / steps : null
|
|
167
|
+
});
|
|
168
|
+
const date = localDay(at);
|
|
169
|
+
let day = daysMap.get(date);
|
|
170
|
+
if (day === void 0) {
|
|
171
|
+
day = {
|
|
172
|
+
date,
|
|
173
|
+
sessions: 0,
|
|
174
|
+
requests: 0,
|
|
175
|
+
tokens: 0,
|
|
176
|
+
cost: 0,
|
|
177
|
+
priced: false,
|
|
178
|
+
partial: false,
|
|
179
|
+
free: true,
|
|
180
|
+
ttftMs: 0,
|
|
181
|
+
ttftSteps: 0
|
|
182
|
+
};
|
|
183
|
+
daysMap.set(date, day);
|
|
184
|
+
}
|
|
185
|
+
day.sessions += 1;
|
|
186
|
+
day.requests += steps;
|
|
187
|
+
day.tokens += input + output;
|
|
188
|
+
day.cost += cost;
|
|
189
|
+
day.priced = day.priced || priced;
|
|
190
|
+
day.partial = day.partial || partial;
|
|
191
|
+
day.free = day.free && free;
|
|
192
|
+
day.ttftMs += session.stats?.ttftMs ?? 0;
|
|
193
|
+
day.ttftSteps += ttftSteps;
|
|
194
|
+
total.sessions += 1;
|
|
195
|
+
total.requests += steps;
|
|
196
|
+
total.inputTokens += input;
|
|
197
|
+
total.outputTokens += output;
|
|
198
|
+
total.cacheReadTokens += cacheRead;
|
|
199
|
+
total.cacheWriteTokens += cacheWrite;
|
|
200
|
+
total.cost += cost;
|
|
201
|
+
total.priced = total.priced || priced;
|
|
202
|
+
total.partial = total.partial || partial;
|
|
203
|
+
total.free = total.free && free;
|
|
204
|
+
total.llmMs += session.stats?.llmMs ?? 0;
|
|
205
|
+
total.ttftMs += session.stats?.ttftMs ?? 0;
|
|
206
|
+
total.ttftSteps += ttftSteps;
|
|
207
|
+
}
|
|
208
|
+
const summary = {
|
|
209
|
+
sessions: total.sessions,
|
|
210
|
+
requests: total.requests,
|
|
211
|
+
tokens: total.inputTokens + total.outputTokens,
|
|
212
|
+
inputTokens: total.inputTokens,
|
|
213
|
+
outputTokens: total.outputTokens,
|
|
214
|
+
reasoningTokens: null,
|
|
215
|
+
cacheReadTokens: total.cacheReadTokens,
|
|
216
|
+
cacheWriteTokens: total.cacheWriteTokens,
|
|
217
|
+
cost: total.cost,
|
|
218
|
+
priced: total.priced,
|
|
219
|
+
partial: total.partial,
|
|
220
|
+
free: total.sessions > 0 && total.free,
|
|
221
|
+
avgTtftMs: total.ttftSteps > 0 ? total.ttftMs / total.ttftSteps : null,
|
|
222
|
+
avgLatencyMs: total.requests > 0 ? total.llmMs / total.requests : null,
|
|
223
|
+
errors: null,
|
|
224
|
+
successRate: null
|
|
225
|
+
};
|
|
226
|
+
return {
|
|
227
|
+
since,
|
|
228
|
+
scannedSessions: sessions.length,
|
|
229
|
+
matchedSessions: matched,
|
|
230
|
+
models: [...models.values()].sort((a, b) => b.cost - a.cost || b.inputTokens + b.outputTokens - (a.inputTokens + a.outputTokens) || a.model.localeCompare(b.model)),
|
|
231
|
+
summary,
|
|
232
|
+
days: [...daysMap.values()].map((day) => ({
|
|
233
|
+
date: day.date,
|
|
234
|
+
sessions: day.sessions,
|
|
235
|
+
requests: day.requests,
|
|
236
|
+
tokens: day.tokens,
|
|
237
|
+
cost: day.cost,
|
|
238
|
+
priced: day.priced,
|
|
239
|
+
partial: day.partial,
|
|
240
|
+
free: day.free,
|
|
241
|
+
avgTtftMs: day.ttftSteps > 0 ? day.ttftMs / day.ttftSteps : null
|
|
242
|
+
})).sort((a, b) => b.date.localeCompare(a.date)),
|
|
243
|
+
sessions: sessionRows.sort((a, b) => b.at - a.at)
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
const cache = /* @__PURE__ */ new Map();
|
|
247
|
+
function projectionDirectory() {
|
|
248
|
+
const home = process.env.DSH_HOME?.trim() || join(homedir(), ".dsh");
|
|
249
|
+
return join(home, "storages", "session_projcache", "sessions");
|
|
250
|
+
}
|
|
251
|
+
/** Read and aggregate the local projection cache; never waits on the network. */
|
|
252
|
+
async function readModelAnalytics(days = 30, priceOverrides = {}) {
|
|
253
|
+
const clamped = Math.max(1, Math.min(365, Math.trunc(days)));
|
|
254
|
+
const cacheKey = `${clamped}:${JSON.stringify(priceOverrides)}`;
|
|
255
|
+
const cached = cache.get(cacheKey);
|
|
256
|
+
if (cached !== void 0 && Date.now() - cached.at < CACHE_TTL_MS) return cached.report;
|
|
257
|
+
const dir = projectionDirectory();
|
|
258
|
+
const sessions = [];
|
|
259
|
+
let truncated = false;
|
|
260
|
+
let totalFiles = 0;
|
|
261
|
+
try {
|
|
262
|
+
const candidates = (await readdir(dir, { withFileTypes: true })).filter((entry) => entry.isFile() && entry.name.endsWith(".json"));
|
|
263
|
+
totalFiles = candidates.length;
|
|
264
|
+
truncated = candidates.length > MAX_FILES;
|
|
265
|
+
let files = candidates;
|
|
266
|
+
if (truncated) files = (await Promise.all(candidates.map(async (entry) => {
|
|
267
|
+
try {
|
|
268
|
+
return {
|
|
269
|
+
entry,
|
|
270
|
+
at: (await stat(join(dir, entry.name))).mtimeMs
|
|
271
|
+
};
|
|
272
|
+
} catch {
|
|
273
|
+
return {
|
|
274
|
+
entry,
|
|
275
|
+
at: 0
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
}))).sort((left, right) => right.at - left.at).slice(0, MAX_FILES).map((item) => item.entry);
|
|
279
|
+
let cursor = 0;
|
|
280
|
+
const workers = Array.from({ length: Math.min(16, files.length) }, async () => {
|
|
281
|
+
while (true) {
|
|
282
|
+
const index = cursor;
|
|
283
|
+
cursor += 1;
|
|
284
|
+
const entry = files[index];
|
|
285
|
+
if (entry === void 0) return;
|
|
286
|
+
try {
|
|
287
|
+
const meta = metadataOf(JSON.parse(await readFile(join(dir, entry.name), "utf8")));
|
|
288
|
+
if (meta !== void 0) {
|
|
289
|
+
meta.id = entry.name.replace(/\.json$/i, "");
|
|
290
|
+
sessions.push(meta);
|
|
291
|
+
}
|
|
292
|
+
} catch (error) {
|
|
293
|
+
bestEffortFailed("projection file skipped", error);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
await Promise.all(workers);
|
|
298
|
+
} catch (error) {
|
|
299
|
+
bestEffortFailed("projection cache is unavailable", error);
|
|
300
|
+
}
|
|
301
|
+
const report = {
|
|
302
|
+
...aggregateSessions(sessions, clamped, Date.now(), priceOverrides),
|
|
303
|
+
truncated,
|
|
304
|
+
totalFiles
|
|
305
|
+
};
|
|
306
|
+
cache.set(cacheKey, {
|
|
307
|
+
at: Date.now(),
|
|
308
|
+
report
|
|
309
|
+
});
|
|
310
|
+
return report;
|
|
311
|
+
}
|
|
312
|
+
export { aggregateSessions, metadataOf, readModelAnalytics };
|
|
313
|
+
|
|
314
|
+
//# sourceMappingURL=analytics.js.map
|
package/lib/attempt.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// src/attempt.ts
|
|
2
|
+
/** One abort/timeout budget for one attempt; callers create a fresh one per retry. */
|
|
3
|
+
function createAttemptBudget(timeoutMs, caller) {
|
|
4
|
+
const controller = new AbortController();
|
|
5
|
+
const budget = {
|
|
6
|
+
signal: controller.signal,
|
|
7
|
+
timedOut: false,
|
|
8
|
+
callerAborted: caller?.aborted === true,
|
|
9
|
+
dispose: () => {}
|
|
10
|
+
};
|
|
11
|
+
const onAbort = () => {
|
|
12
|
+
budget.callerAborted = true;
|
|
13
|
+
controller.abort(caller?.reason);
|
|
14
|
+
};
|
|
15
|
+
caller?.addEventListener("abort", onAbort, { once: true });
|
|
16
|
+
if (caller?.aborted === true) controller.abort(caller.reason);
|
|
17
|
+
const timer = timeoutMs !== void 0 && timeoutMs > 0 ? setTimeout(() => {
|
|
18
|
+
budget.timedOut = true;
|
|
19
|
+
controller.abort();
|
|
20
|
+
}, timeoutMs) : void 0;
|
|
21
|
+
budget.dispose = () => {
|
|
22
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
23
|
+
caller?.removeEventListener("abort", onAbort);
|
|
24
|
+
};
|
|
25
|
+
return budget;
|
|
26
|
+
}
|
|
27
|
+
export { createAttemptBudget };
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=attempt.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { redactText } from "./sanitize.js";
|
|
2
|
+
// src/best-effort.ts
|
|
3
|
+
/**
|
|
4
|
+
* One place for deliberately ignored failures. Every call names what was
|
|
5
|
+
* skipped; when a host logger is registered the label and message go to debug
|
|
6
|
+
* instead of disappearing.
|
|
7
|
+
*/
|
|
8
|
+
let sink;
|
|
9
|
+
function setBestEffortLogger(logger) {
|
|
10
|
+
sink = logger;
|
|
11
|
+
}
|
|
12
|
+
function bestEffortFailed(label, error) {
|
|
13
|
+
try {
|
|
14
|
+
const message = `${label}: ${redactText(error instanceof Error ? error.message : String(error))}`;
|
|
15
|
+
sink?.warn ? sink.warn(message) : sink?.debug?.(message);
|
|
16
|
+
} catch (caught) {}
|
|
17
|
+
}
|
|
18
|
+
export { bestEffortFailed, setBestEffortLogger };
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=best-effort.js.map
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { bestEffortFailed } from "./best-effort.js";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { unlinkSync } from "node:fs";
|
|
4
|
+
import { mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import { dirname, join } from "node:path";
|
|
7
|
+
// src/catalog-cache.ts
|
|
8
|
+
const VERSION = 1;
|
|
9
|
+
function catalogCachePath() {
|
|
10
|
+
const home = process.env.DSH_HOME?.trim() || join(homedir(), ".dsh");
|
|
11
|
+
return join(home, "cache", "dsh-ollama-cloud", "catalog.json");
|
|
12
|
+
}
|
|
13
|
+
function isMeta(value) {
|
|
14
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
15
|
+
const record = value;
|
|
16
|
+
return typeof record.id === "string" && typeof record.transport === "string" && typeof record.contextWindow === "number" && Array.isArray(record.input) && Array.isArray(record.reasoning);
|
|
17
|
+
}
|
|
18
|
+
function parse(value) {
|
|
19
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
|
|
20
|
+
const record = value;
|
|
21
|
+
if (record.version !== VERSION || typeof record.baseURL !== "string") return void 0;
|
|
22
|
+
if (typeof record.at !== "number" || !Number.isFinite(record.at)) return void 0;
|
|
23
|
+
if (!Array.isArray(record.ids) || !record.ids.every((id) => typeof id === "string" && id !== "")) return void 0;
|
|
24
|
+
if (typeof record.meta !== "object" || record.meta === null || Array.isArray(record.meta)) return void 0;
|
|
25
|
+
const meta = {};
|
|
26
|
+
for (const [id, entry] of Object.entries(record.meta)) if (isMeta(entry) && entry.id === id) meta[id] = entry;
|
|
27
|
+
return {
|
|
28
|
+
version: VERSION,
|
|
29
|
+
baseURL: record.baseURL,
|
|
30
|
+
at: record.at,
|
|
31
|
+
ids: record.ids,
|
|
32
|
+
meta
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
async function loadCatalogSnapshot(baseURL) {
|
|
36
|
+
try {
|
|
37
|
+
const parsed = parse(JSON.parse(await readFile(catalogCachePath(), "utf8")));
|
|
38
|
+
if (parsed === void 0 || parsed.baseURL !== baseURL) return void 0;
|
|
39
|
+
return {
|
|
40
|
+
at: parsed.at,
|
|
41
|
+
ids: parsed.ids,
|
|
42
|
+
meta: parsed.meta
|
|
43
|
+
};
|
|
44
|
+
} catch {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async function saveCatalogSnapshot(baseURL, ids, meta) {
|
|
49
|
+
const path = catalogCachePath();
|
|
50
|
+
const temp = `${path}.${process.pid}.${createHash("sha1").update(`${Date.now()}`).digest("hex").slice(0, 8)}.tmp`;
|
|
51
|
+
const payload = {
|
|
52
|
+
version: VERSION,
|
|
53
|
+
baseURL,
|
|
54
|
+
at: Date.now(),
|
|
55
|
+
ids,
|
|
56
|
+
meta
|
|
57
|
+
};
|
|
58
|
+
try {
|
|
59
|
+
await mkdir(dirname(path), { recursive: true });
|
|
60
|
+
await writeFile(temp, `${JSON.stringify(payload)}\n`, "utf8");
|
|
61
|
+
await rename(temp, path);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
bestEffortFailed("catalog cache write failed", error);
|
|
64
|
+
try {
|
|
65
|
+
await unlink(temp);
|
|
66
|
+
} catch (unlinkError) {
|
|
67
|
+
bestEffortFailed("cleanup temp cache failed", unlinkError);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** Remove the persistent snapshot together with the in-memory cache. */
|
|
72
|
+
function clearCatalogSnapshot() {
|
|
73
|
+
try {
|
|
74
|
+
unlinkSync(catalogCachePath());
|
|
75
|
+
} catch (error) {
|
|
76
|
+
if (error.code !== "ENOENT") bestEffortFailed("clear catalog snapshot failed", error);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export { catalogCachePath, clearCatalogSnapshot, loadCatalogSnapshot, saveCatalogSnapshot };
|
|
80
|
+
|
|
81
|
+
//# sourceMappingURL=catalog-cache.js.map
|
package/lib/catalog.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ResolvedConfig, TransportId } from "./config.js";
|
|
2
|
+
import { OllamaCloudModelMeta } from "./models.generated.js";
|
|
3
|
+
declare function clearCatalogCache(): void;
|
|
4
|
+
interface EffectiveModel extends OllamaCloudModelMeta {
|
|
5
|
+
readonly transport: TransportId;
|
|
6
|
+
}
|
|
7
|
+
declare function effectiveModel(id: string, config: ResolvedConfig): EffectiveModel;
|
|
8
|
+
declare function describeModel(meta: EffectiveModel, config: ResolvedConfig): string;
|
|
9
|
+
export { EffectiveModel, clearCatalogCache, describeModel, effectiveModel };
|
|
10
|
+
//# sourceMappingURL=catalog.d.ts.map
|