-
Notifications
You must be signed in to change notification settings - Fork 68
Feat/ez terminal #321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matthewlouisbrockman
wants to merge
14
commits into
main
Choose a base branch
from
feat/ez-terminal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feat/ez terminal #321
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0f7c8fb
Add self-contained dashboard terminal
matthewlouisbrockman 78069b4
Type Supabase auth cookie updates
matthewlouisbrockman be3d80e
Preserve terminal return targets across auth links
matthewlouisbrockman 36db469
Simplify terminal embed panel surface
matthewlouisbrockman b454adc
Support terminal reconnect URLs
matthewlouisbrockman 7b78ce4
Revert "Preserve terminal return targets across auth links"
matthewlouisbrockman 2c079a0
Rename terminal embed route to session
matthewlouisbrockman 3dd4ecb
Move terminal route to dashboard terminal
matthewlouisbrockman 2d007e7
Add dashboard terminal helper tests
matthewlouisbrockman daa6f01
Address terminal route review cleanup
matthewlouisbrockman bf1da8b
Remove redundant terminal icon sizing
matthewlouisbrockman fe70042
Preserve terminal template on restart
matthewlouisbrockman 2b3245a
Harden dashboard terminal edge cases
matthewlouisbrockman 8cf74ca
Harden terminal session storage
matthewlouisbrockman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import '@xterm/xterm/css/xterm.css' | ||
|
|
||
| export default function TerminalLayout({ | ||
| children, | ||
| }: { | ||
| children: React.ReactNode | ||
| }) { | ||
| return children | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,297 @@ | ||
| import Link from 'next/link' | ||
| import type { Metadata } from 'next/types' | ||
| import { SUPABASE_AUTH_HEADERS } from '@/configs/api' | ||
| import { AUTH_URLS } from '@/configs/urls' | ||
| import type { TeamModel } from '@/core/modules/teams/models' | ||
| import { createUserTeamsRepository } from '@/core/modules/teams/user-teams-repository.server' | ||
| import { | ||
| createDefaultTemplatesRepository, | ||
| createTemplatesRepository, | ||
| } from '@/core/modules/templates/repository.server' | ||
| import { getSessionInsecure } from '@/core/server/functions/auth/get-session' | ||
| import getUserByToken from '@/core/server/functions/auth/get-user-by-token' | ||
| import { resolveUserTeam } from '@/core/server/functions/team/resolve-user-team' | ||
| import { infra } from '@/core/shared/clients/api' | ||
| import { SandboxIdSchema } from '@/core/shared/schemas/api' | ||
| import DashboardTerminal from '@/features/dashboard/terminal/dashboard-terminal' | ||
| import { normalizeTerminalTemplate } from '@/features/dashboard/terminal/template' | ||
| import { Button } from '@/ui/primitives/button' | ||
|
|
||
| export const metadata: Metadata = { | ||
| title: 'Terminal - E2B', | ||
| robots: 'noindex, nofollow', | ||
| } | ||
|
|
||
| interface TerminalPageProps { | ||
| searchParams: Promise<{ | ||
| command?: string | ||
| sandboxId?: string | ||
| template?: string | ||
| }> | ||
| } | ||
|
|
||
| export default async function TerminalPage({ | ||
| searchParams, | ||
| }: TerminalPageProps) { | ||
| const { command = '', sandboxId, template } = await searchParams | ||
| const terminalTemplate = normalizeTerminalTemplate(template) | ||
| const terminalSandboxId = normalizeTerminalSandboxId(sandboxId) | ||
|
|
||
| if (!terminalTemplate) { | ||
| return <TerminalUnavailable message="The terminal template is invalid." /> | ||
| } | ||
|
|
||
| if (terminalSandboxId === null) { | ||
| return <TerminalUnavailable message="The terminal sandbox ID is invalid." /> | ||
| } | ||
|
|
||
| const session = await getSessionInsecure() | ||
| const { data, error } = await getUserByToken(session?.access_token) | ||
|
|
||
| if (error || !data.user || !session) { | ||
| return ( | ||
| <TerminalSignIn | ||
| sandboxId={terminalSandboxId} | ||
| template={terminalTemplate} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| const teamsRepository = createUserTeamsRepository({ | ||
| accessToken: session.access_token, | ||
| }) | ||
| const teamsResult = await teamsRepository.listUserTeams() | ||
|
|
||
| if (!teamsResult.ok) { | ||
| return <TerminalUnavailable /> | ||
| } | ||
|
|
||
| const resolvedTeam = await resolveUserTeam(data.user.id, session.access_token) | ||
| const team = terminalSandboxId | ||
| ? await resolveTerminalSandboxTeam({ | ||
| accessToken: session.access_token, | ||
| preferredTeamId: resolvedTeam?.id, | ||
| sandboxId: terminalSandboxId, | ||
| teams: teamsResult.data, | ||
| }) | ||
| : teamsResult.data.find((candidate) => candidate.id === resolvedTeam?.id) | ||
|
|
||
| if (!team) { | ||
| return <TerminalUnavailable /> | ||
| } | ||
|
|
||
| const templateAvailable = terminalSandboxId | ||
| ? { ok: true as const, available: true } | ||
| : await isTerminalTemplateAvailable({ | ||
| accessToken: session.access_token, | ||
| teamId: team.id, | ||
| template: terminalTemplate, | ||
| }) | ||
|
|
||
| if (!templateAvailable.ok) { | ||
| return ( | ||
| <TerminalUnavailable message="We could not verify the terminal template for this account." /> | ||
| ) | ||
| } | ||
|
|
||
| if (!templateAvailable.available) { | ||
| return ( | ||
| <TerminalUnavailable | ||
| message={`Template "${terminalTemplate}" is not available for this account.`} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| return ( | ||
| <main className="h-dvh min-h-[360px] bg-bg p-3"> | ||
| <DashboardTerminal | ||
| autoStart | ||
| initialCommand={command} | ||
| initialSandboxId={terminalSandboxId} | ||
| initialTemplate={terminalTemplate} | ||
| teamId={team.id} | ||
| /> | ||
| </main> | ||
| ) | ||
| } | ||
|
|
||
| function normalizeTerminalSandboxId(sandboxId?: string) { | ||
| const value = sandboxId?.trim() | ||
| if (!value) return undefined | ||
|
|
||
| const parsedSandboxId = SandboxIdSchema.safeParse(value) | ||
| return parsedSandboxId.success ? parsedSandboxId.data : null | ||
| } | ||
|
|
||
| async function resolveTerminalSandboxTeam({ | ||
| accessToken, | ||
| preferredTeamId, | ||
| sandboxId, | ||
| teams, | ||
| }: { | ||
| accessToken: string | ||
| preferredTeamId?: string | ||
| sandboxId: string | ||
| teams: TeamModel[] | ||
| }) { | ||
| if (preferredTeamId) { | ||
| const preferredTeam = teams.find((team) => team.id === preferredTeamId) | ||
| if ( | ||
| preferredTeam && | ||
| (await hasSandboxInTeam({ | ||
| accessToken, | ||
| sandboxId, | ||
| teamId: preferredTeam.id, | ||
| })) | ||
| ) { | ||
| return preferredTeam | ||
| } | ||
| } | ||
|
|
||
| const candidateTeams = teams.filter((team) => team.id !== preferredTeamId) | ||
| const teamMatches = await Promise.all( | ||
| candidateTeams.map(async (team) => ({ | ||
| team, | ||
| ownsSandbox: await hasSandboxInTeam({ | ||
| accessToken, | ||
| sandboxId, | ||
| teamId: team.id, | ||
| }), | ||
| })) | ||
| ) | ||
|
|
||
| return teamMatches.find((match) => match.ownsSandbox)?.team ?? null | ||
| } | ||
|
|
||
| async function hasSandboxInTeam({ | ||
| accessToken, | ||
| sandboxId, | ||
| teamId, | ||
| }: { | ||
| accessToken: string | ||
| sandboxId: string | ||
| teamId: string | ||
| }) { | ||
| try { | ||
| const result = await infra.GET('/sandboxes/{sandboxID}', { | ||
| params: { | ||
| path: { | ||
| sandboxID: sandboxId, | ||
| }, | ||
| }, | ||
| headers: { | ||
| ...SUPABASE_AUTH_HEADERS(accessToken, teamId), | ||
| }, | ||
| cache: 'no-store', | ||
| }) | ||
|
|
||
| return result.response.ok && Boolean(result.data) | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| async function isTerminalTemplateAvailable({ | ||
| accessToken, | ||
| teamId, | ||
| template, | ||
| }: { | ||
| accessToken: string | ||
| teamId: string | ||
| template: string | ||
| }) { | ||
| if (template === 'base') { | ||
| return { ok: true as const, available: true } | ||
| } | ||
|
|
||
| const defaultTemplatesRepository = createDefaultTemplatesRepository({ | ||
| accessToken, | ||
| }) | ||
| const teamTemplatesRepository = createTemplatesRepository({ | ||
| accessToken, | ||
| teamId, | ||
| }) | ||
| const [defaultTemplates, teamTemplates] = await Promise.all([ | ||
| defaultTemplatesRepository.getDefaultTemplatesCached(), | ||
| teamTemplatesRepository.getTeamTemplates(), | ||
| ]) | ||
|
|
||
| if (!defaultTemplates.ok || !teamTemplates.ok) { | ||
| return { ok: false as const } | ||
| } | ||
|
|
||
| const templates = [ | ||
| ...defaultTemplates.data.templates, | ||
| ...teamTemplates.data.templates, | ||
| ] | ||
|
|
||
| return { | ||
| ok: true as const, | ||
| available: templates.some((candidate) => | ||
| [ | ||
| candidate.templateID, | ||
| ...(candidate.aliases ?? []), | ||
| ...(candidate.names ?? []), | ||
| ].includes(template) | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| function TerminalSignIn({ | ||
| sandboxId, | ||
| template, | ||
| }: { | ||
| sandboxId?: string | ||
| template: string | ||
| }) { | ||
| const returnToParams = new URLSearchParams() | ||
|
|
||
| if (template) { | ||
| returnToParams.set('template', template) | ||
| } | ||
|
|
||
| if (sandboxId) { | ||
| returnToParams.set('sandboxId', sandboxId) | ||
| } | ||
|
|
||
| const returnToQuery = returnToParams.toString() | ||
| const returnTo = `/dashboard/terminal${ | ||
| returnToQuery ? `?${returnToQuery}` : '' | ||
| }` | ||
| const signInHref = `${AUTH_URLS.SIGN_IN}?${new URLSearchParams({ | ||
| returnTo, | ||
| }).toString()}` | ||
|
|
||
| return ( | ||
| <main className="flex h-dvh min-h-[360px] items-center justify-center bg-bg p-6"> | ||
| <div className="flex max-w-sm flex-col items-center gap-4 text-center"> | ||
| <div> | ||
| <h1 className="text-lg font-medium">Sign in to open a terminal</h1> | ||
| <p className="text-fg-secondary mt-2 text-sm"> | ||
| The terminal runs in your E2B dashboard account. | ||
| </p> | ||
| </div> | ||
| <Button asChild> | ||
| <Link href={signInHref} target="_top"> | ||
| Sign in | ||
| </Link> | ||
| </Button> | ||
| </div> | ||
| </main> | ||
| ) | ||
| } | ||
|
|
||
| function TerminalUnavailable({ | ||
| message = 'We could not resolve a dashboard team for this account.', | ||
| }: { | ||
| message?: string | ||
| }) { | ||
| return ( | ||
| <main className="flex h-dvh min-h-[360px] items-center justify-center bg-bg p-6"> | ||
| <div className="max-w-sm text-center"> | ||
| <h1 className="text-lg font-medium">Terminal unavailable</h1> | ||
| <p className="text-fg-secondary mt-2 text-sm">{message}</p> | ||
| </div> | ||
| </main> | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export const TERMINAL_SANDBOX_TIMEOUT_MS = 30 * 60 * 1000 | ||
| export const DEFAULT_COLS = 100 | ||
| export const DEFAULT_ROWS = 28 | ||
| export const DEFAULT_PANEL_HEIGHT = 260 | ||
| export const MAX_TERMINAL_TRANSCRIPT_CHARS = 200_000 | ||
| export const TERMINAL_SESSION_STORAGE_PREFIX = 'dashboard-terminal-session' | ||
| export const DEFAULT_CWD = '/home/user' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.