@anvaka/vue-llm 0.2.0 → 0.3.1

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.
@@ -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
+ };
@@ -1,14 +1,17 @@
1
- import { BaseProvider as A } from "./BaseProvider.js";
2
- class _ extends A {
1
+ import { BaseProvider as C } from "./BaseProvider.js";
2
+ class A extends C {
3
3
  async detectCapabilities() {
4
- this.config.model && ((this.config.model.includes("gemini-pro-vision") || this.config.model.includes("gemini-1.5") || this.config.model.includes("gemini-2.0")) && this.capabilities.add("vision"), (this.config.model.includes("gemini-pro") || this.config.model.includes("gemini-1.5") || this.config.model.includes("gemini-2.0")) && this.capabilities.add("tools"), this.config.model.includes("gemini-2.0") && this.capabilities.add("thinking"));
5
- }
6
- prepareRequest(e, t) {
7
- const n = this.processMessages(e, t), s = {
8
- contents: this.convertToGeminiFormat(n),
4
+ const t = this.config.model;
5
+ if (!t) return;
6
+ const e = t.match(/gemini-(\d+)\.(\d+)/), s = e ? Number(e[1]) : null, n = e ? Number(e[2]) : null, i = (a, o) => s != null && (s > a || s === a && n >= o);
7
+ (t.includes("gemini-pro-vision") || i(1, 5)) && this.capabilities.add("vision"), (t.includes("gemini-pro") || i(1, 5)) && this.capabilities.add("tools"), i(2, 0) && this.capabilities.add("thinking");
8
+ }
9
+ prepareRequest(t, e) {
10
+ const s = this.processMessages(t, e), n = {
11
+ contents: this.convertToGeminiFormat(s),
9
12
  generationConfig: {
10
- temperature: t.temperature ?? this.config.temperature ?? 0.7,
11
- maxOutputTokens: t.maxTokens || this.config.maxTokens || 1e3,
13
+ temperature: e.temperature ?? this.config.temperature ?? 0.7,
14
+ maxOutputTokens: e.maxTokens || this.config.maxTokens || 1e3,
12
15
  topP: 0.8,
13
16
  topK: 10
14
17
  },
@@ -18,117 +21,130 @@ class _ extends A {
18
21
  { category: "HARM_CATEGORY_SEXUALLY_EXPLICIT", threshold: "BLOCK_NONE" },
19
22
  { category: "HARM_CATEGORY_DANGEROUS_CONTENT", threshold: "BLOCK_NONE" }
20
23
  ]
21
- }, i = e.find((o) => o.role === "system");
22
- return i && (s.systemInstruction = { parts: [{ text: i.content }] }), t.tools && this.capabilities.has("tools") && (s.tools = this.convertToolsToGeminiFormat(t.tools)), s;
24
+ }, i = t.find((a) => a.role === "system");
25
+ return i && (n.systemInstruction = { parts: [{ text: i.content }] }), e.tools && this.capabilities.has("tools") && (n.tools = this.convertToolsToGeminiFormat(e.tools)), n;
26
+ }
27
+ // Build a {tool_call_id -> function name} index from prior assistant
28
+ // messages so tool-result messages can be reshaped into Gemini's
29
+ // functionResponse (which requires the function name to match).
30
+ buildToolCallNameIndex(t) {
31
+ const e = /* @__PURE__ */ new Map();
32
+ for (const s of t)
33
+ if (s.role === "assistant" && Array.isArray(s.tool_calls))
34
+ for (const n of s.tool_calls)
35
+ n.id && e.set(n.id, n.name);
36
+ return e;
23
37
  }
24
- convertToGeminiFormat(e) {
25
- const t = [];
26
- for (const n of e) {
38
+ convertToGeminiFormat(t) {
39
+ const e = this.buildToolCallNameIndex(t), s = [];
40
+ for (const n of t) {
27
41
  if (n.role === "system") continue;
28
- const s = n.role === "assistant" ? "model" : "user";
42
+ if (n.role === "tool") {
43
+ const a = n.name || e.get(n.tool_call_id) || "unknown";
44
+ let o = n.content;
45
+ if (typeof o == "string")
46
+ try {
47
+ const l = JSON.parse(o);
48
+ o = l !== null && typeof l == "object" && !Array.isArray(l) ? l : { result: l };
49
+ } catch {
50
+ o = { result: n.content };
51
+ }
52
+ else (o === null || typeof o != "object" || Array.isArray(o)) && (o = { result: o });
53
+ s.push({
54
+ role: "user",
55
+ parts: [{ functionResponse: { name: a, response: o } }]
56
+ });
57
+ continue;
58
+ }
59
+ if (n.role === "assistant" && Array.isArray(n.tool_calls) && n.tool_calls.length) {
60
+ const a = [];
61
+ n.content && a.push({ text: String(n.content) });
62
+ for (const o of n.tool_calls)
63
+ a.push({ functionCall: { name: o.name, args: typeof o.args == "string" ? T(o.args) : o.args || {} } });
64
+ s.push({ role: "model", parts: a });
65
+ continue;
66
+ }
67
+ const i = n.role === "assistant" ? "model" : "user";
29
68
  if (Array.isArray(n.content)) {
30
- const i = n.content.map((o) => {
31
- var a, l;
32
- return o.type === "text" ? { text: o.text } : o.type === "image" ? { inlineData: { mimeType: ((a = o.source) == null ? void 0 : a.media_type) || "image/jpeg", data: ((l = o.source) == null ? void 0 : l.data) || o.data } } : o.type === "image_url" ? { inlineData: { mimeType: "image/jpeg", data: o.image_url.url.split(",")[1] } } : { text: String(o) };
69
+ const a = n.content.map((o) => {
70
+ var l, u;
71
+ return o.type === "text" ? { text: o.text } : o.type === "image" ? { inlineData: { mimeType: ((l = o.source) == null ? void 0 : l.media_type) || "image/jpeg", data: ((u = o.source) == null ? void 0 : u.data) || o.data } } : o.type === "image_url" ? { inlineData: { mimeType: "image/jpeg", data: o.image_url.url.split(",")[1] } } : { text: String(o) };
33
72
  });
34
- t.push({ role: s, parts: i });
73
+ s.push({ role: i, parts: a });
35
74
  } else
36
- t.push({ role: s, parts: [{ text: n.content }] });
75
+ s.push({ role: i, parts: [{ text: n.content }] });
37
76
  }
38
- return t;
77
+ return s;
39
78
  }
40
- processMessages(e, t) {
41
- return t.images && this.capabilities.has("vision") ? this.addImagesToMessages(e, t.images) : e;
79
+ processMessages(t, e) {
80
+ return e.images && this.capabilities.has("vision") ? this.addImagesToMessages(t, e.images) : t;
42
81
  }
43
- addImagesToMessages(e, t) {
44
- const n = e[e.length - 1];
45
- if (n && n.role === "user") {
46
- const s = [{ type: "text", text: n.content }];
47
- t.forEach((i) => s.push({ type: "image", data: typeof i == "string" ? i : i.data, mimeType: "image/jpeg" })), n.content = s;
82
+ addImagesToMessages(t, e) {
83
+ const s = t[t.length - 1];
84
+ if (s && s.role === "user") {
85
+ const n = [{ type: "text", text: s.content }];
86
+ e.forEach((i) => n.push({ type: "image", data: typeof i == "string" ? i : i.data, mimeType: "image/jpeg" })), s.content = n;
48
87
  }
49
- return e;
88
+ return t;
50
89
  }
51
- convertToolsToGeminiFormat(e) {
52
- return e.map((t) => ({ functionDeclarations: [{ name: t.function.name, description: t.function.description, parameters: t.function.parameters }] }));
53
- }
54
- processResponse(e) {
55
- var n, s;
56
- const t = { content: "", usage: null, finishReason: null };
57
- if (e.candidates && e.candidates.length > 0) {
58
- const i = e.candidates[0];
59
- if ((n = i.content) != null && n.parts && (t.content = i.content.parts.filter((o) => o.text).map((o) => o.text).join("")), (s = i.content) != null && s.parts) {
60
- const o = i.content.parts.filter((a) => a.functionCall);
61
- o.length > 0 && (t.functionCalls = o);
62
- }
63
- i.finishReason && (t.finishReason = k(i.finishReason));
90
+ convertToolsToGeminiFormat(t) {
91
+ return t.map((e) => ({ functionDeclarations: [{ name: e.function.name, description: e.function.description, parameters: e.function.parameters }] }));
92
+ }
93
+ processResponse(t) {
94
+ var s;
95
+ const e = { content: "", usage: null, finishReason: null };
96
+ if (t.candidates && t.candidates.length > 0) {
97
+ const n = t.candidates[0], i = ((s = n.content) == null ? void 0 : s.parts) || [];
98
+ e.content = i.filter((o) => o.text).map((o) => o.text).join("");
99
+ const a = i.filter((o) => o.functionCall).map((o) => o.functionCall);
100
+ a.length && (e.toolCalls = a.map((o, l) => ({
101
+ id: p(l),
102
+ name: o.name,
103
+ args: o.args || {}
104
+ }))), n.finishReason && (e.finishReason = h(n.finishReason));
64
105
  }
65
- return e.usageMetadata && (t.usage = {
66
- promptTokens: e.usageMetadata.promptTokenCount,
67
- completionTokens: e.usageMetadata.candidatesTokenCount,
68
- totalTokens: e.usageMetadata.totalTokenCount
69
- }), t;
70
- }
71
- async streamRequest(e, t, n) {
72
- const s = this.prepareRequest(e, t), i = new AbortController(), o = t.requestId || this.generateRequestId();
73
- this.activeRequests.set(o, i);
106
+ return e.usage = y(t.usageMetadata), e;
107
+ }
108
+ parseStreamingLine(t) {
109
+ if (!t.startsWith("data: ")) return null;
110
+ const e = t.slice(6).trim();
111
+ if (!e) return null;
74
112
  try {
75
- const l = (await this.makeStreamingRequest(s, i.signal)).body.getReader(), m = new TextDecoder();
76
- let c = "", R = "", u = "", f = 0, d = 0, h = !1, C = !1;
77
- for (; ; ) {
78
- const { done: y, value: M } = await l.read();
79
- if (y) break;
80
- u += m.decode(M, { stream: !0 });
81
- for (let g = d; g < u.length; g++) {
82
- const p = u[g];
83
- if (C) {
84
- C = !1;
85
- continue;
86
- }
87
- if (p === "\\" && h) {
88
- C = !0;
89
- continue;
90
- }
91
- if (p === '"') {
92
- h = !h;
93
- continue;
94
- }
95
- if (!h) {
96
- if (p === "{")
97
- f === 0 && (d = g), f++;
98
- else if (p === "}" && (f--, f === 0)) {
99
- const x = u.slice(d, g + 1);
100
- try {
101
- const b = JSON.parse(x), r = this.extractStreamingContent(b);
102
- if (r && (r.content && (c += r.content), r.thinking && (R += r.thinking), n({ content: r.content || "", thinking: r.thinking || "", fullContent: c, fullThinking: R, done: r.done || !1, usage: r.usage || null, finishReason: r.finishReason || null }), r.done))
103
- return c;
104
- } catch {
105
- }
106
- d = g + 1;
107
- }
108
- }
109
- }
110
- f === 0 && d < u.length && (u = u.slice(d), d = 0);
111
- }
112
- return c;
113
- } catch (a) {
114
- throw a.name === "AbortError" ? new Error("Request cancelled") : a;
115
- } finally {
116
- this.activeRequests.delete(o);
113
+ return JSON.parse(e);
114
+ } catch {
115
+ return null;
117
116
  }
118
117
  }
119
- extractStreamingContent(e) {
120
- var a, l, m;
121
- if (!((a = e == null ? void 0 : e.candidates) != null && a.length)) return null;
122
- const t = e.candidates[0];
123
- let n = "", s = "", i = !1, o = null;
124
- return (l = t.content) != null && l.parts && (n = t.content.parts.filter((c) => c.text).map((c) => c.text).join("")), t.finishReason && (i = !0, o = k(t.finishReason)), ((m = e.usageMetadata) == null ? void 0 : m.thoughtsTokenCount) > 0 && (s = `[Thinking: ${e.usageMetadata.thoughtsTokenCount} tokens]`), { content: n, thinking: s, done: i, usage: e.usageMetadata ? { promptTokens: e.usageMetadata.promptTokenCount, completionTokens: e.usageMetadata.candidatesTokenCount, totalTokens: e.usageMetadata.totalTokenCount } : null, finishReason: o };
118
+ extractStreamingContent(t) {
119
+ var m, d, f;
120
+ if (!((m = t == null ? void 0 : t.candidates) != null && m.length))
121
+ return { content: "", thinking: "", done: !1 };
122
+ const e = t.candidates[0];
123
+ let s = "", n = "", i = !1, a = null;
124
+ const o = ((d = e.content) == null ? void 0 : d.parts) || [];
125
+ s = o.filter((c) => c.text).map((c) => c.text).join("");
126
+ const l = o.filter((c) => c.functionCall).map((c) => c.functionCall);
127
+ e.finishReason && (i = !0, a = h(e.finishReason)), ((f = t.usageMetadata) == null ? void 0 : f.thoughtsTokenCount) > 0 && (n = `[Thinking: ${t.usageMetadata.thoughtsTokenCount} tokens]`);
128
+ const u = {
129
+ content: s,
130
+ thinking: n,
131
+ done: i,
132
+ usage: y(t.usageMetadata),
133
+ finishReason: a
134
+ };
135
+ return l.length && (u.toolCallDeltas = l.map((c, g) => ({
136
+ index: g,
137
+ id: p(g),
138
+ name: c.name || "",
139
+ argsTextDelta: JSON.stringify(c.args ?? {})
140
+ }))), u;
125
141
  }
126
142
  getApiPath() {
127
143
  return `/v1beta/models/${this.config.model || "gemini-pro"}:generateContent`;
128
144
  }
129
145
  getStreamingEndpoint() {
130
- const e = this.config.model || "gemini-pro";
131
- return `${this.config.baseUrl}/v1beta/models/${e}:streamGenerateContent`;
146
+ const t = this.config.model || "gemini-pro";
147
+ return `${this.config.baseUrl}/v1beta/models/${t}:streamGenerateContent?alt=sse`;
132
148
  }
133
149
  requiresAuth() {
134
150
  return !!this.config.apiKey;
@@ -145,19 +161,35 @@ class _ extends A {
145
161
  getModelsEndpoint() {
146
162
  return `${this.config.baseUrl}/v1beta/models`;
147
163
  }
148
- parseModelsResponse(e) {
149
- var t;
150
- return ((t = e.models) == null ? void 0 : t.filter((n) => {
164
+ parseModelsResponse(t) {
165
+ var e;
166
+ return ((e = t.models) == null ? void 0 : e.filter((s) => {
151
167
  var i;
152
- return n.name.toLowerCase().includes("gemini") && ((i = n.supportedGenerationMethods) == null ? void 0 : i.includes("generateContent"));
153
- }).map((n) => n.name.split("/").pop()).sort()) || [];
168
+ return s.name.toLowerCase().includes("gemini") && ((i = s.supportedGenerationMethods) == null ? void 0 : i.includes("generateContent"));
169
+ }).map((s) => s.name.split("/").pop()).sort()) || [];
154
170
  }
155
171
  }
156
- function k(T) {
157
- if (!T) return null;
158
- const e = String(T).toLowerCase();
159
- return e.includes("max") && e.includes("token") ? "length" : e;
172
+ function h(r) {
173
+ if (!r) return null;
174
+ const t = String(r).toLowerCase();
175
+ return t.includes("max") && t.includes("token") ? "length" : t;
176
+ }
177
+ function p(r) {
178
+ return `gemini_call_${r}`;
179
+ }
180
+ function y(r) {
181
+ if (!r) return null;
182
+ const t = r.promptTokenCount ?? 0, e = r.candidatesTokenCount ?? 0, s = r.totalTokenCount ?? t + e, n = { inputTokens: t, outputTokens: e, totalTokens: s, raw: r };
183
+ return r.cachedContentTokenCount != null && (n.cachedInputTokens = r.cachedContentTokenCount), r.thoughtsTokenCount != null && (n.reasoningTokens = r.thoughtsTokenCount), n;
184
+ }
185
+ function T(r) {
186
+ try {
187
+ return JSON.parse(r);
188
+ } catch {
189
+ return {};
190
+ }
160
191
  }
161
192
  export {
162
- _ as GeminiProvider
193
+ A as GeminiProvider,
194
+ y as normalizeGeminiUsage
163
195
  };
@@ -1,50 +1,79 @@
1
- import { BaseProvider as c } from "./BaseProvider.js";
2
- class u extends c {
1
+ import { BaseProvider as g } from "./BaseProvider.js";
2
+ import { convertMessagesToOpenAI as m, normalizeOpenAIUsage as u } from "./OpenAIProvider.js";
3
+ class y extends g {
3
4
  async detectCapabilities() {
4
5
  if (!this.config.model) return;
5
- const e = this.config.model.toLowerCase();
6
- (e.includes("grok-2") || e.includes("vision")) && this.capabilities.add("vision"), this.capabilities.add("tools");
6
+ const t = this.config.model.toLowerCase();
7
+ (t.includes("grok-2") || t.includes("vision")) && this.capabilities.add("vision"), this.capabilities.add("tools");
7
8
  }
8
- prepareRequest(e, t) {
9
+ prepareRequest(t, e) {
9
10
  const s = {
10
- model: t.model || this.config.model || "grok-beta",
11
- messages: this.processMessages(e, t),
12
- temperature: t.temperature ?? 0.7,
13
- max_tokens: t.maxTokens || 1e3,
14
- stream: t.stream || !1
11
+ model: e.model || this.config.model || "grok-beta",
12
+ messages: this.processMessages(t, e),
13
+ temperature: e.temperature ?? 0.7,
14
+ max_tokens: e.maxTokens || 1e3,
15
+ stream: e.stream || !1
15
16
  };
16
- return t.tools && this.capabilities.has("tools") && (s.tools = t.tools), s;
17
+ return s.stream && (s.stream_options = { include_usage: !0 }), e.tools && this.capabilities.has("tools") && (s.tools = e.tools, e.tool_choice && (s.tool_choice = e.tool_choice)), s;
17
18
  }
18
- processMessages(e, t) {
19
- return t.images && this.capabilities.has("vision") ? this.addImagesToMessages(e, t.images) : e;
19
+ processMessages(t, e) {
20
+ const s = m(t);
21
+ return e.images && this.capabilities.has("vision") ? this.addImagesToMessages(s, e.images) : s;
20
22
  }
21
- addImagesToMessages(e, t) {
22
- const s = e[e.length - 1];
23
+ addImagesToMessages(t, e) {
24
+ const s = t[t.length - 1];
23
25
  if (s && s.role === "user") {
24
26
  const n = [{ type: "text", text: s.content }];
25
- t.forEach((r) => n.push({ type: "image_url", image_url: { url: typeof r == "string" ? r : r.url } })), s.content = n;
27
+ e.forEach((r) => n.push({ type: "image_url", image_url: { url: typeof r == "string" ? r : r.url } })), s.content = n;
26
28
  }
27
- return e;
29
+ return t;
28
30
  }
29
- processResponse(e) {
30
- var t, s, n, r, i;
31
- return { content: ((n = (s = (t = e.choices) == null ? void 0 : t[0]) == null ? void 0 : s.message) == null ? void 0 : n.content) || "", usage: e.usage || null, finishReason: a((i = (r = e.choices) == null ? void 0 : r[0]) == null ? void 0 : i.finish_reason) };
31
+ processResponse(t) {
32
+ var n, r, i, o;
33
+ const e = (r = (n = t.choices) == null ? void 0 : n[0]) == null ? void 0 : r.message, s = {
34
+ content: (e == null ? void 0 : e.content) || "",
35
+ usage: u(t.usage),
36
+ finishReason: c((o = (i = t.choices) == null ? void 0 : i[0]) == null ? void 0 : o.finish_reason)
37
+ };
38
+ return Array.isArray(e == null ? void 0 : e.tool_calls) && (s.toolCalls = e.tool_calls.map((l) => {
39
+ var d, f;
40
+ return {
41
+ id: l.id,
42
+ name: (d = l.function) == null ? void 0 : d.name,
43
+ args: h((f = l.function) == null ? void 0 : f.arguments)
44
+ };
45
+ })), s;
32
46
  }
33
- parseStreamingLine(e) {
34
- if (!e.startsWith("data: ")) return null;
35
- const t = e.slice(6).trim();
36
- if (t === "[DONE]") return { done: !0 };
47
+ parseStreamingLine(t) {
48
+ if (!t.startsWith("data: ")) return null;
49
+ const e = t.slice(6).trim();
50
+ if (e === "[DONE]") return { done: !0 };
37
51
  try {
38
- return JSON.parse(t);
52
+ return JSON.parse(e);
39
53
  } catch {
40
54
  return null;
41
55
  }
42
56
  }
43
- extractStreamingContent(e) {
44
- var s, n, r, i;
45
- if (e.done) return { done: !0 };
46
- const t = (n = (s = e.choices) == null ? void 0 : s[0]) == null ? void 0 : n.delta;
47
- return { content: (t == null ? void 0 : t.content) || "", done: !1, usage: e.usage || null, finishReason: a((i = (r = e.choices) == null ? void 0 : r[0]) == null ? void 0 : i.finish_reason) };
57
+ extractStreamingContent(t) {
58
+ var n, r, i;
59
+ if (t.done) return { done: !0 };
60
+ const e = (n = t.choices) == null ? void 0 : n[0], s = e == null ? void 0 : e.delta;
61
+ if (s && Array.isArray(s.tool_calls) && s.tool_calls.length) {
62
+ const o = s.tool_calls[0];
63
+ return {
64
+ content: s.content || "",
65
+ done: !1,
66
+ usage: u(t.usage),
67
+ finishReason: c(e == null ? void 0 : e.finish_reason),
68
+ toolCallDelta: {
69
+ index: o.index ?? 0,
70
+ id: o.id || void 0,
71
+ name: ((r = o.function) == null ? void 0 : r.name) || void 0,
72
+ argsTextDelta: ((i = o.function) == null ? void 0 : i.arguments) || ""
73
+ }
74
+ };
75
+ }
76
+ return { content: (s == null ? void 0 : s.content) || "", done: !1, usage: u(t.usage), finishReason: c(e == null ? void 0 : e.finish_reason) };
48
77
  }
49
78
  getApiPath() {
50
79
  return "/v1/chat/completions";
@@ -55,16 +84,24 @@ class u extends c {
55
84
  getModelsEndpoint() {
56
85
  return `${this.config.baseUrl}/v1/models`;
57
86
  }
58
- parseModelsResponse(e) {
59
- var t, s, n;
60
- return ((n = (s = (t = e.data) == null ? void 0 : t.filter((r) => String(r.id).toLowerCase().includes("grok"))) == null ? void 0 : s.map((r) => r.id)) == null ? void 0 : n.sort()) || [];
87
+ parseModelsResponse(t) {
88
+ var e, s, n;
89
+ return ((n = (s = (e = t.data) == null ? void 0 : e.filter((r) => String(r.id).toLowerCase().includes("grok"))) == null ? void 0 : s.map((r) => r.id)) == null ? void 0 : n.sort()) || [];
61
90
  }
62
91
  }
63
- function a(o) {
64
- if (!o) return null;
65
- const e = String(o).toLowerCase();
66
- return e === "length" || e === "max_tokens" ? "length" : e;
92
+ function c(a) {
93
+ if (!a) return null;
94
+ const t = String(a).toLowerCase();
95
+ return t === "length" || t === "max_tokens" ? "length" : t;
96
+ }
97
+ function h(a) {
98
+ if (!a) return {};
99
+ try {
100
+ return JSON.parse(a);
101
+ } catch {
102
+ return { __parseError: !0, raw: a };
103
+ }
67
104
  }
68
105
  export {
69
- u as GrokProvider
106
+ y as GrokProvider
70
107
  };