Overview

Before a response to /admin/api/canvas/ai is received, the polling begins. Once I sent a chat I saw two /admin/api/canvas/ai-progress?request_id=req_1760990054450_npzc3ipyu calls before the response for the prompt came back.

This seems to be working because AI Agents sets the state pretty quickly, but a race condition could occur where the polling finishes first and sees an empty status and bails.

Proposed resolution

Wait to poll until the response is returned verifying the request went through

User interface changes

Comments

mglaman created an issue. See original summary.

narendrar’s picture

Component: … to be triaged » AI

This polling behaviour is intentional. Polling begins as soon as a chat request is sent to /admin/api/canvas/ai, in order to track the request status and retrieve information as soon as it's available. Currently, maxPolls is set to 500 (about 16 minutes), and we can make this configurable in the future.

kunal.sachdev’s picture

The main request (/admin/api/canvas/ai) is the long-running request which sends the prompt and waits for the final AI response where as the Polling request (/admin/api/canvas/ai-progress) is the short, repeating request providing the live UI updates while the main request is busy. So, I think the suggestion to "Wait to poll until the response is returned" won't work because if we waited for the main request to fully return before polling, the polling would never happen while the user is waiting, and we'd lose the live status updates entirely.

However I noticed one thing that the code currently accounts for this race condition by delaying the very first poll by 1 second in the startPolling function (around line 427 in AiWizard.tsx):

  setTimeout(poll, 1000);
};

This 1-second delay is intended to give the main fetch request a head-start to reach the server and register the job. But It's possible 1 second isn't always enough, so we can certainly adjust this if it becomes a persistent problem.

rakhimandhania’s picture

rakhimandhania’s picture