-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtui.tsx
More file actions
858 lines (793 loc) · 31.3 KB
/
Copy pathtui.tsx
File metadata and controls
858 lines (793 loc) · 31.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
/** @jsxImportSource @opentui/solid */
import type { TuiPlugin, TuiPluginApi, TuiPluginModule } from '@opencode-ai/plugin/tui'
import { createEffect, createMemo, createSignal, onCleanup, Show, untrack } from 'solid-js'
import { VERSION } from './version'
import { loadPluginConfig, resolveBundledContainerDir } from './setup'
import { resolveForgeDbPath, resolveDataDir } from './storage'
import type { ExecutionContextCache } from './utils/tui-execution-context-cache'
import { createExecutionContextCache } from './utils/tui-execution-context-cache'
import type { PluginConfig } from './types'
import { createSbxRuntime } from './sandbox/sbx'
import { buildAndLoadSandboxTemplate, DEFAULT_SANDBOX_IMAGE } from './sandbox/template'
import { runCommand } from './sandbox/process'
import { isSandboxConfigEnabled } from './sandbox/context'
import { tmpdir } from 'os'
import { existsSync } from 'fs'
import { resolveLoopPermissionOptions } from './constants/loop'
import { emitLoopPermissionConfigWarnings } from './utils/loop-permission-warnings'
import { connectForgeProject, resolveTuiProjectIdOnce, type ForgeProjectClient } from './utils/tui-client'
import { ExecutePlanPanel, type ExecutePlanPanelProps } from './tui/execute-plan-panel'
import {
awaitSessionSandboxState,
beginSessionSandboxStateRequest,
deriveSandboxPollDelayMs,
deriveSessionSandboxDisplayStatus,
hostSandboxToggleBlocked,
readSessionSandboxPreference,
} from './tui/session-sandbox-store'
import type { SessionSandboxPreference } from './tui/session-sandbox-store'
import { attachLoopSessionFollower, getCurrentRouteSessionId } from './tui/session-follow'
import { openInBrowser, startDashboardServer, type DashboardServerHandle } from './dashboard/launch'
import { describeDashboardBinding } from './dashboard/config'
import { normalizePastedPlanText } from './utils/marked-plan-parser'
type TuiKeybinds = {
executePlan: string
dashboard: string
toggleHostSandbox: string
}
const DEFAULT_KEYBINDS: TuiKeybinds = {
executePlan: '<leader>f',
dashboard: '',
toggleHostSandbox: '',
}
type TuiOptions = {
sidebar: boolean
showVersion: boolean
keybinds: TuiKeybinds
}
type ForgeConnectionStatus = 'connecting' | 'connected' | 'unavailable'
const SBX_SPINNER_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
function SandboxLoadingSpinner(props: { api: TuiPluginApi }) {
const [frame, setFrame] = createSignal(0)
const animationsEnabled = () => props.api.kv.get('animations_enabled', true)
createEffect(() => {
if (!animationsEnabled()) return
const timer = setInterval(() => setFrame((current) => (current + 1) % SBX_SPINNER_FRAMES.length), 80)
onCleanup(() => clearInterval(timer))
})
return <text fg={props.api.theme.current.textMuted}>{animationsEnabled() ? SBX_SPINNER_FRAMES[frame()] : '⋯'}</text>
}
function SandboxStatusText(props: { api: TuiPluginApi; preference: () => SessionSandboxPreference | null; sessionId?: string }) {
const theme = () => props.api.theme.current
const status = createMemo(() => deriveSessionSandboxDisplayStatus(props.preference(), props.sessionId))
const statusColor = () => {
const current = status()
if (current === 'enabled') return theme().secondary
if (current === 'failed') return theme().error
return theme().textMuted
}
// Secondary while the sandbox is actually acknowledged ON, so an active sandbox stands out
// against the muted status line instead of reading as ordinary chrome.
return (
<Show
when={status() === 'loading'}
fallback={<text fg={statusColor()}>· SBX {status()}</text>}
>
<box flexDirection="row" gap={1}>
<text fg={theme().textMuted}>· SBX</text>
<SandboxLoadingSpinner api={props.api} />
</box>
</Show>
)
}
function ForgeSidebarStatus(props: {
api: TuiPluginApi
opts: TuiOptions
status: () => ForgeConnectionStatus
preference: () => SessionSandboxPreference | null
sessionId?: string
}) {
const theme = () => props.api.theme.current
const title = createMemo(() => props.opts.showVersion ? `Forge v${VERSION}` : 'Forge')
const statusText = createMemo(() => props.status() === 'connecting' ? 'connecting' : 'RPC unavailable')
return (
<Show when={props.opts.sidebar}>
<box>
<box flexDirection="row" gap={1}>
<text fg={theme().text}>
<b>{title()}</b>
</text>
<SandboxStatusText api={props.api} preference={props.preference} sessionId={props.sessionId} />
<text fg={theme().textMuted}>· {statusText()}</text>
</box>
</box>
</Show>
)
}
function SidebarContainer(props: {
api: TuiPluginApi
client: () => ForgeProjectClient | null
cache: () => ExecutionContextCache | null
pluginConfig: PluginConfig
opts: TuiOptions
status: () => ForgeConnectionStatus
preference: () => SessionSandboxPreference | null
sessionId?: string
}) {
const currentClient = createMemo(() => props.client())
return (
<Show
when={currentClient()}
fallback={<ForgeSidebarStatus api={props.api} opts={props.opts} status={props.status} preference={props.preference} sessionId={props.sessionId} />}
>
{(client) => <Sidebar api={props.api} client={client()} cache={props.cache} pluginConfig={props.pluginConfig} opts={props.opts} preference={props.preference} sessionId={props.sessionId} />}
</Show>
)
}
function Sidebar(props: {
api: TuiPluginApi
client: ForgeProjectClient
cache: () => ExecutionContextCache | null
pluginConfig: PluginConfig
opts: TuiOptions
preference: () => SessionSandboxPreference | null
sessionId?: string
}) {
const theme = () => props.api.theme.current
const title = createMemo(() => {
return props.opts.showVersion ? `Forge v${VERSION}` : 'Forge'
})
return (
<Show when={props.opts.sidebar}>
<box>
<box flexDirection="row" gap={1}>
<text fg={theme().text}>
<b>{title()}</b>
</text>
<SandboxStatusText api={props.api} preference={props.preference} sessionId={props.sessionId} />
</box>
</box>
</Show>
)
}
/**
* Standalone wrapper around `ExecutePlanPanel`. The picker sub-dialogs
* (model, variant, loop name) need to fully replace the dialog stack,
* which means we lose the panel's component state every time the user
* touches one. The wrapper re-renders itself via `dialog.replace` when
* the panel reports a new selection, preserving the user's choices
* across picker round-trips. This mirrors the pattern the deleted
* `PlanViewerDialog` used internally.
*/
function ExecutionDialog(props: Omit<ExecutePlanPanelProps, 'onBack' | 'onExecuted' | 'onSelectionChanged'>) {
const theme = () => props.api.theme.current
return (
<box flexDirection="column" paddingX={2}>
<box flexShrink={0} paddingBottom={1} flexDirection="row" gap={1}>
<text fg={theme().text}>
<b>Execute plan</b>
</text>
</box>
<ExecutePlanPanel
api={props.api}
client={props.client}
cache={props.cache}
pluginConfig={props.pluginConfig}
planContent={props.planContent}
sessionId={props.sessionId}
initialExecutionModel={props.initialExecutionModel}
initialAuditorModel={props.initialAuditorModel}
initialExecutionVariant={props.initialExecutionVariant}
initialAuditorVariant={props.initialAuditorVariant}
initialLoopName={props.initialLoopName}
initialTarget={props.initialTarget}
projectDirectory={props.projectDirectory}
onBack={() => props.api.ui.dialog.clear()}
onSelectionChanged={({ executionModel, auditorModel, executionVariant, auditorVariant, loopName, target }) => {
props.cache?.setSelectionOverride({ executionModel, auditorModel, executionVariant, auditorVariant })
props.api.ui.dialog.setSize('xlarge')
props.api.ui.dialog.replace(() => (
<ExecutionDialog
api={props.api}
client={props.client}
cache={props.cache}
pluginConfig={props.pluginConfig}
planContent={props.planContent}
sessionId={props.sessionId}
initialExecutionModel={executionModel}
initialAuditorModel={auditorModel}
initialExecutionVariant={executionVariant}
initialAuditorVariant={auditorVariant}
initialLoopName={loopName}
initialTarget={target}
projectDirectory={props.projectDirectory}
/>
))
}}
/>
<box paddingTop={1} flexShrink={0} flexDirection="row" gap={2}>
<text fg={theme().textMuted} onMouseUp={() => props.api.ui.dialog.clear()}>Close (esc)</text>
</box>
</box>
)
}
function SandboxBuildDialog(props: {
api: TuiPluginApi
buildContextDir: string
image: string
browserControl: boolean
}) {
const theme = () => props.api.theme.current
const doBuild = async () => {
props.api.ui.dialog.clear()
props.api.ui.toast({ message: `Building sandbox template ${props.image}...`, variant: 'info', duration: 5000 })
const logger = { log: () => {}, error: () => {}, debug: () => {} }
try {
await buildAndLoadSandboxTemplate(props.buildContextDir, props.image, {
runCommand,
loadTemplate: (tar) => createSbxRuntime(logger).loadTemplate(tar),
logger,
tmpDir: tmpdir(),
}, { browserControl: props.browserControl })
props.api.ui.toast({
message: `Sandbox template ${props.image} built and loaded successfully`,
variant: 'success',
duration: 5000,
})
} catch (err) {
const message = err instanceof Error ? err.message : String(err)
props.api.ui.toast({ message, variant: 'error', duration: 10_000 })
}
}
return (
<box flexDirection="column" paddingX={2}>
<box flexShrink={0} paddingBottom={1} flexDirection="row" gap={1}>
<text fg={theme().text}>
<b>Build sandbox template</b>
</text>
</box>
<box paddingBottom={1}>
<text fg={theme().textMuted}>
This builds the sandbox image with Docker, then loads it into sbx.
</text>
</box>
<box paddingBottom={1}>
<text fg={theme().textMuted}>Image: {props.image}</text>
</box>
<box paddingBottom={1}>
<text fg={theme().textMuted}>Context: {props.buildContextDir}</text>
</box>
<box paddingBottom={1}>
<text fg={theme().textMuted}>Browser Control: {props.browserControl ? 'included' : 'excluded'}</text>
</box>
<box paddingTop={1} paddingX={1} flexShrink={0}>
<select
focused={true}
selectedIndex={0}
options={[
{ name: 'Build', description: 'Press enter to build the sandbox image', value: 'build' },
{ name: 'Cancel', description: 'Press enter to close this dialog', value: 'cancel' },
]}
onSelect={(_, option) => {
if (option?.value === 'build') {
void doBuild()
return
}
if (option?.value === 'cancel') {
props.api.ui.dialog.clear()
}
}}
showDescription={true}
itemSpacing={1}
wrapSelection={true}
textColor={theme().text}
focusedTextColor={theme().text}
selectedTextColor="#ffffff"
selectedBackgroundColor={theme().borderActive}
minHeight={4}
flexShrink={0}
/>
</box>
</box>
)
}
const id = 'oc-forge'
const tui: TuiPlugin = async (api) => {
const pluginConfig = loadPluginConfig()
const tuiConfig = pluginConfig.tui
const directory = api.state.path.directory
// Every TUI reader of the forge database resolves it here so a configured
// `dataDir` cannot leave the dashboard and the execute-plan dialog pointed at
// different databases.
const forgeDbPath = resolveForgeDbPath(pluginConfig.dataDir)
const opts: TuiOptions = {
sidebar: tuiConfig?.sidebar ?? true,
showVersion: tuiConfig?.showVersion ?? true,
keybinds: { ...DEFAULT_KEYBINDS, ...tuiConfig?.keybinds },
}
createEffect(() => {
if (!api.state.ready) return
emitLoopPermissionConfigWarnings(pluginConfig, pluginConfig.dataDir || resolveDataDir(), directory, {
logger: console,
onWarnings: (warnings) => {
api.ui.toast({ title: 'Forge loop permissions', message: warnings.join(' '), variant: 'warning', duration: 10_000 })
},
})
})
// Shared disposal flag for the sidebar client and the sandbox preference
// init/toggle paths. Registered here so it also guards work that runs with
// the sidebar disabled.
let disposed = false
let retryTimer: ReturnType<typeof setTimeout> | null = null
let sandboxPollTimer: ReturnType<typeof setTimeout> | null = null
let toggleWaiterController: AbortController | null = null
api.lifecycle.onDispose(() => {
disposed = true
if (retryTimer) {
clearTimeout(retryTimer)
retryTimer = null
}
if (sandboxPollTimer) {
clearTimeout(sandboxPollTimer)
sandboxPollTimer = null
}
if (toggleWaiterController) {
toggleWaiterController.abort()
toggleWaiterController = null
}
})
const sleepAbortable = (ms: number, signal: AbortSignal): Promise<void> =>
new Promise<void>((resolve) => {
if (signal.aborted) {
resolve()
return
}
const onAbort = (): void => {
clearTimeout(timer)
resolve()
}
const timer = setTimeout(() => {
signal.removeEventListener('abort', onAbort)
resolve()
}, ms)
signal.addEventListener('abort', onAbort, { once: true })
})
const [sandboxProjectId, setSandboxProjectId] = createSignal<string | null>(null)
const [sandboxPreference, setSandboxPreference] = createSignal<SessionSandboxPreference | null>(null)
let sandboxInitStarted = false
const preferenceFieldsEqual = (
a: SessionSandboxPreference | null,
b: SessionSandboxPreference | null,
): boolean => {
if (a === b) return true
if (!a || !b) return false
return (
JSON.stringify(a.desired) === JSON.stringify(b.desired) &&
JSON.stringify(a.applied) === JSON.stringify(b.applied) &&
JSON.stringify(a.controller) === JSON.stringify(b.controller) &&
JSON.stringify(a.activeLoopSandboxes) === JSON.stringify(b.activeLoopSandboxes)
)
}
const refreshSandboxAcknowledgement = (projectId: string): SessionSandboxPreference | null => {
if (disposed) return null
// When sandboxing is disabled by configuration the server never constructs a
// reconciler or uses that sandbox, so any persisted ON must not be displayed.
if (!isSandboxConfigEnabled(pluginConfig)) {
if (!disposed) setSandboxPreference(null)
return null
}
const pref = readSessionSandboxPreference(projectId, forgeDbPath)
if (!disposed && !preferenceFieldsEqual(sandboxPreference(), pref)) setSandboxPreference(pref)
return pref
}
// Shared acknowledgement-following loop. Whenever a new desired revision is
// written (initial restore or a toggle) it keeps polling the local preference
// pair until it settles, updating the acknowledged signal each step. This runs
// independently of the command timeout so a late acknowledgement — one the
// server applies after the toggle's 15s wait expires — still reaches the
// sidebar instead of leaving it stale until restart.
const ensureSandboxPolling = (projectId: string): void => {
if (disposed || sandboxPollTimer || !opts.sidebar) return
const step = (): void => {
if (disposed || !opts.sidebar) return
sandboxPollTimer = null
if (!isSandboxConfigEnabled(pluginConfig)) return
const pref = refreshSandboxAcknowledgement(projectId)
if (!pref) return
sandboxPollTimer = setTimeout(step, deriveSandboxPollDelayMs(pref))
}
step()
}
createEffect(() => {
if (!api.state.ready || sandboxInitStarted) return
sandboxInitStarted = true
void (async () => {
let projectId: string | null = null
let delayMs = 1000
for (let attempt = 1; attempt <= 4 && !disposed && !projectId; attempt++) {
projectId = await resolveTuiProjectIdOnce(api, directory)
if (disposed || projectId || attempt === 4) break
await sleepAbortable(delayMs, api.lifecycle.signal)
delayMs *= 2
}
if (disposed) return
setSandboxProjectId(projectId)
if (!projectId) return
// Poll until the preference pair settles. On a clean restart the applied
// row can lag the persisted desired state while the server reconciles, so
// a single read would leave ON invisible forever. ensureSandboxPolling
// reuses the store reader and stops as soon as the pair is settled.
ensureSandboxPolling(projectId)
})()
})
const runToggleHostSandbox = async () => {
const toggleBlocked = hostSandboxToggleBlocked(isSandboxConfigEnabled(pluginConfig))
if (toggleBlocked) {
api.ui.toast({ message: toggleBlocked, variant: 'warning', duration: 5000 })
return
}
const sessionId = getCurrentRouteSessionId(api)
if (!sessionId) {
api.ui.toast({ message: 'Open a session first', variant: 'info', duration: 3000 })
return
}
// Re-resolve the project ID lazily when a prior attempt failed so a transient
// discovery failure does not permanently disable the toggle for this process.
let projectId = sandboxProjectId()
if (!projectId) {
projectId = await resolveTuiProjectIdOnce(api, directory)
if (disposed) return
setSandboxProjectId(projectId)
}
// Each failure below reports a distinct cause. The TUI has no usable log sink (console output
// corrupts the rendered screen, which is why the sbx runtime here is given a no-op logger), so
// the reason has to travel in the toast or it is lost entirely.
if (!projectId) {
api.ui.toast({ message: 'Sandbox toggle unavailable: could not resolve this project', variant: 'warning', duration: 5000 })
return
}
if (!existsSync(forgeDbPath)) {
api.ui.toast({ message: `Sandbox toggle unavailable: no Forge database at ${forgeDbPath}`, variant: 'warning', duration: 5000 })
return
}
const pref = readSessionSandboxPreference(projectId, forgeDbPath)
// A snapshot flagged as unavailable (missing/uninitialized table) must not be treated as "no
// persisted state": deriving from null here could issue an ON request that the server never
// acknowledges, or misreport the current state. Reject it before deriving or writing.
if (pref.unavailable) {
const reason = pref.unavailableReason ?? 'unknown reason'
api.ui.toast({ message: `Sandbox toggle unavailable: Forge preferences unreadable (${reason})`, variant: 'warning', duration: 5000 })
return
}
const { desired } = pref
const turningOff = desired?.enabled === true && desired.sessionId === sessionId
const nextEnabled = !turningOff
let revision: string | null = null
let waiterController: AbortController | null = null
try {
revision = beginSessionSandboxStateRequest(projectId, forgeDbPath, {
sessionId,
enabled: nextEnabled,
})
if (disposed) return
// Immediately re-derive the acknowledged state from the authoritative
// desired/applied pair. The new desired revision supersedes the prior
// applied acknowledgement, so a stale ON (previous revision/session) is
// cleared before the request even resolves.
refreshSandboxAcknowledgement(projectId)
// Follow this desired revision to its acknowledgement independently of the
// command timeout, so a late server apply still reaches the sidebar.
ensureSandboxPolling(projectId)
if (toggleWaiterController) toggleWaiterController.abort()
waiterController = new AbortController()
toggleWaiterController = waiterController
const signal = AbortSignal.any([waiterController.signal, api.lifecycle.signal])
const applied = await awaitSessionSandboxState(projectId, forgeDbPath, revision, {
timeoutMs: 15_000,
pollMs: 250,
signal,
})
if (toggleWaiterController === waiterController) toggleWaiterController = null
if (disposed) return
// Re-read the authoritative desired/applied pair before publishing state or
// success. A superseded acknowledgement (a newer toggle already moved the
// desired revision) must not render stale ON; only a current revision still
// warrants a success toast.
const pref = refreshSandboxAcknowledgement(projectId)
if (pref?.desired && pref.desired.revision === applied.revision) {
api.ui.toast({
message: `Host sandbox ${applied.enabled ? 'enabled' : 'disabled'} for this session`,
variant: 'success',
duration: 4000,
})
}
} catch (err) {
if (waiterController && toggleWaiterController === waiterController) toggleWaiterController = null
if (disposed) return
// The failed request may have superseded a prior acknowledged state with a
// new desired revision that never got applied. Re-read the authoritative
// pair and re-derive so a stale ON for the previous session is cleared.
const pref = refreshSandboxAcknowledgement(projectId)
// Suppress errors for superseded requests: when a newer toggle already
// moved the desired revision, this waiter is stale and must not report a
// false failure long after the latest request succeeded.
if (pref?.desired && revision && pref.desired.revision !== revision) return
const message = err instanceof Error ? err.message : String(err)
const guidance = nextEnabled ? 'Toggle off, then on to retry.' : 'Toggle again to retry disabling.'
api.ui.toast({
message: `Sandbox toggle failed: ${message}. ${guidance}`,
variant: 'error',
duration: 6000,
})
}
}
// Auto-follow loop session rotations. Runs independently of the sidebar
// option so users with the sidebar disabled still get follow-on-rotation.
const detachSessionFollower = attachLoopSessionFollower(api)
api.lifecycle.onDispose(detachSessionFollower)
// Dashboard command. Registered independently of the sidebar option so it is
// available even when the sidebar is disabled. The bind host/port come from
// `dashboard.*` in the plugin config (resolved via `startDashboardServer`),
// and the HTTP server is started in-process on first use and reused on
// subsequent invocations.
let dashboardServer: DashboardServerHandle | null = null
const runOpenDashboard = () => {
if (!dashboardServer) {
try {
dashboardServer = startDashboardServer({ dbPath: forgeDbPath, config: pluginConfig })
} catch (err) {
api.ui.toast({
message: err instanceof Error ? err.message : 'Failed to start dashboard',
variant: 'error',
duration: 5000,
})
return
}
}
const notice = describeDashboardBinding(dashboardServer)
const opened = openInBrowser(dashboardServer.localUrl)
const details = [
...(notice.localUrl ? [`Local: ${notice.localUrl}`] : []),
...dashboardServer.warnings,
...(notice.warning ? [notice.warning] : []),
]
const alert = Boolean(notice.warning) || dashboardServer.warnings.length > 0
api.ui.toast({
title: `Forge dashboard: ${notice.url}`,
message: details.length > 0
? details.join('\n')
: opened
? 'Opened in your browser.'
: 'Could not open a browser automatically; open the URL manually.',
variant: alert ? 'warning' : 'info',
duration: alert ? 10_000 : 5000,
})
}
api.lifecycle.onDispose(() => {
if (dashboardServer) {
dashboardServer.stop()
dashboardServer = null
}
})
const runBuildSandboxImage = () => {
const buildContextDir = resolveBundledContainerDir()
const image = pluginConfig.sandbox?.image ?? DEFAULT_SANDBOX_IMAGE
const browserControl = pluginConfig.sandbox?.imageFeatures?.browserControl === true
api.ui.dialog.setSize('medium')
api.ui.dialog.replace(() => (
<SandboxBuildDialog
api={api}
buildContextDir={buildContextDir}
image={image}
browserControl={browserControl}
/>
))
}
api.keymap.registerLayer({
commands: [
{
name: 'forge.dashboard',
title: 'Open dashboard',
desc: 'Start the Forge dashboard server and open it in the browser',
category: 'Forge',
namespace: 'palette',
run: () => { runOpenDashboard() },
},
{
name: 'forge.sandbox.buildImage',
title: 'Build sandbox template',
desc: 'Build the sandbox template image and load it into sbx',
category: 'Forge',
namespace: 'palette',
run: () => { runBuildSandboxImage() },
},
{
name: 'forge.sandbox.toggleHost',
title: 'Toggle host sandbox',
desc: 'Enable or disable the host sandbox for the current session',
category: 'Forge',
namespace: 'palette',
run: () => { void runToggleHostSandbox() },
},
],
bindings: [
...(opts.keybinds.dashboard
? [{ key: opts.keybinds.dashboard, cmd: 'forge.dashboard' as const }]
: []),
...(opts.keybinds.toggleHostSandbox
? [{ key: opts.keybinds.toggleHostSandbox, cmd: 'forge.sandbox.toggleHost' as const }]
: []),
],
})
if (!opts.sidebar) return
const [client, setClient] = createSignal<ForgeProjectClient | null>(null)
const [connectionStatus, setConnectionStatus] = createSignal<ForgeConnectionStatus>('connecting')
const [executionContextCache, setExecutionContextCache] = createSignal<ExecutionContextCache | null>(null)
let connectPromise: Promise<ForgeProjectClient | null> | null = null
let unavailableToastShown = false
const showUnavailableToast = () => {
if (unavailableToastShown) return
unavailableToastShown = true
api.ui.toast({ message: `Forge bus RPC unavailable for ${directory}`, variant: 'warning', duration: 5000 })
}
const scheduleClientRetry = () => {
if (disposed || retryTimer || untrack(client)) return
retryTimer = setTimeout(() => {
retryTimer = null
if (!disposed && !untrack(client)) void startClientConnection()
}, 2000)
}
const startClientConnection = (): Promise<ForgeProjectClient | null> => {
if (connectPromise) return connectPromise
setConnectionStatus('connecting')
connectPromise = connectForgeProject(api, directory, resolveLoopPermissionOptions(pluginConfig), forgeDbPath).then((connected) => untrack(() => {
connectPromise = null
if (disposed) return connected
setClient(connected)
setConnectionStatus(connected ? 'connected' : 'unavailable')
if (!connected) {
showUnavailableToast()
scheduleClientRetry()
} else if (connected && connected.projectId) {
const cache = createExecutionContextCache(
connected.projectId,
pluginConfig,
() => connected.loadExecutionContext(),
)
void cache.ensureLoaded().catch((err) => console.error('[forge] execution context preload failed', err))
setExecutionContextCache(cache)
}
return connected
})).catch((err) => untrack(() => {
connectPromise = null
console.error('[forge] TUI RPC connection failed', err)
if (!disposed) {
setConnectionStatus('unavailable')
showUnavailableToast()
scheduleClientRetry()
}
return null
}))
return connectPromise
}
createEffect(() => {
if (!api.state.ready || client() || connectPromise) return
void startClientConnection()
})
const ensureClient = async (): Promise<ForgeProjectClient | null> => {
const existing = client()
if (existing) return existing
return startClientConnection()
}
const openExecutionDialog = (currentClient: ForgeProjectClient, sessionID: string, planContent: string) => {
api.ui.dialog.setSize('xlarge')
api.ui.dialog.replace(() => (
<ExecutionDialog
api={api}
client={currentClient}
cache={executionContextCache()}
pluginConfig={pluginConfig}
planContent={planContent}
sessionId={sessionID}
projectDirectory={directory}
/>
))
}
const openPastePlanDialog = (currentClient: ForgeProjectClient, sessionID: string) => {
api.ui.dialog.setSize('large')
api.ui.dialog.replace(() => (
<api.ui.DialogPrompt
title="Paste plan"
placeholder="Paste a marked or unmarked implementation plan"
value=""
onConfirm={(value) => {
const normalized = normalizePastedPlanText(value)
if (!normalized.ok) {
api.ui.toast({
message: normalized.reason === 'empty'
? 'Paste a plan before executing'
: `Invalid plan markers: ${normalized.reason}`,
variant: 'error',
duration: 4000,
})
openPastePlanDialog(currentClient, sessionID)
return
}
openExecutionDialog(currentClient, sessionID, normalized.planText)
}}
onCancel={() => api.ui.dialog.clear()}
/>
))
}
const runExecutePlan = async () => {
const sessionID = getCurrentRouteSessionId(api)
if (!sessionID) {
api.ui.toast({ message: 'Open a session first', variant: 'info', duration: 3000 })
return
}
const currentClient = await ensureClient()
if (!currentClient) return
const planText = await currentClient.loadLatestPlan(sessionID)
if (!planText) {
api.ui.toast({
message: 'No plan in current session — paste one to execute',
variant: 'info',
duration: 4000,
})
openPastePlanDialog(currentClient, sessionID)
return
}
openExecutionDialog(currentClient, sessionID, planText)
}
api.keymap.registerLayer({
commands: [
{
name: 'forge.plan.execute',
title: 'Execute plan',
desc: 'Open the execution dialog for the current session plan, or paste one if none is found',
category: 'Forge',
namespace: 'palette',
run: () => { void runExecutePlan() },
},
{
name: 'forge.plan.executePasted',
title: 'Execute pasted plan',
desc: 'Paste a marked or unmarked plan and open the execution dialog',
category: 'Forge',
namespace: 'palette',
run: () => {
const sessionID = getCurrentRouteSessionId(api)
if (!sessionID) {
api.ui.toast({ message: 'Open a session first', variant: 'info', duration: 3000 })
return
}
void ensureClient().then((currentClient) => {
if (currentClient) openPastePlanDialog(currentClient, sessionID)
})
},
},
],
bindings: opts.keybinds.executePlan
? [{ key: opts.keybinds.executePlan, cmd: 'forge.plan.execute' }]
: [],
})
api.slots.register({
order: 150,
slots: {
sidebar_content(_ctx, slotProps) {
return <SidebarContainer
api={api}
client={client}
cache={executionContextCache}
pluginConfig={pluginConfig}
opts={opts}
status={connectionStatus}
preference={sandboxPreference}
sessionId={slotProps.session_id}
/>
},
},
})
}
const plugin: TuiPluginModule & { id: string } = { id, tui }
export default plugin