@broberg/ai-sdk 0.78.5 → 0.78.6

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/index.d.ts CHANGED
@@ -1525,6 +1525,10 @@ interface OpenAICompatibleConfig {
1525
1525
  /** F074.3 — this provider accepts `reasoning_effort` (Mistral). Anywhere else a
1526
1526
  * reasoningEffort is REFUSED, like `prefix`: a dropped flag reads as obeyed. */
1527
1527
  supportsReasoningEffort?: boolean;
1528
+ /** F101 — this provider takes reasoning as an OBJECT, `reasoning:{effort}` or
1529
+ * `reasoning:{enabled:false}` (OpenRouter), not Mistral's flat `reasoning_effort`.
1530
+ * A reasoningEffort is mapped onto it instead of being refused. */
1531
+ reasoningObject?: boolean;
1528
1532
  }
1529
1533
  declare function makeOpenAICompatibleAdapter(config: OpenAICompatibleConfig): ProviderAdapter;
1530
1534
 
@@ -1596,8 +1600,8 @@ declare const falStubAdapter: ProviderAdapter;
1596
1600
  * wires the live adapters. */
1597
1601
  declare const stubProviders: Record<string, ProviderAdapter>;
1598
1602
 
1599
- declare const VERSION: "0.78.5";
1600
- declare const SDK_TAG: "@broberg/ai-sdk@0.78.5";
1603
+ declare const VERSION: "0.78.6";
1604
+ declare const SDK_TAG: "@broberg/ai-sdk@0.78.6";
1601
1605
 
1602
1606
  /** Built-in defaults. Every entry is overridable via AiConfig.defaults or a
1603
1607
  * per-call override.
package/dist/index.js CHANGED
@@ -1059,6 +1059,9 @@ function mapAnthropicStop(reason) {
1059
1059
 
1060
1060
  // src/providers/openai-compatible.ts
1061
1061
  var REASONS_BY_DEFAULT = /* @__PURE__ */ new Set(["mistral-large-4", "mistral-large-4-0"]);
