perf: speed up code queue first paint

This commit is contained in:
Codex
2026-05-16 04:34:25 +00:00
parent 860b2a76b1
commit f1a6cb509b
2 changed files with 107 additions and 32 deletions
+62 -5
View File
@@ -14,7 +14,7 @@ const h = React.createElement;
const { useEffect, useMemo, useRef } = React;
const useState: any = React.useState;
const codexTranscriptChunkLimit = 120;
const codexInitialTaskLimit = 24;
const codexInitialTaskLimit = 12;
const codexMoreTaskLimit = 48;
const queueErrorPreviewLength = 1200;
@@ -317,14 +317,18 @@ function queueRunnableTaskId(queueRows: any[], queueId: string, rows: any[]): st
async function loadTaskList(apiBaseUrl: string, queueId = allQueuesId, searchQuery = ""): Promise<any> {
return requestJson(codexApi(
apiBaseUrl,
`/api/tasks/overview?limit=${codexInitialTaskLimit}&transcriptLimit=1&compact=1&selected=0&skipTrace=1${taskListQuerySuffix(queueId, searchQuery)}`,
`/api/tasks/overview?limit=${codexInitialTaskLimit}&transcriptLimit=1&compact=1&selected=0&includeActive=0&stats=0&skipTrace=1${taskListQuerySuffix(queueId, searchQuery)}`,
), codexNoCacheOptions());
}
async function loadTaskOverview(apiBaseUrl: string, preferId: string, afterSeq = 0, queueId = allQueuesId, searchQuery = "", skipTrace = false): Promise<any> {
async function loadTaskOverview(apiBaseUrl: string, preferId: string, afterSeq = 0, queueId = allQueuesId, searchQuery = "", skipTrace = false, options: AnyRecord = {}): Promise<any> {
const selectedParam = options.selected === false ? "&selected=0" : "";
const includeActiveParam = options.includeActive === false ? "&includeActive=0" : "";
const statsParam = options.stats === false ? "&stats=0" : "";
const limit = Number.isInteger(options.limit) && options.limit > 0 ? Math.min(500, options.limit) : codexInitialTaskLimit;
return requestJson(codexApi(
apiBaseUrl,
`/api/tasks/overview?limit=${codexInitialTaskLimit}&transcriptLimit=3&compact=1&afterSeq=${encodeURIComponent(String(Math.max(0, afterSeq)))}&preferId=${encodeURIComponent(preferId)}${skipTrace ? "&skipTrace=1" : ""}${taskListQuerySuffix(queueId, searchQuery)}`,
`/api/tasks/overview?limit=${encodeURIComponent(String(limit))}&transcriptLimit=3&compact=1&afterSeq=${encodeURIComponent(String(Math.max(0, afterSeq)))}&preferId=${encodeURIComponent(preferId)}${selectedParam}${includeActiveParam}${statsParam}${skipTrace ? "&skipTrace=1" : ""}${taskListQuerySuffix(queueId, searchQuery)}`,
), codexNoCacheOptions());
}
@@ -2528,7 +2532,10 @@ export function CodeQueuePage({ microservices, onRaw, apiBaseUrl = "/api", initi
const requestedTranscript = Array.isArray(requestedCached?.task?.transcript) ? requestedCached.task.transcript : [];
const overviewAfterSeq = transcriptResumeSeq(requestedTranscript);
let tasksResult = null;
tasksResult = await loadTaskOverview(apiBaseUrl, requestedId, overviewAfterSeq, queueFilterId, "", true);
const firstPaintListOnly = trackLoad && requestedId.length === 0;
tasksResult = firstPaintListOnly
? await loadTaskList(apiBaseUrl, queueFilterId, "")
: await loadTaskOverview(apiBaseUrl, requestedId, overviewAfterSeq, queueFilterId, "", true, { stats: false });
if (token !== queueLoadTokenRef.current) {
if (trackLoad) trackedLoadInFlightRef.current = false;
return;
@@ -2574,6 +2581,56 @@ export function CodeQueuePage({ microservices, onRaw, apiBaseUrl = "/api", initi
const cached = sessionCacheRef.current.get(nextId);
if (cached?.task) sessionCacheRef.current.set(nextId, { ...cached, task: { ...row, ...cached.task, status: row.status, updatedAt: row.updatedAt } });
}
if (firstPaintListOnly && row) {
publishCachedTask(nextId, {
...row,
_summaryLoaded: false,
transcript: [],
_detailLoaded: false,
_transcriptComplete: false,
_transcriptPreview: true,
}, detailLoadTokenRef.current);
setSelectedDetailLoading(true);
setLoadStats({
phase: "complete",
taskId: nextId,
queueMs,
detailMs: 0,
totalMs: performance.now() - startedAt,
chunks: 0,
transcriptRows: 0,
partial: true,
completedAt: new Date(),
});
setRefreshedAt(new Date());
if (trackLoad) trackedLoadInFlightRef.current = false;
void ensureTraceSummary(nextId, true)
.catch((err) => setError(errorText(err, "加载 Codex Trace Summary 失败")));
void loadTaskOverview(apiBaseUrl, nextId, 0, queueFilterId, "", false)
.then((full: any) => {
if (token !== queueLoadTokenRef.current) return;
const fullRows = taskRows(full);
const selected = overviewSelectedTask(full);
if (fullRows.length > 0) {
setTasksData((previous: any) => {
const mergedRows = mergeTaskRowsPreferLatest([taskRows(previous), fullRows], activeSortId);
return { ...previous, statistics: full?.statistics || previous?.statistics, tasks: applyLocalReadStateToRows(mergedRows) };
});
}
if (selected?.id === selectedIdRef.current) {
const transcript = Array.isArray(selected.transcript) ? selected.transcript : [];
publishCachedTask(selected.id, {
...selected,
transcript,
_summaryLoaded: true,
_detailLoaded: transcript.length > 0,
_transcriptPreview: Boolean(full?.selected?.preview),
}, detailLoadTokenRef.current);
}
})
.catch(() => {});
return;
}
if (overviewTask?.id === nextId && overviewTranscript !== null) {
const cached = sessionCacheRef.current.get(nextId);
const existingTranscript = Array.isArray(cached?.task?.transcript) ? cached.task.transcript : [];