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

next
tsx 313 lines 8.15 KB
Raw
1 import * as React from "react"
2
3 import {
4 terminalThemeClass,
5 terminalThemeLightClass,
6 type TerminalTheme,
7 } from "@/lib/terminal-themes"
8 import { cn } from "@/lib/utils"
9
10 import { TerminalBodyScroll } from "./terminal-body-scroll"
11
12 export type { TerminalTheme } from "@/lib/terminal-themes"
13
14 export type TerminalProgressConfig = {
15 value: number
16 /** Label beside the bar — e.g. "context". */
17 label?: React.ReactNode
18 /** Show the numeric percentage after the bar. Defaults to false when label is set. */
19 showValue?: boolean
20 }
21
22 export type TerminalProgress = number | TerminalProgressConfig | false
23
24 function resolveProgress(progress: TerminalProgress | undefined) {
25 if (progress === false || progress === undefined) return null
26
27 if (typeof progress === "number") {
28 return { value: progress, label: undefined, showValue: true }
29 }
30
31 return {
32 value: progress.value,
33 label: progress.label,
34 showValue: progress.showValue ?? progress.label == null,
35 }
36 }
37
38 export type TerminalBodyProps = React.HTMLAttributes<HTMLDivElement> & {
39 footer?: React.ReactNode
40 header?: React.ReactNode
41 /** Stretch to fill a fixed-height window and scroll internally. Defaults to content-height flow. */
42 fill?: boolean
43 pinScrollBottom?: boolean
44 }
45
46 function TerminalBodyLayout({
47 className,
48 children,
49 footer,
50 header,
51 fill = false,
52 pinScrollBottom = false,
53 ...props
54 }: TerminalBodyProps) {
55 return (
56 <div
57 className={cn(
58 "flex w-full flex-col justify-start overflow-hidden",
59 fill ? "min-h-0 flex-1" : "shrink-0",
60 className
61 )}
62 {...props}
63 >
64 {header ? (
65 <div className="terminal-body-header w-full shrink-0 px-[var(--terminal-session-pad-x)]">
66 {header}
67 </div>
68 ) : null}
69 <TerminalBodyScroll
70 fill={fill}
71 pinBottom={pinScrollBottom}
72 stickToBottom={pinScrollBottom}
73 >
74 <div
75 className={cn(
76 "terminal-body-content w-full",
77 pinScrollBottom ? "flex min-h-full flex-col justify-end" : "min-h-0"
78 )}
79 >
80 {children}
81 </div>
82 </TerminalBodyScroll>
83 {footer ? (
84 <div className="terminal-body-footer w-full shrink-0 pb-[var(--terminal-session-pad-y)]">
85 {footer}
86 </div>
87 ) : null}
88 </div>
89 )
90 }
91
92 export type TerminalWindowProps = React.HTMLAttributes<HTMLDivElement> & {
93 path?: string
94 /** Pass `false` to hide the header progress indicator. */
95 progress?: TerminalProgress
96 showTrafficLights?: boolean
97 /** Built-in palette — default, grok, or claude. */
98 theme?: TerminalTheme
99 variant?: "dark" | "light"
100 footer?: React.ReactNode
101 header?: React.ReactNode
102 /** Stretch to fill a fixed-height window and scroll internally. */
103 fill?: boolean
104 pinScrollBottom?: boolean
105 /** Classes for the inner body layout wrapper. */
106 bodyClassName?: string
107 /** Optional control rendered at the end of the window chrome header. */
108 headerAction?: React.ReactNode
109 }
110
111 export function TerminalWindow({
112 path,
113 progress,
114 showTrafficLights = true,
115 theme = "default",
116 variant = "dark",
117 className,
118 children,
119 style,
120 footer,
121 header,
122 fill = false,
123 pinScrollBottom = false,
124 bodyClassName,
125 headerAction,
126 ...props
127 }: TerminalWindowProps) {
128 const resolvedProgress = resolveProgress(progress)
129
130 return (
131 <div
132 className={cn(
133 "terminal-theme flex w-full min-h-0 flex-col overflow-hidden border font-mono text-xs leading-relaxed",
134 terminalThemeClass(theme),
135 variant === "light" && terminalThemeLightClass(theme),
136 className
137 )}
138 style={{
139 backgroundColor: "var(--terminal-editor-bg)",
140 borderColor: "var(--terminal-border)",
141 borderRadius: "var(--terminal-radius-window)",
142 color: "var(--terminal-fg)",
143 ...style,
144 }}
145 {...props}
146 >
147 {(path || resolvedProgress || showTrafficLights || headerAction) && (
148 <TerminalHeader
149 path={path}
150 progress={resolvedProgress ?? undefined}
151 showTrafficLights={showTrafficLights}
152 headerAction={headerAction}
153 />
154 )}
155 <TerminalBodyLayout
156 className={bodyClassName}
157 fill={fill}
158 footer={footer}
159 header={header}
160 pinScrollBottom={pinScrollBottom}
161 >
162 {children}
163 </TerminalBodyLayout>
164 </div>
165 )
166 }
167
168 /** @deprecated Pass footer, header, fill, and pinScrollBottom to TerminalWindow instead. */
169 export function TerminalBody(props: TerminalBodyProps) {
170 return <TerminalBodyLayout {...props} />
171 }
172
173 export type TerminalHeaderProps = {
174 path?: string
175 progress?: {
176 value: number
177 label?: React.ReactNode
178 showValue: boolean
179 }
180 showTrafficLights?: boolean
181 className?: string
182 headerAction?: React.ReactNode
183 /** @default true */
184 showBorderBottom?: boolean
185 }
186
187 function TerminalHeaderProgress({
188 progress,
189 label,
190 showValue,
191 }: {
192 progress: number
193 label?: React.ReactNode
194 showValue: boolean
195 }) {
196 const ariaLabel =
197 label != null && label !== "" ? String(label) : "Progress"
198
199 return (
200 <div className="flex shrink-0 items-center gap-2 text-[11px]">
201 <span style={{ color: "var(--terminal-vdim)" }}>|</span>
202 {label ? (
203 <span className="shrink-0" style={{ color: "var(--terminal-dim)" }}>
204 {label}
205 </span>
206 ) : null}
207 <span
208 className="relative inline-flex h-[1em] w-16 items-stretch overflow-hidden terminal-panel-sm"
209 style={{ backgroundColor: "var(--terminal-progress-track)" }}
210 role="progressbar"
211 aria-valuenow={progress}
212 aria-valuemin={0}
213 aria-valuemax={100}
214 aria-label={showValue ? `${ariaLabel} ${progress.toFixed(2)}%` : ariaLabel}
215 >
216 <span
217 className="block h-full"
218 style={{
219 width: `${Math.min(100, Math.max(0, progress))}%`,
220 backgroundColor: "var(--terminal-progress-fill)",
221 }}
222 />
223 </span>
224 {showValue ? (
225 <span style={{ color: "var(--terminal-dim)" }}>{progress.toFixed(2)}%</span>
226 ) : null}
227 </div>
228 )
229 }
230
231 export function TerminalHeader({
232 path,
233 progress,
234 showTrafficLights = true,
235 className,
236 headerAction,
237 showBorderBottom = true,
238 }: TerminalHeaderProps) {
239 return (
240 <div
241 className={cn("flex items-center gap-3 px-3 py-2", className)}
242 style={
243 showBorderBottom
244 ? { borderBottom: "1px solid var(--terminal-border)" }
245 : undefined
246 }
247 >
248 {showTrafficLights && (
249 <div className="terminal-traffic-lights flex shrink-0 gap-1.5">
250 <div
251 className="size-[9px] rounded-full"
252 style={{ backgroundColor: "var(--terminal-traffic-red)" }}
253 />
254 <div
255 className="size-[9px] rounded-full"
256 style={{ backgroundColor: "var(--terminal-traffic-yellow)" }}
257 />
258 <div
259 className="size-[9px] rounded-full"
260 style={{ backgroundColor: "var(--terminal-traffic-green)" }}
261 />
262 </div>
263 )}
264 {path && (
265 <div
266 className="ml-1.5 min-w-0 flex-1 truncate text-[11px]"
267 style={{ color: "var(--terminal-dim)" }}
268 >
269 {path}
270 </div>
271 )}
272 {progress && (
273 <TerminalHeaderProgress
274 progress={progress.value}
275 label={progress.label}
276 showValue={progress.showValue}
277 />
278 )}
279 {headerAction ? <div className="ml-auto shrink-0">{headerAction}</div> : null}
280 </div>
281 )
282 }
283
284 export type TerminalStatusBarProps = React.HTMLAttributes<HTMLDivElement> & {
285 left?: React.ReactNode
286 right?: React.ReactNode
287 }
288
289 export function TerminalStatusBar({
290 left,
291 right,
292 className,
293 children,
294 ...props
295 }: TerminalStatusBarProps) {
296 return (
297 <div
298 className={cn(
299 "flex items-center justify-between gap-3 px-3 py-2 text-[11px]",
300 className
301 )}
302 style={{
303 borderTop: "1px solid var(--terminal-border)",
304 color: "var(--terminal-dim)",
305 backgroundColor: "var(--terminal-surface)",
306 }}
307 {...props}
308 >
309 <div className="min-w-0 truncate">{left ?? children}</div>
310 {right && <div className="shrink-0">{right}</div>}
311 </div>
312 )
313 }