import { useEffect, useRef, useState } from 'react' import { ChevronDown, ChevronUp, Loader2, MoveDownRight, Square, Terminal } from 'lucide-react' import type { SessionMessageInfo } from '@opencode-manager/shared/opencode' import { readShellOutput, type ShellInfo } from '@/api/opencode' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { useBackgroundSession } from '@/hooks/useOpenCode' import { useKillShell, useSessionShells } from '@/hooks/useSessionShells' const SHELL_OUTPUT_LIMIT = 50_000 const SHELL_OUTPUT_POLL_INTERVAL_MS = 1000 const BACKGROUNDABLE_TOOLS = new Set(['shell', 'subagent']) function hasBackgroundableWork(messages: SessionMessageInfo[]): boolean { for (let index = messages.length - 1; index >= 0; index -= 1) { const message = messages[index] if (message?.type !== 'assistant' || message.time.completed !== undefined) continue return message.content.some( (part) => part.type === 'tool' && part.state.status === 'running' && BACKGROUNDABLE_TOOLS.has(part.name), ) } return false } interface ShellOutputState { output: string truncated: boolean } function appendShellOutput(previous: ShellOutputState, output: string, truncated: boolean): ShellOutputState { const next = previous.output + output if (next.length <= SHELL_OUTPUT_LIMIT) return { output: next, truncated: previous.truncated || truncated } return { output: next.slice(-SHELL_OUTPUT_LIMIT), truncated: true } } function useShellOutput(shellID: string, directory: string, running: boolean): ShellOutputState { const [state, setState] = useState({ output: '', truncated: false }) const cursorRef = useRef(0) useEffect(() => { let cancelled = false let loading = false const load = async () => { if (loading) return loading = true let cursor = cursorRef.current let output = '' let truncated = false try { for (;;) { const chunk = await readShellOutput(shellID, directory, cursor, SHELL_OUTPUT_LIMIT) output += chunk.output if (chunk.truncated) truncated = true if (chunk.cursor <= cursor) break cursor = chunk.cursor if (cursor >= chunk.size) break const tail = chunk.size - SHELL_OUTPUT_LIMIT if (tail > cursor) { cursor = tail output = '' truncated = true } } } catch { return } finally { loading = false if (!cancelled) { cursorRef.current = cursor if (output || truncated) setState((previous) => appendShellOutput(previous, output, truncated)) } } } void load() if (!running) { return () => { cancelled = true } } const interval = setInterval(() => void load(), SHELL_OUTPUT_POLL_INTERVAL_MS) return () => { cancelled = true clearInterval(interval) } }, [shellID, directory, running]) return state } function ShellOutputView({ shell, directory, running }: { shell: ShellInfo; directory: string; running: boolean }) { const { output, truncated } = useShellOutput(shell.id, directory, running) const bottomRef = useRef(null) useEffect(() => { bottomRef.current?.scrollIntoView({ block: 'end' }) }, [output]) return (
        {truncated && [earlier output omitted]{'\n'}}
        {output || {running ? 'Waiting for output…' : 'No output'}}
      
) } interface BackgroundWorkBarProps { sessionID: string directory: string messages: SessionMessageInfo[] isSessionActive: boolean } export function BackgroundWorkBar({ sessionID, directory, messages, isSessionActive }: BackgroundWorkBarProps) { const shells = useSessionShells(sessionID, directory) const killShell = useKillShell(directory) const backgroundSession = useBackgroundSession() const [expanded, setExpanded] = useState(false) const [viewing, setViewing] = useState(null) const canBackground = isSessionActive && hasBackgroundableWork(messages) const viewingRunning = viewing !== null && shells.some((shell) => shell.id === viewing.id) if (!canBackground && shells.length === 0 && viewing === null) return null return (
{canBackground && ( )} {shells.length > 0 && ( )}
{expanded && shells.length > 0 && (
    {shells.map((shell) => (
  • {shell.command}
  • ))}
)} { if (!open) setViewing(null) }}> {viewing?.command} {viewingRunning ? 'Running in the background' : 'Finished'} {viewing && }
) }