@ai-sdk/provider-utils 5.0.0-beta.16 → 5.0.0-beta.18

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @ai-sdk/provider-utils
2
2
 
3
+ ## 5.0.0-beta.18
4
+
5
+ ### Patch Changes
6
+
7
+ - 90e2d8a: chore: fix unused vars not being flagged by our lint tooling
8
+
9
+ ## 5.0.0-beta.17
10
+
11
+ ### Patch Changes
12
+
13
+ - 3ae1786: fix: better context type inference
14
+
3
15
  ## 5.0.0-beta.16
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -384,6 +384,13 @@ declare const getFromApi: <T>({ url, headers, successfulResponseHandler, failedR
384
384
 
385
385
  declare function getRuntimeEnvironmentUserAgent(globalThisAny?: any): string;
386
386
 
387
+ /**
388
+ * Checks if an object has required keys.
389
+ * @param OBJECT - The object to check.
390
+ * @returns True if the object has required keys, false otherwise.
391
+ */
392
+ type HasRequiredKey<OBJECT> = {} extends OBJECT ? false : true;
393
+
387
394
  declare function injectJsonInstructionIntoMessages({ messages, schema, schemaPrefix, schemaSuffix, }: {
388
395
  messages: LanguageModelV4Prompt;
389
396
  schema?: JSONSchema7;
@@ -1097,7 +1104,7 @@ type Context = Record<string, unknown>;
1097
1104
  /**
1098
1105
  * Additional options that are sent into each tool execution.
1099
1106
  */
1100
- interface ToolExecutionOptions<CONTEXT extends Context> {
1107
+ interface ToolExecutionOptions<CONTEXT extends Context | unknown | never> {
1101
1108
  /**
1102
1109
  * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
1103
1110
  */
@@ -1126,7 +1133,7 @@ interface ToolExecutionOptions<CONTEXT extends Context> {
1126
1133
  /**
1127
1134
  * Function that is called to determine if the tool needs approval before it can be executed.
1128
1135
  */
1129
- type ToolNeedsApprovalFunction<INPUT, CONTEXT extends Context> = (input: INPUT, options: {
1136
+ type ToolNeedsApprovalFunction<INPUT, CONTEXT extends Context | unknown | never> = (input: INPUT, options: {
1130
1137
  /**
1131
1138
  * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
1132
1139
  */
@@ -1151,12 +1158,12 @@ type ToolNeedsApprovalFunction<INPUT, CONTEXT extends Context> = (input: INPUT,
1151
1158
  /**
1152
1159
  * Function that executes the tool and returns either a single result or a stream of results.
1153
1160
  */
1154
- type ToolExecuteFunction<INPUT, OUTPUT, CONTEXT extends Context> = (input: INPUT, options: ToolExecutionOptions<CONTEXT>) => AsyncIterable<OUTPUT> | PromiseLike<OUTPUT> | OUTPUT;
1161
+ type ToolExecuteFunction<INPUT, OUTPUT, CONTEXT extends Context | unknown | never> = (input: INPUT, options: ToolExecutionOptions<CONTEXT>) => AsyncIterable<OUTPUT> | PromiseLike<OUTPUT> | OUTPUT;
1155
1162
  type NeverOptional<N, T> = 0 extends 1 & N ? Partial<T> : [N] extends [never] ? Partial<Record<keyof T, undefined>> : T;
1156
1163
  /**
1157
1164
  * Helper type to determine the output properties of a tool.
1158
1165
  */
1159
- type ToolOutputProperties<INPUT, OUTPUT, CONTEXT extends Context> = NeverOptional<OUTPUT, {
1166
+ type ToolOutputProperties<INPUT, OUTPUT, CONTEXT extends Context | unknown | never> = NeverOptional<OUTPUT, {
1160
1167
  /**
1161
1168
  * An async function that is called with the arguments from the tool call and produces a result.
1162
1169
  * If not provided, the tool will not be executed automatically.
@@ -1176,7 +1183,7 @@ type ToolOutputProperties<INPUT, OUTPUT, CONTEXT extends Context> = NeverOptiona
1176
1183
  *
1177
1184
  * The tool can also contain an optional execute function for the actual execution function of the tool.
1178
1185
  */
1179
- type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any, CONTEXT extends Context = Context> = {
1186
+ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any, CONTEXT extends Context | unknown | never = any> = {
1180
1187
  /**
1181
1188
  * An optional description of what the tool does.
1182
1189
  * Will be used by the language model to decide whether to use the tool.
@@ -1524,7 +1531,7 @@ declare function executeTool<INPUT, OUTPUT, CONTEXT extends Context>({ execute,
1524
1531
  /**
1525
1532
  * Infer the context type of a tool.
1526
1533
  */
1527
- type InferToolContext<TOOL extends Tool<any, any, any>> = TOOL extends Tool<any, any, infer CONTEXT> ? CONTEXT : never;
1534
+ type InferToolContext<TOOL extends Tool> = TOOL extends Tool<any, any, infer CONTEXT> ? HasRequiredKey<CONTEXT> extends true ? CONTEXT : never : never;
1528
1535
 
1529
1536
  /**
1530
1537
  * Infer the input type of a tool.
@@ -1626,4 +1633,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
1626
1633
  dynamic?: boolean;
1627
1634
  }
1628
1635
 
1629
- export { type AssistantContent, type AssistantModelMessage, type Context, type CustomPart, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolContext, type InferToolInput, type InferToolOutput, type InferToolSetContext, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderReference, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningFilePart, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type ToolSet, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isCustomReasoning, isNonNullable, isParsableJson, isProviderReference, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mapReasoningToProviderBudget, mapReasoningToProviderEffort, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, resolveProviderReference, safeParseJSON, safeValidateTypes, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
1636
+ export { type AssistantContent, type AssistantModelMessage, type Context, type CustomPart, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type FetchFunction, type FilePart, type FlexibleSchema, type HasRequiredKey, type IdGenerator, type ImagePart, type InferSchema, type InferToolContext, type InferToolInput, type InferToolOutput, type InferToolSetContext, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderReference, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningFilePart, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type ToolSet, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isCustomReasoning, isNonNullable, isParsableJson, isProviderReference, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mapReasoningToProviderBudget, mapReasoningToProviderEffort, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, resolveProviderReference, safeParseJSON, safeValidateTypes, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
package/dist/index.d.ts CHANGED
@@ -384,6 +384,13 @@ declare const getFromApi: <T>({ url, headers, successfulResponseHandler, failedR
384
384
 
385
385
  declare function getRuntimeEnvironmentUserAgent(globalThisAny?: any): string;
386
386
 
387
+ /**
388
+ * Checks if an object has required keys.
389
+ * @param OBJECT - The object to check.
390
+ * @returns True if the object has required keys, false otherwise.
391
+ */
392
+ type HasRequiredKey<OBJECT> = {} extends OBJECT ? false : true;
393
+
387
394
  declare function injectJsonInstructionIntoMessages({ messages, schema, schemaPrefix, schemaSuffix, }: {
388
395
  messages: LanguageModelV4Prompt;
389
396
  schema?: JSONSchema7;
@@ -1097,7 +1104,7 @@ type Context = Record<string, unknown>;
1097
1104
  /**
1098
1105
  * Additional options that are sent into each tool execution.
1099
1106
  */
1100
- interface ToolExecutionOptions<CONTEXT extends Context> {
1107
+ interface ToolExecutionOptions<CONTEXT extends Context | unknown | never> {
1101
1108
  /**
1102
1109
  * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
1103
1110
  */
@@ -1126,7 +1133,7 @@ interface ToolExecutionOptions<CONTEXT extends Context> {
1126
1133
  /**
1127
1134
  * Function that is called to determine if the tool needs approval before it can be executed.
1128
1135
  */
1129
- type ToolNeedsApprovalFunction<INPUT, CONTEXT extends Context> = (input: INPUT, options: {
1136
+ type ToolNeedsApprovalFunction<INPUT, CONTEXT extends Context | unknown | never> = (input: INPUT, options: {
1130
1137
  /**
1131
1138
  * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
1132
1139
  */
@@ -1151,12 +1158,12 @@ type ToolNeedsApprovalFunction<INPUT, CONTEXT extends Context> = (input: INPUT,
1151
1158
  /**
1152
1159
  * Function that executes the tool and returns either a single result or a stream of results.
1153
1160
  */
1154
- type ToolExecuteFunction<INPUT, OUTPUT, CONTEXT extends Context> = (input: INPUT, options: ToolExecutionOptions<CONTEXT>) => AsyncIterable<OUTPUT> | PromiseLike<OUTPUT> | OUTPUT;
1161
+ type ToolExecuteFunction<INPUT, OUTPUT, CONTEXT extends Context | unknown | never> = (input: INPUT, options: ToolExecutionOptions<CONTEXT>) => AsyncIterable<OUTPUT> | PromiseLike<OUTPUT> | OUTPUT;
1155
1162
  type NeverOptional<N, T> = 0 extends 1 & N ? Partial<T> : [N] extends [never] ? Partial<Record<keyof T, undefined>> : T;
1156
1163
  /**
1157
1164
  * Helper type to determine the output properties of a tool.
1158
1165
  */
1159
- type ToolOutputProperties<INPUT, OUTPUT, CONTEXT extends Context> = NeverOptional<OUTPUT, {
1166
+ type ToolOutputProperties<INPUT, OUTPUT, CONTEXT extends Context | unknown | never> = NeverOptional<OUTPUT, {
1160
1167
  /**
1161
1168
  * An async function that is called with the arguments from the tool call and produces a result.
1162
1169
  * If not provided, the tool will not be executed automatically.
@@ -1176,7 +1183,7 @@ type ToolOutputProperties<INPUT, OUTPUT, CONTEXT extends Context> = NeverOptiona
1176
1183
  *
1177
1184
  * The tool can also contain an optional execute function for the actual execution function of the tool.
1178
1185
  */
1179
- type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any, CONTEXT extends Context = Context> = {
1186
+ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any, CONTEXT extends Context | unknown | never = any> = {
1180
1187
  /**
1181
1188
  * An optional description of what the tool does.
1182
1189
  * Will be used by the language model to decide whether to use the tool.
@@ -1524,7 +1531,7 @@ declare function executeTool<INPUT, OUTPUT, CONTEXT extends Context>({ execute,
1524
1531
  /**
1525
1532
  * Infer the context type of a tool.
1526
1533
  */
1527
- type InferToolContext<TOOL extends Tool<any, any, any>> = TOOL extends Tool<any, any, infer CONTEXT> ? CONTEXT : never;
1534
+ type InferToolContext<TOOL extends Tool> = TOOL extends Tool<any, any, infer CONTEXT> ? HasRequiredKey<CONTEXT> extends true ? CONTEXT : never : never;
1528
1535
 
1529
1536
  /**
1530
1537
  * Infer the input type of a tool.
@@ -1626,4 +1633,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
1626
1633
  dynamic?: boolean;
1627
1634
  }
1628
1635
 
1629
- export { type AssistantContent, type AssistantModelMessage, type Context, type CustomPart, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolContext, type InferToolInput, type InferToolOutput, type InferToolSetContext, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderReference, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningFilePart, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type ToolSet, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isCustomReasoning, isNonNullable, isParsableJson, isProviderReference, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mapReasoningToProviderBudget, mapReasoningToProviderEffort, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, resolveProviderReference, safeParseJSON, safeValidateTypes, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
1636
+ export { type AssistantContent, type AssistantModelMessage, type Context, type CustomPart, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type FetchFunction, type FilePart, type FlexibleSchema, type HasRequiredKey, type IdGenerator, type ImagePart, type InferSchema, type InferToolContext, type InferToolInput, type InferToolOutput, type InferToolSetContext, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderReference, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningFilePart, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type ToolSet, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isCustomReasoning, isNonNullable, isParsableJson, isProviderReference, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mapReasoningToProviderBudget, mapReasoningToProviderEffort, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, resolveProviderReference, safeParseJSON, safeValidateTypes, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
package/dist/index.js CHANGED
@@ -678,7 +678,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
678
678
  }
679
679
 
680
680
  // src/version.ts
681
- var VERSION = true ? "5.0.0-beta.16" : "0.0.0-test";
681
+ var VERSION = true ? "5.0.0-beta.18" : "0.0.0-test";
682
682
 
683
683
  // src/get-from-api.ts
684
684
  var getOriginalFetch = () => globalThis.fetch;
@@ -1184,7 +1184,7 @@ function parseDateDef(def, refs, overrideDateStrategy) {
1184
1184
  const strategy = overrideDateStrategy != null ? overrideDateStrategy : refs.dateStrategy;
1185
1185
  if (Array.isArray(strategy)) {
1186
1186
  return {
1187
- anyOf: strategy.map((item, i) => parseDateDef(def, refs, item))
1187
+ anyOf: strategy.map((item) => parseDateDef(def, refs, item))
1188
1188
  };
1189
1189
  }
1190
1190
  switch (strategy) {
@@ -1265,7 +1265,7 @@ function parseIntersectionDef(def, refs) {
1265
1265
  } else {
1266
1266
  let nestedSchema = schema;
1267
1267
  if ("additionalProperties" in schema && schema.additionalProperties === false) {
1268
- const { additionalProperties, ...rest } = schema;
1268
+ const { additionalProperties: _additionalProperties, ...rest } = schema;
1269
1269
  nestedSchema = rest;
1270
1270
  }
1271
1271
  mergedAllOf.push(nestedSchema);
@@ -1638,7 +1638,7 @@ function parseRecordDef(def, refs) {
1638
1638
  })) != null ? _a2 : refs.allowedAdditionalProperties
1639
1639
  };
1640
1640
  if (((_b2 = def.keyType) == null ? void 0 : _b2._def.typeName) === import_v32.ZodFirstPartyTypeKind.ZodString && ((_c = def.keyType._def.checks) == null ? void 0 : _c.length)) {
1641
- const { type, ...keyType } = parseStringDef(def.keyType._def, refs);
1641
+ const { type: _type, ...keyType } = parseStringDef(def.keyType._def, refs);
1642
1642
  return {
1643
1643
  ...schema,
1644
1644
  propertyNames: keyType
@@ -1651,7 +1651,7 @@ function parseRecordDef(def, refs) {
1651
1651
  }
1652
1652
  };
1653
1653
  } else if (((_e = def.keyType) == null ? void 0 : _e._def.typeName) === import_v32.ZodFirstPartyTypeKind.ZodBranded && def.keyType._def.type._def.typeName === import_v32.ZodFirstPartyTypeKind.ZodString && ((_f = def.keyType._def.type._def.checks) == null ? void 0 : _f.length)) {
1654
- const { type, ...keyType } = parseBrandedDef(
1654
+ const { type: _type, ...keyType } = parseBrandedDef(
1655
1655
  def.keyType._def,
1656
1656
  refs
1657
1657
  );
@@ -2726,7 +2726,7 @@ var createJsonErrorResponseHandler = ({
2726
2726
  isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
2727
2727
  })
2728
2728
  };
2729
- } catch (parseError) {
2729
+ } catch (e) {
2730
2730
  return {
2731
2731
  responseHeaders,
2732
2732
  value: new import_provider13.APICallError({