108 lines
3.0 KiB
Vue
108 lines
3.0 KiB
Vue
<script setup lang="ts">
|
|
/**
|
|
* The run-scoped sandbox view: the live output of a running attempt's
|
|
* container, read-only (docs/prds/external-agent-harness.md, "Run target").
|
|
* The server refuses a finished or unknown attempt by writing its reason to
|
|
* the socket as text, so the operator reads why there is no view in the
|
|
* terminal itself. No input is sent: this is the peek, not a takeover.
|
|
*/
|
|
import { onBeforeUnmount, onMounted, ref } from "vue";
|
|
import { Terminal } from "@xterm/xterm";
|
|
import { FitAddon } from "@xterm/addon-fit";
|
|
import "@xterm/xterm/css/xterm.css";
|
|
|
|
const props = defineProps<{ taskId: string; attempt: number }>();
|
|
|
|
const host = ref<HTMLDivElement | null>(null);
|
|
const status = ref<"connecting" | "open" | "closed" | "failed">("connecting");
|
|
|
|
let term: Terminal | null = null;
|
|
let socket: WebSocket | null = null;
|
|
let fit: FitAddon | null = null;
|
|
let observer: ResizeObserver | null = null;
|
|
|
|
function fitLocal(): void {
|
|
try {
|
|
fit?.fit();
|
|
} catch {
|
|
// xterm throws when the host has no measurable box yet; fitting is
|
|
// cosmetic and must not fail the view.
|
|
}
|
|
}
|
|
|
|
onMounted(() => {
|
|
const mount = host.value;
|
|
if (!mount) return;
|
|
|
|
term = new Terminal({
|
|
convertEol: true,
|
|
disableStdin: true,
|
|
cursorBlink: false,
|
|
fontFamily: "'IBM Plex Mono', ui-monospace, monospace",
|
|
fontSize: 13,
|
|
theme: { background: "#0b0f14", foreground: "#e6edf3" },
|
|
});
|
|
fit = new FitAddon();
|
|
term.loadAddon(fit);
|
|
term.open(mount);
|
|
fitLocal();
|
|
|
|
const scheme = window.location.protocol === "https:" ? "wss:" : "ws:";
|
|
const query = new URLSearchParams({ attempt: String(props.attempt) });
|
|
const ws = new WebSocket(
|
|
`${scheme}//${window.location.host}/api/tasks/${encodeURIComponent(props.taskId)}/sandbox?${query}`,
|
|
);
|
|
socket = ws;
|
|
ws.binaryType = "arraybuffer";
|
|
ws.onopen = () => {
|
|
status.value = "open";
|
|
};
|
|
ws.onmessage = (event: MessageEvent) => {
|
|
if (!term) return;
|
|
if (typeof event.data === "string") status.value = "failed";
|
|
term.write(
|
|
typeof event.data === "string"
|
|
? event.data
|
|
: new Uint8Array(event.data as ArrayBuffer),
|
|
);
|
|
};
|
|
ws.onerror = () => {
|
|
status.value = "failed";
|
|
};
|
|
ws.onclose = () => {
|
|
if (status.value !== "failed") status.value = "closed";
|
|
};
|
|
|
|
observer = new ResizeObserver(fitLocal);
|
|
observer.observe(mount);
|
|
});
|
|
|
|
onBeforeUnmount(() => {
|
|
observer?.disconnect();
|
|
observer = null;
|
|
socket?.close();
|
|
socket = null;
|
|
term?.dispose();
|
|
term = null;
|
|
fit = null;
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<div class="flex h-[32rem] min-h-0 flex-col gap-2">
|
|
<p
|
|
v-if="status !== 'open'"
|
|
class="text-sm text-fg-muted"
|
|
role="status"
|
|
>
|
|
<template v-if="status === 'connecting'">Connecting…</template>
|
|
<template v-else-if="status === 'closed'">The sandbox ended.</template>
|
|
<template v-else>Sandbox unavailable.</template>
|
|
</p>
|
|
<div
|
|
ref="host"
|
|
class="min-h-0 flex-1 overflow-hidden rounded-md border border-border bg-[#0b0f14] p-2"
|
|
/>
|
|
</div>
|
|
</template>
|