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

projects.ts

224 lines6.6 KB

1import type {
2 CommandId,
3 EnvironmentId,
4 OrchestrationCommand,
5 ProjectId,
6 SourceControlDiscoveryResult,
7 SourceControlProviderKind,
8 SourceControlRepositoryInfo,
9} from "@v12/contracts";
10import { DEFAULT_MODEL, ProviderInstanceId } from "@v12/contracts";
11import * as Arr from "effect/Array";
12import * as Option from "effect/Option";
13import * as Order from "effect/Order";
14​
15import {
16 ensureBrowseDirectoryPath,
17 findProjectByPath,
18 inferProjectTitleFromPath,
19 isExplicitRelativeProjectPath,
20 isUnsupportedWindowsProjectPath,
21 resolveProjectPathForDispatch,
22} from "../state/projects.ts";
23import type { EnvironmentProject } from "../state/models.ts";
24​
25export type AddProjectRemoteProviderKind = Extract<
26 SourceControlProviderKind,
27 "github" | "gitlab" | "bitbucket" | "azure-devops"
28>;
29export type AddProjectRemoteSource = AddProjectRemoteProviderKind | "url";
30​
31export type AddProjectRemoteSourceReadiness = Record<
32 AddProjectRemoteSource,
33 { readonly ready: boolean; readonly hint: string | null }
34>;
35​
36export type AddProjectCloneFlow =
37 | {
38 readonly step: "repository";
39 readonly environmentId: EnvironmentId;
40 readonly source: AddProjectRemoteSource;
41 }
42 | {
43 readonly step: "confirm";
44 readonly environmentId: EnvironmentId;
45 readonly source: AddProjectRemoteSource;
46 readonly repositoryInput: string;
47 readonly repository: SourceControlRepositoryInfo | null;
48 readonly remoteUrl: string;
49 };
50​
51const ADD_PROJECT_REMOTE_SOURCES: ReadonlyArray<AddProjectRemoteSource> = [
52 "url",
53 "github",
54 "gitlab",
55 "bitbucket",
56 "azure-devops",
57];
58​
59const ADD_PROJECT_REMOTE_PROVIDER_SOURCES: ReadonlyArray<AddProjectRemoteProviderKind> = [
60 "github",
61 "gitlab",
62 "bitbucket",
63 "azure-devops",
64];
65​
66export function addProjectRemoteSourceLabel(source: AddProjectRemoteSource): string {
67 switch (source) {
68 case "github":
69 return "GitHub";
70 case "gitlab":
71 return "GitLab";
72 case "bitbucket":
73 return "Bitbucket";
74 case "azure-devops":
75 return "Azure DevOps";
76 case "url":
77 return "Git URL";
78 }
80​
81export function addProjectRemoteSourcePathHint(source: AddProjectRemoteSource): string {
82 switch (source) {
83 case "github":
84 return "owner/repo";
85 case "gitlab":
86 return "group/project";
87 case "bitbucket":
88 return "workspace/repository";
89 case "azure-devops":
90 return "project/repository";
91 case "url":
92 return "URL";
93 }
95​
96export function addProjectRemoteSourceProvider(
97 source: AddProjectRemoteSource,
98): AddProjectRemoteProviderKind | null {
99 return source === "url" ? null : source;
101​
102export function sortAddProjectProviderSources(
103 readinessBySource: AddProjectRemoteSourceReadiness,
104): ReadonlyArray<AddProjectRemoteProviderKind> {
105 return Arr.sort(
106 ADD_PROJECT_REMOTE_PROVIDER_SOURCES,
107 Order.mapInput(
108 Order.Struct({
109 ready: Order.flip(Order.Boolean),
110 label: Order.String,
111 }),
112 (source: AddProjectRemoteProviderKind) => ({
113 ready: readinessBySource[source].ready,
114 label: addProjectRemoteSourceLabel(source),
115 }),
116 ),
117 );
119​
120export function buildAddProjectRemoteSourceReadiness(
121 discovery: SourceControlDiscoveryResult | null,
122): AddProjectRemoteSourceReadiness {
123 const unavailable = {
124 ready: false,
125 hint: "Provider status unavailable. Open Source Control settings and rescan.",
126 } as const;
127 const readiness: AddProjectRemoteSourceReadiness = {
128 url: { ready: true, hint: null },
129 github: unavailable,
130 gitlab: unavailable,
131 bitbucket: unavailable,
132 "azure-devops": unavailable,
133 };
134​
135 if (!discovery) {
136 return readiness;
137 }
138​
139 const providerByKind = new Map(
140 discovery.sourceControlProviders.map((provider) => [provider.kind, provider]),
141 );
142 for (const source of ADD_PROJECT_REMOTE_SOURCES) {
143 const kind = addProjectRemoteSourceProvider(source);
144 if (!kind) continue;
145 const provider = providerByKind.get(kind);
146 if (!provider) {
147 readiness[source] = unavailable;
148 continue;
149 }
150 if (provider.status !== "available") {
151 readiness[source] = { ready: false, hint: provider.installHint };
152 continue;
153 }
154 if (provider.auth.status === "unauthenticated") {
155 readiness[source] = {
156 ready: false,
157 hint:
158 Option.getOrNull(provider.auth.detail) ??
159 `${provider.label} is not authenticated. Open Source Control settings for setup guidance.`,
160 };
161 continue;
162 }
163 readiness[source] = { ready: true, hint: null };
164 }
165 return readiness;
167​
168export function getAddProjectInitialQuery(baseDirectory: string | null | undefined): string {
169 const trimmed = baseDirectory?.trim() ?? "";
170 return trimmed.length === 0 ? "~/" : ensureBrowseDirectoryPath(trimmed);
172​
173export function resolveAddProjectPath(input: {
174 readonly rawPath: string;
175 readonly currentProjectCwd?: string | null;
176 readonly platform: string;
177}): { readonly ok: true; readonly path: string } | { readonly ok: false; readonly error: string } {
178 const rawPath = input.rawPath.trim();
179 if (rawPath.length === 0) {
180 return { ok: false, error: "Enter a project path." };
181 }
182 if (isUnsupportedWindowsProjectPath(rawPath, input.platform)) {
183 return { ok: false, error: "Windows-style paths are only supported on Windows environments." };
184 }
185 if (isExplicitRelativeProjectPath(rawPath) && !input.currentProjectCwd) {
186 return { ok: false, error: "Relative paths require an active project in this environment." };
187 }
188 const path = resolveProjectPathForDispatch(rawPath, input.currentProjectCwd);
189 return path.length === 0 ? { ok: false, error: "Enter a project path." } : { ok: true, path };
191​
192export function findExistingAddProject(input: {
193 readonly projects: ReadonlyArray<EnvironmentProject>;
194 readonly environmentId: EnvironmentId;
195 readonly path: string;
196}): EnvironmentProject | null {
197 return (
198 findProjectByPath(
199 input.projects.filter((project) => project.environmentId === input.environmentId),
200 input.path,
201 ) ?? null
202 );
204​
205export function buildProjectCreateCommand(input: {
206 readonly commandId: CommandId;
207 readonly projectId: ProjectId;
208 readonly workspaceRoot: string;
209 readonly createdAt: string;
210}): Extract<OrchestrationCommand, { type: "project.create" }> {
211 return {
212 type: "project.create",
213 commandId: input.commandId,
214 projectId: input.projectId,
215 title: inferProjectTitleFromPath(input.workspaceRoot),
216 workspaceRoot: input.workspaceRoot,
217 createWorkspaceRootIfMissing: true,
218 defaultModelSelection: {
219 instanceId: ProviderInstanceId.make("codex"),
220 model: DEFAULT_MODEL,
221 },
222 createdAt: input.createdAt,
223 };