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