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