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