@@ -2,7 +2,10 @@ import { AgentState } from "../agents/interface";
22import { COMMON } from "../application/constant" ;
33import { GeminiLLMSnapShot } from "../llms/interface" ;
44import { FileStorage , IStorage } from "./file-storage" ;
5- import { ChatHistoryWorker , ChatHistoryWorkerOperation } from "./chat-history-worker" ;
5+ import {
6+ ChatHistoryWorker ,
7+ ChatHistoryWorkerOperation ,
8+ } from "./chat-history-worker" ;
69
710export class AgentService {
811 private static instance : AgentService ;
@@ -22,7 +25,9 @@ export class AgentService {
2225 }
2326
2427 async getState ( agentId : string ) : Promise < AgentState | undefined > {
25- return this . storage . get < AgentState > ( `${ COMMON . AGENT_STATE_PREFIX } _${ agentId } ` ) ;
28+ return this . storage . get < AgentState > (
29+ `${ COMMON . AGENT_STATE_PREFIX } _${ agentId } ` ,
30+ ) ;
2631 }
2732
2833 async saveState ( agentId : string , state : AgentState ) : Promise < void > {
@@ -36,13 +41,17 @@ export class AgentService {
3641 const history = await this . chatHistoryWorker . processRequest (
3742 ChatHistoryWorkerOperation . GET_CHAT_HISTORY ,
3843 { agentId } ,
39- requestId
44+ requestId ,
4045 ) ;
4146 return history || [ ] ;
4247 } catch ( error ) {
4348 console . warn ( `Failed to get chat history for agent ${ agentId } :` , error ) ;
4449 // Fallback to file storage for backward compatibility
45- return ( await this . storage . get < any [ ] > ( `${ COMMON . CHAT_HISTORY_PREFIX } _${ agentId } ` ) ) || [ ] ;
50+ return (
51+ ( await this . storage . get < any [ ] > (
52+ `${ COMMON . CHAT_HISTORY_PREFIX } _${ agentId } ` ,
53+ ) ) || [ ]
54+ ) ;
4655 }
4756 }
4857
@@ -54,15 +63,21 @@ export class AgentService {
5463 await this . chatHistoryWorker . processRequest (
5564 ChatHistoryWorkerOperation . SAVE_CHAT_HISTORY ,
5665 { agentId, history } ,
57- requestId
66+ requestId ,
5867 ) ;
5968
6069 // Also save to file storage for backward compatibility during transition
61- await this . storage . set ( `${ COMMON . CHAT_HISTORY_PREFIX } _${ agentId } ` , history ) ;
70+ await this . storage . set (
71+ `${ COMMON . CHAT_HISTORY_PREFIX } _${ agentId } ` ,
72+ history ,
73+ ) ;
6274 } catch ( error ) {
6375 console . warn ( `Failed to save chat history for agent ${ agentId } :` , error ) ;
6476 // Fallback to file storage only
65- await this . storage . set ( `${ COMMON . CHAT_HISTORY_PREFIX } _${ agentId } ` , history ) ;
77+ await this . storage . set (
78+ `${ COMMON . CHAT_HISTORY_PREFIX } _${ agentId } ` ,
79+ history ,
80+ ) ;
6681 }
6782 }
6883
@@ -76,7 +91,7 @@ export class AgentService {
7691 await this . chatHistoryWorker . processRequest (
7792 ChatHistoryWorkerOperation . CLEAR_CHAT_HISTORY ,
7893 { agentId } ,
79- requestId
94+ requestId ,
8095 ) ;
8196
8297 // Also clear from file storage for backward compatibility
@@ -99,15 +114,15 @@ export class AgentService {
99114 alias ?: string ;
100115 sessionId ?: string ;
101116 metadata ?: any ;
102- }
117+ } ,
103118 ) : Promise < void > {
104119 try {
105120 // Use the chat history worker for async operations
106121 const requestId = `add-${ agentId } -${ Date . now ( ) } ` ;
107122 await this . chatHistoryWorker . processRequest (
108123 ChatHistoryWorkerOperation . ADD_CHAT_MESSAGE ,
109124 { agentId, message } ,
110- requestId
125+ requestId ,
111126 ) ;
112127 } catch ( error ) {
113128 console . warn ( `Failed to add chat message for agent ${ agentId } :` , error ) ;
@@ -117,18 +132,24 @@ export class AgentService {
117132 /**
118133 * Get recent chat history for an agent (optimized for performance)
119134 */
120- async getRecentChatHistory ( agentId : string , limit : number = 50 ) : Promise < any [ ] > {
135+ async getRecentChatHistory (
136+ agentId : string ,
137+ limit : number = 50 ,
138+ ) : Promise < any [ ] > {
121139 try {
122140 // Use the chat history worker for async operations
123141 const requestId = `recent-${ agentId } -${ Date . now ( ) } ` ;
124142 const history = await this . chatHistoryWorker . processRequest (
125143 ChatHistoryWorkerOperation . GET_RECENT_HISTORY ,
126144 { agentId, config : { limit } } ,
127- requestId
145+ requestId ,
128146 ) ;
129147 return history || [ ] ;
130148 } catch ( error ) {
131- console . warn ( `Failed to get recent chat history for agent ${ agentId } :` , error ) ;
149+ console . warn (
150+ `Failed to get recent chat history for agent ${ agentId } :` ,
151+ error ,
152+ ) ;
132153 // Fallback to regular getChatHistory
133154 const fullHistory = await this . getChatHistory ( agentId ) ;
134155 return fullHistory . slice ( - limit ) ;
@@ -145,18 +166,23 @@ export class AgentService {
145166 await this . chatHistoryWorker . processRequest (
146167 ChatHistoryWorkerOperation . CLEANUP_OLD_HISTORY ,
147168 { agentId : "" , config : { daysToKeep } } ,
148- requestId
169+ requestId ,
149170 ) ;
150171 } catch ( error ) {
151172 console . warn ( "Failed to cleanup old chat history:" , error ) ;
152173 }
153174 }
154175
155176 async getSnapshot ( agentId : string ) : Promise < GeminiLLMSnapShot | undefined > {
156- return this . storage . get < GeminiLLMSnapShot > ( `${ COMMON . SNAPSHOT_PREFIX } _${ agentId } ` ) ;
177+ return this . storage . get < GeminiLLMSnapShot > (
178+ `${ COMMON . SNAPSHOT_PREFIX } _${ agentId } ` ,
179+ ) ;
157180 }
158181
159- async saveSnapshot ( agentId : string , snapshot : GeminiLLMSnapShot ) : Promise < void > {
182+ async saveSnapshot (
183+ agentId : string ,
184+ snapshot : GeminiLLMSnapShot ,
185+ ) : Promise < void > {
160186 return this . storage . set ( `${ COMMON . SNAPSHOT_PREFIX } _${ agentId } ` , snapshot ) ;
161187 }
162188
@@ -167,10 +193,13 @@ export class AgentService {
167193 await this . chatHistoryWorker . processRequest (
168194 ChatHistoryWorkerOperation . CLEAR_CHAT_HISTORY ,
169195 { agentId } ,
170- requestId
196+ requestId ,
171197 ) ;
172198 } catch ( error ) {
173- console . warn ( `Failed to clear chat history from SQLite for agent ${ agentId } :` , error ) ;
199+ console . warn (
200+ `Failed to clear chat history from SQLite for agent ${ agentId } :` ,
201+ error ,
202+ ) ;
174203 }
175204
176205 // Clear from file storage
0 commit comments