-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update api.rest * basic collector * refact * fix * comment * example collect * create user and convo api.rest * Update packages/server/src/converse/service.ts Co-authored-by: Laurent Leclerc Poulin <laurentlp@users.noreply.github.com> * message queue * refact * runMessageQueue * change timeout * fix * queue by threadId * fix failing message failed the entire queue * improve queue * fix merge Co-authored-by: Laurent Leclerc Poulin <laurentlp@users.noreply.github.com>
- Loading branch information
1 parent
335cf3c
commit f4ecbca
Showing
7 changed files
with
133 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Message, uuid } from '@botpress/messaging-base' | ||
import { Service } from '../base/service' | ||
import { MessageCreatedEvent, MessageEvents } from '../messages/events' | ||
import { MessageService } from '../messages/service' | ||
import { Collector } from './types' | ||
|
||
export class ConverseService extends Service { | ||
private collectors: { [conversationId: string]: Collector[] } = {} | ||
|
||
constructor(private messages: MessageService) { | ||
super() | ||
} | ||
|
||
async setup() { | ||
this.messages.events.on(MessageEvents.Created, this.handleMessageCreated.bind(this)) | ||
} | ||
|
||
private async handleMessageCreated({ message }: MessageCreatedEvent) { | ||
const collectors = this.collectors[message.conversationId] || [] | ||
|
||
for (const collector of collectors) { | ||
collector.messages.push(message) | ||
|
||
if (collector.messages.length > 0) { | ||
this.resetCollectorTimeout(collector, 250) | ||
} | ||
} | ||
} | ||
|
||
async collect(conversationId: uuid): Promise<Message[]> { | ||
const collector = this.addCollector(conversationId) | ||
this.resetCollectorTimeout(collector, 5000) | ||
|
||
return new Promise<Message[]>((resolve) => { | ||
collector.resolve = resolve | ||
}) | ||
} | ||
|
||
private addCollector(conversationId: uuid): Collector { | ||
if (!this.collectors[conversationId]) { | ||
this.collectors[conversationId] = [] | ||
} | ||
|
||
const collector: Collector = { | ||
conversationId, | ||
messages: [] | ||
} | ||
|
||
this.collectors[conversationId].push(collector) | ||
|
||
return collector | ||
} | ||
|
||
private removeCollector(collector: Collector) { | ||
const { conversationId } = collector | ||
|
||
const index = this.collectors[conversationId].indexOf(collector) | ||
this.collectors[conversationId].splice(index, 1) | ||
|
||
if (this.collectors[conversationId].length === 0) { | ||
delete this.collectors[conversationId] | ||
} | ||
} | ||
|
||
private resetCollectorTimeout(collector: Collector, time: number) { | ||
if (collector.timeout) { | ||
clearTimeout(collector.timeout) | ||
} | ||
|
||
collector.timeout = setTimeout(() => { | ||
this.removeCollector(collector) | ||
collector.resolve!(collector.messages) | ||
}, time) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { uuid, Message } from '@botpress/messaging-base' | ||
|
||
export interface Collector { | ||
conversationId: uuid | ||
messages: Message[] | ||
resolve?: (x: Message[]) => void | ||
timeout?: NodeJS.Timeout | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters