@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,47 @@
1
+ import { networkInterfaces } from "node:os";
2
+ import { isIP } from "node:net";
3
+ // src/local-host.ts
4
+ const LOOPBACK_NAMES = /* @__PURE__ */ new Set(["localhost", "localhost."]);
5
+ function normalize(value) {
6
+ return value.toLowerCase().replace(/^\[|\]$/g, "").replace(/%.+$/, "");
7
+ }
8
+ /**
9
+ * A DNS name is never loopback: only a literal IPv4/IPv6 loopback address is.
10
+ * Prefix matching on the raw string would accept `127.0.0.1.attacker.example`,
11
+ * which is exactly the host a DNS-rebinding page presents.
12
+ */
13
+ function isLoopbackAddress(value) {
14
+ if (value === void 0) return false;
15
+ const address = normalize(value);
16
+ const family = isIP(address);
17
+ if (family === 4) return address.startsWith("127.");
18
+ if (family === 6) return address === "::1" || address.startsWith("::ffff:127.");
19
+ return false;
20
+ }
21
+ function isLoopbackHostname(value) {
22
+ const hostname = normalize(value);
23
+ if (LOOPBACK_NAMES.has(hostname)) return true;
24
+ return isLoopbackAddress(hostname);
25
+ }
26
+ function localAddresses() {
27
+ const addresses = /* @__PURE__ */ new Set();
28
+ for (const entries of Object.values(networkInterfaces())) for (const entry of entries ?? []) addresses.add(normalize(entry.address));
29
+ return addresses;
30
+ }
31
+ function isLocalHostname(value) {
32
+ const hostname = normalize(value);
33
+ if (isLoopbackHostname(hostname)) return true;
34
+ if (isIP(hostname) === 0) return false;
35
+ return localAddresses().has(hostname);
36
+ }
37
+ function authorityHostname(value) {
38
+ if (value === void 0 || value === "") return void 0;
39
+ try {
40
+ return normalize(new URL(`http://${value}`).hostname);
41
+ } catch {
42
+ return;
43
+ }
44
+ }
45
+ export { authorityHostname, isLocalHostname, isLoopbackAddress, isLoopbackHostname };
46
+
47
+ //# sourceMappingURL=local-host.js.map
@@ -0,0 +1,263 @@
1
+ import { effectiveMaxTokens } from "./catalog.js";
2
+ import { filePlaceholder } from "./files.js";
3
+ import { offloadPlaceholder } from "./images.js";
4
+ import { normalizeToolParameters } from "./tool-schema.js";
5
+ import "@deepseek-ai/dsh-llm";
6
+ // src/messages.ts
7
+ function textOnly(content) {
8
+ const parts = [];
9
+ for (const block of content) if (block.type === "text") parts.push(block.text);
10
+ else if (block.type === "image") parts.push(offloadPlaceholder(block.attachment));
11
+ else if (block.type === "file") parts.push(filePlaceholder(block.attachment));
12
+ return parts.join("");
13
+ }
14
+ function hasImageBlocks(content) {
15
+ return content.some((block) => block.type === "image" && block.offloaded !== true);
16
+ }
17
+ function base64Of(dataUrl) {
18
+ const comma = dataUrl.indexOf(",");
19
+ return comma < 0 ? dataUrl : dataUrl.slice(comma + 1);
20
+ }
21
+ function toolArguments(raw) {
22
+ try {
23
+ const parsed = JSON.parse(raw);
24
+ return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed : {};
25
+ } catch {
26
+ return {};
27
+ }
28
+ }
29
+ const normalizedParameterCache = /* @__PURE__ */ new WeakMap();
30
+ function normalizedParameters(parameters) {
31
+ if (typeof parameters !== "object" || parameters === null) return normalizeToolParameters(parameters);
32
+ const cached = normalizedParameterCache.get(parameters);
33
+ if (cached !== void 0) return cached.value;
34
+ const value = normalizeToolParameters(parameters);
35
+ normalizedParameterCache.set(parameters, { value });
36
+ return value;
37
+ }
38
+ function buildTools(tools) {
39
+ if (tools === void 0 || tools.length === 0) return void 0;
40
+ return tools.map((tool) => ({
41
+ type: "function",
42
+ function: {
43
+ name: tool.name,
44
+ description: tool.description,
45
+ parameters: normalizedParameters(tool.parameters)
46
+ }
47
+ }));
48
+ }
49
+ /** Convert the harness conversation into native `/api/chat` messages. */
50
+ function buildNativeMessages(options, images) {
51
+ const system = [];
52
+ if (options.system !== void 0 && options.system !== "") system.push(options.system);
53
+ const toolNames = /* @__PURE__ */ new Map();
54
+ const messages = [];
55
+ for (const message of options.messages) switch (message.role) {
56
+ case "system":
57
+ case "developer": {
58
+ const text = textOnly(message.content);
59
+ if (text !== "") system.push(text);
60
+ break;
61
+ }
62
+ case "user": {
63
+ const text = [];
64
+ const base64 = [];
65
+ for (const block of message.content) if (block.type === "text") text.push(block.text);
66
+ else if (block.type === "image") {
67
+ const image = block.offloaded === true ? void 0 : images?.get(block.attachment.attachmentId);
68
+ if (image === void 0) text.push(offloadPlaceholder(block.attachment));
69
+ else base64.push(base64Of(image.dataUrl));
70
+ } else if (block.type === "file") text.push(filePlaceholder(block.attachment));
71
+ messages.push({
72
+ role: "user",
73
+ content: text.join(""),
74
+ ...base64.length === 0 ? {} : { images: base64 }
75
+ });
76
+ break;
77
+ }
78
+ case "assistant": {
79
+ const calls = [];
80
+ for (const block of message.content) {
81
+ if (block.type !== "tool-call") continue;
82
+ toolNames.set(block.id, block.name);
83
+ calls.push({ function: {
84
+ name: block.name,
85
+ arguments: toolArguments(block.arguments)
86
+ } });
87
+ }
88
+ messages.push({
89
+ role: "assistant",
90
+ content: textOnly(message.content),
91
+ ...calls.length === 0 ? {} : { tool_calls: calls }
92
+ });
93
+ break;
94
+ }
95
+ case "tool": {
96
+ const name = toolNames.get(message.toolCallId);
97
+ messages.push({
98
+ role: "tool",
99
+ content: textOnly(message.content),
100
+ ...name === void 0 ? {} : { tool_name: name }
101
+ });
102
+ break;
103
+ }
104
+ }
105
+ if (system.length > 0) messages.unshift({
106
+ role: "system",
107
+ content: system.join("\n\n")
108
+ });
109
+ return messages;
110
+ }
111
+ function nativeThink(effort, model) {
112
+ if (effort === void 0 || effort === "") return void 0;
113
+ if (effort === "off" || effort === "none") return false;
114
+ if (model.reasoningToggle) return true;
115
+ if (model.reasoning.includes(effort)) return effort;
116
+ return [
117
+ "minimal",
118
+ "low",
119
+ "medium",
120
+ "high",
121
+ "xhigh",
122
+ "max"
123
+ ].includes(effort) ? effort : void 0;
124
+ }
125
+ /** Build the native `/api/chat` request body for one model. */
126
+ function buildNativeBody(options, model, config, images, numCtxOverride) {
127
+ const tools = buildTools(options.tools);
128
+ const maxTokens = options.maxTokens ?? effectiveMaxTokens(model, config);
129
+ const think = nativeThink(options.reasoningEffort === void 0 ? void 0 : String(options.reasoningEffort), model);
130
+ const temperature = options.temperature ?? config.temperature;
131
+ const stop = options.stop ?? config.stop;
132
+ const numCtx = numCtxOverride ?? config.numCtx;
133
+ const nativeOptions = {
134
+ ...temperature === void 0 ? {} : { temperature },
135
+ ...config.topK === void 0 ? {} : { top_k: config.topK },
136
+ ...config.topP === void 0 ? {} : { top_p: config.topP },
137
+ ...config.minP === void 0 ? {} : { min_p: config.minP },
138
+ ...config.seed === void 0 ? {} : { seed: config.seed },
139
+ ...config.repeatPenalty === void 0 ? {} : { repeat_penalty: config.repeatPenalty },
140
+ ...config.repeatLastN === void 0 ? {} : { repeat_last_n: config.repeatLastN },
141
+ ...numCtx === void 0 ? {} : { num_ctx: numCtx },
142
+ ...maxTokens === void 0 || maxTokens <= 0 ? {} : { num_predict: maxTokens },
143
+ ...stop === void 0 || stop.length === 0 ? {} : { stop }
144
+ };
145
+ return {
146
+ model: options.model,
147
+ messages: buildNativeMessages(options, images),
148
+ stream: true,
149
+ ...tools === void 0 ? {} : { tools },
150
+ ...think === void 0 ? {} : { think },
151
+ ...config.keepAlive === void 0 ? {} : { keep_alive: config.keepAlive },
152
+ ...Object.keys(nativeOptions).length === 0 ? {} : { options: nativeOptions }
153
+ };
154
+ }
155
+ function openAiUserContent(content, images) {
156
+ if (!hasImageBlocks(content)) return textOnly(content);
157
+ const parts = [];
158
+ for (const block of content) {
159
+ if (block.type === "text") {
160
+ parts.push({
161
+ type: "text",
162
+ text: block.text
163
+ });
164
+ continue;
165
+ }
166
+ if (block.type === "image") {
167
+ const image = block.offloaded === true ? void 0 : images?.get(block.attachment.attachmentId);
168
+ if (image === void 0) parts.push({
169
+ type: "text",
170
+ text: offloadPlaceholder(block.attachment)
171
+ });
172
+ else parts.push({
173
+ type: "image_url",
174
+ image_url: { url: image.dataUrl }
175
+ });
176
+ continue;
177
+ }
178
+ if (block.type === "file") parts.push({
179
+ type: "text",
180
+ text: filePlaceholder(block.attachment)
181
+ });
182
+ }
183
+ return parts;
184
+ }
185
+ /** Convert the harness conversation into OpenAI Chat messages. */
186
+ function buildChatMessages(options, images) {
187
+ const system = [];
188
+ if (options.system !== void 0 && options.system !== "") system.push(options.system);
189
+ const messages = [];
190
+ for (const message of options.messages) switch (message.role) {
191
+ case "system":
192
+ case "developer": {
193
+ const text = textOnly(message.content);
194
+ if (text !== "") system.push(text);
195
+ break;
196
+ }
197
+ case "user":
198
+ messages.push({
199
+ role: "user",
200
+ content: openAiUserContent(message.content, images)
201
+ });
202
+ break;
203
+ case "assistant": {
204
+ const calls = [];
205
+ for (const block of message.content) {
206
+ if (block.type !== "tool-call") continue;
207
+ calls.push({
208
+ id: block.id,
209
+ type: "function",
210
+ function: {
211
+ name: block.name,
212
+ arguments: block.arguments
213
+ }
214
+ });
215
+ }
216
+ const text = textOnly(message.content);
217
+ messages.push({
218
+ role: "assistant",
219
+ content: text !== "" ? text : calls.length > 0 ? null : "",
220
+ ...calls.length === 0 ? {} : { tool_calls: calls }
221
+ });
222
+ break;
223
+ }
224
+ case "tool": messages.push({
225
+ role: "tool",
226
+ content: textOnly(message.content),
227
+ tool_call_id: message.toolCallId
228
+ });
229
+ }
230
+ if (system.length > 0) messages.unshift({
231
+ role: "system",
232
+ content: system.join("\n\n")
233
+ });
234
+ return messages;
235
+ }
236
+ /** Build the OpenAI Chat request body for one model. */
237
+ function buildChatBody(options, model, config, images) {
238
+ const tools = buildTools(options.tools);
239
+ const maxTokens = options.maxTokens ?? effectiveMaxTokens(model, config);
240
+ const effort = options.reasoningEffort === void 0 ? void 0 : String(options.reasoningEffort);
241
+ const reasoning = effort === void 0 || effort === "" ? void 0 : effort === "off" ? "none" : effort;
242
+ const temperature = options.temperature ?? config.temperature;
243
+ const stop = options.stop ?? config.stop;
244
+ return {
245
+ model: options.model,
246
+ messages: buildChatMessages(options, images),
247
+ stream: true,
248
+ stream_options: { include_usage: true },
249
+ ...tools === void 0 ? {} : {
250
+ tools,
251
+ tool_choice: "auto"
252
+ },
253
+ ...temperature === void 0 ? {} : { temperature },
254
+ ...config.topP === void 0 ? {} : { top_p: config.topP },
255
+ ...config.seed === void 0 ? {} : { seed: config.seed },
256
+ ...maxTokens === void 0 || maxTokens <= 0 ? {} : { max_tokens: maxTokens },
257
+ ...reasoning === void 0 ? {} : { reasoning_effort: reasoning },
258
+ ...stop === void 0 || stop.length === 0 ? {} : { stop }
259
+ };
260
+ }
261
+ export { buildChatBody, buildChatMessages, buildNativeBody, buildNativeMessages, buildTools };
262
+
263
+ //# sourceMappingURL=messages.js.map
@@ -0,0 +1,26 @@
1
+ type OllamaCloudTransport = 'native' | 'openai-chat';
2
+ interface ModelCost {
3
+ readonly input: number;
4
+ readonly output: number;
5
+ readonly cacheRead: number;
6
+ readonly offPeakInput?: number;
7
+ readonly offPeakOutput?: number;
8
+ readonly offPeakCacheRead?: number;
9
+ }
10
+ interface OllamaCloudModelMeta {
11
+ readonly id: string;
12
+ readonly name: string;
13
+ readonly transport: OllamaCloudTransport;
14
+ readonly contextWindow: number;
15
+ readonly maxTokens: number;
16
+ readonly input: readonly ('text' | 'image')[];
17
+ readonly reasoning: readonly string[];
18
+ readonly reasoningToggle: boolean;
19
+ readonly tools: boolean;
20
+ readonly free: boolean;
21
+ readonly available: boolean;
22
+ readonly defaultReasoning?: string;
23
+ readonly cost: ModelCost;
24
+ }
25
+ export { ModelCost, OllamaCloudModelMeta, OllamaCloudTransport };
26
+ //# sourceMappingURL=models.generated.d.ts.map