|
| 1 | +/** @jsxImportSource @opentui/solid */ |
| 2 | +/** |
| 3 | + * Launching View Component |
| 4 | + * |
| 5 | + * Shows controller agent initialization progress with spinner and log streaming. |
| 6 | + * Streams logs from the agent's log file using the same mechanism as workflow agents. |
| 7 | + */ |
| 8 | + |
| 9 | +import { createSignal, onMount, onCleanup, For, Show } from "solid-js" |
| 10 | +import { useTheme } from "@tui/shared/context/theme" |
| 11 | +import { Spinner } from "@tui/shared/components/spinner" |
| 12 | +import { useLogStream } from "../../workflow/hooks/useLogStream" |
| 13 | +import { LogLineInline } from "../../workflow/components/shared/log-line" |
| 14 | +import type { WorkflowEventBus } from "../../../../../workflows/events/event-bus" |
| 15 | +import type { OnboardingService } from "../../../../../workflows/onboarding/service" |
| 16 | + |
| 17 | +export interface LaunchingViewProps { |
| 18 | + /** Controller agent name being launched */ |
| 19 | + controllerName: string |
| 20 | + /** Event bus for receiving log messages */ |
| 21 | + eventBus: WorkflowEventBus |
| 22 | + /** Onboarding service to trigger launch */ |
| 23 | + service: OnboardingService |
| 24 | + /** Called when launch fails (optional) */ |
| 25 | + onError?: (error: string) => void |
| 26 | +} |
| 27 | + |
| 28 | +export function LaunchingView(props: LaunchingViewProps) { |
| 29 | + const themeCtx = useTheme() |
| 30 | + const [status, setStatus] = createSignal<'launching' | 'completed' | 'failed'>('launching') |
| 31 | + const [errorMessage, setErrorMessage] = createSignal<string | null>(null) |
| 32 | + const [monitoringId, setMonitoringId] = createSignal<number | undefined>(undefined) |
| 33 | + |
| 34 | + // Use the log stream hook to stream logs from the agent's log file |
| 35 | + const logStream = useLogStream(() => monitoringId()) |
| 36 | + |
| 37 | + onMount(() => { |
| 38 | + // Subscribe to monitoring ID event (emitted when agent starts) |
| 39 | + const unsubMonitor = props.eventBus.on('onboard:launching_monitor', (event) => { |
| 40 | + setMonitoringId(event.monitoringId) |
| 41 | + }) |
| 42 | + |
| 43 | + // Subscribe to completion event |
| 44 | + const unsubCompleted = props.eventBus.on('onboard:launching_completed', () => { |
| 45 | + setStatus('completed') |
| 46 | + }) |
| 47 | + |
| 48 | + // Subscribe to failure event |
| 49 | + const unsubFailed = props.eventBus.on('onboard:launching_failed', (event) => { |
| 50 | + setStatus('failed') |
| 51 | + setErrorMessage(event.error) |
| 52 | + props.onError?.(event.error) |
| 53 | + }) |
| 54 | + |
| 55 | + // Trigger the launch |
| 56 | + props.service.launchController() |
| 57 | + |
| 58 | + onCleanup(() => { |
| 59 | + unsubMonitor() |
| 60 | + unsubCompleted() |
| 61 | + unsubFailed() |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + const statusIcon = () => { |
| 66 | + switch (status()) { |
| 67 | + case 'completed': |
| 68 | + return '●' |
| 69 | + case 'failed': |
| 70 | + return '✗' |
| 71 | + default: |
| 72 | + return null |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + const statusColor = () => { |
| 77 | + switch (status()) { |
| 78 | + case 'completed': |
| 79 | + return themeCtx.theme.success |
| 80 | + case 'failed': |
| 81 | + return themeCtx.theme.error |
| 82 | + default: |
| 83 | + return themeCtx.theme.primary |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return ( |
| 88 | + <box flexDirection="column" gap={1}> |
| 89 | + {/* Header with spinner/status and controller name */} |
| 90 | + <box flexDirection="row" gap={1} alignItems="center"> |
| 91 | + {status() === 'launching' ? ( |
| 92 | + <Spinner color={themeCtx.theme.primary} /> |
| 93 | + ) : ( |
| 94 | + <text fg={statusColor()}>{statusIcon()}</text> |
| 95 | + )} |
| 96 | + <text fg={themeCtx.theme.text} attributes={1}> |
| 97 | + {status() === 'launching' |
| 98 | + ? `Initializing ${props.controllerName}...` |
| 99 | + : status() === 'completed' |
| 100 | + ? `${props.controllerName} initialized` |
| 101 | + : `Failed to initialize ${props.controllerName}`} |
| 102 | + </text> |
| 103 | + </box> |
| 104 | + |
| 105 | + {/* Thinking indicator */} |
| 106 | + <Show when={logStream.latestThinking && status() === 'launching'}> |
| 107 | + <box paddingLeft={2}> |
| 108 | + <text fg={themeCtx.theme.textMuted}>↳ {logStream.latestThinking}</text> |
| 109 | + </box> |
| 110 | + </Show> |
| 111 | + |
| 112 | + {/* Log streaming area */} |
| 113 | + <Show when={monitoringId() !== undefined}> |
| 114 | + <box flexDirection="column" paddingLeft={2} marginTop={1} height={12}> |
| 115 | + <Show when={logStream.isConnecting}> |
| 116 | + <text fg={themeCtx.theme.textMuted}>Connecting to agent logs...</text> |
| 117 | + </Show> |
| 118 | + |
| 119 | + <Show when={logStream.error}> |
| 120 | + <text fg={themeCtx.theme.error}>{logStream.error}</text> |
| 121 | + </Show> |
| 122 | + |
| 123 | + <Show when={!logStream.isConnecting && !logStream.error && logStream.lines.length > 0}> |
| 124 | + <scrollbox |
| 125 | + flexGrow={1} |
| 126 | + width="100%" |
| 127 | + stickyScroll={true} |
| 128 | + stickyStart="bottom" |
| 129 | + scrollbarOptions={{ |
| 130 | + showArrows: false, |
| 131 | + trackOptions: { |
| 132 | + foregroundColor: themeCtx.theme.info, |
| 133 | + backgroundColor: themeCtx.theme.borderSubtle, |
| 134 | + }, |
| 135 | + }} |
| 136 | + > |
| 137 | + <For each={logStream.lines.slice(-15)}> |
| 138 | + {(line) => <LogLineInline line={line} />} |
| 139 | + </For> |
| 140 | + </scrollbox> |
| 141 | + </Show> |
| 142 | + |
| 143 | + <Show when={!logStream.isConnecting && !logStream.error && logStream.lines.length === 0}> |
| 144 | + <text fg={themeCtx.theme.textMuted}>Waiting for output...</text> |
| 145 | + </Show> |
| 146 | + </box> |
| 147 | + </Show> |
| 148 | + |
| 149 | + {/* Status messages when no monitoring yet */} |
| 150 | + <Show when={monitoringId() === undefined && status() === 'launching'}> |
| 151 | + <box paddingLeft={2} marginTop={1}> |
| 152 | + <text fg={themeCtx.theme.textMuted}>Starting controller agent...</text> |
| 153 | + </box> |
| 154 | + </Show> |
| 155 | + |
| 156 | + {/* Error message if failed */} |
| 157 | + <Show when={status() === 'failed' && errorMessage()}> |
| 158 | + <box marginTop={1} paddingLeft={2}> |
| 159 | + <text fg={themeCtx.theme.error}>Error: {errorMessage()}</text> |
| 160 | + </box> |
| 161 | + </Show> |
| 162 | + |
| 163 | + {/* Footer hint */} |
| 164 | + <box marginTop={2}> |
| 165 | + <text fg={themeCtx.theme.textMuted}> |
| 166 | + {status() === 'launching' |
| 167 | + ? 'Please wait for controller agent to start...' |
| 168 | + : status() === 'failed' |
| 169 | + ? 'Press Escape to go back' |
| 170 | + : 'Starting workflow...'} |
| 171 | + </text> |
| 172 | + </box> |
| 173 | + </box> |
| 174 | + ) |
| 175 | +} |
0 commit comments