wechaty-web-panel 1.6.112 → 1.6.113
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/dist/bot/chatgpt/index.js +235 -0
- package/dist/bot/coze/sdk/index.js +110 -0
- package/dist/bot/dify/sdk/index.js +461 -0
- package/dist/bot/dify/sdk/office.js +319 -0
- package/dist/bot/fastgpt/index.js +98 -0
- package/dist/bot/qanything/index.js +136 -0
- package/dist/botInstance/coze.js +167 -0
- package/dist/botInstance/cozev3.js +157 -0
- package/dist/botInstance/dify.js +160 -0
- package/dist/botInstance/fastgpt.js +130 -0
- package/dist/botInstance/gpt4v.js +95 -0
- package/dist/botInstance/officialOpenAi.js +186 -0
- package/dist/botInstance/qany.js +144 -0
- package/dist/botInstance/sdk/chatGPT4V.js +89 -0
- package/dist/botInstance/sdk/coze.js +200 -0
- package/dist/botInstance/sdk/difyClient.js +354 -0
- package/dist/botInstance/sdk/pTimeout.js +97 -0
- package/dist/botInstance/sdk/qanything.js +137 -0
- package/dist/botInstance/sdk/quick-lru.js +237 -0
- package/dist/common/hook.js +66 -0
- package/dist/common/index.js +513 -0
- package/dist/common/multiReply.js +158 -0
- package/dist/common/reply.js +23 -0
- package/dist/const/puppet-type.js +71 -0
- package/dist/db/aiDb.js +27 -0
- package/dist/db/aichatDb.js +84 -0
- package/dist/db/chatHistory.js +137 -0
- package/dist/db/configDb.js +97 -0
- package/dist/db/global.js +62 -0
- package/dist/db/gptConfig.js +85 -0
- package/dist/db/nedb.js +88 -0
- package/dist/db/puppetDb.js +58 -0
- package/dist/db/roomDb.js +83 -0
- package/dist/db/rssConfig.js +82 -0
- package/dist/db/rssHistory.js +88 -0
- package/dist/db/userDb.js +27 -0
- package/dist/handlers/on-callback-message.js +183 -0
- package/dist/handlers/on-error.js +5 -0
- package/dist/handlers/on-friend.js +62 -0
- package/dist/handlers/on-heartbeat.js +20 -0
- package/dist/handlers/on-login.js +58 -0
- package/dist/handlers/on-logout.js +17 -0
- package/dist/handlers/on-message.js +644 -0
- package/dist/handlers/on-ready.js +36 -0
- package/dist/handlers/on-record-message.js +56 -0
- package/dist/handlers/on-roomjoin.js +42 -0
- package/dist/handlers/on-roomleave.js +12 -0
- package/dist/handlers/on-roomtopic.js +16 -0
- package/dist/handlers/on-scan.js +64 -0
- package/dist/handlers/on-verifycode.js +42 -0
- package/dist/index.js +81 -69306
- package/dist/lib/contentCensor.js +23 -0
- package/dist/lib/index.js +562 -0
- package/dist/lib/oss.js +43 -0
- package/dist/lib/s3oss.js +33 -0
- package/dist/mcp/mcp-server.js +26 -0
- package/dist/mcp/src/config/database.js +51 -0
- package/dist/mcp/src/index.js +238 -0
- package/dist/mcp/src/mcp/schemas.js +178 -0
- package/dist/mcp/src/mcp/server.js +421 -0
- package/dist/mcp/src/mcp/streamable-server.js +690 -0
- package/dist/mcp/src/models/ChatMessage.js +151 -0
- package/dist/mcp/src/models/Friend.js +64 -0
- package/dist/mcp/src/models/Group.js +55 -0
- package/dist/mcp/src/models/GroupMember.js +67 -0
- package/dist/mcp/src/models/index.js +27 -0
- package/dist/mcp/src/scripts/migrate.js +21 -0
- package/dist/mcp/src/services/ChatDataService.js +284 -0
- package/dist/mcp/src/services/McpService.js +521 -0
- package/dist/mcp/src/services/McpTools.js +504 -0
- package/dist/mcp/streamable-examples.js +283 -0
- package/dist/mcp/streamable-server.js +79 -0
- package/dist/mcp/test-mcp.js +64 -0
- package/dist/mcp/test-streamable-server.js +86 -0
- package/dist/package-json.js +89 -0
- package/dist/proxy/aibotk.js +829 -0
- package/dist/proxy/api.js +431 -0
- package/dist/proxy/apib.js +587 -0
- package/dist/proxy/bot/chatgpt.js +38 -0
- package/dist/proxy/bot/coze.js +38 -0
- package/dist/proxy/bot/cozev3.js +38 -0
- package/dist/proxy/bot/dify.js +38 -0
- package/dist/proxy/bot/dispatch.js +81 -0
- package/dist/proxy/bot/fastgpt.js +27 -0
- package/dist/proxy/bot/qany.js +27 -0
- package/dist/proxy/config.js +14 -0
- package/dist/proxy/cozeAi.js +60 -0
- package/dist/proxy/cozeV3Ai.js +60 -0
- package/dist/proxy/difyAi.js +58 -0
- package/dist/proxy/fastgpt.js +55 -0
- package/dist/proxy/mqtt.js +275 -0
- package/dist/proxy/multimodal.js +122 -0
- package/dist/proxy/openAi.js +63 -0
- package/dist/proxy/outapi.js +62 -0
- package/dist/proxy/qAnyAi.js +57 -0
- package/dist/proxy/superagent.js +200 -0
- package/dist/proxy/tencent-open.js +255 -0
- package/dist/service/event-dispatch-service.js +309 -0
- package/dist/service/gpt4vService.js +45 -0
- package/dist/service/msg-filter-service.js +121 -0
- package/dist/service/msg-filters.js +645 -0
- package/dist/service/room-async-service.js +455 -0
- package/dist/task/index.js +535 -0
- package/dist/task/rss.js +174 -0
- package/package.json +2 -2
- package/src/package-json.js +2 -2
- package/tsconfig.json +3 -12
- package/dist/index.d.ts +0 -9
- package/tsconfig.cjs.json +0 -12
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
export const BASE_URL = 'https://api.dify.ai/v1';
|
|
3
|
+
export const routes = {
|
|
4
|
+
// app's
|
|
5
|
+
feedback: {
|
|
6
|
+
method: 'POST', url: (messageId) => `/messages/${messageId}/feedbacks`
|
|
7
|
+
},
|
|
8
|
+
application: {
|
|
9
|
+
method: 'GET', url: () => `/parameters`
|
|
10
|
+
},
|
|
11
|
+
fileUpload: {
|
|
12
|
+
method: 'POST',
|
|
13
|
+
url: () => `/files/upload`
|
|
14
|
+
},
|
|
15
|
+
textToAudio: {
|
|
16
|
+
method: "POST",
|
|
17
|
+
url: () => `/text-to-audio`,
|
|
18
|
+
},
|
|
19
|
+
getMeta: {
|
|
20
|
+
method: "GET",
|
|
21
|
+
url: () => `/meta`,
|
|
22
|
+
},
|
|
23
|
+
// completion's
|
|
24
|
+
createCompletionMessage: {
|
|
25
|
+
method: 'POST', url: () => `/completion-messages`
|
|
26
|
+
},
|
|
27
|
+
// chat's
|
|
28
|
+
createChatMessage: {
|
|
29
|
+
method: 'POST', url: () => `/chat-messages`
|
|
30
|
+
},
|
|
31
|
+
getSuggested: {
|
|
32
|
+
method: "GET",
|
|
33
|
+
url: (message_id) => `/messages/${message_id}/suggested`,
|
|
34
|
+
},
|
|
35
|
+
stopChatMessage: {
|
|
36
|
+
method: "POST",
|
|
37
|
+
url: (task_id) => `/chat-messages/${task_id}/stop`,
|
|
38
|
+
},
|
|
39
|
+
getConversations: {
|
|
40
|
+
method: 'GET', url: () => '/conversations'
|
|
41
|
+
},
|
|
42
|
+
getConversationMessages: {
|
|
43
|
+
method: 'GET', url: () => '/messages'
|
|
44
|
+
},
|
|
45
|
+
renameConversation: {
|
|
46
|
+
method: "POST",
|
|
47
|
+
url: (conversation_id) => `/conversations/${conversation_id}/name`,
|
|
48
|
+
},
|
|
49
|
+
deleteConversation: {
|
|
50
|
+
method: 'DELETE',
|
|
51
|
+
url: (conversation_id) => `/conversations/${conversation_id}`
|
|
52
|
+
},
|
|
53
|
+
audioToText: {
|
|
54
|
+
method: "POST",
|
|
55
|
+
url: () => `/audio-to-text`,
|
|
56
|
+
},
|
|
57
|
+
// workflow‘s
|
|
58
|
+
getWorkflowInfo: {
|
|
59
|
+
method: "GET",
|
|
60
|
+
url: () => `/info`,
|
|
61
|
+
},
|
|
62
|
+
runWorkflow: {
|
|
63
|
+
method: "POST",
|
|
64
|
+
url: () => `/workflows/run`,
|
|
65
|
+
},
|
|
66
|
+
stopWorkflow: {
|
|
67
|
+
method: "POST",
|
|
68
|
+
url: (task_id) => `/workflows/${task_id}/stop`,
|
|
69
|
+
},
|
|
70
|
+
getWorkflowResult: {
|
|
71
|
+
method: "GET",
|
|
72
|
+
url: (task_id) => `/workflows/run/${task_id}`,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
export class DifyClient {
|
|
76
|
+
constructor({ apiKey, baseUrl = BASE_URL, debug = false, systemMessage = null, stream = false }) {
|
|
77
|
+
this.apiKey = apiKey;
|
|
78
|
+
this.baseUrl = baseUrl;
|
|
79
|
+
this.debug = debug;
|
|
80
|
+
this.stream = stream;
|
|
81
|
+
this.systemMessage = systemMessage;
|
|
82
|
+
}
|
|
83
|
+
updateApiKey(apiKey) {
|
|
84
|
+
this.apiKey = apiKey;
|
|
85
|
+
}
|
|
86
|
+
async sendUploadRequest(method, endpoint, data = null, params = null, stream = false, headerParams = {}) {
|
|
87
|
+
const headers = {
|
|
88
|
+
...{
|
|
89
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
90
|
+
'Content-Type': 'application/json'
|
|
91
|
+
},
|
|
92
|
+
...headerParams
|
|
93
|
+
};
|
|
94
|
+
const url = `${this.baseUrl}${endpoint}`;
|
|
95
|
+
let response = await axios({
|
|
96
|
+
method,
|
|
97
|
+
url,
|
|
98
|
+
data,
|
|
99
|
+
params,
|
|
100
|
+
headers,
|
|
101
|
+
responseType: 'json'
|
|
102
|
+
});
|
|
103
|
+
return response;
|
|
104
|
+
}
|
|
105
|
+
async sendRequest({ method, endpoint, data, params, stream = false, headerParams = {}, timeoutMs = 100 * 1000 }) {
|
|
106
|
+
const headers = {
|
|
107
|
+
...{
|
|
108
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
109
|
+
'Content-Type': 'application/json'
|
|
110
|
+
},
|
|
111
|
+
...headerParams
|
|
112
|
+
};
|
|
113
|
+
const url = `${this.baseUrl}${endpoint}`;
|
|
114
|
+
let response;
|
|
115
|
+
if (this.debug) {
|
|
116
|
+
console.log('dify request', url, { data, headers, params });
|
|
117
|
+
}
|
|
118
|
+
if (!stream) {
|
|
119
|
+
response = await axios({
|
|
120
|
+
method,
|
|
121
|
+
url,
|
|
122
|
+
...(method !== "GET" && { data }),
|
|
123
|
+
params: params || null,
|
|
124
|
+
headers,
|
|
125
|
+
timeout: timeoutMs,
|
|
126
|
+
responseType: 'json'
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
response = await axios({
|
|
131
|
+
method,
|
|
132
|
+
url,
|
|
133
|
+
data,
|
|
134
|
+
params,
|
|
135
|
+
headers,
|
|
136
|
+
responseType: 'stream'
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
return response;
|
|
140
|
+
}
|
|
141
|
+
messageFeedback(messageId, rating, user) {
|
|
142
|
+
const data = {
|
|
143
|
+
rating, user
|
|
144
|
+
};
|
|
145
|
+
return this.sendRequest({ method: routes.feedback.method, endpoint: routes.feedback.url(messageId), data });
|
|
146
|
+
}
|
|
147
|
+
getApplicationParameters(user) {
|
|
148
|
+
const params = { user };
|
|
149
|
+
return this.sendRequest({ method: routes.application.method, endpoint: routes.application.url(), params });
|
|
150
|
+
}
|
|
151
|
+
fileUpload(data) {
|
|
152
|
+
return this.sendUploadRequest(routes.fileUpload.method, routes.fileUpload.url(), data, null, false, {
|
|
153
|
+
'Content-Type': 'multipart/form-data'
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
textToAudio(text, user, streaming = false) {
|
|
157
|
+
const data = {
|
|
158
|
+
text,
|
|
159
|
+
user,
|
|
160
|
+
streaming
|
|
161
|
+
};
|
|
162
|
+
return this.sendRequest(routes.textToAudio.method, routes.textToAudio.url(), data, null, streaming);
|
|
163
|
+
}
|
|
164
|
+
getMeta(user) {
|
|
165
|
+
const params = { user };
|
|
166
|
+
return this.sendRequest(routes.meta.method, routes.meta.url(), null, params);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
export class CompletionClient extends DifyClient {
|
|
170
|
+
createCompletionMessage(inputs, user, stream = false, files = null) {
|
|
171
|
+
const data = {
|
|
172
|
+
inputs,
|
|
173
|
+
user,
|
|
174
|
+
response_mode: stream ? 'streaming' : 'blocking',
|
|
175
|
+
files
|
|
176
|
+
};
|
|
177
|
+
return this.sendRequest({
|
|
178
|
+
method: routes.createCompletionMessage.method,
|
|
179
|
+
endpoint: routes.createCompletionMessage.url(),
|
|
180
|
+
data,
|
|
181
|
+
stream
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
runWorkflow(inputs, user, stream = false, files = null) {
|
|
185
|
+
const data = {
|
|
186
|
+
inputs,
|
|
187
|
+
user,
|
|
188
|
+
response_mode: stream ? "streaming" : "blocking",
|
|
189
|
+
};
|
|
190
|
+
return this.sendRequest(routes.runWorkflow.method, routes.runWorkflow.url(), data, null, stream);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
export class ChatClient extends DifyClient {
|
|
194
|
+
async sendMessage(query, { systemMessage, user, conversationId = null, timeoutMs = 100 * 1000, files = null, inputs }) {
|
|
195
|
+
const data = {
|
|
196
|
+
inputs: {
|
|
197
|
+
...inputs
|
|
198
|
+
},
|
|
199
|
+
query,
|
|
200
|
+
user,
|
|
201
|
+
response_mode: this.stream ? 'streaming' : 'blocking',
|
|
202
|
+
files
|
|
203
|
+
};
|
|
204
|
+
if (systemMessage || this.systemMessage) {
|
|
205
|
+
data.inputs['systemMessage'] = systemMessage || this.systemMessage;
|
|
206
|
+
}
|
|
207
|
+
if (conversationId)
|
|
208
|
+
data.conversation_id = conversationId;
|
|
209
|
+
if (this.debug) {
|
|
210
|
+
console.log('request data', data);
|
|
211
|
+
}
|
|
212
|
+
const res = await this.sendRequest({
|
|
213
|
+
method: routes.createChatMessage.method,
|
|
214
|
+
endpoint: routes.createChatMessage.url(),
|
|
215
|
+
data,
|
|
216
|
+
stream: this.stream,
|
|
217
|
+
timeoutMs
|
|
218
|
+
});
|
|
219
|
+
function unicodeToChar(text) {
|
|
220
|
+
if (!text)
|
|
221
|
+
return '';
|
|
222
|
+
return text.replace(/\\u[0-9a-f]{4}/g, (_match, p1) => {
|
|
223
|
+
return String.fromCharCode(parseInt(p1, 16));
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
const asyncSSE = (stream) => {
|
|
227
|
+
return new Promise((resolve, reject) => {
|
|
228
|
+
const answers = [];
|
|
229
|
+
const thought = [];
|
|
230
|
+
const files = [];
|
|
231
|
+
let conversation_id = '';
|
|
232
|
+
let id = '';
|
|
233
|
+
try {
|
|
234
|
+
stream.on('data', data => {
|
|
235
|
+
const streams = new TextDecoder('utf-8').decode(data, { stream: true }).split('\n');
|
|
236
|
+
streams.forEach(stream => {
|
|
237
|
+
if (stream && stream.startsWith('data: ')) {
|
|
238
|
+
let res = {};
|
|
239
|
+
try {
|
|
240
|
+
res = JSON.parse(stream.substring(6)) || {};
|
|
241
|
+
}
|
|
242
|
+
catch (e) {
|
|
243
|
+
// console.log('json 解析错误,不影响输出', e)
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (!res.event || res.event === 'error' || res.status === 400) {
|
|
247
|
+
console.log(`流式输出错误code:${res.code}`, res.message);
|
|
248
|
+
answers.push(res.message);
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (res.event === 'agent_message' && res.answer || res.event === 'message' && res.answer) {
|
|
252
|
+
conversation_id = res.conversation_id;
|
|
253
|
+
answers.push(unicodeToChar(res.answer));
|
|
254
|
+
}
|
|
255
|
+
if (res.event === 'message_file') {
|
|
256
|
+
console.log('收到一个需要展示的文件,稍后发送');
|
|
257
|
+
files.push(res.url);
|
|
258
|
+
}
|
|
259
|
+
if (res.event === 'agent_thought' && res.thought) {
|
|
260
|
+
console.log('Dify Agent 正在思考...');
|
|
261
|
+
thought.push(res.thought);
|
|
262
|
+
}
|
|
263
|
+
if (res.event === 'message_end') {
|
|
264
|
+
console.log('流数据接收完毕,正在组装数据进行发送');
|
|
265
|
+
conversation_id = res.conversation_id;
|
|
266
|
+
id = res.id;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
stream.on('end', async () => {
|
|
272
|
+
const { data } = conversation_id ? await this.getConversationMessages(user, conversation_id, null, 2) : { data: { data: [] } };
|
|
273
|
+
const lastMessage = data.data[data.data.length - 1] || {};
|
|
274
|
+
if (this.debug) {
|
|
275
|
+
console.log('获取最后一条对话记录', lastMessage);
|
|
276
|
+
}
|
|
277
|
+
let answer = '';
|
|
278
|
+
let finalFiles = [];
|
|
279
|
+
if (lastMessage.answer) {
|
|
280
|
+
answer = lastMessage.answer;
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
answer = thought[thought.length - 1] ? thought[thought.length - 1] : answers.join('');
|
|
284
|
+
}
|
|
285
|
+
if (lastMessage.message_files && lastMessage.message_files.length) {
|
|
286
|
+
lastMessage.message_files.forEach(item => {
|
|
287
|
+
finalFiles.push(item.url);
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
finalFiles = files;
|
|
292
|
+
}
|
|
293
|
+
resolve({ text: answer, conversationId: conversation_id, id, files: finalFiles });
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
catch (e) {
|
|
297
|
+
resolve({ text: `AI agent 出错,${e}`, conversationId: '', files: [] });
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
};
|
|
301
|
+
if (!this.stream) {
|
|
302
|
+
if (res.data.code) {
|
|
303
|
+
if (this.debug) {
|
|
304
|
+
console.log('dify request error', res.data.code, res.data.message);
|
|
305
|
+
}
|
|
306
|
+
return Promise.reject(res.message);
|
|
307
|
+
}
|
|
308
|
+
const response = res.data;
|
|
309
|
+
return {
|
|
310
|
+
text: response.answer,
|
|
311
|
+
conversationId: response.conversation_id,
|
|
312
|
+
files: [],
|
|
313
|
+
id: response.id
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
console.log('进入Dify Agent 智能助手输出模式,请耐心等待模型的思考');
|
|
318
|
+
const result = await asyncSSE(res.data);
|
|
319
|
+
return result;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
getConversationMessages(user, conversationId = '', firstId = null, limit = null) {
|
|
323
|
+
const params = { user };
|
|
324
|
+
if (conversationId)
|
|
325
|
+
params.conversation_id = conversationId;
|
|
326
|
+
if (firstId)
|
|
327
|
+
params.first_id = firstId;
|
|
328
|
+
if (limit)
|
|
329
|
+
params.limit = limit;
|
|
330
|
+
return this.sendRequest({
|
|
331
|
+
method: routes.getConversationMessages.method, endpoint: routes.getConversationMessages.url(), params
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
getConversations(user, firstId = null, limit = null, pinned = null) {
|
|
335
|
+
const params = { user, first_id: firstId, limit, pinned };
|
|
336
|
+
return this.sendRequest({
|
|
337
|
+
method: routes.getConversations.method, endpoint: routes.getConversations.url(), params
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
renameConversation(conversationId, name, user) {
|
|
341
|
+
const data = { name, user };
|
|
342
|
+
return this.sendRequest({
|
|
343
|
+
method: routes.renameConversation.method, endpoint: routes.renameConversation.url(conversationId), data
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
getSuggested(message_id, user) {
|
|
347
|
+
const data = { user };
|
|
348
|
+
return this.sendRequest(routes.getSuggested.method, routes.getSuggested.url(message_id), data);
|
|
349
|
+
}
|
|
350
|
+
stopMessage(task_id, user) {
|
|
351
|
+
const data = { user };
|
|
352
|
+
return this.sendRequest(routes.stopChatMessage.method, routes.stopChatMessage.url(task_id), data);
|
|
353
|
+
}
|
|
354
|
+
audioToText(data) {
|
|
355
|
+
return this.sendRequest(routes.audioToText.method, routes.audioToText.url(), data, null, false, {
|
|
356
|
+
"Content-Type": 'multipart/form-data'
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
export class WorkflowClient extends DifyClient {
|
|
361
|
+
run(inputs, user, stream) {
|
|
362
|
+
const data = {
|
|
363
|
+
inputs,
|
|
364
|
+
response_mode: stream ? "streaming" : "blocking",
|
|
365
|
+
user
|
|
366
|
+
};
|
|
367
|
+
return this.sendRequest(routes.runWorkflow.method, routes.runWorkflow.url(), data, null, stream);
|
|
368
|
+
}
|
|
369
|
+
stop(task_id, user) {
|
|
370
|
+
const data = { user };
|
|
371
|
+
return this.sendRequest(routes.stopWorkflow.method, routes.stopWorkflow.url(task_id), data);
|
|
372
|
+
}
|
|
373
|
+
info(user) {
|
|
374
|
+
const params = { user };
|
|
375
|
+
return this.sendRequest(routes.getWorkflowInfo.method, routes.getWorkflowInfo.url(), null, params);
|
|
376
|
+
}
|
|
377
|
+
result(task_id) {
|
|
378
|
+
return this.sendRequest(routes.getWorkflowResult.method, routes.getWorkflowResult.url(task_id), null);
|
|
379
|
+
}
|
|
380
|
+
async getWorkflowResult(input, user, isStream) {
|
|
381
|
+
const res = await this.run(input, user, isStream);
|
|
382
|
+
function unicodeToChar(text) {
|
|
383
|
+
if (!text)
|
|
384
|
+
return '';
|
|
385
|
+
return text.replace(/\\u[0-9a-f]{4}/g, (_match, p1) => {
|
|
386
|
+
return String.fromCharCode(parseInt(p1, 16));
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
const asyncSSE = (stream) => {
|
|
390
|
+
return new Promise((resolve, reject) => {
|
|
391
|
+
let task_id = '';
|
|
392
|
+
try {
|
|
393
|
+
stream.on('data', data => {
|
|
394
|
+
const streams = new TextDecoder('utf-8').decode(data, { stream: true }).split('\n');
|
|
395
|
+
streams.forEach(stream => {
|
|
396
|
+
if (stream && stream.startsWith('data: ')) {
|
|
397
|
+
let res = {};
|
|
398
|
+
try {
|
|
399
|
+
res = JSON.parse(stream.substring(6)) || {};
|
|
400
|
+
}
|
|
401
|
+
catch (e) {
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
if (!res.event || res.event === 'error' || res.status === 400) {
|
|
405
|
+
console.log(`工作流输出错误code:${res.code}`, res.message);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
if (res.event === 'workflow_started' || res.event === 'tts_message') {
|
|
409
|
+
task_id = res?.workflow_run_id;
|
|
410
|
+
console.log('工作流开始执行');
|
|
411
|
+
}
|
|
412
|
+
if (res.event === 'node_started' || res.event === 'node_finished') {
|
|
413
|
+
task_id = res?.workflow_run_id;
|
|
414
|
+
console.log('工作流node节点执行任务中');
|
|
415
|
+
}
|
|
416
|
+
if (res.event === 'workflow_finished' || res.event === 'tts_message_end') {
|
|
417
|
+
console.log('工作流执行完毕,正在组装数据进行发送');
|
|
418
|
+
task_id = res?.workflow_run_id;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
});
|
|
423
|
+
stream.on('end', async () => {
|
|
424
|
+
const { data } = task_id ? await this.result(task_id) : { data: { outputs: '' } };
|
|
425
|
+
console.log('获取工作流执行结果', task_id, JSON.stringify(data.outputs));
|
|
426
|
+
let outputs = {};
|
|
427
|
+
if (data.outputs) {
|
|
428
|
+
try {
|
|
429
|
+
outputs = JSON.parse(data.outputs);
|
|
430
|
+
}
|
|
431
|
+
catch (error) {
|
|
432
|
+
console.log(`获取工作流执行结果,失败:${error}`);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
resolve({ text: outputs?.text, task_id: task_id });
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
catch (e) {
|
|
439
|
+
resolve({ text: `Dify工作流执行出错,${e}`, task_id: '' });
|
|
440
|
+
}
|
|
441
|
+
});
|
|
442
|
+
};
|
|
443
|
+
if (!isStream) {
|
|
444
|
+
if (res.data.code) {
|
|
445
|
+
console.log('Dify 工作流执行失败', res.data.code, res.data.message);
|
|
446
|
+
return Promise.reject(res.message);
|
|
447
|
+
}
|
|
448
|
+
const response = res.data;
|
|
449
|
+
return {
|
|
450
|
+
text: response?.data?.outputs?.text || '',
|
|
451
|
+
task_id: response?.task_id,
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
console.log('进入Dify工作流,请耐心等待...');
|
|
456
|
+
const result = await asyncSSE(res.data);
|
|
457
|
+
return result;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
//# sourceMappingURL=index.js.map
|