@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
@@ -0,0 +1,108 @@
1
+ import { sanitizeUrl } from "./sanitize.js";
2
+ import { isIP } from "node:net";
3
+ import { lookup } from "node:dns/promises";
4
+ // src/discovery.ts
5
+ const DISCOVERY_TIMEOUT_MS = 15e3;
6
+ var DiscoveryError = class extends Error {
7
+ status;
8
+ constructor(message, status) {
9
+ super(message);
10
+ this.status = status;
11
+ this.name = "DiscoveryError";
12
+ }
13
+ };
14
+ function normalizeBase(raw) {
15
+ const value = raw.trim().replace(/\/+$/, "");
16
+ let url;
17
+ try {
18
+ url = new URL(value);
19
+ } catch {
20
+ throw new DiscoveryError("invalid endpoint URL");
21
+ }
22
+ if (url.protocol !== "http:" && url.protocol !== "https:") throw new DiscoveryError("endpoint must use http or https");
23
+ if (url.username !== "" || url.password !== "") throw new DiscoveryError("endpoint URL must not contain credentials");
24
+ return sanitizeUrl(value);
25
+ }
26
+ function isPrivateV4(address) {
27
+ const parts = address.split(".").map(Number);
28
+ if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) return true;
29
+ const [a, b] = parts;
30
+ return a === 0 || a === 10 || a === 127 || a === 100 && b >= 64 && b <= 127 || a === 169 && b === 254 || a === 172 && b >= 16 && b <= 31 || a === 192 && b === 168 || a === 192 && b === 0 || a === 198 && (b === 18 || b === 19) || a >= 224;
31
+ }
32
+ function isPrivateAddress(address) {
33
+ const clean = address.toLowerCase().replace(/^\[|\]$/g, "");
34
+ if (isIP(clean) === 4) return isPrivateV4(clean);
35
+ if (isIP(clean) !== 6) return true;
36
+ return clean === "::1" || clean === "::" || clean.startsWith("fc") || clean.startsWith("fd") || /^fe[89ab]/.test(clean) || clean.startsWith("ff") || clean.startsWith("2001:db8") || clean.startsWith("::ffff:");
37
+ }
38
+ async function assertPublicEndpoint(base, options) {
39
+ if (options.allowPrivate === true) return;
40
+ const url = new URL(base);
41
+ if (url.protocol !== "https:") throw new DiscoveryError("endpoint must use https");
42
+ const host = url.hostname.toLowerCase().replace(/^\[|\]$/g, "");
43
+ if (host === "localhost" || host.endsWith(".localhost")) throw new DiscoveryError("private endpoint refused");
44
+ let addresses;
45
+ if (isIP(host) !== 0) addresses = [host];
46
+ else try {
47
+ addresses = (await lookup(host, {
48
+ all: true,
49
+ verbatim: true
50
+ })).map((entry) => entry.address);
51
+ } catch {
52
+ throw new DiscoveryError("could not resolve endpoint");
53
+ }
54
+ if (addresses.length === 0 || addresses.some(isPrivateAddress)) throw new DiscoveryError("private endpoint refused");
55
+ }
56
+ function idsOf(payload) {
57
+ if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return [];
58
+ const record = payload;
59
+ const fromData = Array.isArray(record.data) ? record.data.map((entry) => {
60
+ if (typeof entry !== "object" || entry === null) return void 0;
61
+ const id = entry.id;
62
+ return typeof id === "string" && id !== "" ? id : void 0;
63
+ }).filter((id) => id !== void 0) : [];
64
+ if (fromData.length > 0) return fromData;
65
+ return Array.isArray(record.models) ? record.models.map((entry) => {
66
+ if (typeof entry !== "object" || entry === null) return void 0;
67
+ const item = entry;
68
+ const id = typeof item.name === "string" ? item.name : item.model;
69
+ return typeof id === "string" && id !== "" ? id : void 0;
70
+ }).filter((id) => id !== void 0) : [];
71
+ }
72
+ async function get(url, key, fetchImpl, signal) {
73
+ const response = await fetchImpl(url, {
74
+ method: "GET",
75
+ headers: {
76
+ accept: "application/json",
77
+ ...key === "" ? {} : { authorization: `Bearer ${key}` }
78
+ },
79
+ redirect: "error",
80
+ signal
81
+ });
82
+ if (!response.ok) throw new DiscoveryError(`endpoint answered ${response.status}`, response.status);
83
+ return response.json();
84
+ }
85
+ /**
86
+ * Discover a catalog for an unsaved endpoint with a one-shot key.
87
+ * This function performs no persistence and never includes the key in errors.
88
+ */
89
+ async function discoverModels(rawBase, key, fetchImpl = fetch, options = {}) {
90
+ const base = normalizeBase(rawBase);
91
+ await assertPublicEndpoint(base, options);
92
+ const budget = AbortSignal.timeout(DISCOVERY_TIMEOUT_MS);
93
+ let payload;
94
+ try {
95
+ payload = await get(`${base}/v1/models`, key, fetchImpl, budget);
96
+ } catch (error) {
97
+ if (error instanceof DiscoveryError && error.status === 401) throw error;
98
+ if (error instanceof DiscoveryError && error.status === 404) payload = await get(`${base}/api/tags`, key, fetchImpl, budget);
99
+ else throw error;
100
+ }
101
+ return {
102
+ base,
103
+ models: [...new Set(idsOf(payload))]
104
+ };
105
+ }
106
+ export { DiscoveryError, discoverModels };
107
+
108
+ //# sourceMappingURL=discovery.js.map
@@ -0,0 +1,58 @@
1
+ import { isIP } from "node:net";
2
+ // src/endpoints.ts
3
+ function isLinkLocalHost(hostname) {
4
+ const host = hostname.replace(/^\[|\]$/g, "").toLowerCase();
5
+ if (host === "0.0.0.0" || host === "::") return true;
6
+ const family = isIP(host);
7
+ if (family === 4) return host.startsWith("169.254.");
8
+ if (family === 6) return /^fe[89ab]/.test(host);
9
+ return false;
10
+ }
11
+ /**
12
+ * Endpoint URLs carry the API key, so only an absolute http(s) URL without
13
+ * embedded credentials or a fragment is accepted, and link-local/metadata
14
+ * addresses are refused.
15
+ */
16
+ function checkEndpoint(field, value) {
17
+ if (typeof value !== "string" || value.trim() === "") return {
18
+ ok: false,
19
+ error: `${field} must be a non-empty URL`
20
+ };
21
+ const candidate = value.trim();
22
+ let url;
23
+ try {
24
+ url = new URL(candidate);
25
+ } catch {
26
+ return {
27
+ ok: false,
28
+ error: `${field} must be an absolute URL`
29
+ };
30
+ }
31
+ if (url.protocol !== "http:" && url.protocol !== "https:") return {
32
+ ok: false,
33
+ error: `${field} must use http or https`
34
+ };
35
+ if (url.username !== "" || url.password !== "") return {
36
+ ok: false,
37
+ error: `${field} must not embed credentials`
38
+ };
39
+ if (url.hash !== "") return {
40
+ ok: false,
41
+ error: `${field} must not contain a fragment`
42
+ };
43
+ if (isLinkLocalHost(url.hostname)) return {
44
+ ok: false,
45
+ error: `${field} must not target a link-local address`
46
+ };
47
+ return {
48
+ ok: true,
49
+ value: candidate
50
+ };
51
+ }
52
+ function endpointOr(value, field, fallback) {
53
+ const checked = checkEndpoint(field, value);
54
+ return checked.ok ? checked.value ?? fallback : fallback;
55
+ }
56
+ export { checkEndpoint, endpointOr };
57
+
58
+ //# sourceMappingURL=endpoints.js.map
@@ -0,0 +1,9 @@
1
+ import { LlmError } from "@deepseek-ai/dsh-llm";
2
+ /**
3
+ * Translate an Ollama Cloud HTTP failure into the harness failure taxonomy.
4
+ * Ollama reports everything as `{"error": "..."}`, so both the status and the
5
+ * message matter.
6
+ */
7
+ declare function classifyResponseError(status: number, text: string, headers: Headers): LlmError;
8
+ export { classifyResponseError };
9
+ //# sourceMappingURL=errors.d.ts.map
package/lib/errors.js ADDED
@@ -0,0 +1,72 @@
1
+ import { redactText } from "./sanitize.js";
2
+ import { CONTEXT_WINDOW_EXCEEDED_CODE, LlmError, isContextWindowExceededError } from "@deepseek-ai/dsh-llm";
3
+ // src/errors.ts
4
+ function parseErrorBody(text) {
5
+ try {
6
+ const body = JSON.parse(text);
7
+ if (typeof body !== "object" || body === null) return {};
8
+ const outer = body;
9
+ const nested = outer.error;
10
+ if (typeof nested === "string") return { message: nested };
11
+ if (typeof nested === "object" && nested !== null && !Array.isArray(nested)) {
12
+ const error = nested;
13
+ return {
14
+ ...typeof error.type === "string" ? { type: error.type } : {},
15
+ ...typeof error.message === "string" ? { message: error.message } : {}
16
+ };
17
+ }
18
+ return {
19
+ ...typeof outer.type === "string" ? { type: outer.type } : {},
20
+ ...typeof outer.message === "string" ? { message: outer.message } : {}
21
+ };
22
+ } catch {
23
+ return {};
24
+ }
25
+ }
26
+ function retryAfterMs(headers) {
27
+ const raw = headers.get("retry-after");
28
+ if (raw === null || raw.trim() === "") return void 0;
29
+ const seconds = Number(raw);
30
+ if (Number.isFinite(seconds) && seconds >= 0) return Math.trunc(seconds * 1e3);
31
+ const at = Date.parse(raw);
32
+ return Number.isFinite(at) ? Math.max(0, at - Date.now()) : void 0;
33
+ }
34
+ function isQuotaText(detail) {
35
+ return /\bquota\b/.test(detail) || detail.includes("usage limit") || detail.includes("usage_limit") || detail.includes("limit reached") || detail.includes("insufficient credit") || detail.includes("insufficient balance") || detail.includes("insufficient funds") || detail.includes("credit balance") || detail.includes("out of credit") || detail.includes("payment required") || detail.includes("allowance");
36
+ }
37
+ /**
38
+ * Translate an Ollama Cloud HTTP failure into the harness failure taxonomy.
39
+ * Ollama reports everything as `{"error": "..."}`, so both the status and the
40
+ * message matter.
41
+ */
42
+ function classifyResponseError(status, text, headers) {
43
+ const body = parseErrorBody(text);
44
+ const message = redactText(body.message ?? (text.trim() === "" ? `HTTP ${status}` : text.trim().slice(0, 500)));
45
+ const detail = `${body.type ?? ""} ${message}`.toLowerCase();
46
+ const retry = retryAfterMs(headers);
47
+ const options = {
48
+ status,
49
+ ...retry === void 0 ? {} : { providerRetryAfterMs: retry }
50
+ };
51
+ const quota = isQuotaText(detail);
52
+ if (status === 402 || quota && status !== 401) return new LlmError(`Ollama Cloud quota is exhausted: ${message}`, "QUOTA", options);
53
+ if (status === 401 || status === 403) return new LlmError(`Ollama Cloud rejected the credential: ${message}. Create a key at https://ollama.com/settings/keys and store it through the credentials service.`, "INVALID_CREDENTIAL", options);
54
+ if (status === 404) return new LlmError(`Ollama Cloud does not serve this model: ${message}. Refresh the catalog and check that the id matches the cloud list exactly.`, "UNSUPPORTED_MODEL", options);
55
+ if (status === 429) return new LlmError(`Ollama Cloud rate-limited the request: ${message}`, "RATE_LIMIT", options);
56
+ if (isContextWindowExceededError(detail)) return new LlmError(`Ollama Cloud rejected the request: context window exceeded: ${message}`, CONTEXT_WINDOW_EXCEEDED_CODE, options);
57
+ if (/does not support (?:images?|vision|tools?)|unsupported (?:image|tool)|image (?:input|modality) is not supported|tools? (?:are|is) not supported/i.test(detail)) return new LlmError(`Ollama Cloud model does not accept this request: ${message}`, "UNSUPPORTED_MODEL", options);
58
+ if (/request (?:body|entity) too large|payload too large|node count cannot exceed|maximum (?:request|body) size/i.test(detail)) return new LlmError(`Ollama Cloud rejected the request body as too large: ${message}`, "PROVIDER_HTTP_ERROR", options);
59
+ if (status === 408 || status === 504) return new LlmError(`Ollama Cloud timed out (HTTP ${status}): ${message}`, "TIMEOUT", options);
60
+ if (status === 503) return new LlmError(`Ollama Cloud is at its concurrency limit or temporarily unavailable: ${message}`, "SERVER", options);
61
+ if (status >= 500) return new LlmError(`Ollama Cloud gateway error (HTTP ${status}): ${message}`, "SERVER", options);
62
+ return new LlmError(`Ollama Cloud request failed (HTTP ${status}): ${message}`, "PROVIDER_HTTP_ERROR", options);
63
+ }
64
+ /** Parse an in-band error object, when a stream reports one. */
65
+ function classifyStreamError(value, status = 200) {
66
+ if (typeof value !== "object" || value === null) return void 0;
67
+ if (value.error === void 0) return void 0;
68
+ return classifyResponseError(status, JSON.stringify(value), new Headers());
69
+ }
70
+ export { classifyResponseError, classifyStreamError };
71
+
72
+ //# sourceMappingURL=errors.js.map
package/lib/files.js ADDED
@@ -0,0 +1,13 @@
1
+ // src/files.ts
2
+ /**
3
+ * Model-facing handle for a file when the adapter receives a raw FileBlock.
4
+ * The harness normally projects files to text with an execution-world path
5
+ * before dispatch; this is the safe fallback for direct adapter calls.
6
+ */
7
+ function filePlaceholder(ref) {
8
+ const digest = String(ref.attachmentId).slice(7, 15);
9
+ return `[File ${JSON.stringify(ref.name)} (${ref.bytes} bytes, sha256:${digest}) was uploaded, but the current execution environment cannot access a readable path. Report that limitation if its contents are needed; do not claim to have read it.]`;
10
+ }
11
+ export { filePlaceholder };
12
+
13
+ //# sourceMappingURL=files.js.map
package/lib/images.js ADDED
@@ -0,0 +1,78 @@
1
+ // src/images.ts
2
+ const MAX_LONG_EDGE = 1568;
3
+ const MAX_BYTES = 1048576;
4
+ /** Total base64 image budget for one request; leaves room under the gateway cap. */
5
+ const MAX_REQUEST_IMAGE_BYTES = 25165824;
6
+ const MAX_REQUEST_IMAGES = 50;
7
+ /**
8
+ * Keep the newest images under one request-wide budget. The oldest images are
9
+ * dropped first; the transports render them as an "image omitted" placeholder
10
+ * instead of failing the whole request.
11
+ */
12
+ function selectRequestImages(refs) {
13
+ const unique = [];
14
+ const seen = /* @__PURE__ */ new Set();
15
+ for (const ref of refs) {
16
+ if (seen.has(ref.attachmentId)) continue;
17
+ seen.add(ref.attachmentId);
18
+ unique.push(ref);
19
+ }
20
+ let start = 0;
21
+ let bytes = unique.reduce((sum, ref) => sum + Math.max(0, ref.bytes), 0);
22
+ while (start < unique.length && (unique.length - start > MAX_REQUEST_IMAGES || bytes > MAX_REQUEST_IMAGE_BYTES)) {
23
+ const dropped = unique[start];
24
+ if (dropped !== void 0) bytes -= Math.max(0, dropped.bytes);
25
+ start += 1;
26
+ }
27
+ const kept = unique.slice(start);
28
+ if (kept.length === 0 && unique.length > 0) return {
29
+ kept: unique.slice(-1),
30
+ dropped: unique.slice(0, -1)
31
+ };
32
+ return {
33
+ kept,
34
+ dropped: unique.slice(0, start)
35
+ };
36
+ }
37
+ function targetOf(ref) {
38
+ const longEdge = Math.max(ref.width, ref.height);
39
+ const scale = longEdge > MAX_LONG_EDGE ? MAX_LONG_EDGE / longEdge : 1;
40
+ return {
41
+ width: Math.max(1, Math.round(ref.width * scale)),
42
+ height: Math.max(1, Math.round(ref.height * scale)),
43
+ maxBytes: MAX_BYTES
44
+ };
45
+ }
46
+ function hasImages(options) {
47
+ return options.messages.some((message) => message.content.some((block) => block.type === "image"));
48
+ }
49
+ /** Decode the distinct request images that fit the request-wide budget. */
50
+ async function resolveImages(options, store, signal) {
51
+ const refs = [];
52
+ const seen = /* @__PURE__ */ new Set();
53
+ for (const message of options.messages) for (const block of message.content) {
54
+ if (block.type !== "image" || block.offloaded === true) continue;
55
+ const ref = block.attachment;
56
+ if (seen.has(ref.attachmentId)) continue;
57
+ seen.add(ref.attachmentId);
58
+ refs.push(ref);
59
+ }
60
+ const images = /* @__PURE__ */ new Map();
61
+ for (const ref of selectRequestImages(refs).kept) {
62
+ const request = await store.readImageRequest(ref, targetOf(ref), signal);
63
+ const base64 = Buffer.from(request.data).toString("base64");
64
+ images.set(ref.attachmentId, {
65
+ mediaType: request.mediaType,
66
+ dataUrl: `data:${request.mediaType};base64,${base64}`,
67
+ width: request.width,
68
+ height: request.height
69
+ });
70
+ }
71
+ return images;
72
+ }
73
+ function offloadPlaceholder(ref) {
74
+ return `[image omitted: ${ref.name ?? ref.attachmentId}]`;
75
+ }
76
+ export { hasImages, offloadPlaceholder, resolveImages, selectRequestImages };
77
+
78
+ //# sourceMappingURL=images.js.map