Repository navigation
Expand file tree
/
Copy pathstore.ts
More file actions
1150 lines (987 loc) · 37.6 KB
/
Copy pathstore.ts
File metadata and controls
1150 lines (987 loc) · 37.6 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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* WizardStore — the TUI's store: the shared `SessionStore` plus the state only
* the screens use. React components subscribe via useSyncExternalStore.
*
* The session and the run state live in `sessions`, the same kind of store
* headless and embedders use, so `runProgram` writes them directly. This store
* adds the TUI's own state (`TuiState`: the screens' answers and what an
* overlay shows, read as `store.X`), display state (the token HUD, learn
* cards), the router and the flow's gates, and re-resolves the screen and the
* gates after every change to either.
*
* The active screen is derived from the session and the TUI state —
* WizardRouter walks the flow and shows the first step whose `isComplete` is
* still false. Define a step `gate` if its screen needs to await user
* interactions; the TUI host calls `await store.getGate(stepId)` to pause
* until it holds.
*/
import { atom } from 'nanostores';
import { logToFile } from '@utils/debug';
import { type AuthErrorDetail, type TokenUsageDelta } from '@agent/types';
import {
initialTuiState,
type TuiLaunchChoices,
type TuiState,
type TuiView,
} from '@tui/tui-state';
import {
type OutroData,
type PendingQuestion,
type AskAnswers,
type TaskNotice,
} from '@agent/types';
import { type DiscoveredFeature } from '@shared/discovered-feature';
import { McpOutcome, RunPhase } from '@shared/run-state';
import type { SettingsConflict } from '@shared/claude-settings';
import type { CloudRegion } from '@utils/types';
import { type WizardReadinessResult } from '@shared/health-checks/readiness';
import {
WizardRouter,
type ScreenName,
ScreenId,
Overlay,
Program,
type ProgramId,
} from './router.js';
import { analytics, sessionProperties } from '@utils/analytics';
import { buildSession, findProgramConfig, SessionStore } from '@programs';
import type { PlannedEvent, TaskItem, WizardSession } from '@programs/types';
import {
addTokenUsage,
EMPTY_TOKEN_USAGE,
type TokenUsageSnapshot,
} from './token-usage.js';
import type { StoreInitContext } from './flow.js';
import { programFlowSteps } from './ai-opt-in-gate.js';
import { flowOwner } from './flow-owner.js';
import { IS_DEV } from '@shared/constants';
export { ScreenId, Overlay, Program };
export type { ScreenName, OutroData, TuiState, TuiView, ProgramId };
interface GateEntry {
predicate: (view: TuiView) => boolean;
promise: Promise<void>;
resolve: () => void;
resolved: boolean;
}
export class WizardStore implements TuiView {
/** The shared session and run state; every session write goes through it. */
readonly sessions: SessionStore;
/** The TUI's own state: the screens' answers, overlay contents and launch choices. */
private $tui = atom<TuiState>(initialTuiState());
// ── Display-only atoms ────────────────────────────────────────────
private $statusExpanded = atom(false);
private $learnCardBlockIdx = atom(0);
private $learnCardComplete = atom(false);
private $version = atom(0);
// Defaults on for local/dev/test runs (tsx, `pnpm try`, vitest) so
// contributors see it without needing to know the shortcut; defaults off
// for the published build, where it stays genuinely hidden. Still
// Ctrl+T-toggleable either way.
private $tokenHudVisible = atom(IS_DEV);
/** The Visualizer tab's NOW PLAYING stage, and when it started. */
private $currentStage = atom<{ stage: string; startedAt: number } | null>(
null,
);
private $tokenUsage = atom<TokenUsageSnapshot>(EMPTY_TOKEN_USAGE);
/** The code a screen asked to end the run with; the host applies it. */
private $exitRequest = atom<number | null>(null);
/** Last screen seen — used to detect screen transitions for analytics. */
private _lastScreen: ScreenName | null = null;
/** The ask and notice the overlays last showed, to open and close them as the session changes. */
private _shownQuestion: PendingQuestion | null = null;
private _shownNotice: TaskNotice | null = null;
/** Hooks run when transitioning onto a screen. */
private _enterScreenHooks = new Map<ScreenName, (() => void)[]>();
/** Gate promises derived from program step definitions. */
private _gates = new Map<string, GateEntry>();
private _mcpOutcomeReported = false;
version = '';
/** Navigation router — resolves active screen from session state. */
readonly router: WizardRouter;
/** Blocks agent execution until the settings-override overlay is dismissed. */
private _resolveSettingsOverride: (() => void) | null = null;
private _backupAndFixSettings: (() => boolean) | null = null;
/** Blocks OAuth flow until the port-conflict overlay is dismissed. */
private _resolvePortConflict: (() => void) | null = null;
/** Resolves the OAuth flow with a manually-entered authorization code. */
private _resolveManualAuthCode: ((code: string) => void) | null = null;
constructor(
program: ProgramId = Program.PostHogIntegration,
sessions: SessionStore = new SessionStore(buildSession({})),
) {
this.sessions = sessions;
this.router = new WizardRouter(program);
this._initFromProgram(program);
this._shownQuestion = sessions.session.pendingQuestion;
this._shownNotice = sessions.session.taskNotice;
sessions.subscribe(() => this._onSessionChange());
}
/**
* Scan program steps for gate predicates and create gate promises.
*
* Steps come from programFlowSteps (withAiOptInGate applied), so the injected ai-opt-in step's
* gate registers here — the TUI host awaits it before any source leaves the
* machine. Same wrapper screen-sequences.ts uses, so the gate and its screen
* can't drift apart.
*/
private _initFromProgram(program: ProgramId): void {
const steps = programFlowSteps(program);
for (const step of steps) {
if (step.gate) {
let resolve!: () => void;
const promise = new Promise<void>((r) => {
resolve = r;
});
this._gates.set(step.id, {
predicate: step.gate,
promise,
resolve,
resolved: false,
});
}
}
}
/** Open or close the ask and notice overlays as the shared store's requests come and go. */
private _onSessionChange(): void {
const { pendingQuestion, taskNotice } = this.session;
let closedOnly = false;
if (pendingQuestion !== this._shownQuestion) {
if (this._shownQuestion) this.router.popOverlay();
if (pendingQuestion) this.router.pushOverlay(Overlay.WizardAsk);
closedOnly = !pendingQuestion;
this._shownQuestion = pendingQuestion;
}
if (taskNotice !== this._shownNotice) {
if (this._shownNotice) this.router.popOverlay();
if (taskNotice) this.router.pushOverlay(Overlay.TaskNotice);
closedOnly = !taskNotice;
this._shownNotice = taskNotice;
}
this.emitChange();
if (closedOnly) this.router._setDirection('pop');
}
/**
* Run the program steps' onInit callbacks. startTUI calls this once
* the screens are actually rendering — constructing a store alone
* (tests, playground) must not fire init work like the health-check
* pre-flight, whose probes belong only to flows that show its screen.
*/
runInitHooks(): void {
const steps = flowOwner(this.router.activeProgram).flow;
const getSession = (): WizardSession => this.session;
const ctx: StoreInitContext = {
get session() {
return getSession();
},
setReadinessResult: (r) => this.setReadinessResult(r),
setFrameworkContext: (k, v) => this.setFrameworkContext(k, v),
emitChange: () => this.emitChange(),
};
for (const step of steps) {
step.onInit?.(ctx);
}
}
/**
* Run the active program's `onReady` detection through the shared store, and
* mark detection complete so `runProgram` doesn't repeat it. Call it after
* the session is set, so it sees the real installDir.
*/
async runReadyHooks(): Promise<void> {
const config = findProgramConfig(this.router.activeProgram);
await config?.onReady?.(this.sessions.readyContext());
this.sessions.setDetectionComplete();
}
// ── Gate API ────────────────────────────────────────────────────
/**
* Get a gate promise by step ID — the primary blocking checkpoint API
* for the TUI host. `await store.getGate('...')` parks the caller until the
* corresponding program step's gate predicate flips to true (if the
* predicate stays false, the caller stays parked indefinitely — the
* TUI keeps rendering so the user can resolve whatever is blocking).
*
* If the program doesn't define a step with this ID, or the step
* has no `gate` predicate, this returns an already-resolved promise
* so the TUI host flows straight through. This lets programs opt in to
* gates on a per-step basis without the TUI host needing to know which
* gates exist in which flow.
*/
getGate(stepId: string): Promise<void> {
return this._gates.get(stepId)?.promise ?? Promise.resolve();
}
/**
* Resolve once `predicate(store)` is true. Unlike a gate, this is created
* at the await point and evaluated live against the store, so it
* never latches on a startup value — the orchestrator uses it to wait for a
* decision (a project picked, a handoff acknowledged) without the "true while
* undecided" trap that latched gate predicates have.
*/
waitUntil(predicate: (view: TuiView) => boolean): Promise<void> {
if (predicate(this)) return Promise.resolve();
return new Promise((resolve) => {
const unsub = this.subscribe(() => {
if (predicate(this)) {
unsub();
resolve();
}
});
});
}
/**
* Resolve once the flow has reached `stepId`: every step before it is
* hidden or complete. Resolves whether `stepId` itself shows; a step the
* active flow doesn't have shows.
*/
reachStep(stepId: string): Promise<boolean> {
const program = this.router.activeProgram;
const steps = programFlowSteps(program);
const index = steps.findIndex((step) => step.id === stepId);
if (index === -1) return Promise.resolve(true);
const before = steps.slice(0, index);
const passed = (view: TuiView): boolean =>
before.every((step) => {
if (step.show && !step.show(view)) return true;
const done = step.isComplete ?? step.gate;
return !done || done(view);
});
const shows = steps[index].show;
return this.waitUntil(passed).then(() => !shows || shows(this));
}
/**
* Re-evaluate every gate predicate against the store and
* resolve any whose predicate now returns true. Called after every
* emitChange(), so gates unblock as soon as the session mutation
* that satisfies them lands. Gates only resolve once — a predicate
* that goes true → false → true will NOT re-block a caller that
* already awaited through.
*/
private _checkGates(): void {
for (const [, gate] of this._gates) {
if (!gate.resolved && gate.predicate(this)) {
gate.resolved = true;
gate.resolve();
}
}
}
// ── Reads ─────────────────────────────────────────────────────────
get session(): WizardSession {
return this.sessions.session;
}
/** Replace the session; the TUI state stays. */
set session(value: WizardSession) {
this.sessions.session = value;
}
get statusMessages(): string[] {
return this.sessions.statusMessages;
}
get tasks(): TaskItem[] {
return this.sessions.tasks;
}
get eventPlan(): PlannedEvent[] {
return this.sessions.eventPlan;
}
get handoffText(): string | null {
return this.sessions.handoffText;
}
// ── TUI state ─────────────────────────────────────────────────────
get mcpFeatures(): TuiState['mcpFeatures'] {
return this.$tui.get().mcpFeatures;
}
get programLabel(): TuiState['programLabel'] {
return this.$tui.get().programLabel;
}
get setupConfirmed(): TuiState['setupConfirmed'] {
return this.$tui.get().setupConfirmed;
}
get loginUrl(): TuiState['loginUrl'] {
return this.$tui.get().loginUrl;
}
get authorizeUrl(): TuiState['authorizeUrl'] {
return this.$tui.get().authorizeUrl;
}
get mcpComplete(): TuiState['mcpComplete'] {
return this.$tui.get().mcpComplete;
}
get mcpOutcome(): TuiState['mcpOutcome'] {
return this.$tui.get().mcpOutcome;
}
get mcpLoginCommands(): TuiState['mcpLoginCommands'] {
return this.$tui.get().mcpLoginCommands;
}
get mcpInstalledClients(): TuiState['mcpInstalledClients'] {
return this.$tui.get().mcpInstalledClients;
}
get mcpSuggestedPromptsDismissed(): TuiState['mcpSuggestedPromptsDismissed'] {
return this.$tui.get().mcpSuggestedPromptsDismissed;
}
get slackStepDismissed(): TuiState['slackStepDismissed'] {
return this.$tui.get().slackStepDismissed;
}
get slackConnected(): TuiState['slackConnected'] {
return this.$tui.get().slackConnected;
}
get skillsComplete(): TuiState['skillsComplete'] {
return this.$tui.get().skillsComplete;
}
get outroDismissed(): TuiState['outroDismissed'] {
return this.$tui.get().outroDismissed;
}
get integrate(): TuiState['integrate'] {
return this.$tui.get().integrate;
}
get completedRuns(): TuiState['completedRuns'] {
return this.$tui.get().completedRuns;
}
get selfDrivingHandoffConfirmed(): TuiState['selfDrivingHandoffConfirmed'] {
return this.$tui.get().selfDrivingHandoffConfirmed;
}
get githubConnected(): TuiState['githubConnected'] {
return this.$tui.get().githubConnected;
}
get githubDeclined(): TuiState['githubDeclined'] {
return this.$tui.get().githubDeclined;
}
get outageDismissed(): TuiState['outageDismissed'] {
return this.$tui.get().outageDismissed;
}
get settingsOverrideKeys(): TuiState['settingsOverrideKeys'] {
return this.$tui.get().settingsOverrideKeys;
}
get settingsConflicts(): TuiState['settingsConflicts'] {
return this.$tui.get().settingsConflicts;
}
get authErrorDetail(): TuiState['authErrorDetail'] {
return this.$tui.get().authErrorDetail;
}
get portConflictProcess(): TuiState['portConflictProcess'] {
return this.$tui.get().portConflictProcess;
}
get spellbook(): TuiState['spellbook'] {
return this.$tui.get().spellbook;
}
get mintHandoff(): TuiState['mintHandoff'] {
return this.$tui.get().mintHandoff;
}
/** Write TUI state, then `sessionWrites`' session fields, with one notification either way. */
private _write(patch: Partial<TuiState>, sessionWrites?: () => void): void {
this.$tui.set({ ...this.$tui.get(), ...patch });
const before = this.sessions.getVersion();
if (sessionWrites) this.sessions.batch(sessionWrites);
if (this.sessions.getVersion() === before) this.emitChange();
}
// ── Display state ───────────────────────────────────────────────
get currentStage(): { stage: string; startedAt: number } | null {
return this.$currentStage.get();
}
get tokenUsage(): TokenUsageSnapshot {
return this.$tokenUsage.get();
}
/** No-op when the stage hasn't changed, so `startedAt` survives across
* re-renders and tab switches and measures real stage time. */
setCurrentStage(stage: string): void {
const cur = this.$currentStage.get();
if (cur?.stage === stage) return;
this.$currentStage.set({ stage, startedAt: Date.now() });
this.emitChange();
}
get statusExpanded(): boolean {
return this.$statusExpanded.get();
}
toggleStatusExpanded(): void {
this.$statusExpanded.set(!this.$statusExpanded.get());
this.emitChange();
}
setStatusExpanded(expanded: boolean): void {
if (this.$statusExpanded.get() !== expanded) {
this.$statusExpanded.set(expanded);
this.emitChange();
}
}
get exitRequest(): number | null {
return this.$exitRequest.get();
}
/** A screen ends the run with `code`; the first request wins. */
requestExit(code: number): void {
if (this.$exitRequest.get() !== null) return;
this.$exitRequest.set(code);
this.emitChange();
}
// ── Writes ──────────────────────────────────────────────────────
/** Start from the host's launch values: the session, and the TUI state from its defaults, `choices` and the program's label. */
launch(
session: WizardSession,
choices: TuiLaunchChoices = {},
programLabel: string | null = null,
): void {
this.$tui.set(initialTuiState(choices, programLabel));
this.sessions.session = session;
}
/** Sets setupConfirmed, and is the point consent becomes final. */
completeSetup(): void {
// Reports first: analytics merges tags into an event as it is sent, so
// `setup confirmed` only carries the warehouse tags if they are already
// set. On main they were, because reporting happened back in detect.
this.sessions.markWarehouseSourcesReportedIfNeeded();
analytics.wizardCapture('setup confirmed', sessionProperties(this.session));
this._write({ setupConfirmed: true });
}
/**
* Sharing is on: either the user turned it back on in the panel, or they
* pressed Continue without ever touching it. Both are reversible until
* completeSetup() resolves the intro gate and reports.
*/
grantSharing(): void {
this.sessions.grantSharing();
}
/**
* Sharing is off. Suppresses reporting only — local detection still ran and
* the results stay in the session, so the outro suggestion and the warehouse
* task are unaffected; see `scanConsent` on `WizardSession`.
*
* Deliberately does not report. The panel's toggle can come back here, so
* marking the run reported would strand a user who turns sharing off and
* then on again. completeSetup() owns the single report.
*/
declineSharing(): void {
this.sessions.declineSharing();
}
setRunPhase(phase: RunPhase): void {
this.sessions.setRunPhase(phase);
}
setCredentials(credentials: WizardSession['credentials']): void {
this.sessions.setCredentials(credentials);
}
/** Post-refresh credential swap. No `auth complete`. */
setAccessToken(credentials: WizardSession['credentials']): void {
this.sessions.setAccessToken(credentials);
}
setRoleAtOrganization(role: string | null): void {
this.sessions.setRoleAtOrganization(role);
}
setApiUser(user: WizardSession['apiUser']): void {
this.sessions.setApiUser(user);
}
setFrameworkConfig(
integration: WizardSession['integration'],
config: WizardSession['frameworkConfig'],
): void {
this.sessions.setFrameworkConfig(integration, config);
}
setDetectionComplete(): void {
this.sessions.setDetectionComplete();
}
setDetectedFramework(label: string): void {
this.sessions.setDetectedFramework(label);
}
setPosthogSdkDetected(detected: boolean): void {
this.sessions.setPosthogSdkDetected(detected);
}
setSpellbook(spellbook: NonNullable<TuiState['spellbook']>): void {
this._write({ spellbook });
}
setMintHandoff(action: NonNullable<TuiState['mintHandoff']>): void {
// The parked agent may still hold a question or notice open.
this._write({ mintHandoff: action }, () => {
this.cancelPendingQuestion();
if (this.session.taskNotice) this.resolveTaskNotice(false);
});
}
setSkillId(skillId: string | null): void {
this.sessions.setSkillId(skillId);
}
setUnsupportedVersion(info: {
current: string;
minimum: string;
docsUrl: string;
}): void {
this.sessions.setUnsupportedVersion(info);
}
setLoginUrl(url: string | null): void {
this._write({ loginUrl: url });
}
setAuthorizeUrl(url: string | null): void {
this._write({ authorizeUrl: url });
}
setReadinessResult(result: WizardReadinessResult | null): void {
this.sessions.setReadinessResult(result);
}
/** User dismissed the blocking outage screen. Gate resolves via _checkGates(). */
dismissOutage(): void {
logToFile('[health-checks] user dismissed outage screen, continuing');
this._write({ outageDismissed: true });
}
/**
* Push the settings-override overlay and return a promise that blocks
* until the user dismisses it via backupAndFixSettingsOverride().
*/
showSettingsOverride(
conflicts: SettingsConflict[],
backupAndFix: () => boolean,
): Promise<void> {
this._backupAndFixSettings = backupAndFix;
const hasReadOnly = conflicts.some((c) => !c.writable);
if (hasReadOnly) {
this.router.pushOverlay(Overlay.ManagedSettings);
} else {
this.router.pushOverlay(Overlay.SettingsOverride);
}
this._write({
settingsOverrideKeys: conflicts.flatMap((c) => c.keys),
settingsConflicts: conflicts,
});
return new Promise((resolve) => {
this._resolveSettingsOverride = resolve;
});
}
/**
* Push the port-conflict overlay and return a promise that blocks
* until the user frees the ports and retries, or exits.
*/
showPortConflict(processInfo: {
command: string;
pid: string;
port: number;
user: string;
}): Promise<void> {
this.router.pushOverlay(Overlay.PortConflict);
this._write({ portConflictProcess: processInfo });
return new Promise((resolve) => {
this._resolvePortConflict = resolve;
});
}
/** Dismiss the port-conflict overlay and retry the OAuth port loop. */
resolvePortConflict(): void {
this.router.popOverlay();
this._write({ portConflictProcess: null });
this.router._setDirection('pop');
this._resolvePortConflict?.();
this._resolvePortConflict = null;
}
/**
* Show an optional step's notice and return whether to keep that step.
* Asked before the step runs, so nobody is surprised by a prompt mid-run.
*/
showTaskNotice(notice: TaskNotice): Promise<boolean> {
return this.sessions.showTaskNotice(notice);
}
/** Dismiss the notice, keeping (`true`) or skipping (`false`) the step. */
resolveTaskNotice(keep: boolean): void {
this.sessions.resolveTaskNotice(keep);
}
/**
* Return a promise that resolves when the user submits a manually-entered
* OAuth code via the paste modal. The OAuth flow races this against the
* local callback server — see `performOAuthFlow`.
*/
waitForManualAuthCode(): Promise<string> {
return new Promise<string>((resolve) => {
this._resolveManualAuthCode = resolve;
});
}
/** Open the manual OAuth code-entry overlay over the auth screen. */
showManualAuthCode(): void {
this.pushOverlay(Overlay.ManualAuthCode);
}
/** Dismiss the manual OAuth code overlay without submitting. */
dismissManualAuthCode(): void {
this.popOverlay();
}
/**
* Submit a manually-entered authorization code: dismiss the overlay and
* resolve the in-flight OAuth flow so it can exchange the code for a token.
*/
submitManualAuthCode(code: string): void {
this.popOverlay();
this._resolveManualAuthCode?.(code);
this._resolveManualAuthCode = null;
}
/**
* Open the WizardAsk overlay with a set of questions and return a promise
* that resolves once the user submits answers (or the request is cancelled).
*
* Only one request is in flight at a time — calling this while a request
* is already pending throws.
*/
requestQuestion(
question: PendingQuestion,
onAnswer?: () => void,
): Promise<AskAnswers> {
return this.sessions.requestQuestion(question, onAnswer);
}
/**
* Report that the user answered one question of the in-flight request and
* another is coming — the ask bridge's timeout heartbeat.
*/
noteAskProgress(): void {
this.sessions.noteAskProgress();
}
/**
* Resolve the in-flight wizard_ask request with the user's answers and
* dismiss the overlay. Answers flow back to the agent as the tool result.
*/
resolvePendingQuestion(answers: AskAnswers): void {
this.sessions.resolvePendingQuestion(answers);
}
/**
* Cancel the in-flight wizard_ask request — the bridge sends a sentinel
* answer ("__cancelled__") so the skill can decide how to handle it.
*/
cancelPendingQuestion(): void {
this.sessions.cancelPendingQuestion();
}
/**
* Back up .claude/settings.json. Dismisses the overlay on success.
*/
backupAndFixSettingsOverride(): boolean {
const ok = this._backupAndFixSettings?.() ?? false;
if (ok) {
this.router.popOverlay();
this._write({ settingsOverrideKeys: null, settingsConflicts: null });
this.router._setDirection('pop');
this._resolveSettingsOverride?.();
this._resolveSettingsOverride = null;
this._backupAndFixSettings = null;
}
return ok;
}
/** Push the auth-error overlay (no dismiss — user must exit). */
showAuthError(detail?: AuthErrorDetail): void {
this.router.pushOverlay(Overlay.AuthError);
this._write({ authErrorDetail: detail ?? null });
}
/** Push the session-timeout overlay (no dismiss — user must exit). */
showSessionTimeout(): void {
this.pushOverlay(Overlay.SessionTimeout);
}
addDiscoveredFeature(feature: DiscoveredFeature): void {
this.sessions.addDiscoveredFeature(feature);
}
/** Capture the MCP step's outcome once; the screen reports it as the results show, before Enter. */
reportMcpOutcome(
outcome: McpOutcome,
installedClients: string[] = [],
featuresSelected?: 'all' | string[],
): void {
if (this._mcpOutcomeReported) return;
this._mcpOutcomeReported = true;
const featuresPayload =
outcome === McpOutcome.Installed && featuresSelected !== undefined
? { mcp_features_selected: featuresSelected }
: {};
analytics.wizardCapture('mcp complete', {
mcp_outcome: outcome,
mcp_installed_clients: installedClients,
...featuresPayload,
...sessionProperties(this.session),
});
}
/** Complete the MCP step; its `mcp complete` capture goes through `reportMcpOutcome`, so once per store. */
setMcpComplete(
outcome: McpOutcome = McpOutcome.Skipped,
installedClients: string[] = [],
featuresSelected?: 'all' | string[],
loginCommands: string[] = [],
): void {
this.reportMcpOutcome(outcome, installedClients, featuresSelected);
this._write({
mcpComplete: true,
mcpOutcome: outcome,
mcpLoginCommands: loginCommands,
mcpInstalledClients: installedClients,
});
}
setSkillsComplete(kept: boolean): void {
analytics.wizardCapture('skills complete', {
skills_kept: kept,
...sessionProperties(this.session),
});
this._write({ skillsComplete: true });
}
setMcpSuggestedPromptsDismissed(): void {
this._write({ mcpSuggestedPromptsDismissed: true });
}
setSlackStepDismissed(): void {
this._write({ slackStepDismissed: true });
}
setSlackConnected(connected: boolean): void {
this._write({ slackConnected: connected });
}
setGithubConnected(connected: boolean): void {
this._write({ githubConnected: connected });
}
/**
* Self-driving GitHub gate declined. Carries the outro the user lands on,
* since declining ends the flow before the agent runs and there is no abort
* case to render one.
*/
declineGithub(outroData: OutroData): void {
this._write({ githubDeclined: true }, () =>
this.sessions.update({ outroData }),
);
}
/**
* Self-driving integration-check answer. `true` → integrate the SDK as part
* of this run; `false` → PostHog is already set up, go straight to
* Self-driving. Resolves `store.integrate` from null.
*/
setIntegrate(
integrate: boolean,
extra?: { via?: string; path?: string },
): void {
analytics.wizardCapture('self-driving integration check', {
self_driving_integrate: integrate,
...(extra?.via ? { self_driving_integrate_via: extra.via } : {}),
...(extra?.path ? { self_driving_integrate_path: extra.path } : {}),
...sessionProperties(this.session),
});
this._write({ integrate });
}
/**
* Self-driving "no PostHog account" branch of the integration check. The
* project has no SDK, so we always integrate (`integrate = true`); and since
* the user has no account, we flip `signup` and record the `email` / `region`
* collected on the screen so `authenticate` → `getOrAskForProjectData` takes
* the provisioning path (create account + email a login link) instead of
* OAuth. The "yes, I have an account" branch uses `setIntegrate(true)` and
* leaves `signup` false so auth runs the normal OAuth login.
*/
chooseProvisionAccount(email: string, region: CloudRegion): void {
analytics.wizardCapture('self-driving integration check', {
self_driving_integrate: true,
self_driving_has_account: false,
provision_region: region,
...sessionProperties(this.session),
});
this._write({ integrate: true }, () =>
this.sessions.update({ signup: true, email, region }),
);
}
/**
* Self-driving handoff confirmed — the user acknowledged the post-integration
* screen, so the Self-driving run can begin. Gate resolves via _checkGates().
*/
confirmSelfDrivingHandoff(): void {
this._write({ selfDrivingHandoffConfirmed: true });
}
/**
* Mark a composed run step complete (e.g. self-driving's `integrate-run`).
* Records the step id so its `isComplete` predicate holds, clears the task
* list, and resets run phase to Idle so the next run step starts fresh.
*/
completeRunStep(stepId: string): void {
const done = this.completedRuns;
this._write(
{ completedRuns: done.includes(stepId) ? done : [...done, stepId] },
() => {
this.sessions.setTasks([]);
this.sessions.setRunPhase(RunPhase.Idle);
},
);
}
setOutroDismissed(dismissed = true): void {
this._write({ outroDismissed: dismissed });
}
setOutroData(data: OutroData): void {
this.sessions.setOutroData(data);
}
/** Show `data` on the outro screen: the error outro, with the run phase moved to Error. */
showOutroError(data: OutroData): void {
this.sessions.batch(() => {
this.sessions.setOutroData(data);
if (this.session.runPhase !== RunPhase.Error) {
this.sessions.setRunPhase(RunPhase.Error);
}
});
}
setDashboardUrl(url: string): void {
this.sessions.setDashboardUrl(url);
}
setNotebookUrl(url: string): void {
this.sessions.setNotebookUrl(url);
}
setFrameworkContext(key: string, value: unknown): void {
this.sessions.setFrameworkContext(key, value);
}
switchProgram(program: ProgramId): void {
if (program === this.router.activeProgram) return;
// Flush unresolved promises so the wizard can advance
for (const gate of this._gates.values()) gate.resolve();
this._gates.clear();
this.router.setProgram(program);
this._initFromProgram(program);
// start-tui stamps this once at launch; without it here every event
// after the switch still reports under the program the run started as.
analytics.setTag('program_id', program);
this._write({ setupConfirmed: false, programLabel: program }, () =>
this.sessions.update({
skillId: findProgramConfig(program)?.skillId ?? null,
}),
);
}
// ── Derived state ───────────────────────────────────────────────
/**
* The screen that should be rendered right now.
* Derived from session and TUI state via the router.
*/
get currentScreen(): ScreenName {
return this.router.resolve(this);
}
/** Direction hint for screen transitions. */
get lastNavDirection(): 'push' | 'pop' | null {
return this.router.lastNavDirection;
}
// ── Change notification ─────────────────────────────────────────
getVersion(): number {
return this.$version.get();
}
/**
* Notify React that state has changed.
* The router re-resolves the active screen on next render.
* Gate predicates are checked and resolved if ready.
*/
emitChange(): void {
this.router._setDirection('push');
this.$version.set(this.$version.get() + 1);
this._checkGates();
this._detectTransition();
}
// ── Overlay navigation ──────────────────────────────────────────
pushOverlay(overlay: Overlay): void {
this.router._setDirection('push');
this.router.pushOverlay(overlay);
this.$version.set(this.$version.get() + 1);
this._detectTransition();
}
popOverlay(): void {
this.router._setDirection('pop');
this.router.popOverlay();
this.$version.set(this.$version.get() + 1);
this._detectTransition();
}