@providerkit/core 0.21.1 → 0.23.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 (137) hide show
  1. package/README.md +7 -7
  2. package/dist/attribution.d.ts +2 -2
  3. package/dist/auth/flows.d.ts.map +1 -1
  4. package/dist/auth/flows.js +5 -5
  5. package/dist/auth/flows.js.map +1 -1
  6. package/dist/auth/index.d.ts +1 -0
  7. package/dist/auth/index.d.ts.map +1 -1
  8. package/dist/auth/index.js +1 -0
  9. package/dist/auth/index.js.map +1 -1
  10. package/dist/auth/oauth.d.ts +12 -0
  11. package/dist/auth/oauth.d.ts.map +1 -1
  12. package/dist/auth/oauth.js +19 -1
  13. package/dist/auth/oauth.js.map +1 -1
  14. package/dist/auth/token-source.d.ts +9 -3
  15. package/dist/auth/token-source.d.ts.map +1 -1
  16. package/dist/auth/token-source.js +24 -9
  17. package/dist/auth/token-source.js.map +1 -1
  18. package/dist/auth/with-token.d.ts +29 -0
  19. package/dist/auth/with-token.d.ts.map +1 -0
  20. package/dist/auth/with-token.js +85 -0
  21. package/dist/auth/with-token.js.map +1 -0
  22. package/dist/capability.d.ts +9 -9
  23. package/dist/capability.js +9 -9
  24. package/dist/capability.js.map +1 -1
  25. package/dist/context.d.ts +43 -5
  26. package/dist/context.d.ts.map +1 -1
  27. package/dist/context.js +123 -4
  28. package/dist/context.js.map +1 -1
  29. package/dist/errors.d.ts +26 -17
  30. package/dist/errors.d.ts.map +1 -1
  31. package/dist/errors.js +57 -18
  32. package/dist/errors.js.map +1 -1
  33. package/dist/fallback.d.ts +45 -1
  34. package/dist/fallback.d.ts.map +1 -1
  35. package/dist/fallback.js +138 -75
  36. package/dist/fallback.js.map +1 -1
  37. package/dist/image.d.ts +1 -1
  38. package/dist/image.d.ts.map +1 -1
  39. package/dist/jev.d.ts +9 -9
  40. package/dist/jev.d.ts.map +1 -1
  41. package/dist/jev.js +1 -1
  42. package/dist/key-pool.d.ts +14 -8
  43. package/dist/key-pool.d.ts.map +1 -1
  44. package/dist/key-pool.js +35 -18
  45. package/dist/key-pool.js.map +1 -1
  46. package/dist/presets.d.ts +33 -24
  47. package/dist/presets.d.ts.map +1 -1
  48. package/dist/presets.js +25 -14
  49. package/dist/presets.js.map +1 -1
  50. package/dist/providers/anthropic.d.ts +5 -5
  51. package/dist/providers/anthropic.d.ts.map +1 -1
  52. package/dist/providers/anthropic.js +89 -34
  53. package/dist/providers/anthropic.js.map +1 -1
  54. package/dist/providers/factory.d.ts +2 -2
  55. package/dist/providers/factory.d.ts.map +1 -1
  56. package/dist/providers/factory.js +2 -1
  57. package/dist/providers/factory.js.map +1 -1
  58. package/dist/providers/gemini.d.ts +8 -2
  59. package/dist/providers/gemini.d.ts.map +1 -1
  60. package/dist/providers/gemini.js +117 -41
  61. package/dist/providers/gemini.js.map +1 -1
  62. package/dist/providers/openai.d.ts +12 -9
  63. package/dist/providers/openai.d.ts.map +1 -1
  64. package/dist/providers/openai.js +72 -21
  65. package/dist/providers/openai.js.map +1 -1
  66. package/dist/providers/responses.d.ts +3 -3
  67. package/dist/providers/responses.d.ts.map +1 -1
  68. package/dist/providers/responses.js +103 -57
  69. package/dist/providers/responses.js.map +1 -1
  70. package/dist/providers/zai.d.ts +2 -2
  71. package/dist/providers/zai.js +2 -2
  72. package/dist/rate-limit.d.ts +13 -3
  73. package/dist/rate-limit.d.ts.map +1 -1
  74. package/dist/rate-limit.js +10 -4
  75. package/dist/rate-limit.js.map +1 -1
  76. package/dist/retry.d.ts +16 -5
  77. package/dist/retry.d.ts.map +1 -1
  78. package/dist/retry.js +29 -16
  79. package/dist/retry.js.map +1 -1
  80. package/dist/schema.d.ts +6 -6
  81. package/dist/schema.js +8 -8
  82. package/dist/tool-args.d.ts +23 -1
  83. package/dist/tool-args.d.ts.map +1 -1
  84. package/dist/tool-args.js +30 -2
  85. package/dist/tool-args.js.map +1 -1
  86. package/dist/tools.d.ts +3 -3
  87. package/dist/tools.js +2 -2
  88. package/dist/tools.js.map +1 -1
  89. package/dist/transport.d.ts +40 -10
  90. package/dist/transport.d.ts.map +1 -1
  91. package/dist/transport.js +82 -21
  92. package/dist/transport.js.map +1 -1
  93. package/dist/types.d.ts +37 -23
  94. package/dist/types.d.ts.map +1 -1
  95. package/dist/types.js +3 -11
  96. package/dist/types.js.map +1 -1
  97. package/dist/usage.d.ts +4 -4
  98. package/dist/usage.d.ts.map +1 -1
  99. package/dist/usage.js +3 -3
  100. package/dist/usage.js.map +1 -1
  101. package/dist/watchdog.d.ts +14 -12
  102. package/dist/watchdog.d.ts.map +1 -1
  103. package/dist/watchdog.js +35 -16
  104. package/dist/watchdog.js.map +1 -1
  105. package/dist/zod.d.ts +4 -4
  106. package/dist/zod.js +3 -3
  107. package/package.json +1 -1
  108. package/src/attribution.ts +2 -2
  109. package/src/auth/flows.ts +5 -4
  110. package/src/auth/index.ts +1 -0
  111. package/src/auth/oauth.ts +23 -1
  112. package/src/auth/token-source.ts +30 -11
  113. package/src/auth/with-token.ts +97 -0
  114. package/src/capability.ts +12 -12
  115. package/src/context.ts +141 -6
  116. package/src/errors.ts +63 -23
  117. package/src/fallback.ts +173 -75
  118. package/src/image.ts +1 -1
  119. package/src/jev.ts +11 -11
  120. package/src/key-pool.ts +37 -20
  121. package/src/presets.ts +38 -24
  122. package/src/providers/anthropic.ts +97 -38
  123. package/src/providers/factory.ts +4 -3
  124. package/src/providers/gemini.ts +121 -44
  125. package/src/providers/openai.ts +78 -25
  126. package/src/providers/responses.ts +113 -61
  127. package/src/providers/zai.ts +2 -2
  128. package/src/rate-limit.ts +35 -7
  129. package/src/retry.ts +46 -15
  130. package/src/schema.ts +8 -8
  131. package/src/tool-args.ts +46 -2
  132. package/src/tools.ts +4 -4
  133. package/src/transport.ts +103 -23
  134. package/src/types.ts +37 -23
  135. package/src/usage.ts +5 -5
  136. package/src/watchdog.ts +38 -19
  137. package/src/zod.ts +4 -4
