@agentic-kit/ollama 1.0.0 → 1.2.0

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/dist/package.json DELETED
@@ -1,34 +0,0 @@
1
- {
2
- "name": "@agentic-kit/ollama",
3
- "version": "0.1.2",
4
- "author": "Dan Lynch <pyramation@gmail.com>",
5
- "description": "Ollama",
6
- "main": "index.js",
7
- "module": "esm/index.js",
8
- "types": "index.d.ts",
9
- "homepage": "https://github.com/constructive-io/agentic-kit",
10
- "license": "SEE LICENSE IN LICENSE",
11
- "publishConfig": {
12
- "access": "public",
13
- "directory": "dist"
14
- },
15
- "repository": {
16
- "type": "git",
17
- "url": "https://github.com/constructive-io/agentic-kit"
18
- },
19
- "bugs": {
20
- "url": "https://github.com/constructive-io/agentic-kit/issues"
21
- },
22
- "scripts": {
23
- "clean": "makage clean",
24
- "build": "makage build",
25
- "build:dev": "makage build --dev",
26
- "lint": "eslint . --fix",
27
- "test": "jest",
28
- "test:watch": "jest --watch"
29
- },
30
- "keywords": [],
31
- "dependencies": {
32
- "cross-fetch": "^4.1.0"
33
- }
34
- }
package/jest.config.js DELETED
@@ -1,19 +0,0 @@
1
- /** @type {import('ts-jest').JestConfigWithTsJest} */
2
- module.exports = {
3
- preset: 'ts-jest',
4
- testEnvironment: 'node',
5
- transform: {
6
- '^.+\\.tsx?$': [
7
- 'ts-jest',
8
- {
9
- babelConfig: false,
10
- tsconfig: 'tsconfig.json',
11
- },
12
- ],
13
- },
14
- transformIgnorePatterns: [`/node_modules/*`],
15
- testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
16
- moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
17
- modulePathIgnorePatterns: ['dist/*'],
18
- setupFilesAfterEnv: ['<rootDir>/jest.setup.js']
19
- };
package/jest.setup.js DELETED
@@ -1,4 +0,0 @@
1
- global.TextEncoder = require('util').TextEncoder;
2
- global.TextDecoder = require('util').TextDecoder;
3
-
4
- jest.mock('cross-fetch', () => jest.fn());
package/src/index.ts DELETED
@@ -1,224 +0,0 @@
1
- import fetch from 'cross-fetch';
2
-
3
- // ─── Types ────────────────────────────────────────────────────────────────────
4
-
5
- export interface ChatMessage {
6
- role: 'system' | 'user' | 'assistant';
7
- content: string;
8
- }
9
-
10
- export interface GenerateInput {
11
- model: string;
12
- /** Single-shot prompt — routes to /api/generate */
13
- prompt?: string;
14
- /** Multi-turn messages — routes to /api/chat (takes precedence over prompt) */
15
- messages?: ChatMessage[];
16
- /** System prompt — injected into /api/generate or prepended to /api/chat messages */
17
- system?: string;
18
- stream?: boolean;
19
- temperature?: number;
20
- maxTokens?: number;
21
- }
22
-
23
- interface OllamaGenerateResponse {
24
- model: string;
25
- created_at: string;
26
- response: string;
27
- done: boolean;
28
- }
29
-
30
- interface OllamaChatResponse {
31
- model: string;
32
- created_at: string;
33
- message: { role: string; content: string };
34
- done: boolean;
35
- }
36
-
37
- interface OllamaEmbeddingResponse {
38
- embedding: number[];
39
- }
40
-
41
- interface OllamaTagsResponse {
42
- models?: Array<{
43
- name: string;
44
- model: string;
45
- modified_at: string;
46
- size: number;
47
- digest: string;
48
- }>;
49
- tags?: string[];
50
- }
51
-
52
- // ─── Client ───────────────────────────────────────────────────────────────────
53
-
54
- export default class OllamaClient {
55
- private baseUrl: string;
56
-
57
- constructor(baseUrl: string = 'http://localhost:11434') {
58
- this.baseUrl = baseUrl;
59
- }
60
-
61
- // ── Models ──────────────────────────────────────────────────────────────────
62
-
63
- async listModels(): Promise<string[]> {
64
- const res = await fetch(`${this.baseUrl}/api/tags`);
65
- if (!res.ok) {
66
- throw new Error(`listModels failed: ${res.status} ${res.statusText}`);
67
- }
68
- const data: OllamaTagsResponse = await res.json();
69
- if (data.models?.length) return data.models.map((m) => m.name);
70
- if (data.tags?.length) return data.tags;
71
- return [];
72
- }
73
-
74
- async pullModel(model: string): Promise<void> {
75
- const res = await fetch(`${this.baseUrl}/api/pull`, {
76
- method: 'POST',
77
- headers: { 'Content-Type': 'application/json' },
78
- body: JSON.stringify({ name: model }),
79
- });
80
- if (!res.ok) {
81
- throw new Error(`pullModel failed: ${res.status} ${res.statusText}`);
82
- }
83
- }
84
-
85
- async deleteModel(model: string): Promise<void> {
86
- const res = await fetch(`${this.baseUrl}/api/delete`, {
87
- method: 'DELETE',
88
- headers: { 'Content-Type': 'application/json' },
89
- body: JSON.stringify({ name: model }),
90
- });
91
- if (!res.ok) {
92
- throw new Error(`deleteModel failed: ${res.status} ${res.statusText}`);
93
- }
94
- }
95
-
96
- // ── Generate (single-shot or multi-turn) ────────────────────────────────────
97
-
98
- async generate(input: GenerateInput): Promise<string>;
99
- async generate(input: GenerateInput, onChunk: (chunk: string) => void): Promise<void>;
100
- async generate(
101
- input: GenerateInput,
102
- onChunk?: (chunk: string) => void
103
- ): Promise<string | void> {
104
- if (input.messages) {
105
- return this._chat(input, onChunk);
106
- }
107
- return this._generate(input, onChunk);
108
- }
109
-
110
- // ── Embeddings ──────────────────────────────────────────────────────────────
111
-
112
- async generateEmbedding(text: string, model = 'nomic-embed-text'): Promise<number[]> {
113
- const res = await fetch(`${this.baseUrl}/api/embeddings`, {
114
- method: 'POST',
115
- headers: { 'Content-Type': 'application/json' },
116
- body: JSON.stringify({ model, prompt: text }),
117
- });
118
- if (!res.ok) {
119
- throw new Error(`generateEmbedding failed: ${res.status} ${res.statusText}`);
120
- }
121
- const data: OllamaEmbeddingResponse = await res.json();
122
- return data.embedding;
123
- }
124
-
125
- // ── Private: /api/generate ──────────────────────────────────────────────────
126
-
127
- private async _generate(
128
- input: GenerateInput,
129
- onChunk?: (chunk: string) => void
130
- ): Promise<string | void> {
131
- const body: Record<string, unknown> = {
132
- model: input.model,
133
- prompt: input.prompt ?? '',
134
- stream: !!onChunk,
135
- };
136
- if (input.system) body.system = input.system;
137
- if (input.temperature !== undefined) body.temperature = input.temperature;
138
-
139
- const res = await fetch(`${this.baseUrl}/api/generate`, {
140
- method: 'POST',
141
- headers: { 'Content-Type': 'application/json' },
142
- body: JSON.stringify(body),
143
- });
144
- if (!res.ok) {
145
- const text = await res.text().catch(() => '');
146
- throw new Error(`generate failed: ${res.status} ${res.statusText} — ${text}`);
147
- }
148
-
149
- if (onChunk) {
150
- return this._streamResponse(res, (data: OllamaGenerateResponse) => data.response, onChunk);
151
- }
152
-
153
- const data: OllamaGenerateResponse = await res.json();
154
- return data.response;
155
- }
156
-
157
- // ── Private: /api/chat ──────────────────────────────────────────────────────
158
-
159
- private async _chat(
160
- input: GenerateInput,
161
- onChunk?: (chunk: string) => void
162
- ): Promise<string | void> {
163
- const messages: ChatMessage[] = [];
164
- if (input.system) {
165
- messages.push({ role: 'system', content: input.system });
166
- }
167
- messages.push(...(input.messages ?? []));
168
-
169
- const body: Record<string, unknown> = {
170
- model: input.model,
171
- messages,
172
- stream: !!onChunk,
173
- };
174
- if (input.temperature !== undefined) body.temperature = input.temperature;
175
-
176
- const res = await fetch(`${this.baseUrl}/api/chat`, {
177
- method: 'POST',
178
- headers: { 'Content-Type': 'application/json' },
179
- body: JSON.stringify(body),
180
- });
181
- if (!res.ok) {
182
- const text = await res.text().catch(() => '');
183
- throw new Error(`chat failed: ${res.status} ${res.statusText} — ${text}`);
184
- }
185
-
186
- if (onChunk) {
187
- return this._streamResponse(
188
- res,
189
- (data: OllamaChatResponse) => data.message?.content ?? '',
190
- onChunk
191
- );
192
- }
193
-
194
- const data: OllamaChatResponse = await res.json();
195
- return data.message?.content ?? '';
196
- }
197
-
198
- // ── Private: streaming reader ───────────────────────────────────────────────
199
-
200
- private async _streamResponse<T>(
201
- res: Response,
202
- extract: (data: T) => string,
203
- onChunk: (chunk: string) => void
204
- ): Promise<void> {
205
- const reader = res.body?.getReader();
206
- if (!reader) throw new Error('No response body reader');
207
-
208
- const decoder = new TextDecoder();
209
- while (true) {
210
- const { done, value } = await reader.read();
211
- if (done) break;
212
- const lines = decoder.decode(value).split('\n').filter(Boolean);
213
- for (const line of lines) {
214
- try {
215
- const data: T = JSON.parse(line);
216
- const chunk = extract(data);
217
- if (chunk) onChunk(chunk);
218
- } catch {
219
- // partial line — skip
220
- }
221
- }
222
- }
223
- }
224
- }
package/tsconfig.esm.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "dist/esm",
5
- "module": "es2022",
6
- "rootDir": "src/",
7
- "declaration": false
8
- }
9
- }
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "dist",
5
- "rootDir": "src/"
6
- },
7
- "include": ["src/**/*.ts"],
8
- "exclude": ["dist", "node_modules", "**/*.spec.*", "**/*.test.*"]
9
- }