@plurnk/plurnk-providers 1.16.5 → 1.18.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.
Files changed (73) hide show
  1. package/.env.defaults +87 -200
  2. package/README.md +2 -1
  3. package/SPEC.md +73 -66
  4. package/dist/AiSdkProvider.d.ts +0 -1
  5. package/dist/AiSdkProvider.d.ts.map +1 -1
  6. package/dist/AiSdkProvider.js +25 -55
  7. package/dist/AiSdkProvider.js.map +1 -1
  8. package/dist/AiSdkRequestBody.d.ts +0 -1
  9. package/dist/AiSdkRequestBody.d.ts.map +1 -1
  10. package/dist/AiSdkRequestBody.js +1 -17
  11. package/dist/AiSdkRequestBody.js.map +1 -1
  12. package/dist/LeadingReasoning.d.ts +16 -0
  13. package/dist/LeadingReasoning.d.ts.map +1 -0
  14. package/dist/LeadingReasoning.js +81 -0
  15. package/dist/LeadingReasoning.js.map +1 -0
  16. package/dist/aiSdkTransport.d.ts +7 -0
  17. package/dist/aiSdkTransport.d.ts.map +1 -1
  18. package/dist/aiSdkTransport.js +19 -5
  19. package/dist/aiSdkTransport.js.map +1 -1
  20. package/dist/catalogProvider.d.ts +3 -1
  21. package/dist/catalogProvider.d.ts.map +1 -1
  22. package/dist/catalogProvider.js +24 -18
  23. package/dist/catalogProvider.js.map +1 -1
  24. package/dist/compatibleProvider.d.ts.map +1 -1
  25. package/dist/compatibleProvider.js +0 -3
  26. package/dist/compatibleProvider.js.map +1 -1
  27. package/dist/env.d.ts.map +1 -1
  28. package/dist/env.js +0 -1
  29. package/dist/env.js.map +1 -1
  30. package/dist/errors.d.ts +6 -0
  31. package/dist/errors.d.ts.map +1 -1
  32. package/dist/errors.js +46 -15
  33. package/dist/errors.js.map +1 -1
  34. package/dist/index.d.ts +2 -2
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +2 -1
  37. package/dist/index.js.map +1 -1
  38. package/dist/providerError.d.ts +1 -1
  39. package/dist/providerError.d.ts.map +1 -1
  40. package/dist/providerError.js +3 -0
  41. package/dist/providerError.js.map +1 -1
  42. package/dist/sdkModels.d.ts +1 -13
  43. package/dist/sdkModels.d.ts.map +1 -1
  44. package/dist/sdkModels.js +2 -94
  45. package/dist/sdkModels.js.map +1 -1
  46. package/dist/types.d.ts.map +1 -1
  47. package/dist/usage.d.ts.map +1 -1
  48. package/dist/usage.js +18 -9
  49. package/dist/usage.js.map +1 -1
  50. package/docs/models.md +90 -0
  51. package/package.json +6 -6
  52. package/src/AiSdkProvider.test.ts +99 -38
  53. package/src/AiSdkProvider.ts +24 -78
  54. package/src/AiSdkRequestBody.ts +1 -16
  55. package/src/LeadingReasoning.test.ts +34 -0
  56. package/src/LeadingReasoning.ts +87 -0
  57. package/src/ProviderRegistry.test.ts +19 -1
  58. package/src/aiSdkTransport.test.ts +56 -0
  59. package/src/aiSdkTransport.ts +31 -5
  60. package/src/boundaries.test.ts +1 -0
  61. package/src/catalogProvider.test.ts +110 -20
  62. package/src/catalogProvider.ts +40 -18
  63. package/src/compatibleProvider.ts +0 -3
  64. package/src/env.test.ts +1 -0
  65. package/src/env.ts +1 -2
  66. package/src/errors.test.ts +58 -7
  67. package/src/errors.ts +55 -18
  68. package/src/index.ts +2 -2
  69. package/src/providerError.ts +4 -0
  70. package/src/sdkModels.test.ts +1 -42
  71. package/src/sdkModels.ts +1 -123
  72. package/src/types.ts +8 -10
  73. package/src/usage.ts +23 -15
