vue-llm 1.3.0 → 1.4.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.es.md +401 -0
- package/README.ja.md +401 -0
- package/README.md +18 -12
- package/README.pt-BR.md +401 -0
- package/README.ru.md +401 -0
- package/README.zh-CN.md +401 -0
- package/dist/devtools.cjs +1 -1
- package/dist/devtools.js +16 -16
- package/dist/nuxt.cjs +1 -1
- package/dist/nuxt.js +4 -3
- package/dist/providers/webllm.d.ts +14 -1
- package/dist/providers.cjs +5 -5
- package/dist/providers.d.ts +2 -2
- package/dist/providers.js +387 -352
- package/dist/server.d.ts +4 -0
- package/dist/utils/browser.d.ts +1 -0
- package/package.json +4 -2
package/dist/providers.js
CHANGED
|
@@ -1,98 +1,106 @@
|
|
|
1
|
-
import { S as
|
|
2
|
-
import { c as
|
|
3
|
-
import { r as x, i as
|
|
4
|
-
import { c as
|
|
5
|
-
function
|
|
1
|
+
import { S as $ } from "./sse-DU5m1n6r.js";
|
|
2
|
+
import { c as ye } from "./sse-DU5m1n6r.js";
|
|
3
|
+
import { r as x, i as S, d as A } from "./file-x_kW7BHa.js";
|
|
4
|
+
import { c as P } from "./errors-B3A_zmVE.js";
|
|
5
|
+
function M() {
|
|
6
|
+
return typeof window < "u" || typeof globalThis.importScripts == "function";
|
|
7
|
+
}
|
|
8
|
+
function le(t) {
|
|
6
9
|
return {
|
|
7
10
|
id: "openai",
|
|
8
11
|
supportsStreaming: !0,
|
|
9
12
|
validateConfig() {
|
|
10
13
|
if (!t.apiKey)
|
|
11
14
|
throw new Error('[vue-llm] OpenAI API key is required. Pass it via createOpenAI({ apiKey: "..." })');
|
|
12
|
-
if (!t.dangerouslyAllowBrowser &&
|
|
15
|
+
if (!t.dangerouslyAllowBrowser && M())
|
|
13
16
|
throw new Error(
|
|
14
17
|
"[vue-llm] API keys must not be used in the browser. Use a server proxy (/api/chat) to keep keys safe. If you understand the risk, set dangerouslyAllowBrowser: true."
|
|
15
18
|
);
|
|
16
19
|
},
|
|
17
20
|
async chat(e) {
|
|
18
|
-
const o = t.baseURL ?? "https://api.openai.com/v1/chat/completions",
|
|
21
|
+
const o = t.baseURL ?? "https://api.openai.com/v1/chat/completions", n = {
|
|
19
22
|
model: e.model,
|
|
20
|
-
messages:
|
|
23
|
+
messages: D(e.messages),
|
|
21
24
|
stream: !0,
|
|
22
25
|
stream_options: { include_usage: !0 }
|
|
23
26
|
};
|
|
24
|
-
e.providerOptions?.response_format && (
|
|
27
|
+
e.providerOptions?.response_format && (n.response_format = e.providerOptions.response_format), e.tools?.length && (n.tools = e.tools.map((y) => ({
|
|
25
28
|
type: "function",
|
|
26
29
|
function: {
|
|
27
|
-
name:
|
|
28
|
-
description:
|
|
29
|
-
parameters:
|
|
30
|
+
name: y.name,
|
|
31
|
+
description: y.description,
|
|
32
|
+
parameters: y.parameters
|
|
30
33
|
}
|
|
31
34
|
})));
|
|
32
|
-
const
|
|
35
|
+
const c = await fetch(o, {
|
|
33
36
|
method: "POST",
|
|
34
37
|
headers: {
|
|
35
38
|
Authorization: `Bearer ${t.apiKey}`,
|
|
36
39
|
"Content-Type": "application/json",
|
|
37
40
|
...e.providerOptions?.headers
|
|
38
41
|
},
|
|
39
|
-
body: JSON.stringify(
|
|
42
|
+
body: JSON.stringify(n),
|
|
40
43
|
signal: e.signal
|
|
41
44
|
});
|
|
42
|
-
if (!
|
|
43
|
-
throw await
|
|
44
|
-
if (!
|
|
45
|
+
if (!c.ok)
|
|
46
|
+
throw await P("openai", "OpenAI API", c);
|
|
47
|
+
if (!c.body)
|
|
45
48
|
throw new Error("[vue-llm] No response body from OpenAI");
|
|
46
|
-
let
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
});
|
|
50
|
-
return { stream:
|
|
51
|
-
transform(
|
|
52
|
-
if (
|
|
53
|
-
let
|
|
49
|
+
let p;
|
|
50
|
+
const r = new Promise((y) => {
|
|
51
|
+
p = y;
|
|
52
|
+
}), m = /* @__PURE__ */ new Map();
|
|
53
|
+
return { stream: c.body.pipeThrough(new TextDecoderStream()).pipeThrough(new $()).pipeThrough(new TransformStream({
|
|
54
|
+
transform(y, s) {
|
|
55
|
+
if (y.data === "[DONE]") return;
|
|
56
|
+
let l;
|
|
54
57
|
try {
|
|
55
|
-
|
|
58
|
+
l = JSON.parse(y.data);
|
|
56
59
|
} catch {
|
|
57
60
|
return;
|
|
58
61
|
}
|
|
59
|
-
const
|
|
60
|
-
if (!
|
|
61
|
-
|
|
62
|
+
const i = l.choices?.[0];
|
|
63
|
+
if (!i) {
|
|
64
|
+
l.usage && p({
|
|
62
65
|
finishReason: "stop",
|
|
63
66
|
usage: {
|
|
64
|
-
promptTokens:
|
|
65
|
-
completionTokens:
|
|
66
|
-
totalTokens:
|
|
67
|
+
promptTokens: l.usage.prompt_tokens,
|
|
68
|
+
completionTokens: l.usage.completion_tokens,
|
|
69
|
+
totalTokens: l.usage.total_tokens
|
|
67
70
|
}
|
|
68
71
|
});
|
|
69
72
|
return;
|
|
70
73
|
}
|
|
71
|
-
const
|
|
72
|
-
if (
|
|
73
|
-
for (const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
74
|
+
const u = i.delta;
|
|
75
|
+
if (u.content && s.enqueue({ type: "text-delta", text: u.content }), u.tool_calls)
|
|
76
|
+
for (const [f, a] of u.tool_calls.entries()) {
|
|
77
|
+
const w = a.index ?? f;
|
|
78
|
+
let d = m.get(w);
|
|
79
|
+
if (d || (d = { started: !1, pendingArguments: [] }, m.set(w, d)), a.id && (d.id = a.id), a.function?.name && (d.name = a.function.name), d.id && d.name && !d.started) {
|
|
80
|
+
s.enqueue({
|
|
81
|
+
type: "tool-call-start",
|
|
82
|
+
toolCallId: d.id,
|
|
83
|
+
toolName: d.name
|
|
84
|
+
}), d.started = !0;
|
|
85
|
+
for (const g of d.pendingArguments)
|
|
86
|
+
s.enqueue({ type: "tool-call-delta", toolCallId: d.id, args: g });
|
|
87
|
+
d.pendingArguments = [];
|
|
88
|
+
}
|
|
89
|
+
a.function?.arguments && (d.started && d.id ? s.enqueue({ type: "tool-call-delta", toolCallId: d.id, args: a.function.arguments }) : d.pendingArguments.push(a.function.arguments));
|
|
90
|
+
}
|
|
91
|
+
if (i.finish_reason) {
|
|
92
|
+
for (const a of m.values())
|
|
93
|
+
a.started && a.id && s.enqueue({ type: "tool-call-end", toolCallId: a.id });
|
|
94
|
+
m.clear();
|
|
95
|
+
const f = i.finish_reason === "tool_calls" ? "tool-calls" : i.finish_reason === "length" ? "length" : "stop";
|
|
96
|
+
s.enqueue({ type: "finish", finishReason: f, usage: void 0 });
|
|
89
97
|
}
|
|
90
98
|
}
|
|
91
|
-
})), response:
|
|
99
|
+
})), response: r };
|
|
92
100
|
}
|
|
93
101
|
};
|
|
94
102
|
}
|
|
95
|
-
function
|
|
103
|
+
function D(t) {
|
|
96
104
|
return t.map((e) => e.role === "tool" ? {
|
|
97
105
|
role: "tool",
|
|
98
106
|
content: typeof e.content == "string" ? e.content : JSON.stringify(e.content),
|
|
@@ -110,10 +118,10 @@ function L(t) {
|
|
|
110
118
|
}))
|
|
111
119
|
} : {
|
|
112
120
|
role: e.role,
|
|
113
|
-
content: typeof e.content == "string" ? e.content : e.content.map(
|
|
121
|
+
content: typeof e.content == "string" ? e.content : e.content.map(G)
|
|
114
122
|
});
|
|
115
123
|
}
|
|
116
|
-
function
|
|
124
|
+
function G(t) {
|
|
117
125
|
if (t.type === "text") return { type: "text", text: t.text };
|
|
118
126
|
const e = x(t);
|
|
119
127
|
if (e.mediaType.startsWith("image/"))
|
|
@@ -125,44 +133,44 @@ function M(t) {
|
|
|
125
133
|
type: "file",
|
|
126
134
|
file: { filename: e.filename ?? "document.pdf", file_data: e.url }
|
|
127
135
|
};
|
|
128
|
-
const o =
|
|
136
|
+
const o = j[e.mediaType];
|
|
129
137
|
if (o)
|
|
130
138
|
return { type: "input_audio", input_audio: { data: e.base64, format: o } };
|
|
131
|
-
if (
|
|
132
|
-
return { type: "text", text:
|
|
139
|
+
if (S(e.mediaType))
|
|
140
|
+
return { type: "text", text: A(e.base64) };
|
|
133
141
|
throw new Error(`[vue-llm] OpenAI does not support ${e.mediaType} attachments`);
|
|
134
142
|
}
|
|
135
|
-
const
|
|
143
|
+
const j = {
|
|
136
144
|
"audio/wav": "wav",
|
|
137
145
|
"audio/x-wav": "wav",
|
|
138
146
|
"audio/mpeg": "mp3",
|
|
139
147
|
"audio/mp3": "mp3"
|
|
140
148
|
};
|
|
141
|
-
function
|
|
149
|
+
function pe(t) {
|
|
142
150
|
return {
|
|
143
151
|
id: "anthropic",
|
|
144
152
|
supportsStreaming: !0,
|
|
145
153
|
validateConfig() {
|
|
146
154
|
if (!t.apiKey)
|
|
147
155
|
throw new Error('[vue-llm] Anthropic API key is required. Pass it via createAnthropic({ apiKey: "..." })');
|
|
148
|
-
if (!t.dangerouslyAllowBrowser &&
|
|
156
|
+
if (!t.dangerouslyAllowBrowser && M())
|
|
149
157
|
throw new Error(
|
|
150
158
|
"[vue-llm] API keys must not be used in the browser. Use a server proxy (/api/chat) to keep keys safe. If you understand the risk, set dangerouslyAllowBrowser: true."
|
|
151
159
|
);
|
|
152
160
|
},
|
|
153
161
|
async chat(e) {
|
|
154
|
-
const o = (t.baseURL ?? "https://api.anthropic.com") + "/v1/messages",
|
|
162
|
+
const o = (t.baseURL ?? "https://api.anthropic.com") + "/v1/messages", n = e.messages.find((l) => l.role === "system"), c = e.messages.filter((l) => l.role !== "system"), p = {
|
|
155
163
|
model: e.model,
|
|
156
|
-
messages:
|
|
164
|
+
messages: K(c),
|
|
157
165
|
max_tokens: e.providerOptions?.maxTokens ?? 4096,
|
|
158
166
|
stream: !0
|
|
159
167
|
};
|
|
160
|
-
|
|
161
|
-
name:
|
|
162
|
-
description:
|
|
163
|
-
input_schema:
|
|
168
|
+
n && (p.system = typeof n.content == "string" ? n.content : n.content.map((l) => ({ type: "text", text: l.type === "text" ? l.text : "" }))), e.tools?.length && (p.tools = e.tools.map((l) => ({
|
|
169
|
+
name: l.name,
|
|
170
|
+
description: l.description,
|
|
171
|
+
input_schema: l.parameters
|
|
164
172
|
})));
|
|
165
|
-
const
|
|
173
|
+
const r = await fetch(o, {
|
|
166
174
|
method: "POST",
|
|
167
175
|
headers: {
|
|
168
176
|
"x-api-key": t.apiKey,
|
|
@@ -170,73 +178,73 @@ function oe(t) {
|
|
|
170
178
|
"Content-Type": "application/json",
|
|
171
179
|
...e.providerOptions?.headers
|
|
172
180
|
},
|
|
173
|
-
body: JSON.stringify(
|
|
181
|
+
body: JSON.stringify(p),
|
|
174
182
|
signal: e.signal
|
|
175
183
|
});
|
|
176
|
-
if (!
|
|
177
|
-
throw await
|
|
178
|
-
if (!
|
|
184
|
+
if (!r.ok)
|
|
185
|
+
throw await P("anthropic", "Anthropic API", r);
|
|
186
|
+
if (!r.body)
|
|
179
187
|
throw new Error("[vue-llm] No response body from Anthropic");
|
|
180
|
-
let
|
|
181
|
-
const
|
|
182
|
-
|
|
188
|
+
let m;
|
|
189
|
+
const _ = new Promise((l) => {
|
|
190
|
+
m = l;
|
|
183
191
|
});
|
|
184
192
|
let y = "";
|
|
185
|
-
return { stream:
|
|
186
|
-
transform(
|
|
187
|
-
let
|
|
193
|
+
return { stream: r.body.pipeThrough(new TextDecoderStream()).pipeThrough(new $()).pipeThrough(new TransformStream({
|
|
194
|
+
transform(l, i) {
|
|
195
|
+
let u;
|
|
188
196
|
try {
|
|
189
|
-
|
|
197
|
+
u = JSON.parse(l.data);
|
|
190
198
|
} catch {
|
|
191
199
|
return;
|
|
192
200
|
}
|
|
193
|
-
switch (
|
|
201
|
+
switch (l.event ?? u.type) {
|
|
194
202
|
case "content_block_start": {
|
|
195
|
-
const
|
|
196
|
-
|
|
203
|
+
const a = u.content_block;
|
|
204
|
+
a?.type === "tool_use" && (y = a.id, i.enqueue({
|
|
197
205
|
type: "tool-call-start",
|
|
198
206
|
toolCallId: y,
|
|
199
|
-
toolName:
|
|
207
|
+
toolName: a.name
|
|
200
208
|
}));
|
|
201
209
|
break;
|
|
202
210
|
}
|
|
203
211
|
case "content_block_delta": {
|
|
204
|
-
const
|
|
205
|
-
|
|
212
|
+
const a = u.delta;
|
|
213
|
+
a?.type === "text_delta" && a.text && i.enqueue({ type: "text-delta", text: a.text }), a?.type === "thinking_delta" && a.text && i.enqueue({ type: "reasoning-delta", text: a.text }), a?.type === "input_json_delta" && a.partial_json && i.enqueue({
|
|
206
214
|
type: "tool-call-delta",
|
|
207
215
|
toolCallId: y,
|
|
208
|
-
args:
|
|
216
|
+
args: a.partial_json
|
|
209
217
|
});
|
|
210
218
|
break;
|
|
211
219
|
}
|
|
212
220
|
case "content_block_stop": {
|
|
213
|
-
y && (
|
|
221
|
+
y && (i.enqueue({ type: "tool-call-end", toolCallId: y }), y = "");
|
|
214
222
|
break;
|
|
215
223
|
}
|
|
216
224
|
case "message_delta": {
|
|
217
|
-
const
|
|
218
|
-
|
|
225
|
+
const a = u.delta, w = u.usage, d = a?.stop_reason === "tool_use" ? "tool-calls" : a?.stop_reason === "max_tokens" ? "length" : "stop";
|
|
226
|
+
i.enqueue({
|
|
219
227
|
type: "finish",
|
|
220
|
-
finishReason:
|
|
228
|
+
finishReason: d,
|
|
221
229
|
usage: w ? { promptTokens: 0, completionTokens: w.output_tokens ?? 0, totalTokens: w.output_tokens ?? 0 } : void 0
|
|
222
|
-
}),
|
|
230
|
+
}), m({ finishReason: d, usage: void 0 });
|
|
223
231
|
break;
|
|
224
232
|
}
|
|
225
233
|
case "error": {
|
|
226
|
-
const
|
|
227
|
-
|
|
234
|
+
const a = u.error;
|
|
235
|
+
i.enqueue({
|
|
228
236
|
type: "error",
|
|
229
|
-
error: new Error(`[vue-llm] Anthropic stream error: ${
|
|
237
|
+
error: new Error(`[vue-llm] Anthropic stream error: ${a?.message ?? "unknown"}`)
|
|
230
238
|
});
|
|
231
239
|
break;
|
|
232
240
|
}
|
|
233
241
|
}
|
|
234
242
|
}
|
|
235
|
-
})), response:
|
|
243
|
+
})), response: _ };
|
|
236
244
|
}
|
|
237
245
|
};
|
|
238
246
|
}
|
|
239
|
-
function
|
|
247
|
+
function K(t) {
|
|
240
248
|
return t.map((e) => {
|
|
241
249
|
if (e.role === "tool")
|
|
242
250
|
return {
|
|
@@ -250,133 +258,133 @@ function U(t) {
|
|
|
250
258
|
if (e.role === "assistant" && e.toolCalls?.length) {
|
|
251
259
|
const o = [];
|
|
252
260
|
typeof e.content == "string" && e.content && o.push({ type: "text", text: e.content });
|
|
253
|
-
for (const
|
|
261
|
+
for (const n of e.toolCalls)
|
|
254
262
|
o.push({
|
|
255
263
|
type: "tool_use",
|
|
256
|
-
id:
|
|
257
|
-
name:
|
|
258
|
-
input:
|
|
264
|
+
id: n.id,
|
|
265
|
+
name: n.name,
|
|
266
|
+
input: n.args
|
|
259
267
|
});
|
|
260
268
|
return { role: "assistant", content: o };
|
|
261
269
|
}
|
|
262
270
|
return {
|
|
263
271
|
role: e.role,
|
|
264
|
-
content: typeof e.content == "string" ? e.content : e.content.map(
|
|
272
|
+
content: typeof e.content == "string" ? e.content : e.content.map(B)
|
|
265
273
|
};
|
|
266
274
|
});
|
|
267
275
|
}
|
|
268
|
-
function
|
|
276
|
+
function B(t) {
|
|
269
277
|
if (t.type === "text") return { type: "text", text: t.text };
|
|
270
278
|
const e = x(t), o = e.base64 ? { type: "base64", media_type: e.mediaType, data: e.base64 } : { type: "url", url: e.url };
|
|
271
279
|
if (e.mediaType.startsWith("image/"))
|
|
272
280
|
return { type: "image", source: o };
|
|
273
281
|
if (e.mediaType === "application/pdf")
|
|
274
282
|
return { type: "document", source: o, ...e.filename ? { title: e.filename } : {} };
|
|
275
|
-
if (
|
|
283
|
+
if (S(e.mediaType) && e.base64)
|
|
276
284
|
return {
|
|
277
285
|
type: "document",
|
|
278
|
-
source: { type: "text", media_type: "text/plain", data:
|
|
286
|
+
source: { type: "text", media_type: "text/plain", data: A(e.base64) },
|
|
279
287
|
...e.filename ? { title: e.filename } : {}
|
|
280
288
|
};
|
|
281
289
|
throw new Error(`[vue-llm] Anthropic does not support ${e.mediaType} attachments`);
|
|
282
290
|
}
|
|
283
|
-
function
|
|
291
|
+
function ce(t) {
|
|
284
292
|
return {
|
|
285
293
|
id: "google",
|
|
286
294
|
supportsStreaming: !0,
|
|
287
295
|
validateConfig() {
|
|
288
296
|
if (!t.apiKey)
|
|
289
297
|
throw new Error('[vue-llm] Google AI API key is required. Pass it via createGoogle({ apiKey: "..." })');
|
|
290
|
-
if (!t.dangerouslyAllowBrowser &&
|
|
298
|
+
if (!t.dangerouslyAllowBrowser && M())
|
|
291
299
|
throw new Error(
|
|
292
300
|
"[vue-llm] API keys must not be used in the browser. Use a server proxy to keep keys safe. If you understand the risk, set dangerouslyAllowBrowser: true."
|
|
293
301
|
);
|
|
294
302
|
},
|
|
295
303
|
async chat(e) {
|
|
296
|
-
const
|
|
297
|
-
contents:
|
|
298
|
-
},
|
|
299
|
-
|
|
300
|
-
parts: [{ text: typeof
|
|
301
|
-
}), e.tools?.length && (
|
|
302
|
-
functionDeclarations: e.tools.map((
|
|
303
|
-
name:
|
|
304
|
-
description:
|
|
305
|
-
parameters:
|
|
304
|
+
const n = `${t.baseURL ?? "https://generativelanguage.googleapis.com/v1beta"}/models/${e.model}:streamGenerateContent?alt=sse&key=${t.apiKey}`, c = {
|
|
305
|
+
contents: q(e.messages)
|
|
306
|
+
}, p = e.messages.find((i) => i.role === "system");
|
|
307
|
+
p && (c.systemInstruction = {
|
|
308
|
+
parts: [{ text: typeof p.content == "string" ? p.content : "" }]
|
|
309
|
+
}), e.tools?.length && (c.tools = [{
|
|
310
|
+
functionDeclarations: e.tools.map((i) => ({
|
|
311
|
+
name: i.name,
|
|
312
|
+
description: i.description,
|
|
313
|
+
parameters: i.parameters
|
|
306
314
|
}))
|
|
307
|
-
}]),
|
|
315
|
+
}]), c.generationConfig = {
|
|
308
316
|
...e.providerOptions?.response_format?.type === "json_object" ? { responseMimeType: "application/json" } : {}
|
|
309
317
|
};
|
|
310
|
-
const
|
|
318
|
+
const r = await fetch(n, {
|
|
311
319
|
method: "POST",
|
|
312
320
|
headers: {
|
|
313
321
|
"Content-Type": "application/json",
|
|
314
322
|
...e.providerOptions?.headers
|
|
315
323
|
},
|
|
316
|
-
body: JSON.stringify(
|
|
324
|
+
body: JSON.stringify(c),
|
|
317
325
|
signal: e.signal
|
|
318
326
|
});
|
|
319
|
-
if (!
|
|
320
|
-
throw await
|
|
321
|
-
if (!
|
|
327
|
+
if (!r.ok)
|
|
328
|
+
throw await P("google", "Google AI API", r);
|
|
329
|
+
if (!r.body)
|
|
322
330
|
throw new Error("[vue-llm] No response body from Google AI");
|
|
323
|
-
let
|
|
324
|
-
const
|
|
325
|
-
|
|
331
|
+
let m;
|
|
332
|
+
const _ = new Promise((i) => {
|
|
333
|
+
m = i;
|
|
326
334
|
});
|
|
327
|
-
let y = 0,
|
|
328
|
-
return { stream:
|
|
335
|
+
let y = 0, s = !1;
|
|
336
|
+
return { stream: r.body.pipeThrough(new TextDecoderStream()).pipeThrough(new TransformStream({
|
|
329
337
|
buffer: "",
|
|
330
|
-
transform(
|
|
331
|
-
this.buffer +=
|
|
332
|
-
const
|
|
338
|
+
transform(i, u) {
|
|
339
|
+
this.buffer += i;
|
|
340
|
+
const f = this.buffer.split(`
|
|
333
341
|
`);
|
|
334
|
-
this.buffer =
|
|
335
|
-
for (const
|
|
336
|
-
|
|
342
|
+
this.buffer = f.pop();
|
|
343
|
+
for (const a of f)
|
|
344
|
+
a.startsWith("data: ") && u.enqueue(a.slice(6));
|
|
337
345
|
},
|
|
338
|
-
flush(
|
|
339
|
-
const
|
|
340
|
-
|
|
346
|
+
flush(i) {
|
|
347
|
+
const u = this.buffer.trim();
|
|
348
|
+
u.startsWith("data: ") && i.enqueue(u.slice(6));
|
|
341
349
|
}
|
|
342
350
|
})).pipeThrough(new TransformStream({
|
|
343
|
-
transform(
|
|
344
|
-
let
|
|
351
|
+
transform(i, u) {
|
|
352
|
+
let f;
|
|
345
353
|
try {
|
|
346
|
-
|
|
354
|
+
f = JSON.parse(i);
|
|
347
355
|
} catch {
|
|
348
356
|
return;
|
|
349
357
|
}
|
|
350
|
-
const
|
|
351
|
-
if (!
|
|
352
|
-
const w =
|
|
358
|
+
const a = f.candidates?.[0];
|
|
359
|
+
if (!a) return;
|
|
360
|
+
const w = a.content?.parts;
|
|
353
361
|
if (w) {
|
|
354
|
-
for (const
|
|
355
|
-
if (
|
|
362
|
+
for (const d of w)
|
|
363
|
+
if (d.text !== void 0 && u.enqueue({ type: "text-delta", text: d.text }), d.functionCall) {
|
|
356
364
|
const g = `tc_gemini_${++y}`;
|
|
357
|
-
|
|
365
|
+
s = !0, u.enqueue({
|
|
358
366
|
type: "tool-call-start",
|
|
359
367
|
toolCallId: g,
|
|
360
|
-
toolName:
|
|
361
|
-
}),
|
|
368
|
+
toolName: d.functionCall.name
|
|
369
|
+
}), u.enqueue({
|
|
362
370
|
type: "tool-call-delta",
|
|
363
371
|
toolCallId: g,
|
|
364
|
-
args: JSON.stringify(
|
|
365
|
-
}),
|
|
372
|
+
args: JSON.stringify(d.functionCall.args ?? {})
|
|
373
|
+
}), u.enqueue({ type: "tool-call-end", toolCallId: g });
|
|
366
374
|
}
|
|
367
375
|
}
|
|
368
|
-
if (
|
|
369
|
-
const
|
|
370
|
-
|
|
376
|
+
if (a.finishReason) {
|
|
377
|
+
const d = s ? "tool-calls" : a.finishReason === "MAX_TOKENS" ? "length" : "stop", g = f.usageMetadata;
|
|
378
|
+
u.enqueue({
|
|
371
379
|
type: "finish",
|
|
372
|
-
finishReason:
|
|
380
|
+
finishReason: d,
|
|
373
381
|
usage: g ? {
|
|
374
382
|
promptTokens: g.promptTokenCount ?? 0,
|
|
375
383
|
completionTokens: g.candidatesTokenCount ?? 0,
|
|
376
384
|
totalTokens: g.totalTokenCount ?? 0
|
|
377
385
|
} : void 0
|
|
378
|
-
}),
|
|
379
|
-
finishReason:
|
|
386
|
+
}), m({
|
|
387
|
+
finishReason: d,
|
|
380
388
|
usage: g ? {
|
|
381
389
|
promptTokens: g.promptTokenCount ?? 0,
|
|
382
390
|
completionTokens: g.candidatesTokenCount ?? 0,
|
|
@@ -385,49 +393,49 @@ function ne(t) {
|
|
|
385
393
|
});
|
|
386
394
|
}
|
|
387
395
|
}
|
|
388
|
-
})), response:
|
|
396
|
+
})), response: _ };
|
|
389
397
|
}
|
|
390
398
|
};
|
|
391
399
|
}
|
|
392
|
-
function
|
|
400
|
+
function q(t) {
|
|
393
401
|
const e = [], o = /* @__PURE__ */ new Map();
|
|
394
|
-
for (const
|
|
395
|
-
if (
|
|
396
|
-
if (
|
|
397
|
-
const
|
|
402
|
+
for (const n of t) {
|
|
403
|
+
if (n.role === "system") continue;
|
|
404
|
+
if (n.role === "tool") {
|
|
405
|
+
const r = {
|
|
398
406
|
functionResponse: {
|
|
399
|
-
name: o.get(
|
|
400
|
-
response: { result:
|
|
407
|
+
name: o.get(n.toolCallId ?? "") ?? n.toolCallId ?? "",
|
|
408
|
+
response: { result: F(n.content) }
|
|
401
409
|
}
|
|
402
|
-
},
|
|
403
|
-
|
|
410
|
+
}, m = e.at(-1);
|
|
411
|
+
m?.role === "user" && m.parts.every((_) => _.functionResponse) ? m.parts.push(r) : e.push({ role: "user", parts: [r] });
|
|
404
412
|
continue;
|
|
405
413
|
}
|
|
406
|
-
for (const
|
|
407
|
-
o.set(
|
|
408
|
-
const
|
|
409
|
-
if (typeof
|
|
410
|
-
|
|
411
|
-
else if (Array.isArray(
|
|
412
|
-
for (const
|
|
413
|
-
if (
|
|
414
|
-
const
|
|
415
|
-
|
|
414
|
+
for (const r of n.toolCalls ?? [])
|
|
415
|
+
o.set(r.id, r.name);
|
|
416
|
+
const c = n.role === "assistant" ? "model" : "user", p = [];
|
|
417
|
+
if (typeof n.content == "string")
|
|
418
|
+
n.content && p.push({ text: n.content });
|
|
419
|
+
else if (Array.isArray(n.content)) {
|
|
420
|
+
for (const r of n.content)
|
|
421
|
+
if (r.type === "text" && p.push({ text: r.text }), r.type === "image" || r.type === "file") {
|
|
422
|
+
const m = x(r);
|
|
423
|
+
p.push(m.base64 ? { inlineData: { mimeType: m.mediaType, data: m.base64 } } : { fileData: { mimeType: m.mediaType, fileUri: m.url } });
|
|
416
424
|
}
|
|
417
425
|
}
|
|
418
|
-
if (
|
|
419
|
-
for (const
|
|
420
|
-
|
|
426
|
+
if (n.toolCalls?.length)
|
|
427
|
+
for (const r of n.toolCalls)
|
|
428
|
+
p.push({
|
|
421
429
|
functionCall: {
|
|
422
|
-
name:
|
|
423
|
-
args: typeof
|
|
430
|
+
name: r.name,
|
|
431
|
+
args: typeof r.args == "string" ? JSON.parse(r.args) : r.args
|
|
424
432
|
}
|
|
425
433
|
});
|
|
426
|
-
|
|
434
|
+
p.length > 0 && e.push({ role: c, parts: p });
|
|
427
435
|
}
|
|
428
436
|
return e;
|
|
429
437
|
}
|
|
430
|
-
function
|
|
438
|
+
function F(t) {
|
|
431
439
|
if (typeof t != "string") return t;
|
|
432
440
|
try {
|
|
433
441
|
return JSON.parse(t);
|
|
@@ -435,90 +443,90 @@ function j(t) {
|
|
|
435
443
|
return t;
|
|
436
444
|
}
|
|
437
445
|
}
|
|
438
|
-
function
|
|
446
|
+
function ue(t) {
|
|
439
447
|
return {
|
|
440
448
|
id: "ollama",
|
|
441
449
|
supportsStreaming: !0,
|
|
442
450
|
validateConfig() {
|
|
443
451
|
},
|
|
444
452
|
async chat(e) {
|
|
445
|
-
const o = (t?.baseURL ?? "http://localhost:11434") + "/api/chat",
|
|
453
|
+
const o = (t?.baseURL ?? "http://localhost:11434") + "/api/chat", n = {
|
|
446
454
|
model: e.model,
|
|
447
|
-
messages:
|
|
455
|
+
messages: H(e.messages),
|
|
448
456
|
stream: !0
|
|
449
457
|
};
|
|
450
|
-
e.tools?.length && (
|
|
458
|
+
e.tools?.length && (n.tools = e.tools.map((s) => ({
|
|
451
459
|
type: "function",
|
|
452
460
|
function: {
|
|
453
|
-
name:
|
|
454
|
-
description:
|
|
455
|
-
parameters:
|
|
461
|
+
name: s.name,
|
|
462
|
+
description: s.description,
|
|
463
|
+
parameters: s.parameters
|
|
456
464
|
}
|
|
457
465
|
})));
|
|
458
|
-
const
|
|
466
|
+
const c = await fetch(o, {
|
|
459
467
|
method: "POST",
|
|
460
468
|
headers: {
|
|
461
469
|
"Content-Type": "application/json",
|
|
462
470
|
...e.providerOptions?.headers
|
|
463
471
|
},
|
|
464
|
-
body: JSON.stringify(
|
|
472
|
+
body: JSON.stringify(n),
|
|
465
473
|
signal: e.signal
|
|
466
474
|
});
|
|
467
|
-
if (!
|
|
468
|
-
throw await
|
|
469
|
-
if (!
|
|
475
|
+
if (!c.ok)
|
|
476
|
+
throw await P("ollama", "Ollama API", c);
|
|
477
|
+
if (!c.body)
|
|
470
478
|
throw new Error("[vue-llm] No response body from Ollama");
|
|
471
|
-
let
|
|
472
|
-
const
|
|
473
|
-
|
|
479
|
+
let p;
|
|
480
|
+
const r = new Promise((s) => {
|
|
481
|
+
p = s;
|
|
474
482
|
});
|
|
475
|
-
let
|
|
476
|
-
return { stream:
|
|
477
|
-
transform(
|
|
478
|
-
|
|
479
|
-
const
|
|
483
|
+
let m = "", _ = !1;
|
|
484
|
+
return { stream: c.body.pipeThrough(new TextDecoderStream()).pipeThrough(new TransformStream({
|
|
485
|
+
transform(s, l) {
|
|
486
|
+
m += s;
|
|
487
|
+
const i = m.split(`
|
|
480
488
|
`);
|
|
481
|
-
|
|
482
|
-
for (const
|
|
483
|
-
if (!
|
|
484
|
-
let
|
|
489
|
+
m = i.pop() ?? "";
|
|
490
|
+
for (const u of i) {
|
|
491
|
+
if (!u.trim()) continue;
|
|
492
|
+
let f;
|
|
485
493
|
try {
|
|
486
|
-
|
|
494
|
+
f = JSON.parse(u);
|
|
487
495
|
} catch {
|
|
488
496
|
continue;
|
|
489
497
|
}
|
|
490
|
-
if (
|
|
491
|
-
for (const
|
|
492
|
-
|
|
498
|
+
if (f.message?.content && l.enqueue({ type: "text-delta", text: f.message.content }), f.message?.tool_calls)
|
|
499
|
+
for (const a of f.message.tool_calls) {
|
|
500
|
+
_ = !0;
|
|
493
501
|
const w = `ollama-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
494
|
-
|
|
502
|
+
l.enqueue({
|
|
495
503
|
type: "tool-call-start",
|
|
496
504
|
toolCallId: w,
|
|
497
|
-
toolName:
|
|
498
|
-
}),
|
|
505
|
+
toolName: a.function.name
|
|
506
|
+
}), l.enqueue({
|
|
499
507
|
type: "tool-call-delta",
|
|
500
508
|
toolCallId: w,
|
|
501
|
-
args: JSON.stringify(
|
|
502
|
-
}),
|
|
509
|
+
args: JSON.stringify(a.function.arguments)
|
|
510
|
+
}), l.enqueue({ type: "tool-call-end", toolCallId: w });
|
|
503
511
|
}
|
|
504
|
-
if (
|
|
505
|
-
const
|
|
506
|
-
|
|
507
|
-
finishReason:
|
|
508
|
-
usage:
|
|
509
|
-
promptTokens:
|
|
510
|
-
completionTokens:
|
|
511
|
-
totalTokens: (
|
|
512
|
+
if (f.done) {
|
|
513
|
+
const a = _ ? "tool-calls" : "stop";
|
|
514
|
+
l.enqueue({ type: "finish", finishReason: a }), p({
|
|
515
|
+
finishReason: a,
|
|
516
|
+
usage: f.eval_count ? {
|
|
517
|
+
promptTokens: f.prompt_eval_count ?? 0,
|
|
518
|
+
completionTokens: f.eval_count,
|
|
519
|
+
totalTokens: (f.prompt_eval_count ?? 0) + f.eval_count
|
|
512
520
|
} : void 0
|
|
513
521
|
});
|
|
514
522
|
}
|
|
515
523
|
}
|
|
516
524
|
}
|
|
517
|
-
})), response:
|
|
525
|
+
})), response: r };
|
|
518
526
|
}
|
|
519
527
|
};
|
|
520
528
|
}
|
|
521
|
-
function
|
|
529
|
+
function H(t) {
|
|
522
530
|
const e = /* @__PURE__ */ new Map();
|
|
523
531
|
return t.map((o) => {
|
|
524
532
|
if (o.role === "tool")
|
|
@@ -528,172 +536,198 @@ function G(t) {
|
|
|
528
536
|
tool_name: e.get(o.toolCallId ?? "")
|
|
529
537
|
};
|
|
530
538
|
if (o.role === "assistant" && o.toolCalls?.length) {
|
|
531
|
-
for (const
|
|
539
|
+
for (const p of o.toolCalls) e.set(p.id, p.name);
|
|
532
540
|
return {
|
|
533
541
|
role: "assistant",
|
|
534
542
|
content: typeof o.content == "string" ? o.content : "",
|
|
535
|
-
tool_calls: o.toolCalls.map((
|
|
543
|
+
tool_calls: o.toolCalls.map((p) => ({
|
|
536
544
|
function: {
|
|
537
|
-
name:
|
|
538
|
-
arguments: typeof
|
|
545
|
+
name: p.name,
|
|
546
|
+
arguments: typeof p.args == "string" ? JSON.parse(p.args) : p.args
|
|
539
547
|
}
|
|
540
548
|
}))
|
|
541
549
|
};
|
|
542
550
|
}
|
|
543
551
|
if (typeof o.content == "string")
|
|
544
552
|
return { role: o.role, content: o.content };
|
|
545
|
-
const
|
|
546
|
-
for (const
|
|
547
|
-
if (
|
|
548
|
-
|
|
553
|
+
const n = [], c = [];
|
|
554
|
+
for (const p of o.content) {
|
|
555
|
+
if (p.type === "text") {
|
|
556
|
+
n.push(p.text);
|
|
549
557
|
continue;
|
|
550
558
|
}
|
|
551
|
-
const
|
|
552
|
-
if (!
|
|
559
|
+
const r = x(p);
|
|
560
|
+
if (!r.base64)
|
|
553
561
|
throw new Error("[vue-llm] Ollama requires attachments as data URLs, got a remote URL");
|
|
554
|
-
if (
|
|
555
|
-
|
|
556
|
-
else if (
|
|
557
|
-
|
|
562
|
+
if (r.mediaType.startsWith("image/"))
|
|
563
|
+
c.push(r.base64);
|
|
564
|
+
else if (S(r.mediaType))
|
|
565
|
+
n.push(A(r.base64));
|
|
558
566
|
else
|
|
559
|
-
throw new Error(`[vue-llm] Ollama does not support ${
|
|
567
|
+
throw new Error(`[vue-llm] Ollama does not support ${r.mediaType} attachments`);
|
|
560
568
|
}
|
|
561
569
|
return {
|
|
562
570
|
role: o.role,
|
|
563
|
-
content:
|
|
571
|
+
content: n.join(`
|
|
564
572
|
|
|
565
573
|
`),
|
|
566
|
-
...
|
|
574
|
+
...c.length > 0 ? { images: c } : {}
|
|
567
575
|
};
|
|
568
576
|
});
|
|
569
577
|
}
|
|
570
|
-
function
|
|
578
|
+
function z() {
|
|
571
579
|
return typeof navigator < "u" && "gpu" in navigator && !!navigator.gpu;
|
|
572
580
|
}
|
|
573
|
-
function
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
581
|
+
async function X() {
|
|
582
|
+
try {
|
|
583
|
+
const e = await navigator.gpu?.requestAdapter();
|
|
584
|
+
return e?.features.has("shader-f16") ? e.info?.vendor?.toLowerCase().includes("intel") ? "f32" : "f16" : "f32";
|
|
585
|
+
} catch {
|
|
586
|
+
return "f32";
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
const J = /-q(\d)f(16|32)(_\d+)?-/;
|
|
590
|
+
function fe(t) {
|
|
591
|
+
let e, o, n, c, p;
|
|
592
|
+
const r = (s) => t.onProgress?.(s);
|
|
593
|
+
async function m(s) {
|
|
594
|
+
const l = J.exec(s);
|
|
595
|
+
if (!l) return s;
|
|
596
|
+
const i = t.precision ?? "auto";
|
|
597
|
+
if (i === "auto" && l[2] === "32") return s;
|
|
598
|
+
const u = i === "auto" ? await (p ??= X()) : i;
|
|
599
|
+
return s.replace(J, `-q${l[1]}${u}${l[3] ?? ""}-`);
|
|
600
|
+
}
|
|
601
|
+
async function _(s) {
|
|
602
|
+
if (e)
|
|
603
|
+
await (await e).reload(s, t.chatOptions);
|
|
604
|
+
else {
|
|
605
|
+
e = t.createEngine(
|
|
606
|
+
s,
|
|
607
|
+
{ ...t.engineConfig, initProgressCallback: r },
|
|
608
|
+
t.chatOptions
|
|
609
|
+
);
|
|
610
|
+
try {
|
|
611
|
+
await e;
|
|
612
|
+
} catch (l) {
|
|
613
|
+
throw e = void 0, l;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
function y(s) {
|
|
618
|
+
if (o === s && !c) return Promise.resolve();
|
|
619
|
+
const i = (c ?? Promise.resolve()).catch(() => {
|
|
579
620
|
}).then(async () => {
|
|
580
|
-
if (o
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
);
|
|
589
|
-
try {
|
|
590
|
-
await e;
|
|
591
|
-
} catch (m) {
|
|
592
|
-
throw e = void 0, m;
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
o = n;
|
|
621
|
+
if (o === s) return;
|
|
622
|
+
o = void 0, n = void 0;
|
|
623
|
+
let f = await m(s);
|
|
624
|
+
try {
|
|
625
|
+
await _(f);
|
|
626
|
+
} catch (a) {
|
|
627
|
+
if (f === s || a?.name !== "ModelNotFoundError") throw a;
|
|
628
|
+
console.warn(`[vue-llm] ${f} is not available, loading ${s} instead`), f = s, await _(f);
|
|
596
629
|
}
|
|
630
|
+
o = s, n = f;
|
|
597
631
|
});
|
|
598
|
-
|
|
599
|
-
const
|
|
600
|
-
|
|
632
|
+
c = i;
|
|
633
|
+
const u = () => {
|
|
634
|
+
c === i && (c = void 0);
|
|
601
635
|
};
|
|
602
|
-
return
|
|
636
|
+
return i.then(u, u), i;
|
|
603
637
|
}
|
|
604
638
|
return {
|
|
605
639
|
id: "webllm",
|
|
606
640
|
supportsStreaming: !0,
|
|
607
641
|
get loadedModel() {
|
|
608
|
-
return
|
|
642
|
+
return n;
|
|
609
643
|
},
|
|
610
644
|
validateConfig() {
|
|
611
645
|
if (typeof t.createEngine != "function")
|
|
612
646
|
throw new Error(
|
|
613
647
|
"[vue-llm] createWebLLM requires an engine factory. Pass createWebLLM({ createEngine: CreateMLCEngine }) from @mlc-ai/web-llm"
|
|
614
648
|
);
|
|
615
|
-
if (!
|
|
649
|
+
if (!z())
|
|
616
650
|
throw new Error("[vue-llm] In-browser models require WebGPU, which this browser does not support");
|
|
617
651
|
},
|
|
618
|
-
load:
|
|
652
|
+
load: y,
|
|
619
653
|
async unload() {
|
|
620
|
-
const
|
|
621
|
-
e = void 0, o = void 0, n && await (await
|
|
654
|
+
const s = e;
|
|
655
|
+
e = void 0, o = void 0, n = void 0, s && await (await s).unload();
|
|
622
656
|
},
|
|
623
|
-
async chat(
|
|
624
|
-
await s
|
|
625
|
-
const
|
|
626
|
-
if (
|
|
627
|
-
throw
|
|
628
|
-
const
|
|
629
|
-
...
|
|
630
|
-
messages:
|
|
657
|
+
async chat(s) {
|
|
658
|
+
await y(s.model);
|
|
659
|
+
const l = await e;
|
|
660
|
+
if (s.signal?.aborted)
|
|
661
|
+
throw s.signal.reason ?? new DOMException("Aborted", "AbortError");
|
|
662
|
+
const i = {
|
|
663
|
+
...V(s.providerOptions),
|
|
664
|
+
messages: Y(s.messages),
|
|
631
665
|
stream: !0,
|
|
632
666
|
stream_options: { include_usage: !0 }
|
|
633
667
|
};
|
|
634
|
-
|
|
668
|
+
s.providerOptions?.response_format && (i.response_format = s.providerOptions.response_format), s.tools?.length && (i.tools = s.tools.map((h) => ({
|
|
635
669
|
type: "function",
|
|
636
670
|
function: { name: h.name, description: h.description, parameters: h.parameters }
|
|
637
671
|
})));
|
|
638
|
-
const
|
|
639
|
-
|
|
672
|
+
const u = await l.chat.completions.create(i), f = () => {
|
|
673
|
+
l.interruptGenerate();
|
|
640
674
|
};
|
|
641
|
-
|
|
642
|
-
let
|
|
643
|
-
const
|
|
644
|
-
|
|
675
|
+
s.signal?.addEventListener("abort", f, { once: !0 });
|
|
676
|
+
let a, w;
|
|
677
|
+
const d = new Promise((h, b) => {
|
|
678
|
+
a = h, w = b;
|
|
645
679
|
});
|
|
646
|
-
|
|
680
|
+
d.catch(() => {
|
|
647
681
|
});
|
|
648
|
-
const
|
|
649
|
-
let
|
|
650
|
-
const
|
|
651
|
-
h.usage && (
|
|
682
|
+
const g = u[Symbol.asyncIterator](), L = oe(), C = /* @__PURE__ */ new Map();
|
|
683
|
+
let N = "stop", O;
|
|
684
|
+
const E = () => s.signal?.removeEventListener("abort", f), U = (h) => {
|
|
685
|
+
h.usage && (O = {
|
|
652
686
|
promptTokens: h.usage.prompt_tokens,
|
|
653
687
|
completionTokens: h.usage.completion_tokens,
|
|
654
688
|
totalTokens: h.usage.total_tokens
|
|
655
689
|
});
|
|
656
|
-
const
|
|
657
|
-
if (!
|
|
658
|
-
const
|
|
659
|
-
|
|
660
|
-
for (const [
|
|
661
|
-
const
|
|
662
|
-
let k =
|
|
663
|
-
if (k || (k =
|
|
664
|
-
const
|
|
665
|
-
|
|
690
|
+
const b = h.choices?.[0];
|
|
691
|
+
if (!b) return [];
|
|
692
|
+
const T = [];
|
|
693
|
+
b.delta?.content && T.push(...L.push(b.delta.content));
|
|
694
|
+
for (const [I, v] of (b.delta?.tool_calls ?? []).entries()) {
|
|
695
|
+
const R = v.index ?? I;
|
|
696
|
+
let k = C.get(R);
|
|
697
|
+
if (k || (k = v.id ?? `webllm-call-${R}-${Date.now().toString(36)}`, C.set(R, k), T.push({ type: "tool-call-start", toolCallId: k, toolName: v.function?.name ?? "" })), v.function?.arguments) {
|
|
698
|
+
const W = typeof v.function.arguments == "string" ? v.function.arguments : JSON.stringify(v.function.arguments);
|
|
699
|
+
T.push({ type: "tool-call-delta", toolCallId: k, args: W });
|
|
666
700
|
}
|
|
667
701
|
}
|
|
668
|
-
return
|
|
702
|
+
return b.finish_reason && (N = b.finish_reason === "tool_calls" ? "tool-calls" : b.finish_reason === "length" ? "length" : "stop"), T;
|
|
669
703
|
};
|
|
670
704
|
return { stream: new ReadableStream({
|
|
671
705
|
async pull(h) {
|
|
672
706
|
try {
|
|
673
707
|
for (; ; ) {
|
|
674
|
-
const { done:
|
|
675
|
-
if (
|
|
676
|
-
for (const
|
|
677
|
-
for (const
|
|
678
|
-
|
|
708
|
+
const { done: b, value: T } = await g.next();
|
|
709
|
+
if (b) {
|
|
710
|
+
for (const v of L.flush()) h.enqueue(v);
|
|
711
|
+
for (const v of C.values()) h.enqueue({ type: "tool-call-end", toolCallId: v });
|
|
712
|
+
C.clear(), h.enqueue({ type: "finish", finishReason: N, usage: O }), h.close(), E(), a({ finishReason: N, usage: O });
|
|
679
713
|
return;
|
|
680
714
|
}
|
|
681
|
-
const
|
|
682
|
-
for (const
|
|
683
|
-
if (
|
|
715
|
+
const I = U(T);
|
|
716
|
+
for (const v of I) h.enqueue(v);
|
|
717
|
+
if (I.length) return;
|
|
684
718
|
}
|
|
685
|
-
} catch (
|
|
686
|
-
|
|
719
|
+
} catch (b) {
|
|
720
|
+
E(), w(b), h.error(b);
|
|
687
721
|
}
|
|
688
722
|
},
|
|
689
723
|
async cancel() {
|
|
690
|
-
|
|
724
|
+
E(), await l.interruptGenerate(), await g.return?.(), a({ finishReason: "stop", usage: O });
|
|
691
725
|
}
|
|
692
|
-
}), response:
|
|
726
|
+
}), response: d };
|
|
693
727
|
}
|
|
694
728
|
};
|
|
695
729
|
}
|
|
696
|
-
const
|
|
730
|
+
const Q = [
|
|
697
731
|
"temperature",
|
|
698
732
|
"top_p",
|
|
699
733
|
"max_tokens",
|
|
@@ -704,14 +738,14 @@ const K = [
|
|
|
704
738
|
"logit_bias",
|
|
705
739
|
"extra_body"
|
|
706
740
|
];
|
|
707
|
-
function
|
|
741
|
+
function V(t) {
|
|
708
742
|
const e = {};
|
|
709
743
|
if (!t) return e;
|
|
710
|
-
for (const o of
|
|
744
|
+
for (const o of Q)
|
|
711
745
|
t[o] !== void 0 && (e[o] = t[o]);
|
|
712
746
|
return t.maxTokens !== void 0 && e.max_tokens === void 0 && (e.max_tokens = t.maxTokens), e;
|
|
713
747
|
}
|
|
714
|
-
function
|
|
748
|
+
function Y(t) {
|
|
715
749
|
return t.map((e) => {
|
|
716
750
|
if (e.role === "tool")
|
|
717
751
|
return {
|
|
@@ -723,73 +757,74 @@ function F(t) {
|
|
|
723
757
|
return {
|
|
724
758
|
role: "assistant",
|
|
725
759
|
content: typeof e.content == "string" ? e.content : "",
|
|
726
|
-
tool_calls: e.toolCalls.map((
|
|
727
|
-
id:
|
|
760
|
+
tool_calls: e.toolCalls.map((n) => ({
|
|
761
|
+
id: n.id,
|
|
728
762
|
type: "function",
|
|
729
763
|
function: {
|
|
730
|
-
name:
|
|
731
|
-
arguments: typeof
|
|
764
|
+
name: n.name,
|
|
765
|
+
arguments: typeof n.args == "string" ? n.args : JSON.stringify(n.args)
|
|
732
766
|
}
|
|
733
767
|
}))
|
|
734
768
|
};
|
|
735
769
|
if (typeof e.content == "string")
|
|
736
770
|
return { role: e.role, content: e.content };
|
|
737
|
-
const o = e.content.map(
|
|
738
|
-
return e.role !== "user" || o.every((
|
|
771
|
+
const o = e.content.map(Z);
|
|
772
|
+
return e.role !== "user" || o.every((n) => n.type === "text") ? { role: e.role, content: o.map((n) => n.type === "text" ? n.text : "").join(`
|
|
739
773
|
`) } : { role: e.role, content: o };
|
|
740
774
|
});
|
|
741
775
|
}
|
|
742
|
-
function
|
|
776
|
+
function Z(t) {
|
|
743
777
|
if (t.type === "text") return { type: "text", text: t.text };
|
|
744
778
|
const e = x(t);
|
|
745
779
|
if (e.mediaType.startsWith("image/"))
|
|
746
780
|
return { type: "image_url", image_url: { url: e.url } };
|
|
747
|
-
if (e.base64 &&
|
|
748
|
-
return { type: "text", text:
|
|
781
|
+
if (e.base64 && S(e.mediaType))
|
|
782
|
+
return { type: "text", text: A(e.base64) };
|
|
749
783
|
throw new Error(`[vue-llm] In-browser models do not support ${e.mediaType} attachments`);
|
|
750
784
|
}
|
|
751
|
-
const
|
|
752
|
-
function
|
|
785
|
+
const ee = "<think>", te = "</think>";
|
|
786
|
+
function oe() {
|
|
753
787
|
let t = "", e = !1, o = !1;
|
|
754
|
-
const
|
|
755
|
-
|
|
788
|
+
const n = (c, p) => {
|
|
789
|
+
c && p.push(e ? { type: "reasoning-delta", text: c } : { type: "text-delta", text: c });
|
|
756
790
|
};
|
|
757
791
|
return {
|
|
758
|
-
push(
|
|
759
|
-
const
|
|
760
|
-
for (t +=
|
|
792
|
+
push(c) {
|
|
793
|
+
const p = [];
|
|
794
|
+
for (t += c; t; ) {
|
|
761
795
|
if (o) {
|
|
762
796
|
if (t = t.replace(/^\s+/, ""), !t) break;
|
|
763
797
|
o = !1;
|
|
764
798
|
}
|
|
765
|
-
const
|
|
766
|
-
if (
|
|
767
|
-
|
|
799
|
+
const r = e ? te : ee, m = t.indexOf(r);
|
|
800
|
+
if (m !== -1) {
|
|
801
|
+
n(t.slice(0, m), p), t = t.slice(m + r.length), e = !e, o = !0;
|
|
768
802
|
continue;
|
|
769
803
|
}
|
|
770
|
-
const
|
|
771
|
-
|
|
804
|
+
const _ = ne(t, r);
|
|
805
|
+
n(t.slice(0, t.length - _), p), t = t.slice(t.length - _);
|
|
772
806
|
break;
|
|
773
807
|
}
|
|
774
|
-
return
|
|
808
|
+
return p;
|
|
775
809
|
},
|
|
776
810
|
flush() {
|
|
777
|
-
const
|
|
778
|
-
return
|
|
811
|
+
const c = [];
|
|
812
|
+
return n(t, c), t = "", c;
|
|
779
813
|
}
|
|
780
814
|
};
|
|
781
815
|
}
|
|
782
|
-
function
|
|
816
|
+
function ne(t, e) {
|
|
783
817
|
for (let o = Math.min(e.length - 1, t.length); o > 0; o--)
|
|
784
818
|
if (e.startsWith(t.slice(-o))) return o;
|
|
785
819
|
return 0;
|
|
786
820
|
}
|
|
787
821
|
export {
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
822
|
+
pe as createAnthropic,
|
|
823
|
+
ce as createGoogle,
|
|
824
|
+
ue as createOllama,
|
|
825
|
+
le as createOpenAI,
|
|
826
|
+
ye as createSSEProvider,
|
|
827
|
+
fe as createWebLLM,
|
|
828
|
+
X as detectWebGPUPrecision,
|
|
829
|
+
z as isWebGPUSupported
|
|
795
830
|
};
|