@arcadeai/arcadejs 1.4.2 → 1.5.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.
@@ -1,5 +1,6 @@
1
+ import type { Arcade } from "../../index.js";
1
2
  import { AuthorizationResponse } from "../../resources/shared.js";
2
- import { ExecuteToolResponse } from "../../resources/tools/tools.js";
3
+ import { ExecuteToolResponse, ToolDefinition } from "../../resources/tools/tools.js";
3
4
  import { z } from 'zod';
4
5
  /**
5
6
  * Response returned when a tool requires authorization before execution
@@ -10,25 +11,65 @@ export interface ToolAuthorizationResponse {
10
11
  url: string;
11
12
  message: string;
12
13
  }
13
- /**
14
- * Represents a tool with Zod schema validation and execution capabilities.
15
- */
16
- export interface ZodTool {
17
- name: string;
18
- description: string | undefined;
19
- parameters: z.ZodType;
20
- output: z.ZodType | undefined;
21
- execute: <T extends unknown>(input: T) => Promise<ExecuteToolResponse>;
22
- executeOrAuthorize: <T extends unknown>(input: T) => Promise<ExecuteToolResponse | ToolAuthorizationResponse>;
23
- }
24
14
  /**
25
15
  * Schema definition for an Arcade tool, containing the tool's name,
26
16
  * description, and Zod-validated parameter structure.
27
17
  */
28
18
  export interface ZodToolSchema {
19
+ /** Qualified name of the tool */
29
20
  name: string;
30
- description: string | undefined;
21
+ /** Optional, human-readable description */
22
+ description?: string | undefined;
23
+ /** Zod schema for validating tool parameters */
31
24
  parameters: z.ZodType;
25
+ /** Zod schema for validating tool output (if any) */
32
26
  output: z.ZodType | undefined;
33
27
  }
28
+ /**
29
+ * Generic type for any tool execution function
30
+ */
31
+ export type ToolExecuteFunction<TReturn> = (input: unknown) => Promise<TReturn>;
32
+ /**
33
+ * A complete tool implementation with Zod validation and execution capabilities
34
+ */
35
+ export interface ZodTool<TReturn = ExecuteToolResponse> extends ZodToolSchema {
36
+ execute: ToolExecuteFunction<TReturn>;
37
+ }
38
+ /**
39
+ * Arcade client
40
+ */
41
+ export type ArcadeClient = Omit<Arcade, '_options'>;
42
+ /**
43
+ * Input required to create a tool execution function
44
+ */
45
+ export interface ToolExecuteFunctionFactoryInput {
46
+ /** Original tool definition from Arcade */
47
+ toolDefinition: ToolDefinition;
48
+ /** Zod-validated tool schema */
49
+ zodToolSchema: ZodToolSchema;
50
+ /** Arcade client instance */
51
+ client: ArcadeClient;
52
+ /** User ID for tool execution/authorization */
53
+ userId: string;
54
+ }
55
+ /**
56
+ * Base input type for tool creation functions
57
+ */
58
+ export interface BaseToolCreationInput<TReturn = ExecuteToolResponse> {
59
+ client: ArcadeClient;
60
+ userId: string;
61
+ executeFactory?: (props: ToolExecuteFunctionFactoryInput) => ToolExecuteFunction<TReturn>;
62
+ }
63
+ /**
64
+ * Input required to create a single Zod tool
65
+ */
66
+ export interface CreateZodToolInput<TReturn = ExecuteToolResponse> extends BaseToolCreationInput<TReturn> {
67
+ tool: ToolDefinition;
68
+ }
69
+ /**
70
+ * Input required to create multiple Zod tools
71
+ */
72
+ export interface CreateMultipleZodToolsInput<TReturn = ExecuteToolResponse> extends BaseToolCreationInput<TReturn> {
73
+ tools: ToolDefinition[];
74
+ }
34
75
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/zod/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC,sBAAsB,EAAE,IAAI,CAAC;IAC7B,sBAAsB,EAAE,qBAAqB,CAAC;IAC9C,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,GAAG,SAAS,CA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ZodToolSchema } from "./types.js";
1
+ import { ExecuteToolResponse, ToolDefinition } from "../../resources/tools/tools.js";
2
+ import { ZodTool, ZodToolSchema, ToolExecuteFunctionFactoryInput, CreateZodToolInput, CreateMultipleZodToolsInput, ToolExecuteFunction, ToolAuthorizationResponse } from "./types.js";
3
3
  import { z } from 'zod';
4
- import type { Arcade } from "../../index.js";
5
- type ArcadeClient = Omit<Arcade, '_options'>;
6
4
  /**
7
5
  * Checks if an error indicates that authorization for the tool is required
8
6
  */
@@ -23,35 +21,30 @@ export declare function convertOutputToZodSchema(output: ToolDefinition.Output):
23
21
  */
24
22
  export declare function convertSingleToolToSchema(tool: ToolDefinition): ZodToolSchema;
25
23
  /**
26
- * Converts OpenAI formatted tools to Zod-validated tool schemas
24
+ * Converts tools to Zod-validated tool schemas
27
25
  * @param tools - Array of formatted tools
28
26
  * @returns Array of Zod-validated tool schemas
29
27
  */
30
28
  export declare function toZodSchema(tools: ToolDefinition[]): ZodToolSchema[];
29
+ /**
30
+ * Creates a tool execution function that will validate the input and execute the tool
31
+ */
32
+ export declare function executeZodTool({ zodToolSchema, client, userId, }: ToolExecuteFunctionFactoryInput): ToolExecuteFunction<ExecuteToolResponse>;
33
+ /**
34
+ * Creates a tool execution function that will execute the tool if it is authorized,
35
+ * otherwise it will return a ToolAuthorizationResponse
36
+ */
37
+ export declare function executeOrAuthorizeZodTool({ zodToolSchema, toolDefinition, client, userId, }: ToolExecuteFunctionFactoryInput): ToolExecuteFunction<ExecuteToolResponse | ToolAuthorizationResponse>;
31
38
  /**
32
39
  * Converts a single tool to a full tool with execution capabilities
33
- * @param tool - The tool to convert
34
- * @param client - Arcade client instance
35
- * @param userId - User ID to use for the tool execution
36
- * @returns Zod-validated tool with execution methods
37
- * @throws ToolConversionError if the tool is invalid
38
40
  */
