vue-llm 1.5.0 → 1.7.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,12 +1,12 @@
1
- import { unref as X, ref as R, watch as z, computed as F, getCurrentScope as H, onScopeDispose as B, nextTick as U } from "vue";
2
- import { c as ee } from "./file-x_kW7BHa.js";
3
- import { f as Ie } from "./file-x_kW7BHa.js";
4
- import { r as k, t as W } from "./devtools-DnDvylCG.js";
5
- import { L as _ } from "./errors-B3A_zmVE.js";
1
+ import { unref as ae, ref as E, watch as z, computed as F, getCurrentScope as k, onScopeDispose as B, nextTick as V } from "vue";
2
+ import { c as oe } from "./file-x_kW7BHa.js";
3
+ import { f as Fe } from "./file-x_kW7BHa.js";
4
+ import { r as W, t as U } from "./devtools-DnDvylCG.js";
5
+ import { L as K } from "./errors-B3A_zmVE.js";
6
6
  function P(t) {
7
- return typeof t == "function" ? t() : X(t);
7
+ return typeof t == "function" ? t() : ae(t);
8
8
  }
9
- function K(t) {
9
+ function Y(t) {
10
10
  const r = t.trim();
11
11
  if (!r) return;
12
12
  try {
@@ -31,8 +31,8 @@ function K(t) {
31
31
  if (n !== void 0) return n;
32
32
  }
33
33
  for (let n = o.length - 1; n >= 0; n--) {
34
- const l = o[n], f = $(r.slice(0, l.at).replace(/[\s,]+$/, "") + l.closers);
35
- if (f !== void 0) return f;
34
+ const l = o[n], c = $(r.slice(0, l.at).replace(/[\s,]+$/, "") + l.closers);
35
+ if (c !== void 0) return c;
36
36
  }
37
37
  }
38
38
  function $(t) {
@@ -42,7 +42,7 @@ function $(t) {
42
42
  return;
43
43
  }
44
44
  }
45
- class te {
45
+ class se {
46
46
  constructor(r, e, o) {
47
47
  this.messages = r, this.status = e, this.generateId = o;
48
48
  }
@@ -52,52 +52,52 @@ class te {
52
52
  async processStream(r, e, o) {
53
53
  e.aborted || (this.status.value = "streaming");
54
54
  const a = this.getOrCreateAssistantMessage(), s = /* @__PURE__ */ new Map(), n = [];
55
- let l = "stop", f;
56
- const p = r.getReader();
55
+ let l = "stop", c;
56
+ const v = r.getReader();
57
57
  try {
58
58
  for (; !e.aborted; ) {
59
- const { done: d, value: u } = await p.read();
60
- if (d) break;
61
- if (this.applyChunk(a, u, s), u.type === "tool-call-end") {
62
- const b = s.get(u.toolCallId) ?? "{}";
63
- let h;
59
+ const { done: f, value: i } = await v.read();
60
+ if (f) break;
61
+ if (this.applyChunk(a, i, s), i.type === "tool-call-end") {
62
+ const w = s.get(i.toolCallId) ?? "{}";
63
+ let y;
64
64
  try {
65
- h = JSON.parse(b);
65
+ y = JSON.parse(w);
66
66
  } catch {
67
- h = b;
67
+ y = w;
68
68
  }
69
- const C = { toolCallId: u.toolCallId, toolName: this.getToolName(a, u.toolCallId), args: h };
70
- n.push(C);
71
- const m = this.findToolCallPart(a, u.toolCallId);
72
- if (m && (m.args = h, m.state = "pending"), o.onToolCall && (o.canHandleTool?.(C.toolName) ?? !0)) {
69
+ const T = { toolCallId: i.toolCallId, toolName: this.getToolName(a, i.toolCallId), args: y };
70
+ n.push(T);
71
+ const m = this.findToolCallPart(a, i.toolCallId);
72
+ if (m && (m.args = y, m.state = "pending"), o.onToolCall && (o.canHandleTool?.(T.toolName) ?? !0)) {
73
73
  m && (m.state = "running"), this.triggerReactivity();
74
74
  try {
75
- const w = await o.onToolCall(C, { signal: e });
76
- m && (w === void 0 ? m.state = "pending" : (m.state = "result", m.result = w), this.triggerReactivity());
77
- } catch (w) {
78
- m && (m.state = "error", m.error = w instanceof Error ? w.message : String(w), this.triggerReactivity());
75
+ const C = await o.onToolCall(T, { signal: e });
76
+ m && (C === void 0 ? m.state = "pending" : (m.state = "result", m.result = C), this.triggerReactivity());
77
+ } catch (C) {
78
+ m && (m.state = "error", m.error = C instanceof Error ? C.message : String(C), this.triggerReactivity());
79
79
  }
80
80
  }
81
81
  }
82
- if (u.type === "tool-result") {
83
- const b = this.findToolCallPart(a, u.toolCallId);
84
- b && (b.state = u.error !== void 0 ? "error" : "result", b.result = u.result, b.error = u.error);
85
- const h = n.findIndex((C) => C.toolCallId === u.toolCallId);
86
- h !== -1 && n.splice(h, 1);
82
+ if (i.type === "tool-result") {
83
+ const w = this.findToolCallPart(a, i.toolCallId);
84
+ w && (w.state = i.error !== void 0 ? "error" : "result", w.result = i.result, w.error = i.error);
85
+ const y = n.findIndex((T) => T.toolCallId === i.toolCallId);
86
+ y !== -1 && n.splice(y, 1);
87
87
  }
88
- if (u.type === "finish" && (l = u.finishReason, f = u.usage), u.type === "error")
89
- throw u.error;
88
+ if (i.type === "finish" && (l = i.finishReason, c = i.usage), i.type === "error")
89
+ throw i.error;
90
90
  this.triggerReactivity();
91
91
  }
92
- } catch (d) {
92
+ } catch (f) {
93
93
  if (!e.aborted) {
94
- const u = d instanceof Error ? d : new Error(String(d));
95
- throw o.onError?.(u), u;
94
+ const i = f instanceof Error ? f : new Error(String(f));
95
+ throw o.onError?.(i), i;
96
96
  }
97
97
  } finally {
98
- p.releaseLock();
98
+ v.releaseLock();
99
99
  }
100
- return this.finalizeTextParts(a), this.triggerReactivity(), o.onFinish?.(a, { finishReason: l, usage: f }), { finishReason: l, usage: f, toolCalls: n };
100
+ return this.finalizeTextParts(a), this.triggerReactivity(), o.onFinish?.(a, { finishReason: l, usage: c }), { finishReason: l, usage: c, toolCalls: n };
101
101
  }
102
102
  getOrCreateAssistantMessage() {
103
103
  const r = this.messages.value, e = r[r.length - 1];
@@ -134,10 +134,16 @@ class te {
134
134
  case "tool-call-delta": {
135
135
  const a = (o.get(e.toolCallId) ?? "") + e.args;
136
136
  o.set(e.toolCallId, a);
137
- const s = this.findToolCallPart(r, e.toolCallId), n = K(a);
137
+ const s = this.findToolCallPart(r, e.toolCallId), n = Y(a);
138
138
  s && n !== void 0 && (s.args = n);
139
139
  break;
140
140
  }
141
+ case "message-start":
142
+ r.id = e.id;
143
+ break;
144
+ case "message-metadata":
145
+ r.metadata = { ...r.metadata, ...e.metadata };
146
+ break;
141
147
  }
142
148
  }
143
149
  finalizeTextParts(r) {
@@ -154,243 +160,257 @@ class te {
154
160
  this.messages.value = [...this.messages.value];
155
161
  }
156
162
  }
157
- const V = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
158
- function j(t = 16) {
163
+ const _ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
164
+ function J(t = 16) {
159
165
  const r = crypto.getRandomValues(new Uint8Array(t));
160
166
  let e = "";
161
167
  for (let o = 0; o < t; o++)
162
- e += V[r[o] % V.length];
168
+ e += _[r[o] % _.length];
163
169
  return e;
164
170
  }
165
- function fe(t) {
166
- const r = t.id ?? j(), e = R(t.initialMessages ?? []), o = R("ready"), a = R(void 0), s = R("");
167
- let n = null;
168
- const l = R(!t.storage), f = t.storage, p = (c) => {
169
- t.onError?.(c instanceof Error ? c : new Error(String(c)));
171
+ function ge(t) {
172
+ const r = J(), e = () => P(t.id) ?? r, o = e(), a = E(t.initialMessages ?? []), s = E("ready"), n = E(void 0), l = E("");
173
+ let c = null;
174
+ const v = E(!t.storage), f = t.storage, i = (u) => {
175
+ t.onError?.(u instanceof Error ? u : new Error(String(u)));
170
176
  };
171
- if (f) {
172
- const c = e.value, y = (v) => {
173
- v?.length && e.value === c && (e.value = v), l.value = !0;
177
+ let w = 0;
178
+ const y = (u) => {
179
+ if (!f) return;
180
+ const p = ++w, h = a.value;
181
+ v.value = !1;
182
+ const b = (x) => {
183
+ p === w && (x?.length && a.value === h && (a.value = x), v.value = !0);
184
+ }, M = (x) => {
185
+ p === w && (i(x), v.value = !0);
174
186
  };
175
187
  try {
176
- const v = f.load(r);
177
- v instanceof Promise ? v.then(y, (x) => {
178
- p(x), l.value = !0;
179
- }) : y(v);
180
- } catch (v) {
181
- p(v), l.value = !0;
188
+ const x = f.load(u);
189
+ x instanceof Promise ? x.then(b, M) : b(x);
190
+ } catch (x) {
191
+ M(x);
182
192
  }
183
- z([e, o, l], () => {
184
- if (!(!l.value || o.value === "submitted" || o.value === "streaming"))
185
- try {
186
- const v = e.value.length > 0 ? f.save(r, e.value) : f.remove(r);
187
- v instanceof Promise && v.catch(p);
188
- } catch (v) {
189
- p(v);
190
- }
191
- }, { deep: !0 });
192
- }
193
- const d = new te(e, o, j), u = () => {
194
- const c = [], y = [
193
+ };
194
+ f && (y(e()), z([a, s, v], () => {
195
+ if (!(!v.value || s.value === "submitted" || s.value === "streaming"))
196
+ try {
197
+ const u = a.value.length > 0 ? f.save(e(), a.value) : f.remove(e());
198
+ u instanceof Promise && u.catch(i);
199
+ } catch (u) {
200
+ i(u);
201
+ }
202
+ }, { deep: !0 }));
203
+ const T = new se(a, s, J), m = () => {
204
+ const u = [], p = [
195
205
  P(t.system),
196
- ...e.value.filter((v) => v.role === "system").map((v) => v.parts.filter((x) => x.type === "text").map((x) => x.text).join(""))
206
+ ...a.value.filter((h) => h.role === "system").map((h) => h.parts.filter((b) => b.type === "text").map((b) => b.text).join(""))
197
207
  ].filter(Boolean);
198
- y.length > 0 && c.push({ role: "system", content: y.join(`
208
+ p.length > 0 && u.push({ role: "system", content: p.join(`
199
209
 
200
210
  `) });
201
- for (const v of e.value) {
202
- if (v.role === "system") continue;
203
- const M = v.parts.filter((S) => S.type === "text").map((S) => S.text).join(""), O = v.parts.filter((S) => S.type === "tool-call").map((S) => ({ id: S.toolCallId, name: S.toolName, args: S.args }));
204
- if (v.role === "user") {
205
- const S = v.parts.filter((J) => J.type === "file");
211
+ for (const h of a.value) {
212
+ if (h.role === "system") continue;
213
+ const M = h.parts.filter((S) => S.type === "text").map((S) => S.text).join(""), x = h.parts.filter((S) => S.type === "tool-call").map((S) => ({ id: S.toolCallId, name: S.toolName, args: S.args }));
214
+ if (h.role === "user") {
215
+ const S = h.parts.filter((j) => j.type === "file");
206
216
  if (S.length === 0) {
207
- c.push({ role: "user", content: M });
217
+ u.push({ role: "user", content: M });
208
218
  continue;
209
219
  }
210
- const D = S.map((J) => ({
220
+ const L = S.map((j) => ({
211
221
  type: "file",
212
- url: J.url,
213
- mediaType: J.mediaType,
214
- filename: J.filename
222
+ url: j.url,
223
+ mediaType: j.mediaType,
224
+ filename: j.filename
215
225
  }));
216
- M && D.push({ type: "text", text: M }), c.push({ role: "user", content: D });
217
- } else if (v.role === "assistant") {
218
- c.push({
226
+ M && L.push({ type: "text", text: M }), u.push({ role: "user", content: L });
227
+ } else if (h.role === "assistant") {
228
+ u.push({
219
229
  role: "assistant",
220
230
  content: M,
221
- toolCalls: O.length > 0 ? O : void 0
231
+ toolCalls: x.length > 0 ? x : void 0
222
232
  });
223
- for (const S of v.parts)
224
- S.type === "tool-call" && c.push({
233
+ for (const S of h.parts)
234
+ S.type === "tool-call" && u.push({
225
235
  role: "tool",
226
236
  content: S.state === "result" ? JSON.stringify(S.result ?? null) : JSON.stringify({ error: S.error ?? "The tool call was not answered" }),
227
237
  toolCallId: S.toolCallId
228
238
  });
229
239
  }
230
240
  }
231
- return c;
232
- }, b = t.tools?.map(({ name: c, description: y, parameters: v }) => ({ name: c, description: y, parameters: v })), h = (c, { signal: y }) => {
233
- const v = t.tools?.find((x) => x.name === c.toolName);
234
- return v?.execute ? v.execute(c.args, { toolCallId: c.toolCallId, signal: y }) : t.onToolCall?.(c);
235
- }, C = () => {
236
- const c = e.value[e.value.length - 1];
237
- return c?.role === "assistant" && c.parts.some(
238
- (y) => y.type === "tool-call" && y.state !== "result" && y.state !== "error"
241
+ return u;
242
+ }, C = t.tools?.map(({ name: u, description: p, parameters: h }) => ({ name: u, description: p, parameters: h })), d = (u, { signal: p }) => {
243
+ const h = t.tools?.find((b) => b.name === u.toolName);
244
+ return h?.execute ? h.execute(u.args, { toolCallId: u.toolCallId, signal: p }) : t.onToolCall?.(u);
245
+ }, g = () => {
246
+ const u = a.value[a.value.length - 1];
247
+ return u?.role === "assistant" && u.parts.some(
248
+ (p) => p.type === "tool-call" && p.state !== "result" && p.state !== "error"
239
249
  );
240
- }, m = async (c) => {
241
- const y = t.maxToolRoundtrips ?? 5;
242
- let v = 0;
243
- for (; v < y; ) {
244
- const x = u();
250
+ }, O = async (u, p) => {
251
+ const h = t.maxToolRoundtrips ?? 5;
252
+ let b = 0;
253
+ for (; b < h; ) {
254
+ const M = m();
245
255
  t.provider.validateConfig();
246
- const { stream: M } = await W(t.provider, {
256
+ const { stream: x } = await U(t.provider, {
247
257
  model: P(t.model),
248
- messages: x,
249
- tools: b,
250
- signal: c,
258
+ messages: M,
259
+ tools: C,
260
+ signal: u,
251
261
  providerOptions: {
252
262
  ...t.providerOptions,
253
263
  ...P(t.body)
254
- }
255
- }, r), O = await d.processStream(M, c, {
264
+ },
265
+ // Later roundtrips of the same run send tool results back
266
+ ...b === 0 ? p : { trigger: "continue" }
267
+ }, o), S = await T.processStream(x, u, {
256
268
  onFinish: t.onFinish,
257
269
  onError: t.onError,
258
- onToolCall: h,
259
- canHandleTool: (S) => !!t.onToolCall || !!t.tools?.find((D) => D.name === S)?.execute
270
+ onToolCall: d,
271
+ canHandleTool: (L) => !!t.onToolCall || !!t.tools?.find((j) => j.name === L)?.execute
260
272
  });
261
- if (O.finishReason !== "tool-calls" || O.toolCalls.length === 0 || C())
273
+ if (S.finishReason !== "tool-calls" || S.toolCalls.length === 0 || g())
262
274
  break;
263
- v++;
275
+ b++;
264
276
  }
265
- }, w = async (c) => {
266
- const y = typeof c == "string" ? c : c?.text ?? s.value;
267
- let v;
277
+ }, I = async (u) => {
278
+ const p = typeof u == "string" ? u : u?.text ?? l.value;
279
+ let h;
268
280
  try {
269
- v = typeof c == "object" ? await ee(c.files) : [];
270
- } catch (O) {
271
- a.value = O instanceof Error ? O : new Error(String(O)), o.value = "error";
281
+ h = typeof u == "object" ? await oe(u.files) : [];
282
+ } catch (x) {
283
+ n.value = x instanceof Error ? x : new Error(String(x)), s.value = "error";
272
284
  return;
273
285
  }
274
- if (!y.trim() && v.length === 0) return;
275
- (typeof c != "object" || c === null || !("text" in c)) && (s.value = "");
276
- const x = [...v];
277
- y.trim() && x.push({ type: "text", text: y, state: "done" });
286
+ if (!p.trim() && h.length === 0) return;
287
+ (typeof u != "object" || u === null || !("text" in u)) && (l.value = "");
288
+ const b = [...h];
289
+ p.trim() && b.push({ type: "text", text: p, state: "done" });
278
290
  const M = {
279
- id: j(),
291
+ id: J(),
280
292
  role: "user",
281
- parts: x,
293
+ parts: b,
282
294
  createdAt: /* @__PURE__ */ new Date()
283
295
  };
284
- e.value = [...e.value, M], await i();
285
- }, i = async () => {
286
- a.value = void 0, o.value = "submitted", n?.abort();
287
- const c = new AbortController();
288
- n = c;
289
- const y = () => n === c || n === null;
296
+ a.value = [...a.value, M], await A({ trigger: "submit", messageId: M.id });
297
+ }, A = async (u) => {
298
+ n.value = void 0, s.value = "submitted", c?.abort();
299
+ const p = new AbortController();
300
+ c = p;
301
+ const h = () => c === p || c === null;
290
302
  try {
291
- await m(c.signal), y() && (o.value = "ready");
292
- } catch (v) {
293
- if (c.signal.aborted) {
294
- y() && (o.value = "ready");
303
+ await O(p.signal, u), h() && (s.value = "ready");
304
+ } catch (b) {
305
+ if (p.signal.aborted) {
306
+ h() && (s.value = "ready");
295
307
  return;
296
308
  }
297
- a.value = v instanceof Error ? v : new Error(String(v)), o.value = "error";
309
+ n.value = b instanceof Error ? b : new Error(String(b)), s.value = "error";
298
310
  }
299
- }, g = async () => {
300
- const c = e.value, y = c.reduce((v, x, M) => x.role === "assistant" ? M : v, -1);
301
- y !== -1 && (e.value = c.slice(0, y), await i());
302
- }, A = () => {
303
- n?.abort(), n = null, o.value = "ready";
304
- }, E = (c) => {
305
- e.value = typeof c == "function" ? c(e.value) : c;
306
- }, I = (c) => {
307
- c?.preventDefault?.(), s.value.trim() && w();
308
- }, N = F(() => o.value === "submitted" || o.value === "streaming"), T = async ({ toolCallId: c, result: y, error: v }) => {
309
- const x = e.value.flatMap((D) => D.parts).find((D) => D.type === "tool-call" && D.toolCallId === c);
310
- if (!x) return;
311
- v !== void 0 ? (x.state = "error", x.error = v) : (x.state = "result", x.result = y), e.value = [...e.value];
312
- const M = e.value[e.value.length - 1], O = M?.role === "assistant" && M.parts.includes(x), S = o.value === "submitted" || o.value === "streaming";
313
- (t.autoSendToolResults ?? !0) && O && !S && !C() && await i();
314
- }, L = () => {
315
- a.value = void 0, o.value === "error" && (o.value = "ready");
316
- }, q = k({
317
- id: r,
311
+ }, N = async () => {
312
+ const u = a.value, p = u.reduce((b, M, x) => M.role === "assistant" ? x : b, -1);
313
+ if (p === -1) return;
314
+ const h = u[p].id;
315
+ a.value = u.slice(0, p), await A({ trigger: "regenerate", messageId: h });
316
+ }, R = () => {
317
+ c?.abort(), c = null, s.value = "ready";
318
+ }, D = (u) => {
319
+ a.value = typeof u == "function" ? u(a.value) : u;
320
+ }, q = (u) => {
321
+ u?.preventDefault?.(), l.value.trim() && I();
322
+ }, H = F(() => s.value === "submitted" || s.value === "streaming"), ee = async ({ toolCallId: u, result: p, error: h }) => {
323
+ const b = a.value.flatMap((L) => L.parts).find((L) => L.type === "tool-call" && L.toolCallId === u);
324
+ if (!b) return;
325
+ h !== void 0 ? (b.state = "error", b.error = h) : (b.state = "result", b.result = p), a.value = [...a.value];
326
+ const M = a.value[a.value.length - 1], x = M?.role === "assistant" && M.parts.includes(b), S = s.value === "submitted" || s.value === "streaming";
327
+ (t.autoSendToolResults ?? !0) && x && !S && !g() && await A({ trigger: "continue" });
328
+ }, te = () => {
329
+ n.value = void 0, s.value === "error" && (s.value = "ready");
330
+ };
331
+ z(e, (u, p) => {
332
+ u !== p && (R(), n.value = void 0, a.value = [], y(u));
333
+ });
334
+ const re = W({
335
+ id: o,
318
336
  kind: "chat",
319
337
  providerId: t.provider.id,
320
338
  getModel: () => P(t.model),
321
- status: o,
322
- error: a,
323
- messages: e,
324
- stop: A,
325
- clear: () => E([])
339
+ status: s,
340
+ error: n,
341
+ messages: a,
342
+ stop: R,
343
+ clear: () => D([])
326
344
  });
327
- return H() && B(() => {
328
- n?.abort(), q();
345
+ return k() && B(() => {
346
+ c?.abort(), re();
329
347
  }), {
330
- messages: e,
331
- status: F(() => o.value),
332
- error: a,
333
- id: r,
334
- input: s,
335
- sendMessage: w,
336
- regenerate: g,
337
- stop: A,
338
- setMessages: E,
339
- handleSubmit: I,
340
- isLoading: N,
341
- addToolResult: T,
342
- clearError: L,
343
- isRestored: F(() => l.value)
348
+ messages: a,
349
+ status: F(() => s.value),
350
+ error: n,
351
+ get id() {
352
+ return e();
353
+ },
354
+ input: l,
355
+ sendMessage: I,
356
+ regenerate: N,
357
+ stop: R,
358
+ setMessages: D,
359
+ handleSubmit: q,
360
+ isLoading: H,
361
+ addToolResult: ee,
362
+ clearError: te,
363
+ isRestored: F(() => v.value)
344
364
  };
345
365
  }
346
- function de(t) {
347
- const r = t.id ?? j(), e = R(""), o = R("ready"), a = R(void 0), s = R("");
366
+ function pe(t) {
367
+ const r = t.id ?? J(), e = E(""), o = E("ready"), a = E(void 0), s = E("");
348
368
  let n = null;
349
- const l = async (h) => {
350
- const C = h ?? s.value;
351
- if (!C.trim()) return e.value;
352
- h || (s.value = ""), e.value = "", a.value = void 0, o.value = "submitted", n?.abort();
369
+ const l = async (y) => {
370
+ const T = y ?? s.value;
371
+ if (!T.trim()) return e.value;
372
+ y || (s.value = ""), e.value = "", a.value = void 0, o.value = "submitted", n?.abort();
353
373
  const m = new AbortController();
354
374
  n = m;
355
375
  try {
356
376
  t.provider.validateConfig();
357
- const w = P(t.system), i = w ? [
358
- { role: "system", content: w },
359
- { role: "user", content: C }
360
- ] : [{ role: "user", content: C }], { stream: g } = await W(t.provider, {
377
+ const C = P(t.system), d = C ? [
378
+ { role: "system", content: C },
379
+ { role: "user", content: T }
380
+ ] : [{ role: "user", content: T }], { stream: g } = await U(t.provider, {
361
381
  model: P(t.model),
362
- messages: i,
382
+ messages: d,
363
383
  signal: m.signal
364
384
  }, r);
365
385
  if (m.signal.aborted) return e.value;
366
386
  o.value = "streaming";
367
- const A = g.getReader();
368
- let E = "stop", I;
387
+ const O = g.getReader();
388
+ let I = "stop", A;
369
389
  for (; !m.signal.aborted; ) {
370
- const { done: N, value: T } = await A.read();
390
+ const { done: N, value: R } = await O.read();
371
391
  if (m.signal.aborted || N) break;
372
- if (T.type === "text-delta")
373
- e.value += T.text;
374
- else if (T.type === "finish")
375
- E = T.finishReason, I = T.usage;
376
- else if (T.type === "error")
377
- throw T.error;
392
+ if (R.type === "text-delta")
393
+ e.value += R.text;
394
+ else if (R.type === "finish")
395
+ I = R.finishReason, A = R.usage;
396
+ else if (R.type === "error")
397
+ throw R.error;
378
398
  }
379
- return A.releaseLock(), m.signal.aborted || (o.value = "ready", t.onFinish?.(e.value, { finishReason: E, usage: I })), e.value;
380
- } catch (w) {
399
+ return O.releaseLock(), m.signal.aborted || (o.value = "ready", t.onFinish?.(e.value, { finishReason: I, usage: A })), e.value;
400
+ } catch (C) {
381
401
  if (m.signal.aborted) return e.value;
382
- const i = w instanceof Error ? w : new Error(String(w));
383
- return a.value = i, o.value = "error", t.onError?.(i), e.value;
402
+ const d = C instanceof Error ? C : new Error(String(C));
403
+ return a.value = d, o.value = "error", t.onError?.(d), e.value;
384
404
  } finally {
385
405
  n === m && (n = null);
386
406
  }
387
- }, f = () => {
407
+ }, c = () => {
388
408
  n?.abort(), n = null, o.value = "ready";
389
- }, p = () => {
409
+ }, v = () => {
390
410
  a.value = void 0, o.value === "error" && (o.value = "ready");
391
- }, d = (h) => {
392
- h?.preventDefault?.(), s.value.trim() && l();
393
- }, u = F(() => o.value === "submitted" || o.value === "streaming"), b = k({
411
+ }, f = (y) => {
412
+ y?.preventDefault?.(), s.value.trim() && l();
413
+ }, i = F(() => o.value === "submitted" || o.value === "streaming"), w = W({
394
414
  id: r,
395
415
  kind: "completion",
396
416
  providerId: t.provider.id,
@@ -398,46 +418,46 @@ function de(t) {
398
418
  status: o,
399
419
  error: a,
400
420
  output: e,
401
- stop: f
421
+ stop: c
402
422
  });
403
- return H() && B(() => {
404
- n?.abort(), b();
423
+ return k() && B(() => {
424
+ n?.abort(), w();
405
425
  }), {
406
426
  completion: e,
407
427
  status: F(() => o.value),
408
428
  error: a,
409
- isLoading: u,
429
+ isLoading: i,
410
430
  id: r,
411
431
  input: s,
412
432
  complete: l,
413
- stop: f,
414
- clearError: p,
415
- handleSubmit: d
433
+ stop: c,
434
+ clearError: v,
435
+ handleSubmit: f
416
436
  };
417
437
  }
418
- function ve(t) {
419
- const r = t.id ?? j(), e = R(void 0), o = R(void 0), a = R("ready"), s = R(void 0), n = R("");
438
+ function he(t) {
439
+ const r = t.id ?? J(), e = E(void 0), o = E(void 0), a = E("ready"), s = E(void 0), n = E("");
420
440
  let l = null;
421
- const f = async (h) => {
422
- const C = h ?? n.value;
423
- if (!C.trim()) return e.value;
424
- h || (n.value = ""), e.value = void 0, o.value = void 0, s.value = void 0, a.value = "submitted", l?.abort();
441
+ const c = async (y) => {
442
+ const T = y ?? n.value;
443
+ if (!T.trim()) return e.value;
444
+ y || (n.value = ""), e.value = void 0, o.value = void 0, s.value = void 0, a.value = "submitted", l?.abort();
425
445
  const m = new AbortController();
426
446
  l = m;
427
447
  try {
428
448
  t.provider.validateConfig();
429
- const w = [
449
+ const C = [
430
450
  P(t.system) ?? "",
431
451
  "Respond with valid JSON matching this schema:",
432
452
  JSON.stringify(t.schema, null, 2),
433
453
  "Output ONLY the JSON object, no other text."
434
454
  ].filter(Boolean).join(`
435
455
 
436
- `), { stream: i } = await W(t.provider, {
456
+ `), { stream: d } = await U(t.provider, {
437
457
  model: P(t.model),
438
458
  messages: [
439
- { role: "system", content: w },
440
- { role: "user", content: C }
459
+ { role: "system", content: C },
460
+ { role: "user", content: T }
441
461
  ],
442
462
  signal: m.signal,
443
463
  providerOptions: {
@@ -446,40 +466,40 @@ function ve(t) {
446
466
  }, r);
447
467
  if (m.signal.aborted) return e.value;
448
468
  a.value = "streaming";
449
- const g = i.getReader();
450
- let A = "", E = "stop", I;
469
+ const g = d.getReader();
470
+ let O = "", I = "stop", A;
451
471
  for (; !m.signal.aborted; ) {
452
- const { done: N, value: T } = await g.read();
472
+ const { done: N, value: R } = await g.read();
453
473
  if (m.signal.aborted || N) break;
454
- if (T.type === "text-delta") {
455
- A += T.text;
456
- const L = K(A);
457
- L !== null && typeof L == "object" && (o.value = L);
458
- } else if (T.type === "finish")
459
- E = T.finishReason, I = T.usage;
460
- else if (T.type === "error")
461
- throw T.error;
474
+ if (R.type === "text-delta") {
475
+ O += R.text;
476
+ const D = Y(O);
477
+ D !== null && typeof D == "object" && (o.value = D);
478
+ } else if (R.type === "finish")
479
+ I = R.finishReason, A = R.usage;
480
+ else if (R.type === "error")
481
+ throw R.error;
462
482
  }
463
483
  if (g.releaseLock(), m.signal.aborted) return e.value;
464
484
  try {
465
- e.value = JSON.parse(A), o.value = e.value;
485
+ e.value = JSON.parse(O), o.value = e.value;
466
486
  } catch {
467
- throw new Error(`Failed to parse JSON response: ${A.slice(0, 200)}`);
487
+ throw new Error(`Failed to parse JSON response: ${O.slice(0, 200)}`);
468
488
  }
469
- return a.value = "ready", t.onFinish?.(e.value, { finishReason: E, usage: I }), e.value;
470
- } catch (w) {
489
+ return a.value = "ready", t.onFinish?.(e.value, { finishReason: I, usage: A }), e.value;
490
+ } catch (C) {
471
491
  if (m.signal.aborted) return e.value;
472
- const i = w instanceof Error ? w : new Error(String(w));
473
- s.value = i, a.value = "error", t.onError?.(i);
492
+ const d = C instanceof Error ? C : new Error(String(C));
493
+ s.value = d, a.value = "error", t.onError?.(d);
474
494
  return;
475
495
  } finally {
476
496
  l === m && (l = null);
477
497
  }
478
- }, p = () => {
498
+ }, v = () => {
479
499
  l?.abort(), l = null, a.value = "ready";
480
- }, d = () => {
500
+ }, f = () => {
481
501
  s.value = void 0, a.value === "error" && (a.value = "ready");
482
- }, u = F(() => a.value === "submitted" || a.value === "streaming"), b = k({
502
+ }, i = F(() => a.value === "submitted" || a.value === "streaming"), w = W({
483
503
  id: r,
484
504
  kind: "object",
485
505
  providerId: t.provider.id,
@@ -487,45 +507,45 @@ function ve(t) {
487
507
  status: a,
488
508
  error: s,
489
509
  output: o,
490
- stop: p
510
+ stop: v
491
511
  });
492
- return H() && B(() => {
493
- l?.abort(), b();
512
+ return k() && B(() => {
513
+ l?.abort(), w();
494
514
  }), {
495
515
  object: e,
496
516
  partialObject: o,
497
517
  status: F(() => a.value),
498
518
  error: s,
499
- isLoading: u,
519
+ isLoading: i,
500
520
  id: r,
501
521
  input: n,
502
- submit: f,
503
- stop: p,
504
- clearError: d
522
+ submit: c,
523
+ stop: v,
524
+ clearError: f
505
525
  };
506
526
  }
507
- const Y = /(\\)?\{\{\s*([A-Za-z_][\w-]*)\s*\}\}/g;
508
- function re(t, r = {}) {
527
+ const Z = /(\\)?\{\{\s*([A-Za-z_][\w-]*)\s*\}\}/g;
528
+ function ne(t, r = {}) {
509
529
  return {
510
530
  template: t,
511
- variables: G(t),
531
+ variables: Q(t),
512
532
  render(e) {
513
- return Z(t, { ...r.defaults, ...e });
533
+ return G(t, { ...r.defaults, ...e });
514
534
  }
515
535
  };
516
536
  }
517
- function me(t, r = {}) {
537
+ function ye(t, r = {}) {
518
538
  const e = (o) => {
519
539
  const a = { ...r.defaults, ...o };
520
- return t.map((s) => ({ role: s.role, content: Z(s.content, a) }));
540
+ return t.map((s) => ({ role: s.role, content: G(s.content, a) }));
521
541
  };
522
542
  return {
523
543
  messages: t,
524
- variables: [...new Set(t.flatMap((o) => G(o.content)))],
544
+ variables: [...new Set(t.flatMap((o) => Q(o.content)))],
525
545
  render: e,
526
546
  toUIMessages(o) {
527
547
  return e(o).map((a) => ({
528
- id: j(),
548
+ id: J(),
529
549
  role: a.role,
530
550
  parts: [{ type: "text", text: a.content, state: "done" }],
531
551
  createdAt: /* @__PURE__ */ new Date()
@@ -533,8 +553,8 @@ function me(t, r = {}) {
533
553
  }
534
554
  };
535
555
  }
536
- function Z(t, r) {
537
- return t.replace(Y, (e, o, a) => {
556
+ function G(t, r) {
557
+ return t.replace(Z, (e, o, a) => {
538
558
  if (o) return e.slice(1);
539
559
  const s = r[a];
540
560
  if (s == null)
@@ -542,109 +562,109 @@ function Z(t, r) {
542
562
  return String(s);
543
563
  });
544
564
  }
545
- function G(t) {
565
+ function Q(t) {
546
566
  const r = /* @__PURE__ */ new Set();
547
- for (const [, e, o] of t.matchAll(Y))
567
+ for (const [, e, o] of t.matchAll(Z))
548
568
  e || r.add(o);
549
569
  return [...r];
550
570
  }
551
- function pe(t, r) {
552
- const e = typeof t == "string" ? re(t) : t;
571
+ function be(t, r) {
572
+ const e = typeof t == "string" ? ne(t) : t;
553
573
  return F(() => e.render(P(r)));
554
574
  }
555
- function ge(t, r = {}) {
556
- const e = R(null), o = R([]), a = R(!1);
575
+ function we(t, r = {}) {
576
+ const e = E(null), o = E([]), a = E(!1);
557
577
  let s = 0;
558
- const n = F(() => t.status.value === "submitted" || t.status.value === "streaming"), l = F(() => !n.value && (t.input.value.trim() !== "" || o.value.length > 0)), f = (i) => r.accept ? r.accept.split(",").map((g) => g.trim().toLowerCase()).filter(Boolean).some((g) => g.startsWith(".") ? i.name.toLowerCase().endsWith(g) : g.endsWith("/*") ? i.type.toLowerCase().startsWith(g.slice(0, -1)) : i.type.toLowerCase() === g) : !0, p = (i) => {
559
- const g = Array.from(i ?? []);
578
+ const n = F(() => t.status.value === "submitted" || t.status.value === "streaming"), l = F(() => !n.value && (t.input.value.trim() !== "" || o.value.length > 0)), c = (d) => r.accept ? r.accept.split(",").map((g) => g.trim().toLowerCase()).filter(Boolean).some((g) => g.startsWith(".") ? d.name.toLowerCase().endsWith(g) : g.endsWith("/*") ? d.type.toLowerCase().startsWith(g.slice(0, -1)) : d.type.toLowerCase() === g) : !0, v = (d) => {
579
+ const g = Array.from(d ?? []);
560
580
  if (!g.length) return;
561
- const A = g.filter((I) => !f(I));
562
- A.length && r.onReject?.(A, "type");
563
- let E = g.filter(f);
581
+ const O = g.filter((A) => !c(A));
582
+ O.length && r.onReject?.(O, "type");
583
+ let I = g.filter(c);
564
584
  if (r.maxFiles !== void 0) {
565
- const I = Math.max(0, r.maxFiles - o.value.length);
566
- E.length > I && r.onReject?.(E.slice(I), "count"), E = E.slice(0, I);
585
+ const A = Math.max(0, r.maxFiles - o.value.length);
586
+ I.length > A && r.onReject?.(I.slice(A), "count"), I = I.slice(0, A);
567
587
  }
568
- E.length && (o.value = [...o.value, ...E]);
569
- }, d = (i) => {
570
- o.value = o.value.filter((g) => g !== i);
571
- }, u = () => {
572
- const i = e.value;
573
- if (!i || typeof getComputedStyle > "u") return;
574
- const g = getComputedStyle(i), A = parseFloat(g.lineHeight) || parseFloat(g.fontSize) * 1.5 || 20, E = (parseFloat(g.paddingTop) || 0) + (parseFloat(g.paddingBottom) || 0), I = (parseFloat(g.borderTopWidth) || 0) + (parseFloat(g.borderBottomWidth) || 0), N = g.boxSizing === "border-box", T = (c) => N ? c + I : c - E, L = A * (r.maxRows ?? 8) + E;
575
- i.style.height = "auto";
576
- const q = i.scrollHeight > L;
577
- i.style.height = `${T(Math.min(i.scrollHeight, L))}px`, i.style.overflowY = q ? "auto" : "hidden";
588
+ I.length && (o.value = [...o.value, ...I]);
589
+ }, f = (d) => {
590
+ o.value = o.value.filter((g) => g !== d);
591
+ }, i = () => {
592
+ const d = e.value;
593
+ if (!d || typeof getComputedStyle > "u") return;
594
+ const g = getComputedStyle(d), O = parseFloat(g.lineHeight) || parseFloat(g.fontSize) * 1.5 || 20, I = (parseFloat(g.paddingTop) || 0) + (parseFloat(g.paddingBottom) || 0), A = (parseFloat(g.borderTopWidth) || 0) + (parseFloat(g.borderBottomWidth) || 0), N = g.boxSizing === "border-box", R = (H) => N ? H + A : H - I, D = O * (r.maxRows ?? 8) + I;
595
+ d.style.height = "auto";
596
+ const q = d.scrollHeight > D;
597
+ d.style.height = `${R(Math.min(d.scrollHeight, D))}px`, d.style.overflowY = q ? "auto" : "hidden";
578
598
  };
579
- z(t.input, () => U(u)), z(e, () => U(u));
580
- const b = async () => {
599
+ z(t.input, () => V(i)), z(e, () => V(i));
600
+ const w = async () => {
581
601
  if (!l.value) return;
582
- const i = t.input.value, g = o.value;
583
- t.input.value = "", o.value = [], await t.sendMessage(g.length ? { text: i, files: g } : i);
584
- }, h = (i) => {
585
- i.key !== "Enter" || i.shiftKey || r.submitOnEnter === !1 || i.isComposing || i.keyCode === 229 || (i.preventDefault(), b());
586
- }, C = (i) => {
587
- const g = Array.from(i.clipboardData?.files ?? []);
588
- g.length && (i.preventDefault(), p(g));
589
- }, m = (i) => Array.from(i.dataTransfer?.types ?? []).includes("Files");
602
+ const d = t.input.value, g = o.value;
603
+ t.input.value = "", o.value = [], await t.sendMessage(g.length ? { text: d, files: g } : d);
604
+ }, y = (d) => {
605
+ d.key !== "Enter" || d.shiftKey || r.submitOnEnter === !1 || d.isComposing || d.keyCode === 229 || (d.preventDefault(), w());
606
+ }, T = (d) => {
607
+ const g = Array.from(d.clipboardData?.files ?? []);
608
+ g.length && (d.preventDefault(), v(g));
609
+ }, m = (d) => Array.from(d.dataTransfer?.types ?? []).includes("Files");
590
610
  return {
591
611
  textarea: e,
592
612
  files: o,
593
613
  isDragging: a,
594
614
  canSubmit: l,
595
- submit: b,
596
- addFiles: p,
597
- removeFile: d,
598
- handlers: { onKeydown: h, onPaste: C },
615
+ submit: w,
616
+ addFiles: v,
617
+ removeFile: f,
618
+ handlers: { onKeydown: y, onPaste: T },
599
619
  dropzone: {
600
- onDragenter(i) {
601
- m(i) && (i.preventDefault(), s++, a.value = !0);
620
+ onDragenter(d) {
621
+ m(d) && (d.preventDefault(), s++, a.value = !0);
602
622
  },
603
- onDragover(i) {
604
- m(i) && i.preventDefault();
623
+ onDragover(d) {
624
+ m(d) && d.preventDefault();
605
625
  },
606
- onDragleave(i) {
607
- m(i) && (s = Math.max(0, s - 1), s === 0 && (a.value = !1));
626
+ onDragleave(d) {
627
+ m(d) && (s = Math.max(0, s - 1), s === 0 && (a.value = !1));
608
628
  },
609
- onDrop(i) {
610
- m(i) && (i.preventDefault(), s = 0, a.value = !1, p(i.dataTransfer?.files));
629
+ onDrop(d) {
630
+ m(d) && (d.preventDefault(), s = 0, a.value = !1, v(d.dataTransfer?.files));
611
631
  }
612
632
  },
613
- resize: u
633
+ resize: i
614
634
  };
615
635
  }
616
- function he(t = {}) {
617
- const r = R(null), e = R(!0), o = t.threshold ?? 48;
636
+ function Ce(t = {}) {
637
+ const r = E(null), e = E(!0), o = t.threshold ?? 48;
618
638
  let a = !1;
619
- const s = (d) => d.scrollHeight - d.scrollTop - d.clientHeight, n = (d = "auto") => {
620
- const u = r.value;
621
- u && (a = !0, e.value = !0, typeof u.scrollTo == "function" ? u.scrollTo({ top: u.scrollHeight, behavior: d }) : u.scrollTop = u.scrollHeight, oe(() => {
639
+ const s = (f) => f.scrollHeight - f.scrollTop - f.clientHeight, n = (f = "auto") => {
640
+ const i = r.value;
641
+ i && (a = !0, e.value = !0, typeof i.scrollTo == "function" ? i.scrollTo({ top: i.scrollHeight, behavior: f }) : i.scrollTop = i.scrollHeight, le(() => {
622
642
  a = !1;
623
643
  }));
624
644
  }, l = () => {
625
- const d = r.value;
626
- !d || a || (e.value = s(d) <= o);
627
- }, f = () => {
645
+ const f = r.value;
646
+ !f || a || (e.value = s(f) <= o);
647
+ }, c = () => {
628
648
  e.value && n();
629
649
  };
630
- let p = () => {
650
+ let v = () => {
631
651
  };
632
- return z(r, (d) => {
633
- if (p(), !d) return;
634
- d.addEventListener("scroll", l, { passive: !0 });
635
- const u = typeof MutationObserver < "u" ? new MutationObserver(f) : void 0;
636
- u?.observe(d, { childList: !0, subtree: !0, characterData: !0 });
637
- const b = typeof ResizeObserver < "u" ? new ResizeObserver(f) : void 0;
638
- b?.observe(d), d.firstElementChild && b?.observe(d.firstElementChild), n(), p = () => {
639
- d.removeEventListener("scroll", l), u?.disconnect(), b?.disconnect(), p = () => {
652
+ return z(r, (f) => {
653
+ if (v(), !f) return;
654
+ f.addEventListener("scroll", l, { passive: !0 });
655
+ const i = typeof MutationObserver < "u" ? new MutationObserver(c) : void 0;
656
+ i?.observe(f, { childList: !0, subtree: !0, characterData: !0 });
657
+ const w = typeof ResizeObserver < "u" ? new ResizeObserver(c) : void 0;
658
+ w?.observe(f), f.firstElementChild && w?.observe(f.firstElementChild), n(), v = () => {
659
+ f.removeEventListener("scroll", l), i?.disconnect(), w?.disconnect(), v = () => {
640
660
  };
641
661
  };
642
- }, { flush: "post" }), H() && B(() => p()), { container: r, isAtBottom: e, scrollToBottom: n };
662
+ }, { flush: "post" }), k() && B(() => v()), { container: r, isAtBottom: e, scrollToBottom: n };
643
663
  }
644
- function oe(t) {
664
+ function le(t) {
645
665
  typeof requestAnimationFrame == "function" ? requestAnimationFrame(() => requestAnimationFrame(t)) : setTimeout(t, 32);
646
666
  }
647
- function ye(t, r) {
667
+ function xe(t, r) {
648
668
  return r.length ? {
649
669
  id: t.id,
650
670
  supportsStreaming: t.supportsStreaming,
@@ -653,41 +673,41 @@ function ye(t, r) {
653
673
  },
654
674
  async chat(e) {
655
675
  let o = e;
656
- for (const f of r)
657
- f.beforeRequest && (o = await f.beforeRequest(o, t));
676
+ for (const c of r)
677
+ c.beforeRequest && (o = await c.beforeRequest(o, t));
658
678
  const a = r.reduceRight(
659
- (f, p) => p.wrapChat ? (d) => p.wrapChat(f, d) : f,
660
- (f) => t.chat(f)
679
+ (c, v) => v.wrapChat ? (f) => v.wrapChat(c, f) : c,
680
+ (c) => t.chat(c)
661
681
  );
662
682
  let s;
663
683
  try {
664
684
  s = await a(o);
665
- } catch (f) {
666
- let p = f instanceof Error ? f : new Error(String(f));
667
- for (const d of r)
668
- if (d.onError) {
669
- const u = await d.onError(p, o);
670
- u instanceof Error && (p = u);
685
+ } catch (c) {
686
+ let v = c instanceof Error ? c : new Error(String(c));
687
+ for (const f of r)
688
+ if (f.onError) {
689
+ const i = await f.onError(v, o);
690
+ i instanceof Error && (v = i);
671
691
  }
672
- throw p;
692
+ throw v;
673
693
  }
674
- const l = r.some((f) => f.afterStream) ? s.stream.pipeThrough(new TransformStream({
675
- async transform(f, p) {
676
- let d = f;
677
- for (const u of r) {
678
- if (!d) break;
679
- u.afterStream && (d = await u.afterStream(d, o));
694
+ const l = r.some((c) => c.afterStream) ? s.stream.pipeThrough(new TransformStream({
695
+ async transform(c, v) {
696
+ let f = c;
697
+ for (const i of r) {
698
+ if (!f) break;
699
+ i.afterStream && (f = await i.afterStream(f, o));
680
700
  }
681
- d && p.enqueue(d);
701
+ f && v.enqueue(f);
682
702
  }
683
703
  })) : s.stream;
684
- for (const f of r)
685
- f.onFinish && f.onFinish(o, s);
704
+ for (const c of r)
705
+ c.onFinish && c.onFinish(o, s);
686
706
  return { stream: l, response: s.response };
687
707
  }
688
708
  } : t;
689
709
  }
690
- function be(t = "vue-llm") {
710
+ function Se(t = "vue-llm") {
691
711
  return {
692
712
  beforeRequest(r) {
693
713
  return console.log(`[${t}] → ${r.model}`, r.messages.length, "messages"), r;
@@ -700,30 +720,30 @@ function be(t = "vue-llm") {
700
720
  }
701
721
  };
702
722
  }
703
- function we(t = {}, r) {
704
- const e = typeof t == "number" ? { maxRetries: t, initialDelay: r } : t, o = e.maxRetries ?? 3, a = e.initialDelay ?? 1e3, s = e.maxDelay ?? 3e4, n = e.factor ?? 2, l = e.jitter ?? !0, f = e.shouldRetry ?? ae;
723
+ function Te(t = {}, r) {
724
+ const e = typeof t == "number" ? { maxRetries: t, initialDelay: r } : t, o = e.maxRetries ?? 3, a = e.initialDelay ?? 1e3, s = e.maxDelay ?? 3e4, n = e.factor ?? 2, l = e.jitter ?? !0, c = e.shouldRetry ?? ie;
705
725
  return {
706
- async wrapChat(p, d) {
707
- for (let u = 1; ; u++)
726
+ async wrapChat(v, f) {
727
+ for (let i = 1; ; i++)
708
728
  try {
709
- return await p(d);
710
- } catch (b) {
711
- const h = b instanceof Error ? b : new Error(String(b));
712
- if (u > o || d.signal?.aborted || !f(h, u))
713
- throw h;
714
- const C = a * n ** (u - 1), m = h instanceof _ ? h.retryAfter : void 0, w = Math.min(
729
+ return await v(f);
730
+ } catch (w) {
731
+ const y = w instanceof Error ? w : new Error(String(w));
732
+ if (i > o || f.signal?.aborted || !c(y, i))
733
+ throw y;
734
+ const T = a * n ** (i - 1), m = y instanceof K ? y.retryAfter : void 0, C = Math.min(
715
735
  s,
716
- m ?? (l ? C * (0.5 + Math.random() * 0.5) : C)
736
+ m ?? (l ? T * (0.5 + Math.random() * 0.5) : T)
717
737
  );
718
- e.onRetry?.({ error: h, attempt: u, delay: w }), await ne(w, d.signal);
738
+ e.onRetry?.({ error: y, attempt: i, delay: C }), await ce(C, f.signal);
719
739
  }
720
740
  }
721
741
  };
722
742
  }
723
- function ae(t) {
724
- return t.name === "AbortError" ? !1 : t instanceof _ ? t.isRetryable : t instanceof TypeError;
743
+ function ie(t) {
744
+ return t.name === "AbortError" ? !1 : t instanceof K ? t.isRetryable : t instanceof TypeError;
725
745
  }
726
- function Ce(t) {
746
+ function Re(t) {
727
747
  const r = t.interval ?? 6e4, e = [];
728
748
  let o = 0;
729
749
  const a = /* @__PURE__ */ new Set(), s = () => {
@@ -732,37 +752,37 @@ function Ce(t) {
732
752
  }, n = async (l) => {
733
753
  for (; ; ) {
734
754
  l?.throwIfAborted();
735
- const f = Date.now();
736
- for (; e.length > 0 && e[0] <= f - r; ) e.shift();
737
- const p = t.maxRequests !== void 0 && e.length >= t.maxRequests, d = t.maxConcurrent !== void 0 && o >= t.maxConcurrent;
738
- if (!p && !d) {
739
- e.push(f), o++;
755
+ const c = Date.now();
756
+ for (; e.length > 0 && e[0] <= c - r; ) e.shift();
757
+ const v = t.maxRequests !== void 0 && e.length >= t.maxRequests, f = t.maxConcurrent !== void 0 && o >= t.maxConcurrent;
758
+ if (!v && !f) {
759
+ e.push(c), o++;
740
760
  return;
741
761
  }
742
- await new Promise((u, b) => {
743
- const h = () => {
744
- clearTimeout(m), a.delete(h), l?.removeEventListener("abort", C), u();
745
- }, C = () => {
746
- h(), b(l.reason);
747
- }, m = p ? setTimeout(h, e[0] + r - f) : void 0;
748
- a.add(h), l?.addEventListener("abort", C, { once: !0 });
762
+ await new Promise((i, w) => {
763
+ const y = () => {
764
+ clearTimeout(m), a.delete(y), l?.removeEventListener("abort", T), i();
765
+ }, T = () => {
766
+ y(), w(l.reason);
767
+ }, m = v ? setTimeout(y, e[0] + r - c) : void 0;
768
+ a.add(y), l?.addEventListener("abort", T, { once: !0 });
749
769
  });
750
770
  }
751
771
  };
752
772
  return {
753
- async wrapChat(l, f) {
754
- await n(f.signal);
755
- let p;
773
+ async wrapChat(l, c) {
774
+ await n(c.signal);
775
+ let v;
756
776
  try {
757
- p = await l(f);
758
- } catch (d) {
759
- throw s(), d;
777
+ v = await l(c);
778
+ } catch (f) {
779
+ throw s(), f;
760
780
  }
761
- return { ...p, stream: se(p.stream, s) };
781
+ return { ...v, stream: ue(v.stream, s) };
762
782
  }
763
783
  };
764
784
  }
765
- function se(t, r) {
785
+ function ue(t, r) {
766
786
  const e = t.getReader();
767
787
  let o = !1;
768
788
  const a = () => {
@@ -782,7 +802,7 @@ function se(t, r) {
782
802
  }
783
803
  });
784
804
  }
785
- function ne(t, r) {
805
+ function ce(t, r) {
786
806
  return new Promise((e, o) => {
787
807
  if (r?.aborted) return o(r.reason);
788
808
  const a = setTimeout(() => {
@@ -793,7 +813,7 @@ function ne(t, r) {
793
813
  r?.addEventListener("abort", s, { once: !0 });
794
814
  });
795
815
  }
796
- function xe(t) {
816
+ function Ee(t) {
797
817
  return {
798
818
  beforeRequest(r) {
799
819
  return {
@@ -806,17 +826,17 @@ function xe(t) {
806
826
  }
807
827
  };
808
828
  }
809
- function Se(t) {
829
+ function Ie(t) {
810
830
  return t;
811
831
  }
812
- function Te(t = {}) {
832
+ function Ae(t = {}) {
813
833
  const r = t.prefix ?? "vue-llm:chat:", e = () => t.storage ? t.storage : typeof localStorage < "u" ? localStorage : void 0;
814
834
  return {
815
835
  load(o) {
816
836
  const a = e()?.getItem(r + o);
817
837
  if (!a) return null;
818
838
  try {
819
- return Q(a);
839
+ return X(a);
820
840
  } catch {
821
841
  return null;
822
842
  }
@@ -829,12 +849,12 @@ function Te(t = {}) {
829
849
  }
830
850
  };
831
851
  }
832
- function Re() {
852
+ function Me() {
833
853
  const t = /* @__PURE__ */ new Map();
834
854
  return {
835
855
  load(r) {
836
856
  const e = t.get(r);
837
- return e ? Q(e) : null;
857
+ return e ? X(e) : null;
838
858
  },
839
859
  save(r, e) {
840
860
  t.set(r, JSON.stringify(e));
@@ -844,7 +864,7 @@ function Re() {
844
864
  }
845
865
  };
846
866
  }
847
- function Q(t) {
867
+ function X(t) {
848
868
  const r = JSON.parse(t);
849
869
  return Array.isArray(r) ? r.map((e) => ({
850
870
  ...e,
@@ -852,26 +872,26 @@ function Q(t) {
852
872
  })) : [];
853
873
  }
854
874
  export {
855
- _ as LLMAPIError,
856
- ee as convertFilesToParts,
857
- me as createChatPrompt,
858
- Re as createMemoryStorage,
859
- re as createPrompt,
860
- Te as createWebStorage,
861
- Se as defineTool,
862
- Ie as fileToDataURL,
863
- xe as headerMiddleware,
864
- ae as isRetryableError,
865
- be as loggingMiddleware,
866
- K as parsePartialJSON,
867
- Ce as rateLimitMiddleware,
868
- Z as renderTemplate,
869
- we as retryMiddleware,
870
- he as useAutoScroll,
871
- fe as useChat,
872
- de as useCompletion,
873
- ve as useObject,
874
- pe as usePrompt,
875
- ge as usePromptInput,
876
- ye as wrapProvider
875
+ K as LLMAPIError,
876
+ oe as convertFilesToParts,
877
+ ye as createChatPrompt,
878
+ Me as createMemoryStorage,
879
+ ne as createPrompt,
880
+ Ae as createWebStorage,
881
+ Ie as defineTool,
882
+ Fe as fileToDataURL,
883
+ Ee as headerMiddleware,
884
+ ie as isRetryableError,
885
+ Se as loggingMiddleware,
886
+ Y as parsePartialJSON,
887
+ Re as rateLimitMiddleware,
888
+ G as renderTemplate,
889
+ Te as retryMiddleware,
890
+ Ce as useAutoScroll,
891
+ ge as useChat,
892
+ pe as useCompletion,
893
+ he as useObject,
894
+ be as usePrompt,
895
+ we as usePromptInput,
896
+ xe as wrapProvider
877
897
  };