@@ -15,11 +15,11 @@
15
15
  // run put `qwen3.8-flash` at 10/10 with the response format and 1/6 without it,
16
16
  // so a global flip only moves the silence to different models. One call each
17
17
  // way settles it for the model actually being served.
18
- /** Nothing a model can answer from memory — so a tool call is the only correct
19
- * turn, and NOT making one is a real signal rather than a judgement call. */
18
+ /** A question the model cannot answer from memory, so a tool call is the only
19
+ * correct turn. Not making one is a real signal rather than a judgement call. */
20
20
  const PROBE_TOOL = {
21
21
  name: "get_current_time",
22
- description: "The only source of the current time. You cannot know the time without calling this — never answer a time question from memory or guess.",
22
+ description: "The only source of the current time. You cannot know the time without calling this. Never answer a time question from memory or guess.",
23
23
  inputSchema: {
24
24
  type: "object",
25
25
  properties: { timezone: { type: "string", description: "IANA timezone, e.g. Asia/Tokyo" } },
@@ -55,12 +55,12 @@ async function callsTool(provider, shape, opts) {
55
55
  }
56
56
  /**
57
57
  * Ask a model, on the wire, which {@link JsonWithTools} shape it actually
58
- * serves — then configure the provider with `probe.use`.
58
+ * serves. Then configure the provider with `probe.use`.
59
59
  *
60
60
  * Both shapes are always tried, because a result you can read beats a result
61
61
  * you have to trust: `calls` is the table to log, and "0/3 and 3/3" is what
62
62
  * makes a later regression obvious. It costs `samples × 2` short calls, made
63
- * one after another, once, at whatever moment the app decides to ask — boot is
63
+ * one after another, once, at whatever moment the app decides to ask. Boot is
64
64
  * the usual one.
65
65
  *
66
66
  * Probe each provider on its own, never a fallback chain: the shape is a fact
@@ -77,8 +77,8 @@ async function callsTool(provider, shape, opts) {
77
77
  * report "this model cannot call tools", which is a much worse thing to
78
78
  * believe than "the call failed".
79
79
  *
80
- * What it catches is the structural failure — the model that CANNOT emit the
81
- * call, every time, on the easiest question there is. A model that is merely
80
+ * It catches a structural failure: a model that cannot emit the call, every
81
+ * time, on the easiest question there is. A model that is merely
82
82
  * unreliable will pass: `gemini-3.8-flash` answers this probe 3/3 and a real
83
83
  * sales turn 3/10. That is the right line to draw. A harder probe would start
84
84
  * failing good models for being terse, and "sometimes forgets its tools" is a
@@ -126,8 +126,8 @@ function normalizeId(id) {
126
126
  * - `deepseek/deepseek-v4.1-flash` matches `deepseek-v4.1-flash`
127
127
  * - `accounts/fireworks/models/glm-5p3-flash` matches `glm-5.3-flash`
128
128
  *
129
- * Never throws — if the catalog cannot be retrieved or the model is unknown,
130
- * returns `undefined` so capability lookups never break caller execution.
129
+ * Never throws. If the catalog cannot be retrieved or the model is unknown,
130
+ * it returns `undefined` so capability lookups never break caller execution.
131
131
  */
132
132
  export async function resolveModelCapabilities(modelId, options = {}) {
133
133
  if (!modelId)
@@ -1 +1 @@
1
- {"version":3,"file":"capability.js","sourceRoot":"","sources":["../src/capability.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,EAAE;AACF,2EAA2E;AAC3E,8EAA8E;AAC9E,0EAA0E;AAC1E,6EAA6E;AAC7E,gFAAgF;AAChF,6EAA6E;AAC7E,+EAA+E;AAC/E,gFAAgF;AAChF,6EAA6E;AAC7E,iBAAiB;AACjB,EAAE;AACF,gFAAgF;AAChF,gFAAgF;AAChF,6EAA6E;AAC7E,sDAAsD;AAItD;8EAC8E;AAC9E,MAAM,UAAU,GAAG;IACjB,IAAI,EAAE,kBAAkB;IACxB,WAAW,EACT,yIAAyI;IAC3I,WAAW,EAAE;QACX,IAAI,EAAE,QAAiB;QACvB,UAAU,EAAE,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,gCAAgC,EAAE,EAAE;QAC3F,QAAQ,EAAE,EAAE;KACb;CACF,CAAC;AAEF,MAAM,UAAU,GAAG;IACjB,IAAI,EAAE,OAAO;IACb,MAAM,EAAE;QACN,IAAI,EAAE,QAAiB;QACvB,UAAU,EAAE,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,yBAAyB,EAAE,EAAE;QACnF,QAAQ,EAAE,CAAC,SAAS,CAAC;QACrB,oBAAoB,EAAE,KAAK;KAC5B;CACF,CAAC;AAEF,MAAM,MAAM,GAAoB,CAAC,iBAAiB,EAAE,QA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AAC;YACD,IAAI,SAAS;gBAAE,MAAM;QACvB,CAAC;IACH,CAAC;IAED,IAAI,CAAC,SAAS;QAAE,OAAO,SAAS,CAAC;IAEjC,MAAM,MAAM,GAAG,SAAS,CAAC,UAAU,EAAE,KAAK,IAAI,EAAE,CAAC;IACjD,OAAO;QACL,EAAE,EAAE,SAAS,CAAC,EAAE,IAAI,OAAO;QAC3B,IAAI,EAAE,SAAS,CAAC,IAAI;QACpB,aAAa,EAAE,SAAS,CAAC,KAAK,EAAE,OAAO;QACvC,SAAS,EAAE,SAAS,CAAC,KAAK,EAAE,MAAM;QAClC,aAAa,EAAE,SAAS,CAAC,SAAS,KAAK,IAAI;QAC3C,wBAAwB,EAAE,SAAS,CAAC,iBAAiB,KAAK,IAAI;QAC9D,cAAc,EAAE,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,SAAS,CAAC,UAAU,KAAK,IAAI;QACzE,iBAAiB,EAAE,SAAS,CAAC,SAAS,KAAK,IAAI;KAChD,CAAC;AACJ,CAAC"}
package/dist/context.d.ts CHANGED
@@ -1,14 +1,14 @@
1
- import type { ChatMessage } from "./types.ts";
1
+ import type { ChatMessage, ToolCall } from "./types.ts";
2
2
  /**
3
3
  * What the answer needs after the prompt: an output budget, plus the tools'
4
4
  * own schemas, plus the slack no provider documents.
5
5
  *
6
- * Absolute rather than a percentage on purpose — a 1M window does not need a
6
+ * Absolute rather than a percentage on purpose: a 1M window does not need a
7
7
  * 100k cushion, and a 128k window needs more than 12k.
8
8
  */
9
9
  export declare const CONTEXT_RESERVE_TOKENS = 32000;
10
10
  /**
11
- * Four characters per token — the rule of thumb every provider's own
11
+ * Four characters per token is the rule of thumb every provider's own
12
12
  * calculator agrees with to within a fifth, which is all the precision this
13
13
  * needs. It decides WHEN to fold: folding one turn early costs a cheap model
14
14
  * call, folding one turn late costs the whole turn.
@@ -19,7 +19,7 @@ export declare function estimateTokens(text: string): number;
19
19
  export declare function messageTokens(message: ChatMessage): number;
20
20
  export declare function conversationTokens(messages: readonly ChatMessage[]): number;
21
21
  /**
22
- * The provider's own input count says the wall is close — fold before the next
22
+ * The provider's own input count says the limit is close. Fold before the next
23
23
  * step rather than after the 400.
24
24
  *
25
25
  * Takes the REPORTED count, not an estimate, because it is the only figure
@@ -41,7 +41,7 @@ export declare function historyBudgetTokens(contextWindow: number): number;
41
41
  *
42
42
  * 1. Never split a tool call from its result. Every provider rejects a tool
43
43
  * result whose call is missing, so a cut landing between them produces a
44
- * 400 on the very next turn — the failure compaction was called to avoid.
44
+ * 400 on the very next turn, the failure compaction was called to avoid.
45
45
  * 2. Never cut into the system prompt. It is not history.
46
46
  *
47
47
  * Returns the index the kept tail starts at, or 0 when everything already fits.
@@ -55,6 +55,44 @@ export declare function pickCut(messages: readonly ChatMessage[], budget: number
55
55
  * several providers require the system block to be first and singular anyway.
56
56
  */
57
57
  export declare function applyCompaction(messages: readonly ChatMessage[], cut: number, summary: string): ChatMessage[];
58
+ /**
59
+ * Make a saved, resumed or interrupted history valid to send: every call has
60
+ * exactly one result, right after it, under an id no other call uses.
61
+ *
62
+ * A span runs from an assistant message with tool calls to the next assistant
63
+ * message. Its results move to just after the calls, in call order, and the
64
+ * rest of the span (a steer the user typed while the tools ran) keeps its
65
+ * order after them. Then:
66
+ *
67
+ * - A call with no result in its span gets one under the same id, saying it was
68
+ * interrupted. `isReadOnly` picks the wording; without it every tool may
69
+ * have partly run. These calls come back as `interrupted`, under the ids
70
+ * the request carries.
71
+ * - A result whose call is not in its span becomes a user note, images kept.
72
+ * Paired against the span only, because chat and Messages both require the
73
+ * result in the turn right after its call: a looser pairing still 400s.
74
+ * - A repeated id becomes `{id}_{k}`, the smallest free k, call and result
75
+ * together. Kimi and Gemini restart their ids every reply
76
+ * (`functions.search:0`, `call_0`), and Anthropic and Responses reject the
77
+ * second use.
78
+ * - An empty id becomes `call_{message}_{call}`, never `normalizeToolId`'s
79
+ * random fallback.
80
+ * - An assistant message with no calls and only whitespace is dropped, as if
81
+ * it were never there, so a placeholder saved before the tools ran orphans
82
+ * nothing.
83
+ *
84
+ * No side effects, and the same input always gives the same bytes: a span's
85
+ * output and every id depend only on what came before, so repairing a longer
86
+ * history keeps the shorter one's bytes and the prompt cache keeps hitting.
87
+ * Repairing a repaired history changes nothing. Every adapter here runs this
88
+ * on what it sends; call it yourself to know what `interrupted` holds.
89
+ */
90
+ export declare function repairHistory(messages: readonly ChatMessage[], opts?: {
91
+ isReadOnly?: (toolName: string) => boolean;
92
+ }): {
93
+ messages: ChatMessage[];
94
+ interrupted: ToolCall[];
95
+ };
58
96
  /**
59
97
  * The context window for a model, when nothing volunteered one.
60
98
  *
@@ -1 +1 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,QAAS,CAAC;AAE7C;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEnD;AAWD;sCACsC;AACtC,wBAAgB,aAAa,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,CAO1D;AAiBD,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,SAAS,WAAW,EAAE,GAAG,MAAM,CAE3E;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,WAAW,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,OAAO,CASnF;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM,CAEjE;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,OAAO,CAAC,QAAQ,EAAE,SAAS,WAAW,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CA0BhF;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC7B,QAAQ,EAAE,SAAS,WAAW,EAAE,EAChC,GAAG,EAAE,MAAM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AAE,WAAW,EAAE,CAAC;IAAC,WAAW,EAAE,QAAQ,EAAE,CAAA;CAAE,CAsDtD;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAMxD"}
package/dist/context.js CHANGED
@@ -2,12 +2,12 @@
2
2
  * What the answer needs after the prompt: an output budget, plus the tools'
3
3
  * own schemas, plus the slack no provider documents.
4
4
  *
5
- * Absolute rather than a percentage on purpose — a 1M window does not need a
5
+ * Absolute rather than a percentage on purpose: a 1M window does not need a
6
6
  * 100k cushion, and a 128k window needs more than 12k.
7
7
  */
8
8
  export const CONTEXT_RESERVE_TOKENS = 32_000;
9
9
  /**
10
- * Four characters per token — the rule of thumb every provider's own
10
+ * Four characters per token is the rule of thumb every provider's own
11
11
  * calculator agrees with to within a fifth, which is all the precision this
12
12
  * needs. It decides WHEN to fold: folding one turn early costs a cheap model
13
13
  * call, folding one turn late costs the whole turn.
@@ -52,7 +52,7 @@ export function conversationTokens(messages) {
52
52
  return messages.reduce((total, message) => total + messageTokens(message), 0);
53
53
  }
54
54
  /**
55
- * The provider's own input count says the wall is close — fold before the next
55
+ * The provider's own input count says the limit is close. Fold before the next
56
56
  * step rather than after the 400.
57
57
  *
58
58
  * Takes the REPORTED count, not an estimate, because it is the only figure
@@ -86,7 +86,7 @@ export function historyBudgetTokens(contextWindow) {
86
86
  *
87
87
  * 1. Never split a tool call from its result. Every provider rejects a tool
88
88
  * result whose call is missing, so a cut landing between them produces a
89
- * 400 on the very next turn — the failure compaction was called to avoid.
89
+ * 400 on the very next turn, the failure compaction was called to avoid.
90
90
  * 2. Never cut into the system prompt. It is not history.
91
91
  *
92
92
  * Returns the index the kept tail starts at, or 0 when everything already fits.
@@ -133,6 +133,125 @@ export function applyCompaction(messages, cut, summary) {
133
133
  ...tail,
134
134
  ];
135
135
  }
136
+ /** What a call with no result answers, by whether running it again is safe.
137
+ * The model reads this and decides whether to call again, so the two must
138
+ * differ on exactly that. */
139
+ const INTERRUPTED_READ_ONLY = "This call was interrupted before it returned. It changed nothing, so it is safe to call again.";
140
+ const INTERRUPTED = "This call was interrupted before it returned. It may have partly run, so check its effect before calling it again.";
141
+ /** `id`, or `id_2`, `id_3`… whichever is free first. Only ids already handed
142
+ * out are consulted, so an id never depends on what comes after it. */
143
+ function freshId(id, used) {
144
+ let fresh = id;
145
+ for (let k = 2; used.has(fresh); k++)
146
+ fresh = `${id}_${k}`;
147
+ used.add(fresh);
148
+ return fresh;
149
+ }
150
+ /** The first call in the span asked under `id` that has no result yet, or -1.
151
+ * First unanswered rather than first: two calls in one reply can share an id,
152
+ * and their results then pair in order. */
153
+ function openSlot(span, id) {
154
+ return span.vendorIds.findIndex((asked, i) => asked === id && !span.results[i]);
155
+ }
156
+ /** A result with no call to answer, kept as words the model can still read
157
+ * instead of a `tool` message every provider answers with a 400. */
158
+ function orphanNote(result) {
159
+ const text = `[Tool result for ${result.toolCallId}; the tool call is missing or already has a result]\n${result.content}`;
160
+ return {
161
+ role: "user",
162
+ content: result.images?.length ? [{ type: "text", text }, ...result.images] : text,
163
+ };
164
+ }
165
+ /**
166
+ * Make a saved, resumed or interrupted history valid to send: every call has
167
+ * exactly one result, right after it, under an id no other call uses.
168
+ *
169
+ * A span runs from an assistant message with tool calls to the next assistant
170
+ * message. Its results move to just after the calls, in call order, and the
171
+ * rest of the span (a steer the user typed while the tools ran) keeps its
172
+ * order after them. Then:
173
+ *
174
+ * - A call with no result in its span gets one under the same id, saying it was
175
+ * interrupted. `isReadOnly` picks the wording; without it every tool may
176
+ * have partly run. These calls come back as `interrupted`, under the ids
177
+ * the request carries.
178
+ * - A result whose call is not in its span becomes a user note, images kept.
179
+ * Paired against the span only, because chat and Messages both require the
180
+ * result in the turn right after its call: a looser pairing still 400s.
181
+ * - A repeated id becomes `{id}_{k}`, the smallest free k, call and result
182
+ * together. Kimi and Gemini restart their ids every reply
183
+ * (`functions.search:0`, `call_0`), and Anthropic and Responses reject the
184
+ * second use.
185
+ * - An empty id becomes `call_{message}_{call}`, never `normalizeToolId`'s
186
+ * random fallback.
187
+ * - An assistant message with no calls and only whitespace is dropped, as if
188
+ * it were never there, so a placeholder saved before the tools ran orphans
189
+ * nothing.
190
+ *
191
+ * No side effects, and the same input always gives the same bytes: a span's
192
+ * output and every id depend only on what came before, so repairing a longer
193
+ * history keeps the shorter one's bytes and the prompt cache keeps hitting.
194
+ * Repairing a repaired history changes nothing. Every adapter here runs this
195
+ * on what it sends; call it yourself to know what `interrupted` holds.
196
+ */
197
+ export function repairHistory(messages, opts = {}) {
198
+ const out = [];
199
+ const interrupted = [];
200
+ const used = new Set();
201
+ // The open span: its calls as sent, the id the vendor gave each, the result
202
+ // each has found so far, and everything else in arrival order.
203
+ let span;
204
+ const close = () => {
205
+ if (!span)
206
+ return;
207
+ for (const [i, call] of span.calls.entries()) {
208
+ const result = span.results[i];
209
+ if (result) {
210
+ out.push(result);
211
+ continue;
212
+ }
213
+ interrupted.push(call);
214
+ const content = opts.isReadOnly?.(call.name) ? INTERRUPTED_READ_ONLY : INTERRUPTED;
215
+ out.push({ role: "tool", toolCallId: call.id, name: call.name, content });
216
+ }
217
+ out.push(...span.rest);
218
+ span = undefined;
219
+ };
220
+ for (const [index, message] of messages.entries()) {
221
+ if (message.role === "assistant") {
222
+ const calls = message.toolCalls ?? [];
223
+ if (calls.length === 0 && !message.content.trim())
224
+ continue;
225
+ close();
226
+ if (calls.length === 0) {
227
+ out.push(message);
228
+ continue;
229
+ }
230
+ const sent = calls.map((call, c) => {
231
+ const id = freshId(call.id.trim() ? call.id : `call_${index}_${c}`, used);
232
+ return id === call.id ? call : { ...call, id };
233
+ });
234
+ const renamed = sent.some((call, c) => call !== calls[c]);
235
+ out.push(renamed ? { ...message, toolCalls: sent } : message);
236
+ span = { calls: sent, vendorIds: calls.map((call) => call.id), results: [], rest: [] };
237
+ }
238
+ else if (message.role === "tool") {
239
+ const slot = span ? openSlot(span, message.toolCallId) : -1;
240
+ if (span && slot !== -1) {
241
+ const id = span.calls[slot].id;
242
+ span.results[slot] = id === message.toolCallId ? message : { ...message, toolCallId: id };
243
+ }
244
+ else {
245
+ (span?.rest ?? out).push(orphanNote(message));
246
+ }
247
+ }
248
+ else {
249
+ (span?.rest ?? out).push(message);
250
+ }
251
+ }
252
+ close();
253
+ return { messages: out, interrupted };
254
+ }
136
255
  /**
137
256
  * The context window for a model, when nothing volunteered one.
138
257
  *
@@ -1 +1 @@
1
- {"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAOA;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACpC,CAAC;AAED,SAAS,MAAM,CAAC,OAAoB;IAClC,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,IAAI,OAAO,OAAO,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;QACnE,mEAAmE;QACnE,oEAAoE;QACpE,OAAO,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACzF,CAAC;IACD,OAAO,OAAO,OAAO,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;AACpE,CAAC;AAED;sCACsC;AACtC,MAAM,UAAU,aAAa,CAAC,OAAoB;IAChD,MAAM,KAAK,GACT,OAAO,CAAC,IAAI,KAAK,WAAW;QAC1B,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,IAAI,EAAE,CAAC;YAC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;gBACxB,MAAM,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAE,CAAC,EAAE,CAAC;gBAChC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC;YAC5F,CAAC;iBAAM,CAAC;gBACN,CAAC,IAAI,EAAE,IAAI,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC;YAChD,CAAC;QACH,CAAC;aAAM,CAAC;YACN,CAAC,IAAI,EAAE,IAAI,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACpC,CAAC;IACH,CAAC;IACD,KAAK,EAAE,CAAC;IACR,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,WAAW,EAAE,CAAC;AACxC,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAa;IAC9C,MAAM,EAAE,GAAG,KAAK,CAAC,WAAW,EAAE,CAAC;IAC/B,IAAI,gCAAgC,CAAC,IAAI,CAAC,EAAE,CAAC;QAAE,OAAO,SAAS,CAAC;IAChE,IAAI,2CAA2C,CAAC,IAAI,CAAC,EAAE,CAAC;QAAE,OAAO,OAAO,CAAC;IACzE,IAAI,0BAA0B,CAAC,IAAI,CAAC,EAAE,CAAC;QAAE,OAAO,OAAO,CAAC;IACxD,OAAO,OAAO,CAAC;AACjB,CAAC"}
package/dist/errors.d.ts CHANGED
@@ -2,7 +2,7 @@ import { type RateLimitWindow } from "./rate-limit.ts";
2
2
  import type { FilePart } from "./types.ts";
3
3
  /** What kind of failure this is, named by what actually fixes it. */
4
4
  export type ErrorKind =
5
- /** Our own cancel — the caller pressed Stop. Never retried. */
5
+ /** Our own cancel: the caller pressed Stop. Never retried. */
6
6
  "aborted"
7
7
  /** Our own deadline, or the provider's 408. Retry. */
8
8
  | "timeout"
@@ -11,13 +11,13 @@ export type ErrorKind =
11
11
  /** Theirs, and temporary: 5xx, Anthropic's 529, "overloaded". Retry, and
12
12
  * worth trying a different model. */
13
13
  | "overload"
14
- /** 429 per-minute throttle. Wait — or rotate to another key or model. */
14
+ /** 429 per-minute throttle. Wait, or rotate to another key or model. */
15
15
  | "rate"
16
16
  /** Balance or usage window exhausted. Waiting minutes will not fix it. */
17
17
  | "quota"
18
18
  /** The plan never included this API. Neither a new key nor a top-up fixes it. */
19
19
  | "entitlement"
20
- /** 401/403 — the key is wrong, not the request. */
20
+ /** 401/403: the key is wrong, not the request. */
21
21
  | "auth"
22
22
  /** The model id does not exist or is not served here. */
23
23
  | "model"
@@ -25,7 +25,7 @@ export type ErrorKind =
25
25
  | "context"
26
26
  /** Safety filter or refusal. */
27
27
  | "content"
28
- /** Any other 4xx — a bug in what we sent. */
28
+ /** Any other 4xx: a bug in what we sent. */
29
29
  | "invalid" | "unknown";
30
30
  export declare function isTransient(kind: ErrorKind): boolean;
31
31
  export declare function isBackupEligible(kind: ErrorKind): boolean;
@@ -37,7 +37,7 @@ export declare function isBackupEligible(kind: ErrorKind): boolean;
37
37
  * 1. Caller aborts are never retryable.
38
38
  * 2. An explicit `shouldRetry` directive on ProviderError (e.g. from server `x-should-retry`)
39
39
  * 3. Non-retryable kinds (quota, context, model, entitlement, content, invalid) return false.
40
- * 4. Excessive retry-after waits (exceeding `maxWaitMs`) return false — long waits belong to fallbacks.
40
+ * 4. Excessive retry-after waits (exceeding `maxWaitMs`) return false. Long waits belong to fallbacks.
41
41
  * 5. Transient kinds (rate, overload, network, timeout) return true.
42
42
  * 6. Transport-level network faults (fetch failed, connection lost, SSL blips) return true.
43
43
  */
@@ -55,7 +55,7 @@ export declare class ProviderError extends Error {
55
55
  readonly window?: RateLimitWindow;
56
56
  /** Explicit server directive (`x-should-retry`), overriding default classification. */
57
57
  readonly shouldRetry?: boolean;
58
- /** The provider's own response body, truncated — the actual reason, which is
58
+ /** The provider's own response body, truncated. It carries the actual reason,
59
59
  * otherwise lost behind "400 status code (no body)". */
60
60
  readonly body?: string;
61
61
  constructor(provider: string, kind: ErrorKind, message: string, opts?: {
@@ -79,7 +79,7 @@ export declare function messageOf(err: unknown): string;
79
79
  * A transport fault: the socket died before or during the response, so there
80
80
  * is NO status and no body for the patterns below to read.
81
81
  *
82
- * The chain is walked because the useful code is rarely on the thrown error —
82
+ * The chain is walked because the useful code is rarely on the thrown error:
83
83
  * it sits on `cause`, sometimes several wrappers deep. Without this walk every
84
84
  * network blip classifies as permanent, and a long run dies on its first
85
85
  * hiccup, which is the single likeliest way to lose a minutes-long job.
@@ -92,18 +92,18 @@ export declare function isTransportFailure(err: unknown): boolean;
92
92
  * The kind of failure, from whatever was thrown.
93
93
  *
94
94
  * Body patterns outrank status for the 4xx family; within them, context beats
95
- * entitlement beats quota beats auth — each earlier category's fix is useless
95
+ * entitlement beats quota beats auth. Each earlier category's fix is useless
96
96
  * for the later ones.
97
97
  */
98
98
  /**
99
- * The kind of a failure that arrived as an HTTP response — a status and a body,
99
+ * The kind of a failure that arrived as an HTTP response: a status and a body,
100
100
  * with nothing thrown.
101
101
  *
102
102
  * `classify` below is for a caught error, and it reads `status` and the body
103
103
  * text off that error when they are not passed separately. A response has no
104
104
  * error object, so callers were inventing one to fill the slot: a bare body
105
105
  * string, the same text twice, a `{ status, error }` literal. All three are
106
- * inert — nothing on them can satisfy `isAbort` or `isTransportFailure` — so
106
+ * inert (nothing on them can satisfy `isAbort` or `isTransportFailure`), so
107
107
  * they were three spellings of `undefined`.
108
108
  */
109
109
  export declare function classifyHttp(status: number | undefined, body: string): ErrorKind;
@@ -111,7 +111,7 @@ export declare function classify(err: unknown, status?: number, body?: string):
111
111
  /**
112
112
  * How long the provider asked us to wait, in ms. Two dialects: Gemini's
113
113
  * RetryInfo (`"retryDelay": "52s"`, inside the body) and the `Retry-After`
114
- * header, which SDKs keep on the error. Honouring it beats guessing — a
114
+ * header, which SDKs keep on the error. Honouring it beats guessing: a
115
115
  * backoff shorter than the window just burns an attempt.
116
116
  */
117
117
  export declare function parseRetryAfterMs(err: unknown, body?: string): number | undefined;
@@ -141,16 +141,25 @@ export declare function parseContextOverflow(err: unknown, body?: string): Conte
141
141
  * completion: retry sees nothing to retry, and a key pool never rotates off an
142
142
  * exhausted key.
143
143
  *
144
- * `error` is the vendor's own payload, whatever shape it came in — the numeric
144
+ * `error` is the vendor's own payload, whatever shape it came in: the numeric
145
145
  * HTTP `code` Gemini and OpenRouter use, the slug OpenAI and Anthropic put in
146
146
  * `code`/`type`, the canonical `status` name. The whole thing serializes into
147
147
  * the searchable body, so RetryInfo's `retryDelay` is honoured wherever it sits.
148
148
  *
149
+ * A number is a status only from 400 to 599. Vendors put their own codes in
150
+ * the same field: Z.ai's 1113 is an empty balance, and read as a status it was
151
+ * past 500, which skips the 4xx body patterns that name it quota and lands on
152
+ * overload. With no status stated, the error's type says which one it stands
153
+ * for (`STATUS_BY_TYPE`), and the body still outranks it (invariant 3).
154
+ *
149
155
  * `unknown` is floored to "overload" rather than kept: a stream that dies after
150
156
  * its headers is by construction a transient upstream fault, and "unknown" is
151
- * never retried. Everything the body DOES name keeps its own kind — which is
152
- * what tells the caller to wait, rotate a key, or compact instead of retrying
153
- * a failure that will repeat.
157
+ * never retried. Only an error with no status and no type it knows gets there.
158
+ * Before the type was read, one `invalid_request_error` in words no pattern
159
+ * knew was floored too: retried, then walked across every backup, cooling each
160
+ * for 30s, on a request that fails the same way everywhere. Everything the
161
+ * body does name keeps its own kind, which is what tells the caller to wait,
162
+ * rotate a key, or compact instead of retrying a failure that will repeat.
154
163
  */
155
164
  export declare function streamError(provider: string, error: unknown): ProviderError;
156
165
  /**
@@ -161,7 +170,7 @@ export declare function streamError(provider: string, error: unknown): ProviderE
161
170
  * read it: a caller that never checks the finish keeps half an answer as if it
162
171
  * were the whole one, the ledger records a billed turn as free, and no pool
163
172
  * learns the endpoint is dropping streams. cc-proxy shipped the same fix
164
- * (0.1.39) for the same reason — "instead of marking affected streamed
173
+ * (0.1.39) for the same reason: "instead of marking affected streamed
165
174
  * responses as successful".
166
175
  *
167
176
  * `network`, because that is what it is: the same failure as a socket that
@@ -170,7 +179,7 @@ export declare function streamError(provider: string, error: unknown): ProviderE
170
179
  * is not (invariant 1), and reaches the caller after everything it delivered.
171
180
  */
172
181
  export declare function streamCut(provider: string): ProviderError;
173
- /** The loggable surface of a failure — so a dead run never reads
182
+ /** The failure details you can log, so a dead run never reads
174
183
  * "400 status code (no body)". */
175
184
  export declare function describeProviderError(err: unknown): Record<string, unknown>;
176
185
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAYA,OAAO,EAA+C,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACpG,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C,qEAAqE;AACrE,MAAM,MAAM,SAAS;AACnB,+DAA+D;AAC7D,SAAS;AACX,sDAAsD;GACpD,SAAS;AACX,8EAA8E;GAC5E,SAAS;AACX;sCACsC;GACpC,UAAU;AACZ,yEAAyE;GACvE,MAAM;AACR,0EAA0E;GACxE,OAAO;AACT,iFAAiF;GAC/E,aAAa;AACf,mDAAmD;GACjD,MAAM;AACR,yDAAyD;GACvD,OAAO;AACT,sEAAsE;GACpE,SAAS;AACX,gCAAgC;GAC9B,SAAS;AACX,6CAA6C;GAC3C,SAAS,GACT,SAAS,CAAC;AAyBd,wBAAgB,WAAW,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAEpD;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAEzD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,OAAO,EAAE,SAAS,SAAS,GAAG,OAAO,CAerE;AAED,qBAAa,aAAc,SAAQ,KAAK;IACt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