@@ -0,0 +1,87 @@
1
+ export type ReasoningEnvelope = readonly [opening: string, closing: string];
2
+
3
+ // {§provider-tagged-reasoning} One leading envelope, shared by incremental
4
+ // observation and settled response projection. Only an undecided delimiter is held.
5
+ export default class LeadingReasoning {
6
+ static readonly THINK: readonly ReasoningEnvelope[] = [["<think>", "</think>"]];
7
+ static readonly TEMPLATE: readonly ReasoningEnvelope[] = [
8
+ ["<|channel>thought\n", "<channel|>"],
9
+ ["<think>\n", "</think>"],
10
+ ];
11
+
12
+ readonly #envelopes: readonly ReasoningEnvelope[];
13
+ #pending = "";
14
+ #closing: string | null = null;
15
+ #closed = false;
16
+ #projected = false;
17
+ #reasoning = "";
18
+ #content = "";
19
+ #contentStart = 0;
20
+
21
+ constructor(envelopes: readonly ReasoningEnvelope[]) {
22
+ this.#envelopes = envelopes;
23
+ }
24
+
25
+ push(text: string): string {
26
+ if (this.#closed) {
27
+ this.#content += text;
28
+ return "";
29
+ }
30
+ this.#pending += text;
31
+ if (this.#closing === null) {
32
+ const envelope = this.#envelopes.find(([opening]) => this.#pending.startsWith(opening));
33
+ if (envelope === undefined) {
34
+ if (!this.#envelopes.some(([opening]) => opening.startsWith(this.#pending))) {
35
+ this.#content = this.#pending;
36
+ this.#pending = "";
37
+ this.#closed = true;
38
+ }
39
+ return "";
40
+ }
41
+ const [opening, closing] = envelope;
42
+ this.#pending = this.#pending.slice(opening.length);
43
+ this.#closing = closing;
44
+ this.#projected = true;
45
+ this.#contentStart = [...opening].length;
46
+ }
47
+ const end = this.#pending.indexOf(this.#closing);
48
+ if (end !== -1) {
49
+ const delta = this.#emit(this.#pending.slice(0, end));
50
+ this.#contentStart += [...this.#closing].length;
51
+ this.#content = this.#pending.slice(end + this.#closing.length);
52
+ this.#pending = "";
53
+ this.#closed = true;
54
+ return delta;
55
+ }
56
+ let held = Math.min(this.#pending.length, this.#closing.length - 1);
57
+ while (held > 0 && !this.#pending.endsWith(this.#closing.slice(0, held))) held--;
58
+ const split = this.#pending.length - held;
59
+ const delta = this.#emit(this.#pending.slice(0, split));
60
+ this.#pending = this.#pending.slice(split);
61
+ return delta;
62
+ }
63
+
64
+ finish(): string {
65
+ const delta = this.#projected ? this.#emit(this.#pending) : "";
66
+ if (!this.#projected) this.#content += this.#pending;
67
+ this.#pending = "";
68
+ this.#closed = true;
69
+ return delta;
70
+ }
71
+
72
+ #emit(delta: string): string {
73
+ this.#reasoning += delta;
74
+ this.#contentStart += [...delta].length;
75
+ return delta;
76
+ }
77
+
78
+ static project(content: string, reasoning: string, envelopes: readonly ReasoningEnvelope[]): {
79
+ content: string; reasoning: string; projected: boolean; contentStart: number;
80
+ } {
81
+ if (reasoning.length > 0) return { content, reasoning, projected: false, contentStart: 0 };
82
+ const parser = new LeadingReasoning(envelopes);
83
+ parser.push(content);
84
+ parser.finish();
85
+ return { content: parser.#content, reasoning: parser.#reasoning, projected: parser.#projected, contentStart: parser.#contentStart };
86
+ }
87
+ }
@@ -1,6 +1,22 @@
1
1
  import test, { mock } from "node:test";
2
2
  import { strict as assert } from "node:assert";
3
3
  import { instantiateProvider, loadActiveProvider, resetDiscoveryCache } from "./ProviderRegistry.ts";
4
+ import { resolveModel } from "@plurnk/plurnk-models";
5
+ import { calculateCostUsdDecimal } from "./usage.ts";
6
+
7
+ // A rate literal breaks at every catalog refresh (the 1.17.0 stamp moved DeepSeek's rates); the
8
+ // claim under test is that the estimate is the catalog's rates applied to the reported usage.
9
+ const catalogRatesOf = (provider: string, model: string) => {
10
+ const cost = resolveModel(provider, model)?.info.cost;
11
+ if (cost === undefined) throw new Error(`${provider}/${model} has no catalog rates`);
12
+ return {
13
+ input: cost.inputPer1M,
14
+ output: cost.outputPer1M,
15
+ ...(cost.reasoningPer1M === undefined ? {} : { reasoning: cost.reasoningPer1M }),
16
+ ...(cost.cacheReadPer1M === undefined ? {} : { cacheRead: cost.cacheReadPer1M }),
17
+ ...(cost.cacheWritePer1M === undefined ? {} : { cacheWrite: cost.cacheWritePer1M }),
18
+ };
19
+ };
4
20
  import type { PluginAttributionContext } from "@plurnk/plurnk-meta";
5
21
 
6
22
  const mapOf = (entries: Record<string, string>, skipped: Record<string, string> = {}) =>
@@ -279,9 +295,11 @@ test("{§deepseek-reasoning-request} #157: direct DeepSeek composes catalog fact
279
295
  totalTokens: 12,
280
296
  inputTokenDetails: { noCacheTokens: 2, cacheReadTokens: 8 },
281
297
  });
298
+ const expected = calculateCostUsdDecimal(response.accounting[0]!.usage!, catalogRatesOf("deepseek", "deepseek-v4-flash"));
299
+ assert.match(String(expected), /^0\.0*[1-9]/, "the catalog prices this usage");
282
300
  assert.deepEqual(response.accounting[0]?.cost, {
283
301
  kind: "estimated",
284
- amount: { amount: "0.0000008624", currency: "USD" },
302
+ amount: { amount: expected, currency: "USD" },
285
303
  source: "Models.dev catalog rates",
286
304
  });
287
305
  mock.restoreAll();
@@ -360,3 +360,59 @@ test("normalizeRetryAttemptError — only provider-directed waits retry: 429, Re
360
360
  });
361
361
  assert.equal((normalizeRetryAttemptError(bareServerError) as APICallError).isRetryable, false, "a bare 5xx surfaces at once for the engine's recovery");
362
362
  });
