Skip to main content
The Agent Platform JavaScript SDK (AgP JS SDK) makes it easy to manage multiple AI-driven tasks simultaneously. This is often useful in scenarios where you need to gather varied information quickly. Instead of running each task one by one, the AgP JS SDK lets you launch and monitor several objectives in parallel, streamlining your workflow and saving valuable time. In this example, we’ll explore how to run multiple tasks at once using the agent.runBatch() and agent.waitForAllComplete() methods. You’ll see how the SDK handles concurrent execution, tracks progress in real time, and reports results — all with minimal code.

Run multiple tasks

Situation: I want to run multiple tasks at once, finding important information for an upcoming trip to New York, like checking the weather and finding places to visit. Manually handling each task would be slow and tedious. Problem: How can I run several tasks in parallel, track their progress, and view the results without writing repetitive code for each task? Solution: The AgP JS SDK provides agent.runBatch() and agent.waitForAllComplete() to simplify and automate this workflow. You can define multiple objectives, run them simultaneously, and monitor their progress using events for status, chat messages, web actions, and errors. This allows you to efficiently manage and automate multiple tasks at the same time.

Step 1: Initialize the SDK

Start by initializing the SDK and setting up authentication via Portal-H by adding the following:
<button id="loginBtn" style="display:none;">Login to Portal-H</button>

<script src="https://unpkg.com/agp-js-sdk"></script>
<script>
(async () => {
  const agent = await AGP.WebAgent.init({
    authCallback: (login) => {
      const btn = document.getElementById('loginBtn');
      btn.style.display = 'block';
      btn.onclick = login;
    }
  });

Step 2: Wait for completion

To be notified in real time when your task has finished executing, add the following code:
await task.waitForCompletion();

Step 3: View task results

Add this code to monitor each task’s progress, messages, web actions, and errors in real time.
  tasks.forEach((task) => {
    task.onStatusChange((status) => {
      console.log(`Task ${task.id} status:`, status);
    });

    task.onChatMessage((message) => {
      console.log(`Task ${task.id} message:`, message.data.content);
    });

    task.onWebAction((action) => {
      console.log(`Task ${task.id} action:`, action.data.action.action_type);
    });

    task.onError((error) => {
      console.error(`Task ${task.id} error:`, error);

Step 4: Put it all together and run

Here’s a complete example that includes all previous steps, assembled and ready to run:
Browser example
<button id="loginBtn" style="display:none;">Login to Portal-H</button>

<script src="https://unpkg.com/agp-js-sdk"></script>
<script>
(async () => {
  const agent = await AGP.WebAgent.init({
    authCallback: (login) => {
      const btn = document.getElementById('loginBtn');
      btn.style.display = 'block';
      btn.onclick = login;
    }
  });

  // Step 1: Configure the tasks
  const tasks = await agent.runBatch([
    { objective: 'Check weather for New York', startUrl: 'https://weather.com' },
    { objective: 'Look up places to visit in New York', startUrl: 'www.google.com' }
  ]);

  // Step 2: Wait for completion
  await agent.waitForAllComplete(tasks);
  console.log('All tasks completed!');

  // Step 3: View task results (Optional)
  tasks.forEach((task) => {
    task.onStatusChange((status) => {
      console.log(`Task ${task.id} status:`, status);
    });

    task.onChatMessage((message) => {
      console.log(`Task ${task.id} message:`, message.data.content);
    });

    task.onWebAction((action) => {
      console.log(`Task ${task.id} action:`, action.data.action.action_type);
    });

    task.onError((error) => {
      console.error(`Task ${task.id} error:`, error);
    });
  });
})();
</script>

Outcome

The AgP JS SDK, running the above code asyncronously, will follow a sequence that:
  • Initializes the SDK and authenticates through Portal-H.
  • Provides real-time updates.
  • Generates an accurate and detailed response, for example:
Current Weather for New York City, NY
Temperature: 58°F
Condition: Cloudy/Wind
Feels Like: 54°F
High / Low: Day 59°F / Night 57°F
Wind: 20 mph
Humidity: 96%
Dew Point: 57°F
Pressure: 29.92 in
UV Index: 0 of 11
Visibility: 6 mi
Moon Phase: Last Quarter
Gale Warning is in effect for the area.
Time of report: 6:13 pm EDT
Source: The Weather Channel
I