-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
64 lines (59 loc) · 1.82 KB
/
node_helper.js
File metadata and controls
64 lines (59 loc) · 1.82 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
require('dotenv').config()
const exec = require('child_process').exec
const NodeHelper = require('node_helper')
const { Configuration, OpenAIApi } = require("openai")
const API_KEY = process.env.OPENAI_API_KEY
const ORGANIZATION = process.env.OPENAI_ORGANIZATION
const configuration = new Configuration({
organization: ORGANIZATION,
apiKey: API_KEY,
})
const openai = new OpenAIApi(configuration)
module.exports = NodeHelper.create({
start: function () {
},
socketNotificationReceived: function (notification, payload) {
if (notification === 'REQUEST') {
this.job(payload).catch((error) => {
console.log(error)
})
}
if (notification == "SHELL_EXEC") {
console.log("[OPENAI] shellExec trying:", payload)
exec(payload, (error, stdout, stderr)=>{
if (error) {
console.log("[OPENAI] shellExec error:\n ------ \n", error, "\n ----- \n")
}
if (stderr) console.log("[OPENAI] shellExec stdErr:\n ------ \n", stderr, "\n ----- \n")
console.log("[OPENAI] shellExec stdOut:\n ------ \n", stdout, "\n ----- \n")
})
}
},
job: async function (payload) {
const jobMap = {
'IMAGE': 'createImage',
'TEXT': 'createCompletion',
'CHAT': 'createChatCompletion'
}
let e = null
let {request, options} = payload
let response = null
try {
response = await openai[jobMap[options.method]](request)
} catch (error) {
console.log('[OPENAI] ERROR')
console.log("E_DATA", error.response.data)
e = error?.response?.data ?? error
} finally {
let r = {
error: e,
response: response?.data ?? e,
request: request,
options: options,
responseTimestamp: Date.now(),
stealth: options.stealth,
}
this.sendSocketNotification('RESPONSE', r)
}
}
})