Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import type { SidebarProjectSortOrder } from "@akeru/contracts/settings";
import {
getThreadSortTimestamp,
toSortableTimestamp,
type ThreadSortInput,
} from "../lib/threadSort";
type SidebarProject = {
id: string;
title: string;
createdAt?: string | undefined;
updatedAt?: string | undefined;
};
type ScopedSidebarProject = SidebarProject & {
environmentId: string;
};
type ScopedSidebarThread = ThreadSortInput & {
environmentId: string;
projectId: string;
archivedAt: string | null;
};
function getProjectSortTimestamp(
project: SidebarProject,
projectThreads: readonly ThreadSortInput[],
sortOrder: Exclude<SidebarProjectSortOrder, "manual">,
): number {
if (projectThreads.length > 0) {
return projectThreads.reduce(
(latest, thread) => Math.max(latest, getThreadSortTimestamp(thread, sortOrder)),
Number.NEGATIVE_INFINITY,
);
}
if (sortOrder === "created_at") {
return toSortableTimestamp(project.createdAt) ?? Number.NEGATIVE_INFINITY;
}
return toSortableTimestamp(project.updatedAt ?? project.createdAt) ?? Number.NEGATIVE_INFINITY;
}
function sortProjectsByActivity<TProject extends SidebarProject>(
projects: readonly TProject[],
sortOrder: SidebarProjectSortOrder,
getProjectThreads: (project: TProject) => readonly ThreadSortInput[],
compareTies: (left: TProject, right: TProject) => number,
): TProject[] {
if (sortOrder === "manual") {
return [...projects];
}
return [...projects].toSorted((left, right) => {
const rightTimestamp = getProjectSortTimestamp(right, getProjectThreads(right), sortOrder);
const leftTimestamp = getProjectSortTimestamp(left, getProjectThreads(left), sortOrder);
const byTimestamp =
rightTimestamp === leftTimestamp ? 0 : rightTimestamp > leftTimestamp ? 1 : -1;
return byTimestamp || compareTies(left, right);
});
}
/**
* Sorts the cross-environment project collection used by landing surfaces.
* Project ids are only unique within an environment, and archived threads
* must not make a project appear recently active.
*/
export function sortScopedProjectsForSidebar<
TProject extends ScopedSidebarProject,
TThread extends ScopedSidebarThread,
>(
projects: readonly TProject[],
threads: readonly TThread[],
sortOrder: SidebarProjectSortOrder,
): TProject[] {
const scopedKey = (environmentId: string, projectId: string) =>
`${environmentId}\u0000${projectId}`;
const threadsByProject = new Map<string, TThread[]>();
for (const thread of threads) {
if (thread.archivedAt !== null) {
continue;
}
const key = scopedKey(thread.environmentId, thread.projectId);
const existing = threadsByProject.get(key) ?? [];
existing.push(thread);
threadsByProject.set(key, existing);
}
return sortProjectsByActivity(
projects,
sortOrder,
(project) => threadsByProject.get(scopedKey(project.environmentId, project.id)) ?? [],
(left, right) =>
left.title.localeCompare(right.title) ||
left.environmentId.localeCompare(right.environmentId) ||
left.id.localeCompare(right.id),
);
}