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

projectImport.logic.ts

139 lines4.9 KB

1import { findProjectByPath } from "@t3tools/client-runtime/state/projects";
2import type { AgentSessionProjectCandidate, EnvironmentId, ProjectId } from "@t3tools/contracts";
3​
4const RECENT_PROJECT_WINDOW_MS = 30 * 24 * 60 * 60 * 1000;
5const DEFAULT_SELECTION_MIN_THREADS = 3;
6​
7/**
8 * Servers that predate the git scan omit `git`; their candidates are treated as repositories so old
9 * computers still get a useful default selection.
10 */
11export function partitionOnboardingProjects<T extends AgentSessionProjectCandidate>(
12 candidates: ReadonlyArray<T>,
13 now = Date.now(),
14) {
15 const cutoff = now - RECENT_PROJECT_WINDOW_MS;
16​
17 return {
18 available: candidates,
19 recent: candidates.filter((candidate) => {
20 if (candidate.git === null) return false;
21 if (candidate.threadCount < DEFAULT_SELECTION_MIN_THREADS) return false;
22 if (candidate.lastActiveAt === null) return false;
23 const lastActiveAt = Date.parse(candidate.lastActiveAt);
24 return lastActiveAt >= cutoff && lastActiveAt <= now;
25 }),
26 };
28​
29export interface OnboardingProjectGroup<T> {
30 readonly key: string;
31 readonly label: string;
32 readonly repository: string | null;
33 readonly candidates: ReadonlyArray<T>;
34 readonly threadCount: number;
35 readonly lastActiveAt: string | null;
37​
38function latestActivity(left: string | null, right: string | null): string | null {
39 if (left === null) return right;
40 if (right === null) return left;
41 return left > right ? left : right;
43​
44export function groupOnboardingProjects<
45 T extends Pick<
46 AgentSessionProjectCandidate,
47 "path" | "title" | "git" | "threadCount" | "lastActiveAt"
48 >,
49>(candidates: ReadonlyArray<T>) {
50 const groups = new Map<string, OnboardingProjectGroup<T> & { candidates: Array<T> }>();
51 const other: Array<T> = [];
52​
53 for (const candidate of candidates) {
54 if (candidate.git === null) {
55 other.push(candidate);
56 continue;
57 }
58 const git = candidate.git ?? { remoteKey: null, repository: null };
59 const key = git.remoteKey === null ? `path:${candidate.path}` : `remote:${git.remoteKey}`;
60 const existing = groups.get(key);
61 if (existing === undefined) {
62 groups.set(key, {
63 key,
64 label: git.repository ?? candidate.title,
65 repository: git.repository,
66 candidates: [candidate],
67 threadCount: candidate.threadCount,
68 lastActiveAt: candidate.lastActiveAt,
69 });
70 continue;
71 }
72 existing.candidates.push(candidate);
73 groups.set(key, {
74 ...existing,
75 threadCount: existing.threadCount + candidate.threadCount,
76 lastActiveAt: latestActivity(existing.lastActiveAt, candidate.lastActiveAt),
77 });
78 }
79​
80 const repositories = [...groups.values()].sort((left, right) => {
81 if (left.lastActiveAt === right.lastActiveAt) return left.label.localeCompare(right.label);
82 if (left.lastActiveAt === null) return 1;
83 if (right.lastActiveAt === null) return -1;
84 return right.lastActiveAt.localeCompare(left.lastActiveAt);
85 });
86​
87 return { repositories, other };
89​
90/** Use the server's project match before the client snapshot, which can lag behind the scan. */
91export function resolveOnboardingProjectId(
92 projects: ReadonlyArray<{
93 readonly id: ProjectId;
94 readonly environmentId: EnvironmentId;
95 readonly workspaceRoot: string;
96 }>,
97 environmentId: EnvironmentId,
98 candidate: Pick<AgentSessionProjectCandidate, "path" | "projectId">,
99): ProjectId | null {
100 if (candidate.projectId !== undefined) return candidate.projectId;
101 const environmentProjects = projects.filter((project) => project.environmentId === environmentId);
102 const currentRootMatch = findProjectByPath(environmentProjects, candidate.path);
103 if (currentRootMatch !== undefined) return currentRootMatch.id;
104 return null;
106​
107export function resolveOnboardingLandingProject<T>(
108 selection: ReadonlyArray<string>,
109 projectsWithImportedHistory: ReadonlyMap<string, T>,
110 completedProjects: ReadonlyMap<string, T>,
111): T | undefined {
112 for (const path of selection) {
113 const project = projectsWithImportedHistory.get(path);
114 if (project !== undefined) return project;
115 }
116 for (const path of selection) {
117 const project = completedProjects.get(path);
118 if (project !== undefined) return project;
119 }
120 return undefined;
122​
123export function splitOnboardingProjectPath(path: string): {
124 readonly name: string;
125 readonly parent: string;
126} {
127 if (/^[A-Za-z]:[\\/]+$/.test(path) || /^[\\/]{2}[^\\/]+[\\/][^\\/]+[\\/]*$/.test(path)) {
128 return { name: path, parent: "" };
129 }
130 const trimmed = path.replace(/[\\/]+$/, "");
131 const separatorIndex = Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\"));
132 if (trimmed.length === 0) return { name: path, parent: "" };
133 if (separatorIndex < 0) return { name: trimmed, parent: "" };
134 return { name: trimmed.slice(separatorIndex + 1), parent: trimmed.slice(0, separatorIndex + 1) };
136​
137export function onboardingProjectKey(environmentId: EnvironmentId, path: string): string {
138 return JSON.stringify([environmentId, path]);