39
- export declare function createZodTool({ tool, client, userId, }: {
40
- tool: ToolDefinition;
41
- client: ArcadeClient;
42
- userId: string;
43
- }): ZodTool;
41
+ export declare function createZodTool<TReturn = ExecuteToolResponse>(props: CreateZodToolInput<TReturn>): ZodTool<TReturn>;
44
42
  /**
45
- * Converts formatted tools to full tools with execution capabilities
46
- * @param tools - Array of formatted tools
47
- * @param client - Arcade client instance
48
- * @param userId - User ID to use for the tool execution
49
- * @returns Array of Zod-validated tools with execution methods
50
- */
51
- export declare function toZod({ tools, client, userId, }: {
52
- tools: ToolDefinition[];
53
- client: ArcadeClient;
54
- userId: string;
55
- }): ZodTool[];
56
- export {};
43
+ * Converts Arcade tools to zod tools with execution capabilities
44
+ */
45
+ export declare function toZod<TReturn = ExecuteToolResponse>({ tools, client, userId, executeFactory: providedExecuteFactory, }: CreateMultipleZodToolsInput<TReturn>): ZodTool<TReturn>[];
46
+ /**
47
+ * Creates a record of Zod-validated tools with configurable execute functions
48
+ */
49
+ export declare function toZodToolSet<TReturn = ExecuteToolResponse>({ tools, client, userId, executeFactory: providedExecuteFactory, }: CreateMultipleZodToolsInput<TReturn>): Record<string, ZodTool<TReturn>>;
57
50
  //# sourceMappingURL=zod.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"zod.d.ts","sourceRoot":"","sources":["../../src/lib/zod/zod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAClF,OAAO,EAA6B,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC5E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,KAAK,YAAY,GAAG,IAAI,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAE7C;;GAEG;AACH,wBAAgB,4BAA4B,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAMlE;AAED;;GAEG;AACH,wBAAgB,4BAA4B,CAAC,UAAU,EAAE,cAAc,CAAC,KAAK,GAAG,CAAC,CAAC,OAAO,CAmBxF;AAyCD;;GAEG;AACH,wBAAgB,wBAAwB,CAA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,CAAC,CAAC,OAAO,CAMjF;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,cAAc,GAAG,aAAa,CAU7E;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,cAAc,EAAE,GAAG,aAAa,EAAE,CAEpE;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,EAC7B,aAAa,EACb,MAAM,EACN,MAAM,GACP,EAAE,+BAA+B,GAAG,mBAAmB,CAAC,mBAAmB,CAAC,CAgB5E;AAED;;;GAGG;AACH,wBAAgB,yBAAyB,CAAC,EACxC,aAAa,EACb,cAAc,EACd,MAAM,EACN,MAAM,GACP,EAAE,+BAA+B,GAAG,mBAAmB,CAAC,mBAAmB,GAAG,yBAAyB,CAAC,CAwBxG;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,OAAO,GAAG,mBAAmB,EACzD,KAAK,EAAE,kBAAkB,CAAC,OAAO,CAAC,GACjC,OAAO,CAAC,OAAO,CAAC,CAgBlB;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,OAAO,GAAG,mBAAmB,EAAE,EACnD,KAAK,EACL,MAAM,EACN,MAAM,EACN,cAAc,EAAE,sBAAsB,GACvC,EAAE,2BAA2B,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,EAAE,CAK3D;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAG,mBAAmB,EAAE,EAC1D,KAAK,EACL,MAAM,EACN,MAAM,EACN,cAAc,EAAE,sBAAsB,GACvC,EAAE,2BAA2B,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAUzE"}
package/lib/zod/zod.js CHANGED
@@ -1,14 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toZod = exports.createZodTool = exports.toZodSchema = exports.convertSingleToolToSchema = exports.convertOutputToZodSchema = exports.convertParametersToZodSchema = exports.isAuthorizationRequiredError = void 0;
3
+ exports.toZodToolSet = exports.toZod = exports.createZodTool = exports.executeOrAuthorizeZodTool = exports.executeZodTool = exports.toZodSchema = exports.convertSingleToolToSchema = exports.convertOutputToZodSchema = exports.convertParametersToZodSchema = exports.isAuthorizationRequiredError = void 0;
4
4
  const zod_1 = require("zod");
5
5
  /**
6
6
  * Checks if an error indicates that authorization for the tool is required
7
7
  */
8
8
  function isAuthorizationRequiredError(error) {
9
+ const errorMessage = error?.message?.toLowerCase() || '';
9
10
  return (error?.name === 'PermissionDeniedError' ||
10
- error?.message?.includes('permission denied') ||
11
- error?.message?.includes('authorization required'));
11
+ errorMessage.includes('permission denied') ||
12
+ errorMessage.includes('authorization required'));
12
13
  }
13
14
  exports.isAuthorizationRequiredError = isAuthorizationRequiredError;
14
15
  /**
@@ -34,33 +35,25 @@ exports.convertParametersToZodSchema = convertParametersToZodSchema;
34
35
  * Converts a value schema to Zod type
35
36
  */
