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