vue-llm 1.0.0 → 1.2.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 +2 -1
- package/dist/composables/use-chat.d.ts +2 -1
- package/dist/composables/use-completion.d.ts +2 -1
- package/dist/composables/use-object.d.ts +2 -1
- package/dist/composables/use-prompt.d.ts +2 -1
- package/dist/index.cjs +3 -3
- package/dist/index.js +297 -287
- package/dist/providers/sse.d.ts +18 -0
- package/dist/providers/types.d.ts +5 -0
- package/dist/providers.cjs +4 -8
- package/dist/providers.d.ts +2 -0
- package/dist/providers.js +257 -321
- package/dist/server.cjs +2 -3
- package/dist/server.d.ts +8 -1
- package/dist/server.js +109 -100
- package/dist/sse-DU5m1n6r.js +104 -0
- package/dist/sse-PZGBuYKy.cjs +2 -0
- package/dist/utils/vue.d.ts +4 -0
- package/package.json +6 -4
package/dist/index.js
CHANGED
|
@@ -1,35 +1,39 @@
|
|
|
1
|
-
import { ref as x, watch as
|
|
2
|
-
import { c as
|
|
3
|
-
import { f as
|
|
4
|
-
import { L as
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { unref as q, ref as x, watch as U, computed as A, getCurrentScope as L, onScopeDispose as D } from "vue";
|
|
2
|
+
import { c as _ } from "./file-x_kW7BHa.js";
|
|
3
|
+
import { f as me } from "./file-x_kW7BHa.js";
|
|
4
|
+
import { L as F } from "./errors-B3A_zmVE.js";
|
|
5
|
+
function N(t) {
|
|
6
|
+
return typeof t == "function" ? t() : q(t);
|
|
7
|
+
}
|
|
8
|
+
class k {
|
|
9
|
+
constructor(r, e, a) {
|
|
10
|
+
this.messages = r, this.status = e, this.generateId = a;
|
|
8
11
|
}
|
|
9
12
|
messages;
|
|
10
13
|
status;
|
|
11
14
|
generateId;
|
|
12
|
-
async processStream(
|
|
15
|
+
async processStream(r, e, a) {
|
|
13
16
|
this.status.value = "streaming";
|
|
14
17
|
const o = this.getOrCreateAssistantMessage(), s = /* @__PURE__ */ new Map(), l = [];
|
|
15
|
-
let
|
|
16
|
-
const v =
|
|
18
|
+
let f = "stop", i;
|
|
19
|
+
const v = r.getReader();
|
|
17
20
|
try {
|
|
18
21
|
for (; !e.aborted; ) {
|
|
19
|
-
const { done: d, value:
|
|
22
|
+
const { done: d, value: c } = await v.read();
|
|
20
23
|
if (d) break;
|
|
21
|
-
if (this.applyChunk(o,
|
|
22
|
-
const
|
|
24
|
+
if (this.applyChunk(o, c, s), c.type === "tool-call-end") {
|
|
25
|
+
const p = s.get(c.toolCallId) ?? "{}";
|
|
23
26
|
let m;
|
|
24
27
|
try {
|
|
25
|
-
m = JSON.parse(
|
|
28
|
+
m = JSON.parse(p);
|
|
26
29
|
} catch {
|
|
27
|
-
m =
|
|
30
|
+
m = p;
|
|
28
31
|
}
|
|
29
|
-
const h = { toolCallId:
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
const h = { toolCallId: c.toolCallId, toolName: this.getToolName(o, c.toolCallId), args: m };
|
|
33
|
+
l.push(h);
|
|
34
|
+
const g = this.findToolCallPart(o, c.toolCallId);
|
|
35
|
+
if (g && (g.args = m), a.onToolCall) {
|
|
36
|
+
g && (g.state = "running"), this.triggerReactivity();
|
|
33
37
|
try {
|
|
34
38
|
const b = await a.onToolCall(h);
|
|
35
39
|
b !== void 0 && g && (g.state = "result", g.result = b, this.triggerReactivity());
|
|
@@ -38,22 +42,28 @@ class U {
|
|
|
38
42
|
}
|
|
39
43
|
}
|
|
40
44
|
}
|
|
41
|
-
if (
|
|
42
|
-
|
|
45
|
+
if (c.type === "tool-result") {
|
|
46
|
+
const p = this.findToolCallPart(o, c.toolCallId);
|
|
47
|
+
p && (p.state = c.error !== void 0 ? "error" : "result", p.result = c.result, p.error = c.error);
|
|
48
|
+
const m = l.findIndex((h) => h.toolCallId === c.toolCallId);
|
|
49
|
+
m !== -1 && l.splice(m, 1);
|
|
50
|
+
}
|
|
51
|
+
if (c.type === "finish" && (f = c.finishReason, i = c.usage), c.type === "error")
|
|
52
|
+
throw c.error;
|
|
43
53
|
this.triggerReactivity();
|
|
44
54
|
}
|
|
45
55
|
} catch (d) {
|
|
46
56
|
if (!e.aborted) {
|
|
47
|
-
const
|
|
48
|
-
throw a.onError?.(
|
|
57
|
+
const c = d instanceof Error ? d : new Error(String(d));
|
|
58
|
+
throw a.onError?.(c), c;
|
|
49
59
|
}
|
|
50
60
|
} finally {
|
|
51
61
|
v.releaseLock();
|
|
52
62
|
}
|
|
53
|
-
return this.finalizeTextParts(o), this.triggerReactivity(), a.onFinish?.(o, { finishReason:
|
|
63
|
+
return this.finalizeTextParts(o), this.triggerReactivity(), a.onFinish?.(o, { finishReason: f, usage: i }), { finishReason: f, usage: i, toolCalls: l };
|
|
54
64
|
}
|
|
55
65
|
getOrCreateAssistantMessage() {
|
|
56
|
-
const
|
|
66
|
+
const r = this.messages.value, e = r[r.length - 1];
|
|
57
67
|
if (e?.role === "assistant") return e;
|
|
58
68
|
const a = {
|
|
59
69
|
id: this.generateId(),
|
|
@@ -61,22 +71,22 @@ class U {
|
|
|
61
71
|
parts: [],
|
|
62
72
|
createdAt: /* @__PURE__ */ new Date()
|
|
63
73
|
};
|
|
64
|
-
return
|
|
74
|
+
return r.push(a), a;
|
|
65
75
|
}
|
|
66
|
-
applyChunk(
|
|
76
|
+
applyChunk(r, e, a) {
|
|
67
77
|
switch (e.type) {
|
|
68
78
|
case "text-delta": {
|
|
69
|
-
const o =
|
|
70
|
-
o?.type === "text" && o.state === "streaming" ? o.text += e.text :
|
|
79
|
+
const o = r.parts[r.parts.length - 1];
|
|
80
|
+
o?.type === "text" && o.state === "streaming" ? o.text += e.text : r.parts.push({ type: "text", text: e.text, state: "streaming" });
|
|
71
81
|
break;
|
|
72
82
|
}
|
|
73
83
|
case "reasoning-delta": {
|
|
74
|
-
const o =
|
|
75
|
-
o?.type === "reasoning" && o.state === "streaming" ? o.text += e.text :
|
|
84
|
+
const o = r.parts[r.parts.length - 1];
|
|
85
|
+
o?.type === "reasoning" && o.state === "streaming" ? o.text += e.text : r.parts.push({ type: "reasoning", text: e.text, state: "streaming" });
|
|
76
86
|
break;
|
|
77
87
|
}
|
|
78
88
|
case "tool-call-start":
|
|
79
|
-
|
|
89
|
+
r.parts.push({
|
|
80
90
|
type: "tool-call",
|
|
81
91
|
toolCallId: e.toolCallId,
|
|
82
92
|
toolName: e.toolName,
|
|
@@ -92,89 +102,89 @@ class U {
|
|
|
92
102
|
break;
|
|
93
103
|
}
|
|
94
104
|
}
|
|
95
|
-
finalizeTextParts(
|
|
96
|
-
for (const e of
|
|
105
|
+
finalizeTextParts(r) {
|
|
106
|
+
for (const e of r.parts)
|
|
97
107
|
(e.type === "text" || e.type === "reasoning") && e.state === "streaming" && (e.state = "done");
|
|
98
108
|
}
|
|
99
|
-
findToolCallPart(
|
|
100
|
-
return
|
|
109
|
+
findToolCallPart(r, e) {
|
|
110
|
+
return r.parts.find(
|
|
101
111
|
(a) => a.type === "tool-call" && a.toolCallId === e
|
|
102
112
|
);
|
|
103
113
|
}
|
|
104
|
-
getToolName(
|
|
105
|
-
return this.findToolCallPart(
|
|
114
|
+
getToolName(r, e) {
|
|
115
|
+
return this.findToolCallPart(r, e)?.toolName ?? "";
|
|
106
116
|
}
|
|
107
117
|
triggerReactivity() {
|
|
108
118
|
this.messages.value = [...this.messages.value];
|
|
109
119
|
}
|
|
110
120
|
}
|
|
111
|
-
const
|
|
112
|
-
function
|
|
113
|
-
const
|
|
121
|
+
const J = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
122
|
+
function P(t = 16) {
|
|
123
|
+
const r = crypto.getRandomValues(new Uint8Array(t));
|
|
114
124
|
let e = "";
|
|
115
|
-
for (let a = 0; a <
|
|
116
|
-
e +=
|
|
125
|
+
for (let a = 0; a < t; a++)
|
|
126
|
+
e += J[r[a] % J.length];
|
|
117
127
|
return e;
|
|
118
128
|
}
|
|
119
|
-
function
|
|
120
|
-
const
|
|
129
|
+
function ee(t) {
|
|
130
|
+
const r = t.id ?? P(), e = x(t.initialMessages ?? []), a = x("ready"), o = x(void 0), s = x("");
|
|
121
131
|
let l = null;
|
|
122
|
-
const
|
|
123
|
-
|
|
132
|
+
const f = x(!t.storage), i = t.storage, v = (n) => {
|
|
133
|
+
t.onError?.(n instanceof Error ? n : new Error(String(n)));
|
|
124
134
|
};
|
|
125
135
|
if (i) {
|
|
126
|
-
const n = e.value, C = (
|
|
127
|
-
|
|
136
|
+
const n = e.value, C = (u) => {
|
|
137
|
+
u?.length && e.value === n && (e.value = u), f.value = !0;
|
|
128
138
|
};
|
|
129
139
|
try {
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
v(
|
|
133
|
-
}) : C(
|
|
134
|
-
} catch (
|
|
135
|
-
v(
|
|
140
|
+
const u = i.load(r);
|
|
141
|
+
u instanceof Promise ? u.then(C, (y) => {
|
|
142
|
+
v(y), f.value = !0;
|
|
143
|
+
}) : C(u);
|
|
144
|
+
} catch (u) {
|
|
145
|
+
v(u), f.value = !0;
|
|
136
146
|
}
|
|
137
|
-
|
|
138
|
-
if (!(!
|
|
147
|
+
U([e, a, f], () => {
|
|
148
|
+
if (!(!f.value || a.value === "submitted" || a.value === "streaming"))
|
|
139
149
|
try {
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
} catch (
|
|
143
|
-
v(
|
|
150
|
+
const u = e.value.length > 0 ? i.save(r, e.value) : i.remove(r);
|
|
151
|
+
u instanceof Promise && u.catch(v);
|
|
152
|
+
} catch (u) {
|
|
153
|
+
v(u);
|
|
144
154
|
}
|
|
145
155
|
}, { deep: !0 });
|
|
146
156
|
}
|
|
147
|
-
const d = new
|
|
157
|
+
const d = new k(e, a, P), c = () => {
|
|
148
158
|
const n = [], C = [
|
|
149
|
-
N(
|
|
150
|
-
...e.value.filter((
|
|
159
|
+
N(t.system),
|
|
160
|
+
...e.value.filter((u) => u.role === "system").map((u) => u.parts.filter((y) => y.type === "text").map((y) => y.text).join(""))
|
|
151
161
|
].filter(Boolean);
|
|
152
162
|
C.length > 0 && n.push({ role: "system", content: C.join(`
|
|
153
163
|
|
|
154
164
|
`) });
|
|
155
|
-
for (const
|
|
156
|
-
if (
|
|
157
|
-
const E =
|
|
158
|
-
if (
|
|
159
|
-
const w =
|
|
165
|
+
for (const u of e.value) {
|
|
166
|
+
if (u.role === "system") continue;
|
|
167
|
+
const E = u.parts.filter((w) => w.type === "text").map((w) => w.text).join(""), S = u.parts.filter((w) => w.type === "tool-call").map((w) => ({ id: w.toolCallId, name: w.toolName, args: w.args }));
|
|
168
|
+
if (u.role === "user") {
|
|
169
|
+
const w = u.parts.filter((O) => O.type === "file");
|
|
160
170
|
if (w.length === 0) {
|
|
161
171
|
n.push({ role: "user", content: E });
|
|
162
172
|
continue;
|
|
163
173
|
}
|
|
164
|
-
const
|
|
174
|
+
const j = w.map((O) => ({
|
|
165
175
|
type: "file",
|
|
166
176
|
url: O.url,
|
|
167
177
|
mediaType: O.mediaType,
|
|
168
178
|
filename: O.filename
|
|
169
179
|
}));
|
|
170
|
-
E &&
|
|
171
|
-
} else if (
|
|
180
|
+
E && j.push({ type: "text", text: E }), n.push({ role: "user", content: j });
|
|
181
|
+
} else if (u.role === "assistant") {
|
|
172
182
|
n.push({
|
|
173
183
|
role: "assistant",
|
|
174
184
|
content: E,
|
|
175
185
|
toolCalls: S.length > 0 ? S : void 0
|
|
176
186
|
});
|
|
177
|
-
for (const w of
|
|
187
|
+
for (const w of u.parts)
|
|
178
188
|
w.type !== "tool-call" || w.state !== "result" || n.push({
|
|
179
189
|
role: "tool",
|
|
180
190
|
content: JSON.stringify(w.result),
|
|
@@ -183,52 +193,52 @@ function Q(r) {
|
|
|
183
193
|
}
|
|
184
194
|
}
|
|
185
195
|
return n;
|
|
186
|
-
},
|
|
187
|
-
const C =
|
|
188
|
-
let
|
|
189
|
-
for (;
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
const { stream: E } = await
|
|
193
|
-
model:
|
|
194
|
-
messages:
|
|
195
|
-
tools:
|
|
196
|
+
}, p = async (n) => {
|
|
197
|
+
const C = t.maxToolRoundtrips ?? 5;
|
|
198
|
+
let u = 0;
|
|
199
|
+
for (; u < C; ) {
|
|
200
|
+
const y = c();
|
|
201
|
+
t.provider.validateConfig();
|
|
202
|
+
const { stream: E } = await t.provider.chat({
|
|
203
|
+
model: t.model,
|
|
204
|
+
messages: y,
|
|
205
|
+
tools: t.tools,
|
|
196
206
|
signal: n,
|
|
197
207
|
providerOptions: {
|
|
198
|
-
...
|
|
199
|
-
...N(
|
|
208
|
+
...t.providerOptions,
|
|
209
|
+
...N(t.body)
|
|
200
210
|
}
|
|
201
211
|
}), S = await d.processStream(E, n, {
|
|
202
|
-
onFinish:
|
|
203
|
-
onError:
|
|
204
|
-
onToolCall:
|
|
212
|
+
onFinish: t.onFinish,
|
|
213
|
+
onError: t.onError,
|
|
214
|
+
onToolCall: t.onToolCall
|
|
205
215
|
});
|
|
206
216
|
if (S.finishReason !== "tool-calls" || S.toolCalls.length === 0)
|
|
207
217
|
break;
|
|
208
|
-
|
|
218
|
+
u++;
|
|
209
219
|
}
|
|
210
220
|
}, m = async (n) => {
|
|
211
221
|
const C = typeof n == "string" ? n : n?.text ?? s.value;
|
|
212
|
-
let
|
|
222
|
+
let u;
|
|
213
223
|
try {
|
|
214
|
-
|
|
224
|
+
u = typeof n == "object" ? await _(n.files) : [];
|
|
215
225
|
} catch (S) {
|
|
216
226
|
o.value = S instanceof Error ? S : new Error(String(S)), a.value = "error";
|
|
217
227
|
return;
|
|
218
228
|
}
|
|
219
|
-
if (!C.trim() &&
|
|
229
|
+
if (!C.trim() && u.length === 0) return;
|
|
220
230
|
(typeof n != "object" || n === null || !("text" in n)) && (s.value = "");
|
|
221
|
-
const
|
|
222
|
-
C.trim() &&
|
|
231
|
+
const y = [...u];
|
|
232
|
+
C.trim() && y.push({ type: "text", text: C, state: "done" });
|
|
223
233
|
const E = {
|
|
224
|
-
id:
|
|
234
|
+
id: P(),
|
|
225
235
|
role: "user",
|
|
226
|
-
parts:
|
|
236
|
+
parts: y,
|
|
227
237
|
createdAt: /* @__PURE__ */ new Date()
|
|
228
238
|
};
|
|
229
239
|
e.value = [...e.value, E], o.value = void 0, a.value = "submitted", l?.abort(), l = new AbortController();
|
|
230
240
|
try {
|
|
231
|
-
await
|
|
241
|
+
await p(l.signal), a.value = "ready";
|
|
232
242
|
} catch (S) {
|
|
233
243
|
if (l.signal.aborted) {
|
|
234
244
|
a.value = "ready";
|
|
@@ -237,17 +247,17 @@ function Q(r) {
|
|
|
237
247
|
o.value = S instanceof Error ? S : new Error(String(S)), a.value = "error";
|
|
238
248
|
}
|
|
239
249
|
}, h = async () => {
|
|
240
|
-
const n = e.value, C = n.reduce((
|
|
250
|
+
const n = e.value, C = n.reduce((u, y, E) => y.role === "assistant" ? E : u, -1);
|
|
241
251
|
if (C !== -1) {
|
|
242
252
|
e.value = n.slice(0, C), o.value = void 0, a.value = "submitted", l?.abort(), l = new AbortController();
|
|
243
253
|
try {
|
|
244
|
-
await
|
|
245
|
-
} catch (
|
|
254
|
+
await p(l.signal), a.value = "ready";
|
|
255
|
+
} catch (u) {
|
|
246
256
|
if (l.signal.aborted) {
|
|
247
257
|
a.value = "ready";
|
|
248
258
|
return;
|
|
249
259
|
}
|
|
250
|
-
o.value =
|
|
260
|
+
o.value = u instanceof Error ? u : new Error(String(u)), a.value = "error";
|
|
251
261
|
}
|
|
252
262
|
}
|
|
253
263
|
}, g = () => {
|
|
@@ -256,23 +266,23 @@ function Q(r) {
|
|
|
256
266
|
e.value = typeof n == "function" ? n(e.value) : n;
|
|
257
267
|
}, R = (n) => {
|
|
258
268
|
n?.preventDefault?.(), s.value.trim() && m();
|
|
259
|
-
}, T = A(() => a.value === "submitted" || a.value === "streaming"),
|
|
260
|
-
for (const
|
|
261
|
-
for (const
|
|
262
|
-
if (
|
|
263
|
-
|
|
269
|
+
}, T = A(() => a.value === "submitted" || a.value === "streaming"), I = ({ toolCallId: n, result: C }) => {
|
|
270
|
+
for (const u of e.value)
|
|
271
|
+
for (const y of u.parts)
|
|
272
|
+
if (y.type === "tool-call" && y.toolCallId === n) {
|
|
273
|
+
y.state = "result", y.result = C, e.value = [...e.value];
|
|
264
274
|
return;
|
|
265
275
|
}
|
|
266
276
|
}, M = () => {
|
|
267
277
|
o.value = void 0, a.value === "error" && (a.value = "ready");
|
|
268
278
|
};
|
|
269
|
-
return L(() => {
|
|
279
|
+
return L() && D(() => {
|
|
270
280
|
l?.abort();
|
|
271
281
|
}), {
|
|
272
282
|
messages: e,
|
|
273
283
|
status: A(() => a.value),
|
|
274
284
|
error: o,
|
|
275
|
-
id:
|
|
285
|
+
id: r,
|
|
276
286
|
input: s,
|
|
277
287
|
sendMessage: m,
|
|
278
288
|
regenerate: h,
|
|
@@ -280,111 +290,111 @@ function Q(r) {
|
|
|
280
290
|
setMessages: b,
|
|
281
291
|
handleSubmit: R,
|
|
282
292
|
isLoading: T,
|
|
283
|
-
addToolResult:
|
|
293
|
+
addToolResult: I,
|
|
284
294
|
clearError: M,
|
|
285
|
-
isRestored: A(() =>
|
|
295
|
+
isRestored: A(() => f.value)
|
|
286
296
|
};
|
|
287
297
|
}
|
|
288
|
-
function
|
|
289
|
-
const
|
|
298
|
+
function te(t) {
|
|
299
|
+
const r = t.id ?? P(), e = x(""), a = x("ready"), o = x(void 0), s = x("");
|
|
290
300
|
let l = null;
|
|
291
|
-
const
|
|
292
|
-
const m =
|
|
301
|
+
const f = async (p) => {
|
|
302
|
+
const m = p ?? s.value;
|
|
293
303
|
if (!m.trim()) return e.value;
|
|
294
|
-
|
|
304
|
+
p || (s.value = ""), e.value = "", o.value = void 0, a.value = "submitted", l?.abort(), l = new AbortController();
|
|
295
305
|
try {
|
|
296
|
-
|
|
297
|
-
const h = N(
|
|
306
|
+
t.provider.validateConfig();
|
|
307
|
+
const h = N(t.system), g = h ? [
|
|
298
308
|
{ role: "system", content: h },
|
|
299
309
|
{ role: "user", content: m }
|
|
300
|
-
] : [{ role: "user", content: m }], { stream: b } = await
|
|
301
|
-
model:
|
|
310
|
+
] : [{ role: "user", content: m }], { stream: b } = await t.provider.chat({
|
|
311
|
+
model: t.model,
|
|
302
312
|
messages: g,
|
|
303
313
|
signal: l.signal
|
|
304
314
|
});
|
|
305
315
|
a.value = "streaming";
|
|
306
316
|
const R = b.getReader();
|
|
307
|
-
let T = "stop",
|
|
317
|
+
let T = "stop", I;
|
|
308
318
|
for (; !l.signal.aborted; ) {
|
|
309
319
|
const { done: M, value: n } = await R.read();
|
|
310
320
|
if (M) break;
|
|
311
321
|
if (n.type === "text-delta")
|
|
312
322
|
e.value += n.text;
|
|
313
323
|
else if (n.type === "finish")
|
|
314
|
-
T = n.finishReason,
|
|
324
|
+
T = n.finishReason, I = n.usage;
|
|
315
325
|
else if (n.type === "error")
|
|
316
326
|
throw n.error;
|
|
317
327
|
}
|
|
318
|
-
return R.releaseLock(), a.value = "ready",
|
|
328
|
+
return R.releaseLock(), a.value = "ready", t.onFinish?.(e.value, { finishReason: T, usage: I }), e.value;
|
|
319
329
|
} catch (h) {
|
|
320
330
|
if (!l || l.signal.aborted)
|
|
321
331
|
return a.value = "ready", e.value;
|
|
322
332
|
const g = h instanceof Error ? h : new Error(String(h));
|
|
323
|
-
return o.value = g, a.value = "error",
|
|
333
|
+
return o.value = g, a.value = "error", t.onError?.(g), e.value;
|
|
324
334
|
}
|
|
325
335
|
}, i = () => {
|
|
326
336
|
l?.abort(), l = null, a.value = "ready";
|
|
327
337
|
}, v = () => {
|
|
328
338
|
o.value = void 0, a.value === "error" && (a.value = "ready");
|
|
329
|
-
}, d = (
|
|
330
|
-
|
|
331
|
-
},
|
|
332
|
-
return L(() => {
|
|
339
|
+
}, d = (p) => {
|
|
340
|
+
p?.preventDefault?.(), s.value.trim() && f();
|
|
341
|
+
}, c = A(() => a.value === "submitted" || a.value === "streaming");
|
|
342
|
+
return L() && D(() => {
|
|
333
343
|
l?.abort();
|
|
334
344
|
}), {
|
|
335
345
|
completion: e,
|
|
336
346
|
status: A(() => a.value),
|
|
337
347
|
error: o,
|
|
338
|
-
isLoading:
|
|
339
|
-
id:
|
|
348
|
+
isLoading: c,
|
|
349
|
+
id: r,
|
|
340
350
|
input: s,
|
|
341
|
-
complete:
|
|
351
|
+
complete: f,
|
|
342
352
|
stop: i,
|
|
343
353
|
clearError: v,
|
|
344
354
|
handleSubmit: d
|
|
345
355
|
};
|
|
346
356
|
}
|
|
347
|
-
function
|
|
348
|
-
const
|
|
349
|
-
let
|
|
350
|
-
const i = async (
|
|
351
|
-
const m =
|
|
357
|
+
function re(t) {
|
|
358
|
+
const r = t.id ?? P(), e = x(void 0), a = x(void 0), o = x("ready"), s = x(void 0), l = x("");
|
|
359
|
+
let f = null;
|
|
360
|
+
const i = async (p) => {
|
|
361
|
+
const m = p ?? l.value;
|
|
352
362
|
if (!m.trim()) return e.value;
|
|
353
|
-
|
|
363
|
+
p || (l.value = ""), e.value = void 0, a.value = void 0, s.value = void 0, o.value = "submitted", f?.abort(), f = new AbortController();
|
|
354
364
|
try {
|
|
355
|
-
|
|
365
|
+
t.provider.validateConfig();
|
|
356
366
|
const h = [
|
|
357
|
-
N(
|
|
367
|
+
N(t.system) ?? "",
|
|
358
368
|
"Respond with valid JSON matching this schema:",
|
|
359
|
-
JSON.stringify(
|
|
369
|
+
JSON.stringify(t.schema, null, 2),
|
|
360
370
|
"Output ONLY the JSON object, no other text."
|
|
361
371
|
].filter(Boolean).join(`
|
|
362
372
|
|
|
363
|
-
`), { stream: g } = await
|
|
364
|
-
model:
|
|
373
|
+
`), { stream: g } = await t.provider.chat({
|
|
374
|
+
model: t.model,
|
|
365
375
|
messages: [
|
|
366
376
|
{ role: "system", content: h },
|
|
367
377
|
{ role: "user", content: m }
|
|
368
378
|
],
|
|
369
|
-
signal:
|
|
379
|
+
signal: f.signal,
|
|
370
380
|
providerOptions: {
|
|
371
381
|
response_format: { type: "json_object" }
|
|
372
382
|
}
|
|
373
383
|
});
|
|
374
384
|
o.value = "streaming";
|
|
375
385
|
const b = g.getReader();
|
|
376
|
-
let R = "", T = "stop",
|
|
377
|
-
for (; !
|
|
386
|
+
let R = "", T = "stop", I;
|
|
387
|
+
for (; !f.signal.aborted; ) {
|
|
378
388
|
const { done: M, value: n } = await b.read();
|
|
379
389
|
if (M) break;
|
|
380
390
|
if (n.type === "text-delta") {
|
|
381
391
|
R += n.text;
|
|
382
392
|
try {
|
|
383
|
-
a.value =
|
|
393
|
+
a.value = W(R);
|
|
384
394
|
} catch {
|
|
385
395
|
}
|
|
386
396
|
} else if (n.type === "finish")
|
|
387
|
-
T = n.finishReason,
|
|
397
|
+
T = n.finishReason, I = n.usage;
|
|
388
398
|
else if (n.type === "error")
|
|
389
399
|
throw n.error;
|
|
390
400
|
}
|
|
@@ -394,40 +404,40 @@ function ee(r) {
|
|
|
394
404
|
} catch {
|
|
395
405
|
throw new Error(`Failed to parse JSON response: ${R.slice(0, 200)}`);
|
|
396
406
|
}
|
|
397
|
-
return o.value = "ready",
|
|
407
|
+
return o.value = "ready", t.onFinish?.(e.value, { finishReason: T, usage: I }), e.value;
|
|
398
408
|
} catch (h) {
|
|
399
|
-
if (
|
|
409
|
+
if (f.signal.aborted)
|
|
400
410
|
return o.value = "ready", e.value;
|
|
401
411
|
const g = h instanceof Error ? h : new Error(String(h));
|
|
402
|
-
s.value = g, o.value = "error",
|
|
412
|
+
s.value = g, o.value = "error", t.onError?.(g);
|
|
403
413
|
return;
|
|
404
414
|
}
|
|
405
415
|
}, v = () => {
|
|
406
|
-
|
|
416
|
+
f?.abort(), f = null, o.value = "ready";
|
|
407
417
|
}, d = () => {
|
|
408
418
|
s.value = void 0, o.value === "error" && (o.value = "ready");
|
|
409
|
-
},
|
|
410
|
-
return L(() => {
|
|
411
|
-
|
|
419
|
+
}, c = A(() => o.value === "submitted" || o.value === "streaming");
|
|
420
|
+
return L() && D(() => {
|
|
421
|
+
f?.abort();
|
|
412
422
|
}), {
|
|
413
423
|
object: e,
|
|
414
424
|
partialObject: a,
|
|
415
425
|
status: A(() => o.value),
|
|
416
426
|
error: s,
|
|
417
|
-
isLoading:
|
|
418
|
-
id:
|
|
427
|
+
isLoading: c,
|
|
428
|
+
id: r,
|
|
419
429
|
input: l,
|
|
420
430
|
submit: i,
|
|
421
431
|
stop: v,
|
|
422
432
|
clearError: d
|
|
423
433
|
};
|
|
424
434
|
}
|
|
425
|
-
function
|
|
426
|
-
let
|
|
427
|
-
if (!
|
|
435
|
+
function W(t) {
|
|
436
|
+
let r = t.trim();
|
|
437
|
+
if (!r.startsWith("{")) return;
|
|
428
438
|
let e = 0, a = !1, o = !1;
|
|
429
|
-
for (let s = 0; s <
|
|
430
|
-
const l =
|
|
439
|
+
for (let s = 0; s < r.length; s++) {
|
|
440
|
+
const l = r[s];
|
|
431
441
|
if (o) {
|
|
432
442
|
o = !1;
|
|
433
443
|
continue;
|
|
@@ -443,35 +453,35 @@ function _(r) {
|
|
|
443
453
|
a || (l === "{" && e++, l === "}" && e--);
|
|
444
454
|
}
|
|
445
455
|
for (; e > 0; )
|
|
446
|
-
|
|
456
|
+
r += "}", e--;
|
|
447
457
|
try {
|
|
448
|
-
return JSON.parse(
|
|
458
|
+
return JSON.parse(r);
|
|
449
459
|
} catch {
|
|
450
460
|
return;
|
|
451
461
|
}
|
|
452
462
|
}
|
|
453
|
-
const
|
|
454
|
-
function
|
|
463
|
+
const $ = /(\\)?\{\{\s*([A-Za-z_][\w-]*)\s*\}\}/g;
|
|
464
|
+
function H(t, r = {}) {
|
|
455
465
|
return {
|
|
456
|
-
template:
|
|
457
|
-
variables:
|
|
466
|
+
template: t,
|
|
467
|
+
variables: B(t),
|
|
458
468
|
render(e) {
|
|
459
|
-
return
|
|
469
|
+
return z(t, { ...r.defaults, ...e });
|
|
460
470
|
}
|
|
461
471
|
};
|
|
462
472
|
}
|
|
463
|
-
function
|
|
473
|
+
function ae(t, r = {}) {
|
|
464
474
|
const e = (a) => {
|
|
465
|
-
const o = { ...
|
|
466
|
-
return
|
|
475
|
+
const o = { ...r.defaults, ...a };
|
|
476
|
+
return t.map((s) => ({ role: s.role, content: z(s.content, o) }));
|
|
467
477
|
};
|
|
468
478
|
return {
|
|
469
|
-
messages:
|
|
470
|
-
variables: [...new Set(
|
|
479
|
+
messages: t,
|
|
480
|
+
variables: [...new Set(t.flatMap((a) => B(a.content)))],
|
|
471
481
|
render: e,
|
|
472
482
|
toUIMessages(a) {
|
|
473
483
|
return e(a).map((o) => ({
|
|
474
|
-
id:
|
|
484
|
+
id: P(),
|
|
475
485
|
role: o.role,
|
|
476
486
|
parts: [{ type: "text", text: o.content, state: "done" }],
|
|
477
487
|
createdAt: /* @__PURE__ */ new Date()
|
|
@@ -479,154 +489,154 @@ function te(r, t = {}) {
|
|
|
479
489
|
}
|
|
480
490
|
};
|
|
481
491
|
}
|
|
482
|
-
function
|
|
483
|
-
return
|
|
492
|
+
function z(t, r) {
|
|
493
|
+
return t.replace($, (e, a, o) => {
|
|
484
494
|
if (a) return e.slice(1);
|
|
485
|
-
const s =
|
|
495
|
+
const s = r[o];
|
|
486
496
|
if (s == null)
|
|
487
497
|
throw new Error(`[vue-llm] Missing prompt variable "${o}"`);
|
|
488
498
|
return String(s);
|
|
489
499
|
});
|
|
490
500
|
}
|
|
491
|
-
function
|
|
492
|
-
const
|
|
493
|
-
for (const [, e, a] of
|
|
494
|
-
e ||
|
|
495
|
-
return [...
|
|
501
|
+
function B(t) {
|
|
502
|
+
const r = /* @__PURE__ */ new Set();
|
|
503
|
+
for (const [, e, a] of t.matchAll($))
|
|
504
|
+
e || r.add(a);
|
|
505
|
+
return [...r];
|
|
496
506
|
}
|
|
497
|
-
function
|
|
498
|
-
const e = typeof
|
|
499
|
-
return A(() => e.render(N(
|
|
507
|
+
function oe(t, r) {
|
|
508
|
+
const e = typeof t == "string" ? H(t) : t;
|
|
509
|
+
return A(() => e.render(N(r)));
|
|
500
510
|
}
|
|
501
|
-
function
|
|
502
|
-
return
|
|
503
|
-
id:
|
|
504
|
-
supportsStreaming:
|
|
511
|
+
function ne(t, r) {
|
|
512
|
+
return r.length ? {
|
|
513
|
+
id: t.id,
|
|
514
|
+
supportsStreaming: t.supportsStreaming,
|
|
505
515
|
validateConfig() {
|
|
506
|
-
|
|
516
|
+
t.validateConfig();
|
|
507
517
|
},
|
|
508
518
|
async chat(e) {
|
|
509
519
|
let a = e;
|
|
510
|
-
for (const i of
|
|
511
|
-
i.beforeRequest && (a = await i.beforeRequest(a,
|
|
512
|
-
const o =
|
|
520
|
+
for (const i of r)
|
|
521
|
+
i.beforeRequest && (a = await i.beforeRequest(a, t));
|
|
522
|
+
const o = r.reduceRight(
|
|
513
523
|
(i, v) => v.wrapChat ? (d) => v.wrapChat(i, d) : i,
|
|
514
|
-
(i) =>
|
|
524
|
+
(i) => t.chat(i)
|
|
515
525
|
);
|
|
516
526
|
let s;
|
|
517
527
|
try {
|
|
518
528
|
s = await o(a);
|
|
519
529
|
} catch (i) {
|
|
520
530
|
let v = i instanceof Error ? i : new Error(String(i));
|
|
521
|
-
for (const d of
|
|
531
|
+
for (const d of r)
|
|
522
532
|
if (d.onError) {
|
|
523
|
-
const
|
|
524
|
-
|
|
533
|
+
const c = await d.onError(v, a);
|
|
534
|
+
c instanceof Error && (v = c);
|
|
525
535
|
}
|
|
526
536
|
throw v;
|
|
527
537
|
}
|
|
528
|
-
const
|
|
538
|
+
const f = r.some((i) => i.afterStream) ? s.stream.pipeThrough(new TransformStream({
|
|
529
539
|
async transform(i, v) {
|
|
530
540
|
let d = i;
|
|
531
|
-
for (const
|
|
541
|
+
for (const c of r) {
|
|
532
542
|
if (!d) break;
|
|
533
|
-
|
|
543
|
+
c.afterStream && (d = await c.afterStream(d, a));
|
|
534
544
|
}
|
|
535
545
|
d && v.enqueue(d);
|
|
536
546
|
}
|
|
537
547
|
})) : s.stream;
|
|
538
|
-
for (const i of
|
|
548
|
+
for (const i of r)
|
|
539
549
|
i.onFinish && i.onFinish(a, s);
|
|
540
|
-
return { stream:
|
|
550
|
+
return { stream: f, response: s.response };
|
|
541
551
|
}
|
|
542
|
-
} :
|
|
552
|
+
} : t;
|
|
543
553
|
}
|
|
544
|
-
function
|
|
554
|
+
function se(t = "vue-llm") {
|
|
545
555
|
return {
|
|
546
|
-
beforeRequest(
|
|
547
|
-
return console.log(`[${
|
|
556
|
+
beforeRequest(r) {
|
|
557
|
+
return console.log(`[${t}] → ${r.model}`, r.messages.length, "messages"), r;
|
|
548
558
|
},
|
|
549
|
-
afterStream(
|
|
550
|
-
return
|
|
559
|
+
afterStream(r) {
|
|
560
|
+
return r.type === "finish" && console.log(`[${t}] ← finish:`, r.finishReason, r.usage ?? ""), r;
|
|
551
561
|
},
|
|
552
|
-
onError(
|
|
553
|
-
console.error(`[${
|
|
562
|
+
onError(r) {
|
|
563
|
+
console.error(`[${t}] error:`, r.message);
|
|
554
564
|
}
|
|
555
565
|
};
|
|
556
566
|
}
|
|
557
|
-
function
|
|
558
|
-
const e = typeof
|
|
567
|
+
function le(t = {}, r) {
|
|
568
|
+
const e = typeof t == "number" ? { maxRetries: t, initialDelay: r } : t, a = e.maxRetries ?? 3, o = e.initialDelay ?? 1e3, s = e.maxDelay ?? 3e4, l = e.factor ?? 2, f = e.jitter ?? !0, i = e.shouldRetry ?? Y;
|
|
559
569
|
return {
|
|
560
570
|
async wrapChat(v, d) {
|
|
561
|
-
for (let
|
|
571
|
+
for (let c = 1; ; c++)
|
|
562
572
|
try {
|
|
563
573
|
return await v(d);
|
|
564
|
-
} catch (
|
|
565
|
-
const m =
|
|
566
|
-
if (
|
|
574
|
+
} catch (p) {
|
|
575
|
+
const m = p instanceof Error ? p : new Error(String(p));
|
|
576
|
+
if (c > a || d.signal?.aborted || !i(m, c))
|
|
567
577
|
throw m;
|
|
568
|
-
const h = o * l ** (
|
|
578
|
+
const h = o * l ** (c - 1), g = m instanceof F ? m.retryAfter : void 0, b = Math.min(
|
|
569
579
|
s,
|
|
570
|
-
g ?? (
|
|
580
|
+
g ?? (f ? h * (0.5 + Math.random() * 0.5) : h)
|
|
571
581
|
);
|
|
572
|
-
e.onRetry?.({ error: m, attempt:
|
|
582
|
+
e.onRetry?.({ error: m, attempt: c, delay: b }), await G(b, d.signal);
|
|
573
583
|
}
|
|
574
584
|
}
|
|
575
585
|
};
|
|
576
586
|
}
|
|
577
|
-
function
|
|
578
|
-
return
|
|
587
|
+
function Y(t) {
|
|
588
|
+
return t.name === "AbortError" ? !1 : t instanceof F ? t.isRetryable : t instanceof TypeError;
|
|
579
589
|
}
|
|
580
|
-
function
|
|
581
|
-
const
|
|
590
|
+
function ie(t) {
|
|
591
|
+
const r = t.interval ?? 6e4, e = [];
|
|
582
592
|
let a = 0;
|
|
583
593
|
const o = /* @__PURE__ */ new Set(), s = () => {
|
|
584
594
|
a--;
|
|
585
|
-
for (const
|
|
586
|
-
}, l = async (
|
|
595
|
+
for (const f of [...o]) f();
|
|
596
|
+
}, l = async (f) => {
|
|
587
597
|
for (; ; ) {
|
|
588
|
-
|
|
598
|
+
f?.throwIfAborted();
|
|
589
599
|
const i = Date.now();
|
|
590
|
-
for (; e.length > 0 && e[0] <= i -
|
|
591
|
-
const v =
|
|
600
|
+
for (; e.length > 0 && e[0] <= i - r; ) e.shift();
|
|
601
|
+
const v = t.maxRequests !== void 0 && e.length >= t.maxRequests, d = t.maxConcurrent !== void 0 && a >= t.maxConcurrent;
|
|
592
602
|
if (!v && !d) {
|
|
593
603
|
e.push(i), a++;
|
|
594
604
|
return;
|
|
595
605
|
}
|
|
596
|
-
await new Promise((
|
|
606
|
+
await new Promise((c, p) => {
|
|
597
607
|
const m = () => {
|
|
598
|
-
clearTimeout(g), o.delete(m),
|
|
608
|
+
clearTimeout(g), o.delete(m), f?.removeEventListener("abort", h), c();
|
|
599
609
|
}, h = () => {
|
|
600
|
-
m(),
|
|
601
|
-
}, g = v ? setTimeout(m, e[0] +
|
|
602
|
-
o.add(m),
|
|
610
|
+
m(), p(f.reason);
|
|
611
|
+
}, g = v ? setTimeout(m, e[0] + r - i) : void 0;
|
|
612
|
+
o.add(m), f?.addEventListener("abort", h, { once: !0 });
|
|
603
613
|
});
|
|
604
614
|
}
|
|
605
615
|
};
|
|
606
616
|
return {
|
|
607
|
-
async wrapChat(
|
|
617
|
+
async wrapChat(f, i) {
|
|
608
618
|
await l(i.signal);
|
|
609
619
|
let v;
|
|
610
620
|
try {
|
|
611
|
-
v = await
|
|
621
|
+
v = await f(i);
|
|
612
622
|
} catch (d) {
|
|
613
623
|
throw s(), d;
|
|
614
624
|
}
|
|
615
|
-
return { ...v, stream:
|
|
625
|
+
return { ...v, stream: Z(v.stream, s) };
|
|
616
626
|
}
|
|
617
627
|
};
|
|
618
628
|
}
|
|
619
|
-
function
|
|
620
|
-
const e =
|
|
629
|
+
function Z(t, r) {
|
|
630
|
+
const e = t.getReader();
|
|
621
631
|
let a = !1;
|
|
622
632
|
const o = () => {
|
|
623
|
-
a || (a = !0,
|
|
633
|
+
a || (a = !0, r());
|
|
624
634
|
};
|
|
625
635
|
return new ReadableStream({
|
|
626
636
|
async pull(s) {
|
|
627
637
|
try {
|
|
628
|
-
const { done: l, value:
|
|
629
|
-
l ? (o(), s.close()) : s.enqueue(
|
|
638
|
+
const { done: l, value: f } = await e.read();
|
|
639
|
+
l ? (o(), s.close()) : s.enqueue(f);
|
|
630
640
|
} catch (l) {
|
|
631
641
|
o(), s.error(l);
|
|
632
642
|
}
|
|
@@ -636,89 +646,89 @@ function H(r, t) {
|
|
|
636
646
|
}
|
|
637
647
|
});
|
|
638
648
|
}
|
|
639
|
-
function
|
|
649
|
+
function G(t, r) {
|
|
640
650
|
return new Promise((e, a) => {
|
|
641
|
-
if (
|
|
651
|
+
if (r?.aborted) return a(r.reason);
|
|
642
652
|
const o = setTimeout(() => {
|
|
643
|
-
|
|
644
|
-
},
|
|
645
|
-
clearTimeout(o), a(
|
|
653
|
+
r?.removeEventListener("abort", s), e();
|
|
654
|
+
}, t), s = () => {
|
|
655
|
+
clearTimeout(o), a(r.reason);
|
|
646
656
|
};
|
|
647
|
-
|
|
657
|
+
r?.addEventListener("abort", s, { once: !0 });
|
|
648
658
|
});
|
|
649
659
|
}
|
|
650
|
-
function
|
|
660
|
+
function ce(t) {
|
|
651
661
|
return {
|
|
652
|
-
beforeRequest(
|
|
662
|
+
beforeRequest(r) {
|
|
653
663
|
return {
|
|
654
|
-
...
|
|
664
|
+
...r,
|
|
655
665
|
providerOptions: {
|
|
656
|
-
...
|
|
657
|
-
headers: { ...
|
|
666
|
+
...r.providerOptions,
|
|
667
|
+
headers: { ...r.providerOptions?.headers, ...t }
|
|
658
668
|
}
|
|
659
669
|
};
|
|
660
670
|
}
|
|
661
671
|
};
|
|
662
672
|
}
|
|
663
|
-
function
|
|
664
|
-
const
|
|
673
|
+
function ue(t = {}) {
|
|
674
|
+
const r = t.prefix ?? "vue-llm:chat:", e = () => t.storage ? t.storage : typeof localStorage < "u" ? localStorage : void 0;
|
|
665
675
|
return {
|
|
666
676
|
load(a) {
|
|
667
|
-
const o = e()?.getItem(
|
|
677
|
+
const o = e()?.getItem(r + a);
|
|
668
678
|
if (!o) return null;
|
|
669
679
|
try {
|
|
670
|
-
return
|
|
680
|
+
return V(o);
|
|
671
681
|
} catch {
|
|
672
682
|
return null;
|
|
673
683
|
}
|
|
674
684
|
},
|
|
675
685
|
save(a, o) {
|
|
676
|
-
e()?.setItem(
|
|
686
|
+
e()?.setItem(r + a, JSON.stringify(o));
|
|
677
687
|
},
|
|
678
688
|
remove(a) {
|
|
679
|
-
e()?.removeItem(
|
|
689
|
+
e()?.removeItem(r + a);
|
|
680
690
|
}
|
|
681
691
|
};
|
|
682
692
|
}
|
|
683
|
-
function
|
|
684
|
-
const
|
|
693
|
+
function fe() {
|
|
694
|
+
const t = /* @__PURE__ */ new Map();
|
|
685
695
|
return {
|
|
686
|
-
load(
|
|
687
|
-
const e =
|
|
688
|
-
return e ?
|
|
696
|
+
load(r) {
|
|
697
|
+
const e = t.get(r);
|
|
698
|
+
return e ? V(e) : null;
|
|
689
699
|
},
|
|
690
|
-
save(
|
|
691
|
-
|
|
700
|
+
save(r, e) {
|
|
701
|
+
t.set(r, JSON.stringify(e));
|
|
692
702
|
},
|
|
693
|
-
remove(
|
|
694
|
-
|
|
703
|
+
remove(r) {
|
|
704
|
+
t.delete(r);
|
|
695
705
|
}
|
|
696
706
|
};
|
|
697
707
|
}
|
|
698
|
-
function
|
|
699
|
-
const
|
|
700
|
-
return Array.isArray(
|
|
708
|
+
function V(t) {
|
|
709
|
+
const r = JSON.parse(t);
|
|
710
|
+
return Array.isArray(r) ? r.map((e) => ({
|
|
701
711
|
...e,
|
|
702
712
|
createdAt: e.createdAt ? new Date(e.createdAt) : void 0
|
|
703
713
|
})) : [];
|
|
704
714
|
}
|
|
705
715
|
export {
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
716
|
+
F as LLMAPIError,
|
|
717
|
+
_ as convertFilesToParts,
|
|
718
|
+
ae as createChatPrompt,
|
|
719
|
+
fe as createMemoryStorage,
|
|
720
|
+
H as createPrompt,
|
|
721
|
+
ue as createWebStorage,
|
|
722
|
+
me as fileToDataURL,
|
|
723
|
+
ce as headerMiddleware,
|
|
724
|
+
Y as isRetryableError,
|
|
725
|
+
se as loggingMiddleware,
|
|
726
|
+
ie as rateLimitMiddleware,
|
|
727
|
+
z as renderTemplate,
|
|
728
|
+
le as retryMiddleware,
|
|
729
|
+
ee as useChat,
|
|
730
|
+
te as useCompletion,
|
|
731
|
+
re as useObject,
|
|
732
|
+
oe as usePrompt,
|
|
733
|
+
ne as wrapProvider
|
|
724
734
|
};
|