backend-manager 3.2.12 → 3.2.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backend-manager",
3
- "version": "3.2.12",
3
+ "version": "3.2.13",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -389,39 +389,39 @@ Manager.prototype.init = function (exporter, options) {
389
389
  self.libraries.functions
390
390
  .runWith({memory: '256MB', timeoutSeconds: 60})
391
391
  .auth.user()
392
- .beforeCreate(async (user, context) => self._process((new (require(`${core}/events/auth/before-create.js`))(), {middleware: false}).init(self, { user: user, context: context})));
392
+ .beforeCreate(async (user, context) => self._process((new (require(`${core}/events/auth/before-create.js`))()).init(self, { user: user, context: context})));
393
393
 
394
394
  exporter.bm_authBeforeSignIn =
395
395
  self.libraries.functions
396
396
  .runWith({memory: '256MB', timeoutSeconds: 60})
397
397
  .auth.user()
398
- .beforeSignIn(async (user, context) => self._process((new (require(`${core}/events/auth/before-signin.js`))(), {middleware: false}).init(self, { user: user, context: context})));
398
+ .beforeSignIn(async (user, context) => self._process((new (require(`${core}/events/auth/before-signin.js`))()).init(self, { user: user, context: context})));
399
399
  }
400
400
 
401
401
  exporter.bm_authOnCreate =
402
402
  self.libraries.functions
403
403
  .runWith({memory: '256MB', timeoutSeconds: 60})
404
404
  .auth.user()
405
- .onCreate(async (user, context) => self._process((new (require(`${core}/events/auth/on-create.js`))(), {middleware: false}).init(self, { user: user, context: context})));
405
+ .onCreate(async (user, context) => self._process((new (require(`${core}/events/auth/on-create.js`))()).init(self, { user: user, context: context})));
406
406
 
407
407
  exporter.bm_authOnDelete =
408
408
  self.libraries.functions
409
409
  .runWith({memory: '256MB', timeoutSeconds: 60})
410
410
  .auth.user()
411
- .onDelete(async (user, context) => self._process((new (require(`${core}/events/auth/on-delete.js`))(), {middleware: false}).init(self, { user: user, context: context})));
411
+ .onDelete(async (user, context) => self._process((new (require(`${core}/events/auth/on-delete.js`))()).init(self, { user: user, context: context})));
412
412
 
413
413
  exporter.bm_subOnWrite =
414
414
  self.libraries.functions
415
415
  .runWith({memory: '256MB', timeoutSeconds: 60})
416
416
  .firestore.document('notifications/subscriptions/all/{token}')
417
- .onWrite(async (change, context) => self._process((new (require(`${core}/events/firestore/on-subscription.js`))(), {middleware: false}).init(self, { change: change, context: context, })));
417
+ .onWrite(async (change, context) => self._process((new (require(`${core}/events/firestore/on-subscription.js`))()).init(self, { change: change, context: context, })));
418
418
 
419
419
  // Cron
420
420
  exporter.bm_cronDaily =
421
421
  self.libraries.functions
422
422
  .runWith({ memory: '256MB', timeoutSeconds: 120 })
423
423
  .pubsub.schedule('every 24 hours')
424
- .onRun(async (context) => self._process((new (require(`${core}/cron/daily.js`))(), {middleware: false}).init(self, { context: context, })));
424
+ .onRun(async (context) => self._process((new (require(`${core}/cron/daily.js`))()).init(self, { context: context, })));
425
425
  }
426
426
 
427
427
  // Set dotenv
