@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.
@@ -1,18 +1,19 @@
1
- import { BaseProvider as f } from "./BaseProvider.js";
2
- class m extends f {
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 = h(e), o = {
9
- model: t.model || this.config.model || "claude-3-sonnet-20240229",
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
- }, s = e.find((l) => l.role === "system");
15
- return s && (o.system = s.content), t.tools && this.capabilities.has("tools") && (o.tools = p(t.tools), t.tool_choice && (o.tool_choice = t.tool_choice)), o;
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((s) => {
25
+ t.forEach((i) => {
25
26
  o.push({
26
27
  type: "image",
27
- source: { type: "base64", media_type: "image/jpeg", data: typeof s == "string" ? s : s.data }
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 = u(e.stop_reason), n = e.content || [], o = n.find((r) => r.type === "text"), l = n.filter((r) => r.type === "tool_use").map((r) => ({ id: r.id, name: r.name, args: r.input || {} }));
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 || null,
38
+ usage: d(e.usage),
38
39
  finishReason: t,
39
- toolCalls: l
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, s, l, r, d;
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", c = new Error(((n = e.error) == null ? void 0 : n.message) || "Anthropic streaming error");
57
- throw c.code = a, e.request_id && (c.requestId = e.request_id), c;
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" ? ((o = e.delta) == null ? void 0 : o.type) === "text_delta" ? { content: ((s = e.delta) == null ? void 0 : s.text) || "", thinking: "", done: !1, usage: null, finishReason: null } : ((l = e.delta) == null ? void 0 : l.type) === "input_json_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: ((r = e.delta) == null ? void 0 : r.partial_json) || "" }
71
- } : null : e.type === "message_delta" ? { content: "", thinking: "", done: !1, usage: e.usage ? { tokens: e.usage.output_tokens || 0 } : null, finishReason: u((d = e.delta) == null ? void 0 : d.stop_reason) } : e.type === "message_stop" ? { content: "", thinking: "", done: !0, usage: null, finishReason: u(e.stop_reason) } : null;
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 u(i) {
98
- if (!i) return null;
99
- const e = String(i).toLowerCase();
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 h(i) {
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 i)
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 p(i) {
135
- return i.map((e) => e.type === "function" && e.function ? {
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
- m as AnthropicProvider
150
+ y as AnthropicProvider,
151
+ d as normalizeAnthropicUsage
143
152
  };
@@ -1,4 +1,4 @@
1
- class q {
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, r) {
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, r, n) {
17
- const a = this.prepareRequest(e, r), s = new AbortController(), l = r.requestId || this.generateRequestId();
18
- this.activeRequests.set(l, s);
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 y = (await this.makeStreamingRequest(a, s.signal)).body.getReader(), R = new TextDecoder();
21
- let p = "", w = "", c = "";
22
- const g = /* @__PURE__ */ new Map(), f = () => Array.from(g.values()).sort((o, t) => o.index - t.index).map((o) => {
23
- let t = {};
24
- if (o.argsText)
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
- t = JSON.parse(o.argsText);
26
+ s = JSON.parse(a.argsText);
27
27
  } catch {
28
- t = { __parseError: !0, raw: o.argsText };
28
+ s = { __parseError: !0, raw: a.argsText };
29
29
  }
30
- return { id: o.id, name: o.name, args: t };
31
- }), b = (h) => {
32
- if (!h.trim()) return null;
33
- const o = this.parseStreamingLine(h);
34
- if (!o) return null;
35
- const t = this.extractStreamingContent(o);
36
- if (!t) return null;
37
- if (t.content && (p += t.content), t.thinking && (w += t.thinking), t.toolCallDelta) {
38
- const i = t.toolCallDelta;
39
- let u = g.get(i.index);
40
- u ? (i.id && (u.id = i.id), i.name && (u.name = i.name)) : (u = { index: i.index, id: i.id || null, name: i.name || "", argsText: "" }, g.set(i.index, u)), i.argsTextDelta && (u.argsText += i.argsTextDelta);
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 m = t.done || !1, E = m ? f() : null;
43
+ const T = s.done || !1, C = T ? b() : null;
43
44
  return n({
44
- content: t.content || "",
45
- thinking: t.thinking || "",
46
- fullContent: p,
47
- fullThinking: w,
48
- done: m,
49
- usage: t.usage || null,
50
- finishReason: t.finishReason || null,
51
- toolCallDelta: t.toolCallDelta || null,
52
- toolCalls: E
53
- }), m ? "done" : null;
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: h, value: o } = await y.read();
57
- if (h) {
58
- c.length > 0 && b(c);
58
+ const { done: p, value: a } = await k.read();
59
+ if (p) {
60
+ u.length > 0 && R(u);
59
61
  break;
60
62
  }
61
- c += R.decode(o, { stream: !0 });
62
- let t;
63
- for (; (t = c.indexOf(`
63
+ u += E.decode(a, { stream: !0 });
64
+ let s;
65
+ for (; (s = u.indexOf(`
64
66
  `)) !== -1; ) {
65
- const m = c.slice(0, t);
66
- if (c = c.slice(t + 1), b(m) === "done")
67
- return { content: p, toolCalls: f() };
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: p, toolCalls: f() };
71
- } catch (d) {
72
- throw d.name === "AbortError" ? new Error("Request cancelled") : d;
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, r, n) {
78
- const a = r ? { signal: r } : new AbortController();
79
- r || (n = n || this.generateRequestId(), this.activeRequests.set(n, a));
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 s = this.buildHeaders(), l = await fetch(this.getEndpoint(), {
83
+ const o = this.buildHeaders(), l = await fetch(this.getEndpoint(), {
82
84
  method: "POST",
83
- headers: s,
85
+ headers: o,
84
86
  body: JSON.stringify(e),
85
- signal: a.signal || r
87
+ signal: r.signal || t
86
88
  });
87
89
  if (!l.ok) {
88
- const d = await l.text();
89
- throw new Error(`LLM API Error (${l.status}): ${d}`);
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 (s) {
93
- throw s.name === "AbortError" ? new Error("Request cancelled") : s;
94
+ } catch (o) {
95
+ throw o.name === "AbortError" ? new Error("Request cancelled") : o;
94
96
  } finally {
95
- !r && n && this.activeRequests.delete(n);
97
+ !t && n && this.activeRequests.delete(n);
96
98
  }
97
99
  }
98
- async makeStreamingRequest(e, r) {
99
- const n = this.buildHeaders(), a = await fetch(this.getStreamingEndpoint(), {
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: r
105
+ signal: t
104
106
  });
105
- if (!a.ok) {
106
- const s = await a.text();
107
- throw new Error(`LLM API Error (${a.status}): ${s}`);
107
+ if (!r.ok) {
108
+ const o = await r.text();
109
+ throw new Error(`LLM API Error (${r.status}): ${o}`);
108
110
  }
109
- return a;
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 r = this.activeRequests.get(e);
151
- r && (r.abort(), this.activeRequests.delete(e));
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 r = this.buildHeaders(), n = new AbortController(), a = setTimeout(() => n.abort(), e), s = await fetch(this.getModelsEndpoint(), {
162
+ const t = this.buildHeaders(), n = new AbortController(), r = setTimeout(() => n.abort(), e), o = await fetch(this.getModelsEndpoint(), {
161
163
  method: "GET",
162
- headers: r,
164
+ headers: t,
163
165
  signal: n.signal
164
166
  });
165
- if (clearTimeout(a), !s.ok)
166
- throw new Error(`Failed to fetch models: ${s.status} ${s.statusText}`);
167
- const l = await s.json();
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 (r) {
170
- throw r.name === "AbortError" ? new Error("Model discovery timeout - please check your connection") : r;
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
- q as BaseProvider
191
+ A as BaseProvider
182
192
  };
@@ -1,33 +1,66 @@
1
- import { BaseProvider as u } from "./BaseProvider.js";
2
- class l extends u {
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(e, t) {
6
- return {
7
- model: t.model || this.config.model || "gpt-3.5-turbo",
8
- messages: e,
9
- temperature: t.temperature ?? 0.7,
10
- max_tokens: t.maxTokens || 1e3,
11
- stream: t.stream || !1
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(e) {
15
- var t, n, r, s, a;
16
- return { content: ((r = (n = (t = e.choices) == null ? void 0 : t[0]) == null ? void 0 : n.message) == null ? void 0 : r.content) || "", usage: e.usage || null, finishReason: o((a = (s = e.choices) == null ? void 0 : s[0]) == null ? void 0 : a.finish_reason) };
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(e) {
19
- if (!e.startsWith("data: ")) return null;
20
- const t = e.slice(6).trim();
21
- if (t === "[DONE]") return { done: !0 };
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(t);
38
+ return JSON.parse(e);
24
39
  } catch {
25
40
  return null;
26
41
  }
27
42
  }
28
- extractStreamingContent(e) {
29
- var t, n, r, s, a;
30
- return e.done ? { done: !0 } : { content: ((r = (n = (t = e.choices) == null ? void 0 : t[0]) == null ? void 0 : n.delta) == null ? void 0 : r.content) || "", thinking: "", done: !1, usage: e.usage || null, finishReason: o((a = (s = e.choices) == null ? void 0 : s[0]) == null ? void 0 : a.finish_reason) };
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(e) {
42
- var t, n;
43
- return ((n = (t = e.data) == null ? void 0 : t.map((r) => r.id)) == null ? void 0 : n.sort()) || [];
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 o(i) {
47
- if (!i) return null;
48
- const e = String(i).toLowerCase();
49
- return e === "max_tokens" ? "length" : e;
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
- l as CustomProvider
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
+ };