From 088b1d7b18a2f4e0ea33bdef92b64d09d99248d9 Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Sun, 21 Jun 2026 17:17:33 -0400 Subject: [PATCH] chore: update Docker config, dev-proxy, and HTML preview components --- Dockerfile | 2 +- backend/src/routes/dev-proxy.ts | 2 +- .../src/services/dev-server/proxy-utils.ts | 8 +- docker-compose.yml | 5 +- docs/configuration/docker.md | 26 +++--- .../components/file-browser/FileBrowser.tsx | 88 +++++++------------ .../html-preview/HtmlArtifactPanel.tsx | 2 +- .../html-preview/HtmlPreviewFrame.tsx | 3 +- frontend/src/components/message/TextPart.tsx | 4 +- 9 files changed, 53 insertions(+), 87 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7924a064..769a480f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -112,7 +112,7 @@ RUN chmod +x /docker-entrypoint.sh RUN mkdir -p /workspace /app/data /home/node/.cache /home/node/.opencode && \ chown -R node:node /workspace /app/data /home/node -EXPOSE 5003 5100 5101 5102 5103 +EXPOSE 5003 3055 HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \ CMD curl -f http://localhost:5003/api/health || exit 1 diff --git a/backend/src/routes/dev-proxy.ts b/backend/src/routes/dev-proxy.ts index 20647f68..838b0ae5 100644 --- a/backend/src/routes/dev-proxy.ts +++ b/backend/src/routes/dev-proxy.ts @@ -49,7 +49,6 @@ async function handleProxyRequest(c: Context, db: Database): Promise { return c.json({ error: 'Invalid repoId' }, 400) } - const config = getDevServerConfig(db, repoId) const port = getDevServerPort(db) if (isWebSocketUpgrade((key: string) => c.req.header(key))) { @@ -91,6 +90,7 @@ async function handleProxyRequest(c: Context, db: Database): Promise { const contentType = upstreamResponse.headers.get('content-type') ?? '' if (contentType.includes('text/html')) { + const config = getDevServerConfig(db, repoId) const text = await upstreamResponse.text() const basePath = `${DEV_PROXY_PREFIX}/${repoId}/` const modified = config.injectBase ? injectBaseTag(text, basePath) : rewriteDevProxyHtmlPaths(text, basePath) diff --git a/backend/src/services/dev-server/proxy-utils.ts b/backend/src/services/dev-server/proxy-utils.ts index 38b1a8d7..a7e4ac97 100644 --- a/backend/src/services/dev-server/proxy-utils.ts +++ b/backend/src/services/dev-server/proxy-utils.ts @@ -1,6 +1,6 @@ export const DEV_PROXY_PREFIX = '/api/dev-proxy' -export const HOP_BY_HOP_HEADERS = new Set([ +const HOP_BY_HOP_HEADERS = new Set([ 'connection', 'keep-alive', 'proxy-authenticate', @@ -166,8 +166,7 @@ function rewriteSrcSetCandidate(candidate: string, basePath: string): string { function toDevProxyPath(path: string, basePath: string): string { const normalizedBasePath = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath - const escapedBasePath = escapeRegExp(normalizedBasePath) - if (new RegExp(`^${escapedBasePath}(?:/|$)`).test(path)) return path + if (path === normalizedBasePath || path.startsWith(`${normalizedBasePath}/`)) return path return `${normalizedBasePath}${path}` } @@ -177,6 +176,3 @@ function isBaseTagAttribute(html: string, attributeOffset: number): boolean { return /^<\s*base\b/i.test(html.slice(tagStart, attributeOffset)) } -function escapeRegExp(value: string): string { - return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') -} diff --git a/docker-compose.yml b/docker-compose.yml index 00a02334..74340c0f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,10 +8,7 @@ services: container_name: opencode-manager ports: - "5003:5003" - - "5100:5100" - - "5101:5101" - - "5102:5102" - - "5103:5103" + - "3055:3055" environment: - NODE_ENV=${NODE_ENV:-production} - HOST=0.0.0.0 diff --git a/docs/configuration/docker.md b/docs/configuration/docker.md index 3c8bcf3f..53a29422 100644 --- a/docs/configuration/docker.md +++ b/docs/configuration/docker.md @@ -38,10 +38,7 @@ services: container_name: opencode-manager ports: - "5003:5003" - - "5100:5100" - - "5101:5101" - - "5102:5102" - - "5103:5103" + - "3055:3055" environment: - NODE_ENV=${NODE_ENV:-production} - HOST=0.0.0.0 @@ -146,19 +143,18 @@ ports: - "8080:5003" # Access at localhost:8080 ``` -### Dev Server Ports +### Dev Server Port -Ports 5100-5103 are exposed for running dev servers inside repositories: +The preview proxy reaches a repository's dev server through the main `5003` port at `/api/dev-proxy//`, so the dev server only needs to listen on a single port inside the container. That port defaults to `3055` and is configurable in **Settings** (`devServerPort`); agents receive it as the `$OCM_DEV_SERVER_PORT` environment variable. + +Exposing the port to the host is optional — the preview works entirely through `5003`. Map it only if you also want to hit the dev server directly: ```yaml ports: - - "5100:5100" - - "5101:5101" - - "5102:5102" - - "5103:5103" + - "3055:3055" ``` -Configure your dev server to use one of these ports: +Run your dev server on `$OCM_DEV_SERVER_PORT` and bind to `0.0.0.0`: === "Vite" @@ -166,7 +162,7 @@ Configure your dev server to use one of these ports: // vite.config.ts export default { server: { - port: 5100, + port: Number(process.env.OCM_DEV_SERVER_PORT) || 3055, host: '0.0.0.0' } } @@ -175,13 +171,13 @@ Configure your dev server to use one of these ports: === "Next.js" ```bash - next dev -p 5100 -H 0.0.0.0 + next dev -p $OCM_DEV_SERVER_PORT -H 0.0.0.0 ``` === "Express" ```javascript - app.listen(5100, '0.0.0.0') + app.listen(process.env.OCM_DEV_SERVER_PORT || 3055, '0.0.0.0') ``` ## Volume Mounts @@ -372,7 +368,7 @@ The container creates a default `AGENTS.md` file at `/workspace/.config/opencode Instructions for AI agents working in the container: - Reserved ports information -- Available dev server ports +- Dev server port (`$OCM_DEV_SERVER_PORT`, default 3055) - Docker-specific guidelines ### Editing diff --git a/frontend/src/components/file-browser/FileBrowser.tsx b/frontend/src/components/file-browser/FileBrowser.tsx index b88c38f1..59d1db7c 100644 --- a/frontend/src/components/file-browser/FileBrowser.tsx +++ b/frontend/src/components/file-browser/FileBrowser.tsx @@ -154,27 +154,27 @@ export const FileBrowser = forwardRef(funct const uploadCancelledRef = useRef(false) const isMobile = useMobile() + const openMobilePreview = useCallback((file: FileInfo) => { + if (isHtmlFileInfo(file)) { + setHtmlArtifact(createHtmlArtifact({ source: 'file', path: file.path, title: file.name })) + setIsPreviewModalOpen(false) + } else { + setHtmlArtifact(null) + setIsPreviewModalOpen(true) + } + onPreviewStateChange?.(true) + }, [onPreviewStateChange]) + const { data: initialFileData, error: initialFileError } = useFile(initialSelectedFile) useEffect(() => { if (initialFileData) { setSelectedFile(initialFileData) if (isMobile) { - if (isHtmlFileInfo(initialFileData)) { - setHtmlArtifact(createHtmlArtifact({ - source: 'file', - path: initialFileData.path, - title: initialFileData.name, - })) - setIsPreviewModalOpen(false) - } else { - setHtmlArtifact(null) - setIsPreviewModalOpen(true) - } - onPreviewStateChange?.(true) + openMobilePreview(initialFileData) } } -}, [initialFileData, isMobile, onPreviewStateChange]) +}, [initialFileData, isMobile, openMobilePreview]) useEffect(() => { if (initialFileError) { @@ -280,18 +280,7 @@ useEffect(() => { // On mobile, open preview in modal if (isMobile) { - if (isHtmlFileInfo(fullFileData)) { - setHtmlArtifact(createHtmlArtifact({ - source: 'file', - path: fullFileData.path, - title: fullFileData.name, - })) - setIsPreviewModalOpen(false) - } else { - setHtmlArtifact(null) - setIsPreviewModalOpen(true) - } - onPreviewStateChange?.(true) + openMobilePreview(fullFileData) } } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load file') @@ -299,23 +288,15 @@ useEffect(() => { } finally { setLoading(false) } - }, [onFileSelect, isMobile, onPreviewStateChange]) + }, [onFileSelect, isMobile, openMobilePreview]) - const handleCloseModal = useCallback(() => { + const handleClosePreview = useCallback(() => { setIsPreviewModalOpen(false) setHtmlArtifact(null) setSelectedFile(null) onPreviewStateChange?.(false) }, [onPreviewStateChange]) - const handleCloseHtmlArtifact = useCallback(() => { - setHtmlArtifact(null) - setSelectedFile(null) - onPreviewStateChange?.(false) - }, [onPreviewStateChange]) - - const handleToggleHtmlArtifactFullscreen = useCallback(() => {}, []) - const handleDirectoryClick = (path: string) => { loadFiles(path) } @@ -513,13 +494,13 @@ useEffect(() => { const handleEscape = (e: KeyboardEvent) => { if (e.key === 'Escape') { - handleCloseModal() + handleClosePreview() } } document.addEventListener('keydown', handleEscape) return () => document.removeEventListener('keydown', handleEscape) - }, [isPreviewModalOpen, handleCloseModal]) + }, [isPreviewModalOpen, handleClosePreview]) const showNavigateUp = allowNavigateAboveBase && normalizePath(currentPath) !== '..' ? true @@ -592,6 +573,15 @@ useEffect(() => { ) + const htmlArtifactPanel = isMobile && htmlArtifact && ( + + ) + if (embedded) { return (
{ {/* Mobile: File Preview Modal */} - {isMobile && htmlArtifact && ( - - )} + {htmlArtifactPanel}
) } @@ -790,19 +772,11 @@ useEffect(() => { {/* Mobile: File Preview Modal */} - {isMobile && htmlArtifact && ( - - )} + {htmlArtifactPanel} {uploadDialog} diff --git a/frontend/src/components/html-preview/HtmlArtifactPanel.tsx b/frontend/src/components/html-preview/HtmlArtifactPanel.tsx index bc559f5a..ad5dd105 100644 --- a/frontend/src/components/html-preview/HtmlArtifactPanel.tsx +++ b/frontend/src/components/html-preview/HtmlArtifactPanel.tsx @@ -14,7 +14,7 @@ interface HtmlArtifactPanelProps { isFullscreen: boolean isMobile: boolean onClose: () => void - onToggleFullscreen: () => void + onToggleFullscreen?: () => void } export function HtmlArtifactPanel({ diff --git a/frontend/src/components/html-preview/HtmlPreviewFrame.tsx b/frontend/src/components/html-preview/HtmlPreviewFrame.tsx index 2f79f425..9b49279b 100644 --- a/frontend/src/components/html-preview/HtmlPreviewFrame.tsx +++ b/frontend/src/components/html-preview/HtmlPreviewFrame.tsx @@ -1,3 +1,4 @@ +import { useMemo } from 'react' import { cn } from '@/lib/utils' import { normalizeHtmlPreviewDocument } from '@/lib/htmlArtifacts' @@ -11,7 +12,7 @@ interface HtmlPreviewFrameProps { export function HtmlPreviewFrame({ title, src, srcDoc, className, sandbox }: HtmlPreviewFrameProps) { const resolvedSandbox = sandbox ?? (src ? 'allow-scripts allow-same-origin' : 'allow-scripts') - const normalizedSrcDoc = srcDoc ? normalizeHtmlPreviewDocument(srcDoc) : undefined + const normalizedSrcDoc = useMemo(() => (srcDoc ? normalizeHtmlPreviewDocument(srcDoc) : undefined), [srcDoc]) return (