vue-ai-hooks 0.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/LICENSE +21 -0
- package/README.md +205 -0
- package/README.zh-CN.md +178 -0
- package/dist/composables/_tc_merge.d.ts +13 -0
- package/dist/composables/_tc_merge.d.ts.map +1 -0
- package/dist/composables/useChat.d.ts +39 -0
- package/dist/composables/useChat.d.ts.map +1 -0
- package/dist/composables/useCompletion.d.ts +31 -0
- package/dist/composables/useCompletion.d.ts.map +1 -0
- package/dist/composables/useEmbedding.d.ts +29 -0
- package/dist/composables/useEmbedding.d.ts.map +1 -0
- package/dist/composables/usePersist.d.ts +48 -0
- package/dist/composables/usePersist.d.ts.map +1 -0
- package/dist/index.cjs +8 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +679 -0
- package/dist/providers/anthropic.d.ts +40 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/openai.d.ts +39 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/types.d.ts +20 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/types/index.d.ts +140 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/utils/fetch.d.ts +10 -0
- package/dist/utils/fetch.d.ts.map +1 -0
- package/dist/utils/id.d.ts +2 -0
- package/dist/utils/id.d.ts.map +1 -0
- package/dist/utils/stream.d.ts +9 -0
- package/dist/utils/stream.d.ts.map +1 -0
- package/package.json +88 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,679 @@
|
|
|
1
|
+
var Y = Object.defineProperty;
|
|
2
|
+
var Z = (u, m, s) => m in u ? Y(u, m, { enumerable: !0, configurable: !0, writable: !0, value: s }) : u[m] = s;
|
|
3
|
+
var V = (u, m, s) => Z(u, typeof m != "symbol" ? m + "" : m, s);
|
|
4
|
+
import { watch as q, ref as M, shallowRef as B } from "vue";
|
|
5
|
+
function F(u, m) {
|
|
6
|
+
var g, i, b, l;
|
|
7
|
+
const s = u ? [...u] : [];
|
|
8
|
+
for (const a of m ?? []) {
|
|
9
|
+
const f = a.index;
|
|
10
|
+
f !== void 0 && (s[f] ? (a.id && (s[f].id = a.id), (b = a.function) != null && b.name && (s[f].function.name += a.function.name), (l = a.function) != null && l.arguments && (s[f].function.arguments += a.function.arguments)) : s[f] = {
|
|
11
|
+
id: a.id ?? "",
|
|
12
|
+
type: "function",
|
|
13
|
+
function: { name: ((g = a.function) == null ? void 0 : g.name) ?? "", arguments: ((i = a.function) == null ? void 0 : i.arguments) ?? "" }
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return s;
|
|
17
|
+
}
|
|
18
|
+
function ee(u, m) {
|
|
19
|
+
const { key: s, version: g, serialize: i, deserialize: b, storage: l, onError: a } = m, f = g !== void 0 ? `${s}:v${g}` : s, p = l !== void 0 ? l : typeof window < "u" ? window.localStorage : null;
|
|
20
|
+
if (p)
|
|
21
|
+
try {
|
|
22
|
+
const C = p.getItem(f);
|
|
23
|
+
if (C !== null) {
|
|
24
|
+
const x = JSON.parse(C), t = b ? b(x) : x;
|
|
25
|
+
t != null && (u.value = t);
|
|
26
|
+
}
|
|
27
|
+
} catch {
|
|
28
|
+
}
|
|
29
|
+
p && q(
|
|
30
|
+
u,
|
|
31
|
+
(C) => {
|
|
32
|
+
try {
|
|
33
|
+
const x = i ? i(C) : C;
|
|
34
|
+
p.setItem(f, JSON.stringify(x));
|
|
35
|
+
} catch (x) {
|
|
36
|
+
a == null || a(x instanceof Error ? x : new Error(String(x)));
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
{ deep: !0 }
|
|
40
|
+
);
|
|
41
|
+
function n() {
|
|
42
|
+
if (p)
|
|
43
|
+
try {
|
|
44
|
+
p.removeItem(f);
|
|
45
|
+
} catch {
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return { clear: n };
|
|
49
|
+
}
|
|
50
|
+
let Q = 0;
|
|
51
|
+
function W(u = "msg") {
|
|
52
|
+
return Q += 1, `${u}_${Date.now().toString(36)}_${Q.toString(36)}`;
|
|
53
|
+
}
|
|
54
|
+
class N extends Error {
|
|
55
|
+
constructor(s, g = {}) {
|
|
56
|
+
super(s);
|
|
57
|
+
V(this, "cause");
|
|
58
|
+
V(this, "status");
|
|
59
|
+
this.name = "AiHooksError", this.cause = g.cause, this.status = g.status;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const te = [];
|
|
63
|
+
function se(u) {
|
|
64
|
+
const {
|
|
65
|
+
provider: m,
|
|
66
|
+
initialMessages: s = te,
|
|
67
|
+
defaultRequest: g = {},
|
|
68
|
+
onUpdate: i,
|
|
69
|
+
onFinish: b,
|
|
70
|
+
onError: l,
|
|
71
|
+
persist: a,
|
|
72
|
+
tools: f,
|
|
73
|
+
toolChoice: p,
|
|
74
|
+
toolHandlers: n,
|
|
75
|
+
maxToolRoundtrips: C = 1
|
|
76
|
+
} = u;
|
|
77
|
+
if (!m) throw new Error("useChat requires a provider option");
|
|
78
|
+
const x = m, t = M([...s]), d = M(""), v = M(!1), T = M(null), _ = B(null), e = a ? ee(t, {
|
|
79
|
+
key: a.key,
|
|
80
|
+
version: a.version,
|
|
81
|
+
onError: (o) => {
|
|
82
|
+
T.value = o;
|
|
83
|
+
}
|
|
84
|
+
}) : null;
|
|
85
|
+
function c(o) {
|
|
86
|
+
t.value = [...o];
|
|
87
|
+
}
|
|
88
|
+
function h() {
|
|
89
|
+
_.value && _.value.abort(), _.value = null, t.value = [], T.value = null, v.value = !1, d.value = "", e == null || e.clear();
|
|
90
|
+
}
|
|
91
|
+
function r() {
|
|
92
|
+
_.value && _.value.abort(), _.value = null, v.value = !1;
|
|
93
|
+
}
|
|
94
|
+
function A() {
|
|
95
|
+
return {
|
|
96
|
+
id: W("assistant"),
|
|
97
|
+
role: "assistant",
|
|
98
|
+
content: "",
|
|
99
|
+
createdAt: /* @__PURE__ */ new Date()
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function $(o) {
|
|
103
|
+
return o instanceof Error ? o : new Error(String(o));
|
|
104
|
+
}
|
|
105
|
+
function y(o) {
|
|
106
|
+
const w = $(o);
|
|
107
|
+
return T.value = w, l == null || l(w), w;
|
|
108
|
+
}
|
|
109
|
+
function D(o) {
|
|
110
|
+
const w = o.function.arguments.trim();
|
|
111
|
+
if (!w) return {};
|
|
112
|
+
try {
|
|
113
|
+
return JSON.parse(w);
|
|
114
|
+
} catch (k) {
|
|
115
|
+
throw new N(`Invalid JSON arguments for tool "${o.function.name}"`, {
|
|
116
|
+
cause: k
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function L(o) {
|
|
121
|
+
return typeof o == "string" ? o : o === void 0 ? "" : JSON.stringify(o);
|
|
122
|
+
}
|
|
123
|
+
async function S(o) {
|
|
124
|
+
const w = [];
|
|
125
|
+
for (const k of o) {
|
|
126
|
+
const R = n == null ? void 0 : n[k.function.name];
|
|
127
|
+
if (!R)
|
|
128
|
+
throw new N(`No tool handler registered for "${k.function.name}"`);
|
|
129
|
+
const P = await R(D(k), {
|
|
130
|
+
toolCall: k,
|
|
131
|
+
messages: [...t.value]
|
|
132
|
+
});
|
|
133
|
+
w.push({
|
|
134
|
+
id: W("tool"),
|
|
135
|
+
role: "tool",
|
|
136
|
+
content: L(P),
|
|
137
|
+
toolCallId: k.id,
|
|
138
|
+
createdAt: /* @__PURE__ */ new Date()
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return w;
|
|
142
|
+
}
|
|
143
|
+
async function E(o, w) {
|
|
144
|
+
var R;
|
|
145
|
+
const k = new AbortController();
|
|
146
|
+
_.value = k, v.value = !0, T.value = null;
|
|
147
|
+
try {
|
|
148
|
+
const P = {
|
|
149
|
+
...g,
|
|
150
|
+
...f && !w.tools ? { tools: f } : {},
|
|
151
|
+
...p && !w.toolChoice ? { toolChoice: p } : {},
|
|
152
|
+
...w,
|
|
153
|
+
signal: k.signal
|
|
154
|
+
}, I = await x.chat(P);
|
|
155
|
+
for await (const J of I) {
|
|
156
|
+
if (J.content) {
|
|
157
|
+
o.content += J.content;
|
|
158
|
+
const O = t.value.findIndex((U) => U.id === o.id);
|
|
159
|
+
O >= 0 && (t.value = [
|
|
160
|
+
...t.value.slice(0, O),
|
|
161
|
+
{ ...o },
|
|
162
|
+
...t.value.slice(O + 1)
|
|
163
|
+
]), i == null || i({ ...o });
|
|
164
|
+
}
|
|
165
|
+
if ((R = J.toolCalls) != null && R.length) {
|
|
166
|
+
o.toolCalls = F(o.toolCalls, J.toolCalls);
|
|
167
|
+
const O = t.value.findIndex((U) => U.id === o.id);
|
|
168
|
+
O >= 0 && (t.value = [
|
|
169
|
+
...t.value.slice(0, O),
|
|
170
|
+
{ ...o },
|
|
171
|
+
...t.value.slice(O + 1)
|
|
172
|
+
]);
|
|
173
|
+
}
|
|
174
|
+
if (J.finishReason) {
|
|
175
|
+
o.metadata = { ...o.metadata ?? {}, finishReason: J.finishReason };
|
|
176
|
+
const O = t.value.findIndex((U) => U.id === o.id);
|
|
177
|
+
O >= 0 && (t.value = [
|
|
178
|
+
...t.value.slice(0, O),
|
|
179
|
+
{ ...o },
|
|
180
|
+
...t.value.slice(O + 1)
|
|
181
|
+
]);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
b == null || b({ ...o });
|
|
185
|
+
} catch (P) {
|
|
186
|
+
const I = $(P);
|
|
187
|
+
if (I.name === "AbortError" || k.signal.aborted) {
|
|
188
|
+
b == null || b({ ...o });
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
throw T.value = I, l == null || l(I), I;
|
|
192
|
+
} finally {
|
|
193
|
+
_.value = null, v.value = !1;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
async function j(o, w, k) {
|
|
197
|
+
await E(o, w);
|
|
198
|
+
const R = o.toolCalls;
|
|
199
|
+
if (!n || !(R != null && R.length)) return;
|
|
200
|
+
if (k <= 0)
|
|
201
|
+
throw y(new N("Maximum tool roundtrips exceeded"));
|
|
202
|
+
v.value = !0;
|
|
203
|
+
let P;
|
|
204
|
+
try {
|
|
205
|
+
P = await S(R);
|
|
206
|
+
} catch (J) {
|
|
207
|
+
throw v.value = !1, y(J);
|
|
208
|
+
}
|
|
209
|
+
const I = A();
|
|
210
|
+
t.value = [...t.value, ...P, I], await j(
|
|
211
|
+
I,
|
|
212
|
+
{ messages: t.value.filter((J) => J.id !== I.id) },
|
|
213
|
+
k - 1
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
async function z(o, w = {}) {
|
|
217
|
+
const k = typeof o == "string" ? { id: W("user"), role: "user", content: o, createdAt: /* @__PURE__ */ new Date() } : { ...o, id: o.id || W(o.role) }, R = A();
|
|
218
|
+
t.value = [...t.value, k, R], await j(
|
|
219
|
+
R,
|
|
220
|
+
{
|
|
221
|
+
messages: t.value.filter((P) => P.id !== R.id),
|
|
222
|
+
...w
|
|
223
|
+
},
|
|
224
|
+
C
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
async function H() {
|
|
228
|
+
var R;
|
|
229
|
+
const o = (R = [...t.value].map((P, I) => ({ m: P, i: I })).reverse().find(({ m: P }) => P.role === "assistant")) == null ? void 0 : R.i;
|
|
230
|
+
if (o === void 0)
|
|
231
|
+
throw new N("reload() called with no assistant message to re-run");
|
|
232
|
+
const w = t.value.slice(0, o), k = A();
|
|
233
|
+
t.value = [...w, k], await j(k, { messages: w }, C);
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
messages: t,
|
|
237
|
+
input: d,
|
|
238
|
+
isLoading: v,
|
|
239
|
+
error: T,
|
|
240
|
+
append: z,
|
|
241
|
+
reload: H,
|
|
242
|
+
stop: r,
|
|
243
|
+
setMessages: c,
|
|
244
|
+
clear: h,
|
|
245
|
+
abortController: _
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
function re(u) {
|
|
249
|
+
const { provider: m, initialCompletion: s = "", defaultRequest: g = {}, onFinish: i, onError: b } = u, l = M(s), a = M(""), f = M(!1), p = M(null), n = B(null);
|
|
250
|
+
function C() {
|
|
251
|
+
n.value && n.value.abort(), n.value = null, f.value = !1;
|
|
252
|
+
}
|
|
253
|
+
function x(d) {
|
|
254
|
+
l.value = d;
|
|
255
|
+
}
|
|
256
|
+
async function t(d, v = {}) {
|
|
257
|
+
const T = d ?? a.value;
|
|
258
|
+
if (!T)
|
|
259
|
+
throw new Error("complete() requires a prompt (either as argument or via input.value)");
|
|
260
|
+
const _ = new AbortController();
|
|
261
|
+
n.value = _, f.value = !0, p.value = null, l.value = "";
|
|
262
|
+
try {
|
|
263
|
+
const e = await m.completion({
|
|
264
|
+
...g,
|
|
265
|
+
...v,
|
|
266
|
+
prompt: T,
|
|
267
|
+
signal: _.signal,
|
|
268
|
+
stream: !0
|
|
269
|
+
});
|
|
270
|
+
for await (const c of e)
|
|
271
|
+
l.value += c;
|
|
272
|
+
return i == null || i(l.value), l.value;
|
|
273
|
+
} catch (e) {
|
|
274
|
+
const c = e instanceof Error ? e : new Error(String(e));
|
|
275
|
+
if (c.name === "AbortError" || _.signal.aborted)
|
|
276
|
+
return i == null || i(l.value), l.value;
|
|
277
|
+
throw p.value = c, b == null || b(c), c;
|
|
278
|
+
} finally {
|
|
279
|
+
n.value = null, f.value = !1;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return {
|
|
283
|
+
completion: l,
|
|
284
|
+
input: a,
|
|
285
|
+
isLoading: f,
|
|
286
|
+
error: p,
|
|
287
|
+
complete: t,
|
|
288
|
+
stop: C,
|
|
289
|
+
setCompletion: x,
|
|
290
|
+
abortController: n
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
function ie(u) {
|
|
294
|
+
const { provider: m, defaultRequest: s = {}, onSuccess: g, onError: i } = u, b = M([]), l = M(!1), a = M(null), f = B(null), p = B(null);
|
|
295
|
+
async function n(C, x = {}) {
|
|
296
|
+
const t = new AbortController();
|
|
297
|
+
p.value = t, l.value = !0, a.value = null;
|
|
298
|
+
try {
|
|
299
|
+
const d = await m.embedding({
|
|
300
|
+
...s,
|
|
301
|
+
...x,
|
|
302
|
+
input: C,
|
|
303
|
+
signal: t.signal
|
|
304
|
+
});
|
|
305
|
+
return b.value = d.embeddings, f.value = d, g == null || g(d), d;
|
|
306
|
+
} catch (d) {
|
|
307
|
+
const v = d instanceof Error ? d : new Error(String(d));
|
|
308
|
+
throw a.value = v, i == null || i(v), v;
|
|
309
|
+
} finally {
|
|
310
|
+
p.value = null, l.value = !1;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return {
|
|
314
|
+
embeddings: b,
|
|
315
|
+
isLoading: l,
|
|
316
|
+
error: a,
|
|
317
|
+
result: f,
|
|
318
|
+
embed: n,
|
|
319
|
+
abortController: p
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
async function* G(u, m) {
|
|
323
|
+
if (!u.body)
|
|
324
|
+
throw new Error("Response has no body");
|
|
325
|
+
const s = u.body.getReader(), g = new TextDecoder("utf-8");
|
|
326
|
+
let i = "";
|
|
327
|
+
try {
|
|
328
|
+
for (; ; ) {
|
|
329
|
+
if (m != null && m.aborted) {
|
|
330
|
+
await s.cancel();
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
const { done: b, value: l } = await s.read();
|
|
334
|
+
if (b) break;
|
|
335
|
+
i += g.decode(l, { stream: !0 }).replace(/\r\n/g, `
|
|
336
|
+
`).replace(/\r/g, `
|
|
337
|
+
`);
|
|
338
|
+
let a;
|
|
339
|
+
for (; (a = i.indexOf(`
|
|
340
|
+
|
|
341
|
+
`)) !== -1; ) {
|
|
342
|
+
const f = i.slice(0, a);
|
|
343
|
+
i = i.slice(a + 2);
|
|
344
|
+
for (const p of f.split(`
|
|
345
|
+
`)) {
|
|
346
|
+
if (!p.startsWith("data:")) continue;
|
|
347
|
+
const n = p.slice(5).trim();
|
|
348
|
+
if (n === "[DONE]") return;
|
|
349
|
+
if (n)
|
|
350
|
+
try {
|
|
351
|
+
yield JSON.parse(n);
|
|
352
|
+
} catch {
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
} finally {
|
|
358
|
+
s.releaseLock();
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
async function K(u, m = {}) {
|
|
362
|
+
const { timeoutMs: s, signal: g, fetcher: i, ...b } = m, l = i ?? (typeof globalThis.fetch == "function" ? globalThis.fetch.bind(globalThis) : void 0);
|
|
363
|
+
if (!l)
|
|
364
|
+
throw new N("No fetch implementation available");
|
|
365
|
+
const a = new AbortController(), f = () => a.abort();
|
|
366
|
+
g && (g.aborted ? a.abort() : g.addEventListener("abort", f, { once: !0 }));
|
|
367
|
+
let p;
|
|
368
|
+
s && s > 0 && (p = setTimeout(() => a.abort(), s));
|
|
369
|
+
try {
|
|
370
|
+
const n = await l(u, { ...b, signal: a.signal });
|
|
371
|
+
if (!n.ok) {
|
|
372
|
+
let C;
|
|
373
|
+
try {
|
|
374
|
+
C = await n.json();
|
|
375
|
+
} catch {
|
|
376
|
+
C = await n.text().catch(() => {
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
throw new N(
|
|
380
|
+
`Request failed with status ${n.status} ${n.statusText}`,
|
|
381
|
+
{ status: n.status, cause: C }
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
return n;
|
|
385
|
+
} catch (n) {
|
|
386
|
+
throw n instanceof N ? n : (n == null ? void 0 : n.name) === "AbortError" ? new N("Request aborted", { cause: n }) : new N("Network error", { cause: n });
|
|
387
|
+
} finally {
|
|
388
|
+
p && clearTimeout(p), g && g.removeEventListener("abort", f);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
function oe(u) {
|
|
392
|
+
const {
|
|
393
|
+
apiKey: m,
|
|
394
|
+
baseURL: s,
|
|
395
|
+
headers: g = {},
|
|
396
|
+
defaultModel: i,
|
|
397
|
+
chatPath: b = "/chat/completions",
|
|
398
|
+
completionPath: l = "/completions",
|
|
399
|
+
embeddingPath: a = "/embeddings",
|
|
400
|
+
fetch: f
|
|
401
|
+
} = u, p = {
|
|
402
|
+
"Content-Type": "application/json",
|
|
403
|
+
Authorization: `Bearer ${m}`,
|
|
404
|
+
...g
|
|
405
|
+
}, n = (x, t) => {
|
|
406
|
+
const d = x.replace(/\/+$/, ""), v = t.startsWith("/") ? t : `/${t}`;
|
|
407
|
+
return `${d}${v}`;
|
|
408
|
+
};
|
|
409
|
+
function C(x) {
|
|
410
|
+
return x.map((t) => {
|
|
411
|
+
var v;
|
|
412
|
+
const d = { role: t.role, content: t.content };
|
|
413
|
+
return t.name && (d.name = t.name), t.toolCallId && (d.tool_call_id = t.toolCallId), (v = t.toolCalls) != null && v.length && (d.tool_calls = t.toolCalls), d;
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
return {
|
|
417
|
+
id: "openai-compatible",
|
|
418
|
+
async chat(x) {
|
|
419
|
+
const {
|
|
420
|
+
messages: t,
|
|
421
|
+
model: d = i,
|
|
422
|
+
temperature: v,
|
|
423
|
+
maxTokens: T,
|
|
424
|
+
topP: _,
|
|
425
|
+
frequencyPenalty: e,
|
|
426
|
+
presencePenalty: c,
|
|
427
|
+
stop: h,
|
|
428
|
+
tools: r,
|
|
429
|
+
toolChoice: A,
|
|
430
|
+
user: $,
|
|
431
|
+
stream: y = !0,
|
|
432
|
+
signal: D,
|
|
433
|
+
headers: L
|
|
434
|
+
} = x, S = {
|
|
435
|
+
model: d,
|
|
436
|
+
messages: C(t),
|
|
437
|
+
stream: y
|
|
438
|
+
};
|
|
439
|
+
v !== void 0 && (S.temperature = v), T !== void 0 && (S.max_tokens = T), _ !== void 0 && (S.top_p = _), e !== void 0 && (S.frequency_penalty = e), c !== void 0 && (S.presence_penalty = c), h !== void 0 && (S.stop = h), r && (S.tools = r), A && (S.tool_choice = A), $ && (S.user = $);
|
|
440
|
+
const E = await K(n(s, b), {
|
|
441
|
+
method: "POST",
|
|
442
|
+
headers: { ...p, ...L },
|
|
443
|
+
body: JSON.stringify(S),
|
|
444
|
+
signal: D,
|
|
445
|
+
fetcher: f
|
|
446
|
+
});
|
|
447
|
+
if (!y) {
|
|
448
|
+
const j = await E.json();
|
|
449
|
+
return async function* () {
|
|
450
|
+
var z, H, o, w, k;
|
|
451
|
+
yield {
|
|
452
|
+
content: ((o = (H = (z = j.choices) == null ? void 0 : z[0]) == null ? void 0 : H.message) == null ? void 0 : o.content) ?? "",
|
|
453
|
+
finishReason: ((k = (w = j.choices) == null ? void 0 : w[0]) == null ? void 0 : k.finish_reason) ?? "stop",
|
|
454
|
+
usage: j.usage
|
|
455
|
+
};
|
|
456
|
+
}();
|
|
457
|
+
}
|
|
458
|
+
return async function* () {
|
|
459
|
+
var j, z, H;
|
|
460
|
+
for await (const o of G(E, D)) {
|
|
461
|
+
const w = (j = o.choices) == null ? void 0 : j[0];
|
|
462
|
+
if (!w) continue;
|
|
463
|
+
const k = o.usage;
|
|
464
|
+
yield {
|
|
465
|
+
content: (z = w.delta) == null ? void 0 : z.content,
|
|
466
|
+
toolCalls: (H = w.delta) == null ? void 0 : H.tool_calls,
|
|
467
|
+
finishReason: w.finish_reason ?? void 0,
|
|
468
|
+
usage: k
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
}();
|
|
472
|
+
},
|
|
473
|
+
async completion(x) {
|
|
474
|
+
const {
|
|
475
|
+
prompt: t,
|
|
476
|
+
model: d = i,
|
|
477
|
+
temperature: v,
|
|
478
|
+
maxTokens: T,
|
|
479
|
+
topP: _,
|
|
480
|
+
frequencyPenalty: e,
|
|
481
|
+
presencePenalty: c,
|
|
482
|
+
stop: h,
|
|
483
|
+
stream: r = !0,
|
|
484
|
+
signal: A,
|
|
485
|
+
headers: $
|
|
486
|
+
} = x, y = { model: d, prompt: t, stream: r };
|
|
487
|
+
v !== void 0 && (y.temperature = v), T !== void 0 && (y.max_tokens = T), _ !== void 0 && (y.top_p = _), e !== void 0 && (y.frequency_penalty = e), c !== void 0 && (y.presence_penalty = c), h !== void 0 && (y.stop = h);
|
|
488
|
+
const D = await K(n(s, l), {
|
|
489
|
+
method: "POST",
|
|
490
|
+
headers: { ...p, ...$ },
|
|
491
|
+
body: JSON.stringify(y),
|
|
492
|
+
signal: A,
|
|
493
|
+
fetcher: f
|
|
494
|
+
});
|
|
495
|
+
if (!r) {
|
|
496
|
+
const L = await D.json();
|
|
497
|
+
return async function* () {
|
|
498
|
+
var S, E;
|
|
499
|
+
yield ((E = (S = L.choices) == null ? void 0 : S[0]) == null ? void 0 : E.text) ?? "";
|
|
500
|
+
}();
|
|
501
|
+
}
|
|
502
|
+
return async function* () {
|
|
503
|
+
var L;
|
|
504
|
+
for await (const S of G(D, A)) {
|
|
505
|
+
const E = (L = S.choices) == null ? void 0 : L[0];
|
|
506
|
+
E != null && E.text && (yield E.text);
|
|
507
|
+
}
|
|
508
|
+
}();
|
|
509
|
+
},
|
|
510
|
+
async embedding(x) {
|
|
511
|
+
const { input: t, model: d = i, user: v, signal: T, headers: _ } = x, e = { input: t };
|
|
512
|
+
d && (e.model = d), v && (e.user = v);
|
|
513
|
+
const h = await (await K(n(s, a), {
|
|
514
|
+
method: "POST",
|
|
515
|
+
headers: { ...p, ..._ },
|
|
516
|
+
body: JSON.stringify(e),
|
|
517
|
+
signal: T,
|
|
518
|
+
fetcher: f
|
|
519
|
+
})).json();
|
|
520
|
+
return {
|
|
521
|
+
embeddings: h.data.map((r) => r.embedding),
|
|
522
|
+
model: h.model,
|
|
523
|
+
usage: {
|
|
524
|
+
promptTokens: h.usage.prompt_tokens,
|
|
525
|
+
totalTokens: h.usage.total_tokens
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
function le(u) {
|
|
532
|
+
return oe({
|
|
533
|
+
baseURL: "https://api.openai.com/v1",
|
|
534
|
+
defaultModel: "gpt-4o-mini",
|
|
535
|
+
...u
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
function X(u) {
|
|
539
|
+
switch (u) {
|
|
540
|
+
case "end_turn":
|
|
541
|
+
case "stop_sequence":
|
|
542
|
+
return "stop";
|
|
543
|
+
case "max_tokens":
|
|
544
|
+
return "length";
|
|
545
|
+
case "tool_use":
|
|
546
|
+
return "tool_calls";
|
|
547
|
+
default:
|
|
548
|
+
return "stop";
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
function ce(u) {
|
|
552
|
+
const {
|
|
553
|
+
apiKey: m,
|
|
554
|
+
baseURL: s = "https://api.anthropic.com",
|
|
555
|
+
defaultModel: g = "claude-3-5-sonnet-20241022",
|
|
556
|
+
maxTokens: i = 1024,
|
|
557
|
+
anthropicVersion: b = "2023-06-01",
|
|
558
|
+
headers: l = {},
|
|
559
|
+
fetch: a
|
|
560
|
+
} = u, f = {
|
|
561
|
+
"Content-Type": "application/json",
|
|
562
|
+
"x-api-key": m,
|
|
563
|
+
"anthropic-version": b,
|
|
564
|
+
...l
|
|
565
|
+
}, p = (e) => {
|
|
566
|
+
const c = s.replace(/\/+$/, ""), h = e.startsWith("/") ? e : `/${e}`;
|
|
567
|
+
return `${c}${h}`;
|
|
568
|
+
};
|
|
569
|
+
function n(e) {
|
|
570
|
+
return typeof e == "string" ? e : e.map((c) => C(c));
|
|
571
|
+
}
|
|
572
|
+
function C(e) {
|
|
573
|
+
if (e.type === "text")
|
|
574
|
+
return { type: "text", text: e.text };
|
|
575
|
+
const c = e.image_url.url;
|
|
576
|
+
if (c.startsWith("data:")) {
|
|
577
|
+
const h = c.match(/^data:([^;]+);base64,(.*)$/s);
|
|
578
|
+
if (h)
|
|
579
|
+
return {
|
|
580
|
+
type: "image",
|
|
581
|
+
source: { type: "base64", media_type: h[1], data: h[2] }
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
return { type: "image", source: { type: "url", url: c } };
|
|
585
|
+
}
|
|
586
|
+
function x(e) {
|
|
587
|
+
const c = [], h = [];
|
|
588
|
+
for (const r of e)
|
|
589
|
+
r.role === "system" ? typeof r.content == "string" && r.content && c.push(r.content) : (r.role === "user" || r.role === "assistant") && h.push({ role: r.role, content: n(r.content) });
|
|
590
|
+
return {
|
|
591
|
+
system: c.length ? c.join(`
|
|
592
|
+
|
|
593
|
+
`) : void 0,
|
|
594
|
+
messages: h
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
function t(e) {
|
|
598
|
+
const { system: c, messages: h } = x(e.messages), r = {
|
|
599
|
+
model: e.model ?? g,
|
|
600
|
+
messages: h,
|
|
601
|
+
max_tokens: e.maxTokens ?? i,
|
|
602
|
+
stream: e.stream ?? !0
|
|
603
|
+
};
|
|
604
|
+
return c && (r.system = c), e.temperature !== void 0 && (r.temperature = e.temperature), e.topP !== void 0 && (r.top_p = e.topP), e.stop !== void 0 && (r.stop_sequences = Array.isArray(e.stop) ? e.stop : [e.stop]), e.user && (r.metadata = { user_id: e.user }), r;
|
|
605
|
+
}
|
|
606
|
+
async function d(e) {
|
|
607
|
+
const c = t({ ...e, stream: e.stream ?? !0 }), h = await K(p("/v1/messages"), {
|
|
608
|
+
method: "POST",
|
|
609
|
+
headers: { ...f, ...e.headers },
|
|
610
|
+
body: JSON.stringify(c),
|
|
611
|
+
signal: e.signal,
|
|
612
|
+
fetcher: a
|
|
613
|
+
});
|
|
614
|
+
if (e.stream === !1) {
|
|
615
|
+
const r = await h.json();
|
|
616
|
+
return async function* () {
|
|
617
|
+
const A = r.content.find(($) => $.type === "text");
|
|
618
|
+
yield {
|
|
619
|
+
content: (A == null ? void 0 : A.text) ?? "",
|
|
620
|
+
finishReason: X(r.stop_reason),
|
|
621
|
+
usage: r.usage ? {
|
|
622
|
+
promptTokens: r.usage.input_tokens,
|
|
623
|
+
completionTokens: r.usage.output_tokens,
|
|
624
|
+
totalTokens: r.usage.input_tokens + r.usage.output_tokens
|
|
625
|
+
} : void 0
|
|
626
|
+
};
|
|
627
|
+
}();
|
|
628
|
+
}
|
|
629
|
+
return async function* () {
|
|
630
|
+
var r;
|
|
631
|
+
for await (const A of G(h, e.signal)) {
|
|
632
|
+
const $ = A.type;
|
|
633
|
+
if ($ === "content_block_delta") {
|
|
634
|
+
const y = A.delta;
|
|
635
|
+
(y == null ? void 0 : y.type) === "text_delta" && typeof y.text == "string" && (yield { content: y.text });
|
|
636
|
+
} else if ($ === "message_delta") {
|
|
637
|
+
const y = (r = A.delta) == null ? void 0 : r.stop_reason;
|
|
638
|
+
y && (yield { finishReason: X(y) });
|
|
639
|
+
} else if ($ === "error") {
|
|
640
|
+
const y = A.error;
|
|
641
|
+
throw new N(
|
|
642
|
+
`Anthropic API error: ${(y == null ? void 0 : y.type) ?? "unknown"}: ${(y == null ? void 0 : y.message) ?? "unknown"}`,
|
|
643
|
+
{ cause: A }
|
|
644
|
+
);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
}();
|
|
648
|
+
}
|
|
649
|
+
async function v(e) {
|
|
650
|
+
return (async function* () {
|
|
651
|
+
const c = await this.chat({
|
|
652
|
+
...e,
|
|
653
|
+
messages: [{ id: "prompt", role: "user", content: e.prompt }]
|
|
654
|
+
});
|
|
655
|
+
for await (const h of c)
|
|
656
|
+
h.content && (yield h.content);
|
|
657
|
+
}).call(_);
|
|
658
|
+
}
|
|
659
|
+
async function T() {
|
|
660
|
+
throw new N("Anthropic has no embedding API", { status: 501 });
|
|
661
|
+
}
|
|
662
|
+
const _ = {
|
|
663
|
+
id: "anthropic",
|
|
664
|
+
chat: d,
|
|
665
|
+
completion: v,
|
|
666
|
+
embedding: T
|
|
667
|
+
};
|
|
668
|
+
return _;
|
|
669
|
+
}
|
|
670
|
+
export {
|
|
671
|
+
N as AiHooksError,
|
|
672
|
+
ce as anthropic,
|
|
673
|
+
le as openai,
|
|
674
|
+
oe as openaiCompatible,
|
|
675
|
+
se as useChat,
|
|
676
|
+
re as useCompletion,
|
|
677
|
+
ie as useEmbedding,
|
|
678
|
+
ee as usePersist
|
|
679
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ChatProvider } from './types';
|
|
2
|
+
/** Configuration for the Anthropic provider. */
|
|
3
|
+
export interface AnthropicConfig {
|
|
4
|
+
/** Your Anthropic API key. */
|
|
5
|
+
apiKey: string;
|
|
6
|
+
/** Base URL. Defaults to https://api.anthropic.com */
|
|
7
|
+
baseURL?: string;
|
|
8
|
+
/** Default model when a request omits one. Defaults to claude-3-5-sonnet. */
|
|
9
|
+
defaultModel?: string;
|
|
10
|
+
/** Default max_tokens. Anthropic requires this. Defaults to 1024. */
|
|
11
|
+
maxTokens?: number;
|
|
12
|
+
/** Anthropic API version header. Defaults to 2023-06-01. */
|
|
13
|
+
anthropicVersion?: string;
|
|
14
|
+
/** Additional headers. */
|
|
15
|
+
headers?: Record<string, string>;
|
|
16
|
+
/** Custom fetch implementation. */
|
|
17
|
+
fetch?: typeof fetch;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Build an Anthropic Claude provider. Compatible with the Claude 3 / 3.5 / 4
|
|
21
|
+
* family on the official API and on proxies that follow the same wire format.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { useChat, anthropic } from 'vue-ai-hooks'
|
|
25
|
+
*
|
|
26
|
+
* const { messages, append } = useChat({
|
|
27
|
+
* provider: anthropic({ apiKey: import.meta.env.VITE_ANTHROPIC_KEY })
|
|
28
|
+
* })
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* Notes:
|
|
32
|
+
* - Anthropic has no embeddings API. Calling `useEmbedding` with this provider
|
|
33
|
+
* throws a clear `AiHooksError`.
|
|
34
|
+
* - Anthropic has no `/v1/completions` endpoint. `useCompletion` is implemented
|
|
35
|
+
* as a single-turn chat with a user message.
|
|
36
|
+
* - The system prompt is a top-level field, not part of `messages[]`. The
|
|
37
|
+
* adapter extracts any `role: 'system'` messages and joins them.
|
|
38
|
+
*/
|
|
39
|
+
export declare function anthropic(config: AnthropicConfig): ChatProvider;
|
|
40
|
+
//# sourceMappingURL=anthropic.d.ts.map
|