@stacksjs/ai 0.70.88 → 0.70.90
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/agents/claude/index.d.ts +29 -0
- package/dist/agents/claude/index.js +197 -0
- package/dist/agents/index.d.ts +6 -0
- package/dist/agents/index.js +1 -0
- package/dist/buddy.d.ts +67 -0
- package/dist/buddy.js +393 -0
- package/dist/drivers/anthropic/index.d.ts +40 -0
- package/dist/drivers/anthropic/index.js +276 -0
- package/dist/drivers/claude-agent-sdk/index.d.ts +40 -0
- package/dist/drivers/claude-agent-sdk/index.js +198 -0
- package/dist/drivers/index.d.ts +13 -0
- package/dist/drivers/index.js +4 -0
- package/dist/drivers/ollama/index.d.ts +93 -0
- package/dist/drivers/ollama/index.js +332 -0
- package/dist/drivers/openai/index.d.ts +66 -0
- package/dist/drivers/openai/index.js +351 -0
- package/dist/image.d.ts +83 -0
- package/dist/image.js +375 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +16 -0
- package/dist/mcp.d.ts +115 -0
- package/dist/mcp.js +361 -0
- package/dist/personalization.d.ts +118 -0
- package/dist/personalization.js +244 -0
- package/dist/search.d.ts +101 -0
- package/dist/search.js +316 -0
- package/dist/text.d.ts +10 -0
- package/dist/text.js +51 -0
- package/dist/types.d.ts +185 -0
- package/dist/types.js +0 -0
- package/dist/utils/client-bedrock-runtime.d.ts +16 -0
- package/dist/utils/client-bedrock-runtime.js +17 -0
- package/dist/utils/client-bedrock.d.ts +27 -0
- package/dist/utils/client-bedrock.js +20 -0
- package/dist/utils/model-access.d.ts +1 -0
- package/dist/utils/model-access.js +21 -0
- package/dist/utils/retry.d.ts +38 -0
- package/dist/utils/retry.js +39 -0
- package/dist/utils/tokens.d.ts +50 -0
- package/dist/utils/tokens.js +59 -0
- package/dist/utils/usage.d.ts +56 -0
- package/dist/utils/usage.js +27 -0
- package/dist/utils/vision.d.ts +22 -0
- package/dist/utils/vision.js +54 -0
- package/package.json +1 -1
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
const DEFAULT_HOST = "http://localhost:11434", DEFAULT_MODEL = "llama3.2", DEFAULT_EMBEDDING_MODEL = "nomic-embed-text";
|
|
2
|
+
let globalConfig = {};
|
|
3
|
+
export function configure(config) {
|
|
4
|
+
globalConfig = { ...globalConfig, ...config };
|
|
5
|
+
}
|
|
6
|
+
function getConfig(config) {
|
|
7
|
+
return {
|
|
8
|
+
host: config?.host || globalConfig.host || process.env.OLLAMA_HOST || DEFAULT_HOST,
|
|
9
|
+
model: config?.model || globalConfig.model || process.env.OLLAMA_MODEL || DEFAULT_MODEL,
|
|
10
|
+
embeddingModel: config?.embeddingModel || globalConfig.embeddingModel || DEFAULT_EMBEDDING_MODEL
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export function createOllamaDriver(config = {}) {
|
|
14
|
+
const {
|
|
15
|
+
host = process.env.OLLAMA_HOST || DEFAULT_HOST,
|
|
16
|
+
model = process.env.OLLAMA_MODEL || DEFAULT_MODEL,
|
|
17
|
+
embeddingModel = DEFAULT_EMBEDDING_MODEL
|
|
18
|
+
} = config;
|
|
19
|
+
return {
|
|
20
|
+
name: "Ollama",
|
|
21
|
+
async process(command, systemPrompt, history) {
|
|
22
|
+
const response = await fetch(`${host}/api/chat`, {
|
|
23
|
+
method: "POST",
|
|
24
|
+
headers: { "Content-Type": "application/json" },
|
|
25
|
+
body: JSON.stringify({
|
|
26
|
+
model,
|
|
27
|
+
messages: [
|
|
28
|
+
{ role: "system", content: systemPrompt },
|
|
29
|
+
...history,
|
|
30
|
+
{ role: "user", content: command }
|
|
31
|
+
],
|
|
32
|
+
stream: !1
|
|
33
|
+
})
|
|
34
|
+
});
|
|
35
|
+
if (!response.ok) {
|
|
36
|
+
const error = await response.text();
|
|
37
|
+
throw Error(`Ollama API error: ${error}`);
|
|
38
|
+
}
|
|
39
|
+
return (await response.json()).message.content;
|
|
40
|
+
},
|
|
41
|
+
async* stream(command, systemPrompt, history) {
|
|
42
|
+
const response = await fetch(`${host}/api/chat`, {
|
|
43
|
+
method: "POST",
|
|
44
|
+
headers: { "Content-Type": "application/json" },
|
|
45
|
+
body: JSON.stringify({
|
|
46
|
+
model,
|
|
47
|
+
messages: [
|
|
48
|
+
{ role: "system", content: systemPrompt },
|
|
49
|
+
...history,
|
|
50
|
+
{ role: "user", content: command }
|
|
51
|
+
],
|
|
52
|
+
stream: !0
|
|
53
|
+
})
|
|
54
|
+
});
|
|
55
|
+
if (!response.ok) {
|
|
56
|
+
const error = await response.text();
|
|
57
|
+
throw Error(`Ollama API error: ${error}`);
|
|
58
|
+
}
|
|
59
|
+
const reader = response.body?.getReader();
|
|
60
|
+
if (!reader)
|
|
61
|
+
throw Error("No response body");
|
|
62
|
+
const decoder = new TextDecoder;
|
|
63
|
+
let buffer = "";
|
|
64
|
+
while (!0) {
|
|
65
|
+
const { done, value } = await reader.read();
|
|
66
|
+
if (done)
|
|
67
|
+
break;
|
|
68
|
+
buffer += decoder.decode(value, { stream: !0 });
|
|
69
|
+
const lines = buffer.split(`
|
|
70
|
+
`);
|
|
71
|
+
buffer = lines.pop() || "";
|
|
72
|
+
for (const line of lines) {
|
|
73
|
+
if (!line.trim())
|
|
74
|
+
continue;
|
|
75
|
+
try {
|
|
76
|
+
const data = JSON.parse(line);
|
|
77
|
+
if (data.message?.content)
|
|
78
|
+
yield data.message.content;
|
|
79
|
+
} catch {}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
async embed(input) {
|
|
84
|
+
const inputs = Array.isArray(input) ? input : [input], embeddings = [];
|
|
85
|
+
for (const text of inputs) {
|
|
86
|
+
const response = await fetch(`${host}/api/embeddings`, {
|
|
87
|
+
method: "POST",
|
|
88
|
+
headers: { "Content-Type": "application/json" },
|
|
89
|
+
body: JSON.stringify({
|
|
90
|
+
model: embeddingModel,
|
|
91
|
+
prompt: text
|
|
92
|
+
})
|
|
93
|
+
});
|
|
94
|
+
if (!response.ok) {
|
|
95
|
+
const error = await response.text();
|
|
96
|
+
throw Error(`Ollama Embeddings API error: ${error}`);
|
|
97
|
+
}
|
|
98
|
+
const data = await response.json();
|
|
99
|
+
embeddings.push(data.embedding);
|
|
100
|
+
}
|
|
101
|
+
return Array.isArray(input) ? embeddings : embeddings[0];
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
export async function chat(messages, options = {}) {
|
|
106
|
+
const config = getConfig(), {
|
|
107
|
+
model = config.model,
|
|
108
|
+
temperature,
|
|
109
|
+
topP,
|
|
110
|
+
stop
|
|
111
|
+
} = options, response = await fetch(`${config.host}/api/chat`, {
|
|
112
|
+
method: "POST",
|
|
113
|
+
headers: { "Content-Type": "application/json" },
|
|
114
|
+
body: JSON.stringify({
|
|
115
|
+
model,
|
|
116
|
+
messages,
|
|
117
|
+
stream: !1,
|
|
118
|
+
options: {
|
|
119
|
+
temperature,
|
|
120
|
+
top_p: topP,
|
|
121
|
+
stop
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
});
|
|
125
|
+
if (!response.ok) {
|
|
126
|
+
const error = await response.text();
|
|
127
|
+
throw Error(`Ollama API error: ${error}`);
|
|
128
|
+
}
|
|
129
|
+
const data = await response.json();
|
|
130
|
+
return {
|
|
131
|
+
content: data.message.content,
|
|
132
|
+
model: data.model,
|
|
133
|
+
usage: {
|
|
134
|
+
promptTokens: data.prompt_eval_count || 0,
|
|
135
|
+
completionTokens: data.eval_count || 0,
|
|
136
|
+
totalTokens: (data.prompt_eval_count || 0) + (data.eval_count || 0)
|
|
137
|
+
},
|
|
138
|
+
finishReason: data.done_reason || "stop"
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
export async function* streamChat(messages, options = {}) {
|
|
142
|
+
const config = getConfig(), {
|
|
143
|
+
model = config.model,
|
|
144
|
+
temperature,
|
|
145
|
+
topP,
|
|
146
|
+
stop
|
|
147
|
+
} = options, response = await fetch(`${config.host}/api/chat`, {
|
|
148
|
+
method: "POST",
|
|
149
|
+
headers: { "Content-Type": "application/json" },
|
|
150
|
+
body: JSON.stringify({
|
|
151
|
+
model,
|
|
152
|
+
messages,
|
|
153
|
+
stream: !0,
|
|
154
|
+
options: {
|
|
155
|
+
temperature,
|
|
156
|
+
top_p: topP,
|
|
157
|
+
stop
|
|
158
|
+
}
|
|
159
|
+
})
|
|
160
|
+
});
|
|
161
|
+
if (!response.ok) {
|
|
162
|
+
const error = await response.text();
|
|
163
|
+
throw Error(`Ollama API error: ${error}`);
|
|
164
|
+
}
|
|
165
|
+
const reader = response.body?.getReader();
|
|
166
|
+
if (!reader)
|
|
167
|
+
throw Error("No response body");
|
|
168
|
+
const decoder = new TextDecoder;
|
|
169
|
+
let buffer = "";
|
|
170
|
+
while (!0) {
|
|
171
|
+
const { done, value } = await reader.read();
|
|
172
|
+
if (done)
|
|
173
|
+
break;
|
|
174
|
+
buffer += decoder.decode(value, { stream: !0 });
|
|
175
|
+
const lines = buffer.split(`
|
|
176
|
+
`);
|
|
177
|
+
buffer = lines.pop() || "";
|
|
178
|
+
for (const line of lines) {
|
|
179
|
+
if (!line.trim())
|
|
180
|
+
continue;
|
|
181
|
+
try {
|
|
182
|
+
const data = JSON.parse(line);
|
|
183
|
+
if (data.message?.content)
|
|
184
|
+
yield data.message.content;
|
|
185
|
+
} catch {}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
export async function generate(prompt, options = {}) {
|
|
190
|
+
const config = getConfig(), response = await fetch(`${config.host}/api/generate`, {
|
|
191
|
+
method: "POST",
|
|
192
|
+
headers: { "Content-Type": "application/json" },
|
|
193
|
+
body: JSON.stringify({
|
|
194
|
+
model: options.model || config.model,
|
|
195
|
+
prompt,
|
|
196
|
+
system: options.system,
|
|
197
|
+
template: options.template,
|
|
198
|
+
context: options.context,
|
|
199
|
+
raw: options.raw,
|
|
200
|
+
format: options.format,
|
|
201
|
+
images: options.images,
|
|
202
|
+
stream: !1
|
|
203
|
+
})
|
|
204
|
+
});
|
|
205
|
+
if (!response.ok) {
|
|
206
|
+
const error = await response.text();
|
|
207
|
+
throw Error(`Ollama API error: ${error}`);
|
|
208
|
+
}
|
|
209
|
+
const data = await response.json();
|
|
210
|
+
return {
|
|
211
|
+
content: data.response,
|
|
212
|
+
model: data.model,
|
|
213
|
+
usage: {
|
|
214
|
+
promptTokens: data.prompt_eval_count || 0,
|
|
215
|
+
completionTokens: data.eval_count || 0,
|
|
216
|
+
totalTokens: (data.prompt_eval_count || 0) + (data.eval_count || 0)
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
export async function embed(input, model) {
|
|
221
|
+
const config = getConfig(), embeddingModel = model || config.embeddingModel, inputs = Array.isArray(input) ? input : [input], embeddings = [];
|
|
222
|
+
for (const text of inputs) {
|
|
223
|
+
const response = await fetch(`${config.host}/api/embeddings`, {
|
|
224
|
+
method: "POST",
|
|
225
|
+
headers: { "Content-Type": "application/json" },
|
|
226
|
+
body: JSON.stringify({
|
|
227
|
+
model: embeddingModel,
|
|
228
|
+
prompt: text
|
|
229
|
+
})
|
|
230
|
+
});
|
|
231
|
+
if (!response.ok) {
|
|
232
|
+
const error = await response.text();
|
|
233
|
+
throw Error(`Ollama Embeddings API error: ${error}`);
|
|
234
|
+
}
|
|
235
|
+
const data = await response.json();
|
|
236
|
+
embeddings.push(data.embedding);
|
|
237
|
+
}
|
|
238
|
+
return Array.isArray(input) ? embeddings : embeddings[0];
|
|
239
|
+
}
|
|
240
|
+
export async function listModels() {
|
|
241
|
+
const config = getConfig(), response = await fetch(`${config.host}/api/tags`, {
|
|
242
|
+
method: "GET"
|
|
243
|
+
});
|
|
244
|
+
if (!response.ok) {
|
|
245
|
+
const error = await response.text();
|
|
246
|
+
throw Error(`Ollama API error: ${error}`);
|
|
247
|
+
}
|
|
248
|
+
return (await response.json()).models;
|
|
249
|
+
}
|
|
250
|
+
export async function pullModel(name, onProgress) {
|
|
251
|
+
const config = getConfig(), response = await fetch(`${config.host}/api/pull`, {
|
|
252
|
+
method: "POST",
|
|
253
|
+
headers: { "Content-Type": "application/json" },
|
|
254
|
+
body: JSON.stringify({ name, stream: !0 })
|
|
255
|
+
});
|
|
256
|
+
if (!response.ok) {
|
|
257
|
+
const error = await response.text();
|
|
258
|
+
throw Error(`Ollama API error: ${error}`);
|
|
259
|
+
}
|
|
260
|
+
const reader = response.body?.getReader();
|
|
261
|
+
if (!reader)
|
|
262
|
+
return;
|
|
263
|
+
const decoder = new TextDecoder;
|
|
264
|
+
let buffer = "";
|
|
265
|
+
while (!0) {
|
|
266
|
+
const { done, value } = await reader.read();
|
|
267
|
+
if (done)
|
|
268
|
+
break;
|
|
269
|
+
buffer += decoder.decode(value, { stream: !0 });
|
|
270
|
+
const lines = buffer.split(`
|
|
271
|
+
`);
|
|
272
|
+
buffer = lines.pop() || "";
|
|
273
|
+
for (const line of lines) {
|
|
274
|
+
if (!line.trim())
|
|
275
|
+
continue;
|
|
276
|
+
try {
|
|
277
|
+
const data = JSON.parse(line);
|
|
278
|
+
if (onProgress)
|
|
279
|
+
onProgress(data.status ?? "", data.completed, data.total);
|
|
280
|
+
} catch {}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
export async function deleteModel(name) {
|
|
285
|
+
const config = getConfig(), response = await fetch(`${config.host}/api/delete`, {
|
|
286
|
+
method: "DELETE",
|
|
287
|
+
headers: { "Content-Type": "application/json" },
|
|
288
|
+
body: JSON.stringify({ name })
|
|
289
|
+
});
|
|
290
|
+
if (!response.ok) {
|
|
291
|
+
const error = await response.text();
|
|
292
|
+
throw Error(`Ollama API error: ${error}`);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
export async function showModel(_name) {
|
|
296
|
+
const config = getConfig(), response = await fetch(`${config.host}/api/show`, {
|
|
297
|
+
method: "POST",
|
|
298
|
+
headers: { "Content-Type": "application/json" },
|
|
299
|
+
body: JSON.stringify({ name: _name })
|
|
300
|
+
});
|
|
301
|
+
if (!response.ok) {
|
|
302
|
+
const error = await response.text();
|
|
303
|
+
throw Error(`Ollama API error: ${error}`);
|
|
304
|
+
}
|
|
305
|
+
return response.json();
|
|
306
|
+
}
|
|
307
|
+
export async function isRunning() {
|
|
308
|
+
const config = getConfig();
|
|
309
|
+
try {
|
|
310
|
+
return (await fetch(`${config.host}/api/tags`, {
|
|
311
|
+
method: "GET"
|
|
312
|
+
})).ok;
|
|
313
|
+
} catch {
|
|
314
|
+
return !1;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
export const ollamaDriver = {
|
|
318
|
+
create: createOllamaDriver
|
|
319
|
+
}, ollama = {
|
|
320
|
+
configure,
|
|
321
|
+
chat,
|
|
322
|
+
streamChat,
|
|
323
|
+
generate,
|
|
324
|
+
embed,
|
|
325
|
+
listModels,
|
|
326
|
+
pullModel,
|
|
327
|
+
deleteModel,
|
|
328
|
+
showModel,
|
|
329
|
+
isRunning,
|
|
330
|
+
createDriver: createOllamaDriver
|
|
331
|
+
};
|
|
332
|
+
export default ollama;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { AIDriver, AIDriverConfig, AIMessage, AIResult, ChatCompletionOptions } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Configure OpenAI globally
|
|
4
|
+
*/
|
|
5
|
+
export declare function configure(config: OpenAIDriverConfig): void;
|
|
6
|
+
export declare function createOpenAIDriver(config: OpenAIDriverConfig): AIDriver;
|
|
7
|
+
/**
|
|
8
|
+
* Chat completion with full options
|
|
9
|
+
*/
|
|
10
|
+
export declare function chat(messages: AIMessage[], options?: ChatCompletionOptions): Promise<AIResult>;
|
|
11
|
+
/**
|
|
12
|
+
* Stream chat completion
|
|
13
|
+
*/
|
|
14
|
+
export declare function streamChat(messages: AIMessage[], options?: ChatCompletionOptions): AsyncGenerator<string>;
|
|
15
|
+
/**
|
|
16
|
+
* Create embeddings
|
|
17
|
+
*/
|
|
18
|
+
export declare function embed(input: string | string[], model?: unknown): Promise<number[] | number[][]>;
|
|
19
|
+
/**
|
|
20
|
+
* Generate images using DALL-E
|
|
21
|
+
*/
|
|
22
|
+
export declare function generateImage(prompt: string, options?: {
|
|
23
|
+
model?: 'dall-e-2' | 'dall-e-3'
|
|
24
|
+
size?: '256x256' | '512x512' | '1024x1024' | '1792x1024' | '1024x1792'
|
|
25
|
+
quality?: 'standard' | 'hd'
|
|
26
|
+
n?: number
|
|
27
|
+
responseFormat?: 'url' | 'b64_json'
|
|
28
|
+
}): Promise<{ url?: string, b64_json?: string }[]>;
|
|
29
|
+
/**
|
|
30
|
+
* Transcribe audio using Whisper
|
|
31
|
+
*/
|
|
32
|
+
export declare function transcribe(audioFile: Blob | File, options?: {
|
|
33
|
+
model?: 'whisper-1'
|
|
34
|
+
language?: string
|
|
35
|
+
prompt?: string
|
|
36
|
+
responseFormat?: 'json' | 'text' | 'srt' | 'verbose_json' | 'vtt'
|
|
37
|
+
temperature?: number
|
|
38
|
+
}): Promise<{ text: string }>;
|
|
39
|
+
/**
|
|
40
|
+
* Text-to-speech using OpenAI TTS
|
|
41
|
+
*/
|
|
42
|
+
export declare function textToSpeech(input: string, options?: {
|
|
43
|
+
model?: 'tts-1' | 'tts-1-hd'
|
|
44
|
+
voice?: 'alloy' | 'echo' | 'fable' | 'onyx' | 'nova' | 'shimmer'
|
|
45
|
+
responseFormat?: 'mp3' | 'opus' | 'aac' | 'flac' | 'wav' | 'pcm'
|
|
46
|
+
speed?: number
|
|
47
|
+
}): Promise<ArrayBuffer>;
|
|
48
|
+
export declare const openaiDriver: { create: typeof createOpenAIDriver };
|
|
49
|
+
export declare const openai: {
|
|
50
|
+
configure: typeof configure;
|
|
51
|
+
chat: typeof chat;
|
|
52
|
+
streamChat: typeof streamChat;
|
|
53
|
+
embed: typeof embed;
|
|
54
|
+
generateImage: typeof generateImage;
|
|
55
|
+
transcribe: typeof transcribe;
|
|
56
|
+
textToSpeech: typeof textToSpeech;
|
|
57
|
+
createDriver: unknown
|
|
58
|
+
};
|
|
59
|
+
export declare interface OpenAIDriverConfig extends AIDriverConfig {
|
|
60
|
+
apiKey: string
|
|
61
|
+
model?: string
|
|
62
|
+
maxTokens?: number
|
|
63
|
+
embeddingModel?: string
|
|
64
|
+
baseUrl?: string
|
|
65
|
+
}
|
|
66
|
+
export default openai;
|