@anvaka/vue-llm 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +64 -3
- package/dist/core/LLMClient.js +92 -63
- package/dist/index.js +37 -29
- package/dist/pricing/calculate.js +58 -0
- package/dist/pricing/index.js +10 -0
- package/dist/pricing/rates.js +80 -0
- package/dist/providers/AnthropicProvider.js +35 -26
- package/dist/providers/BaseProvider.js +84 -74
- package/dist/providers/CustomProvider.js +69 -28
- package/dist/providers/DeepSeekProvider.js +115 -0
- package/dist/providers/GeminiProvider.js +143 -111
- package/dist/providers/GrokProvider.js +75 -38
- package/dist/providers/LlamaServerProvider.js +72 -31
- package/dist/providers/OllamaProvider.js +117 -41
- package/dist/providers/OpenAIProvider.js +64 -57
- package/dist/providers/OpenRouterProvider.js +40 -40
- package/dist/providers/factory.js +12 -7
- package/dist/providers/index.js +10 -8
- package/package.json +7 -2
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import { BaseProvider as
|
|
2
|
-
class
|
|
1
|
+
import { BaseProvider as p } from "./BaseProvider.js";
|
|
2
|
+
class y extends p {
|
|
3
3
|
async detectCapabilities() {
|
|
4
4
|
var e, t, n, o;
|
|
5
5
|
((e = this.config.model) != null && e.includes("claude-3") || (t = this.config.model) != null && t.includes("claude-sonnet") || (n = this.config.model) != null && n.includes("claude-opus") || (o = this.config.model) != null && o.includes("claude-haiku")) && (this.capabilities.add("vision"), this.capabilities.add("tools"));
|
|
6
6
|
}
|
|
7
7
|
prepareRequest(e, t) {
|
|
8
|
-
const n =
|
|
9
|
-
model:
|
|
8
|
+
const n = _(e), o = t.model || this.config.model || "claude-3-sonnet-20240229", i = {
|
|
9
|
+
model: o,
|
|
10
10
|
max_tokens: t.maxTokens || 1e3,
|
|
11
|
-
temperature: t.temperature ?? 0.7,
|
|
12
11
|
messages: n,
|
|
13
12
|
stream: t.stream || !1
|
|
14
|
-
}
|
|
15
|
-
|
|
13
|
+
};
|
|
14
|
+
o.includes("claude-opus-4-7") || (i.temperature = t.temperature ?? 0.7);
|
|
15
|
+
const c = e.find((r) => r.role === "system");
|
|
16
|
+
return c && (i.system = c.content), t.tools && this.capabilities.has("tools") && (i.tools = g(t.tools), t.tool_choice && (i.tool_choice = t.tool_choice)), i;
|
|
16
17
|
}
|
|
17
18
|
processMessages(e, t) {
|
|
18
19
|
return t.images && this.capabilities.has("vision") ? this.addImagesToMessages(e, t.images) : e;
|
|
@@ -21,22 +22,22 @@ class m extends f {
|
|
|
21
22
|
const n = e[e.length - 1];
|
|
22
23
|
if (n && n.role === "user") {
|
|
23
24
|
const o = [{ type: "text", text: n.content }];
|
|
24
|
-
t.forEach((
|
|
25
|
+
t.forEach((i) => {
|
|
25
26
|
o.push({
|
|
26
27
|
type: "image",
|
|
27
|
-
source: { type: "base64", media_type: "image/jpeg", data: typeof
|
|
28
|
+
source: { type: "base64", media_type: "image/jpeg", data: typeof i == "string" ? i : i.data }
|
|
28
29
|
});
|
|
29
30
|
}), n.content = o;
|
|
30
31
|
}
|
|
31
32
|
return e;
|
|
32
33
|
}
|
|
33
34
|
processResponse(e) {
|
|
34
|
-
const t =
|
|
35
|
+
const t = l(e.stop_reason), n = e.content || [], o = n.find((r) => r.type === "text"), c = n.filter((r) => r.type === "tool_use").map((r) => ({ id: r.id, name: r.name, args: r.input || {} }));
|
|
35
36
|
return {
|
|
36
37
|
content: (o == null ? void 0 : o.text) || "",
|
|
37
|
-
usage: e.usage
|
|
38
|
+
usage: d(e.usage),
|
|
38
39
|
finishReason: t,
|
|
39
|
-
toolCalls:
|
|
40
|
+
toolCalls: c
|
|
40
41
|
};
|
|
41
42
|
}
|
|
42
43
|
parseStreamingLine(e) {
|
|
@@ -50,12 +51,14 @@ class m extends f {
|
|
|
50
51
|
}
|
|
51
52
|
}
|
|
52
53
|
extractStreamingContent(e) {
|
|
53
|
-
var t, n, o,
|
|
54
|
+
var t, n, o, i, c, r, h, f;
|
|
54
55
|
if (e.done) return { done: !0 };
|
|
55
56
|
if (e.type === "error") {
|
|
56
|
-
const a = ((t = e.error) == null ? void 0 : t.type) || "anthropic_error",
|
|
57
|
-
throw
|
|
57
|
+
const a = ((t = e.error) == null ? void 0 : t.type) || "anthropic_error", u = new Error(((n = e.error) == null ? void 0 : n.message) || "Anthropic streaming error");
|
|
58
|
+
throw u.code = a, e.request_id && (u.requestId = e.request_id), u;
|
|
58
59
|
}
|
|
60
|
+
if (e.type === "message_start")
|
|
61
|
+
return { content: "", thinking: "", done: !1, usage: d((o = e.message) == null ? void 0 : o.usage), finishReason: null };
|
|
59
62
|
if (e.type === "content_block_start") {
|
|
60
63
|
const a = e.content_block;
|
|
61
64
|
return (a == null ? void 0 : a.type) === "tool_use" ? {
|
|
@@ -64,11 +67,11 @@ class m extends f {
|
|
|
64
67
|
toolCallDelta: { index: e.index, id: a.id, name: a.name, argsTextDelta: "" }
|
|
65
68
|
} : null;
|
|
66
69
|
}
|
|
67
|
-
return e.type === "content_block_delta" ? ((
|
|
70
|
+
return e.type === "content_block_delta" ? ((i = e.delta) == null ? void 0 : i.type) === "text_delta" ? { content: ((c = e.delta) == null ? void 0 : c.text) || "", thinking: "", done: !1, usage: null, finishReason: null } : ((r = e.delta) == null ? void 0 : r.type) === "input_json_delta" ? {
|
|
68
71
|
content: "",
|
|
69
72
|
done: !1,
|
|
70
|
-
toolCallDelta: { index: e.index, argsTextDelta: ((
|
|
71
|
-
} : null : e.type === "message_delta" ? { content: "", thinking: "", done: !1, usage: e.usage
|
|
73
|
+
toolCallDelta: { index: e.index, argsTextDelta: ((h = e.delta) == null ? void 0 : h.partial_json) || "" }
|
|
74
|
+
} : null : e.type === "message_delta" ? { content: "", thinking: "", done: !1, usage: d(e.usage), finishReason: l((f = e.delta) == null ? void 0 : f.stop_reason) } : e.type === "message_stop" ? { content: "", thinking: "", done: !0, usage: null, finishReason: l(e.stop_reason) } : null;
|
|
72
75
|
}
|
|
73
76
|
getApiPath() {
|
|
74
77
|
return "/v1/messages";
|
|
@@ -94,14 +97,19 @@ class m extends f {
|
|
|
94
97
|
return ((t = e.data) == null ? void 0 : t.map((n) => n.id)) || [];
|
|
95
98
|
}
|
|
96
99
|
}
|
|
97
|
-
function
|
|
98
|
-
if (!
|
|
99
|
-
const e = String(
|
|
100
|
+
function l(s) {
|
|
101
|
+
if (!s) return null;
|
|
102
|
+
const e = String(s).toLowerCase();
|
|
100
103
|
return e === "max_tokens" ? "length" : e;
|
|
101
104
|
}
|
|
102
|
-
function
|
|
105
|
+
function d(s) {
|
|
106
|
+
if (!s) return null;
|
|
107
|
+
const e = s.input_tokens ?? 0, t = s.cache_read_input_tokens ?? 0, n = s.cache_creation_input_tokens ?? 0, i = s.input_tokens != null || s.cache_read_input_tokens != null || s.cache_creation_input_tokens != null ? e + t + n : 0, c = s.output_tokens ?? 0, r = { inputTokens: i, outputTokens: c, totalTokens: i + c, raw: s };
|
|
108
|
+
return s.cache_read_input_tokens != null && (r.cachedInputTokens = t), s.cache_creation_input_tokens != null && (r.cacheCreationInputTokens = n), r;
|
|
109
|
+
}
|
|
110
|
+
function _(s) {
|
|
103
111
|
const e = [];
|
|
104
|
-
for (const t of
|
|
112
|
+
for (const t of s)
|
|
105
113
|
if (t.role !== "system") {
|
|
106
114
|
if (t.role === "assistant" && Array.isArray(t.tool_calls) && t.tool_calls.length) {
|
|
107
115
|
const n = [];
|
|
@@ -131,13 +139,14 @@ function h(i) {
|
|
|
131
139
|
}
|
|
132
140
|
return e;
|
|
133
141
|
}
|
|
134
|
-
function
|
|
135
|
-
return
|
|
142
|
+
function g(s) {
|
|
143
|
+
return s.map((e) => e.type === "function" && e.function ? {
|
|
136
144
|
name: e.function.name,
|
|
137
145
|
description: e.function.description,
|
|
138
146
|
input_schema: e.function.parameters || { type: "object", properties: {} }
|
|
139
147
|
} : e);
|
|
140
148
|
}
|
|
141
149
|
export {
|
|
142
|
-
|
|
150
|
+
y as AnthropicProvider,
|
|
151
|
+
d as normalizeAnthropicUsage
|
|
143
152
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
class
|
|
1
|
+
class A {
|
|
2
2
|
constructor(e) {
|
|
3
3
|
this.config = e, this.capabilities = /* @__PURE__ */ new Set(), this.activeRequests = /* @__PURE__ */ new Map();
|
|
4
4
|
}
|
|
@@ -7,106 +7,108 @@ class q {
|
|
|
7
7
|
}
|
|
8
8
|
async detectCapabilities() {
|
|
9
9
|
}
|
|
10
|
-
prepareRequest(e,
|
|
10
|
+
prepareRequest(e, t) {
|
|
11
11
|
throw new Error("prepareRequest must be implemented by subclass");
|
|
12
12
|
}
|
|
13
13
|
processResponse(e) {
|
|
14
14
|
throw new Error("processResponse must be implemented by subclass");
|
|
15
15
|
}
|
|
16
|
-
async streamRequest(e,
|
|
17
|
-
const
|
|
18
|
-
this.activeRequests.set(l,
|
|
16
|
+
async streamRequest(e, t, n) {
|
|
17
|
+
const r = this.prepareRequest(e, t), o = new AbortController(), l = t.requestId || this.generateRequestId();
|
|
18
|
+
this.activeRequests.set(l, o);
|
|
19
19
|
try {
|
|
20
|
-
const
|
|
21
|
-
let
|
|
22
|
-
const
|
|
23
|
-
let
|
|
24
|
-
if (
|
|
20
|
+
const k = (await this.makeStreamingRequest(r, o.signal)).body.getReader(), E = new TextDecoder();
|
|
21
|
+
let m = "", f = "", g = null, u = "";
|
|
22
|
+
const w = /* @__PURE__ */ new Map(), b = () => Array.from(w.values()).sort((a, s) => a.index - s.index).map((a) => {
|
|
23
|
+
let s = {};
|
|
24
|
+
if (a.argsText)
|
|
25
25
|
try {
|
|
26
|
-
|
|
26
|
+
s = JSON.parse(a.argsText);
|
|
27
27
|
} catch {
|
|
28
|
-
|
|
28
|
+
s = { __parseError: !0, raw: a.argsText };
|
|
29
29
|
}
|
|
30
|
-
return { id:
|
|
31
|
-
}),
|
|
32
|
-
if (!
|
|
33
|
-
const
|
|
34
|
-
if (!
|
|
35
|
-
const
|
|
36
|
-
if (!
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
30
|
+
return { id: a.id, name: a.name, args: s };
|
|
31
|
+
}), R = (p) => {
|
|
32
|
+
if (!p.trim()) return null;
|
|
33
|
+
const a = this.parseStreamingLine(p);
|
|
34
|
+
if (!a) return null;
|
|
35
|
+
const s = this.extractStreamingContent(a);
|
|
36
|
+
if (!s) return null;
|
|
37
|
+
s.content && (m += s.content), s.thinking && (f += s.thinking), s.usage && (g = q(g, s.usage));
|
|
38
|
+
const y = Array.isArray(s.toolCallDeltas) ? s.toolCallDeltas : s.toolCallDelta ? [s.toolCallDelta] : [];
|
|
39
|
+
for (const i of y) {
|
|
40
|
+
let d = w.get(i.index);
|
|
41
|
+
d ? (i.id && (d.id = i.id), i.name && (d.name = i.name)) : (d = { index: i.index, id: i.id || null, name: i.name || "", argsText: "" }, w.set(i.index, d)), i.argsTextDelta && (d.argsText += i.argsTextDelta);
|
|
41
42
|
}
|
|
42
|
-
const
|
|
43
|
+
const T = s.done || !1, C = T ? b() : null;
|
|
43
44
|
return n({
|
|
44
|
-
content:
|
|
45
|
-
thinking:
|
|
46
|
-
fullContent:
|
|
47
|
-
fullThinking:
|
|
48
|
-
done:
|
|
49
|
-
usage:
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
45
|
+
content: s.content || "",
|
|
46
|
+
thinking: s.thinking || "",
|
|
47
|
+
fullContent: m,
|
|
48
|
+
fullThinking: f,
|
|
49
|
+
done: T,
|
|
50
|
+
usage: s.usage || null,
|
|
51
|
+
fullUsage: g,
|
|
52
|
+
finishReason: s.finishReason || null,
|
|
53
|
+
toolCallDelta: s.toolCallDelta || null,
|
|
54
|
+
toolCalls: C
|
|
55
|
+
}), T ? "done" : null;
|
|
54
56
|
};
|
|
55
57
|
for (; ; ) {
|
|
56
|
-
const { done:
|
|
57
|
-
if (
|
|
58
|
-
|
|
58
|
+
const { done: p, value: a } = await k.read();
|
|
59
|
+
if (p) {
|
|
60
|
+
u.length > 0 && R(u);
|
|
59
61
|
break;
|
|
60
62
|
}
|
|
61
|
-
|
|
62
|
-
let
|
|
63
|
-
for (; (
|
|
63
|
+
u += E.decode(a, { stream: !0 });
|
|
64
|
+
let s;
|
|
65
|
+
for (; (s = u.indexOf(`
|
|
64
66
|
`)) !== -1; ) {
|
|
65
|
-
const
|
|
66
|
-
if (
|
|
67
|
-
return { content:
|
|
67
|
+
const y = u.slice(0, s);
|
|
68
|
+
if (u = u.slice(s + 1), R(y) === "done")
|
|
69
|
+
return { content: m, thinking: f, toolCalls: b(), usage: g };
|
|
68
70
|
}
|
|
69
71
|
}
|
|
70
|
-
return { content:
|
|
71
|
-
} catch (
|
|
72
|
-
throw
|
|
72
|
+
return { content: m, thinking: f, toolCalls: b(), usage: g };
|
|
73
|
+
} catch (h) {
|
|
74
|
+
throw h.name === "AbortError" ? new Error("Request cancelled") : h;
|
|
73
75
|
} finally {
|
|
74
76
|
this.activeRequests.delete(l);
|
|
75
77
|
}
|
|
76
78
|
}
|
|
77
|
-
async makeRequest(e,
|
|
78
|
-
const
|
|
79
|
-
|
|
79
|
+
async makeRequest(e, t, n) {
|
|
80
|
+
const r = t ? { signal: t } : new AbortController();
|
|
81
|
+
t || (n = n || this.generateRequestId(), this.activeRequests.set(n, r));
|
|
80
82
|
try {
|
|
81
|
-
const
|
|
83
|
+
const o = this.buildHeaders(), l = await fetch(this.getEndpoint(), {
|
|
82
84
|
method: "POST",
|
|
83
|
-
headers:
|
|
85
|
+
headers: o,
|
|
84
86
|
body: JSON.stringify(e),
|
|
85
|
-
signal:
|
|
87
|
+
signal: r.signal || t
|
|
86
88
|
});
|
|
87
89
|
if (!l.ok) {
|
|
88
|
-
const
|
|
89
|
-
throw new Error(`LLM API Error (${l.status}): ${
|
|
90
|
+
const h = await l.text();
|
|
91
|
+
throw new Error(`LLM API Error (${l.status}): ${h}`);
|
|
90
92
|
}
|
|
91
93
|
return l.json();
|
|
92
|
-
} catch (
|
|
93
|
-
throw
|
|
94
|
+
} catch (o) {
|
|
95
|
+
throw o.name === "AbortError" ? new Error("Request cancelled") : o;
|
|
94
96
|
} finally {
|
|
95
|
-
!
|
|
97
|
+
!t && n && this.activeRequests.delete(n);
|
|
96
98
|
}
|
|
97
99
|
}
|
|
98
|
-
async makeStreamingRequest(e,
|
|
99
|
-
const n = this.buildHeaders(),
|
|
100
|
+
async makeStreamingRequest(e, t) {
|
|
101
|
+
const n = this.buildHeaders(), r = await fetch(this.getStreamingEndpoint(), {
|
|
100
102
|
method: "POST",
|
|
101
103
|
headers: n,
|
|
102
104
|
body: JSON.stringify(e),
|
|
103
|
-
signal:
|
|
105
|
+
signal: t
|
|
104
106
|
});
|
|
105
|
-
if (!
|
|
106
|
-
const
|
|
107
|
-
throw new Error(`LLM API Error (${
|
|
107
|
+
if (!r.ok) {
|
|
108
|
+
const o = await r.text();
|
|
109
|
+
throw new Error(`LLM API Error (${r.status}): ${o}`);
|
|
108
110
|
}
|
|
109
|
-
return
|
|
111
|
+
return r;
|
|
110
112
|
}
|
|
111
113
|
buildHeaders() {
|
|
112
114
|
const e = { "Content-Type": "application/json" };
|
|
@@ -147,8 +149,8 @@ class q {
|
|
|
147
149
|
}
|
|
148
150
|
// Cancel specific request
|
|
149
151
|
cancelRequest(e) {
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
+
const t = this.activeRequests.get(e);
|
|
153
|
+
t && (t.abort(), this.activeRequests.delete(e));
|
|
152
154
|
}
|
|
153
155
|
// Check if provider has capability
|
|
154
156
|
hasCapability(e) {
|
|
@@ -157,17 +159,17 @@ class q {
|
|
|
157
159
|
// Discover available models (override in subclass if needed)
|
|
158
160
|
async discoverModels(e = 1e4) {
|
|
159
161
|
try {
|
|
160
|
-
const
|
|
162
|
+
const t = this.buildHeaders(), n = new AbortController(), r = setTimeout(() => n.abort(), e), o = await fetch(this.getModelsEndpoint(), {
|
|
161
163
|
method: "GET",
|
|
162
|
-
headers:
|
|
164
|
+
headers: t,
|
|
163
165
|
signal: n.signal
|
|
164
166
|
});
|
|
165
|
-
if (clearTimeout(
|
|
166
|
-
throw new Error(`Failed to fetch models: ${
|
|
167
|
-
const l = await
|
|
167
|
+
if (clearTimeout(r), !o.ok)
|
|
168
|
+
throw new Error(`Failed to fetch models: ${o.status} ${o.statusText}`);
|
|
169
|
+
const l = await o.json();
|
|
168
170
|
return this.parseModelsResponse(l);
|
|
169
|
-
} catch (
|
|
170
|
-
throw
|
|
171
|
+
} catch (t) {
|
|
172
|
+
throw t.name === "AbortError" ? new Error("Model discovery timeout - please check your connection") : t;
|
|
171
173
|
}
|
|
172
174
|
}
|
|
173
175
|
getModelsEndpoint() {
|
|
@@ -177,6 +179,14 @@ class q {
|
|
|
177
179
|
throw new Error("parseModelsResponse must be implemented by subclass");
|
|
178
180
|
}
|
|
179
181
|
}
|
|
182
|
+
function q(c, e) {
|
|
183
|
+
if (!e) return c;
|
|
184
|
+
if (!c) return { ...e };
|
|
185
|
+
const t = { ...c }, n = ["inputTokens", "outputTokens", "totalTokens", "cachedInputTokens", "cacheCreationInputTokens", "reasoningTokens"];
|
|
186
|
+
for (const r of n)
|
|
187
|
+
e[r] != null && (t[r] = c[r] != null ? Math.max(c[r], e[r]) : e[r]);
|
|
188
|
+
return e.raw && (t.raw = e.raw), t.totalTokens == null && t.inputTokens != null && t.outputTokens != null && (t.totalTokens = t.inputTokens + t.outputTokens), t;
|
|
189
|
+
}
|
|
180
190
|
export {
|
|
181
|
-
|
|
191
|
+
A as BaseProvider
|
|
182
192
|
};
|
|
@@ -1,33 +1,66 @@
|
|
|
1
|
-
import { BaseProvider as
|
|
2
|
-
|
|
1
|
+
import { BaseProvider as d } from "./BaseProvider.js";
|
|
2
|
+
import { convertMessagesToOpenAI as g, normalizeOpenAIUsage as u } from "./OpenAIProvider.js";
|
|
3
|
+
class A extends d {
|
|
3
4
|
async detectCapabilities() {
|
|
5
|
+
this.capabilities.add("tools");
|
|
4
6
|
}
|
|
5
|
-
prepareRequest(
|
|
6
|
-
|
|
7
|
-
model:
|
|
8
|
-
messages:
|
|
9
|
-
temperature:
|
|
10
|
-
max_tokens:
|
|
11
|
-
stream:
|
|
7
|
+
prepareRequest(n, e) {
|
|
8
|
+
const t = {
|
|
9
|
+
model: e.model || this.config.model || "gpt-3.5-turbo",
|
|
10
|
+
messages: g(n),
|
|
11
|
+
temperature: e.temperature ?? 0.7,
|
|
12
|
+
max_tokens: e.maxTokens || 1e3,
|
|
13
|
+
stream: e.stream || !1
|
|
12
14
|
};
|
|
15
|
+
return t.stream && (t.stream_options = { include_usage: !0 }), e.tools && this.capabilities.has("tools") && (t.tools = e.tools, e.tool_choice && (t.tool_choice = e.tool_choice)), t;
|
|
13
16
|
}
|
|
14
|
-
processResponse(
|
|
15
|
-
var
|
|
16
|
-
|
|
17
|
+
processResponse(n) {
|
|
18
|
+
var s, o, i, a;
|
|
19
|
+
const e = (o = (s = n.choices) == null ? void 0 : s[0]) == null ? void 0 : o.message, t = {
|
|
20
|
+
content: (e == null ? void 0 : e.content) || "",
|
|
21
|
+
usage: u(n.usage),
|
|
22
|
+
finishReason: c((a = (i = n.choices) == null ? void 0 : i[0]) == null ? void 0 : a.finish_reason)
|
|
23
|
+
};
|
|
24
|
+
return Array.isArray(e == null ? void 0 : e.tool_calls) && (t.toolCalls = e.tool_calls.map((l) => {
|
|
25
|
+
var f, m;
|
|
26
|
+
return {
|
|
27
|
+
id: l.id,
|
|
28
|
+
name: (f = l.function) == null ? void 0 : f.name,
|
|
29
|
+
args: h((m = l.function) == null ? void 0 : m.arguments)
|
|
30
|
+
};
|
|
31
|
+
})), t;
|
|
17
32
|
}
|
|
18
|
-
parseStreamingLine(
|
|
19
|
-
if (!
|
|
20
|
-
const
|
|
21
|
-
if (
|
|
33
|
+
parseStreamingLine(n) {
|
|
34
|
+
if (!n.startsWith("data: ")) return null;
|
|
35
|
+
const e = n.slice(6).trim();
|
|
36
|
+
if (e === "[DONE]") return { done: !0 };
|
|
22
37
|
try {
|
|
23
|
-
return JSON.parse(
|
|
38
|
+
return JSON.parse(e);
|
|
24
39
|
} catch {
|
|
25
40
|
return null;
|
|
26
41
|
}
|
|
27
42
|
}
|
|
28
|
-
extractStreamingContent(
|
|
29
|
-
var
|
|
30
|
-
|
|
43
|
+
extractStreamingContent(n) {
|
|
44
|
+
var s, o, i;
|
|
45
|
+
if (n.done) return { done: !0 };
|
|
46
|
+
const e = (s = n.choices) == null ? void 0 : s[0], t = e == null ? void 0 : e.delta;
|
|
47
|
+
if (t && Array.isArray(t.tool_calls) && t.tool_calls.length) {
|
|
48
|
+
const a = t.tool_calls[0];
|
|
49
|
+
return {
|
|
50
|
+
content: t.content || "",
|
|
51
|
+
thinking: "",
|
|
52
|
+
done: !1,
|
|
53
|
+
usage: u(n.usage),
|
|
54
|
+
finishReason: c(e == null ? void 0 : e.finish_reason),
|
|
55
|
+
toolCallDelta: {
|
|
56
|
+
index: a.index ?? 0,
|
|
57
|
+
id: a.id || void 0,
|
|
58
|
+
name: ((o = a.function) == null ? void 0 : o.name) || void 0,
|
|
59
|
+
argsTextDelta: ((i = a.function) == null ? void 0 : i.arguments) || ""
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return { content: (t == null ? void 0 : t.content) || "", thinking: "", done: !1, usage: u(n.usage), finishReason: c(e == null ? void 0 : e.finish_reason) };
|
|
31
64
|
}
|
|
32
65
|
getApiPath() {
|
|
33
66
|
return "/v1/chat/completions";
|
|
@@ -38,16 +71,24 @@ class l extends u {
|
|
|
38
71
|
getModelsEndpoint() {
|
|
39
72
|
return `${this.config.baseUrl}/v1/models`;
|
|
40
73
|
}
|
|
41
|
-
parseModelsResponse(
|
|
42
|
-
var
|
|
43
|
-
return ((
|
|
74
|
+
parseModelsResponse(n) {
|
|
75
|
+
var e, t;
|
|
76
|
+
return ((t = (e = n.data) == null ? void 0 : e.map((s) => s.id)) == null ? void 0 : t.sort()) || [];
|
|
44
77
|
}
|
|
45
78
|
}
|
|
46
|
-
function
|
|
47
|
-
if (!
|
|
48
|
-
const
|
|
49
|
-
return
|
|
79
|
+
function c(r) {
|
|
80
|
+
if (!r) return null;
|
|
81
|
+
const n = String(r).toLowerCase();
|
|
82
|
+
return n === "max_tokens" ? "length" : n;
|
|
83
|
+
}
|
|
84
|
+
function h(r) {
|
|
85
|
+
if (!r) return {};
|
|
86
|
+
try {
|
|
87
|
+
return JSON.parse(r);
|
|
88
|
+
} catch {
|
|
89
|
+
return { __parseError: !0, raw: r };
|
|
90
|
+
}
|
|
50
91
|
}
|
|
51
92
|
export {
|
|
52
|
-
|
|
93
|
+
A as CustomProvider
|
|
53
94
|
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { BaseProvider as d } from "./BaseProvider.js";
|
|
2
|
+
import { convertMessagesToOpenAI as h } from "./OpenAIProvider.js";
|
|
3
|
+
class b extends d {
|
|
4
|
+
async detectCapabilities() {
|
|
5
|
+
if (!this.config.model) return;
|
|
6
|
+
const t = this.config.model.toLowerCase();
|
|
7
|
+
this.capabilities.add("tools"), (t.includes("reasoner") || t.includes("reasoning")) && this.capabilities.add("thinking");
|
|
8
|
+
}
|
|
9
|
+
prepareRequest(t, e) {
|
|
10
|
+
const n = {
|
|
11
|
+
model: e.model || this.config.model || "deepseek-chat",
|
|
12
|
+
messages: _(t),
|
|
13
|
+
temperature: e.temperature ?? 0.7,
|
|
14
|
+
max_tokens: e.maxTokens || 1e3,
|
|
15
|
+
stream: e.stream || !1
|
|
16
|
+
};
|
|
17
|
+
return n.stream && (n.stream_options = { include_usage: !0 }), e.tools && this.capabilities.has("tools") && (n.tools = e.tools, e.tool_choice && (n.tool_choice = e.tool_choice)), e.enableThinking && this.capabilities.has("thinking") && (n.thinking = { type: "enabled" }), n;
|
|
18
|
+
}
|
|
19
|
+
processResponse(t) {
|
|
20
|
+
var s, i, a, r;
|
|
21
|
+
const e = (i = (s = t.choices) == null ? void 0 : s[0]) == null ? void 0 : i.message, n = {
|
|
22
|
+
content: (e == null ? void 0 : e.content) || "",
|
|
23
|
+
usage: u(t.usage),
|
|
24
|
+
finishReason: l((r = (a = t.choices) == null ? void 0 : a[0]) == null ? void 0 : r.finish_reason)
|
|
25
|
+
};
|
|
26
|
+
return e != null && e.reasoning_content && (n.thinking = e.reasoning_content), Array.isArray(e == null ? void 0 : e.tool_calls) && (n.toolCalls = e.tool_calls.map((c) => {
|
|
27
|
+
var f, g;
|
|
28
|
+
return {
|
|
29
|
+
id: c.id,
|
|
30
|
+
name: (f = c.function) == null ? void 0 : f.name,
|
|
31
|
+
args: p((g = c.function) == null ? void 0 : g.arguments)
|
|
32
|
+
};
|
|
33
|
+
})), n;
|
|
34
|
+
}
|
|
35
|
+
parseStreamingLine(t) {
|
|
36
|
+
if (!t.startsWith("data: ")) return null;
|
|
37
|
+
const e = t.slice(6).trim();
|
|
38
|
+
if (e === "[DONE]") return { done: !0 };
|
|
39
|
+
try {
|
|
40
|
+
return JSON.parse(e);
|
|
41
|
+
} catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
extractStreamingContent(t) {
|
|
46
|
+
var s, i, a;
|
|
47
|
+
if (t.done) return { done: !0 };
|
|
48
|
+
const e = (s = t.choices) == null ? void 0 : s[0], n = e == null ? void 0 : e.delta;
|
|
49
|
+
if (n && Array.isArray(n.tool_calls) && n.tool_calls.length) {
|
|
50
|
+
const r = n.tool_calls[0];
|
|
51
|
+
return {
|
|
52
|
+
content: n.content || "",
|
|
53
|
+
thinking: n.reasoning_content || "",
|
|
54
|
+
done: !1,
|
|
55
|
+
usage: u(t.usage),
|
|
56
|
+
finishReason: l(e == null ? void 0 : e.finish_reason),
|
|
57
|
+
toolCallDelta: {
|
|
58
|
+
index: r.index ?? 0,
|
|
59
|
+
id: r.id || void 0,
|
|
60
|
+
name: ((i = r.function) == null ? void 0 : i.name) || void 0,
|
|
61
|
+
argsTextDelta: ((a = r.function) == null ? void 0 : a.arguments) || ""
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
content: (n == null ? void 0 : n.content) || "",
|
|
67
|
+
thinking: (n == null ? void 0 : n.reasoning_content) || "",
|
|
68
|
+
done: !1,
|
|
69
|
+
usage: u(t.usage),
|
|
70
|
+
finishReason: l(e == null ? void 0 : e.finish_reason)
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
getApiPath() {
|
|
74
|
+
return "/chat/completions";
|
|
75
|
+
}
|
|
76
|
+
requiresAuth() {
|
|
77
|
+
return !!this.config.apiKey;
|
|
78
|
+
}
|
|
79
|
+
getModelsEndpoint() {
|
|
80
|
+
return `${this.config.baseUrl}/models`;
|
|
81
|
+
}
|
|
82
|
+
parseModelsResponse(t) {
|
|
83
|
+
var e, n, s;
|
|
84
|
+
return ((s = (n = (e = t.data) == null ? void 0 : e.filter((i) => String(i.id).toLowerCase().includes("deepseek"))) == null ? void 0 : n.map((i) => i.id)) == null ? void 0 : s.sort()) || [];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function l(o) {
|
|
88
|
+
if (!o) return null;
|
|
89
|
+
const t = String(o).toLowerCase();
|
|
90
|
+
return t === "length" || t === "max_tokens" ? "length" : t;
|
|
91
|
+
}
|
|
92
|
+
function p(o) {
|
|
93
|
+
if (!o) return {};
|
|
94
|
+
try {
|
|
95
|
+
return JSON.parse(o);
|
|
96
|
+
} catch {
|
|
97
|
+
return { __parseError: !0, raw: o };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function u(o) {
|
|
101
|
+
if (!o) return null;
|
|
102
|
+
const t = o.prompt_tokens ?? 0, e = o.completion_tokens ?? 0, n = o.total_tokens ?? t + e, s = { inputTokens: t, outputTokens: e, totalTokens: n, raw: o };
|
|
103
|
+
return o.prompt_cache_hit_tokens != null && (s.cachedInputTokens = o.prompt_cache_hit_tokens), s;
|
|
104
|
+
}
|
|
105
|
+
function _(o) {
|
|
106
|
+
return h(o).map((e, n) => {
|
|
107
|
+
if (e.role !== "assistant") return e;
|
|
108
|
+
const s = o[n] || {}, i = s.thinking || s.reasoning_content, { thinking: a, reasoning_content: r, ...c } = e;
|
|
109
|
+
return i && (c.reasoning_content = i), c;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
export {
|
|
113
|
+
b as DeepSeekProvider,
|
|
114
|
+
u as normalizeDeepSeekUsage
|
|
115
|
+
};
|