@@ -16,129 +16,129 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
16
16
let cleanedMessage = clean ( message . content , clientId )
17
17
log ( `Message \"${ cleanedMessage } \" from ${ message . author . tag } in channel/thread ${ message . channelId } .` )
18
18
19
- // Do not respond if bot talks in the chat
20
- if ( message . author . username === message . client . user . username ) return
21
-
22
- // Only respond if message mentions the bot
23
- if ( ! message . mentions . has ( clientId ) ) return
24
-
25
- // default stream to false
26
- let shouldStream = false
27
-
28
- // Params for Preferences Fetching
29
- const maxRetries = 3
30
- const delay = 1000 // in millisecons
31
-
32
- try {
33
- // Retrieve Server/Guild Preferences
34
- let attempt = 0
35
- while ( attempt < maxRetries ) {
36
- try {
37
- await new Promise ( ( resolve , reject ) => {
38
- getServerConfig ( `${ message . guildId } -config.json` , ( config ) => {
39
- // check if config.json exists
40
- if ( config === undefined ) {
41
- // Allowing chat options to be available
42
- openConfig ( `${ message . guildId } -config.json` , 'toggle-chat' , true )
43
- reject ( new Error ( 'Failed to locate or create Server Preferences\n\nPlease try chatting again...' ) )
44
- }
45
-
46
- // check if chat is disabled
47
- else if ( ! config . options [ 'toggle-chat' ] )
48
- reject ( new Error ( 'Admin(s) have disabled chat features.\n\n Please contact your server\'s admin(s).' ) )
49
- else
50
- resolve ( config )
51
- } )
19
+ // Do not respond if bot talks in the chat
20
+ if ( message . author . username === message . client . user . username ) return
21
+
22
+ // Only respond if message mentions the bot
23
+ if ( ! message . mentions . has ( clientId ) ) return
24
+
25
+ // default stream to false
26
+ let shouldStream = false
27
+
28
+ // Params for Preferences Fetching
29
+ const maxRetries = 3
30
+ const delay = 1000 // in millisecons
31
+
32
+ try {
33
+ // Retrieve Server/Guild Preferences
34
+ let attempt = 0
35
+ while ( attempt < maxRetries ) {
36
+ try {
37
+ await new Promise ( ( resolve , reject ) => {
38
+ getServerConfig ( `${ message . guildId } -config.json` , ( config ) => {
39
+ // check if config.json exists
40
+ if ( config === undefined ) {
41
+ // Allowing chat options to be available
42
+ openConfig ( `${ message . guildId } -config.json` , 'toggle-chat' , true )
43
+ reject ( new Error ( 'Failed to locate or create Server Preferences\n\nPlease try chatting again...' ) )
44
+ }
45
+
46
+ // check if chat is disabled
47
+ else if ( ! config . options [ 'toggle-chat' ] )
48
+ reject ( new Error ( 'Admin(s) have disabled chat features.\n\n Please contact your server\'s admin(s).' ) )
49
+ else
50
+ resolve ( config )
52
51
} )
53
- break // successful
54
- } catch ( error ) {
55
- ++ attempt
56
- if ( attempt < maxRetries ) {
57
- log ( `Attempt ${ attempt } failed for Server Preferences. Retrying in ${ delay } ms...` )
58
- await new Promise ( ret => setTimeout ( ret , delay ) )
59
- } else
60
- throw new Error ( `Could not retrieve Server Preferences, please try chatting again...` )
61
- }
52
+ } )
53
+ break // successful
54
+ } catch ( error ) {
55
+ ++ attempt
56
+ if ( attempt < maxRetries ) {
57
+ log ( `Attempt ${ attempt } failed for Server Preferences. Retrying in ${ delay } ms...` )
58
+ await new Promise ( ret => setTimeout ( ret , delay ) )
59
+ } else
60
+ throw new Error ( `Could not retrieve Server Preferences, please try chatting again...` )
62
61
}
62
+ }
63
63
64
- // Reset attempts for User preferences
65
- attempt = 0
66
- let userConfig : UserConfig | undefined
67
-
68
- while ( attempt < maxRetries ) {
69
- try {
70
- // Retrieve User Preferences
71
- userConfig = await new Promise ( ( resolve , reject ) => {
72
- getUserConfig ( `${ message . author . username } -config.json` , ( config ) => {
73
- if ( config === undefined ) {
74
- openConfig ( `${ message . author . username } -config.json` , 'message-style' , false )
75
- openConfig ( `${ message . author . username } -config.json` , 'switch-model' , defaultModel )
76
- reject ( new Error ( 'No User Preferences is set up.\n\nCreating preferences file with \`message-style\` set as \`false\` for regular message style.\nPlease try chatting again.' ) )
77
- return
78
- }
79
-
80
- // check if there is a set capacity in config
81
- else if ( typeof config . options [ 'modify-capacity' ] !== 'number' )
82
- log ( `Capacity is undefined, using default capacity of ${ msgHist . capacity } .` )
83
- else if ( config . options [ 'modify-capacity' ] === msgHist . capacity )
84
- log ( `Capacity matches config as ${ msgHist . capacity } , no changes made.` )
85
- else {
86
- log ( `New Capacity found. Setting Context Capacity to ${ config . options [ 'modify-capacity' ] } .` )
87
- msgHist . capacity = config . options [ 'modify-capacity' ]
88
- }
89
-
90
- // set stream state
91
- shouldStream = config . options [ 'message-stream' ] as boolean || false
92
-
93
- if ( typeof config . options [ 'switch-model' ] !== 'string' )
94
- reject ( new Error ( `No Model was set. Please set a model by running \`/switch-model <model of choice>\`.\n\nIf you do not have any models. Run \`/pull-model <model name>\`.` ) )
64
+ // Reset attempts for User preferences
65
+ attempt = 0
66
+ let userConfig : UserConfig | undefined
67
+
68
+ while ( attempt < maxRetries ) {
69
+ try {
70
+ // Retrieve User Preferences
71
+ userConfig = await new Promise ( ( resolve , reject ) => {
72
+ getUserConfig ( `${ message . author . username } -config.json` , ( config ) => {
73
+ if ( config === undefined ) {
74
+ openConfig ( `${ message . author . username } -config.json` , 'message-style' , false )
75
+ openConfig ( `${ message . author . username } -config.json` , 'switch-model' , defaultModel )
76
+ reject ( new Error ( 'No User Preferences is set up.\n\nCreating preferences file with \`message-style\` set as \`false\` for regular message style.\nPlease try chatting again.' ) )
77
+ return
78
+ }
95
79
96
- resolve ( config )
97
- } )
80
+ // check if there is a set capacity in config
81
+ else if ( typeof config . options [ 'modify-capacity' ] !== 'number' )
82
+ log ( `Capacity is undefined, using default capacity of ${ msgHist . capacity } .` )
83
+ else if ( config . options [ 'modify-capacity' ] === msgHist . capacity )
84
+ log ( `Capacity matches config as ${ msgHist . capacity } , no changes made.` )
85
+ else {
86
+ log ( `New Capacity found. Setting Context Capacity to ${ config . options [ 'modify-capacity' ] } .` )
87
+ msgHist . capacity = config . options [ 'modify-capacity' ]
88
+ }
89
+
90
+ // set stream state
91
+ shouldStream = config . options [ 'message-stream' ] as boolean || false
92
+
93
+ if ( typeof config . options [ 'switch-model' ] !== 'string' )
94
+ reject ( new Error ( `No Model was set. Please set a model by running \`/switch-model <model of choice>\`.\n\nIf you do not have any models. Run \`/pull-model <model name>\`.` ) )
95
+
96
+ resolve ( config )
98
97
} )
99
- break // successful
100
- } catch ( error ) {
101
- ++ attempt
102
- if ( attempt < maxRetries ) {
103
- log ( `Attempt ${ attempt } failed for User Preferences. Retrying in ${ delay } ms...` )
104
- await new Promise ( ret => setTimeout ( ret , delay ) )
105
- } else
106
- throw new Error ( `Could not retrieve User Preferences, please try chatting again...` )
107
- }
98
+ } )
99
+ break // successful
100
+ } catch ( error ) {
101
+ ++ attempt
102
+ if ( attempt < maxRetries ) {
103
+ log ( `Attempt ${ attempt } failed for User Preferences. Retrying in ${ delay } ms...` )
104
+ await new Promise ( ret => setTimeout ( ret , delay ) )
105
+ } else
106
+ throw new Error ( `Could not retrieve User Preferences, please try chatting again...` )
108
107
}
108
+ }
109
109
110
- // need new check for "open/active" threads/channels here!
111
- let chatMessages : UserMessage [ ] = await new Promise ( ( resolve ) => {
112
- // set new queue to modify
110
+ // need new check for "open/active" threads/channels here!
111
+ let chatMessages : UserMessage [ ] = await new Promise ( ( resolve ) => {
112
+ // set new queue to modify
113
+ getChannelInfo ( `${ message . channelId } -${ message . author . username } .json` , ( channelInfo ) => {
114
+ if ( channelInfo ?. messages )
115
+ resolve ( channelInfo . messages )
116
+ else {
117
+ log ( `Channel/Thread ${ message . channel } -${ message . author . username } does not exist. File will be created shortly...` )
118
+ resolve ( [ ] )
119
+ }
120
+ } )
121
+ } )
122
+
123
+ if ( chatMessages . length === 0 ) {
124
+ chatMessages = await new Promise ( ( resolve , reject ) => {
125
+ openChannelInfo ( message . channelId ,
126
+ message . channel as TextChannel ,
127
+ message . author . tag
128
+ )
113
129
getChannelInfo ( `${ message . channelId } -${ message . author . username } .json` , ( channelInfo ) => {
114
130
if ( channelInfo ?. messages )
115
131
resolve ( channelInfo . messages )
116
132
else {
117
133
log ( `Channel/Thread ${ message . channel } -${ message . author . username } does not exist. File will be created shortly...` )
118
- resolve ( [ ] )
134
+ reject ( new Error ( `Failed to find ${ message . author . username } 's history. Try chatting again.` ) )
119
135
}
120
136
} )
121
137
} )
138
+ }
122
139
123
- if ( chatMessages . length === 0 ) {
124
- chatMessages = await new Promise ( ( resolve , reject ) => {
125
- openChannelInfo ( message . channelId ,
126
- message . channel as TextChannel ,
127
- message . author . tag
128
- )
129
- getChannelInfo ( `${ message . channelId } -${ message . author . username } .json` , ( channelInfo ) => {
130
- if ( channelInfo ?. messages )
131
- resolve ( channelInfo . messages )
132
- else {
133
- log ( `Channel/Thread ${ message . channel } -${ message . author . username } does not exist. File will be created shortly...` )
134
- reject ( new Error ( `Failed to find ${ message . author . username } 's history. Try chatting again.` ) )
135
- }
136
- } )
137
- } )
138
- }
139
-
140
- if ( ! userConfig )
141
- throw new Error ( `Failed to initialize User Preference for **${ message . author . username } **.\n\nIt's likely you do not have a model set. Please use the \`switch-model\` command to do that.` )
140
+ if ( ! userConfig )
141
+ throw new Error ( `Failed to initialize User Preference for **${ message . author . username } **.\n\nIt's likely you do not have a model set. Please use the \`switch-model\` command to do that.` )
142
142
143
143
// get message attachment if exists
144
144
const attachment = message . attachments . first ( )
@@ -148,47 +148,46 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
148
148
cleanedMessage += await getTextFileAttachmentData ( attachment )
149
149
else if ( attachment )
150
150
messageAttachment = await getAttachmentData ( attachment )
151
-
152
- const model : string = userConfig . options [ 'switch-model' ]
153
-
154
- // set up new queue
155
- msgHist . setQueue ( chatMessages )
156
151
157
- // check if we can push, if not, remove oldest
158
- while ( msgHist . size ( ) >= msgHist . capacity ) msgHist . dequeue ( )
159
-
160
- // push user response before ollama query
161
- msgHist . enqueue ( {
162
- role : 'user' ,
163
- content : cleanedMessage ,
164
- images : messageAttachment || [ ]
165
- } )
166
-
167
- // response string for ollama to put its response
168
- const response : string = await normalMessage ( message , ollama , model , msgHist , shouldStream )
169
-
170
- // If something bad happened, remove user query and stop
171
- if ( response == undefined ) { msgHist . pop ( ) ; return }
172
-
173
- // if queue is full, remove the oldest message
174
- while ( msgHist . size ( ) >= msgHist . capacity ) msgHist . dequeue ( )
175
-
176
- // successful query, save it in context history
177
- msgHist . enqueue ( {
178
- role : 'assistant' ,
179
- content : response ,
180
- images : messageAttachment || [ ]
181
- } )
152
+ const model : string = userConfig . options [ 'switch-model' ]
182
153
183
- // only update the json on success
184
- openChannelInfo ( message . channelId ,
185
- message . channel as TextChannel ,
186
- message . author . tag ,
187
- msgHist . getItems ( )
188
- )
189
- } catch ( error : any ) {
190
- msgHist . pop ( ) // remove message because of failure
191
- message . reply ( `**Error Occurred:**\n\n**Reason:** *${ error . message } *` )
192
- }
154
+ // set up new queue
155
+ msgHist . setQueue ( chatMessages )
156
+
157
+ // check if we can push, if not, remove oldest
158
+ while ( msgHist . size ( ) >= msgHist . capacity ) msgHist . dequeue ( )
159
+
160
+ // push user response before ollama query
161
+ msgHist . enqueue ( {
162
+ role : 'user' ,
163
+ content : cleanedMessage ,
164
+ images : messageAttachment || [ ]
165
+ } )
166
+
167
+ // response string for ollama to put its response
168
+ const response : string = await normalMessage ( message , ollama , model , msgHist , shouldStream )
169
+
170
+ // If something bad happened, remove user query and stop
171
+ if ( response == undefined ) { msgHist . pop ( ) ; return }
172
+
173
+ // if queue is full, remove the oldest message
174
+ while ( msgHist . size ( ) >= msgHist . capacity ) msgHist . dequeue ( )
175
+
176
+ // successful query, save it in context history
177
+ msgHist . enqueue ( {
178
+ role : 'assistant' ,
179
+ content : response ,
180
+ images : messageAttachment || [ ]
181
+ } )
182
+
183
+ // only update the json on success
184
+ openChannelInfo ( message . channelId ,
185
+ message . channel as TextChannel ,
186
+ message . author . tag ,
187
+ msgHist . getItems ( )
188
+ )
189
+ } catch ( error : any ) {
190
+ msgHist . pop ( ) // remove message because of failure
191
+ message . reply ( `**Error Occurred:**\n\n**Reason:** *${ error . message } *` )
193
192
}
194
- )
193
+ } )
0 commit comments