@tanstack/ai-remix 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +11 -0
- package/package.json +71 -0
- package/src/chat-ui/chat-input.tsx +125 -0
- package/src/chat-ui/chat-message.tsx +197 -0
- package/src/chat-ui/chat-messages.tsx +67 -0
- package/src/chat-ui/chat.tsx +48 -0
- package/src/chat-ui/create-chat-hook.ts +66 -0
- package/src/chat-ui/create-ui.tsx +516 -0
- package/src/chat-ui/text-part.tsx +29 -0
- package/src/chat-ui/thinking-part.tsx +44 -0
- package/src/chat-ui/tool-approval.tsx +94 -0
- package/src/create-audio-recorder.ts +106 -0
- package/src/create-byok.ts +29 -0
- package/src/create-chat.ts +421 -0
- package/src/create-generate-audio.ts +93 -0
- package/src/create-generate-image.ts +94 -0
- package/src/create-generate-speech.ts +93 -0
- package/src/create-generate-video.ts +347 -0
- package/src/create-generation.ts +333 -0
- package/src/create-mcp-app-bridge.ts +36 -0
- package/src/create-realtime-chat.ts +242 -0
- package/src/create-summarize.ts +96 -0
- package/src/create-transcription.ts +105 -0
- package/src/index.ts +92 -0
- package/src/realtime-types.ts +150 -0
- package/src/types.ts +313 -0
- package/src/ui.ts +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Tanner Linsley
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# @tanstack/ai-remix
|
|
2
|
+
|
|
3
|
+
Remix 3 helpers for TanStack AI streaming chat.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @tanstack/ai-remix @tanstack/ai @tanstack/ai-client remix
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
This package publishes uncompiled source. Remix compiles JSX through `jsxImportSource` `remix/ui`.
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tanstack/ai-remix",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Remix 3 bindings for TanStack AI streaming chat, structured outputs, and media generation.",
|
|
5
|
+
"author": "Tanner Linsley",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://tanstack.com/ai",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/TanStack/ai.git",
|
|
11
|
+
"directory": "packages/ai-remix"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/TanStack/ai/issues"
|
|
15
|
+
},
|
|
16
|
+
"funding": {
|
|
17
|
+
"type": "github",
|
|
18
|
+
"url": "https://github.com/sponsors/tannerlinsley"
|
|
19
|
+
},
|
|
20
|
+
"type": "module",
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=22"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"ai",
|
|
26
|
+
"ai-sdk",
|
|
27
|
+
"typescript",
|
|
28
|
+
"tanstack",
|
|
29
|
+
"remix",
|
|
30
|
+
"chat",
|
|
31
|
+
"streaming",
|
|
32
|
+
"tool-calling",
|
|
33
|
+
"structured-outputs",
|
|
34
|
+
"media-generation"
|
|
35
|
+
],
|
|
36
|
+
"//": "This package publishes uncompiled source (.ts / .tsx). Remix compiles JSX via jsxImportSource remix/ui. There is therefore no build/dist and no publint test:build target.",
|
|
37
|
+
"main": "./src/index.ts",
|
|
38
|
+
"module": "./src/index.ts",
|
|
39
|
+
"types": "./src/index.ts",
|
|
40
|
+
"exports": {
|
|
41
|
+
".": "./src/index.ts",
|
|
42
|
+
"./ui": "./src/ui.ts"
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"src",
|
|
46
|
+
"README.md"
|
|
47
|
+
],
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@tanstack/ai-client": "^0.30.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"remix": "^3.0.0-rc.1",
|
|
53
|
+
"@tanstack/ai": "^0.52.1"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@types/node": "^24.10.1",
|
|
57
|
+
"@vitest/coverage-v8": "4.1.10",
|
|
58
|
+
"happy-dom": "^20.11.2",
|
|
59
|
+
"remix": "^3.0.0-rc.1",
|
|
60
|
+
"vite": "^8.2.1",
|
|
61
|
+
"vitest": "^4.1.10",
|
|
62
|
+
"@tanstack/ai": "0.52.1"
|
|
63
|
+
},
|
|
64
|
+
"scripts": {
|
|
65
|
+
"lint:fix": "oxlint src --type-aware --fix",
|
|
66
|
+
"test:oxlint": "oxlint src --type-aware",
|
|
67
|
+
"test:lib": "vitest run",
|
|
68
|
+
"test:lib:dev": "pnpm test:lib --watch",
|
|
69
|
+
"test:types": "tsc"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { on } from 'remix/ui'
|
|
2
|
+
import type { Handle, RemixNode } from 'remix/ui'
|
|
3
|
+
import { useChatContext } from './chat.tsx'
|
|
4
|
+
|
|
5
|
+
export interface ChatInputRenderProps {
|
|
6
|
+
value: string
|
|
7
|
+
onChange: (value: string) => void
|
|
8
|
+
onSubmit: () => void
|
|
9
|
+
isLoading: boolean
|
|
10
|
+
disabled: boolean
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** @deprecated Use `createChatUI()` and an application-owned input component. Removed in 1.0.0. */
|
|
14
|
+
export interface ChatInputProps {
|
|
15
|
+
children?: (props: ChatInputRenderProps) => RemixNode
|
|
16
|
+
class?: string
|
|
17
|
+
placeholder?: string
|
|
18
|
+
disabled?: boolean
|
|
19
|
+
submitOnEnter?: boolean
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated Use `createChatUI()` and an application-owned input component.
|
|
24
|
+
* Removed in 1.0.0.
|
|
25
|
+
*/
|
|
26
|
+
export function ChatInput(handle: Handle<ChatInputProps>) {
|
|
27
|
+
let value = ''
|
|
28
|
+
|
|
29
|
+
return () => {
|
|
30
|
+
const { sendMessage, isLoading } = useChatContext(handle)
|
|
31
|
+
const disabled = Boolean(handle.props.disabled || isLoading)
|
|
32
|
+
const submitOnEnter = handle.props.submitOnEnter !== false
|
|
33
|
+
|
|
34
|
+
function onChange(next: string) {
|
|
35
|
+
value = next
|
|
36
|
+
void handle.update()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function onSubmit() {
|
|
40
|
+
if (!value.trim() || disabled) return
|
|
41
|
+
void sendMessage(value)
|
|
42
|
+
value = ''
|
|
43
|
+
void handle.update()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const renderProps: ChatInputRenderProps = {
|
|
47
|
+
value,
|
|
48
|
+
onChange,
|
|
49
|
+
onSubmit,
|
|
50
|
+
isLoading,
|
|
51
|
+
disabled,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (typeof handle.props.children === 'function') {
|
|
55
|
+
return handle.props.children(renderProps)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<div
|
|
60
|
+
class={handle.props.class}
|
|
61
|
+
data-chat-input
|
|
62
|
+
style={{
|
|
63
|
+
display: 'flex',
|
|
64
|
+
gap: '0.75rem',
|
|
65
|
+
alignItems: 'center',
|
|
66
|
+
width: '100%',
|
|
67
|
+
}}
|
|
68
|
+
>
|
|
69
|
+
<input
|
|
70
|
+
data-chat-textarea
|
|
71
|
+
disabled={disabled}
|
|
72
|
+
placeholder={handle.props.placeholder ?? 'Type a message...'}
|
|
73
|
+
type="text"
|
|
74
|
+
value={value}
|
|
75
|
+
mix={[
|
|
76
|
+
on('input', (event) => {
|
|
77
|
+
onChange((event.currentTarget as HTMLInputElement).value)
|
|
78
|
+
}),
|
|
79
|
+
on('keydown', (event) => {
|
|
80
|
+
if (
|
|
81
|
+
submitOnEnter &&
|
|
82
|
+
event.key === 'Enter' &&
|
|
83
|
+
!event.isComposing
|
|
84
|
+
) {
|
|
85
|
+
event.preventDefault()
|
|
86
|
+
onSubmit()
|
|
87
|
+
}
|
|
88
|
+
}),
|
|
89
|
+
]}
|
|
90
|
+
style={{
|
|
91
|
+
flex: '1',
|
|
92
|
+
padding: '0.75rem 1rem',
|
|
93
|
+
fontSize: '0.875rem',
|
|
94
|
+
border: '1px solid rgba(255, 255, 255, 0.1)',
|
|
95
|
+
borderRadius: '0.75rem',
|
|
96
|
+
backgroundColor: 'rgba(31, 41, 55, 0.5)',
|
|
97
|
+
color: 'white',
|
|
98
|
+
outline: 'none',
|
|
99
|
+
}}
|
|
100
|
+
/>
|
|
101
|
+
<button
|
|
102
|
+
data-chat-submit
|
|
103
|
+
disabled={disabled || !value.trim()}
|
|
104
|
+
mix={[on('click', onSubmit)]}
|
|
105
|
+
style={{
|
|
106
|
+
padding: '0.75rem 1.5rem',
|
|
107
|
+
fontSize: '0.875rem',
|
|
108
|
+
fontWeight: 500,
|
|
109
|
+
color: 'white',
|
|
110
|
+
backgroundColor:
|
|
111
|
+
disabled || !value.trim()
|
|
112
|
+
? 'rgba(107, 114, 128, 0.5)'
|
|
113
|
+
: 'rgb(249, 115, 22)',
|
|
114
|
+
border: 'none',
|
|
115
|
+
borderRadius: '0.75rem',
|
|
116
|
+
cursor: disabled || !value.trim() ? 'not-allowed' : 'pointer',
|
|
117
|
+
whiteSpace: 'nowrap',
|
|
118
|
+
}}
|
|
119
|
+
>
|
|
120
|
+
{isLoading ? 'Sending...' : 'Send'}
|
|
121
|
+
</button>
|
|
122
|
+
</div>
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import type { Handle, RemixNode } from 'remix/ui'
|
|
2
|
+
import { ThinkingPart } from './thinking-part.tsx'
|
|
3
|
+
import type { UIMessage } from '../types.ts'
|
|
4
|
+
|
|
5
|
+
export interface ToolCallRenderProps {
|
|
6
|
+
id: string
|
|
7
|
+
name: string
|
|
8
|
+
arguments: string
|
|
9
|
+
state: string
|
|
10
|
+
approval?: {
|
|
11
|
+
id: string
|
|
12
|
+
needsApproval: boolean
|
|
13
|
+
approved?: boolean
|
|
14
|
+
}
|
|
15
|
+
output?: unknown
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** @deprecated Use `createChatUI()` Message instead. Removed in 1.0.0. */
|
|
19
|
+
export interface ChatMessageProps {
|
|
20
|
+
message: UIMessage
|
|
21
|
+
class?: string
|
|
22
|
+
userClass?: string
|
|
23
|
+
assistantClass?: string
|
|
24
|
+
textPartRenderer?: (props: { content: string }) => RemixNode
|
|
25
|
+
thinkingPartRenderer?: (props: {
|
|
26
|
+
content: string
|
|
27
|
+
isComplete?: boolean
|
|
28
|
+
}) => RemixNode
|
|
29
|
+
toolsRenderer?: Record<string, (props: ToolCallRenderProps) => RemixNode>
|
|
30
|
+
defaultToolRenderer?: (props: ToolCallRenderProps) => RemixNode
|
|
31
|
+
toolResultRenderer?: (props: {
|
|
32
|
+
toolCallId: string
|
|
33
|
+
content: string
|
|
34
|
+
state: string
|
|
35
|
+
}) => RemixNode
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function toolResultContentToString(
|
|
39
|
+
content: string | Array<{ type: string; content?: string }>,
|
|
40
|
+
): string {
|
|
41
|
+
if (typeof content === 'string') return content
|
|
42
|
+
return content
|
|
43
|
+
.filter((part) => part.type === 'text')
|
|
44
|
+
.map((part) => part.content ?? '')
|
|
45
|
+
.join('')
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** @deprecated Use `createChatUI()` Message instead. Removed in 1.0.0. */
|
|
49
|
+
export function ChatMessage(handle: Handle<ChatMessageProps>) {
|
|
50
|
+
return () => {
|
|
51
|
+
const message = handle.props.message
|
|
52
|
+
const roleClass =
|
|
53
|
+
message.role === 'user'
|
|
54
|
+
? handle.props.userClass
|
|
55
|
+
: handle.props.assistantClass
|
|
56
|
+
const combinedClass = [handle.props.class, roleClass]
|
|
57
|
+
.filter(Boolean)
|
|
58
|
+
.join(' ')
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<div
|
|
62
|
+
class={combinedClass || undefined}
|
|
63
|
+
data-message-id={message.id}
|
|
64
|
+
data-message-role={message.role}
|
|
65
|
+
data-message-created={message.createdAt?.toISOString()}
|
|
66
|
+
>
|
|
67
|
+
{message.parts.map((part, index) => (
|
|
68
|
+
<MessagePart
|
|
69
|
+
key={`${message.id}-part-${index}`}
|
|
70
|
+
defaultToolRenderer={handle.props.defaultToolRenderer}
|
|
71
|
+
isThinkingComplete={
|
|
72
|
+
part.type === 'thinking' &&
|
|
73
|
+
message.parts.slice(index + 1).some((p) => p.type === 'text')
|
|
74
|
+
}
|
|
75
|
+
part={part}
|
|
76
|
+
textPartRenderer={handle.props.textPartRenderer}
|
|
77
|
+
thinkingPartRenderer={handle.props.thinkingPartRenderer}
|
|
78
|
+
toolResultRenderer={handle.props.toolResultRenderer}
|
|
79
|
+
toolsRenderer={handle.props.toolsRenderer}
|
|
80
|
+
/>
|
|
81
|
+
))}
|
|
82
|
+
</div>
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function MessagePart(
|
|
88
|
+
handle: Handle<{
|
|
89
|
+
part: UIMessage['parts'][number]
|
|
90
|
+
isThinkingComplete?: boolean
|
|
91
|
+
textPartRenderer?: ChatMessageProps['textPartRenderer']
|
|
92
|
+
thinkingPartRenderer?: ChatMessageProps['thinkingPartRenderer']
|
|
93
|
+
toolsRenderer?: ChatMessageProps['toolsRenderer']
|
|
94
|
+
defaultToolRenderer?: ChatMessageProps['defaultToolRenderer']
|
|
95
|
+
toolResultRenderer?: ChatMessageProps['toolResultRenderer']
|
|
96
|
+
}>,
|
|
97
|
+
) {
|
|
98
|
+
return () => {
|
|
99
|
+
const part = handle.props.part
|
|
100
|
+
|
|
101
|
+
if (part.type === 'text') {
|
|
102
|
+
if (handle.props.textPartRenderer) {
|
|
103
|
+
return handle.props.textPartRenderer({ content: part.content })
|
|
104
|
+
}
|
|
105
|
+
return (
|
|
106
|
+
<div data-part-type="text" data-part-content>
|
|
107
|
+
{part.content}
|
|
108
|
+
</div>
|
|
109
|
+
)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (part.type === 'thinking') {
|
|
113
|
+
if (handle.props.thinkingPartRenderer) {
|
|
114
|
+
return handle.props.thinkingPartRenderer({
|
|
115
|
+
content: part.content,
|
|
116
|
+
isComplete: handle.props.isThinkingComplete,
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
return (
|
|
120
|
+
<ThinkingPart
|
|
121
|
+
content={part.content}
|
|
122
|
+
isComplete={handle.props.isThinkingComplete}
|
|
123
|
+
/>
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (part.type === 'tool-call') {
|
|
128
|
+
const toolProps: ToolCallRenderProps = {
|
|
129
|
+
id: part.id,
|
|
130
|
+
name: part.name,
|
|
131
|
+
arguments: part.arguments,
|
|
132
|
+
state: part.state,
|
|
133
|
+
approval: part.approval,
|
|
134
|
+
output: part.output,
|
|
135
|
+
}
|
|
136
|
+
const named = handle.props.toolsRenderer?.[part.name]
|
|
137
|
+
if (named) return named(toolProps)
|
|
138
|
+
if (handle.props.defaultToolRenderer) {
|
|
139
|
+
return handle.props.defaultToolRenderer(toolProps)
|
|
140
|
+
}
|
|
141
|
+
return (
|
|
142
|
+
<div
|
|
143
|
+
data-part-type="tool-call"
|
|
144
|
+
data-tool-id={part.id}
|
|
145
|
+
data-tool-name={part.name}
|
|
146
|
+
data-tool-state={part.state}
|
|
147
|
+
>
|
|
148
|
+
<div data-tool-header>
|
|
149
|
+
<strong>{part.name}</strong>
|
|
150
|
+
<span data-tool-state-badge>{part.state}</span>
|
|
151
|
+
</div>
|
|
152
|
+
{part.arguments ? (
|
|
153
|
+
<div data-tool-arguments>
|
|
154
|
+
<pre>{part.arguments}</pre>
|
|
155
|
+
</div>
|
|
156
|
+
) : null}
|
|
157
|
+
{part.approval ? (
|
|
158
|
+
<div data-tool-approval>
|
|
159
|
+
{part.approval.approved !== undefined
|
|
160
|
+
? part.approval.approved
|
|
161
|
+
? 'Approved'
|
|
162
|
+
: 'Denied'
|
|
163
|
+
: 'Awaiting approval...'}
|
|
164
|
+
</div>
|
|
165
|
+
) : null}
|
|
166
|
+
{part.output ? (
|
|
167
|
+
<div data-tool-output>
|
|
168
|
+
<pre>{JSON.stringify(part.output, null, 2)}</pre>
|
|
169
|
+
</div>
|
|
170
|
+
) : null}
|
|
171
|
+
</div>
|
|
172
|
+
)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (part.type === 'tool-result') {
|
|
176
|
+
const content = toolResultContentToString(part.content)
|
|
177
|
+
if (handle.props.toolResultRenderer) {
|
|
178
|
+
return handle.props.toolResultRenderer({
|
|
179
|
+
toolCallId: part.toolCallId,
|
|
180
|
+
content,
|
|
181
|
+
state: part.state,
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
return (
|
|
185
|
+
<div
|
|
186
|
+
data-part-type="tool-result"
|
|
187
|
+
data-tool-call-id={part.toolCallId}
|
|
188
|
+
data-tool-result-state={part.state}
|
|
189
|
+
>
|
|
190
|
+
<div data-tool-result-content>{content}</div>
|
|
191
|
+
</div>
|
|
192
|
+
)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return null
|
|
196
|
+
}
|
|
197
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ref } from 'remix/ui'
|
|
2
|
+
import type { Handle, RemixNode } from 'remix/ui'
|
|
3
|
+
import { useChatContext } from './chat.tsx'
|
|
4
|
+
import { ChatMessage } from './chat-message.tsx'
|
|
5
|
+
import type { UIMessage } from '../types.ts'
|
|
6
|
+
|
|
7
|
+
/** @deprecated Use `createChatUI()` Messages instead. Removed in 1.0.0. */
|
|
8
|
+
export interface ChatMessagesProps {
|
|
9
|
+
children?: (message: UIMessage, index: number) => RemixNode
|
|
10
|
+
class?: string
|
|
11
|
+
emptyState?: RemixNode
|
|
12
|
+
loadingState?: RemixNode
|
|
13
|
+
errorState?: (props: {
|
|
14
|
+
error: Error
|
|
15
|
+
reload: () => Promise<void>
|
|
16
|
+
}) => RemixNode
|
|
17
|
+
autoScroll?: boolean
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** @deprecated Use `createChatUI()` Messages instead. Removed in 1.0.0. */
|
|
21
|
+
export function ChatMessages(handle: Handle<ChatMessagesProps>) {
|
|
22
|
+
let container: HTMLElement | null = null
|
|
23
|
+
|
|
24
|
+
return () => {
|
|
25
|
+
const { messages, isLoading, error, reload } = useChatContext(handle)
|
|
26
|
+
const autoScroll = handle.props.autoScroll !== false
|
|
27
|
+
|
|
28
|
+
if (autoScroll && container) {
|
|
29
|
+
container.scrollTop = container.scrollHeight
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (error && handle.props.errorState) {
|
|
33
|
+
return handle.props.errorState({ error, reload })
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (isLoading && messages.length === 0 && handle.props.loadingState) {
|
|
37
|
+
return handle.props.loadingState
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (messages.length === 0 && handle.props.emptyState) {
|
|
41
|
+
return handle.props.emptyState
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<div
|
|
46
|
+
class={handle.props.class}
|
|
47
|
+
data-chat-messages
|
|
48
|
+
data-message-count={messages.length}
|
|
49
|
+
mix={[
|
|
50
|
+
ref((node) => {
|
|
51
|
+
container = node
|
|
52
|
+
}),
|
|
53
|
+
]}
|
|
54
|
+
>
|
|
55
|
+
{messages.map((message, index) =>
|
|
56
|
+
typeof handle.props.children === 'function' ? (
|
|
57
|
+
<div key={message.id} data-message-id={message.id}>
|
|
58
|
+
{handle.props.children(message, index)}
|
|
59
|
+
</div>
|
|
60
|
+
) : (
|
|
61
|
+
<ChatMessage key={message.id} message={message} />
|
|
62
|
+
),
|
|
63
|
+
)}
|
|
64
|
+
</div>
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { ConnectionAdapter } from '@tanstack/ai-client'
|
|
2
|
+
import type { Handle, RemixNode } from 'remix/ui'
|
|
3
|
+
import { createChat } from '../create-chat.ts'
|
|
4
|
+
import type { CreateChatReturn, UIMessage } from '../types.ts'
|
|
5
|
+
|
|
6
|
+
/** @deprecated Use `createChatUI()` Chat/Provider instead. Removed in 1.0.0. */
|
|
7
|
+
export interface ChatProps {
|
|
8
|
+
children?: RemixNode
|
|
9
|
+
class?: string
|
|
10
|
+
connection: ConnectionAdapter
|
|
11
|
+
initialMessages?: Array<UIMessage>
|
|
12
|
+
id?: string
|
|
13
|
+
body?: Record<string, any>
|
|
14
|
+
tools?: Array<any>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** @deprecated Use `createChatHook().useChatContext(handle)` from `@tanstack/ai-remix/ui` instead. Removed in 1.0.0. */
|
|
18
|
+
export function useChatContext(handle: Handle<any>): CreateChatReturn {
|
|
19
|
+
const chat = handle.context.get(Chat)
|
|
20
|
+
if (!chat) {
|
|
21
|
+
throw new Error(
|
|
22
|
+
'Chat components must be wrapped in <Chat>. Make sure you use Chat.Messages, Chat.Input, and the rest inside a <Chat> component.',
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
return chat
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @deprecated Use `createChatHook()` from `@tanstack/ai-remix/ui` instead.
|
|
30
|
+
* Removed in 1.0.0.
|
|
31
|
+
*/
|
|
32
|
+
export function Chat(handle: Handle<ChatProps, CreateChatReturn>) {
|
|
33
|
+
const chat = createChat(handle, {
|
|
34
|
+
connection: handle.props.connection,
|
|
35
|
+
...(handle.props.initialMessages !== undefined
|
|
36
|
+
? { initialMessages: handle.props.initialMessages }
|
|
37
|
+
: {}),
|
|
38
|
+
...(handle.props.id !== undefined ? { threadId: handle.props.id } : {}),
|
|
39
|
+
...(handle.props.body !== undefined ? { body: handle.props.body } : {}),
|
|
40
|
+
...(handle.props.tools !== undefined ? { tools: handle.props.tools } : {}),
|
|
41
|
+
})
|
|
42
|
+
handle.context.set(chat)
|
|
43
|
+
return () => (
|
|
44
|
+
<div class={handle.props.class} data-chat-root>
|
|
45
|
+
{handle.props.children}
|
|
46
|
+
</div>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { InferredClientContext } from '@tanstack/ai-client'
|
|
2
|
+
import type {
|
|
3
|
+
ChatUIInterruptsOf,
|
|
4
|
+
ChatUISchemaOf,
|
|
5
|
+
ChatUIToolsOf,
|
|
6
|
+
} from '@tanstack/ai-client/ui'
|
|
7
|
+
import type { Handle } from 'remix/ui'
|
|
8
|
+
import { createChat as createUnboundChat } from '../create-chat.ts'
|
|
9
|
+
import type { CreateChatOptions } from '../types.ts'
|
|
10
|
+
import { createChatUI } from './create-ui.tsx'
|
|
11
|
+
import type { ChatUIFactoryConfig, ChatUIHost } from './create-ui.tsx'
|
|
12
|
+
|
|
13
|
+
type HeadlessOptions<TOptions> = CreateChatOptions<
|
|
14
|
+
ChatUIToolsOf<TOptions>,
|
|
15
|
+
ChatUISchemaOf<TOptions>,
|
|
16
|
+
InferredClientContext<ChatUIToolsOf<TOptions>>,
|
|
17
|
+
ChatUIInterruptsOf<TOptions>
|
|
18
|
+
>
|
|
19
|
+
|
|
20
|
+
type ChatInstanceOverrides<TOptions> = {
|
|
21
|
+
threadId?: string
|
|
22
|
+
live?: boolean
|
|
23
|
+
forwardedProps?: Record<string, any>
|
|
24
|
+
body?: Record<string, any>
|
|
25
|
+
initialMessages?: HeadlessOptions<TOptions>['initialMessages']
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Bind chat options and UI widgets once at module scope.
|
|
30
|
+
*
|
|
31
|
+
* Returns a bound `createAppChat`, the UI kit (`ui`), and `useChatContext`.
|
|
32
|
+
* Call `createAppChat(handle)` in a Remix setup function. Render
|
|
33
|
+
* `<ui.Chat chat={chat} />`. Pass instance overrides such as `threadId`
|
|
34
|
+
* into `createAppChat(handle, overrides)`.
|
|
35
|
+
*/
|
|
36
|
+
export function createChatHook<const TOptions>({
|
|
37
|
+
options,
|
|
38
|
+
...chatComponents
|
|
39
|
+
}: {
|
|
40
|
+
options: TOptions
|
|
41
|
+
} & ChatUIFactoryConfig<NoInfer<TOptions>>) {
|
|
42
|
+
const ui = createChatUI(
|
|
43
|
+
options,
|
|
44
|
+
chatComponents as ChatUIFactoryConfig<NoInfer<TOptions>>,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
function createAppChat(
|
|
48
|
+
handle: Handle<any>,
|
|
49
|
+
overrides?: ChatInstanceOverrides<TOptions>,
|
|
50
|
+
): ChatUIHost<TOptions> {
|
|
51
|
+
const chat = overrides
|
|
52
|
+
? createUnboundChat(handle, {
|
|
53
|
+
...(options as HeadlessOptions<TOptions>),
|
|
54
|
+
...overrides,
|
|
55
|
+
})
|
|
56
|
+
: createUnboundChat(handle, options as HeadlessOptions<TOptions>)
|
|
57
|
+
// oxlint-disable-next-line eslint-js/no-restricted-syntax -- return shape always includes partial/final; ChatUIHost gates those on TSchema
|
|
58
|
+
return chat as unknown as ChatUIHost<TOptions>
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
createAppChat,
|
|
63
|
+
ui,
|
|
64
|
+
useChatContext: ui.useChatContext,
|
|
65
|
+
}
|
|
66
|
+
}
|