wechaty-web-panel 1.6.7 → 1.6.9
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/CHANGELOG.md +6 -0
- package/dist/cjs/src/botInstance/dify.d.ts +36 -0
- package/dist/cjs/src/botInstance/dify.js +122 -0
- package/dist/cjs/src/{lib → botInstance}/officialOpenAi.d.ts +1 -1
- package/dist/cjs/src/{lib → botInstance}/officialOpenAi.js +3 -4
- package/dist/cjs/src/botInstance/sdk/difyClient.d.ts +86 -0
- package/dist/cjs/src/botInstance/sdk/difyClient.js +171 -0
- package/dist/cjs/src/{lib → botInstance}/unOfficialOpenAi.js +2 -2
- package/dist/cjs/src/lib/contentCensor.js +0 -1
- package/dist/cjs/src/package-json.js +1 -1
- package/dist/cjs/src/proxy/aibotk.js +2 -0
- package/dist/cjs/src/proxy/bot/chatgpt-web.js +1 -1
- package/dist/cjs/src/proxy/bot/chatgpt.js +1 -1
- package/dist/cjs/src/proxy/bot/dify.d.ts +14 -0
- package/dist/cjs/src/proxy/bot/dify.js +47 -0
- package/dist/cjs/src/proxy/bot/dispatch.js +7 -0
- package/dist/cjs/src/proxy/difyAi.d.ts +6 -0
- package/dist/cjs/src/proxy/difyAi.js +47 -0
- package/dist/cjs/src/proxy/openAi.js +1 -1
- package/dist/cjs/src/proxy/openAiHook.js +1 -1
- package/dist/cjs/src/service/event-dispatch-service.js +7 -0
- package/dist/esm/src/botInstance/dify.d.ts +36 -0
- package/dist/esm/src/botInstance/dify.js +117 -0
- package/dist/esm/src/{lib → botInstance}/officialOpenAi.d.ts +1 -1
- package/dist/esm/src/{lib → botInstance}/officialOpenAi.js +3 -4
- package/dist/esm/src/botInstance/sdk/difyClient.d.ts +86 -0
- package/dist/esm/src/botInstance/sdk/difyClient.js +162 -0
- package/dist/esm/src/{lib → botInstance}/unOfficialOpenAi.js +2 -2
- package/dist/esm/src/lib/contentCensor.js +0 -1
- package/dist/esm/src/package-json.js +1 -1
- package/dist/esm/src/proxy/aibotk.js +2 -0
- package/dist/esm/src/proxy/bot/chatgpt-web.js +1 -1
- package/dist/esm/src/proxy/bot/chatgpt.js +1 -1
- package/dist/esm/src/proxy/bot/dify.d.ts +14 -0
- package/dist/esm/src/proxy/bot/dify.js +38 -0
- package/dist/esm/src/proxy/bot/dispatch.js +7 -0
- package/dist/esm/src/proxy/difyAi.d.ts +6 -0
- package/dist/esm/src/proxy/difyAi.js +39 -0
- package/dist/esm/src/proxy/openAi.js +1 -1
- package/dist/esm/src/proxy/openAiHook.js +1 -1
- package/dist/esm/src/service/event-dispatch-service.js +7 -0
- package/package.json +1 -1
- /package/dist/cjs/src/{lib → botInstance/sdk}/chatGPT.d.ts +0 -0
- /package/dist/cjs/src/{lib → botInstance/sdk}/chatGPT.js +0 -0
- /package/dist/cjs/src/{lib → botInstance/sdk}/pTimeout.d.ts +0 -0
- /package/dist/cjs/src/{lib → botInstance/sdk}/pTimeout.js +0 -0
- /package/dist/cjs/src/{lib → botInstance/sdk}/quick-lru.d.ts +0 -0
- /package/dist/cjs/src/{lib → botInstance/sdk}/quick-lru.js +0 -0
- /package/dist/cjs/src/{lib → botInstance}/unOfficialOpenAi.d.ts +0 -0
- /package/dist/esm/src/{lib → botInstance/sdk}/chatGPT.d.ts +0 -0
- /package/dist/esm/src/{lib → botInstance/sdk}/chatGPT.js +0 -0
- /package/dist/esm/src/{lib → botInstance/sdk}/pTimeout.d.ts +0 -0
- /package/dist/esm/src/{lib → botInstance/sdk}/pTimeout.js +0 -0
- /package/dist/esm/src/{lib → botInstance/sdk}/quick-lru.d.ts +0 -0
- /package/dist/esm/src/{lib → botInstance/sdk}/quick-lru.js +0 -0
- /package/dist/esm/src/{lib → botInstance}/unOfficialOpenAi.d.ts +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export default DifyAi;
|
|
2
|
+
declare class DifyAi {
|
|
3
|
+
constructor(config?: {
|
|
4
|
+
token: string;
|
|
5
|
+
proxyPass: string;
|
|
6
|
+
showQuestion: boolean;
|
|
7
|
+
timeoutMs: number;
|
|
8
|
+
promotId: string;
|
|
9
|
+
systemMessage: string;
|
|
10
|
+
});
|
|
11
|
+
difyChat: ChatClient | null;
|
|
12
|
+
config: {
|
|
13
|
+
token: string;
|
|
14
|
+
proxyPass: string;
|
|
15
|
+
showQuestion: boolean;
|
|
16
|
+
timeoutMs: number;
|
|
17
|
+
promotId: string;
|
|
18
|
+
systemMessage: string;
|
|
19
|
+
};
|
|
20
|
+
contentCensor: ContentCensor | null;
|
|
21
|
+
chatOption: {};
|
|
22
|
+
eol: string;
|
|
23
|
+
init(): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* 重置apikey
|
|
26
|
+
* @return {Promise<void>}
|
|
27
|
+
*/
|
|
28
|
+
reset(): Promise<void>;
|
|
29
|
+
getReply(content: any, uid: any, adminId?: string, systemMessage?: string): Promise<{
|
|
30
|
+
type: number;
|
|
31
|
+
content: any;
|
|
32
|
+
}[]>;
|
|
33
|
+
}
|
|
34
|
+
import { ChatClient } from "./sdk/difyClient.js";
|
|
35
|
+
import { ContentCensor } from "../lib/contentCensor.js";
|
|
36
|
+
//# sourceMappingURL=dify.d.ts.map
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const difyClient_js_1 = require("./sdk/difyClient.js");
|
|
7
|
+
const aichatDb_js_1 = require("../db/aichatDb.js");
|
|
8
|
+
const aibotk_js_1 = require("../proxy/aibotk.js");
|
|
9
|
+
const contentCensor_js_1 = require("../lib/contentCensor.js");
|
|
10
|
+
const puppet_type_js_1 = require("../const/puppet-type.js");
|
|
11
|
+
const dayjs_1 = __importDefault(require("dayjs"));
|
|
12
|
+
class DifyAi {
|
|
13
|
+
constructor(config = {
|
|
14
|
+
token: '',
|
|
15
|
+
proxyPass: '',
|
|
16
|
+
showQuestion: true,
|
|
17
|
+
timeoutMs: 60,
|
|
18
|
+
promotId: '',
|
|
19
|
+
systemMessage: '', // 预设promotion
|
|
20
|
+
}) {
|
|
21
|
+
console.log('difyAi config', config);
|
|
22
|
+
this.difyChat = null;
|
|
23
|
+
this.config = config;
|
|
24
|
+
this.contentCensor = null;
|
|
25
|
+
this.chatOption = {};
|
|
26
|
+
this.eol = '\n';
|
|
27
|
+
}
|
|
28
|
+
async init() {
|
|
29
|
+
this.eol = await (0, puppet_type_js_1.getPuppetEol)();
|
|
30
|
+
if (this.config.promotId) {
|
|
31
|
+
const promotInfo = await (0, aibotk_js_1.getPromotInfo)(this.config.promotId);
|
|
32
|
+
if (promotInfo) {
|
|
33
|
+
this.config.systemMessage = promotInfo.promot;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (this.config.filter) {
|
|
37
|
+
this.contentCensor = new contentCensor_js_1.ContentCensor(this.config.filterConfig);
|
|
38
|
+
}
|
|
39
|
+
const baseOptions = {
|
|
40
|
+
apiKey: this.config.token,
|
|
41
|
+
debug: !!this.config.debug,
|
|
42
|
+
systemMessage: this.config.systemMessage || '',
|
|
43
|
+
};
|
|
44
|
+
console.log(`api请求地址:${this.config.proxyPass}`);
|
|
45
|
+
this.difyChat = new difyClient_js_1.ChatClient({
|
|
46
|
+
...baseOptions,
|
|
47
|
+
baseUrl: this.config.proxyPass,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* 重置apikey
|
|
52
|
+
* @return {Promise<void>}
|
|
53
|
+
*/
|
|
54
|
+
reset() {
|
|
55
|
+
this.difyChat = null;
|
|
56
|
+
}
|
|
57
|
+
async getReply(content, uid, adminId = '', systemMessage = '') {
|
|
58
|
+
try {
|
|
59
|
+
if (!this.difyChat) {
|
|
60
|
+
console.log('看到此消息说明已启用dify 对话平台');
|
|
61
|
+
await this.init();
|
|
62
|
+
}
|
|
63
|
+
if (this.config.filter) {
|
|
64
|
+
const censor = await this.contentCensor.checkText(content);
|
|
65
|
+
if (!censor) {
|
|
66
|
+
console.log(`问题:${content},包含违规词,已拦截`);
|
|
67
|
+
return [{ type: 1, content: '这个话题不适合讨论,换个话题吧。' }];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (systemMessage) {
|
|
71
|
+
console.log('带角色重新更新上下文对话');
|
|
72
|
+
this.chatOption[uid] = {};
|
|
73
|
+
}
|
|
74
|
+
console.log('this.chatOption[uid]', this.chatOption[uid]);
|
|
75
|
+
const { conversationId, text } = systemMessage ? await this.difyChat.sendMessage(content, { ...this.chatOption[uid], systemMessage, timeoutMs: this.config.timeoutMs * 1000 || 80 * 1000, user: uid }) : await this.difyChat.sendMessage(content, { ...this.chatOption[uid], timeoutMs: this.config.timeoutMs * 1000 || 80 * 1000, user: uid });
|
|
76
|
+
if (this.config.filter) {
|
|
77
|
+
const censor = await this.contentCensor.checkText(text);
|
|
78
|
+
if (!censor) {
|
|
79
|
+
console.log(`回复: ${text},包含违规词,已拦截`);
|
|
80
|
+
return [{ type: 1, content: '这个话题不适合讨论,换个话题吧。' }];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (this.config.record) {
|
|
84
|
+
void (0, aichatDb_js_1.addAichatRecord)({ contactId: uid, adminId, input: content, output: text, time: (0, dayjs_1.default)().format('YYYY-MM-DD HH:mm:ss') });
|
|
85
|
+
}
|
|
86
|
+
// 保存对话id 对于同一个用户的对话不更新conversationId
|
|
87
|
+
if (!this.chatOption[uid]?.conversationId) {
|
|
88
|
+
this.chatOption = {
|
|
89
|
+
[uid]: {
|
|
90
|
+
conversationId
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
let replys = [];
|
|
95
|
+
let message;
|
|
96
|
+
if (this.config.showQuestion) {
|
|
97
|
+
message = `${content}${this.eol}-----------${this.eol}` + text.replaceAll('\n', this.eol);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
message = text.replaceAll('\n', this.eol);
|
|
101
|
+
}
|
|
102
|
+
while (message.length > 500) {
|
|
103
|
+
replys.push(message.slice(0, 500));
|
|
104
|
+
message = message.slice(500);
|
|
105
|
+
}
|
|
106
|
+
replys.push(message);
|
|
107
|
+
replys = replys.map(item => {
|
|
108
|
+
return {
|
|
109
|
+
type: 1,
|
|
110
|
+
content: item.trim()
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
return replys;
|
|
114
|
+
}
|
|
115
|
+
catch (e) {
|
|
116
|
+
console.log('dify 请求报错:' + e);
|
|
117
|
+
return [];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
exports.default = DifyAi;
|
|
122
|
+
//# sourceMappingURL=dify.js.map
|
|
@@ -5,10 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const https_proxy_agent_1 = __importDefault(require("https-proxy-agent"));
|
|
7
7
|
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
8
|
-
const chatGPT_js_1 = require("./chatGPT.js");
|
|
8
|
+
const chatGPT_js_1 = require("./sdk/chatGPT.js");
|
|
9
9
|
const aichatDb_js_1 = require("../db/aichatDb.js");
|
|
10
10
|
const aibotk_js_1 = require("../proxy/aibotk.js");
|
|
11
|
-
const contentCensor_js_1 = require("
|
|
11
|
+
const contentCensor_js_1 = require("../lib/contentCensor.js");
|
|
12
12
|
const puppet_type_js_1 = require("../const/puppet-type.js");
|
|
13
13
|
const dayjs_1 = __importDefault(require("dayjs"));
|
|
14
14
|
let chatGPT = null;
|
|
@@ -114,7 +114,6 @@ class OfficialOpenAi {
|
|
|
114
114
|
console.log('看到此消息说明已启用最新版chat gpt 3.5 turbo模型');
|
|
115
115
|
await this.init();
|
|
116
116
|
}
|
|
117
|
-
console.log('this.config.filter', this.config.filter);
|
|
118
117
|
if (this.config.filter) {
|
|
119
118
|
const censor = await this.contentCensor.checkText(content);
|
|
120
119
|
if (!censor) {
|
|
@@ -126,7 +125,7 @@ class OfficialOpenAi {
|
|
|
126
125
|
console.log('带角色重新更新上下文对话');
|
|
127
126
|
this.chatOption[uid] = {};
|
|
128
127
|
}
|
|
129
|
-
const { conversationId, text, id } = await this.chatGPT.sendMessage(content, { ...this.chatOption[uid], systemMessage, timeoutMs: this.config.timeoutMs * 1000 || 80 * 1000 });
|
|
128
|
+
const { conversationId, text, id } = systemMessage ? await this.chatGPT.sendMessage(content, { ...this.chatOption[uid], systemMessage, timeoutMs: this.config.timeoutMs * 1000 || 80 * 1000 }) : await this.chatGPT.sendMessage(content, { ...this.chatOption[uid], timeoutMs: this.config.timeoutMs * 1000 || 80 * 1000 });
|
|
130
129
|
if (this.config.filter) {
|
|
131
130
|
const censor = await this.contentCensor.checkText(text);
|
|
132
131
|
if (!censor) {
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
export const BASE_URL: "https://api.dify.ai/v1";
|
|
2
|
+
export namespace routes {
|
|
3
|
+
namespace application {
|
|
4
|
+
const method: string;
|
|
5
|
+
function url(): string;
|
|
6
|
+
}
|
|
7
|
+
namespace feedback {
|
|
8
|
+
const method_1: string;
|
|
9
|
+
export { method_1 as method };
|
|
10
|
+
export function url_1(messageId: any): string;
|
|
11
|
+
export { url_1 as url };
|
|
12
|
+
}
|
|
13
|
+
namespace createCompletionMessage {
|
|
14
|
+
const method_2: string;
|
|
15
|
+
export { method_2 as method };
|
|
16
|
+
export function url_2(): string;
|
|
17
|
+
export { url_2 as url };
|
|
18
|
+
}
|
|
19
|
+
namespace createChatMessage {
|
|
20
|
+
const method_3: string;
|
|
21
|
+
export { method_3 as method };
|
|
22
|
+
export function url_3(): string;
|
|
23
|
+
export { url_3 as url };
|
|
24
|
+
}
|
|
25
|
+
namespace getConversationMessages {
|
|
26
|
+
const method_4: string;
|
|
27
|
+
export { method_4 as method };
|
|
28
|
+
export function url_4(): string;
|
|
29
|
+
export { url_4 as url };
|
|
30
|
+
}
|
|
31
|
+
namespace getConversations {
|
|
32
|
+
const method_5: string;
|
|
33
|
+
export { method_5 as method };
|
|
34
|
+
export function url_5(): string;
|
|
35
|
+
export { url_5 as url };
|
|
36
|
+
}
|
|
37
|
+
namespace renameConversation {
|
|
38
|
+
const method_6: string;
|
|
39
|
+
export { method_6 as method };
|
|
40
|
+
export function url_6(conversationId: any): string;
|
|
41
|
+
export { url_6 as url };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export class DifyClient {
|
|
45
|
+
constructor({ apiKey, baseUrl, debug, systemMessage }: {
|
|
46
|
+
apiKey: any;
|
|
47
|
+
baseUrl?: string | undefined;
|
|
48
|
+
debug?: boolean | undefined;
|
|
49
|
+
systemMessage?: null | undefined;
|
|
50
|
+
});
|
|
51
|
+
apiKey: any;
|
|
52
|
+
baseUrl: string;
|
|
53
|
+
debug: boolean;
|
|
54
|
+
systemMessage: any;
|
|
55
|
+
updateApiKey(apiKey: any): void;
|
|
56
|
+
sendRequest({ method, endpoint, data, params, stream, timeoutMs }: {
|
|
57
|
+
method: any;
|
|
58
|
+
endpoint: any;
|
|
59
|
+
data: any;
|
|
60
|
+
params: any;
|
|
61
|
+
stream?: boolean | undefined;
|
|
62
|
+
timeoutMs?: number | undefined;
|
|
63
|
+
}): Promise<any>;
|
|
64
|
+
messageFeedback(messageId: any, rating: any, user: any): Promise<any>;
|
|
65
|
+
getApplicationParameters(user: any): Promise<any>;
|
|
66
|
+
}
|
|
67
|
+
export class CompletionClient extends DifyClient {
|
|
68
|
+
createCompletionMessage(inputs: any, query: any, user: any, responseMode: any): Promise<any>;
|
|
69
|
+
}
|
|
70
|
+
export class ChatClient extends DifyClient {
|
|
71
|
+
sendMessage(query: any, { systemMessage, user, responseMode, conversationId, timeoutMs }: {
|
|
72
|
+
systemMessage: any;
|
|
73
|
+
user: any;
|
|
74
|
+
responseMode?: string | undefined;
|
|
75
|
+
conversationId?: null | undefined;
|
|
76
|
+
timeoutMs?: number | undefined;
|
|
77
|
+
}): Promise<{
|
|
78
|
+
text: any;
|
|
79
|
+
conversationId: any;
|
|
80
|
+
id: any;
|
|
81
|
+
}>;
|
|
82
|
+
getConversationMessages(user: any, conversationId?: string, firstId?: null, limit?: null): Promise<any>;
|
|
83
|
+
getConversations(user: any, firstId?: null, limit?: null, pinned?: null): Promise<any>;
|
|
84
|
+
renameConversation(conversationId: any, name: any, user: any): Promise<any>;
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=difyClient.d.ts.map
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ChatClient = exports.CompletionClient = exports.DifyClient = exports.routes = exports.BASE_URL = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
exports.BASE_URL = "https://api.dify.ai/v1";
|
|
9
|
+
exports.routes = {
|
|
10
|
+
application: {
|
|
11
|
+
method: "GET",
|
|
12
|
+
url: () => `/parameters`
|
|
13
|
+
},
|
|
14
|
+
feedback: {
|
|
15
|
+
method: "POST",
|
|
16
|
+
url: (messageId) => `/messages/${messageId}/feedbacks`
|
|
17
|
+
},
|
|
18
|
+
createCompletionMessage: {
|
|
19
|
+
method: "POST",
|
|
20
|
+
url: () => `/completion-messages`
|
|
21
|
+
},
|
|
22
|
+
createChatMessage: {
|
|
23
|
+
method: "POST",
|
|
24
|
+
url: () => `/chat-messages`
|
|
25
|
+
},
|
|
26
|
+
getConversationMessages: {
|
|
27
|
+
method: "GET",
|
|
28
|
+
url: () => "/messages"
|
|
29
|
+
},
|
|
30
|
+
getConversations: {
|
|
31
|
+
method: "GET",
|
|
32
|
+
url: () => "/conversations"
|
|
33
|
+
},
|
|
34
|
+
renameConversation: {
|
|
35
|
+
method: "PATCH",
|
|
36
|
+
url: (conversationId) => `/conversations/${conversationId}`
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
class DifyClient {
|
|
40
|
+
constructor({ apiKey, baseUrl = exports.BASE_URL, debug = false, systemMessage = null }) {
|
|
41
|
+
this.apiKey = apiKey;
|
|
42
|
+
this.baseUrl = baseUrl;
|
|
43
|
+
this.debug = debug;
|
|
44
|
+
this.systemMessage = systemMessage;
|
|
45
|
+
}
|
|
46
|
+
updateApiKey(apiKey) {
|
|
47
|
+
this.apiKey = apiKey;
|
|
48
|
+
}
|
|
49
|
+
async sendRequest({ method, endpoint, data, params, stream = false, timeoutMs = 60 * 1000 }) {
|
|
50
|
+
const headers = {
|
|
51
|
+
"Authorization": `Bearer ${this.apiKey}`,
|
|
52
|
+
"Content-Type": "application/json"
|
|
53
|
+
};
|
|
54
|
+
const url = `${this.baseUrl}${endpoint}`;
|
|
55
|
+
let response;
|
|
56
|
+
if (this.debug) {
|
|
57
|
+
console.log("dify request", url, { data, headers, params });
|
|
58
|
+
}
|
|
59
|
+
if (!stream) {
|
|
60
|
+
response = await (0, axios_1.default)({
|
|
61
|
+
method,
|
|
62
|
+
url,
|
|
63
|
+
data: data || null,
|
|
64
|
+
params: params || null,
|
|
65
|
+
headers,
|
|
66
|
+
timeout: timeoutMs,
|
|
67
|
+
responseType: stream ? "stream" : "json"
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
response = await fetch(url, {
|
|
72
|
+
headers,
|
|
73
|
+
method,
|
|
74
|
+
body: JSON.stringify(data)
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
return response;
|
|
78
|
+
}
|
|
79
|
+
messageFeedback(messageId, rating, user) {
|
|
80
|
+
const data = {
|
|
81
|
+
rating,
|
|
82
|
+
user
|
|
83
|
+
};
|
|
84
|
+
return this.sendRequest({ method: exports.routes.feedback.method, endpoint: exports.routes.feedback.url(messageId), data });
|
|
85
|
+
}
|
|
86
|
+
getApplicationParameters(user) {
|
|
87
|
+
const params = { user };
|
|
88
|
+
return this.sendRequest({ method: exports.routes.application.method, endpoint: exports.routes.application.url(), params });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.DifyClient = DifyClient;
|
|
92
|
+
class CompletionClient extends DifyClient {
|
|
93
|
+
createCompletionMessage(inputs, query, user, responseMode) {
|
|
94
|
+
const data = {
|
|
95
|
+
inputs,
|
|
96
|
+
query,
|
|
97
|
+
responseMode,
|
|
98
|
+
user
|
|
99
|
+
};
|
|
100
|
+
return this.sendRequest({
|
|
101
|
+
method: exports.routes.createCompletionMessage.method,
|
|
102
|
+
endpoint: exports.routes.createCompletionMessage.url(),
|
|
103
|
+
data,
|
|
104
|
+
stream: responseMode === "streaming"
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.CompletionClient = CompletionClient;
|
|
109
|
+
class ChatClient extends DifyClient {
|
|
110
|
+
async sendMessage(query, { systemMessage, user, responseMode = "json", conversationId = null, timeoutMs = 60 * 1000 }) {
|
|
111
|
+
const data = {
|
|
112
|
+
inputs: {},
|
|
113
|
+
query,
|
|
114
|
+
user,
|
|
115
|
+
responseMode
|
|
116
|
+
};
|
|
117
|
+
if (systemMessage || this.systemMessage) {
|
|
118
|
+
data.inputs['systemMessage'] = systemMessage || this.systemMessage;
|
|
119
|
+
}
|
|
120
|
+
if (conversationId)
|
|
121
|
+
data.conversation_id = conversationId;
|
|
122
|
+
console.log('request data', data);
|
|
123
|
+
const res = await this.sendRequest({
|
|
124
|
+
method: exports.routes.createChatMessage.method,
|
|
125
|
+
endpoint: exports.routes.createChatMessage.url(),
|
|
126
|
+
data,
|
|
127
|
+
stream: responseMode === "streaming",
|
|
128
|
+
timeoutMs
|
|
129
|
+
});
|
|
130
|
+
if (res.data.code) {
|
|
131
|
+
if (this.debug) {
|
|
132
|
+
console.log("dify request error", res.data.code, res.data.message);
|
|
133
|
+
}
|
|
134
|
+
return Promise.reject(res.message);
|
|
135
|
+
}
|
|
136
|
+
const response = res.data;
|
|
137
|
+
return {
|
|
138
|
+
text: response.answer,
|
|
139
|
+
conversationId: response.conversation_id,
|
|
140
|
+
id: response.id
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
getConversationMessages(user, conversationId = "", firstId = null, limit = null) {
|
|
144
|
+
const params = { user };
|
|
145
|
+
if (conversationId)
|
|
146
|
+
params.conversation_id = conversationId;
|
|
147
|
+
if (firstId)
|
|
148
|
+
params.first_id = firstId;
|
|
149
|
+
if (limit)
|
|
150
|
+
params.limit = limit;
|
|
151
|
+
return this.sendRequest({
|
|
152
|
+
method: exports.routes.getConversationMessages.method,
|
|
153
|
+
endpoint: exports.routes.getConversationMessages.url(),
|
|
154
|
+
params
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
getConversations(user, firstId = null, limit = null, pinned = null) {
|
|
158
|
+
const params = { user, first_id: firstId, limit, pinned };
|
|
159
|
+
return this.sendRequest({ method: exports.routes.getConversations.method, endpoint: exports.routes.getConversations.url(), params });
|
|
160
|
+
}
|
|
161
|
+
renameConversation(conversationId, name, user) {
|
|
162
|
+
const data = { name, user };
|
|
163
|
+
return this.sendRequest({
|
|
164
|
+
method: exports.routes.renameConversation.method,
|
|
165
|
+
endpoint: exports.routes.renameConversation.url(conversationId),
|
|
166
|
+
data
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
exports.ChatClient = ChatClient;
|
|
171
|
+
//# sourceMappingURL=difyClient.js.map
|
|
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const https_proxy_agent_1 = __importDefault(require("https-proxy-agent"));
|
|
7
7
|
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
8
|
-
const chatGPT_js_1 = require("./chatGPT.js");
|
|
8
|
+
const chatGPT_js_1 = require("./sdk/chatGPT.js");
|
|
9
9
|
const aichatDb_js_1 = require("../db/aichatDb.js");
|
|
10
10
|
const dayjs_1 = __importDefault(require("dayjs"));
|
|
11
11
|
const aibotk_js_1 = require("../proxy/aibotk.js");
|
|
@@ -88,7 +88,7 @@ class UnOfficialOpenAi {
|
|
|
88
88
|
console.log('带角色重新更新上下文对话');
|
|
89
89
|
this.chatOption[uid] = {};
|
|
90
90
|
}
|
|
91
|
-
const { conversationId, text, id } = await this.chatGPT.sendMessage(question, { ...this.chatOption[uid], systemMessage, timeoutMs: this.config.timeoutMs * 1000 || 80 * 1000 });
|
|
91
|
+
const { conversationId, text, id } = systemMessage ? await this.chatGPT.sendMessage(question, { ...this.chatOption[uid], systemMessage, timeoutMs: this.config.timeoutMs * 1000 || 80 * 1000 }) : await this.chatGPT.sendMessage(question, { ...this.chatOption[uid], timeoutMs: this.config.timeoutMs * 1000 || 80 * 1000 });
|
|
92
92
|
if (this.config.record) {
|
|
93
93
|
void (0, aichatDb_js_1.addAichatRecord)({
|
|
94
94
|
contactId: uid,
|
|
@@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ContentCensor = void 0;
|
|
7
7
|
const baidu_aip_sdk_1 = __importDefault(require("baidu-aip-sdk"));
|
|
8
|
-
const wechaty_1 = require("wechaty");
|
|
9
8
|
class ContentCensor {
|
|
10
9
|
constructor(config) {
|
|
11
10
|
this.client = null;
|
|
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.getChatGPTWebReply = exports.resetAll = exports.reset = void 0;
|
|
7
|
-
const unOfficialOpenAi_js_1 = __importDefault(require("../../
|
|
7
|
+
const unOfficialOpenAi_js_1 = __importDefault(require("../../botInstance/unOfficialOpenAi.js"));
|
|
8
8
|
const aibotk_js_1 = require("../aibotk.js");
|
|
9
9
|
let chatGPT = {};
|
|
10
10
|
function reset(adminId) {
|
|
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.getChatGPTReply = exports.resetAll = exports.reset = void 0;
|
|
7
|
-
const officialOpenAi_js_1 = __importDefault(require("../../
|
|
7
|
+
const officialOpenAi_js_1 = __importDefault(require("../../botInstance/officialOpenAi.js"));
|
|
8
8
|
const aibotk_js_1 = require("../aibotk.js");
|
|
9
9
|
let chatGPT = {};
|
|
10
10
|
function reset(adminId) {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function reset(adminId: any): void;
|
|
2
|
+
export function resetAll(): void;
|
|
3
|
+
export function getDifyAiReply(content: any, uid: any, adminId: any, config?: {
|
|
4
|
+
token: string;
|
|
5
|
+
debug: boolean;
|
|
6
|
+
proxyPass: string;
|
|
7
|
+
proxyUrl: string;
|
|
8
|
+
showQuestion: boolean;
|
|
9
|
+
timeoutMs: number;
|
|
10
|
+
model: string;
|
|
11
|
+
systemMessage: string;
|
|
12
|
+
keywordSystemMessages: never[];
|
|
13
|
+
}): Promise<any>;
|
|
14
|
+
//# sourceMappingURL=dify.d.ts.map
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getDifyAiReply = exports.resetAll = exports.reset = void 0;
|
|
7
|
+
const dify_js_1 = __importDefault(require("../../botInstance/dify.js"));
|
|
8
|
+
const aibotk_js_1 = require("../aibotk.js");
|
|
9
|
+
let difyAi = {};
|
|
10
|
+
function reset(adminId) {
|
|
11
|
+
if (!difyAi[adminId])
|
|
12
|
+
return;
|
|
13
|
+
difyAi[adminId].reset();
|
|
14
|
+
difyAi[adminId] = null;
|
|
15
|
+
}
|
|
16
|
+
exports.reset = reset;
|
|
17
|
+
function resetAll() {
|
|
18
|
+
Object.keys(difyAi).forEach(key => {
|
|
19
|
+
if (difyAi[key]) {
|
|
20
|
+
difyAi[key].reset();
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
difyAi = {};
|
|
24
|
+
}
|
|
25
|
+
exports.resetAll = resetAll;
|
|
26
|
+
async function getDifyAiReply(content, uid, adminId, config = { token: "", debug: false, proxyPass: "", proxyUrl: "", showQuestion: false, timeoutMs: 80, model: "", systemMessage: "", keywordSystemMessages: [] }) {
|
|
27
|
+
if (!config.token) {
|
|
28
|
+
console.log('请到智能微秘书平台配置dify的 api秘钥方可使用');
|
|
29
|
+
return [{ type: 1, content: '请到智能微秘书平台配置dify的 api秘钥方可使用' }];
|
|
30
|
+
}
|
|
31
|
+
if (!difyAi[adminId]) {
|
|
32
|
+
difyAi[adminId] = new dify_js_1.default(config);
|
|
33
|
+
}
|
|
34
|
+
let systemMessage = '';
|
|
35
|
+
if (config.keywordSystemMessages && config.keywordSystemMessages.length) {
|
|
36
|
+
const finalSystemMsg = config.keywordSystemMessages.find(item => content.startsWith(item.keyword));
|
|
37
|
+
if (finalSystemMsg) {
|
|
38
|
+
const promotInfo = await (0, aibotk_js_1.getPromotInfo)(finalSystemMsg.promotId);
|
|
39
|
+
console.log(`触发关键词角色功能,使用对应预设角色:${promotInfo.name}`);
|
|
40
|
+
systemMessage = promotInfo.promot;
|
|
41
|
+
content = content.replace(finalSystemMsg.keyword, '');
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return await difyAi[adminId].getReply(content, uid, adminId, systemMessage);
|
|
45
|
+
}
|
|
46
|
+
exports.getDifyAiReply = getDifyAiReply;
|
|
47
|
+
//# sourceMappingURL=dify.js.map
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.dispatchBot = void 0;
|
|
4
4
|
const chatgpt_js_1 = require("./chatgpt.js");
|
|
5
5
|
const chatgpt_web_js_1 = require("./chatgpt-web.js");
|
|
6
|
+
const dify_js_1 = require("./dify.js");
|
|
6
7
|
const gptConfig_js_1 = require("../../db/gptConfig.js");
|
|
7
8
|
const aibotk_js_1 = require("../aibotk.js");
|
|
8
9
|
/**
|
|
@@ -27,6 +28,12 @@ async function dispatchBot({ botType, content, uid, adminId, config }) {
|
|
|
27
28
|
res = await (0, chatgpt_web_js_1.getChatGPTWebReply)(content, uid, adminId, config);
|
|
28
29
|
replys = res;
|
|
29
30
|
break;
|
|
31
|
+
case 8:
|
|
32
|
+
// dify ai
|
|
33
|
+
console.log('进入dify聊天');
|
|
34
|
+
res = await (0, dify_js_1.getDifyAiReply)(content, uid, adminId, config);
|
|
35
|
+
replys = res;
|
|
36
|
+
break;
|
|
30
37
|
default:
|
|
31
38
|
replys = [];
|
|
32
39
|
break;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getDifyReply = exports.reset = void 0;
|
|
7
|
+
const configDb_js_1 = require("../db/configDb.js");
|
|
8
|
+
const dify_js_1 = __importDefault(require("../botInstance/dify.js"));
|
|
9
|
+
let difyAi = null;
|
|
10
|
+
/**
|
|
11
|
+
* 重置实例
|
|
12
|
+
*/
|
|
13
|
+
function reset() {
|
|
14
|
+
if (difyAi) {
|
|
15
|
+
difyAi.reset();
|
|
16
|
+
difyAi = null;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.reset = reset;
|
|
20
|
+
async function getDifyReply(content, uid) {
|
|
21
|
+
const config = await (0, configDb_js_1.allConfig)();
|
|
22
|
+
if (!config.dify_token) {
|
|
23
|
+
console.log('请到智能微秘书平台配置difyAi apikey参数方可使用');
|
|
24
|
+
return [{ type: 1, content: '请到平台配置difyAi apikey参数方可使用' }];
|
|
25
|
+
}
|
|
26
|
+
const chatConfig = {
|
|
27
|
+
token: config.dify_token,
|
|
28
|
+
debug: config.openaiDebug,
|
|
29
|
+
proxyPass: config.dify_baseUrl,
|
|
30
|
+
showQuestion: config.showQuestion,
|
|
31
|
+
timeoutMs: config.openaiTimeout,
|
|
32
|
+
systemMessage: config.openaiSystemMessage,
|
|
33
|
+
filter: config.chatFilter,
|
|
34
|
+
filterConfig: {
|
|
35
|
+
type: 1,
|
|
36
|
+
appId: config.filterAppid,
|
|
37
|
+
apiKey: config.filterApiKey,
|
|
38
|
+
secretKey: config.filterSecretKey
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
if (!difyAi) {
|
|
42
|
+
difyAi = new dify_js_1.default(chatConfig);
|
|
43
|
+
}
|
|
44
|
+
return await difyAi.getReply(content, uid);
|
|
45
|
+
}
|
|
46
|
+
exports.getDifyReply = getDifyReply;
|
|
47
|
+
//# sourceMappingURL=difyAi.js.map
|