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