Skip to content

Commit 007b1b8

Browse files
Olasunkanmi OyinlolaOlasunkanmi Oyinlola
authored andcommitted
feat(agent): Add WASM SQLite integration for chat history
- Integrates WASM SQLite for chat history storage via chat history worker. - Adds sql-wasm.wasm file. - Implements fallback mechanism to file storage for backward compatibility. - Optimizes recent chat history retrieval using the worker.
1 parent f3689c7 commit 007b1b8

File tree

4 files changed

+109
-44
lines changed

4 files changed

+109
-44
lines changed

sql-wasm.wasm

644 KB
Binary file not shown.

src/services/agent-state.ts

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { AgentState } from "../agents/interface";
22
import { COMMON } from "../application/constant";
33
import { GeminiLLMSnapShot } from "../llms/interface";
44
import { 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

710
export 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

src/services/chat-history-worker-fixed.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class ChatHistoryWorker {
8989
async processRequest(
9090
operation: ChatHistoryWorkerOperation,
9191
data: ChatHistoryWorkerData,
92-
requestId: string
92+
requestId: string,
9393
): Promise<any> {
9494
if (this.isProcessing) {
9595
throw new Error("Worker is busy processing another request");
@@ -161,7 +161,8 @@ export class ChatHistoryWorker {
161161
const history = this.chatHistoryRepo.get(agentId);
162162
resolve(history || []);
163163
} catch (error) {
164-
const errorMessage = error instanceof Error ? error.message : String(error);
164+
const errorMessage =
165+
error instanceof Error ? error.message : String(error);
165166
console.error(`Failed to get chat history: ${errorMessage}`);
166167
reject(new Error(`Failed to get chat history: ${errorMessage}`));
167168
}
@@ -172,7 +173,10 @@ export class ChatHistoryWorker {
172173
/**
173174
* Save chat history for an agent
174175
*/
175-
private async saveChatHistory(agentId: string, history: any[]): Promise<void> {
176+
private async saveChatHistory(
177+
agentId: string,
178+
history: any[],
179+
): Promise<void> {
176180
return new Promise((resolve, reject) => {
177181
setTimeout(() => {
178182
try {
@@ -192,7 +196,8 @@ export class ChatHistoryWorker {
192196
this.chatHistoryRepo.set(agentId, formattedHistory);
193197
resolve();
194198
} catch (error) {
195-
const errorMessage = error instanceof Error ? error.message : String(error);
199+
const errorMessage =
200+
error instanceof Error ? error.message : String(error);
196201
console.error(`Failed to save chat history: ${errorMessage}`);
197202
reject(new Error(`Failed to save chat history: ${errorMessage}`));
198203
}
@@ -210,7 +215,8 @@ export class ChatHistoryWorker {
210215
this.chatHistoryRepo.clear(agentId);
211216
resolve();
212217
} catch (error) {
213-
const errorMessage = error instanceof Error ? error.message : String(error);
218+
const errorMessage =
219+
error instanceof Error ? error.message : String(error);
214220
console.error(`Failed to clear chat history: ${errorMessage}`);
215221
reject(new Error(`Failed to clear chat history: ${errorMessage}`));
216222
}
@@ -229,7 +235,7 @@ export class ChatHistoryWorker {
229235
alias?: string;
230236
sessionId?: string;
231237
metadata?: any;
232-
}
238+
},
233239
): Promise<void> {
234240
return new Promise((resolve, reject) => {
235241
setTimeout(() => {
@@ -246,7 +252,8 @@ export class ChatHistoryWorker {
246252
});
247253
resolve();
248254
} catch (error) {
249-
const errorMessage = error instanceof Error ? error.message : String(error);
255+
const errorMessage =
256+
error instanceof Error ? error.message : String(error);
250257
console.error(`Failed to add chat message: ${errorMessage}`);
251258
reject(new Error(`Failed to add chat message: ${errorMessage}`));
252259
}
@@ -257,16 +264,22 @@ export class ChatHistoryWorker {
257264
/**
258265
* Get recent chat history for an agent
259266
*/
260-
private async getRecentHistory(agentId: string, limit: number): Promise<any[]> {
267+
private async getRecentHistory(
268+
agentId: string,
269+
limit: number,
270+
): Promise<any[]> {
261271
return new Promise((resolve, reject) => {
262272
setTimeout(() => {
263273
try {
264274
const history = this.chatHistoryRepo.getRecent(agentId, limit);
265275
resolve(history || []);
266276
} catch (error) {
267-
const errorMessage = error instanceof Error ? error.message : String(error);
277+
const errorMessage =
278+
error instanceof Error ? error.message : String(error);
268279
console.error(`Failed to get recent chat history: ${errorMessage}`);
269-
reject(new Error(`Failed to get recent chat history: ${errorMessage}`));
280+
reject(
281+
new Error(`Failed to get recent chat history: ${errorMessage}`),
282+
);
270283
}
271284
}, 0);
272285
});
@@ -282,9 +295,12 @@ export class ChatHistoryWorker {
282295
this.chatHistoryRepo.cleanup(daysToKeep);
283296
resolve();
284297
} catch (error) {
285-
const errorMessage = error instanceof Error ? error.message : String(error);
298+
const errorMessage =
299+
error instanceof Error ? error.message : String(error);
286300
console.error(`Failed to cleanup old chat history: ${errorMessage}`);
287-
reject(new Error(`Failed to cleanup old chat history: ${errorMessage}`));
301+
reject(
302+
new Error(`Failed to cleanup old chat history: ${errorMessage}`),
303+
);
288304
}
289305
}, 0);
290306
});
@@ -309,7 +325,9 @@ export class ChatHistoryWorker {
309325
*/
310326
cancel(): void {
311327
if (this.isProcessing) {
312-
console.warn(`Cancelling chat history operation: ${this.currentRequestId}`);
328+
console.warn(
329+
`Cancelling chat history operation: ${this.currentRequestId}`,
330+
);
313331
this.isProcessing = false;
314332
this.currentRequestId = null;
315333
}

0 commit comments

Comments
 (0)