@ai-sdk/provider-utils 5.0.75 → 5.0.76

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/provider-utils",
3
- "version": "5.0.75",
3
+ "version": "5.0.76",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -42,7 +42,7 @@
42
42
  }
43
43
  },
44
44
  "dependencies": {
45
- "@ai-sdk/provider": "4.0.36",
45
+ "@ai-sdk/provider": "4.0.37",
46
46
  "@standard-schema/spec": "^1.1.0",
47
47
  "@workflow/serde": "4.1.0",
48
48
  "eventsource-parser": "^3.0.8",
@@ -341,13 +341,16 @@ export function detectMediaType({
341
341
  });
342
342
  }
343
343
 
344
- const signatures = topLevelSignatureTables[topLevelType as TopLevelMediaType];
345
-
346
- if (signatures === undefined) {
344
+ if (
345
+ !Object.prototype.hasOwnProperty.call(topLevelSignatureTables, topLevelType)
346
+ ) {
347
347
  return undefined;
348
348
  }
349
349
 
350
- return detectMediaTypeBySignatures({ data, signatures });
350
+ return detectMediaTypeBySignatures({
351
+ data,
352
+ signatures: topLevelSignatureTables[topLevelType as TopLevelMediaType],
353
+ });
351
354
  }
352
355
 
353
356
  /**
@@ -80,6 +80,9 @@ export async function fetchWithValidatedEndpoint({
80
80
  * target (e.g. an internal address) before we ever see its URL, defeating the
81
81
  * guard.
82
82
  *
83
+ * Manually followed redirects from HTTPS to HTTP are rejected before requesting
84
+ * the target.
85
+ *
83
86
  * Request headers are also protected: {@link sanitizeRequestHeaders} strips
84
87
  * proxy/metadata/cookie/hop-by-hop headers before the first request, and all
85
88
  * caller headers except `User-Agent` are dropped on a cross-origin redirect.
@@ -193,6 +196,16 @@ export async function fetchWithValidatedRedirects({
193
196
  void cancelResponseBody(response);
194
197
  const nextUrl = new URL(location, currentUrl).toString();
195
198
 
199
+ if (
200
+ new URL(currentUrl).protocol === 'https:' &&
201
+ new URL(nextUrl).protocol === 'http:'
202
+ ) {
203
+ throw new DownloadError({
204
+ url,
205
+ message: 'Redirect from HTTPS to HTTP was blocked',
206
+ });
207
+ }
208
+
196
209
  // Drop all caller headers except the user-agent before following a
197
210
  // redirect that crosses origin. Only stripping Authorization (as the
198
211
  // fetch spec does) is not enough on the server: providers authenticate
package/src/index.ts CHANGED
@@ -76,6 +76,10 @@ export { normalizeBatchRequestCounts } from './normalize-batch-request-counts';
76
76
  export * from './parse-json';
77
77
  export { parseJsonEventStream } from './parse-json-event-stream';
78
78
  export { parseProviderOptions } from './parse-provider-options';
79
+ export {
80
+ resolveProviderOptions,
81
+ type ProviderOptionsResolution,
82
+ } from './resolve-provider-options';
79
83
  export * from './post-multipart-stream-to-api';
80
84
  export * from './post-to-api';
81
85
  export {
@@ -0,0 +1,68 @@
1
+ import type { FlexibleSchema } from './schema';
2
+ import { parseProviderOptions } from './parse-provider-options';
3
+
4
+ /**
5
+ * How options found under several `providerOptions` namespaces are combined.
6
+ *
7
+ * - `first`: namespaces are aliases. The first namespace that is present wins
8
+ * as a whole and later namespaces are ignored (no merging).
9
+ * - `merge`: every present namespace is parsed and the results are merged in
10
+ * order, so later namespaces override earlier ones key by key.
11
+ */
12
+ export type ProviderOptionsResolution = 'first' | 'merge';
13
+
14
+ /**
15
+ * Parses provider options that a model accepts under more than one
16
+ * `providerOptions` namespace, e.g. a canonical key plus a provider-specific
17
+ * or legacy alias.
18
+ *
19
+ * Each present namespace is validated against `schema` exactly like
20
+ * `parseProviderOptions` does, so an invalid namespace throws.
21
+ *
22
+ * @returns the resolved options (`undefined` when no namespace is present)
23
+ * and the namespaces that contributed to them, in resolution order.
24
+ */
25
+ export async function resolveProviderOptions<OPTIONS>({
26
+ providerOptions,
27
+ namespaces,
28
+ resolution,
29
+ schema,
30
+ }: {
31
+ providerOptions: Record<string, unknown> | undefined;
32
+ namespaces: readonly string[];
33
+ resolution: ProviderOptionsResolution;
34
+ schema: FlexibleSchema<OPTIONS>;
35
+ }): Promise<{
36
+ options: OPTIONS | undefined;
37
+ namespaces: string[];
38
+ }> {
39
+ const parsed: OPTIONS[] = [];
40
+ const matched: string[] = [];
41
+
42
+ for (const namespace of new Set(namespaces)) {
43
+ const options = await parseProviderOptions({
44
+ provider: namespace,
45
+ providerOptions,
46
+ schema,
47
+ });
48
+
49
+ if (options == null) {
50
+ continue;
51
+ }
52
+
53
+ if (resolution === 'first') {
54
+ return { options, namespaces: [namespace] };
55
+ }
56
+
57
+ parsed.push(options);
58
+ matched.push(namespace);
59
+ }
60
+
61
+ return {
62
+ options:
63
+ parsed.length === 0
64
+ ? undefined
65
+ : (Object.assign({}, ...parsed) as OPTIONS),
66
+ namespaces: matched,
67
+ };
68
+ }