@anvaka/vue-llm 0.1.2 → 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,33 +1,66 @@
1
- import { BaseProvider as l } from "./BaseProvider.js";
2
- class c extends l {
1
+ import { BaseProvider as m } from "./BaseProvider.js";
2
+ import { convertMessagesToOpenAI as g, normalizeOpenAIUsage as u } from "./OpenAIProvider.js";
3
+ class A extends m {
3
4
  async detectCapabilities() {
5
+ this.capabilities.add("tools");
4
6
  }
5
- prepareRequest(e, t) {
6
- return {
7
- model: t.model || this.config.model || "llama2",
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 || "llama2",
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, r, n, s, a;
16
- return { content: ((n = (r = (t = e.choices) == null ? void 0 : t[0]) == null ? void 0 : r.message) == null ? void 0 : n.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 r, o, i, a;
19
+ const e = (o = (r = n.choices) == null ? void 0 : r[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 d, f;
26
+ return {
27
+ id: l.id,
28
+ name: (d = l.function) == null ? void 0 : d.name,
29
+ args: h((f = l.function) == null ? void 0 : f.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, r, n, s, a;
30
- return e.done ? { done: !0 } : { content: ((n = (r = (t = e.choices) == null ? void 0 : t[0]) == null ? void 0 : r.delta) == null ? void 0 : n.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 r, o, i;
45
+ if (n.done) return { done: !0 };
46
+ const e = (r = n.choices) == null ? void 0 : r[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,19 +71,27 @@ class c extends l {
38
71
  getModelsEndpoint() {
39
72
  return `${this.config.baseUrl}/v1/models`;
40
73
  }
41
- parseModelsResponse(e) {
42
- var t;
43
- return ((t = e.data) == null ? void 0 : t.filter((r) => {
44
- const n = r.id.toLowerCase();
45
- return n.includes("mistral") || n.includes("llama") || n.includes("codellama") || n.includes(".gguf") || n.includes(".bin");
46
- }).map((r) => r.id).sort()) || [];
74
+ parseModelsResponse(n) {
75
+ var e;
76
+ return ((e = n.data) == null ? void 0 : e.filter((t) => {
77
+ const r = t.id.toLowerCase();
78
+ return r.includes("mistral") || r.includes("llama") || r.includes("codellama") || r.includes(".gguf") || r.includes(".bin");
79
+ }).map((t) => t.id).sort()) || [];
47
80
  }
48
81
  }
49
- function o(i) {
50
- if (!i) return null;
51
- const e = String(i).toLowerCase();
52
- return e === "max_tokens" ? "length" : e;
82
+ function c(s) {
83
+ if (!s) return null;
84
+ const n = String(s).toLowerCase();
85
+ return n === "max_tokens" ? "length" : n;
86
+ }
87
+ function h(s) {
88
+ if (!s) return {};
89
+ try {
90
+ return JSON.parse(s);
91
+ } catch {
92
+ return { __parseError: !0, raw: s };
93
+ }
53
94
  }
54
95
  export {
55
- c as LlamaServerProvider
96
+ A as LlamaServerProvider
56
97
  };
@@ -1,57 +1,86 @@
1
- import { BaseProvider as r } from "./BaseProvider.js";
2
- class l extends r {
1
+ import { BaseProvider as m } from "./BaseProvider.js";
2
+ class k extends m {
3
3
  async detectCapabilities() {
4
4
  if (this.config.model)
5
5
  try {
6
- const t = (await this.fetchModelInfo()).capabilities || [];
7
- t.includes("thinking") && this.capabilities.add("thinking"), t.includes("vision") && this.capabilities.add("vision"), t.includes("tools") && this.capabilities.add("tools");
8
- } catch (e) {
9
- console.warn("Ollama capability detection failed:", e);
6
+ const e = (await this.fetchModelInfo()).capabilities || [];
7
+ e.includes("thinking") && this.capabilities.add("thinking"), e.includes("vision") && this.capabilities.add("vision"), e.includes("tools") && this.capabilities.add("tools");
8
+ } catch (t) {
9
+ console.warn("Ollama capability detection failed:", t);
10
10
  }
11
11
  }
12
12
  async fetchModelInfo() {
13
- const e = await fetch(`${this.config.baseUrl}/api/show`, {
13
+ const t = await fetch(`${this.config.baseUrl}/api/show`, {
14
14
  method: "POST",
15
15
  headers: { "Content-Type": "application/json" },
16
16
  body: JSON.stringify({ name: this.config.model })
17
17
  });
18
- if (!e.ok) throw new Error(`Failed to fetch model info: ${e.status}`);
19
- return e.json();
18
+ if (!t.ok) throw new Error(`Failed to fetch model info: ${t.status}`);
19
+ return t.json();
20
20
  }
21
- prepareRequest(e, t) {
22
- const i = t.model || this.config.model;
23
- if (!i) throw new Error("Model must be specified for Ollama requests");
24
- const n = {
25
- model: i,
26
- messages: this.processMessages(e, t),
27
- stream: t.stream || !1,
28
- think: t.enableThinking || !1,
29
- options: { temperature: t.temperature ?? 0.7, num_predict: t.maxTokens || 1e3 }
21
+ prepareRequest(t, e) {
22
+ const s = e.model || this.config.model;
23
+ if (!s) throw new Error("Model must be specified for Ollama requests");
24
+ const i = {
25
+ model: s,
26
+ messages: this.processMessages(t, e),
27
+ stream: e.stream || !1,
28
+ think: e.enableThinking || !1,
29
+ options: { temperature: e.temperature ?? 0.7, num_predict: e.maxTokens || 1e3 }
30
30
  };
31
- return t.enableThinking && this.capabilities.has("thinking") && (n.options.enable_thinking = !0), n;
31
+ return e.enableThinking && this.capabilities.has("thinking") && (i.options.enable_thinking = !0), e.tools && this.capabilities.has("tools") && (i.tools = e.tools), i;
32
32
  }
33
- processMessages(e, t) {
34
- return t.images && this.capabilities.has("vision") ? this.addImagesToMessages(e, t.images) : e;
33
+ processMessages(t, e) {
34
+ const s = p(t);
35
+ return e.images && this.capabilities.has("vision") ? this.addImagesToMessages(s, e.images) : s;
35
36
  }
36
- addImagesToMessages(e, t) {
37
- const i = e[e.length - 1];
38
- return i && i.role === "user" && (i.images = t.map((n) => typeof n == "string" && n.startsWith("data:") ? n.split(",")[1] : n)), e;
37
+ addImagesToMessages(t, e) {
38
+ const s = t[t.length - 1];
39
+ return s && s.role === "user" && (s.images = e.map((i) => typeof i == "string" && i.startsWith("data:") ? i.split(",")[1] : i)), t;
39
40
  }
40
- processResponse(e) {
41
- var i;
42
- const t = { content: ((i = e.message) == null ? void 0 : i.content) || "", usage: e.eval_count ? { tokens: e.eval_count } : null, finishReason: a(e.finish_reason) };
43
- return e.thinking && (t.thinking = e.thinking), t;
41
+ processResponse(t) {
42
+ var i, a;
43
+ const e = {
44
+ content: ((i = t.message) == null ? void 0 : i.content) || "",
45
+ usage: g(t),
46
+ finishReason: f(t.finish_reason)
47
+ };
48
+ t.thinking && (e.thinking = t.thinking);
49
+ const s = (a = t.message) == null ? void 0 : a.tool_calls;
50
+ return Array.isArray(s) && s.length && (e.toolCalls = s.map((r, c) => {
51
+ var o, l;
52
+ return {
53
+ id: h(c),
54
+ name: (o = r.function) == null ? void 0 : o.name,
55
+ args: d((l = r.function) == null ? void 0 : l.arguments)
56
+ };
57
+ })), e;
44
58
  }
45
- parseStreamingLine(e) {
59
+ parseStreamingLine(t) {
46
60
  try {
47
- return JSON.parse(e);
61
+ return JSON.parse(t);
48
62
  } catch {
49
63
  return null;
50
64
  }
51
65
  }
52
- extractStreamingContent(e) {
53
- var t;
54
- return { content: ((t = e.message) == null ? void 0 : t.content) || "", thinking: e.thinking || "", done: e.done || !1, usage: e.eval_count ? { tokens: e.eval_count } : null, finishReason: a(e.finish_reason) };
66
+ extractStreamingContent(t) {
67
+ var i, a, r;
68
+ const e = {
69
+ content: ((i = t.message) == null ? void 0 : i.content) || "",
70
+ thinking: t.thinking || ((a = t.message) == null ? void 0 : a.thinking) || "",
71
+ done: t.done || !1,
72
+ usage: g(t),
73
+ finishReason: f(t.finish_reason)
74
+ }, s = (r = t.message) == null ? void 0 : r.tool_calls;
75
+ return Array.isArray(s) && s.length && (e.toolCallDeltas = s.map((c, o) => {
76
+ var l, u;
77
+ return {
78
+ index: o,
79
+ id: h(o),
80
+ name: ((l = c.function) == null ? void 0 : l.name) || "",
81
+ argsTextDelta: JSON.stringify(((u = c.function) == null ? void 0 : u.arguments) ?? {})
82
+ };
83
+ })), e;
55
84
  }
56
85
  getApiPath() {
57
86
  return "/api/chat";
@@ -62,16 +91,63 @@ class l extends r {
62
91
  getModelsEndpoint() {
63
92
  return `${this.config.baseUrl}/api/tags`;
64
93
  }
65
- parseModelsResponse(e) {
66
- var t;
67
- return ((t = e.models) == null ? void 0 : t.map((i) => i.name)) || [];
94
+ parseModelsResponse(t) {
95
+ var e;
96
+ return ((e = t.models) == null ? void 0 : e.map((s) => s.name)) || [];
68
97
  }
69
98
  }
70
- function a(s) {
71
- if (!s) return null;
72
- const e = String(s).toLowerCase();
73
- return e === "max_tokens" ? "length" : e;
99
+ function f(n) {
100
+ if (!n) return null;
101
+ const t = String(n).toLowerCase();
102
+ return t === "max_tokens" ? "length" : t;
103
+ }
104
+ function h(n) {
105
+ return `ollama_call_${n}`;
106
+ }
107
+ function g(n) {
108
+ if (!n || n.prompt_eval_count == null && n.eval_count == null) return null;
109
+ const t = n.prompt_eval_count ?? 0, e = n.eval_count ?? 0;
110
+ return { inputTokens: t, outputTokens: e, totalTokens: t + e, raw: n };
111
+ }
112
+ function d(n) {
113
+ if (n == null) return {};
114
+ if (typeof n == "string")
115
+ try {
116
+ return JSON.parse(n);
117
+ } catch {
118
+ return { __parseError: !0, raw: n };
119
+ }
120
+ return n;
121
+ }
122
+ function p(n) {
123
+ return n.map((t) => {
124
+ if (t.role === "assistant" && Array.isArray(t.tool_calls) && t.tool_calls.length)
125
+ return {
126
+ role: "assistant",
127
+ content: t.content ?? "",
128
+ tool_calls: t.tool_calls.map((e) => ({
129
+ function: {
130
+ name: e.name,
131
+ arguments: typeof e.args == "string" ? _(e.args) : e.args || {}
132
+ }
133
+ }))
134
+ };
135
+ if (t.role === "tool") {
136
+ const { tool_call_id: e, ...s } = t;
137
+ return s;
138
+ }
139
+ return t;
140
+ });
141
+ }
142
+ function _(n) {
143
+ try {
144
+ return JSON.parse(n);
145
+ } catch {
146
+ return {};
147
+ }
74
148
  }
75
149
  export {
76
- l as OllamaProvider
150
+ k as OllamaProvider,
151
+ p as convertMessagesToOllama,
152
+ g as normalizeOllamaUsage
77
153
  };
@@ -1,15 +1,15 @@
1
- var u = (n) => {
2
- throw TypeError(n);
1
+ var h = (s) => {
2
+ throw TypeError(s);
3
3
  };
4
- var f = (n, r, t) => r.has(n) || u("Cannot " + t);
5
- var h = (n, r, t) => r.has(n) ? u("Cannot add the same private member more than once") : r instanceof WeakSet ? r.add(n) : r.set(n, t);
6
- var g = (n, r, t) => (f(n, r, "access private method"), t);
7
- import { BaseProvider as m } from "./BaseProvider.js";
8
- var c, d;
9
- class k extends m {
4
+ var k = (s, o, t) => o.has(s) || h("Cannot " + t);
5
+ var p = (s, o, t) => o.has(s) ? h("Cannot add the same private member more than once") : o instanceof WeakSet ? o.add(s) : o.set(s, t);
6
+ var m = (s, o, t) => (k(s, o, "access private method"), t);
7
+ import { BaseProvider as y } from "./BaseProvider.js";
8
+ var u, _;
9
+ class v extends y {
10
10
  constructor() {
11
11
  super(...arguments);
12
- h(this, c);
12
+ p(this, u);
13
13
  }
14
14
  async detectCapabilities() {
15
15
  if (!this.config.model) return;
@@ -17,38 +17,46 @@ class k extends m {
17
17
  (t.startsWith("o1") || t.startsWith("o2") || t.startsWith("o3") || t.startsWith("o-") || t.includes("gpt-5") || t === "gpt5") && this.capabilities.add("thinking"), t.includes("gpt-4") && t.includes("vision") && this.capabilities.add("vision"), (t.includes("gpt-4") || t.includes("gpt-3.5") || t.includes("gpt-5")) && this.capabilities.add("tools");
18
18
  }
19
19
  prepareRequest(t, e) {
20
- const i = e.model || this.config.model || "gpt-3.5-turbo", s = {
21
- model: i,
20
+ const n = e.model || this.config.model || "gpt-3.5-turbo", i = {
21
+ model: n,
22
22
  messages: this.processMessages(t, e),
23
23
  temperature: e.temperature ?? 0.7,
24
24
  stream: e.stream || !1
25
25
  };
26
- return g(this, c, d).call(this, i) ? (s.max_completion_tokens = e.maxTokens || 1e3, s.temperature = 1) : s.max_tokens = e.maxTokens || 1e3, e.enableThinking && this.capabilities.has("thinking") && (s.reasoning_effort = e.reasoningEffort || "medium"), e.tools && this.capabilities.has("tools") && (s.tools = e.tools), s;
26
+ return i.stream && (i.stream_options = { include_usage: !0 }), m(this, u, _).call(this, n) ? (i.max_completion_tokens = e.maxTokens || 1e3, i.temperature = 1) : i.max_tokens = e.maxTokens || 1e3, e.enableThinking && this.capabilities.has("thinking") && (i.reasoning_effort = e.reasoningEffort || "medium"), e.tools && this.capabilities.has("tools") && (i.tools = e.tools), i;
27
27
  }
28
28
  processMessages(t, e) {
29
- return e.images && this.capabilities.has("vision") ? this.addImagesToMessages(t, e.images) : t;
29
+ const n = A(t);
30
+ return e.images && this.capabilities.has("vision") ? this.addImagesToMessages(n, e.images) : n;
30
31
  }
31
32
  addImagesToMessages(t, e) {
32
- const i = t[t.length - 1];
33
- if (i && i.role === "user") {
34
- const s = [{ type: "text", text: i.content }];
35
- e.forEach((a) => {
36
- s.push({
33
+ const n = t[t.length - 1];
34
+ if (n && n.role === "user") {
35
+ const i = [{ type: "text", text: n.content }];
36
+ e.forEach((r) => {
37
+ i.push({
37
38
  type: "image_url",
38
- image_url: { url: typeof a == "string" ? a : a.url }
39
+ image_url: { url: typeof r == "string" ? r : r.url }
39
40
  });
40
- }), i.content = s;
41
+ }), n.content = i;
41
42
  }
42
43
  return t;
43
44
  }
44
45
  processResponse(t) {
45
- var i, s, a, o, l;
46
- const e = {
47
- content: ((a = (s = (i = t.choices) == null ? void 0 : i[0]) == null ? void 0 : s.message) == null ? void 0 : a.content) || "",
48
- usage: t.usage || null,
49
- finishReason: ((l = (o = t.choices) == null ? void 0 : o[0]) == null ? void 0 : l.finish_reason) || null
46
+ var i, r, l, a;
47
+ const e = (r = (i = t.choices) == null ? void 0 : i[0]) == null ? void 0 : r.message, n = {
48
+ content: (e == null ? void 0 : e.content) || "",
49
+ usage: g(t.usage),
50
+ finishReason: ((a = (l = t.choices) == null ? void 0 : l[0]) == null ? void 0 : a.finish_reason) || null
50
51
  };
51
- return t.reasoning && (e.thinking = t.reasoning), e;
52
+ return t.reasoning && (n.thinking = t.reasoning), Array.isArray(e == null ? void 0 : e.tool_calls) && (n.toolCalls = e.tool_calls.map((c) => {
53
+ var d, f;
54
+ return {
55
+ id: c.id,
56
+ name: (d = c.function) == null ? void 0 : d.name,
57
+ args: T((f = c.function) == null ? void 0 : f.arguments)
58
+ };
59
+ })), n;
52
60
  }
53
61
  parseStreamingLine(t) {
54
62
  if (!t.startsWith("data: ")) return null;
@@ -61,15 +69,31 @@ class k extends m {
61
69
  }
62
70
  }
63
71
  extractStreamingContent(t) {
64
- var i, s, a, o;
72
+ var i, r, l;
65
73
  if (t.done) return { done: !0 };
66
- const e = (s = (i = t.choices) == null ? void 0 : i[0]) == null ? void 0 : s.delta;
74
+ const e = (i = t.choices) == null ? void 0 : i[0], n = e == null ? void 0 : e.delta;
75
+ if (n && Array.isArray(n.tool_calls) && n.tool_calls.length) {
76
+ const a = n.tool_calls[0];
77
+ return {
78
+ content: n.content || "",
79
+ thinking: n.reasoning || "",
80
+ done: !1,
81
+ usage: g(t.usage),
82
+ finishReason: (e == null ? void 0 : e.finish_reason) || null,
83
+ toolCallDelta: {
84
+ index: a.index ?? 0,
85
+ id: a.id || void 0,
86
+ name: ((r = a.function) == null ? void 0 : r.name) || void 0,
87
+ argsTextDelta: ((l = a.function) == null ? void 0 : l.arguments) || ""
88
+ }
89
+ };
90
+ }
67
91
  return {
68
- content: (e == null ? void 0 : e.content) || "",
69
- thinking: (e == null ? void 0 : e.reasoning) || "",
92
+ content: (n == null ? void 0 : n.content) || "",
93
+ thinking: (n == null ? void 0 : n.reasoning) || "",
70
94
  done: !1,
71
- usage: t.usage || null,
72
- finishReason: ((o = (a = t.choices) == null ? void 0 : a[0]) == null ? void 0 : o.finish_reason) || null
95
+ usage: g(t.usage),
96
+ finishReason: (e == null ? void 0 : e.finish_reason) || null
73
97
  };
74
98
  }
75
99
  getApiPath() {
@@ -83,16 +107,46 @@ class k extends m {
83
107
  }
84
108
  parseModelsResponse(t) {
85
109
  var e;
86
- return ((e = t.data) == null ? void 0 : e.filter((i) => {
87
- const s = i.id.toLowerCase();
88
- return s.includes("gpt") || s.includes("chat");
89
- }).map((i) => i.id).sort()) || [];
110
+ return ((e = t.data) == null ? void 0 : e.filter((n) => {
111
+ const i = n.id.toLowerCase();
112
+ return i.includes("gpt") || i.includes("chat");
113
+ }).map((n) => n.id).sort()) || [];
90
114
  }
91
115
  }
92
- c = new WeakSet(), d = function(t) {
116
+ u = new WeakSet(), _ = function(t) {
93
117
  const e = (t || "").toLowerCase();
94
118
  return e.startsWith("o1") || e.startsWith("o2") || e.startsWith("o3") || e.startsWith("o-") || e.includes("gpt-5") || e === "gpt5" || e.includes("reasoning") || this.capabilities.has("thinking");
95
119
  };
120
+ function T(s) {
121
+ if (!s) return {};
122
+ try {
123
+ return JSON.parse(s);
124
+ } catch {
125
+ return { __parseError: !0, raw: s };
126
+ }
127
+ }
128
+ function g(s) {
129
+ var l, a;
130
+ if (!s) return null;
131
+ const o = s.prompt_tokens ?? 0, t = s.completion_tokens ?? 0, e = s.total_tokens ?? o + t, n = (l = s.prompt_tokens_details) == null ? void 0 : l.cached_tokens, i = (a = s.completion_tokens_details) == null ? void 0 : a.reasoning_tokens, r = { inputTokens: o, outputTokens: t, totalTokens: e, raw: s };
132
+ return n != null && (r.cachedInputTokens = n), i != null && (r.reasoningTokens = i), r;
133
+ }
134
+ function A(s) {
135
+ return s.map((o) => o.role === "assistant" && Array.isArray(o.tool_calls) && o.tool_calls.length ? {
136
+ role: "assistant",
137
+ content: o.content ?? null,
138
+ tool_calls: o.tool_calls.map((t) => ({
139
+ id: t.id,
140
+ type: "function",
141
+ function: {
142
+ name: t.name,
143
+ arguments: typeof t.args == "string" ? t.args : JSON.stringify(t.args || {})
144
+ }
145
+ }))
146
+ } : o);
147
+ }
96
148
  export {
97
- k as OpenAIProvider
149
+ v as OpenAIProvider,
150
+ A as convertMessagesToOpenAI,
151
+ g as normalizeOpenAIUsage
98
152
  };
@@ -1,37 +1,46 @@
1
- import { BaseProvider as d } from "./BaseProvider.js";
2
- class f extends d {
1
+ import { BaseProvider as f } from "./BaseProvider.js";
2
+ import { convertMessagesToOpenAI as g, normalizeOpenAIUsage as c } from "./OpenAIProvider.js";
3
+ class y extends f {
3
4
  async detectCapabilities() {
4
5
  this.config.model && (this.capabilities.add("tools"), (this.config.model.includes("o1") || this.config.model.includes("thinking") || this.config.model.includes("reasoning")) && this.capabilities.add("thinking"), (this.config.model.includes("vision") || this.config.model.includes("gpt-4") || this.config.model.includes("claude") || this.config.model.includes("gemini")) && this.capabilities.add("vision"));
5
6
  }
6
- prepareRequest(t, e) {
7
- const i = {
7
+ prepareRequest(i, e) {
8
+ const t = {
8
9
  model: e.model || this.config.model,
9
- messages: this.processMessages(t, e),
10
+ messages: this.processMessages(i, e),
10
11
  temperature: e.temperature ?? 0.7,
11
12
  max_tokens: e.maxTokens || 1e3,
12
13
  stream: e.stream || !1
13
14
  };
14
- return e.enableThinking && this.capabilities.has("thinking") && (i.reasoning = e.reasoning !== !1, e.reasoningEffort && (i.reasoning_effort = e.reasoningEffort)), e.tools && this.capabilities.has("tools") && (i.tools = e.tools, e.tool_choice && (i.tool_choice = e.tool_choice)), i;
15
+ return t.stream && (t.stream_options = { include_usage: !0 }), e.enableThinking && this.capabilities.has("thinking") && (t.reasoning = e.reasoning !== !1, e.reasoningEffort && (t.reasoning_effort = e.reasoningEffort)), e.tools && this.capabilities.has("tools") && (t.tools = e.tools, e.tool_choice && (t.tool_choice = e.tool_choice)), t;
15
16
  }
16
- processMessages(t, e) {
17
+ processMessages(i, e) {
18
+ const t = g(i);
17
19
  return e.images && this.capabilities.has("vision") ? this.addImagesToMessages(t, e.images) : t;
18
20
  }
19
- addImagesToMessages(t, e) {
20
- const i = t[t.length - 1];
21
- return i && i.role === "user" && (i.content = [{ type: "text", text: i.content }, ...e.map((s) => ({ type: "image_url", image_url: { url: s } }))]), t;
21
+ addImagesToMessages(i, e) {
22
+ const t = i[i.length - 1];
23
+ return t && t.role === "user" && (t.content = [{ type: "text", text: t.content }, ...e.map((n) => ({ type: "image_url", image_url: { url: n } }))]), i;
22
24
  }
23
25
  buildHeaders() {
24
- const t = { "Content-Type": "application/json" };
25
- return this.requiresAuth() && (t[this.getAuthHeaderName()] = this.getAuthHeaderValue()), this.config.siteUrl && (t["HTTP-Referer"] = this.config.siteUrl), this.config.siteName && (t["X-Title"] = this.config.siteName), t;
26
+ const i = { "Content-Type": "application/json" };
27
+ return this.requiresAuth() && (i[this.getAuthHeaderName()] = this.getAuthHeaderValue()), this.config.siteUrl && (i["HTTP-Referer"] = this.config.siteUrl), this.config.siteName && (i["X-Title"] = this.config.siteName), i;
26
28
  }
27
- processResponse(t) {
28
- var i, s, n, r, a, c, l, h;
29
- const e = { content: ((n = (s = (i = t.choices) == null ? void 0 : i[0]) == null ? void 0 : s.message) == null ? void 0 : n.content) || "", usage: t.usage || null, finishReason: u((a = (r = t.choices) == null ? void 0 : r[0]) == null ? void 0 : a.finish_reason) };
30
- return (h = (l = (c = t.choices) == null ? void 0 : c[0]) == null ? void 0 : l.message) != null && h.reasoning && (e.thinking = t.choices[0].message.reasoning), e;
29
+ processResponse(i) {
30
+ var n, s, o, a;
31
+ const e = (s = (n = i.choices) == null ? void 0 : n[0]) == null ? void 0 : s.message, t = { content: (e == null ? void 0 : e.content) || "", usage: c(i.usage), finishReason: u((a = (o = i.choices) == null ? void 0 : o[0]) == null ? void 0 : a.finish_reason) };
32
+ return e != null && e.reasoning && (t.thinking = e.reasoning), Array.isArray(e == null ? void 0 : e.tool_calls) && (t.toolCalls = e.tool_calls.map((l) => {
33
+ var d, h;
34
+ return {
35
+ id: l.id,
36
+ name: (d = l.function) == null ? void 0 : d.name,
37
+ args: m((h = l.function) == null ? void 0 : h.arguments)
38
+ };
39
+ })), t;
31
40
  }
32
- parseStreamingLine(t) {
33
- if (!t.startsWith("data: ")) return null;
34
- const e = t.slice(6).trim();
41
+ parseStreamingLine(i) {
42
+ if (!i.startsWith("data: ")) return null;
43
+ const e = i.slice(6).trim();
35
44
  if (e === "[DONE]") return { done: !0 };
36
45
  try {
37
46
  return JSON.parse(e);
@@ -39,11 +48,27 @@ class f extends d {
39
48
  return null;
40
49
  }
41
50
  }
42
- extractStreamingContent(t) {
43
- var i, s, n, r;
44
- if (t.done) return { done: !0 };
45
- const e = (s = (i = t.choices) == null ? void 0 : i[0]) == null ? void 0 : s.delta;
46
- return { content: (e == null ? void 0 : e.content) || "", thinking: (e == null ? void 0 : e.reasoning) || "", done: !1, usage: t.usage || null, finishReason: u((r = (n = t.choices) == null ? void 0 : n[0]) == null ? void 0 : r.finish_reason) };
51
+ extractStreamingContent(i) {
52
+ var n, s, o;
53
+ if (i.done) return { done: !0 };
54
+ const e = (n = i.choices) == null ? void 0 : n[0], t = e == null ? void 0 : e.delta;
55
+ if (t && Array.isArray(t.tool_calls) && t.tool_calls.length) {
56
+ const a = t.tool_calls[0];
57
+ return {
58
+ content: t.content || "",
59
+ thinking: t.reasoning || "",
60
+ done: !1,
61
+ usage: c(i.usage),
62
+ finishReason: u(e == null ? void 0 : e.finish_reason),
63
+ toolCallDelta: {
64
+ index: a.index ?? 0,
65
+ id: a.id || void 0,
66
+ name: ((s = a.function) == null ? void 0 : s.name) || void 0,
67
+ argsTextDelta: ((o = a.function) == null ? void 0 : o.arguments) || ""
68
+ }
69
+ };
70
+ }
71
+ return { content: (t == null ? void 0 : t.content) || "", thinking: (t == null ? void 0 : t.reasoning) || "", done: !1, usage: c(i.usage), finishReason: u(e == null ? void 0 : e.finish_reason) };
47
72
  }
48
73
  getApiPath() {
49
74
  return "/v1/chat/completions";
@@ -54,28 +79,36 @@ class f extends d {
54
79
  getModelsEndpoint() {
55
80
  return `${this.config.baseUrl}/v1/models`;
56
81
  }
57
- parseModelsResponse(t) {
58
- return Array.isArray(t.data) ? t.data.filter((e) => {
59
- var s;
60
- const i = (s = e.architecture) == null ? void 0 : s.modality;
61
- return i && (i.includes("text->text") || i.includes("text+image->text"));
82
+ parseModelsResponse(i) {
83
+ return Array.isArray(i.data) ? i.data.filter((e) => {
84
+ var n;
85
+ const t = (n = e.architecture) == null ? void 0 : n.modality;
86
+ return t && (t.includes("text->text") || t.includes("text+image->text"));
62
87
  }).map((e) => e.id).sort() : [];
63
88
  }
64
- async discoverModelsWithMetadata(t = 15e3) {
89
+ async discoverModelsWithMetadata(i = 15e3) {
65
90
  try {
66
- const e = this.buildHeaders(), i = new AbortController(), s = setTimeout(() => i.abort(), t), n = await fetch(this.getModelsEndpoint(), { method: "GET", headers: e, signal: i.signal });
67
- if (clearTimeout(s), !n.ok) throw new Error(`Failed to fetch models: ${n.status} ${n.statusText}`);
68
- return (await n.json()).data || [];
91
+ const e = this.buildHeaders(), t = new AbortController(), n = setTimeout(() => t.abort(), i), s = await fetch(this.getModelsEndpoint(), { method: "GET", headers: e, signal: t.signal });
92
+ if (clearTimeout(n), !s.ok) throw new Error(`Failed to fetch models: ${s.status} ${s.statusText}`);
93
+ return (await s.json()).data || [];
69
94
  } catch (e) {
70
95
  throw e.name === "AbortError" ? new Error("Model discovery timeout - please check your connection") : e;
71
96
  }
72
97
  }
73
98
  }
74
- function u(o) {
75
- if (!o) return null;
76
- const t = String(o).toLowerCase();
77
- return t === "length" || t === "max_tokens" ? "length" : t;
99
+ function u(r) {
100
+ if (!r) return null;
101
+ const i = String(r).toLowerCase();
102
+ return i === "length" || i === "max_tokens" ? "length" : i;
103
+ }
104
+ function m(r) {
105
+ if (!r) return {};
106
+ try {
107
+ return JSON.parse(r);
108
+ } catch {
109
+ return { __parseError: !0, raw: r };
110
+ }
78
111
  }
79
112
  export {
80
- f as OpenRouterProvider
113
+ y as OpenRouterProvider
81
114
  };