@ai-sdk/provider-utils 5.0.0-canary.40 → 5.0.0-canary.41

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,12 @@
1
1
  # @ai-sdk/provider-utils
2
2
 
3
+ ## 5.0.0-canary.41
4
+
5
+ ### Patch Changes
6
+
7
+ - 28dfa06: fix: support tools with optional context
8
+ - e93fa91: rename Sandbox.executeCommand to Sandbox.runCommand
9
+
3
10
  ## 5.0.0-canary.40
4
11
 
5
12
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -1293,9 +1293,9 @@ type Experimental_Sandbox = {
1293
1293
  */
1294
1294
  readonly description: string;
1295
1295
  /**
1296
- * Execute a command in the sandbox.
1296
+ * Run a command in the sandbox.
1297
1297
  */
1298
- readonly executeCommand: (options: {
1298
+ readonly runCommand: (options: {
1299
1299
  /**
1300
1300
  * Command to execute in the sandbox.
1301
1301
  */
@@ -1971,10 +1971,25 @@ type ExecutableTool<TOOL extends Tool = Tool> = TOOL & {
1971
1971
  */
1972
1972
  declare function isExecutableTool<TOOL extends Tool>(tool: TOOL | undefined): tool is ExecutableTool<TOOL>;
1973
1973
 
1974
+ /**
1975
+ * Detects the `any` type so untyped tools can be treated as having no explicit
1976
+ * context type.
1977
+ */
1978
+ type IsAny<T> = 0 extends 1 & T ? true : false;
1979
+ /**
1980
+ * Detects exact empty object contexts, including `{}` combined with
1981
+ * `undefined`, which do not provide tool-specific context properties.
1982
+ */
1983
+ type IsEmptyObject<T> = keyof NonNullable<T> extends never ? true : false;
1984
+ /**
1985
+ * Detects context types that come from omitted or broad context declarations
1986
+ * rather than a concrete tool context schema.
1987
+ */
1988
+ type IsUntypedContext<CONTEXT> = IsAny<CONTEXT> extends true ? true : unknown extends CONTEXT ? true : IsEmptyObject<CONTEXT> extends true ? true : string extends keyof CONTEXT ? CONTEXT extends Context ? true : false : false;
1974
1989
  /**
1975
1990
  * Infer the context type of a tool.
1976
1991
  */
1977
- type InferToolContext<TOOL extends Tool> = TOOL extends Tool<any, any, infer CONTEXT> ? HasRequiredKey<CONTEXT> extends true ? CONTEXT : never : never;
1992
+ type InferToolContext<TOOL extends Tool> = TOOL extends Tool<any, any, infer CONTEXT> ? IsUntypedContext<CONTEXT> extends true ? never : CONTEXT : never;
1978
1993
 
1979
1994
  /**
1980
1995
  * Infer the input type of a tool.
@@ -2017,16 +2032,36 @@ declare function executeTool<TOOL extends Tool>({ tool, input, options, }: {
2017
2032
  */
2018
2033
  type ToolSet = Record<string, (Tool<never, never, any> | Tool<any, any, any> | Tool<any, never, any> | Tool<never, any, any>) & Pick<Tool<any, any, any>, 'execute' | 'onInputAvailable' | 'onInputStart' | 'onInputDelta' | 'needsApproval'>>;
2019
2034
 
2035
+ /**
2036
+ * Builds the required portion of the tool context map for tools whose context
2037
+ * type does not include `undefined`.
2038
+ */
2039
+ type RequiredToolSetContext<TOOLS extends ToolSet> = {
2040
+ [K in keyof TOOLS as InferToolContext<NoInfer<TOOLS[K]>> extends never ? never : undefined extends InferToolContext<NoInfer<TOOLS[K]>> ? never : K]: InferToolContext<NoInfer<TOOLS[K]>>;
2041
+ };
2042
+ /**
2043
+ * Builds the optional portion of the tool context map for tools whose context
2044
+ * object itself may be `undefined`.
2045
+ */
2046
+ type OptionalToolSetContext<TOOLS extends ToolSet> = {
2047
+ [K in keyof TOOLS as InferToolContext<NoInfer<TOOLS[K]>> extends never ? never : undefined extends InferToolContext<NoInfer<TOOLS[K]>> ? K : never]?: InferToolContext<NoInfer<TOOLS[K]>>;
2048
+ };
2049
+ /**
2050
+ * Flattens intersected mapped types so type equality assertions and editor
2051
+ * hovers show the resulting object shape.
2052
+ */
2053
+ type Normalize<OBJECT> = {
2054
+ [KEY in keyof OBJECT]: OBJECT[KEY];
2055
+ };
2020
2056
  /**
2021
2057
  * Infer the context type for a tool set.
2022
2058
  *
2023
- * The inferred type maps each tool name to its required context type.
2059
+ * The inferred type maps each contextual tool name to its context type.
2024
2060
  *
2025
- * Tools without required context properties are omitted from the result.
2061
+ * Tools without concrete context are omitted. Tool contexts that include
2062
+ * `undefined` are represented as optional properties.
2026
2063
  */
2027
- type InferToolSetContext<TOOLS extends ToolSet> = {
2028
- [K in keyof TOOLS as InferToolContext<NoInfer<TOOLS[K]>> extends never ? never : K]: InferToolContext<NoInfer<TOOLS[K]>>;
2029
- };
2064
+ type InferToolSetContext<TOOLS extends ToolSet> = Normalize<RequiredToolSetContext<TOOLS> & OptionalToolSetContext<TOOLS>>;
2030
2065
 
2031
2066
  /**
2032
2067
  * Typed tool call that is returned by generateText and streamText.
package/dist/index.js CHANGED
@@ -862,7 +862,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
862
862
  }
863
863
 
864
864
  // src/version.ts
865
- var VERSION = true ? "5.0.0-canary.40" : "0.0.0-test";
865
+ var VERSION = true ? "5.0.0-canary.41" : "0.0.0-test";
866
866
 
867
867
  // src/get-from-api.ts
868
868
  var getOriginalFetch = () => globalThis.fetch;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/provider-utils",
3
- "version": "5.0.0-canary.40",
3
+ "version": "5.0.0-canary.41",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -1,12 +1,41 @@
1
- import type { HasRequiredKey } from '../has-required-key';
1
+ import type { Context } from './context';
2
2
  import type { Tool } from './tool';
3
3
 
4
+ /**
5
+ * Detects the `any` type so untyped tools can be treated as having no explicit
6
+ * context type.
7
+ */
8
+ type IsAny<T> = 0 extends 1 & T ? true : false;
9
+
10
+ /**
11
+ * Detects exact empty object contexts, including `{}` combined with
12
+ * `undefined`, which do not provide tool-specific context properties.
13
+ */
14
+ type IsEmptyObject<T> = keyof NonNullable<T> extends never ? true : false;
15
+
16
+ /**
17
+ * Detects context types that come from omitted or broad context declarations
18
+ * rather than a concrete tool context schema.
19
+ */
20
+ type IsUntypedContext<CONTEXT> =
21
+ IsAny<CONTEXT> extends true
22
+ ? true
23
+ : unknown extends CONTEXT
24
+ ? true
25
+ : IsEmptyObject<CONTEXT> extends true
26
+ ? true
27
+ : string extends keyof CONTEXT
28
+ ? CONTEXT extends Context
29
+ ? true
30
+ : false
31
+ : false;
32
+
4
33
  /**
5
34
  * Infer the context type of a tool.
6
35
  */
7
36
  export type InferToolContext<TOOL extends Tool> =
8
37
  TOOL extends Tool<any, any, infer CONTEXT>
9
- ? HasRequiredKey<CONTEXT> extends true
10
- ? CONTEXT
11
- : never
38
+ ? IsUntypedContext<CONTEXT> extends true
39
+ ? never
40
+ : CONTEXT
12
41
  : never;
@@ -2,14 +2,43 @@ import type { InferToolContext } from './infer-tool-context';
2
2
  import type { ToolSet } from './tool-set';
3
3
 
4
4
  /**
5
- * Infer the context type for a tool set.
6
- *
7
- * The inferred type maps each tool name to its required context type.
8
- *
9
- * Tools without required context properties are omitted from the result.
5
+ * Builds the required portion of the tool context map for tools whose context
6
+ * type does not include `undefined`.
7
+ */
8
+ type RequiredToolSetContext<TOOLS extends ToolSet> = {
9
+ [K in keyof TOOLS as InferToolContext<NoInfer<TOOLS[K]>> extends never
10
+ ? never
11
+ : undefined extends InferToolContext<NoInfer<TOOLS[K]>>
12
+ ? never
13
+ : K]: InferToolContext<NoInfer<TOOLS[K]>>;
14
+ };
15
+
16
+ /**
17
+ * Builds the optional portion of the tool context map for tools whose context
18
+ * object itself may be `undefined`.
10
19
  */
11
- export type InferToolSetContext<TOOLS extends ToolSet> = {
20
+ type OptionalToolSetContext<TOOLS extends ToolSet> = {
12
21
  [K in keyof TOOLS as InferToolContext<NoInfer<TOOLS[K]>> extends never
13
22
  ? never
14
- : K]: InferToolContext<NoInfer<TOOLS[K]>>;
23
+ : undefined extends InferToolContext<NoInfer<TOOLS[K]>>
24
+ ? K
25
+ : never]?: InferToolContext<NoInfer<TOOLS[K]>>;
15
26
  };
27
+
28
+ /**
29
+ * Flattens intersected mapped types so type equality assertions and editor
30
+ * hovers show the resulting object shape.
31
+ */
32
+ type Normalize<OBJECT> = { [KEY in keyof OBJECT]: OBJECT[KEY] };
33
+
34
+ /**
35
+ * Infer the context type for a tool set.
36
+ *
37
+ * The inferred type maps each contextual tool name to its context type.
38
+ *
39
+ * Tools without concrete context are omitted. Tool contexts that include
40
+ * `undefined` are represented as optional properties.
41
+ */
42
+ export type InferToolSetContext<TOOLS extends ToolSet> = Normalize<
43
+ RequiredToolSetContext<TOOLS> & OptionalToolSetContext<TOOLS>
44
+ >;
@@ -10,9 +10,9 @@ export type Experimental_Sandbox = {
10
10
  readonly description: string;
11
11
 
12
12
  /**
13
- * Execute a command in the sandbox.
13
+ * Run a command in the sandbox.
14
14
  */
15
- readonly executeCommand: (options: {
15
+ readonly runCommand: (options: {
16
16
  /**
17
17
  * Command to execute in the sandbox.
18
18
  */