@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,6 +11,8 @@ import {
11
11
  streamError,
12
12
  validateMaxAttempts,
13
13
  } from "../errors.ts";
14
+ import type { HostedToolType } from "../hosted/types.ts";
15
+ import { refuseHostedTools, type HostedCodecs } from "./hosted-codecs.ts";
14
16
  import { streamSse, apiUrl, parseFrame, type FetchLike } from "../transport.ts";
15
17
  import { settleToolCalls, type ToolCallIdentity } from "../tool-args.ts";
16
18
  import { attributionHeaders } from "../attribution.ts";
@@ -28,7 +30,7 @@ import type {
28
30
  } from "../types.ts";
29
31
  import { toDataUri } from "../types.ts";
30
32
  import { isStrictSchema, schemaPrompt } from "../schema.ts";
31
- import { withConfiguredFallbacks, type ProviderFallbackConfig } from "../fallback.ts";
33
+ import { configuredFallbacks, type ProviderFallbackConfig } from "../fallback.ts";
32
34
 
33
35
  export interface OpenAIConfig extends ProviderFallbackConfig {
34
36
  apiKey: string;
@@ -139,6 +141,16 @@ export interface OpenAIConfig extends ProviderFallbackConfig {
139
141
  noSampling?: boolean;
140
142
  /** Default `service_tier` for every call; `StreamOptions.serviceTier` wins. */
141
143
  serviceTier?: ServiceTier;
144
+ /**
145
+ * For providers imported from `@providerkit/core/hosted`.
146
+ * The hosted tools this endpoint runs. This wire has one, web search.
147
+ * Defaults to it on openrouter.ai, which runs it as its own server tool,
148
+ * and on api.openai.com for the `-search-preview` models, and to none
149
+ * anywhere else: a gateway that ignores the tool answers unsearched
150
+ * without saying so. A host other than OpenRouter that runs it is sent
151
+ * OpenAI's `web_search_options`.
152
+ */
153
+ servesHostedTools?: readonly HostedToolType[];
142
154
  }
143
155
 
144
156
  const DEFAULT_BASE_URL = "https://api.openai.com";
@@ -476,7 +488,7 @@ export function toOpenAIMessages(messages: readonly ChatMessage[], provider = "o
476
488
  return out;
477
489
  }
478
490
 
479
- interface OpenAIChunk {
491
+ export interface OpenAIChunk {
480
492
  choices?: {
481
493
  delta?: {
482
494
  content?: string | null;
@@ -484,6 +496,8 @@ interface OpenAIChunk {
484
496
  reasoning?: string | null;
485
497
  /** OpenRouter's normalized reasoning payload, on the final delta. */
486
498
  reasoning_details?: unknown[];
499
+ /** The sources a searched answer cites. */
500
+ annotations?: import("../hosted/openai.ts").ChatAnnotation[];
487
501
  tool_calls?: {
488
502
  index?: number;
489
503
  id?: string;
@@ -507,6 +521,8 @@ interface OpenAIChunk {
507
521
  cost?: unknown;
508
522
  is_byok?: boolean;
509
523
  cost_details?: { upstream_inference_cost?: unknown } | null;
524
+ /** The server tool calls OpenRouter ran for the turn. */
525
+ server_tool_use_details?: { web_search_requests?: number } | null;
510
526
  } | null;
511
527
  /** Present only on the in-band failure below. Never beside a choice.
512
528
  * `code` is the numeric HTTP status on the gateways, a slug on OpenAI. */
@@ -550,6 +566,11 @@ export function openRouterCostUsd(usage: {
550
566
  }
551
567
 
552
568
  export function createOpenAIProvider(config: OpenAIConfig): Provider {
569
+ return openaiProvider(config);
570
+ }
571
+
572
+ /** @internal Shared by the plain and hosted entry points. */
573
+ export function openaiProvider(config: OpenAIConfig, codecs?: HostedCodecs): Provider {
553
574
  const baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;
554
575
  const id = config.id ?? "openai";
555
576
  // Keyed on the host, not the id: the host is who sends the bill, and
@@ -570,6 +591,7 @@ export function createOpenAIProvider(config: OpenAIConfig): Provider {
570
591
  opts: StreamOptions = {},
571
592
  ): AsyncIterable<ProviderChunk> {
572
593
  validateMaxAttempts(opts.maxAttempts);
594
+ if (!codecs) refuseHostedTools(id, opts, messages);
573
595
  const effort = opts.effort ?? config.effort;
574
596
  const model = opts.model ?? config.model;
575
597
 
@@ -594,18 +616,25 @@ export function createOpenAIProvider(config: OpenAIConfig): Provider {
594
616
  const serviceTier = opts.serviceTier ?? config.serviceTier;
595
617
  if (serviceTier) request.service_tier = serviceTier;
596
618
  Object.assign(request, effortParams(config.effortDialect ?? dialectFor(id), effort, model));
597
- if (tools.length > 0) {
598
- request.tools = tools.map((tool) => ({
599
- type: "function",
600
- function: {
601
- name: tool.name,
602
- description: tool.description,
603
- parameters: tool.inputSchema,
604
- },
605
- }));
619
+ const openRouter = isOpenRouter(baseUrl);
620
+ const hosted = codecs?.openai.plan(config, baseUrl, id, model, openRouter, opts);
621
+ if (tools.length > 0 || hosted?.tools.length) {
622
+ request.tools = [
623
+ ...tools.map((tool) => ({
624
+ type: "function",
625
+ function: {
626
+ name: tool.name,
627
+ description: tool.description,
628
+ parameters: tool.inputSchema,
629
+ },
630
+ })),
631
+ ...(hosted?.tools ?? []),
632
+ ];
606
633
  // Only beside tools: OpenAI refuses the field on a request without any.
607
- if (opts.parallelToolCalls === false) request.parallel_tool_calls = false;
634
+ if (tools.length > 0 && opts.parallelToolCalls === false)
635
+ request.parallel_tool_calls = false;
608
636
  }
637
+ if (hosted?.searchOptions) request.web_search_options = hosted.searchOptions;
609
638
  if (opts.toolChoice && opts.toolChoice !== "auto") {
610
639
  request.tool_choice =
611
640
  typeof opts.toolChoice === "string"
@@ -620,7 +649,8 @@ export function createOpenAIProvider(config: OpenAIConfig): Provider {
620
649
  // here the only job is to leave the response format off those calls, so
621
650
  // the schema reaches the model the one way nothing can suppress.
622
651
  const promptCarried =
623
- tools.length > 0 && (opts.jsonWithTools ?? config.jsonWithTools) === "prompt";
652
+ (tools.length > 0 || hosted?.active) &&
653
+ (opts.jsonWithTools ?? config.jsonWithTools) === "prompt";
624
654
  const enforced =
625
655
  !promptCarried &&
626
656
  (config.jsonMode ?? (id === "openai" ? "schema" : "object")) === "schema";
@@ -687,6 +717,7 @@ export function createOpenAIProvider(config: OpenAIConfig): Provider {
687
717
  // Each call as its deltas named it, by index. The finish checks them
688
718
  // before it goes out, and they are what says the turn called a tool.
689
719
  const calls = new Map<number, ToolCallIdentity>();
720
+ const hostedStream = codecs?.openai.stream();
690
721
  for await (const data of streamSse({
691
722
  url: apiUrl(baseUrl, config.path ?? defaultPath),
692
723
  headers: {
@@ -738,6 +769,7 @@ export function createOpenAIProvider(config: OpenAIConfig): Provider {
738
769
  chunk.usage.cached_tokens ??
739
770
  0;
740
771
  const reportedCostUsd = reportsCost ? openRouterCostUsd(chunk.usage) : undefined;
772
+ const hostedCalls = codecs?.openai.counts(chunk.usage);
741
773
  yield {
742
774
  type: "usage",
743
775
  usage: {
@@ -750,6 +782,7 @@ export function createOpenAIProvider(config: OpenAIConfig): Provider {
750
782
  ? { reasoningTokens: chunk.usage.completion_tokens_details.reasoning_tokens }
751
783
  : {}),
752
784
  ...(reportedCostUsd !== undefined ? { reportedCostUsd } : {}),
785
+ ...(hostedCalls ? { hostedCalls } : {}),
753
786
  },
754
787
  };
755
788
  }
@@ -765,6 +798,7 @@ export function createOpenAIProvider(config: OpenAIConfig): Provider {
765
798
  out.content = delta.content;
766
799
  has = true;
767
800
  }
801
+ hostedStream?.delta(delta);
768
802
  // `reasoning_content` is DeepSeek's field; `reasoning` is
769
803
  // OpenRouter's normalized one. Whichever arrives is the same thing.
770
804
  const reasoning = delta.reasoning_content ?? delta.reasoning;
@@ -799,6 +833,7 @@ export function createOpenAIProvider(config: OpenAIConfig): Provider {
799
833
 
800
834
  if (choice.finish_reason) {
801
835
  const stated = mapFinishReason(id, choice.finish_reason);
836
+ if (hostedStream) yield* hostedStream.close();
802
837
  const minted = settleToolCalls(id, calls.values());
803
838
  if (minted.length > 0) yield { type: "delta", toolCalls: minted };
804
839
  ended = true;
@@ -812,11 +847,13 @@ export function createOpenAIProvider(config: OpenAIConfig): Provider {
812
847
  // Either proof will do: some gateways send `[DONE]` with no finish, and
813
848
  // some a finish with no `[DONE]`. Neither means the socket closed early.
814
849
  if (!ended) throw streamCut(id);
815
- // A `[DONE]` with no finish stated still owes its calls the same check.
850
+ // A `[DONE]` with no finish stated still owes its calls the same check,
851
+ // and its citations their place.
852
+ if (hostedStream) yield* hostedStream.close();
816
853
  const minted = settleToolCalls(id, calls.values());
817
854
  if (minted.length > 0) yield { type: "delta", toolCalls: minted };
818
855
  },
819
856
  };
820
857
 
821
- return withConfiguredFallbacks(provider, config);
858
+ return configuredFallbacks(provider, config, codecs);
822
859
  }
@@ -10,6 +10,11 @@
10
10
  // The event names arrive on the SSE `event:` line and are repeated inside each
11
11
  // payload's own `type`. The transport yields only `data:` payloads, so this
12
12
  // adapter reads `type` — which is what survives, and what gateways agree on.
13
+ //
14
+ // Hosted tool shapes, include values and item fields follow Vercel's AI SDK
15
+ // (Apache-2.0, Copyright 2023 Vercel, Inc.: openai-responses-prepare-tools.ts,
16
+ // openai-responses-language-model.ts, map-openai-responses-annotation.ts); the
17
+ // marker-span check follows PydanticAI (MIT). See THIRD_PARTY_NOTICES.
13
18
  import { repairHistory } from "../context.ts";
14
19
  import {
15
20
  fileRefused,
@@ -18,6 +23,8 @@ import {
18
23
  streamError,
19
24
  validateMaxAttempts,
20
25
  } from "../errors.ts";
26
+ import type { HostedToolType } from "../hosted/types.ts";
27
+ import { refuseHostedTools, type HostedCodecs } from "./hosted-codecs.ts";
21
28
  import { streamSse, apiUrl, parseFrame, type FetchLike } from "../transport.ts";
22
29
  import { parseRateLimitReset } from "../rate-limit.ts";
23
30
  import { settleToolCalls } from "../tool-args.ts";
@@ -35,7 +42,7 @@ import type {
35
42
  } from "../types.ts";
36
43
  import { toDataUri } from "../types.ts";
37
44
  import { isStrictSchema, withoutPatterns } from "../schema.ts";
38
- import { withConfiguredFallbacks, type ProviderFallbackConfig } from "../fallback.ts";
45
+ import { configuredFallbacks, type ProviderFallbackConfig } from "../fallback.ts";
39
46
  import { gateImages, type ImageLimits } from "../image.ts";
40
47
 
41
48
  export interface ResponsesConfig extends ProviderFallbackConfig {
@@ -83,6 +90,14 @@ export interface ResponsesConfig extends ProviderFallbackConfig {
83
90
  replayReasoning?: boolean;
84
91
  /** Default `service_tier` for every call; `StreamOptions.serviceTier` wins. */
85
92
  serviceTier?: ServiceTier;
93
+ /**
94
+ * For providers imported from `@providerkit/core/hosted`.
95
+ * The hosted tools this endpoint runs. Defaults to web search, code
96
+ * execution, file search and image generation on api.openai.com, and to
97
+ * none anywhere else, the ChatGPT backend included, until someone checks
98
+ * that endpoint. One that ignores a tool answers unsearched without saying so.
99
+ */
100
+ servesHostedTools?: readonly HostedToolType[];
86
101
  }
87
102
 
88
103
  const DEFAULT_BASE_URL = "https://api.openai.com";
@@ -275,7 +290,7 @@ interface ResponsesUsage {
275
290
  output_tokens_details?: { reasoning_tokens?: number };
276
291
  }
277
292
 
278
- interface ResponsesItem {
293
+ export interface ResponsesItem {
279
294
  type?: string;
280
295
  /** The output item's own id (`fc_…`), which the argument deltas reference. */
281
296
  id?: string;
@@ -284,12 +299,36 @@ interface ResponsesItem {
284
299
  name?: string;
285
300
  arguments?: string;
286
301
  encrypted_content?: string;
302
+ /** A hosted item's state: `in_progress`, `searching`, `completed`, `failed`… */
303
+ status?: string;
304
+ error?: { code?: string; message?: string } | null;
305
+ /** `web_search_call`: what the search did, sources included when asked for. */
306
+ action?: {
307
+ type?: string;
308
+ query?: string | null;
309
+ queries?: string[] | null;
310
+ url?: string | null;
311
+ sources?: { type?: string; url?: string }[] | null;
312
+ } | null;
313
+ /** `code_interpreter_call` */
314
+ code?: string | null;
315
+ container_id?: string;
316
+ outputs?: { type?: string; logs?: string; url?: string }[] | null;
317
+ /** `file_search_call` */
318
+ queries?: string[] | null;
319
+ results?: { file_id?: string; filename?: string; text?: string }[] | null;
320
+ /** `image_generation_call`: the image, as base64. */
321
+ result?: string | null;
322
+ revised_prompt?: string | null;
287
323
  }
288
324
 
289
- interface ResponsesEvent {
325
+ export interface ResponsesEvent {
290
326
  type?: string;
291
327
  delta?: string;
292
328
  item_id?: string;
329
+ /** Which part of a message item a text delta or annotation belongs to. */
330
+ content_index?: number;
331
+ annotation?: import("../hosted/responses.ts").ResponsesAnnotation;
293
332
  /** xAI keys argument events by the CALL id instead of the item id. */
294
333
  call_id?: string;
295
334
  /** The whole argument string, on `function_call_arguments.done`. */
@@ -373,7 +412,12 @@ function codexHeaders(limits: CodexRateLimits): Headers {
373
412
  return headers;
374
413
  }
375
414
 
376
- function usageChunk(usage: ResponsesUsage): ProviderChunk {
415
+ /** The usage record, with the hosted calls the stream showed: this shape bills
416
+ * them per call and states no count of its own. */
417
+ function usageChunk(
418
+ usage: ResponsesUsage,
419
+ hostedCalls?: Partial<Record<HostedToolType, number>>,
420
+ ): ProviderChunk {
377
421
  return {
378
422
  type: "usage",
379
423
  usage: {
@@ -386,6 +430,7 @@ function usageChunk(usage: ResponsesUsage): ProviderChunk {
386
430
  ...(usage.output_tokens_details?.reasoning_tokens !== undefined
387
431
  ? { reasoningTokens: usage.output_tokens_details.reasoning_tokens }
388
432
  : {}),
433
+ ...(hostedCalls ? { hostedCalls } : {}),
389
434
  },
390
435
  };
391
436
  }
@@ -410,6 +455,11 @@ function unsentTail(snapshot: string, streamed: string): string {
410
455
  }
411
456
 
412
457
  export function createResponsesProvider(config: ResponsesConfig): Provider {
458
+ return responsesProvider(config);
459
+ }
460
+
461
+ /** @internal Shared by the plain and hosted entry points. */
462
+ export function responsesProvider(config: ResponsesConfig, codecs?: HostedCodecs): Provider {
413
463
  const baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;
414
464
  const id = config.id ?? "openai-responses";
415
465
  const instanceSessionId = config.sessionHeader
@@ -426,6 +476,7 @@ export function createResponsesProvider(config: ResponsesConfig): Provider {
426
476
  opts: StreamOptions = {},
427
477
  ): AsyncIterable<ProviderChunk> {
428
478
  validateMaxAttempts(opts.maxAttempts);
479
+ if (!codecs) refuseHostedTools(id, opts, messages);
429
480
  const effort = opts.effort ?? config.effort;
430
481
  const model = opts.model ?? config.model;
431
482
  const museContributor = isMuseSparkContributor(model);
@@ -448,7 +499,16 @@ export function createResponsesProvider(config: ResponsesConfig): Provider {
448
499
  store: false,
449
500
  };
450
501
  if (instructions) request.instructions = instructions;
451
- if (config.replayReasoning) request.include = ["reasoning.encrypted_content"];
502
+ // Hosted items are never sent back: `store` is off, so the backend has
503
+ // no copy to reference, and their results ride in the answer instead.
504
+ const hosted = codecs?.responses.plan(config, baseUrl, id, opts);
505
+ // Merged, not overwritten: the reasoning items and each hosted tool's
506
+ // results are asked for in the same list.
507
+ const include = [
508
+ ...(config.replayReasoning ? ["reasoning.encrypted_content"] : []),
509
+ ...(hosted?.include ?? []),
510
+ ];
511
+ if (include.length > 0) request.include = include;
452
512
  const maxTokens = opts.maxTokens ?? config.maxTokens;
453
513
  if (maxTokens !== undefined) request.max_output_tokens = maxTokens;
454
514
  if (opts.temperature !== undefined) request.temperature = opts.temperature;
@@ -465,22 +525,27 @@ export function createResponsesProvider(config: ResponsesConfig): Provider {
465
525
  request.reasoning =
466
526
  level === "none" ? { effort: "none" } : { effort: level, summary: "auto" };
467
527
  }
468
- if (requestTools.length > 0) {
528
+ if (requestTools.length > 0 || (hosted?.tools.length ?? 0) > 0) {
469
529
  // Flat here — no nested `function` envelope, unlike chat/completions.
470
- request.tools = requestTools.map((tool) => ({
471
- type: "function",
472
- name: tool.name,
473
- description: tool.description,
474
- parameters: withoutPatterns(tool.inputSchema),
475
- // Said either way, never left to the default. Strict demands every
476
- // property required and every object closed; on a schema that is not
477
- // already shaped that way the backend makes optional arguments
478
- // mandatory, and the model starts filling in parameters nobody asked
479
- // for (cc-proxy, 0.1.15). Where the schema already qualifies, the
480
- // enforcement is free.
481
- strict: isStrictSchema(tool.inputSchema),
482
- }));
483
- if (opts.parallelToolCalls === false) request.parallel_tool_calls = false;
530
+ request.tools = [
531
+ ...requestTools.map((tool) => ({
532
+ type: "function",
533
+ name: tool.name,
534
+ description: tool.description,
535
+ parameters: withoutPatterns(tool.inputSchema),
536
+ // Said either way, never left to the default. Strict demands every
537
+ // property required and every object closed; on a schema that is not
538
+ // already shaped that way the backend makes optional arguments
539
+ // mandatory, and the model starts filling in parameters nobody asked
540
+ // for (cc-proxy, 0.1.15). Where the schema already qualifies, the
541
+ // enforcement is free.
542
+ strict: isStrictSchema(tool.inputSchema),
543
+ })),
544
+ ...(hosted?.tools ?? []),
545
+ ];
546
+ if (requestTools.length > 0 && opts.parallelToolCalls === false) {
547
+ request.parallel_tool_calls = false;
548
+ }
484
549
  }
485
550
  // The cache affinity key. Without one the backend spreads a
486
551
  // conversation's turns across cache shards, and a re-sent prefix misses.
@@ -528,7 +593,7 @@ export function createResponsesProvider(config: ResponsesConfig): Provider {
528
593
  reasoningItems.length
529
594
  ? [{ type: "delta", reasoningItems: { provider: id, items: reasoningItems } }]
530
595
  : [];
531
-
596
+ const hostedStream = codecs?.responses.stream(hosted?.hosted ?? []);
532
597
  for await (const data of streamSse({
533
598
  url: apiUrl(baseUrl, config.path ?? DEFAULT_PATH),
534
599
  headers: {
@@ -550,6 +615,8 @@ export function createResponsesProvider(config: ResponsesConfig): Provider {
550
615
  produced = true;
551
616
  }
552
617
 
618
+ if (hostedStream) yield* hostedStream.event(event);
619
+
553
620
  switch (event.type) {
554
621
  case "response.output_text.delta":
555
622
  if (event.delta) yield { type: "delta", content: event.delta };
@@ -716,8 +783,9 @@ export function createResponsesProvider(config: ResponsesConfig): Provider {
716
783
  const minted = settleToolCalls(id, calls);
717
784
  if (minted.length > 0) yield { type: "delta", toolCalls: minted };
718
785
  }
786
+ if (hostedStream) yield* hostedStream.close();
719
787
  yield* reasoningChunk();
720
- if (response?.usage) yield usageChunk(response.usage);
788
+ if (response?.usage) yield usageChunk(response.usage, hostedStream?.counts());
721
789
  yield { type: "finish", finishReason };
722
790
  return;
723
791
  }
@@ -730,9 +798,10 @@ export function createResponsesProvider(config: ResponsesConfig): Provider {
730
798
  // A turn that ran out of output tokens still billed for its input,
731
799
  // and this event carries usage in the same shape as `completed`.
732
800
  const finishReason = incompleteFinish(id, event.response?.incomplete_details?.reason);
801
+ if (hostedStream) yield* hostedStream.close();
733
802
  yield* reasoningChunk();
734
803
  const usage = event.response?.usage;
735
- if (usage) yield usageChunk(usage);
804
+ if (usage) yield usageChunk(usage, hostedStream?.counts());
736
805
  yield { type: "finish", finishReason };
737
806
  return;
738
807
  }
@@ -764,5 +833,5 @@ export function createResponsesProvider(config: ResponsesConfig): Provider {
764
833
  },
765
834
  };
766
835
 
767
- return withConfiguredFallbacks(provider, config);
836
+ return configuredFallbacks(provider, config, codecs);
768
837
  }
@@ -0,0 +1,68 @@
1
+ // The transcription twin of `createSpeechProvider`: name a preset, hand over a key, get a
2
+ // transcription provider. The preset row (`ProviderPreset.transcription`) picks the wire.
3
+ import {
4
+ PROVIDER_PRESETS,
5
+ TRANSCRIPTION_PRESET_IDS,
6
+ type ProviderPreset,
7
+ type ProviderPresetId,
8
+ } from "../presets.ts";
9
+ import type { Attribution } from "../attribution.ts";
10
+ import type { TranscriptionLimits, TranscriptionProvider } from "../transcription-core.ts";
11
+ import { createOpenAITranscriptionProvider } from "./openai-transcription.ts";
12
+ import type { FetchLike } from "../transport.ts";
13
+
14
+ export interface TranscriptionProviderConfig extends TranscriptionLimits, Attribution {
15
+ apiKey: string;
16
+ /** Absent: the preset's `transcription.defaultModel`. A preset without one needs this. */
17
+ model?: string;
18
+ /**
19
+ * Replaces the preset's host, for a server that copies its route (Groq, a local Whisper
20
+ * server, a proxy). The preset's default model is a fact about its own host, so it does not
21
+ * follow you here: pass the `model`.
22
+ */
23
+ baseUrl?: string;
24
+ headers?: Record<string, string>;
25
+ /** Swapped in tests, or to route through a proxy. Defaults to global fetch. */
26
+ fetchImpl?: FetchLike;
27
+ }
28
+
29
+ /**
30
+ * Build a transcription provider for a preset that can transcribe. A preset without a
31
+ * `transcription` row throws, naming the ones that do (`TRANSCRIPTION_PRESET_IDS`). Deepgram is
32
+ * not a chat preset: `createDeepgramTranscriptionProvider` builds it.
33
+ *
34
+ * ```ts
35
+ * const listen = createTranscriptionProvider("openai", { apiKey });
36
+ * const { text } = await listen.transcribe({ audio: file });
37
+ * ```
38
+ */
39
+ export function createTranscriptionProvider(
40
+ id: ProviderPresetId,
41
+ config: TranscriptionProviderConfig,
42
+ ): TranscriptionProvider {
43
+ const preset: ProviderPreset = PROVIDER_PRESETS[id];
44
+ const spec = preset.transcription;
45
+ if (!spec) {
46
+ throw new Error(
47
+ `[providerkit] Preset "${id}" has no transcription endpoint. Presets that do: ${TRANSCRIPTION_PRESET_IDS.join(", ")}. Deepgram has its own: createDeepgramTranscriptionProvider.`,
48
+ );
49
+ }
50
+ // A default is a fact about the host that documented it.
51
+ const own = config.baseUrl === undefined;
52
+ const model = config.model ?? (own ? spec.defaultModel : undefined);
53
+ if (!model) {
54
+ throw new Error(
55
+ `[providerkit] Transcription on "${id}" has no default model${own ? "" : " for another host"}. Pass a model.`,
56
+ );
57
+ }
58
+ const shared = {
59
+ ...config,
60
+ id,
61
+ model,
62
+ baseUrl: config.baseUrl ?? spec.baseUrl ?? preset.baseUrl,
63
+ };
64
+ switch (spec.shape) {
65
+ case "openai":
66
+ return createOpenAITranscriptionProvider(shared);
67
+ }
68
+ }
package/src/registry.ts CHANGED
@@ -19,7 +19,8 @@ import { withFallbackProviders, type Cooldown, type FallbackOptions } from "./fa
19
19
  import { createGate, withGate, type Gate } from "./gate.ts";
20
20
  import { KeyPool, withKeyPool, type KeyPoolOptions, type KeyTier } from "./key-pool.ts";
21
21
  import { PROVIDER_PRESETS, type ProviderPreset, type ProviderPresetId } from "./presets.ts";
22
- import { presetAdapter, type PresetAdapterConfig } from "./providers/factory.ts";
22
+ import { buildPresetAdapter, type PresetAdapterConfig } from "./providers/factory.ts";
23
+ import type { HostedCodecs } from "./providers/hosted-codecs.ts";
23
24
  import type { RateLimitWindow } from "./rate-limit.ts";
24
25
  import type { Effort, Provider, ProviderChunk } from "./types.ts";
25
26
  import { withWatchdog, type WatchdogOptions } from "./watchdog.ts";
@@ -130,6 +131,11 @@ interface Member {
130
131
  }
131
132
 
132
133
  export function createRegistry(options: RegistryOptions): Registry {
134
+ return providerRegistry(options);
135
+ }
136
+
137
+ /** @internal The same registry, with codecs bound to every rebuilt adapter. */
138
+ export function providerRegistry(options: RegistryOptions, codecs?: HostedCodecs): Registry {
133
139
  const connections = new Map<string, Connection>();
134
140
  const chainRefs = new Map(Object.entries(options.chains ?? {}));
135
141
  /** Built chains by ref, with the connections each one uses. */
@@ -264,15 +270,19 @@ export function createRegistry(options: RegistryOptions): Registry {
264
270
  // The credential's host and headers win: Copilot gives each plan its
265
271
  // own host, and the Codex backend refuses a call without its account id.
266
272
  provider = withToken(config.token, (auth) =>
267
- presetAdapter(preset, { ...base, ...auth, headers: { ...headers, ...auth.headers } }),
273
+ buildPresetAdapter(
274
+ preset,
275
+ { ...base, ...auth, headers: { ...headers, ...auth.headers } },
276
+ codecs,
277
+ ),
268
278
  );
269
279
  } else if (config.keys) {
270
280
  // `open` builds the pool whenever `keys` is set.
271
281
  provider = withKeyPool(keyPool!, (key) =>
272
- presetAdapter(preset, { ...base, headers, apiKey: key }),
282
+ buildPresetAdapter(preset, { ...base, headers, apiKey: key }, codecs),
273
283
  );
274
284
  } else {
275
- provider = presetAdapter(preset, { ...base, headers, apiKey: config.apiKey });
285
+ provider = buildPresetAdapter(preset, { ...base, headers, apiKey: config.apiKey }, codecs);
276
286
  }
277
287
  // On unless turned off, where `createPresetProvider` adds one only when
278
288
  // asked. Handing apps the whole composition is the point of a registry:
@@ -10,7 +10,7 @@
10
10
  // 2. Audio is named by its own bytes, not by what was asked for. Many servers ignore
11
11
  // the format they were sent, and "WebM with an MP4 name" is not MP4.
12
12
  import { ProviderError, streamError, type ErrorKind } from "./errors.ts";
13
- import { withDeadline } from "./transport.ts";
13
+ import { callOnce, noReplay as sharedNoReplay, type ErrorRebuild } from "./transport.ts";
14
14
  import type { TokenUsage } from "./types.ts";
15
15
 
16
16
  export const SPEECH_FORMATS = ["wav", "pcm", "mp3", "opus", "aac", "flac"] as const;
@@ -178,63 +178,25 @@ export function prepareSpeech(
178
178
  return { text, voice, format };
179
179
  }
180
180
 
181
- const REPLAY_UNSAFE: ReadonlySet<ErrorKind> = new Set<ErrorKind>([
182
- "network",
183
- "timeout",
184
- "overload",
185
- "unknown",
186
- ]);
187
-
188
- function neverRetry(err: ProviderError): ProviderError {
189
- const opts = {
190
- status: err.status,
191
- code: err.code,
192
- retryAfterMs: err.retryAfterMs,
193
- resetAtMs: err.resetAtMs,
194
- window: err.window,
195
- shouldRetry: false,
196
- body: err.body,
197
- cause: err.cause,
198
- };
199
- return err instanceof SpeechError
181
+ /** A `SpeechError` comes back as itself, with the usage it carries. */
182
+ const asSpeechError: ErrorRebuild = (err, opts) =>
183
+ err instanceof SpeechError
200
184
  ? new SpeechError(err.provider, err.kind, err.message, { ...opts, usage: err.usage })
201
185
  : new ProviderError(err.provider, err.kind, err.message, opts);
202
- }
203
186
 
204
- /**
205
- * @internal The "could the vendor already have billed this?" rule, applied in one
206
- * place so a new failure path cannot forget it.
207
- *
208
- * - The caller's Stop comes back untouched.
209
- * - A timeout, a dead socket, a 5xx or anything unclassified may have landed after
210
- * the vendor started work, so it is marked `shouldRetry: false`, and a vendor's
211
- * `x-should-retry: true` does not undo that: the header says the vendor thinks a
212
- * resend is fine, not that the first call was not billed.
213
- * - Every other kind keeps what it came with. A 429 means the vendor refused before
214
- * doing any work, so sending it again is safe, and `Retry-After` is honoured by
215
- * the caller's own retry. `x-should-retry` is believed there, in either direction.
216
- */
187
+ /** @internal The shared no-replay rule (`transport.ts`), keeping a `SpeechError` whole. */
217
188
  export function noReplay(provider: string, error: unknown, signal?: AbortSignal): unknown {
218
- if (signal?.aborted) return error;
219
- const err = ProviderError.from(provider, error);
220
- return REPLAY_UNSAFE.has(err.kind) && err.shouldRetry !== false ? neverRetry(err) : err;
189
+ return sharedNoReplay(provider, error, signal, asSpeechError);
221
190
  }
222
191
 
223
- /**
224
- * @internal The one place a call runs: under one deadline, once, and with the
225
- * no-replay rule applied to whatever it throws. Both wires go through here.
226
- */
227
- export async function synthesizeOnce<T>(
192
+ /** @internal One paid call, once, under one deadline. All three wires go through here. */
193
+ export function synthesizeOnce<T>(
228
194
  provider: string,
229
195
  timeoutMs: number,
230
196
  request: SpeechRequest,
231
197
  call: (signal: AbortSignal) => Promise<T>,
232
198
  ): Promise<T> {
233
- try {
234
- return await withDeadline(provider, timeoutMs, request.signal, call);
235
- } catch (error) {
236
- throw noReplay(provider, error, request.signal);
237
- }
199
+ return callOnce(provider, timeoutMs, request.signal, call, asSpeechError);
238
200
  }
239
201
 
240
202
  // ── failures ──────────────────────────────────────────────────────────────