@agentic-kit/ollama 2.0.0 → 2.0.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.
Files changed (4) hide show
  1. package/esm/index.js +1 -105
  2. package/index.d.ts +1 -150
  3. package/index.js +16 -120
  4. package/package.json +5 -2
package/esm/index.js CHANGED
@@ -1,78 +1,5 @@
1
+ import { calculateUsageCost, clone, createAssistantMessage, DefaultAssistantMessageEventStream, } from '@agentic-kit/protocol';
1
2
  const fetch = globalThis.fetch.bind(globalThis);
2
- class EventStream {
3
- isTerminal;
4
- extractResult;
5
- queue = [];
6
- waiting = [];
7
- done = false;
8
- finalResultPromise;
9
- resolveFinalResult;
10
- constructor(isTerminal, extractResult) {
11
- this.isTerminal = isTerminal;
12
- this.extractResult = extractResult;
13
- this.finalResultPromise = new Promise((resolve) => {
14
- this.resolveFinalResult = resolve;
15
- });
16
- }
17
- push(event) {
18
- if (this.done) {
19
- return;
20
- }
21
- if (this.isTerminal(event)) {
22
- this.done = true;
23
- this.resolveFinalResult(this.extractResult(event));
24
- }
25
- const waiter = this.waiting.shift();
26
- if (waiter) {
27
- waiter({ value: event, done: false });
28
- return;
29
- }
30
- this.queue.push(event);
31
- }
32
- end(result) {
33
- this.done = true;
34
- if (result !== undefined) {
35
- this.resolveFinalResult(result);
36
- }
37
- while (this.waiting.length > 0) {
38
- this.waiting.shift()({ value: undefined, done: true });
39
- }
40
- }
41
- async *[Symbol.asyncIterator]() {
42
- while (true) {
43
- if (this.queue.length > 0) {
44
- yield this.queue.shift();
45
- continue;
46
- }
47
- if (this.done) {
48
- return;
49
- }
50
- const next = await new Promise((resolve) => {
51
- this.waiting.push(resolve);
52
- });
53
- if (next.done) {
54
- return;
55
- }
56
- yield next.value;
57
- }
58
- }
59
- result() {
60
- return this.finalResultPromise;
61
- }
62
- }
63
- class DefaultAssistantMessageEventStream extends EventStream {
64
- constructor() {
65
- super((event) => event.type === 'done' || event.type === 'error', (event) => {
66
- if (event.type === 'done') {
67
- return event.message;
68
- }
69
- if (event.type === 'error') {
70
- return event.error;
71
- }
72
- throw new Error('Unexpected terminal event');
73
- });
74
- }
75
- }
76
3
  export const OLLAMA_MODELS = [];
77
4
  export class OllamaClient {
78
5
  baseUrl;
@@ -405,34 +332,6 @@ function toOllamaMessages(context) {
405
332
  }
406
333
  return messages;
407
334
  }
408
- function createAssistantMessage(model) {
409
- return {
410
- role: 'assistant',
411
- api: model.api,
412
- provider: model.provider,
413
- model: model.id,
414
- content: [],
415
- usage: {
416
- input: 0,
417
- output: 0,
418
- reasoning: 0,
419
- cacheRead: 0,
420
- cacheWrite: 0,
421
- totalTokens: 0,
422
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
423
- },
424
- stopReason: 'stop',
425
- timestamp: Date.now(),
426
- };
427
- }
428
- function calculateUsageCost(model, usage) {
429
- usage.cost.input = ((model.cost?.input ?? 0) / 1_000_000) * usage.input;
430
- usage.cost.output = ((model.cost?.output ?? 0) / 1_000_000) * usage.output;
431
- usage.cost.cacheRead = ((model.cost?.cacheRead ?? 0) / 1_000_000) * usage.cacheRead;
432
- usage.cost.cacheWrite = ((model.cost?.cacheWrite ?? 0) / 1_000_000) * usage.cacheWrite;
433
- usage.cost.total =
434
- usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
435
- }
436
335
  function legacyInputToContext(input) {
437
336
  const messages = input.messages
438
337
  ? input.messages
@@ -467,9 +366,6 @@ function legacyInputToContext(input) {
467
366
  messages,
468
367
  };
469
368
  }
