Files
opencode-manager/frontend/src/api/terminals.ts
T
Chris Scott c60a9b35f2 fix(terminal-dev-loop): address branch review findings
Security:
- Reject same-site and cross-site state-changing requests on cookie-authenticated routes unless the origin is trusted
- Drop Manager and ocm_preview Set-Cookie headers from preview responses
- Show and hash the full executable content of .ocm/project.json at trust time
- Restrict action URLs to http(s) and refuse symlinked repo config paths
- Keep the preview start redirect on the gateway origin

Lifecycle:
- Stop terminals in OpenCode workspace siblings when a repo is deleted
- Report preview as unavailable instead of crashing when the gateway port cannot be bound

Performance:
- Skip branch lookups when validating terminal and project-config directories
- Stop the /proc scan once all listening sockets are mapped and pause port polling while a preview is open
- Close slow WebSocket clients with 1013 so they reconnect with replay
- Lazy-load the xterm terminal view and skip unchanged resize requests
- Request uncompressed responses from preview targets

Cleanup:
- Share one repo directory resolver, trusted-origins parser, cookie prefix, WebSocket frame forwarder, header filter, icon map and dialog-param helper
- Remove unused props, options and exports

Docs: update terminal, preview, project actions, sandboxing, internal API and environment pages
2026-10-03 10:00:33 -04:00

83 lines
2.8 KiB
TypeScript

import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import type { CreateTerminalRequest, ResizeTerminalRequest, TerminalInfo } from '@opencode-manager/shared/types'
import { API_BASE_URL } from '@/config'
import { fetchWrapper, fetchWrapperVoid } from './fetchWrapper'
function terminalsQueryKey(repoId: number, directory: string | undefined) {
return ['terminals', repoId, directory ?? null] as const
}
function terminalsRepoQueryKey(repoId: number) {
return ['terminals', repoId] as const
}
async function listTerminals(repoId: number, directory?: string): Promise<TerminalInfo[]> {
const data = await fetchWrapper<{ terminals: TerminalInfo[] }>(
`${API_BASE_URL}/api/repos/${repoId}/terminals`,
{ params: { directory } },
)
return data.terminals
}
async function createTerminal(repoId: number, body: CreateTerminalRequest): Promise<TerminalInfo> {
return fetchWrapper<TerminalInfo>(`${API_BASE_URL}/api/repos/${repoId}/terminals`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
}
export async function resizeTerminal(repoId: number, ptyID: string, body: ResizeTerminalRequest): Promise<void> {
return fetchWrapperVoid(`${API_BASE_URL}/api/repos/${repoId}/terminals/${encodeURIComponent(ptyID)}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
}
async function removeTerminal(repoId: number, ptyID: string, directory?: string): Promise<void> {
return fetchWrapperVoid(`${API_BASE_URL}/api/repos/${repoId}/terminals/${encodeURIComponent(ptyID)}`, {
method: 'DELETE',
params: { directory },
})
}
interface UseTerminalsOptions {
enabled: boolean
refetchInterval?: number | false
}
export function useTerminals(repoId: number, directory: string | undefined, options: UseTerminalsOptions) {
return useQuery({
queryKey: terminalsQueryKey(repoId, directory),
queryFn: () => listTerminals(repoId, directory),
enabled: options.enabled,
refetchInterval: options.refetchInterval,
})
}
export function useCreateTerminal(repoId: number) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (body: CreateTerminalRequest) => createTerminal(repoId, body),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: terminalsRepoQueryKey(repoId) })
},
})
}
interface RemoveTerminalVariables {
ptyID: string
directory?: string
}
export function useRemoveTerminal(repoId: number) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: ({ ptyID, directory }: RemoveTerminalVariables) => removeTerminal(repoId, ptyID, directory),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: terminalsRepoQueryKey(repoId) })
},
})
}