@arcadeai/arcadejs 1.4.2 → 1.6.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,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
  }
@@ -125,6 +125,12 @@ export interface AuthRequest {
125
125
  auth_requirement: AuthRequest.AuthRequirement;
126
126
 
127
127
  user_id: string;
128
+
129
+ /**
130
+ * Optional: if provided, the user will be redirected to this URI after
131
+ * authorization
132
+ */
133
+ next_uri?: string;
128
134
  }
129
135
 
130
136
  export namespace AuthRequest {
@@ -155,6 +161,12 @@ export interface AuthAuthorizeParams {
155
161
  auth_requirement: AuthAuthorizeParams.AuthRequirement;
156
162
 
157
163
  user_id: string;
164
+
165
+ /**
166
+ * Optional: if provided, the user will be redirected to this URI after
167
+ * authorization
168
+ */
169
+ next_uri?: string;
158
170
  }
159
171
 
160
172
  export namespace AuthAuthorizeParams {
@@ -15,7 +15,7 @@ export interface AuthorizationResponse {
15
15
 
16
16
  scopes?: Array<string>;
17
17
 
18
- status?: 'pending' | 'completed' | 'failed';
18
+ status?: 'not_started' | 'pending' | 'completed' | 'failed';
19
19
 
20
20
  url?: string;
21
21
 
@@ -63,6 +63,11 @@ export interface FormattedListParams extends OffsetPageParams {
63
63
  * Toolkit name
64
64
  */
65
65
  toolkit?: string;
66
+
67
+ /**
68
+ * User ID
69
+ */
70
+ user_id?: string;
66
71
  }
67
72
 
68
73
  export interface FormattedGetParams {
@@ -70,6 +75,11 @@ export interface FormattedGetParams {
70
75
  * Provider format
71
76
  */
72
77
  format?: string;
78
+
79
+ /**
80
+ * User ID
81
+ */
82
+ user_id?: string;
73
83
  }
74
84
 
75
85
  Formatted.FormattedListResponsesOffsetPage = FormattedListResponsesOffsetPage;
@@ -82,6 +82,12 @@ export class ToolExecutionsOffsetPage extends OffsetPage<ToolExecution> {}
82
82
  export interface AuthorizeToolRequest {
83
83
  tool_name: string;
84
84
 
85
+ /**
86
+ * Optional: if provided, the user will be redirected to this URI after
87
+ * authorization
88
+ */
89
+ next_uri?: string;
90
+
85
91
  /**
86
92
  * Optional: if not provided, any version is used
87
93
  */
@@ -232,6 +238,8 @@ export namespace ToolDefinition {
232
238
  export interface Requirements {
233
239
  authorization?: Requirements.Authorization;
234
240
 
241
+ met?: boolean;
242
+
235
243
  secrets?: Array<Requirements.Secret>;
236
244
  }
237
245
 
@@ -244,6 +252,12 @@ export namespace ToolDefinition {
244
252
  provider_id?: string;
245
253
 
246
254
  provider_type?: string;
255
+
256
+ status?: 'active' | 'inactive';
257
+
258
+ status_reason?: string;
259
+
260
+ token_status?: 'not_started' | 'pending' | 'completed' | 'failed';
247
261
  }
248
262
 
249
263
  export namespace Authorization {
@@ -254,6 +268,10 @@ export namespace ToolDefinition {
254
268
 
255
269
  export interface Secret {
256
270
  key: string;
271
+
272
+ met?: boolean;
273
+
274
+ status_reason?: string;
257
275
  }
258
276
  }
259
277
  }
@@ -350,11 +368,22 @@ export interface ToolListParams extends OffsetPageParams {
350
368
  * Toolkit name
351
369
  */
352
370
  toolkit?: string;
371
+
372
+ /**
373
+ * User ID
374
+ */
375
+ user_id?: string;
353
376
  }
354
377
 
355
378
  export interface ToolAuthorizeParams {
356
379
  tool_name: string;
357
380
 
381
+ /**
382
+ * Optional: if provided, the user will be redirected to this URI after
383
+ * authorization
384
+ */
385
+ next_uri?: string;
386
+
358
387
  /**
359
388
  * Optional: if not provided, any version is used
360
389
  */
@@ -393,6 +422,11 @@ export interface ToolGetParams {
393
422
  * Comma separated tool formats that will be included in the response.
394
423
  */
395
424
  include_format?: Array<'arcade' | 'openai' | 'anthropic'>;
425
+
426
+ /**
427
+ * User ID
428
+ */
429
+ user_id?: string;
396
430
  }
397
431
 
398
432
  Tools.ToolDefinitionsOffsetPage = ToolDefinitionsOffsetPage;
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '1.4.2'; // x-release-please-version
1
+ export const VERSION = '1.6.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.6.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.6.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.6.0'; // x-release-please-version
2
2
  //# sourceMappingURL=version.mjs.map