From 22a715cefae0865600e32b5705681212cc76ecb7 Mon Sep 17 00:00:00 2001 From: Bob Lee Date: Sun, 8 Nov 2026 09:11:57 +0800 Subject: [PATCH] fix(mobile-web): stamp assistant messages with the turn completion time The reducer captured `Date.now()` on `dialog-turn-started` and reused it for the assistant message, so a turn that ran tools for minutes showed the moment the user pressed send instead of when the answer landed. Record the completion time when the turn closes. Replayed turns now prefer the host's `endTime` and fall back to the turn timestamp for records written before that field existed. --- src/mobile-web/src/services/SessionEventReducer.ts | 2 +- src/mobile-web/src/services/SessionRecordPresentation.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mobile-web/src/services/SessionEventReducer.ts b/src/mobile-web/src/services/SessionEventReducer.ts index 3007390c53..f57167275b 100644 --- a/src/mobile-web/src/services/SessionEventReducer.ts +++ b/src/mobile-web/src/services/SessionEventReducer.ts @@ -80,7 +80,7 @@ export class SessionEventReducer { const current = readBackgroundDelivery(this.active.backgroundDelivery, turnId); if (delivery && (delivery.executionGeneration ?? 0) < (current?.executionGeneration ?? 0)) return null; const active=this.active;this.active=null; - const message:ChatMessage={id:`${turnId}_assistant`,turn_id:turnId,role:'assistant',content:active.text,thinking:active.thinking,tools:active.tools,items:active.items,timestamp:this.timestamp,status:envelope.event.endsWith('failed')||payload.success===false?'error':envelope.event.endsWith('completed')?'completed':'cancelled',error:typeof payload.error==='string'?payload.error:undefined}; + const message:ChatMessage={id:`${turnId}_assistant`,turn_id:turnId,role:'assistant',content:active.text,thinking:active.thinking,tools:active.tools,items:active.items,timestamp:String(Math.floor(Date.now()/1000)),status:envelope.event.endsWith('failed')||payload.success===false?'error':envelope.event.endsWith('completed')?'completed':'cancelled',error:typeof payload.error==='string'?payload.error:undefined}; if (delivery || active.backgroundDelivery) { message.metadata = { backgroundDelivery: delivery }; const user:ChatMessage = {id:`${turnId}_user`,turn_id:turnId,role:'user',content:this.userText,timestamp:this.timestamp,status:message.status,metadata:{...this.userMetadata,backgroundDelivery:delivery}}; diff --git a/src/mobile-web/src/services/SessionRecordPresentation.ts b/src/mobile-web/src/services/SessionRecordPresentation.ts index d6b7699616..119ffacb81 100644 --- a/src/mobile-web/src/services/SessionRecordPresentation.ts +++ b/src/mobile-web/src/services/SessionRecordPresentation.ts @@ -2,7 +2,7 @@ import type { ChatMessage, ChatMessageItem, RemoteToolStatus } from './RemoteSes /** Rendering accepts rich records without narrowing their stored protocol data. */ export interface MobileStoredTurn { - turnId: string; turnIndex: number; status: string; timestamp: number; + turnId: string; turnIndex: number; status: string; timestamp: number; endTime?: number; userMessage: { id: string; content: string; timestamp: number; metadata?: unknown }; modelRounds: Array<{ roundIndex: number; textItems: RichText[]; thinkingItems?: RichText[]; toolItems: RichTool[] }>; error?: string; @@ -48,6 +48,6 @@ export function presentSessionTurn(turn: MobileStoredTurn): ChatMessage[] { }); return [ {id:turn.userMessage.id,turn_id:turn.turnId,turn_index:turn.turnIndex,role:'user',content:turn.userMessage.content,timestamp:String(turn.userMessage.timestamp),metadata:turn.userMessage.metadata,images:presentUserMessageImages(turn.userMessage.metadata)}, - {id:`${turn.turnId}_assistant`,turn_id:turn.turnId,role:'assistant',content:items.filter(i=>i.type==='text').map(i=>i.content??'').join(''),thinking:items.filter(i=>i.type==='thinking').map(i=>i.content??'').join(''),items,tools:items.flatMap(i=>i.tool?[i.tool]:[]),status:turn.status==='inprogress'?'streaming':turn.status,error:turn.error,timestamp:String(turn.timestamp)}, + {id:`${turn.turnId}_assistant`,turn_id:turn.turnId,role:'assistant',content:items.filter(i=>i.type==='text').map(i=>i.content??'').join(''),thinking:items.filter(i=>i.type==='thinking').map(i=>i.content??'').join(''),items,tools:items.flatMap(i=>i.tool?[i.tool]:[]),status:turn.status==='inprogress'?'streaming':turn.status,error:turn.error,timestamp:String(turn.endTime ?? turn.timestamp)}, ]; }