470
- function clone(value) {
471
- return JSON.parse(JSON.stringify(value));
472
- }
473
369
  async function* iterateResponseBody(body) {
474
370
  if ('getReader' in body && typeof body.getReader === 'function') {
475
371
  const reader = body.getReader();
package/index.d.ts CHANGED
@@ -1,153 +1,4 @@
1
- type JsonPrimitive = string | number | boolean | null;
2
- type JsonValue = JsonPrimitive | JsonObject | JsonValue[];
3
- interface JsonObject {
4
- [key: string]: JsonValue | undefined;
5
- }
6
- interface ModelDescriptor {
7
- id: string;
8
- name: string;
9
- api: string;
10
- provider: string;
11
- baseUrl: string;
12
- input: Array<'text' | 'image'>;
13
- reasoning: boolean;
14
- tools?: boolean;
15
- contextWindow?: number;
16
- maxOutputTokens?: number;
17
- cost?: {
18
- input?: number;
19
- output?: number;
20
- cacheRead?: number;
21
- cacheWrite?: number;
22
- };
23
- headers?: Record<string, string>;
24
- }
25
- interface TextContent {
26
- type: 'text';
27
- text: string;
28
- }
29
- interface ImageContent {
30
- type: 'image';
31
- data: string;
32
- mimeType: string;
33
- }
34
- interface ThinkingContent {
35
- type: 'thinking';
36
- thinking: string;
37
- }
38
- interface ToolCallContent {
39
- type: 'toolCall';
40
- id: string;
41
- name: string;
42
- arguments: Record<string, JsonValue | undefined>;
43
- }
44
- interface Usage {
45
- input: number;
46
- output: number;
47
- reasoning: number;
48
- cacheRead: number;
49
- cacheWrite: number;
50
- totalTokens: number;
51
- cost: {
52
- input: number;
53
- output: number;
54
- cacheRead: number;
55
- cacheWrite: number;
56
- total: number;
57
- };
58
- }
59
- type Message = {
60
- role: 'user';
61
- content: string | Array<TextContent | ImageContent>;
62
- timestamp: number;
63
- } | {
64
- role: 'assistant';
65
- content: Array<TextContent | ThinkingContent | ToolCallContent>;
66
- api: string;
67
- provider: string;
68
- model: string;
69
- usage: Usage;
70
- stopReason: 'stop' | 'length' | 'toolUse' | 'error' | 'aborted';
71
- errorMessage?: string;
72
- timestamp: number;
73
- } | {
74
- role: 'toolResult';
75
- toolCallId: string;
76
- toolName: string;
77
- content: Array<TextContent | ImageContent>;
78
- isError: boolean;
79
- details?: unknown;
80
- timestamp: number;
81
- };
82
- interface Context {
83
- systemPrompt?: string;
84
- messages: Message[];
85
- }
86
- interface StreamOptions {
87
- maxTokens?: number;
88
- signal?: AbortSignal;
89
- temperature?: number;
90
- }
91
- type AssistantMessage = Extract<Message, {
92
- role: 'assistant';
93
- }>;
94
- type AssistantMessageEvent = {
95
- type: 'start';
96
- partial: AssistantMessage;
97
- } | {
98
- type: 'text_start';
99
- contentIndex: number;
100
- partial: AssistantMessage;
101
- } | {
102
- type: 'text_delta';
103
- contentIndex: number;
104
- delta: string;
105
- partial: AssistantMessage;
106
- } | {
107
- type: 'text_end';
108
- contentIndex: number;
109
- content: string;
110
- partial: AssistantMessage;
111
- } | {
112
- type: 'thinking_start';
113
- contentIndex: number;
114
- partial: AssistantMessage;
115
- } | {
116
- type: 'thinking_delta';
117
- contentIndex: number;
118
- delta: string;
119
- partial: AssistantMessage;
120
- } | {
121
- type: 'thinking_end';
122
- contentIndex: number;
123
- content: string;
124
- partial: AssistantMessage;
125
- } | {
126
- type: 'toolcall_start';
127
- contentIndex: number;
128
- partial: AssistantMessage;
129
- } | {
130
- type: 'toolcall_delta';
131
- contentIndex: number;
132
- delta: string;
133
- partial: AssistantMessage;
134
- } | {
135
- type: 'toolcall_end';
136
- contentIndex: number;
137
- toolCall: ToolCallContent;
138
- partial: AssistantMessage;
139
- } | {
140
- type: 'done';
141
- reason: 'stop' | 'length' | 'toolUse';
142
- message: AssistantMessage;
143
- } | {
144
- type: 'error';
145
- reason: 'error' | 'aborted';
146
- error: AssistantMessage;
147
- };
148
- interface AssistantMessageEventStream extends AsyncIterable<AssistantMessageEvent> {
149
- result(): Promise<AssistantMessage>;
150
- }
1
+ import { type AssistantMessageEventStream, type Context, type ModelDescriptor, type StreamOptions } from '@agentic-kit/protocol';
151
2
  export interface ChatMessage {
152
3
  role: 'system' | 'user' | 'assistant';
153
4
  content: string;
package/index.js CHANGED
@@ -1,81 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OllamaAdapter = exports.OllamaClient = exports.OLLAMA_MODELS = void 0;
4
+ const protocol_1 = require("@agentic-kit/protocol");
4
5
  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
6
  exports.OLLAMA_MODELS = [];
80
7
  class OllamaClient {
81
8
  baseUrl;
@@ -200,8 +127,8 @@ class OllamaAdapter {
200
127
  return this.client.generateEmbedding(text, model);
201
128
  }
202
129
  stream(model, context, options) {
203
- const stream = new DefaultAssistantMessageEventStream();
204
- const output = createAssistantMessage(model);
130
+ const stream = new protocol_1.DefaultAssistantMessageEventStream();
131
+ const output = (0, protocol_1.createAssistantMessage)(model);
205
132
  void (async () => {
206
133
  const body = {
207
134
  model: model.id,
@@ -225,7 +152,7 @@ class OllamaAdapter {
225
152
  const text = await response.text().catch(() => '');
226
153
  throw new Error(`Ollama error ${response.status}: ${text}`);
227
154
  }
228
- stream.push({ type: 'start', partial: clone(output) });
155
+ stream.push({ type: 'start', partial: (0, protocol_1.clone)(output) });
229
156
  if (!response.body) {
230
157
  throw new Error('No response body');
231
158
  }
@@ -242,7 +169,7 @@ class OllamaAdapter {
242
169
  stream.push({
243
170
  type: 'thinking_start',
244
171
  contentIndex: thinkingIndex,
245
- partial: clone(output),
172
+ partial: (0, protocol_1.clone)(output),
246
173
  });
247
174
  }
248
175
  const block = output.content[thinkingIndex];
@@ -251,14 +178,14 @@ class OllamaAdapter {
251
178
  type: 'thinking_delta',
252
179
  contentIndex: thinkingIndex,
253
180
  delta: thinking,
254
- partial: clone(output),
181
+ partial: (0, protocol_1.clone)(output),
255
182
  });
256
183
  }
257
184
  const text = payload.message?.content ?? payload.response ?? '';
258
185
  if (text) {
259
186
  if (textIndex === undefined) {
260
187
  textIndex = output.content.push({ type: 'text', text: '' }) - 1;
261
- stream.push({ type: 'text_start', contentIndex: textIndex, partial: clone(output) });
188
+ stream.push({ type: 'text_start', contentIndex: textIndex, partial: (0, protocol_1.clone)(output) });
262
189
  }
263
190
  const block = output.content[textIndex];
264
191
  block.text += text;
@@ -266,7 +193,7 @@ class OllamaAdapter {
266
193
  type: 'text_delta',
267
194
  contentIndex: textIndex,
268
195
  delta: text,
269
- partial: clone(output),
196
+ partial: (0, protocol_1.clone)(output),
270
197
  });
271
198
  }
272
199
  if (!payload.done) {
@@ -275,14 +202,14 @@ class OllamaAdapter {
275
202
  output.usage.input = payload.prompt_eval_count ?? output.usage.input;
276
203
  output.usage.output = payload.eval_count ?? output.usage.output;
277
204
  output.usage.totalTokens = output.usage.input + output.usage.output;
278
- calculateUsageCost(model, output.usage);
205
+ (0, protocol_1.calculateUsageCost)(model, output.usage);
279
206
  output.stopReason = payload.done_reason === 'length' ? 'length' : 'stop';
280
207
  if (thinkingIndex !== undefined) {
281
208
  stream.push({
282
209
  type: 'thinking_end',
283
210
  contentIndex: thinkingIndex,
284
211
  content: output.content[thinkingIndex].thinking,
285
- partial: clone(output),
212
+ partial: (0, protocol_1.clone)(output),
286
213
  });
287
214
  }
288
215
  if (textIndex !== undefined) {
@@ -290,13 +217,13 @@ class OllamaAdapter {
290
217
  type: 'text_end',
291
218
  contentIndex: textIndex,
292
219
  content: output.content[textIndex].text,
293
- partial: clone(output),
220
+ partial: (0, protocol_1.clone)(output),
294
221
  });
295
222
  }
296
223
  stream.push({
297
224
  type: 'done',
298
225
  reason: output.stopReason === 'length' ? 'length' : 'stop',
299
- message: clone(output),
226
+ message: (0, protocol_1.clone)(output),
300
227
  });
301
228
  stream.end(output);
302
229
  finished = true;
@@ -329,7 +256,7 @@ class OllamaAdapter {
329
256
  type: 'thinking_end',
330
257
  contentIndex: thinkingIndex,
331
258
  content: output.content[thinkingIndex].thinking,
332
- partial: clone(output),
259
+ partial: (0, protocol_1.clone)(output),
333
260
  });
334
261
  }
335
262
  if (textIndex !== undefined) {
@@ -337,13 +264,13 @@ class OllamaAdapter {
337
264
  type: 'text_end',
338
265
  contentIndex: textIndex,
339
266
  content: output.content[textIndex].text,
340
- partial: clone(output),
267
+ partial: (0, protocol_1.clone)(output),
341
268
  });
342
269
  }
343
270
  stream.push({
344
271
  type: 'done',
345
272
  reason: output.stopReason === 'length' ? 'length' : 'stop',
346
- message: clone(output),
273
+ message: (0, protocol_1.clone)(output),
347
274
  });
348
275
  stream.end(output);
349
276
  }
@@ -353,7 +280,7 @@ class OllamaAdapter {
353
280
  stream.push({
354
281
  type: 'error',
355
282
  reason: output.stopReason === 'aborted' ? 'aborted' : 'error',
356
- error: clone(output),
283
+ error: (0, protocol_1.clone)(output),
357
284
  });
358
285
  stream.end(output);
359
286
  }
@@ -410,34 +337,6 @@ function toOllamaMessages(context) {
410
337
  }
411
338
  return messages;
412
339
  }
413
- function createAssistantMessage(model) {
414
- return {
415
- role: 'assistant',
416
- api: model.api,
417
- provider: model.provider,
418
- model: model.id,
419
- content: [],
420
- usage: {
421
- input: 0,
422
- output: 0,
423
- reasoning: 0,
424
- cacheRead: 0,
425
- cacheWrite: 0,
426
- totalTokens: 0,
427
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
428
- },
429
- stopReason: 'stop',
430
- timestamp: Date.now(),
431
- };
432
- }
433
- function calculateUsageCost(model, usage) {
434
- usage.cost.input = ((model.cost?.input ?? 0) / 1_000_000) * usage.input;
435
- usage.cost.output = ((model.cost?.output ?? 0) / 1_000_000) * usage.output;
436
- usage.cost.cacheRead = ((model.cost?.cacheRead ?? 0) / 1_000_000) * usage.cacheRead;
437
- usage.cost.cacheWrite = ((model.cost?.cacheWrite ?? 0) / 1_000_000) * usage.cacheWrite;
438
- usage.cost.total =
439
- usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
440
- }
441
340
  function legacyInputToContext(input) {
442
341
  const messages = input.messages
443
342
  ? input.messages
@@ -472,9 +371,6 @@ function legacyInputToContext(input) {
472
371
  messages,
473
372
  };
474
373
  }
475
- function clone(value) {
476
- return JSON.parse(JSON.stringify(value));
477
- }
478
374
  async function* iterateResponseBody(body) {
479
375
  if ('getReader' in body && typeof body.getReader === 'function') {
480
376
  const reader = body.getReader();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentic-kit/ollama",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "author": "Dan Lynch <pyramation@gmail.com>",
5
5
  "description": "Ollama",
6
6
  "main": "index.js",
@@ -39,6 +39,9 @@
39
39
  "test:live:extended": "node ./scripts/run-live-tests.js extended",
40
40
  "test:watch": "jest --watch"
41
41
  },
42
+ "dependencies": {
43
+ "@agentic-kit/protocol": "1.1.0"
44
+ },
42
45
  "keywords": [],
43
- "gitHead": "c2b13e65b26f14c9983e9b7cc4eafbe4919b13d4"
46
+ "gitHead": "fbf34d1146e8543664d904f75e5b3440f2b1d2a4"
44
47
  }