vue-llm 1.4.0 → 1.4.1

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
@@ -24,18 +24,18 @@ function K(t) {
24
24
  l === '"' ? a = !0 : l === "{" || l === "[" ? (e.unshift(l === "{" ? "}" : "]"), o.push({ at: n + 1, closers: e.join("") })) : l === "}" || l === "]" ? (e.shift(), o.push({ at: n + 1, closers: e.join("") })) : l === "," && o.push({ at: n, closers: e.join("") });
25
25
  }
26
26
  if (a) {
27
- const n = s ? r.slice(0, -1) : r, l = q(n.replace(/\\u[0-9a-fA-F]{0,3}$/, "") + '"' + e.join(""));
27
+ const n = s ? r.slice(0, -1) : r, l = $(n.replace(/\\u[0-9a-fA-F]{0,3}$/, "") + '"' + e.join(""));
28
28
  if (l !== void 0) return l;
29
29
  } else {
30
- const n = q(r.replace(/[\s,]+$/, "") + e.join(""));
30
+ const n = $(r.replace(/[\s,]+$/, "") + e.join(""));
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], c = q(r.slice(0, l.at).replace(/[\s,]+$/, "") + l.closers);
35
- if (c !== void 0) return c;
34
+ const l = o[n], f = $(r.slice(0, l.at).replace(/[\s,]+$/, "") + l.closers);
35
+ if (f !== void 0) return f;
36
36
  }
37
37
  }
