feat(historyV2): create sythetic queue priority (#6336)

## Summary

Create display priority based on execution success timestamps.

Next up is displaying in progress prompts in the queue.

## Review Focus

@DrJKL and I discussed logic and decided for history, execution success
(when the prompt finishes) is the best way to assign priority.

This does differ from existing cloud logic which uses execution start
time.

For prompt progress I intend to create a priority for them based on
start time.

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-6336-feat-historyV2-create-sythetic-queue-priority-2996d73d365081ffa3a0f8071c178066)
by [Unito](https://www.unito.io)
This commit is contained in:
Arjan Singh
2025-10-27 16:08:33 -07:00
committed by GitHub
parent d8657aaee3
commit d26309c7ab
4 changed files with 318 additions and 23 deletions

View File

@@ -1,10 +1,7 @@
/**
* @fileoverview Adapter to convert V2 history format to V1 format
* @module platform/remote/comfyui/history/adapters/v2ToV1Adapter
*
* Converts cloud API V2 response format to the V1 format expected by the app.
*/
import type { HistoryTaskItem, TaskPrompt } from '../types/historyV1Types'
import type {
HistoryResponseV2,
@@ -13,31 +10,55 @@ import type {
TaskPromptV2
} from '../types/historyV2Types'
/**
* Maps V2 prompt format to V1 prompt tuple format.
*/
function mapPromptV2toV1(
promptV2: TaskPromptV2,
outputs: TaskOutput
outputs: TaskOutput,
syntheticPriority: number
): TaskPrompt {
const outputNodesIds = Object.keys(outputs)
const { priority, prompt_id, extra_data } = promptV2
return [priority, prompt_id, {}, extra_data, outputNodesIds]
return [
syntheticPriority,
promptV2.prompt_id,
{},
promptV2.extra_data,
Object.keys(outputs)
]
}
function getExecutionSuccessTimestamp(item: RawHistoryItemV2): number {
return (
item.status?.messages?.find((m) => m[0] === 'execution_success')?.[1]
?.timestamp ?? 0
)
}
/**
* Maps V2 history format to V1 history format.
*/
export function mapHistoryV2toHistory(
historyV2Response: HistoryResponseV2
): HistoryTaskItem[] {
return historyV2Response.history.map(
({ prompt, status, outputs, meta }: RawHistoryItemV2): HistoryTaskItem => ({
const history = historyV2Response.history
// Sort by execution_success timestamp, descending (newest first)
history.sort((a, b) => {
return getExecutionSuccessTimestamp(b) - getExecutionSuccessTimestamp(a)
})
// Count items with valid timestamps for synthetic priority calculation
const countWithTimestamps = history.filter(
(item) => getExecutionSuccessTimestamp(item) > 0
).length
return history.map((item, index): HistoryTaskItem => {
const { prompt, outputs, status, meta } = item
const timestamp = getExecutionSuccessTimestamp(item)
// Items with timestamps get priority based on sorted position (highest first)
const syntheticPriority = timestamp > 0 ? countWithTimestamps - index : 0
return {
taskType: 'History' as const,
prompt: mapPromptV2toV1(prompt, outputs),
prompt: mapPromptV2toV1(prompt, outputs, syntheticPriority),
status,
outputs,
meta
})
)
}
})
}