backend-manager 3.2.172 → 3.2.173

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/package.json +3 -3
  2. package/src/cli/cli.js +27 -11
  3. package/dist/cli/cli.js +0 -1534
  4. package/dist/manager/functions/core/actions/api/admin/backup.js +0 -338
  5. package/dist/manager/functions/core/actions/api/admin/create-post.js +0 -388
  6. package/dist/manager/functions/core/actions/api/admin/cron.js +0 -37
  7. package/dist/manager/functions/core/actions/api/admin/database-read.js +0 -35
  8. package/dist/manager/functions/core/actions/api/admin/database-write.js +0 -39
  9. package/dist/manager/functions/core/actions/api/admin/edit-post.js +0 -158
  10. package/dist/manager/functions/core/actions/api/admin/firestore-query.js +0 -165
  11. package/dist/manager/functions/core/actions/api/admin/firestore-read.js +0 -38
  12. package/dist/manager/functions/core/actions/api/admin/firestore-write.js +0 -54
  13. package/dist/manager/functions/core/actions/api/admin/get-stats.js +0 -269
  14. package/dist/manager/functions/core/actions/api/admin/payment-processor.js +0 -57
  15. package/dist/manager/functions/core/actions/api/admin/run-hook.js +0 -95
  16. package/dist/manager/functions/core/actions/api/admin/send-notification.js +0 -197
  17. package/dist/manager/functions/core/actions/api/admin/sync-users.js +0 -125
  18. package/dist/manager/functions/core/actions/api/admin/templates/post.html +0 -16
  19. package/dist/manager/functions/core/actions/api/firebase/get-providers.js +0 -102
  20. package/dist/manager/functions/core/actions/api/general/emails/general:download-app-link.js +0 -21
  21. package/dist/manager/functions/core/actions/api/general/fetch-post.js +0 -99
  22. package/dist/manager/functions/core/actions/api/general/generate-uuid.js +0 -41
  23. package/dist/manager/functions/core/actions/api/general/send-email.js +0 -112
  24. package/dist/manager/functions/core/actions/api/handler/create-post.js +0 -146
  25. package/dist/manager/functions/core/actions/api/special/setup-electron-manager-client.js +0 -103
  26. package/dist/manager/functions/core/actions/api/template.js +0 -33
  27. package/dist/manager/functions/core/actions/api/test/authenticate.js +0 -22
  28. package/dist/manager/functions/core/actions/api/test/create-test-accounts.js +0 -27
  29. package/dist/manager/functions/core/actions/api/test/lab.js +0 -55
  30. package/dist/manager/functions/core/actions/api/test/redirect.js +0 -26
  31. package/dist/manager/functions/core/actions/api/test/webhook.js +0 -30
  32. package/dist/manager/functions/core/actions/api/user/create-custom-token.js +0 -32
  33. package/dist/manager/functions/core/actions/api/user/delete.js +0 -68
  34. package/dist/manager/functions/core/actions/api/user/get-active-sessions.js +0 -45
  35. package/dist/manager/functions/core/actions/api/user/get-subscription-info.js +0 -49
  36. package/dist/manager/functions/core/actions/api/user/oauth2/discord.js +0 -114
  37. package/dist/manager/functions/core/actions/api/user/oauth2/google.js +0 -99
  38. package/dist/manager/functions/core/actions/api/user/oauth2.js +0 -476
  39. package/dist/manager/functions/core/actions/api/user/regenerate-api-keys.js +0 -54
  40. package/dist/manager/functions/core/actions/api/user/resolve.js +0 -32
  41. package/dist/manager/functions/core/actions/api/user/sign-out-all-sessions.js +0 -118
  42. package/dist/manager/functions/core/actions/api/user/sign-up copy.js +0 -544
  43. package/dist/manager/functions/core/actions/api/user/sign-up.js +0 -99
  44. package/dist/manager/functions/core/actions/api/user/submit-feedback.js +0 -96
  45. package/dist/manager/functions/core/actions/api/user/validate-settings.js +0 -86
  46. package/dist/manager/functions/core/actions/api.js +0 -354
  47. package/dist/manager/functions/core/actions/create-post-handler.js +0 -184
  48. package/dist/manager/functions/core/actions/generate-uuid.js +0 -62
  49. package/dist/manager/functions/core/actions/sign-up-handler.js +0 -205
  50. package/dist/manager/functions/core/admin/create-post.js +0 -206
  51. package/dist/manager/functions/core/admin/firestore-write.js +0 -72
  52. package/dist/manager/functions/core/admin/get-stats.js +0 -218
  53. package/dist/manager/functions/core/admin/query.js +0 -198
  54. package/dist/manager/functions/core/admin/send-notification.js +0 -206
  55. package/dist/manager/functions/core/cron/daily/ghostii-auto-publisher.js +0 -377
  56. package/dist/manager/functions/core/cron/daily/reset-usage.js +0 -197
  57. package/dist/manager/functions/core/cron/daily.js +0 -114
  58. package/dist/manager/functions/core/events/auth/before-create.js +0 -124
  59. package/dist/manager/functions/core/events/auth/before-signin.js +0 -62
  60. package/dist/manager/functions/core/events/auth/on-create copy.js +0 -121
  61. package/dist/manager/functions/core/events/auth/on-create.js +0 -564
  62. package/dist/manager/functions/core/events/auth/on-delete.js +0 -72
  63. package/dist/manager/functions/core/events/firestore/on-subscription.js +0 -107
  64. package/dist/manager/functions/test/authenticate.js +0 -38
  65. package/dist/manager/functions/test/create-test-accounts.js +0 -144
  66. package/dist/manager/functions/test/webhook.js +0 -37
  67. package/dist/manager/functions/wrappers/mailchimp/addToList.js +0 -25
  68. package/dist/manager/helpers/analytics copy.js +0 -217
  69. package/dist/manager/helpers/analytics.js +0 -467
  70. package/dist/manager/helpers/api-manager.js +0 -324
  71. package/dist/manager/helpers/assistant.js +0 -1043
  72. package/dist/manager/helpers/metadata.js +0 -32
  73. package/dist/manager/helpers/middleware.js +0 -154
  74. package/dist/manager/helpers/roles.js +0 -69
  75. package/dist/manager/helpers/settings.js +0 -158
  76. package/dist/manager/helpers/subscription-resolver-new.js +0 -828
  77. package/dist/manager/helpers/subscription-resolver.js +0 -842
  78. package/dist/manager/helpers/usage.js +0 -381
  79. package/dist/manager/helpers/user.js +0 -198
  80. package/dist/manager/helpers/utilities.js +0 -292
  81. package/dist/manager/index.js +0 -1076
  82. package/dist/manager/libraries/openai.js +0 -460
  83. package/dist/manager/routes/restart/index.js +0 -52
  84. package/dist/manager/routes/test/index.js +0 -43
  85. package/dist/manager/schemas/restart.js +0 -13
  86. package/dist/manager/schemas/test.js +0 -13
  87. package/dist/require.js +0 -3
