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