Skip to content

Commit 4f3e637

Browse files
authored
fix (ui): avoid caching globalThis.fetch in case it is patched by other libraries (#6962)
## Background Libraries might patch `globalThis.fetch`, but we are storing a potentially stale instance in `HttpChatTransport`. ## Summary Fallback to `globalThis.fetch` at runtime when no custom fetch function is provided. ## Verification Test `examples/next-openai`.
1 parent 878bf45 commit 4f3e637

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

.changeset/eighty-planets-drum.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
fix (ui): avoid caching globalThis.fetch in case it is patched by other libraries

packages/ai/src/ui/http-chat-transport.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import { UIMessageStreamPart } from '../ui-message-stream/ui-message-stream-part
33
import { ChatTransport } from './chat-transport';
44
import { UIMessage } from './ui-messages';
55

6-
// use function to allow for mocking in tests:
7-
const getOriginalFetch = () => fetch;
8-
96
export type PrepareSendMessagesRequest<UI_MESSAGE extends UIMessage> = (
107
options: {
118
id: string;
@@ -111,7 +108,7 @@ export abstract class HttpChatTransport<UI_MESSAGE extends UIMessage>
111108
protected credentials?: RequestCredentials;
112109
protected headers?: Record<string, string> | Headers;
113110
protected body?: object;
114-
protected fetch: FetchFunction;
111+
protected fetch?: FetchFunction;
115112
protected prepareSendMessagesRequest?: PrepareSendMessagesRequest<UI_MESSAGE>;
116113
protected prepareReconnectToStreamRequest?: PrepareReconnectToStreamRequest;
117114

@@ -120,7 +117,7 @@ export abstract class HttpChatTransport<UI_MESSAGE extends UIMessage>
120117
credentials,
121118
headers,
122119
body,
123-
fetch = getOriginalFetch(),
120+
fetch,
124121
prepareSendMessagesRequest,
125122
prepareReconnectToStreamRequest,
126123
}: HttpChatTransportInitOptions<UI_MESSAGE>) {
@@ -167,7 +164,10 @@ export abstract class HttpChatTransport<UI_MESSAGE extends UIMessage>
167164
};
168165
const credentials = preparedRequest?.credentials ?? this.credentials;
169166

170-
const response = await this.fetch.call(undefined, api, {
167+
// avoid caching globalThis.fetch in case it is patched by other libraries
168+
const fetch = this.fetch ?? globalThis.fetch;
169+
170+
const response = await fetch(api, {
171171
method: 'POST',
172172
headers: {
173173
'Content-Type': 'application/json',
@@ -210,7 +210,10 @@ export abstract class HttpChatTransport<UI_MESSAGE extends UIMessage>
210210
: { ...this.headers, ...options.headers };
211211
const credentials = preparedRequest?.credentials ?? this.credentials;
212212

213-
const response = await this.fetch.call(undefined, api, {
213+
// avoid caching globalThis.fetch in case it is patched by other libraries
214+
const fetch = this.fetch ?? globalThis.fetch;
215+
216+
const response = await fetch(api, {
214217
method: 'GET',
215218
headers,
216219
credentials,

0 commit comments

Comments
 (0)