-
-
Notifications
You must be signed in to change notification settings - Fork 238
fix(ai-client): add missing methods to the no-op chat devtools bridge #752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jan-kubica
wants to merge
2
commits into
TanStack:main
Choose a base branch
from
jan-kubica:fix/noop-chat-devtools-bridge-parity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/ai-client': patch | ||
| --- | ||
|
|
||
| Fix `ChatClient` throwing `TypeError: this.devtoolsBridge.mountWithTools is not a function` on the first `sendMessage()` (and on `updateOptions({ tools })`) when no devtools bridge factory is supplied. The default `NoOpChatDevtoolsBridge` was missing the `mountWithTools`, `notifyToolsChanged`, and `recordStreamId` methods of the real bridge; the throw happened before the user message was appended, so the first message was silently lost. The compile-time parity check between the real and no-op bridges now fails the build when the surfaces drift. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/ai-client/tests/chat-client-noop-devtools.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Regression coverage for the shipping default. The suite-wide setup file | ||
| // (`use-real-devtools-bridges.ts`) re-routes the no-op devtools factories to | ||
| // the real bridges, so no other test exercises the bridge production | ||
| // consumers actually get when they don't opt into devtools. Unmock here so | ||
| // `ChatClient` runs against the actual no-op bridge that ships as the | ||
| // default, instead of the real-bridge substitute the setup file installs. | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { ChatClient } from '../src/chat-client' | ||
| import { createMockConnectionAdapter, createTextChunks } from './test-utils' | ||
|
|
||
| vi.unmock('../src/devtools-noop') | ||
|
|
||
| describe('ChatClient with default no-op devtools bridge', () => { | ||
| it('sends the first message and appends it', async () => { | ||
| const adapter = createMockConnectionAdapter({ | ||
| chunks: createTextChunks('Hi there'), | ||
| }) | ||
| const client = new ChatClient({ connection: adapter }) | ||
|
|
||
| await client.sendMessage('hello') | ||
|
|
||
| const messages = client.getMessages() | ||
| expect(messages.at(0)?.role).toBe('user') | ||
| expect(messages.at(0)?.parts).toEqual([{ type: 'text', content: 'hello' }]) | ||
| expect(messages.at(1)?.role).toBe('assistant') | ||
| }) | ||
|
|
||
| it('updates tools without throwing', () => { | ||
| const adapter = createMockConnectionAdapter() | ||
| const client = new ChatClient({ connection: adapter }) | ||
|
|
||
| expect(() => client.updateOptions({ tools: [] })).not.toThrow() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { useEffect, useState } from 'react' | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
| import { ChatClient } from '@tanstack/ai-client' | ||
| import type { UIMessage } from '@tanstack/ai-client' | ||
|
|
||
| export const Route = createFileRoute('/chat-client-default-bridge')({ | ||
| component: ChatClientDefaultBridgePage, | ||
| }) | ||
|
|
||
| // Covers the vanilla `ChatClient` shipping default: no `devtoolsBridgeFactory`, | ||
| // so the client falls back to the no-op devtools bridge. The framework hooks | ||
| // (`useChat` etc.) always inject the real bridge, so every other route in this | ||
| // suite bypasses the no-op path entirely. A static SSE body keeps the scenario | ||
| // deterministic; the transport is not what is under test here. | ||
| const SSE_BODY = [ | ||
| 'data: {"type":"RUN_STARTED","threadId":"thread-default-bridge","runId":"run-default-bridge"}\n\n', | ||
| 'data: {"type":"TEXT_MESSAGE_CONTENT","messageId":"msg-default-bridge","model":"test","timestamp":0,"delta":"Hi from the assistant","content":"Hi from the assistant"}\n\n', | ||
| 'data: {"type":"RUN_FINISHED","threadId":"thread-default-bridge","runId":"run-default-bridge","model":"test","timestamp":0,"finishReason":"stop"}\n\n', | ||
| ].join('') | ||
|
|
||
| function ChatClientDefaultBridgePage() { | ||
| const [messages, setMessages] = useState<Array<UIMessage>>([]) | ||
| const [error, setError] = useState<string | null>(null) | ||
| const [client] = useState( | ||
| () => | ||
| new ChatClient({ | ||
| fetcher: () => | ||
| new Response(SSE_BODY, { | ||
| headers: { 'Content-Type': 'text/event-stream' }, | ||
| }), | ||
| onMessagesChange: setMessages, | ||
| }), | ||
| ) | ||
|
|
||
| // The button starts disabled in the server-rendered HTML and enables on | ||
| // hydration, so Playwright's actionability check cannot click before the | ||
| // onClick handler is attached. | ||
| const [hydrated, setHydrated] = useState(false) | ||
| useEffect(() => { | ||
| setHydrated(true) | ||
| }, []) | ||
|
|
||
| const handleSend = () => { | ||
| client | ||
| .sendMessage('hello from the vanilla client') | ||
| .catch((sendError: unknown) => setError(String(sendError))) | ||
| } | ||
|
|
||
| return ( | ||
| <div className="p-6 max-w-2xl mx-auto space-y-4"> | ||
| <h1 className="text-xl font-semibold"> | ||
| Vanilla ChatClient (default no-op devtools bridge) | ||
| </h1> | ||
| <button | ||
| data-testid="send-button" | ||
| type="button" | ||
| disabled={!hydrated} | ||
| onClick={handleSend} | ||
| > | ||
| Send | ||
| </button> | ||
| {error !== null && <div data-testid="send-error">{error}</div>} | ||
| <div data-testid="messages"> | ||
| {messages.map((message) => ( | ||
| <div | ||
| key={message.id} | ||
| data-testid={ | ||
| message.role === 'user' ? 'user-message' : 'assistant-message' | ||
| } | ||
| > | ||
| {message.parts | ||
| .map((part) => (part.type === 'text' ? part.content : '')) | ||
| .join('')} | ||
| </div> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { expect, test } from '@playwright/test' | ||
|
|
||
| // The framework hooks always pass the real devtools bridge factory, so this | ||
| // is the only scenario in the suite that exercises the no-op bridge that | ||
| // vanilla `ChatClient` consumers get by default. | ||
| test.describe('vanilla ChatClient with default no-op devtools bridge', () => { | ||
| test('first sendMessage appends the user message and streams a reply', async ({ | ||
| page, | ||
| }) => { | ||
| await page.goto('/chat-client-default-bridge') | ||
|
|
||
| await page.getByTestId('send-button').click() | ||
|
|
||
| await expect(page.getByTestId('user-message')).toHaveText( | ||
| 'hello from the vanilla client', | ||
| ) | ||
| await expect(page.getByTestId('assistant-message')).toContainText( | ||
| 'Hi from the assistant', | ||
| ) | ||
| await expect(page.getByTestId('send-error')).toHaveCount(0) | ||
| }) | ||
| }) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document the aimock policy exception in the header comment.
Based on learnings, E2E specs that don't reach the provider HTTP layer should document why aimock is not used. This test uses a mock fetcher (static SSE Response in the route file) that never reaches the provider HTTP layer, qualifying as an exception.
π Suggested header comment expansion
π Committable suggestion
π€ Prompt for AI Agents
Source: Learnings