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/index.js CHANGED
@@ -1,35 +1,39 @@
1
- import { ref as x, watch as V, computed as A, onScopeDispose as L, toValue as N } from "vue";
2
- import { c as q } from "./file-x_kW7BHa.js";
3
- import { f as de } from "./file-x_kW7BHa.js";
4
- import { L as J } from "./errors-B3A_zmVE.js";
5
- class U {
6
- constructor(t, e, a) {
7
- this.messages = t, this.status = e, this.generateId = a;
1
+ import { unref as q, ref as x, watch as U, computed as A, getCurrentScope as L, onScopeDispose as D } from "vue";
2
+ import { c as _ } from "./file-x_kW7BHa.js";
3
+ import { f as me } from "./file-x_kW7BHa.js";
4
+ import { L as F } from "./errors-B3A_zmVE.js";
5
+ function N(t) {
6
+ return typeof t == "function" ? t() : q(t);
7
+ }
8
+ class k {
9
+ constructor(r, e, a) {
10
+ this.messages = r, this.status = e, this.generateId = a;
8
11
  }
9
12
  messages;
10
13
  status;
11
14
  generateId;
12
- async processStream(t, e, a) {
15
+ async processStream(r, e, a) {
13
16
  this.status.value = "streaming";
14
17
  const o = this.getOrCreateAssistantMessage(), s = /* @__PURE__ */ new Map(), l = [];
15
- let u = "stop", i;
16
- const v = t.getReader();
18
+ let f = "stop", i;
19
+ const v = r.getReader();
17
20
  try {
18
21
  for (; !e.aborted; ) {
19
- const { done: d, value: f } = await v.read();
22
+ const { done: d, value: c } = await v.read();
20
23
  if (d) break;
21
- if (this.applyChunk(o, f, s), f.type === "tool-call-end") {
22
- const y = s.get(f.toolCallId) ?? "{}";
24
+ if (this.applyChunk(o, c, s), c.type === "tool-call-end") {
25
+ const p = s.get(c.toolCallId) ?? "{}";
23
26
  let m;
24
27
  try {
25
- m = JSON.parse(y);
28
+ m = JSON.parse(p);
26
29
  } catch {
27
- m = y;
30
+ m = p;
28
31
  }
29
- const h = { toolCallId: f.toolCallId, toolName: this.getToolName(o, f.toolCallId), args: m };
30
- if (l.push(h), a.onToolCall) {
31
- const g = this.findToolCallPart(o, f.toolCallId);
32
- g && (g.state = "running", g.args = m), this.triggerReactivity();
32
+ const h = { toolCallId: c.toolCallId, toolName: this.getToolName(o, c.toolCallId), args: m };
33
+ l.push(h);
34
+ const g = this.findToolCallPart(o, c.toolCallId);
35
+ if (g && (g.args = m), a.onToolCall) {
36
+ g && (g.state = "running"), this.triggerReactivity();
33
37
  try {
34
38
  const b = await a.onToolCall(h);
35
39
  b !== void 0 && g && (g.state = "result", g.result = b, this.triggerReactivity());
@@ -38,22 +42,28 @@ class U {
38
42
  }
39
43
  }
40
44
  }
41
- if (f.type === "finish" && (u = f.finishReason, i = f.usage), f.type === "error")
42
- throw f.error;
45
+ if (c.type === "tool-result") {
46
+ const p = this.findToolCallPart(o, c.toolCallId);
47
+ p && (p.state = c.error !== void 0 ? "error" : "result", p.result = c.result, p.error = c.error);
48
+ const m = l.findIndex((h) => h.toolCallId === c.toolCallId);
49
+ m !== -1 && l.splice(m, 1);
50
+ }
51
+ if (c.type === "finish" && (f = c.finishReason, i = c.usage), c.type === "error")
52
+ throw c.error;
43
53
  this.triggerReactivity();
44
54
  }
45
55
  } catch (d) {
46
56
  if (!e.aborted) {
47
- const f = d instanceof Error ? d : new Error(String(d));
48
- throw a.onError?.(f), f;
57
+ const c = d instanceof Error ? d : new Error(String(d));
58
+ throw a.onError?.(c), c;
49
59
  }
50
60
  } finally {
51
61
  v.releaseLock();
52
62
  }
53
- return this.finalizeTextParts(o), this.triggerReactivity(), a.onFinish?.(o, { finishReason: u, usage: i }), { finishReason: u, usage: i, toolCalls: l };
63
+ return this.finalizeTextParts(o), this.triggerReactivity(), a.onFinish?.(o, { finishReason: f, usage: i }), { finishReason: f, usage: i, toolCalls: l };
54
64
  }
55
65
  getOrCreateAssistantMessage() {
56
- const t = this.messages.value, e = t[t.length - 1];
66
+ const r = this.messages.value, e = r[r.length - 1];
57
67
  if (e?.role === "assistant") return e;
58
68
  const a = {
59
69
  id: this.generateId(),
@@ -61,22 +71,22 @@ class U {
61
71
  parts: [],
62
72
  createdAt: /* @__PURE__ */ new Date()
63
73
  };
64
- return t.push(a), a;
74
+ return r.push(a), a;
65
75
  }
66
- applyChunk(t, e, a) {
76
+ applyChunk(r, e, a) {
67
77
  switch (e.type) {
68
78
  case "text-delta": {
69
- const o = t.parts[t.parts.length - 1];
70
- o?.type === "text" && o.state === "streaming" ? o.text += e.text : t.parts.push({ type: "text", text: e.text, state: "streaming" });
79
+ const o = r.parts[r.parts.length - 1];
80
+ o?.type === "text" && o.state === "streaming" ? o.text += e.text : r.parts.push({ type: "text", text: e.text, state: "streaming" });
71
81
  break;
72
82
  }
73
83
  case "reasoning-delta": {
74
- const o = t.parts[t.parts.length - 1];
75
- o?.type === "reasoning" && o.state === "streaming" ? o.text += e.text : t.parts.push({ type: "reasoning", text: e.text, state: "streaming" });
84
+ const o = r.parts[r.parts.length - 1];
85
+ o?.type === "reasoning" && o.state === "streaming" ? o.text += e.text : r.parts.push({ type: "reasoning", text: e.text, state: "streaming" });
76
86
  break;
77
87
  }
78
88
  case "tool-call-start":
79
- t.parts.push({
89
+ r.parts.push({
80
90
  type: "tool-call",
81
91
  toolCallId: e.toolCallId,
82
92
  toolName: e.toolName,
@@ -92,89 +102,89 @@ class U {
92
102
  break;
93
103
  }
94
104
  }
95
- finalizeTextParts(t) {
96
- for (const e of t.parts)
105
+ finalizeTextParts(r) {
106
+ for (const e of r.parts)
97
107
  (e.type === "text" || e.type === "reasoning") && e.state === "streaming" && (e.state = "done");
98
108
  }
99
- findToolCallPart(t, e) {
100
- return t.parts.find(
109
+ findToolCallPart(r, e) {
110
+ return r.parts.find(
101
111
  (a) => a.type === "tool-call" && a.toolCallId === e
102
112
  );
103
113
  }
104
- getToolName(t, e) {
105
- return this.findToolCallPart(t, e)?.toolName ?? "";
114
+ getToolName(r, e) {
115
+ return this.findToolCallPart(r, e)?.toolName ?? "";
106
116
  }
107
117
  triggerReactivity() {
108
118
  this.messages.value = [...this.messages.value];
109
119
  }
110
120
  }
111
- const j = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
112
- function I(r = 16) {
113
- const t = crypto.getRandomValues(new Uint8Array(r));
121
+ const J = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
122
+ function P(t = 16) {
123
+ const r = crypto.getRandomValues(new Uint8Array(t));
114
124
  let e = "";
115
- for (let a = 0; a < r; a++)
116
- e += j[t[a] % j.length];
125
+ for (let a = 0; a < t; a++)
126
+ e += J[r[a] % J.length];
117
127
  return e;
118
128
  }
119
- function Q(r) {
120
- const t = r.id ?? I(), e = x(r.initialMessages ?? []), a = x("ready"), o = x(void 0), s = x("");
129
+ function ee(t) {
130
+ const r = t.id ?? P(), e = x(t.initialMessages ?? []), a = x("ready"), o = x(void 0), s = x("");
121
131
  let l = null;
122
- const u = x(!r.storage), i = r.storage, v = (n) => {
123
- r.onError?.(n instanceof Error ? n : new Error(String(n)));
132
+ const f = x(!t.storage), i = t.storage, v = (n) => {
133
+ t.onError?.(n instanceof Error ? n : new Error(String(n)));
124
134
  };
125
135
  if (i) {
126
- const n = e.value, C = (c) => {
127
- c?.length && e.value === n && (e.value = c), u.value = !0;
136
+ const n = e.value, C = (u) => {
137
+ u?.length && e.value === n && (e.value = u), f.value = !0;
128
138
  };
129
139
  try {
130
- const c = i.load(t);
131
- c instanceof Promise ? c.then(C, (p) => {
132
- v(p), u.value = !0;
133
- }) : C(c);
134
- } catch (c) {
135
- v(c), u.value = !0;
140
+ const u = i.load(r);
141
+ u instanceof Promise ? u.then(C, (y) => {
142
+ v(y), f.value = !0;
143
+ }) : C(u);
144
+ } catch (u) {
145
+ v(u), f.value = !0;
136
146
  }
137
- V([e, a, u], () => {
138
- if (!(!u.value || a.value === "submitted" || a.value === "streaming"))
147
+ U([e, a, f], () => {
148
+ if (!(!f.value || a.value === "submitted" || a.value === "streaming"))
139
149
  try {
140
- const c = e.value.length > 0 ? i.save(t, e.value) : i.remove(t);
141
- c instanceof Promise && c.catch(v);
142
- } catch (c) {
143
- v(c);
150
+ const u = e.value.length > 0 ? i.save(r, e.value) : i.remove(r);
151
+ u instanceof Promise && u.catch(v);
152
+ } catch (u) {
153
+ v(u);
144
154
  }
145
155
  }, { deep: !0 });
146
156
  }
147
- const d = new U(e, a, I), f = () => {
157
+ const d = new k(e, a, P), c = () => {
148
158
  const n = [], C = [
149
- N(r.system),
150
- ...e.value.filter((c) => c.role === "system").map((c) => c.parts.filter((p) => p.type === "text").map((p) => p.text).join(""))
159
+ N(t.system),
160
+ ...e.value.filter((u) => u.role === "system").map((u) => u.parts.filter((y) => y.type === "text").map((y) => y.text).join(""))
151
161
  ].filter(Boolean);
152
162
  C.length > 0 && n.push({ role: "system", content: C.join(`
153
163
 
154
164
  `) });
155
- for (const c of e.value) {
156
- if (c.role === "system") continue;
157
- const E = c.parts.filter((w) => w.type === "text").map((w) => w.text).join(""), S = c.parts.filter((w) => w.type === "tool-call").map((w) => ({ id: w.toolCallId, name: w.toolName, args: w.args }));
158
- if (c.role === "user") {
159
- const w = c.parts.filter((O) => O.type === "file");
165
+ for (const u of e.value) {
166
+ if (u.role === "system") continue;
167
+ const E = u.parts.filter((w) => w.type === "text").map((w) => w.text).join(""), S = u.parts.filter((w) => w.type === "tool-call").map((w) => ({ id: w.toolCallId, name: w.toolName, args: w.args }));
168
+ if (u.role === "user") {
169
+ const w = u.parts.filter((O) => O.type === "file");
160
170
  if (w.length === 0) {
161
171
  n.push({ role: "user", content: E });
162
172
  continue;
163
173
  }
164
- const D = w.map((O) => ({
174
+ const j = w.map((O) => ({
165
175
  type: "file",
166
176
  url: O.url,
167
177
  mediaType: O.mediaType,
168
178
  filename: O.filename
169
179
  }));
170
- E && D.push({ type: "text", text: E }), n.push({ role: "user", content: D });
171
- } else if (c.role === "assistant") {
180
+ E && j.push({ type: "text", text: E }), n.push({ role: "user", content: j });
181
+ } else if (u.role === "assistant") {
172
182
  n.push({
173
183
  role: "assistant",
174
184
  content: E,
175
185
  toolCalls: S.length > 0 ? S : void 0
176
186
  });
177
- for (const w of c.parts)
187
+ for (const w of u.parts)
178
188
  w.type !== "tool-call" || w.state !== "result" || n.push({
179
189
  role: "tool",
180
190
  content: JSON.stringify(w.result),
@@ -183,52 +193,52 @@ function Q(r) {
183
193
  }
184
194
  }
185
195
  return n;
186
- }, y = async (n) => {
187
- const C = r.maxToolRoundtrips ?? 5;
188
- let c = 0;
189
- for (; c < C; ) {
190
- const p = f();
191
- r.provider.validateConfig();
192
- const { stream: E } = await r.provider.chat({
193
- model: r.model,
194
- messages: p,
195
- tools: r.tools,
196
+ }, p = async (n) => {
197
+ const C = t.maxToolRoundtrips ?? 5;
198
+ let u = 0;
199
+ for (; u < C; ) {
200
+ const y = c();
201
+ t.provider.validateConfig();
202
+ const { stream: E } = await t.provider.chat({
203
+ model: t.model,
204
+ messages: y,
205
+ tools: t.tools,
196
206
  signal: n,
197
207
  providerOptions: {
198
- ...r.providerOptions,
199
- ...N(r.body)
208
+ ...t.providerOptions,
209
+ ...N(t.body)
200
210
  }
201
211
  }), S = await d.processStream(E, n, {
202
- onFinish: r.onFinish,
203
- onError: r.onError,
204
- onToolCall: r.onToolCall
212
+ onFinish: t.onFinish,
213
+ onError: t.onError,
214
+ onToolCall: t.onToolCall
205
215
  });
206
216
  if (S.finishReason !== "tool-calls" || S.toolCalls.length === 0)
207
217
  break;
208
- c++;
218
+ u++;
209
219
  }
210
220
  }, m = async (n) => {
211
221
  const C = typeof n == "string" ? n : n?.text ?? s.value;
212
- let c;
222
+ let u;
213
223
  try {
214
- c = typeof n == "object" ? await q(n.files) : [];
224
+ u = typeof n == "object" ? await _(n.files) : [];
215
225
  } catch (S) {
216
226
  o.value = S instanceof Error ? S : new Error(String(S)), a.value = "error";
217
227
  return;
218
228
  }
219
- if (!C.trim() && c.length === 0) return;
229
+ if (!C.trim() && u.length === 0) return;
220
230
  (typeof n != "object" || n === null || !("text" in n)) && (s.value = "");
221
- const p = [...c];
222
- C.trim() && p.push({ type: "text", text: C, state: "done" });
231
+ const y = [...u];
232
+ C.trim() && y.push({ type: "text", text: C, state: "done" });
223
233
  const E = {
224
- id: I(),
234
+ id: P(),
225
235
  role: "user",
226
- parts: p,
236
+ parts: y,
227
237
  createdAt: /* @__PURE__ */ new Date()
228
238
  };
229
239
  e.value = [...e.value, E], o.value = void 0, a.value = "submitted", l?.abort(), l = new AbortController();
230
240
  try {
231
- await y(l.signal), a.value = "ready";
241
+ await p(l.signal), a.value = "ready";
232
242
  } catch (S) {
233
243
  if (l.signal.aborted) {
234
244
  a.value = "ready";
@@ -237,17 +247,17 @@ function Q(r) {
237
247
  o.value = S instanceof Error ? S : new Error(String(S)), a.value = "error";
238
248
  }
239
249
  }, h = async () => {
240
- const n = e.value, C = n.reduce((c, p, E) => p.role === "assistant" ? E : c, -1);
250
+ const n = e.value, C = n.reduce((u, y, E) => y.role === "assistant" ? E : u, -1);
241
251
  if (C !== -1) {
242
252
  e.value = n.slice(0, C), o.value = void 0, a.value = "submitted", l?.abort(), l = new AbortController();
243
253
  try {
244
- await y(l.signal), a.value = "ready";
245
- } catch (c) {
254
+ await p(l.signal), a.value = "ready";
255
+ } catch (u) {
246
256
  if (l.signal.aborted) {
247
257
  a.value = "ready";
248
258
  return;
249
259
  }
250
- o.value = c instanceof Error ? c : new Error(String(c)), a.value = "error";
260
+ o.value = u instanceof Error ? u : new Error(String(u)), a.value = "error";
251
261
  }
252
262
  }
253
263
  }, g = () => {
@@ -256,23 +266,23 @@ function Q(r) {
256
266
  e.value = typeof n == "function" ? n(e.value) : n;
257
267
  }, R = (n) => {
258
268
  n?.preventDefault?.(), s.value.trim() && m();
259
- }, T = A(() => a.value === "submitted" || a.value === "streaming"), P = ({ toolCallId: n, result: C }) => {
260
- for (const c of e.value)
261
- for (const p of c.parts)
262
- if (p.type === "tool-call" && p.toolCallId === n) {
263
- p.state = "result", p.result = C, e.value = [...e.value];
269
+ }, T = A(() => a.value === "submitted" || a.value === "streaming"), I = ({ toolCallId: n, result: C }) => {
270
+ for (const u of e.value)
271
+ for (const y of u.parts)
272
+ if (y.type === "tool-call" && y.toolCallId === n) {
273
+ y.state = "result", y.result = C, e.value = [...e.value];
264
274
  return;
265
275
  }
266
276
  }, M = () => {
267
277
  o.value = void 0, a.value === "error" && (a.value = "ready");
268
278
  };
269
- return L(() => {
279
+ return L() && D(() => {
270
280
  l?.abort();
271
281
  }), {
272
282
  messages: e,
273
283
  status: A(() => a.value),
274
284
  error: o,
275
- id: t,
285
+ id: r,
276
286
  input: s,
277
287
  sendMessage: m,
278
288
  regenerate: h,
@@ -280,111 +290,111 @@ function Q(r) {
280
290
  setMessages: b,
281
291
  handleSubmit: R,
282
292
  isLoading: T,
283
- addToolResult: P,
293
+ addToolResult: I,
284
294
  clearError: M,
285
- isRestored: A(() => u.value)
295
+ isRestored: A(() => f.value)
286
296
  };
287
297
  }
288
- function X(r) {
289
- const t = r.id ?? I(), e = x(""), a = x("ready"), o = x(void 0), s = x("");
298
+ function te(t) {
299
+ const r = t.id ?? P(), e = x(""), a = x("ready"), o = x(void 0), s = x("");
290
300
  let l = null;
291
- const u = async (y) => {
292
- const m = y ?? s.value;
301
+ const f = async (p) => {
302
+ const m = p ?? s.value;
293
303
  if (!m.trim()) return e.value;
294
- y || (s.value = ""), e.value = "", o.value = void 0, a.value = "submitted", l?.abort(), l = new AbortController();
304
+ p || (s.value = ""), e.value = "", o.value = void 0, a.value = "submitted", l?.abort(), l = new AbortController();
295
305
  try {
296
- r.provider.validateConfig();
297
- const h = N(r.system), g = h ? [
306
+ t.provider.validateConfig();
307
+ const h = N(t.system), g = h ? [
298
308
  { role: "system", content: h },
299
309
  { role: "user", content: m }
300
- ] : [{ role: "user", content: m }], { stream: b } = await r.provider.chat({
301
- model: r.model,
310
+ ] : [{ role: "user", content: m }], { stream: b } = await t.provider.chat({
311
+ model: t.model,
302
312
  messages: g,
303
313
  signal: l.signal
304
314
  });
305
315
  a.value = "streaming";
306
316
  const R = b.getReader();
307
- let T = "stop", P;
317
+ let T = "stop", I;
308
318
  for (; !l.signal.aborted; ) {
309
319
  const { done: M, value: n } = await R.read();
310
320
  if (M) break;
311
321
  if (n.type === "text-delta")
312
322
  e.value += n.text;
313
323
  else if (n.type === "finish")
314
- T = n.finishReason, P = n.usage;
324
+ T = n.finishReason, I = n.usage;
315
325
  else if (n.type === "error")
316
326
  throw n.error;
317
327
  }
318
- return R.releaseLock(), a.value = "ready", r.onFinish?.(e.value, { finishReason: T, usage: P }), e.value;
328
+ return R.releaseLock(), a.value = "ready", t.onFinish?.(e.value, { finishReason: T, usage: I }), e.value;
319
329
  } catch (h) {
320
330
  if (!l || l.signal.aborted)
321
331
  return a.value = "ready", e.value;
322
332
  const g = h instanceof Error ? h : new Error(String(h));
323
- return o.value = g, a.value = "error", r.onError?.(g), e.value;
333
+ return o.value = g, a.value = "error", t.onError?.(g), e.value;
324
334
  }
325
335
  }, i = () => {
326
336
  l?.abort(), l = null, a.value = "ready";
327
337
  }, v = () => {
328
338
  o.value = void 0, a.value === "error" && (a.value = "ready");
329
- }, d = (y) => {
330
- y?.preventDefault?.(), s.value.trim() && u();
331
- }, f = A(() => a.value === "submitted" || a.value === "streaming");
332
- return L(() => {
339
+ }, d = (p) => {
340
+ p?.preventDefault?.(), s.value.trim() && f();
341
+ }, c = A(() => a.value === "submitted" || a.value === "streaming");
342
+ return L() && D(() => {
333
343
  l?.abort();
334
344
  }), {
335
345
  completion: e,
336
346
  status: A(() => a.value),
337
347
  error: o,
338
- isLoading: f,
339
- id: t,
348
+ isLoading: c,
349
+ id: r,
340
350
  input: s,
341
- complete: u,
351
+ complete: f,
342
352
  stop: i,
343
353
  clearError: v,
344
354
  handleSubmit: d
345
355
  };
346
356
  }
347
- function ee(r) {
348
- const t = r.id ?? I(), e = x(void 0), a = x(void 0), o = x("ready"), s = x(void 0), l = x("");
349
- let u = null;
350
- const i = async (y) => {
351
- const m = y ?? l.value;
357
+ function re(t) {
358
+ const r = t.id ?? P(), e = x(void 0), a = x(void 0), o = x("ready"), s = x(void 0), l = x("");
359
+ let f = null;
360
+ const i = async (p) => {
361
+ const m = p ?? l.value;
352
362
  if (!m.trim()) return e.value;
353
- y || (l.value = ""), e.value = void 0, a.value = void 0, s.value = void 0, o.value = "submitted", u?.abort(), u = new AbortController();
363
+ p || (l.value = ""), e.value = void 0, a.value = void 0, s.value = void 0, o.value = "submitted", f?.abort(), f = new AbortController();
354
364
  try {
355
- r.provider.validateConfig();
365
+ t.provider.validateConfig();
356
366
  const h = [
357
- N(r.system) ?? "",
367
+ N(t.system) ?? "",
358
368
  "Respond with valid JSON matching this schema:",
359
- JSON.stringify(r.schema, null, 2),
369
+ JSON.stringify(t.schema, null, 2),
360
370
  "Output ONLY the JSON object, no other text."
361
371
  ].filter(Boolean).join(`
362
372
 
363
- `), { stream: g } = await r.provider.chat({
364
- model: r.model,
373
+ `), { stream: g } = await t.provider.chat({
374
+ model: t.model,
365
375
  messages: [
366
376
  { role: "system", content: h },
367
377
  { role: "user", content: m }
368
378
  ],
369
- signal: u.signal,
379
+ signal: f.signal,
370
380
  providerOptions: {
371
381
  response_format: { type: "json_object" }
372
382
  }
373
383
  });
374
384
  o.value = "streaming";
375
385
  const b = g.getReader();
376
- let R = "", T = "stop", P;
377
- for (; !u.signal.aborted; ) {
386
+ let R = "", T = "stop", I;
387
+ for (; !f.signal.aborted; ) {
378
388
  const { done: M, value: n } = await b.read();
379
389
  if (M) break;
380
390
  if (n.type === "text-delta") {
381
391
  R += n.text;
382
392
  try {
383
- a.value = _(R);
393
+ a.value = W(R);
384
394
  } catch {
385
395
  }
386
396
  } else if (n.type === "finish")
387
- T = n.finishReason, P = n.usage;
397
+ T = n.finishReason, I = n.usage;
388
398
  else if (n.type === "error")
389
399
  throw n.error;
390
400
  }
@@ -394,40 +404,40 @@ function ee(r) {
394
404
  } catch {
395
405
  throw new Error(`Failed to parse JSON response: ${R.slice(0, 200)}`);
396
406
  }
397
- return o.value = "ready", r.onFinish?.(e.value, { finishReason: T, usage: P }), e.value;
407
+ return o.value = "ready", t.onFinish?.(e.value, { finishReason: T, usage: I }), e.value;
398
408
  } catch (h) {
399
- if (u.signal.aborted)
409
+ if (f.signal.aborted)
400
410
  return o.value = "ready", e.value;
401
411
  const g = h instanceof Error ? h : new Error(String(h));
402
- s.value = g, o.value = "error", r.onError?.(g);
412
+ s.value = g, o.value = "error", t.onError?.(g);
403
413
  return;
404
414
  }
405
415
  }, v = () => {
406
- u?.abort(), u = null, o.value = "ready";
416
+ f?.abort(), f = null, o.value = "ready";
407
417
  }, d = () => {
408
418
  s.value = void 0, o.value === "error" && (o.value = "ready");
409
- }, f = A(() => o.value === "submitted" || o.value === "streaming");
410
- return L(() => {
411
- u?.abort();
419
+ }, c = A(() => o.value === "submitted" || o.value === "streaming");
420
+ return L() && D(() => {
421
+ f?.abort();
412
422
  }), {
413
423
  object: e,
414
424
  partialObject: a,
415
425
  status: A(() => o.value),
416
426
  error: s,
417
- isLoading: f,
418
- id: t,
427
+ isLoading: c,
428
+ id: r,
419
429
  input: l,
420
430
  submit: i,
421
431
  stop: v,
422
432
  clearError: d
423
433
  };
424
434
  }
425
- function _(r) {
426
- let t = r.trim();
427
- if (!t.startsWith("{")) return;
435
+ function W(t) {
436
+ let r = t.trim();
437
+ if (!r.startsWith("{")) return;
428
438
  let e = 0, a = !1, o = !1;
429
- for (let s = 0; s < t.length; s++) {
430
- const l = t[s];
439
+ for (let s = 0; s < r.length; s++) {
440
+ const l = r[s];
431
441
  if (o) {
432
442
  o = !1;
433
443
  continue;
@@ -443,35 +453,35 @@ function _(r) {
443
453
  a || (l === "{" && e++, l === "}" && e--);
444
454
  }
445
455
  for (; e > 0; )
446
- t += "}", e--;
456
+ r += "}", e--;
447
457
  try {
448
- return JSON.parse(t);
458
+ return JSON.parse(r);
449
459
  } catch {
450
460
  return;
451
461
  }
452
462
  }
453
- const F = /(\\)?\{\{\s*([A-Za-z_][\w-]*)\s*\}\}/g;
454
- function k(r, t = {}) {
463
+ const $ = /(\\)?\{\{\s*([A-Za-z_][\w-]*)\s*\}\}/g;
464
+ function H(t, r = {}) {
455
465
  return {
456
- template: r,
457
- variables: z(r),
466
+ template: t,
467
+ variables: B(t),
458
468
  render(e) {
459
- return $(r, { ...t.defaults, ...e });
469
+ return z(t, { ...r.defaults, ...e });
460
470
  }
461
471
  };
462
472
  }
463
- function te(r, t = {}) {
473
+ function ae(t, r = {}) {
464
474
  const e = (a) => {
465
- const o = { ...t.defaults, ...a };
466
- return r.map((s) => ({ role: s.role, content: $(s.content, o) }));
475
+ const o = { ...r.defaults, ...a };
476
+ return t.map((s) => ({ role: s.role, content: z(s.content, o) }));
467
477
  };
468
478
  return {
469
- messages: r,
470
- variables: [...new Set(r.flatMap((a) => z(a.content)))],
479
+ messages: t,
480
+ variables: [...new Set(t.flatMap((a) => B(a.content)))],
471
481
  render: e,
472
482
  toUIMessages(a) {
473
483
  return e(a).map((o) => ({
474
- id: I(),
484
+ id: P(),
475
485
  role: o.role,
476
486
  parts: [{ type: "text", text: o.content, state: "done" }],
477
487
  createdAt: /* @__PURE__ */ new Date()
@@ -479,154 +489,154 @@ function te(r, t = {}) {
479
489
  }
480
490
  };
481
491
  }
482
- function $(r, t) {
483
- return r.replace(F, (e, a, o) => {
492
+ function z(t, r) {
493
+ return t.replace($, (e, a, o) => {
484
494
  if (a) return e.slice(1);
485
- const s = t[o];
495
+ const s = r[o];
486
496
  if (s == null)
487
497
  throw new Error(`[vue-llm] Missing prompt variable "${o}"`);
488
498
  return String(s);
489
499
  });
490
500
  }
491
- function z(r) {
492
- const t = /* @__PURE__ */ new Set();
493
- for (const [, e, a] of r.matchAll(F))
494
- e || t.add(a);
495
- return [...t];
501
+ function B(t) {
502
+ const r = /* @__PURE__ */ new Set();
503
+ for (const [, e, a] of t.matchAll($))
504
+ e || r.add(a);
505
+ return [...r];
496
506
  }
497
- function re(r, t) {
498
- const e = typeof r == "string" ? k(r) : r;
499
- return A(() => e.render(N(t)));
507
+ function oe(t, r) {
508
+ const e = typeof t == "string" ? H(t) : t;
509
+ return A(() => e.render(N(r)));
500
510
  }
501
- function ae(r, t) {
502
- return t.length ? {
503
- id: r.id,
504
- supportsStreaming: r.supportsStreaming,
511
+ function ne(t, r) {
512
+ return r.length ? {
513
+ id: t.id,
514
+ supportsStreaming: t.supportsStreaming,
505
515
  validateConfig() {
506
- r.validateConfig();
516
+ t.validateConfig();
507
517
  },
508
518
  async chat(e) {
509
519
  let a = e;
510
- for (const i of t)
511
- i.beforeRequest && (a = await i.beforeRequest(a, r));
512
- const o = t.reduceRight(
520
+ for (const i of r)
521
+ i.beforeRequest && (a = await i.beforeRequest(a, t));
522
+ const o = r.reduceRight(
513
523
  (i, v) => v.wrapChat ? (d) => v.wrapChat(i, d) : i,
514
- (i) => r.chat(i)
524
+ (i) => t.chat(i)
515
525
  );
516
526
  let s;
517
527
  try {
518
528
  s = await o(a);
519
529
  } catch (i) {
520
530
  let v = i instanceof Error ? i : new Error(String(i));
521
- for (const d of t)
531
+ for (const d of r)
522
532
  if (d.onError) {
523
- const f = await d.onError(v, a);
524
- f instanceof Error && (v = f);
533
+ const c = await d.onError(v, a);
534
+ c instanceof Error && (v = c);
525
535
  }
526
536
  throw v;
527
537
  }
528
- const u = t.some((i) => i.afterStream) ? s.stream.pipeThrough(new TransformStream({
538
+ const f = r.some((i) => i.afterStream) ? s.stream.pipeThrough(new TransformStream({
529
539
  async transform(i, v) {
530
540
  let d = i;
531
- for (const f of t) {
541
+ for (const c of r) {
532
542
  if (!d) break;
533
- f.afterStream && (d = await f.afterStream(d, a));
543
+ c.afterStream && (d = await c.afterStream(d, a));
534
544
  }
535
545
  d && v.enqueue(d);
536
546
  }
537
547
  })) : s.stream;
538
- for (const i of t)
548
+ for (const i of r)
539
549
  i.onFinish && i.onFinish(a, s);
540
- return { stream: u, response: s.response };
550
+ return { stream: f, response: s.response };
541
551
  }
542
- } : r;
552
+ } : t;
543
553
  }
544
- function oe(r = "vue-llm") {
554
+ function se(t = "vue-llm") {
545
555
  return {
546
- beforeRequest(t) {
547
- return console.log(`[${r}] → ${t.model}`, t.messages.length, "messages"), t;
556
+ beforeRequest(r) {
557
+ return console.log(`[${t}] → ${r.model}`, r.messages.length, "messages"), r;
548
558
  },
549
- afterStream(t) {
550
- return t.type === "finish" && console.log(`[${r}] ← finish:`, t.finishReason, t.usage ?? ""), t;
559
+ afterStream(r) {
560
+ return r.type === "finish" && console.log(`[${t}] ← finish:`, r.finishReason, r.usage ?? ""), r;
551
561
  },
552
- onError(t) {
553
- console.error(`[${r}] error:`, t.message);
562
+ onError(r) {
563
+ console.error(`[${t}] error:`, r.message);
554
564
  }
555
565
  };
556
566
  }
557
- function ne(r = {}, t) {
558
- const e = typeof r == "number" ? { maxRetries: r, initialDelay: t } : r, a = e.maxRetries ?? 3, o = e.initialDelay ?? 1e3, s = e.maxDelay ?? 3e4, l = e.factor ?? 2, u = e.jitter ?? !0, i = e.shouldRetry ?? W;
567
+ function le(t = {}, r) {
568
+ const e = typeof t == "number" ? { maxRetries: t, initialDelay: r } : t, a = e.maxRetries ?? 3, o = e.initialDelay ?? 1e3, s = e.maxDelay ?? 3e4, l = e.factor ?? 2, f = e.jitter ?? !0, i = e.shouldRetry ?? Y;
559
569
  return {
560
570
  async wrapChat(v, d) {
561
- for (let f = 1; ; f++)
571
+ for (let c = 1; ; c++)
562
572
  try {
563
573
  return await v(d);
564
- } catch (y) {
565
- const m = y instanceof Error ? y : new Error(String(y));
566
- if (f > a || d.signal?.aborted || !i(m, f))
574
+ } catch (p) {
575
+ const m = p instanceof Error ? p : new Error(String(p));
576
+ if (c > a || d.signal?.aborted || !i(m, c))
567
577
  throw m;
568
- const h = o * l ** (f - 1), g = m instanceof J ? m.retryAfter : void 0, b = Math.min(
578
+ const h = o * l ** (c - 1), g = m instanceof F ? m.retryAfter : void 0, b = Math.min(
569
579
  s,
570
- g ?? (u ? h * (0.5 + Math.random() * 0.5) : h)
580
+ g ?? (f ? h * (0.5 + Math.random() * 0.5) : h)
571
581
  );
572
- e.onRetry?.({ error: m, attempt: f, delay: b }), await Y(b, d.signal);
582
+ e.onRetry?.({ error: m, attempt: c, delay: b }), await G(b, d.signal);
573
583
  }
574
584
  }
575
585
  };
576
586
  }
577
- function W(r) {
578
- return r.name === "AbortError" ? !1 : r instanceof J ? r.isRetryable : r instanceof TypeError;
587
+ function Y(t) {
588
+ return t.name === "AbortError" ? !1 : t instanceof F ? t.isRetryable : t instanceof TypeError;
579
589
  }
580
- function se(r) {
581
- const t = r.interval ?? 6e4, e = [];
590
+ function ie(t) {
591
+ const r = t.interval ?? 6e4, e = [];
582
592
  let a = 0;
583
593
  const o = /* @__PURE__ */ new Set(), s = () => {
584
594
  a--;
585
- for (const u of [...o]) u();
586
- }, l = async (u) => {
595
+ for (const f of [...o]) f();
596
+ }, l = async (f) => {
587
597
  for (; ; ) {
588
- u?.throwIfAborted();
598
+ f?.throwIfAborted();
589
599
  const i = Date.now();
590
- for (; e.length > 0 && e[0] <= i - t; ) e.shift();
591
- const v = r.maxRequests !== void 0 && e.length >= r.maxRequests, d = r.maxConcurrent !== void 0 && a >= r.maxConcurrent;
600
+ for (; e.length > 0 && e[0] <= i - r; ) e.shift();
601
+ const v = t.maxRequests !== void 0 && e.length >= t.maxRequests, d = t.maxConcurrent !== void 0 && a >= t.maxConcurrent;
592
602
  if (!v && !d) {
593
603
  e.push(i), a++;
594
604
  return;
595
605
  }
596
- await new Promise((f, y) => {
606
+ await new Promise((c, p) => {
597
607
  const m = () => {
598
- clearTimeout(g), o.delete(m), u?.removeEventListener("abort", h), f();
608
+ clearTimeout(g), o.delete(m), f?.removeEventListener("abort", h), c();
599
609
  }, h = () => {
600
- m(), y(u.reason);
601
- }, g = v ? setTimeout(m, e[0] + t - i) : void 0;
602
- o.add(m), u?.addEventListener("abort", h, { once: !0 });
610
+ m(), p(f.reason);
611
+ }, g = v ? setTimeout(m, e[0] + r - i) : void 0;
612
+ o.add(m), f?.addEventListener("abort", h, { once: !0 });
603
613
  });
604
614
  }
605
615
  };
606
616
  return {
607
- async wrapChat(u, i) {
617
+ async wrapChat(f, i) {
608
618
  await l(i.signal);
609
619
  let v;
610
620
  try {
611
- v = await u(i);
621
+ v = await f(i);
612
622
  } catch (d) {
613
623
  throw s(), d;
614
624
  }
615
- return { ...v, stream: H(v.stream, s) };
625
+ return { ...v, stream: Z(v.stream, s) };
616
626
  }
617
627
  };
618
628
  }
619
- function H(r, t) {
620
- const e = r.getReader();
629
+ function Z(t, r) {
630
+ const e = t.getReader();
621
631
  let a = !1;
622
632
  const o = () => {
623
- a || (a = !0, t());
633
+ a || (a = !0, r());
624
634
  };
625
635
  return new ReadableStream({
626
636
  async pull(s) {
627
637
  try {
628
- const { done: l, value: u } = await e.read();
629
- l ? (o(), s.close()) : s.enqueue(u);
638
+ const { done: l, value: f } = await e.read();
639
+ l ? (o(), s.close()) : s.enqueue(f);
630
640
  } catch (l) {
631
641
  o(), s.error(l);
632
642
  }
@@ -636,89 +646,89 @@ function H(r, t) {
636
646
  }
637
647
  });
638
648
  }
639
- function Y(r, t) {
649
+ function G(t, r) {
640
650
  return new Promise((e, a) => {
641
- if (t?.aborted) return a(t.reason);
651
+ if (r?.aborted) return a(r.reason);
642
652
  const o = setTimeout(() => {
643
- t?.removeEventListener("abort", s), e();
644
- }, r), s = () => {
645
- clearTimeout(o), a(t.reason);
653
+ r?.removeEventListener("abort", s), e();
654
+ }, t), s = () => {
655
+ clearTimeout(o), a(r.reason);
646
656
  };
647
- t?.addEventListener("abort", s, { once: !0 });
657
+ r?.addEventListener("abort", s, { once: !0 });
648
658
  });
649
659
  }
650
- function le(r) {
660
+ function ce(t) {
651
661
  return {
652
- beforeRequest(t) {
662
+ beforeRequest(r) {
653
663
  return {
654
- ...t,
664
+ ...r,
655
665
  providerOptions: {
656
- ...t.providerOptions,
657
- headers: { ...t.providerOptions?.headers, ...r }
666
+ ...r.providerOptions,
667
+ headers: { ...r.providerOptions?.headers, ...t }
658
668
  }
659
669
  };
660
670
  }
661
671
  };
662
672
  }
663
- function ie(r = {}) {
664
- const t = r.prefix ?? "vue-llm:chat:", e = () => r.storage ? r.storage : typeof localStorage < "u" ? localStorage : void 0;
673
+ function ue(t = {}) {
674
+ const r = t.prefix ?? "vue-llm:chat:", e = () => t.storage ? t.storage : typeof localStorage < "u" ? localStorage : void 0;
665
675
  return {
666
676
  load(a) {
667
- const o = e()?.getItem(t + a);
677
+ const o = e()?.getItem(r + a);
668
678
  if (!o) return null;
669
679
  try {
670
- return B(o);
680
+ return V(o);
671
681
  } catch {
672
682
  return null;
673
683
  }
674
684
  },
675
685
  save(a, o) {
676
- e()?.setItem(t + a, JSON.stringify(o));
686
+ e()?.setItem(r + a, JSON.stringify(o));
677
687
  },
678
688
  remove(a) {
679
- e()?.removeItem(t + a);
689
+ e()?.removeItem(r + a);
680
690
  }
681
691
  };
682
692
  }
683
- function ce() {
684
- const r = /* @__PURE__ */ new Map();
693
+ function fe() {
694
+ const t = /* @__PURE__ */ new Map();
685
695
  return {
686
- load(t) {
687
- const e = r.get(t);
688
- return e ? B(e) : null;
696
+ load(r) {
697
+ const e = t.get(r);
698
+ return e ? V(e) : null;
689
699
  },
690
- save(t, e) {
691
- r.set(t, JSON.stringify(e));
700
+ save(r, e) {
701
+ t.set(r, JSON.stringify(e));
692
702
  },
693
- remove(t) {
694
- r.delete(t);
703
+ remove(r) {
704
+ t.delete(r);
695
705
  }
696
706
  };
697
707
  }
698
- function B(r) {
699
- const t = JSON.parse(r);
700
- return Array.isArray(t) ? t.map((e) => ({
708
+ function V(t) {
709
+ const r = JSON.parse(t);
710
+ return Array.isArray(r) ? r.map((e) => ({
701
711
  ...e,
702
712
  createdAt: e.createdAt ? new Date(e.createdAt) : void 0
703
713
  })) : [];
704
714
  }
705
715
  export {
706
- J as LLMAPIError,
707
- q as convertFilesToParts,
708
- te as createChatPrompt,
709
- ce as createMemoryStorage,
710
- k as createPrompt,
711
- ie as createWebStorage,
712
- de as fileToDataURL,
713
- le as headerMiddleware,
714
- W as isRetryableError,
715
- oe as loggingMiddleware,
716
- se as rateLimitMiddleware,
717
- $ as renderTemplate,
718
- ne as retryMiddleware,
719
- Q as useChat,
720
- X as useCompletion,
721
- ee as useObject,
722
- re as usePrompt,
723
- ae as wrapProvider
716
+ F as LLMAPIError,
717
+ _ as convertFilesToParts,
718
+ ae as createChatPrompt,
719
+ fe as createMemoryStorage,
720
+ H as createPrompt,
721
+ ue as createWebStorage,
722
+ me as fileToDataURL,
723
+ ce as headerMiddleware,
724
+ Y as isRetryableError,
725
+ se as loggingMiddleware,
726
+ ie as rateLimitMiddleware,
727
+ z as renderTemplate,
728
+ le as retryMiddleware,
729
+ ee as useChat,
730
+ te as useCompletion,
731
+ re as useObject,
732
+ oe as usePrompt,
733
+ ne as wrapProvider
724
734
  };