@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.
@@ -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
- ], n = this.validateCapabilities({
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
- }), o = this.provider.prepareRequest(e, n), l = await this.provider.makeRequest(o);
45
- return (r = this.provider.processResponse(l).content) == null ? void 0 : r.trim();
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 n;
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 (n = this.provider.processResponse(r).content) == null ? void 0 : n.trim();
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 = "", n = null;
93
- return await this.provider.streamRequest(r, i, (o) => {
94
- e = o.fullContent, o.fullUsage && (n = o.fullUsage);
95
- const l = o.fullUsage ? { ...o, cost: this.costFor(o.fullUsage, { provider: this.config.provider, model: i.model }) } : o;
96
- s && s(l);
97
- }), { content: e, usage: n, cost: this.costFor(n, { provider: this.config.provider, model: i.model }) };
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: n,
121
- model: o,
122
- maxTokens: l,
123
- enableThinking: d
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 c = t.slice();
128
- let p = 0, m = null;
129
- const f = { inputTokens: 0, outputTokens: 0, totalTokens: 0, cachedInputTokens: 0, cacheCreationInputTokens: 0, reasoningTokens: 0 };
130
- let b = !1;
131
- for (; p < e; ) {
132
- p++, r && r({ type: "iter-start", iter: p });
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: c,
151
+ messages: p,
135
152
  tools: s,
136
153
  stream: !0,
137
- model: o || this.config.model,
138
- temperature: n ?? this.config.temperature,
139
- maxTokens: l ?? this.config.maxTokens ?? 4096,
140
- enableThinking: d ?? !1,
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
- }), h = await this.provider.streamRequest(c, x, (a) => {
146
- a.content && r && r({ type: "text-delta", text: a.content }), a.toolCallDelta && r && r({ type: "tool-call-delta", delta: a.toolCallDelta });
147
- }), q = (h == null ? void 0 : h.content) || "", y = (h == null ? void 0 : h.toolCalls) || [], C = (h == null ? void 0 : h.thinking) || "", w = (h == null ? void 0 : h.usage) || null;
148
- if (w) {
149
- b = !0;
150
- for (const a of Object.keys(f))
151
- w[a] != null && (f[a] += w[a]);
152
- r && r({ type: "usage", usage: w, cost: this.costFor(w, { provider: this.config.provider, model: x.model }) });
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 (y.length && (k.tool_calls = y), C && (k.thinking = C), c.push(k), r && r({ type: "assistant-message", content: q, toolCalls: y, thinking: C }), !y.length) {
156
- m = "no-tool-calls";
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 y) {
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 T;
163
- if (!R)
164
- T = `Error: unknown tool '${a.name}'`;
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 u = await R(a.args || {});
168
- T = typeof u == "string" ? u : JSON.stringify(u ?? "");
169
- } catch (u) {
170
- T = `Error: ${(u == null ? void 0 : u.message) || String(u)}`;
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
- c.push({ role: "tool", tool_call_id: a.id, content: T }), r && r({ type: "tool-result", id: a.id, name: a.name, content: T });
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
- m || (m = "max-iters");
176
- const g = b ? f : null, v = this.costFor(g, { provider: this.config.provider, model: o || this.config.model });
177
- return r && r({ type: "stop", reason: m, iterations: p, usage: g, cost: v }), { messages: c, iterations: p, stopReason: m, usage: g, cost: v };
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((n) => n.id).join(", ");
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(n, o = {}) {
218
- return new $(r, t, n, o, s, i);
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, n) {
225
- this.client = t, this.contextNode = s, this.prompt = i, this.options = r || {}, this.originatingCode = e, this.defaultPresetName = n, this.targetNode = null, this.targetAttributes = {}, this.operationName = null, this._executed = !1, this._promise = null;
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 o;
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 n = {
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 d = r({ ...n, stream: !0 });
263
- return await this._streamIntoTarget(e, d, s, i);
299
+ const m = r({ ...o, stream: !0 });
300
+ return await this._streamIntoTarget(e, m, s, i);
264
301
  }
265
- const l = r({ ...n, stream: !1 });
266
- return await this._promiseResponse(e, l, s, i);
302
+ const c = r({ ...o, stream: !1 });
303
+ return await this._promiseResponse(e, c, s, i);
267
304
  } finally {
268
- (o = t.cleanup) == null || o.call(t);
305
+ (n = t.cleanup) == null || n.call(t);
269
306
  }
270
307
  }
271
308
  async _createExecutionContext() {
272
- var r, e, n, o;
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 l = (((o = (n = this.client.configStore).getEnabledConfigs) == null ? void 0 : o.call(n)) || []).map((c) => c.name || `${c.provider} (${c.baseUrl})`).filter(Boolean), d = l.length ? ` Available presets: ${l.join(", ")}` : " No configured providers found.";
282
- throw new Error(`Provider preset '${t}' not found.${d}`);
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 l;
287
- return (l = i.cancelAllRequests) == null ? void 0 : l.call(i);
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 (n) {
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: n });
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, m, f, b, g, v, x, h, q, y, C, w, k, a, R, T;
305
- let e, n;
306
- typeof this.targetNode == "string" ? (n = this.targetNode, e = this.contextNode.createChild(n), (p = e.setSelected) == null || p.call(e)) : (e = this.targetNode, n = ((m = e.getText) == null ? void 0 : m.call(e)) || ""), this._applyAttributes(e);
307
- const o = r == null ? void 0 : r.name;
308
- o && e.addTag && e.addTag("provider", o), (f = e.addLLMLog) == null || f.call(e, this.operationName || "streaming-request", {
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
- }), (b = e.setStreamingState) == null || b.call(e, !0), e._activeRequestId = s.requestId;
314
- let l = "", d = null, c = null;
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
- _.content && (l = _.fullContent, (I = e.setText) == null || I.call(e, l)), _.fullUsage && (c = _.fullUsage), _.finishReason && (d = _.finishReason), _.done && ((S = e.setStreamingState) == null || S.call(e, !1), e._activeRequestId = null);
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 u = (v = (g = this.client).costFor) == null ? void 0 : v.call(g, c, { provider: r == null ? void 0 : r.provider, model: s.model });
321
- return (x = e.addLLMLog) == null || x.call(e, (this.operationName || "streaming-request") + "-response", { requestId: s.requestId }, { content: l, usage: c, cost: u }), d === "length" ? ((h = e.setAttribute) == null || h.call(e, "_truncated", !0), (q = e.addLog) == null || q.call(e, "Response truncated. Increase Max Tokens.", "warn", { finish_reason: "length" })) : (y = e.setAttribute) == null || y.call(e, "_truncated", !1), await ((C = e.persistNow) == null ? void 0 : C.call(e)), e;
322
- } catch (u) {
323
- throw (w = e.addLLMLog) == null || w.call(e, (this.operationName || "streaming-request") + "-error", { requestId: s.requestId }, null, u), (k = e.setStreamingState) == null || k.call(e, !1), e._activeRequestId = null, (a = e.setText) == null || a.call(e, `Error: ${u.message}`), (R = e.setAttribute) == null || R.call(e, "_truncated", !1), await ((T = e.persistNow) == null ? void 0 : T.call(e)), u;
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 d, c, p, m, f, b, g;
328
- const e = i.prepareRequest(t, s), n = await i.makeRequest(e), o = i.processResponse(n);
329
- if ((o.finishReason || ((c = (d = n == null ? void 0 : n.choices) == null ? void 0 : d[0]) == null ? void 0 : c.finish_reason) || null) === "length")
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 (v) {
333
- (m = (p = this.client.logger) == null ? void 0 : p.warn) == null || m.call(p, "Unable to mark node as truncated", { error: v });
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 (v) {
339
- (b = (f = this.client.logger) == null ? void 0 : f.warn) == null || b.call(f, "Unable to clear truncated marker on node", { error: v });
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 ((g = o.content) == null ? void 0 : g.trim()) || "";
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 g } from "./BaseProvider.js";
2
- class b extends g {
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
- var t, e, o, s;
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 = e.model || this.config.model || "claude-3-sonnet-20240229", r = {
9
- model: s,
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
- _(s) || (r.temperature = e.temperature ?? 0.7);
15
- const c = t.find((i) => i.role === "system");
16
- return c && (r.system = c.content), e.tools && this.capabilities.has("tools") && (r.tools = y(e.tools), e.tool_choice && (r.tool_choice = e.tool_choice)), this.promptCachingEnabled(e) && k(r, e), r;
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 s = [{ type: "text", text: o.content }];
34
- e.forEach((r) => {
35
- s.push({
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 r == "string" ? r : r.data }
37
+ source: { type: "base64", media_type: "image/jpeg", data: typeof s == "string" ? s : s.data }
38
38
  });
39
- }), o.content = s;
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 || [], s = o.find((i) => i.type === "text"), c = o.filter((i) => i.type === "tool_use").map((i) => ({ id: i.id, name: i.name, args: i.input || {} }));
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: (s == null ? void 0 : s.text) || "",
46
+ content: (i == null ? void 0 : i.text) || "",
47
47
  usage: f(t.usage),
48
48
  finishReason: e,
49
- toolCalls: c
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, s, r, c, i, d, p;
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 a = ((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 = a, t.request_id && (u.requestId = t.request_id), u;
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((s = t.message) == null ? void 0 : s.usage), finishReason: null };
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 a = t.content_block;
73
- return (a == null ? void 0 : a.type) === "tool_use" ? {
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: a.id, name: a.name, argsTextDelta: "" }
76
+ toolCallDelta: { index: t.index, id: c.id, name: c.name, argsTextDelta: "" }
77
77
  } : null;
78
78
  }
79
- return t.type === "content_block_delta" ? ((r = t.delta) == null ? void 0 : r.type) === "text_delta" ? { content: ((c = t.delta) == null ? void 0 : c.text) || "", thinking: "", done: !1, usage: null, finishReason: null } : ((i = t.delta) == null ? void 0 : i.type) === "input_json_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: ((d = t.delta) == null ? void 0 : d.partial_json) || "" }
83
- } : null : t.type === "message_delta" ? { content: "", thinking: "", done: !1, usage: f(t.usage), finishReason: h((p = t.delta) == null ? void 0 : p.stop_reason) } : t.type === "message_stop" ? { content: "", thinking: "", done: !0, usage: null, finishReason: h(t.stop_reason) } : null;
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, r = n.input_tokens != null || n.cache_read_input_tokens != null || n.cache_creation_input_tokens != null ? t + e + o : 0, c = n.output_tokens ?? 0, i = { inputTokens: r, outputTokens: c, totalTokens: r + c, raw: n };
120
- return n.cache_read_input_tokens != null && (i.cachedInputTokens = e), n.cache_creation_input_tokens != null && (i.cacheCreationInputTokens = o), i;
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 s of e.tool_calls)
126
+ for (const i of e.tool_calls)
130
127
  o.push({
131
128
  type: "tool_use",
132
- id: s.id,
133
- name: s.name,
134
- input: s.args || {}
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
  };