363
+
364
+ // {§provider-usage-refusal} (#580) — the provider's counters disagree with themselves; the
365
+ // exchange is not the casualty.
366
+ test("inconsistent usage counters refuse normalization without failing the response, in both shapes", async (t) => {
367
+ const usage = { prompt_tokens: 1, completion_tokens: 5, total_tokens: 6, completion_tokens_details: { reasoning_tokens: 7 } };
368
+ await t.test("non-streamed", async () => {
369
+ const result = await executeOpenAICompatible({
370
+ ...request,
371
+ fetch: async () => new Response(JSON.stringify({
372
+ id: "response-1", object: "chat.completion", created: 1, model: "served-model",
373
+ choices: [{ index: 0, message: { role: "assistant", content: "answer" }, finish_reason: "stop" }],
374
+ usage,
375
+ }), { headers: { "content-type": "application/json" } }),
376
+ });
377
+ assert.equal(result.content, "answer", "the model's answer survives the provider's bookkeeping");
378
+ assert.equal(result.usage, undefined, "no counter is invented, clamped, or zeroed");
379
+ assert.deepEqual(result.usageRefusal, {
380
+ reason: "provider usage.outputTokenDetails.textTokens must be a non-negative safe integer",
381
+ usage,
382
+ }, "the counters as reported ride beside the refusal");
383
+ assert.deepEqual(result.chargeEvidence.usage, usage, "charge evidence still carries the wire usage");
384
+ });
385
+ await t.test("streamed", async () => {
386
+ const chunks = [
387
+ { id: "r", object: "chat.completion.chunk", created: 1, model: "served-model", choices: [{ index: 0, delta: { content: "answer" }, finish_reason: null }] },
388
+ { id: "r", object: "chat.completion.chunk", created: 1, model: "served-model", choices: [{ index: 0, delta: {}, finish_reason: "stop" }], usage },
389
+ ];
390
+ const result = await executeOpenAICompatible({
391
+ ...request,
392
+ streaming: true,
393
+ fetch: async () => new Response(new ReadableStream({
394
+ start(controller) {
395
+ for (const chunk of chunks) controller.enqueue(new TextEncoder().encode(`data: ${JSON.stringify(chunk)}\n\n`));
396
+ controller.enqueue(new TextEncoder().encode("data: [DONE]\n\n"));
397
+ controller.close();
398
+ },
399
+ }), { headers: { "content-type": "text/event-stream" } }),
400
+ });
401
+ assert.equal(result.content, "answer");
402
+ assert.equal(result.usage, undefined);
403
+ assert.equal(result.usageRefusal?.reason, "provider usage.outputTokenDetails.textTokens must be a non-negative safe integer");
404
+ assert.deepEqual(result.usageRefusal?.usage, usage);
405
+ });
406
+ await t.test("consistent counters still normalize", async () => {
407
+ const result = await executeOpenAICompatible({
408
+ ...request,
409
+ fetch: async () => new Response(JSON.stringify({
410
+ id: "response-1", object: "chat.completion", created: 1, model: "served-model",
411
+ choices: [{ index: 0, message: { role: "assistant", content: "answer" }, finish_reason: "stop" }],
412
+ usage: { prompt_tokens: 1, completion_tokens: 5, total_tokens: 6, completion_tokens_details: { reasoning_tokens: 2 } },
413
+ }), { headers: { "content-type": "application/json" } }),
414
+ });
415
+ assert.deepEqual(result.usage, { inputTokens: 1, outputTokens: 5, totalTokens: 6, outputTokenDetails: { textTokens: 3, reasoningTokens: 2 } });
416
+ assert.equal(result.usageRefusal, undefined);
417
+ });
418
+ });
@@ -89,6 +89,25 @@ const wireUsageOf = (
89
89
  return undefined;
90
90
  };
91
91
 
