Open History to see the last commit
1import { TerminalIcon } from "lucide-react";
2
3import { cn } from "~/lib/utils";
4import type { ContextPresentationCapability } from "../contextPresentationRegistry";
5import {
6 CHAT_INLINE_CHIP_CLASS_NAME,
7 COMPOSER_INLINE_CHIP_CLASS_NAME,
8 COMPOSER_INLINE_CHIP_ICON_CLASS_NAME,
9 COMPOSER_INLINE_CHIP_LABEL_CLASS_NAME,
10 CONTEXT_INLINE_CHIP_ICON_TONE_CLASS_NAMES,
11 CONTEXT_INLINE_CHIP_INTERACTIVE_CLASS_NAME,
12 CONTEXT_INLINE_CHIP_TONE_CLASS_NAMES,
13} from "../composerInlineChip";
14import { ContextChipPopover, ContextChipShell } from "../contextChipParts";
15
16interface TerminalContextInlineChipProps {
17 label: string;
18 terminalLabel: string;
19 lineStart: number;
20 lineEnd: number;
21 text: string;
22 detailsMode: ContextPresentationCapability["details"];
23 expired?: boolean;
24 surface?: "composer" | "transcript";
25}
26
27export function TerminalContextInlineChip(props: TerminalContextInlineChipProps) {
28 const { label, terminalLabel, lineStart, lineEnd, text, detailsMode, expired = false } = props;
29 const chipClassName =
30 props.surface === "transcript" ? CHAT_INLINE_CHIP_CLASS_NAME : COMPOSER_INLINE_CHIP_CLASS_NAME;
31
32 const icon = (
33 <TerminalIcon
34 className={cn(
35 COMPOSER_INLINE_CHIP_ICON_CLASS_NAME,
36 CONTEXT_INLINE_CHIP_ICON_TONE_CLASS_NAMES.terminal,
37 "size-3.5",
38 expired && "opacity-100",
39 )}
40 />
41 );
42
43 if (!expired && text.length > 0 && detailsMode === "popover") {
44 return (
45 <ContextChipPopover
46 accessibleLabel={`Terminal excerpt, ${label}`}
47 chip={
48 <>
49 {icon}
50 <span className={COMPOSER_INLINE_CHIP_LABEL_CLASS_NAME}>{label}</span>
51 </>
52 }
53 triggerClassName={cn(
54 chipClassName,
55 CONTEXT_INLINE_CHIP_TONE_CLASS_NAMES.terminal,
56 CONTEXT_INLINE_CHIP_INTERACTIVE_CLASS_NAME,
57 "cursor-pointer",
58 )}
59 popupClassName="w-[min(40rem,calc(100vw-2rem))]"
60 viewportClassName="overflow-hidden p-2"
61 >
62 <div className="overflow-hidden rounded-md border border-border/70 bg-background/80">
63 <div className="flex items-center gap-2 border-b border-border/70 px-3 py-2">
64 <TerminalIcon className="size-4 shrink-0 text-emerald-500" aria-hidden />
65 <span className="min-w-0 truncate text-sm font-medium text-foreground">
66 {terminalLabel}
67 </span>
68 <span className="ml-auto shrink-0 text-secondary-label text-xs">
69 {lineStart === lineEnd ? `Line ${lineStart}` : `Lines ${lineStart}–${lineEnd}`}
70 </span>
71 </div>
72 <pre
73 className="max-h-80 overflow-auto whitespace-pre bg-muted p-3 font-mono text-foreground text-xs leading-relaxed outline-none [tab-size:4] focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
74 aria-label="Captured terminal output"
75 tabIndex={0}
76 >
77 {text}
78 </pre>
79 </div>
80 </ContextChipPopover>
81 );
82 }
83
84 return (
85 <ContextChipShell
86 icon={icon}
87 label={label}
88 className={cn(
89 chipClassName,
90 CONTEXT_INLINE_CHIP_TONE_CLASS_NAMES.terminal,
91 expired && "border-destructive/35 bg-destructive/8 text-destructive",
92 )}
93 labelClassName={COMPOSER_INLINE_CHIP_LABEL_CLASS_NAME}
94 aria-label={`Terminal excerpt, ${label}${expired ? ", expired" : ""}`}
95 data-terminal-context-expired={expired ? "true" : undefined}
96 tooltipClassName="max-w-80 whitespace-pre-wrap leading-tight"
97 tooltip={
98 expired
99 ? `Terminal context expired. Remove and re-add ${label} to include it in your message.`
100 : detailsMode === "none"
101 ? undefined
102 : text
103 }
104 />
105 );
106}