@arcadeai/arcadejs 1.4.1 → 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.
- package/lib/zod/types.d.ts +54 -13
- package/lib/zod/types.d.ts.map +1 -1
- package/lib/zod/zod.d.ts +20 -25
- package/lib/zod/zod.d.ts.map +1 -1
- package/lib/zod/zod.js +82 -72
- package/lib/zod/zod.js.map +1 -1
- package/lib/zod/zod.mjs +78 -71
- package/lib/zod/zod.mjs.map +1 -1
- package/package.json +6 -1
- package/src/lib/zod/types.ts +61 -15
- package/src/lib/zod/zod.ts +116 -92
- package/src/version.ts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
package/lib/zod/types.d.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
package/lib/zod/types.d.ts.map
CHANGED
|
@@ -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;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/zod/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAClF,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;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC5B,iCAAiC;I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AAqB,CAAC,OAAO,CAAC;IACtC,KAAK,EAAE,cAAc,EAAE,CAAC;CACzB"}
|
package/lib/zod/zod.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { ToolDefinition } from "../../resources/tools/tools.js";
|
|
2
|
-
import { ZodTool, 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 '@arcadeai/arcadejs';
|
|
5
4
|
/**
|
|
6
5
|
* Checks if an error indicates that authorization for the tool is required
|
|
7
6
|
*/
|
|
@@ -22,34 +21,30 @@ export declare function convertOutputToZodSchema(output: ToolDefinition.Output):
|
|
|
22
21
|
*/
|
|
23
22
|
export declare function convertSingleToolToSchema(tool: ToolDefinition): ZodToolSchema;
|
|
24
23
|
/**
|
|
25
|
-
* Converts
|
|
24
|
+
* Converts tools to Zod-validated tool schemas
|
|
26
25
|
* @param tools - Array of formatted tools
|
|
27
26
|
* @returns Array of Zod-validated tool schemas
|
|
28
27
|
*/
|
|
29
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>;
|
|
30
38
|
/**
|
|
31
39
|
* Converts a single tool to a full tool with execution capabilities
|
|
32
|
-
* @param tool - The tool to convert
|
|
33
|
-
* @param client - Arcade client instance
|
|
34
|
-
* @param userId - User ID to use for the tool execution
|
|
35
|
-
* @returns Zod-validated tool with execution methods
|
|
36
|
-
* @throws ToolConversionError if the tool is invalid
|
|
37
40
|
*/
|
|
38
|
-
export declare function createZodTool
|
|
39
|
-
tool: ToolDefinition;
|
|
40
|
-
client: Arcade;
|
|
41
|
-
userId: string;
|
|
42
|
-
}): ZodTool;
|
|
41
|
+
export declare function createZodTool<TReturn = ExecuteToolResponse>(props: CreateZodToolInput<TReturn>): ZodTool<TReturn>;
|
|
43
42
|
/**
|
|
44
|
-
* Converts
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
*
|
|
49
|
-
*/
|
|
50
|
-
export declare function
|
|
51
|
-
tools: ToolDefinition[];
|
|
52
|
-
client: Arcade;
|
|
53
|
-
userId: string;
|
|
54
|
-
}): ZodTool[];
|
|
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>>;
|
|
55
50
|
//# sourceMappingURL=zod.d.ts.map
|
package/lib/zod/zod.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.d.ts","sourceRoot":"","sources":["../../src/lib/zod/zod.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|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AgB,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
|
-
|
|
11
|
-
|
|
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
|
-
|
|
41
|
-
break;
|
|
40
|
+
return schema.enum ? zod_1.z.enum(schema.enum) : zod_1.z.string();
|
|
42
41
|
case 'integer':
|
|
43
|
-
|
|
44
|
-
break;
|
|
42
|
+
return zod_1.z.number().int();
|
|
45
43
|
case 'number':
|
|
46
|
-
|
|
47
|
-
break;
|
|
44
|
+
return zod_1.z.number();
|
|
48
45
|
case 'boolean':
|
|
49
|
-
|
|
50
|
-
break;
|
|
46
|
+
return zod_1.z.boolean();
|
|
51
47
|
case 'json':
|
|
52
|
-
|
|
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
|
-
|
|
59
|
-
break;
|
|
53
|
+
return zod_1.z.array(convertValueSchemaToZod({ val_type: schema.inner_val_type }));
|
|
60
54
|
default:
|
|
61
|
-
|
|
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
|
|
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(
|
|
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:
|
|
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
|
|
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
|
-
|
|
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
|
package/lib/zod/zod.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.js","sourceRoot":"","sources":["../../src/lib/zod/zod.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"zod.js","sourceRoot":"","sources":["../../src/lib/zod/zod.ts"],"names":[],"mappings":";;;AAUA,6BAAwB;AAExB;;GAEG;AACH,SAAgB,4BAA4B,CAAC,KAAY;IACvD,MAAM,YAAY,GAAG,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC;IACzD,OAAO,CACL,KAAK,EAAE,IAAI,KAAK,uBAAuB;QACvC,YAAY,CAAC,QAAQ,CAAC,mBAAmB,CAAC;QAC1C,YAAY,CAAC,QAAQ,CAAC,wBAAwB,CAAC,CAChD,CAAC;AACJ,CAAC;AAPD,oEAOC;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,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,OAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;AAC7B,CAAC;AAnBD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AAgB,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
|
-
|
|
8
|
-
|
|
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
|
-
|
|
36
|
-
break;
|
|
35
|
+
return schema.enum ? z.enum(schema.enum) : z.string();
|
|
37
36
|
case 'integer':
|
|
38
|
-
|
|
39
|
-
break;
|
|
37
|
+
return z.number().int();
|
|
40
38
|
case 'number':
|
|
41
|
-
|
|
42
|
-
break;
|
|
39
|
+
return z.number();
|
|
43
40
|
case 'boolean':
|
|
44
|
-
|
|
45
|
-
break;
|
|
41
|
+
return z.boolean();
|
|
46
42
|
case 'json':
|
|
47
|
-
|
|
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
|
-
|
|
54
|
-
break;
|
|
48
|
+
return z.array(convertValueSchemaToZod({ val_type: schema.inner_val_type }));
|
|
55
49
|
default:
|
|
56
|
-
|
|
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
|
|
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(
|
|
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:
|
|
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
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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
|
|
160
|
-
|
|
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
|
package/lib/zod/zod.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.mjs","sourceRoot":"","sources":["../../src/lib/zod/zod.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"zod.mjs","sourceRoot":"","sources":["../../src/lib/zod/zod.ts"],"names":[],"mappings":"OAUO,EAAE,CAAC,EAAE,MAAM,KAAK;AAEvB;;GAEG;AACH,MAAM,UAAU,4BAA4B,CAAC,KAAY;IACvD,MAAM,YAAY,GAAG,KAAK,EAAE,OAAO,EAAE,WAAW,EA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AsG,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.
|
|
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",
|
|
@@ -79,6 +79,11 @@
|
|
|
79
79
|
"require": "./_shims/auto/*.js",
|
|
80
80
|
"default": "./_shims/auto/*.mjs"
|
|
81
81
|
},
|
|
82
|
+
"./lib": {
|
|
83
|
+
"types": "./lib/index.d.ts",
|
|
84
|
+
"require": "./lib/index.js",
|
|
85
|
+
"default": "./lib/index.mjs"
|
|
86
|
+
},
|
|
82
87
|
".": {
|
|
83
88
|
"require": {
|
|
84
89
|
"types": "./index.d.ts",
|
package/src/lib/zod/types.ts
CHANGED
|
@@ -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
|
-
*
|
|
17
|
+
* Schema definition for an Arcade tool, containing the tool's name,
|
|
18
|
+
* description, and Zod-validated parameter structure.
|
|
17
19
|
*/
|
|
18
|
-
export interface
|
|
20
|
+
export interface ZodToolSchema {
|
|
21
|
+
/** Qualified name of the tool */
|
|
19
22
|
name: string;
|
|
20
|
-
|
|
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
|
-
*
|
|
31
|
-
* description, and Zod-validated parameter structure.
|
|
32
|
+
* Generic type for any tool execution function
|
|
32
33
|
*/
|
|
33
|
-
export
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
}
|
package/src/lib/zod/zod.ts
CHANGED
|
@@ -1,16 +1,24 @@
|
|
|
1
1
|
import { ExecuteToolResponse, ToolDefinition } from '../../resources/tools/tools';
|
|
2
|
-
import {
|
|
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 '@arcadeai/arcadejs';
|
|
5
12
|
|
|
6
13
|
/**
|
|
7
14
|
* Checks if an error indicates that authorization for the tool is required
|
|
8
15
|
*/
|
|
9
16
|
export function isAuthorizationRequiredError(error: Error): boolean {
|
|
17
|
+
const errorMessage = error?.message?.toLowerCase() || '';
|
|
10
18
|
return (
|
|
11
19
|
error?.name === 'PermissionDeniedError' ||
|
|
12
|
-
|
|
13
|
-
|
|
20
|
+
errorMessage.includes('permission denied') ||
|
|
21
|
+
errorMessage.includes('authorization required')
|
|
14
22
|
);
|
|
15
23
|
}
|
|
16
24
|
|
|
@@ -46,35 +54,25 @@ function convertValueSchemaToZod(schema: {
|
|
|
46
54
|
inner_val_type?: string | null;
|
|
47
55
|
enum?: string[] | null;
|
|
48
56
|
}): z.ZodType {
|
|
49
|
-
let baseType: z.ZodType;
|
|
50
|
-
|
|
51
57
|
switch (schema.val_type) {
|
|
52
58
|
case 'string':
|
|
53
|
-
|
|
54
|
-
break;
|
|
59
|
+
return schema.enum ? z.enum(schema.enum as [string, ...string[]]) : z.string();
|
|
55
60
|
case 'integer':
|
|
56
|
-
|
|
57
|
-
break;
|
|
61
|
+
return z.number().int();
|
|
58
62
|
case 'number':
|
|
59
|
-
|
|
60
|
-
break;
|
|
63
|
+
return z.number();
|
|
61
64
|
case 'boolean':
|
|
62
|
-
|
|
63
|
-
break;
|
|
65
|
+
return z.boolean();
|
|
64
66
|
case 'json':
|
|
65
|
-
|
|
66
|
-
break;
|
|
67
|
+
return z.any();
|
|
67
68
|
case 'array':
|
|
68
69
|
if (!schema.inner_val_type) {
|
|
69
70
|
throw new Error('Array type must have inner_val_type specified');
|
|
70
71
|
}
|
|
71
|
-
|
|
72
|
-
break;
|
|
72
|
+
return z.array(convertValueSchemaToZod({ val_type: schema.inner_val_type }));
|
|
73
73
|
default:
|
|
74
|
-
|
|
74
|
+
return z.any();
|
|
75
75
|
}
|
|
76
|
-
|
|
77
|
-
return baseType;
|
|
78
76
|
}
|
|
79
77
|
|
|
80
78
|
/**
|
|
@@ -107,7 +105,7 @@ export function convertSingleToolToSchema(tool: ToolDefinition): ZodToolSchema {
|
|
|
107
105
|
}
|
|
108
106
|
|
|
109
107
|
/**
|
|
110
|
-
* Converts
|
|
108
|
+
* Converts tools to Zod-validated tool schemas
|
|
111
109
|
* @param tools - Array of formatted tools
|
|
112
110
|
* @returns Array of Zod-validated tool schemas
|
|
113
111
|
*/
|
|
@@ -116,93 +114,119 @@ export function toZodSchema(tools: ToolDefinition[]): ZodToolSchema[] {
|
|
|
116
114
|
}
|
|
117
115
|
|
|
118
116
|
/**
|
|
119
|
-
*
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
|
125
144
|
*/
|
|
126
|
-
export function
|
|
127
|
-
|
|
145
|
+
export function executeOrAuthorizeZodTool({
|
|
146
|
+
zodToolSchema,
|
|
147
|
+
toolDefinition,
|
|
128
148
|
client,
|
|
129
149
|
userId,
|
|
130
|
-
}: {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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;
|
|
135
183
|
const schema = convertSingleToolToSchema(tool);
|
|
136
184
|
const { name, description, parameters, output } = schema;
|
|
137
185
|
|
|
186
|
+
const factoryToUse =
|
|
187
|
+
providedExecuteFactory ??
|
|
188
|
+
(executeZodTool as unknown as (props: ToolExecuteFunctionFactoryInput) => ToolExecuteFunction<TReturn>);
|
|
189
|
+
|
|
138
190
|
return {
|
|
139
191
|
name,
|
|
140
192
|
description,
|
|
141
193
|
parameters,
|
|
142
194
|
output,
|
|
143
|
-
execute:
|
|
144
|
-
const validationResult = parameters.safeParse(input);
|
|
145
|
-
if (!validationResult.success) {
|
|
146
|
-
throw new Error(`Invalid input: ${validationResult.error.message}`);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
return client.tools.execute({
|
|
150
|
-
tool_name: name,
|
|
151
|
-
input: validationResult.data,
|
|
152
|
-
user_id: userId,
|
|
153
|
-
});
|
|
154
|
-
},
|
|
155
|
-
executeOrAuthorize: async (
|
|
156
|
-
input: z.infer<typeof parameters>,
|
|
157
|
-
): Promise<ExecuteToolResponse | ToolAuthorizationResponse> => {
|
|
158
|
-
const validationResult = parameters.safeParse(input);
|
|
159
|
-
if (!validationResult.success) {
|
|
160
|
-
throw new Error(`Invalid input: ${validationResult.error.message}`);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
try {
|
|
164
|
-
return await client.tools.execute({
|
|
165
|
-
tool_name: name,
|
|
166
|
-
input: validationResult.data,
|
|
167
|
-
user_id: userId,
|
|
168
|
-
});
|
|
169
|
-
} catch (error) {
|
|
170
|
-
if (error instanceof Error && isAuthorizationRequiredError(error)) {
|
|
171
|
-
const response = (await client.tools.authorize({
|
|
172
|
-
tool_name: name,
|
|
173
|
-
user_id: userId,
|
|
174
|
-
})) as { url: string };
|
|
175
|
-
|
|
176
|
-
return {
|
|
177
|
-
authorization_required: true,
|
|
178
|
-
authorization_response: response,
|
|
179
|
-
url: response.url,
|
|
180
|
-
message:
|
|
181
|
-
'This tool requires authorization. Please visit the following URL to authorize the tool: ' +
|
|
182
|
-
response.url,
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
throw error;
|
|
186
|
-
}
|
|
187
|
-
},
|
|
195
|
+
execute: factoryToUse({ toolDefinition: tool, zodToolSchema: schema, client, userId }),
|
|
188
196
|
};
|
|
189
197
|
}
|
|
190
198
|
|
|
191
199
|
/**
|
|
192
|
-
* Converts
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
200
|
+
* Converts Arcade tools to zod tools with execution capabilities
|
|
201
|
+
*/
|
|
202
|
+
export function toZod<TReturn = ExecuteToolResponse>({
|
|
203
|
+
tools,
|
|
204
|
+
client,
|
|
205
|
+
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
|
|
197
216
|
*/
|
|
198
|
-
export function
|
|
217
|
+
export function toZodToolSet<TReturn = ExecuteToolResponse>({
|
|
199
218
|
tools,
|
|
200
219
|
client,
|
|
201
220
|
userId,
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
return
|
|
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
|
+
);
|
|
208
232
|
}
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '1.
|
|
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.
|
|
1
|
+
export declare const VERSION = "1.5.0";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/version.js
CHANGED
package/version.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const VERSION = '1.
|
|
1
|
+
export const VERSION = '1.5.0'; // x-release-please-version
|
|
2
2
|
//# sourceMappingURL=version.mjs.map
|