92
+ // {§provider-usage-refusal} — the provider's bookkeeping is not the exchange. When the reported
93
+ // counters cannot be normalized (a reasoning detail exceeding its output aggregate, a total that
94
+ // contradicts its parts), the response stands, usage is unknown (never invented, clamped, or zero),
95
+ // and the counters as reported ride beside the refusal so forensics can see what the wire said.
96
+ export type UsageRefusal = { readonly reason: string; readonly usage: unknown };
97
+
98
+ const settledUsage = (
99
+ values: readonly unknown[],
100
+ sdkUsage: LanguageModelUsage | undefined,
101
+ ): { usage?: ProviderUsage; usageRefusal?: UsageRefusal } => {
102
+ try {
103
+ const usage = wireUsageOf(values) ?? (sdkUsage === undefined ? undefined : usageOf(sdkUsage));
104
+ return usage === undefined ? {} : { usage };
105
+ } catch (cause) {
106
+ if (!(cause instanceof TypeError)) throw cause;
107
+ return { usageRefusal: { reason: cause.message, usage: wireUsageEvidenceOf(values) ?? sdkUsage } };
108
+ }
109
+ };
110
+
92
111
  const wireUsageEvidenceOf = (values: readonly unknown[]): unknown => {
93
112
  for (let index = values.length - 1; index >= 0; index -= 1) {
94
113
  const record = recordOf(values[index]);
@@ -169,6 +188,7 @@ export type AiSdkTransportRequest = {
169
188
  streaming: boolean;
170
189
  captureRawBody: boolean;
171
190
  observeReasoning?: ProviderReasoningObserver;
191
+ observeText?: (delta: string) => void;
172
192
  };
173
193
 
174
194
  export type AiSdkTransportResponse = {
@@ -179,6 +199,7 @@ export type AiSdkTransportResponse = {
179
199
  finishReason: ProviderAttemptFinishReason;
180
200
  rawFinishReason?: string;
181
201
  usage?: ProviderUsage;
202
+ usageRefusal?: UsageRefusal;
182
203
  metadata: Record<string, unknown>;
183
204
  reasoningEncrypted: Array<{
184
205
  id: string | null;
@@ -437,7 +458,7 @@ const executeModelOnce = async (
437
458
  reasoningProjected: evidence.reasoningProjected,
438
459
  finishReason: finishReasonOf(rawFinishReason),
439
460
  ...(rawFinishReason === undefined ? {} : { rawFinishReason }),
440
- usage: wireUsageOf(values) ?? usageOf(result.usage),
461
+ ...settledUsage(values, result.usage),
441
462
  metadata: metadataOf(values),
442
463
  reasoningEncrypted: evidence.reasoningEncrypted,
443
464
  logprobs: evidence.logprobs,
@@ -472,7 +493,10 @@ const executeModelOnce = async (
472
493
  try {
473
494
  for await (const part of result.fullStream) {
474
495
  if (part.type === "raw") rawChunks.push(part.rawValue);
475
- if (part.type === "text-delta" && part.text.length > 0) outputObserved = true;
496
+ if (part.type === "text-delta" && part.text.length > 0) {
497
+ outputObserved = true;
498
+ request.observeText?.(part.text);
499
+ }
476
500
  if (part.type === "reasoning-delta" && part.text.length > 0) {
477
501
  outputObserved = true;
478
502
  request.observeReasoning?.(part.text);
@@ -504,7 +528,7 @@ const executeModelOnce = async (
504
528
  reasoningProjected: evidence.reasoningProjected,
505
529
  finishReason: finishReasonOf(rawFinishReason),
506
530
  ...(rawFinishReason === undefined ? {} : { rawFinishReason }),
507
- usage: wireUsageOf(rawChunks) ?? usageOf(await result.usage),
531
+ ...settledUsage(rawChunks, await result.usage),
508
532
  metadata: metadataOf(rawChunks),
509
533
  reasoningEncrypted: evidence.reasoningEncrypted,
510
534
  logprobs: evidence.logprobs,
@@ -563,6 +587,7 @@ export const executeOpenAICompatible = async (
563
587
  streaming: request.streaming,
564
588
  captureRawBody: request.captureRawBody,
565
589
  ...(request.observeReasoning === undefined ? {} : { observeReasoning: request.observeReasoning }),
590
+ ...(request.observeText === undefined ? {} : { observeText: request.observeText }),
566
591
  });
567
592
  };
568
593
 
@@ -577,6 +602,7 @@ const responseBodyValues = (error: APICallError): readonly unknown[] => {
577
602
 
578
603
  export type AiSdkTransportFailureEvidence = {
579
604
  readonly usage?: ProviderUsage;
605
+ readonly usageRefusal?: UsageRefusal;
580
606
  readonly chargeEvidence: ProviderChargeEvidence;
581
607
  readonly status?: number;
582
608
  };
@@ -587,7 +613,7 @@ export const transportFailureEvidence = (
587
613
  const values = typeof error === "object" && error !== null
588
614
  ? streamFailureValues.get(error) ?? (APICallError.isInstance(error) ? responseBodyValues(error) : [])
589
615
  : [];
590
- const usage = wireUsageOf(values);
616
+ const settled = settledUsage(values, undefined);
591
617
  const usageEvidence = wireUsageEvidenceOf(values);
592
618
  const charge = wireChargeEvidenceOf(values);
593
619
  const wireStatus = values
@@ -600,7 +626,7 @@ export const transportFailureEvidence = (
600
626
  ? wireStatus as number
601
627
  : undefined;
602
628
  return {
603
- ...(usage === undefined ? {} : { usage }),
629
+ ...settled,
604
630
  chargeEvidence: {
605
631
  ...(charge === undefined ? {} : { charge }),
606
632
  ...(usageEvidence === undefined ? {} : { usage: usageEvidence }),
@@ -52,6 +52,7 @@ test("the OpenAI-compatible entrypoint excludes Node-owned provider machinery",
52
52
  "cost.ts",
53
53
  "env.ts",
54
54
  "errors.ts",
55
+ "LeadingReasoning.ts",
55
56
  "notices.ts",
56
57
  "openai.ts",
57
58
  "promptTokens.ts",
@@ -2,10 +2,27 @@ import test, { mock } from "node:test";
2
2
  import { strict as assert } from "node:assert";
3
3
  import { once } from "node:events";
4
4
  import { createServer } from "node:http";
5
- import { catalogProviderFromEnv, providerFromSdkModel } from "./catalogProvider.ts";
5
+ import { catalogProviderFromEnv, catalogReasoningPolicies, providerFromSdkModel } from "./catalogProvider.ts";
6
+ import { lookupProvider, resolveModel } from "@plurnk/plurnk-models";
7
+ import { calculateCostUsdDecimal } from "./usage.ts";
8
+
9
+ // A rate literal breaks at every catalog refresh (the 1.17.0 stamp moved DeepSeek's rates); the
10
+ // claim under test is that the estimate is the catalog's rates applied to the reported usage.
11
+ const catalogRatesOf = (provider: string, model: string) => {
12
+ const cost = resolveModel(provider, model)?.info.cost;
13
+ if (cost === undefined) throw new Error(`${provider}/${model} has no catalog rates`);
14
+ return {
15
+ input: cost.inputPer1M,
16
+ output: cost.outputPer1M,
17
+ ...(cost.reasoningPer1M === undefined ? {} : { reasoning: cost.reasoningPer1M }),
18
+ ...(cost.cacheReadPer1M === undefined ? {} : { cacheRead: cost.cacheReadPer1M }),
19
+ ...(cost.cacheWritePer1M === undefined ? {} : { cacheWrite: cost.cacheWritePer1M }),
20
+ };
21
+ };
6
22
  import { withProviderDefaults } from "./defaults.ts";
7
23
  import type { LanguageModel } from "ai";
8
24
  import { resetEmittedWarnings } from "./warnings.ts";
25
+ import { UnsupportedReasoningPolicyError } from "./types.ts";
9
26
 
10
27
  const env = {
11
28
  OPENAI_API_KEY: "test-key",
@@ -104,6 +121,73 @@ test("provider adapters advertise only reasoning policies they can preserve", ()
104
121
  assert.deepEqual(gemini?.supportedReasoningPolicies, ["adaptive", "low", "medium", "high"], "Gemini 3's mandatory minimum is not advertised as off");
105
122
  });
106
123
 
124
+ test("{§provider-reasoning-policy}: catalog discovery and construction agree on native and compatible routes", () => {
125
+ const fetch = mock.method(globalThis, "fetch", () => {
126
+ throw new Error("capability discovery and construction require no provider request");
127
+ });
128
+ const configured = withProviderDefaults({
129
+ ...env,
130
+ DEEPSEEK_API_KEY: "test-key",
131
+ GEMINI_API_KEY: "test-key",
132
+ MISTRAL_API_KEY: "test-key",
133
+ CLOUDFLARE_ACCOUNT_ID: "test-account",
134
+ CLOUDFLARE_API_KEY: "test-key",
135
+ PLURNK_PROVIDERS_REASONING: "adaptive",
136
+ PLURNK_PROVIDERS_PROVIDER_CLOUDFLARE_WORKERS_AI_REASONING_STYLE: "effort_required",
137
+ });
138
+ for (const [name, model] of [
139
+ ["openai", "gpt-4.1-mini"],
140
+ ["google", "gemini-3.7-flash"],
141
+ ["mistral", "mistral-small-latest"],
142
+ ["deepseek", "deepseek-v4-flash"],
143
+ ["cloudflare-workers-ai", "@cf/qwen/qwen3.8-27b"],
144
+ ["cloudflare-workers-ai", "@cf/ibm-granite/granite-4.0-h-micro"],
145
+ ["cloudflare-workers-ai", "@cf/zai-org/glm-5.3-flash"],
146
+ ]) {
147
+ const providerInfo = lookupProvider(name);
148
+ const info = resolveModel(name, model)?.info;
149
+ assert.ok(providerInfo);
150
+ assert.ok(info);
151
+ for (const declaration of ["", "medium,high,max"]) {
152
+ const key = `PLURNK_PROVIDERS_PROVIDER_${name.replaceAll(/[^a-zA-Z0-9]/g, "_").toUpperCase()}_REASONING_EFFORTS`;
153
+ const localEnv = { ...configured, [key]: declaration };
154
+ const provider = catalogProviderFromEnv(name, localEnv, model);
155
+ assert.ok(provider);
156
+ assert.deepEqual(
157
+ catalogReasoningPolicies(providerInfo, info, localEnv),
158
+ provider.supportedReasoningPolicies,
159
+ `${name}/${model} with declared efforts ${JSON.stringify(declaration)}`,
160
+ );
161
+ }
162
+ }
163
+ assert.equal(fetch.mock.callCount(), 0);
164
+ });
165
+
166
+ test("{§provider-reasoning-policy}: discovery never offers a fixed effort the installed transport rejects", () => {
167
+ const info = resolveModel("deepseek", "deepseek-v4-flash")?.info;
168
+ assert.ok(info);
169
+ const declared = withProviderDefaults({
170
+ ...env,
171
+ PLURNK_PROVIDERS_REASONING: "adaptive",
172
+ PLURNK_PROVIDERS_PROVIDER_OPENAI_REASONING_EFFORTS: "medium,xhigh",
173
+ PLURNK_PROVIDERS_PROVIDER_OPENROUTER_REASONING_EFFORTS: "medium,xhigh",
174
+ });
175
+ for (const [name, expected] of [
176
+ ["openai", ["off", "adaptive", "low", "medium", "high", "xhigh"]],
177
+ ["openrouter", ["off", "adaptive", "low", "medium", "high"]],
178
+ ] as const) {
179
+ const catalog = lookupProvider(name);
180
+ assert.ok(catalog);
181
+ assert.deepEqual(catalogReasoningPolicies(catalog, info, declared), expected);
182
+ assert.throws(() => providerFromSdkModel({
183
+ name, model: "fixture", contextWindow: 32_768, info,
184
+ env: { ...declared, PLURNK_PROVIDERS_REASONING: "max" },
185
+ languageModel: { specificationVersion: "v4" } as LanguageModel,
186
+ sdkPackage: catalog.npm,
187
+ }), UnsupportedReasoningPolicyError);
188
+ }
189
+ });
190
+
107
191
  test("Models.dev controls Cloudflare's exact effort vocabulary", async () => {
108
192
  const bodies: Record<string, unknown>[] = [];
109
193
  mock.method(globalThis, "fetch", async (_input: string | URL | Request, init?: RequestInit) => {
@@ -357,7 +441,7 @@ test("xAI's native chat contract requests its strongest cataloged effort and cap
357
441
  });
358
442
  });
359
443
 
360
- test("Cerebras explicit reasoning activation needs no operator effort or token budget", async () => {
444
+ test("Cerebras explicit and adaptive reasoning do not invent a token budget", async () => {
361
445
  let body: Record<string, unknown> | undefined;
362
446
  mock.method(globalThis, "fetch", async (_input: string | URL | Request, init?: RequestInit) => {
363
447
  body = JSON.parse(String(init?.body)) as Record<string, unknown>;
@@ -366,21 +450,21 @@ test("Cerebras explicit reasoning activation needs no operator effort or token b
366
450
  id: "chatcmpl-cerebras",
367
451
  object: "chat.completion.chunk",
368
452
  created: 1,
369
- model: "gemma-4-31b",
453
+ model: "qwen-3.8-27b",
370
454
  choices: [{ index: 0, delta: { reasoning: "consider" }, finish_reason: null }],
371
455
  })}`,
372
456
  `data: ${JSON.stringify({
373
457
  id: "chatcmpl-cerebras",
374
458
  object: "chat.completion.chunk",
375
459
  created: 2,
376
- model: "gemma-4-31b",
460
+ model: "qwen-3.8-27b",
377
461
  choices: [{ index: 0, delta: { content: "done" }, finish_reason: "stop" }],
378
462
  })}`,
379
463
  `data: ${JSON.stringify({
380
464
  id: "chatcmpl-cerebras",
381
465
  object: "chat.completion.chunk",
382
466
  created: 3,
383
- model: "gemma-4-31b",
467
+ model: "qwen-3.8-27b",
384
468
  choices: [],
385
469
  usage: {
386
470
  prompt_tokens: 2,
@@ -393,20 +477,22 @@ test("Cerebras explicit reasoning activation needs no operator effort or token b
393
477
  ].join("\n\n"), { headers: { "content-type": "text/event-stream" } });
394
478
  });
395
479
 
396
- const provider = catalogProviderFromEnv("cerebras", {
397
- ...env,
398
- CEREBRAS_API_KEY: "test-key",
399
- PLURNK_PROVIDERS_REASONING: "high",
400
- }, "gemma-4-31b");
401
- const result = await provider?.generate({
402
- workerId: "worker",
403
- messages: [{ role: "user", content: "hello" }],
404
- });
480
+ for (const policy of ["high", "adaptive"] as const) {
481
+ const provider = catalogProviderFromEnv("cerebras", {
482
+ ...env,
483
+ CEREBRAS_API_KEY: "test-key",
484
+ PLURNK_PROVIDERS_REASONING: policy,
485
+ }, "qwen-3.8-27b");
486
+ const result = await provider?.generate({
487
+ workerId: "worker",
488
+ messages: [{ role: "user", content: "hello" }],
489
+ });
405
490
 
406
- assert.equal(body?.reasoning_effort, "high", "the native SDK preserves the explicit durable effort");
407
- assert.equal("thinking_budget_tokens" in (body ?? {}), false, "activation does not invent a token budget");
408
- assert.equal(result?.assistant.reasoning, "consider");
409
- assert.equal(result?.accounting[0]?.usage?.outputTokenDetails?.reasoningTokens, 1);
491
+ assert.equal(body?.reasoning_effort, "high", `${policy} reaches the native SDK as high`);
492
+ assert.equal("thinking_budget_tokens" in (body ?? {}), false, "activation does not invent a token budget");
493
+ assert.equal(result?.assistant.reasoning, "consider");
494
+ assert.equal(result?.accounting[0]?.usage?.outputTokenDetails?.reasoningTokens, 1);
495
+ }
410
496
  });
411
497
 
412
498
  test("Meta Muse adaptive reasoning is requested even when the endpoint returns no readable trace", async () => {
@@ -807,9 +893,11 @@ test("Models.dev is the only fallback rate table", async () => {
807
893
  }, "deepseek-v4-flash");
808
894
  assert.notEqual(cataloged, null);
809
895
  const catalogedResponse = await cataloged!.generate({ workerId: "cataloged", messages: [] });
896
+ const catalogAmount = calculateCostUsdDecimal(catalogedResponse.accounting[0]!.usage!, catalogRatesOf("deepseek", "deepseek-v4-flash"));
897
+ assert.match(String(catalogAmount), /^0\.0*[1-9]/, "the catalog prices this usage");
810
898
  assert.deepEqual(catalogedResponse.accounting[0]?.cost, {
811
899
  kind: "estimated",
812
- amount: { amount: "0.00012712", currency: "USD" },
900
+ amount: { amount: catalogAmount, currency: "USD" },
813
901
  source: "Models.dev catalog rates",
814
902
  });
815
903
 
@@ -854,9 +942,11 @@ test("{§operator-cost-override} declared rates overlay the catalog and the sour
854
942
  PLURNK_PROVIDERS_COST: "input=0.22,output=0.66,cacheRead=0.007",
855
943
  }, "deepseek-v4-flash");
856
944
  const response = await overridden!.generate({ workerId: "overridden", messages: [] });
945
+ const overriddenAmount = calculateCostUsdDecimal(response.accounting[0]!.usage!, { ...catalogRatesOf("deepseek", "deepseek-v4-flash"), input: 0.22, output: 0.66, cacheRead: 0.007 });
946
+ assert.notEqual(overriddenAmount, calculateCostUsdDecimal(response.accounting[0]!.usage!, catalogRatesOf("deepseek", "deepseek-v4-flash")), "the override changes the price");
857
947
  assert.deepEqual(response.accounting[0]?.cost, {
858
948
  kind: "estimated",
859
- amount: { amount: "0.0002148", currency: "USD" },
949
+ amount: { amount: overriddenAmount, currency: "USD" },
860
950
  source: "operator PLURNK_PROVIDERS_COST override over Models.dev catalog rates",
861
951
  });
862
952
  mock.restoreAll();
@@ -3,6 +3,7 @@ import {
3
3
  resolveModel,
4
4
  type ModelInfo,
5
5
  type ModelReasoningEffort,
6
+ type ProviderInfo,
6
7
  } from "@plurnk/plurnk-models";
7
8
  import {
8
9
  costOverrideFromEnv,
@@ -176,6 +177,41 @@ const supportedReasoningPolicies = ({
176
177
  return activationPolicies;
177
178
  };
178
179
 
180
+ const reasoningCapabilities = (
181
+ name: string,
182
+ env: NodeJS.ProcessEnv,
183
+ info: ModelInfo | undefined,
184
+ native: boolean,
185
+ sdkPackage?: string,
186
+ ) => {
187
+ const declaredEfforts = declaredEffortsFromEnv(env, name);
188
+ const declaredStyle = info?.reasoning === false ? "none" : reasoningStyleFromEnv(env, name) ?? "none";
189
+ const style = info?.reasoningOptions !== undefined
190
+ && (declaredStyle === "effort" || declaredStyle === "effort_required")
191
+ && catalogEfforts(info, declaredEfforts).length === 0
192
+ ? "none"
193
+ : declaredStyle;
194
+ const [first, ...rest] = supportedReasoningPolicies({ info, native, style, declared: declaredEfforts })
195
+ .filter((policy) => (!native || policy !== "max")
196
+ && (sdkPackage !== "@openrouter/ai-sdk-provider" || policy !== "xhigh"));
197
+ if (first === undefined) throw new TypeError(`${name} provider: no portable reasoning policy is representable`);
198
+ return {
199
+ declaredEfforts,
200
+ style,
201
+ policies: [first, ...rest] satisfies [ReasoningPolicy, ...ReasoningPolicy[]],
202
+ };
203
+ };
204
+
205
+ // {§provider-reasoning-policy} Read-only discovery and construction share admission;
206
+ // the compatible SDK package is the only catalog transport without a native model.
207
+ export const catalogReasoningPolicies = (
208
+ provider: ProviderInfo,
209
+ info: ModelInfo,
210
+ env: NodeJS.ProcessEnv,
211
+ ): readonly [ReasoningPolicy, ...ReasoningPolicy[]] => reasoningCapabilities(
212
+ provider.id, env, info, provider.npm !== "@ai-sdk/openai-compatible", provider.npm,
213
+ ).policies;
214
+
179
215
  const adaptiveReasoningProjection = ({
180
216
  sdkPackage,
181
217
  model,
@@ -276,15 +312,9 @@ export const providerFromSdkModel = ({
276
312
  );
277
313
  const reasoning = reasoningFromEnv(env, name, envelope.reasoningBudget);
278
314
  const reasoningCapable = info?.reasoning === true;
279
- const declaredEfforts = declaredEffortsFromEnv(env, name);
280
- const declaredReasoningStyle = info?.reasoning === false
281
- ? "none"
282
- : reasoningStyleFromEnv(env, name) ?? "none";
283
- const reasoningStyle = info?.reasoningOptions !== undefined
284
- && (declaredReasoningStyle === "effort" || declaredReasoningStyle === "effort_required")
285
- && catalogEfforts(info, declaredEfforts).length === 0
286
- ? "none"
287
- : declaredReasoningStyle;
315
+ const { declaredEfforts, style: reasoningStyle, policies } = reasoningCapabilities(
316
+ name, env, info, languageModel !== undefined, sdkPackage,
317
+ );
288
318
  const adaptiveReasoning = adaptiveReasoningProjection({
289
319
  sdkPackage,
290
320
  model,
@@ -351,12 +381,7 @@ export const providerFromSdkModel = ({
351
381
  reasoningBudget: reasoning.budget,
352
382
  // #457 — a catalog-declared toggle control makes `adaptive` an explicit enable.
353
383
  reasoningToggle: info?.reasoningOptions?.some((option) => option.type === "toggle") === true,
354
- supportedReasoningPolicies: supportedReasoningPolicies({
355
- info,
356
- native: languageModel !== undefined,
357
- style: reasoningStyle,
358
- declared: declaredEfforts,
359
- }),
384
+ supportedReasoningPolicies: policies,
360
385
  ...adaptiveReasoning,
361
386
  ...(compatibleAdaptiveReasoning === undefined ? {} : { compatibleAdaptiveReasoning }),
362
387
  ...(compatibleOffReasoning === undefined ? {} : { compatibleOffReasoning }),
@@ -386,9 +411,6 @@ export const providerFromSdkModel = ({
386
411
  estimateCost,
387
412
  source: providerSource(name),
388
413
  ...(grammarStyle === undefined ? {} : { grammarStyle }),
389
- gbnfDebug: env.PLURNK_PROVIDERS_GBNF_DEBUG !== undefined
390
- && env.PLURNK_PROVIDERS_GBNF_DEBUG !== ""
391
- && env.PLURNK_PROVIDERS_GBNF_DEBUG !== "0",
392
414
  ...dataCaptureFromEnv(env, name),
393
415
  });
394
416
  };
@@ -215,9 +215,6 @@ export const compatibleProviderFromEnv = async (
215
215
  errorDetailLimit: parseRequiredInt(env.PLURNK_PROVIDERS_ERROR_DETAIL_LIMIT, "PLURNK_PROVIDERS_ERROR_DETAIL_LIMIT", provider),
216
216
  source: providerSource(provider),
217
217
  grammarStyle,
218
- gbnfDebug: env.PLURNK_PROVIDERS_GBNF_DEBUG !== undefined
219
- && env.PLURNK_PROVIDERS_GBNF_DEBUG !== ""
220
- && env.PLURNK_PROVIDERS_GBNF_DEBUG !== "0",
221
218
  ...dataCaptureFromEnv(env, provider),
222
219
  firstPartyMetadata: provider === "plurnk",
223
220
  normalizeCost: provider === "plurnk" ? plurnkCostNormalizer : undefined,
package/src/env.test.ts CHANGED
@@ -211,6 +211,7 @@ test("every PROVIDERS_KNOBS entry appears in the shipped .env.defaults", async (
211
211
  const missing = PROVIDERS_KNOBS.filter((k) => !defaults.includes(k));
212
212
  assert.deepEqual([...missing], [], "knobs read by code but undeclared in .env.defaults");
213
213
  assert.ok(defaults.includes("PLURNK_PROVIDERS_GBNF="), "GBNF (service-read, providers-namespace) must be declared with its default");
214
+ assert.equal(defaults.includes("PLURNK_PROVIDERS_GBNF_DEBUG"), false, "the debug knob is gone with the generator (#588)");
214
215
  });
215
216
 
216
217
  // The family word is REASONING (industry standard). Old names fail hard
package/src/env.ts CHANGED
@@ -243,7 +243,7 @@ export const resolveGenerationEnvelopeFromEnv = (
243
243
  // PLURNK_PROVIDERS_REASONING_BUDGET optional reasoning subset of the total
244
244
  // output budget, used for tier/budget mapping where the backend supports it.
245
245
  // The provider maps intent to the backend's mechanism; the consumer states
246
- // intent, never mechanism. PLAN is a separate public complete-Plan record.
246
+ // intent, never mechanism. TASK inventory is separate from provider reasoning.
247
247
  export type Reasoning = { mode: ReasoningPolicy; budget: number | null };
248
248
 
249
249
  export const parseReasoningPolicy = (value: unknown, label: string): ReasoningPolicy => {
@@ -316,7 +316,6 @@ export const PROVIDERS_KNOBS = Object.freeze([
316
316
  "PLURNK_PROVIDERS_DRY_ALLOWED_LENGTH",
317
317
  "PLURNK_PROVIDERS_PROBE_ATTEMPTS",
318
318
  "PLURNK_PROVIDERS_PROBE_DELAY",
319
- "PLURNK_PROVIDERS_GBNF_DEBUG",
320
319
  "PLURNK_PROVIDERS_TOP_LOGPROBS",
321
320
  "PLURNK_PROVIDERS_RAWBODY",
322
321
  ]);