-
-
Notifications
You must be signed in to change notification settings - Fork 312
feat(ui): add typed headless createUI adapters #1252
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
AlemTuzlak
wants to merge
3
commits into
main
Choose a base branch
from
feat/typed-headless-chat-ui
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,9 @@ | ||
| --- | ||
| '@tanstack/ai-client': minor | ||
| '@tanstack/ai-react-ui': minor | ||
| '@tanstack/ai-solid-ui': minor | ||
| '@tanstack/ai-vue-ui': minor | ||
| '@tanstack/ai-svelte-ui': minor | ||
| --- | ||
|
|
||
| Add typed headless `createUI()` adapters. Chat options control the types of message parts, tools, structured output, and interrupts. Old Chat orchestration stays importable and deprecated until 1.0. |
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
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
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
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,117 @@ | ||
| --- | ||
| title: Migrate to createUI | ||
| id: migrate-create-ui | ||
| order: 5 | ||
| description: "Move chat-state ownership out of the old Chat component and onto createUI with a typed component map." | ||
| keywords: | ||
| - tanstack ai | ||
| - createUI | ||
| - migration | ||
| - deprecation | ||
| --- | ||
|
|
||
| The old `Chat` component owned chat state and lost configured types. `createUI` keeps types from your `chatOptions` and leaves `useChat` in your app. | ||
|
|
||
| This is a semantic migration. There is no codemod. | ||
|
|
||
| ## What changes | ||
|
|
||
| 1. You call `useChat` or `createChat` yourself. | ||
| 2. You supply every visible component. | ||
| 3. Tool inputs stay optional while they stream. | ||
| 4. Tool approvals come from `chat.interrupts`. | ||
| 5. Unknown runtime keys can use a fallback or render nothing. | ||
| 6. `createUI()` must run at module scope so identity stays stable. | ||
|
|
||
| ## Why | ||
|
|
||
| The old APIs drop configured types, keep unused properties, use a deprecated approval path, cover only part of the message protocol, and own chat state. Two orchestration models duplicate fixes. | ||
|
|
||
| ## Minimum versions | ||
|
|
||
| - `@tanstack/ai-react-ui` 0.9.0 | ||
| - `@tanstack/ai-solid-ui` 0.8.0 | ||
| - `@tanstack/ai-vue-ui` 0.3.0 | ||
| - `@tanstack/ai-svelte-ui` 0.1.0 | ||
|
|
||
| Old orchestration exports stay importable until each package's `1.0.0`. `TextPart` and `ThinkingPart` stay supported. | ||
|
|
||
| ## Before | ||
|
|
||
| ```tsx | ||
| import { fetchServerSentEvents } from '@tanstack/ai-react' | ||
| import { Chat, ChatMessages, ChatInput } from '@tanstack/ai-react-ui' | ||
|
|
||
| const connection = fetchServerSentEvents('/api/chat') | ||
|
|
||
| export function OldChat() { | ||
| return ( | ||
| <Chat connection={connection}> | ||
| <ChatMessages /> | ||
| <ChatInput /> | ||
| </Chat> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| ## After | ||
|
|
||
| ```tsx | ||
| import { fetchServerSentEvents, useChat } from '@tanstack/ai-react' | ||
| import { createUI } from '@tanstack/ai-react-ui' | ||
|
|
||
| const chatOptions = { | ||
| connection: fetchServerSentEvents('/api/chat'), | ||
| } | ||
|
|
||
| const UI = createUI(chatOptions) | ||
|
|
||
| const components = UI.defineComponents({ | ||
| layout: ({ renderMessages, renderInput }) => ( | ||
| <main> | ||
| {renderMessages()} | ||
| {renderInput()} | ||
| </main> | ||
| ), | ||
| message: ({ renderParts }) => <article>{renderParts()}</article>, | ||
| input: ({ chat }) => ( | ||
| <form | ||
| onSubmit={(event) => { | ||
| event.preventDefault() | ||
| const field = event.currentTarget.elements.namedItem('message') | ||
| if (!(field instanceof HTMLInputElement)) return | ||
| const text = field.value.trim() | ||
| if (!text) return | ||
| field.value = '' | ||
| void chat.sendMessage?.(text) | ||
| }} | ||
| > | ||
| <input name="message" /> | ||
| </form> | ||
| ), | ||
| parts: { fallback: () => null }, | ||
| }) | ||
|
|
||
| export function NewChat() { | ||
| const chat = useChat(chatOptions) | ||
| return <UI.Chat chat={chat} components={components} /> | ||
| } | ||
| ``` | ||
|
|
||
| ## Steps | ||
|
|
||
| 1. Move `connection`, `tools`, and `interrupts` into a module-level `chatOptions` object. | ||
| 2. Call `createUI(chatOptions)` next to that object. | ||
| 3. Call `useChat(chatOptions)` in the screen component. | ||
| 4. Define `layout`, `message`, `parts`, `tools`, and `interrupts` in `defineComponents`. | ||
| 5. Replace `<Chat>` with `<UI.Chat chat={chat} components={components} />`. | ||
|
|
||
| ## Gotchas | ||
|
|
||
| - A shared `chatOptions` variable does not need `as const`. | ||
| - `{ component, placement: 'inline' }` puts a tool approval in the tool slot. A direct tool interrupt component uses the list. | ||
| - Generic interrupts live under `interrupts.generic`: a registered id such as `choosePlan`, plus `fallback`. Unbound interrupts use `fallback`. | ||
| - Matched `tool-result` parts are hidden in automatic traversal. Unmatched results stay visible. | ||
| - Nested providers use the nearest chat instance. | ||
|
|
||
| See the [React UI guide](../ui/react) for a full map. | ||
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 @@ | ||
| --- | ||
| title: Custom Chat UI Adapters | ||
| id: typed-headless-ui-custom-adapters | ||
| order: 5 | ||
| description: "Build a framework adapter on @tanstack/ai-client/ui. The core is types and selectors only." | ||
| keywords: | ||
| - tanstack ai | ||
| - createUI | ||
| - custom adapter | ||
| - headless ui | ||
| --- | ||
|
|
||
| Import `@tanstack/ai-client/ui`. Do not import it from the main client entry. | ||
|
|
||
| The subpath gives you: | ||
|
|
||
| 1. `selectChatUI` to match tool results and split list vs inline interrupts | ||
| 2. `partTypeToKey` to turn `tool-call` into `toolCall` | ||
| 3. Option types for tools, generic interrupts, and `outputSchema` | ||
|
|
||
| Your adapter owns: | ||
|
|
||
| 1. Native components and context | ||
| 2. Native reactivity | ||
| 3. Render callbacks, slots, or snippets | ||
| 4. Development warnings for missing mapped keys | ||
|
|
||
| Do not add default markup. Do not add a new store. The app owns `useChat` or `createChat`. | ||
|
|
||
| Call `selectChatUI({ messages, interrupts, inlineToolNames })`. Automatic traversal skips a `tool-result` only when `matched` is true. Keep unmatched results. | ||
|
|
||
| Warn once per missing runtime key in development. Each build tool detects development mode differently, so the adapter prints the warning. | ||
|
|
||
| See the [React](./react), [Solid](./solid), [Vue](./vue), and [Svelte](./svelte) adapters for the public names to match: `Chat`, `Provider`, `Messages`, `Message`, `Part`, `Interrupts`, `Interrupt`, and `defineComponents`. |
Oops, something went wrong.
Oops, something went wrong.
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: TanStack/ai
Length of output: 12360
🏁 Script executed:
Repository: TanStack/ai
Length of output: 12606
🏁 Script executed:
Repository: TanStack/ai
Length of output: 37772
🏁 Script executed:
Repository: TanStack/ai
Length of output: 19621
Update
@tanstack/ai-svelte-uito0.2.0.The package is currently
0.1.0, and the changeset marks it for a minor release. Its next version is0.2.0. The1.0.0removal boundary is documented in the React, Solid, and Vue source comments.🤖 Prompt for AI Agents