Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Sidebar: pick each automation's newest run by start time, not list order

AdityaUmale94ca926cd7ae
diff --git a/src/components/SidebarThreadRow.test.ts b/src/components/SidebarThreadRow.test.ts
index f3f6ef300f..604dcef14d 100644
--- a/src/components/SidebarThreadRow.test.ts
+++ b/src/components/SidebarThreadRow.test.ts
@@ -66,13 +66,20 @@ describe("sidebar thread visibility", () => {
{ threadId: "weekly-10", title: "Weekly", createdAt: 10 },
{ threadId: "daily-5", title: "Daily brief", createdAt: 5, unread: true },
];
- const runs = [...["daily-40", "daily-30", "daily-20", "daily-5"].map((threadId) => ({ routineId: "daily", threadId })), { routineId: "weekly", threadId: "weekly-10" }, { routineId: "room" }];
+ const runs = [...[40, 30, 20, 5].map((at) => ({ routineId: "daily", threadId: `daily-${at}`, createdAt: at })), { routineId: "weekly", threadId: "weekly-10", createdAt: 10 }, { routineId: "room", createdAt: 60 }];
expect(visibleSidebarThreads(rows, "chat", "", [], false, runs).map((task) => task.threadId)).toEqual(["chat", "daily-40", "weekly-10", "daily-5"]);
expect(visibleSidebarThreads(rows, "daily-20", "", [], false, runs).map((task) => task.threadId)).toEqual(["chat", "daily-40", "daily-20", "weekly-10", "daily-5"]);
expect(visibleSidebarThreads(rows, "chat", "", [], true, runs)).toEqual(rows);
expect(visibleSidebarThreads(rows, "chat", "daily", [], false, runs)).toHaveLength(4);
expect(visibleSidebarThreads(rows, "chat").map((task) => task.threadId)).toEqual(rows.map((task) => task.threadId));
});
+ it("picks a routine's newest run by when it started, not by when it last replied", () => {
+ // Run A started at 10 and finished at 50; run B started at 30 and finished at 35. A sorts first in the list.
+ const rows = [{ threadId: "run-a", title: "Daily brief", createdAt: 10, lastThreadOrderAt: 50 }, { threadId: "run-b", title: "Daily brief", createdAt: 30, lastThreadOrderAt: 35 }];
+ const runs = [{ routineId: "daily", threadId: "run-a", createdAt: 10 }, { routineId: "daily", threadId: "run-b", createdAt: 30 }];
+ expect(visibleSidebarThreads(rows, "", "", [], false, runs).map((task) => task.threadId)).toEqual(["run-b"]);
+ expect(visibleSidebarThreads([{ ...rows[0] }, { ...rows[1], archivedAt: 40 }], "", "", [], false, runs).map((task) => task.threadId)).toEqual(["run-a"]);
+ });
it("keeps queued older threads visible", () => {
const rows = tasks.map((task) => ({ ...task, queued: task.threadId === "9" }));
expect(visibleSidebarThreads(rows, "0").map((task) => task.threadId)).toEqual(["0", "1", "2", "3", "4", "5", "9"]);
diff --git a/src/components/SidebarThreadRow.tsx b/src/components/SidebarThreadRow.tsx
index 55e0d3c94c..cf69ccec7c 100644
--- a/src/components/SidebarThreadRow.tsx
+++ b/src/components/SidebarThreadRow.tsx
@@ -124,24 +124,28 @@ export function HelperThreadGroup<T>({ tasks, renderThread }: { tasks: T[]; rend
}
/** Default list shows each routine's newest run only; older runs stay under "Show all". */
-export function visibleSidebarThreads<T extends ThreadRowTask>(tasks: T[], activeId: string, query = "", folders: BotProject[] = [], showAll = false, routineRuns: readonly Pick<RoutineRun, "routineId" | "threadId">[] = []): T[] {
+export function visibleSidebarThreads<T extends ThreadRowTask>(tasks: T[], activeId: string, query = "", folders: BotProject[] = [], showAll = false, routineRuns: readonly Pick<RoutineRun, "routineId" | "threadId" | "createdAt">[] = []): T[] {
const needle = query.trim().toLowerCase();
if (needle) {
return tasks.filter((task) => task.title.toLowerCase().includes(needle) || folders.some((folder) => folder.id === task.projectId && folder.name.toLowerCase().includes(needle)));
}
if (showAll) return tasks;
let open = 0;
- const routineOf = new Map(routineRuns.flatMap((run) => run.threadId ? [[run.threadId, run.routineId] as const] : []));
- const shownRoutines = new Set<string>();
+ const runOf = new Map(routineRuns.flatMap((run) => run.threadId ? [[run.threadId, run] as const] : []));
+ // Newest by the run's start, not sidebar order: an older run that finished later sorts first.
+ const newestRun = new Map<string, Pick<RoutineRun, "routineId" | "createdAt">>();
+ for (const task of tasks) {
+ const run = runOf.get(task.threadId);
+ if (!run || task.pinned === true || task.openedBy?.burstId || isArchived(task)) continue;
+ const seen = newestRun.get(run.routineId);
+ if (!seen || run.createdAt > seen.createdAt) newestRun.set(run.routineId, run);
+ }
return orderedThreadList(tasks).filter((task) => {
if (task.pinned === true) return true;
if (task.openedBy?.burstId) return task.threadId === activeId || demandsAttention(task, activeId);
if (isArchived(task)) return task.threadId === activeId;
- const routineId = routineOf.get(task.threadId);
- if (routineId !== undefined) {
- if (shownRoutines.has(routineId)) return demandsAttention(task, activeId);
- shownRoutines.add(routineId);
- }
+ const run = runOf.get(task.threadId);
+ if (run && newestRun.get(run.routineId) !== run) return demandsAttention(task, activeId);
return task.closedBy || isSnoozed(task)
? demandsAttention(task, activeId)
: open++ < 6 || demandsAttention(task, activeId);