vue-llm 1.4.0 → 1.4.1
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/components/MessageParts.vue.d.ts +1 -1
- package/dist/components.cjs +3 -3
- package/dist/components.d.ts +3 -3
- package/dist/components.js +2 -2
- package/dist/composables/use-chat.d.ts +5 -5
- package/dist/composables/use-completion.d.ts +3 -3
- package/dist/composables/use-object.d.ts +3 -3
- package/dist/composables/use-prompt-input.d.ts +2 -2
- package/dist/composables/use-prompt.d.ts +2 -2
- package/dist/core/devtools.d.ts +2 -2
- package/dist/core/prompt.d.ts +2 -2
- package/dist/core/storage.d.ts +1 -1
- package/dist/core/stream-processor.d.ts +3 -3
- package/dist/index.cjs +3 -3
- package/dist/index.d.ts +24 -24
- package/dist/index.js +252 -244
- package/dist/middleware.d.ts +1 -1
- package/dist/providers/anthropic.d.ts +1 -1
- package/dist/providers/google.d.ts +1 -1
- package/dist/providers/ollama.d.ts +1 -1
- package/dist/providers/openai.d.ts +1 -1
- package/dist/providers/sse.d.ts +2 -2
- package/dist/providers/types.d.ts +1 -1
- package/dist/providers/webllm.d.ts +1 -1
- package/dist/providers.cjs +5 -5
- package/dist/providers.d.ts +12 -12
- package/dist/providers.js +368 -357
- package/dist/server.cjs +3 -3
- package/dist/server.d.ts +2 -2
- package/dist/server.js +85 -83
- package/dist/utils/file.d.ts +1 -1
- package/dist/vue-llm.css +1 -1
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -24,18 +24,18 @@ function K(t) {
|
|
|
24
24
|
l === '"' ? a = !0 : l === "{" || l === "[" ? (e.unshift(l === "{" ? "}" : "]"), o.push({ at: n + 1, closers: e.join("") })) : l === "}" || l === "]" ? (e.shift(), o.push({ at: n + 1, closers: e.join("") })) : l === "," && o.push({ at: n, closers: e.join("") });
|
|
25
25
|
}
|
|
26
26
|
if (a) {
|
|
27
|
-
const n = s ? r.slice(0, -1) : r, l =
|
|
27
|
+
const n = s ? r.slice(0, -1) : r, l = $(n.replace(/\\u[0-9a-fA-F]{0,3}$/, "") + '"' + e.join(""));
|
|
28
28
|
if (l !== void 0) return l;
|
|
29
29
|
} else {
|
|
30
|
-
const n =
|
|
30
|
+
const n = $(r.replace(/[\s,]+$/, "") + e.join(""));
|
|
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], f = $(r.slice(0, l.at).replace(/[\s,]+$/, "") + l.closers);
|
|
35
|
+
if (f !== void 0) return f;
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
function
|
|
38
|
+
function $(t) {
|
|
39
39
|
try {
|
|
40
40
|
return JSON.parse(t);
|
|
41
41
|
} catch {
|
|
@@ -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", f;
|
|
56
|
+
const p = r.getReader();
|
|
57
57
|
try {
|
|
58
58
|
for (; !e.aborted; ) {
|
|
59
|
-
const { done: d, value:
|
|
59
|
+
const { done: d, value: u } = await p.read();
|
|
60
60
|
if (d) break;
|
|
61
|
-
if (this.applyChunk(a,
|
|
62
|
-
const b = s.get(
|
|
61
|
+
if (this.applyChunk(a, u, s), u.type === "tool-call-end") {
|
|
62
|
+
const b = s.get(u.toolCallId) ?? "{}";
|
|
63
63
|
let h;
|
|
64
64
|
try {
|
|
65
65
|
h = JSON.parse(b);
|
|
66
66
|
} catch {
|
|
67
67
|
h = b;
|
|
68
68
|
}
|
|
69
|
-
const
|
|
70
|
-
n.push(
|
|
71
|
-
const
|
|
72
|
-
if (
|
|
73
|
-
|
|
69
|
+
const C = { toolCallId: u.toolCallId, toolName: this.getToolName(a, u.toolCallId), args: h };
|
|
70
|
+
n.push(C);
|
|
71
|
+
const m = this.findToolCallPart(a, u.toolCallId);
|
|
72
|
+
if (m && (m.args = h, m.state = "pending"), o.onToolCall && (o.canHandleTool?.(C.toolName) ?? !0)) {
|
|
73
|
+
m && (m.state = "running"), this.triggerReactivity();
|
|
74
74
|
try {
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
} catch (
|
|
78
|
-
|
|
75
|
+
const w = await o.onToolCall(C, { signal: e });
|
|
76
|
+
m && (w === void 0 ? m.state = "pending" : (m.state = "result", m.result = w), this.triggerReactivity());
|
|
77
|
+
} catch (w) {
|
|
78
|
+
m && (m.state = "error", m.error = w instanceof Error ? w.message : String(w), this.triggerReactivity());
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
|
-
if (
|
|
83
|
-
const b = this.findToolCallPart(a,
|
|
84
|
-
b && (b.state =
|
|
85
|
-
const h = n.findIndex((
|
|
82
|
+
if (u.type === "tool-result") {
|
|
83
|
+
const b = this.findToolCallPart(a, u.toolCallId);
|
|
84
|
+
b && (b.state = u.error !== void 0 ? "error" : "result", b.result = u.result, b.error = u.error);
|
|
85
|
+
const h = n.findIndex((C) => C.toolCallId === u.toolCallId);
|
|
86
86
|
h !== -1 && n.splice(h, 1);
|
|
87
87
|
}
|
|
88
|
-
if (
|
|
89
|
-
throw
|
|
88
|
+
if (u.type === "finish" && (l = u.finishReason, f = u.usage), u.type === "error")
|
|
89
|
+
throw u.error;
|
|
90
90
|
this.triggerReactivity();
|
|
91
91
|
}
|
|
92
92
|
} catch (d) {
|
|
93
93
|
if (!e.aborted) {
|
|
94
|
-
const
|
|
95
|
-
throw o.onError?.(
|
|
94
|
+
const u = d instanceof Error ? d : new Error(String(d));
|
|
95
|
+
throw o.onError?.(u), u;
|
|
96
96
|
}
|
|
97
97
|
} finally {
|
|
98
|
-
|
|
98
|
+
p.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: f }), { finishReason: l, usage: f, toolCalls: n };
|
|
101
101
|
}
|
|
102
102
|
getOrCreateAssistantMessage() {
|
|
103
103
|
const r = this.messages.value, e = r[r.length - 1];
|
|
@@ -155,7 +155,7 @@ class te {
|
|
|
155
155
|
}
|
|
156
156
|
}
|
|
157
157
|
const V = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
158
|
-
function
|
|
158
|
+
function j(t = 16) {
|
|
159
159
|
const r = crypto.getRandomValues(new Uint8Array(t));
|
|
160
160
|
let e = "";
|
|
161
161
|
for (let o = 0; o < t; o++)
|
|
@@ -163,157 +163,157 @@ function N(t = 16) {
|
|
|
163
163
|
return e;
|
|
164
164
|
}
|
|
165
165
|
function fe(t) {
|
|
166
|
-
const r = t.id ??
|
|
166
|
+
const r = t.id ?? j(), e = R(t.initialMessages ?? []), o = R("ready"), a = R(void 0), s = R("");
|
|
167
167
|
let n = null;
|
|
168
|
-
const l = R(!t.storage),
|
|
169
|
-
t.onError?.(
|
|
168
|
+
const l = R(!t.storage), f = t.storage, p = (c) => {
|
|
169
|
+
t.onError?.(c instanceof Error ? c : new Error(String(c)));
|
|
170
170
|
};
|
|
171
|
-
if (
|
|
172
|
-
const
|
|
173
|
-
v?.length && e.value ===
|
|
171
|
+
if (f) {
|
|
172
|
+
const c = e.value, y = (v) => {
|
|
173
|
+
v?.length && e.value === c && (e.value = v), l.value = !0;
|
|
174
174
|
};
|
|
175
175
|
try {
|
|
176
|
-
const v =
|
|
176
|
+
const v = f.load(r);
|
|
177
177
|
v instanceof Promise ? v.then(y, (x) => {
|
|
178
|
-
|
|
178
|
+
p(x), l.value = !0;
|
|
179
179
|
}) : y(v);
|
|
180
180
|
} catch (v) {
|
|
181
|
-
|
|
181
|
+
p(v), l.value = !0;
|
|
182
182
|
}
|
|
183
183
|
z([e, o, l], () => {
|
|
184
184
|
if (!(!l.value || o.value === "submitted" || o.value === "streaming"))
|
|
185
185
|
try {
|
|
186
|
-
const v = e.value.length > 0 ?
|
|
187
|
-
v instanceof Promise && v.catch(
|
|
186
|
+
const v = e.value.length > 0 ? f.save(r, e.value) : f.remove(r);
|
|
187
|
+
v instanceof Promise && v.catch(p);
|
|
188
188
|
} catch (v) {
|
|
189
|
-
|
|
189
|
+
p(v);
|
|
190
190
|
}
|
|
191
191
|
}, { deep: !0 });
|
|
192
192
|
}
|
|
193
|
-
const d = new te(e, o,
|
|
194
|
-
const
|
|
193
|
+
const d = new te(e, o, j), u = () => {
|
|
194
|
+
const c = [], y = [
|
|
195
195
|
P(t.system),
|
|
196
196
|
...e.value.filter((v) => v.role === "system").map((v) => v.parts.filter((x) => x.type === "text").map((x) => x.text).join(""))
|
|
197
197
|
].filter(Boolean);
|
|
198
|
-
y.length > 0 &&
|
|
198
|
+
y.length > 0 && c.push({ role: "system", content: y.join(`
|
|
199
199
|
|
|
200
200
|
`) });
|
|
201
201
|
for (const v of e.value) {
|
|
202
202
|
if (v.role === "system") continue;
|
|
203
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
204
|
if (v.role === "user") {
|
|
205
|
-
const S = v.parts.filter((
|
|
205
|
+
const S = v.parts.filter((J) => J.type === "file");
|
|
206
206
|
if (S.length === 0) {
|
|
207
|
-
|
|
207
|
+
c.push({ role: "user", content: M });
|
|
208
208
|
continue;
|
|
209
209
|
}
|
|
210
|
-
const D = S.map((
|
|
210
|
+
const D = S.map((J) => ({
|
|
211
211
|
type: "file",
|
|
212
|
-
url:
|
|
213
|
-
mediaType:
|
|
214
|
-
filename:
|
|
212
|
+
url: J.url,
|
|
213
|
+
mediaType: J.mediaType,
|
|
214
|
+
filename: J.filename
|
|
215
215
|
}));
|
|
216
|
-
M && D.push({ type: "text", text: M }),
|
|
216
|
+
M && D.push({ type: "text", text: M }), c.push({ role: "user", content: D });
|
|
217
217
|
} else if (v.role === "assistant") {
|
|
218
|
-
|
|
218
|
+
c.push({
|
|
219
219
|
role: "assistant",
|
|
220
220
|
content: M,
|
|
221
221
|
toolCalls: O.length > 0 ? O : void 0
|
|
222
222
|
});
|
|
223
223
|
for (const S of v.parts)
|
|
224
|
-
S.type === "tool-call" &&
|
|
224
|
+
S.type === "tool-call" && c.push({
|
|
225
225
|
role: "tool",
|
|
226
226
|
content: S.state === "result" ? JSON.stringify(S.result ?? null) : JSON.stringify({ error: S.error ?? "The tool call was not answered" }),
|
|
227
227
|
toolCallId: S.toolCallId
|
|
228
228
|
});
|
|
229
229
|
}
|
|
230
230
|
}
|
|
231
|
-
return
|
|
232
|
-
}, b = t.tools?.map(({ name:
|
|
233
|
-
const v = t.tools?.find((x) => x.name ===
|
|
234
|
-
return v?.execute ? v.execute(
|
|
235
|
-
},
|
|
236
|
-
const
|
|
237
|
-
return
|
|
231
|
+
return c;
|
|
232
|
+
}, b = t.tools?.map(({ name: c, description: y, parameters: v }) => ({ name: c, description: y, parameters: v })), h = (c, { signal: y }) => {
|
|
233
|
+
const v = t.tools?.find((x) => x.name === c.toolName);
|
|
234
|
+
return v?.execute ? v.execute(c.args, { toolCallId: c.toolCallId, signal: y }) : t.onToolCall?.(c);
|
|
235
|
+
}, C = () => {
|
|
236
|
+
const c = e.value[e.value.length - 1];
|
|
237
|
+
return c?.role === "assistant" && c.parts.some(
|
|
238
238
|
(y) => y.type === "tool-call" && y.state !== "result" && y.state !== "error"
|
|
239
239
|
);
|
|
240
|
-
},
|
|
240
|
+
}, m = async (c) => {
|
|
241
241
|
const y = t.maxToolRoundtrips ?? 5;
|
|
242
242
|
let v = 0;
|
|
243
243
|
for (; v < y; ) {
|
|
244
|
-
const x =
|
|
244
|
+
const x = u();
|
|
245
245
|
t.provider.validateConfig();
|
|
246
246
|
const { stream: M } = await W(t.provider, {
|
|
247
247
|
model: P(t.model),
|
|
248
248
|
messages: x,
|
|
249
249
|
tools: b,
|
|
250
|
-
signal:
|
|
250
|
+
signal: c,
|
|
251
251
|
providerOptions: {
|
|
252
252
|
...t.providerOptions,
|
|
253
253
|
...P(t.body)
|
|
254
254
|
}
|
|
255
|
-
}, r), O = await d.processStream(M,
|
|
255
|
+
}, r), O = await d.processStream(M, c, {
|
|
256
256
|
onFinish: t.onFinish,
|
|
257
257
|
onError: t.onError,
|
|
258
258
|
onToolCall: h,
|
|
259
259
|
canHandleTool: (S) => !!t.onToolCall || !!t.tools?.find((D) => D.name === S)?.execute
|
|
260
260
|
});
|
|
261
|
-
if (O.finishReason !== "tool-calls" || O.toolCalls.length === 0 ||
|
|
261
|
+
if (O.finishReason !== "tool-calls" || O.toolCalls.length === 0 || C())
|
|
262
262
|
break;
|
|
263
263
|
v++;
|
|
264
264
|
}
|
|
265
|
-
},
|
|
266
|
-
const y = typeof
|
|
265
|
+
}, w = async (c) => {
|
|
266
|
+
const y = typeof c == "string" ? c : c?.text ?? s.value;
|
|
267
267
|
let v;
|
|
268
268
|
try {
|
|
269
|
-
v = typeof
|
|
269
|
+
v = typeof c == "object" ? await ee(c.files) : [];
|
|
270
270
|
} catch (O) {
|
|
271
271
|
a.value = O instanceof Error ? O : new Error(String(O)), o.value = "error";
|
|
272
272
|
return;
|
|
273
273
|
}
|
|
274
274
|
if (!y.trim() && v.length === 0) return;
|
|
275
|
-
(typeof
|
|
275
|
+
(typeof c != "object" || c === null || !("text" in c)) && (s.value = "");
|
|
276
276
|
const x = [...v];
|
|
277
277
|
y.trim() && x.push({ type: "text", text: y, state: "done" });
|
|
278
278
|
const M = {
|
|
279
|
-
id:
|
|
279
|
+
id: j(),
|
|
280
280
|
role: "user",
|
|
281
281
|
parts: x,
|
|
282
282
|
createdAt: /* @__PURE__ */ new Date()
|
|
283
283
|
};
|
|
284
|
-
e.value = [...e.value, M], await
|
|
285
|
-
},
|
|
284
|
+
e.value = [...e.value, M], await i();
|
|
285
|
+
}, i = async () => {
|
|
286
286
|
a.value = void 0, o.value = "submitted", n?.abort();
|
|
287
|
-
const
|
|
288
|
-
n =
|
|
289
|
-
const y = () => n ===
|
|
287
|
+
const c = new AbortController();
|
|
288
|
+
n = c;
|
|
289
|
+
const y = () => n === c || n === null;
|
|
290
290
|
try {
|
|
291
|
-
await
|
|
291
|
+
await m(c.signal), y() && (o.value = "ready");
|
|
292
292
|
} catch (v) {
|
|
293
|
-
if (
|
|
293
|
+
if (c.signal.aborted) {
|
|
294
294
|
y() && (o.value = "ready");
|
|
295
295
|
return;
|
|
296
296
|
}
|
|
297
297
|
a.value = v instanceof Error ? v : new Error(String(v)), o.value = "error";
|
|
298
298
|
}
|
|
299
|
-
},
|
|
300
|
-
const
|
|
301
|
-
y !== -1 && (e.value =
|
|
299
|
+
}, g = async () => {
|
|
300
|
+
const c = e.value, y = c.reduce((v, x, M) => x.role === "assistant" ? M : v, -1);
|
|
301
|
+
y !== -1 && (e.value = c.slice(0, y), await i());
|
|
302
302
|
}, A = () => {
|
|
303
303
|
n?.abort(), n = null, o.value = "ready";
|
|
304
|
-
}, E = (
|
|
305
|
-
e.value = typeof
|
|
306
|
-
}, I = (
|
|
307
|
-
|
|
308
|
-
},
|
|
309
|
-
const x = e.value.flatMap((D) => D.parts).find((D) => D.type === "tool-call" && D.toolCallId ===
|
|
304
|
+
}, E = (c) => {
|
|
305
|
+
e.value = typeof c == "function" ? c(e.value) : c;
|
|
306
|
+
}, I = (c) => {
|
|
307
|
+
c?.preventDefault?.(), s.value.trim() && w();
|
|
308
|
+
}, N = F(() => o.value === "submitted" || o.value === "streaming"), T = async ({ toolCallId: c, result: y, error: v }) => {
|
|
309
|
+
const x = e.value.flatMap((D) => D.parts).find((D) => D.type === "tool-call" && D.toolCallId === c);
|
|
310
310
|
if (!x) return;
|
|
311
311
|
v !== void 0 ? (x.state = "error", x.error = v) : (x.state = "result", x.result = y), e.value = [...e.value];
|
|
312
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 && !
|
|
314
|
-
},
|
|
313
|
+
(t.autoSendToolResults ?? !0) && O && !S && !C() && await i();
|
|
314
|
+
}, L = () => {
|
|
315
315
|
a.value = void 0, o.value === "error" && (o.value = "ready");
|
|
316
|
-
},
|
|
316
|
+
}, q = k({
|
|
317
317
|
id: r,
|
|
318
318
|
kind: "chat",
|
|
319
319
|
providerId: t.provider.id,
|
|
@@ -325,68 +325,72 @@ function fe(t) {
|
|
|
325
325
|
clear: () => E([])
|
|
326
326
|
});
|
|
327
327
|
return H() && B(() => {
|
|
328
|
-
n?.abort(),
|
|
328
|
+
n?.abort(), q();
|
|
329
329
|
}), {
|
|
330
330
|
messages: e,
|
|
331
331
|
status: F(() => o.value),
|
|
332
332
|
error: a,
|
|
333
333
|
id: r,
|
|
334
334
|
input: s,
|
|
335
|
-
sendMessage:
|
|
336
|
-
regenerate:
|
|
335
|
+
sendMessage: w,
|
|
336
|
+
regenerate: g,
|
|
337
337
|
stop: A,
|
|
338
338
|
setMessages: E,
|
|
339
339
|
handleSubmit: I,
|
|
340
|
-
isLoading:
|
|
341
|
-
addToolResult:
|
|
342
|
-
clearError:
|
|
340
|
+
isLoading: N,
|
|
341
|
+
addToolResult: T,
|
|
342
|
+
clearError: L,
|
|
343
343
|
isRestored: F(() => l.value)
|
|
344
344
|
};
|
|
345
345
|
}
|
|
346
346
|
function de(t) {
|
|
347
|
-
const r = t.id ??
|
|
347
|
+
const r = t.id ?? j(), e = R(""), o = R("ready"), a = R(void 0), s = R("");
|
|
348
348
|
let n = null;
|
|
349
349
|
const l = async (h) => {
|
|
350
|
-
const
|
|
351
|
-
if (!
|
|
352
|
-
h || (s.value = ""), e.value = "", a.value = void 0, o.value = "submitted", n?.abort()
|
|
350
|
+
const C = h ?? s.value;
|
|
351
|
+
if (!C.trim()) return e.value;
|
|
352
|
+
h || (s.value = ""), e.value = "", a.value = void 0, o.value = "submitted", n?.abort();
|
|
353
|
+
const m = new AbortController();
|
|
354
|
+
n = m;
|
|
353
355
|
try {
|
|
354
356
|
t.provider.validateConfig();
|
|
355
|
-
const
|
|
356
|
-
{ role: "system", content:
|
|
357
|
-
{ role: "user", content:
|
|
358
|
-
] : [{ role: "user", content:
|
|
357
|
+
const w = P(t.system), i = w ? [
|
|
358
|
+
{ role: "system", content: w },
|
|
359
|
+
{ role: "user", content: C }
|
|
360
|
+
] : [{ role: "user", content: C }], { stream: g } = await W(t.provider, {
|
|
359
361
|
model: P(t.model),
|
|
360
|
-
messages:
|
|
361
|
-
signal:
|
|
362
|
+
messages: i,
|
|
363
|
+
signal: m.signal
|
|
362
364
|
}, r);
|
|
365
|
+
if (m.signal.aborted) return e.value;
|
|
363
366
|
o.value = "streaming";
|
|
364
|
-
const
|
|
365
|
-
let
|
|
366
|
-
for (; !
|
|
367
|
-
const { done:
|
|
368
|
-
if (
|
|
367
|
+
const A = g.getReader();
|
|
368
|
+
let E = "stop", I;
|
|
369
|
+
for (; !m.signal.aborted; ) {
|
|
370
|
+
const { done: N, value: T } = await A.read();
|
|
371
|
+
if (m.signal.aborted || N) break;
|
|
369
372
|
if (T.type === "text-delta")
|
|
370
373
|
e.value += T.text;
|
|
371
374
|
else if (T.type === "finish")
|
|
372
|
-
|
|
375
|
+
E = T.finishReason, I = T.usage;
|
|
373
376
|
else if (T.type === "error")
|
|
374
377
|
throw T.error;
|
|
375
378
|
}
|
|
376
|
-
return
|
|
377
|
-
} catch (
|
|
378
|
-
if (
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
379
|
+
return A.releaseLock(), m.signal.aborted || (o.value = "ready", t.onFinish?.(e.value, { finishReason: E, usage: I })), e.value;
|
|
380
|
+
} catch (w) {
|
|
381
|
+
if (m.signal.aborted) return e.value;
|
|
382
|
+
const i = w instanceof Error ? w : new Error(String(w));
|
|
383
|
+
return a.value = i, o.value = "error", t.onError?.(i), e.value;
|
|
384
|
+
} finally {
|
|
385
|
+
n === m && (n = null);
|
|
382
386
|
}
|
|
383
|
-
},
|
|
387
|
+
}, f = () => {
|
|
384
388
|
n?.abort(), n = null, o.value = "ready";
|
|
385
|
-
},
|
|
389
|
+
}, p = () => {
|
|
386
390
|
a.value = void 0, o.value === "error" && (o.value = "ready");
|
|
387
391
|
}, d = (h) => {
|
|
388
392
|
h?.preventDefault?.(), s.value.trim() && l();
|
|
389
|
-
},
|
|
393
|
+
}, u = F(() => o.value === "submitted" || o.value === "streaming"), b = k({
|
|
390
394
|
id: r,
|
|
391
395
|
kind: "completion",
|
|
392
396
|
providerId: t.provider.id,
|
|
@@ -394,7 +398,7 @@ function de(t) {
|
|
|
394
398
|
status: o,
|
|
395
399
|
error: a,
|
|
396
400
|
output: e,
|
|
397
|
-
stop:
|
|
401
|
+
stop: f
|
|
398
402
|
});
|
|
399
403
|
return H() && B(() => {
|
|
400
404
|
n?.abort(), b();
|
|
@@ -402,76 +406,80 @@ function de(t) {
|
|
|
402
406
|
completion: e,
|
|
403
407
|
status: F(() => o.value),
|
|
404
408
|
error: a,
|
|
405
|
-
isLoading:
|
|
409
|
+
isLoading: u,
|
|
406
410
|
id: r,
|
|
407
411
|
input: s,
|
|
408
412
|
complete: l,
|
|
409
|
-
stop:
|
|
410
|
-
clearError:
|
|
413
|
+
stop: f,
|
|
414
|
+
clearError: p,
|
|
411
415
|
handleSubmit: d
|
|
412
416
|
};
|
|
413
417
|
}
|
|
414
418
|
function ve(t) {
|
|
415
|
-
const r = t.id ??
|
|
419
|
+
const r = t.id ?? j(), e = R(void 0), o = R(void 0), a = R("ready"), s = R(void 0), n = R("");
|
|
416
420
|
let l = null;
|
|
417
|
-
const
|
|
418
|
-
const
|
|
419
|
-
if (!
|
|
420
|
-
h || (n.value = ""), e.value = void 0, o.value = void 0, s.value = void 0, a.value = "submitted", l?.abort()
|
|
421
|
+
const f = async (h) => {
|
|
422
|
+
const C = h ?? n.value;
|
|
423
|
+
if (!C.trim()) return e.value;
|
|
424
|
+
h || (n.value = ""), e.value = void 0, o.value = void 0, s.value = void 0, a.value = "submitted", l?.abort();
|
|
425
|
+
const m = new AbortController();
|
|
426
|
+
l = m;
|
|
421
427
|
try {
|
|
422
428
|
t.provider.validateConfig();
|
|
423
|
-
const
|
|
429
|
+
const w = [
|
|
424
430
|
P(t.system) ?? "",
|
|
425
431
|
"Respond with valid JSON matching this schema:",
|
|
426
432
|
JSON.stringify(t.schema, null, 2),
|
|
427
433
|
"Output ONLY the JSON object, no other text."
|
|
428
434
|
].filter(Boolean).join(`
|
|
429
435
|
|
|
430
|
-
`), { stream:
|
|
436
|
+
`), { stream: i } = await W(t.provider, {
|
|
431
437
|
model: P(t.model),
|
|
432
438
|
messages: [
|
|
433
|
-
{ role: "system", content:
|
|
434
|
-
{ role: "user", content:
|
|
439
|
+
{ role: "system", content: w },
|
|
440
|
+
{ role: "user", content: C }
|
|
435
441
|
],
|
|
436
|
-
signal:
|
|
442
|
+
signal: m.signal,
|
|
437
443
|
providerOptions: {
|
|
438
444
|
response_format: { type: "json_object" }
|
|
439
445
|
}
|
|
440
446
|
}, r);
|
|
447
|
+
if (m.signal.aborted) return e.value;
|
|
441
448
|
a.value = "streaming";
|
|
442
|
-
const
|
|
443
|
-
let
|
|
444
|
-
for (; !
|
|
445
|
-
const { done:
|
|
446
|
-
if (
|
|
449
|
+
const g = i.getReader();
|
|
450
|
+
let A = "", E = "stop", I;
|
|
451
|
+
for (; !m.signal.aborted; ) {
|
|
452
|
+
const { done: N, value: T } = await g.read();
|
|
453
|
+
if (m.signal.aborted || N) break;
|
|
447
454
|
if (T.type === "text-delta") {
|
|
448
|
-
|
|
449
|
-
const L = K(
|
|
455
|
+
A += T.text;
|
|
456
|
+
const L = K(A);
|
|
450
457
|
L !== null && typeof L == "object" && (o.value = L);
|
|
451
458
|
} else if (T.type === "finish")
|
|
452
|
-
|
|
459
|
+
E = T.finishReason, I = T.usage;
|
|
453
460
|
else if (T.type === "error")
|
|
454
461
|
throw T.error;
|
|
455
462
|
}
|
|
456
|
-
|
|
463
|
+
if (g.releaseLock(), m.signal.aborted) return e.value;
|
|
457
464
|
try {
|
|
458
|
-
e.value = JSON.parse(
|
|
465
|
+
e.value = JSON.parse(A), o.value = e.value;
|
|
459
466
|
} catch {
|
|
460
|
-
throw new Error(`Failed to parse JSON response: ${
|
|
467
|
+
throw new Error(`Failed to parse JSON response: ${A.slice(0, 200)}`);
|
|
461
468
|
}
|
|
462
|
-
return a.value = "ready", t.onFinish?.(e.value, { finishReason:
|
|
463
|
-
} catch (
|
|
464
|
-
if (
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
s.value = C, a.value = "error", t.onError?.(C);
|
|
469
|
+
return a.value = "ready", t.onFinish?.(e.value, { finishReason: E, usage: I }), e.value;
|
|
470
|
+
} catch (w) {
|
|
471
|
+
if (m.signal.aborted) return e.value;
|
|
472
|
+
const i = w instanceof Error ? w : new Error(String(w));
|
|
473
|
+
s.value = i, a.value = "error", t.onError?.(i);
|
|
468
474
|
return;
|
|
475
|
+
} finally {
|
|
476
|
+
l === m && (l = null);
|
|
469
477
|
}
|
|
470
|
-
},
|
|
478
|
+
}, p = () => {
|
|
471
479
|
l?.abort(), l = null, a.value = "ready";
|
|
472
480
|
}, d = () => {
|
|
473
481
|
s.value = void 0, a.value === "error" && (a.value = "ready");
|
|
474
|
-
},
|
|
482
|
+
}, u = F(() => a.value === "submitted" || a.value === "streaming"), b = k({
|
|
475
483
|
id: r,
|
|
476
484
|
kind: "object",
|
|
477
485
|
providerId: t.provider.id,
|
|
@@ -479,7 +487,7 @@ function ve(t) {
|
|
|
479
487
|
status: a,
|
|
480
488
|
error: s,
|
|
481
489
|
output: o,
|
|
482
|
-
stop:
|
|
490
|
+
stop: p
|
|
483
491
|
});
|
|
484
492
|
return H() && B(() => {
|
|
485
493
|
l?.abort(), b();
|
|
@@ -488,11 +496,11 @@ function ve(t) {
|
|
|
488
496
|
partialObject: o,
|
|
489
497
|
status: F(() => a.value),
|
|
490
498
|
error: s,
|
|
491
|
-
isLoading:
|
|
499
|
+
isLoading: u,
|
|
492
500
|
id: r,
|
|
493
501
|
input: n,
|
|
494
|
-
submit:
|
|
495
|
-
stop:
|
|
502
|
+
submit: f,
|
|
503
|
+
stop: p,
|
|
496
504
|
clearError: d
|
|
497
505
|
};
|
|
498
506
|
}
|
|
@@ -517,7 +525,7 @@ function me(t, r = {}) {
|
|
|
517
525
|
render: e,
|
|
518
526
|
toUIMessages(o) {
|
|
519
527
|
return e(o).map((a) => ({
|
|
520
|
-
id:
|
|
528
|
+
id: j(),
|
|
521
529
|
role: a.role,
|
|
522
530
|
parts: [{ type: "text", text: a.content, state: "done" }],
|
|
523
531
|
createdAt: /* @__PURE__ */ new Date()
|
|
@@ -547,91 +555,91 @@ function pe(t, r) {
|
|
|
547
555
|
function ge(t, r = {}) {
|
|
548
556
|
const e = R(null), o = R([]), a = R(!1);
|
|
549
557
|
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)),
|
|
551
|
-
const
|
|
552
|
-
if (!
|
|
553
|
-
const A =
|
|
558
|
+
const n = F(() => t.status.value === "submitted" || t.status.value === "streaming"), l = F(() => !n.value && (t.input.value.trim() !== "" || o.value.length > 0)), f = (i) => r.accept ? r.accept.split(",").map((g) => g.trim().toLowerCase()).filter(Boolean).some((g) => g.startsWith(".") ? i.name.toLowerCase().endsWith(g) : g.endsWith("/*") ? i.type.toLowerCase().startsWith(g.slice(0, -1)) : i.type.toLowerCase() === g) : !0, p = (i) => {
|
|
559
|
+
const g = Array.from(i ?? []);
|
|
560
|
+
if (!g.length) return;
|
|
561
|
+
const A = g.filter((I) => !f(I));
|
|
554
562
|
A.length && r.onReject?.(A, "type");
|
|
555
|
-
let E =
|
|
563
|
+
let E = g.filter(f);
|
|
556
564
|
if (r.maxFiles !== void 0) {
|
|
557
565
|
const I = Math.max(0, r.maxFiles - o.value.length);
|
|
558
566
|
E.length > I && r.onReject?.(E.slice(I), "count"), E = E.slice(0, I);
|
|
559
567
|
}
|
|
560
568
|
E.length && (o.value = [...o.value, ...E]);
|
|
561
|
-
}, d = (
|
|
562
|
-
o.value = o.value.filter((
|
|
563
|
-
},
|
|
564
|
-
const
|
|
565
|
-
if (!
|
|
566
|
-
const
|
|
567
|
-
|
|
568
|
-
const
|
|
569
|
-
|
|
569
|
+
}, d = (i) => {
|
|
570
|
+
o.value = o.value.filter((g) => g !== i);
|
|
571
|
+
}, u = () => {
|
|
572
|
+
const i = e.value;
|
|
573
|
+
if (!i || typeof getComputedStyle > "u") return;
|
|
574
|
+
const g = getComputedStyle(i), A = parseFloat(g.lineHeight) || parseFloat(g.fontSize) * 1.5 || 20, E = (parseFloat(g.paddingTop) || 0) + (parseFloat(g.paddingBottom) || 0), I = (parseFloat(g.borderTopWidth) || 0) + (parseFloat(g.borderBottomWidth) || 0), N = g.boxSizing === "border-box", T = (c) => N ? c + I : c - E, L = A * (r.maxRows ?? 8) + E;
|
|
575
|
+
i.style.height = "auto";
|
|
576
|
+
const q = i.scrollHeight > L;
|
|
577
|
+
i.style.height = `${T(Math.min(i.scrollHeight, L))}px`, i.style.overflowY = q ? "auto" : "hidden";
|
|
570
578
|
};
|
|
571
|
-
z(t.input, () => U(
|
|
579
|
+
z(t.input, () => U(u)), z(e, () => U(u));
|
|
572
580
|
const b = async () => {
|
|
573
581
|
if (!l.value) return;
|
|
574
|
-
const
|
|
575
|
-
t.input.value = "", o.value = [], await t.sendMessage(
|
|
576
|
-
}, h = (
|
|
577
|
-
|
|
578
|
-
},
|
|
579
|
-
const
|
|
580
|
-
|
|
581
|
-
},
|
|
582
|
+
const i = t.input.value, g = o.value;
|
|
583
|
+
t.input.value = "", o.value = [], await t.sendMessage(g.length ? { text: i, files: g } : i);
|
|
584
|
+
}, h = (i) => {
|
|
585
|
+
i.key !== "Enter" || i.shiftKey || r.submitOnEnter === !1 || i.isComposing || i.keyCode === 229 || (i.preventDefault(), b());
|
|
586
|
+
}, C = (i) => {
|
|
587
|
+
const g = Array.from(i.clipboardData?.files ?? []);
|
|
588
|
+
g.length && (i.preventDefault(), p(g));
|
|
589
|
+
}, m = (i) => Array.from(i.dataTransfer?.types ?? []).includes("Files");
|
|
582
590
|
return {
|
|
583
591
|
textarea: e,
|
|
584
592
|
files: o,
|
|
585
593
|
isDragging: a,
|
|
586
594
|
canSubmit: l,
|
|
587
595
|
submit: b,
|
|
588
|
-
addFiles:
|
|
596
|
+
addFiles: p,
|
|
589
597
|
removeFile: d,
|
|
590
|
-
handlers: { onKeydown: h, onPaste:
|
|
598
|
+
handlers: { onKeydown: h, onPaste: C },
|
|
591
599
|
dropzone: {
|
|
592
|
-
onDragenter(
|
|
593
|
-
|
|
600
|
+
onDragenter(i) {
|
|
601
|
+
m(i) && (i.preventDefault(), s++, a.value = !0);
|
|
594
602
|
},
|
|
595
|
-
onDragover(
|
|
596
|
-
|
|
603
|
+
onDragover(i) {
|
|
604
|
+
m(i) && i.preventDefault();
|
|
597
605
|
},
|
|
598
|
-
onDragleave(
|
|
599
|
-
|
|
606
|
+
onDragleave(i) {
|
|
607
|
+
m(i) && (s = Math.max(0, s - 1), s === 0 && (a.value = !1));
|
|
600
608
|
},
|
|
601
|
-
onDrop(
|
|
602
|
-
|
|
609
|
+
onDrop(i) {
|
|
610
|
+
m(i) && (i.preventDefault(), s = 0, a.value = !1, p(i.dataTransfer?.files));
|
|
603
611
|
}
|
|
604
612
|
},
|
|
605
|
-
resize:
|
|
613
|
+
resize: u
|
|
606
614
|
};
|
|
607
615
|
}
|
|
608
616
|
function he(t = {}) {
|
|
609
617
|
const r = R(null), e = R(!0), o = t.threshold ?? 48;
|
|
610
618
|
let a = !1;
|
|
611
619
|
const s = (d) => d.scrollHeight - d.scrollTop - d.clientHeight, n = (d = "auto") => {
|
|
612
|
-
const
|
|
613
|
-
|
|
620
|
+
const u = r.value;
|
|
621
|
+
u && (a = !0, e.value = !0, typeof u.scrollTo == "function" ? u.scrollTo({ top: u.scrollHeight, behavior: d }) : u.scrollTop = u.scrollHeight, oe(() => {
|
|
614
622
|
a = !1;
|
|
615
623
|
}));
|
|
616
624
|
}, l = () => {
|
|
617
625
|
const d = r.value;
|
|
618
626
|
!d || a || (e.value = s(d) <= o);
|
|
619
|
-
},
|
|
627
|
+
}, f = () => {
|
|
620
628
|
e.value && n();
|
|
621
629
|
};
|
|
622
|
-
let
|
|
630
|
+
let p = () => {
|
|
623
631
|
};
|
|
624
632
|
return z(r, (d) => {
|
|
625
|
-
if (
|
|
633
|
+
if (p(), !d) return;
|
|
626
634
|
d.addEventListener("scroll", l, { passive: !0 });
|
|
627
|
-
const
|
|
628
|
-
|
|
629
|
-
const b = typeof ResizeObserver < "u" ? new ResizeObserver(
|
|
630
|
-
b?.observe(d), d.firstElementChild && b?.observe(d.firstElementChild), n(),
|
|
631
|
-
d.removeEventListener("scroll", l),
|
|
635
|
+
const u = typeof MutationObserver < "u" ? new MutationObserver(f) : void 0;
|
|
636
|
+
u?.observe(d, { childList: !0, subtree: !0, characterData: !0 });
|
|
637
|
+
const b = typeof ResizeObserver < "u" ? new ResizeObserver(f) : void 0;
|
|
638
|
+
b?.observe(d), d.firstElementChild && b?.observe(d.firstElementChild), n(), p = () => {
|
|
639
|
+
d.removeEventListener("scroll", l), u?.disconnect(), b?.disconnect(), p = () => {
|
|
632
640
|
};
|
|
633
641
|
};
|
|
634
|
-
}, { flush: "post" }), H() && B(() =>
|
|
642
|
+
}, { flush: "post" }), H() && B(() => p()), { container: r, isAtBottom: e, scrollToBottom: n };
|
|
635
643
|
}
|
|
636
644
|
function oe(t) {
|
|
637
645
|
typeof requestAnimationFrame == "function" ? requestAnimationFrame(() => requestAnimationFrame(t)) : setTimeout(t, 32);
|
|
@@ -645,36 +653,36 @@ function ye(t, r) {
|
|
|
645
653
|
},
|
|
646
654
|
async chat(e) {
|
|
647
655
|
let o = e;
|
|
648
|
-
for (const
|
|
649
|
-
|
|
656
|
+
for (const f of r)
|
|
657
|
+
f.beforeRequest && (o = await f.beforeRequest(o, t));
|
|
650
658
|
const a = r.reduceRight(
|
|
651
|
-
(
|
|
652
|
-
(
|
|
659
|
+
(f, p) => p.wrapChat ? (d) => p.wrapChat(f, d) : f,
|
|
660
|
+
(f) => t.chat(f)
|
|
653
661
|
);
|
|
654
662
|
let s;
|
|
655
663
|
try {
|
|
656
664
|
s = await a(o);
|
|
657
|
-
} catch (
|
|
658
|
-
let
|
|
665
|
+
} catch (f) {
|
|
666
|
+
let p = f instanceof Error ? f : new Error(String(f));
|
|
659
667
|
for (const d of r)
|
|
660
668
|
if (d.onError) {
|
|
661
|
-
const
|
|
662
|
-
|
|
669
|
+
const u = await d.onError(p, o);
|
|
670
|
+
u instanceof Error && (p = u);
|
|
663
671
|
}
|
|
664
|
-
throw
|
|
672
|
+
throw p;
|
|
665
673
|
}
|
|
666
|
-
const l = r.some((
|
|
667
|
-
async transform(
|
|
668
|
-
let d =
|
|
669
|
-
for (const
|
|
674
|
+
const l = r.some((f) => f.afterStream) ? s.stream.pipeThrough(new TransformStream({
|
|
675
|
+
async transform(f, p) {
|
|
676
|
+
let d = f;
|
|
677
|
+
for (const u of r) {
|
|
670
678
|
if (!d) break;
|
|
671
|
-
|
|
679
|
+
u.afterStream && (d = await u.afterStream(d, o));
|
|
672
680
|
}
|
|
673
|
-
d &&
|
|
681
|
+
d && p.enqueue(d);
|
|
674
682
|
}
|
|
675
683
|
})) : s.stream;
|
|
676
|
-
for (const
|
|
677
|
-
|
|
684
|
+
for (const f of r)
|
|
685
|
+
f.onFinish && f.onFinish(o, s);
|
|
678
686
|
return { stream: l, response: s.response };
|
|
679
687
|
}
|
|
680
688
|
} : t;
|
|
@@ -693,21 +701,21 @@ function be(t = "vue-llm") {
|
|
|
693
701
|
};
|
|
694
702
|
}
|
|
695
703
|
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,
|
|
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, f = e.shouldRetry ?? ae;
|
|
697
705
|
return {
|
|
698
|
-
async wrapChat(
|
|
699
|
-
for (let
|
|
706
|
+
async wrapChat(p, d) {
|
|
707
|
+
for (let u = 1; ; u++)
|
|
700
708
|
try {
|
|
701
|
-
return await
|
|
709
|
+
return await p(d);
|
|
702
710
|
} catch (b) {
|
|
703
711
|
const h = b instanceof Error ? b : new Error(String(b));
|
|
704
|
-
if (
|
|
712
|
+
if (u > o || d.signal?.aborted || !f(h, u))
|
|
705
713
|
throw h;
|
|
706
|
-
const
|
|
714
|
+
const C = a * n ** (u - 1), m = h instanceof _ ? h.retryAfter : void 0, w = Math.min(
|
|
707
715
|
s,
|
|
708
|
-
|
|
716
|
+
m ?? (l ? C * (0.5 + Math.random() * 0.5) : C)
|
|
709
717
|
);
|
|
710
|
-
e.onRetry?.({ error: h, attempt:
|
|
718
|
+
e.onRetry?.({ error: h, attempt: u, delay: w }), await ne(w, d.signal);
|
|
711
719
|
}
|
|
712
720
|
}
|
|
713
721
|
};
|
|
@@ -724,33 +732,33 @@ function Ce(t) {
|
|
|
724
732
|
}, n = async (l) => {
|
|
725
733
|
for (; ; ) {
|
|
726
734
|
l?.throwIfAborted();
|
|
727
|
-
const
|
|
728
|
-
for (; e.length > 0 && e[0] <=
|
|
729
|
-
const
|
|
730
|
-
if (!
|
|
731
|
-
e.push(
|
|
735
|
+
const f = Date.now();
|
|
736
|
+
for (; e.length > 0 && e[0] <= f - r; ) e.shift();
|
|
737
|
+
const p = t.maxRequests !== void 0 && e.length >= t.maxRequests, d = t.maxConcurrent !== void 0 && o >= t.maxConcurrent;
|
|
738
|
+
if (!p && !d) {
|
|
739
|
+
e.push(f), o++;
|
|
732
740
|
return;
|
|
733
741
|
}
|
|
734
|
-
await new Promise((
|
|
742
|
+
await new Promise((u, b) => {
|
|
735
743
|
const h = () => {
|
|
736
|
-
clearTimeout(
|
|
737
|
-
},
|
|
744
|
+
clearTimeout(m), a.delete(h), l?.removeEventListener("abort", C), u();
|
|
745
|
+
}, C = () => {
|
|
738
746
|
h(), b(l.reason);
|
|
739
|
-
},
|
|
740
|
-
a.add(h), l?.addEventListener("abort",
|
|
747
|
+
}, m = p ? setTimeout(h, e[0] + r - f) : void 0;
|
|
748
|
+
a.add(h), l?.addEventListener("abort", C, { once: !0 });
|
|
741
749
|
});
|
|
742
750
|
}
|
|
743
751
|
};
|
|
744
752
|
return {
|
|
745
|
-
async wrapChat(l,
|
|
746
|
-
await n(
|
|
747
|
-
let
|
|
753
|
+
async wrapChat(l, f) {
|
|
754
|
+
await n(f.signal);
|
|
755
|
+
let p;
|
|
748
756
|
try {
|
|
749
|
-
|
|
757
|
+
p = await l(f);
|
|
750
758
|
} catch (d) {
|
|
751
759
|
throw s(), d;
|
|
752
760
|
}
|
|
753
|
-
return { ...
|
|
761
|
+
return { ...p, stream: se(p.stream, s) };
|
|
754
762
|
}
|
|
755
763
|
};
|
|
756
764
|
}
|