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

projectImport.logic.ts

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