Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadSort.ts

101 lines3.0 KB

1import type { ProjectId } from "@t3tools/contracts";
2import type { SidebarProjectSortOrder, SidebarThreadSortOrder } from "@t3tools/contracts/settings";
3import * as Arr from "effect/Array";
4import * as Order from "effect/Order";
5​
6export interface ThreadSortInput {
7 readonly createdAt: string;
8 readonly updatedAt: string;
9 readonly latestUserMessageAt?: string | null;
10 readonly messages?: ReadonlyArray<{
11 readonly createdAt: string;
12 readonly role: string;
13 }>;
15​
16export function toSortableTimestamp(iso: string | undefined): number | null {
17 if (!iso) return null;
18 const ms = Date.parse(iso);
19 return Number.isFinite(ms) ? ms : null;
21​
22function getFirstSortableTimestamp(...values: Array<string | null | undefined>): number | null {
23 for (const value of values) {
24 const timestamp = toSortableTimestamp(value ?? undefined);
25 if (timestamp !== null) {
26 return timestamp;
27 }
28 }
29​
30 return null;
32​
33function getLatestUserMessageTimestamp(thread: ThreadSortInput): number {
34 if (thread.latestUserMessageAt) {
35 return toSortableTimestamp(thread.latestUserMessageAt) ?? Number.NEGATIVE_INFINITY;
36 }
37​
38 let latestUserMessageTimestamp: number | null = null;
39​
40 for (const message of thread.messages ?? []) {
41 if (message.role !== "user") continue;
42 const messageTimestamp = toSortableTimestamp(message.createdAt);
43 if (messageTimestamp === null) continue;
44 latestUserMessageTimestamp =
45 latestUserMessageTimestamp === null
46 ? messageTimestamp
47 : Math.max(latestUserMessageTimestamp, messageTimestamp);
48 }
49​
50 if (latestUserMessageTimestamp !== null) {
51 return latestUserMessageTimestamp;
52 }
53​
54 return getFirstSortableTimestamp(thread.updatedAt, thread.createdAt) ?? Number.NEGATIVE_INFINITY;
56​
57export function getThreadSortTimestamp(
58 thread: ThreadSortInput,
59 sortOrder: SidebarThreadSortOrder | Exclude<SidebarProjectSortOrder, "manual">,
60): number {
61 if (sortOrder === "created_at") {
62 return (
63 getFirstSortableTimestamp(thread.createdAt, thread.updatedAt) ?? Number.NEGATIVE_INFINITY
64 );
65 }
66 return getLatestUserMessageTimestamp(thread);
68​
69export function sortThreads<T extends { readonly id: string } & ThreadSortInput>(
70 threads: readonly T[],
71 sortOrder: SidebarThreadSortOrder,
72): T[] {
73 return Arr.sort(
74 threads,
75 Order.mapInput(
76 Order.Struct({
77 timestamp: Order.flip(Order.Number),
78 id: Order.flip(Order.String),
79 }),
80 (thread: T) => ({
81 timestamp: getThreadSortTimestamp(thread, sortOrder),
82 id: thread.id,
83 }),
84 ),
85 );
87​
88export function getLatestThreadForProject<
89 T extends {
90 readonly id: string;
91 readonly projectId: ProjectId;
92 readonly archivedAt: string | null;
93 } & ThreadSortInput,
94>(threads: readonly T[], projectId: ProjectId, sortOrder: SidebarThreadSortOrder): T | null {
95 return (
96 sortThreads(
97 threads.filter((thread) => thread.projectId === projectId && thread.archivedAt === null),
98 sortOrder,
99 )[0] ?? null
100 );