36
37
  function convertValueSchemaToZod(schema) {
37
- let baseType;
38
38
  switch (schema.val_type) {
39
39
  case 'string':
40
- baseType = schema.enum ? zod_1.z.enum(schema.enum) : zod_1.z.string();
41
- break;
40
+ return schema.enum ? zod_1.z.enum(schema.enum) : zod_1.z.string();
42
41
  case 'integer':
43
- baseType = zod_1.z.number().int();
44
- break;
42
+ return zod_1.z.number().int();
45
43
  case 'number':
46
- baseType = zod_1.z.number();
47
- break;
44
+ return zod_1.z.number();
48
45
  case 'boolean':
49
- baseType = zod_1.z.boolean();
50
- break;
46
+ return zod_1.z.boolean();
51
47
  case 'json':
52
- baseType = zod_1.z.any();
53
- break;
48
+ return zod_1.z.any();
54
49
  case 'array':
55
50
  if (!schema.inner_val_type) {
56
51
  throw new Error('Array type must have inner_val_type specified');
57
52
  }
58
- baseType = zod_1.z.array(convertValueSchemaToZod({ val_type: schema.inner_val_type }));
59
- break;
53
+ return zod_1.z.array(convertValueSchemaToZod({ val_type: schema.inner_val_type }));
60
54
  default:
61
- throw new Error(`Unsupported value type: ${schema.val_type}`);
55
+ return zod_1.z.any();
62
56
  }
63
- return baseType;
64
57
  }
65
58
  /**
66
59
  * Converts Arcade Tool Output to Zod schema
@@ -91,7 +84,7 @@ function convertSingleToolToSchema(tool) {
91
84
  }
92
85
  exports.convertSingleToolToSchema = convertSingleToolToSchema;
93
86
  /**
94
- * Converts OpenAI formatted tools to Zod-validated tool schemas
87
+ * Converts tools to Zod-validated tool schemas
95
88
  * @param tools - Array of formatted tools
96
89
  * @returns Array of Zod-validated tool schemas
97
90
  */
@@ -99,74 +92,91 @@ function toZodSchema(tools) {
99
92
  return tools.map(convertSingleToolToSchema);
100
93
  }
101
94
  exports.toZodSchema = toZodSchema;
95
+ /**
96
+ * Creates a tool execution function that will validate the input and execute the tool
97
+ */
98
+ function executeZodTool({ zodToolSchema, client, userId, }) {
99
+ const { parameters, name: toolName } = zodToolSchema;
100
+ return async (input) => {
101
+ const validationResult = parameters.safeParse(input);
102
+ if (!validationResult.success) {
103
+ throw new Error(`Invalid input: ${validationResult.error.message}`);
104
+ }
105
+ const result = await client.tools.execute({
106
+ tool_name: toolName,
107
+ input: validationResult.data,
108
+ user_id: userId,
109
+ });
110
+ return result;
111
+ };
112
+ }
113
+ exports.executeZodTool = executeZodTool;
114
+ /**
115
+ * Creates a tool execution function that will execute the tool if it is authorized,
116
+ * otherwise it will return a ToolAuthorizationResponse
117
+ */
118
+ function executeOrAuthorizeZodTool({ zodToolSchema, toolDefinition, client, userId, }) {
119
+ const { name: toolName } = zodToolSchema;
120
+ return async (input) => {
121
+ try {
122
+ const result = await executeZodTool({ zodToolSchema, toolDefinition, client, userId })(input);
123
+ return result;
124
+ }
125
+ catch (error) {
126
+ if (error instanceof Error && isAuthorizationRequiredError(error)) {
127
+ const response = await client.tools.authorize({
128
+ tool_name: toolName,
129
+ user_id: userId,
130
+ });
131
+ return {
132
+ authorization_required: true,
133
+ authorization_response: response,
134
+ url: response.url ?? '',
135
+ message: `This tool requires authorization. Please visit the following URL to authorize the tool: ${response.url}`,
136
+ };
137
+ }
138
+ throw error;
139
+ }
140
+ };
141
+ }
142
+ exports.executeOrAuthorizeZodTool = executeOrAuthorizeZodTool;
102
143
  /**
103
144
  * Converts a single tool to a full tool with execution capabilities
104
- * @param tool - The tool to convert
105
- * @param client - Arcade client instance
106
- * @param userId - User ID to use for the tool execution
107
- * @returns Zod-validated tool with execution methods
108
- * @throws ToolConversionError if the tool is invalid
109
145
  */