1062
+ function openrouterDefaultReasoning(model) {
1063
+ return model.startsWith("deepseek/deepseek-v4") && model.includes("flash") ? "none" : void 0;
1064
+ }
1062
1065
  function autoCacheKey(messages) {
1063
1066
  const system = messages.find((m) => m.role === "system");
1064
1067
  if (!system || typeof system.content !== "string" || system.content.length < 200) return void 0;
@@ -1166,11 +1169,17 @@ function buildChatBody(req, config) {
1166
1169
  if (req.maxTokens !== void 0) body.max_tokens = req.maxTokens;
1167
1170
  if (req.temperature !== void 0) body.temperature = req.temperature;
1168
1171
  if (req.responseFormat === "json") body.response_format = { type: "json_object" };
1169
- if (req.reasoningEffort !== void 0 && !config.supportsReasoningEffort) {
1170
- throw new Error(`${config.name} adapter: reasoningEffort is a Mistral option and is not supported here`);
1172
+ if (req.reasoningEffort !== void 0 && !config.supportsReasoningEffort && !config.reasoningObject) {
1173
+ throw new Error(`${config.name} adapter: reasoningEffort is a Mistral/OpenRouter option and is not supported here`);
1174
+ }
1175
+ if (config.reasoningObject) {
1176
+ const eff = req.reasoningEffort ?? openrouterDefaultReasoning(req.spec.model);
1177
+ if (eff === "none") body.reasoning = { enabled: false };
1178
+ else if (eff !== void 0) body.reasoning = { effort: eff };
1179
+ } else {
1180
+ const effort = req.reasoningEffort ?? (config.supportsReasoningEffort && REASONS_BY_DEFAULT.has(req.spec.model) ? "low" : void 0);
1181
+ if (effort !== void 0) body.reasoning_effort = effort;
1171
1182
  }
1172
- const effort = req.reasoningEffort ?? (config.supportsReasoningEffort && REASONS_BY_DEFAULT.has(req.spec.model) ? "low" : void 0);
1173
- if (effort !== void 0) body.reasoning_effort = effort;
1174
1183
  if (config.supportsPromptCacheKey && req.promptCache !== false) {
1175
1184
  const k = req.promptCacheKey ?? autoCacheKey(req.messages);
1176
1185
  if (k !== void 0) body.prompt_cache_key = k;
@@ -1208,6 +1217,11 @@ function makeOpenAICompatibleAdapter(config) {
1208
1217
  const toolCalls = msg?.tool_calls?.map(
1209
1218
  (tc) => fromProviderToolCall(tc, "openai")
1210
1219
  );
1220
+ if (text.length === 0 && !(toolCalls && toolCalls.length > 0) && hasReasoning(msg)) {
1221
+ throw new Error(
1222
+ `${config.name} chat: the model spent its whole output budget thinking and returned no answer (${data.usage?.completion_tokens ?? "?"} completion tokens, empty content). Raise maxTokens, or bound the thinking with reasoningEffort ("low" or "none").`
1223
+ );
1224
+ }
1211
1225
  const cachedIn = data.usage?.prompt_tokens_details?.cached_tokens;
1212
1226
  const usage = freshUsage({
1213
1227
  provider: config.name,
@@ -1266,6 +1280,8 @@ function makeOpenAICompatibleAdapter(config) {
1266
1280
  });
1267
1281
  const toolAcc = /* @__PURE__ */ new Map();
1268
1282
  let finishReason = null;
1283
+ let sawReasoning = false;
1284
+ let emittedText = false;
1269
1285
  for await (const data of stream) {
1270
1286
  let chunk;
1271
1287
  try {
@@ -1276,10 +1292,14 @@ function makeOpenAICompatibleAdapter(config) {
1276
1292
  const choice = chunk.choices?.[0];
1277
1293
  if (choice) {
1278
1294
  const delta = choice.delta ?? {};
1295
+ if (hasReasoning(delta)) sawReasoning = true;
1279
1296
  const piece = contentToText(delta.content);
1280
1297
  if (piece.length > 0) {
1281
1298
  const out = stripStreamPrefix(piece);
1282
- if (out.length > 0) yield { type: "text", delta: out };
1299
+ if (out.length > 0) {
1300
+ emittedText = true;
1301
+ yield { type: "text", delta: out };
1302
+ }
1283
1303
  }
1284
1304
  for (const tc of delta.tool_calls ?? []) {
1285
1305
  const idx = tc.index ?? 0;
@@ -1323,7 +1343,8 @@ function makeOpenAICompatibleAdapter(config) {
1323
1343
  }
1324
1344
  yield { type: "tool_call", id: t.id, name: t.name, args };
1325
1345
  }
1326
- yield { type: "finish", reason: mapFinishReason(finishReason) };
1346
+ const reason = mapFinishReason(finishReason);
1347
+ yield { type: "finish", reason: sawReasoning && !emittedText && toolAcc.size === 0 && reason !== "tool_calls" ? "length" : reason };
1327
1348
  }
1328
1349
  return {
1329
1350
  name: config.name,
@@ -1344,6 +1365,11 @@ function contentToText(content) {
1344
1365
  }
1345
1366
  return "";
1346
1367
  }
1368
+ function hasReasoning(m) {
1369
+ if (!m) return false;
1370
+ if (typeof m.reasoning === "string" && m.reasoning.length > 0) return true;
1371
+ return Array.isArray(m.reasoning_details) && m.reasoning_details.length > 0;
1372
+ }
1347
1373
  function mapFinishReason(reason) {
1348
1374
  switch (reason) {
1349
1375
  case "tool_calls":
@@ -1743,7 +1769,9 @@ function openrouterAdapter(config = {}) {
1743
1769
  fetch: config.fetch,
1744
1770
  // OpenRouter returns ground-truth usage.cost (USD) when usage:{include:true}
1745
1771
  // is set — use it over the local pricing-table estimate (F010).
1746
- costFromResponseField: true
1772
+ costFromResponseField: true,
1773
+ // F101 — OpenRouter takes reasoning as an object ({effort} / {enabled:false}).
1774
+ reasoningObject: true
1747
1775
  });
1748
1776
  async function image(req) {
1749
1777
  const apiKey = config.apiKey ?? env().OPENROUTER_API_KEY;
@@ -4609,8 +4637,8 @@ var aiConfigSchema = z.object({
4609
4637
  import { randomUUID } from "crypto";
4610
4638
 
4611
4639
  // src/version.ts
4612
- var VERSION = "0.78.5";
4613
- var SDK_TAG = "@broberg/ai-sdk@0.78.5";
4640
+ var VERSION = "0.78.6";
4641
+ var SDK_TAG = "@broberg/ai-sdk@0.78.6";
4614
4642
 
4615
4643
  // src/cost/sinks/upmetrics.ts
4616
4644
  function isRetryableStatus(status) {