forked from Kilo-Org/kilocode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageQueueService.ts
More file actions
98 lines (74 loc) · 1.9 KB
/
MessageQueueService.ts
File metadata and controls
98 lines (74 loc) · 1.9 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { EventEmitter } from "events"
import { v4 as uuidv4 } from "uuid"
import { QueuedMessage } from "@roo-code/types"
export interface MessageQueueState {
messages: QueuedMessage[]
isProcessing: boolean
isPaused: boolean
}
export interface QueueEvents {
stateChanged: [messages: QueuedMessage[]]
}
export class MessageQueueService extends EventEmitter<QueueEvents> {
private _messages: QueuedMessage[]
constructor() {
super()
this._messages = []
}
private findMessage(id: string) {
const index = this._messages.findIndex((msg) => msg.id === id)
if (index === -1) {
return { index, message: undefined }
}
return { index, message: this._messages[index] }
}
public addMessage(text: string, images?: string[]): QueuedMessage | undefined {
if (!text && !images?.length) {
return undefined
}
const message: QueuedMessage = {
timestamp: Date.now(),
id: uuidv4(),
text,
images,
}
this._messages.push(message)
this.emit("stateChanged", this._messages)
return message
}
public removeMessage(id: string): boolean {
const { index, message } = this.findMessage(id)
if (!message) {
return false
}
this._messages.splice(index, 1)
this.emit("stateChanged", this._messages)
return true
}
public updateMessage(id: string, text: string, images?: string[]): boolean {
const { message } = this.findMessage(id)
if (!message) {
return false
}
message.timestamp = Date.now()
message.text = text
message.images = images
this.emit("stateChanged", this._messages)
return true
}
public dequeueMessage(): QueuedMessage | undefined {
const message = this._messages.shift()
this.emit("stateChanged", this._messages)
return message
}
public get messages(): QueuedMessage[] {
return this._messages
}
public isEmpty(): boolean {
return this._messages.length === 0
}
public dispose(): void {
this._messages = []
this.removeAllListeners()
}
}