Open History to see the last commit
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;
5
6/** Existing projects still need their agent history imported, so every scan candidate is offered. */
7export function partitionOnboardingProjects<T extends AgentSessionProjectCandidate>(
8 candidates: ReadonlyArray<T>,
9 now = Date.now(),
10) {
11 const cutoff = now - RECENT_PROJECT_WINDOW_MS;
12
13 return {
14 available: candidates,
15 recent: candidates.filter((candidate) => {
16 if (candidate.lastActiveAt === null) return false;
17 const lastActiveAt = Date.parse(candidate.lastActiveAt);
18 return lastActiveAt >= cutoff && lastActiveAt <= now;
19 }),
20 };
21}
22
23/** Use the server's project match before the client snapshot, which can lag behind the scan. */
24export function resolveOnboardingProjectId(
25 projects: ReadonlyArray<{
26 readonly id: ProjectId;
27 readonly environmentId: EnvironmentId;
28 readonly workspaceRoot: string;
29 }>,
30 environmentId: EnvironmentId,
31 candidate: Pick<AgentSessionProjectCandidate, "path" | "projectId">,
32): ProjectId | null {
33 if (candidate.projectId !== undefined) return candidate.projectId;
34 const environmentProjects = projects.filter((project) => project.environmentId === environmentId);
35 const currentRootMatch = findProjectByPath(environmentProjects, candidate.path);
36 if (currentRootMatch !== undefined) return currentRootMatch.id;
37 return null;
38}
39
40/** Prefer a selected project with imported history, then a completed empty import. */
41export function resolveOnboardingLandingProject<T>(
42 selection: ReadonlyArray<string>,
43 projectsWithImportedHistory: ReadonlyMap<string, T>,
44 completedProjects: ReadonlyMap<string, T>,
45): T | undefined {
46 for (const path of selection) {
47 const project = projectsWithImportedHistory.get(path);
48 if (project !== undefined) return project;
49 }
50 for (const path of selection) {
51 const project = completedProjects.get(path);
52 if (project !== undefined) return project;
53 }
54 return undefined;
55}
56
57/** Paths identify projects only within the computer that owns them. */
58export function onboardingProjectKey(environmentId: EnvironmentId, path: string): string {
59 return JSON.stringify([environmentId, path]);
60}