vue-llm 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/providers.js CHANGED
@@ -1,176 +1,111 @@
1
- import { r as T, i as _, d as b } from "./file-x_kW7BHa.js";
2
- import { c as v } from "./errors-B3A_zmVE.js";
3
- class k extends TransformStream {
4
- constructor() {
5
- let e = "";
6
- super({
7
- transform(n, a) {
8
- e += n;
9
- const i = e.split(`
10
- `);
11
- e = i.pop() ?? "";
12
- let t = {};
13
- for (const o of i) {
14
- if (o === "") {
15
- t.data !== void 0 && a.enqueue({
16
- event: t.event,
17
- data: t.data,
18
- id: t.id,
19
- retry: t.retry
20
- }), t = {};
21
- continue;
22
- }
23
- if (o.startsWith(":")) continue;
24
- const u = o.indexOf(":");
25
- if (u === -1) continue;
26
- const f = o.slice(0, u), d = o.slice(u + 1).replace(/^ /, "");
27
- switch (f) {
28
- case "event":
29
- t.event = d;
30
- break;
31
- case "data":
32
- t.data = t.data !== void 0 ? t.data + `
33
- ` + d : d;
34
- break;
35
- case "id":
36
- t.id = d;
37
- break;
38
- case "retry":
39
- t.retry = parseInt(d, 10);
40
- break;
41
- }
42
- }
43
- },
44
- flush(n) {
45
- if (e.trim()) {
46
- const a = e.split(`
47
- `);
48
- let i = {};
49
- for (const t of a) {
50
- if (t === "" && i.data !== void 0) {
51
- n.enqueue({
52
- event: i.event,
53
- data: i.data,
54
- id: i.id,
55
- retry: i.retry
56
- }), i = {};
57
- continue;
58
- }
59
- const o = t.indexOf(":");
60
- if (o === -1 || t.startsWith(":")) continue;
61
- const u = t.slice(0, o), f = t.slice(o + 1).replace(/^ /, "");
62
- u === "data" && (i.data = i.data !== void 0 ? i.data + `
63
- ` + f : f);
64
- }
65
- }
66
- }
67
- });
68
- }
69
- }
70
- function J(s) {
1
+ import { S as k } from "./sse-DU5m1n6r.js";
2
+ import { c as L } from "./sse-DU5m1n6r.js";
3
+ import { r as T, i as b, d as v } from "./file-x_kW7BHa.js";
4
+ import { c as _ } from "./errors-B3A_zmVE.js";
5
+ function U(o) {
71
6
  return {
72
7
  id: "openai",
73
8
  supportsStreaming: !0,
74
9
  validateConfig() {
75
- if (!s.apiKey)
10
+ if (!o.apiKey)
76
11
  throw new Error('[vue-llm] OpenAI API key is required. Pass it via createOpenAI({ apiKey: "..." })');
77
- if (!s.dangerouslyAllowBrowser && typeof window < "u")
12
+ if (!o.dangerouslyAllowBrowser && typeof window < "u")
78
13
  throw new Error(
79
14
  "[vue-llm] API keys must not be used in the browser. Use a server proxy (/api/chat) to keep keys safe. If you understand the risk, set dangerouslyAllowBrowser: true."
80
15
  );
81
16
  },
82
17
  async chat(e) {
83
- const n = s.baseURL ?? "https://api.openai.com/v1/chat/completions", a = {
18
+ const t = o.baseURL ?? "https://api.openai.com/v1/chat/completions", a = {
84
19
  model: e.model,
85
20
  messages: x(e.messages),
86
21
  stream: !0,
87
22
  stream_options: { include_usage: !0 }
88
23
  };
89
- e.providerOptions?.response_format && (a.response_format = e.providerOptions.response_format), e.tools?.length && (a.tools = e.tools.map((f) => ({
24
+ e.providerOptions?.response_format && (a.response_format = e.providerOptions.response_format), e.tools?.length && (a.tools = e.tools.map((m) => ({
90
25
  type: "function",
91
26
  function: {
92
- name: f.name,
93
- description: f.description,
94
- parameters: f.parameters
27
+ name: m.name,
28
+ description: m.description,
29
+ parameters: m.parameters
95
30
  }
96
31
  })));
97
- const i = await fetch(n, {
32
+ const c = await fetch(t, {
98
33
  method: "POST",
99
34
  headers: {
100
- Authorization: `Bearer ${s.apiKey}`,
35
+ Authorization: `Bearer ${o.apiKey}`,
101
36
  "Content-Type": "application/json",
102
37
  ...e.providerOptions?.headers
103
38
  },
104
39
  body: JSON.stringify(a),
105
40
  signal: e.signal
106
41
  });
107
- if (!i.ok)
108
- throw await v("openai", "OpenAI API", i);
109
- if (!i.body)
42
+ if (!c.ok)
43
+ throw await _("openai", "OpenAI API", c);
44
+ if (!c.body)
110
45
  throw new Error("[vue-llm] No response body from OpenAI");
111
- let t;
112
- const o = new Promise((f) => {
113
- t = f;
46
+ let r;
47
+ const n = new Promise((m) => {
48
+ r = m;
114
49
  });
115
- return { stream: i.body.pipeThrough(new TextDecoderStream()).pipeThrough(new k()).pipeThrough(new TransformStream({
116
- transform(f, d) {
117
- if (f.data === "[DONE]") return;
118
- let y;
50
+ return { stream: c.body.pipeThrough(new TextDecoderStream()).pipeThrough(new k()).pipeThrough(new TransformStream({
51
+ transform(m, y) {
52
+ if (m.data === "[DONE]") return;
53
+ let d;
119
54
  try {
120
- y = JSON.parse(f.data);
55
+ d = JSON.parse(m.data);
121
56
  } catch {
122
57
  return;
123
58
  }
124
- const c = y.choices?.[0];
125
- if (!c) {
126
- y.usage && t({
59
+ const p = d.choices?.[0];
60
+ if (!p) {
61
+ d.usage && r({
127
62
  finishReason: "stop",
128
63
  usage: {
129
- promptTokens: y.usage.prompt_tokens,
130
- completionTokens: y.usage.completion_tokens,
131
- totalTokens: y.usage.total_tokens
64
+ promptTokens: d.usage.prompt_tokens,
65
+ completionTokens: d.usage.completion_tokens,
66
+ totalTokens: d.usage.total_tokens
132
67
  }
133
68
  });
134
69
  return;
135
70
  }
136
- const l = c.delta;
137
- if (l.content && d.enqueue({ type: "text-delta", text: l.content }), l.tool_calls)
138
- for (const p of l.tool_calls)
139
- p.id && d.enqueue({
71
+ const i = p.delta;
72
+ if (i.content && y.enqueue({ type: "text-delta", text: i.content }), i.tool_calls)
73
+ for (const l of i.tool_calls)
74
+ l.id && y.enqueue({
140
75
  type: "tool-call-start",
141
- toolCallId: p.id,
142
- toolName: p.function?.name ?? ""
143
- }), p.function?.arguments && d.enqueue({
76
+ toolCallId: l.id,
77
+ toolName: l.function?.name ?? ""
78
+ }), l.function?.arguments && y.enqueue({
144
79
  type: "tool-call-delta",
145
- toolCallId: p.id ?? "",
146
- args: p.function.arguments
80
+ toolCallId: l.id ?? "",
81
+ args: l.function.arguments
147
82
  });
148
- if (c.finish_reason) {
149
- const p = l.tool_calls?.filter((r) => r.id) ?? [];
150
- for (const r of p)
151
- r.id && d.enqueue({ type: "tool-call-end", toolCallId: r.id });
152
- const m = c.finish_reason === "tool_calls" ? "tool-calls" : c.finish_reason === "length" ? "length" : "stop";
153
- d.enqueue({ type: "finish", finishReason: m, usage: void 0 });
83
+ if (p.finish_reason) {
84
+ const l = i.tool_calls?.filter((s) => s.id) ?? [];
85
+ for (const s of l)
86
+ s.id && y.enqueue({ type: "tool-call-end", toolCallId: s.id });
87
+ const u = p.finish_reason === "tool_calls" ? "tool-calls" : p.finish_reason === "length" ? "length" : "stop";
88
+ y.enqueue({ type: "finish", finishReason: u, usage: void 0 });
154
89
  }
155
90
  }
156
- })), response: o };
91
+ })), response: n };
157
92
  }
158
93
  };
159
94
  }
160
- function x(s) {
161
- return s.map((e) => e.role === "tool" ? {
95
+ function x(o) {
96
+ return o.map((e) => e.role === "tool" ? {
162
97
  role: "tool",
163
98
  content: typeof e.content == "string" ? e.content : JSON.stringify(e.content),
164
99
  tool_call_id: e.toolCallId
165
100
  } : e.role === "assistant" && e.toolCalls?.length ? {
166
101
  role: "assistant",
167
102
  content: typeof e.content == "string" && e.content ? e.content : null,
168
- tool_calls: e.toolCalls.map((n) => ({
169
- id: n.id,
103
+ tool_calls: e.toolCalls.map((t) => ({
104
+ id: t.id,
170
105
  type: "function",
171
106
  function: {
172
- name: n.name,
173
- arguments: typeof n.args == "string" ? n.args : JSON.stringify(n.args)
107
+ name: t.name,
108
+ arguments: typeof t.args == "string" ? t.args : JSON.stringify(t.args)
174
109
  }
175
110
  }))
176
111
  } : {
@@ -178,9 +113,9 @@ function x(s) {
178
113
  content: typeof e.content == "string" ? e.content : e.content.map(C)
179
114
  });
180
115
  }
181
- function C(s) {
182
- if (s.type === "text") return { type: "text", text: s.text };
183
- const e = T(s);
116
+ function C(o) {
117
+ if (o.type === "text") return { type: "text", text: o.text };
118
+ const e = T(o);
184
119
  if (e.mediaType.startsWith("image/"))
185
120
  return { type: "image_url", image_url: { url: e.url } };
186
121
  if (!e.base64)
@@ -190,11 +125,11 @@ function C(s) {
190
125
  type: "file",
191
126
  file: { filename: e.filename ?? "document.pdf", file_data: e.url }
192
127
  };
193
- const n = O[e.mediaType];
194
- if (n)
195
- return { type: "input_audio", input_audio: { data: e.base64, format: n } };
196
- if (_(e.mediaType))
197
- return { type: "text", text: b(e.base64) };
128
+ const t = O[e.mediaType];
129
+ if (t)
130
+ return { type: "input_audio", input_audio: { data: e.base64, format: t } };
131
+ if (b(e.mediaType))
132
+ return { type: "text", text: v(e.base64) };
198
133
  throw new Error(`[vue-llm] OpenAI does not support ${e.mediaType} attachments`);
199
134
  }
200
135
  const O = {
@@ -203,106 +138,106 @@ const O = {
203
138
  "audio/mpeg": "mp3",
204
139
  "audio/mp3": "mp3"
205
140
  };
206
- function U(s) {
141
+ function M(o) {
207
142
  return {
208
143
  id: "anthropic",
209
144
  supportsStreaming: !0,
210
145
  validateConfig() {
211
- if (!s.apiKey)
146
+ if (!o.apiKey)
212
147
  throw new Error('[vue-llm] Anthropic API key is required. Pass it via createAnthropic({ apiKey: "..." })');
213
- if (!s.dangerouslyAllowBrowser && typeof window < "u")
148
+ if (!o.dangerouslyAllowBrowser && typeof window < "u")
214
149
  throw new Error(
215
150
  "[vue-llm] API keys must not be used in the browser. Use a server proxy (/api/chat) to keep keys safe. If you understand the risk, set dangerouslyAllowBrowser: true."
216
151
  );
217
152
  },
218
153
  async chat(e) {
219
- const n = (s.baseURL ?? "https://api.anthropic.com") + "/v1/messages", a = e.messages.find((c) => c.role === "system"), i = e.messages.filter((c) => c.role !== "system"), t = {
154
+ const t = (o.baseURL ?? "https://api.anthropic.com") + "/v1/messages", a = e.messages.find((p) => p.role === "system"), c = e.messages.filter((p) => p.role !== "system"), r = {
220
155
  model: e.model,
221
- messages: I(i),
156
+ messages: I(c),
222
157
  max_tokens: e.providerOptions?.maxTokens ?? 4096,
223
158
  stream: !0
224
159
  };
225
- a && (t.system = typeof a.content == "string" ? a.content : a.content.map((c) => ({ type: "text", text: c.type === "text" ? c.text : "" }))), e.tools?.length && (t.tools = e.tools.map((c) => ({
226
- name: c.name,
227
- description: c.description,
228
- input_schema: c.parameters
160
+ a && (r.system = typeof a.content == "string" ? a.content : a.content.map((p) => ({ type: "text", text: p.type === "text" ? p.text : "" }))), e.tools?.length && (r.tools = e.tools.map((p) => ({
161
+ name: p.name,
162
+ description: p.description,
163
+ input_schema: p.parameters
229
164
  })));
230
- const o = await fetch(n, {
165
+ const n = await fetch(t, {
231
166
  method: "POST",
232
167
  headers: {
233
- "x-api-key": s.apiKey,
168
+ "x-api-key": o.apiKey,
234
169
  "anthropic-version": "2023-06-01",
235
170
  "Content-Type": "application/json",
236
171
  ...e.providerOptions?.headers
237
172
  },
238
- body: JSON.stringify(t),
173
+ body: JSON.stringify(r),
239
174
  signal: e.signal
240
175
  });
241
- if (!o.ok)
242
- throw await v("anthropic", "Anthropic API", o);
243
- if (!o.body)
176
+ if (!n.ok)
177
+ throw await _("anthropic", "Anthropic API", n);
178
+ if (!n.body)
244
179
  throw new Error("[vue-llm] No response body from Anthropic");
245
- let u;
246
- const f = new Promise((c) => {
247
- u = c;
180
+ let f;
181
+ const m = new Promise((p) => {
182
+ f = p;
248
183
  });
249
- let d = "";
250
- return { stream: o.body.pipeThrough(new TextDecoderStream()).pipeThrough(new k()).pipeThrough(new TransformStream({
251
- transform(c, l) {
252
- let p;
184
+ let y = "";
185
+ return { stream: n.body.pipeThrough(new TextDecoderStream()).pipeThrough(new k()).pipeThrough(new TransformStream({
186
+ transform(p, i) {
187
+ let l;
253
188
  try {
254
- p = JSON.parse(c.data);
189
+ l = JSON.parse(p.data);
255
190
  } catch {
256
191
  return;
257
192
  }
258
- switch (c.event ?? p.type) {
193
+ switch (p.event ?? l.type) {
259
194
  case "content_block_start": {
260
- const r = p.content_block;
261
- r?.type === "tool_use" && (d = r.id, l.enqueue({
195
+ const s = l.content_block;
196
+ s?.type === "tool_use" && (y = s.id, i.enqueue({
262
197
  type: "tool-call-start",
263
- toolCallId: d,
264
- toolName: r.name
198
+ toolCallId: y,
199
+ toolName: s.name
265
200
  }));
266
201
  break;
267
202
  }
268
203
  case "content_block_delta": {
269
- const r = p.delta;
270
- r?.type === "text_delta" && r.text && l.enqueue({ type: "text-delta", text: r.text }), r?.type === "thinking_delta" && r.text && l.enqueue({ type: "reasoning-delta", text: r.text }), r?.type === "input_json_delta" && r.partial_json && l.enqueue({
204
+ const s = l.delta;
205
+ s?.type === "text_delta" && s.text && i.enqueue({ type: "text-delta", text: s.text }), s?.type === "thinking_delta" && s.text && i.enqueue({ type: "reasoning-delta", text: s.text }), s?.type === "input_json_delta" && s.partial_json && i.enqueue({
271
206
  type: "tool-call-delta",
272
- toolCallId: d,
273
- args: r.partial_json
207
+ toolCallId: y,
208
+ args: s.partial_json
274
209
  });
275
210
  break;
276
211
  }
277
212
  case "content_block_stop": {
278
- d && (l.enqueue({ type: "tool-call-end", toolCallId: d }), d = "");
213
+ y && (i.enqueue({ type: "tool-call-end", toolCallId: y }), y = "");
279
214
  break;
280
215
  }
281
216
  case "message_delta": {
282
- const r = p.delta, w = p.usage, g = r?.stop_reason === "tool_use" ? "tool-calls" : r?.stop_reason === "max_tokens" ? "length" : "stop";
283
- l.enqueue({
217
+ const s = l.delta, w = l.usage, g = s?.stop_reason === "tool_use" ? "tool-calls" : s?.stop_reason === "max_tokens" ? "length" : "stop";
218
+ i.enqueue({
284
219
  type: "finish",
285
220
  finishReason: g,
286
221
  usage: w ? { promptTokens: 0, completionTokens: w.output_tokens ?? 0, totalTokens: w.output_tokens ?? 0 } : void 0
287
- }), u({ finishReason: g, usage: void 0 });
222
+ }), f({ finishReason: g, usage: void 0 });
288
223
  break;
289
224
  }
290
225
  case "error": {
291
- const r = p.error;
292
- l.enqueue({
226
+ const s = l.error;
227
+ i.enqueue({
293
228
  type: "error",
294
- error: new Error(`[vue-llm] Anthropic stream error: ${r?.message ?? "unknown"}`)
229
+ error: new Error(`[vue-llm] Anthropic stream error: ${s?.message ?? "unknown"}`)
295
230
  });
296
231
  break;
297
232
  }
298
233
  }
299
234
  }
300
- })), response: f };
235
+ })), response: m };
301
236
  }
302
237
  };
303
238
  }
304
- function I(s) {
305
- return s.map((e) => {
239
+ function I(o) {
240
+ return o.map((e) => {
306
241
  if (e.role === "tool")
307
242
  return {
308
243
  role: "user",
@@ -313,16 +248,16 @@ function I(s) {
313
248
  }]
314
249
  };
315
250
  if (e.role === "assistant" && e.toolCalls?.length) {
316
- const n = [];
317
- typeof e.content == "string" && e.content && n.push({ type: "text", text: e.content });
251
+ const t = [];
252
+ typeof e.content == "string" && e.content && t.push({ type: "text", text: e.content });
318
253
  for (const a of e.toolCalls)
319
- n.push({
254
+ t.push({
320
255
  type: "tool_use",
321
256
  id: a.id,
322
257
  name: a.name,
323
258
  input: a.args
324
259
  });
325
- return { role: "assistant", content: n };
260
+ return { role: "assistant", content: t };
326
261
  }
327
262
  return {
328
263
  role: e.role,
@@ -330,109 +265,109 @@ function I(s) {
330
265
  };
331
266
  });
332
267
  }
333
- function A(s) {
334
- if (s.type === "text") return { type: "text", text: s.text };
335
- const e = T(s), n = e.base64 ? { type: "base64", media_type: e.mediaType, data: e.base64 } : { type: "url", url: e.url };
268
+ function A(o) {
269
+ if (o.type === "text") return { type: "text", text: o.text };
270
+ const e = T(o), t = e.base64 ? { type: "base64", media_type: e.mediaType, data: e.base64 } : { type: "url", url: e.url };
336
271
  if (e.mediaType.startsWith("image/"))
337
- return { type: "image", source: n };
272
+ return { type: "image", source: t };
338
273
  if (e.mediaType === "application/pdf")
339
- return { type: "document", source: n, ...e.filename ? { title: e.filename } : {} };
340
- if (_(e.mediaType) && e.base64)
274
+ return { type: "document", source: t, ...e.filename ? { title: e.filename } : {} };
275
+ if (b(e.mediaType) && e.base64)
341
276
  return {
342
277
  type: "document",
343
- source: { type: "text", media_type: "text/plain", data: b(e.base64) },
278
+ source: { type: "text", media_type: "text/plain", data: v(e.base64) },
344
279
  ...e.filename ? { title: e.filename } : {}
345
280
  };
346
281
  throw new Error(`[vue-llm] Anthropic does not support ${e.mediaType} attachments`);
347
282
  }
348
- function M(s) {
283
+ function $(o) {
349
284
  return {
350
285
  id: "google",
351
286
  supportsStreaming: !0,
352
287
  validateConfig() {
353
- if (!s.apiKey)
288
+ if (!o.apiKey)
354
289
  throw new Error('[vue-llm] Google AI API key is required. Pass it via createGoogle({ apiKey: "..." })');
355
- if (!s.dangerouslyAllowBrowser && typeof window < "u")
290
+ if (!o.dangerouslyAllowBrowser && typeof window < "u")
356
291
  throw new Error(
357
292
  "[vue-llm] API keys must not be used in the browser. Use a server proxy to keep keys safe. If you understand the risk, set dangerouslyAllowBrowser: true."
358
293
  );
359
294
  },
360
295
  async chat(e) {
361
- const a = `${s.baseURL ?? "https://generativelanguage.googleapis.com/v1beta"}/models/${e.model}:streamGenerateContent?alt=sse&key=${s.apiKey}`, i = {
296
+ const a = `${o.baseURL ?? "https://generativelanguage.googleapis.com/v1beta"}/models/${e.model}:streamGenerateContent?alt=sse&key=${o.apiKey}`, c = {
362
297
  contents: S(e.messages)
363
- }, t = e.messages.find((l) => l.role === "system");
364
- t && (i.systemInstruction = {
365
- parts: [{ text: typeof t.content == "string" ? t.content : "" }]
366
- }), e.tools?.length && (i.tools = [{
367
- functionDeclarations: e.tools.map((l) => ({
368
- name: l.name,
369
- description: l.description,
370
- parameters: l.parameters
298
+ }, r = e.messages.find((i) => i.role === "system");
299
+ r && (c.systemInstruction = {
300
+ parts: [{ text: typeof r.content == "string" ? r.content : "" }]
301
+ }), e.tools?.length && (c.tools = [{
302
+ functionDeclarations: e.tools.map((i) => ({
303
+ name: i.name,
304
+ description: i.description,
305
+ parameters: i.parameters
371
306
  }))
372
- }]), i.generationConfig = {
307
+ }]), c.generationConfig = {
373
308
  ...e.providerOptions?.response_format?.type === "json_object" ? { responseMimeType: "application/json" } : {}
374
309
  };
375
- const o = await fetch(a, {
310
+ const n = await fetch(a, {
376
311
  method: "POST",
377
312
  headers: {
378
313
  "Content-Type": "application/json",
379
314
  ...e.providerOptions?.headers
380
315
  },
381
- body: JSON.stringify(i),
316
+ body: JSON.stringify(c),
382
317
  signal: e.signal
383
318
  });
384
- if (!o.ok)
385
- throw await v("google", "Google AI API", o);
386
- if (!o.body)
319
+ if (!n.ok)
320
+ throw await _("google", "Google AI API", n);
321
+ if (!n.body)
387
322
  throw new Error("[vue-llm] No response body from Google AI");
388
- let u;
389
- const f = new Promise((l) => {
390
- u = l;
323
+ let f;
324
+ const m = new Promise((i) => {
325
+ f = i;
391
326
  });
392
- let d = 0, y = !1;
393
- return { stream: o.body.pipeThrough(new TextDecoderStream()).pipeThrough(new TransformStream({
327
+ let y = 0, d = !1;
328
+ return { stream: n.body.pipeThrough(new TextDecoderStream()).pipeThrough(new TransformStream({
394
329
  buffer: "",
395
- transform(l, p) {
396
- this.buffer += l;
397
- const m = this.buffer.split(`
330
+ transform(i, l) {
331
+ this.buffer += i;
332
+ const u = this.buffer.split(`
398
333
  `);
399
- this.buffer = m.pop();
400
- for (const r of m)
401
- r.startsWith("data: ") && p.enqueue(r.slice(6));
334
+ this.buffer = u.pop();
335
+ for (const s of u)
336
+ s.startsWith("data: ") && l.enqueue(s.slice(6));
402
337
  },
403
- flush(l) {
404
- const p = this.buffer.trim();
405
- p.startsWith("data: ") && l.enqueue(p.slice(6));
338
+ flush(i) {
339
+ const l = this.buffer.trim();
340
+ l.startsWith("data: ") && i.enqueue(l.slice(6));
406
341
  }
407
342
  })).pipeThrough(new TransformStream({
408
- transform(l, p) {
409
- let m;
343
+ transform(i, l) {
344
+ let u;
410
345
  try {
411
- m = JSON.parse(l);
346
+ u = JSON.parse(i);
412
347
  } catch {
413
348
  return;
414
349
  }
415
- const r = m.candidates?.[0];
416
- if (!r) return;
417
- const w = r.content?.parts;
350
+ const s = u.candidates?.[0];
351
+ if (!s) return;
352
+ const w = s.content?.parts;
418
353
  if (w) {
419
354
  for (const g of w)
420
- if (g.text !== void 0 && p.enqueue({ type: "text-delta", text: g.text }), g.functionCall) {
421
- const h = `tc_gemini_${++d}`;
422
- y = !0, p.enqueue({
355
+ if (g.text !== void 0 && l.enqueue({ type: "text-delta", text: g.text }), g.functionCall) {
356
+ const h = `tc_gemini_${++y}`;
357
+ d = !0, l.enqueue({
423
358
  type: "tool-call-start",
424
359
  toolCallId: h,
425
360
  toolName: g.functionCall.name
426
- }), p.enqueue({
361
+ }), l.enqueue({
427
362
  type: "tool-call-delta",
428
363
  toolCallId: h,
429
364
  args: JSON.stringify(g.functionCall.args ?? {})
430
- }), p.enqueue({ type: "tool-call-end", toolCallId: h });
365
+ }), l.enqueue({ type: "tool-call-end", toolCallId: h });
431
366
  }
432
367
  }
433
- if (r.finishReason) {
434
- const g = y ? "tool-calls" : r.finishReason === "MAX_TOKENS" ? "length" : "stop", h = m.usageMetadata;
435
- p.enqueue({
368
+ if (s.finishReason) {
369
+ const g = d ? "tool-calls" : s.finishReason === "MAX_TOKENS" ? "length" : "stop", h = u.usageMetadata;
370
+ l.enqueue({
436
371
  type: "finish",
437
372
  finishReason: g,
438
373
  usage: h ? {
@@ -440,7 +375,7 @@ function M(s) {
440
375
  completionTokens: h.candidatesTokenCount ?? 0,
441
376
  totalTokens: h.totalTokenCount ?? 0
442
377
  } : void 0
443
- }), u({
378
+ }), f({
444
379
  finishReason: g,
445
380
  usage: h ? {
446
381
  promptTokens: h.promptTokenCount ?? 0,
@@ -450,77 +385,77 @@ function M(s) {
450
385
  });
451
386
  }
452
387
  }
453
- })), response: f };
388
+ })), response: m };
454
389
  }
455
390
  };
456
391
  }
457
- function S(s) {
458
- const e = [], n = /* @__PURE__ */ new Map();
459
- for (const a of s) {
392
+ function S(o) {
393
+ const e = [], t = /* @__PURE__ */ new Map();
394
+ for (const a of o) {
460
395
  if (a.role === "system") continue;
461
396
  if (a.role === "tool") {
462
- const o = {
397
+ const n = {
463
398
  functionResponse: {
464
- name: n.get(a.toolCallId ?? "") ?? a.toolCallId ?? "",
399
+ name: t.get(a.toolCallId ?? "") ?? a.toolCallId ?? "",
465
400
  response: { result: P(a.content) }
466
401
  }
467
- }, u = e.at(-1);
468
- u?.role === "user" && u.parts.every((f) => f.functionResponse) ? u.parts.push(o) : e.push({ role: "user", parts: [o] });
402
+ }, f = e.at(-1);
403
+ f?.role === "user" && f.parts.every((m) => m.functionResponse) ? f.parts.push(n) : e.push({ role: "user", parts: [n] });
469
404
  continue;
470
405
  }
471
- for (const o of a.toolCalls ?? [])
472
- n.set(o.id, o.name);
473
- const i = a.role === "assistant" ? "model" : "user", t = [];
406
+ for (const n of a.toolCalls ?? [])
407
+ t.set(n.id, n.name);
408
+ const c = a.role === "assistant" ? "model" : "user", r = [];
474
409
  if (typeof a.content == "string")
475
- a.content && t.push({ text: a.content });
410
+ a.content && r.push({ text: a.content });
476
411
  else if (Array.isArray(a.content)) {
477
- for (const o of a.content)
478
- if (o.type === "text" && t.push({ text: o.text }), o.type === "image" || o.type === "file") {
479
- const u = T(o);
480
- t.push(u.base64 ? { inlineData: { mimeType: u.mediaType, data: u.base64 } } : { fileData: { mimeType: u.mediaType, fileUri: u.url } });
412
+ for (const n of a.content)
413
+ if (n.type === "text" && r.push({ text: n.text }), n.type === "image" || n.type === "file") {
414
+ const f = T(n);
415
+ r.push(f.base64 ? { inlineData: { mimeType: f.mediaType, data: f.base64 } } : { fileData: { mimeType: f.mediaType, fileUri: f.url } });
481
416
  }
482
417
  }
483
418
  if (a.toolCalls?.length)
484
- for (const o of a.toolCalls)
485
- t.push({
419
+ for (const n of a.toolCalls)
420
+ r.push({
486
421
  functionCall: {
487
- name: o.name,
488
- args: typeof o.args == "string" ? JSON.parse(o.args) : o.args
422
+ name: n.name,
423
+ args: typeof n.args == "string" ? JSON.parse(n.args) : n.args
489
424
  }
490
425
  });
491
- t.length > 0 && e.push({ role: i, parts: t });
426
+ r.length > 0 && e.push({ role: c, parts: r });
492
427
  }
493
428
  return e;
494
429
  }
495
- function P(s) {
496
- if (typeof s != "string") return s;
430
+ function P(o) {
431
+ if (typeof o != "string") return o;
497
432
  try {
498
- return JSON.parse(s);
433
+ return JSON.parse(o);
499
434
  } catch {
500
- return s;
435
+ return o;
501
436
  }
502
437
  }
503
- function $(s) {
438
+ function j(o) {
504
439
  return {
505
440
  id: "ollama",
506
441
  supportsStreaming: !0,
507
442
  validateConfig() {
508
443
  },
509
444
  async chat(e) {
510
- const n = (s?.baseURL ?? "http://localhost:11434") + "/api/chat", a = {
445
+ const t = (o?.baseURL ?? "http://localhost:11434") + "/api/chat", a = {
511
446
  model: e.model,
512
447
  messages: N(e.messages),
513
448
  stream: !0
514
449
  };
515
- e.tools?.length && (a.tools = e.tools.map((y) => ({
450
+ e.tools?.length && (a.tools = e.tools.map((d) => ({
516
451
  type: "function",
517
452
  function: {
518
- name: y.name,
519
- description: y.description,
520
- parameters: y.parameters
453
+ name: d.name,
454
+ description: d.description,
455
+ parameters: d.parameters
521
456
  }
522
457
  })));
523
- const i = await fetch(n, {
458
+ const c = await fetch(t, {
524
459
  method: "POST",
525
460
  headers: {
526
461
  "Content-Type": "application/json",
@@ -529,112 +464,113 @@ function $(s) {
529
464
  body: JSON.stringify(a),
530
465
  signal: e.signal
531
466
  });
532
- if (!i.ok)
533
- throw await v("ollama", "Ollama API", i);
534
- if (!i.body)
467
+ if (!c.ok)
468
+ throw await _("ollama", "Ollama API", c);
469
+ if (!c.body)
535
470
  throw new Error("[vue-llm] No response body from Ollama");
536
- let t;
537
- const o = new Promise((y) => {
538
- t = y;
471
+ let r;
472
+ const n = new Promise((d) => {
473
+ r = d;
539
474
  });
540
- let u = "", f = !1;
541
- return { stream: i.body.pipeThrough(new TextDecoderStream()).pipeThrough(new TransformStream({
542
- transform(y, c) {
543
- u += y;
544
- const l = u.split(`
475
+ let f = "", m = !1;
476
+ return { stream: c.body.pipeThrough(new TextDecoderStream()).pipeThrough(new TransformStream({
477
+ transform(d, p) {
478
+ f += d;
479
+ const i = f.split(`
545
480
  `);
546
- u = l.pop() ?? "";
547
- for (const p of l) {
548
- if (!p.trim()) continue;
549
- let m;
481
+ f = i.pop() ?? "";
482
+ for (const l of i) {
483
+ if (!l.trim()) continue;
484
+ let u;
550
485
  try {
551
- m = JSON.parse(p);
486
+ u = JSON.parse(l);
552
487
  } catch {
553
488
  continue;
554
489
  }
555
- if (m.message?.content && c.enqueue({ type: "text-delta", text: m.message.content }), m.message?.tool_calls)
556
- for (const r of m.message.tool_calls) {
557
- f = !0;
490
+ if (u.message?.content && p.enqueue({ type: "text-delta", text: u.message.content }), u.message?.tool_calls)
491
+ for (const s of u.message.tool_calls) {
492
+ m = !0;
558
493
  const w = `ollama-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
559
- c.enqueue({
494
+ p.enqueue({
560
495
  type: "tool-call-start",
561
496
  toolCallId: w,
562
- toolName: r.function.name
563
- }), c.enqueue({
497
+ toolName: s.function.name
498
+ }), p.enqueue({
564
499
  type: "tool-call-delta",
565
500
  toolCallId: w,
566
- args: JSON.stringify(r.function.arguments)
567
- }), c.enqueue({ type: "tool-call-end", toolCallId: w });
501
+ args: JSON.stringify(s.function.arguments)
502
+ }), p.enqueue({ type: "tool-call-end", toolCallId: w });
568
503
  }
569
- if (m.done) {
570
- const r = f ? "tool-calls" : "stop";
571
- c.enqueue({ type: "finish", finishReason: r }), t({
572
- finishReason: r,
573
- usage: m.eval_count ? {
574
- promptTokens: m.prompt_eval_count ?? 0,
575
- completionTokens: m.eval_count,
576
- totalTokens: (m.prompt_eval_count ?? 0) + m.eval_count
504
+ if (u.done) {
505
+ const s = m ? "tool-calls" : "stop";
506
+ p.enqueue({ type: "finish", finishReason: s }), r({
507
+ finishReason: s,
508
+ usage: u.eval_count ? {
509
+ promptTokens: u.prompt_eval_count ?? 0,
510
+ completionTokens: u.eval_count,
511
+ totalTokens: (u.prompt_eval_count ?? 0) + u.eval_count
577
512
  } : void 0
578
513
  });
579
514
  }
580
515
  }
581
516
  }
582
- })), response: o };
517
+ })), response: n };
583
518
  }
584
519
  };
585
520
  }
586
- function N(s) {
521
+ function N(o) {
587
522
  const e = /* @__PURE__ */ new Map();
588
- return s.map((n) => {
589
- if (n.role === "tool")
523
+ return o.map((t) => {
524
+ if (t.role === "tool")
590
525
  return {
591
526
  role: "tool",
592
- content: typeof n.content == "string" ? n.content : JSON.stringify(n.content),
593
- tool_name: e.get(n.toolCallId ?? "")
527
+ content: typeof t.content == "string" ? t.content : JSON.stringify(t.content),
528
+ tool_name: e.get(t.toolCallId ?? "")
594
529
  };
595
- if (n.role === "assistant" && n.toolCalls?.length) {
596
- for (const t of n.toolCalls) e.set(t.id, t.name);
530
+ if (t.role === "assistant" && t.toolCalls?.length) {
531
+ for (const r of t.toolCalls) e.set(r.id, r.name);
597
532
  return {
598
533
  role: "assistant",
599
- content: typeof n.content == "string" ? n.content : "",
600
- tool_calls: n.toolCalls.map((t) => ({
534
+ content: typeof t.content == "string" ? t.content : "",
535
+ tool_calls: t.toolCalls.map((r) => ({
601
536
  function: {
602
- name: t.name,
603
- arguments: typeof t.args == "string" ? JSON.parse(t.args) : t.args
537
+ name: r.name,
538
+ arguments: typeof r.args == "string" ? JSON.parse(r.args) : r.args
604
539
  }
605
540
  }))
606
541
  };
607
542
  }
608
- if (typeof n.content == "string")
609
- return { role: n.role, content: n.content };
610
- const a = [], i = [];
611
- for (const t of n.content) {
612
- if (t.type === "text") {
613
- a.push(t.text);
543
+ if (typeof t.content == "string")
544
+ return { role: t.role, content: t.content };
545
+ const a = [], c = [];
546
+ for (const r of t.content) {
547
+ if (r.type === "text") {
548
+ a.push(r.text);
614
549
  continue;
615
550
  }
616
- const o = T(t);
617
- if (!o.base64)
551
+ const n = T(r);
552
+ if (!n.base64)
618
553
  throw new Error("[vue-llm] Ollama requires attachments as data URLs, got a remote URL");
619
- if (o.mediaType.startsWith("image/"))
620
- i.push(o.base64);
621
- else if (_(o.mediaType))
622
- a.push(b(o.base64));
554
+ if (n.mediaType.startsWith("image/"))
555
+ c.push(n.base64);
556
+ else if (b(n.mediaType))
557
+ a.push(v(n.base64));
623
558
  else
624
- throw new Error(`[vue-llm] Ollama does not support ${o.mediaType} attachments`);
559
+ throw new Error(`[vue-llm] Ollama does not support ${n.mediaType} attachments`);
625
560
  }
626
561
  return {
627
- role: n.role,
562
+ role: t.role,
628
563
  content: a.join(`
629
564
 
630
565
  `),
631
- ...i.length > 0 ? { images: i } : {}
566
+ ...c.length > 0 ? { images: c } : {}
632
567
  };
633
568
  });
634
569
  }
635
570
  export {
636
- U as createAnthropic,
637
- M as createGoogle,
638
- $ as createOllama,
639
- J as createOpenAI
571
+ M as createAnthropic,
572
+ $ as createGoogle,
573
+ j as createOllama,
574
+ U as createOpenAI,
575
+ L as createSSEProvider
640
576
  };