-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathchatSessionManagementService.ts
More file actions
71 lines (54 loc) · 2.07 KB
/
chatSessionManagementService.ts
File metadata and controls
71 lines (54 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Result } from '../types'
import { ChatSessionService } from './chatSessionService'
import { AmazonQTokenServiceManager } from '../../shared/amazonQServiceManager/AmazonQTokenServiceManager'
export class ChatSessionManagementService {
static #instance?: ChatSessionManagementService
#sessionByTab: Map<string, ChatSessionService> = new Map<string, any>()
#amazonQServiceManager?: AmazonQTokenServiceManager
public static getInstance() {
if (!ChatSessionManagementService.#instance) {
ChatSessionManagementService.#instance = new ChatSessionManagementService()
}
return ChatSessionManagementService.#instance
}
public static reset() {
ChatSessionManagementService.#instance = undefined
}
private constructor() {}
public withAmazonQServiceManager(amazonQServiceManager: AmazonQTokenServiceManager) {
this.#amazonQServiceManager = amazonQServiceManager
return this
}
public hasSession(tabId: string): boolean {
return this.#sessionByTab.has(tabId)
}
public createSession(tabId: string): Result<ChatSessionService, string> {
if (this.#sessionByTab.has(tabId)) {
return {
success: true,
data: this.#sessionByTab.get(tabId)!,
}
}
const newSession = new ChatSessionService(this.#amazonQServiceManager)
this.#sessionByTab.set(tabId, newSession)
return {
success: true,
data: newSession,
}
}
public getSession(tabId: string): Result<ChatSessionService, string> {
const session = this.#sessionByTab.get(tabId)
return session ? { success: true, data: session } : this.createSession(tabId)
}
public deleteSession(tabId: string): Result<void, string> {
this.#sessionByTab.get(tabId)?.dispose()
this.#sessionByTab.delete(tabId)
return {
success: true,
data: undefined,
}
}
public dispose() {
this.#sessionByTab.forEach(session => session.dispose())
}
}