Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(export): sort conversation messages by timestamp instants (#4559)

fengyue-xvece231af136fe
diff --git a/frontend/src/utils/sessionMarkdown.test.ts b/frontend/src/utils/sessionMarkdown.test.ts
index 120b8a9a4..25973cb4f 100644
--- a/frontend/src/utils/sessionMarkdown.test.ts
+++ b/frontend/src/utils/sessionMarkdown.test.ts
@@ -61,3 +61,40 @@ test('buildSessionMarkdown exports visible conversation content without internal
assert.doesNotMatch(markdown, /hidden system prompt/)
assert.equal(markdown.match(/\[Notes\]/g)?.length, 1)
})
+
+
+test('export messages sort by instants across fractional timestamps and timezone offsets', async () => {
+ const messages = [
+ { id: 'late', role: 'assistant', created_at: '2026-01-01T00:00:00.100Z' },
+ { id: 'early', role: 'user', created_at: '2026-01-01T00:00:00Z' },
+ { id: 'earliest', role: 'user', created_at: '2026-01-01T08:59:59+09:00' },
+ ]
+ const result = await collectAllSessionMessages(async () => messages)
+ assert.deepEqual(result.map(message => message.id), ['earliest', 'early', 'late'])
+})
+
+test('equal instants use role ordering even with different timestamp representations', async () => {
+ const result = await collectAllSessionMessages(async () => [
+ { id: 'answer', role: 'assistant', created_at: '2026-01-01T00:00:00Z' },
+ { id: 'question', role: 'user', created_at: '2026-01-01T08:00:00+08:00' },
+ ])
+ assert.deepEqual(result.map(message => message.id), ['question', 'answer'])
+})
+
+
+test('export ordering preserves RFC3339Nano precision below a millisecond', async () => {
+ const result = await collectAllSessionMessages(async () => [
+ { id: 'later-user', role: 'user', created_at: '2026-01-01T00:00:00.000000200Z' },
+ { id: 'earlier-assistant', role: 'assistant', created_at: '2026-01-01T00:00:00.000000100Z' },
+ ])
+ assert.deepEqual(result.map(message => message.id), ['earlier-assistant', 'later-user'])
+})
+
+test('missing and invalid export timestamps have a deterministic fallback', async () => {
+ const result = await collectAllSessionMessages(async () => [
+ { id: 'valid', role: 'user', created_at: '2026-01-01T00:00:00Z' },
+ { id: 'invalid', role: 'user', created_at: 'not-a-date' },
+ { id: 'missing', role: 'assistant' },
+ ])
+ assert.deepEqual(result.map(message => message.id), ['missing', 'invalid', 'valid'])
+})
diff --git a/frontend/src/utils/sessionMarkdown.ts b/frontend/src/utils/sessionMarkdown.ts
index 2813b6f37..b7eb2fc35 100644
--- a/frontend/src/utils/sessionMarkdown.ts
+++ b/frontend/src/utils/sessionMarkdown.ts
@@ -40,6 +40,20 @@ const roleOrder = (role?: string): number => {
return 2
}
+function compareMessageTimes(left: string, right: string): number {
+ const a = Date.parse(left), b = Date.parse(right)
+ const validA = Number.isFinite(a), validB = Number.isFinite(b)
+ if (!validA || !validB) {
+ if (validA !== validB) return validA ? 1 : -1
+ return left.localeCompare(right)
+ }
+ // Go's RFC3339Nano timestamps may carry precision below Date's milliseconds.
+ const remainder = (value: string) => Number(
+ (value.match(/\.(\d+)(?:Z|[+-]\d{2}:\d{2})$/i)?.[1] || '').padEnd(9, '0').slice(3, 9),
+ )
+ return a - b || remainder(left) - remainder(right)
+}
+
export async function collectAllSessionMessages(
fetchPage: SessionMessagePageFetcher,
pageSize = 100,
@@ -69,7 +83,7 @@ export async function collectAllSessionMessages(
return true
})
.sort((a, b) => {
- const timeCompare = String(a.created_at || '').localeCompare(String(b.created_at || ''))
+ const timeCompare = compareMessageTimes(a.created_at || '', b.created_at || '')
return timeCompare || roleOrder(a.role) - roleOrder(b.role)
})
}