11import * as fs from 'fs'
22import path from 'path'
3+ import { randomUUID } from 'node:crypto'
34
4- import { getCurrentChatDir , getMostRecentChatDir , getProjectDataDir } from '../project-files'
5+ import {
6+ getCurrentChatDir ,
7+ getMostRecentChatDir ,
8+ getProjectDataDir ,
9+ } from '../project-files'
510import { logger } from './logger'
611
712import type { ChatMessage , ContentBlock } from '../types/chat'
@@ -21,9 +26,9 @@ type SavedChatState = {
2126 */
2227function extractToggleIds ( blocks : ContentBlock [ ] | undefined ) : string [ ] {
2328 if ( ! blocks ) return [ ]
24-
29+
2530 const ids : string [ ] = [ ]
26-
31+
2732 for ( const block of blocks ) {
2833 if ( block . type === 'agent' ) {
2934 ids . push ( block . agentId )
@@ -33,7 +38,7 @@ function extractToggleIds(blocks: ContentBlock[] | undefined): string[] {
3338 ids . push ( block . toolCallId )
3439 }
3540 }
36-
41+
3742 return ids
3843}
3944
@@ -42,11 +47,11 @@ function extractToggleIds(blocks: ContentBlock[] | undefined): string[] {
4247 */
4348export function getAllToggleIdsFromMessages ( messages : ChatMessage [ ] ) : string [ ] {
4449 const ids : string [ ] = [ ]
45-
50+
4651 for ( const message of messages ) {
4752 ids . push ( ...extractToggleIds ( message . blocks ) )
4853 }
49-
54+
5055 return ids
5156}
5257
@@ -69,11 +74,14 @@ export function getChatMessagesPath(): string {
6974/**
7075 * Save both the RunState and ChatMessage[] to disk
7176 */
72- export function saveChatState ( runState : RunState , messages : ChatMessage [ ] ) : void {
77+ export function saveChatState (
78+ runState : RunState ,
79+ messages : ChatMessage [ ] ,
80+ ) : void {
7381 try {
7482 const runStatePath = getRunStatePath ( )
7583 const messagesPath = getChatMessagesPath ( )
76-
84+
7785 fs . writeFileSync ( runStatePath , JSON . stringify ( runState , null , 2 ) )
7886 fs . writeFileSync ( messagesPath , JSON . stringify ( messages , null , 2 ) )
7987 } catch ( error ) {
@@ -92,14 +100,19 @@ export function saveChatState(runState: RunState, messages: ChatMessage[]): void
92100 * recently modified chat directory is used.
93101 * Returns null if no previous chat exists or files can't be parsed.
94102 */
95- export function loadMostRecentChatState ( chatId ?: string ) : SavedChatState | null {
103+ export function loadMostRecentChatState (
104+ chatId ?: string ,
105+ ) : SavedChatState | null {
96106 try {
97107 let chatDir : string | null = null
98108
99109 if ( chatId && chatId . trim ( ) . length > 0 ) {
100110 const baseDir = path . join ( getProjectDataDir ( ) , 'chats' )
101111 const candidateDir = path . join ( baseDir , chatId . trim ( ) )
102- if ( fs . existsSync ( candidateDir ) && fs . statSync ( candidateDir ) . isDirectory ( ) ) {
112+ if (
113+ fs . existsSync ( candidateDir ) &&
114+ fs . statSync ( candidateDir ) . isDirectory ( )
115+ ) {
103116 chatDir = candidateDir
104117 } else {
105118 logger . debug (
@@ -133,12 +146,18 @@ export function loadMostRecentChatState(chatId?: string): SavedChatState | null
133146 const messagesContent = fs . readFileSync ( messagesPath , 'utf8' )
134147
135148 const runState = JSON . parse ( runStateContent ) as RunState
149+ runState . traceSessionId ??= randomUUID ( )
136150 const messages = JSON . parse ( messagesContent ) as ChatMessage [ ]
137151
138152 const resolvedChatId = path . basename ( chatDir )
139153
140154 logger . info (
141- { runStatePath, messagesPath, messageCount : messages . length , chatId : resolvedChatId } ,
155+ {
156+ runStatePath,
157+ messagesPath,
158+ messageCount : messages . length ,
159+ chatId : resolvedChatId ,
160+ } ,
142161 'Loaded chat state from chat directory' ,
143162 )
144163
@@ -161,18 +180,15 @@ export function clearChatState(): void {
161180 try {
162181 const runStatePath = getRunStatePath ( )
163182 const messagesPath = getChatMessagesPath ( )
164-
183+
165184 if ( fs . existsSync ( runStatePath ) ) {
166185 fs . unlinkSync ( runStatePath )
167186 }
168187 if ( fs . existsSync ( messagesPath ) ) {
169188 fs . unlinkSync ( messagesPath )
170189 }
171-
172- logger . debug (
173- { runStatePath, messagesPath } ,
174- 'Cleared chat state files'
175- )
190+
191+ logger . debug ( { runStatePath, messagesPath } , 'Cleared chat state files' )
176192 } catch ( error ) {
177193 logger . error (
178194 {
0 commit comments