Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(conversations): add thread grouping helper with tests

Steven Enamakel7c00bb1426fb
diff --git a/app/src/features/conversations/threadList/groupThreads.test.ts b/app/src/features/conversations/threadList/groupThreads.test.ts
new file mode 100644
index 0000000000..bad2501893
--- /dev/null
+++ b/app/src/features/conversations/threadList/groupThreads.test.ts
@@ -0,0 +1,103 @@
+import { describe, expect, it } from 'vitest';
+
+import type { Thread } from '../../../types/thread';
+import {
+ folderBasename,
+ groupThreads,
+ isThreadPinned,
+ labelsWithPin,
+ PINNED_THREAD_LABEL,
+ recencyGroupFor,
+ threadMatchesQuery,
+} from './groupThreads';
+
+const NOW = new Date(2026, 9, 6, 15, 0, 0);
+
+function thread(id: string, lastMessageAt: Date, labels: string[] = []): Thread {
+ return {
+ id,
+ title: id,
+ chatId: null,
+ isActive: true,
+ messageCount: 1,
+ lastMessageAt: lastMessageAt.toISOString(),
+ createdAt: lastMessageAt.toISOString(),
+ labels,
+ };
+}
+
+const daysAgo = (days: number, hour = 12) =>
+ new Date(NOW.getFullYear(), NOW.getMonth(), NOW.getDate() - days, hour);
+
+describe('recencyGroupFor', () => {
+ it('buckets by local calendar day', () => {
+ expect(recencyGroupFor(daysAgo(0, 0).toISOString(), NOW)).toBe('today');
+ expect(recencyGroupFor(daysAgo(1, 23).toISOString(), NOW)).toBe('yesterday');
+ expect(recencyGroupFor(daysAgo(1, 0).toISOString(), NOW)).toBe('yesterday');
+ expect(recencyGroupFor(daysAgo(5).toISOString(), NOW)).toBe('previous7Days');
+ expect(recencyGroupFor(daysAgo(20).toISOString(), NOW)).toBe('previous30Days');
+ expect(recencyGroupFor(daysAgo(90).toISOString(), NOW)).toBe('older');
+ });
+
+ it('sends unparseable timestamps to older', () => {
+ expect(recencyGroupFor('not a date', NOW)).toBe('older');
+ });
+});
+
+describe('groupThreads', () => {
+ it('puts pinned threads first and keeps order within sections', () => {
+ const groups = groupThreads(
+ [
+ thread('a', daysAgo(0)),
+ thread('p', daysAgo(40), [PINNED_THREAD_LABEL]),
+ thread('b', daysAgo(0, 9)),
+ thread('c', daysAgo(3)),
+ ],
+ NOW
+ );
+ expect(groups.map(g => [g.key, g.threads.map(t => t.id)])).toEqual([
+ ['pinned', ['p']],
+ ['today', ['a', 'b']],
+ ['previous7Days', ['c']],
+ ]);
+ });
+
+ it('honours a custom pin predicate', () => {
+ const groups = groupThreads([thread('a', daysAgo(0))], NOW, () => true);
+ expect(groups).toEqual([{ key: 'pinned', threads: [expect.objectContaining({ id: 'a' })] }]);
+ });
+
+ it('returns no sections for no threads', () => {
+ expect(groupThreads([], NOW)).toEqual([]);
+ });
+});
+
+describe('pin labels', () => {
+ it('toggles only the reserved label', () => {
+ expect(labelsWithPin(['general'], true)).toEqual(['general', PINNED_THREAD_LABEL]);
+ expect(labelsWithPin(['general', PINNED_THREAD_LABEL], true)).toEqual([
+ 'general',
+ PINNED_THREAD_LABEL,
+ ]);
+ expect(labelsWithPin([PINNED_THREAD_LABEL, 'general'], false)).toEqual(['general']);
+ expect(labelsWithPin(undefined, false)).toEqual([]);
+ expect(isThreadPinned(thread('x', NOW, [PINNED_THREAD_LABEL]))).toBe(true);
+ expect(isThreadPinned(thread('y', NOW))).toBe(false);
+ });
+});
+
+describe('threadMatchesQuery', () => {
+ it('matches case-insensitively and treats blank as all', () => {
+ expect(threadMatchesQuery('Fix Gmail OAuth', 'gmail')).toBe(true);
+ expect(threadMatchesQuery('Fix Gmail OAuth', ' ')).toBe(true);
+ expect(threadMatchesQuery('Fix Gmail OAuth', 'slack')).toBe(false);
+ });
+});
+
+describe('folderBasename', () => {
+ it('returns the last segment on either separator', () => {
+ expect(folderBasename('/home/me/projects/site/')).toBe('site');
+ expect(folderBasename('C:\\Users\\me\\repo')).toBe('repo');
+ expect(folderBasename('/')).toBe('/');
+ });
+});
diff --git a/app/src/features/conversations/threadList/groupThreads.ts b/app/src/features/conversations/threadList/groupThreads.ts
new file mode 100644
index 0000000000..d4f64bc6db
--- /dev/null
+++ b/app/src/features/conversations/threadList/groupThreads.ts
@@ -0,0 +1,97 @@
+import type { Thread } from '../../../types/thread';
+
+/**
+ * Reserved thread label that marks a conversation as pinned. Persisted by the
+ * core like any other label (`openhuman.threads_update_labels`), so a pin
+ * survives restarts and follows the workspace. It never makes a thread a task
+ * (`isThreadVisibleInTab` keys tasks off other labels), so pinned threads stay
+ * in the General tab.
+ */
+export const PINNED_THREAD_LABEL = 'pinned';
+
+export type ThreadGroupKey =
+ | 'pinned'
+ | 'today'
+ | 'yesterday'
+ | 'previous7Days'
+ | 'previous30Days'
+ | 'older';
+
+export interface ThreadGroup {
+ key: ThreadGroupKey;
+ threads: Thread[];
+}
+
+const DAY_MS = 24 * 60 * 60 * 1000;
+
+export function isThreadPinned(thread: Thread): boolean {
+ return Boolean(thread.labels?.includes(PINNED_THREAD_LABEL));
+}
+
+/** Labels with the pin toggled; every other label is kept in order. */
+export function labelsWithPin(labels: readonly string[] | undefined, pinned: boolean): string[] {
+ const rest = (labels ?? []).filter(label => label !== PINNED_THREAD_LABEL);
+ return pinned ? [...rest, PINNED_THREAD_LABEL] : rest;
+}
+
+/** Case-insensitive title match; a blank query matches everything. */
+export function threadMatchesQuery(title: string, query: string): boolean {
+ const needle = query.trim().toLowerCase();
+ return needle === '' || title.toLowerCase().includes(needle);
+}
+
+function startOfLocalDay(date: Date): number {
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
+}
+
+/** Which recency bucket a timestamp falls into, relative to `now`'s local day. */
+export function recencyGroupFor(timestamp: string, now: Date): Exclude<ThreadGroupKey, 'pinned'> {
+ const time = new Date(timestamp).getTime();
+ // An unparseable timestamp sorts to the bottom rather than claiming "Today".
+ if (Number.isNaN(time)) return 'older';
+ const today = startOfLocalDay(now);
+ if (time >= today) return 'today';
+ if (time >= today - DAY_MS) return 'yesterday';
+ if (time >= today - 7 * DAY_MS) return 'previous7Days';
+ if (time >= today - 30 * DAY_MS) return 'previous30Days';
+ return 'older';
+}
+
+const GROUP_ORDER: ThreadGroupKey[] = [
+ 'pinned',
+ 'today',
+ 'yesterday',
+ 'previous7Days',
+ 'previous30Days',
+ 'older',
+];
+
+/**
+ * Splits an already-sorted thread list into the sidebar's sections: pinned
+ * first, then recency buckets by `lastMessageAt`. Input order is preserved
+ * inside each section and empty sections are dropped.
+ */
+export function groupThreads(
+ threads: readonly Thread[],
+ now: Date,
+ isPinned: (thread: Thread) => boolean = isThreadPinned
+): ThreadGroup[] {
+ const buckets = new Map<ThreadGroupKey, Thread[]>();
+ for (const thread of threads) {
+ const key = isPinned(thread) ? 'pinned' : recencyGroupFor(thread.lastMessageAt, now);
+ const bucket = buckets.get(key);
+ if (bucket) bucket.push(thread);
+ else buckets.set(key, [thread]);
+ }
+ return GROUP_ORDER.filter(key => buckets.has(key)).map(key => ({
+ key,
+ threads: buckets.get(key) ?? [],
+ }));
+}
+
+/** Last path segment of a folder, for a compact tooltip. */
+export function folderBasename(path: string): string {
+ const trimmed = path.replace(/[\\/]+$/, '');
+ const parts = trimmed.split(/[\\/]/);
+ return parts[parts.length - 1] || trimmed || path;
+}