1
1
import { continueThread , registerChatPlugin } from "./openai-wrapper" ;
2
- import { Log } from "debug-level"
3
- import { mmClient , wsClient } from "./mm-client" ;
2
+ import { Log } from "debug-level"
3
+ import { mmClient , wsClient } from "./mm-client" ;
4
4
import 'babel-polyfill'
5
5
import 'isomorphic-fetch'
6
6
import { WebSocketMessage } from "@mattermost/client" ;
@@ -13,7 +13,7 @@ import {JSONMessageData, MessageData} from "./types";
13
13
import { ExitPlugin } from "./plugins/ExitPlugin" ;
14
14
import { MessageCollectPlugin } from "./plugins/MessageCollectPlugin" ;
15
15
16
- if ( ! global . FormData ) {
16
+ if ( ! global . FormData ) {
17
17
global . FormData = require ( 'form-data' )
18
18
}
19
19
@@ -36,15 +36,15 @@ const botInstructions = "Your name is " + name + " and you are a helpful assista
36
36
"meta data of his messages."
37
37
38
38
async function onClientMessage ( msg : WebSocketMessage < JSONMessageData > , meId : string , log : Log ) {
39
- if ( msg . event !== 'posted' || ! meId ) {
39
+ if ( msg . event !== 'posted' || ! meId ) {
40
40
log . debug ( { msg : msg } )
41
41
return
42
42
}
43
43
44
44
const msgData = parseMessageData ( msg . data )
45
- const posts = await getOlderPosts ( msgData . post , { lookBackTime : 1000 * 60 * 60 * 24 } )
45
+ const posts = await getOlderPosts ( msgData . post , { lookBackTime : 1000 * 60 * 60 * 24 } )
46
46
47
- if ( isMessageIgnored ( msgData , meId , posts ) ) {
47
+ if ( isMessageIgnored ( msgData , meId , posts ) ) {
48
48
return
49
49
}
50
50
@@ -56,7 +56,7 @@ async function onClientMessage(msg: WebSocketMessage<JSONMessageData>, meId: str
56
56
]
57
57
58
58
// create the context
59
- for ( const threadPost of posts . slice ( - contextMsgCount ) ) {
59
+ for ( const threadPost of posts . slice ( - contextMsgCount ) ) {
60
60
log . trace ( { msg : threadPost } )
61
61
if ( threadPost . user_id === meId ) {
62
62
chatmessages . push ( {
@@ -79,7 +79,7 @@ async function onClientMessage(msg: WebSocketMessage<JSONMessageData>, meId: str
79
79
80
80
try {
81
81
log . trace ( { chatmessages} )
82
- const { message, fileId, props } = await continueThread ( chatmessages , msgData )
82
+ const { message, fileId, props} = await continueThread ( chatmessages , msgData )
83
83
log . trace ( { message} )
84
84
85
85
// create answer response
@@ -91,7 +91,7 @@ async function onClientMessage(msg: WebSocketMessage<JSONMessageData>, meId: str
91
91
file_ids : fileId ? [ fileId ] : undefined
92
92
} )
93
93
log . trace ( { msg : newPost } )
94
- } catch ( e ) {
94
+ } catch ( e ) {
95
95
log . error ( e )
96
96
await mmClient . createPost ( {
97
97
message : "Sorry, but I encountered an internal error when trying to process your message" ,
@@ -114,16 +114,22 @@ async function onClientMessage(msg: WebSocketMessage<JSONMessageData>, meId: str
114
114
*/
115
115
function isMessageIgnored ( msgData : MessageData , meId : string , previousPosts : Post [ ] ) : boolean {
116
116
// we are not in a thread and not mentioned
117
- if ( msgData . post . root_id === '' && ! msgData . mentions . includes ( meId ) ) { return true }
117
+ if ( msgData . post . root_id === '' && ! msgData . mentions . includes ( meId ) ) {
118
+ return true
119
+ }
118
120
119
121
// it is our own message
120
- if ( msgData . post . user_id === meId ) { return true }
122
+ if ( msgData . post . user_id === meId ) {
123
+ return true
124
+ }
121
125
122
- for ( let i = previousPosts . length - 1 ; i > 0 ; i -- ) {
126
+ for ( let i = previousPosts . length - 1 ; i > 0 ; i -- ) {
123
127
// we were asked to stop participating in the conversation
124
- if ( previousPosts [ i ] . props . bot_status === 'stopped' ) { return true }
128
+ if ( previousPosts [ i ] . props . bot_status === 'stopped' ) {
129
+ return true
130
+ }
125
131
126
- if ( previousPosts [ i ] . user_id === meId || previousPosts [ i ] . message . includes ( name ) ) {
132
+ if ( previousPosts [ i ] . user_id === meId || previousPosts [ i ] . message . includes ( name ) ) {
127
133
// we are in a thread were we are actively participating, or we were mentioned in the thread => respond
128
134
return false
129
135
}
@@ -155,23 +161,24 @@ function parseMessageData(msg: JSONMessageData): MessageData {
155
161
* <li><b>postCount</b>: Determines how many of the previous posts should be collected. If this parameter is omitted all posts are returned.</li>
156
162
* </ul>
157
163
*/
158
- async function getOlderPosts ( refPost : Post , options : { lookBackTime ?: number , postCount ?: number } ) {
164
+ async function getOlderPosts ( refPost : Post , options : { lookBackTime ?: number , postCount ?: number } ) {
159
165
const thread = await mmClient . getPostThread ( refPost . id , true , false , true )
160
166
161
167
let posts : Post [ ] = [ ...new Set ( thread . order ) ] . map ( id => thread . posts [ id ] )
162
168
. sort ( ( a , b ) => a . create_at - b . create_at )
163
169
164
- if ( options . lookBackTime && options . lookBackTime > 0 ) {
170
+ if ( options . lookBackTime && options . lookBackTime > 0 ) {
165
171
posts = posts . filter ( a => a . create_at > refPost . create_at - options . lookBackTime ! )
166
172
}
167
- if ( options . postCount && options . postCount > 0 ) {
173
+ if ( options . postCount && options . postCount > 0 ) {
168
174
posts = posts . slice ( - options . postCount )
169
175
}
170
176
171
177
return posts
172
178
}
173
179
174
- const usernameCache : Record < string , { username : string , expireTime : number } > = { }
180
+ const usernameCache : Record < string , { username : string , expireTime : number } > = { }
181
+
175
182
/**
176
183
* Looks up the mattermost username for the given userId. Every username which is looked up will be cached for 5 minutes.
177
184
* @param userId
@@ -180,17 +187,17 @@ async function userIdToName(userId: string): Promise<string> {
180
187
let username : string
181
188
182
189
// check if userId is in cache and not outdated
183
- if ( usernameCache [ userId ] && Date . now ( ) < usernameCache [ userId ] . expireTime ) {
190
+ if ( usernameCache [ userId ] && Date . now ( ) < usernameCache [ userId ] . expireTime ) {
184
191
username = usernameCache [ userId ] . username
185
192
} else {
186
193
// username not in cache our outdated
187
194
username = ( await mmClient . getUser ( userId ) ) . username
188
195
189
- if ( ! / ^ [ a - z A - Z 0 - 9 _ - ] { 1 , 64 } $ / . test ( username ) ) {
190
- username = username . replace ( / [ . @ ! ? ] / g, '_' ) . slice ( 0 , 64 )
196
+ if ( ! / ^ [ a - z A - Z 0 - 9 _ - ] { 1 , 64 } $ / . test ( username ) ) {
197
+ username = username . replace ( / [ . @ ! ? ] / g, '_' ) . slice ( 0 , 64 )
191
198
}
192
199
193
- if ( ! / ^ [ a - z A - Z 0 - 9 _ - ] { 1 , 64 } $ / . test ( username ) ) {
200
+ if ( ! / ^ [ a - z A - Z 0 - 9 _ - ] { 1 , 64 } $ / . test ( username ) ) {
194
201
username = [ ...username . matchAll ( / [ a - z A - Z 0 - 9 _ - ] / g) ] . join ( '' ) . slice ( 0 , 64 )
195
202
}
196
203
@@ -203,20 +210,28 @@ async function userIdToName(userId: string): Promise<string> {
203
210
return username
204
211
}
205
212
213
+ Log . options ( { json : true , colors : true } )
214
+ Log . wrapConsole ( 'bot-ws' , { level4log : 'INFO' } )
215
+ const log = new Log ( 'bot' )
216
+
206
217
/* Entry point */
207
218
async function main ( ) : Promise < void > {
208
- Log . options ( { json : true , colors : true } )
209
- Log . wrapConsole ( 'bot-ws' , { level4log : 'INFO' } )
210
- const log = new Log ( 'bot' )
211
219
const meId = ( await mmClient . getMe ( ) ) . id
212
220
213
- for ( const plugin of plugins ) {
214
- if ( plugin . setup ( ) ) {
221
+ log . log ( "Connected to Mattermost." )
222
+
223
+ for ( const plugin of plugins ) {
224
+ if ( plugin . setup ( ) ) {
215
225
registerChatPlugin ( plugin )
226
+ log . trace ( "Registered plugin " + plugin . key )
216
227
}
217
228
}
218
229
219
230
wsClient . addMessageListener ( ( e ) => onClientMessage ( e , meId , log ) )
231
+ log . trace ( "Listening to MM messages..." )
220
232
}
221
233
222
- main ( ) . catch ( console . error )
234
+ main ( ) . catch ( reason => {
235
+ log . error ( reason ) ;
236
+ process . exit ( - 1 )
237
+ } )
0 commit comments