@@ -179,24 +179,21 @@ This automatically returns authorization URLs instead of throwing errors.
179179``` javascript
180180import { isAuthorizationRequiredError } from " @arcadeai/arcadejs/lib" ;
181181
182- const tools = arcadeTools .map (({ name, description, execute, parameters }) =>
183- tool ({
184- name,
185- description: description ?? " " ,
186- parameters: parameters as any,
187- execute,
188- errorFunction: async (_, error) => {
189- if (error instanceof Error && isAuthorizationRequiredError (error)) {
190- const response = await client .tools .authorize ({
191- tool_name: name,
192- user_id: " <YOUR_SYSTEM_USER_ID_2>" ,
193- });
194- return ` Please login to Google: ${ response .url } ` ;
195- }
196- return " Error executing tool"
197- }
198- }),
199- );
182+ const tools = arcadeTools .map ((arcadeTool ) => {
183+ return tool ({
184+ ... arcadeTool,
185+ errorFunction: async (_ , error ) => {
186+ if (error instanceof Error && isAuthorizationRequiredError (error)) {
187+ const response = await client .tools .authorize ({
188+ tool_name: arcadeTool .name ,
189+ user_id: " <YOUR_SYSTEM_USER_ID>" ,
190+ });
191+ return ` Please login to Google: ${ response .url } ` ;
192+ }
193+ return " Error executing tool"
194+ }
195+ })
196+ });
200197```
201198</Tabs.Tab >
202199
@@ -260,15 +257,12 @@ To wait for authorization completion, follow this approach:
260257
261258
262259``` javascript
263- const tools = arcadeTools .map (({ name, description, execute, parameters }) =>
264- tool ({
265- name,
266- description: description ?? " " ,
267- parameters: parameters as any,
268- execute,
269- errorFunction: (_ , error ) => { throw error } // Throw the error to the agent for handling
270- }),
271- );
260+ const tools = arcadeTools .map ((arcadeTool ) => {
261+ return tool ({
262+ ... arcadeTool,
263+ errorFunction : (_ , error ) => { throw error } // Throw the error to the agent for handling
264+ })
265+ });
272266
273267while (true ) {
274268 try {
@@ -374,31 +368,21 @@ async function main() {
374368
375369 // 2) Fetch Google toolkit from Arcade and prepare tools for OpenAI Agents
376370 const googleToolkit = await client .tools .list ({ toolkit: " google" , limit: 30 });
377- const arcadeTools = toZod ({
371+ const tools = toZod ({
378372 tools: googleToolkit .items ,
379373 client,
380374 userId: " <YOUR_SYSTEM_USER_ID_2>" , // Replace this with your application's user ID (e.g. email address, UUID, etc.)
381- })
375+ }). map (tool);
382376
383- // 3) Convert the tools to a format that OpenAI Agents can use
384- const tools = arcadeTools .map (({ name, description, execute, parameters }) =>
385- tool ({
386- name,
387- description: description ?? " " ,
388- parameters: parameters as any,
389- execute,
390- errorFunction: (_ , error ) => { throw error }
391- }),
392- );
393-
394- // 4) Create a new agent with the Google toolkit
377+ // 3) Create a new agent with the Google toolkit
395378 const googleAgent = new Agent ({
396379 name: " Google agent" ,
397380 instructions: " You are a helpful assistant that can assist with Google API calls." ,
398381 model: " gpt-4o-mini" ,
399382 tools
400383 });
401384
385+ // 4) Run the agent, if authorization is required, wait for it to complete and retry
402386 while (true ) {
403387 try {
404388 const result = await run (googleAgent, " What are my latest emails?" );
@@ -413,7 +397,7 @@ async function main() {
413397 if (response .status !== " completed" ) {
414398 console .log (` Please complete the authorization challenge in your browser: ${ response .url } ` );
415399 }
416-
400+
417401 // Wait for the authorization to complete
418402 await client .auth .waitForCompletion (response);
419403 console .log (" Authorization completed, retrying..." );
0 commit comments