-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathxiaoai-mibrain.js
198 lines (179 loc) · 6.69 KB
/
xiaoai-mibrain.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const Xiaoai = require('./lib/XiaoAi')
const MessageProcess = require('./lib/MessageProcess')
const XiaoAiError = require('./xiaoai/XiaoAiError')
module.exports = RED => {
// nlp-result
RED.nodes.registerType('xiaoai-nlp-result', class {
constructor (config) {
const node = this
RED.nodes.createNode(node, config)
const xiaomiConfig = RED.nodes.getNode(config.xiaoai)
const xiaoai = new Xiaoai(node, xiaomiConfig)
node.on('input', async data => {
for (const key in config) { if (config[key] != '' && config[key] != null) { data[key] = config[key] } }
data.payload = data.device || data.payload
try {
const res = await xiaoai.nlpResule(data.device)
data.res = res
node.status({ text: `获取nlp结果成功:${data._msgid}` })
node.send([data, null])
} catch (err) {
if (err instanceof XiaoAiError) {
node.status({ text: '用户名密码错误,请xiaomi网站登陆尝试', fill: 'red', shape: 'ring' })
} else {
if (err.status && err.status == 401) {
xiaoai.clean()
node.status({ text: '授权信息失效, 请再试一次', fill: 'red', shape: 'ring' })
} else {
node.status({ text: err.message, fill: 'red', shape: 'ring' })
}
}
node.warn(err)
data.payload = {}
data.error_msg = err.message
node.send([null, data])
}
})
}
})
RED.nodes.registerType('xiaoai-nlp-conversation', class {
constructor (config) {
const node = this
RED.nodes.createNode(node, config)
const xiaomiConfig = RED.nodes.getNode(config.xiaoai)
const xiaoai = new Xiaoai(node, xiaomiConfig)
node.on('input', async data => {
for (const key in config) { if (config[key] != '' && config[key] != null) { data[key] = config[key] } }
try {
const res = await xiaoai.conversation(data.limit, data.device)
data.res = res
try{
const r = JSON.parse(res.data)
let record = r?.records[0] || {}
const query = record.query
const answers = record.answers || []
const answer = answers.find((answer) => answer.type === 'TTS') || {}
const tts = answer.tts || {}
data.payload = query
data.answers = answers
data.answer = answer
answer.text = tts.text
answer.time = record.time
}catch(e) {
console.log(e)
}
node.status({ text: `获取对话记录成功:${data._msgid}` })
node.send([data, null])
} catch (err) {
if (err instanceof XiaoAiError) {
node.status({ text: '用户名密码错误,请xiaomi网站登陆尝试', fill: 'red', shape: 'ring' })
} else {
if (err.status && err.status == 401) {
xiaoai.clean()
node.status({ text: '授权信息失效, 请再试一次', fill: 'red', shape: 'ring' })
} else {
node.status({ text: err.message, fill: 'red', shape: 'ring' })
}
}
console.log(err)
node.warn(err)
data.payload = {}
data.error_msg = err.message
node.send([null, data])
}
})
}
})
// ai service
RED.nodes.registerType('xiaoai-ai-service', class {
constructor (config) {
const node = this
RED.nodes.createNode(node, config)
const xiaomiConfig = RED.nodes.getNode(config.xiaoai)
const messageProcess = new MessageProcess(node, xiaomiConfig)
messageProcess.changeType()
node.on('input', data => {
for (const key in config) { if (config[key] != '' && config[key] != null) { data[key] = config[key] } }
data.payload = data.message || data.payload
// 修正数据格式
if (data.tts) {
data.tts = 1
} else {
data.tts = 0
}
if (data.tts_play) {
data.tts_play = 1
} else {
data.tts_play = 0
}
messageProcess.say(data)
})
}
})
RED.nodes.registerType('xiaoai-nlp-session', class {
constructor (config) {
const node = this
RED.nodes.createNode(node, config)
const xiaomiConfig = RED.nodes.getNode(config.xiaoai)
const xiaoai = new Xiaoai(node, xiaomiConfig)
node.on('input', async data => {
for (const key in config) { if (config[key] != '' && config[key] != null) { data[key] = config[key] } }
try {
const res = await xiaoai.getSession()
data.payload = res
node.status({ text: `获取登录信息成功:${data._msgid}` })
node.send([data, null])
} catch (err) {
if (err instanceof XiaoAiError) {
node.status({ text: '用户名密码错误,请xiaomi网站登陆尝试', fill: 'red', shape: 'ring' })
} else {
if (err.status && err.status == 401) {
xiaoai.clean()
node.status({ text: '授权信息失效, 请再试一次', fill: 'red', shape: 'ring' })
} else {
node.status({ text: err.message, fill: 'red', shape: 'ring' })
}
}
console.log(err)
node.warn(err)
data.payload = {}
data.error_msg = err.message
node.send([null, data])
}
})
}
})
RED.nodes.registerType('xiaoai-nlp-debug', class {
constructor (config) {
const node = this
RED.nodes.createNode(node, config)
const xiaomiConfig = RED.nodes.getNode(config.xiaoai)
const xiaoai = new Xiaoai(node, xiaomiConfig)
node.on('input', async msg => {
const {url, method, headers, data, type, needCookie } = msg
try {
const res = await xiaoai.debug(url, method, headers, data, type, needCookie)
msg.payload = res
node.status({ text: `获取debug:${msg._msgid}` })
node.send([msg, null])
} catch (err) {
if (err instanceof XiaoAiError) {
node.status({ text: '用户名密码错误,请xiaomi网站登陆尝试', fill: 'red', shape: 'ring' })
} else {
if (err.status && err.status == 401) {
xiaoai.clean()
node.status({ text: '授权信息失效, 请再试一次', fill: 'red', shape: 'ring' })
} else {
node.status({ text: err.message, fill: 'red', shape: 'ring' })
}
}
console.log(err)
node.warn(err)
msg.payload = {}
msg.error_msg = err.message
node.send([null, msg])
}
})
}
})
}