Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

firstRun.logic.ts

184 lines5.7 KB

1import { normalizeProjectPathForComparison } from "@t3tools/shared/path";
2​
3export type FirstRunDecision = "pending" | "app" | "wizard";
4​
5export interface FirstRunGateState {
6 readonly decision: FirstRunDecision;
7 readonly stalled: boolean;
8}
9​
10type FirstRunGateEvent =
11 | { readonly type: "evidence"; readonly decision: FirstRunDecision }
12 | { readonly type: "timeout" };
13​
14interface FirstRunWorkspaceInput {
15 readonly primaryEnvironmentId: string | null;
16 readonly serverCwd: string | null;
17 readonly bootstrapProjectId?: string | undefined;
18 readonly bootstrapThreadId?: string | undefined;
19 readonly bootstrapProjectCreated?: boolean | undefined;
20 readonly bootstrapThreadCreated?: boolean | undefined;
21 readonly projects: ReadonlyArray<{
22 readonly id: string;
23 readonly environmentId: string;
24 readonly workspaceRoot: string;
25 }>;
26 readonly threads: ReadonlyArray<{
27 readonly id: string;
28 readonly projectId: string;
29 readonly environmentId: string;
30 readonly latestTurn: unknown;
31 readonly latestUserMessageAt: string | null;
32 readonly session: unknown;
33 }>;
35​
36interface FirstRunDecisionInput {
37 readonly enabled: boolean;
38 readonly hydrated: boolean;
39 readonly completed: boolean;
40 readonly bootstrapped: boolean;
41 readonly authoritative: boolean;
42 readonly workspaceAuthoritative: boolean;
43 readonly workspaceProvenanceAuthoritative: boolean;
44 readonly catalogReady: boolean;
45 readonly serverConfigAvailable: boolean;
46 readonly workspaceFresh: boolean;
47 readonly projectCount: number;
48 readonly threadCount: number;
50​
51interface HostedFirstRunDecisionInput {
52 readonly hydrated: boolean;
53 readonly completed: boolean;
54 readonly catalogReady: boolean;
55 readonly environmentCount: number;
57​
58export function isFirstRunWorkspaceProvenanceAuthoritative(input: {
59 readonly welcomeReceived: boolean;
60 readonly bootstrapStatus: "pending" | "complete" | null;
61}): boolean {
62 // An empty catalog is not final while cwd auto-bootstrap is pending. Older
63 // servers omit bootstrapStatus, so a received welcome with null stays valid.
64 return input.welcomeReceived && input.bootstrapStatus !== "pending";
66​
67/** Keeps the authenticated app unmounted until workspace evidence settles. */
68export function transitionFirstRunGateState(
69 state: FirstRunGateState,
70 event: FirstRunGateEvent,
71): FirstRunGateState {
72 if (event.type === "timeout") {
73 return state.decision === "pending" && !state.stalled ? { ...state, stalled: true } : state;
74 }
75​
76 if (
77 state.decision === "wizard" ||
78 event.decision === "pending" ||
79 (state.decision === "app" && event.decision !== "wizard")
80 ) {
81 return state;
82 }
83​
84 return { decision: event.decision, stalled: false };
86​
87/** Only a project and thread created by this startup count as a fresh nonempty workspace. */
88export function isFreshFirstRunWorkspace(input: FirstRunWorkspaceInput): boolean {
89 if (input.projects.length > 1 || input.threads.length > 1) {
90 return false;
91 }
92​
93 const bootstrapProject = input.projects[0];
94 if (bootstrapProject !== undefined) {
95 if (
96 input.bootstrapProjectCreated !== true ||
97 input.bootstrapProjectId !== bootstrapProject.id ||
98 input.serverCwd === null ||
99 bootstrapProject.environmentId !== input.primaryEnvironmentId ||
100 normalizeProjectPathForComparison(bootstrapProject.workspaceRoot) !==
101 normalizeProjectPathForComparison(input.serverCwd)
102 ) {
103 return false;
104 }
105 }
106​
107 const bootstrapThread = input.threads[0];
108 if (bootstrapThread === undefined) {
109 return true;
110 }
111​
112 return (
113 bootstrapProject !== undefined &&
114 input.bootstrapThreadCreated === true &&
115 input.bootstrapThreadId === bootstrapThread.id &&
116 bootstrapThread.environmentId === input.primaryEnvironmentId &&
117 bootstrapThread.projectId === bootstrapProject.id &&
118 bootstrapThread.latestTurn === null &&
119 bootstrapThread.latestUserMessageAt === null &&
120 bootstrapThread.session === null
121 );
123​
124/** Cached projects may open the app, but only live workspace data may complete onboarding. */
125export function resolveFirstRunDecision(input: FirstRunDecisionInput): {
126 readonly decision: FirstRunDecision;
127 readonly persistCompletion: boolean;
128} {
129 if (!input.enabled || (input.hydrated && input.completed)) {
130 return { decision: "app", persistCompletion: false };
131 }
132​
133 if (!input.hydrated) {
134 return { decision: "pending", persistCompletion: false };
135 }
136​
137 if (input.projectCount > 1 || input.threadCount > 1) {
138 return {
139 decision: "app",
140 persistCompletion:
141 input.bootstrapped &&
142 input.authoritative &&
143 input.workspaceAuthoritative &&
144 input.catalogReady &&
145 input.serverConfigAvailable,
146 };
147 }
148​
149 if (
150 !input.bootstrapped ||
151 !input.authoritative ||
152 !input.workspaceProvenanceAuthoritative ||
153 !input.catalogReady ||
154 !input.serverConfigAvailable
155 ) {
156 return { decision: "pending", persistCompletion: false };
157 }
158​
159 return input.workspaceFresh
160 ? { decision: "wizard", persistCompletion: false }
161 : { decision: "app", persistCompletion: input.workspaceAuthoritative };
163​
164/** Hosted onboarding depends on saved environments because there is no primary server. */
165export function resolveHostedFirstRunDecision(input: HostedFirstRunDecisionInput): {
166 readonly decision: FirstRunDecision;
167 readonly persistCompletion: boolean;
168} {
169 if (!input.hydrated) {
170 return { decision: "pending", persistCompletion: false };
171 }
172​
173 if (input.completed) {
174 return { decision: "app", persistCompletion: false };
175 }
176​
177 if (!input.catalogReady) {
178 return { decision: "pending", persistCompletion: false };
179 }
180​
181 return input.environmentCount === 0
182 ? { decision: "wizard", persistCompletion: false }
183 : { decision: "app", persistCompletion: true };