From a5f44fdabbd2a8a8fe166f8f2368879d8ee1f100 Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:19:48 +0800 Subject: [PATCH] Import apps/android/src/state/session-state.ts --- apps/android/src/state/session-state.ts | 61 +++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 apps/android/src/state/session-state.ts diff --git a/apps/android/src/state/session-state.ts b/apps/android/src/state/session-state.ts new file mode 100644 index 0000000..cfd2178 --- /dev/null +++ b/apps/android/src/state/session-state.ts @@ -0,0 +1,61 @@ +import type { CodexTimelineState } from '@dsh-remote/client-core' +import type { ChatItem, ChatMessage, HistoryEntry, MuxStreamFrame } from '../types' +import type { AppState } from './store-state' + +export function applyFeedback(items: ChatItem[], ratings?: Map): ChatItem[] { + return ratings === undefined ? items : items.map(item => item.kind === 'message' + ? { ...item, feedback: ratings.get(item.id) } : item) +} + +export function latestHistoryLifecycle(events: HistoryEntry[]): AppState['sessionLifecycles'][string] | undefined { + let latest: AppState['sessionLifecycles'][string] | undefined + for (const { event } of events) { + if (event.type !== 'turn/start' && event.type !== 'turn/end') continue + if (latest !== undefined && event.seq < latest.seq) continue + latest = { + seq: event.seq, running: event.type === 'turn/start', + ...(event.data.turn === undefined ? {} : { turn: String(event.data.turn) }) + } + } + return latest +} + +export function withActiveCodexTurn(timeline: CodexTimelineState, activeTurnId: string | undefined): CodexTimelineState { + if (activeTurnId === undefined) return timeline + return { + ...timeline, + activeTurnId, + session: timeline.session.status === 'waiting' + ? timeline.session + : { ...timeline.session, status: 'running' }, + } +} + +export function isHarnessPromptStarted(frame: MuxStreamFrame): boolean { + const payload = frame.payload + if (payload.type === 'approval/requested' || payload.type === 'question/requested') return true + if (payload.type !== 'session/event' || payload.event === undefined) return false + const event = payload.event + if (event.type === 'assistant/message' || event.type === 'tool/call' || event.type === 'tool/result') return true + if (event.type !== 'assistant/chunk') return false + const chunk = event.data.chunk + if (typeof chunk !== 'object' || chunk === null) return false + const value = chunk as Record + if (value.type === 'text-delta' || value.type === 'reasoning-delta') { + return typeof value.text === 'string' && value.text.length > 0 + } + return value.type === 'tool-call-delta' + && (typeof value.argumentsDelta !== 'string' || value.argumentsDelta.length > 0) +} + +export function record(value: unknown): Record { + return isRecord(value) ? value : {} +} + +export function stringValue(value: unknown): string | undefined { + return typeof value === 'string' && value.length > 0 ? value : undefined +} + +export function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null +}