@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/routing.js ADDED
@@ -0,0 +1,20 @@
1
+ // src/routing.ts
2
+ function trim(url) {
3
+ return url.replace(/\/+$/, "");
4
+ }
5
+ function rootFor(config) {
6
+ return trim(config.apiBase);
7
+ }
8
+ function modelsUrlFor(config) {
9
+ return `${rootFor(config)}/v1/models`;
10
+ }
11
+ function tagsUrlFor(config) {
12
+ return `${rootFor(config)}/api/tags`;
13
+ }
14
+ /** Endpoint for one model and one wire protocol. */
15
+ function endpointFor(transport, config) {
16
+ return transport === "openai-chat" ? `${trim(config.openAiBase)}/chat/completions` : `${rootFor(config)}/api/chat`;
17
+ }
18
+ export { endpointFor, modelsUrlFor, rootFor, tagsUrlFor };
19
+
20
+ //# sourceMappingURL=routing.js.map
@@ -0,0 +1,43 @@
1
+ // src/sanitize.ts
2
+ const SECRET_QUERY = /* @__PURE__ */ new Set([
3
+ "api_key",
4
+ "apikey",
5
+ "key",
6
+ "token",
7
+ "access_token",
8
+ "refresh_token",
9
+ "password",
10
+ "passwd",
11
+ "secret",
12
+ "client_secret",
13
+ "sig",
14
+ "signature",
15
+ "credential",
16
+ "code",
17
+ "auth"
18
+ ]);
19
+ const MAX_TEXT = 300;
20
+ function redactText(value, max = MAX_TEXT) {
21
+ let text = value.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g, "").replace(/\r?\n/g, " ").replace(/\s+/g, " ").trim();
22
+ text = text.replace(/\bBearer\s+[A-Za-z0-9._~+/=-]+/gi, "Bearer [redacted]").replace(/\bsk-[A-Za-z0-9_-]{8,}\b/g, "sk-[redacted]").replace(/\b(api[_-]?key|token|secret|password|authorization)\s*[:=]\s*[^\s&,"']+/gi, "$1=[redacted]");
23
+ return text.length > max ? `${text.slice(0, Math.max(0, max - 1))}…` : text;
24
+ }
25
+ function sanitizeUrl(raw) {
26
+ try {
27
+ const url = new URL(raw);
28
+ url.username = "";
29
+ url.password = "";
30
+ url.hash = "";
31
+ for (const key of [...url.searchParams.keys()]) if (SECRET_QUERY.has(key.toLowerCase())) url.searchParams.delete(key);
32
+ return url.toString().replace(/\/$/, "");
33
+ } catch {
34
+ return redactText(raw);
35
+ }
36
+ }
37
+ function safeErrorMessage(error, fallback = "request failed") {
38
+ const text = error instanceof Error ? error.message : typeof error === "string" ? error : fallback;
39
+ return redactText(text === "" ? fallback : text);
40
+ }
41
+ export { redactText, safeErrorMessage, sanitizeUrl };
42
+
43
+ //# sourceMappingURL=sanitize.js.map
package/lib/status.js ADDED
@@ -0,0 +1,60 @@
1
+ import { safeErrorMessage } from "./sanitize.js";
2
+ import { tagsUrlFor } from "./routing.js";
3
+ // src/status.ts
4
+ const PROBE_TIMEOUT_MS = 5e3;
5
+ /**
6
+ * Probe the public Ollama Cloud tag endpoint. This request carries no key and
7
+ * does not consume quota; it only answers whether the configured API base is
8
+ * reachable from this host.
9
+ */
10
+ async function probeService(config, fetchImpl = fetch) {
11
+ const started = Date.now();
12
+ const controller = new AbortController();
13
+ const timer = setTimeout(() => controller.abort(), PROBE_TIMEOUT_MS);
14
+ try {
15
+ const response = await fetchImpl(tagsUrlFor(config), {
16
+ method: "GET",
17
+ signal: controller.signal,
18
+ headers: { accept: "application/json" }
19
+ });
20
+ const latencyMs = Date.now() - started;
21
+ return {
22
+ ok: response.ok,
23
+ status: response.status,
24
+ latencyMs,
25
+ checkedAt: (/* @__PURE__ */ new Date()).toISOString(),
26
+ ...response.ok ? {} : { error: `HTTP ${response.status}` }
27
+ };
28
+ } catch (error) {
29
+ return {
30
+ ok: false,
31
+ status: null,
32
+ latencyMs: Date.now() - started,
33
+ checkedAt: (/* @__PURE__ */ new Date()).toISOString(),
34
+ error: controller.signal.aborted ? "timeout" : safeErrorMessage(error)
35
+ };
36
+ } finally {
37
+ clearTimeout(timer);
38
+ }
39
+ }
40
+ const STATUS_CACHE_MS = 1e4;
41
+ let cached;
42
+ function clearStatusCache() {
43
+ cached = void 0;
44
+ }
45
+ /** Cached probe: repeated UI polls must not fan out external requests. */
46
+ async function probeServiceCached(config, fetchImpl = fetch, now = Date.now) {
47
+ const key = tagsUrlFor(config);
48
+ const at = now();
49
+ if (cached !== void 0 && cached.key === key && at - cached.at < STATUS_CACHE_MS) return cached.value;
50
+ const value = await probeService(config, fetchImpl);
51
+ cached = {
52
+ key,
53
+ at,
54
+ value
55
+ };
56
+ return value;
57
+ }
58
+ export { clearStatusCache, probeService, probeServiceCached };
59
+
60
+ //# sourceMappingURL=status.js.map
@@ -0,0 +1,109 @@
1
+ // src/tool-schema.ts
2
+ /**
3
+ * Normalize a tool's parameters schema to the object root the gateway
4
+ * validates. Ported behavior from the Command Code provider: pass an object
5
+ * root through, add the type to a type-less object-shaped schema, resolve a
6
+ * local root `$ref`, merge combinators, and degrade anything else to a
7
+ * permissive object rather than failing the whole turn.
8
+ */
9
+ const MAX_DEPTH = 4;
10
+ function isRecord(value) {
11
+ return typeof value === "object" && value !== null && !Array.isArray(value);
12
+ }
13
+ function isObjectShaped(node) {
14
+ return isRecord(node.properties) || isRecord(node.patternProperties) || Array.isArray(node.required) || node.additionalProperties !== void 0;
15
+ }
16
+ function requiredNames(node) {
17
+ return Array.isArray(node.required) ? node.required.filter((name) => typeof name === "string") : [];
18
+ }
19
+ function resolveLocalRef(root, ref) {
20
+ if (!ref.startsWith("#/")) return void 0;
21
+ let node = root;
22
+ for (const rawSegment of ref.slice(2).split("/")) {
23
+ const segment = rawSegment.replace(/~1/g, "/").replace(/~0/g, "~");
24
+ if (!isRecord(node)) return void 0;
25
+ node = node[segment];
26
+ }
27
+ return isRecord(node) ? node : void 0;
28
+ }
29
+ function mergeCombinatorBranches(node, depth) {
30
+ if (depth >= MAX_DEPTH) return void 0;
31
+ const properties = {};
32
+ const required = /* @__PURE__ */ new Set();
33
+ const alternatives = [];
34
+ let sawBranch = false;
35
+ for (const key of [
36
+ "allOf",
37
+ "anyOf",
38
+ "oneOf"
39
+ ]) {
40
+ const branches = node[key];
41
+ if (!Array.isArray(branches)) continue;
42
+ const objectBranches = [];
43
+ for (const branch of branches) {
44
+ if (!isRecord(branch)) continue;
45
+ objectBranches.push(normalizeToolParameters(branch, depth + 1));
46
+ }
47
+ if (objectBranches.length === 0) continue;
48
+ sawBranch = true;
49
+ const names = objectBranches.map(requiredNames);
50
+ if (key === "allOf") for (const name of names.flat()) required.add(name);
51
+ else if (names.length > 0) alternatives.push(names[0].filter((name) => names.every((list) => list.includes(name))));
52
+ for (const branch of objectBranches) {
53
+ const branchProperties = isRecord(branch.properties) ? branch.properties : {};
54
+ for (const [name, schema] of Object.entries(branchProperties)) if (!(name in properties)) properties[name] = schema;
55
+ }
56
+ }
57
+ if (!sawBranch) return void 0;
58
+ for (const group of alternatives) for (const name of group) required.add(name);
59
+ const merged = {
60
+ type: "object",
61
+ properties
62
+ };
63
+ if (required.size > 0) merged.required = [...required];
64
+ if (node.additionalProperties !== void 0) merged.additionalProperties = node.additionalProperties;
65
+ for (const key of ["description", "title"]) if (node[key] !== void 0) merged[key] = node[key];
66
+ return merged;
67
+ }
68
+ function normalizeToolParameters(parameters, depth = 0) {
69
+ if (!isRecord(parameters)) return {
70
+ type: "object",
71
+ properties: {},
72
+ additionalProperties: true
73
+ };
74
+ if (parameters.type === "object") return parameters;
75
+ if (Array.isArray(parameters.type) && parameters.type.includes("object")) return {
76
+ ...parameters,
77
+ type: "object"
78
+ };
79
+ if (parameters.type === void 0 || parameters.type === null) {
80
+ if (typeof parameters.$ref === "string" && depth < MAX_DEPTH) {
81
+ const target = resolveLocalRef(parameters, parameters.$ref);
82
+ if (target !== void 0) {
83
+ const { $ref: _ref, ...rest } = parameters;
84
+ return normalizeToolParameters({
85
+ ...target,
86
+ ...rest
87
+ }, depth + 1);
88
+ }
89
+ }
90
+ if (isObjectShaped(parameters)) return {
91
+ ...parameters,
92
+ type: "object"
93
+ };
94
+ const merged = mergeCombinatorBranches(parameters, depth);
95
+ if (merged !== void 0) return merged;
96
+ if (typeof parameters.$ref === "string") return {
97
+ ...parameters,
98
+ type: "object"
99
+ };
100
+ }
101
+ return {
102
+ type: "object",
103
+ properties: {},
104
+ additionalProperties: true
105
+ };
106
+ }
107
+ export { normalizeToolParameters };
108
+
109
+ //# sourceMappingURL=tool-schema.js.map
package/lib/tools.js ADDED
@@ -0,0 +1,38 @@
1
+ /** Harness tools that must survive the cap when a profile defines too many. */
2
+ const CORE_TOOLS = /* @__PURE__ */ new Set([
3
+ "read",
4
+ "write",
5
+ "edit",
6
+ "apply_patch",
7
+ "bash",
8
+ "glob",
9
+ "grep",
10
+ "task",
11
+ "todo",
12
+ "web_search",
13
+ "web_fetch",
14
+ "notebook"
15
+ ]);
16
+ /**
17
+ * Deduplicate tools by name and keep at most the provider's limit. Duplicate
18
+ * names would be ambiguous to the model anyway; the first declaration wins.
19
+ * At the cap, core harness tools move ahead of the rest so a large MCP set
20
+ * cannot silently evict file/shell access.
21
+ */
22
+ function limitTools(tools, max = 128) {
23
+ if (tools === void 0) return void 0;
24
+ const seen = /* @__PURE__ */ new Set();
25
+ const out = [];
26
+ for (const tool of tools) {
27
+ if (seen.has(tool.name)) continue;
28
+ seen.add(tool.name);
29
+ out.push(tool);
30
+ }
31
+ if (out.length <= max) return out;
32
+ const core = out.filter((tool) => CORE_TOOLS.has(tool.name));
33
+ const rest = out.filter((tool) => !CORE_TOOLS.has(tool.name));
34
+ return [...core, ...rest].slice(0, max);
35
+ }
36
+ export { limitTools };
37
+
38
+ //# sourceMappingURL=tools.js.map
@@ -0,0 +1,156 @@
1
+ import { classifyStreamError } from "../errors.js";
2
+ import { ndjsonObjects } from "./ndjson.js";
3
+ import { uniqueToolCallId } from "./tool-call-id.js";
4
+ import { LlmError, ToolCallId } from "@deepseek-ai/dsh-llm";
5
+ // src/transports/native.ts
6
+ function count(value) {
7
+ return typeof value === "number" && Number.isFinite(value) && value >= 0 ? Math.trunc(value) : void 0;
8
+ }
9
+ function text(value) {
10
+ return typeof value === "string" ? value : "";
11
+ }
12
+ function finishReasonOf(raw, hasToolCalls) {
13
+ if (raw === "length") return { kind: "max-tokens" };
14
+ if (hasToolCalls) return { kind: "tool-calls" };
15
+ return { kind: "stop" };
16
+ }
17
+ function usageOf(chunk) {
18
+ const prompt = count(chunk.prompt_eval_count);
19
+ const output = count(chunk.eval_count);
20
+ if (prompt === void 0 && output === void 0) return void 0;
21
+ const cached = count(chunk.prompt_eval_cached_count) ?? 0;
22
+ return {
23
+ inputTokens: Math.max(0, (prompt ?? 0) - cached),
24
+ outputTokens: output ?? 0,
25
+ totalTokens: (prompt ?? 0) + (output ?? 0),
26
+ ...cached > 0 ? { cacheReadTokens: cached } : {}
27
+ };
28
+ }
29
+ /**
30
+ * Parse a native Ollama `/api/chat` NDJSON stream into harness chunks:
31
+ * text, thinking, complete tool calls (Ollama sends arguments as an object),
32
+ * counters from the final line, and a terminal finish reason.
33
+ */
34
+ async function* parseNativeStream(response, idleTimeoutMs, signal) {
35
+ if (response.body === null) throw new LlmError("Ollama Cloud returned an empty response body", "EMPTY_RESPONSE");
36
+ let nextIndex = 0;
37
+ let textIndex;
38
+ let textValue = "";
39
+ let reasoningIndex;
40
+ let reasoningValue = "";
41
+ const toolBlocks = [];
42
+ let sawContent = false;
43
+ let doneReason = "stop";
44
+ for await (const raw of ndjsonObjects(response.body, idleTimeoutMs, signal)) {
45
+ const chunk = raw;
46
+ const streamError = classifyStreamError(chunk);
47
+ if (streamError !== void 0) throw streamError;
48
+ const thinking = text(chunk.message?.thinking);
49
+ if (thinking !== "") {
50
+ if (reasoningIndex === void 0) {
51
+ reasoningIndex = nextIndex++;
52
+ yield {
53
+ type: "block-start",
54
+ index: reasoningIndex,
55
+ blockType: "reasoning"
56
+ };
57
+ }
58
+ reasoningValue += thinking;
59
+ sawContent = true;
60
+ yield {
61
+ type: "reasoning-delta",
62
+ index: reasoningIndex,
63
+ text: thinking
64
+ };
65
+ }
66
+ const content = text(chunk.message?.content);
67
+ if (content !== "") {
68
+ if (textIndex === void 0) {
69
+ textIndex = nextIndex++;
70
+ yield {
71
+ type: "block-start",
72
+ index: textIndex,
73
+ blockType: "text"
74
+ };
75
+ }
76
+ textValue += content;
77
+ sawContent = true;
78
+ yield {
79
+ type: "text-delta",
80
+ index: textIndex,
81
+ text: content
82
+ };
83
+ }
84
+ for (const call of chunk.message?.tool_calls ?? []) {
85
+ const name = text(call.function?.name);
86
+ const rawArguments = call.function?.arguments;
87
+ const argumentsText = typeof rawArguments === "string" ? rawArguments : JSON.stringify(rawArguments ?? {});
88
+ const index = nextIndex++;
89
+ const id = uniqueToolCallId(call.id);
90
+ toolBlocks.push({
91
+ index,
92
+ id,
93
+ name,
94
+ arguments: argumentsText
95
+ });
96
+ sawContent = true;
97
+ yield {
98
+ type: "block-start",
99
+ index,
100
+ blockType: "tool-call"
101
+ };
102
+ yield {
103
+ type: "tool-call-delta",
104
+ index,
105
+ id,
106
+ ...name === "" ? {} : { name },
107
+ argumentsDelta: argumentsText
108
+ };
109
+ }
110
+ if (chunk.done === true) {
111
+ doneReason = text(chunk.done_reason) || "stop";
112
+ if (reasoningIndex !== void 0) yield {
113
+ type: "block-end",
114
+ index: reasoningIndex,
115
+ block: {
116
+ type: "reasoning",
117
+ text: reasoningValue
118
+ }
119
+ };
120
+ if (textIndex !== void 0) yield {
121
+ type: "block-end",
122
+ index: textIndex,
123
+ block: {
124
+ type: "text",
125
+ text: textValue
126
+ }
127
+ };
128
+ for (const block of toolBlocks) yield {
129
+ type: "block-end",
130
+ index: block.index,
131
+ block: {
132
+ type: "tool-call",
133
+ id: ToolCallId(block.id),
134
+ name: block.name,
135
+ arguments: block.arguments === "" ? "{}" : block.arguments
136
+ }
137
+ };
138
+ const usage = usageOf(chunk);
139
+ if (usage !== void 0) yield {
140
+ type: "usage",
141
+ usage
142
+ };
143
+ if (!sawContent) throw new LlmError("Ollama Cloud completed the request without content", "EMPTY_RESPONSE");
144
+ yield {
145
+ type: "finish",
146
+ reason: finishReasonOf(doneReason, toolBlocks.length > 0)
147
+ };
148
+ return;
149
+ }
150
+ }
151
+ if (!sawContent) throw new LlmError("Ollama Cloud returned an empty stream", "EMPTY_RESPONSE");
152
+ throw new LlmError("Ollama Cloud stream ended before done:true", "TRANSPORT");
153
+ }
154
+ export { parseNativeStream };
155
+
156
+ //# sourceMappingURL=native.js.map
@@ -0,0 +1,73 @@
1
+ import { bestEffortFailed } from "../best-effort.js";
2
+ import { LlmError } from "@deepseek-ai/dsh-llm";
3
+ // src/transports/ndjson.ts
4
+ async function readWithIdleTimeout(reader, idleTimeoutMs, signal) {
5
+ if (signal?.aborted === true) throw signal.reason;
6
+ return await new Promise((resolve, reject) => {
7
+ let settled = false;
8
+ const finish = (action) => {
9
+ if (settled) return;
10
+ settled = true;
11
+ clearTimeout(timer);
12
+ signal?.removeEventListener("abort", onAbort);
13
+ action();
14
+ };
15
+ const timer = setTimeout(() => {
16
+ reader.cancel().catch((err) => bestEffortFailed("ndjson:cancel", err));
17
+ finish(() => reject(new LlmError(`Ollama Cloud stream went idle for ${idleTimeoutMs}ms`, "TIMEOUT")));
18
+ }, idleTimeoutMs);
19
+ timer.unref?.();
20
+ const onAbort = () => {
21
+ reader.cancel().catch((err) => bestEffortFailed("ndjson:cancel", err));
22
+ finish(() => reject(signal?.reason ?? new DOMException("aborted", "AbortError")));
23
+ };
24
+ signal?.addEventListener("abort", onAbort, { once: true });
25
+ reader.read().then((value) => finish(() => resolve(value)), (error) => finish(() => reject(error)));
26
+ });
27
+ }
28
+ /**
29
+ * Yield parsed NDJSON objects from a `/api/chat` stream. Ollama uses one JSON
30
+ * object per line, with no `data:` prefix, and a final `{done:true}` line.
31
+ */
32
+ async function* ndjsonObjects(body, idleTimeoutMs, signal) {
33
+ const reader = body.getReader();
34
+ const decoder = new TextDecoder();
35
+ let buffer = "";
36
+ try {
37
+ for (;;) {
38
+ const result = await readWithIdleTimeout(reader, idleTimeoutMs, signal);
39
+ if (result.done) break;
40
+ buffer += decoder.decode(result.value, { stream: true }).replace(/\r\n/g, "\n");
41
+ for (;;) {
42
+ const boundary = buffer.indexOf("\n");
43
+ if (boundary < 0) break;
44
+ const line = buffer.slice(0, boundary).trim();
45
+ buffer = buffer.slice(boundary + 1);
46
+ if (line === "") continue;
47
+ let parsed;
48
+ try {
49
+ parsed = JSON.parse(line);
50
+ } catch (error) {
51
+ bestEffortFailed("ndjson line parse failed", error);
52
+ continue;
53
+ }
54
+ if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) yield parsed;
55
+ }
56
+ }
57
+ const tail = buffer.trim();
58
+ if (tail !== "") {
59
+ let parsed;
60
+ try {
61
+ parsed = JSON.parse(tail);
62
+ } catch (error) {
63
+ bestEffortFailed("ndjson tail parse failed", error);
64
+ }
65
+ if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) yield parsed;
66
+ }
67
+ } finally {
68
+ reader.releaseLock();
69
+ }
70
+ }
71
+ export { ndjsonObjects };
72
+
73
+ //# sourceMappingURL=ndjson.js.map