Skip to content

fix: prevent duplicate AI agent invocation on first interaction - #3267

Merged
Iru89 merged 5 commits into
masterfrom
fix/first-interaction-duplicate-ai-agent
Sep 3, 2026
Merged

fix: prevent duplicate AI agent invocation on first interaction#3267
Iru89 merged 5 commits into
masterfrom
fix/first-interaction-duplicate-ai-agent

Conversation

@Iru89

@Iru89 Iru89 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Description

On the first interaction, Flow Builder no longer appends a second AI Agent resolution when the start contents already handle the user input (a Bot Action as the last node, or an AI Agent / AI Agent Router anywhere in the start chain). The user message is still processed once by the agent already reached from the start flow.

Context

When session.is_first_interaction is true, getContentsByFirstInteraction always resolved the Main start contents and then, if the user sent text, concatenated getContentsByAiAgentFromUserInput.

A common layout is: Main start node → go-to-flow → AI Agents. getStartContents() already follows that go-to-flow and includes the AI Agent. Concatenating the user-input AI Agent path invoked inference twice and returned two FlowAiAgent contents with the same greeting, so WhatsApp (and other channels) showed two identical messages.

This only happened on the first interaction. Later turns use getContents() without that concatenation, so the agent answered once as expected.

The same first-interaction path already skipped extra matching when the last start content was a Bot Action. AI Agent / AI Agent Router were not covered.

Approach taken / Explain the design

startContentsAlreadyHandleUserInput decides whether start contents already consume the user message:

  • Bot Action: only if it is the last content (existing behaviour).
  • AI Agent or AI Agent Router: if either appears anywhere in the start contents (the go-to-flow may not leave the agent as the last item in the array).

If that helper returns true, first interaction returns only the start contents. FlowBuilderAction.prepareContentsToRender still runs inference once on the agent already in that list, using the current user input.

If the start chain is only welcome text (no agent), behaviour is unchanged: keywords / intents / AI Agent from user input are still appended after the start contents.

To document / Usage example

No public API change. Bots whose Main start goes to the AI Agents flow no longer need a workaround on first interaction.

No extra configuration is required. disableAIAgentInFirstInteraction still disables the appended user-input AI Agent path when start contents do not already include an agent.

Testing

The pull request...

  • has unit tests
  • has integration tests
  • doesn't need tests because... [provide a description]

Added a first-interaction test with Main start = go-to-flow to AI Agents and user text hola: getAiAgentResponse is called once and contents contain a single FlowAiAgent. Existing first-interaction tests (welcome + agent, disableAIAgentInFirstInteraction) still pass.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@Iru89
Iru89 merged commit d15d0d9 into master Sep 3, 2026
6 checks passed
@Iru89
Iru89 deleted the fix/first-interaction-duplicate-ai-agent branch September 3, 2026 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants