@anvaka/vue-llm 0.4.0 → 0.4.2
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/dist/core/LLMClient.js +126 -89
- package/dist/providers/AnthropicProvider.js +34 -38
- package/dist/providers/BaseProvider.js +114 -60
- package/dist/providers/BedrockMantleProvider.js +55 -48
- package/dist/providers/BedrockProvider.js +63 -70
- package/dist/providers/CustomProvider.js +3 -4
- package/dist/providers/DeepSeekProvider.js +5 -6
- package/dist/providers/GrokProvider.js +43 -44
- package/dist/providers/LlamaServerProvider.js +7 -8
- package/dist/providers/OpenAIProvider.js +77 -77
- package/dist/providers/OpenRouterProvider.js +3 -4
- package/dist/providers/samplingPolicy.js +41 -0
- package/package.json +1 -1
package/dist/core/LLMClient.js
CHANGED
|
@@ -35,14 +35,14 @@ class P {
|
|
|
35
35
|
const e = [
|
|
36
36
|
{ role: "system", content: 'Respond with exactly "pong"' },
|
|
37
37
|
{ role: "user", content: "ping" }
|
|
38
|
-
],
|
|
38
|
+
], o = this.validateCapabilities({
|
|
39
39
|
model: this.config.model,
|
|
40
40
|
enableThinking: !1,
|
|
41
41
|
temperature: t.temperature ?? 0.1,
|
|
42
42
|
maxTokens: 10,
|
|
43
43
|
stream: !1
|
|
44
|
-
}),
|
|
45
|
-
return (r = this.provider.processResponse(
|
|
44
|
+
}), n = this.provider.prepareRequest(e, o), c = await this.provider.makeRequest(n);
|
|
45
|
+
return (r = this.provider.processResponse(c).content) == null ? void 0 : r.trim();
|
|
46
46
|
} finally {
|
|
47
47
|
this.config = s, this.provider = i;
|
|
48
48
|
}
|
|
@@ -73,7 +73,7 @@ class P {
|
|
|
73
73
|
};
|
|
74
74
|
}
|
|
75
75
|
async ping() {
|
|
76
|
-
var
|
|
76
|
+
var o;
|
|
77
77
|
await this.ensureInitialized();
|
|
78
78
|
const t = [
|
|
79
79
|
{ role: "system", content: 'Respond with exactly "pong"' },
|
|
@@ -84,17 +84,17 @@ class P {
|
|
|
84
84
|
maxTokens: 10,
|
|
85
85
|
stream: !1
|
|
86
86
|
}), i = this.provider.prepareRequest(t, s), r = await this.provider.makeRequest(i);
|
|
87
|
-
return (
|
|
87
|
+
return (o = this.provider.processResponse(r).content) == null ? void 0 : o.trim();
|
|
88
88
|
}
|
|
89
89
|
async stream(t, s) {
|
|
90
90
|
await this.ensureInitialized();
|
|
91
91
|
const i = this.validateCapabilities({ ...t, stream: !0, model: t.model || this.config.model, requestId: this.generateRequestId() }), r = t.messages;
|
|
92
|
-
let e = "",
|
|
93
|
-
return await this.provider.streamRequest(r, i, (
|
|
94
|
-
e =
|
|
95
|
-
const
|
|
96
|
-
s && s(
|
|
97
|
-
}), { content: e, usage:
|
|
92
|
+
let e = "", o = null;
|
|
93
|
+
return await this.provider.streamRequest(r, i, (n) => {
|
|
94
|
+
e = n.fullContent, n.fullUsage && (o = n.fullUsage);
|
|
95
|
+
const c = n.fullUsage ? { ...n, cost: this.costFor(n.fullUsage, { provider: this.config.provider, model: i.model }) } : n;
|
|
96
|
+
s && s(c);
|
|
97
|
+
}), { content: e, usage: o, cost: this.costFor(o, { provider: this.config.provider, model: i.model }) };
|
|
98
98
|
}
|
|
99
99
|
// Multi-turn tool-calling loop. Each iteration:
|
|
100
100
|
// 1) stream a model response (text + accumulated tool_calls)
|
|
@@ -111,70 +111,107 @@ class P {
|
|
|
111
111
|
//
|
|
112
112
|
// `onEvent` (optional) fires for: iter-start, text-delta, tool-call-delta,
|
|
113
113
|
// assistant-message, tool-call, tool-result, stop. Use it to drive a trace UI.
|
|
114
|
+
// `signal` (optional AbortSignal) stops the loop. It is the only way to end a
|
|
115
|
+
// run that is mid-stream: every other exit is the model's decision (it stopped
|
|
116
|
+
// calling tools) or a backstop (maxIters). Cancelling aborts the in-flight
|
|
117
|
+
// fetch, so it stops the spend immediately rather than asking the model to
|
|
118
|
+
// wind down — which would cost another full round-trip per iteration.
|
|
119
|
+
//
|
|
120
|
+
// Every abort path leaves `messages` VALID to resume from, which is the whole
|
|
121
|
+
// design constraint here: cancel during the stream and we break before the
|
|
122
|
+
// assistant message is appended; cancel during tool execution and we still
|
|
123
|
+
// record a result for every tool_call. A transcript carrying tool_calls with
|
|
124
|
+
// no matching tool result is a 400 on the next request — a "stop" that
|
|
125
|
+
// stranded the conversation would be worse than no stop at all.
|
|
114
126
|
async runAgentLoop({
|
|
115
127
|
messages: t,
|
|
116
128
|
tools: s,
|
|
117
129
|
executors: i,
|
|
118
130
|
onEvent: r,
|
|
119
131
|
maxIters: e = 10,
|
|
120
|
-
temperature:
|
|
121
|
-
model:
|
|
122
|
-
maxTokens:
|
|
123
|
-
enableThinking:
|
|
132
|
+
temperature: o,
|
|
133
|
+
model: n,
|
|
134
|
+
maxTokens: c,
|
|
135
|
+
enableThinking: m,
|
|
136
|
+
signal: l
|
|
124
137
|
} = {}) {
|
|
125
138
|
if (await this.ensureInitialized(), !this.provider.hasCapability("tools"))
|
|
126
139
|
throw new Error("Configured provider does not support tools");
|
|
127
|
-
const
|
|
128
|
-
let
|
|
129
|
-
const
|
|
130
|
-
let
|
|
131
|
-
for (;
|
|
132
|
-
|
|
140
|
+
const p = t.slice();
|
|
141
|
+
let g = 0, h = null;
|
|
142
|
+
const v = { inputTokens: 0, outputTokens: 0, totalTokens: 0, cachedInputTokens: 0, cacheCreationInputTokens: 0, reasoningTokens: 0 };
|
|
143
|
+
let y = !1;
|
|
144
|
+
for (; g < e; ) {
|
|
145
|
+
if (l != null && l.aborted) {
|
|
146
|
+
h = "aborted";
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
g++, r && r({ type: "iter-start", iter: g });
|
|
133
150
|
const x = this.validateCapabilities({
|
|
134
|
-
messages:
|
|
151
|
+
messages: p,
|
|
135
152
|
tools: s,
|
|
136
153
|
stream: !0,
|
|
137
|
-
model:
|
|
138
|
-
temperature:
|
|
139
|
-
maxTokens:
|
|
140
|
-
enableThinking:
|
|
154
|
+
model: n || this.config.model,
|
|
155
|
+
temperature: o ?? this.config.temperature,
|
|
156
|
+
maxTokens: c ?? this.config.maxTokens ?? 4096,
|
|
157
|
+
enableThinking: m ?? !1,
|
|
141
158
|
// Re-sends the whole growing conversation each turn, so cache the
|
|
142
159
|
// rolling transcript prefix (Anthropic-family providers honor this).
|
|
143
160
|
cacheTranscript: !0,
|
|
144
|
-
requestId: this.generateRequestId()
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
161
|
+
requestId: this.generateRequestId(),
|
|
162
|
+
// validateCapabilities spreads its input, so this reaches streamRequest,
|
|
163
|
+
// which links it to the fetch's own controller.
|
|
164
|
+
signal: l
|
|
165
|
+
});
|
|
166
|
+
let u;
|
|
167
|
+
try {
|
|
168
|
+
u = await this.provider.streamRequest(p, x, (a) => {
|
|
169
|
+
a.content && r && r({ type: "text-delta", text: a.content }), a.toolCallDelta && r && r({ type: "tool-call-delta", delta: a.toolCallDelta });
|
|
170
|
+
});
|
|
171
|
+
} catch (a) {
|
|
172
|
+
if (l != null && l.aborted || (a == null ? void 0 : a.name) === "AbortError") {
|
|
173
|
+
h = "aborted";
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
throw a;
|
|
177
|
+
}
|
|
178
|
+
const q = (u == null ? void 0 : u.content) || "", w = (u == null ? void 0 : u.toolCalls) || [], _ = (u == null ? void 0 : u.thinking) || "", T = (u == null ? void 0 : u.usage) || null;
|
|
179
|
+
if (T) {
|
|
180
|
+
y = !0;
|
|
181
|
+
for (const a of Object.keys(v))
|
|
182
|
+
T[a] != null && (v[a] += T[a]);
|
|
183
|
+
r && r({ type: "usage", usage: T, cost: this.costFor(T, { provider: this.config.provider, model: x.model }) });
|
|
153
184
|
}
|
|
154
185
|
const k = { role: "assistant", content: q };
|
|
155
|
-
if (
|
|
156
|
-
|
|
186
|
+
if (w.length && (k.tool_calls = w), _ && (k.thinking = _), p.push(k), r && r({ type: "assistant-message", content: q, toolCalls: w, thinking: _ }), !w.length) {
|
|
187
|
+
h = "no-tool-calls";
|
|
157
188
|
break;
|
|
158
189
|
}
|
|
159
|
-
for (const a of
|
|
190
|
+
for (const a of w) {
|
|
160
191
|
r && r({ type: "tool-call", id: a.id, name: a.name, args: a.args });
|
|
161
192
|
const R = i && i[a.name];
|
|
162
|
-
let
|
|
163
|
-
if (
|
|
164
|
-
|
|
193
|
+
let f;
|
|
194
|
+
if (l != null && l.aborted)
|
|
195
|
+
f = "Error: cancelled by the user.";
|
|
196
|
+
else if (!R)
|
|
197
|
+
f = `Error: unknown tool '${a.name}'`;
|
|
165
198
|
else
|
|
166
199
|
try {
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
} catch (
|
|
170
|
-
|
|
200
|
+
const d = await R(a.args || {});
|
|
201
|
+
f = typeof d == "string" ? d : JSON.stringify(d ?? "");
|
|
202
|
+
} catch (d) {
|
|
203
|
+
f = `Error: ${(d == null ? void 0 : d.message) || String(d)}`;
|
|
171
204
|
}
|
|
172
|
-
|
|
205
|
+
p.push({ role: "tool", tool_call_id: a.id, content: f }), r && r({ type: "tool-result", id: a.id, name: a.name, content: f });
|
|
206
|
+
}
|
|
207
|
+
if (l != null && l.aborted) {
|
|
208
|
+
h = "aborted";
|
|
209
|
+
break;
|
|
173
210
|
}
|
|
174
211
|
}
|
|
175
|
-
|
|
176
|
-
const
|
|
177
|
-
return r && r({ type: "stop", reason:
|
|
212
|
+
h || (h = "max-iters");
|
|
213
|
+
const b = y ? v : null, C = this.costFor(b, { provider: this.config.provider, model: n || this.config.model });
|
|
214
|
+
return r && r({ type: "stop", reason: h, iterations: g, usage: b, cost: C }), { messages: p, iterations: g, stopReason: h, usage: b, cost: C };
|
|
178
215
|
}
|
|
179
216
|
generateRequestId() {
|
|
180
217
|
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
@@ -198,7 +235,7 @@ class P {
|
|
|
198
235
|
const s = t.trim(), i = this.configStore.getEnabledConfigs().filter((e) => this._matchesDisplayName(e, s));
|
|
199
236
|
if (i.length === 0) return null;
|
|
200
237
|
if (i.length > 1) {
|
|
201
|
-
const e = i.map((
|
|
238
|
+
const e = i.map((o) => o.id).join(", ");
|
|
202
239
|
throw new Error(`Multiple provider presets share the name '${t}'. Conflicting ids: ${e}`);
|
|
203
240
|
}
|
|
204
241
|
return i[0];
|
|
@@ -214,15 +251,15 @@ class P {
|
|
|
214
251
|
// Provides llm(prompt, opts).into(target, attrs?) and .withOperation(name) chain.
|
|
215
252
|
createLLMWrapper(t, s = null, i = null) {
|
|
216
253
|
const r = this;
|
|
217
|
-
function e(
|
|
218
|
-
return new $(r, t,
|
|
254
|
+
function e(o, n = {}) {
|
|
255
|
+
return new $(r, t, o, n, s, i);
|
|
219
256
|
}
|
|
220
257
|
return e;
|
|
221
258
|
}
|
|
222
259
|
}
|
|
223
260
|
class $ {
|
|
224
|
-
constructor(t, s, i, r, e,
|
|
225
|
-
this.client = t, this.contextNode = s, this.prompt = i, this.options = r || {}, this.originatingCode = e, this.defaultPresetName =
|
|
261
|
+
constructor(t, s, i, r, e, o) {
|
|
262
|
+
this.client = t, this.contextNode = s, this.prompt = i, this.options = r || {}, this.originatingCode = e, this.defaultPresetName = o, this.targetNode = null, this.targetAttributes = {}, this.operationName = null, this._executed = !1, this._promise = null;
|
|
226
263
|
}
|
|
227
264
|
withOperation(t) {
|
|
228
265
|
return this.operationName = t, this;
|
|
@@ -243,12 +280,12 @@ class $ {
|
|
|
243
280
|
this._executed || (this._executed = !0, this._promise = this._run());
|
|
244
281
|
}
|
|
245
282
|
async _run() {
|
|
246
|
-
var
|
|
283
|
+
var n;
|
|
247
284
|
const t = await this._createExecutionContext(), { provider: s, config: i } = t;
|
|
248
285
|
if (!s || !i) throw new Error("LLM not configured");
|
|
249
286
|
const r = this.client.validateCapabilities.bind({ provider: s, config: i }), e = [];
|
|
250
287
|
this.options.system && e.push({ role: "system", content: this.options.system }), e.push({ role: "user", content: this.prompt });
|
|
251
|
-
const
|
|
288
|
+
const o = {
|
|
252
289
|
model: this.options.model || i.model,
|
|
253
290
|
temperature: this.options.temperature ?? i.temperature,
|
|
254
291
|
maxTokens: this.options.maxTokens ?? i.maxTokens,
|
|
@@ -259,17 +296,17 @@ class $ {
|
|
|
259
296
|
};
|
|
260
297
|
try {
|
|
261
298
|
if (this.targetNode) {
|
|
262
|
-
const
|
|
263
|
-
return await this._streamIntoTarget(e,
|
|
299
|
+
const m = r({ ...o, stream: !0 });
|
|
300
|
+
return await this._streamIntoTarget(e, m, s, i);
|
|
264
301
|
}
|
|
265
|
-
const
|
|
266
|
-
return await this._promiseResponse(e,
|
|
302
|
+
const c = r({ ...o, stream: !1 });
|
|
303
|
+
return await this._promiseResponse(e, c, s, i);
|
|
267
304
|
} finally {
|
|
268
|
-
(
|
|
305
|
+
(n = t.cleanup) == null || n.call(t);
|
|
269
306
|
}
|
|
270
307
|
}
|
|
271
308
|
async _createExecutionContext() {
|
|
272
|
-
var r, e,
|
|
309
|
+
var r, e, o, n;
|
|
273
310
|
const t = this._resolvePresetName();
|
|
274
311
|
if (!t)
|
|
275
312
|
return await this.client.ensureInitialized(), { provider: this.client.provider, config: this.client.config, cleanup: () => {
|
|
@@ -278,13 +315,13 @@ class $ {
|
|
|
278
315
|
throw new Error("Provider presets require a configured ConfigStore instance");
|
|
279
316
|
const s = (e = (r = this.client).getConfigByName) == null ? void 0 : e.call(r, t);
|
|
280
317
|
if (!s) {
|
|
281
|
-
const
|
|
282
|
-
throw new Error(`Provider preset '${t}' not found.${
|
|
318
|
+
const c = (((n = (o = this.client.configStore).getEnabledConfigs) == null ? void 0 : n.call(o)) || []).map((l) => l.name || `${l.provider} (${l.baseUrl})`).filter(Boolean), m = c.length ? ` Available presets: ${c.join(", ")}` : " No configured providers found.";
|
|
319
|
+
throw new Error(`Provider preset '${t}' not found.${m}`);
|
|
283
320
|
}
|
|
284
321
|
const i = U(s.provider, s);
|
|
285
322
|
return await i.initialize(), { provider: i, config: s, cleanup: () => {
|
|
286
|
-
var
|
|
287
|
-
return (
|
|
323
|
+
var c;
|
|
324
|
+
return (c = i.cancelAllRequests) == null ? void 0 : c.call(i);
|
|
288
325
|
} };
|
|
289
326
|
}
|
|
290
327
|
_resolvePresetName() {
|
|
@@ -296,49 +333,49 @@ class $ {
|
|
|
296
333
|
for (const [r, e] of Object.entries(this.targetAttributes || {}))
|
|
297
334
|
try {
|
|
298
335
|
t.setAttribute(r, e);
|
|
299
|
-
} catch (
|
|
300
|
-
(i = (s = this.client.logger) == null ? void 0 : s.warn) == null || i.call(s, "Failed to set attribute on target node", { key: r, value: e, error:
|
|
336
|
+
} catch (o) {
|
|
337
|
+
(i = (s = this.client.logger) == null ? void 0 : s.warn) == null || i.call(s, "Failed to set attribute on target node", { key: r, value: e, error: o });
|
|
301
338
|
}
|
|
302
339
|
}
|
|
303
340
|
async _streamIntoTarget(t, s, i, r) {
|
|
304
|
-
var p,
|
|
305
|
-
let e,
|
|
306
|
-
typeof this.targetNode == "string" ? (
|
|
307
|
-
const
|
|
308
|
-
|
|
341
|
+
var p, g, h, v, y, b, C, x, u, q, w, _, T, k, a, R;
|
|
342
|
+
let e, o;
|
|
343
|
+
typeof this.targetNode == "string" ? (o = this.targetNode, e = this.contextNode.createChild(o), (p = e.setSelected) == null || p.call(e)) : (e = this.targetNode, o = ((g = e.getText) == null ? void 0 : g.call(e)) || ""), this._applyAttributes(e);
|
|
344
|
+
const n = r == null ? void 0 : r.name;
|
|
345
|
+
n && e.addTag && e.addTag("provider", n), (h = e.addLLMLog) == null || h.call(e, this.operationName || "streaming-request", {
|
|
309
346
|
provider: r == null ? void 0 : r.provider,
|
|
310
347
|
model: r == null ? void 0 : r.model,
|
|
311
348
|
messages: t,
|
|
312
349
|
options: s
|
|
313
|
-
}), (
|
|
314
|
-
let
|
|
350
|
+
}), (v = e.setStreamingState) == null || v.call(e, !0), e._activeRequestId = s.requestId;
|
|
351
|
+
let c = "", m = null, l = null;
|
|
315
352
|
try {
|
|
316
|
-
await i.streamRequest(t, s, (
|
|
353
|
+
await i.streamRequest(t, s, (d) => {
|
|
317
354
|
var I, S;
|
|
318
|
-
|
|
355
|
+
d.content && (c = d.fullContent, (I = e.setText) == null || I.call(e, c)), d.fullUsage && (l = d.fullUsage), d.finishReason && (m = d.finishReason), d.done && ((S = e.setStreamingState) == null || S.call(e, !1), e._activeRequestId = null);
|
|
319
356
|
});
|
|
320
|
-
const
|
|
321
|
-
return (
|
|
322
|
-
} catch (
|
|
323
|
-
throw (
|
|
357
|
+
const f = (b = (y = this.client).costFor) == null ? void 0 : b.call(y, l, { provider: r == null ? void 0 : r.provider, model: s.model });
|
|
358
|
+
return (C = e.addLLMLog) == null || C.call(e, (this.operationName || "streaming-request") + "-response", { requestId: s.requestId }, { content: c, usage: l, cost: f }), m === "length" ? ((x = e.setAttribute) == null || x.call(e, "_truncated", !0), (u = e.addLog) == null || u.call(e, "Response truncated. Increase Max Tokens.", "warn", { finish_reason: "length" })) : (q = e.setAttribute) == null || q.call(e, "_truncated", !1), await ((w = e.persistNow) == null ? void 0 : w.call(e)), e;
|
|
359
|
+
} catch (f) {
|
|
360
|
+
throw (_ = e.addLLMLog) == null || _.call(e, (this.operationName || "streaming-request") + "-error", { requestId: s.requestId }, null, f), (T = e.setStreamingState) == null || T.call(e, !1), e._activeRequestId = null, (k = e.setText) == null || k.call(e, `Error: ${f.message}`), (a = e.setAttribute) == null || a.call(e, "_truncated", !1), await ((R = e.persistNow) == null ? void 0 : R.call(e)), f;
|
|
324
361
|
}
|
|
325
362
|
}
|
|
326
363
|
async _promiseResponse(t, s, i, r) {
|
|
327
|
-
var
|
|
328
|
-
const e = i.prepareRequest(t, s),
|
|
329
|
-
if ((
|
|
364
|
+
var m, l, p, g, h, v, y;
|
|
365
|
+
const e = i.prepareRequest(t, s), o = await i.makeRequest(e), n = i.processResponse(o);
|
|
366
|
+
if ((n.finishReason || ((l = (m = o == null ? void 0 : o.choices) == null ? void 0 : m[0]) == null ? void 0 : l.finish_reason) || null) === "length")
|
|
330
367
|
try {
|
|
331
368
|
this.contextNode.setAttribute("_truncated", !0);
|
|
332
|
-
} catch (
|
|
333
|
-
(
|
|
369
|
+
} catch (b) {
|
|
370
|
+
(g = (p = this.client.logger) == null ? void 0 : p.warn) == null || g.call(p, "Unable to mark node as truncated", { error: b });
|
|
334
371
|
}
|
|
335
372
|
else
|
|
336
373
|
try {
|
|
337
374
|
this.contextNode.setAttribute("_truncated", !1);
|
|
338
|
-
} catch (
|
|
339
|
-
(
|
|
375
|
+
} catch (b) {
|
|
376
|
+
(v = (h = this.client.logger) == null ? void 0 : h.warn) == null || v.call(h, "Unable to clear truncated marker on node", { error: b });
|
|
340
377
|
}
|
|
341
|
-
return ((
|
|
378
|
+
return ((y = n.content) == null ? void 0 : y.trim()) || "";
|
|
342
379
|
}
|
|
343
380
|
}
|
|
344
381
|
export {
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { BaseProvider as
|
|
2
|
-
|
|
1
|
+
import { BaseProvider as d } from "./BaseProvider.js";
|
|
2
|
+
const _ = /claude-(instant|v?2)\b/;
|
|
3
|
+
class b extends d {
|
|
3
4
|
async detectCapabilities() {
|
|
4
|
-
|
|
5
|
-
((t = this.config.model) != null && t.includes("claude-3") || (e = this.config.model) != null && e.includes("claude-sonnet") || (o = this.config.model) != null && o.includes("claude-opus") || (s = this.config.model) != null && s.includes("claude-haiku")) && (this.capabilities.add("vision"), this.capabilities.add("tools"));
|
|
5
|
+
_.test(this.config.model || "") || (this.capabilities.add("vision"), this.capabilities.add("tools"));
|
|
6
6
|
}
|
|
7
7
|
prepareRequest(t, e) {
|
|
8
|
-
const o = m(t), s =
|
|
9
|
-
model:
|
|
8
|
+
const o = m(t), s = {
|
|
9
|
+
model: e.model || this.config.model || "claude-3-sonnet-20240229",
|
|
10
10
|
max_tokens: e.maxTokens || 1e3,
|
|
11
11
|
messages: o,
|
|
12
12
|
stream: e.stream || !1
|
|
13
13
|
};
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
return
|
|
14
|
+
this.applySamplingParams(s, e);
|
|
15
|
+
const a = t.find((r) => r.role === "system");
|
|
16
|
+
return a && (s.system = a.content), e.tools && this.capabilities.has("tools") && (s.tools = y(e.tools), e.tool_choice && (s.tool_choice = e.tool_choice)), this.promptCachingEnabled(e) && k(s, e), s;
|
|
17
17
|
}
|
|
18
18
|
// Prompt caching is on by default for the Claude/Anthropic-family path
|
|
19
19
|
// (Bedrock inherits this method) because it's strictly cheaper and degrades
|
|
@@ -30,23 +30,23 @@ class b extends g {
|
|
|
30
30
|
addImagesToMessages(t, e) {
|
|
31
31
|
const o = t[t.length - 1];
|
|
32
32
|
if (o && o.role === "user") {
|
|
33
|
-
const
|
|
34
|
-
e.forEach((
|
|
35
|
-
|
|
33
|
+
const i = [{ type: "text", text: o.content }];
|
|
34
|
+
e.forEach((s) => {
|
|
35
|
+
i.push({
|
|
36
36
|
type: "image",
|
|
37
|
-
source: { type: "base64", media_type: "image/jpeg", data: typeof
|
|
37
|
+
source: { type: "base64", media_type: "image/jpeg", data: typeof s == "string" ? s : s.data }
|
|
38
38
|
});
|
|
39
|
-
}), o.content =
|
|
39
|
+
}), o.content = i;
|
|
40
40
|
}
|
|
41
41
|
return t;
|
|
42
42
|
}
|
|
43
43
|
processResponse(t) {
|
|
44
|
-
const e = h(t.stop_reason), o = t.content || [],
|
|
44
|
+
const e = h(t.stop_reason), o = t.content || [], i = o.find((r) => r.type === "text"), a = o.filter((r) => r.type === "tool_use").map((r) => ({ id: r.id, name: r.name, args: r.input || {} }));
|
|
45
45
|
return {
|
|
46
|
-
content: (
|
|
46
|
+
content: (i == null ? void 0 : i.text) || "",
|
|
47
47
|
usage: f(t.usage),
|
|
48
48
|
finishReason: e,
|
|
49
|
-
toolCalls:
|
|
49
|
+
toolCalls: a
|
|
50
50
|
};
|
|
51
51
|
}
|
|
52
52
|
parseStreamingLine(t) {
|
|
@@ -60,27 +60,27 @@ class b extends g {
|
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
extractStreamingContent(t) {
|
|
63
|
-
var e, o,
|
|
63
|
+
var e, o, i, s, a, r, p, g;
|
|
64
64
|
if (t.done) return { done: !0 };
|
|
65
65
|
if (t.type === "error") {
|
|
66
|
-
const
|
|
67
|
-
throw u.code =
|
|
66
|
+
const c = ((e = t.error) == null ? void 0 : e.type) || "anthropic_error", u = new Error(((o = t.error) == null ? void 0 : o.message) || "Anthropic streaming error");
|
|
67
|
+
throw u.code = c, t.request_id && (u.requestId = t.request_id), u;
|
|
68
68
|
}
|
|
69
69
|
if (t.type === "message_start")
|
|
70
|
-
return { content: "", thinking: "", done: !1, usage: f((
|
|
70
|
+
return { content: "", thinking: "", done: !1, usage: f((i = t.message) == null ? void 0 : i.usage), finishReason: null };
|
|
71
71
|
if (t.type === "content_block_start") {
|
|
72
|
-
const
|
|
73
|
-
return (
|
|
72
|
+
const c = t.content_block;
|
|
73
|
+
return (c == null ? void 0 : c.type) === "tool_use" ? {
|
|
74
74
|
content: "",
|
|
75
75
|
done: !1,
|
|
76
|
-
toolCallDelta: { index: t.index, id:
|
|
76
|
+
toolCallDelta: { index: t.index, id: c.id, name: c.name, argsTextDelta: "" }
|
|
77
77
|
} : null;
|
|
78
78
|
}
|
|
79
|
-
return t.type === "content_block_delta" ? ((
|
|
79
|
+
return t.type === "content_block_delta" ? ((s = t.delta) == null ? void 0 : s.type) === "text_delta" ? { content: ((a = t.delta) == null ? void 0 : a.text) || "", thinking: "", done: !1, usage: null, finishReason: null } : ((r = t.delta) == null ? void 0 : r.type) === "input_json_delta" ? {
|
|
80
80
|
content: "",
|
|
81
81
|
done: !1,
|
|
82
|
-
toolCallDelta: { index: t.index, argsTextDelta: ((
|
|
83
|
-
} : null : t.type === "message_delta" ? { content: "", thinking: "", done: !1, usage: f(t.usage), finishReason: h((
|
|
82
|
+
toolCallDelta: { index: t.index, argsTextDelta: ((p = t.delta) == null ? void 0 : p.partial_json) || "" }
|
|
83
|
+
} : null : t.type === "message_delta" ? { content: "", thinking: "", done: !1, usage: f(t.usage), finishReason: h((g = t.delta) == null ? void 0 : g.stop_reason) } : t.type === "message_stop" ? { content: "", thinking: "", done: !0, usage: null, finishReason: h(t.stop_reason) } : null;
|
|
84
84
|
}
|
|
85
85
|
getApiPath() {
|
|
86
86
|
return "/v1/messages";
|
|
@@ -106,9 +106,6 @@ class b extends g {
|
|
|
106
106
|
return ((e = t.data) == null ? void 0 : e.map((o) => o.id)) || [];
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
|
-
function _(n) {
|
|
110
|
-
return /claude-opus-4-[789]/.test(n);
|
|
111
|
-
}
|
|
112
109
|
function h(n) {
|
|
113
110
|
if (!n) return null;
|
|
114
111
|
const t = String(n).toLowerCase();
|
|
@@ -116,8 +113,8 @@ function h(n) {
|
|
|
116
113
|
}
|
|
117
114
|
function f(n) {
|
|
118
115
|
if (!n) return null;
|
|
119
|
-
const t = n.input_tokens ?? 0, e = n.cache_read_input_tokens ?? 0, o = n.cache_creation_input_tokens ?? 0,
|
|
120
|
-
return n.cache_read_input_tokens != null && (
|
|
116
|
+
const t = n.input_tokens ?? 0, e = n.cache_read_input_tokens ?? 0, o = n.cache_creation_input_tokens ?? 0, s = n.input_tokens != null || n.cache_read_input_tokens != null || n.cache_creation_input_tokens != null ? t + e + o : 0, a = n.output_tokens ?? 0, r = { inputTokens: s, outputTokens: a, totalTokens: s + a, raw: n };
|
|
117
|
+
return n.cache_read_input_tokens != null && (r.cachedInputTokens = e), n.cache_creation_input_tokens != null && (r.cacheCreationInputTokens = o), r;
|
|
121
118
|
}
|
|
122
119
|
function m(n) {
|
|
123
120
|
const t = [];
|
|
@@ -126,12 +123,12 @@ function m(n) {
|
|
|
126
123
|
if (e.role === "assistant" && Array.isArray(e.tool_calls) && e.tool_calls.length) {
|
|
127
124
|
const o = [];
|
|
128
125
|
e.content && o.push({ type: "text", text: e.content });
|
|
129
|
-
for (const
|
|
126
|
+
for (const i of e.tool_calls)
|
|
130
127
|
o.push({
|
|
131
128
|
type: "tool_use",
|
|
132
|
-
id:
|
|
133
|
-
name:
|
|
134
|
-
input:
|
|
129
|
+
id: i.id,
|
|
130
|
+
name: i.name,
|
|
131
|
+
input: i.args || {}
|
|
135
132
|
});
|
|
136
133
|
t.push({ role: "assistant", content: o });
|
|
137
134
|
continue;
|
|
@@ -196,6 +193,5 @@ function A(n) {
|
|
|
196
193
|
}
|
|
197
194
|
export {
|
|
198
195
|
b as AnthropicProvider,
|
|
199
|
-
f as normalizeAnthropicUsage
|
|
200
|
-
_ as samplingParamsRemoved
|
|
196
|
+
f as normalizeAnthropicUsage
|
|
201
197
|
};
|