vue-llm 0.2.1 → 0.4.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,302 +1,302 @@
1
- import { ref as m, computed as R, onScopeDispose as O, toValue as T } from "vue";
1
+ import { ref as C, computed as R, onScopeDispose as I, toValue as N } from "vue";
2
2
  class P {
3
- constructor(s, t, e) {
4
- this.messages = s, this.status = t, this.generateId = e;
3
+ constructor(r, e, t) {
4
+ this.messages = r, this.status = e, this.generateId = t;
5
5
  }
6
6
  messages;
7
7
  status;
8
8
  generateId;
9
- async processStream(s, t, e) {
9
+ async processStream(r, e, t) {
10
10
  this.status.value = "streaming";
11
- const r = this.getOrCreateAssistantMessage(), u = /* @__PURE__ */ new Map(), o = [];
12
- let f = "stop", w;
13
- const x = s.getReader();
11
+ const s = this.getOrCreateAssistantMessage(), i = /* @__PURE__ */ new Map(), o = [];
12
+ let n = "stop", p;
13
+ const g = r.getReader();
14
14
  try {
15
- for (; !t.aborted; ) {
16
- const { done: b, value: v } = await x.read();
17
- if (b) break;
18
- if (this.applyChunk(r, v, u), v.type === "tool-call-end") {
19
- const C = u.get(v.toolCallId) ?? "{}";
20
- let h;
15
+ for (; !e.aborted; ) {
16
+ const { done: m, value: v } = await g.read();
17
+ if (m) break;
18
+ if (this.applyChunk(s, v, i), v.type === "tool-call-end") {
19
+ const S = i.get(v.toolCallId) ?? "{}";
20
+ let w;
21
21
  try {
22
- h = JSON.parse(C);
22
+ w = JSON.parse(S);
23
23
  } catch {
24
- h = C;
24
+ w = S;
25
25
  }
26
- const y = { toolCallId: v.toolCallId, toolName: this.getToolName(r, v.toolCallId), args: h };
27
- if (o.push(y), e.onToolCall) {
28
- const c = this.findToolCallPart(r, v.toolCallId);
29
- c && (c.state = "running", c.args = h), this.triggerReactivity();
26
+ const y = { toolCallId: v.toolCallId, toolName: this.getToolName(s, v.toolCallId), args: w };
27
+ if (o.push(y), t.onToolCall) {
28
+ const d = this.findToolCallPart(s, v.toolCallId);
29
+ d && (d.state = "running", d.args = w), this.triggerReactivity();
30
30
  try {
31
- const p = await e.onToolCall(y);
32
- p !== void 0 && c && (c.state = "result", c.result = p, this.triggerReactivity());
33
- } catch (p) {
34
- c && (c.state = "error", c.error = p instanceof Error ? p.message : String(p), this.triggerReactivity());
31
+ const b = await t.onToolCall(y);
32
+ b !== void 0 && d && (d.state = "result", d.result = b, this.triggerReactivity());
33
+ } catch (b) {
34
+ d && (d.state = "error", d.error = b instanceof Error ? b.message : String(b), this.triggerReactivity());
35
35
  }
36
36
  }
37
37
  }
38
- if (v.type === "finish" && (f = v.finishReason, w = v.usage), v.type === "error")
38
+ if (v.type === "finish" && (n = v.finishReason, p = v.usage), v.type === "error")
39
39
  throw v.error;
40
40
  this.triggerReactivity();
41
41
  }
42
- } catch (b) {
43
- if (!t.aborted) {
44
- const v = b instanceof Error ? b : new Error(String(b));
45
- throw e.onError?.(v), v;
42
+ } catch (m) {
43
+ if (!e.aborted) {
44
+ const v = m instanceof Error ? m : new Error(String(m));
45
+ throw t.onError?.(v), v;
46
46
  }
47
47
  } finally {
48
- x.releaseLock();
48
+ g.releaseLock();
49
49
  }
50
- return this.finalizeTextParts(r), this.triggerReactivity(), e.onFinish?.(r, { finishReason: f, usage: w }), { finishReason: f, usage: w, toolCalls: o };
50
+ return this.finalizeTextParts(s), this.triggerReactivity(), t.onFinish?.(s, { finishReason: n, usage: p }), { finishReason: n, usage: p, toolCalls: o };
51
51
  }
52
52
  getOrCreateAssistantMessage() {
53
- const s = this.messages.value, t = s[s.length - 1];
54
- if (t?.role === "assistant") return t;
55
- const e = {
53
+ const r = this.messages.value, e = r[r.length - 1];
54
+ if (e?.role === "assistant") return e;
55
+ const t = {
56
56
  id: this.generateId(),
57
57
  role: "assistant",
58
58
  parts: [],
59
59
  createdAt: /* @__PURE__ */ new Date()
60
60
  };
61
- return s.push(e), e;
61
+ return r.push(t), t;
62
62
  }
63
- applyChunk(s, t, e) {
64
- switch (t.type) {
63
+ applyChunk(r, e, t) {
64
+ switch (e.type) {
65
65
  case "text-delta": {
66
- const r = s.parts[s.parts.length - 1];
67
- r?.type === "text" && r.state === "streaming" ? r.text += t.text : s.parts.push({ type: "text", text: t.text, state: "streaming" });
66
+ const s = r.parts[r.parts.length - 1];
67
+ s?.type === "text" && s.state === "streaming" ? s.text += e.text : r.parts.push({ type: "text", text: e.text, state: "streaming" });
68
68
  break;
69
69
  }
70
70
  case "reasoning-delta": {
71
- const r = s.parts[s.parts.length - 1];
72
- r?.type === "reasoning" && r.state === "streaming" ? r.text += t.text : s.parts.push({ type: "reasoning", text: t.text, state: "streaming" });
71
+ const s = r.parts[r.parts.length - 1];
72
+ s?.type === "reasoning" && s.state === "streaming" ? s.text += e.text : r.parts.push({ type: "reasoning", text: e.text, state: "streaming" });
73
73
  break;
74
74
  }
75
75
  case "tool-call-start":
76
- s.parts.push({
76
+ r.parts.push({
77
77
  type: "tool-call",
78
- toolCallId: t.toolCallId,
79
- toolName: t.toolName,
78
+ toolCallId: e.toolCallId,
79
+ toolName: e.toolName,
80
80
  args: void 0,
81
81
  state: "pending"
82
- }), e.set(t.toolCallId, "");
82
+ }), t.set(e.toolCallId, "");
83
83
  break;
84
84
  case "tool-call-delta":
85
- e.set(
86
- t.toolCallId,
87
- (e.get(t.toolCallId) ?? "") + t.args
85
+ t.set(
86
+ e.toolCallId,
87
+ (t.get(e.toolCallId) ?? "") + e.args
88
88
  );
89
89
  break;
90
90
  }
91
91
  }
92
- finalizeTextParts(s) {
93
- for (const t of s.parts)
94
- (t.type === "text" || t.type === "reasoning") && t.state === "streaming" && (t.state = "done");
92
+ finalizeTextParts(r) {
93
+ for (const e of r.parts)
94
+ (e.type === "text" || e.type === "reasoning") && e.state === "streaming" && (e.state = "done");
95
95
  }
96
- findToolCallPart(s, t) {
97
- return s.parts.find(
98
- (e) => e.type === "tool-call" && e.toolCallId === t
96
+ findToolCallPart(r, e) {
97
+ return r.parts.find(
98
+ (t) => t.type === "tool-call" && t.toolCallId === e
99
99
  );
100
100
  }
101
- getToolName(s, t) {
102
- return this.findToolCallPart(s, t)?.toolName ?? "";
101
+ getToolName(r, e) {
102
+ return this.findToolCallPart(r, e)?.toolName ?? "";
103
103
  }
104
104
  triggerReactivity() {
105
105
  this.messages.value = [...this.messages.value];
106
106
  }
107
107
  }
108
- const N = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
109
- function I(a = 16) {
110
- const s = crypto.getRandomValues(new Uint8Array(a));
111
- let t = "";
112
- for (let e = 0; e < a; e++)
113
- t += N[s[e] % N.length];
114
- return t;
108
+ const T = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
109
+ function O(a = 16) {
110
+ const r = crypto.getRandomValues(new Uint8Array(a));
111
+ let e = "";
112
+ for (let t = 0; t < a; t++)
113
+ e += T[r[t] % T.length];
114
+ return e;
115
115
  }
116
116
  function j(a) {
117
- const s = a.id ?? I(), t = m(a.initialMessages ?? []), e = m("ready"), r = m(void 0), u = m("");
117
+ const r = a.id ?? O(), e = C(a.initialMessages ?? []), t = C("ready"), s = C(void 0), i = C("");
118
118
  let o = null;
119
- const f = new P(t, e, I), w = () => {
119
+ const n = new P(e, t, O), p = () => {
120
120
  const l = [];
121
121
  a.system && l.push({ role: "system", content: a.system });
122
- for (const d of t.value) {
123
- if (d.role === "system") continue;
124
- const i = d.parts.filter((g) => g.type === "text").map((g) => g.text).join(""), E = d.parts.filter((g) => g.type === "tool-call").map((g) => ({ id: g.toolCallId, name: g.toolName, args: g.args }));
125
- if (d.role === "user")
126
- l.push({ role: "user", content: i });
127
- else if (d.role === "assistant") {
122
+ for (const f of e.value) {
123
+ if (f.role === "system") continue;
124
+ const c = f.parts.filter((h) => h.type === "text").map((h) => h.text).join(""), E = f.parts.filter((h) => h.type === "tool-call").map((h) => ({ id: h.toolCallId, name: h.toolName, args: h.args }));
125
+ if (f.role === "user")
126
+ l.push({ role: "user", content: c });
127
+ else if (f.role === "assistant") {
128
128
  l.push({
129
129
  role: "assistant",
130
- content: i,
130
+ content: c,
131
131
  toolCalls: E.length > 0 ? E : void 0
132
132
  });
133
- for (const g of d.parts)
134
- g.type !== "tool-call" || g.state !== "result" || l.push({
133
+ for (const h of f.parts)
134
+ h.type !== "tool-call" || h.state !== "result" || l.push({
135
135
  role: "tool",
136
- content: JSON.stringify(g.result),
137
- toolCallId: g.toolCallId
136
+ content: JSON.stringify(h.result),
137
+ toolCallId: h.toolCallId
138
138
  });
139
139
  }
140
140
  }
141
141
  return l;
142
- }, x = async (l) => {
143
- const d = a.maxToolRoundtrips ?? 5;
144
- let n = 0;
145
- for (; n < d; ) {
146
- const i = w();
142
+ }, g = async (l) => {
143
+ const f = a.maxToolRoundtrips ?? 5;
144
+ let u = 0;
145
+ for (; u < f; ) {
146
+ const c = p();
147
147
  a.provider.validateConfig();
148
148
  const { stream: E } = await a.provider.chat({
149
149
  model: a.model,
150
- messages: i,
150
+ messages: c,
151
151
  tools: a.tools,
152
152
  signal: l,
153
153
  providerOptions: {
154
154
  ...a.providerOptions,
155
- ...T(a.body)
155
+ ...N(a.body)
156
156
  }
157
- }), g = await f.processStream(E, l, {
157
+ }), h = await n.processStream(E, l, {
158
158
  onFinish: a.onFinish,
159
159
  onError: a.onError,
160
160
  onToolCall: a.onToolCall
161
161
  });
162
- if (g.finishReason !== "tool-calls" || g.toolCalls.length === 0)
162
+ if (h.finishReason !== "tool-calls" || h.toolCalls.length === 0)
163
163
  break;
164
- n++;
164
+ u++;
165
165
  }
166
- }, b = async (l) => {
167
- const d = typeof l == "string" ? l : l?.text ?? u.value;
168
- if (!d.trim()) return;
169
- (typeof l != "object" || l === null || !("text" in l)) && (u.value = "");
170
- const n = {
171
- id: I(),
166
+ }, m = async (l) => {
167
+ const f = typeof l == "string" ? l : l?.text ?? i.value;
168
+ if (!f.trim()) return;
169
+ (typeof l != "object" || l === null || !("text" in l)) && (i.value = "");
170
+ const u = {
171
+ id: O(),
172
172
  role: "user",
173
- parts: [{ type: "text", text: d, state: "done" }],
173
+ parts: [{ type: "text", text: f, state: "done" }],
174
174
  createdAt: /* @__PURE__ */ new Date()
175
175
  };
176
- t.value = [...t.value, n], r.value = void 0, e.value = "submitted", o?.abort(), o = new AbortController();
176
+ e.value = [...e.value, u], s.value = void 0, t.value = "submitted", o?.abort(), o = new AbortController();
177
177
  try {
178
- await x(o.signal), e.value = "ready";
179
- } catch (i) {
178
+ await g(o.signal), t.value = "ready";
179
+ } catch (c) {
180
180
  if (o.signal.aborted) {
181
- e.value = "ready";
181
+ t.value = "ready";
182
182
  return;
183
183
  }
184
- r.value = i instanceof Error ? i : new Error(String(i)), e.value = "error";
184
+ s.value = c instanceof Error ? c : new Error(String(c)), t.value = "error";
185
185
  }
186
186
  }, v = async () => {
187
- const l = t.value, d = l.reduce((n, i, E) => i.role === "assistant" ? E : n, -1);
188
- if (d !== -1) {
189
- t.value = l.slice(0, d), r.value = void 0, e.value = "submitted", o?.abort(), o = new AbortController();
187
+ const l = e.value, f = l.reduce((u, c, E) => c.role === "assistant" ? E : u, -1);
188
+ if (f !== -1) {
189
+ e.value = l.slice(0, f), s.value = void 0, t.value = "submitted", o?.abort(), o = new AbortController();
190
190
  try {
191
- await x(o.signal), e.value = "ready";
192
- } catch (n) {
191
+ await g(o.signal), t.value = "ready";
192
+ } catch (u) {
193
193
  if (o.signal.aborted) {
194
- e.value = "ready";
194
+ t.value = "ready";
195
195
  return;
196
196
  }
197
- r.value = n instanceof Error ? n : new Error(String(n)), e.value = "error";
197
+ s.value = u instanceof Error ? u : new Error(String(u)), t.value = "error";
198
198
  }
199
199
  }
200
- }, C = () => {
201
- o?.abort(), o = null, e.value = "ready";
202
- }, h = (l) => {
203
- t.value = typeof l == "function" ? l(t.value) : l;
200
+ }, S = () => {
201
+ o?.abort(), o = null, t.value = "ready";
202
+ }, w = (l) => {
203
+ e.value = typeof l == "function" ? l(e.value) : l;
204
204
  }, y = (l) => {
205
- l?.preventDefault?.(), u.value.trim() && b();
206
- }, c = R(() => e.value === "submitted" || e.value === "streaming"), p = ({ toolCallId: l, result: d }) => {
207
- for (const n of t.value)
208
- for (const i of n.parts)
209
- if (i.type === "tool-call" && i.toolCallId === l) {
210
- i.state = "result", i.result = d, t.value = [...t.value];
205
+ l?.preventDefault?.(), i.value.trim() && m();
206
+ }, d = R(() => t.value === "submitted" || t.value === "streaming"), b = ({ toolCallId: l, result: f }) => {
207
+ for (const u of e.value)
208
+ for (const c of u.parts)
209
+ if (c.type === "tool-call" && c.toolCallId === l) {
210
+ c.state = "result", c.result = f, e.value = [...e.value];
211
211
  return;
212
212
  }
213
- }, S = () => {
214
- r.value = void 0, e.value === "error" && (e.value = "ready");
213
+ }, x = () => {
214
+ s.value = void 0, t.value === "error" && (t.value = "ready");
215
215
  };
216
- return O(() => {
216
+ return I(() => {
217
217
  o?.abort();
218
218
  }), {
219
- messages: t,
220
- status: R(() => e.value),
221
- error: r,
222
- id: s,
223
- input: u,
224
- sendMessage: b,
219
+ messages: e,
220
+ status: R(() => t.value),
221
+ error: s,
222
+ id: r,
223
+ input: i,
224
+ sendMessage: m,
225
225
  regenerate: v,
226
- stop: C,
227
- setMessages: h,
226
+ stop: S,
227
+ setMessages: w,
228
228
  handleSubmit: y,
229
- isLoading: c,
230
- addToolResult: p,
231
- clearError: S
229
+ isLoading: d,
230
+ addToolResult: b,
231
+ clearError: x
232
232
  };
233
233
  }
234
234
  function J(a) {
235
- const s = a.id ?? I(), t = m(""), e = m("ready"), r = m(void 0), u = m("");
235
+ const r = a.id ?? O(), e = C(""), t = C("ready"), s = C(void 0), i = C("");
236
236
  let o = null;
237
- const f = async (C) => {
238
- const h = C ?? u.value;
239
- if (!h.trim()) return t.value;
240
- C || (u.value = ""), t.value = "", r.value = void 0, e.value = "submitted", o?.abort(), o = new AbortController();
237
+ const n = async (S) => {
238
+ const w = S ?? i.value;
239
+ if (!w.trim()) return e.value;
240
+ S || (i.value = ""), e.value = "", s.value = void 0, t.value = "submitted", o?.abort(), o = new AbortController();
241
241
  try {
242
242
  a.provider.validateConfig();
243
243
  const y = a.system ? [
244
244
  { role: "system", content: a.system },
245
- { role: "user", content: h }
246
- ] : [{ role: "user", content: h }], { stream: c } = await a.provider.chat({
245
+ { role: "user", content: w }
246
+ ] : [{ role: "user", content: w }], { stream: d } = await a.provider.chat({
247
247
  model: a.model,
248
248
  messages: y,
249
249
  signal: o.signal
250
250
  });
251
- e.value = "streaming";
252
- const p = c.getReader();
253
- let S = "stop", l;
251
+ t.value = "streaming";
252
+ const b = d.getReader();
253
+ let x = "stop", l;
254
254
  for (; !o.signal.aborted; ) {
255
- const { done: d, value: n } = await p.read();
256
- if (d) break;
257
- if (n.type === "text-delta")
258
- t.value += n.text;
259
- else if (n.type === "finish")
260
- S = n.finishReason, l = n.usage;
261
- else if (n.type === "error")
262
- throw n.error;
255
+ const { done: f, value: u } = await b.read();
256
+ if (f) break;
257
+ if (u.type === "text-delta")
258
+ e.value += u.text;
259
+ else if (u.type === "finish")
260
+ x = u.finishReason, l = u.usage;
261
+ else if (u.type === "error")
262
+ throw u.error;
263
263
  }
264
- return p.releaseLock(), e.value = "ready", a.onFinish?.(t.value, { finishReason: S, usage: l }), t.value;
264
+ return b.releaseLock(), t.value = "ready", a.onFinish?.(e.value, { finishReason: x, usage: l }), e.value;
265
265
  } catch (y) {
266
266
  if (!o || o.signal.aborted)
267
- return e.value = "ready", t.value;
268
- const c = y instanceof Error ? y : new Error(String(y));
269
- return r.value = c, e.value = "error", a.onError?.(c), t.value;
267
+ return t.value = "ready", e.value;
268
+ const d = y instanceof Error ? y : new Error(String(y));
269
+ return s.value = d, t.value = "error", a.onError?.(d), e.value;
270
270
  }
271
- }, w = () => {
272
- o?.abort(), o = null, e.value = "ready";
273
- }, x = () => {
274
- r.value = void 0, e.value === "error" && (e.value = "ready");
275
- }, b = (C) => {
276
- C?.preventDefault?.(), u.value.trim() && f();
277
- }, v = R(() => e.value === "submitted" || e.value === "streaming");
278
- return O(() => {
271
+ }, p = () => {
272
+ o?.abort(), o = null, t.value = "ready";
273
+ }, g = () => {
274
+ s.value = void 0, t.value === "error" && (t.value = "ready");
275
+ }, m = (S) => {
276
+ S?.preventDefault?.(), i.value.trim() && n();
277
+ }, v = R(() => t.value === "submitted" || t.value === "streaming");
278
+ return I(() => {
279
279
  o?.abort();
280
280
  }), {
281
- completion: t,
282
- status: R(() => e.value),
283
- error: r,
281
+ completion: e,
282
+ status: R(() => t.value),
283
+ error: s,
284
284
  isLoading: v,
285
- id: s,
286
- input: u,
287
- complete: f,
288
- stop: w,
289
- clearError: x,
290
- handleSubmit: b
285
+ id: r,
286
+ input: i,
287
+ complete: n,
288
+ stop: p,
289
+ clearError: g,
290
+ handleSubmit: m
291
291
  };
292
292
  }
293
- function L(a) {
294
- const s = a.id ?? I(), t = m(void 0), e = m(void 0), r = m("ready"), u = m(void 0), o = m("");
295
- let f = null;
296
- const w = async (C) => {
297
- const h = C ?? o.value;
298
- if (!h.trim()) return t.value;
299
- C || (o.value = ""), t.value = void 0, e.value = void 0, u.value = void 0, r.value = "submitted", f?.abort(), f = new AbortController();
293
+ function F(a) {
294
+ const r = a.id ?? O(), e = C(void 0), t = C(void 0), s = C("ready"), i = C(void 0), o = C("");
295
+ let n = null;
296
+ const p = async (S) => {
297
+ const w = S ?? o.value;
298
+ if (!w.trim()) return e.value;
299
+ S || (o.value = ""), e.value = void 0, t.value = void 0, i.value = void 0, s.value = "submitted", n?.abort(), n = new AbortController();
300
300
  try {
301
301
  a.provider.validateConfig();
302
302
  const y = [
@@ -306,98 +306,178 @@ function L(a) {
306
306
  "Output ONLY the JSON object, no other text."
307
307
  ].filter(Boolean).join(`
308
308
 
309
- `), { stream: c } = await a.provider.chat({
309
+ `), { stream: d } = await a.provider.chat({
310
310
  model: a.model,
311
311
  messages: [
312
312
  { role: "system", content: y },
313
- { role: "user", content: h }
313
+ { role: "user", content: w }
314
314
  ],
315
- signal: f.signal,
315
+ signal: n.signal,
316
316
  providerOptions: {
317
317
  response_format: { type: "json_object" }
318
318
  }
319
319
  });
320
- r.value = "streaming";
321
- const p = c.getReader();
322
- let S = "", l = "stop", d;
323
- for (; !f.signal.aborted; ) {
324
- const { done: n, value: i } = await p.read();
325
- if (n) break;
326
- if (i.type === "text-delta") {
327
- S += i.text;
320
+ s.value = "streaming";
321
+ const b = d.getReader();
322
+ let x = "", l = "stop", f;
323
+ for (; !n.signal.aborted; ) {
324
+ const { done: u, value: c } = await b.read();
325
+ if (u) break;
326
+ if (c.type === "text-delta") {
327
+ x += c.text;
328
328
  try {
329
- e.value = A(S);
329
+ t.value = M(x);
330
330
  } catch {
331
331
  }
332
- } else if (i.type === "finish")
333
- l = i.finishReason, d = i.usage;
334
- else if (i.type === "error")
335
- throw i.error;
332
+ } else if (c.type === "finish")
333
+ l = c.finishReason, f = c.usage;
334
+ else if (c.type === "error")
335
+ throw c.error;
336
336
  }
337
- p.releaseLock();
337
+ b.releaseLock();
338
338
  try {
339
- t.value = JSON.parse(S), e.value = t.value;
339
+ e.value = JSON.parse(x), t.value = e.value;
340
340
  } catch {
341
- throw new Error(`Failed to parse JSON response: ${S.slice(0, 200)}`);
341
+ throw new Error(`Failed to parse JSON response: ${x.slice(0, 200)}`);
342
342
  }
343
- return r.value = "ready", a.onFinish?.(t.value, { finishReason: l, usage: d }), t.value;
343
+ return s.value = "ready", a.onFinish?.(e.value, { finishReason: l, usage: f }), e.value;
344
344
  } catch (y) {
345
- if (f.signal.aborted)
346
- return r.value = "ready", t.value;
347
- const c = y instanceof Error ? y : new Error(String(y));
348
- u.value = c, r.value = "error", a.onError?.(c);
345
+ if (n.signal.aborted)
346
+ return s.value = "ready", e.value;
347
+ const d = y instanceof Error ? y : new Error(String(y));
348
+ i.value = d, s.value = "error", a.onError?.(d);
349
349
  return;
350
350
  }
351
- }, x = () => {
352
- f?.abort(), f = null, r.value = "ready";
353
- }, b = () => {
354
- u.value = void 0, r.value === "error" && (r.value = "ready");
355
- }, v = R(() => r.value === "submitted" || r.value === "streaming");
356
- return O(() => {
357
- f?.abort();
351
+ }, g = () => {
352
+ n?.abort(), n = null, s.value = "ready";
353
+ }, m = () => {
354
+ i.value = void 0, s.value === "error" && (s.value = "ready");
355
+ }, v = R(() => s.value === "submitted" || s.value === "streaming");
356
+ return I(() => {
357
+ n?.abort();
358
358
  }), {
359
- object: t,
360
- partialObject: e,
361
- status: R(() => r.value),
362
- error: u,
359
+ object: e,
360
+ partialObject: t,
361
+ status: R(() => s.value),
362
+ error: i,
363
363
  isLoading: v,
364
- id: s,
364
+ id: r,
365
365
  input: o,
366
- submit: w,
367
- stop: x,
368
- clearError: b
366
+ submit: p,
367
+ stop: g,
368
+ clearError: m
369
369
  };
370
370
  }
371
- function A(a) {
372
- let s = a.trim();
373
- if (!s.startsWith("{")) return;
374
- let t = 0, e = !1, r = !1;
375
- for (let u = 0; u < s.length; u++) {
376
- const o = s[u];
377
- if (r) {
378
- r = !1;
371
+ function M(a) {
372
+ let r = a.trim();
373
+ if (!r.startsWith("{")) return;
374
+ let e = 0, t = !1, s = !1;
375
+ for (let i = 0; i < r.length; i++) {
376
+ const o = r[i];
377
+ if (s) {
378
+ s = !1;
379
379
  continue;
380
380
  }
381
381
  if (o === "\\") {
382
- r = !0;
382
+ s = !0;
383
383
  continue;
384
384
  }
385
385
  if (o === '"') {
386
- e = !e;
386
+ t = !t;
387
387
  continue;
388
388
  }
389
- e || (o === "{" && t++, o === "}" && t--);
389
+ t || (o === "{" && e++, o === "}" && e--);
390
390
  }
391
- for (; t > 0; )
392
- s += "}", t--;
391
+ for (; e > 0; )
392
+ r += "}", e--;
393
393
  try {
394
- return JSON.parse(s);
394
+ return JSON.parse(r);
395
395
  } catch {
396
396
  return;
397
397
  }
398
398
  }
399
+ function L(a, r) {
400
+ return r.length ? {
401
+ id: a.id,
402
+ supportsStreaming: a.supportsStreaming,
403
+ validateConfig() {
404
+ a.validateConfig();
405
+ },
406
+ async chat(e) {
407
+ let t = e;
408
+ for (const n of r)
409
+ n.beforeRequest && (t = await n.beforeRequest(t, a));
410
+ let s;
411
+ try {
412
+ s = await a.chat(t);
413
+ } catch (n) {
414
+ let p = n instanceof Error ? n : new Error(String(n));
415
+ for (const g of r)
416
+ if (g.onError) {
417
+ const m = await g.onError(p, t);
418
+ m instanceof Error && (p = m);
419
+ }
420
+ throw p;
421
+ }
422
+ const o = r.some((n) => n.afterStream) ? s.stream.pipeThrough(new TransformStream({
423
+ async transform(n, p) {
424
+ let g = n;
425
+ for (const m of r) {
426
+ if (!g) break;
427
+ m.afterStream && (g = await m.afterStream(g, t));
428
+ }
429
+ g && p.enqueue(g);
430
+ }
431
+ })) : s.stream;
432
+ for (const n of r)
433
+ n.onFinish && n.onFinish(t, s);
434
+ return { stream: o, response: s.response };
435
+ }
436
+ } : a;
437
+ }
438
+ function $(a = "vue-llm") {
439
+ return {
440
+ beforeRequest(r) {
441
+ return console.log(`[${a}] → ${r.model}`, r.messages.length, "messages"), r;
442
+ },
443
+ afterStream(r) {
444
+ return r.type === "finish" && console.log(`[${a}] ← finish:`, r.finishReason, r.usage ?? ""), r;
445
+ },
446
+ onError(r) {
447
+ console.error(`[${a}] error:`, r.message);
448
+ }
449
+ };
450
+ }
451
+ function D(a = 3, r = 1e3) {
452
+ return {
453
+ async onError(e, t) {
454
+ const s = e.message;
455
+ if (!(s.includes("429") || s.includes("500") || s.includes("502") || s.includes("503"))) return e;
456
+ for (let o = 0; o < a; o++)
457
+ await new Promise((n) => setTimeout(n, r * (o + 1))), console.log(`[vue-llm] retry ${o + 1}/${a}`);
458
+ return e;
459
+ }
460
+ };
461
+ }
462
+ function q(a) {
463
+ return {
464
+ beforeRequest(r) {
465
+ return {
466
+ ...r,
467
+ providerOptions: {
468
+ ...r.providerOptions,
469
+ headers: { ...r.providerOptions?.headers, ...a }
470
+ }
471
+ };
472
+ }
473
+ };
474
+ }
399
475
  export {
476
+ q as headerMiddleware,
477
+ $ as loggingMiddleware,
478
+ D as retryMiddleware,
400
479
  j as useChat,
401
480
  J as useCompletion,
402
- L as useObject
481
+ F as useObject,
482
+ L as wrapProvider
403
483
  };