fix(tui): keep v2 realtime messages newest first

This commit is contained in:
Dax Raad 2026-05-03 15:09:45 -04:00
commit a1bfbd7852

View file

@ -11,21 +11,21 @@ import { createSimpleContext } from "./helper"
import { useSDK } from "./sdk" import { useSDK } from "./sdk"
function activeAssistant(messages: SessionMessage[]) { function activeAssistant(messages: SessionMessage[]) {
const index = messages.findLastIndex((message) => message.type === "assistant" && !message.time.completed) const index = messages.findIndex((message) => message.type === "assistant" && !message.time.completed)
if (index < 0) return if (index < 0) return
const assistant = messages[index] const assistant = messages[index]
return assistant?.type === "assistant" ? assistant : undefined return assistant?.type === "assistant" ? assistant : undefined
} }
function activeCompaction(messages: SessionMessage[]) { function activeCompaction(messages: SessionMessage[]) {
const index = messages.findLastIndex((message) => message.type === "compaction") const index = messages.findIndex((message) => message.type === "compaction")
if (index < 0) return if (index < 0) return
const compaction = messages[index] const compaction = messages[index]
return compaction?.type === "compaction" ? compaction : undefined return compaction?.type === "compaction" ? compaction : undefined
} }
function activeShell(messages: SessionMessage[], callID: string) { function activeShell(messages: SessionMessage[], callID: string) {
const index = messages.findLastIndex((message) => message.type === "shell" && message.callID === callID) const index = messages.findIndex((message) => message.type === "shell" && message.callID === callID)
if (index < 0) return if (index < 0) return
const shell = messages[index] const shell = messages[index]
return shell?.type === "shell" ? shell : undefined return shell?.type === "shell" ? shell : undefined
@ -74,7 +74,7 @@ export const { use: useSyncV2, provider: SyncProviderV2 } = createSimpleContext(
switch (event.type) { switch (event.type) {
case "session.next.prompted": { case "session.next.prompted": {
update(event.properties.sessionID, (draft) => { update(event.properties.sessionID, (draft) => {
draft.push({ draft.unshift({
id: event.id, id: event.id,
type: "user", type: "user",
text: event.properties.prompt.text, text: event.properties.prompt.text,
@ -87,7 +87,7 @@ export const { use: useSyncV2, provider: SyncProviderV2 } = createSimpleContext(
} }
case "session.next.synthetic": case "session.next.synthetic":
update(event.properties.sessionID, (draft) => { update(event.properties.sessionID, (draft) => {
draft.push({ draft.unshift({
id: event.id, id: event.id,
type: "synthetic", type: "synthetic",
sessionID: event.properties.sessionID, sessionID: event.properties.sessionID,
@ -98,7 +98,7 @@ export const { use: useSyncV2, provider: SyncProviderV2 } = createSimpleContext(
break break
case "session.next.shell.started": case "session.next.shell.started":
update(event.properties.sessionID, (draft) => { update(event.properties.sessionID, (draft) => {
draft.push({ draft.unshift({
id: event.id, id: event.id,
type: "shell", type: "shell",
callID: event.properties.callID, callID: event.properties.callID,
@ -120,7 +120,7 @@ export const { use: useSyncV2, provider: SyncProviderV2 } = createSimpleContext(
update(event.properties.sessionID, (draft) => { update(event.properties.sessionID, (draft) => {
const currentAssistant = activeAssistant(draft) const currentAssistant = activeAssistant(draft)
if (currentAssistant) currentAssistant.time.completed = event.properties.timestamp if (currentAssistant) currentAssistant.time.completed = event.properties.timestamp
draft.push({ draft.unshift({
id: event.id, id: event.id,
type: "assistant", type: "assistant",
agent: event.properties.agent, agent: event.properties.agent,
@ -259,7 +259,7 @@ export const { use: useSyncV2, provider: SyncProviderV2 } = createSimpleContext(
break break
case "session.next.compaction.started": case "session.next.compaction.started":
update(event.properties.sessionID, (draft) => { update(event.properties.sessionID, (draft) => {
draft.push({ draft.unshift({
id: event.id, id: event.id,
type: "compaction", type: "compaction",
reason: event.properties.reason, reason: event.properties.reason,