/** * How entries of the Projects inventory form rows. Presentation only: grouping never changes * which scope a memory or session resolves to. The inventory computes each entry's key against * every entry and returns it as `group_key`; the UI groups by that key alone. */ export interface ProjectInventoryGroupEntry { display_project: string; git_remote: string | null; identity_source: string; project: string | null; repository_identity?: string | null; resolved_scope_id: string; workspace_identity: string; } function isAbsolutePath(value: string): boolean { return /^(\/|[A-Za-z]:[\\/])/.test(value); } /** * The Project name a row shows, lower-cased, or null when the entry has no name of its own (a * generated workspace ID). It is the title projectHeading shows in the UI. */ export function projectInventoryNameKey(entry: ProjectInventoryGroupEntry): string | null { const name = entry.display_project.trim(); if ( !entry.project && entry.identity_source === "workspace_id" && name === entry.workspace_identity ) return null; const title = isAbsolutePath(name) ? (name.split(/[\\/]/).filter(Boolean).at(-1) ?? name) : name; return title ? title.toLowerCase() : null; } function repositoryOf(entry: ProjectInventoryGroupEntry): string | null { return entry.repository_identity?.trim() || entry.git_remote?.trim() || null; } /** * The row key of each entry. Entries form one row when they have the same name, resolve to the * same scope and carry the same repository identity. An entry without a repository identity * joins the row of its name and scope when exactly one repository identity is recorded there. */ export function projectInventoryGroupKeys( entries: readonly T[], ): Map { const base = (entry: T, name: string) => JSON.stringify(["name", name, entry.resolved_scope_id]); const repositories = new Map>(); for (const entry of entries) { const name = projectInventoryNameKey(entry); const repository = repositoryOf(entry); if (!name || !repository) continue; const key = base(entry, name); repositories.set(key, (repositories.get(key) ?? new Set()).add(repository)); } const keys = new Map(); for (const entry of entries) { const name = projectInventoryNameKey(entry); if (!name) { keys.set(entry, `identity:${entry.workspace_identity}`); continue; } const shared = base(entry, name); const known = repositories.get(shared); const only = known?.size === 1 ? [...known][0] : null; keys.set(entry, JSON.stringify([shared, repositoryOf(entry) ?? only ?? ""])); } return keys; }