Skip to content

Commit 5144900

Browse files
fix(client-runtime): last user message sort ignores agent-posted messages
Sort by latestUserAuthoredMessageAt (messages the user wrote) when the server sends it, falling back to latestUserMessageAt for older servers. Fixes #33866 AI-assisted; verified with the tests and tools listed above.
1 parent 3491f8c commit 5144900

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

‎packages/client-runtime/src/state/threadSort.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface ThreadSortInput {
66
readonly createdAt: string;
77
readonly updatedAt: string;
88
readonly latestUserMessageAt?: string | null;
9+
readonly latestUserAuthoredMessageAt?: string | null;
910
readonly messages?: ReadonlyArray<{
1011
readonly createdAt: string;
1112
readonly role: string;
@@ -80,6 +81,18 @@ function getFirstSortableTimestamp(...values: Array<string | null | undefined>):
8081
}
8182

8283
function getLatestUserMessageTimestamp(thread: ThreadSortInput): number {
84+
// Agents also post in the user role; only what the user wrote counts. A thread
85+
// the user never wrote in counts from its creation; older servers omit the field.
86+
if (thread.latestUserAuthoredMessageAt !== undefined) {
87+
return (
88+
getFirstSortableTimestamp(
89+
thread.latestUserAuthoredMessageAt,
90+
thread.createdAt,
91+
thread.updatedAt,
92+
) ?? Number.NEGATIVE_INFINITY
93+
);
94+
}
95+
8396
if (thread.latestUserMessageAt) {
8497
const latestUserMessageTimestamp = toSortableTimestamp(thread.latestUserMessageAt);
8598
if (latestUserMessageTimestamp !== null) {

0 commit comments

Comments
 (0)