backend-manager 3.2.170 → 3.2.172
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.
- package/LICENSE +1 -1
- package/README.md +1 -1
- package/dist/cli/cli.js +1534 -0
- package/dist/manager/functions/core/actions/api/admin/backup.js +338 -0
- package/dist/manager/functions/core/actions/api/admin/create-post.js +388 -0
- package/dist/manager/functions/core/actions/api/admin/cron.js +37 -0
- package/dist/manager/functions/core/actions/api/admin/database-read.js +35 -0
- package/dist/manager/functions/core/actions/api/admin/database-write.js +39 -0
- package/dist/manager/functions/core/actions/api/admin/edit-post.js +158 -0
- package/dist/manager/functions/core/actions/api/admin/firestore-query.js +165 -0
- package/dist/manager/functions/core/actions/api/admin/firestore-read.js +38 -0
- package/dist/manager/functions/core/actions/api/admin/firestore-write.js +54 -0
- package/dist/manager/functions/core/actions/api/admin/get-stats.js +269 -0
- package/dist/manager/functions/core/actions/api/admin/payment-processor.js +57 -0
- package/dist/manager/functions/core/actions/api/admin/run-hook.js +95 -0
- package/dist/manager/functions/core/actions/api/admin/send-notification.js +197 -0
- package/dist/manager/functions/core/actions/api/admin/sync-users.js +125 -0
- package/dist/manager/functions/core/actions/api/admin/templates/post.html +16 -0
- package/dist/manager/functions/core/actions/api/firebase/get-providers.js +102 -0
- package/dist/manager/functions/core/actions/api/general/emails/general:download-app-link.js +21 -0
- package/dist/manager/functions/core/actions/api/general/fetch-post.js +99 -0
- package/dist/manager/functions/core/actions/api/general/generate-uuid.js +41 -0
- package/dist/manager/functions/core/actions/api/general/send-email.js +112 -0
- package/dist/manager/functions/core/actions/api/handler/create-post.js +146 -0
- package/dist/manager/functions/core/actions/api/special/setup-electron-manager-client.js +103 -0
- package/dist/manager/functions/core/actions/api/template.js +33 -0
- package/dist/manager/functions/core/actions/api/test/authenticate.js +22 -0
- package/dist/manager/functions/core/actions/api/test/create-test-accounts.js +27 -0
- package/dist/manager/functions/core/actions/api/test/lab.js +55 -0
- package/dist/manager/functions/core/actions/api/test/redirect.js +26 -0
- package/dist/manager/functions/core/actions/api/test/webhook.js +30 -0
- package/dist/manager/functions/core/actions/api/user/create-custom-token.js +32 -0
- package/dist/manager/functions/core/actions/api/user/delete.js +68 -0
- package/dist/manager/functions/core/actions/api/user/get-active-sessions.js +45 -0
- package/dist/manager/functions/core/actions/api/user/get-subscription-info.js +49 -0
- package/dist/manager/functions/core/actions/api/user/oauth2/discord.js +114 -0
- package/dist/manager/functions/core/actions/api/user/oauth2/google.js +99 -0
- package/dist/manager/functions/core/actions/api/user/oauth2.js +476 -0
- package/dist/manager/functions/core/actions/api/user/regenerate-api-keys.js +54 -0
- package/dist/manager/functions/core/actions/api/user/resolve.js +32 -0
- package/dist/manager/functions/core/actions/api/user/sign-out-all-sessions.js +118 -0
- package/dist/manager/functions/core/actions/api/user/sign-up copy.js +544 -0
- package/dist/manager/functions/core/actions/api/user/sign-up.js +99 -0
- package/dist/manager/functions/core/actions/api/user/submit-feedback.js +96 -0
- package/dist/manager/functions/core/actions/api/user/validate-settings.js +86 -0
- package/dist/manager/functions/core/actions/api.js +354 -0
- package/dist/manager/functions/core/actions/create-post-handler.js +184 -0
- package/dist/manager/functions/core/actions/generate-uuid.js +62 -0
- package/dist/manager/functions/core/actions/sign-up-handler.js +205 -0
- package/dist/manager/functions/core/admin/create-post.js +206 -0
- package/dist/manager/functions/core/admin/firestore-write.js +72 -0
- package/dist/manager/functions/core/admin/get-stats.js +218 -0
- package/dist/manager/functions/core/admin/query.js +198 -0
- package/dist/manager/functions/core/admin/send-notification.js +206 -0
- package/dist/manager/functions/core/cron/daily/ghostii-auto-publisher.js +377 -0
- package/dist/manager/functions/core/cron/daily/reset-usage.js +197 -0
- package/dist/manager/functions/core/cron/daily.js +114 -0
- package/dist/manager/functions/core/events/auth/before-create.js +124 -0
- package/dist/manager/functions/core/events/auth/before-signin.js +62 -0
- package/dist/manager/functions/core/events/auth/on-create copy.js +121 -0
- package/dist/manager/functions/core/events/auth/on-create.js +564 -0
- package/dist/manager/functions/core/events/auth/on-delete.js +72 -0
- package/dist/manager/functions/core/events/firestore/on-subscription.js +107 -0
- package/dist/manager/functions/test/authenticate.js +38 -0
- package/dist/manager/functions/test/create-test-accounts.js +144 -0
- package/dist/manager/functions/test/webhook.js +37 -0
- package/dist/manager/functions/wrappers/mailchimp/addToList.js +25 -0
- package/dist/manager/helpers/analytics copy.js +217 -0
- package/dist/manager/helpers/analytics.js +467 -0
- package/dist/manager/helpers/api-manager.js +324 -0
- package/dist/manager/helpers/assistant.js +1043 -0
- package/dist/manager/helpers/metadata.js +32 -0
- package/dist/manager/helpers/middleware.js +154 -0
- package/dist/manager/helpers/roles.js +69 -0
- package/dist/manager/helpers/settings.js +158 -0
- package/dist/manager/helpers/subscription-resolver-new.js +828 -0
- package/dist/manager/helpers/subscription-resolver.js +842 -0
- package/dist/manager/helpers/usage.js +381 -0
- package/dist/manager/helpers/user.js +198 -0
- package/dist/manager/helpers/utilities.js +292 -0
- package/dist/manager/index.js +1076 -0
- package/dist/manager/libraries/openai.js +460 -0
- package/dist/manager/routes/restart/index.js +52 -0
- package/dist/manager/routes/test/index.js +43 -0
- package/dist/manager/schemas/restart.js +13 -0
- package/dist/manager/schemas/test.js +13 -0
- package/dist/require.js +3 -0
- package/package.json +19 -9
|
@@ -0,0 +1,460 @@
|
|
|
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;
|
|
@@ -0,0 +1,52 @@
|
|
|
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;
|
|
@@ -0,0 +1,43 @@
|
|
|
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;
|
package/dist/require.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "backend-manager",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.172",
|
|
4
4
|
"description": "Quick tools for developing Firebase functions",
|
|
5
5
|
"main": "src/manager/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -13,7 +13,9 @@
|
|
|
13
13
|
"test:usage": "./node_modules/mocha/bin/mocha test/usage.js --timeout=10000",
|
|
14
14
|
"test:payment-resolver": "./node_modules/mocha/bin/mocha test/payment-resolver.js --timeout=10000",
|
|
15
15
|
"test:user": "./node_modules/mocha/bin/mocha test/user.js --timeout=10000",
|
|
16
|
-
"start": "node src/index.js"
|
|
16
|
+
"start": "node src/index.js",
|
|
17
|
+
"prepare": "node -e \"require('prepare-package')()\"",
|
|
18
|
+
"prepare:watch": "nodemon -w ./src -e '*' --exec 'npm run prepare'"
|
|
17
19
|
},
|
|
18
20
|
"engines": {
|
|
19
21
|
"node": "16"
|
|
@@ -33,6 +35,11 @@
|
|
|
33
35
|
"url": "https://github.com/itw-creative-works/backend-manager/issues"
|
|
34
36
|
},
|
|
35
37
|
"homepage": "https://itwcreativeworks.com",
|
|
38
|
+
"preparePackage": {
|
|
39
|
+
"input": "./src",
|
|
40
|
+
"output": "./dist",
|
|
41
|
+
"replace": {}
|
|
42
|
+
},
|
|
36
43
|
"dependencies": {
|
|
37
44
|
"@firebase/rules-unit-testing": "^2.0.7",
|
|
38
45
|
"@google-cloud/storage": "^7.11.2",
|
|
@@ -51,7 +58,7 @@
|
|
|
51
58
|
"glob": "^10.4.2",
|
|
52
59
|
"hcaptcha": "^0.1.1",
|
|
53
60
|
"inquirer": "^8.2.5",
|
|
54
|
-
"itwcw-package-analytics": "^1.0.
|
|
61
|
+
"itwcw-package-analytics": "^1.0.4",
|
|
55
62
|
"json5": "^2.2.3",
|
|
56
63
|
"jwt-decode": "^3.1.2",
|
|
57
64
|
"lodash": "^4.17.21",
|
|
@@ -62,19 +69,22 @@
|
|
|
62
69
|
"moment": "^2.30.1",
|
|
63
70
|
"nanoid": "^3.3.7",
|
|
64
71
|
"node-fetch": "^2.7.0",
|
|
65
|
-
"node-powertools": "^1.4.
|
|
72
|
+
"node-powertools": "^1.4.4",
|
|
66
73
|
"npm-api": "^1.0.1",
|
|
67
|
-
"paypal-server-api": "^2.0.
|
|
74
|
+
"paypal-server-api": "^2.0.14",
|
|
68
75
|
"pushid": "^1.0.0",
|
|
69
|
-
"resolve-account": "^1.0.
|
|
76
|
+
"resolve-account": "^1.0.25",
|
|
70
77
|
"semver": "^7.6.2",
|
|
71
78
|
"shortid": "^2.2.16",
|
|
72
|
-
"sizeitup": "^1.0.
|
|
79
|
+
"sizeitup": "^1.0.9",
|
|
73
80
|
"uid-generator": "^2.0.0",
|
|
74
81
|
"ultimate-jekyll-poster": "^1.0.2",
|
|
75
82
|
"uuid": "^9.0.1",
|
|
76
|
-
"wonderful-fetch": "^1.1.
|
|
77
|
-
"wonderful-log": "^1.0.
|
|
83
|
+
"wonderful-fetch": "^1.1.12",
|
|
84
|
+
"wonderful-log": "^1.0.7",
|
|
78
85
|
"yargs": "^17.7.2"
|
|
86
|
+
},
|
|
87
|
+
"devDependencies": {
|
|
88
|
+
"prepare-package": "^1.1.12"
|
|
79
89
|
}
|
|
80
90
|
}
|