110
- function createZodTool({ tool, client, userId, }) {
146
+ function createZodTool(props) {
147
+ const { tool, client, userId, executeFactory: providedExecuteFactory } = props;
111
148
  const schema = convertSingleToolToSchema(tool);
112
149
  const { name, description, parameters, output } = schema;
150
+ const factoryToUse = providedExecuteFactory ??
151
+ executeZodTool;
113
152
  return {
114
153
  name,
115
154
  description,
116
155
  parameters,
117
156
  output,
118
- execute: async (input) => {
119
- const validationResult = parameters.safeParse(input);
120
- if (!validationResult.success) {
121
- throw new Error(`Invalid input: ${validationResult.error.message}`);
122
- }
123
- return client.tools.execute({
124
- tool_name: name,
125
- input: validationResult.data,
126
- user_id: userId,
127
- });
128
- },
129
- executeOrAuthorize: async (input) => {
130
- const validationResult = parameters.safeParse(input);
131
- if (!validationResult.success) {
132
- throw new Error(`Invalid input: ${validationResult.error.message}`);
133
- }
134
- try {
135
- return await client.tools.execute({
136
- tool_name: name,
137
- input: validationResult.data,
138
- user_id: userId,
139
- });
140
- }
141
- catch (error) {
142
- if (error instanceof Error && isAuthorizationRequiredError(error)) {
143
- const response = (await client.tools.authorize({
144
- tool_name: name,
145
- user_id: userId,
146
- }));
147
- return {
148
- authorization_required: true,
149
- authorization_response: response,
150
- url: response.url,
151
- message: 'This tool requires authorization. Please visit the following URL to authorize the tool: ' +
152
- response.url,
153
- };
154
- }
155
- throw error;
156
- }
157
- },
157
+ execute: factoryToUse({ toolDefinition: tool, zodToolSchema: schema, client, userId }),
158
158
  };
159
159
  }
160
160
  exports.createZodTool = createZodTool;
161
161
  /**
162
- * Converts formatted tools to full tools with execution capabilities
163
- * @param tools - Array of formatted tools
164
- * @param client - Arcade client instance
165
- * @param userId - User ID to use for the tool execution
166
- * @returns Array of Zod-validated tools with execution methods
162
+ * Converts Arcade tools to zod tools with execution capabilities
167
163
  */
168
- function toZod({ tools, client, userId, }) {
169
- return tools.map((tool) => createZodTool({ tool, client, userId }));
164
+ function toZod({ tools, client, userId, executeFactory: providedExecuteFactory, }) {
165
+ const factoryToUse = providedExecuteFactory ??
166
+ executeZodTool;
167
+ return tools.map((tool) => createZodTool({ tool, client, userId, executeFactory: factoryToUse }));
170
168
  }
171
169
  exports.toZod = toZod;
170
+ /**
171
+ * Creates a record of Zod-validated tools with configurable execute functions
172
+ */
173
+ function toZodToolSet({ tools, client, userId, executeFactory: providedExecuteFactory, }) {
174
+ const factoryToUse = providedExecuteFactory ??
175
+ executeZodTool;
176
+ return Object.fromEntries(tools.map((tool) => {
177
+ const zodTool = createZodTool({ tool, client, userId, executeFactory: factoryToUse });
178
+ return [zodTool.name, zodTool];
179
+ }));
180
+ }
181
+ exports.toZodToolSet = toZodToolSet;
172
182
  //# sourceMappingURL=zod.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"zod.js","sourceRoot":"","sources":["../../src/lib/zod/zod.ts"],"names":[],"mappings":";;;AAEA,6BAAwB;AAKxB;;GAEG;AACH,SAAgB,4BAA4B,CAAC,KAAY;IACvD,OAAO,CACL,KAAK,EAAE,IAAI,KAAK,uBAAuB;QACvC,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,mBAAmB,CAAC;QAC7C,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,wBAAwB,CAAC,CACnD,CAAC;AACJ,CAAC;AAND,oEAMC;AAED;;GAEG;AACH,SAAgB,4BAA4B,CAAC,UAAgC;IAC3E,IAAI,CAAC,UAAU,CAAC,UAAU,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE;QACnE,OAAO,OAAC,CAAC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,GAAG,MAAM,CAAC;IAEzD,MAAM,YAAY,GAChB,sBAAsB;QACrB,cAAsG,CAAC;IAE1G,OAAO;QACL,IAAI;QACJ,WAAW;QACX,UAAU;QACV,MAAM;QACN,OAAO,EAAE,YAAY,CAAC,EAAE,cAAc,EAAE,IAAI,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;KACvF,CAAC;AACJ,CAAC;AAlBD,sCAkBC;AAED;;GAEG;AACH,SAAgB,KAAK,CAAgC,EACnD,KAAK,EACL,MAAM,EACN,MAAM,EACN,cAAc,EAAE,sBAAsB,GACD;IACrC,MAAM,YAAY,GAChB,sBAAsB;QACrB,cAAsG,CAAC;IAC1G,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,aAAa,CAAU,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC;AAC7G,CAAC;AAVD,sBAUC;AAED;;GAEG;AACH,SAAgB,YAAY,CAAgC,EAC1D,KAAK,EACL,MAAM,EACN,MAAM,EACN,cAAc,EAAE,sBAAsB,GACD;IACrC,MAAM,YAAY,GAChB,sBAAsB;QACrB,cAAsG,CAAC;IAC1G,OAAO,MAAM,CAAC,WAAW,CACvB,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;QACjB,MAAM,OAAO,GAAG,aAAa,CAAU,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,YAAY,EAAE,CAAC,CAAC;QAC/F,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACjC,CAAC,CAAC,CACH,CAAC;AACJ,CAAC;AAfD,oCAeC"}
package/lib/zod/zod.mjs CHANGED
@@ -3,9 +3,10 @@ import { z } from 'zod';
3
3
  * Checks if an error indicates that authorization for the tool is required
4
4
  */
5
5
  export function isAuthorizationRequiredError(error) {
6
+ const errorMessage = error?.message?.toLowerCase() || '';
6
7
  return (error?.name === 'PermissionDeniedError' ||
7
- error?.message?.includes('permission denied') ||
8
- error?.message?.includes('authorization required'));
8
+ errorMessage.includes('permission denied') ||
9
+ errorMessage.includes('authorization required'));
9
10
  }
10
11
  /**
11
12
  * Converts Arcade Tool Input to Zod schema
@@ -29,33 +30,25 @@ export function convertParametersToZodSchema(parameters) {
29
30
  * Converts a value schema to Zod type
30
31
  */
31
32
  function convertValueSchemaToZod(schema) {
32
- let baseType;
33
33
  switch (schema.val_type) {
34
34
  case 'string':
35
- baseType = schema.enum ? z.enum(schema.enum) : z.string();
36
- break;
35
+ return schema.enum ? z.enum(schema.enum) : z.string();
37
36
  case 'integer':
38
- baseType = z.number().int();
39
- break;
37
+ return z.number().int();
40
38
  case 'number':
41
- baseType = z.number();
42
- break;
39
+ return z.number();
43
40
  case 'boolean':
44
- baseType = z.boolean();
45
- break;
41
+ return z.boolean();
46
42
  case 'json':
47
- baseType = z.any();
48
- break;
43
+ return z.any();
49
44
  case 'array':
50
45
  if (!schema.inner_val_type) {
51
46
  throw new Error('Array type must have inner_val_type specified');
52
47
  }
53
- baseType = z.array(convertValueSchemaToZod({ val_type: schema.inner_val_type }));
54
- break;
48
+ return z.array(convertValueSchemaToZod({ val_type: schema.inner_val_type }));
55
49
  default:
56
- throw new Error(`Unsupported value type: ${schema.val_type}`);
50
+ return z.any();
57
51
  }
58
- return baseType;
59
52
  }
60
53
  /**
61
54
  * Converts Arcade Tool Output to Zod schema
@@ -84,79 +77,93 @@ export function convertSingleToolToSchema(tool) {
84
77
  };
85
78
  }
86
79
  /**
87
- * Converts OpenAI formatted tools to Zod-validated tool schemas
80
+ * Converts tools to Zod-validated tool schemas
88
81
  * @param tools - Array of formatted tools
89
82
  * @returns Array of Zod-validated tool schemas
90
83
  */
91
84
  export function toZodSchema(tools) {
92
85
  return tools.map(convertSingleToolToSchema);
93
86
  }
87
+ /**
88
+ * Creates a tool execution function that will validate the input and execute the tool
89
+ */
90
+ export function executeZodTool({ zodToolSchema, client, userId, }) {
91
+ const { parameters, name: toolName } = zodToolSchema;
92
+ return async (input) => {
93
+ const validationResult = parameters.safeParse(input);
94
+ if (!validationResult.success) {
95
+ throw new Error(`Invalid input: ${validationResult.error.message}`);
96
+ }
97
+ const result = await client.tools.execute({
98
+ tool_name: toolName,
99
+ input: validationResult.data,
100
+ user_id: userId,
101
+ });
102
+ return result;
103
+ };
104
+ }
105
+ /**
106
+ * Creates a tool execution function that will execute the tool if it is authorized,
107
+ * otherwise it will return a ToolAuthorizationResponse
108
+ */
109
+ export function executeOrAuthorizeZodTool({ zodToolSchema, toolDefinition, client, userId, }) {
110
+ const { name: toolName } = zodToolSchema;
111
+ return async (input) => {
112
+ try {
113
+ const result = await executeZodTool({ zodToolSchema, toolDefinition, client, userId })(input);
114
+ return result;
115
+ }
116
+ catch (error) {
117
+ if (error instanceof Error && isAuthorizationRequiredError(error)) {
118
+ const response = await client.tools.authorize({
119
+ tool_name: toolName,
120
+ user_id: userId,
121
+ });
122
+ return {
123
+ authorization_required: true,
124
+ authorization_response: response,
125
+ url: response.url ?? '',
126
+ message: `This tool requires authorization. Please visit the following URL to authorize the tool: ${response.url}`,
127
+ };
128
+ }
129
+ throw error;
130
+ }
131
+ };
132
+ }
94
133
  /**
95
134
  * Converts a single tool to a full tool with execution capabilities
96
- * @param tool - The tool to convert
97
- * @param client - Arcade client instance
98
- * @param userId - User ID to use for the tool execution
99
- * @returns Zod-validated tool with execution methods
100
- * @throws ToolConversionError if the tool is invalid
101
135
  */
102
- export function createZodTool({ tool, client, userId, }) {
136
+ export function createZodTool(props) {
137
+ const { tool, client, userId, executeFactory: providedExecuteFactory } = props;
103
138
  const schema = convertSingleToolToSchema(tool);
104
139
  const { name, description, parameters, output } = schema;
140
+ const factoryToUse = providedExecuteFactory ??
141
+ executeZodTool;
105
142
  return {
106
143
  name,
107
144
  description,
108
145
  parameters,
109
146
  output,
110
- execute: async (input) => {
111
- const validationResult = parameters.safeParse(input);
112
- if (!validationResult.success) {
113
- throw new Error(`Invalid input: ${validationResult.error.message}`);
114
- }
115
- return client.tools.execute({
116
- tool_name: name,
117
- input: validationResult.data,
118
- user_id: userId,
119
- });
120
- },
121
- executeOrAuthorize: async (input) => {
122
- const validationResult = parameters.safeParse(input);
123
- if (!validationResult.success) {
124
- throw new Error(`Invalid input: ${validationResult.error.message}`);
125
- }
126
- try {
127
- return await client.tools.execute({
128
- tool_name: name,
129
- input: validationResult.data,
130
- user_id: userId,
131
- });
132
- }
133
- catch (error) {
134
- if (error instanceof Error && isAuthorizationRequiredError(error)) {
135
- const response = (await client.tools.authorize({
136
- tool_name: name,
137
- user_id: userId,
138
- }));
139
- return {
140
- authorization_required: true,
141
- authorization_response: response,
142
- url: response.url,
143
- message: 'This tool requires authorization. Please visit the following URL to authorize the tool: ' +
144
- response.url,
145
- };
146
- }
147
- throw error;
148
- }
149
- },
147
+ execute: factoryToUse({ toolDefinition: tool, zodToolSchema: schema, client, userId }),
150
148
  };
151
149
  }
152
150
  /**
153
- * Converts formatted tools to full tools with execution capabilities
154
- * @param tools - Array of formatted tools
155
- * @param client - Arcade client instance
156
- * @param userId - User ID to use for the tool execution
157
- * @returns Array of Zod-validated tools with execution methods
151
+ * Converts Arcade tools to zod tools with execution capabilities
152
+ */
153
+ export function toZod({ tools, client, userId, executeFactory: providedExecuteFactory, }) {
154
+ const factoryToUse = providedExecuteFactory ??
155
+ executeZodTool;
156
+ return tools.map((tool) => createZodTool({ tool, client, userId, executeFactory: factoryToUse }));
157
+ }
158
+ /**
159
+ * Creates a record of Zod-validated tools with configurable execute functions
158
160
  */
159
- export function toZod({ tools, client, userId, }) {
160
- return tools.map((tool) => createZodTool({ tool, client, userId }));
161
+ export function toZodToolSet({ tools, client, userId, executeFactory: providedExecuteFactory, }) {
162
+ const factoryToUse = providedExecuteFactory ??
163
+ executeZodTool;
164
+ return Object.fromEntries(tools.map((tool) => {
165
+ const zodTool = createZodTool({ tool, client, userId, executeFactory: factoryToUse });
166
+ return [zodTool.name, zodTool];
167
+ }));
161
168
  }
162
169
  //# sourceMappingURL=zod.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"zod.mjs","sourceRoot":"","sources":["../../src/lib/zod/zod.ts"],"names":[],"mappings":"OAEO,EAAE,CAAC,EAAE,MAAM,KAAK;AAKvB;;GAEG;AACH,MAAM,UAAU,4BAA4B,CAAC,KAAY;IACvD,OAAO,CACL,KAAK,EAAE,IAAI,KAAK,uBAAuB;QACvC,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,mBAAmB,CAAC;QAC7C,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,wBAAwB,CAAC,CACnD,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,4BAA4B,CAAC,UAAgC;IAC3E,IAAI,CAAC,UAAU,CAAC,UAAU,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE;QACnE,OAAO,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;KACrB;IAED,MAAM,SAAS,GAA8B,EAAE,CAAC;IAEhD,KAAK,MAAM,KAAK,IAAI,UAAU,CAAC,UAAU,EAAE;QACzC,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,YAAY,EAAE,GAAG,KAAK,CAAC;QAC/C,IAAI,OAAO,GAAG,uBAAuB,CAAC,YAAY,CAAC,CAAC;QAEpD,IAAI,CAAC,QAAQ,EAAE;YACb,OAAO,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;SAC9B;QAED,SAAS,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC;KAC3B;IAED,OAAO,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;AAC7B,CAAC;AAED;;GAEG;AACH,SAAS,uBAAuB,CAAC,MAIhC;IACC,IAAI,QAAmB,CAAC;IAExB,QAAQ,MAAM,CAAC,QAAQ,EAAE;QACvB,KAAK,QAAQ;YACX,QAAQ,GAAG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D;IACrC,MAAM,YAAY,GAChB,sBAAsB;QACrB,cAAsG,CAAC;IAC1G,OAAO,MAAM,CAAC,WAAW,CACvB,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;QACjB,MAAM,OAAO,GAAG,aAAa,CAAU,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,YAAY,EAAE,CAAC,CAAC;QAC/F,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACjC,CAAC,CAAC,CACH,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcadeai/arcadejs",
3
- "version": "1.4.2",
3
+ "version": "1.5.0",
4
4
  "description": "The official TypeScript library for the Arcade API",
5
5
  "author": "Arcade <dev@arcade.dev>",
6
6
  "types": "./index.d.ts",
@@ -1,5 +1,6 @@
1
+ import type { Arcade } from '../../index';
1
2
  import { AuthorizationResponse } from '../../resources/shared';
2
- import { ExecuteToolResponse } from '../../resources/tools/tools';
3
+ import { ExecuteToolResponse, ToolDefinition } from '../../resources/tools/tools';
3
4
  import { z } from 'zod';
4
5
 
5
6
  /**
@@ -13,26 +14,71 @@ export interface ToolAuthorizationResponse {
13
14
  }
14
15
 
15
16
  /**
16
- * Represents a tool with Zod schema validation and execution capabilities.
17
+ * Schema definition for an Arcade tool, containing the tool's name,
18
+ * description, and Zod-validated parameter structure.
17
19
  */
18
- export interface ZodTool {
20
+ export interface ZodToolSchema {
21
+ /** Qualified name of the tool */
19
22
  name: string;
20
- description: string | undefined;
23
+ /** Optional, human-readable description */
24
+ description?: string | undefined;
25
+ /** Zod schema for validating tool parameters */
21
26
  parameters: z.ZodType;
27
+ /** Zod schema for validating tool output (if any) */
22
28
  output: z.ZodType | undefined;
23
- execute: <T extends unknown>(input: T) => Promise<ExecuteToolResponse>;
24
- executeOrAuthorize: <T extends unknown>(
25
- input: T,
26
- ) => Promise<ExecuteToolResponse | ToolAuthorizationResponse>;
27
29
  }
28
30
 
29
31
  /**
30
- * Schema definition for an Arcade tool, containing the tool's name,
31
- * description, and Zod-validated parameter structure.
32
+ * Generic type for any tool execution function
32
33
  */
33
- export interface ZodToolSchema {
34
- name: string;
35
- description: string | undefined;
36
- parameters: z.ZodType;
37
- output: z.ZodType | undefined;
34
+ export type ToolExecuteFunction<TReturn> = (input: unknown) => Promise<TReturn>;
35
+
36
+ /**
37
+ * A complete tool implementation with Zod validation and execution capabilities
38
+ */
39
+ export interface ZodTool<TReturn = ExecuteToolResponse> extends ZodToolSchema {
40
+ execute: ToolExecuteFunction<TReturn>;
41
+ }
42
+
43
+ /**
44
+ * Arcade client
45
+ */
46
+ export type ArcadeClient = Omit<Arcade, '_options'>;
47
+
48
+ /**
49
+ * Input required to create a tool execution function
50
+ */
51
+ export interface ToolExecuteFunctionFactoryInput {
52
+ /** Original tool definition from Arcade */
53
+ toolDefinition: ToolDefinition;
54
+ /** Zod-validated tool schema */
55
+ zodToolSchema: ZodToolSchema;
56
+ /** Arcade client instance */
57
+ client: ArcadeClient;
58
+ /** User ID for tool execution/authorization */
59
+ userId: string;
60
+ }
61
+
62
+ /**
63
+ * Base input type for tool creation functions
64
+ */
65
+ export interface BaseToolCreationInput<TReturn = ExecuteToolResponse> {
66
+ client: ArcadeClient;
67
+ userId: string;
68
+ executeFactory?: (props: ToolExecuteFunctionFactoryInput) => ToolExecuteFunction<TReturn>;
69
+ }
70
+
71
+ /**
72
+ * Input required to create a single Zod tool
73
+ */
74
+ export interface CreateZodToolInput<TReturn = ExecuteToolResponse> extends BaseToolCreationInput<TReturn> {
75
+ tool: ToolDefinition;
76
+ }
77
+
78
+ /**
79
+ * Input required to create multiple Zod tools
80
+ */
81
+ export interface CreateMultipleZodToolsInput<TReturn = ExecuteToolResponse>
82
+ extends BaseToolCreationInput<TReturn> {
83
+ tools: ToolDefinition[];
38
84
  }
@@ -1,18 +1,24 @@
1
1
  import { ExecuteToolResponse, ToolDefinition } from '../../resources/tools/tools';
2
- import { ToolAuthorizationResponse, ZodTool, ZodToolSchema } from './types';
2
+ import {
3
+ ZodTool,
4
+ ZodToolSchema,
5
+ ToolExecuteFunctionFactoryInput,
6
+ CreateZodToolInput,
7
+ CreateMultipleZodToolsInput,
8
+ ToolExecuteFunction,
9
+ ToolAuthorizationResponse,
10
+ } from './types';
3
11
  import { z } from 'zod';
4
- import type { Arcade } from '../../index';
5
-
6
- type ArcadeClient = Omit<Arcade, '_options'>;
7
12
 
8
13
  /**
9
14
  * Checks if an error indicates that authorization for the tool is required
10
15
  */
11
16
  export function isAuthorizationRequiredError(error: Error): boolean {
17
+ const errorMessage = error?.message?.toLowerCase() || '';
12
18
  return (
13
19
  error?.name === 'PermissionDeniedError' ||
14
- error?.message?.includes('permission denied') ||
15
- error?.message?.includes('authorization required')
20
+ errorMessage.includes('permission denied') ||
21
+ errorMessage.includes('authorization required')
16
22
  );
17
23
  }
18
24
 
@@ -48,35 +54,25 @@ function convertValueSchemaToZod(schema: {
48
54
  inner_val_type?: string | null;
49
55
  enum?: string[] | null;
50
56
  }): z.ZodType {
51
- let baseType: z.ZodType;
52
-
53
57
  switch (schema.val_type) {
54
58
  case 'string':
55
- baseType = schema.enum ? z.enum(schema.enum as [string, ...string[]]) : z.string();
56
- break;
59
+ return schema.enum ? z.enum(schema.enum as [string, ...string[]]) : z.string();
57
60
  case 'integer':
58
- baseType = z.number().int();
59
- break;
61
+ return z.number().int();
60
62
  case 'number':
61
- baseType = z.number();
62
- break;
63
+ return z.number();
63
64
  case 'boolean':
64
- baseType = z.boolean();
65
- break;
65
+ return z.boolean();
66
66
  case 'json':
67
- baseType = z.any();
68
- break;
67
+ return z.any();
69
68
  case 'array':
70
69
  if (!schema.inner_val_type) {
71
70
  throw new Error('Array type must have inner_val_type specified');
72
71
  }
73
- baseType = z.array(convertValueSchemaToZod({ val_type: schema.inner_val_type }));
74
- break;
72
+ return z.array(convertValueSchemaToZod({ val_type: schema.inner_val_type }));
75
73
  default:
76
- throw new Error(`Unsupported value type: ${schema.val_type}`);
74
+ return z.any();
77
75
  }
78
-
79
- return baseType;
80
76
  }
81
77
 
82
78
  /**
@@ -109,7 +105,7 @@ export function convertSingleToolToSchema(tool: ToolDefinition): ZodToolSchema {
109
105
  }
110
106
 
111
107
  /**
112
- * Converts OpenAI formatted tools to Zod-validated tool schemas
108
+ * Converts tools to Zod-validated tool schemas
113
109
  * @param tools - Array of formatted tools
114
110
  * @returns Array of Zod-validated tool schemas
115
111
  */
@@ -118,93 +114,119 @@ export function toZodSchema(tools: ToolDefinition[]): ZodToolSchema[] {
118
114
  }
119
115
 
120
116
  /**
121
- * Converts a single tool to a full tool with execution capabilities
122
- * @param tool - The tool to convert
123
- * @param client - Arcade client instance
124
- * @param userId - User ID to use for the tool execution
125
- * @returns Zod-validated tool with execution methods
126
- * @throws ToolConversionError if the tool is invalid
117
+ * Creates a tool execution function that will validate the input and execute the tool
118
+ */
119
+ export function executeZodTool({
120
+ zodToolSchema,
121
+ client,
122
+ userId,
123
+ }: ToolExecuteFunctionFactoryInput): ToolExecuteFunction<ExecuteToolResponse> {
124
+ const { parameters, name: toolName } = zodToolSchema;
125
+
126
+ return async (input: unknown): Promise<ExecuteToolResponse> => {
127
+ const validationResult = parameters.safeParse(input);
128
+ if (!validationResult.success) {
129
+ throw new Error(`Invalid input: ${validationResult.error.message}`);
130
+ }
131
+
132
+ const result = await client.tools.execute({
133
+ tool_name: toolName,
134
+ input: validationResult.data,
135
+ user_id: userId,
136
+ });
137
+ return result;
138
+ };
139
+ }
140
+
141
+ /**
142
+ * Creates a tool execution function that will execute the tool if it is authorized,
143
+ * otherwise it will return a ToolAuthorizationResponse
127
144
  */
128
- export function createZodTool({
129
- tool,
145
+ export function executeOrAuthorizeZodTool({
146
+ zodToolSchema,
147
+ toolDefinition,
130
148
  client,
131
149
  userId,
132
- }: {
133
- tool: ToolDefinition;
134
- client: ArcadeClient;
135
- userId: string;
136
- }): ZodTool {
150
+ }: ToolExecuteFunctionFactoryInput): ToolExecuteFunction<ExecuteToolResponse | ToolAuthorizationResponse> {
151
+ const { name: toolName } = zodToolSchema;
152
+
153
+ return async (input: unknown) => {
154
+ try {
155
+ const result = await executeZodTool({ zodToolSchema, toolDefinition, client, userId })(input);
156
+ return result;
157
+ } catch (error) {
158
+ if (error instanceof Error && isAuthorizationRequiredError(error)) {
159
+ const response = await client.tools.authorize({
160
+ tool_name: toolName,
161
+ user_id: userId,
162
+ });
163
+
164
+ return {
165
+ authorization_required: true,
166
+ authorization_response: response,
167
+ url: response.url ?? '',
168
+ message: `This tool requires authorization. Please visit the following URL to authorize the tool: ${response.url}`,
169
+ };
170
+ }
171
+ throw error;
172
+ }
173
+ };
174
+ }
175
+
176
+ /**
177
+ * Converts a single tool to a full tool with execution capabilities
178
+ */
179
+ export function createZodTool<TReturn = ExecuteToolResponse>(
180
+ props: CreateZodToolInput<TReturn>,
181
+ ): ZodTool<TReturn> {
182
+ const { tool, client, userId, executeFactory: providedExecuteFactory } = props;
137
183
  const schema = convertSingleToolToSchema(tool);
138
184
  const { name, description, parameters, output } = schema;
139
185
 
186
+ const factoryToUse =
187
+ providedExecuteFactory ??
188
+ (executeZodTool as unknown as (props: ToolExecuteFunctionFactoryInput) => ToolExecuteFunction<TReturn>);
189
+
140
190
  return {
141
191
  name,
142
192
  description,
143
193
  parameters,
144
194
  output,
145
- execute: async (input: z.infer<typeof parameters>): Promise<ExecuteToolResponse> => {
146
- const validationResult = parameters.safeParse(input);
147
- if (!validationResult.success) {
148
- throw new Error(`Invalid input: ${validationResult.error.message}`);
149
- }
150
-
151
- return client.tools.execute({
152
- tool_name: name,
153
- input: validationResult.data,
154
- user_id: userId,
155
- });
156
- },
157
- executeOrAuthorize: async (
158
- input: z.infer<typeof parameters>,
159
- ): Promise<ExecuteToolResponse | ToolAuthorizationResponse> => {
160
- const validationResult = parameters.safeParse(input);
161
- if (!validationResult.success) {
162
- throw new Error(`Invalid input: ${validationResult.error.message}`);
163
- }
164
-
165
- try {
166
- return await client.tools.execute({
167
- tool_name: name,
168
- input: validationResult.data,
169
- user_id: userId,
170
- });
171
- } catch (error) {
172
- if (error instanceof Error && isAuthorizationRequiredError(error)) {
173
- const response = (await client.tools.authorize({
174
- tool_name: name,
175
- user_id: userId,
176
- })) as { url: string };
177
-
178
- return {
179
- authorization_required: true,
180
- authorization_response: response,
181
- url: response.url,
182
- message:
183
- 'This tool requires authorization. Please visit the following URL to authorize the tool: ' +
184
- response.url,
185
- };
186
- }
187
- throw error;
188
- }
189
- },
195
+ execute: factoryToUse({ toolDefinition: tool, zodToolSchema: schema, client, userId }),
190
196
  };
191
197
  }
192
198
 
193
199
  /**
194
- * Converts formatted tools to full tools with execution capabilities
195
- * @param tools - Array of formatted tools
196
- * @param client - Arcade client instance
197
- * @param userId - User ID to use for the tool execution
198
- * @returns Array of Zod-validated tools with execution methods
200
+ * Converts Arcade tools to zod tools with execution capabilities
199
201
  */
200
- export function toZod({
202
+ export function toZod<TReturn = ExecuteToolResponse>({
201
203
  tools,
202
204
  client,
203
205
  userId,
204
- }: {
205
- tools: ToolDefinition[];
206
- client: ArcadeClient;
207
- userId: string;
208
- }): ZodTool[] {
209
- return tools.map((tool) => createZodTool({ tool, client, userId }));
206
+ executeFactory: providedExecuteFactory,
207
+ }: CreateMultipleZodToolsInput<TReturn>): ZodTool<TReturn>[] {
208
+ const factoryToUse =
209
+ providedExecuteFactory ??
210
+ (executeZodTool as unknown as (props: ToolExecuteFunctionFactoryInput) => ToolExecuteFunction<TReturn>);
211
+ return tools.map((tool) => createZodTool<TReturn>({ tool, client, userId, executeFactory: factoryToUse }));
212
+ }
213
+
214
+ /**
215
+ * Creates a record of Zod-validated tools with configurable execute functions
216
+ */
217
+ export function toZodToolSet<TReturn = ExecuteToolResponse>({
218
+ tools,
219
+ client,
220
+ userId,
221
+ executeFactory: providedExecuteFactory,
222
+ }: CreateMultipleZodToolsInput<TReturn>): Record<string, ZodTool<TReturn>> {
223
+ const factoryToUse =
224
+ providedExecuteFactory ??
225
+ (executeZodTool as unknown as (props: ToolExecuteFunctionFactoryInput) => ToolExecuteFunction<TReturn>);
226
+ return Object.fromEntries(
227
+ tools.map((tool) => {
228
+ const zodTool = createZodTool<TReturn>({ tool, client, userId, executeFactory: factoryToUse });
229
+ return [zodTool.name, zodTool];
230
+ }),
231
+ );
210
232
  }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '1.4.2'; // x-release-please-version
1
+ export const VERSION = '1.5.0'; // x-release-please-version
package/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "1.4.2";
1
+ export declare const VERSION = "1.5.0";
2
2
  //# sourceMappingURL=version.d.ts.map
package/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
- exports.VERSION = '1.4.2'; // x-release-please-version
4
+ exports.VERSION = '1.5.0'; // x-release-please-version
5
5
  //# sourceMappingURL=version.js.map
package/version.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export const VERSION = '1.4.2'; // x-release-please-version
1
+ export const VERSION = '1.5.0'; // x-release-please-version
2
2
  //# sourceMappingURL=version.mjs.map