This repository was archived by the owner on Aug 7, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
289 lines (274 loc) · 10.3 KB
/
Copy pathutils.js
File metadata and controls
289 lines (274 loc) · 10.3 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
const fs = require('fs');
const Discord = require('discord.js');
const gptEncoder = require('gpt-3-encoder');
const OpenAI = require('openai');
const crypto = require('crypto');
const config = require('./config.json');
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
const md5sum = str => crypto.createHash('md5').update(str).digest('hex');
// Count the number of tokens in a string
const countStringTokens = str => {
return gptEncoder.encode(str).length;
};
const adjustImageDimensions = (width, height) => {
const maxLongSide = config.gpt.vision.resize.long_side;
const maxShortSide = config.gpt.vision.resize.short_side;
// Determine the longer and shorter dimensions
let longSide = Math.max(width, height);
let shortSide = Math.min(width, height);
// Check if resizing is needed
if (longSide <= maxLongSide && shortSide <= maxShortSide) {
return { width, height }; // No resize needed
}
// Calculate aspect ratio
const aspectRatio = longSide / shortSide;
// Rescale
if (longSide > maxLongSide) {
longSide = maxLongSide;
shortSide = longSide / aspectRatio;
}
if (shortSide > maxShortSide) {
shortSide = maxShortSide;
longSide = shortSide * aspectRatio;
}
// Return the resized dimensions, keeping width and height identifiers
return width > height
? { width: longSide, height: shortSide }
: { width: shortSide, height: longSide };
};
const countImageTokens = (width, height) => {
const dims = adjustImageDimensions(width, height);
let tokenCount = config.gpt.vision.tokens_base;
const widthTiles = Math.ceil(dims.width / config.gpt.vision.tile_size);
const heightTiles = Math.ceil(dims.height / config.gpt.vision.tile_size);
tokenCount += widthTiles * heightTiles * config.gpt.vision.tokens_per_tile;
return tokenCount;
};
const imageDimensions = [];
const countTokensInContentEntry = contentEntry => {
if (typeof contentEntry == 'string') {
return countStringTokens(contentEntry);
}
if (contentEntry.type == 'text') {
return countStringTokens(contentEntry.text);
} else if (contentEntry.type == 'image_url') {
const dims = imageDimensions[md5sum(contentEntry.image_url.url)];
return countImageTokens(dims.width, dims.height);
}
return 0;
};
const replaceConfigPlaceholders = (text) => {
for (const placeholder in config.gpt.replacements) {
const replacement = config.gpt.replacements[placeholder];
text = text.replace(new RegExp(placeholder.replace('\\', '\\\\'), 'g'), replacement);
}
return text;
};
// Determines if a received message is a valid AI prompt
const isValidInputMsg = msg => {
const bot = require('./bot');
// If the message isn't a normal text message, ignore it
if (msg.type != Discord.MessageType.Default && msg.type != Discord.MessageType.Reply) return false;
// If the message is from a bot, ignore it
if (msg.author.bot) return false;
// If the message doesn't mention the bot and this isn't in a DM, ignore it
if (msg.guild && !msg.mentions.has(bot.user.id)) return false;
// If vision is enabled
if (config.gpt.vision.enabled) {
// If the message has no content and no attachments, ignore it
if (!msg.attachments.size && !msg.content) return false;
} else {
// If the message has no content, ignore it
if (!msg.content) return false;
}
return true;
};
// Determines if a message is valid for context use
const isValidContextMsg = msg => {
// If the message isn't a normal text message, ignore it
if (msg.type != Discord.MessageType.Default && msg.type != Discord.MessageType.Reply) return false;
// If vision is enabled
if (config.gpt.vision.enabled) {
// If the message has no content and no attachments, ignore it
if (!msg.attachments.size && !msg.content) return false;
} else {
// If the message has no content, ignore it
if (!msg.content) return false;
}
// If bots are configured to be ignored and it's a bot
// that isn't outs, ignore it
if (config.gpt.ignore_bots && msg.author.bot && msg.author.id != msg.client.user.id)
return false;
// If this message is from the bot and it's an interaction,
// but not contextbarrier, ignore it
if (msg.author.id == msg.client.user.id && msg.interaction && msg.interaction.commandName != 'contextbarrier')
return false;
return true;
};
// Returns 1 for access granted, 0 for denied, and -1 for blocked
const getUserAccessStatus = id => {
// Get access data
const data = require('./access.json');
let userStatus = data.users[id] || 0;
data.users[id] = userStatus;
if (id == config.bot.owner_id) data.users[id] = 1;
// Write access data
fs.writeFileSync('./access.json', JSON.stringify(data, null, 4));
// Disallow blocked users
if (userStatus == -1) return -1;
// If the bot is private, disallow all users not explicitly allowed
if (!data.public_usage) {
if (userStatus != 1) return 0;
}
return 1;
};
// Get the nickname/name/username of the author of a message
const getMsgAuthorName = msg => {
return msg.member?.nickname || msg.author.globalName || msg.author.username;
};
// Get the nickname/name/username of a user
const getUserName = (id, guild = null) => {
const bot = require('./bot');
const member = guild?.members.cache.get(id);
const user = bot.users.cache.get(id);
return member?.nickname || user?.globalName || user?.username || id;
};
// Get message content with user mentions replaced with their names
const getSanitizedContent = msg => {
if (!msg.content) return '';
return msg.content.replace(/<@!?(\d+)>/g, (match, id) => {
return getUserName(id, msg.guild) || match;
});
};
const streamChatCompletionOpts = {
messages: [{
role: 'system',
content: ''
}, {
role: 'assistant',
content: [{
type: 'text', text: ''
}]
}, {
role: 'user',
content: [{
type: 'image_url',
image_url: {
url: '', detail: 'auto'
}
}]
}],
onChunk: (chunk = '') => { },
onFinish: (response = '') => { return response; },
onError: (error) => { throw new Error(error); },
model: config.gpt.model
};
const streamChatCompletion = async (opts = streamChatCompletionOpts) => {
// Merge opts
opts = Object.assign({}, streamChatCompletionOpts, opts);
try {
// Initialize OpenAI
let initOpts;
switch (config.gpt.provider) {
case 'openai': {
initOpts = {
apiKey: config.credentials.openai_secret
};
break;
}
case 'google': {
initOpts = {
baseURL: 'https://generativelanguage.googleapis.com/v1beta/openai/',
apiKey: config.credentials.google_secret
};
break;
}
case 'deepseek': {
initOpts = {
baseURL: 'https://api.deepseek.com',
apiKey: config.credentials.deepseek_secret
};
const messages = [];
for (const message of opts.messages) {
const role = message.role;
// Ensure message content is always a string
let content = '';
if (typeof message.content == 'string') {
content = message.content;
} else {
for (const contentEntry of message.content) {
if (contentEntry.type == 'text') {
content += contentEntry.text + '\n';
} else if (contentEntry.type == 'image_url') {
content += contentEntry.image_url.url + '\n';
}
}
}
content = content.trim();
// Ensure roles aren't repeated successively
if (messages[messages.length - 1]?.role == role) {
messages[messages.length - 1].content += '\n' + content;
// Ensure system messages don't appear after non-system messages
} else if (messages[messages.length - 1]?.role != 'system' && role == 'system') {
continue;
} else {
messages.push({ role, content });
}
}
opts.messages = messages;
break;
}
default: {
throw new Error(`"${config.gpt.provider}" isn't a valid provider. Please update your configuration.`);
}
}
const openai = new OpenAI(initOpts);
// Get response stream
const res = await openai.chat.completions.create({
model: opts.model,
messages: opts.messages,
temperature: config.gpt.temperature,
stream: config.gpt.should_stream
});
let response = '';
if (config.gpt.should_stream) {
// Handle response chunks as they come in
for await (const chunkData of res) {
const chunk = chunkData.choices[0].delta.content || '';
response += chunk;
opts.onChunk(chunk);
}
} else {
// Handle response
response = res.choices[0].message.content;
const split = response.split(' ');
const last = split.pop();
for (const token of split) {
opts.onChunk(token + ' ');
}
opts.onChunk(last);
}
// Handle completion
return opts.onFinish(response);
} catch (error) {
opts.onError(error);
return null;
}
};
module.exports = {
sleep,
md5sum,
countStringTokens,
adjustImageDimensions,
imageDimensions,
countImageTokens,
countTokensInContentEntry,
replaceConfigPlaceholders,
isValidInputMsg,
isValidContextMsg,
getUserAccessStatus,
getMsgAuthorName,
getUserName,
getSanitizedContent,
streamChatCompletion
};