-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
127 lines (116 loc) · 3.64 KB
/
node_helper.js
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
"use strict"
var NodeHelper = require("node_helper")
var log = (...args) => { /* do nothing */ }
var mds = require('markdown-styles')
var path = require('path')
var fs = require("fs")
module.exports = NodeHelper.create({
start: function() {
this.config = null
this.tmpPath = path.resolve(__dirname, 'tmp')
this.Bard = null
this.Conversation = null
this.Ids = null
},
socketNotificationReceived: function (noti, payload) {
switch (noti) {
case "INIT":
console.log("[BARD] EXT-Bard Version:", require('./package.json').version, "rev:", require('./package.json').rev)
this.config = payload
if (this.config.debug) log = (...args) => { console.log("[BARD]", ...args) }
this.initialize()
break
case "QUERY":
if (!this.conversation) return
this.BardQuery(payload)
break
case "TB_QUERY":
if (!this.conversation) return
let query = payload.Query
let telegramBot = payload.TBkey
this.BardQuery(query, telegramBot)
break
}
},
initialize: async function() {
this.Bard = await this.loadBard()
try {
await this.Bard.init(this.config.COOKIE_KEY)
this.Ids = this.loadIds()
if (this.Ids) this.conversation = new this.Bard.Chat(this.Ids)
else this.conversation = new this.Bard.Chat()
this.sendSocketNotification("INITIALIZED")
console.log("[BARD] Initialized!")
} catch (e) {
console.error("[BARD]", e.message)
this.sendSocketNotification("Error", e.message)
}
},
BardQuery: async function(query, Telegram) {
log("[Query]", query)
if (!Telegram) this.sendSocketNotification("THINK", query)
let result = await this.conversation.ask(query)
log("[Result]", result)
if (!this.Ids) {
let exportIds = await this.conversation.export()
this.saveIds(exportIds)
}
if (Telegram) return this.sendSocketNotification("TB_RESULT", { Result: result, TBKey: Telegram })
this.createHTML(result)
},
loadBard: async function () {
const loaded = await import("bard-ai")
return loaded.default
},
createHTML: function(result) {
try {
fs.writeFileSync(this.tmpPath+ "/input/tmp.md", result)
log("[MD] MD file saved!")
} catch (e) {
console.error("[BARD] [MD]", e.message)
this.sendSocketNotification("ERROR", e.message)
return
}
/** generate HTML file from the MD source file **/
try {
mds.render(mds.resolveArgs({
input: this.tmpPath + '/input',
output: this.tmpPath + '/output',
layout: this.tmpPath + '/layout'
}), () => {
this.sendSocketNotification("REPLY", result)
log("[HTML] File Created!")
})
} catch (e) {
console.error("[BARD] [HTML]", e.message)
this.sendSocketNotification("ERROR", e.message)
}
},
loadIds() {
var result = null
try {
let Ids = fs.readFileSync(this.tmpPath + "/Chat/Ids.json")
result = JSON.parse(Ids)
console.log("[BARD] [Ids] Continue last Conversation")
} catch (e) {
if (e.code == "ENOENT") console.log("[BARD] [Ids] Create new Conversation")
else {
console.error("[BARD] [Ids] ", e.message)
this.sendSocketNotification("Error", e.message)
}
}
return result
},
saveIds(Ids) {
try {
let result = JSON.stringify(Ids)
fs.writeFileSync(this.tmpPath + "/Chat/Ids.json", result)
this.Ids = Ids
log("[BARD] [Ids] Conversation Ids Saved")
} catch (e) {
console.error("[BARD] [Ids]", e.message)
this.sendSocketNotification("Error", e.message)
this.Ids = null
}
}
})