feat: add static previews to the room pane

The preview tool could only name a port, so a static build needed a
server the terminal had to keep alive. `preview` now accepts a path:
the app reads the file or folder over the room's ssh sandbox and serves
it from a signed host, with `..` and dotfiles refused and a 16 MiB cap.

The target is stored with the room and replayed as a durable event. The
pane shows a static path with a reload control and keeps the port field
as a local fallback.
This commit is contained in:
vhaudiquet committed 2026-11-28 11:24:57 +01:00
1 parent 5be17cf4ef
commit 36de0aace7
20 files changed
+1046 -145

No files matched your search

+9
View File
@@ -1470,6 +1470,15 @@ body.pane-resizing * {
border-bottom: 1px solid var(--border);
}
.preview-target {
min-width: 0;
overflow: hidden;
color: var(--muted);
font-size: var(--fs-meta);
text-overflow: ellipsis;
white-space: nowrap;
}
.port-field {
width: 88px;
min-height: 28px;
+117 -26
View File
@@ -1,25 +1,38 @@
<script setup lang="ts">
import { ref } from 'vue'
import { Maximize2, Minimize2, X } from 'lucide-vue-next'
import { computed, ref, watch } from 'vue'
import { Maximize2, Minimize2, RefreshCw, X } from 'lucide-vue-next'
import type { PreviewResponse, PreviewTarget } from '~/types'
const props = defineProps<{ roomId: string }>()
const props = defineProps<{ roomId: string; target: PreviewTarget | null }>()
const port = ref<number | string>(3000)
const url = ref('')
const frameKey = ref(0)
const loading = ref(false)
const error = ref('')
const fullscreen = ref(false)
const manual = ref(false)
async function load() {
const value = Number(port.value)
if (!Number.isInteger(value) || value < 1 || value > 65535) {
error.value = 'Enter a port between 1 and 65535.'
return
}
const staticTarget = computed(() =>
!manual.value && props.target?.kind === 'static' ? props.target : null,
)
const targetPath = computed(() => staticTarget.value?.path ?? '')
const actionLabel = computed(() => (loading.value ? 'Opening' : url.value ? 'Reload' : 'Go'))
const frameTitle = computed(() => {
if (props.target?.kind === 'static' && !manual.value) return `Preview of ${props.target.path}`
return `Preview on port ${port.value}`
})
const emptyText = computed(() =>
props.target
? 'The preview has not loaded.'
: 'No preview yet. Enter a port, or ask @rhema to open one.',
)
async function request(query: string) {
loading.value = true
error.value = ''
try {
const response = await fetch(`/api/rooms/${encodeURIComponent(props.roomId)}/preview?port=${value}`, {
const response = await fetch(`/api/rooms/${encodeURIComponent(props.roomId)}/preview${query}`, {
credentials: 'include',
headers: { Accept: 'application/json' },
})
@@ -41,7 +54,8 @@ async function load() {
if (!body || typeof body !== 'object' || typeof (body as { url?: unknown }).url !== 'string') {
throw new Error('The server returned no preview address.')
}
url.value = (body as { url: string }).url
url.value = (body as PreviewResponse).url
frameKey.value += 1
} catch (err) {
url.value = ''
error.value = err instanceof Error ? err.message : 'The preview could not be opened.'
@@ -50,27 +64,103 @@ async function load() {
}
}
function openTarget() {
if (!props.target) return openPort()
manual.value = false
return request('')
}
function openPort() {
const value = Number(port.value)
if (!Number.isInteger(value) || value < 1 || value > 65535) {
error.value = 'Enter a port between 1 and 65535.'
return
}
manual.value = true
return request(`?port=${value}`)
}
function open() {
return manual.value || !props.target ? openPort() : openTarget()
}
function startManual() {
manual.value = true
url.value = ''
error.value = ''
if (props.target?.kind === 'port') port.value = props.target.port
}
function useTarget() {
manual.value = false
if (props.target?.kind === 'port') port.value = props.target.port
void openTarget()
}
function toggleFullscreen() {
fullscreen.value = !fullscreen.value
}
watch(
() => props.target,
(target) => {
manual.value = false
if (!target) {
url.value = ''
error.value = ''
return
}
if (target.kind === 'port') port.value = target.port
void openTarget()
},
{ immediate: true },
)
</script>
<template>
<div class="preview-wrap" :class="{ 'preview-full': fullscreen }">
<div class="preview-bar">
<label class="sr-only" for="preview-port">Port</label>
<input
id="preview-port"
v-model.number="port"
class="input port-field"
type="number"
min="1"
max="65535"
aria-label="Preview port"
>
<button type="button" class="btn btn-sm" :disabled="loading" @click="load">
{{ loading ? 'Opening' : url ? 'Reload' : 'Go' }}
</button>
<template v-if="staticTarget">
<span class="preview-target mono" :title="targetPath">{{ targetPath }}</span>
<button type="button" class="btn btn-sm btn-quiet" @click="startManual">
Port
</button>
<button
type="button"
class="icon-btn"
aria-label="Reload preview"
title="Reload preview"
:disabled="loading"
@click="openTarget"
>
<RefreshCw :size="16" aria-hidden="true" />
</button>
</template>
<template v-else>
<label class="sr-only" for="preview-port">Port</label>
<input
id="preview-port"
v-model.number="port"
class="input port-field"
type="number"
min="1"
max="65535"
aria-label="Preview port"
@input="manual = true"
@keyup.enter="open"
>
<button type="button" class="btn btn-sm" :disabled="loading" @click="open">
{{ actionLabel }}
</button>
<button
v-if="target && manual"
type="button"
class="btn btn-sm btn-quiet"
@click="useTarget"
>
Room preview
</button>
</template>
<div class="room-topbar-spacer" />
<button
type="button"
@@ -97,14 +187,15 @@ function toggleFullscreen() {
<div v-if="url" class="preview-frame-wrap">
<iframe
:key="frameKey"
class="preview-frame"
:src="url"
:title="`Preview on port ${port}`"
:title="frameTitle"
sandbox="allow-scripts allow-same-origin allow-forms allow-popups"
/>
</div>
<p v-else-if="!error" class="preview-message">
Enter the port the room serves and choose Go.
{{ emptyText }}
</p>
</div>
</template>
+3 -2
View File
@@ -1,10 +1,11 @@
<script setup lang="ts">
import { ref } from 'vue'
import { Eye, FolderTree, PanelRightClose, Terminal } from 'lucide-vue-next'
import type { PreviewTarget } from '~/types'
type Tab = 'terminal' | 'preview' | 'files'
defineProps<{ roomId: string; roomName: string }>()
defineProps<{ roomId: string; roomName: string; preview: PreviewTarget | null }>()
const emit = defineEmits<{ close: [] }>()
const tab = ref<Tab>('files')
@@ -49,7 +50,7 @@ const tabs: Array<{ id: Tab; label: string; icon: typeof Terminal }> = [
<div id="pane-panel" class="pane-body" role="tabpanel" :aria-labelledby="`pane-tab-${tab}`">
<FilesPane v-if="tab === 'files'" :room-id="roomId" :room-name="roomName" />
<TerminalPane v-else-if="tab === 'terminal'" :room-id="roomId" />
<PreviewPane v-else :room-id="roomId" />
<PreviewPane v-else :room-id="roomId" :target="preview" />
</div>
</aside>
</template>
+10 -1
View File
@@ -8,6 +8,7 @@ import type {
MessagePage,
NoticeData,
PresencePeer,
PreviewTarget,
Room,
RoomDetailResponse,
StepStartData,
@@ -36,6 +37,7 @@ const detail = ref<RoomDetailResponse | null>(null)
const detailLoading = ref(true)
const detailError = ref('')
const previewTarget = ref<PreviewTarget | null>(null)
const messages = ref<Message[]>([])
const notices = ref<Array<{ seq: number; level: string; text: string }>>([])
const activities = ref<TurnActivity[]>([])
@@ -96,7 +98,7 @@ let presenceSocket: WebSocket | null = null
let presenceTimer: number | null = null
let presenceRetry = 0
const DURABLE_KINDS = new Set(['message', 'notice', 'turn_start', 'turn_end', 'tool_result'])
const DURABLE_KINDS = new Set(['message', 'notice', 'turn_start', 'turn_end', 'tool_result', 'preview'])
const BACKOFF = [1000, 2000, 5000, 10000]
useHead({
@@ -235,6 +237,11 @@ function handleEnvelope(envelope: Envelope) {
case 'message':
addMessage(eventData<Message>(envelope))
break
case 'preview': {
const data = eventData<PreviewTarget>(envelope)
if (data && (data.kind === 'port' || data.kind === 'static')) previewTarget.value = data
break
}
case 'notice': {
const data = eventData<NoticeData>(envelope)
addNotice(envelope.seq, data.level, data.text)
@@ -638,6 +645,7 @@ async function loadDetail() {
try {
const response = await api.get<RoomDetailResponse>(`/rooms/${roomId.value}`)
detail.value = response
previewTarget.value = response.preview ?? null
if (onlineIds.value.length === 0) {
onlineIds.value = response.members.filter((member) => member.online).map((member) => member.id)
}
@@ -863,6 +871,7 @@ onBeforeUnmount(() => {
v-if="paneOpen"
:room-id="detail.room.id"
:room-name="detail.room.name"
:preview="previewTarget"
@close="paneOpen = false"
/>
</div>
+6
View File
@@ -78,9 +78,14 @@ export interface Me {
color: string
}
export type PreviewTarget =
| { kind: 'port'; port: number }
| { kind: 'static'; path: string; root: string; entry: string }
export interface RoomDetailResponse {
room: Room
members: Member[]
preview: PreviewTarget | null
me: Me
}
@@ -214,6 +219,7 @@ export interface GitSummaryResponse {
export interface PreviewResponse {
url: string
target: PreviewTarget
}
export interface PresencePeer {