1
+ import Box , { BoxProps } from "@mui/material/Box"
2
+ import Popover from "@mui/material/Popover"
3
+ import Skeleton from "@mui/material/Skeleton"
4
+ import Tooltip from "@mui/material/Tooltip"
1
5
import makeStyles from "@mui/styles/makeStyles"
2
6
import { watchAgentMetadata } from "api/api"
3
- import { WorkspaceAgent , WorkspaceAgentMetadata } from "api/typesGenerated"
7
+ import {
8
+ WorkspaceAgent ,
9
+ WorkspaceAgentMetadata ,
10
+ WorkspaceAgentMetadataResult ,
11
+ } from "api/typesGenerated"
4
12
import { Stack } from "components/Stack/Stack"
5
13
import dayjs from "dayjs"
6
14
import {
7
- createContext ,
8
15
FC ,
16
+ createContext ,
9
17
useContext ,
10
18
useEffect ,
11
19
useRef ,
12
20
useState ,
13
21
} from "react"
14
- import Skeleton from "@mui/material/Skeleton "
22
+ import { colors } from "theme/colors "
15
23
import { MONOSPACE_FONT_FAMILY } from "theme/constants"
16
24
import { combineClasses } from "utils/combineClasses"
17
- import Tooltip from "@mui/material/Tooltip"
18
- import Box , { BoxProps } from "@mui/material/Box"
25
+ import * as XTerm from "xterm"
26
+ import { FitAddon } from "xterm-addon-fit"
27
+ import { WebglAddon } from "xterm-addon-webgl"
28
+ import { Unicode11Addon } from "xterm-addon-unicode11"
29
+
30
+ import "xterm/css/xterm.css"
19
31
20
32
type ItemStatus = "stale" | "valid" | "loading"
21
33
22
34
export const WatchAgentMetadataContext = createContext ( watchAgentMetadata )
23
35
36
+ const MetadataTerminalPopover : FC < {
37
+ id : string
38
+ result : WorkspaceAgentMetadataResult
39
+ } > = ( { id, result } ) => {
40
+ const styles = useStyles ( )
41
+
42
+ const viewTermRef = useRef < HTMLDivElement > ( null )
43
+ const [ open , setOpen ] = useState ( false )
44
+
45
+ const [ xtermRef , setXtermRef ] = useState < HTMLDivElement | null > ( null )
46
+ const [ terminal , setTerminal ] = useState < XTerm . Terminal | null > ( null )
47
+ const [ fitAddon , setFitAddon ] = useState < FitAddon | null > ( null )
48
+
49
+ const writeTerminal = ( ) => {
50
+ if ( ! terminal || ! fitAddon ) {
51
+ return
52
+ }
53
+
54
+ // We write the clearCode with the new value to avoid a flash of blankness
55
+ // when the result value updates.
56
+ const clearCode = "\x1B[2J\x1B[H"
57
+ terminal . write ( clearCode + result . value , ( ) => {
58
+ fitAddon . fit ( )
59
+ } )
60
+ }
61
+
62
+ // Create the terminal.
63
+ // Largely taken from TerminalPage.
64
+ useEffect ( ( ) => {
65
+ if ( ! xtermRef ) {
66
+ return
67
+ }
68
+ const terminal = new XTerm . Terminal ( {
69
+ allowTransparency : true ,
70
+ allowProposedApi : true ,
71
+ disableStdin : true ,
72
+ fontFamily : MONOSPACE_FONT_FAMILY ,
73
+ fontSize : 16 ,
74
+ theme : {
75
+ background : colors . gray [ 16 ] ,
76
+ } ,
77
+ } )
78
+ terminal . loadAddon ( new WebglAddon ( ) )
79
+ terminal . loadAddon ( new FitAddon ( ) )
80
+
81
+ // This addon fixes multi-width codepoint rendering such as
82
+ // 🟢.
83
+ terminal . loadAddon ( new Unicode11Addon ( ) )
84
+ terminal . unicode . activeVersion = "11"
85
+
86
+ const fitAddon = new FitAddon ( )
87
+ setTerminal ( terminal )
88
+ setFitAddon ( fitAddon )
89
+ terminal . open ( xtermRef )
90
+ writeTerminal ( )
91
+
92
+ const resizeInterval = setInterval ( ( ) => {
93
+ window . dispatchEvent ( new Event ( "resize" ) )
94
+ } , 100 )
95
+
96
+ return ( ) => {
97
+ clearInterval ( resizeInterval )
98
+ terminal . dispose ( )
99
+ }
100
+ } , [ xtermRef , open ] )
101
+
102
+ useEffect ( ( ) => {
103
+ writeTerminal ( )
104
+ } , [ xtermRef , open , result ] )
105
+
106
+ return (
107
+ < >
108
+ < div
109
+ className = { styles . viewTerminal }
110
+ ref = { viewTermRef }
111
+ onMouseOver = { ( ) => {
112
+ setOpen ( true )
113
+ } }
114
+ >
115
+ View Terminal
116
+ </ div >
117
+
118
+ < Popover
119
+ id = { id }
120
+ open = { open }
121
+ onClose = { ( ) => setOpen ( false ) }
122
+ anchorEl = { viewTermRef . current }
123
+ anchorOrigin = { {
124
+ vertical : "bottom" ,
125
+ horizontal : "left" ,
126
+ } }
127
+ >
128
+ < div
129
+ className = { styles . terminal }
130
+ ref = { ( el ) => {
131
+ setXtermRef ( el )
132
+ } }
133
+ data-testid = "terminal"
134
+ />
135
+ </ Popover >
136
+ </ >
137
+ )
138
+ }
139
+
24
140
const MetadataItem : FC < { item : WorkspaceAgentMetadata } > = ( { item } ) => {
25
141
const styles = useStyles ( )
26
142
@@ -31,6 +147,13 @@ const MetadataItem: FC<{ item: WorkspaceAgentMetadata }> = ({ item }) => {
31
147
throw new Error ( "Metadata item description is undefined" )
32
148
}
33
149
150
+ const terminalPrefix = "terminal:"
151
+ const isTerminal = item . description . display_name . startsWith ( terminalPrefix )
152
+
153
+ const displayName = isTerminal
154
+ ? item . description . display_name . slice ( terminalPrefix . length )
155
+ : item . description . display_name
156
+
34
157
const staleThreshold = Math . max (
35
158
item . description . interval + item . description . timeout * 2 ,
36
159
// In case there is intense backpressure, we give a little bit of slack.
@@ -88,10 +211,15 @@ const MetadataItem: FC<{ item: WorkspaceAgentMetadata }> = ({ item }) => {
88
211
89
212
return (
90
213
< div className = { styles . metadata } >
91
- < div className = { styles . metadataLabel } >
92
- { item . description . display_name }
93
- </ div >
94
- < Box > { value } </ Box >
214
+ < div className = { styles . metadataLabel } > { displayName } </ div >
215
+ { isTerminal ? (
216
+ < MetadataTerminalPopover
217
+ id = { `metadata-terminal-${ item . description . key } ` }
218
+ result = { item . result }
219
+ />
220
+ ) : (
221
+ < Box > { value } </ Box >
222
+ ) }
95
223
</ div >
96
224
)
97
225
}
@@ -105,6 +233,7 @@ export const AgentMetadataView: FC<AgentMetadataViewProps> = ({ metadata }) => {
105
233
if ( metadata . length === 0 ) {
106
234
return < > </ >
107
235
}
236
+
108
237
return (
109
238
< div className = { styles . root } >
110
239
< Stack alignItems = "baseline" direction = "row" spacing = { 6 } >
@@ -228,6 +357,53 @@ const useStyles = makeStyles((theme) => ({
228
357
scrollPadding : theme . spacing ( 0 , 4 ) ,
229
358
} ,
230
359
360
+ viewTerminal : {
361
+ fontFamily : MONOSPACE_FONT_FAMILY ,
362
+ display : "inline-block" ,
363
+ textDecoration : "underline" ,
364
+ fontWeight : 600 ,
365
+ margin : 0 ,
366
+ fontSize : 14 ,
367
+ borderRadius : 4 ,
368
+ color : theme . palette . text . primary ,
369
+ } ,
370
+
371
+ terminal : {
372
+ width : "80ch" ,
373
+ overflow : "auto" ,
374
+ backgroundColor : theme . palette . background . paper ,
375
+ // flex: 1,
376
+ padding : theme . spacing ( 1 ) ,
377
+ // These styles attempt to mimic the VS Code scrollbar.
378
+ "& .xterm" : {
379
+ padding : 4 ,
380
+ width : "100vw" ,
381
+ height : "40vh" ,
382
+ } ,
383
+ "& .xterm-viewport" : {
384
+ // This is required to force full-width on the terminal.
385
+ // Otherwise there's a small white bar to the right of the scrollbar.
386
+ width : "auto !important" ,
387
+ } ,
388
+ "& .xterm-viewport::-webkit-scrollbar" : {
389
+ width : "10px" ,
390
+ } ,
391
+ "& .xterm-viewport::-webkit-scrollbar-track" : {
392
+ backgroundColor : "inherit" ,
393
+ } ,
394
+ "& .xterm-viewport::-webkit-scrollbar-thumb" : {
395
+ minHeight : 20 ,
396
+ backgroundColor : "rgba(255, 255, 255, 0.18)" ,
397
+ } ,
398
+ } ,
399
+
400
+ popover : {
401
+ padding : 0 ,
402
+ width : theme . spacing ( 38 ) ,
403
+ color : theme . palette . text . secondary ,
404
+ marginTop : theme . spacing ( 0.5 ) ,
405
+ } ,
406
+
231
407
metadata : {
232
408
fontSize : 12 ,
233
409
lineHeight : "normal" ,
0 commit comments