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/providers.js
CHANGED
|
@@ -1,176 +1,111 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { c as
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
super({
|
|
7
|
-
transform(n, a) {
|
|
8
|
-
e += n;
|
|
9
|
-
const i = e.split(`
|
|
10
|
-
`);
|
|
11
|
-
e = i.pop() ?? "";
|
|
12
|
-
let t = {};
|
|
13
|
-
for (const o of i) {
|
|
14
|
-
if (o === "") {
|
|
15
|
-
t.data !== void 0 && a.enqueue({
|
|
16
|
-
event: t.event,
|
|
17
|
-
data: t.data,
|
|
18
|
-
id: t.id,
|
|
19
|
-
retry: t.retry
|
|
20
|
-
}), t = {};
|
|
21
|
-
continue;
|
|
22
|
-
}
|
|
23
|
-
if (o.startsWith(":")) continue;
|
|
24
|
-
const u = o.indexOf(":");
|
|
25
|
-
if (u === -1) continue;
|
|
26
|
-
const f = o.slice(0, u), d = o.slice(u + 1).replace(/^ /, "");
|
|
27
|
-
switch (f) {
|
|
28
|
-
case "event":
|
|
29
|
-
t.event = d;
|
|
30
|
-
break;
|
|
31
|
-
case "data":
|
|
32
|
-
t.data = t.data !== void 0 ? t.data + `
|
|
33
|
-
` + d : d;
|
|
34
|
-
break;
|
|
35
|
-
case "id":
|
|
36
|
-
t.id = d;
|
|
37
|
-
break;
|
|
38
|
-
case "retry":
|
|
39
|
-
t.retry = parseInt(d, 10);
|
|
40
|
-
break;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
flush(n) {
|
|
45
|
-
if (e.trim()) {
|
|
46
|
-
const a = e.split(`
|
|
47
|
-
`);
|
|
48
|
-
let i = {};
|
|
49
|
-
for (const t of a) {
|
|
50
|
-
if (t === "" && i.data !== void 0) {
|
|
51
|
-
n.enqueue({
|
|
52
|
-
event: i.event,
|
|
53
|
-
data: i.data,
|
|
54
|
-
id: i.id,
|
|
55
|
-
retry: i.retry
|
|
56
|
-
}), i = {};
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
const o = t.indexOf(":");
|
|
60
|
-
if (o === -1 || t.startsWith(":")) continue;
|
|
61
|
-
const u = t.slice(0, o), f = t.slice(o + 1).replace(/^ /, "");
|
|
62
|
-
u === "data" && (i.data = i.data !== void 0 ? i.data + `
|
|
63
|
-
` + f : f);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
function J(s) {
|
|
1
|
+
import { S as k } from "./sse-DU5m1n6r.js";
|
|
2
|
+
import { c as L } from "./sse-DU5m1n6r.js";
|
|
3
|
+
import { r as T, i as b, d as v } from "./file-x_kW7BHa.js";
|
|
4
|
+
import { c as _ } from "./errors-B3A_zmVE.js";
|
|
5
|
+
function U(o) {
|
|
71
6
|
return {
|
|
72
7
|
id: "openai",
|
|
73
8
|
supportsStreaming: !0,
|
|
74
9
|
validateConfig() {
|
|
75
|
-
if (!
|
|
10
|
+
if (!o.apiKey)
|
|
76
11
|
throw new Error('[vue-llm] OpenAI API key is required. Pass it via createOpenAI({ apiKey: "..." })');
|
|
77
|
-
if (!
|
|
12
|
+
if (!o.dangerouslyAllowBrowser && typeof window < "u")
|
|
78
13
|
throw new Error(
|
|
79
14
|
"[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."
|
|
80
15
|
);
|
|
81
16
|
},
|
|
82
17
|
async chat(e) {
|
|
83
|
-
const
|
|
18
|
+
const t = o.baseURL ?? "https://api.openai.com/v1/chat/completions", a = {
|
|
84
19
|
model: e.model,
|
|
85
20
|
messages: x(e.messages),
|
|
86
21
|
stream: !0,
|
|
87
22
|
stream_options: { include_usage: !0 }
|
|
88
23
|
};
|
|
89
|
-
e.providerOptions?.response_format && (a.response_format = e.providerOptions.response_format), e.tools?.length && (a.tools = e.tools.map((
|
|
24
|
+
e.providerOptions?.response_format && (a.response_format = e.providerOptions.response_format), e.tools?.length && (a.tools = e.tools.map((m) => ({
|
|
90
25
|
type: "function",
|
|
91
26
|
function: {
|
|
92
|
-
name:
|
|
93
|
-
description:
|
|
94
|
-
parameters:
|
|
27
|
+
name: m.name,
|
|
28
|
+
description: m.description,
|
|
29
|
+
parameters: m.parameters
|
|
95
30
|
}
|
|
96
31
|
})));
|
|
97
|
-
const
|
|
32
|
+
const c = await fetch(t, {
|
|
98
33
|
method: "POST",
|
|
99
34
|
headers: {
|
|
100
|
-
Authorization: `Bearer ${
|
|
35
|
+
Authorization: `Bearer ${o.apiKey}`,
|
|
101
36
|
"Content-Type": "application/json",
|
|
102
37
|
...e.providerOptions?.headers
|
|
103
38
|
},
|
|
104
39
|
body: JSON.stringify(a),
|
|
105
40
|
signal: e.signal
|
|
106
41
|
});
|
|
107
|
-
if (!
|
|
108
|
-
throw await
|
|
109
|
-
if (!
|
|
42
|
+
if (!c.ok)
|
|
43
|
+
throw await _("openai", "OpenAI API", c);
|
|
44
|
+
if (!c.body)
|
|
110
45
|
throw new Error("[vue-llm] No response body from OpenAI");
|
|
111
|
-
let
|
|
112
|
-
const
|
|
113
|
-
|
|
46
|
+
let r;
|
|
47
|
+
const n = new Promise((m) => {
|
|
48
|
+
r = m;
|
|
114
49
|
});
|
|
115
|
-
return { stream:
|
|
116
|
-
transform(
|
|
117
|
-
if (
|
|
118
|
-
let
|
|
50
|
+
return { stream: c.body.pipeThrough(new TextDecoderStream()).pipeThrough(new k()).pipeThrough(new TransformStream({
|
|
51
|
+
transform(m, y) {
|
|
52
|
+
if (m.data === "[DONE]") return;
|
|
53
|
+
let d;
|
|
119
54
|
try {
|
|
120
|
-
|
|
55
|
+
d = JSON.parse(m.data);
|
|
121
56
|
} catch {
|
|
122
57
|
return;
|
|
123
58
|
}
|
|
124
|
-
const
|
|
125
|
-
if (!
|
|
126
|
-
|
|
59
|
+
const p = d.choices?.[0];
|
|
60
|
+
if (!p) {
|
|
61
|
+
d.usage && r({
|
|
127
62
|
finishReason: "stop",
|
|
128
63
|
usage: {
|
|
129
|
-
promptTokens:
|
|
130
|
-
completionTokens:
|
|
131
|
-
totalTokens:
|
|
64
|
+
promptTokens: d.usage.prompt_tokens,
|
|
65
|
+
completionTokens: d.usage.completion_tokens,
|
|
66
|
+
totalTokens: d.usage.total_tokens
|
|
132
67
|
}
|
|
133
68
|
});
|
|
134
69
|
return;
|
|
135
70
|
}
|
|
136
|
-
const
|
|
137
|
-
if (
|
|
138
|
-
for (const
|
|
139
|
-
|
|
71
|
+
const i = p.delta;
|
|
72
|
+
if (i.content && y.enqueue({ type: "text-delta", text: i.content }), i.tool_calls)
|
|
73
|
+
for (const l of i.tool_calls)
|
|
74
|
+
l.id && y.enqueue({
|
|
140
75
|
type: "tool-call-start",
|
|
141
|
-
toolCallId:
|
|
142
|
-
toolName:
|
|
143
|
-
}),
|
|
76
|
+
toolCallId: l.id,
|
|
77
|
+
toolName: l.function?.name ?? ""
|
|
78
|
+
}), l.function?.arguments && y.enqueue({
|
|
144
79
|
type: "tool-call-delta",
|
|
145
|
-
toolCallId:
|
|
146
|
-
args:
|
|
80
|
+
toolCallId: l.id ?? "",
|
|
81
|
+
args: l.function.arguments
|
|
147
82
|
});
|
|
148
|
-
if (
|
|
149
|
-
const
|
|
150
|
-
for (const
|
|
151
|
-
|
|
152
|
-
const
|
|
153
|
-
|
|
83
|
+
if (p.finish_reason) {
|
|
84
|
+
const l = i.tool_calls?.filter((s) => s.id) ?? [];
|
|
85
|
+
for (const s of l)
|
|
86
|
+
s.id && y.enqueue({ type: "tool-call-end", toolCallId: s.id });
|
|
87
|
+
const u = p.finish_reason === "tool_calls" ? "tool-calls" : p.finish_reason === "length" ? "length" : "stop";
|
|
88
|
+
y.enqueue({ type: "finish", finishReason: u, usage: void 0 });
|
|
154
89
|
}
|
|
155
90
|
}
|
|
156
|
-
})), response:
|
|
91
|
+
})), response: n };
|
|
157
92
|
}
|
|
158
93
|
};
|
|
159
94
|
}
|
|
160
|
-
function x(
|
|
161
|
-
return
|
|
95
|
+
function x(o) {
|
|
96
|
+
return o.map((e) => e.role === "tool" ? {
|
|
162
97
|
role: "tool",
|
|
163
98
|
content: typeof e.content == "string" ? e.content : JSON.stringify(e.content),
|
|
164
99
|
tool_call_id: e.toolCallId
|
|
165
100
|
} : e.role === "assistant" && e.toolCalls?.length ? {
|
|
166
101
|
role: "assistant",
|
|
167
102
|
content: typeof e.content == "string" && e.content ? e.content : null,
|
|
168
|
-
tool_calls: e.toolCalls.map((
|
|
169
|
-
id:
|
|
103
|
+
tool_calls: e.toolCalls.map((t) => ({
|
|
104
|
+
id: t.id,
|
|
170
105
|
type: "function",
|
|
171
106
|
function: {
|
|
172
|
-
name:
|
|
173
|
-
arguments: typeof
|
|
107
|
+
name: t.name,
|
|
108
|
+
arguments: typeof t.args == "string" ? t.args : JSON.stringify(t.args)
|
|
174
109
|
}
|
|
175
110
|
}))
|
|
176
111
|
} : {
|
|
@@ -178,9 +113,9 @@ function x(s) {
|
|
|
178
113
|
content: typeof e.content == "string" ? e.content : e.content.map(C)
|
|
179
114
|
});
|
|
180
115
|
}
|
|
181
|
-
function C(
|
|
182
|
-
if (
|
|
183
|
-
const e = T(
|
|
116
|
+
function C(o) {
|
|
117
|
+
if (o.type === "text") return { type: "text", text: o.text };
|
|
118
|
+
const e = T(o);
|
|
184
119
|
if (e.mediaType.startsWith("image/"))
|
|
185
120
|
return { type: "image_url", image_url: { url: e.url } };
|
|
186
121
|
if (!e.base64)
|
|
@@ -190,11 +125,11 @@ function C(s) {
|
|
|
190
125
|
type: "file",
|
|
191
126
|
file: { filename: e.filename ?? "document.pdf", file_data: e.url }
|
|
192
127
|
};
|
|
193
|
-
const
|
|
194
|
-
if (
|
|
195
|
-
return { type: "input_audio", input_audio: { data: e.base64, format:
|
|
196
|
-
if (
|
|
197
|
-
return { type: "text", text:
|
|
128
|
+
const t = O[e.mediaType];
|
|
129
|
+
if (t)
|
|
130
|
+
return { type: "input_audio", input_audio: { data: e.base64, format: t } };
|
|
131
|
+
if (b(e.mediaType))
|
|
132
|
+
return { type: "text", text: v(e.base64) };
|
|
198
133
|
throw new Error(`[vue-llm] OpenAI does not support ${e.mediaType} attachments`);
|
|
199
134
|
}
|
|
200
135
|
const O = {
|
|
@@ -203,106 +138,106 @@ const O = {
|
|
|
203
138
|
"audio/mpeg": "mp3",
|
|
204
139
|
"audio/mp3": "mp3"
|
|
205
140
|
};
|
|
206
|
-
function
|
|
141
|
+
function M(o) {
|
|
207
142
|
return {
|
|
208
143
|
id: "anthropic",
|
|
209
144
|
supportsStreaming: !0,
|
|
210
145
|
validateConfig() {
|
|
211
|
-
if (!
|
|
146
|
+
if (!o.apiKey)
|
|
212
147
|
throw new Error('[vue-llm] Anthropic API key is required. Pass it via createAnthropic({ apiKey: "..." })');
|
|
213
|
-
if (!
|
|
148
|
+
if (!o.dangerouslyAllowBrowser && typeof window < "u")
|
|
214
149
|
throw new Error(
|
|
215
150
|
"[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."
|
|
216
151
|
);
|
|
217
152
|
},
|
|
218
153
|
async chat(e) {
|
|
219
|
-
const
|
|
154
|
+
const t = (o.baseURL ?? "https://api.anthropic.com") + "/v1/messages", a = e.messages.find((p) => p.role === "system"), c = e.messages.filter((p) => p.role !== "system"), r = {
|
|
220
155
|
model: e.model,
|
|
221
|
-
messages: I(
|
|
156
|
+
messages: I(c),
|
|
222
157
|
max_tokens: e.providerOptions?.maxTokens ?? 4096,
|
|
223
158
|
stream: !0
|
|
224
159
|
};
|
|
225
|
-
a && (
|
|
226
|
-
name:
|
|
227
|
-
description:
|
|
228
|
-
input_schema:
|
|
160
|
+
a && (r.system = typeof a.content == "string" ? a.content : a.content.map((p) => ({ type: "text", text: p.type === "text" ? p.text : "" }))), e.tools?.length && (r.tools = e.tools.map((p) => ({
|
|
161
|
+
name: p.name,
|
|
162
|
+
description: p.description,
|
|
163
|
+
input_schema: p.parameters
|
|
229
164
|
})));
|
|
230
|
-
const
|
|
165
|
+
const n = await fetch(t, {
|
|
231
166
|
method: "POST",
|
|
232
167
|
headers: {
|
|
233
|
-
"x-api-key":
|
|
168
|
+
"x-api-key": o.apiKey,
|
|
234
169
|
"anthropic-version": "2023-06-01",
|
|
235
170
|
"Content-Type": "application/json",
|
|
236
171
|
...e.providerOptions?.headers
|
|
237
172
|
},
|
|
238
|
-
body: JSON.stringify(
|
|
173
|
+
body: JSON.stringify(r),
|
|
239
174
|
signal: e.signal
|
|
240
175
|
});
|
|
241
|
-
if (!
|
|
242
|
-
throw await
|
|
243
|
-
if (!
|
|
176
|
+
if (!n.ok)
|
|
177
|
+
throw await _("anthropic", "Anthropic API", n);
|
|
178
|
+
if (!n.body)
|
|
244
179
|
throw new Error("[vue-llm] No response body from Anthropic");
|
|
245
|
-
let
|
|
246
|
-
const
|
|
247
|
-
|
|
180
|
+
let f;
|
|
181
|
+
const m = new Promise((p) => {
|
|
182
|
+
f = p;
|
|
248
183
|
});
|
|
249
|
-
let
|
|
250
|
-
return { stream:
|
|
251
|
-
transform(
|
|
252
|
-
let
|
|
184
|
+
let y = "";
|
|
185
|
+
return { stream: n.body.pipeThrough(new TextDecoderStream()).pipeThrough(new k()).pipeThrough(new TransformStream({
|
|
186
|
+
transform(p, i) {
|
|
187
|
+
let l;
|
|
253
188
|
try {
|
|
254
|
-
|
|
189
|
+
l = JSON.parse(p.data);
|
|
255
190
|
} catch {
|
|
256
191
|
return;
|
|
257
192
|
}
|
|
258
|
-
switch (
|
|
193
|
+
switch (p.event ?? l.type) {
|
|
259
194
|
case "content_block_start": {
|
|
260
|
-
const
|
|
261
|
-
|
|
195
|
+
const s = l.content_block;
|
|
196
|
+
s?.type === "tool_use" && (y = s.id, i.enqueue({
|
|
262
197
|
type: "tool-call-start",
|
|
263
|
-
toolCallId:
|
|
264
|
-
toolName:
|
|
198
|
+
toolCallId: y,
|
|
199
|
+
toolName: s.name
|
|
265
200
|
}));
|
|
266
201
|
break;
|
|
267
202
|
}
|
|
268
203
|
case "content_block_delta": {
|
|
269
|
-
const
|
|
270
|
-
|
|
204
|
+
const s = l.delta;
|
|
205
|
+
s?.type === "text_delta" && s.text && i.enqueue({ type: "text-delta", text: s.text }), s?.type === "thinking_delta" && s.text && i.enqueue({ type: "reasoning-delta", text: s.text }), s?.type === "input_json_delta" && s.partial_json && i.enqueue({
|
|
271
206
|
type: "tool-call-delta",
|
|
272
|
-
toolCallId:
|
|
273
|
-
args:
|
|
207
|
+
toolCallId: y,
|
|
208
|
+
args: s.partial_json
|
|
274
209
|
});
|
|
275
210
|
break;
|
|
276
211
|
}
|
|
277
212
|
case "content_block_stop": {
|
|
278
|
-
|
|
213
|
+
y && (i.enqueue({ type: "tool-call-end", toolCallId: y }), y = "");
|
|
279
214
|
break;
|
|
280
215
|
}
|
|
281
216
|
case "message_delta": {
|
|
282
|
-
const
|
|
283
|
-
|
|
217
|
+
const s = l.delta, w = l.usage, g = s?.stop_reason === "tool_use" ? "tool-calls" : s?.stop_reason === "max_tokens" ? "length" : "stop";
|
|
218
|
+
i.enqueue({
|
|
284
219
|
type: "finish",
|
|
285
220
|
finishReason: g,
|
|
286
221
|
usage: w ? { promptTokens: 0, completionTokens: w.output_tokens ?? 0, totalTokens: w.output_tokens ?? 0 } : void 0
|
|
287
|
-
}),
|
|
222
|
+
}), f({ finishReason: g, usage: void 0 });
|
|
288
223
|
break;
|
|
289
224
|
}
|
|
290
225
|
case "error": {
|
|
291
|
-
const
|
|
292
|
-
|
|
226
|
+
const s = l.error;
|
|
227
|
+
i.enqueue({
|
|
293
228
|
type: "error",
|
|
294
|
-
error: new Error(`[vue-llm] Anthropic stream error: ${
|
|
229
|
+
error: new Error(`[vue-llm] Anthropic stream error: ${s?.message ?? "unknown"}`)
|
|
295
230
|
});
|
|
296
231
|
break;
|
|
297
232
|
}
|
|
298
233
|
}
|
|
299
234
|
}
|
|
300
|
-
})), response:
|
|
235
|
+
})), response: m };
|
|
301
236
|
}
|
|
302
237
|
};
|
|
303
238
|
}
|
|
304
|
-
function I(
|
|
305
|
-
return
|
|
239
|
+
function I(o) {
|
|
240
|
+
return o.map((e) => {
|
|
306
241
|
if (e.role === "tool")
|
|
307
242
|
return {
|
|
308
243
|
role: "user",
|
|
@@ -313,16 +248,16 @@ function I(s) {
|
|
|
313
248
|
}]
|
|
314
249
|
};
|
|
315
250
|
if (e.role === "assistant" && e.toolCalls?.length) {
|
|
316
|
-
const
|
|
317
|
-
typeof e.content == "string" && e.content &&
|
|
251
|
+
const t = [];
|
|
252
|
+
typeof e.content == "string" && e.content && t.push({ type: "text", text: e.content });
|
|
318
253
|
for (const a of e.toolCalls)
|
|
319
|
-
|
|
254
|
+
t.push({
|
|
320
255
|
type: "tool_use",
|
|
321
256
|
id: a.id,
|
|
322
257
|
name: a.name,
|
|
323
258
|
input: a.args
|
|
324
259
|
});
|
|
325
|
-
return { role: "assistant", content:
|
|
260
|
+
return { role: "assistant", content: t };
|
|
326
261
|
}
|
|
327
262
|
return {
|
|
328
263
|
role: e.role,
|
|
@@ -330,109 +265,109 @@ function I(s) {
|
|
|
330
265
|
};
|
|
331
266
|
});
|
|
332
267
|
}
|
|
333
|
-
function A(
|
|
334
|
-
if (
|
|
335
|
-
const e = T(
|
|
268
|
+
function A(o) {
|
|
269
|
+
if (o.type === "text") return { type: "text", text: o.text };
|
|
270
|
+
const e = T(o), t = e.base64 ? { type: "base64", media_type: e.mediaType, data: e.base64 } : { type: "url", url: e.url };
|
|
336
271
|
if (e.mediaType.startsWith("image/"))
|
|
337
|
-
return { type: "image", source:
|
|
272
|
+
return { type: "image", source: t };
|
|
338
273
|
if (e.mediaType === "application/pdf")
|
|
339
|
-
return { type: "document", source:
|
|
340
|
-
if (
|
|
274
|
+
return { type: "document", source: t, ...e.filename ? { title: e.filename } : {} };
|
|
275
|
+
if (b(e.mediaType) && e.base64)
|
|
341
276
|
return {
|
|
342
277
|
type: "document",
|
|
343
|
-
source: { type: "text", media_type: "text/plain", data:
|
|
278
|
+
source: { type: "text", media_type: "text/plain", data: v(e.base64) },
|
|
344
279
|
...e.filename ? { title: e.filename } : {}
|
|
345
280
|
};
|
|
346
281
|
throw new Error(`[vue-llm] Anthropic does not support ${e.mediaType} attachments`);
|
|
347
282
|
}
|
|
348
|
-
function
|
|
283
|
+
function $(o) {
|
|
349
284
|
return {
|
|
350
285
|
id: "google",
|
|
351
286
|
supportsStreaming: !0,
|
|
352
287
|
validateConfig() {
|
|
353
|
-
if (!
|
|
288
|
+
if (!o.apiKey)
|
|
354
289
|
throw new Error('[vue-llm] Google AI API key is required. Pass it via createGoogle({ apiKey: "..." })');
|
|
355
|
-
if (!
|
|
290
|
+
if (!o.dangerouslyAllowBrowser && typeof window < "u")
|
|
356
291
|
throw new Error(
|
|
357
292
|
"[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."
|
|
358
293
|
);
|
|
359
294
|
},
|
|
360
295
|
async chat(e) {
|
|
361
|
-
const a = `${
|
|
296
|
+
const a = `${o.baseURL ?? "https://generativelanguage.googleapis.com/v1beta"}/models/${e.model}:streamGenerateContent?alt=sse&key=${o.apiKey}`, c = {
|
|
362
297
|
contents: S(e.messages)
|
|
363
|
-
},
|
|
364
|
-
|
|
365
|
-
parts: [{ text: typeof
|
|
366
|
-
}), e.tools?.length && (
|
|
367
|
-
functionDeclarations: e.tools.map((
|
|
368
|
-
name:
|
|
369
|
-
description:
|
|
370
|
-
parameters:
|
|
298
|
+
}, r = e.messages.find((i) => i.role === "system");
|
|
299
|
+
r && (c.systemInstruction = {
|
|
300
|
+
parts: [{ text: typeof r.content == "string" ? r.content : "" }]
|
|
301
|
+
}), e.tools?.length && (c.tools = [{
|
|
302
|
+
functionDeclarations: e.tools.map((i) => ({
|
|
303
|
+
name: i.name,
|
|
304
|
+
description: i.description,
|
|
305
|
+
parameters: i.parameters
|
|
371
306
|
}))
|
|
372
|
-
}]),
|
|
307
|
+
}]), c.generationConfig = {
|
|
373
308
|
...e.providerOptions?.response_format?.type === "json_object" ? { responseMimeType: "application/json" } : {}
|
|
374
309
|
};
|
|
375
|
-
const
|
|
310
|
+
const n = await fetch(a, {
|
|
376
311
|
method: "POST",
|
|
377
312
|
headers: {
|
|
378
313
|
"Content-Type": "application/json",
|
|
379
314
|
...e.providerOptions?.headers
|
|
380
315
|
},
|
|
381
|
-
body: JSON.stringify(
|
|
316
|
+
body: JSON.stringify(c),
|
|
382
317
|
signal: e.signal
|
|
383
318
|
});
|
|
384
|
-
if (!
|
|
385
|
-
throw await
|
|
386
|
-
if (!
|
|
319
|
+
if (!n.ok)
|
|
320
|
+
throw await _("google", "Google AI API", n);
|
|
321
|
+
if (!n.body)
|
|
387
322
|
throw new Error("[vue-llm] No response body from Google AI");
|
|
388
|
-
let
|
|
389
|
-
const
|
|
390
|
-
|
|
323
|
+
let f;
|
|
324
|
+
const m = new Promise((i) => {
|
|
325
|
+
f = i;
|
|
391
326
|
});
|
|
392
|
-
let
|
|
393
|
-
return { stream:
|
|
327
|
+
let y = 0, d = !1;
|
|
328
|
+
return { stream: n.body.pipeThrough(new TextDecoderStream()).pipeThrough(new TransformStream({
|
|
394
329
|
buffer: "",
|
|
395
|
-
transform(
|
|
396
|
-
this.buffer +=
|
|
397
|
-
const
|
|
330
|
+
transform(i, l) {
|
|
331
|
+
this.buffer += i;
|
|
332
|
+
const u = this.buffer.split(`
|
|
398
333
|
`);
|
|
399
|
-
this.buffer =
|
|
400
|
-
for (const
|
|
401
|
-
|
|
334
|
+
this.buffer = u.pop();
|
|
335
|
+
for (const s of u)
|
|
336
|
+
s.startsWith("data: ") && l.enqueue(s.slice(6));
|
|
402
337
|
},
|
|
403
|
-
flush(
|
|
404
|
-
const
|
|
405
|
-
|
|
338
|
+
flush(i) {
|
|
339
|
+
const l = this.buffer.trim();
|
|
340
|
+
l.startsWith("data: ") && i.enqueue(l.slice(6));
|
|
406
341
|
}
|
|
407
342
|
})).pipeThrough(new TransformStream({
|
|
408
|
-
transform(
|
|
409
|
-
let
|
|
343
|
+
transform(i, l) {
|
|
344
|
+
let u;
|
|
410
345
|
try {
|
|
411
|
-
|
|
346
|
+
u = JSON.parse(i);
|
|
412
347
|
} catch {
|
|
413
348
|
return;
|
|
414
349
|
}
|
|
415
|
-
const
|
|
416
|
-
if (!
|
|
417
|
-
const w =
|
|
350
|
+
const s = u.candidates?.[0];
|
|
351
|
+
if (!s) return;
|
|
352
|
+
const w = s.content?.parts;
|
|
418
353
|
if (w) {
|
|
419
354
|
for (const g of w)
|
|
420
|
-
if (g.text !== void 0 &&
|
|
421
|
-
const h = `tc_gemini_${++
|
|
422
|
-
|
|
355
|
+
if (g.text !== void 0 && l.enqueue({ type: "text-delta", text: g.text }), g.functionCall) {
|
|
356
|
+
const h = `tc_gemini_${++y}`;
|
|
357
|
+
d = !0, l.enqueue({
|
|
423
358
|
type: "tool-call-start",
|
|
424
359
|
toolCallId: h,
|
|
425
360
|
toolName: g.functionCall.name
|
|
426
|
-
}),
|
|
361
|
+
}), l.enqueue({
|
|
427
362
|
type: "tool-call-delta",
|
|
428
363
|
toolCallId: h,
|
|
429
364
|
args: JSON.stringify(g.functionCall.args ?? {})
|
|
430
|
-
}),
|
|
365
|
+
}), l.enqueue({ type: "tool-call-end", toolCallId: h });
|
|
431
366
|
}
|
|
432
367
|
}
|
|
433
|
-
if (
|
|
434
|
-
const g =
|
|
435
|
-
|
|
368
|
+
if (s.finishReason) {
|
|
369
|
+
const g = d ? "tool-calls" : s.finishReason === "MAX_TOKENS" ? "length" : "stop", h = u.usageMetadata;
|
|
370
|
+
l.enqueue({
|
|
436
371
|
type: "finish",
|
|
437
372
|
finishReason: g,
|
|
438
373
|
usage: h ? {
|
|
@@ -440,7 +375,7 @@ function M(s) {
|
|
|
440
375
|
completionTokens: h.candidatesTokenCount ?? 0,
|
|
441
376
|
totalTokens: h.totalTokenCount ?? 0
|
|
442
377
|
} : void 0
|
|
443
|
-
}),
|
|
378
|
+
}), f({
|
|
444
379
|
finishReason: g,
|
|
445
380
|
usage: h ? {
|
|
446
381
|
promptTokens: h.promptTokenCount ?? 0,
|
|
@@ -450,77 +385,77 @@ function M(s) {
|
|
|
450
385
|
});
|
|
451
386
|
}
|
|
452
387
|
}
|
|
453
|
-
})), response:
|
|
388
|
+
})), response: m };
|
|
454
389
|
}
|
|
455
390
|
};
|
|
456
391
|
}
|
|
457
|
-
function S(
|
|
458
|
-
const e = [],
|
|
459
|
-
for (const a of
|
|
392
|
+
function S(o) {
|
|
393
|
+
const e = [], t = /* @__PURE__ */ new Map();
|
|
394
|
+
for (const a of o) {
|
|
460
395
|
if (a.role === "system") continue;
|
|
461
396
|
if (a.role === "tool") {
|
|
462
|
-
const
|
|
397
|
+
const n = {
|
|
463
398
|
functionResponse: {
|
|
464
|
-
name:
|
|
399
|
+
name: t.get(a.toolCallId ?? "") ?? a.toolCallId ?? "",
|
|
465
400
|
response: { result: P(a.content) }
|
|
466
401
|
}
|
|
467
|
-
},
|
|
468
|
-
|
|
402
|
+
}, f = e.at(-1);
|
|
403
|
+
f?.role === "user" && f.parts.every((m) => m.functionResponse) ? f.parts.push(n) : e.push({ role: "user", parts: [n] });
|
|
469
404
|
continue;
|
|
470
405
|
}
|
|
471
|
-
for (const
|
|
472
|
-
|
|
473
|
-
const
|
|
406
|
+
for (const n of a.toolCalls ?? [])
|
|
407
|
+
t.set(n.id, n.name);
|
|
408
|
+
const c = a.role === "assistant" ? "model" : "user", r = [];
|
|
474
409
|
if (typeof a.content == "string")
|
|
475
|
-
a.content &&
|
|
410
|
+
a.content && r.push({ text: a.content });
|
|
476
411
|
else if (Array.isArray(a.content)) {
|
|
477
|
-
for (const
|
|
478
|
-
if (
|
|
479
|
-
const
|
|
480
|
-
|
|
412
|
+
for (const n of a.content)
|
|
413
|
+
if (n.type === "text" && r.push({ text: n.text }), n.type === "image" || n.type === "file") {
|
|
414
|
+
const f = T(n);
|
|
415
|
+
r.push(f.base64 ? { inlineData: { mimeType: f.mediaType, data: f.base64 } } : { fileData: { mimeType: f.mediaType, fileUri: f.url } });
|
|
481
416
|
}
|
|
482
417
|
}
|
|
483
418
|
if (a.toolCalls?.length)
|
|
484
|
-
for (const
|
|
485
|
-
|
|
419
|
+
for (const n of a.toolCalls)
|
|
420
|
+
r.push({
|
|
486
421
|
functionCall: {
|
|
487
|
-
name:
|
|
488
|
-
args: typeof
|
|
422
|
+
name: n.name,
|
|
423
|
+
args: typeof n.args == "string" ? JSON.parse(n.args) : n.args
|
|
489
424
|
}
|
|
490
425
|
});
|
|
491
|
-
|
|
426
|
+
r.length > 0 && e.push({ role: c, parts: r });
|
|
492
427
|
}
|
|
493
428
|
return e;
|
|
494
429
|
}
|
|
495
|
-
function P(
|
|
496
|
-
if (typeof
|
|
430
|
+
function P(o) {
|
|
431
|
+
if (typeof o != "string") return o;
|
|
497
432
|
try {
|
|
498
|
-
return JSON.parse(
|
|
433
|
+
return JSON.parse(o);
|
|
499
434
|
} catch {
|
|
500
|
-
return
|
|
435
|
+
return o;
|
|
501
436
|
}
|
|
502
437
|
}
|
|
503
|
-
function
|
|
438
|
+
function j(o) {
|
|
504
439
|
return {
|
|
505
440
|
id: "ollama",
|
|
506
441
|
supportsStreaming: !0,
|
|
507
442
|
validateConfig() {
|
|
508
443
|
},
|
|
509
444
|
async chat(e) {
|
|
510
|
-
const
|
|
445
|
+
const t = (o?.baseURL ?? "http://localhost:11434") + "/api/chat", a = {
|
|
511
446
|
model: e.model,
|
|
512
447
|
messages: N(e.messages),
|
|
513
448
|
stream: !0
|
|
514
449
|
};
|
|
515
|
-
e.tools?.length && (a.tools = e.tools.map((
|
|
450
|
+
e.tools?.length && (a.tools = e.tools.map((d) => ({
|
|
516
451
|
type: "function",
|
|
517
452
|
function: {
|
|
518
|
-
name:
|
|
519
|
-
description:
|
|
520
|
-
parameters:
|
|
453
|
+
name: d.name,
|
|
454
|
+
description: d.description,
|
|
455
|
+
parameters: d.parameters
|
|
521
456
|
}
|
|
522
457
|
})));
|
|
523
|
-
const
|
|
458
|
+
const c = await fetch(t, {
|
|
524
459
|
method: "POST",
|
|
525
460
|
headers: {
|
|
526
461
|
"Content-Type": "application/json",
|
|
@@ -529,112 +464,113 @@ function $(s) {
|
|
|
529
464
|
body: JSON.stringify(a),
|
|
530
465
|
signal: e.signal
|
|
531
466
|
});
|
|
532
|
-
if (!
|
|
533
|
-
throw await
|
|
534
|
-
if (!
|
|
467
|
+
if (!c.ok)
|
|
468
|
+
throw await _("ollama", "Ollama API", c);
|
|
469
|
+
if (!c.body)
|
|
535
470
|
throw new Error("[vue-llm] No response body from Ollama");
|
|
536
|
-
let
|
|
537
|
-
const
|
|
538
|
-
|
|
471
|
+
let r;
|
|
472
|
+
const n = new Promise((d) => {
|
|
473
|
+
r = d;
|
|
539
474
|
});
|
|
540
|
-
let
|
|
541
|
-
return { stream:
|
|
542
|
-
transform(
|
|
543
|
-
|
|
544
|
-
const
|
|
475
|
+
let f = "", m = !1;
|
|
476
|
+
return { stream: c.body.pipeThrough(new TextDecoderStream()).pipeThrough(new TransformStream({
|
|
477
|
+
transform(d, p) {
|
|
478
|
+
f += d;
|
|
479
|
+
const i = f.split(`
|
|
545
480
|
`);
|
|
546
|
-
|
|
547
|
-
for (const
|
|
548
|
-
if (!
|
|
549
|
-
let
|
|
481
|
+
f = i.pop() ?? "";
|
|
482
|
+
for (const l of i) {
|
|
483
|
+
if (!l.trim()) continue;
|
|
484
|
+
let u;
|
|
550
485
|
try {
|
|
551
|
-
|
|
486
|
+
u = JSON.parse(l);
|
|
552
487
|
} catch {
|
|
553
488
|
continue;
|
|
554
489
|
}
|
|
555
|
-
if (
|
|
556
|
-
for (const
|
|
557
|
-
|
|
490
|
+
if (u.message?.content && p.enqueue({ type: "text-delta", text: u.message.content }), u.message?.tool_calls)
|
|
491
|
+
for (const s of u.message.tool_calls) {
|
|
492
|
+
m = !0;
|
|
558
493
|
const w = `ollama-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
559
|
-
|
|
494
|
+
p.enqueue({
|
|
560
495
|
type: "tool-call-start",
|
|
561
496
|
toolCallId: w,
|
|
562
|
-
toolName:
|
|
563
|
-
}),
|
|
497
|
+
toolName: s.function.name
|
|
498
|
+
}), p.enqueue({
|
|
564
499
|
type: "tool-call-delta",
|
|
565
500
|
toolCallId: w,
|
|
566
|
-
args: JSON.stringify(
|
|
567
|
-
}),
|
|
501
|
+
args: JSON.stringify(s.function.arguments)
|
|
502
|
+
}), p.enqueue({ type: "tool-call-end", toolCallId: w });
|
|
568
503
|
}
|
|
569
|
-
if (
|
|
570
|
-
const
|
|
571
|
-
|
|
572
|
-
finishReason:
|
|
573
|
-
usage:
|
|
574
|
-
promptTokens:
|
|
575
|
-
completionTokens:
|
|
576
|
-
totalTokens: (
|
|
504
|
+
if (u.done) {
|
|
505
|
+
const s = m ? "tool-calls" : "stop";
|
|
506
|
+
p.enqueue({ type: "finish", finishReason: s }), r({
|
|
507
|
+
finishReason: s,
|
|
508
|
+
usage: u.eval_count ? {
|
|
509
|
+
promptTokens: u.prompt_eval_count ?? 0,
|
|
510
|
+
completionTokens: u.eval_count,
|
|
511
|
+
totalTokens: (u.prompt_eval_count ?? 0) + u.eval_count
|
|
577
512
|
} : void 0
|
|
578
513
|
});
|
|
579
514
|
}
|
|
580
515
|
}
|
|
581
516
|
}
|
|
582
|
-
})), response:
|
|
517
|
+
})), response: n };
|
|
583
518
|
}
|
|
584
519
|
};
|
|
585
520
|
}
|
|
586
|
-
function N(
|
|
521
|
+
function N(o) {
|
|
587
522
|
const e = /* @__PURE__ */ new Map();
|
|
588
|
-
return
|
|
589
|
-
if (
|
|
523
|
+
return o.map((t) => {
|
|
524
|
+
if (t.role === "tool")
|
|
590
525
|
return {
|
|
591
526
|
role: "tool",
|
|
592
|
-
content: typeof
|
|
593
|
-
tool_name: e.get(
|
|
527
|
+
content: typeof t.content == "string" ? t.content : JSON.stringify(t.content),
|
|
528
|
+
tool_name: e.get(t.toolCallId ?? "")
|
|
594
529
|
};
|
|
595
|
-
if (
|
|
596
|
-
for (const
|
|
530
|
+
if (t.role === "assistant" && t.toolCalls?.length) {
|
|
531
|
+
for (const r of t.toolCalls) e.set(r.id, r.name);
|
|
597
532
|
return {
|
|
598
533
|
role: "assistant",
|
|
599
|
-
content: typeof
|
|
600
|
-
tool_calls:
|
|
534
|
+
content: typeof t.content == "string" ? t.content : "",
|
|
535
|
+
tool_calls: t.toolCalls.map((r) => ({
|
|
601
536
|
function: {
|
|
602
|
-
name:
|
|
603
|
-
arguments: typeof
|
|
537
|
+
name: r.name,
|
|
538
|
+
arguments: typeof r.args == "string" ? JSON.parse(r.args) : r.args
|
|
604
539
|
}
|
|
605
540
|
}))
|
|
606
541
|
};
|
|
607
542
|
}
|
|
608
|
-
if (typeof
|
|
609
|
-
return { role:
|
|
610
|
-
const a = [],
|
|
611
|
-
for (const
|
|
612
|
-
if (
|
|
613
|
-
a.push(
|
|
543
|
+
if (typeof t.content == "string")
|
|
544
|
+
return { role: t.role, content: t.content };
|
|
545
|
+
const a = [], c = [];
|
|
546
|
+
for (const r of t.content) {
|
|
547
|
+
if (r.type === "text") {
|
|
548
|
+
a.push(r.text);
|
|
614
549
|
continue;
|
|
615
550
|
}
|
|
616
|
-
const
|
|
617
|
-
if (!
|
|
551
|
+
const n = T(r);
|
|
552
|
+
if (!n.base64)
|
|
618
553
|
throw new Error("[vue-llm] Ollama requires attachments as data URLs, got a remote URL");
|
|
619
|
-
if (
|
|
620
|
-
|
|
621
|
-
else if (
|
|
622
|
-
a.push(
|
|
554
|
+
if (n.mediaType.startsWith("image/"))
|
|
555
|
+
c.push(n.base64);
|
|
556
|
+
else if (b(n.mediaType))
|
|
557
|
+
a.push(v(n.base64));
|
|
623
558
|
else
|
|
624
|
-
throw new Error(`[vue-llm] Ollama does not support ${
|
|
559
|
+
throw new Error(`[vue-llm] Ollama does not support ${n.mediaType} attachments`);
|
|
625
560
|
}
|
|
626
561
|
return {
|
|
627
|
-
role:
|
|
562
|
+
role: t.role,
|
|
628
563
|
content: a.join(`
|
|
629
564
|
|
|
630
565
|
`),
|
|
631
|
-
...
|
|
566
|
+
...c.length > 0 ? { images: c } : {}
|
|
632
567
|
};
|
|
633
568
|
});
|
|
634
569
|
}
|
|
635
570
|
export {
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
571
|
+
M as createAnthropic,
|
|
572
|
+
$ as createGoogle,
|
|
573
|
+
j as createOllama,
|
|
574
|
+
U as createOpenAI,
|
|
575
|
+
L as createSSEProvider
|
|
640
576
|
};
|