Skip to content

Commit d4efb50

Browse files
committed
Fix older thread message review findings
1 parent 20629ec commit d4efb50

3 files changed

Lines changed: 48 additions & 39 deletions

File tree

src/api/codexGateway.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,7 @@ export async function renameThread(threadId: string, threadName: string): Promis
14381438

14391439
export async function rollbackThread(threadId: string, numTurns: number): Promise<UiMessage[]> {
14401440
const payload = await callRpc<ThreadReadResponse>('thread/rollback', { threadId, numTurns })
1441-
return normalizeThreadMessagesV2(payload)
1441+
return normalizeThreadMessagesV2(payload, readThreadTurnStartIndex(payload))
14421442
}
14431443

14441444
export async function revertThreadFileChanges(threadId: string, turnId: string, cwd: string): Promise<{ reverted: number; errors: string[] }> {
@@ -1545,7 +1545,7 @@ export async function forkThread(
15451545
threadId: forkedThreadId,
15461546
cwd: normalizeThreadCwdFromPayload(payload),
15471547
model: normalizeThreadModelFromPayload(payload),
1548-
messages: normalizeThreadMessagesV2(payload),
1548+
messages: normalizeThreadMessagesV2(payload, readThreadTurnStartIndex(payload)),
15491549
}
15501550
} catch (error) {
15511551
throw normalizeCodexApiError(error, `Failed to fork thread ${threadId}`, 'thread/fork')

src/components/content/ThreadConversation.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ const props = defineProps<{
12401240
cwd: string
12411241
hasMorePersistedAbove?: boolean
12421242
isLoadingPersistedAbove?: boolean
1243-
loadEarlierMessages?: () => Promise<void>
1243+
loadEarlierMessages?: (threadId: string) => Promise<void>
12441244
}>()
12451245
12461246
const emit = defineEmits<{
@@ -4026,7 +4026,7 @@ async function loadMoreAbove(): Promise<void> {
40264026
if (renderWindowStart.value > 0) {
40274027
renderWindowStart.value = Math.max(0, renderWindowStart.value - LOAD_MORE_CHUNK)
40284028
} else if (props.hasMorePersistedAbove === true) {
4029-
await props.loadEarlierMessages?.()
4029+
await props.loadEarlierMessages?.(threadIdAtStart)
40304030
}
40314031
40324032
await nextTick()

src/server/codexAppServerBridge.ts

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5776,43 +5776,52 @@ export function createCodexBridgeMiddleware(): CodexBridgeMiddleware {
57765776
}
57775777

57785778
if (req.method === 'GET' && url.pathname === '/codex-api/thread-turn-page') {
5779-
const threadId = url.searchParams.get('threadId')?.trim() ?? ''
5780-
const beforeTurnId = url.searchParams.get('beforeTurnId')?.trim() ?? ''
5781-
const limitRaw = url.searchParams.get('limit')?.trim() ?? String(THREAD_RESPONSE_TURN_LIMIT)
5782-
const limit = Math.max(1, Math.min(50, Number.parseInt(limitRaw, 10) || THREAD_RESPONSE_TURN_LIMIT))
5783-
if (!threadId) {
5784-
setJson(res, 400, { error: 'Missing threadId' })
5785-
return
5786-
}
5779+
try {
5780+
const threadId = url.searchParams.get('threadId')?.trim() ?? ''
5781+
const beforeTurnId = url.searchParams.get('beforeTurnId')?.trim() ?? ''
5782+
const limitRaw = url.searchParams.get('limit')?.trim() ?? String(THREAD_RESPONSE_TURN_LIMIT)
5783+
const limit = Math.max(1, Math.min(50, Number.parseInt(limitRaw, 10) || THREAD_RESPONSE_TURN_LIMIT))
5784+
if (!threadId) {
5785+
setJson(res, 400, { error: 'Missing threadId' })
5786+
return
5787+
}
57875788

5788-
const threadReadResult = await appServer.rpc('thread/read', {
5789-
threadId,
5790-
includeTurns: true,
5791-
})
5792-
const record = asRecord(threadReadResult)
5793-
const thread = asRecord(record?.thread)
5794-
const turns = Array.isArray(thread?.turns) ? thread.turns : []
5795-
const beforeIndex = beforeTurnId
5796-
? turns.findIndex((turn) => asRecord(turn)?.id === beforeTurnId)
5797-
: turns.length
5798-
const endIndex = beforeIndex >= 0 ? beforeIndex : turns.length
5799-
const startIndex = Math.max(0, endIndex - limit)
5800-
const pageTurns = turns.slice(startIndex, endIndex)
5801-
const pagedResult = {
5802-
...record,
5803-
thread: {
5804-
...thread,
5805-
turns: pageTurns,
5806-
},
5807-
}
5808-
const sanitized = await sanitizeThreadTurnsInlinePayloads('thread/read', pagedResult)
5809-
const result = await mergeSessionSkillInputsIntoThreadResult(sanitized)
5789+
const threadReadResult = await appServer.rpc('thread/read', {
5790+
threadId,
5791+
includeTurns: true,
5792+
})
5793+
const record = asRecord(threadReadResult)
5794+
const thread = asRecord(record?.thread)
5795+
if (!record || !thread) {
5796+
setJson(res, 502, { error: 'thread/read returned an invalid thread response' })
5797+
return
5798+
}
58105799

5811-
setJson(res, 200, {
5812-
result,
5813-
startTurnIndex: startIndex,
5814-
hasMoreOlder: startIndex > 0,
5815-
})
5800+
const turns = Array.isArray(thread.turns) ? thread.turns : []
5801+
const beforeIndex = beforeTurnId
5802+
? turns.findIndex((turn) => asRecord(turn)?.id === beforeTurnId)
5803+
: turns.length
5804+
const endIndex = beforeIndex >= 0 ? beforeIndex : turns.length
5805+
const startIndex = Math.max(0, endIndex - limit)
5806+
const pageTurns = turns.slice(startIndex, endIndex)
5807+
const pagedResult = {
5808+
...record,
5809+
thread: {
5810+
...thread,
5811+
turns: pageTurns,
5812+
},
5813+
}
5814+
const sanitized = await sanitizeThreadTurnsInlinePayloads('thread/read', pagedResult)
5815+
const result = await mergeSessionSkillInputsIntoThreadResult(sanitized)
5816+
5817+
setJson(res, 200, {
5818+
result,
5819+
startTurnIndex: startIndex,
5820+
hasMoreOlder: startIndex > 0,
5821+
})
5822+
} catch (error) {
5823+
setJson(res, 500, { error: getErrorMessage(error, 'Failed to load earlier thread messages') })
5824+
}
58165825
return
58175826
}
58185827

0 commit comments

Comments
 (0)