38
- function q(t) {
38
+ function $(t) {
39
39
  try {
40
40
  return JSON.parse(t);
41
41
  } catch {
@@ -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", c;
56
- const m = r.getReader();
55
+ let l = "stop", f;
56
+ const p = r.getReader();
57
57
  try {
58
58
  for (; !e.aborted; ) {
59
- const { done: d, value: i } = await m.read();
59
+ const { done: d, value: u } = await p.read();
60
60
  if (d) break;
61
- if (this.applyChunk(a, i, s), i.type === "tool-call-end") {
62
- const b = s.get(i.toolCallId) ?? "{}";
61
+ if (this.applyChunk(a, u, s), u.type === "tool-call-end") {
62
+ const b = s.get(u.toolCallId) ?? "{}";
63
63
  let h;
64
64
  try {
65
65
  h = JSON.parse(b);
66
66
  } catch {
67
67
  h = b;
68
68
  }
69
- const w = { toolCallId: i.toolCallId, toolName: this.getToolName(a, i.toolCallId), args: h };
70
- n.push(w);
71
- const g = this.findToolCallPart(a, i.toolCallId);
72
- if (g && (g.args = h, g.state = "pending"), o.onToolCall && (o.canHandleTool?.(w.toolName) ?? !0)) {
73
- g && (g.state = "running"), this.triggerReactivity();
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)) {
73
+ m && (m.state = "running"), this.triggerReactivity();
74
74
  try {
75
- const C = await o.onToolCall(w, { signal: e });
76
- g && (C === void 0 ? g.state = "pending" : (g.state = "result", g.result = C), this.triggerReactivity());
77
- } catch (C) {
78
- g && (g.state = "error", g.error = C instanceof Error ? C.message : String(C), this.triggerReactivity());
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());
79
79
  }
80
80
  }
81
81
  }
82
- if (i.type === "tool-result") {
83
- const b = this.findToolCallPart(a, i.toolCallId);
84
- b && (b.state = i.error !== void 0 ? "error" : "result", b.result = i.result, b.error = i.error);
85
- const h = n.findIndex((w) => w.toolCallId === i.toolCallId);
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
86
  h !== -1 && n.splice(h, 1);
87
87
  }
88
- if (i.type === "finish" && (l = i.finishReason, c = i.usage), i.type === "error")
89
- throw i.error;
88
+ if (u.type === "finish" && (l = u.finishReason, f = u.usage), u.type === "error")
89
+ throw u.error;
90
90
  this.triggerReactivity();
91
91
  }
92
92
  } catch (d) {
93
93
  if (!e.aborted) {
94
- const i = d instanceof Error ? d : new Error(String(d));
95
- throw o.onError?.(i), i;
94
+ const u = d instanceof Error ? d : new Error(String(d));
95
+ throw o.onError?.(u), u;
96
96
  }
97
97
  } finally {
98
- m.releaseLock();
98
+ p.releaseLock();
99
99
  }
100
- return this.finalizeTextParts(a), this.triggerReactivity(), o.onFinish?.(a, { finishReason: l, usage: c }), { finishReason: l, usage: c, toolCalls: n };
100
+ return this.finalizeTextParts(a), this.triggerReactivity(), o.onFinish?.(a, { finishReason: l, usage: f }), { finishReason: l, usage: f, toolCalls: n };
101
101
  }
102
102
  getOrCreateAssistantMessage() {
103
103
  const r = this.messages.value, e = r[r.length - 1];
@@ -155,7 +155,7 @@ class te {
155
155
  }
156
156
  }
157
157
  const V = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
158
- function N(t = 16) {
158
+ function j(t = 16) {
159
159
  const r = crypto.getRandomValues(new Uint8Array(t));
160
160
  let e = "";
161
161
  for (let o = 0; o < t; o++)
@@ -163,157 +163,157 @@ function N(t = 16) {
163
163
  return e;
164
164
  }
165
165
  function fe(t) {
166
- const r = t.id ?? N(), e = R(t.initialMessages ?? []), o = R("ready"), a = R(void 0), s = R("");
166
+ const r = t.id ?? j(), e = R(t.initialMessages ?? []), o = R("ready"), a = R(void 0), s = R("");
167
167
  let n = null;
168
- const l = R(!t.storage), c = t.storage, m = (u) => {
169
- t.onError?.(u instanceof Error ? u : new Error(String(u)));
168
+ const l = R(!t.storage), f = t.storage, p = (c) => {
169
+ t.onError?.(c instanceof Error ? c : new Error(String(c)));
170
170
  };
171
- if (c) {
172
- const u = e.value, y = (v) => {
173
- v?.length && e.value === u && (e.value = v), l.value = !0;
171
+ if (f) {
172
+ const c = e.value, y = (v) => {
173
+ v?.length && e.value === c && (e.value = v), l.value = !0;
174
174
  };
175
175
  try {
176
- const v = c.load(r);
176
+ const v = f.load(r);
177
177
  v instanceof Promise ? v.then(y, (x) => {
178
- m(x), l.value = !0;
178
+ p(x), l.value = !0;
179
179
  }) : y(v);
180
180
  } catch (v) {
181
- m(v), l.value = !0;
181
+ p(v), l.value = !0;
182
182
  }
183
183
  z([e, o, l], () => {
184
184
  if (!(!l.value || o.value === "submitted" || o.value === "streaming"))
185
185
  try {
186
- const v = e.value.length > 0 ? c.save(r, e.value) : c.remove(r);
187
- v instanceof Promise && v.catch(m);
186
+ const v = e.value.length > 0 ? f.save(r, e.value) : f.remove(r);
187
+ v instanceof Promise && v.catch(p);
188
188
  } catch (v) {
189
- m(v);
189
+ p(v);
190
190
  }
191
191
  }, { deep: !0 });
192
192
  }
193
- const d = new te(e, o, N), i = () => {
194
- const u = [], y = [
193
+ const d = new te(e, o, j), u = () => {
194
+ const c = [], y = [
195
195
  P(t.system),
196
196
  ...e.value.filter((v) => v.role === "system").map((v) => v.parts.filter((x) => x.type === "text").map((x) => x.text).join(""))
197
197
  ].filter(Boolean);
198
- y.length > 0 && u.push({ role: "system", content: y.join(`
198
+ y.length > 0 && c.push({ role: "system", content: y.join(`
199
199
 
200
200
  `) });
201
201
  for (const v of e.value) {
202
202
  if (v.role === "system") continue;
203
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
204
  if (v.role === "user") {
205
- const S = v.parts.filter((j) => j.type === "file");
205
+ const S = v.parts.filter((J) => J.type === "file");
206
206
  if (S.length === 0) {
207
- u.push({ role: "user", content: M });
207
+ c.push({ role: "user", content: M });
208
208
  continue;
209
209
  }
210
- const D = S.map((j) => ({
210
+ const D = S.map((J) => ({
211
211
  type: "file",
212
- url: j.url,
213
- mediaType: j.mediaType,
214
- filename: j.filename
212
+ url: J.url,
213
+ mediaType: J.mediaType,
214
+ filename: J.filename
215
215
  }));
216
- M && D.push({ type: "text", text: M }), u.push({ role: "user", content: D });
216
+ M && D.push({ type: "text", text: M }), c.push({ role: "user", content: D });
217
217
  } else if (v.role === "assistant") {
218
- u.push({
218
+ c.push({
219
219
  role: "assistant",
220
220
  content: M,
221
221
  toolCalls: O.length > 0 ? O : void 0
222
222
  });
223
223
  for (const S of v.parts)
224
- S.type === "tool-call" && u.push({
224
+ S.type === "tool-call" && c.push({
225
225
  role: "tool",
226
226
  content: S.state === "result" ? JSON.stringify(S.result ?? null) : JSON.stringify({ error: S.error ?? "The tool call was not answered" }),
227
227
  toolCallId: S.toolCallId
228
228
  });
229
229
  }
230
230
  }
231
- return u;
232
- }, b = t.tools?.map(({ name: u, description: y, parameters: v }) => ({ name: u, description: y, parameters: v })), h = (u, { signal: y }) => {
233
- const v = t.tools?.find((x) => x.name === u.toolName);
234
- return v?.execute ? v.execute(u.args, { toolCallId: u.toolCallId, signal: y }) : t.onToolCall?.(u);
235
- }, w = () => {
236
- const u = e.value[e.value.length - 1];
237
- return u?.role === "assistant" && u.parts.some(
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
238
  (y) => y.type === "tool-call" && y.state !== "result" && y.state !== "error"
239
239
  );
240
- }, g = async (u) => {
240
+ }, m = async (c) => {
241
241
  const y = t.maxToolRoundtrips ?? 5;
242
242
  let v = 0;
243
243
  for (; v < y; ) {
244
- const x = i();
244
+ const x = u();
245
245
  t.provider.validateConfig();
246
246
  const { stream: M } = await W(t.provider, {
247
247
  model: P(t.model),
248
248
  messages: x,
249
249
  tools: b,
250
- signal: u,
250
+ signal: c,
251
251
  providerOptions: {
252
252
  ...t.providerOptions,
253
253
  ...P(t.body)
254
254
  }
255
- }, r), O = await d.processStream(M, u, {
255
+ }, r), O = await d.processStream(M, c, {
256
256
  onFinish: t.onFinish,
257
257
  onError: t.onError,
258
258
  onToolCall: h,
259
259
  canHandleTool: (S) => !!t.onToolCall || !!t.tools?.find((D) => D.name === S)?.execute
260
260
  });
261
- if (O.finishReason !== "tool-calls" || O.toolCalls.length === 0 || w())
261
+ if (O.finishReason !== "tool-calls" || O.toolCalls.length === 0 || C())
262
262
  break;
263
263
  v++;
264
264
  }
265
- }, C = async (u) => {
266
- const y = typeof u == "string" ? u : u?.text ?? s.value;
265
+ }, w = async (c) => {
266
+ const y = typeof c == "string" ? c : c?.text ?? s.value;
267
267
  let v;
268
268
  try {
269
- v = typeof u == "object" ? await ee(u.files) : [];
269
+ v = typeof c == "object" ? await ee(c.files) : [];
270
270
  } catch (O) {
271
271
  a.value = O instanceof Error ? O : new Error(String(O)), o.value = "error";
272
272
  return;
273
273
  }
274
274
  if (!y.trim() && v.length === 0) return;
275
- (typeof u != "object" || u === null || !("text" in u)) && (s.value = "");
275
+ (typeof c != "object" || c === null || !("text" in c)) && (s.value = "");
276
276
  const x = [...v];
277
277
  y.trim() && x.push({ type: "text", text: y, state: "done" });
278
278
  const M = {
279
- id: N(),
279
+ id: j(),
280
280
  role: "user",
281
281
  parts: x,
282
282
  createdAt: /* @__PURE__ */ new Date()
283
283
  };
284
- e.value = [...e.value, M], await f();
285
- }, f = async () => {
284
+ e.value = [...e.value, M], await i();
285
+ }, i = async () => {
286
286
  a.value = void 0, o.value = "submitted", n?.abort();
287
- const u = new AbortController();
288
- n = u;
289
- const y = () => n === u || n === null;
287
+ const c = new AbortController();
288
+ n = c;
289
+ const y = () => n === c || n === null;
290
290
  try {
291
- await g(u.signal), y() && (o.value = "ready");
291
+ await m(c.signal), y() && (o.value = "ready");
292
292
  } catch (v) {
293
- if (u.signal.aborted) {
293
+ if (c.signal.aborted) {
294
294
  y() && (o.value = "ready");
295
295
  return;
296
296
  }
297
297
  a.value = v instanceof Error ? v : new Error(String(v)), o.value = "error";
298
298
  }
299
- }, p = async () => {
300
- const u = e.value, y = u.reduce((v, x, M) => x.role === "assistant" ? M : v, -1);
301
- y !== -1 && (e.value = u.slice(0, y), await f());
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
302
  }, A = () => {
303
303
  n?.abort(), n = null, o.value = "ready";
304
- }, E = (u) => {
305
- e.value = typeof u == "function" ? u(e.value) : u;
306
- }, I = (u) => {
307
- u?.preventDefault?.(), s.value.trim() && C();
308
- }, T = F(() => o.value === "submitted" || o.value === "streaming"), L = async ({ toolCallId: u, result: y, error: v }) => {
309
- const x = e.value.flatMap((D) => D.parts).find((D) => D.type === "tool-call" && D.toolCallId === u);
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
310
  if (!x) return;
311
311
  v !== void 0 ? (x.state = "error", x.error = v) : (x.state = "result", x.result = y), e.value = [...e.value];
312
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 && !w() && await f();
314
- }, J = () => {
313
+ (t.autoSendToolResults ?? !0) && O && !S && !C() && await i();
314
+ }, L = () => {
315
315
  a.value = void 0, o.value === "error" && (o.value = "ready");
316
- }, $ = k({
316
+ }, q = k({
317
317
  id: r,
318
318
  kind: "chat",
319
319
  providerId: t.provider.id,
@@ -325,68 +325,72 @@ function fe(t) {
325
325
  clear: () => E([])
326
326
  });
327
327
  return H() && B(() => {
328
- n?.abort(), $();
328
+ n?.abort(), q();
329
329
  }), {
330
330
  messages: e,
331
331
  status: F(() => o.value),
332
332
  error: a,
333
333
  id: r,
334
334
  input: s,
335
- sendMessage: C,
336
- regenerate: p,
335
+ sendMessage: w,
336
+ regenerate: g,
337
337
  stop: A,
338
338
  setMessages: E,
339
339
  handleSubmit: I,
340
- isLoading: T,
341
- addToolResult: L,
342
- clearError: J,
340
+ isLoading: N,
341
+ addToolResult: T,
342
+ clearError: L,
343
343
  isRestored: F(() => l.value)
344
344
  };
345
345
  }
346
346
  function de(t) {
347
- const r = t.id ?? N(), e = R(""), o = R("ready"), a = R(void 0), s = R("");
347
+ const r = t.id ?? j(), e = R(""), o = R("ready"), a = R(void 0), s = R("");
348
348
  let n = null;
349
349
  const l = async (h) => {
350
- const w = h ?? s.value;
351
- if (!w.trim()) return e.value;
352
- h || (s.value = ""), e.value = "", a.value = void 0, o.value = "submitted", n?.abort(), n = new AbortController();
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();
353
+ const m = new AbortController();
354
+ n = m;
353
355
  try {
354
356
  t.provider.validateConfig();
355
- const g = P(t.system), C = g ? [
356
- { role: "system", content: g },
357
- { role: "user", content: w }
358
- ] : [{ role: "user", content: w }], { stream: f } = await W(t.provider, {
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, {
359
361
  model: P(t.model),
360
- messages: C,
361
- signal: n.signal
362
+ messages: i,
363
+ signal: m.signal
362
364
  }, r);
365
+ if (m.signal.aborted) return e.value;
363
366
  o.value = "streaming";
364
- const p = f.getReader();
365
- let A = "stop", E;
366
- for (; !n.signal.aborted; ) {
367
- const { done: I, value: T } = await p.read();
368
- if (I) break;
367
+ const A = g.getReader();
368
+ let E = "stop", I;
369
+ for (; !m.signal.aborted; ) {
370
+ const { done: N, value: T } = await A.read();
371
+ if (m.signal.aborted || N) break;
369
372
  if (T.type === "text-delta")
370
373
  e.value += T.text;
371
374
  else if (T.type === "finish")
372
- A = T.finishReason, E = T.usage;
375
+ E = T.finishReason, I = T.usage;
373
376
  else if (T.type === "error")
374
377
  throw T.error;
375
378
  }
376
- return p.releaseLock(), o.value = "ready", t.onFinish?.(e.value, { finishReason: A, usage: E }), e.value;
377
- } catch (g) {
378
- if (!n || n.signal.aborted)
379
- return o.value = "ready", e.value;
380
- const C = g instanceof Error ? g : new Error(String(g));
381
- return a.value = C, o.value = "error", t.onError?.(C), e.value;
379
+ return A.releaseLock(), m.signal.aborted || (o.value = "ready", t.onFinish?.(e.value, { finishReason: E, usage: I })), e.value;
380
+ } catch (w) {
381
+ 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;
384
+ } finally {
385
+ n === m && (n = null);
382
386
  }
383
- }, c = () => {
387
+ }, f = () => {
384
388
  n?.abort(), n = null, o.value = "ready";
385
- }, m = () => {
389
+ }, p = () => {
386
390
  a.value = void 0, o.value === "error" && (o.value = "ready");
387
391
  }, d = (h) => {
388
392
  h?.preventDefault?.(), s.value.trim() && l();
389
- }, i = F(() => o.value === "submitted" || o.value === "streaming"), b = k({
393
+ }, u = F(() => o.value === "submitted" || o.value === "streaming"), b = k({
390
394
  id: r,
391
395
  kind: "completion",
392
396
  providerId: t.provider.id,
@@ -394,7 +398,7 @@ function de(t) {
394
398
  status: o,
395
399
  error: a,
396
400
  output: e,
397
- stop: c
401
+ stop: f
398
402
  });
399
403
  return H() && B(() => {
400
404
  n?.abort(), b();
@@ -402,76 +406,80 @@ function de(t) {
402
406
  completion: e,
403
407
  status: F(() => o.value),
404
408
  error: a,
405
- isLoading: i,
409
+ isLoading: u,
406
410
  id: r,
407
411
  input: s,
408
412
  complete: l,
409
- stop: c,
410
- clearError: m,
413
+ stop: f,
414
+ clearError: p,
411
415
  handleSubmit: d
412
416
  };
413
417
  }
414
418
  function ve(t) {
415
- const r = t.id ?? N(), e = R(void 0), o = R(void 0), a = R("ready"), s = R(void 0), n = R("");
419
+ const r = t.id ?? j(), e = R(void 0), o = R(void 0), a = R("ready"), s = R(void 0), n = R("");
416
420
  let l = null;
417
- const c = async (h) => {
418
- const w = h ?? n.value;
419
- if (!w.trim()) return e.value;
420
- h || (n.value = ""), e.value = void 0, o.value = void 0, s.value = void 0, a.value = "submitted", l?.abort(), l = new AbortController();
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();
425
+ const m = new AbortController();
426
+ l = m;
421
427
  try {
422
428
  t.provider.validateConfig();
423
- const g = [
429
+ const w = [
424
430
  P(t.system) ?? "",
425
431
  "Respond with valid JSON matching this schema:",
426
432
  JSON.stringify(t.schema, null, 2),
427
433
  "Output ONLY the JSON object, no other text."
428
434
  ].filter(Boolean).join(`
429
435
 
430
- `), { stream: C } = await W(t.provider, {
436
+ `), { stream: i } = await W(t.provider, {
431
437
  model: P(t.model),
432
438
  messages: [
433
- { role: "system", content: g },
434
- { role: "user", content: w }
439
+ { role: "system", content: w },
440
+ { role: "user", content: C }
435
441
  ],
436
- signal: l.signal,
442
+ signal: m.signal,
437
443
  providerOptions: {
438
444
  response_format: { type: "json_object" }
439
445
  }
440
446
  }, r);
447
+ if (m.signal.aborted) return e.value;
441
448
  a.value = "streaming";
442
- const f = C.getReader();
443
- let p = "", A = "stop", E;
444
- for (; !l.signal.aborted; ) {
445
- const { done: I, value: T } = await f.read();
446
- if (I) break;
449
+ const g = i.getReader();
450
+ let A = "", E = "stop", I;
451
+ for (; !m.signal.aborted; ) {
452
+ const { done: N, value: T } = await g.read();
453
+ if (m.signal.aborted || N) break;
447
454
  if (T.type === "text-delta") {
448
- p += T.text;
449
- const L = K(p);
455
+ A += T.text;
456
+ const L = K(A);
450
457
  L !== null && typeof L == "object" && (o.value = L);
451
458
  } else if (T.type === "finish")
452
- A = T.finishReason, E = T.usage;
459
+ E = T.finishReason, I = T.usage;
453
460
  else if (T.type === "error")
454
461
  throw T.error;
455
462
  }
456
- f.releaseLock();
463
+ if (g.releaseLock(), m.signal.aborted) return e.value;
457
464
  try {
458
- e.value = JSON.parse(p), o.value = e.value;
465
+ e.value = JSON.parse(A), o.value = e.value;
459
466
  } catch {
460
- throw new Error(`Failed to parse JSON response: ${p.slice(0, 200)}`);
467
+ throw new Error(`Failed to parse JSON response: ${A.slice(0, 200)}`);
461
468
  }
462
- return a.value = "ready", t.onFinish?.(e.value, { finishReason: A, usage: E }), e.value;
463
- } catch (g) {
464
- if (l.signal.aborted)
465
- return a.value = "ready", e.value;
466
- const C = g instanceof Error ? g : new Error(String(g));
467
- s.value = C, a.value = "error", t.onError?.(C);
469
+ return a.value = "ready", t.onFinish?.(e.value, { finishReason: E, usage: I }), e.value;
470
+ } catch (w) {
471
+ 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);
468
474
  return;
475
+ } finally {
476
+ l === m && (l = null);
469
477
  }
470
- }, m = () => {
478
+ }, p = () => {
471
479
  l?.abort(), l = null, a.value = "ready";
472
480
  }, d = () => {
473
481
  s.value = void 0, a.value === "error" && (a.value = "ready");
474
- }, i = F(() => a.value === "submitted" || a.value === "streaming"), b = k({
482
+ }, u = F(() => a.value === "submitted" || a.value === "streaming"), b = k({
475
483
  id: r,
476
484
  kind: "object",
477
485
  providerId: t.provider.id,
@@ -479,7 +487,7 @@ function ve(t) {
479
487
  status: a,
480
488
  error: s,
481
489
  output: o,
482
- stop: m
490
+ stop: p
483
491
  });
484
492
  return H() && B(() => {
485
493
  l?.abort(), b();
@@ -488,11 +496,11 @@ function ve(t) {
488
496
  partialObject: o,
489
497
  status: F(() => a.value),
490
498
  error: s,
491
- isLoading: i,
499
+ isLoading: u,
492
500
  id: r,
493
501
  input: n,
494
- submit: c,
495
- stop: m,
502
+ submit: f,
503
+ stop: p,
496
504
  clearError: d
497
505
  };
498
506
  }
@@ -517,7 +525,7 @@ function me(t, r = {}) {
517
525
  render: e,
518
526
  toUIMessages(o) {
519
527
  return e(o).map((a) => ({
520
- id: N(),
528
+ id: j(),
521
529
  role: a.role,
522
530
  parts: [{ type: "text", text: a.content, state: "done" }],
523
531
  createdAt: /* @__PURE__ */ new Date()
@@ -547,91 +555,91 @@ function pe(t, r) {
547
555
  function ge(t, r = {}) {
548
556
  const e = R(null), o = R([]), a = R(!1);
549
557
  let s = 0;
550
- const n = F(() => t.status.value === "submitted" || t.status.value === "streaming"), l = F(() => !n.value && (t.input.value.trim() !== "" || o.value.length > 0)), c = (f) => r.accept ? r.accept.split(",").map((p) => p.trim().toLowerCase()).filter(Boolean).some((p) => p.startsWith(".") ? f.name.toLowerCase().endsWith(p) : p.endsWith("/*") ? f.type.toLowerCase().startsWith(p.slice(0, -1)) : f.type.toLowerCase() === p) : !0, m = (f) => {
551
- const p = Array.from(f ?? []);
552
- if (!p.length) return;
553
- const A = p.filter((I) => !c(I));
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 ?? []);
560
+ if (!g.length) return;
561
+ const A = g.filter((I) => !f(I));
554
562
  A.length && r.onReject?.(A, "type");
555
- let E = p.filter(c);
563
+ let E = g.filter(f);
556
564
  if (r.maxFiles !== void 0) {
557
565
  const I = Math.max(0, r.maxFiles - o.value.length);
558
566
  E.length > I && r.onReject?.(E.slice(I), "count"), E = E.slice(0, I);
559
567
  }
560
568
  E.length && (o.value = [...o.value, ...E]);
561
- }, d = (f) => {
562
- o.value = o.value.filter((p) => p !== f);
563
- }, i = () => {
564
- const f = e.value;
565
- if (!f || typeof getComputedStyle > "u") return;
566
- const p = getComputedStyle(f), A = parseFloat(p.lineHeight) || parseFloat(p.fontSize) * 1.5 || 20, E = (parseFloat(p.paddingTop) || 0) + (parseFloat(p.paddingBottom) || 0), I = (parseFloat(p.borderTopWidth) || 0) + (parseFloat(p.borderBottomWidth) || 0), T = p.boxSizing === "border-box", L = (u) => T ? u + I : u - E, J = A * (r.maxRows ?? 8) + E;
567
- f.style.height = "auto";
568
- const $ = f.scrollHeight > J;
569
- f.style.height = `${L(Math.min(f.scrollHeight, J))}px`, f.style.overflowY = $ ? "auto" : "hidden";
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";
570
578
  };
571
- z(t.input, () => U(i)), z(e, () => U(i));
579
+ z(t.input, () => U(u)), z(e, () => U(u));
572
580
  const b = async () => {
573
581
  if (!l.value) return;
574
- const f = t.input.value, p = o.value;
575
- t.input.value = "", o.value = [], await t.sendMessage(p.length ? { text: f, files: p } : f);
576
- }, h = (f) => {
577
- f.key !== "Enter" || f.shiftKey || r.submitOnEnter === !1 || f.isComposing || f.keyCode === 229 || (f.preventDefault(), b());
578
- }, w = (f) => {
579
- const p = Array.from(f.clipboardData?.files ?? []);
580
- p.length && (f.preventDefault(), m(p));
581
- }, g = (f) => Array.from(f.dataTransfer?.types ?? []).includes("Files");
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");
582
590
  return {
583
591
  textarea: e,
584
592
  files: o,
585
593
  isDragging: a,
586
594
  canSubmit: l,
587
595
  submit: b,
588
- addFiles: m,
596
+ addFiles: p,
589
597
  removeFile: d,
590
- handlers: { onKeydown: h, onPaste: w },
598
+ handlers: { onKeydown: h, onPaste: C },
591
599
  dropzone: {
592
- onDragenter(f) {
593
- g(f) && (f.preventDefault(), s++, a.value = !0);
600
+ onDragenter(i) {
601
+ m(i) && (i.preventDefault(), s++, a.value = !0);
594
602
  },
595
- onDragover(f) {
596
- g(f) && f.preventDefault();
603
+ onDragover(i) {
604
+ m(i) && i.preventDefault();
597
605
  },
598
- onDragleave(f) {
599
- g(f) && (s = Math.max(0, s - 1), s === 0 && (a.value = !1));
606
+ onDragleave(i) {
607
+ m(i) && (s = Math.max(0, s - 1), s === 0 && (a.value = !1));
600
608
  },
601
- onDrop(f) {
602
- g(f) && (f.preventDefault(), s = 0, a.value = !1, m(f.dataTransfer?.files));
609
+ onDrop(i) {
610
+ m(i) && (i.preventDefault(), s = 0, a.value = !1, p(i.dataTransfer?.files));
603
611
  }
604
612
  },
605
- resize: i
613
+ resize: u
606
614
  };
607
615
  }
608
616
  function he(t = {}) {
609
617
  const r = R(null), e = R(!0), o = t.threshold ?? 48;
610
618
  let a = !1;
611
619
  const s = (d) => d.scrollHeight - d.scrollTop - d.clientHeight, n = (d = "auto") => {
612
- const i = r.value;
613
- i && (a = !0, e.value = !0, typeof i.scrollTo == "function" ? i.scrollTo({ top: i.scrollHeight, behavior: d }) : i.scrollTop = i.scrollHeight, oe(() => {
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(() => {
614
622
  a = !1;
615
623
  }));
616
624
  }, l = () => {
617
625
  const d = r.value;
618
626
  !d || a || (e.value = s(d) <= o);
619
- }, c = () => {
627
+ }, f = () => {
620
628
  e.value && n();
621
629
  };
622
- let m = () => {
630
+ let p = () => {
623
631
  };
624
632
  return z(r, (d) => {
625
- if (m(), !d) return;
633
+ if (p(), !d) return;
626
634
  d.addEventListener("scroll", l, { passive: !0 });
627
- const i = typeof MutationObserver < "u" ? new MutationObserver(c) : void 0;
628
- i?.observe(d, { childList: !0, subtree: !0, characterData: !0 });
629
- const b = typeof ResizeObserver < "u" ? new ResizeObserver(c) : void 0;
630
- b?.observe(d), d.firstElementChild && b?.observe(d.firstElementChild), n(), m = () => {
631
- d.removeEventListener("scroll", l), i?.disconnect(), b?.disconnect(), m = () => {
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 = () => {
632
640
  };
633
641
  };
634
- }, { flush: "post" }), H() && B(() => m()), { container: r, isAtBottom: e, scrollToBottom: n };
642
+ }, { flush: "post" }), H() && B(() => p()), { container: r, isAtBottom: e, scrollToBottom: n };
635
643
  }
636
644
  function oe(t) {
637
645
  typeof requestAnimationFrame == "function" ? requestAnimationFrame(() => requestAnimationFrame(t)) : setTimeout(t, 32);
@@ -645,36 +653,36 @@ function ye(t, r) {
645
653
  },
646
654
  async chat(e) {
647
655
  let o = e;
648
- for (const c of r)
649
- c.beforeRequest && (o = await c.beforeRequest(o, t));
656
+ for (const f of r)
657
+ f.beforeRequest && (o = await f.beforeRequest(o, t));
650
658
  const a = r.reduceRight(
651
- (c, m) => m.wrapChat ? (d) => m.wrapChat(c, d) : c,
652
- (c) => t.chat(c)
659
+ (f, p) => p.wrapChat ? (d) => p.wrapChat(f, d) : f,
660
+ (f) => t.chat(f)
653
661
  );
654
662
  let s;
655
663
  try {
656
664
  s = await a(o);
657
- } catch (c) {
658
- let m = c instanceof Error ? c : new Error(String(c));
665
+ } catch (f) {
666
+ let p = f instanceof Error ? f : new Error(String(f));
659
667
  for (const d of r)
660
668
  if (d.onError) {
661
- const i = await d.onError(m, o);
662
- i instanceof Error && (m = i);
669
+ const u = await d.onError(p, o);
670
+ u instanceof Error && (p = u);
663
671
  }
664
- throw m;
672
+ throw p;
665
673
  }
666
- const l = r.some((c) => c.afterStream) ? s.stream.pipeThrough(new TransformStream({
667
- async transform(c, m) {
668
- let d = c;
669
- for (const i of r) {
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) {
670
678
  if (!d) break;
671
- i.afterStream && (d = await i.afterStream(d, o));
679
+ u.afterStream && (d = await u.afterStream(d, o));
672
680
  }
673
- d && m.enqueue(d);
681
+ d && p.enqueue(d);
674
682
  }
675
683
  })) : s.stream;
676
- for (const c of r)
677
- c.onFinish && c.onFinish(o, s);
684
+ for (const f of r)
685
+ f.onFinish && f.onFinish(o, s);
678
686
  return { stream: l, response: s.response };
679
687
  }
680
688
  } : t;
@@ -693,21 +701,21 @@ function be(t = "vue-llm") {
693
701
  };
694
702
  }
695
703
  function we(t = {}, r) {
696
- 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 ?? ae;
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;
697
705
  return {
698
- async wrapChat(m, d) {
699
- for (let i = 1; ; i++)
706
+ async wrapChat(p, d) {
707
+ for (let u = 1; ; u++)
700
708
  try {
701
- return await m(d);
709
+ return await p(d);
702
710
  } catch (b) {
703
711
  const h = b instanceof Error ? b : new Error(String(b));
704
- if (i > o || d.signal?.aborted || !c(h, i))
712
+ if (u > o || d.signal?.aborted || !f(h, u))
705
713
  throw h;
706
- const w = a * n ** (i - 1), g = h instanceof _ ? h.retryAfter : void 0, C = Math.min(
714
+ const C = a * n ** (u - 1), m = h instanceof _ ? h.retryAfter : void 0, w = Math.min(
707
715
  s,
708
- g ?? (l ? w * (0.5 + Math.random() * 0.5) : w)
716
+ m ?? (l ? C * (0.5 + Math.random() * 0.5) : C)
709
717
  );
710
- e.onRetry?.({ error: h, attempt: i, delay: C }), await ne(C, d.signal);
718
+ e.onRetry?.({ error: h, attempt: u, delay: w }), await ne(w, d.signal);
711
719
  }
712
720
  }
713
721
  };
@@ -724,33 +732,33 @@ function Ce(t) {
724
732
  }, n = async (l) => {
725
733
  for (; ; ) {
726
734
  l?.throwIfAborted();
727
- const c = Date.now();
728
- for (; e.length > 0 && e[0] <= c - r; ) e.shift();
729
- const m = t.maxRequests !== void 0 && e.length >= t.maxRequests, d = t.maxConcurrent !== void 0 && o >= t.maxConcurrent;
730
- if (!m && !d) {
731
- e.push(c), o++;
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++;
732
740
  return;
733
741
  }
734
- await new Promise((i, b) => {
742
+ await new Promise((u, b) => {
735
743
  const h = () => {
736
- clearTimeout(g), a.delete(h), l?.removeEventListener("abort", w), i();
737
- }, w = () => {
744
+ clearTimeout(m), a.delete(h), l?.removeEventListener("abort", C), u();
745
+ }, C = () => {
738
746
  h(), b(l.reason);
739
- }, g = m ? setTimeout(h, e[0] + r - c) : void 0;
740
- a.add(h), l?.addEventListener("abort", w, { once: !0 });
747
+ }, m = p ? setTimeout(h, e[0] + r - f) : void 0;
748
+ a.add(h), l?.addEventListener("abort", C, { once: !0 });
741
749
  });
742
750
  }
743
751
  };
744
752
  return {
745
- async wrapChat(l, c) {
746
- await n(c.signal);
747
- let m;
753
+ async wrapChat(l, f) {
754
+ await n(f.signal);
755
+ let p;
748
756
  try {
749
- m = await l(c);
757
+ p = await l(f);
750
758
  } catch (d) {
751
759
  throw s(), d;
752
760
  }
753
- return { ...m, stream: se(m.stream, s) };
761
+ return { ...p, stream: se(p.stream, s) };
754
762
  }
755
763
  };
756
764
  }