messages parameter to fetchServerSentEvents function#521
messages parameter to fetchServerSentEvents function#521tornado-softwares wants to merge 1 commit intoTanStack:mainfrom
Conversation
📝 WalkthroughWalkthroughThe ChangesOptions Function Resolution Update
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
packages/typescript/ai-client/src/connection-adapters.ts (2)
264-275: ⚡ Quick winNo test asserts that
messagesis actually forwarded to the options callback.The existing test suite (Context snippet 1) covers the zero-arg form but never verifies that the messages array is passed correctly to the new signature. A targeted test — asserting the callback receives the expected messages — would guard against regressions (e.g., a future refactor accidentally reverting to
options()).🧪 Suggested test
it('should pass messages to dynamic options function', async () => { fetchMock.mockResolvedValue(mockResponse as any) const receivedMessages: Array<any> = [] const adapter = fetchServerSentEvents('/api/chat', (messages) => { receivedMessages.push(...messages) return { headers: { 'X-Custom': 'dynamic' } } }) const inputMessages = [{ role: 'user', content: 'Hello' }] for await (const _ of adapter.connect(inputMessages as any)) { // Consume } expect(receivedMessages).toEqual(inputMessages) })🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/typescript/ai-client/src/connection-adapters.ts` around lines 264 - 275, Add a unit test that verifies fetchServerSentEvents forwards the messages array to a dynamic options callback: create a test that calls fetchServerSentEvents('/api/chat', (messages) => { record messages; return options }) then invoke the adapter.connect generator with a known inputMessages array and iterate it to completion, finally asserting the recorded messages equal inputMessages; target the fetchServerSentEvents function and its returned connect generator to ensure the callback receives the messages argument.
368-379: ⚡ Quick win
fetchHttpStreamhas the same pattern but wasn't updated — API inconsistency.
fetchHttpStreamis structurally identical tofetchServerSentEventsand themessages-aware options callback is equally useful there (same PR motivation applies). Leaving it at() => FetchConnectionOptionscreates a confusing asymmetry where the same pattern works differently across the two adapters.♻️ Proposed parity fix for `fetchHttpStream`
export function fetchHttpStream( url: string | (() => string), options: | FetchConnectionOptions - | (() => FetchConnectionOptions | Promise<FetchConnectionOptions>) = {}, + | ((messages: Array<UIMessage> | Array<ModelMessage>) => FetchConnectionOptions | Promise<FetchConnectionOptions>) = {}, ): ConnectConnectionAdapter { return { async *connect(messages, data, abortSignal) { const resolvedUrl = typeof url === 'function' ? url() : url const resolvedOptions = - typeof options === 'function' ? await options() : options + typeof options === 'function' ? await options(messages) : options🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/typescript/ai-client/src/connection-adapters.ts` around lines 368 - 379, The fetchHttpStream adapter currently resolves options with a parameterless callback, causing API inconsistency with fetchServerSentEvents; change the options callback signature so FetchConnectionOptions can be returned from a function that receives the messages iterator (i.e., from () => FetchConnectionOptions to (messages) => FetchConnectionOptions | Promise<FetchConnectionOptions>), and when resolving options inside fetchHttpStream (function fetchHttpStream / method connect) call await options(messages) instead of await options(); update any related types/overloads for FetchConnectionOptions to accept the messages parameter so the adapter matches the other messages-aware connection adapters.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/typescript/ai-client/src/connection-adapters.ts`:
- Around line 264-275: Add a unit test that verifies fetchServerSentEvents
forwards the messages array to a dynamic options callback: create a test that
calls fetchServerSentEvents('/api/chat', (messages) => { record messages; return
options }) then invoke the adapter.connect generator with a known inputMessages
array and iterate it to completion, finally asserting the recorded messages
equal inputMessages; target the fetchServerSentEvents function and its returned
connect generator to ensure the callback receives the messages argument.
- Around line 368-379: The fetchHttpStream adapter currently resolves options
with a parameterless callback, causing API inconsistency with
fetchServerSentEvents; change the options callback signature so
FetchConnectionOptions can be returned from a function that receives the
messages iterator (i.e., from () => FetchConnectionOptions to (messages) =>
FetchConnectionOptions | Promise<FetchConnectionOptions>), and when resolving
options inside fetchHttpStream (function fetchHttpStream / method connect) call
await options(messages) instead of await options(); update any related
types/overloads for FetchConnectionOptions to accept the messages parameter so
the adapter matches the other messages-aware connection adapters.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: af930c5a-6652-4a9f-9892-238282243658
📒 Files selected for processing (1)
packages/typescript/ai-client/src/connection-adapters.ts
Hi,
I added a
messagesparameter to thefetchServerSentEventsconnection adapter.You might ask why. It's simple: Transtack AI provides all the tools to create an AI chat, for example, but we still manage our backend as we see fit in a scenario where my chats and messages are saved in a database that I can quickly access.
I don't need the frontend to send me ALL the messages again. The culprit is the React
useChathook from@tanstack/ai-react, and it doesn't directly allow sending only new messages to the backend whensendMessage()function is called.So I figured the simplest way to implement this would be with a few lines in the connection adapter. I can therefore create something that solves my problem and looks like this:
This pull request doesn't introduce any breaking changes. And I think it could be relevant for certain use cases like mine.
But maybe I'm missing something ? Is there a cleaner, more appropriate way to do this without adding this hacky parameter ? Perhaps by using
@tanstack/ai-client? Anyway, that's how I solved my problem. I'd really like to know if anyone else has had the same question as me and if there's another way to do it.Thanks.
Summary by CodeRabbit