1- import * as sdk from '@botpress/sdk'
21import { isBrowser } from 'browser-or-node'
3- import * as updateScheduler from './summaryUpdateScheduler '
2+ import * as onNewMessageHandler from './onNewMessageHandler '
43import * as summaryUpdater from './tagsUpdater'
54import * as types from './types'
65import * as bp from '.botpress'
76
8- type CommonProps = types . CommonProps
7+ const HOUR_MILLISECONDS = 60 * 60 * 1000
98
109const plugin = new bp . Plugin ( {
1110 actions : { } ,
@@ -16,10 +15,16 @@ plugin.on.afterIncomingMessage('*', async (props) => {
1615 return
1716 }
1817 const { conversation } = await props . client . getConversation ( { id : props . data . conversationId } )
19- const { message_count } = await _onNewMessage ( { ...props , conversation } )
18+ await onNewMessageHandler . onNewMessage ( { ...props , conversation } )
2019
21- if ( props . configuration . aiEnabled && updateScheduler . isTimeToUpdate ( message_count ) ) {
22- await props . events . updateAiInsight . withConversationId ( props . data . conversationId ) . emit ( { } )
20+ if ( props . configuration . aiEnabled ) {
21+ const eventType = `${ props . alias } #updateAiInsight`
22+ const events = await props . client . listEvents ( { type : eventType , status : 'scheduled' } )
23+
24+ if ( events . events . length === 0 ) {
25+ const dateTime = new Date ( Date . now ( ) + HOUR_MILLISECONDS ) . toISOString ( )
26+ await props . events . updateAiInsight . schedule ( { } , { dateTime } )
27+ }
2328 }
2429
2530 return undefined
@@ -30,53 +35,59 @@ plugin.on.afterOutgoingMessage('*', async (props) => {
3035 return
3136 }
3237 const { conversation } = await props . client . getConversation ( { id : props . data . message . conversationId } )
33- await _onNewMessage ( { ...props , conversation } )
38+ await onNewMessageHandler . onNewMessage ( { ...props , conversation } )
3439 return undefined
3540} )
3641
37- type OnNewMessageProps = CommonProps & {
38- conversation : bp . ClientOutputs [ 'getConversation' ] [ 'conversation' ]
39- }
40- const _onNewMessage = async (
41- props : OnNewMessageProps
42- ) : Promise < { message_count : number ; participant_count : number } > => {
43- const message_count = props . conversation . tags . message_count ? parseInt ( props . conversation . tags . message_count ) + 1 : 1
44-
45- const participant_count = await props . client
46- . listParticipants ( { id : props . conversation . id } )
47- . then ( ( { participants } ) => participants . length )
48-
49- const tags = {
50- message_count : message_count . toString ( ) ,
51- participant_count : participant_count . toString ( ) ,
52- }
53-
54- await props . client . updateConversation ( {
55- id : props . conversation . id ,
56- tags,
57- } )
58- return { message_count, participant_count }
59- }
60-
6142plugin . on . event ( 'updateAiInsight' , async ( props ) => {
6243 if ( isBrowser ) {
6344 props . logger . error ( 'This event is not supported by the browser' )
6445 return
6546 }
66- const firstMessagePage = await props . client
67- . listMessages ( { conversationId : props . event . conversationId } )
68- . then ( ( res ) => res . messages )
6947
70- if ( ! props . event . conversationId ) {
71- throw new sdk . RuntimeError ( `The conversationId cannot be null when calling the event '${ props . event . type } '` )
48+ const workflows = await props . client . listWorkflows ( {
49+ name : 'updateAllConversations' ,
50+ statuses : [ 'in_progress' , 'listening' , 'pending' ] ,
51+ } )
52+
53+ if ( workflows . workflows . length === 0 ) {
54+ props . workflows . updateAllConversations . startNewInstance ( { input : { } } )
7255 }
73- const conversation = await props . client . getConversation ( { id : props . event . conversationId } )
56+ } )
7457
75- await summaryUpdater . updateTitleAndSummary ( {
76- ...props ,
77- conversation : conversation . conversation ,
78- messages : firstMessagePage ,
79- } )
58+ plugin . on . workflowStart ( 'updateAllConversations' , async ( props ) => {
59+ props . logger . info ( 'Starting updateAllConversations workflow' )
60+ await _updateAllConversations ( props )
61+
62+ return undefined
63+ } )
64+
65+ plugin . on . workflowContinue ( 'updateAllConversations' , async ( props ) => {
66+ await _updateAllConversations ( props )
67+
68+ return undefined
69+ } )
70+
71+ plugin . on . workflowTimeout ( 'updateAllConversations' , async ( props ) => {
72+ props . logger . error ( 'Workflow timed out' )
8073} )
8174
75+ type WorkflowProps = types . CommonProps & bp . WorkflowHandlerProps [ 'updateAllConversations' ]
76+ const _updateAllConversations = async ( props : WorkflowProps ) => {
77+ const dirtyConversations = await props . client . listConversations ( { tags : { isDirty : 'true' } } )
78+
79+ const promises : Promise < void > [ ] = [ ]
80+ for ( const conversation of dirtyConversations . conversations ) {
81+ const firstMessagePage = await props . client
82+ . listMessages ( { conversationId : conversation . id } )
83+ . then ( ( res ) => res . messages )
84+ const promise = summaryUpdater . updateTitleAndSummary ( { ...props , conversation, messages : firstMessagePage } )
85+ promises . push ( promise )
86+ }
87+
88+ await Promise . all ( promises )
89+ await props . workflow . setCompleted ( )
90+ props . logger . info ( 'updateAllConversations workflow completed' )
91+ }
92+
8293export default plugin
0 commit comments