@happyvertical/ai 0.79.0 → 0.80.1
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/AGENT.md +1 -1
- package/dist/chunks/anthropic-DA8sdt12.js +438 -0
- package/dist/chunks/anthropic-DA8sdt12.js.map +1 -0
- package/dist/chunks/bedrock-DDiYFUw0.js +699 -0
- package/dist/chunks/bedrock-DDiYFUw0.js.map +1 -0
- package/dist/chunks/bifrost-fM9FpW_w.js +198 -0
- package/dist/chunks/bifrost-fM9FpW_w.js.map +1 -0
- package/dist/chunks/claude-cli-CnJh5KQT.js +498 -0
- package/dist/chunks/claude-cli-CnJh5KQT.js.map +1 -0
- package/dist/chunks/gateway-admin-CiKvHst7.js +283 -0
- package/dist/chunks/gateway-admin-CiKvHst7.js.map +1 -0
- package/dist/chunks/gemini-Bu7Fke7c.js +593 -0
- package/dist/chunks/gemini-Bu7Fke7c.js.map +1 -0
- package/dist/chunks/huggingface-B07_6wHW.js +295 -0
- package/dist/chunks/huggingface-B07_6wHW.js.map +1 -0
- package/dist/chunks/litellm-D_Oo9OQ_.js +185 -0
- package/dist/chunks/litellm-D_Oo9OQ_.js.map +1 -0
- package/dist/chunks/ollama-D4ksOTO8.js +626 -0
- package/dist/chunks/ollama-D4ksOTO8.js.map +1 -0
- package/dist/chunks/openai--F38QHiJ.js +691 -0
- package/dist/chunks/openai--F38QHiJ.js.map +1 -0
- package/dist/chunks/qwen-tts-DRRbOrhA.js +333 -0
- package/dist/chunks/qwen-tts-DRRbOrhA.js.map +1 -0
- package/dist/chunks/rate-limit-BwyPGXFW.js +199 -0
- package/dist/chunks/rate-limit-BwyPGXFW.js.map +1 -0
- package/dist/chunks/safety-CEnoJS6X.js +260 -0
- package/dist/chunks/safety-CEnoJS6X.js.map +1 -0
- package/dist/chunks/types-FHs-KbGL.js +117 -0
- package/dist/chunks/types-FHs-KbGL.js.map +1 -0
- package/dist/chunks/usage-C1Y1Nlg4.js +35 -0
- package/dist/chunks/usage-C1Y1Nlg4.js.map +1 -0
- package/dist/cli/claude-context.js +17 -17
- package/dist/cli/claude-context.js.map +1 -1
- package/dist/index.js +717 -23
- package/dist/index.js.map +1 -1
- package/metadata.json +1 -1
- package/package.json +5 -5
- package/dist/chunks/anthropic-2z-82zgr.js +0 -530
- package/dist/chunks/anthropic-2z-82zgr.js.map +0 -1
- package/dist/chunks/bedrock-Dc2eVPUD.js +0 -920
- package/dist/chunks/bedrock-Dc2eVPUD.js.map +0 -1
- package/dist/chunks/bifrost-CEnCsciy.js +0 -258
- package/dist/chunks/bifrost-CEnCsciy.js.map +0 -1
- package/dist/chunks/claude-cli-X1ONjE8K.js +0 -603
- package/dist/chunks/claude-cli-X1ONjE8K.js.map +0 -1
- package/dist/chunks/gateway-admin-BUhBzXZb.js +0 -359
- package/dist/chunks/gateway-admin-BUhBzXZb.js.map +0 -1
- package/dist/chunks/gemini-CS56gY0D.js +0 -768
- package/dist/chunks/gemini-CS56gY0D.js.map +0 -1
- package/dist/chunks/huggingface-wQSfO5xA.js +0 -411
- package/dist/chunks/huggingface-wQSfO5xA.js.map +0 -1
- package/dist/chunks/index-DCXO0nZA.js +0 -1293
- package/dist/chunks/index-DCXO0nZA.js.map +0 -1
- package/dist/chunks/litellm-BMFTYbWc.js +0 -245
- package/dist/chunks/litellm-BMFTYbWc.js.map +0 -1
- package/dist/chunks/ollama-DsGDrA-c.js +0 -962
- package/dist/chunks/ollama-DsGDrA-c.js.map +0 -1
- package/dist/chunks/openai-CJEo69jb.js +0 -882
- package/dist/chunks/openai-CJEo69jb.js.map +0 -1
- package/dist/chunks/qwen-tts-BLYZ6d9s.js +0 -365
- package/dist/chunks/qwen-tts-BLYZ6d9s.js.map +0 -1
- package/dist/chunks/usage-DMWiJ2oB.js +0 -21
- package/dist/chunks/usage-DMWiJ2oB.js.map +0 -1
|
@@ -0,0 +1,626 @@
|
|
|
1
|
+
import { a as ContextLengthError, c as extractTextContent, i as ContentFilterError, o as ModelNotFoundError, r as AuthenticationError, s as RateLimitError, t as AIError } from "./types-FHs-KbGL.js";
|
|
2
|
+
import { a as normalizeBaseAIOptions, c as prepareRequestControls, o as normalizeChatOptions, s as normalizeImageGenerationOptions } from "./safety-CEnoJS6X.js";
|
|
3
|
+
import { t as emitUsage } from "./usage-C1Y1Nlg4.js";
|
|
4
|
+
import { ValidationError } from "@happyvertical/utils";
|
|
5
|
+
//#region src/shared/providers/ollama.ts
|
|
6
|
+
/**
|
|
7
|
+
* Ollama provider implementation.
|
|
8
|
+
*
|
|
9
|
+
* Uses Ollama's native `/api/*` endpoints for chat, completions, embeddings,
|
|
10
|
+
* and model discovery, while bridging to the experimental OpenAI-compatible
|
|
11
|
+
* image-generation endpoint when available.
|
|
12
|
+
*/
|
|
13
|
+
var DEFAULT_OLLAMA_HOST = "http://localhost:11434";
|
|
14
|
+
var OLLAMA_CAPABILITIES = {
|
|
15
|
+
chat: true,
|
|
16
|
+
completion: true,
|
|
17
|
+
embeddings: true,
|
|
18
|
+
streaming: true,
|
|
19
|
+
functions: true,
|
|
20
|
+
vision: true,
|
|
21
|
+
fineTuning: false,
|
|
22
|
+
imageEmbeddings: true,
|
|
23
|
+
imageGeneration: true,
|
|
24
|
+
tts: false,
|
|
25
|
+
voiceCloning: false,
|
|
26
|
+
voiceDesign: false,
|
|
27
|
+
maxContextLength: 131072,
|
|
28
|
+
supportedOperations: [
|
|
29
|
+
"chat",
|
|
30
|
+
"completion",
|
|
31
|
+
"embedding",
|
|
32
|
+
"streaming",
|
|
33
|
+
"functions",
|
|
34
|
+
"vision",
|
|
35
|
+
"image_embedding",
|
|
36
|
+
"image_generation"
|
|
37
|
+
]
|
|
38
|
+
};
|
|
39
|
+
function stripTrailingSlash(value) {
|
|
40
|
+
return value.replace(/\/+$/, "");
|
|
41
|
+
}
|
|
42
|
+
function normalizeHost(baseUrl) {
|
|
43
|
+
const rawBase = baseUrl?.trim() || DEFAULT_OLLAMA_HOST;
|
|
44
|
+
const base = stripTrailingSlash(/^[a-z][a-z0-9+.-]*:\/\//i.test(rawBase) ? rawBase : `http://${rawBase}`);
|
|
45
|
+
if (base.endsWith("/api")) return base.slice(0, -4);
|
|
46
|
+
if (base.endsWith("/v1")) return base.slice(0, -3);
|
|
47
|
+
return base;
|
|
48
|
+
}
|
|
49
|
+
function isEmbeddingModel(modelId) {
|
|
50
|
+
return /(?:^|[-_:/])(?:embed|embedding)|nomic-embed|mxbai-embed/i.test(modelId);
|
|
51
|
+
}
|
|
52
|
+
function isImageGenerationModel(modelId) {
|
|
53
|
+
return /(?:^|[-_:/])(flux|sdxl|stable(?:[-_ ]?diffusion)?|z-image|image(?:[-_ ]?(?:turbo|gen|generator)))/i.test(modelId);
|
|
54
|
+
}
|
|
55
|
+
function inferVisionSupport(modelId, capabilities) {
|
|
56
|
+
if (capabilities.has("vision")) return true;
|
|
57
|
+
return /vision|llava|bakllava|moondream|pixtral|qwen.*vl|vl-|gemma3/i.test(modelId);
|
|
58
|
+
}
|
|
59
|
+
function inferFunctionSupport(modelId, capabilities) {
|
|
60
|
+
if (capabilities.has("tools") || capabilities.has("tool_calling") || capabilities.has("function_calling")) return true;
|
|
61
|
+
if (isEmbeddingModel(modelId) || isImageGenerationModel(modelId)) return false;
|
|
62
|
+
return /qwen|llama3|gpt-oss|mistral|deepseek|command-r|phi4/i.test(modelId);
|
|
63
|
+
}
|
|
64
|
+
function parseCapabilities(modelId, show) {
|
|
65
|
+
const capabilities = new Set((show?.capabilities || []).map((capability) => capability.toLowerCase()));
|
|
66
|
+
if (isImageGenerationModel(modelId)) return ["image_generation"];
|
|
67
|
+
if (capabilities.has("embedding") || capabilities.has("embeddings") || isEmbeddingModel(modelId)) return ["embeddings"];
|
|
68
|
+
const supported = /* @__PURE__ */ new Set(["text", "chat"]);
|
|
69
|
+
if (inferVisionSupport(modelId, capabilities)) supported.add("vision");
|
|
70
|
+
if (inferFunctionSupport(modelId, capabilities)) supported.add("functions");
|
|
71
|
+
return [...supported];
|
|
72
|
+
}
|
|
73
|
+
function parseContextLength(modelId, show) {
|
|
74
|
+
const modelInfo = show?.model_info || {};
|
|
75
|
+
for (const [key, value] of Object.entries(modelInfo)) if (key.endsWith(".context_length") && typeof value === "number") return value;
|
|
76
|
+
const numCtxMatch = show?.parameters?.match(/\bnum_ctx\s+(\d+)/);
|
|
77
|
+
if (numCtxMatch?.[1]) return Number(numCtxMatch[1]);
|
|
78
|
+
if (isEmbeddingModel(modelId)) return 8192;
|
|
79
|
+
if (/gemma3|qwen3|gpt-oss|llama3/i.test(modelId)) return 131072;
|
|
80
|
+
return 32768;
|
|
81
|
+
}
|
|
82
|
+
function mapUsage(promptTokens, completionTokens) {
|
|
83
|
+
if (typeof promptTokens !== "number" && typeof completionTokens !== "number") return;
|
|
84
|
+
const prompt = promptTokens || 0;
|
|
85
|
+
const completion = completionTokens || 0;
|
|
86
|
+
return {
|
|
87
|
+
promptTokens: prompt,
|
|
88
|
+
completionTokens: completion,
|
|
89
|
+
totalTokens: prompt + completion
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function mapFinishReason(reason) {
|
|
93
|
+
switch (reason) {
|
|
94
|
+
case "stop": return "stop";
|
|
95
|
+
case "length": return "length";
|
|
96
|
+
case "content_filter": return "content_filter";
|
|
97
|
+
default: return "stop";
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function scoreModelForCapability(model, capability) {
|
|
101
|
+
const id = model.id.toLowerCase();
|
|
102
|
+
let score = 0;
|
|
103
|
+
if (id.includes(":latest")) score += 6;
|
|
104
|
+
if (id.includes("instruct") || id.includes("-it")) score += 6;
|
|
105
|
+
if (id.startsWith("hf.co/")) score -= 8;
|
|
106
|
+
if (/ocr|rerank|embed|image|diffusion|flux/.test(id)) score -= 12;
|
|
107
|
+
switch (capability) {
|
|
108
|
+
case "vision":
|
|
109
|
+
if (model.supportsVision) score += 25;
|
|
110
|
+
if (/gemma4|gemma3/.test(id)) score += 12;
|
|
111
|
+
if (/qwen.*vl|llava|moondream|vision/.test(id)) score += 8;
|
|
112
|
+
if (/ocr/.test(id)) score -= 10;
|
|
113
|
+
break;
|
|
114
|
+
case "embedding":
|
|
115
|
+
if (model.capabilities.includes("embeddings")) score += 20;
|
|
116
|
+
if (/nomic|mxbai|embed/.test(id)) score += 6;
|
|
117
|
+
break;
|
|
118
|
+
case "image_generation":
|
|
119
|
+
if (model.capabilities.includes("image_generation")) score += 20;
|
|
120
|
+
if (/flux|sdxl|stable/.test(id)) score += 8;
|
|
121
|
+
break;
|
|
122
|
+
default:
|
|
123
|
+
if (model.capabilities.includes("chat")) score += 12;
|
|
124
|
+
if (model.supportsFunctions) score += 4;
|
|
125
|
+
if (/llama|mistral|qwen|gemma|phi|command-r|deepseek-coder/.test(id)) score += 8;
|
|
126
|
+
if (/gpt-oss/.test(id)) score -= 3;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
return score;
|
|
130
|
+
}
|
|
131
|
+
var OllamaProvider = class {
|
|
132
|
+
options;
|
|
133
|
+
host;
|
|
134
|
+
configuredDefaultModel;
|
|
135
|
+
modelListPromise;
|
|
136
|
+
modelShowCache = /* @__PURE__ */ new Map();
|
|
137
|
+
resolvedModelCache = /* @__PURE__ */ new Map();
|
|
138
|
+
constructor(options) {
|
|
139
|
+
this.host = normalizeHost(options.baseUrl);
|
|
140
|
+
this.options = normalizeBaseAIOptions({
|
|
141
|
+
...options,
|
|
142
|
+
baseUrl: this.host
|
|
143
|
+
});
|
|
144
|
+
this.configuredDefaultModel = options.defaultModel;
|
|
145
|
+
}
|
|
146
|
+
get nativeBaseUrl() {
|
|
147
|
+
return `${this.host}/api`;
|
|
148
|
+
}
|
|
149
|
+
get compatibilityBaseUrl() {
|
|
150
|
+
return `${this.host}/v1`;
|
|
151
|
+
}
|
|
152
|
+
buildHeaders(stream = false) {
|
|
153
|
+
const headers = new Headers(this.options.headers);
|
|
154
|
+
headers.set("Content-Type", "application/json");
|
|
155
|
+
headers.set("Accept", stream ? "application/x-ndjson, application/json" : "application/json");
|
|
156
|
+
if (this.options.apiKey) headers.set("Authorization", `Bearer ${this.options.apiKey}`);
|
|
157
|
+
return headers;
|
|
158
|
+
}
|
|
159
|
+
async buildHttpError(response) {
|
|
160
|
+
const text = await response.text();
|
|
161
|
+
const message = text && text.trim().length > 0 ? text.trim() : `Ollama request failed with status ${response.status}`;
|
|
162
|
+
switch (response.status) {
|
|
163
|
+
case 401:
|
|
164
|
+
case 403: return new AuthenticationError("ollama");
|
|
165
|
+
case 404: return new ModelNotFoundError(message, "ollama");
|
|
166
|
+
case 413: return new ContextLengthError("ollama");
|
|
167
|
+
case 429: return new RateLimitError("ollama");
|
|
168
|
+
default:
|
|
169
|
+
if (/content[_ -]?filter/i.test(message)) return new ContentFilterError("ollama");
|
|
170
|
+
if (/context|too long|maximum context/i.test(message)) return new ContextLengthError("ollama");
|
|
171
|
+
return new AIError(message, "API_ERROR", "ollama");
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
mapError(error) {
|
|
175
|
+
if (error instanceof AIError) return error;
|
|
176
|
+
if (error instanceof Error && error.name === "AbortError") return new AIError("Ollama request timed out", "TIMEOUT", "ollama");
|
|
177
|
+
return new AIError(error instanceof Error ? error.message : "Unknown error occurred", "UNKNOWN_ERROR", "ollama");
|
|
178
|
+
}
|
|
179
|
+
async requestJson(path, body, options = {}) {
|
|
180
|
+
const baseUrl = options.compatibility ? this.compatibilityBaseUrl : this.nativeBaseUrl;
|
|
181
|
+
const response = await fetch(`${baseUrl}${path}`, {
|
|
182
|
+
method: body ? "POST" : "GET",
|
|
183
|
+
headers: this.buildHeaders(),
|
|
184
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
185
|
+
signal: options.signal
|
|
186
|
+
});
|
|
187
|
+
if (!response.ok) throw await this.buildHttpError(response);
|
|
188
|
+
return await response.json();
|
|
189
|
+
}
|
|
190
|
+
async requestStream(path, body, signal) {
|
|
191
|
+
const response = await fetch(`${this.nativeBaseUrl}${path}`, {
|
|
192
|
+
method: "POST",
|
|
193
|
+
headers: this.buildHeaders(true),
|
|
194
|
+
body: JSON.stringify(body),
|
|
195
|
+
signal
|
|
196
|
+
});
|
|
197
|
+
if (!response.ok) throw await this.buildHttpError(response);
|
|
198
|
+
return response;
|
|
199
|
+
}
|
|
200
|
+
async *parseNdjson(response) {
|
|
201
|
+
const reader = response.body?.getReader();
|
|
202
|
+
if (!reader) return;
|
|
203
|
+
const decoder = new TextDecoder();
|
|
204
|
+
let buffer = "";
|
|
205
|
+
while (true) {
|
|
206
|
+
const { value, done } = await reader.read();
|
|
207
|
+
if (done) break;
|
|
208
|
+
buffer += decoder.decode(value, { stream: true });
|
|
209
|
+
let newlineIndex = buffer.indexOf("\n");
|
|
210
|
+
while (newlineIndex !== -1) {
|
|
211
|
+
const line = buffer.slice(0, newlineIndex).trim();
|
|
212
|
+
buffer = buffer.slice(newlineIndex + 1);
|
|
213
|
+
if (line) yield JSON.parse(line);
|
|
214
|
+
newlineIndex = buffer.indexOf("\n");
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
buffer += decoder.decode();
|
|
218
|
+
const line = buffer.trim();
|
|
219
|
+
if (line) yield JSON.parse(line);
|
|
220
|
+
}
|
|
221
|
+
async showModel(model) {
|
|
222
|
+
const cached = this.modelShowCache.get(model);
|
|
223
|
+
if (cached) return cached;
|
|
224
|
+
const pending = this.requestJson("/show", {
|
|
225
|
+
model,
|
|
226
|
+
verbose: false
|
|
227
|
+
}).catch((_error) => null);
|
|
228
|
+
this.modelShowCache.set(model, pending);
|
|
229
|
+
return pending;
|
|
230
|
+
}
|
|
231
|
+
async resolveModel(capability, explicitModel) {
|
|
232
|
+
if (explicitModel) return explicitModel;
|
|
233
|
+
if (this.configuredDefaultModel && (capability === "chat" || capability === "vision")) return this.configuredDefaultModel;
|
|
234
|
+
const cached = this.resolvedModelCache.get(capability);
|
|
235
|
+
if (cached) return cached;
|
|
236
|
+
const pending = this.selectModel(capability).catch((error) => {
|
|
237
|
+
this.resolvedModelCache.delete(capability);
|
|
238
|
+
throw error;
|
|
239
|
+
});
|
|
240
|
+
this.resolvedModelCache.set(capability, pending);
|
|
241
|
+
return pending;
|
|
242
|
+
}
|
|
243
|
+
async selectModel(capability) {
|
|
244
|
+
const model = (await this.getModels()).filter((candidate) => {
|
|
245
|
+
switch (capability) {
|
|
246
|
+
case "vision": return candidate.capabilities.includes("vision") || candidate.supportsVision === true;
|
|
247
|
+
case "embedding": return candidate.capabilities.includes("embeddings");
|
|
248
|
+
case "image_generation": return candidate.capabilities.includes("image_generation");
|
|
249
|
+
default: return candidate.capabilities.includes("chat");
|
|
250
|
+
}
|
|
251
|
+
}).sort((left, right) => scoreModelForCapability(right, capability) - scoreModelForCapability(left, capability))[0];
|
|
252
|
+
if (!model) throw new ValidationError(`No ${capability} model is available from the Ollama host`, {
|
|
253
|
+
provider: "ollama",
|
|
254
|
+
capability,
|
|
255
|
+
hint: "Pass model/defaultModel explicitly or make sure the Ollama host has a compatible model installed"
|
|
256
|
+
});
|
|
257
|
+
return model.id;
|
|
258
|
+
}
|
|
259
|
+
isVisionRequest(messages) {
|
|
260
|
+
return messages.some((message) => Array.isArray(message.content) && message.content.some((part) => part.type === "image_url"));
|
|
261
|
+
}
|
|
262
|
+
async imageToBase64Payload(image, signal) {
|
|
263
|
+
if (Buffer.isBuffer(image)) return image.toString("base64");
|
|
264
|
+
if (image.startsWith("data:")) {
|
|
265
|
+
const commaIndex = image.indexOf(",");
|
|
266
|
+
return commaIndex === -1 ? image : image.slice(commaIndex + 1);
|
|
267
|
+
}
|
|
268
|
+
const response = await fetch(image, { signal });
|
|
269
|
+
if (!response.ok) throw new AIError(`Failed to fetch image: ${response.status} ${response.statusText}`, "IMAGE_FETCH_ERROR", "ollama");
|
|
270
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
271
|
+
return Buffer.from(arrayBuffer).toString("base64");
|
|
272
|
+
}
|
|
273
|
+
async imageToDataUrl(image, signal) {
|
|
274
|
+
if (Buffer.isBuffer(image)) return `data:image/png;base64,${image.toString("base64")}`;
|
|
275
|
+
if (image.startsWith("data:")) return image;
|
|
276
|
+
const response = await fetch(image, { signal });
|
|
277
|
+
if (!response.ok) throw new AIError(`Failed to fetch image: ${response.status} ${response.statusText}`, "IMAGE_FETCH_ERROR", "ollama");
|
|
278
|
+
const contentType = response.headers.get("content-type") || "image/png";
|
|
279
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
280
|
+
return `data:${contentType};base64,${Buffer.from(arrayBuffer).toString("base64")}`;
|
|
281
|
+
}
|
|
282
|
+
parseToolArguments(argumentsText) {
|
|
283
|
+
if (!argumentsText) return {};
|
|
284
|
+
try {
|
|
285
|
+
const parsed = JSON.parse(argumentsText);
|
|
286
|
+
return parsed && typeof parsed === "object" ? parsed : {};
|
|
287
|
+
} catch {
|
|
288
|
+
return {};
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
async mapMessagesToOllama(messages, signal) {
|
|
292
|
+
return await Promise.all(messages.map(async (message) => {
|
|
293
|
+
const content = extractTextContent(message.content);
|
|
294
|
+
const mapped = {
|
|
295
|
+
role: message.role === "function" ? "tool" : message.role,
|
|
296
|
+
content
|
|
297
|
+
};
|
|
298
|
+
if (Array.isArray(message.content)) {
|
|
299
|
+
const imageParts = message.content.filter((part) => part.type === "image_url");
|
|
300
|
+
if (imageParts.length > 0) mapped.images = await Promise.all(imageParts.map((part) => this.imageToBase64Payload(part.image_url.url, signal)));
|
|
301
|
+
}
|
|
302
|
+
if (mapped.role === "assistant" && Array.isArray(message.tool_calls) && message.tool_calls.length > 0) mapped.tool_calls = message.tool_calls.map((toolCall, index) => ({
|
|
303
|
+
type: "function",
|
|
304
|
+
function: {
|
|
305
|
+
index,
|
|
306
|
+
name: toolCall.function.name,
|
|
307
|
+
arguments: this.parseToolArguments(toolCall.function.arguments)
|
|
308
|
+
}
|
|
309
|
+
}));
|
|
310
|
+
if (mapped.role === "tool" && message.name) mapped.tool_name = message.name;
|
|
311
|
+
return mapped;
|
|
312
|
+
}));
|
|
313
|
+
}
|
|
314
|
+
mapTools(tools, toolChoice) {
|
|
315
|
+
if (!tools || tools.length === 0 || toolChoice === "none") return;
|
|
316
|
+
const scopedTools = toolChoice && typeof toolChoice === "object" ? tools.filter((tool) => tool.function.name === toolChoice.function.name) : tools;
|
|
317
|
+
if (scopedTools.length === 0) return;
|
|
318
|
+
return scopedTools.map((tool) => ({
|
|
319
|
+
type: "function",
|
|
320
|
+
function: {
|
|
321
|
+
name: tool.function.name,
|
|
322
|
+
description: tool.function.description,
|
|
323
|
+
parameters: tool.function.parameters || { type: "object" }
|
|
324
|
+
}
|
|
325
|
+
}));
|
|
326
|
+
}
|
|
327
|
+
buildRuntimeOptions(options) {
|
|
328
|
+
const runtimeOptions = {};
|
|
329
|
+
if (typeof options.maxTokens === "number") runtimeOptions.num_predict = options.maxTokens;
|
|
330
|
+
if (typeof options.temperature === "number") runtimeOptions.temperature = options.temperature;
|
|
331
|
+
if (typeof options.topP === "number") runtimeOptions.top_p = options.topP;
|
|
332
|
+
if (options.stop) runtimeOptions.stop = Array.isArray(options.stop) ? options.stop : [options.stop];
|
|
333
|
+
if (typeof options.seed === "number") runtimeOptions.seed = options.seed;
|
|
334
|
+
return Object.keys(runtimeOptions).length > 0 ? runtimeOptions : void 0;
|
|
335
|
+
}
|
|
336
|
+
mapThink(options) {
|
|
337
|
+
if (options.reasoning?.maxTokens === 0) return false;
|
|
338
|
+
if (options.reasoning?.effort) return options.reasoning.effort === "none" ? false : options.reasoning.effort;
|
|
339
|
+
if (!Object.hasOwn(options, "thinkingLevel")) return;
|
|
340
|
+
return options.thinkingLevel;
|
|
341
|
+
}
|
|
342
|
+
async chat(messages, options = {}) {
|
|
343
|
+
const startTime = Date.now();
|
|
344
|
+
let controls;
|
|
345
|
+
try {
|
|
346
|
+
options = normalizeChatOptions(this.options, options, "ollama", options.model || this.options.defaultModel);
|
|
347
|
+
controls = prepareRequestControls(this.options, options);
|
|
348
|
+
const model = await this.resolveModel(this.isVisionRequest(messages) ? "vision" : "chat", options.model);
|
|
349
|
+
const response = await this.requestJson("/chat", {
|
|
350
|
+
model,
|
|
351
|
+
messages: await this.mapMessagesToOllama(messages, controls.signal),
|
|
352
|
+
stream: false,
|
|
353
|
+
format: options.responseFormat?.type === "json_object" ? "json" : void 0,
|
|
354
|
+
tools: this.mapTools(options.tools, options.toolChoice),
|
|
355
|
+
think: this.mapThink(options),
|
|
356
|
+
keep_alive: this.options.keepAlive,
|
|
357
|
+
options: this.buildRuntimeOptions(options)
|
|
358
|
+
}, { signal: controls.signal });
|
|
359
|
+
const usage = mapUsage(response.prompt_eval_count, response.eval_count);
|
|
360
|
+
emitUsage(this.options, "ollama", "chat", response.model || model, usage, startTime, options.usageTags);
|
|
361
|
+
const toolCalls = response.message?.tool_calls?.filter((call) => call.function?.name).map((call, index) => ({
|
|
362
|
+
id: `${response.model || model}-tool-${index + 1}`,
|
|
363
|
+
type: "function",
|
|
364
|
+
function: {
|
|
365
|
+
name: call.function?.name || "",
|
|
366
|
+
arguments: JSON.stringify(call.function?.arguments || {})
|
|
367
|
+
}
|
|
368
|
+
})) || void 0;
|
|
369
|
+
return {
|
|
370
|
+
content: response.message?.content || "",
|
|
371
|
+
model: response.model || model,
|
|
372
|
+
usage,
|
|
373
|
+
finishReason: toolCalls && toolCalls.length > 0 ? "tool_calls" : mapFinishReason(response.done_reason),
|
|
374
|
+
toolCalls: toolCalls && toolCalls.length > 0 ? toolCalls : void 0
|
|
375
|
+
};
|
|
376
|
+
} catch (error) {
|
|
377
|
+
if (controls?.didTimeout()) throw new AIError(`AI request timed out after ${controls.timeout}ms`, "AI_TIMEOUT", "ollama", options.model);
|
|
378
|
+
if (options.signal?.aborted) throw new AIError("AI request aborted by caller", "AI_ABORTED", "ollama", options.model);
|
|
379
|
+
throw this.mapError(error);
|
|
380
|
+
} finally {
|
|
381
|
+
controls?.cleanup();
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
async complete(prompt, options = {}) {
|
|
385
|
+
const startTime = Date.now();
|
|
386
|
+
let controls;
|
|
387
|
+
try {
|
|
388
|
+
options = normalizeChatOptions(this.options, options, "ollama", options.model || this.options.defaultModel);
|
|
389
|
+
controls = prepareRequestControls(this.options, options);
|
|
390
|
+
const model = await this.resolveModel("chat", options.model);
|
|
391
|
+
const response = await this.requestJson("/generate", {
|
|
392
|
+
model,
|
|
393
|
+
prompt,
|
|
394
|
+
stream: false,
|
|
395
|
+
keep_alive: this.options.keepAlive,
|
|
396
|
+
options: this.buildRuntimeOptions(options)
|
|
397
|
+
}, { signal: controls.signal });
|
|
398
|
+
const usage = mapUsage(response.prompt_eval_count, response.eval_count);
|
|
399
|
+
emitUsage(this.options, "ollama", "complete", response.model || model, usage, startTime, options.usageTags);
|
|
400
|
+
return {
|
|
401
|
+
content: response.response || "",
|
|
402
|
+
model: response.model || model,
|
|
403
|
+
usage,
|
|
404
|
+
finishReason: mapFinishReason(response.done_reason)
|
|
405
|
+
};
|
|
406
|
+
} catch (error) {
|
|
407
|
+
if (controls?.didTimeout()) throw new AIError(`AI request timed out after ${controls.timeout}ms`, "AI_TIMEOUT", "ollama", options.model);
|
|
408
|
+
if (options.signal?.aborted) throw new AIError("AI request aborted by caller", "AI_ABORTED", "ollama", options.model);
|
|
409
|
+
throw this.mapError(error);
|
|
410
|
+
} finally {
|
|
411
|
+
controls?.cleanup();
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
async message(text, options = {}) {
|
|
415
|
+
const messages = [...options.history || [], {
|
|
416
|
+
role: options.role || "user",
|
|
417
|
+
content: text
|
|
418
|
+
}];
|
|
419
|
+
return (await this.chat(messages, {
|
|
420
|
+
model: options.model,
|
|
421
|
+
maxTokens: options.maxTokens,
|
|
422
|
+
temperature: options.temperature,
|
|
423
|
+
topP: options.topP,
|
|
424
|
+
stop: options.stop,
|
|
425
|
+
stream: options.stream,
|
|
426
|
+
frequencyPenalty: options.frequencyPenalty,
|
|
427
|
+
presencePenalty: options.presencePenalty,
|
|
428
|
+
responseFormat: options.responseFormat,
|
|
429
|
+
seed: options.seed,
|
|
430
|
+
tools: options.tools,
|
|
431
|
+
toolChoice: options.toolChoice,
|
|
432
|
+
onProgress: options.onProgress,
|
|
433
|
+
signal: options.signal,
|
|
434
|
+
timeout: options.timeout,
|
|
435
|
+
reasoning: options.reasoning,
|
|
436
|
+
usageTags: options.usageTags
|
|
437
|
+
})).content;
|
|
438
|
+
}
|
|
439
|
+
async embed(text, options = {}) {
|
|
440
|
+
const startTime = Date.now();
|
|
441
|
+
try {
|
|
442
|
+
const model = await this.resolveModel("embedding", options.model);
|
|
443
|
+
const input = Array.isArray(text) ? text : [text];
|
|
444
|
+
const response = await this.requestJson("/embed", {
|
|
445
|
+
model,
|
|
446
|
+
input,
|
|
447
|
+
keep_alive: this.options.keepAlive
|
|
448
|
+
});
|
|
449
|
+
const embeddings = response.embeddings || (response.embedding ? [response.embedding] : void 0);
|
|
450
|
+
if (!embeddings || embeddings.length === 0) throw new AIError("Invalid embedding response from Ollama", "INVALID_RESPONSE", "ollama");
|
|
451
|
+
const usage = mapUsage(response.prompt_eval_count, 0);
|
|
452
|
+
emitUsage(this.options, "ollama", "embed", response.model || model, usage, startTime, options.usageTags);
|
|
453
|
+
return {
|
|
454
|
+
embeddings,
|
|
455
|
+
usage,
|
|
456
|
+
model: response.model || model
|
|
457
|
+
};
|
|
458
|
+
} catch (error) {
|
|
459
|
+
throw this.mapError(error);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
async embedImage(image, options = {}) {
|
|
463
|
+
const description = await this.describeImage(image, "Describe this image in detail for semantic embedding. Include objects, people, text, setting, colors, composition, and notable visual relationships.");
|
|
464
|
+
return this.embed(description, {
|
|
465
|
+
model: options.model,
|
|
466
|
+
dimensions: options.dimensions,
|
|
467
|
+
user: options.user
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
async describeImage(image, prompt, options = {}) {
|
|
471
|
+
const defaultPrompt = "Describe this image for a search index. Include objects, mood, lighting, and any visible text.";
|
|
472
|
+
const imageUrl = await this.imageToDataUrl(image, options.signal);
|
|
473
|
+
return (await this.chat([{
|
|
474
|
+
role: "user",
|
|
475
|
+
content: [{
|
|
476
|
+
type: "text",
|
|
477
|
+
text: prompt || defaultPrompt
|
|
478
|
+
}, {
|
|
479
|
+
type: "image_url",
|
|
480
|
+
image_url: {
|
|
481
|
+
url: imageUrl,
|
|
482
|
+
detail: options.detail || "auto"
|
|
483
|
+
}
|
|
484
|
+
}]
|
|
485
|
+
}], {
|
|
486
|
+
model: options.model,
|
|
487
|
+
maxTokens: options.maxTokens ?? 500,
|
|
488
|
+
reasoning: options.reasoning || {
|
|
489
|
+
effort: "none",
|
|
490
|
+
maxTokens: 0
|
|
491
|
+
},
|
|
492
|
+
signal: options.signal,
|
|
493
|
+
timeout: options.timeout,
|
|
494
|
+
usageTags: options.usageTags
|
|
495
|
+
})).content;
|
|
496
|
+
}
|
|
497
|
+
async generateImage(prompt, options = {}) {
|
|
498
|
+
let controls;
|
|
499
|
+
try {
|
|
500
|
+
options = normalizeImageGenerationOptions(this.options, options, "ollama", options.model);
|
|
501
|
+
controls = prepareRequestControls(this.options, options);
|
|
502
|
+
if (options.imageInput) throw new AIError("Ollama image generation does not support imageInput in this adapter yet.", "NOT_IMPLEMENTED", "ollama");
|
|
503
|
+
if (options.outputFormat === "url") throw new AIError("Ollama image generation only supports buffer or base64 outputs in this adapter.", "NOT_SUPPORTED", "ollama");
|
|
504
|
+
const model = await this.resolveModel("image_generation", options.model);
|
|
505
|
+
return {
|
|
506
|
+
images: ((await this.requestJson("/images/generations", {
|
|
507
|
+
model,
|
|
508
|
+
prompt,
|
|
509
|
+
n: options.n,
|
|
510
|
+
size: options.size || "1024x1024",
|
|
511
|
+
response_format: "b64_json",
|
|
512
|
+
quality: options.quality,
|
|
513
|
+
style: options.style
|
|
514
|
+
}, {
|
|
515
|
+
compatibility: true,
|
|
516
|
+
signal: controls.signal
|
|
517
|
+
})).data || []).map((item) => {
|
|
518
|
+
const b64 = item.b64_json || "";
|
|
519
|
+
return {
|
|
520
|
+
data: options.outputFormat === "base64" ? b64 : Buffer.from(b64, "base64"),
|
|
521
|
+
mimeType: "image/png",
|
|
522
|
+
revisedPrompt: item.revised_prompt
|
|
523
|
+
};
|
|
524
|
+
}),
|
|
525
|
+
model
|
|
526
|
+
};
|
|
527
|
+
} catch (error) {
|
|
528
|
+
if (controls?.didTimeout()) throw new AIError(`AI request timed out after ${controls.timeout}ms`, "AI_TIMEOUT", "ollama", options.model);
|
|
529
|
+
if (options.signal?.aborted) throw new AIError("AI request aborted by caller", "AI_ABORTED", "ollama", options.model);
|
|
530
|
+
throw this.mapError(error);
|
|
531
|
+
} finally {
|
|
532
|
+
controls?.cleanup();
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
async *stream(messages, options = {}) {
|
|
536
|
+
const startTime = Date.now();
|
|
537
|
+
let controls;
|
|
538
|
+
try {
|
|
539
|
+
options = normalizeChatOptions(this.options, options, "ollama", options.model || this.options.defaultModel);
|
|
540
|
+
controls = prepareRequestControls(this.options, options);
|
|
541
|
+
const model = await this.resolveModel(this.isVisionRequest(messages) ? "vision" : "chat", options.model);
|
|
542
|
+
const response = await this.requestStream("/chat", {
|
|
543
|
+
model,
|
|
544
|
+
messages: await this.mapMessagesToOllama(messages, controls.signal),
|
|
545
|
+
stream: true,
|
|
546
|
+
format: options.responseFormat?.type === "json_object" ? "json" : void 0,
|
|
547
|
+
tools: this.mapTools(options.tools, options.toolChoice),
|
|
548
|
+
think: this.mapThink(options),
|
|
549
|
+
keep_alive: this.options.keepAlive,
|
|
550
|
+
options: this.buildRuntimeOptions(options)
|
|
551
|
+
}, controls.signal);
|
|
552
|
+
let finalUsage;
|
|
553
|
+
let finalModel = model;
|
|
554
|
+
for await (const chunk of this.parseNdjson(response)) {
|
|
555
|
+
if (chunk.model) finalModel = chunk.model;
|
|
556
|
+
if (typeof chunk.message?.content === "string" && chunk.message.content) {
|
|
557
|
+
if (options.onProgress) options.onProgress(chunk.message.content);
|
|
558
|
+
yield chunk.message.content;
|
|
559
|
+
}
|
|
560
|
+
if (chunk.done) finalUsage = mapUsage(chunk.prompt_eval_count, chunk.eval_count);
|
|
561
|
+
}
|
|
562
|
+
emitUsage(this.options, "ollama", "stream", finalModel, finalUsage, startTime, options.usageTags);
|
|
563
|
+
} catch (error) {
|
|
564
|
+
if (controls?.didTimeout()) throw new AIError(`AI request timed out after ${controls.timeout}ms`, "AI_TIMEOUT", "ollama", options.model);
|
|
565
|
+
if (options.signal?.aborted) throw new AIError("AI request aborted by caller", "AI_ABORTED", "ollama", options.model);
|
|
566
|
+
throw this.mapError(error);
|
|
567
|
+
} finally {
|
|
568
|
+
controls?.cleanup();
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
async countTokens(text) {
|
|
572
|
+
return Math.ceil(text.length / 4);
|
|
573
|
+
}
|
|
574
|
+
async getModels() {
|
|
575
|
+
if (this.modelListPromise) return this.modelListPromise;
|
|
576
|
+
this.modelListPromise = (async () => {
|
|
577
|
+
const summaries = (await this.requestJson("/tags")).models || [];
|
|
578
|
+
return Promise.all(summaries.map(async (summary) => {
|
|
579
|
+
const modelId = summary.name || summary.model;
|
|
580
|
+
if (!modelId) throw new AIError("Ollama returned a model without an identifier", "INVALID_RESPONSE", "ollama");
|
|
581
|
+
const show = await this.showModel(modelId);
|
|
582
|
+
const capabilities = parseCapabilities(modelId, show);
|
|
583
|
+
const vision = capabilities.includes("vision");
|
|
584
|
+
const functions = capabilities.includes("functions");
|
|
585
|
+
const parameterSize = show?.details?.parameter_size || summary.details?.parameter_size;
|
|
586
|
+
const family = show?.details?.family || summary.details?.family;
|
|
587
|
+
return {
|
|
588
|
+
id: modelId,
|
|
589
|
+
name: modelId,
|
|
590
|
+
description: parameterSize || family ? `${family || "Ollama"} ${parameterSize || ""}`.trim() : `Ollama model: ${modelId}`,
|
|
591
|
+
contextLength: parseContextLength(modelId, show),
|
|
592
|
+
capabilities,
|
|
593
|
+
supportsFunctions: functions,
|
|
594
|
+
supportsVision: vision
|
|
595
|
+
};
|
|
596
|
+
}));
|
|
597
|
+
})().catch((error) => {
|
|
598
|
+
this.modelListPromise = void 0;
|
|
599
|
+
throw this.mapError(error);
|
|
600
|
+
});
|
|
601
|
+
return this.modelListPromise;
|
|
602
|
+
}
|
|
603
|
+
async getCapabilities() {
|
|
604
|
+
return { ...OLLAMA_CAPABILITIES };
|
|
605
|
+
}
|
|
606
|
+
async synthesizeSpeech(_text, _options) {
|
|
607
|
+
throw new AIError("TTS is not supported by the Ollama provider.", "NOT_IMPLEMENTED", "ollama");
|
|
608
|
+
}
|
|
609
|
+
streamSpeech(_text, _options) {
|
|
610
|
+
const error = new AIError("TTS streaming is not supported by the Ollama provider.", "NOT_IMPLEMENTED", "ollama");
|
|
611
|
+
return { [Symbol.asyncIterator]: () => ({ next: () => Promise.reject(error) }) };
|
|
612
|
+
}
|
|
613
|
+
async cloneVoice(_options) {
|
|
614
|
+
throw new AIError("Voice cloning is not supported by the Ollama provider.", "NOT_IMPLEMENTED", "ollama");
|
|
615
|
+
}
|
|
616
|
+
async designVoice(_options) {
|
|
617
|
+
throw new AIError("Voice design is not supported by the Ollama provider.", "NOT_IMPLEMENTED", "ollama");
|
|
618
|
+
}
|
|
619
|
+
async getVoices(_options) {
|
|
620
|
+
throw new AIError("Voice listing is not supported by the Ollama provider.", "NOT_IMPLEMENTED", "ollama");
|
|
621
|
+
}
|
|
622
|
+
};
|
|
623
|
+
//#endregion
|
|
624
|
+
export { OllamaProvider };
|
|
625
|
+
|
|
626
|
+
//# sourceMappingURL=ollama-D4ksOTO8.js.map
|