@agentic-kit/ollama 1.0.3 → 1.2.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/README.md +47 -12
- package/esm/index.js +448 -99
- package/esm/package.json +1 -0
- package/index.d.ts +169 -9
- package/index.js +449 -101
- package/package.json +13 -5
package/index.js
CHANGED
|
@@ -1,145 +1,493 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
exports.OllamaAdapter = exports.OllamaClient = exports.OLLAMA_MODELS = void 0;
|
|
4
|
+
const fetch = globalThis.fetch.bind(globalThis);
|
|
5
|
+
class EventStream {
|
|
6
|
+
isTerminal;
|
|
7
|
+
extractResult;
|
|
8
|
+
queue = [];
|
|
9
|
+
waiting = [];
|
|
10
|
+
done = false;
|
|
11
|
+
finalResultPromise;
|
|
12
|
+
resolveFinalResult;
|
|
13
|
+
constructor(isTerminal, extractResult) {
|
|
14
|
+
this.isTerminal = isTerminal;
|
|
15
|
+
this.extractResult = extractResult;
|
|
16
|
+
this.finalResultPromise = new Promise((resolve) => {
|
|
17
|
+
this.resolveFinalResult = resolve;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
push(event) {
|
|
21
|
+
if (this.done) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (this.isTerminal(event)) {
|
|
25
|
+
this.done = true;
|
|
26
|
+
this.resolveFinalResult(this.extractResult(event));
|
|
27
|
+
}
|
|
28
|
+
const waiter = this.waiting.shift();
|
|
29
|
+
if (waiter) {
|
|
30
|
+
waiter({ value: event, done: false });
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
this.queue.push(event);
|
|
34
|
+
}
|
|
35
|
+
end(result) {
|
|
36
|
+
this.done = true;
|
|
37
|
+
if (result !== undefined) {
|
|
38
|
+
this.resolveFinalResult(result);
|
|
39
|
+
}
|
|
40
|
+
while (this.waiting.length > 0) {
|
|
41
|
+
this.waiting.shift()({ value: undefined, done: true });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async *[Symbol.asyncIterator]() {
|
|
45
|
+
while (true) {
|
|
46
|
+
if (this.queue.length > 0) {
|
|
47
|
+
yield this.queue.shift();
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (this.done) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const next = await new Promise((resolve) => {
|
|
54
|
+
this.waiting.push(resolve);
|
|
55
|
+
});
|
|
56
|
+
if (next.done) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
yield next.value;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
result() {
|
|
63
|
+
return this.finalResultPromise;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
class DefaultAssistantMessageEventStream extends EventStream {
|
|
67
|
+
constructor() {
|
|
68
|
+
super((event) => event.type === 'done' || event.type === 'error', (event) => {
|
|
69
|
+
if (event.type === 'done') {
|
|
70
|
+
return event.message;
|
|
71
|
+
}
|
|
72
|
+
if (event.type === 'error') {
|
|
73
|
+
return event.error;
|
|
74
|
+
}
|
|
75
|
+
throw new Error('Unexpected terminal event');
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.OLLAMA_MODELS = [];
|
|
8
80
|
class OllamaClient {
|
|
9
81
|
baseUrl;
|
|
10
82
|
constructor(baseUrl = 'http://localhost:11434') {
|
|
11
83
|
this.baseUrl = baseUrl;
|
|
12
84
|
}
|
|
13
|
-
// ── Models ──────────────────────────────────────────────────────────────────
|
|
14
85
|
async listModels() {
|
|
15
|
-
const
|
|
16
|
-
if (!
|
|
17
|
-
throw new Error(`listModels failed: ${
|
|
86
|
+
const response = await fetch(`${this.baseUrl}/api/tags`);
|
|
87
|
+
if (!response.ok) {
|
|
88
|
+
throw new Error(`listModels failed: ${response.status} ${response.statusText}`);
|
|
18
89
|
}
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
90
|
+
const payload = (await response.json());
|
|
91
|
+
return payload.models?.map((model) => model.name) ?? [];
|
|
92
|
+
}
|
|
93
|
+
async showModel(model) {
|
|
94
|
+
const response = await fetch(`${this.baseUrl}/api/show`, {
|
|
95
|
+
method: 'POST',
|
|
96
|
+
headers: { 'Content-Type': 'application/json' },
|
|
97
|
+
body: JSON.stringify({ model }),
|
|
98
|
+
});
|
|
99
|
+
if (!response.ok)
|
|
100
|
+
return null;
|
|
101
|
+
return (await response.json());
|
|
25
102
|
}
|
|
26
103
|
async pullModel(model) {
|
|
27
|
-
const
|
|
104
|
+
const response = await fetch(`${this.baseUrl}/api/pull`, {
|
|
28
105
|
method: 'POST',
|
|
29
106
|
headers: { 'Content-Type': 'application/json' },
|
|
30
107
|
body: JSON.stringify({ name: model }),
|
|
31
108
|
});
|
|
32
|
-
if (!
|
|
33
|
-
throw new Error(`pullModel failed: ${
|
|
109
|
+
if (!response.ok) {
|
|
110
|
+
throw new Error(`pullModel failed: ${response.status} ${response.statusText}`);
|
|
34
111
|
}
|
|
35
112
|
}
|
|
36
113
|
async deleteModel(model) {
|
|
37
|
-
const
|
|
114
|
+
const response = await fetch(`${this.baseUrl}/api/delete`, {
|
|
38
115
|
method: 'DELETE',
|
|
39
116
|
headers: { 'Content-Type': 'application/json' },
|
|
40
117
|
body: JSON.stringify({ name: model }),
|
|
41
118
|
});
|
|
42
|
-
if (!
|
|
43
|
-
throw new Error(`deleteModel failed: ${
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
async generate(input, onChunk) {
|
|
47
|
-
if (input.messages) {
|
|
48
|
-
return this._chat(input, onChunk);
|
|
119
|
+
if (!response.ok) {
|
|
120
|
+
throw new Error(`deleteModel failed: ${response.status} ${response.statusText}`);
|
|
49
121
|
}
|
|
50
|
-
return this._generate(input, onChunk);
|
|
51
122
|
}
|
|
52
|
-
// ── Embeddings ──────────────────────────────────────────────────────────────
|
|
53
123
|
async generateEmbedding(text, model = 'nomic-embed-text') {
|
|
54
|
-
const
|
|
124
|
+
const response = await fetch(`${this.baseUrl}/api/embeddings`, {
|
|
55
125
|
method: 'POST',
|
|
56
126
|
headers: { 'Content-Type': 'application/json' },
|
|
57
127
|
body: JSON.stringify({ model, prompt: text }),
|
|
58
128
|
});
|
|
59
|
-
if (!
|
|
60
|
-
throw new Error(`generateEmbedding failed: ${
|
|
129
|
+
if (!response.ok) {
|
|
130
|
+
throw new Error(`generateEmbedding failed: ${response.status} ${response.statusText}`);
|
|
61
131
|
}
|
|
62
|
-
const
|
|
63
|
-
return
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
132
|
+
const payload = (await response.json());
|
|
133
|
+
return payload.embedding;
|
|
134
|
+
}
|
|
135
|
+
async generate(input, onChunk) {
|
|
136
|
+
const context = legacyInputToContext(input);
|
|
137
|
+
const model = {
|
|
138
|
+
id: input.model,
|
|
139
|
+
name: input.model,
|
|
140
|
+
api: 'ollama-native',
|
|
141
|
+
provider: 'ollama',
|
|
142
|
+
baseUrl: this.baseUrl,
|
|
143
|
+
input: ['text', 'image'],
|
|
144
|
+
reasoning: false,
|
|
145
|
+
tools: false,
|
|
146
|
+
maxOutputTokens: input.maxTokens,
|
|
71
147
|
};
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
method: 'POST',
|
|
78
|
-
headers: { 'Content-Type': 'application/json' },
|
|
79
|
-
body: JSON.stringify(body),
|
|
148
|
+
const adapter = new OllamaAdapter(this.baseUrl);
|
|
149
|
+
const response = adapter.stream(model, context, {
|
|
150
|
+
maxTokens: input.maxTokens,
|
|
151
|
+
signal: undefined,
|
|
152
|
+
temperature: input.temperature,
|
|
80
153
|
});
|
|
81
|
-
if (
|
|
82
|
-
|
|
83
|
-
|
|
154
|
+
if (onChunk || input.stream) {
|
|
155
|
+
for await (const event of response) {
|
|
156
|
+
if (event.type === 'text_delta') {
|
|
157
|
+
onChunk?.(event.delta);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return;
|
|
84
161
|
}
|
|
85
|
-
|
|
86
|
-
|
|
162
|
+
const message = await response.result();
|
|
163
|
+
return message.content
|
|
164
|
+
.filter((block) => block.type === 'text')
|
|
165
|
+
.map((block) => block.text)
|
|
166
|
+
.join('');
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
exports.OllamaClient = OllamaClient;
|
|
170
|
+
class OllamaAdapter {
|
|
171
|
+
api = 'ollama-native';
|
|
172
|
+
provider = 'ollama';
|
|
173
|
+
name = 'ollama';
|
|
174
|
+
client;
|
|
175
|
+
baseUrl;
|
|
176
|
+
constructor(baseUrl) {
|
|
177
|
+
this.baseUrl = baseUrl ?? 'http://localhost:11434';
|
|
178
|
+
this.client = new OllamaClient(this.baseUrl);
|
|
179
|
+
}
|
|
180
|
+
createModel(modelId, overrides) {
|
|
181
|
+
return {
|
|
182
|
+
id: modelId,
|
|
183
|
+
name: modelId,
|
|
184
|
+
api: this.api,
|
|
185
|
+
provider: this.provider,
|
|
186
|
+
baseUrl: this.baseUrl,
|
|
187
|
+
input: ['text', 'image'],
|
|
188
|
+
reasoning: false,
|
|
189
|
+
tools: false,
|
|
190
|
+
...overrides,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
async listModels() {
|
|
194
|
+
return this.client.listModels();
|
|
195
|
+
}
|
|
196
|
+
stream(model, context, options) {
|
|
197
|
+
const stream = new DefaultAssistantMessageEventStream();
|
|
198
|
+
const output = createAssistantMessage(model);
|
|
199
|
+
void (async () => {
|
|
200
|
+
const body = {
|
|
201
|
+
model: model.id,
|
|
202
|
+
stream: true,
|
|
203
|
+
messages: toOllamaMessages(context),
|
|
204
|
+
options: {
|
|
205
|
+
temperature: options?.temperature,
|
|
206
|
+
num_predict: options?.maxTokens,
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
if (model.reasoning)
|
|
210
|
+
body.think = true;
|
|
211
|
+
try {
|
|
212
|
+
const response = await fetch(`${model.baseUrl}/api/chat`, {
|
|
213
|
+
method: 'POST',
|
|
214
|
+
headers: { 'Content-Type': 'application/json' },
|
|
215
|
+
body: JSON.stringify(body),
|
|
216
|
+
signal: options?.signal,
|
|
217
|
+
});
|
|
218
|
+
if (!response.ok) {
|
|
219
|
+
const text = await response.text().catch(() => '');
|
|
220
|
+
throw new Error(`Ollama error ${response.status}: ${text}`);
|
|
221
|
+
}
|
|
222
|
+
stream.push({ type: 'start', partial: clone(output) });
|
|
223
|
+
if (!response.body) {
|
|
224
|
+
throw new Error('No response body');
|
|
225
|
+
}
|
|
226
|
+
const decoder = new TextDecoder();
|
|
227
|
+
let buffer = '';
|
|
228
|
+
let textIndex;
|
|
229
|
+
let thinkingIndex;
|
|
230
|
+
let finished = false;
|
|
231
|
+
const processPayload = (payload) => {
|
|
232
|
+
const thinking = payload.message?.thinking ?? '';
|
|
233
|
+
if (thinking) {
|
|
234
|
+
if (thinkingIndex === undefined) {
|
|
235
|
+
thinkingIndex = output.content.push({ type: 'thinking', thinking: '' }) - 1;
|
|
236
|
+
stream.push({
|
|
237
|
+
type: 'thinking_start',
|
|
238
|
+
contentIndex: thinkingIndex,
|
|
239
|
+
partial: clone(output),
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
const block = output.content[thinkingIndex];
|
|
243
|
+
block.thinking += thinking;
|
|
244
|
+
stream.push({
|
|
245
|
+
type: 'thinking_delta',
|
|
246
|
+
contentIndex: thinkingIndex,
|
|
247
|
+
delta: thinking,
|
|
248
|
+
partial: clone(output),
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
const text = payload.message?.content ?? payload.response ?? '';
|
|
252
|
+
if (text) {
|
|
253
|
+
if (textIndex === undefined) {
|
|
254
|
+
textIndex = output.content.push({ type: 'text', text: '' }) - 1;
|
|
255
|
+
stream.push({ type: 'text_start', contentIndex: textIndex, partial: clone(output) });
|
|
256
|
+
}
|
|
257
|
+
const block = output.content[textIndex];
|
|
258
|
+
block.text += text;
|
|
259
|
+
stream.push({
|
|
260
|
+
type: 'text_delta',
|
|
261
|
+
contentIndex: textIndex,
|
|
262
|
+
delta: text,
|
|
263
|
+
partial: clone(output),
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
if (!payload.done) {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
output.usage.input = payload.prompt_eval_count ?? output.usage.input;
|
|
270
|
+
output.usage.output = payload.eval_count ?? output.usage.output;
|
|
271
|
+
output.usage.totalTokens = output.usage.input + output.usage.output;
|
|
272
|
+
calculateUsageCost(model, output.usage);
|
|
273
|
+
output.stopReason = payload.done_reason === 'length' ? 'length' : 'stop';
|
|
274
|
+
if (thinkingIndex !== undefined) {
|
|
275
|
+
stream.push({
|
|
276
|
+
type: 'thinking_end',
|
|
277
|
+
contentIndex: thinkingIndex,
|
|
278
|
+
content: output.content[thinkingIndex].thinking,
|
|
279
|
+
partial: clone(output),
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
if (textIndex !== undefined) {
|
|
283
|
+
stream.push({
|
|
284
|
+
type: 'text_end',
|
|
285
|
+
contentIndex: textIndex,
|
|
286
|
+
content: output.content[textIndex].text,
|
|
287
|
+
partial: clone(output),
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
stream.push({
|
|
291
|
+
type: 'done',
|
|
292
|
+
reason: output.stopReason === 'length' ? 'length' : 'stop',
|
|
293
|
+
message: clone(output),
|
|
294
|
+
});
|
|
295
|
+
stream.end(output);
|
|
296
|
+
finished = true;
|
|
297
|
+
return true;
|
|
298
|
+
};
|
|
299
|
+
for await (const chunk of iterateResponseBody(response.body)) {
|
|
300
|
+
buffer += decoder.decode(chunk, { stream: true });
|
|
301
|
+
const lines = buffer.split('\n');
|
|
302
|
+
buffer = lines.pop() ?? '';
|
|
303
|
+
for (const line of lines) {
|
|
304
|
+
const trimmed = line.trim();
|
|
305
|
+
if (!trimmed) {
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
const payload = JSON.parse(trimmed);
|
|
309
|
+
if (processPayload(payload)) {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
const trailing = buffer.trim();
|
|
315
|
+
if (!finished && trailing) {
|
|
316
|
+
const payload = JSON.parse(trailing);
|
|
317
|
+
if (processPayload(payload)) {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (thinkingIndex !== undefined) {
|
|
322
|
+
stream.push({
|
|
323
|
+
type: 'thinking_end',
|
|
324
|
+
contentIndex: thinkingIndex,
|
|
325
|
+
content: output.content[thinkingIndex].thinking,
|
|
326
|
+
partial: clone(output),
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
if (textIndex !== undefined) {
|
|
330
|
+
stream.push({
|
|
331
|
+
type: 'text_end',
|
|
332
|
+
contentIndex: textIndex,
|
|
333
|
+
content: output.content[textIndex].text,
|
|
334
|
+
partial: clone(output),
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
stream.push({
|
|
338
|
+
type: 'done',
|
|
339
|
+
reason: output.stopReason === 'length' ? 'length' : 'stop',
|
|
340
|
+
message: clone(output),
|
|
341
|
+
});
|
|
342
|
+
stream.end(output);
|
|
343
|
+
}
|
|
344
|
+
catch (error) {
|
|
345
|
+
output.stopReason = options?.signal?.aborted ? 'aborted' : 'error';
|
|
346
|
+
output.errorMessage = error instanceof Error ? error.message : String(error);
|
|
347
|
+
stream.push({
|
|
348
|
+
type: 'error',
|
|
349
|
+
reason: output.stopReason === 'aborted' ? 'aborted' : 'error',
|
|
350
|
+
error: clone(output),
|
|
351
|
+
});
|
|
352
|
+
stream.end(output);
|
|
353
|
+
}
|
|
354
|
+
})();
|
|
355
|
+
return stream;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
exports.OllamaAdapter = OllamaAdapter;
|
|
359
|
+
function toOllamaMessages(context) {
|
|
360
|
+
const messages = context.messages.map((message) => {
|
|
361
|
+
if (message.role === 'user') {
|
|
362
|
+
if (typeof message.content === 'string') {
|
|
363
|
+
return { role: 'user', content: message.content };
|
|
364
|
+
}
|
|
365
|
+
return {
|
|
366
|
+
role: 'user',
|
|
367
|
+
content: message.content
|
|
368
|
+
.filter((block) => block.type === 'text')
|
|
369
|
+
.map((block) => block.text)
|
|
370
|
+
.join('\n'),
|
|
371
|
+
images: message.content
|
|
372
|
+
.filter((block) => block.type === 'image')
|
|
373
|
+
.map((block) => block.data),
|
|
374
|
+
};
|
|
87
375
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
messages.push({ role: 'system', content: input.system });
|
|
376
|
+
if (message.role === 'toolResult') {
|
|
377
|
+
return {
|
|
378
|
+
role: 'user',
|
|
379
|
+
content: message.content
|
|
380
|
+
.map((block) => block.type === 'text' ? block.text : `[image:${block.mimeType};bytes=${block.data.length}]`)
|
|
381
|
+
.join('\n'),
|
|
382
|
+
};
|
|
96
383
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
384
|
+
return {
|
|
385
|
+
role: 'assistant',
|
|
386
|
+
content: message.content
|
|
387
|
+
.map((block) => {
|
|
388
|
+
if (block.type === 'text') {
|
|
389
|
+
return block.text;
|
|
390
|
+
}
|
|
391
|
+
if (block.type === 'thinking') {
|
|
392
|
+
return `<thinking>${block.thinking}</thinking>`;
|
|
393
|
+
}
|
|
394
|
+
return `<tool-call name="${block.name}">${JSON.stringify(block.arguments)}</tool-call>`;
|
|
395
|
+
})
|
|
396
|
+
.join('\n'),
|
|
102
397
|
};
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
body: JSON.stringify(body),
|
|
398
|
+
});
|
|
399
|
+
if (context.systemPrompt) {
|
|
400
|
+
messages.unshift({
|
|
401
|
+
role: 'system',
|
|
402
|
+
content: context.systemPrompt,
|
|
109
403
|
});
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
404
|
+
}
|
|
405
|
+
return messages;
|
|
406
|
+
}
|
|
407
|
+
function createAssistantMessage(model) {
|
|
408
|
+
return {
|
|
409
|
+
role: 'assistant',
|
|
410
|
+
api: model.api,
|
|
411
|
+
provider: model.provider,
|
|
412
|
+
model: model.id,
|
|
413
|
+
content: [],
|
|
414
|
+
usage: {
|
|
415
|
+
input: 0,
|
|
416
|
+
output: 0,
|
|
417
|
+
reasoning: 0,
|
|
418
|
+
cacheRead: 0,
|
|
419
|
+
cacheWrite: 0,
|
|
420
|
+
totalTokens: 0,
|
|
421
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
422
|
+
},
|
|
423
|
+
stopReason: 'stop',
|
|
424
|
+
timestamp: Date.now(),
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
function calculateUsageCost(model, usage) {
|
|
428
|
+
usage.cost.input = ((model.cost?.input ?? 0) / 1_000_000) * usage.input;
|
|
429
|
+
usage.cost.output = ((model.cost?.output ?? 0) / 1_000_000) * usage.output;
|
|
430
|
+
usage.cost.cacheRead = ((model.cost?.cacheRead ?? 0) / 1_000_000) * usage.cacheRead;
|
|
431
|
+
usage.cost.cacheWrite = ((model.cost?.cacheWrite ?? 0) / 1_000_000) * usage.cacheWrite;
|
|
432
|
+
usage.cost.total =
|
|
433
|
+
usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
|
|
434
|
+
}
|
|
435
|
+
function legacyInputToContext(input) {
|
|
436
|
+
const messages = input.messages
|
|
437
|
+
? input.messages
|
|
438
|
+
.filter((message) => message.role !== 'system')
|
|
439
|
+
.map((message) => message.role === 'assistant'
|
|
440
|
+
? {
|
|
441
|
+
role: 'assistant',
|
|
442
|
+
api: 'ollama-native',
|
|
443
|
+
provider: 'ollama',
|
|
444
|
+
model: input.model,
|
|
445
|
+
content: [{ type: 'text', text: message.content }],
|
|
446
|
+
usage: {
|
|
447
|
+
input: 0,
|
|
448
|
+
output: 0,
|
|
449
|
+
reasoning: 0,
|
|
450
|
+
cacheRead: 0,
|
|
451
|
+
cacheWrite: 0,
|
|
452
|
+
totalTokens: 0,
|
|
453
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
454
|
+
},
|
|
455
|
+
stopReason: 'stop',
|
|
456
|
+
timestamp: Date.now(),
|
|
457
|
+
}
|
|
458
|
+
: {
|
|
459
|
+
role: 'user',
|
|
460
|
+
content: message.content,
|
|
461
|
+
timestamp: Date.now(),
|
|
462
|
+
})
|
|
463
|
+
: [{ role: 'user', content: input.prompt ?? '', timestamp: Date.now() }];
|
|
464
|
+
return {
|
|
465
|
+
systemPrompt: input.system ?? input.messages?.find((message) => message.role === 'system')?.content,
|
|
466
|
+
messages,
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
function clone(value) {
|
|
470
|
+
return JSON.parse(JSON.stringify(value));
|
|
471
|
+
}
|
|
472
|
+
async function* iterateResponseBody(body) {
|
|
473
|
+
if ('getReader' in body && typeof body.getReader === 'function') {
|
|
474
|
+
const reader = body.getReader();
|
|
126
475
|
while (true) {
|
|
127
476
|
const { done, value } = await reader.read();
|
|
128
|
-
if (done)
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const data = JSON.parse(line);
|
|
134
|
-
const chunk = extract(data);
|
|
135
|
-
if (chunk)
|
|
136
|
-
onChunk(chunk);
|
|
137
|
-
}
|
|
138
|
-
catch {
|
|
139
|
-
// partial line — skip
|
|
140
|
-
}
|
|
477
|
+
if (done) {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
if (value) {
|
|
481
|
+
yield value;
|
|
141
482
|
}
|
|
142
483
|
}
|
|
143
484
|
}
|
|
485
|
+
for await (const chunk of body) {
|
|
486
|
+
if (typeof chunk === 'string') {
|
|
487
|
+
yield new TextEncoder().encode(chunk);
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
yield chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk);
|
|
491
|
+
}
|
|
144
492
|
}
|
|
145
493
|
exports.default = OllamaClient;
|
package/package.json
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentic-kit/ollama",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
5
|
"description": "Ollama",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"module": "esm/index.js",
|
|
8
8
|
"types": "index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./index.d.ts",
|
|
12
|
+
"import": "./esm/index.js",
|
|
13
|
+
"require": "./index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
9
16
|
"homepage": "https://github.com/constructive-io/agentic-kit",
|
|
10
17
|
"license": "SEE LICENSE IN LICENSE",
|
|
11
18
|
"publishConfig": {
|
|
@@ -23,14 +30,15 @@
|
|
|
23
30
|
"clean": "makage clean",
|
|
24
31
|
"prepack": "npm run build",
|
|
25
32
|
"build": "makage build",
|
|
33
|
+
"postbuild": "node ../../scripts/write-esm-package-json.js",
|
|
26
34
|
"build:dev": "makage build --dev",
|
|
27
35
|
"lint": "eslint . --fix",
|
|
28
36
|
"test": "jest",
|
|
37
|
+
"test:live": "node ./scripts/run-live-tests.js smoke",
|
|
38
|
+
"test:live:smoke": "node ./scripts/run-live-tests.js smoke",
|
|
39
|
+
"test:live:extended": "node ./scripts/run-live-tests.js extended",
|
|
29
40
|
"test:watch": "jest --watch"
|
|
30
41
|
},
|
|
31
42
|
"keywords": [],
|
|
32
|
-
"
|
|
33
|
-
"cross-fetch": "^4.1.0"
|
|
34
|
-
},
|
|
35
|
-
"gitHead": "cbd1f7833d4a107f6fcc897ac7d0df9041cabbdd"
|
|
43
|
+
"gitHead": "af045fe54e55e593a3e4292335faa28fdb878619"
|
|
36
44
|
}
|