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/** One or two threads in a directory is usually a one-off question, not a project. */
6const DEFAULT_SELECTION_MIN_THREADS = 3;
7
8/**
9 * Existing projects still need their agent history imported, so every scan
10 * candidate is offered. The default selection is narrower: git repositories
11 * active in the last 30 days with enough threads to look like real work.
12 */
13export function partitionOnboardingProjects<T extends AgentSessionProjectCandidate>(
14 candidates: ReadonlyArray<T>,
15 now = Date.now(),
16) {
17 const cutoff = now - RECENT_PROJECT_WINDOW_MS;
18
19 return {
20 available: candidates,
21 recent: candidates.filter((candidate) => {
22 if (candidate.git === null) return false;
23 if (candidate.threadCount < DEFAULT_SELECTION_MIN_THREADS) return false;
24 if (candidate.lastActiveAt === null) return false;
25 const lastActiveAt = Date.parse(candidate.lastActiveAt);
26 return lastActiveAt >= cutoff && lastActiveAt <= now;
27 }),
28 };
29}
30
31export interface OnboardingProjectGroup<T> {
32 /** Stable identity for collapse state and React keys. */
33 readonly key: string;
34 /** GitHub `owner/name`, or the checkout's folder name when the origin is elsewhere. */
35 readonly label: string;
36 readonly repository: string | null;
37 readonly candidates: ReadonlyArray<T>;
38 readonly threadCount: number;
39 readonly lastActiveAt: string | null;
40}
41
42function latestActivity(left: string | null, right: string | null): string | null {
43 if (left === null) return right;
44 if (right === null) return left;
45 return left > right ? left : right;
46}
47
48/**
49 * Group scan candidates for the onboarding picker. Clones of one repository
50 * share a group keyed by their normalized origin URL. Repositories without an
51 * origin get a group each. Directories that are not git repositories are
52 * returned separately so the UI can fold them away by default. Groups sort by
53 * most recent activity, newest first.
54 */
55export function groupOnboardingProjects<
56 T extends Pick<
57 AgentSessionProjectCandidate,
58 "path" | "title" | "git" | "threadCount" | "lastActiveAt"
59 >,
60>(candidates: ReadonlyArray<T>) {
61 const groups = new Map<string, OnboardingProjectGroup<T> & { candidates: Array<T> }>();
62 const other: Array<T> = [];
63
64 for (const candidate of candidates) {
65 if (candidate.git === null) {
66 other.push(candidate);
67 continue;
68 }
69 const key =
70 candidate.git.remoteKey === null
71 ? `path:${candidate.path}`
72 : `remote:${candidate.git.remoteKey}`;
73 const existing = groups.get(key);
74 if (existing === undefined) {
75 groups.set(key, {
76 key,
77 label: candidate.git.repository ?? candidate.title,
78 repository: candidate.git.repository,
79 candidates: [candidate],
80 threadCount: candidate.threadCount,
81 lastActiveAt: candidate.lastActiveAt,
82 });
83 continue;
84 }
85 existing.candidates.push(candidate);
86 groups.set(key, {
87 ...existing,
88 threadCount: existing.threadCount + candidate.threadCount,
89 lastActiveAt: latestActivity(existing.lastActiveAt, candidate.lastActiveAt),
90 });
91 }
92
93 const repositories = [...groups.values()].sort((left, right) => {
94 if (left.lastActiveAt === right.lastActiveAt) return left.label.localeCompare(right.label);
95 if (left.lastActiveAt === null) return 1;
96 if (right.lastActiveAt === null) return -1;
97 return right.lastActiveAt.localeCompare(left.lastActiveAt);
98 });
99
100 return { repositories, other };
101}
102
103/** Use the server's project match before the client snapshot, which can lag behind the scan. */
104export function resolveOnboardingProjectId(
105 projects: ReadonlyArray<{
106 readonly id: ProjectId;
107 readonly environmentId: EnvironmentId;
108 readonly workspaceRoot: string;
109 }>,
110 environmentId: EnvironmentId,
111 candidate: Pick<AgentSessionProjectCandidate, "path" | "projectId">,
112): ProjectId | null {
113 if (candidate.projectId !== undefined) return candidate.projectId;
114 const environmentProjects = projects.filter((project) => project.environmentId === environmentId);
115 const currentRootMatch = findProjectByPath(environmentProjects, candidate.path);
116 if (currentRootMatch !== undefined) return currentRootMatch.id;
117 return null;
118}
119
120/** Prefer a selected project with imported history, then a completed empty import. */
121export function resolveOnboardingLandingProject<T>(
122 selection: ReadonlyArray<string>,
123 projectsWithImportedHistory: ReadonlyMap<string, T>,
124 completedProjects: ReadonlyMap<string, T>,
125): T | undefined {
126 for (const path of selection) {
127 const project = projectsWithImportedHistory.get(path);
128 if (project !== undefined) return project;
129 }
130 for (const path of selection) {
131 const project = completedProjects.get(path);
132 if (project !== undefined) return project;
133 }
134 return undefined;
135}
136
137/** Paths identify projects only within the computer that owns them. */
138export function onboardingProjectKey(environmentId: EnvironmentId, path: string): string {
139 return JSON.stringify([environmentId, path]);
140}