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,319 @@
|
|
|
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",
|
|
7
|
+
url: (message_id) => `/messages/${message_id}/feedbacks`,
|
|
8
|
+
},
|
|
9
|
+
application: {
|
|
10
|
+
method: "GET",
|
|
11
|
+
url: () => `/parameters`,
|
|
12
|
+
},
|
|
13
|
+
fileUpload: {
|
|
14
|
+
method: "POST",
|
|
15
|
+
url: () => `/files/upload`,
|
|
16
|
+
},
|
|
17
|
+
textToAudio: {
|
|
18
|
+
method: "POST",
|
|
19
|
+
url: () => `/text-to-audio`,
|
|
20
|
+
},
|
|
21
|
+
getMeta: {
|
|
22
|
+
method: "GET",
|
|
23
|
+
url: () => `/meta`,
|
|
24
|
+
},
|
|
25
|
+
// completion's
|
|
26
|
+
createCompletionMessage: {
|
|
27
|
+
method: "POST",
|
|
28
|
+
url: () => `/completion-messages`,
|
|
29
|
+
},
|
|
30
|
+
// chat's
|
|
31
|
+
createChatMessage: {
|
|
32
|
+
method: "POST",
|
|
33
|
+
url: () => `/chat-messages`,
|
|
34
|
+
},
|
|
35
|
+
getSuggested: {
|
|
36
|
+
method: "GET",
|
|
37
|
+
url: (message_id) => `/messages/${message_id}/suggested`,
|
|
38
|
+
},
|
|
39
|
+
stopChatMessage: {
|
|
40
|
+
method: "POST",
|
|
41
|
+
url: (task_id) => `/chat-messages/${task_id}/stop`,
|
|
42
|
+
},
|
|
43
|
+
getConversations: {
|
|
44
|
+
method: "GET",
|
|
45
|
+
url: () => `/conversations`,
|
|
46
|
+
},
|
|
47
|
+
getConversationMessages: {
|
|
48
|
+
method: "GET",
|
|
49
|
+
url: () => `/messages`,
|
|
50
|
+
},
|
|
51
|
+
renameConversation: {
|
|
52
|
+
method: "POST",
|
|
53
|
+
url: (conversation_id) => `/conversations/${conversation_id}/name`,
|
|
54
|
+
},
|
|
55
|
+
deleteConversation: {
|
|
56
|
+
method: "DELETE",
|
|
57
|
+
url: (conversation_id) => `/conversations/${conversation_id}`,
|
|
58
|
+
},
|
|
59
|
+
audioToText: {
|
|
60
|
+
method: "POST",
|
|
61
|
+
url: () => `/audio-to-text`,
|
|
62
|
+
},
|
|
63
|
+
// workflow‘s
|
|
64
|
+
getWorkflowInfo: {
|
|
65
|
+
method: "GET",
|
|
66
|
+
url: () => `/info`,
|
|
67
|
+
},
|
|
68
|
+
runWorkflow: {
|
|
69
|
+
method: "POST",
|
|
70
|
+
url: () => `/workflows/run`,
|
|
71
|
+
},
|
|
72
|
+
stopWorkflow: {
|
|
73
|
+
method: "POST",
|
|
74
|
+
url: (task_id) => `/workflows/${task_id}/stop`,
|
|
75
|
+
},
|
|
76
|
+
getWorkflowResult: {
|
|
77
|
+
method: "GET",
|
|
78
|
+
url: (task_id) => `/workflows/run/${task_id}`,
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
export class DifyClient {
|
|
82
|
+
constructor(apiKey, baseUrl = BASE_URL) {
|
|
83
|
+
this.apiKey = apiKey;
|
|
84
|
+
this.baseUrl = baseUrl;
|
|
85
|
+
}
|
|
86
|
+
updateApiKey(apiKey) {
|
|
87
|
+
this.apiKey = apiKey;
|
|
88
|
+
}
|
|
89
|
+
async sendRequest(method, endpoint, data = null, params = null, stream = false, headerParams = {}) {
|
|
90
|
+
const headers = {
|
|
91
|
+
...{
|
|
92
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
93
|
+
"Content-Type": "application/json",
|
|
94
|
+
},
|
|
95
|
+
...headerParams
|
|
96
|
+
};
|
|
97
|
+
const url = `${this.baseUrl}${endpoint}`;
|
|
98
|
+
let response;
|
|
99
|
+
if (stream) {
|
|
100
|
+
response = await axios({
|
|
101
|
+
method,
|
|
102
|
+
url,
|
|
103
|
+
data,
|
|
104
|
+
params,
|
|
105
|
+
headers,
|
|
106
|
+
responseType: "stream",
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
response = await axios({
|
|
111
|
+
method,
|
|
112
|
+
url,
|
|
113
|
+
...(method !== "GET" && { data }),
|
|
114
|
+
params,
|
|
115
|
+
headers,
|
|
116
|
+
responseType: "json",
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return response;
|
|
120
|
+
}
|
|
121
|
+
messageFeedback(message_id, rating, user) {
|
|
122
|
+
const data = {
|
|
123
|
+
rating,
|
|
124
|
+
user,
|
|
125
|
+
};
|
|
126
|
+
return this.sendRequest(routes.feedback.method, routes.feedback.url(message_id), data);
|
|
127
|
+
}
|
|
128
|
+
getApplicationParameters(user) {
|
|
129
|
+
const params = { user };
|
|
130
|
+
return this.sendRequest(routes.application.method, routes.application.url(), null, params);
|
|
131
|
+
}
|
|
132
|
+
fileUpload(data) {
|
|
133
|
+
return this.sendRequest(routes.fileUpload.method, routes.fileUpload.url(), data, null, false, {
|
|
134
|
+
"Content-Type": 'multipart/form-data'
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
textToAudio(text, user, streaming = false) {
|
|
138
|
+
const data = {
|
|
139
|
+
text,
|
|
140
|
+
user,
|
|
141
|
+
streaming
|
|
142
|
+
};
|
|
143
|
+
return this.sendRequest(routes.textToAudio.method, routes.textToAudio.url(), data, null, streaming);
|
|
144
|
+
}
|
|
145
|
+
getMeta(user) {
|
|
146
|
+
const params = { user };
|
|
147
|
+
return this.sendRequest(routes.meta.method, routes.meta.url(), null, params);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
export class CompletionClient extends DifyClient {
|
|
151
|
+
createCompletionMessage(inputs, user, stream = false, files = null) {
|
|
152
|
+
const data = {
|
|
153
|
+
inputs,
|
|
154
|
+
user,
|
|
155
|
+
response_mode: stream ? "streaming" : "blocking",
|
|
156
|
+
files,
|
|
157
|
+
};
|
|
158
|
+
return this.sendRequest(routes.createCompletionMessage.method, routes.createCompletionMessage.url(), data, null, stream);
|
|
159
|
+
}
|
|
160
|
+
runWorkflow(inputs, user, stream = false, files = null) {
|
|
161
|
+
const data = {
|
|
162
|
+
inputs,
|
|
163
|
+
user,
|
|
164
|
+
response_mode: stream ? "streaming" : "blocking",
|
|
165
|
+
};
|
|
166
|
+
return this.sendRequest(routes.runWorkflow.method, routes.runWorkflow.url(), data, null, stream);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
export class ChatClient extends DifyClient {
|
|
170
|
+
createChatMessage(inputs, query, user, stream = false, conversation_id = null, files = null) {
|
|
171
|
+
const data = {
|
|
172
|
+
inputs,
|
|
173
|
+
query,
|
|
174
|
+
user,
|
|
175
|
+
response_mode: stream ? "streaming" : "blocking",
|
|
176
|
+
files,
|
|
177
|
+
};
|
|
178
|
+
if (conversation_id)
|
|
179
|
+
data.conversation_id = conversation_id;
|
|
180
|
+
return this.sendRequest(routes.createChatMessage.method, routes.createChatMessage.url(), data, null, stream);
|
|
181
|
+
}
|
|
182
|
+
getSuggested(message_id, user) {
|
|
183
|
+
const data = { user };
|
|
184
|
+
return this.sendRequest(routes.getSuggested.method, routes.getSuggested.url(message_id), data);
|
|
185
|
+
}
|
|
186
|
+
stopMessage(task_id, user) {
|
|
187
|
+
const data = { user };
|
|
188
|
+
return this.sendRequest(routes.stopChatMessage.method, routes.stopChatMessage.url(task_id), data);
|
|
189
|
+
}
|
|
190
|
+
getConversations(user, first_id = null, limit = null, pinned = null) {
|
|
191
|
+
const params = { user, first_id: first_id, limit, pinned };
|
|
192
|
+
return this.sendRequest(routes.getConversations.method, routes.getConversations.url(), null, params);
|
|
193
|
+
}
|
|
194
|
+
getConversationMessages(user, conversation_id = "", first_id = null, limit = null) {
|
|
195
|
+
const params = { user };
|
|
196
|
+
if (conversation_id)
|
|
197
|
+
params.conversation_id = conversation_id;
|
|
198
|
+
if (first_id)
|
|
199
|
+
params.first_id = first_id;
|
|
200
|
+
if (limit)
|
|
201
|
+
params.limit = limit;
|
|
202
|
+
return this.sendRequest(routes.getConversationMessages.method, routes.getConversationMessages.url(), null, params);
|
|
203
|
+
}
|
|
204
|
+
renameConversation(conversation_id, name, user, auto_generate) {
|
|
205
|
+
const data = { name, user, auto_generate };
|
|
206
|
+
return this.sendRequest(routes.renameConversation.method, routes.renameConversation.url(conversation_id), data);
|
|
207
|
+
}
|
|
208
|
+
deleteConversation(conversation_id, user) {
|
|
209
|
+
const data = { user };
|
|
210
|
+
return this.sendRequest(routes.deleteConversation.method, routes.deleteConversation.url(conversation_id), data);
|
|
211
|
+
}
|
|
212
|
+
audioToText(data) {
|
|
213
|
+
return this.sendRequest(routes.audioToText.method, routes.audioToText.url(), data, null, false, {
|
|
214
|
+
"Content-Type": 'multipart/form-data'
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
export class WorkflowClient extends DifyClient {
|
|
219
|
+
run(inputs, user, stream) {
|
|
220
|
+
const data = {
|
|
221
|
+
inputs,
|
|
222
|
+
response_mode: stream ? "streaming" : "blocking",
|
|
223
|
+
user
|
|
224
|
+
};
|
|
225
|
+
return this.sendRequest(routes.runWorkflow.method, routes.runWorkflow.url(), data, null, stream);
|
|
226
|
+
}
|
|
227
|
+
stop(task_id, user) {
|
|
228
|
+
const data = { user };
|
|
229
|
+
return this.sendRequest(routes.stopWorkflow.method, routes.stopWorkflow.url(task_id), data);
|
|
230
|
+
}
|
|
231
|
+
info(user) {
|
|
232
|
+
const params = { user };
|
|
233
|
+
return this.sendRequest(routes.getWorkflowInfo.method, routes.getWorkflowInfo.url(), null, params);
|
|
234
|
+
}
|
|
235
|
+
result(task_id) {
|
|
236
|
+
return this.sendRequest(routes.getWorkflowResult.method, routes.getWorkflowResult.url(task_id), null);
|
|
237
|
+
}
|
|
238
|
+
async getWorkflowResult(input, user, isStream) {
|
|
239
|
+
const res = await this.run(input, user, isStream);
|
|
240
|
+
function unicodeToChar(text) {
|
|
241
|
+
if (!text)
|
|
242
|
+
return '';
|
|
243
|
+
return text.replace(/\\u[0-9a-f]{4}/g, (_match, p1) => {
|
|
244
|
+
return String.fromCharCode(parseInt(p1, 16));
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
const asyncSSE = (stream) => {
|
|
248
|
+
return new Promise((resolve, reject) => {
|
|
249
|
+
let task_id = '';
|
|
250
|
+
try {
|
|
251
|
+
stream.on('data', data => {
|
|
252
|
+
const streams = new TextDecoder('utf-8').decode(data, { stream: true }).split('\n');
|
|
253
|
+
streams.forEach(stream => {
|
|
254
|
+
if (stream && stream.startsWith('data: ')) {
|
|
255
|
+
let res = {};
|
|
256
|
+
try {
|
|
257
|
+
res = JSON.parse(stream.substring(6)) || {};
|
|
258
|
+
}
|
|
259
|
+
catch (e) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (!res.event || res.event === 'error' || res.status === 400) {
|
|
263
|
+
console.log(`工作流输出错误code:${res.code}`, res.message);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (res.event === 'workflow_started' || res.event === 'tts_message') {
|
|
267
|
+
task_id = res?.workflow_run_id;
|
|
268
|
+
console.log('工作流开始执行');
|
|
269
|
+
}
|
|
270
|
+
if (res.event === 'node_started' || res.event === 'node_finished') {
|
|
271
|
+
task_id = res?.workflow_run_id;
|
|
272
|
+
console.log('工作流node节点执行任务中');
|
|
273
|
+
}
|
|
274
|
+
if (res.event === 'workflow_finished' || res.event === 'tts_message_end') {
|
|
275
|
+
console.log('工作流执行完毕,正在组装数据进行发送');
|
|
276
|
+
task_id = res?.workflow_run_id;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
stream.on('end', async () => {
|
|
282
|
+
const { data } = task_id ? await this.result(task_id) : { data: { outputs: '' } };
|
|
283
|
+
console.log('获取工作流执行结果', task_id, JSON.stringify(data.outputs));
|
|
284
|
+
let outputs = {};
|
|
285
|
+
if (data.outputs) {
|
|
286
|
+
try {
|
|
287
|
+
outputs = JSON.parse(data.outputs);
|
|
288
|
+
}
|
|
289
|
+
catch (error) {
|
|
290
|
+
console.log(`获取工作流执行结果,失败:${error}`);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
resolve({ text: outputs?.text, task_id: task_id });
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
catch (e) {
|
|
297
|
+
resolve({ text: `Dify工作流执行出错,${e}`, task_id: '' });
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
};
|
|
301
|
+
if (!isStream) {
|
|
302
|
+
if (res.data.code) {
|
|
303
|
+
console.log('Dify 工作流执行失败', res.data.code, res.data.message);
|
|
304
|
+
return Promise.reject(res.message);
|
|
305
|
+
}
|
|
306
|
+
const response = res.data;
|
|
307
|
+
return {
|
|
308
|
+
text: response?.data?.outputs?.text || '',
|
|
309
|
+
task_id: response?.task_id,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
console.log('进入Dify工作流,请耐心等待...');
|
|
314
|
+
const result = await asyncSSE(res.data);
|
|
315
|
+
return result;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
//# sourceMappingURL=office.js.map
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
export class FastGPTAPI {
|
|
3
|
+
constructor(opts) {
|
|
4
|
+
const { apiKey, apiBaseUrl = 'https://cloud.fastgpt.cn/api/v1', debug } = opts;
|
|
5
|
+
this._apiKey = apiKey;
|
|
6
|
+
this._apiBaseUrl = apiBaseUrl;
|
|
7
|
+
this._debug = debug;
|
|
8
|
+
if (!this._apiKey) {
|
|
9
|
+
throw new Error('缺少 apiKey');
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
async sendMessage({ text, file }, opts = {}) {
|
|
13
|
+
const { timeoutMs, chatId, variables } = opts;
|
|
14
|
+
const body = {
|
|
15
|
+
chatId,
|
|
16
|
+
variables: {
|
|
17
|
+
...variables,
|
|
18
|
+
},
|
|
19
|
+
messages: [
|
|
20
|
+
{
|
|
21
|
+
role: 'user',
|
|
22
|
+
content: text,
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
stream: false,
|
|
26
|
+
customUid: `${variables?.uname || ''}${variables?.ualias ? `(${variables?.ualias})` : ''}`,
|
|
27
|
+
detail: false,
|
|
28
|
+
};
|
|
29
|
+
if (file && file.fileType === '图片') {
|
|
30
|
+
body.messages[0].content = [
|
|
31
|
+
{
|
|
32
|
+
type: "text",
|
|
33
|
+
text: text
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
type: 'image_url',
|
|
37
|
+
image_url: {
|
|
38
|
+
url: file.fileUrl,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
else if (file && file.fileType === '文件') {
|
|
44
|
+
body.messages[0].content = [
|
|
45
|
+
{
|
|
46
|
+
type: "text",
|
|
47
|
+
text: text
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
type: 'file_url',
|
|
51
|
+
name: file.fileName,
|
|
52
|
+
url: file.fileUrl,
|
|
53
|
+
},
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
if (this._debug) {
|
|
57
|
+
console.log('request body', body);
|
|
58
|
+
}
|
|
59
|
+
const response = await axios({
|
|
60
|
+
method: 'POST',
|
|
61
|
+
url: `${this._apiBaseUrl}/chat/completions`,
|
|
62
|
+
data: body || null,
|
|
63
|
+
headers: {
|
|
64
|
+
'Content-Type': 'application/json',
|
|
65
|
+
Authorization: `Bearer ${this._apiKey}`,
|
|
66
|
+
},
|
|
67
|
+
timeout: timeoutMs,
|
|
68
|
+
responseType: 'json',
|
|
69
|
+
});
|
|
70
|
+
if (response.status === 200) {
|
|
71
|
+
if (this._debug) {
|
|
72
|
+
console.log('response.data', JSON.stringify(response.data));
|
|
73
|
+
}
|
|
74
|
+
if (response.data.code) {
|
|
75
|
+
console.log('FastGPT请求出错', JSON.stringify(response.data));
|
|
76
|
+
return {
|
|
77
|
+
text: '',
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
const data = response.data.choices[0].message;
|
|
81
|
+
let finalText = data.content;
|
|
82
|
+
if (Array.isArray(finalText)) {
|
|
83
|
+
const finalContent = finalText.find((item) => item.type === 'text');
|
|
84
|
+
if (finalContent) {
|
|
85
|
+
finalText = finalContent.text.content || '';
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
text: finalText,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
console.log('接口请求报错', response.data);
|
|
93
|
+
return {
|
|
94
|
+
text: '',
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
const BASE_URL = 'https://openapi.youdao.com';
|
|
3
|
+
const routes = {
|
|
4
|
+
// 发起对话
|
|
5
|
+
creatChat: {
|
|
6
|
+
method: 'POST', url: () => '/q_anything/api/bot/chat_stream',
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
class QAnyClient {
|
|
10
|
+
constructor({ apiKey, baseUrl = BASE_URL, debug = false, stream = true, botId }) {
|
|
11
|
+
this.apiKey = apiKey;
|
|
12
|
+
this.baseUrl = baseUrl || BASE_URL;
|
|
13
|
+
this.debug = debug;
|
|
14
|
+
this.stream = stream;
|
|
15
|
+
this.botId = botId;
|
|
16
|
+
this.history = [];
|
|
17
|
+
}
|
|
18
|
+
updateApiKey(apiKey) {
|
|
19
|
+
this.apiKey = apiKey;
|
|
20
|
+
}
|
|
21
|
+
async sendRequest({ method, endpoint, data, params, stream = false, headerParams = {}, timeoutMs = 100 * 1000 }) {
|
|
22
|
+
const headers = {
|
|
23
|
+
...{
|
|
24
|
+
Authorization: `${this.apiKey}`,
|
|
25
|
+
'Content-Type': 'application/json',
|
|
26
|
+
},
|
|
27
|
+
...headerParams,
|
|
28
|
+
};
|
|
29
|
+
const url = `${this.baseUrl}${endpoint}`;
|
|
30
|
+
let response;
|
|
31
|
+
if (this.debug) {
|
|
32
|
+
console.log('request', url, { data, headers, params });
|
|
33
|
+
}
|
|
34
|
+
if (!stream) {
|
|
35
|
+
response = await axios.request({
|
|
36
|
+
method,
|
|
37
|
+
url,
|
|
38
|
+
data: data || {},
|
|
39
|
+
params: params || {},
|
|
40
|
+
headers,
|
|
41
|
+
timeout: timeoutMs,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
response = await axios({
|
|
46
|
+
method,
|
|
47
|
+
url,
|
|
48
|
+
data,
|
|
49
|
+
params,
|
|
50
|
+
headers,
|
|
51
|
+
responseType: 'stream',
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return response;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export class QAnyApi extends QAnyClient {
|
|
58
|
+
async sendMessage(query, { needHistory, user, timeoutMs = 100 * 1000, variables, }) {
|
|
59
|
+
if (needHistory) {
|
|
60
|
+
this.history = this.history.slice(-2);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
this.history = [];
|
|
64
|
+
}
|
|
65
|
+
const data = {
|
|
66
|
+
uuid: this.botId,
|
|
67
|
+
question: query,
|
|
68
|
+
history: this.history
|
|
69
|
+
};
|
|
70
|
+
const res = await this.sendRequest({
|
|
71
|
+
method: routes.creatChat.method,
|
|
72
|
+
endpoint: routes.creatChat.url(),
|
|
73
|
+
data,
|
|
74
|
+
stream: this.stream,
|
|
75
|
+
timeoutMs,
|
|
76
|
+
});
|
|
77
|
+
const asyncSSE = stream => {
|
|
78
|
+
return new Promise((resolve, reject) => {
|
|
79
|
+
const answers = [];
|
|
80
|
+
let answer = '';
|
|
81
|
+
let id = '';
|
|
82
|
+
const chunks = [];
|
|
83
|
+
try {
|
|
84
|
+
stream.on('data', data => {
|
|
85
|
+
chunks.push(data);
|
|
86
|
+
});
|
|
87
|
+
stream.on('end', async () => {
|
|
88
|
+
const result = Buffer.concat(chunks);
|
|
89
|
+
console.log('思考完毕,准备回复');
|
|
90
|
+
const streams = new TextDecoder('utf-8').decode(result, { stream: true }).split('\n');
|
|
91
|
+
for (const stream of streams) {
|
|
92
|
+
let res = {};
|
|
93
|
+
try {
|
|
94
|
+
res = JSON.parse(stream.substring(5)) || {};
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
// console.log('json 解析错误,不影响输出', stream)
|
|
98
|
+
try {
|
|
99
|
+
res = JSON.parse(stream) || {};
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
// console.log('json 解析错误,不影响输出', stream)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (res.errorCode && res.errorCode !== '0') {
|
|
106
|
+
console.log('QAnything 请求报错', res.msg);
|
|
107
|
+
answer = res.msg;
|
|
108
|
+
}
|
|
109
|
+
else if (res.errorCode) {
|
|
110
|
+
if (res.result.response && !res.result.singleQAId) {
|
|
111
|
+
answers.push(res.result.response);
|
|
112
|
+
}
|
|
113
|
+
if (res.result.singleQAId) {
|
|
114
|
+
this.history = res.result.history;
|
|
115
|
+
id = res.result.singleQAId;
|
|
116
|
+
answer = res.result.response;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (!answer) {
|
|
121
|
+
answer = answers.join('');
|
|
122
|
+
}
|
|
123
|
+
resolve({ text: answer, conversationId: '', id: id });
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
resolve({ text: `agent 出错,${e}`, conversationId: '', files: [] });
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
console.log('进入流式输出模式,请耐心等待模型的思考');
|
|
132
|
+
const result = await asyncSSE(res.data);
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=index.js.map
|