@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
tsx 306 lines 9.28 KB
Raw
1 "use client"
2
3 import { Zap } from "lucide-react"
4 import * as React from "react"
5
6 import { useResolvedTerminalTheme } from "@/hooks/use-resolved-terminal-theme"
7 import { cn } from "@/lib/utils"
8
9 /** Default composer meta — lightning level indicator (e.g. max). */
10 export function InputLevelMeta({ children = "max" }: { children?: React.ReactNode }) {
11 return (
12 <span className="inline-flex items-center gap-1">
13 <Zap className="size-3 shrink-0" aria-hidden />
14 {children}
15 </span>
16 )
17 }
18
19 export type InputProps = {
20 value?: string
21 defaultValue?: string
22 onValueChange?: (value: string) => void
23 onSubmit?: (value: string) => void
24 prompt?: string
25 status?: React.ReactNode
26 /** Footer hint on the left — composer (default) and Claude stacked layouts. */
27 metaLeft?: React.ReactNode
28 /** Footer hint on the right — composer (default) and Claude stacked layouts. */
29 metaRight?: React.ReactNode
30 placeholder?: string
31 disabled?: boolean
32 /** Keep the blinking cursor visible even when unfocused. */
33 showCursor?: boolean
34 /**
35 * `auto` — composer on default, inline box on grok, stacked lines on Claude.
36 * `inline` — status on the right in a bordered box. `stacked` — always stacked.
37 */
38 layout?: "auto" | "inline" | "stacked"
39 className?: string
40 }
41
42 function refocusTextarea(textarea: HTMLTextAreaElement | null) {
43 if (!textarea) return
44 textarea.focus()
45 requestAnimationFrame(() => {
46 textarea.focus()
47 requestAnimationFrame(() => textarea.focus())
48 })
49 }
50
51 export const Input = React.forwardRef<HTMLTextAreaElement, InputProps>(function Input(
52 {
53 value,
54 defaultValue = "",
55 onValueChange,
56 onSubmit,
57 prompt,
58 status,
59 metaLeft,
60 metaRight,
61 placeholder,
62 disabled = false,
63 showCursor = false,
64 layout = "auto",
65 className,
66 },
67 ref
68 ) {
69 const [internalValue, setInternalValue] = React.useState(defaultValue)
70 const [focused, setFocused] = React.useState(false)
71 const [caretLeft, setCaretLeft] = React.useState(0)
72 const textareaRef = React.useRef<HTMLTextAreaElement>(null)
73 const mirrorRef = React.useRef<HTMLSpanElement>(null)
74 const { ref: themeRef, isClaude, isBubble } = useResolvedTerminalTheme()
75 const currentValue = value ?? internalValue
76
77 const stacked = layout === "stacked" || (layout === "auto" && isClaude)
78 const composer = layout === "auto" && isBubble
79 const inline = !stacked && !composer
80 const resolvedPrompt = prompt ?? "❯"
81 const footerRight = metaRight ?? (stacked ? status : undefined)
82 const showComposerMeta =
83 composer && (metaLeft != null || metaRight != null || status != null)
84 const showStackedFooter = stacked && (metaLeft != null || footerRight != null)
85 const textColor = stacked ? "var(--terminal-fg)" : "var(--terminal-white)"
86 const promptColor = "var(--terminal-teal)"
87 const cursorColor = stacked ? "var(--terminal-fg)" : "var(--terminal-progress-fill)"
88 const fieldLineHeight = stacked ? 18 : 16
89 const cursorHeight = stacked ? 13 : 12
90
91 React.useImperativeHandle(ref, () => textareaRef.current as HTMLTextAreaElement)
92
93 const syncCaretPosition = React.useCallback(() => {
94 const textarea = textareaRef.current
95 const mirror = mirrorRef.current
96 if (!textarea || !mirror) return
97
98 const selectionStart = textarea.selectionStart ?? currentValue.length
99 mirror.textContent = currentValue.slice(0, selectionStart) || "\u200b"
100 setCaretLeft(mirror.offsetWidth)
101 }, [currentValue])
102
103 React.useLayoutEffect(() => {
104 syncCaretPosition()
105 }, [syncCaretPosition])
106
107 const handleChange = (next: string) => {
108 if (value === undefined) setInternalValue(next)
109 onValueChange?.(next)
110 }
111
112 const handleKeyDown = (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
113 if (event.key === "Enter" && !event.shiftKey) {
114 event.preventDefault()
115 if (!currentValue.trim() || disabled) return
116 onSubmit?.(currentValue)
117 refocusTextarea(textareaRef.current)
118 }
119 }
120
121 const field = (
122 <div
123 className={cn(
124 "flex min-w-0 flex-1 items-center gap-2 overflow-hidden",
125 stacked ? "gap-2.5" : undefined
126 )}
127 style={{ color: textColor }}
128 >
129 <span
130 aria-hidden
131 className="inline-flex shrink-0 items-center"
132 style={{
133 color: promptColor,
134 fontSize: 9,
135 lineHeight: `${fieldLineHeight}px`,
136 }}
137 >
138 {resolvedPrompt}
139 </span>
140 <span
141 className="relative flex min-w-0 flex-1 items-center overflow-hidden"
142 style={{ lineHeight: `${fieldLineHeight}px`, minHeight: fieldLineHeight }}
143 >
144 <span
145 ref={mirrorRef}
146 aria-hidden
147 className="pointer-events-none invisible absolute top-0 left-0 whitespace-pre text-[11px] leading-4"
148 />
149 <textarea
150 ref={textareaRef}
151 value={currentValue}
152 onChange={(event) => handleChange(event.target.value)}
153 onKeyDown={handleKeyDown}
154 onKeyUp={syncCaretPosition}
155 onSelect={syncCaretPosition}
156 onClick={syncCaretPosition}
157 onFocus={() => {
158 setFocused(true)
159 syncCaretPosition()
160 }}
161 onBlur={() => setFocused(false)}
162 placeholder={placeholder}
163 disabled={disabled}
164 rows={1}
165 spellCheck={false}
166 className={cn(
167 "terminal-input-control block w-full resize-none overflow-hidden border-0 bg-transparent p-0 font-inherit text-[11px] leading-4 caret-transparent outline-none"
168 )}
169 style={{
170 color: textColor,
171 minHeight: fieldLineHeight,
172 maxHeight: fieldLineHeight,
173 }}
174 />
175 {!disabled && (focused || showCursor) ? (
176 <span
177 aria-hidden
178 className="terminal-cursor pointer-events-none absolute top-1/2 inline-block w-[0.55em] shrink-0 -translate-y-1/2"
179 style={{
180 left: caretLeft,
181 height: cursorHeight,
182 backgroundColor: cursorColor,
183 }}
184 />
185 ) : null}
186 </span>
187 {inline && status ? (
188 <div
189 className="shrink-0 whitespace-nowrap text-[11px]"
190 style={{ color: "var(--terminal-dim)" }}
191 >
192 {status}
193 </div>
194 ) : null}
195 </div>
196 )
197
198 const fieldShell = (
199 <div
200 className={cn(
201 "terminal-input-field flex items-center",
202 stacked && "terminal-input-field--stacked px-0",
203 composer && "terminal-input-field--composer terminal-panel gap-3 border px-3 py-2.5 sm:px-4",
204 inline && "gap-3 rounded border px-3 py-2 sm:px-4"
205 )}
206 style={
207 stacked
208 ? undefined
209 : {
210 borderColor: "var(--terminal-border)",
211 backgroundColor: "var(--terminal-input-bg)",
212 }
213 }
214 >
215 {field}
216 </div>
217 )
218
219 return (
220 <div
221 ref={themeRef}
222 className={cn("terminal-input w-full text-[11px]", className)}
223 onClick={() => textareaRef.current?.focus()}
224 >
225 {composer ? (
226 <div className="terminal-input-composer">
227 {fieldShell}
228 {showComposerMeta ? (
229 <div className="terminal-input-meta-layer flex items-center justify-between gap-2">
230 <span className="min-w-0 truncate">{metaLeft}</span>
231 <span className="flex shrink-0 items-center gap-2 whitespace-nowrap">
232 {metaRight}
233 {status}
234 </span>
235 </div>
236 ) : null}
237 </div>
238 ) : (
239 <>
240 {fieldShell}
241 {showStackedFooter ? (
242 <div
243 className="terminal-input-meta mt-1.5 flex items-center justify-between gap-3 text-[11px] leading-4"
244 style={{ color: "var(--terminal-dim)" }}
245 >
246 <span className="min-w-0 truncate">{metaLeft}</span>
247 <span className="shrink-0 whitespace-nowrap">{footerRight}</span>
248 </div>
249 ) : null}
250 </>
251 )}
252 </div>
253 )
254 })
255
256 /** @deprecated Use Input */
257 export type ShellInputProps = InputProps
258
259 /** @deprecated Use Input */
260 export const ShellInput = React.forwardRef<HTMLTextAreaElement, InputProps>(
261 function ShellInput(props, ref) {
262 return <Input {...props} ref={ref} />
263 }
264 )
265
266 export type CommandSuggestProps = {
267 commands: string[]
268 activeIndex?: number
269 onSelect?: (command: string) => void
270 className?: string
271 }
272
273 export function CommandSuggest({
274 commands,
275 activeIndex = 0,
276 onSelect,
277 className,
278 }: CommandSuggestProps) {
279 return (
280 <div
281 className={cn("terminal-panel overflow-hidden border font-mono text-xs", className)}
282 style={{
283 borderColor: "var(--terminal-border)",
284 backgroundColor: "var(--terminal-popover)",
285 }}
286 >
287 {commands.map((command, index) => (
288 <button
289 key={command}
290 type="button"
291 onClick={() => onSelect?.(command)}
292 className="flex w-full items-center px-3 py-1.5 text-left"
293 style={{
294 color:
295 index === activeIndex ? "var(--terminal-white)" : "var(--terminal-dim)",
296 backgroundColor:
297 index === activeIndex ? "var(--terminal-panel-strong)" : "transparent",
298 }}
299 >
300 <span style={{ color: "var(--terminal-green)" }}>/</span>
301 {command.replace(/^\//, "")}
302 </button>
303 ))}
304 </div>
305 )
306 }