-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAssistantChatForm.tsx
More file actions
176 lines (164 loc) · 5.87 KB
/
Copy pathAssistantChatForm.tsx
File metadata and controls
176 lines (164 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { ArrowUp, Loader2, Square } from 'lucide-react'
import { ChangeEvent, FormEvent, forwardRef, KeyboardEvent, memo, useRef } from 'react'
import { useBreakpoint } from 'common'
import { ExpandingTextArea } from 'ui'
import { cn } from 'ui/src/lib/utils'
import { ButtonTooltip } from '../ButtonTooltip'
import { type SqlSnippet } from './AIAssistant.types'
import { ModelSelector } from './ModelSelector'
import { getSnippetContent, SnippetRow } from './SnippetRow'
export interface FormProps {
/* The ref for the textarea, optional. Exposed for the CommandsPopover to attach events. */
textAreaRef?: React.RefObject<HTMLTextAreaElement>
/* The loading state of the form */
loading: boolean
/* The disabled state of the form */
disabled?: boolean
/* The value of the textarea */
value?: string
/* The function to handle the value change */
onValueChange: (value: ChangeEvent<HTMLTextAreaElement>) => void
/**
* If true, include SQL snippets in the message sent to onSubmit
*/
includeSnippetsInMessage?: boolean
/**
* The function to handle the form submission
*/
onSubmit: (message: string) => void
/**
* The function to handle stopping the stream
*/
onStop?: () => void
/* The placeholder of the textarea */
placeholder?: string
/* SQL snippets to display above the form - can be strings or objects with label and content */
sqlSnippets?: SqlSnippet[]
/* Function to handle removing a SQL snippet */
onRemoveSnippet?: (index: number) => void
/* Additional class name for the snippets container */
snippetsClassName?: string
/* Additional class name for the form wrapper */
className?: string
/* If currently editing an existing message */
isEditing?: boolean
/* The currently selected AI model */
selectedModel: 'gpt-5.3-codex' | 'gpt-5.4-nano'
/* Callback when a model is chosen */
onSelectModel: (model: 'gpt-5.3-codex' | 'gpt-5.4-nano') => void
}
const AssistantChatFormComponent = forwardRef<HTMLFormElement, FormProps>(
(
{
loading = false,
disabled = false,
value = '',
textAreaRef,
onValueChange,
onSubmit,
onStop,
placeholder,
sqlSnippets,
onRemoveSnippet,
snippetsClassName,
includeSnippetsInMessage = false,
className,
isEditing = false,
selectedModel,
onSelectModel,
...props
},
ref
) => {
const formRef = useRef<HTMLFormElement>(null)
const isMobile = useBreakpoint('md')
const handleSubmit = (event?: FormEvent<HTMLFormElement>) => {
if (event) event.preventDefault()
if (!value || (loading && !isEditing)) return
let finalMessage = value
if (includeSnippetsInMessage && sqlSnippets && sqlSnippets.length > 0) {
const sqlSnippetsString = sqlSnippets
.map((snippet: SqlSnippet) => '```sql\n' + getSnippetContent(snippet) + '\n```')
.join('\n')
finalMessage = [value, sqlSnippetsString].filter(Boolean).join('\n\n')
}
onSubmit(finalMessage)
}
const handleKeyDown = (event: KeyboardEvent<HTMLTextAreaElement>) => {
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault()
handleSubmit()
}
}
const canSubmit = !loading && !!value
return (
<div className="w-full">
<form
id="assistant-chat"
ref={formRef}
{...props}
onSubmit={handleSubmit}
className={cn('relative', className)}
>
{sqlSnippets && sqlSnippets.length > 0 && (
<SnippetRow
snippets={sqlSnippets}
onRemoveSnippet={onRemoveSnippet}
className="absolute top-0 left-0 right-0 px-1.5 py-1.5"
/>
)}
<ExpandingTextArea
autoFocus={!isMobile}
ref={textAreaRef}
disabled={disabled}
className={cn(
'text-sm pr-10 pb-9 max-h-64',
sqlSnippets && sqlSnippets.length > 0 && 'pt-10'
)}
placeholder={placeholder}
spellCheck={false}
rows={3}
value={value}
onChange={(event) => onValueChange(event)}
onKeyDown={handleKeyDown}
/>
<div className="absolute inset-x-1.5 bottom-1.5 flex items-center justify-between pointer-events-none">
<div className="pointer-events-auto">
<ModelSelector selectedModel={selectedModel} onSelectModel={onSelectModel} />
</div>
<div className="flex gap-3 items-center pointer-events-auto">
{loading ? (
onStop ? (
<ButtonTooltip
type="outline"
aria-label="Stop response"
icon={<Square fill="currentColor" className="scale-75" />}
onClick={onStop}
className="w-7 h-7 rounded-full p-0 text-center flex items-center justify-center"
tooltip={{ content: { side: 'top', text: 'Stop response' } }}
/>
) : (
<Loader2 size={22} className="animate-spin size-7 text-muted" strokeWidth={1} />
)
) : (
<ButtonTooltip
htmlType="submit"
aria-label="Send message"
icon={<ArrowUp />}
disabled={!canSubmit}
className={cn(
'w-7 h-7 rounded-full p-0 text-center flex items-center justify-center',
!canSubmit ? 'opacity-50' : 'opacity-100'
)}
tooltip={{ content: { side: 'top', text: 'Send message' } }}
/>
)}
</div>
</div>
</form>
</div>
)
}
)
AssistantChatFormComponent.displayName = 'AssistantChatFormComponent'
export const AssistantChatForm = memo(AssistantChatFormComponent)