@providerkit/core 0.29.1 → 0.31.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 (163) hide show
  1. package/README.md +58 -9
  2. package/THIRD_PARTY_NOTICES +144 -0
  3. package/dist/auth/chatgpt-models.js.map +1 -1
  4. package/dist/auth/oauth.d.ts.map +1 -1
  5. package/dist/auth/oauth.js.map +1 -1
  6. package/dist/capability.js.map +1 -1
  7. package/dist/context.d.ts +4 -3
  8. package/dist/context.d.ts.map +1 -1
  9. package/dist/context.js +19 -5
  10. package/dist/context.js.map +1 -1
  11. package/dist/decisions.js.map +1 -1
  12. package/dist/errors.d.ts.map +1 -1
  13. package/dist/errors.js +29 -2
  14. package/dist/errors.js.map +1 -1
  15. package/dist/fallback.d.ts +3 -0
  16. package/dist/fallback.d.ts.map +1 -1
  17. package/dist/fallback.js +9 -5
  18. package/dist/fallback.js.map +1 -1
  19. package/dist/gate.js.map +1 -1
  20. package/dist/hosted/anthropic.d.ts +30 -0
  21. package/dist/hosted/anthropic.d.ts.map +1 -0
  22. package/dist/hosted/anthropic.js +465 -0
  23. package/dist/hosted/anthropic.js.map +1 -0
  24. package/dist/hosted/gemini.d.ts +22 -0
  25. package/dist/hosted/gemini.d.ts.map +1 -0
  26. package/dist/hosted/gemini.js +440 -0
  27. package/dist/hosted/gemini.js.map +1 -0
  28. package/dist/hosted/openai.d.ts +33 -0
  29. package/dist/hosted/openai.d.ts.map +1 -0
  30. package/dist/hosted/openai.js +107 -0
  31. package/dist/hosted/openai.js.map +1 -0
  32. package/dist/hosted/responses.d.ts +28 -0
  33. package/dist/hosted/responses.d.ts.map +1 -0
  34. package/dist/hosted/responses.js +358 -0
  35. package/dist/hosted/responses.js.map +1 -0
  36. package/dist/hosted/shared.d.ts +107 -0
  37. package/dist/hosted/shared.d.ts.map +1 -0
  38. package/dist/hosted/shared.js +264 -0
  39. package/dist/hosted/shared.js.map +1 -0
  40. package/dist/hosted/types.d.ts +191 -0
  41. package/dist/hosted/types.d.ts.map +1 -0
  42. package/dist/hosted/types.js +10 -0
  43. package/dist/hosted/types.js.map +1 -0
  44. package/dist/hosted.d.ts +33 -0
  45. package/dist/hosted.d.ts.map +1 -0
  46. package/dist/hosted.js +61 -0
  47. package/dist/hosted.js.map +1 -0
  48. package/dist/index.d.ts +8 -7
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/index.js +8 -7
  51. package/dist/index.js.map +1 -1
  52. package/dist/key-pool.d.ts.map +1 -1
  53. package/dist/key-pool.js.map +1 -1
  54. package/dist/presets.d.ts +36 -5
  55. package/dist/presets.d.ts.map +1 -1
  56. package/dist/presets.js +32 -8
  57. package/dist/presets.js.map +1 -1
  58. package/dist/providers/anthropic.d.ts +75 -1
  59. package/dist/providers/anthropic.d.ts.map +1 -1
  60. package/dist/providers/anthropic.js +207 -135
  61. package/dist/providers/anthropic.js.map +1 -1
  62. package/dist/providers/deepgram-transcription.d.ts +24 -0
  63. package/dist/providers/deepgram-transcription.d.ts.map +1 -0
  64. package/dist/providers/deepgram-transcription.js +84 -0
  65. package/dist/providers/deepgram-transcription.js.map +1 -0
  66. package/dist/providers/factory.d.ts +10 -0
  67. package/dist/providers/factory.d.ts.map +1 -1
  68. package/dist/providers/factory.js +31 -23
  69. package/dist/providers/factory.js.map +1 -1
  70. package/dist/providers/gemini.d.ts +123 -1
  71. package/dist/providers/gemini.d.ts.map +1 -1
  72. package/dist/providers/gemini.js +114 -54
  73. package/dist/providers/gemini.js.map +1 -1
  74. package/dist/providers/hosted-codecs.d.ts +10 -0
  75. package/dist/providers/hosted-codecs.d.ts.map +1 -0
  76. package/dist/providers/hosted-codecs.js +8 -0
  77. package/dist/providers/hosted-codecs.js.map +1 -0
  78. package/dist/providers/openai-transcription.d.ts +21 -0
  79. package/dist/providers/openai-transcription.d.ts.map +1 -0
  80. package/dist/providers/openai-transcription.js +144 -0
  81. package/dist/providers/openai-transcription.js.map +1 -0
  82. package/dist/providers/openai.d.ts +69 -0
  83. package/dist/providers/openai.d.ts.map +1 -1
  84. package/dist/providers/openai.js +38 -14
  85. package/dist/providers/openai.js.map +1 -1
  86. package/dist/providers/responses.d.ts +123 -0
  87. package/dist/providers/responses.d.ts.map +1 -1
  88. package/dist/providers/responses.js +57 -22
  89. package/dist/providers/responses.js.map +1 -1
  90. package/dist/providers/speech-factory.js.map +1 -1
  91. package/dist/providers/transcription-factory.d.ts +30 -0
  92. package/dist/providers/transcription-factory.d.ts.map +1 -0
  93. package/dist/providers/transcription-factory.js +38 -0
  94. package/dist/providers/transcription-factory.js.map +1 -0
  95. package/dist/registry.d.ts +3 -0
  96. package/dist/registry.d.ts.map +1 -1
  97. package/dist/registry.js +8 -4
  98. package/dist/registry.js.map +1 -1
  99. package/dist/retry.js.map +1 -1
  100. package/dist/speech-core.d.ts +2 -17
  101. package/dist/speech-core.d.ts.map +1 -1
  102. package/dist/speech-core.js +10 -50
  103. package/dist/speech-core.js.map +1 -1
  104. package/dist/tools.d.ts.map +1 -1
  105. package/dist/tools.js.map +1 -1
  106. package/dist/transcription-core.d.ts +146 -0
  107. package/dist/transcription-core.d.ts.map +1 -0
  108. package/dist/transcription-core.js +193 -0
  109. package/dist/transcription-core.js.map +1 -0
  110. package/dist/transcription.d.ts +7 -0
  111. package/dist/transcription.d.ts.map +1 -0
  112. package/dist/transcription.js +12 -0
  113. package/dist/transcription.js.map +1 -0
  114. package/dist/transport.d.ts +26 -0
  115. package/dist/transport.d.ts.map +1 -1
  116. package/dist/transport.js +57 -2
  117. package/dist/transport.js.map +1 -1
  118. package/dist/turn.d.ts +10 -1
  119. package/dist/turn.d.ts.map +1 -1
  120. package/dist/turn.js +53 -1
  121. package/dist/turn.js.map +1 -1
  122. package/dist/types.d.ts +51 -0
  123. package/dist/types.d.ts.map +1 -1
  124. package/dist/types.js.map +1 -1
  125. package/dist/usage.d.ts +10 -1
  126. package/dist/usage.d.ts.map +1 -1
  127. package/dist/usage.js +40 -2
  128. package/dist/usage.js.map +1 -1
  129. package/dist/watchdog.d.ts +6 -0
  130. package/dist/watchdog.d.ts.map +1 -1
  131. package/dist/watchdog.js +10 -2
  132. package/dist/watchdog.js.map +1 -1
  133. package/package.json +14 -5
  134. package/src/context.ts +21 -5
  135. package/src/errors.ts +35 -2
  136. package/src/fallback.ts +21 -6
  137. package/src/hosted/anthropic.ts +512 -0
  138. package/src/hosted/gemini.ts +510 -0
  139. package/src/hosted/openai.ts +137 -0
  140. package/src/hosted/responses.ts +420 -0
  141. package/src/hosted/shared.ts +341 -0
  142. package/src/hosted/types.ts +214 -0
  143. package/src/hosted.ts +101 -0
  144. package/src/index.ts +64 -7
  145. package/src/presets.ts +54 -8
  146. package/src/providers/anthropic.ts +252 -143
  147. package/src/providers/deepgram-transcription.ts +109 -0
  148. package/src/providers/factory.ts +127 -82
  149. package/src/providers/gemini.ts +184 -60
  150. package/src/providers/hosted-codecs.ts +29 -0
  151. package/src/providers/openai-transcription.ts +177 -0
  152. package/src/providers/openai.ts +52 -15
  153. package/src/providers/responses.ts +93 -24
  154. package/src/providers/transcription-factory.ts +68 -0
  155. package/src/registry.ts +14 -4
  156. package/src/speech-core.ts +9 -47
  157. package/src/transcription-core.ts +345 -0
  158. package/src/transcription.ts +24 -0
  159. package/src/transport.ts +77 -3
  160. package/src/turn.ts +55 -1
  161. package/src/types.ts +62 -1
  162. package/src/usage.ts +32 -2
  163. package/src/watchdog.ts +12 -2
