Skip to content

fix: invoke onRequest callback for streaming prediction requests#362

Open
mmattu-wd wants to merge 2 commits intomainfrom
bug/fix-on-request-not-used-for-streaming
Open

fix: invoke onRequest callback for streaming prediction requests#362
mmattu-wd wants to merge 2 commits intomainfrom
bug/fix-on-request-not-used-for-streaming

Conversation

@mmattu-wd
Copy link
Copy Markdown
Contributor

@mmattu-wd mmattu-wd commented Apr 3, 2026

Issue

The onRequest callback prop ((request: RequestInit) => Promise<void>) is not called when sending streaming prediction requests.

This breaks any consumer relying on it to inject auth headers, set credentials: 'include', or mutate the request before it is sent.

Root cause:
The streaming path in fetchResponseFromEventStream calls fetchEventSource from @microsoft/fetch-event-source directly, and never invokes onRequest. All other request paths (config fetch, stream availability check, non-streaming prediction, TTS) go through sendRequest() in src/utils/index.ts, which correctly calls onRequest(requestInfo) before each fetch().

This means GET requests like getChatbotConfig and isStreamAvailableQuery receive injected headers/credentials, but the streaming POST to /api/v1/prediction/{chatflowid} does not — causing auth failures for users who authenticate via cookies or bearer tokens.

Resolution

fetchEventSource accepts a fetch parameter (FetchEventSourceInit.fetch?: typeof fetch) that replaces its underlying fetch implementation. A customFetch wrapper is defined inside fetchResponseFromEventStream that:

  1. Calls props.onRequest(init) if the callback is provided — giving the consumer a chance to mutate the RequestInit (add headers, set credentials, etc.)
  2. Delegates to native fetch with the (potentially mutated) init object

This mirrors the existing pattern in sendRequest() and generateTTSQuery(), and requires no changes to any other file.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a customFetch wrapper in the Bot component to allow for request interception via an onRequest prop. A critical syntax error was identified in the fetchEventSource URL template literal, where extra quotes and a closing brace were accidentally included, which will lead to malformed request URLs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant