-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsandbox-terminal.ts
More file actions
219 lines (201 loc) · 6.96 KB
/
Copy pathsandbox-terminal.ts
File metadata and controls
219 lines (201 loc) · 6.96 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { useCallback, useEffect, useRef, useState } from 'react'
export interface SandboxTerminalConnection {
runtimeUrl: string | null
sidecarUrl: string | null
token: string | null
expiresAt: string | null
status: string
error: string | null
loading: boolean
sandboxId?: string
}
export interface SandboxTerminalConnectionResponse {
runtimeUrl?: string
sidecarUrl?: string
token?: string
expiresAt?: string
status?: string
error?: string
sandboxId?: string
}
export interface UseSandboxTerminalConnectionOptions {
workspaceId: string
connectionUrl?: string | ((workspaceId: string) => string)
fetcher?: typeof fetch
provisionPollIntervalMs?: number
provisionPollTimeoutMs?: number
tokenRefreshSkewMs?: number
}
export interface UseSandboxTerminalConnectionResult extends SandboxTerminalConnection {
connect: () => Promise<void>
}
const DEFAULT_PROVISION_POLL_INTERVAL_MS = 2_000
const DEFAULT_PROVISION_POLL_TIMEOUT_MS = 90_000
const DEFAULT_TOKEN_REFRESH_SKEW_MS = 120_000
const EMPTY_CONNECTION: SandboxTerminalConnection = {
runtimeUrl: null,
sidecarUrl: null,
token: null,
expiresAt: null,
status: 'idle',
error: null,
loading: false,
}
export function useSandboxTerminalConnection(opts: UseSandboxTerminalConnectionOptions): UseSandboxTerminalConnectionResult {
const [conn, setConn] = useState<SandboxTerminalConnection>(EMPTY_CONNECTION)
const mountedRef = useRef(false)
const generationRef = useRef(0)
const fetcher = opts.fetcher ?? fetch
const pollIntervalMs = opts.provisionPollIntervalMs ?? DEFAULT_PROVISION_POLL_INTERVAL_MS
const pollTimeoutMs = opts.provisionPollTimeoutMs ?? DEFAULT_PROVISION_POLL_TIMEOUT_MS
const refreshSkewMs = opts.tokenRefreshSkewMs ?? DEFAULT_TOKEN_REFRESH_SKEW_MS
const connectionUrl = useCallback(() => {
if (typeof opts.connectionUrl === 'function') return opts.connectionUrl(opts.workspaceId)
return opts.connectionUrl ?? `/api/workspaces/${encodeURIComponent(opts.workspaceId)}/sandbox/connection`
}, [opts.connectionUrl, opts.workspaceId])
const connect = useCallback(async () => {
const generation = generationRef.current + 1
generationRef.current = generation
const isCurrent = () => mountedRef.current && generationRef.current === generation
const setCurrentConn: typeof setConn = (value) => {
if (!isCurrent()) return
setConn(value)
}
setCurrentConn((current) => ({ ...current, loading: true, error: null }))
const deadline = Date.now() + pollTimeoutMs
while (isCurrent()) {
try {
const res = await fetcher(connectionUrl())
const data = (await res.json()) as SandboxTerminalConnectionResponse
if (!isCurrent()) return
const runtimeUrl = data.runtimeUrl ?? data.sidecarUrl
if (res.ok && runtimeUrl && data.token && data.expiresAt) {
setCurrentConn({
runtimeUrl,
sidecarUrl: data.sidecarUrl ?? runtimeUrl,
token: data.token,
expiresAt: data.expiresAt,
status: data.status ?? 'running',
error: null,
loading: false,
...(data.sandboxId ? { sandboxId: data.sandboxId } : {}),
})
return
}
if (res.ok) {
setCurrentConn({
runtimeUrl: null,
sidecarUrl: null,
token: null,
expiresAt: null,
loading: false,
error: 'Sandbox connection response is missing required fields',
status: data.status ?? 'error',
...(data.sandboxId ? { sandboxId: data.sandboxId } : {}),
})
return
}
if (res.status === 503 && Date.now() < deadline) {
setCurrentConn((current) => ({
...current,
loading: true,
status: data.status ?? 'provisioning',
error: null,
}))
await sleep(pollIntervalMs)
continue
}
setCurrentConn((current) => ({
...current,
runtimeUrl: null,
sidecarUrl: null,
token: null,
expiresAt: null,
loading: false,
error: data.error ?? 'Sandbox not available',
status: data.status ?? 'error',
}))
return
} catch (err) {
if (!isCurrent()) return
if (Date.now() < deadline) {
await sleep(pollIntervalMs)
continue
}
setCurrentConn((current) => ({
...current,
runtimeUrl: null,
sidecarUrl: null,
token: null,
expiresAt: null,
loading: false,
error: err instanceof Error ? err.message : 'Connection failed',
}))
return
}
}
}, [connectionUrl, fetcher, pollIntervalMs, pollTimeoutMs])
useEffect(() => {
mountedRef.current = true
return () => {
mountedRef.current = false
generationRef.current += 1
}
}, [])
useEffect(() => {
void connect()
}, [connect])
useEffect(() => {
if (!conn.runtimeUrl || !conn.token || !conn.expiresAt) return
const refreshAt = Date.parse(conn.expiresAt) - Date.now() - refreshSkewMs
if (!Number.isFinite(refreshAt)) {
setConn((current) => ({
...current,
runtimeUrl: null,
sidecarUrl: null,
token: null,
expiresAt: null,
status: 'error',
error: 'Sandbox token expiry is invalid',
}))
return
}
const timer = window.setTimeout(() => {
void connect()
}, Math.max(1_000, refreshAt))
return () => window.clearTimeout(timer)
}, [conn.runtimeUrl, conn.token, conn.expiresAt, connect, refreshSkewMs])
return { ...conn, connect }
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => window.setTimeout(resolve, ms))
}
const DEFAULT_TERMINAL_CID_KEY = 'agent-app:terminal-connection-id'
function newConnectionId(): string {
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') return crypto.randomUUID()
return `cid-${Date.now().toString(36)}-${Math.floor(Math.random() * 1e9).toString(36)}`
}
/**
* Stable-per-tab, unique-per-client terminal connection id.
*
* Persists in `sessionStorage` so a reload in the same tab reuses the id (the
* sidecar restores the same PTY session via `TerminalView.connectionId`), while
* separate tabs/windows each get a distinct id. Pass the result as
* `TerminalView`'s `connectionId`. Without it (e.g. gtm-agent today) every tab
* shares one connection id and their reconnects evict each other.
*
* Falls back to an ephemeral id when `sessionStorage` is unavailable (SSR,
* privacy mode) — still unique per call, just not reload-stable.
*/
export function tabTerminalConnectionId(storageKey: string = DEFAULT_TERMINAL_CID_KEY): string {
try {
const store = globalThis.sessionStorage
const existing = store?.getItem(storageKey)
if (existing) return existing
const id = newConnectionId()
store?.setItem(storageKey, id)
return id
} catch {
return newConnectionId()
}
}