From 21a988a34259eec14474aa90a82bc4e66663a1ca Mon Sep 17 00:00:00 2001 From: Richard Yu Date: Wed, 9 Jul 2025 16:44:15 -0700 Subject: [PATCH] [feat] Sort history by execution_start timestamp descending - Modified getHistory method in api.ts to sort history items by execution_start timestamp in descending order (newest first) - Added helper function to extract execution_start timestamp from history item status messages - Maintains backward compatibility with existing API interface --- src/scripts/api.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/scripts/api.ts b/src/scripts/api.ts index 85316a74a..040c3d2ab 100644 --- a/src/scripts/api.ts +++ b/src/scripts/api.ts @@ -712,11 +712,26 @@ export class ComfyApi extends EventTarget { try { const res = await this.fetchApi(`/history?max_items=${max_items}`) const json: Promise = await res.json() + const historyItems = Object.values(json).map((item) => ({ + ...item, + taskType: 'History' + })) + + // Sort by execution_start timestamp in descending order (newest first) + historyItems.sort((a, b) => { + const getExecutionStartTimestamp = (item: HistoryTaskItem) => { + if (!item.status?.messages) return 0 + const executionStartMessage = item.status.messages.find( + (msg) => msg[0] === 'execution_start' + ) + return executionStartMessage ? executionStartMessage[1].timestamp : 0 + } + + return getExecutionStartTimestamp(b) - getExecutionStartTimestamp(a) + }) + return { - History: Object.values(json).map((item) => ({ - ...item, - taskType: 'History' - })) + History: historyItems } } catch (error) { console.error(error)