@@ -11,10 +11,11 @@
11
11
  // thinking dialects (Z.ai's explicit disabled marker, OpenRouter's floors)
12
12
  // are the adapters' knowledge, and this factory must not re-derive them.
13
13
  import { ProviderError, validateMaxAttempts } from "../errors.ts";
14
- import { createAnthropicProvider } from "./anthropic.ts";
15
- import { createGeminiProvider } from "./gemini.ts";
16
- import { createOpenAIProvider } from "./openai.ts";
17
- import { createResponsesProvider } from "./responses.ts";
14
+ import { anthropicProvider } from "./anthropic.ts";
15
+ import { geminiProvider } from "./gemini.ts";
16
+ import { openaiProvider } from "./openai.ts";
17
+ import { responsesProvider } from "./responses.ts";
18
+ import type { HostedCodecs } from "./hosted-codecs.ts";
18
19
  import {
19
20
  PRESET_IDS,
20
21
  PROVIDER_PRESETS,
@@ -32,7 +33,8 @@ import type {
32
33
  StreamOptions,
33
34
  ToolDefinition,
34
35
  } from "../types.ts";
35
- import { withConfiguredFallbacks, type ProviderFallbackConfig } from "../fallback.ts";
36
+ import { configuredFallbacks, type ProviderFallbackConfig } from "../fallback.ts";
37
+ import type { HostedToolType } from "../hosted/types.ts";
36
38
  import type { FetchLike } from "../transport.ts";
37
39
 
38
40
  export interface PresetProviderConfig extends ProviderFallbackConfig {
@@ -93,6 +95,10 @@ export interface PresetProviderConfig extends ProviderFallbackConfig {
93
95
  /** Default `service_tier` for every call on the OpenAI and Responses shapes;
94
96
  * `StreamOptions.serviceTier` wins. Anthropic and Gemini presets refuse it. */
95
97
  serviceTier?: ServiceTier;
98
+ /** For providers imported from `@providerkit/core/hosted`.
99
+ * The hosted tools this endpoint runs, once you have checked it. Absent,
100
+ * each adapter decides by host. See `StreamOptions.hostedTools`. */
101
+ servesHostedTools?: readonly HostedToolType[];
96
102
  fetchImpl?: FetchLike;
97
103
  }
98
104
 
@@ -104,23 +110,37 @@ export type PresetAdapterConfig = Omit<
104
110
  >;
105
111
 
106
112
  export function createPresetProvider(id: ProviderPresetId, config: PresetProviderConfig): Provider {
113
+ return presetProvider(id, config);
114
+ }
115
+
116
+ /** @internal Shared by the plain and hosted entry points. */
117
+ export function presetProvider(
118
+ id: ProviderPresetId,
119
+ config: PresetProviderConfig,
120
+ codecs?: HostedCodecs,
121
+ ): Provider {
107
122
  const preset = presetFor(id);
108
123
  const chain = config.models?.length ? config.models : config.model ? undefined : preset.rotation;
109
124
  if (chain && chain.length > 1) {
110
125
  const { models: _models, fallbacks = [], ...single } = config;
111
- return createPresetProvider(id, {
112
- ...single,
113
- model: chain[0],
114
- fallbacks: [
115
- ...chain.slice(1).map((model) => ({ ...single, preset: id, model })),
116
- ...fallbacks,
117
- ],
118
- });
126
+ return presetProvider(
127
+ id,
128
+ {
129
+ ...single,
130
+ model: chain[0],
131
+ fallbacks: [
132
+ ...chain.slice(1).map((model) => ({ ...single, preset: id, model })),
133
+ ...fallbacks,
134
+ ],
135
+ },
136
+ codecs,
137
+ );
119
138
  }
120
139
  const { models: _models, ...single } = config;
121
- return withConfiguredFallbacks(
122
- presetAdapter(id, { ...single, model: chain?.[0] ?? config.model }),
140
+ return configuredFallbacks(
141
+ buildPresetAdapter(id, { ...single, model: chain?.[0] ?? config.model }, codecs),
123
142
  config,
143
+ codecs,
124
144
  );
125
145
  }
126
146
 
@@ -136,6 +156,15 @@ export function createPresetProvider(id: ProviderPresetId, config: PresetProvide
136
156
  * effort dialect from it (`opencode-go`, `openrouter`, `deepseek`).
137
157
  */
138
158
  export function presetAdapter(id: ProviderPresetId, config: PresetAdapterConfig): Provider {
159
+ return buildPresetAdapter(id, config);
160
+ }
161
+
162
+ /** @internal Preset routing is shared; only the hosted codecs differ. */
163
+ export function buildPresetAdapter(
164
+ id: ProviderPresetId,
165
+ config: PresetAdapterConfig,
166
+ codecs?: HostedCodecs,
167
+ ): Provider {
139
168
  const preset = presetFor(id);
140
169
  const model = config.model ?? preset.defaultModel;
141
170
  if (!model) {
@@ -148,9 +177,9 @@ export function presetAdapter(id: ProviderPresetId, config: PresetAdapterConfig)
148
177
  "Remove serviceTier.",
149
178
  );
150
179
  }
151
- let provider = buildWireAdapter(id, preset, wire, model, config, config.fetchImpl);
180
+ let provider = buildWireAdapter(id, preset, wire, model, config, config.fetchImpl, codecs);
152
181
  if (id === "opencode-zen") {
153
- provider = wrapOpenCodeZen(provider, preset, wire, config, model);
182
+ provider = wrapOpenCodeZen(provider, preset, wire, config, model, codecs);
154
183
  }
155
184
 
156
185
  // Here rather than in `createPresetProvider`: the wire is resolved here, and
@@ -167,6 +196,7 @@ function buildWireAdapter(
167
196
  model: string,
168
197
  config: PresetAdapterConfig,
169
198
  fetchImpl: FetchLike | undefined,
199
+ codecs?: HostedCodecs,
170
200
  ): Provider {
171
201
  const headers = { ...preset.headers, ...config.headers };
172
202
  const baseUrl = config.baseUrl ?? wire.baseUrl;
@@ -186,72 +216,84 @@ function buildWireAdapter(
186
216
  case "anthropic":
187
217
  // `key` rides Anthropic's native x-api-key; coding plans and OAuth
188
218
  // tokens read Bearer (measured across the family's coding endpoints).
189
- return createAnthropicProvider({
190
- ...baseConfig,
191
- apiKey: config.apiKey,
192
- model,
193
- effort: config.effort,
194
- maxTokens,
195
- baseUrl,
196
- id,
197
- headers,
198
- bearer: wire.auth !== "key",
199
- ...(preset.sessionHeader ? { sessionHeader: preset.sessionHeader } : {}),
200
- // The endpoint's thinking dialect rides the table (zai: silence means
201
- // the model default, which is ON — "none" must be said out loud).
202
- ...(preset.explicitNone ? { explicitNone: true } : {}),
203
- fetchImpl,
204
- });
219
+ return anthropicProvider(
220
+ {
221
+ ...baseConfig,
222
+ apiKey: config.apiKey,
223
+ model,
224
+ effort: config.effort,
225
+ maxTokens,
226
+ baseUrl,
227
+ id,
228
+ headers,
229
+ bearer: wire.auth !== "key",
230
+ ...(preset.sessionHeader ? { sessionHeader: preset.sessionHeader } : {}),
231
+ // The endpoint's thinking dialect rides the table (zai: silence means
232
+ // the model default, which is ON — "none" must be said out loud).
233
+ ...(preset.explicitNone ? { explicitNone: true } : {}),
234
+ fetchImpl,
235
+ },
236
+ codecs,
237
+ );
205
238
  case "openai":
206
- return createOpenAIProvider({
207
- ...baseConfig,
208
- id,
209
- apiKey: config.apiKey,
210
- model,
211
- effort: config.effort,
212
- maxTokens,
213
- baseUrl,
214
- ...(wire.path ? { path: wire.path } : {}),
215
- headers,
216
- ...(config.providerOrder ? { providerOrder: config.providerOrder } : {}),
217
- ...(config.dataCollection ? { dataCollection: config.dataCollection } : {}),
218
- ...(preset.sessionHeader ? { sessionHeader: preset.sessionHeader } : {}),
219
- ...(preset.initiatorHeader ? { initiatorHeader: true } : {}),
220
- ...(preset.noSampling ? { noSampling: true } : {}),
221
- fetchImpl,
222
- });
239
+ return openaiProvider(
240
+ {
241
+ ...baseConfig,
242
+ id,
243
+ apiKey: config.apiKey,
244
+ model,
245
+ effort: config.effort,
246
+ maxTokens,
247
+ baseUrl,
248
+ ...(wire.path ? { path: wire.path } : {}),
249
+ headers,
250
+ ...(config.providerOrder ? { providerOrder: config.providerOrder } : {}),
251
+ ...(config.dataCollection ? { dataCollection: config.dataCollection } : {}),
252
+ ...(preset.sessionHeader ? { sessionHeader: preset.sessionHeader } : {}),
253
+ ...(preset.initiatorHeader ? { initiatorHeader: true } : {}),
254
+ ...(preset.noSampling ? { noSampling: true } : {}),
255
+ fetchImpl,
256
+ },
257
+ codecs,
258
+ );
223
259
  case "responses":
224
- return createResponsesProvider({
225
- ...baseConfig,
226
- apiKey: config.apiKey,
227
- model,
228
- effort: config.effort,
229
- maxTokens,
230
- baseUrl,
231
- ...(wire.path ? { path: wire.path } : {}),
232
- id,
233
- headers,
234
- ...(preset.sessionHeader ? { sessionHeader: preset.sessionHeader } : {}),
235
- ...(preset.imageLimits ? { imageLimits: preset.imageLimits } : {}),
236
- ...(preset.replayReasoning ? { replayReasoning: true } : {}),
237
- fetchImpl,
238
- });
260
+ return responsesProvider(
261
+ {
262
+ ...baseConfig,
263
+ apiKey: config.apiKey,
264
+ model,
265
+ effort: config.effort,
266
+ maxTokens,
267
+ baseUrl,
268
+ ...(wire.path ? { path: wire.path } : {}),
269
+ id,
270
+ headers,
271
+ ...(preset.sessionHeader ? { sessionHeader: preset.sessionHeader } : {}),
272
+ ...(preset.imageLimits ? { imageLimits: preset.imageLimits } : {}),
273
+ ...(preset.replayReasoning ? { replayReasoning: true } : {}),
274
+ fetchImpl,
275
+ },
276
+ codecs,
277
+ );
239
278
  case "gemini":
240
- return createGeminiProvider({
241
- ...baseConfig,
242
- apiKey: config.apiKey,
243
- model,
244
- effort: config.effort,
245
- maxTokens,
246
- baseUrl,
247
- ...(wire.path ? { path: wire.path } : {}),
248
- bearer: wire.auth !== "key",
249
- ...(config.projectId ? { projectId: config.projectId } : {}),
250
- ...(preset.sessionHeader ? { sessionHeader: preset.sessionHeader } : {}),
251
- id,
252
- headers,
253
- fetchImpl,
254
- });
279
+ return geminiProvider(
280
+ {
281
+ ...baseConfig,
282
+ apiKey: config.apiKey,
283
+ model,
284
+ effort: config.effort,
285
+ maxTokens,
286
+ baseUrl,
287
+ ...(wire.path ? { path: wire.path } : {}),
288
+ bearer: wire.auth !== "key",
289
+ ...(config.projectId ? { projectId: config.projectId } : {}),
290
+ ...(preset.sessionHeader ? { sessionHeader: preset.sessionHeader } : {}),
291
+ id,
292
+ headers,
293
+ fetchImpl,
294
+ },
295
+ codecs,
296
+ );
255
297
  }
256
298
  }
257
299
 
@@ -298,7 +340,7 @@ export async function forgeOpenCodeSessionId(sessionId?: string): Promise<string
298
340
  * OpenCode's own `read` (`cc-proxy/src/providers/opencode/mod.rs`). A call with
299
341
  * `Read` sends it as `read` and gets `Read` back; a call with neither gets a
300
342
  * decoy `read` the model is told not to call (and `toolChoice: "none"` when the
301
- * caller passed no tools).
343
+ * caller passed no tools, and no hosted tools either, which `none` would drop).
302
344
  */
303
345
  function offerOpenCodeRead(
304
346
  messages: ChatMessage[],
@@ -323,7 +365,8 @@ function offerOpenCodeRead(
323
365
  return {
324
366
  messages,
325
367
  tools: [...tools, decoy],
326
- opts: tools.length === 0 ? { ...opts, toolChoice: "none" } : opts,
368
+ opts:
369
+ tools.length === 0 && !opts.hostedTools?.length ? { ...opts, toolChoice: "none" } : opts,
327
370
  renamedRead: false,
328
371
  };
329
372
  }
@@ -353,6 +396,7 @@ function wrapOpenCodeZen(
353
396
  wire: Wire,
354
397
  config: PresetAdapterConfig,
355
398
  defaultModel: string,
399
+ codecs?: HostedCodecs,
356
400
  ): Provider {
357
401
  const instanceSessionId = config.instanceSessionId ?? crypto.randomUUID();
358
402
  const baseFetch = config.fetchImpl ?? fetch;
@@ -399,6 +443,7 @@ function wrapOpenCodeZen(
399
443
  defaultModel,
400
444
  config,
401
445
  fetchImpl,
446
+ codecs,
402
447
  );
403
448
  for await (const chunk of bound.createStream(prepared.messages, prepared.tools, {
404
449
  ...prepared.opts,