@@ -1,460 +0,0 @@
1
- const fetch = require('wonderful-fetch');
2
- const jetpack = require('fs-jetpack');
3
- const powertools = require('node-powertools');
4
- const _ = require('lodash');
5
- const JSON5 = require('json5');
6
-
7
- const TOKEN_COST_TABLE = {
8
- // May 13th, 2024
9
- 'gpt-4o': {
10
- input: 0.0050,
11
- output: 0.0150,
12
- },
13
- 'gpt-4-turbo': {
14
- input: 0.0100,
15
- output: 0.0300,
16
- },
17
- 'gpt-4-turbo-preview': {
18
- input: 0.0100,
19
- output: 0.0300,
20
- },
21
- 'gpt-4-vision-preview': {
22
- input: 0.0100,
23
- output: 0.0300,
24
- },
25
- 'gpt-4-1106-preview': {
26
- input: 0.0100,
27
- output: 0.0300,
28
- },
29
- 'gpt-4': {
30
- input: 0.0300,
31
- output: 0.0600,
32
- },
33
- 'gpt-3.5-turbo': {
34
- input: 0.0005,
35
- output: 0.0015,
36
- },
37
-
38
- // // Apr 9th, 2024
39
- // 'gpt-4-turbo': {
40
- // input: 0.0100,
41
- // output: 0.0300,
42
- // },
43
- // 'gpt-4-turbo-preview': {
44
- // input: 0.0100,
45
- // output: 0.0300,
46
- // },
47
- // 'gpt-4-vision-preview': {
48
- // input: 0.0100,
49
- // output: 0.0300,
50
- // },
51
- // 'gpt-4-1106-preview': {
52
- // input: 0.0100,
53
- // output: 0.0300,
54
- // },
55
- // 'gpt-4': {
56
- // input: 0.0300,
57
- // output: 0.0600,
58
- // },
59
- // 'gpt-3.5-turbo': {
60
- // input: 0.0005,
61
- // output: 0.0015,
62
- // },
63
-
64
- // Mar 6th, 2024
65
- // 'gpt-4-turbo-preview': {
66
- // input: 0.0100,
67
- // output: 0.0300,
68
- // },
69
- // 'gpt-4-vision-preview': {
70
- // input: 0.0100,
71
- // output: 0.0300,
72
- // },
73
- // 'gpt-4-1106-preview': {
74
- // input: 0.0100,
75
- // output: 0.0300,
76
- // },
77
- // 'gpt-4': {
78
- // input: 0.0300,
79
- // output: 0.0600,
80
- // },
81
- // 'gpt-3.5-turbo': {
82
- // input: 0.0005,
83
- // output: 0.0015,
84
- // },
85
-
86
- // Nov 6th, 2023
87
- // 'gpt-4-turbo-preview': {
88
- // input: 0.0100,
89
- // output: 0.0300,
90
- // },
91
- // 'gpt-4-1106-preview': {
92
- // input: 0.0100,
93
- // output: 0.0300,
94
- // },
95
- // 'gpt-4': {
96
- // input: 0.0300,
97
- // output: 0.0600,
98
- // },
99
- // 'gpt-3.5-turbo': {
100
- // input: 0.0010,
101
- // output: 0.0020,
102
- // },
103
- }
104
-
105
- const UNSUPPORTED_JSON = [
106
- /gpt-3.5/,
107
- /gpt-4-vision/,
108
- ];
109
-
110
-
111
- function OpenAI(assistant, key) {
112
- const self = this;
113
-
114
- self.assistant = assistant;
115
- self.Manager = assistant.Manager;
116
- self.user = assistant.user;
117
- self.key = key || self.Manager.config?.openai?.key;
118
-
119
- self.tokens = {
120
- total: {
121
- count: 0,
122
- price: 0,
123
- },
124
- input: {
125
- count: 0,
126
- price: 0,
127
- },
128
- output: {
129
- count: 0,
130
- price: 0,
131
- },
132
- }
133
-
134
- return self;
135
- }
136
-
137
- OpenAI.prototype.request = function (options) {
138
- const self = this;
139
- const Manager = self.Manager;
140
- const assistant = self.assistant;
141
-
142
- return new Promise(async function(resolve, reject) {
143
- options = _.merge({}, options);
144
-
145
- options.model = typeof options.model === 'undefined' ? 'gpt-3.5-turbo' : options.model;
146
- options.timeout = typeof options.timeout === 'undefined' ? 120000 : options.timeout;
147
- options.moderate = typeof options.moderate === 'undefined' ? true : options.moderate;
148
- options.log = typeof options.log === 'undefined' ? false : options.log;
149
- options.user = options.user || assistant.getUser();
150
-
151
- options.retries = typeof options.retries === 'undefined' ? 0 : options.retries;
152
- options.retryTriggers = typeof options.retryTriggers === 'undefined' ? ['network', 'parse'] : options.retryTriggers;
153
-
154
- options.prompt = options.prompt || {};
155
- options.prompt.path = options.prompt.path || '';
156
- options.prompt.text = options.prompt.text || options.prompt.content || '';
157
- options.prompt.settings = options.prompt.settings || {};
158
-
159
- options.message = options.message || {};
160
- options.message.path = options.message.path || '';
161
- options.message.text = options.message.text || options.message.content || '';
162
- options.message.settings = options.message.settings || {};
163
- options.message.images = options.message.images || [];
164
-
165
- options.history = options.history || {};
166
- options.history.messages = options.history.messages || [];
167
- options.history.limit = typeof options.history.limit === 'undefined' ? 5 : options.history.limit;
168
-
169
- options.response = typeof options.response === 'undefined' ? undefined : options.response;
170
- options.temperature = typeof options.temperature === 'undefined' ? 0.7 : options.temperature;
171
- options.maxTokens = typeof options.maxTokens === 'undefined' ? 512 : options.maxTokens;
172
-
173
- let attempt = 0;
174
-
175
- function _log() {
176
- if (!options.log) {
177
- return;
178
- }
179
-
180
- assistant.log('callOpenAI():', ...arguments);
181
- }
182
-
183
- function _load(input) {
184
- // console.log('*** input!!!', input.content.slice(0, 50), input.path);
185
- // console.log('*** input.content', input.content.slice(0, 50));
186
- // console.log('*** input.path', input.path);
187
-
188
- let text = '';
189
-
190
- // Load text
191
- if (input.path) {
192
- const exists = jetpack.exists(input.path);
193
-
194
- _log('Reading prompt from path:', input.path);
195
-
196
- if (!exists) {
197
- return new Error(`Path ${input.path} not found`);
198
- } else if (exists === 'dir') {
199
- return new Error(`Path ${input.path} is a directory`);
200
- }
201
-
202
- try {
203
- text = jetpack.read(input.path);
204
- } catch (e) {
205
- return new Error(`Error reading file ${input.path}: ${e}`);
206
- }
207
- } else {
208
- text = input.text;
209
- }
210
-
211
- return powertools.template(text, input.settings).trim();
212
- }
213
-
214
- // Log
215
- _log('Starting', options);
216
-
217
- // Determine response format
218
- let responseFormat = options.response === 'json' ? { type: 'json_object' } : undefined;
219
- if (UNSUPPORTED_JSON.some((model) => options.model.match(model))) {
220
- responseFormat = undefined;
221
- assistant.warn(`Model ${options.model} does not support JSON response format`);
222
- }
223
-
224
- _log('responseFormat', responseFormat);
225
-
226
- // Load prompt
227
- const prompt = _load(options.prompt);
228
- const message = _load(options.message);
229
- const user = options.user?.auth?.uid || assistant.request.geolocation.ip;
230
-
231
- // Log
232
- _log('Prompt', prompt);
233
- _log('Message', message);
234
- _log('User', user);
235
-
236
- // Check for errors
237
- if (prompt instanceof Error) {
238
- return reject(assistant.errorify(`Error loading prompt: ${prompt}`, {code: 400}));
239
- }
240
-
241
- if (message instanceof Error) {
242
- return reject(assistant.errorify(`Error loading message: ${message}`, {code: 400}));
243
- }
244
-
245
- // Request
246
- function _request(mode, options) {
247
- return new Promise(async function(resolve, reject) {
248
- let resultPath = '';
249
- const request = {
250
- url: '',
251
- method: 'post',
252
- response: 'json',
253
- // response: 'raw',
254
- // log: true,
255
- attachResponseHeaders: true,
256
- tries: 1,
257
- timeout: options.timeout,
258
- headers: {
259
- 'Authorization': `Bearer ${self.key}`,
260
- },
261
- body: {},
262
- }
263
-
264
- if (mode === 'chatgpt') {
265
- request.url = 'https://api.openai.com/v1/chat/completions';
266
-
267
- // Get history
268
- const history = options.history.messages.slice(-options.history.limit);
269
-
270
- // Add prompt to history
271
- history.unshift({
272
- role: 'system',
273
- text: prompt,
274
- images: [],
275
- });
276
-
277
- // Set last history item
278
- const lastHistory = history[history.length - 1];
279
-
280
- // If message is different than last message in history, add it
281
- if (lastHistory?.text !== message) {
282
- history.push({
283
- role: 'user',
284
- text: message,
285
- images: options.message.images,
286
- });
287
- }
288
-
289
- // Format history
290
- history.map((m) => {
291
- m.role = m.role || 'system';
292
-
293
- m.content = [];
294
-
295
- // Set content
296
- if (m.text) {
297
- m.content.push({
298
- type: 'text',
299
- text: m.text,
300
- })
301
- }
302
-
303
- // Set images
304
- m.images = m.images || [];
305
-
306
- // Loop through and add
307
- m.images.forEach((i) => {
308
- if (i.url) {
309
- m.content.push({
310
- type: 'image_url',
311
- image_url: {
312
- url: i.url,
313
- detail: i.detail || 'low',
314
- }
315
- });
316
- }
317
- }),
318
-
319
- // Delete text and images
320
- delete m.text;
321
- delete m.images;
322
- })
323
-
324
- // Log message
325
- history.forEach((m) => {
326
- _log('Message', m.role, m.content);
327
- });
328
-
329
- request.body = {
330
- model: options.model,
331
- response_format: responseFormat,
332
- messages: history,
333
- temperature: options.temperature,
334
- max_tokens: options.maxTokens,
335
- user: user,
336
- }
337
- resultPath = 'choices[0].message.content';
338
- } else if (mode === 'moderation') {
339
- request.url = 'https://api.openai.com/v1/moderations';
340
- request.body = {
341
- input: message,
342
- user: user,
343
- }
344
- resultPath = 'results[0]';
345
- }
346
-
347
- // Request
348
- await fetch(request.url, request)
349
- .then(async (r) => {
350
- // Set token counts
351
- self.tokens.input.count += r?.usage?.prompt_tokens || 0;
352
- self.tokens.output.count += r?.usage?.completion_tokens || 0;
353
- self.tokens.total.count = self.tokens.input.count + self.tokens.output.count;
354
-
355
- // Set token prices
356
- self.tokens.input.price = (self.tokens.input.count / 1000) * TOKEN_COST_TABLE[options.model].input;
357
- self.tokens.output.price = (self.tokens.output.count / 1000) * TOKEN_COST_TABLE[options.model].output;
358
- self.tokens.total.price = self.tokens.input.price + self.tokens.output.price;
359
-
360
- // Return
361
- return resolve(_.get(r, resultPath));
362
- })
363
- .catch((e) => {
364
- return reject(e);
365
- })
366
- });
367
- }
368
-
369
- // Moderate if needed
370
- let moderation = null;
371
- if (options.moderate) {
372
- moderation = await _request('moderation', options)
373
- .then(async (r) => {
374
- _log('Moderated', r);
375
-
376
- return r;
377
- })
378
- .catch((e) => e);
379
-
380
- // Check for moderation flag
381
- if (moderation.flagged) {
382
- return reject(assistant.errorify(`This request is inappropriate`, {code: 451}));
383
- }
384
- }
385
-
386
- function _attempt() {
387
- const retries = options.retries;
388
- const triggers = options.retryTriggers;
389
-
390
- // Increment attempt
391
- attempt++;
392
-
393
- // Log
394
- _log(`Request ${attempt}/${retries}`);
395
-
396
- // Request
397
- _request('chatgpt', options)
398
- .then((r) => {
399
- _log('Response', r);
400
- _log('Tokens', self.tokens);
401
-
402
- // Try to parse JSON response if needed
403
- try {
404
- const content = options.response === 'json' ? JSON5.parse(r) : r;
405
-
406
- // Return
407
- return resolve({
408
- content: content,
409
- tokens: self.tokens,
410
- moderation: moderation,
411
- })
412
- } catch (e) {
413
- assistant.error('Error parsing response', r, e);
414
-
415
- // Retry
416
- if (attempt < retries && triggers.includes('parse')) {
417
- return _attempt();
418
- }
419
-
420
- // Return
421
- return reject(e);
422
- }
423
- })
424
- .catch((e) => {
425
- const parsed = tryParse(e.message)?.error || {};
426
- const type = parsed?.type || '';
427
- const message = parsed?.message || e.message;
428
-
429
- // Log
430
- assistant.error(`Error requesting (type=${type}, message=${message})`, e);
431
-
432
- // Check for invalid request error
433
- if (type === 'invalid_request_error') {
434
- return reject(assistant.errorify(message, {code: 400}));
435
- }
436
-
437
- // Retry
438
- if (attempt < retries && triggers.includes('network')) {
439
- return _attempt();
440
- }
441
-
442
- // Return
443
- return reject(e);
444
- });
445
- }
446
-
447
- // Make attempt
448
- _attempt();
449
- });
450
- }
451
-
452
- function tryParse(content) {
453
- try {
454
- return JSON5.parse(content);
455
- } catch (e) {
456
- return content;
457
- }
458
- }
459
-
460
- module.exports = OpenAI;
@@ -1,52 +0,0 @@
1
- function Route() {
2
- const self = this;
3
-
4
- return self;
5
- }
6
-
7
- Route.prototype.main = async function (assistant) {
8
- const self = this;
9
-
10
- // Set shortcuts
11
- const Manager = assistant.Manager;
12
- const usage = assistant.usage;
13
- const user = assistant.usage.user;
14
- const analytics = assistant.analytics;
15
- const settings = assistant.settings;
16
-
17
- // Load preloaded libraries
18
- const jetpack = require('fs-jetpack');
19
-
20
- // Send analytics event
21
- analytics.event({
22
- name: 'restart',
23
- params: {},
24
- });
25
-
26
- // Check for user authentication
27
- // if (!user.roles.admin) {
28
- // return assistant.respond(`Admin required`, {code: 401});;
29
- // }
30
-
31
- // Log
32
- assistant.log('Restarting...');
33
-
34
- // Remove node_modules
35
- jetpack.remove('node_modules');
36
-
37
- // Perform delayed refresh to allow a successful response
38
- setTimeout(function () {
39
- require('child_process').exec('refresh', (error, stdout, stderr) => {
40
- // Quit the process if there is an error
41
- if (error || stderr) {
42
- console.log(`error: ${error ? error.message : stderr}`);
43
- return process.exit(1);
44
- }
45
- });
46
- }, 1000);
47
-
48
- // Return success
49
- assistant.respond({success: true});
50
- };
51
-
52
- module.exports = Route;
@@ -1,43 +0,0 @@
1
- function Route() {
2
- const self = this;
3
-
4
- return self;
5
- }
6
-
7
- Route.prototype.main = async function (assistant) {
8
- const self = this;
9
-
10
- // Set shortcuts
11
- const Manager = assistant.Manager;
12
- const usage = assistant.usage;
13
- const user = assistant.usage.user;
14
- const analytics = assistant.analytics;
15
- const settings = assistant.settings;
16
-
17
- // Load preloaded libraries
18
- // ..
19
-
20
- // Send analytics event
21
- analytics.event({
22
- name: 'test',
23
- params: {},
24
- });
25
-
26
- // Check for user authentication
27
- // if (!user.roles.admin) {
28
- // return assistant.respond(`Admin required`, {code: 401});;
29
- // }
30
-
31
- // Log
32
- assistant.log('Running test');
33
- assistant.log('assistant.request.body', assistant.request.body);
34
- assistant.log('assistant.request.query', assistant.request.query);
35
- assistant.log('assistant.request.headers', assistant.request.headers);
36
- assistant.log('assistant.request.data', assistant.request.data);
37
- assistant.log('assistant.settings', assistant.settings);
38
-
39
- // Return success
40
- assistant.respond({timestamp: new Date().toISOString(), id: assistant.id});
41
- };
42
-
43
- module.exports = Route;
@@ -1,13 +0,0 @@
1
- module.exports = function (assistant) {
2
- return {
3
- // DEFAULTS
4
- ['defaults']: {
5
- // Common
6
- delay: {
7
- types: ['number'],
8
- default: 1000,
9
- value: undefined,
10
- },
11
- },
12
- };
13
- }
@@ -1,13 +0,0 @@
1
- module.exports = function (assistant) {
2
- return {
3
- // DEFAULTS
4
- ['defaults']: {
5
- // Common
6
- delay: {
7
- types: ['number'],
8
- default: 1000,
9
- value: undefined,
10
- },
11
- },
12
- };
13
- }
package/dist/require.js DELETED
@@ -1,3 +0,0 @@
1
- module.exports = function (lib) {
2
- return require(lib)
3
- }