@@ -0,0 +1,130 @@
1
+ const fetch = require('wonderful-fetch');
2
+ const powertools = require('node-powertools');
3
+
4
+ function OpenAI(assistant, key) {
5
+ const self = this;
6
+
7
+ self.assistant = assistant;
8
+ self.Manager = assistant.Manager;
9
+ self.user = assistant.user;
10
+ self.key = key;
11
+
12
+ return self;
13
+ }
14
+
15
+ OpenAI.prototype.request = function (options, messages, template) {
16
+ const self = this;
17
+ const Manager = self.Manager;
18
+ const assistant = self.assistant;
19
+
20
+ assistant.log('callOpenAI(): Starting', self.key);
21
+ assistant.log('callOpenAI(): Starting', options, messages, template);
22
+
23
+ return new Promise(async function(resolve, reject) {
24
+ options = options || {};
25
+ options.prompt = options.prompt || '';
26
+ options.timeout = options.timeout || 120000;
27
+ options.model = options.model || (
28
+ self.user.plan.id === 'basic'
29
+ ? BASIC_MODEL
30
+ : PREMIUM_MODEL
31
+ );
32
+ options.user = options.user || assistant.request.geolocation.ip;
33
+
34
+ // Load prompt
35
+ options.prompt = powertools.template(
36
+ jetpack.read(`${__dirname}/prompts/${options.prompt}.md`),
37
+ template,
38
+ );
39
+
40
+ // assistant.log('callOpenAI():', options);
41
+ assistant.log('callOpenAI(): Starting', options);
42
+
43
+ function _request(mode, options) {
44
+ return new Promise(async function(resolve, reject) {
45
+ let resultPath = '';
46
+ const request = {
47
+ url: '',
48
+ method: 'post',
49
+ response: 'json',
50
+ // log: true,
51
+ tries: 1,
52
+ timeout: options.timeout,
53
+ headers: {
54
+ 'Authorization': `Bearer ${Manager.config.openai.key}`,
55
+ },
56
+ body: {},
57
+ }
58
+
59
+ if (mode === 'chatgpt') {
60
+ request.url = 'https://api.openai.com/v1/chat/completions';
61
+ messages = messages.slice(-MAX_MESSAGE_LENGTH);
62
+ messages.unshift({
63
+ role: 'system',
64
+ content: options.prompt,
65
+ });
66
+ request.body = {
67
+ model: options.model,
68
+ response_format: { type: 'json_object' },
69
+ messages: messages,
70
+ temperature: 0.7, // default: 0.7
71
+ max_tokens: 512, // default: infinity
72
+ user: options.user,
73
+ }
74
+ resultPath = 'choices[0].message.content';
75
+ } else if (mode === 'moderation') {
76
+ request.url = 'https://api.openai.com/v1/moderations';
77
+ request.body = {
78
+ input: messages.map(obj => obj.content).join('\n\nMessage:\n'),
79
+ user: options.user,
80
+ }
81
+ resultPath = 'results[0]';
82
+ }
83
+
84
+ // Request
85
+ await fetch(request.url, request)
86
+ .then((r) => {
87
+ // Set token counts
88
+ self.tokens.total.count += r?.usage?.total_tokens || 0;
89
+ self.tokens.input.count += r?.usage?.prompt_tokens || 0;
90
+ self.tokens.output.count += r?.usage?.completion_tokens || 0;
91
+
92
+ // Set token prices
93
+ self.tokens.total.price = (self.tokens.total.count / 1000) * TOKEN_COST_TABLE[options.model].input;
94
+ self.tokens.input.price = (self.tokens.input.count / 1000) * TOKEN_COST_TABLE[options.model].input;
95
+ self.tokens.output.price = (self.tokens.output.count / 1000) * TOKEN_COST_TABLE[options.model].output;
96
+
97
+ return resolve(_.get(r, resultPath));
98
+ })
99
+ .catch((e) => {
100
+ return reject(e);
101
+ })
102
+ });
103
+ }
104
+
105
+ await _request('moderation', options)
106
+ .then(async (r) => {
107
+ // assistant.log('callOpenAI(): moderation', r);
108
+ assistant.log('callOpenAI(): Moderated');
109
+
110
+ // If the moderation is not approved, return
111
+ if (r.flagged) {
112
+ return reject(new Error('Moderation not approved'));
113
+ }
114
+
115
+ await _request('chatgpt', options)
116
+ .then((r) => {
117
+ try {
118
+ return resolve(JSON.parse(r));
119
+ } catch (e) {
120
+ return reject(e);
121
+ }
122
+ })
123
+ .catch((e) => reject(e));
124
+ })
125
+ .catch((e) => reject(e));
126
+ });
127
+ }
128
+
129
+
130
+ module.exports = OpenAI;