@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/provider-utils",
3
- "version": "5.0.0-beta.16",
3
+ "version": "5.0.0-beta.18",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Checks if an object has required keys.
3
+ * @param OBJECT - The object to check.
4
+ * @returns True if the object has required keys, false otherwise.
5
+ */
6
+ export type HasRequiredKey<OBJECT> = {} extends OBJECT ? false : true;
package/src/index.ts CHANGED
@@ -20,6 +20,7 @@ export { createIdGenerator, generateId, type IdGenerator } from './generate-id';
20
20
  export * from './get-error-message';
21
21
  export * from './get-from-api';
22
22
  export { getRuntimeEnvironmentUserAgent } from './get-runtime-environment-user-agent';
23
+ export type { HasRequiredKey } from './has-required-key';
23
24
  export { injectJsonInstructionIntoMessages } from './inject-json-instruction';
24
25
  export * from './is-abort-error';
25
26
  export { isNonNullable } from './is-non-nullable';
@@ -64,7 +64,7 @@ export const createJsonErrorResponseHandler =
64
64
  isRetryable: isRetryable?.(response, parsedError),
65
65
  }),
66
66
  };
67
- } catch (parseError) {
67
+ } catch {
68
68
  return {
69
69
  responseHeaders,
70
70
  value: new APICallError({
@@ -83,7 +83,7 @@ export function secureJsonParse(text: string) {
83
83
  try {
84
84
  // Performance optimization, see https://github.com/fastify/secure-json-parse/pull/90
85
85
  Error.stackTraceLimit = 0;
86
- } catch (e) {
86
+ } catch {
87
87
  // Fallback in case Error is immutable (v8 readonly)
88
88
  return _parse(text);
89
89
  }
@@ -22,7 +22,7 @@ export function parseDateDef(
22
22
 
23
23
  if (Array.isArray(strategy)) {
24
24
  return {
25
- anyOf: strategy.map((item, i) => parseDateDef(def, refs, item)),
25
+ anyOf: strategy.map(item => parseDateDef(def, refs, item)),
26
26
  };
27
27
  }
28
28
 
@@ -42,7 +42,7 @@ export function parseIntersectionDef(
42
42
  'additionalProperties' in schema &&
43
43
  schema.additionalProperties === false
44
44
  ) {
45
- const { additionalProperties, ...rest } = schema;
45
+ const { additionalProperties: _additionalProperties, ...rest } = schema;
46
46
  nestedSchema = rest;
47
47
  }
48
48
  mergedAllOf.push(nestedSchema);
@@ -38,7 +38,7 @@ export function parseRecordDef(
38
38
  def.keyType?._def.typeName === ZodFirstPartyTypeKind.ZodString &&
39
39
  def.keyType._def.checks?.length
40
40
  ) {
41
- const { type, ...keyType } = parseStringDef(def.keyType._def, refs);
41
+ const { type: _type, ...keyType } = parseStringDef(def.keyType._def, refs);
42
42
 
43
43
  return {
44
44
  ...schema,
@@ -56,7 +56,7 @@ export function parseRecordDef(
56
56
  def.keyType._def.type._def.typeName === ZodFirstPartyTypeKind.ZodString &&
57
57
  def.keyType._def.type._def.checks?.length
58
58
  ) {
59
- const { type, ...keyType } = parseBrandedDef(
59
+ const { type: _type, ...keyType } = parseBrandedDef(
60
60
  def.keyType._def,
61
61
  refs,
62
62
  ) as JsonSchema7StringType;
@@ -1,7 +1,12 @@
1
+ import { HasRequiredKey } from '../has-required-key';
1
2
  import type { Tool } from './tool';
2
3
 
3
4
  /**
4
5
  * Infer the context type of a tool.
5
6
  */
6
- export type InferToolContext<TOOL extends Tool<any, any, any>> =
7
- TOOL extends Tool<any, any, infer CONTEXT> ? CONTEXT : never;
7
+ export type InferToolContext<TOOL extends Tool> =
8
+ TOOL extends Tool<any, any, infer CONTEXT>
9
+ ? HasRequiredKey<CONTEXT> extends true
10
+ ? CONTEXT
11
+ : never
12
+ : never;
package/src/types/tool.ts CHANGED
@@ -8,7 +8,9 @@ import { Context } from './context';
8
8
  /**
9
9
  * Additional options that are sent into each tool execution.
10
10
  */
11
- export interface ToolExecutionOptions<CONTEXT extends Context> {
11
+ export interface ToolExecutionOptions<
12
+ CONTEXT extends Context | unknown | never,
13
+ > {
12
14
  /**
13
15
  * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
14
16
  */
@@ -41,7 +43,10 @@ export interface ToolExecutionOptions<CONTEXT extends Context> {
41
43
  /**
42
44
  * Function that is called to determine if the tool needs approval before it can be executed.
43
45
  */
44
- export type ToolNeedsApprovalFunction<INPUT, CONTEXT extends Context> = (
46
+ export type ToolNeedsApprovalFunction<
47
+ INPUT,
48
+ CONTEXT extends Context | unknown | never,
49
+ > = (
45
50
  input: INPUT,
46
51
  options: {
47
52
  /**
@@ -72,7 +77,11 @@ export type ToolNeedsApprovalFunction<INPUT, CONTEXT extends Context> = (
72
77
  /**
73
78
  * Function that executes the tool and returns either a single result or a stream of results.
74
79
  */
75
- export type ToolExecuteFunction<INPUT, OUTPUT, CONTEXT extends Context> = (
80
+ export type ToolExecuteFunction<
81
+ INPUT,
82
+ OUTPUT,
83
+ CONTEXT extends Context | unknown | never,
84
+ > = (
76
85
  input: INPUT,
77
86
  options: ToolExecutionOptions<CONTEXT>,
78
87
  ) => AsyncIterable<OUTPUT> | PromiseLike<OUTPUT> | OUTPUT;
@@ -91,7 +100,7 @@ type NeverOptional<N, T> = 0 extends 1 & N
91
100
  type ToolOutputProperties<
92
101
  INPUT,
93
102
  OUTPUT,
94
- CONTEXT extends Context,
103
+ CONTEXT extends Context | unknown | never,
95
104
  > = NeverOptional<
96
105
  OUTPUT,
97
106
  | {
@@ -122,7 +131,7 @@ type ToolOutputProperties<
122
131
  export type Tool<
123
132
  INPUT extends JSONValue | unknown | never = any,
124
133
  OUTPUT extends JSONValue | unknown | never = any,
125
- CONTEXT extends Context = Context,
134
+ CONTEXT extends Context | unknown | never = any,
126
135
  > = {
127
136
  /**
128
137
  * An optional description of what the tool does.