doomiaichat 7.1.26 → 7.1.28
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/corzbot.d.ts +8 -0
- package/dist/corzbot.js +53 -7
- package/package.json +1 -1
- package/src/corzbot.ts +52 -9
package/dist/corzbot.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export default class CorzBot extends GptBase {
|
|
|
12
12
|
private workflowid;
|
|
13
13
|
private talkflowid;
|
|
14
14
|
private apiKey;
|
|
15
|
+
private debugDir;
|
|
16
|
+
private debug;
|
|
15
17
|
/**
|
|
16
18
|
*
|
|
17
19
|
* @param apikey 调用AI中台 的key
|
|
@@ -69,6 +71,12 @@ export default class CorzBot extends GptBase {
|
|
|
69
71
|
* @returns
|
|
70
72
|
*/
|
|
71
73
|
private parseDeepThinkingJson;
|
|
74
|
+
/**
|
|
75
|
+
*
|
|
76
|
+
* @param date
|
|
77
|
+
* @returns
|
|
78
|
+
*/
|
|
79
|
+
private formatDateWithMs;
|
|
72
80
|
/**
|
|
73
81
|
* 流式传输聊天请求
|
|
74
82
|
* @param chatText
|
package/dist/corzbot.js
CHANGED
|
@@ -25,6 +25,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
25
25
|
const api_1 = require("@coze/api");
|
|
26
26
|
const gptbase_1 = __importDefault(require("./gptbase"));
|
|
27
27
|
const querystring_1 = require("querystring");
|
|
28
|
+
const fs_1 = require("fs");
|
|
29
|
+
const path_1 = require("path");
|
|
28
30
|
// 定义深度思考的动作标签
|
|
29
31
|
// 这是小鹭里面专用的几个思考动作标签
|
|
30
32
|
const DeepThinkingAction = {
|
|
@@ -55,6 +57,8 @@ class CorzBot extends gptbase_1.default {
|
|
|
55
57
|
this.botid = null; // 智能体id
|
|
56
58
|
this.workflowid = null; // 工作流id
|
|
57
59
|
this.talkflowid = null; // 对话流id
|
|
60
|
+
this.debugDir = '';
|
|
61
|
+
this.debug = false;
|
|
58
62
|
////初始化扣子客户端
|
|
59
63
|
if (this.setting.workflowid)
|
|
60
64
|
this.workflowid = this.setting.workflowid;
|
|
@@ -64,6 +68,12 @@ class CorzBot extends gptbase_1.default {
|
|
|
64
68
|
this.botid = this.setting.botid || this.setting.botID;
|
|
65
69
|
else
|
|
66
70
|
throw new Error('no botid or talkflowid or workflowid setting for coze');
|
|
71
|
+
/// 如果开启debug模式,则创建记录日志的目录,将日志输出到该目录
|
|
72
|
+
if (setting.debug === true) {
|
|
73
|
+
this.debug = true;
|
|
74
|
+
this.debugDir = setting.logPath || (0, path_1.resolve)(process.cwd(), './cozedebug');
|
|
75
|
+
(0, fs_1.mkdirSync)(this.debugDir, { recursive: true });
|
|
76
|
+
}
|
|
67
77
|
this.apiKey = this.setting['apiKey'];
|
|
68
78
|
}
|
|
69
79
|
createClient() {
|
|
@@ -213,22 +223,24 @@ class CorzBot extends gptbase_1.default {
|
|
|
213
223
|
}
|
|
214
224
|
];
|
|
215
225
|
const client = yield this.createClient();
|
|
226
|
+
const debugStream = this.debug ? (0, fs_1.createWriteStream)((0, path_1.join)(this.debugDir, `sync_${this.formatDateWithMs()}.log`), { flags: 'a' }) : null;
|
|
216
227
|
////如果参数中用的是Workflow,则调用对话流来输出结果
|
|
217
228
|
////否则用智能体的对话来输出结果
|
|
218
229
|
const params = yield this.getRequestStream(client, message, callChatOption);
|
|
230
|
+
if (debugStream) {
|
|
231
|
+
debugStream.write(`callChatOption:${JSON.stringify(callChatOption)}\n`);
|
|
232
|
+
debugStream.write(`parameters:${JSON.stringify(params)}\n`);
|
|
233
|
+
}
|
|
219
234
|
const response = this.botid ? yield client.chat.create(params) : yield client.workflows.runs.create(params);
|
|
220
235
|
if (this.workflowid) {
|
|
221
236
|
const workflowResp = response;
|
|
222
237
|
if (workflowResp.msg === 'Success' || (!workflowResp.msg && workflowResp.data)) {
|
|
223
238
|
const resp = workflowResp.data;
|
|
239
|
+
debugStream && debugStream.end(resp);
|
|
224
240
|
return { successed: true, message: [{ role: 'assistant', type: 'answer', content: resp }] };
|
|
225
241
|
}
|
|
242
|
+
debugStream && debugStream.end('workflow run failed');
|
|
226
243
|
console.error('workflow run failed', workflowResp.msg, workflowResp);
|
|
227
|
-
// try {
|
|
228
|
-
// return { successed: true, message: [{ role: 'assistant', type: 'answer', content: JSON.parse(resp).data }] };
|
|
229
|
-
// } catch (error) {
|
|
230
|
-
// return { successed: true, message: [{ role: 'assistant', type: 'answer', content: resp }] };
|
|
231
|
-
// }
|
|
232
244
|
}
|
|
233
245
|
if (!this.workflowid && response.conversation_id && response.id) {
|
|
234
246
|
const ccd = response;
|
|
@@ -242,6 +254,7 @@ class CorzBot extends gptbase_1.default {
|
|
|
242
254
|
return { successed: true, message, usage: chatData.usage, session_id: ccd.conversation_id };
|
|
243
255
|
}
|
|
244
256
|
}
|
|
257
|
+
debugStream && debugStream.end('聊天未完成');
|
|
245
258
|
return { successed: false, error: { message: '聊天未完成' } };
|
|
246
259
|
});
|
|
247
260
|
}
|
|
@@ -298,6 +311,23 @@ class CorzBot extends gptbase_1.default {
|
|
|
298
311
|
}, content
|
|
299
312
|
};
|
|
300
313
|
}
|
|
314
|
+
/**
|
|
315
|
+
*
|
|
316
|
+
* @param date
|
|
317
|
+
* @returns
|
|
318
|
+
*/
|
|
319
|
+
formatDateWithMs(date = new Date()) {
|
|
320
|
+
// 获取各时间部分
|
|
321
|
+
const year = date.getFullYear(); // 年(4位)
|
|
322
|
+
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月(补0,2位)
|
|
323
|
+
const day = String(date.getDate()).padStart(2, '0'); // 日(补0,2位)
|
|
324
|
+
const hours = String(date.getHours()).padStart(2, '0'); // 时(补0,2位)
|
|
325
|
+
const minutes = String(date.getMinutes()).padStart(2, '0'); // 分(补0,2位)
|
|
326
|
+
const seconds = String(date.getSeconds()).padStart(2, '0'); // 秒(补0,2位)
|
|
327
|
+
const milliseconds = String(date.getMilliseconds()).padStart(3, '0'); // 毫秒(补0,3位)
|
|
328
|
+
// 拼接成 yyyymmddhhmmssSSS 格式
|
|
329
|
+
return `${year}${month}${day}${hours}${minutes}${seconds}${milliseconds}`;
|
|
330
|
+
}
|
|
301
331
|
/**
|
|
302
332
|
* 流式传输聊天请求
|
|
303
333
|
* @param chatText
|
|
@@ -325,7 +355,13 @@ class CorzBot extends gptbase_1.default {
|
|
|
325
355
|
////如果参数中用的是Workflow,则调用对话流来输出结果
|
|
326
356
|
////否则用智能体的对话来输出结果
|
|
327
357
|
let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000), index = 0;
|
|
358
|
+
const debugStream = this.debug ? (0, fs_1.createWriteStream)((0, path_1.join)(this.debugDir, `${this.formatDateWithMs()}.log`), { flags: 'a' }) : null;
|
|
328
359
|
const params = yield this.getRequestStream(client, message, callChatOption);
|
|
360
|
+
if (debugStream) {
|
|
361
|
+
debugStream.write(`callChatOption:${JSON.stringify(callChatOption)}\n`);
|
|
362
|
+
debugStream.write(`attach:${attach ? JSON.stringify(attach) : 'null'}\n`);
|
|
363
|
+
debugStream.write(`parameters:${JSON.stringify(params)}\n`);
|
|
364
|
+
}
|
|
329
365
|
const stream = this.botid ? client.chat.stream(params) :
|
|
330
366
|
(this.workflowid ? client.workflows.runs.stream(params) :
|
|
331
367
|
client.workflows.chat.stream(params));
|
|
@@ -337,11 +373,16 @@ class CorzBot extends gptbase_1.default {
|
|
|
337
373
|
_g = false;
|
|
338
374
|
try {
|
|
339
375
|
const part = _c;
|
|
340
|
-
if (part.event === api_1.ChatEventType.ERROR)
|
|
376
|
+
if (part.event === api_1.ChatEventType.ERROR) {
|
|
377
|
+
if (debugStream)
|
|
378
|
+
debugStream.write('chat error:');
|
|
341
379
|
return this.emit('chaterror', { successed: false, error: 'call failed' });
|
|
380
|
+
}
|
|
342
381
|
if (part.event === api_1.ChatEventType.CONVERSATION_MESSAGE_DELTA ||
|
|
343
382
|
part.event === api_1.WorkflowEventType.MESSAGE) {
|
|
344
383
|
let { conversation_id, content_type, type = 'answer', role = 'assistant', content, reasoning_content: reasoning } = part.data;
|
|
384
|
+
if (debugStream)
|
|
385
|
+
debugStream.write(content);
|
|
345
386
|
if (content === 'Default')
|
|
346
387
|
continue;
|
|
347
388
|
if (!content && reasoning) {
|
|
@@ -412,8 +453,11 @@ class CorzBot extends gptbase_1.default {
|
|
|
412
453
|
////在流式传输中,提取相关推荐问题
|
|
413
454
|
if (part.event === api_1.ChatEventType.CONVERSATION_MESSAGE_COMPLETED) {
|
|
414
455
|
const { type, content } = (_e = part.data) !== null && _e !== void 0 ? _e : {};
|
|
415
|
-
if (type === 'follow_up')
|
|
456
|
+
if (type === 'follow_up') {
|
|
457
|
+
if (debugStream)
|
|
458
|
+
debugStream.write('followup:' + content);
|
|
416
459
|
followup.push(content);
|
|
460
|
+
}
|
|
417
461
|
}
|
|
418
462
|
///整个对话结束
|
|
419
463
|
if (part.event === api_1.ChatEventType.CONVERSATION_CHAT_COMPLETED ||
|
|
@@ -422,6 +466,8 @@ class CorzBot extends gptbase_1.default {
|
|
|
422
466
|
let output = { successed: true, cards: cardData.length ? cardData : null, cardfollowup: cardfollowup.filter(x => typeof x === 'string' && x.length > 0), followup, type: 'answer', content_type: 'text', role: api_1.RoleType.Assistant, requestid, segment: null, text: content !== null && content !== void 0 ? content : fullanswer.join(''), index: index++, session_id: conversation_id };
|
|
423
467
|
if (attach)
|
|
424
468
|
output = Object.assign({}, output, attach);
|
|
469
|
+
if (debugStream)
|
|
470
|
+
debugStream.end('chat completed');
|
|
425
471
|
this.emit('chatdone', output);
|
|
426
472
|
}
|
|
427
473
|
}
|
package/package.json
CHANGED
package/src/corzbot.ts
CHANGED
|
@@ -25,7 +25,8 @@ import GptBase from "./gptbase"
|
|
|
25
25
|
import { CorzAuthorization } from './corzauthorization';
|
|
26
26
|
import { parse } from 'querystring'
|
|
27
27
|
import { ApiResult } from './declare';
|
|
28
|
-
|
|
28
|
+
import {createWriteStream,mkdirSync} from 'fs';
|
|
29
|
+
import {resolve,join} from 'path';
|
|
29
30
|
// 定义深度思考的动作标签
|
|
30
31
|
// 这是小鹭里面专用的几个思考动作标签
|
|
31
32
|
const DeepThinkingAction: any = {
|
|
@@ -49,6 +50,8 @@ export default class CorzBot extends GptBase {
|
|
|
49
50
|
private workflowid: string | null = null; // 工作流id
|
|
50
51
|
private talkflowid: string | null = null; // 对话流id
|
|
51
52
|
private apiKey: string;
|
|
53
|
+
private debugDir: string = '';
|
|
54
|
+
private debug: boolean = false;
|
|
52
55
|
/**
|
|
53
56
|
*
|
|
54
57
|
* @param apikey 调用AI中台 的key
|
|
@@ -66,6 +69,12 @@ export default class CorzBot extends GptBase {
|
|
|
66
69
|
this.botid = this.setting.botid || this.setting.botID;
|
|
67
70
|
else
|
|
68
71
|
throw new Error('no botid or talkflowid or workflowid setting for coze');
|
|
72
|
+
/// 如果开启debug模式,则创建记录日志的目录,将日志输出到该目录
|
|
73
|
+
if (setting.debug === true) {
|
|
74
|
+
this.debug = true;
|
|
75
|
+
this.debugDir = setting.logPath || resolve(process.cwd(), './cozedebug')
|
|
76
|
+
mkdirSync(this.debugDir!, { recursive: true });
|
|
77
|
+
}
|
|
69
78
|
this.apiKey = this.setting['apiKey'];
|
|
70
79
|
}
|
|
71
80
|
private async createClient(): Promise<CozeAPI> {
|
|
@@ -192,23 +201,24 @@ export default class CorzBot extends GptBase {
|
|
|
192
201
|
}
|
|
193
202
|
];
|
|
194
203
|
const client = await this.createClient();
|
|
195
|
-
|
|
204
|
+
const debugStream = this.debug ? createWriteStream(join(this.debugDir, `sync_${this.formatDateWithMs()}.log`), { flags: 'a' }) : null;
|
|
196
205
|
////如果参数中用的是Workflow,则调用对话流来输出结果
|
|
197
206
|
////否则用智能体的对话来输出结果
|
|
198
207
|
const params = await this.getRequestStream(client, message, callChatOption);
|
|
208
|
+
if (debugStream) {
|
|
209
|
+
debugStream.write(`callChatOption:${JSON.stringify(callChatOption)}\n`);
|
|
210
|
+
debugStream.write(`parameters:${JSON.stringify(params)}\n`);
|
|
211
|
+
}
|
|
199
212
|
const response = this.botid ? await client.chat.create(params as CreateChatReq) : await client.workflows.runs.create(params as RunWorkflowReq);
|
|
200
213
|
if (this.workflowid) {
|
|
201
214
|
const workflowResp = response as RunWorkflowData;
|
|
202
215
|
if (workflowResp.msg === 'Success' || (!workflowResp.msg && workflowResp.data)){
|
|
203
216
|
const resp = workflowResp.data;
|
|
217
|
+
debugStream && debugStream.end(resp);
|
|
204
218
|
return { successed: true, message: [{ role: 'assistant', type: 'answer', content: resp }] };
|
|
205
219
|
}
|
|
220
|
+
debugStream && debugStream.end('workflow run failed');
|
|
206
221
|
console.error('workflow run failed', workflowResp.msg, workflowResp)
|
|
207
|
-
// try {
|
|
208
|
-
// return { successed: true, message: [{ role: 'assistant', type: 'answer', content: JSON.parse(resp).data }] };
|
|
209
|
-
// } catch (error) {
|
|
210
|
-
// return { successed: true, message: [{ role: 'assistant', type: 'answer', content: resp }] };
|
|
211
|
-
// }
|
|
212
222
|
}
|
|
213
223
|
if (!this.workflowid && (response as CreateChatData).conversation_id && (response as CreateChatData).id) {
|
|
214
224
|
const ccd = response as CreateChatData;
|
|
@@ -222,6 +232,7 @@ export default class CorzBot extends GptBase {
|
|
|
222
232
|
return { successed: true, message, usage: chatData.usage, session_id: ccd.conversation_id };
|
|
223
233
|
}
|
|
224
234
|
}
|
|
235
|
+
debugStream && debugStream.end('聊天未完成');
|
|
225
236
|
return { successed: false, error: { message: '聊天未完成' } };
|
|
226
237
|
|
|
227
238
|
}
|
|
@@ -276,6 +287,24 @@ export default class CorzBot extends GptBase {
|
|
|
276
287
|
};
|
|
277
288
|
|
|
278
289
|
}
|
|
290
|
+
/**
|
|
291
|
+
*
|
|
292
|
+
* @param date
|
|
293
|
+
* @returns
|
|
294
|
+
*/
|
|
295
|
+
private formatDateWithMs(date = new Date()) {
|
|
296
|
+
// 获取各时间部分
|
|
297
|
+
const year = date.getFullYear(); // 年(4位)
|
|
298
|
+
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月(补0,2位)
|
|
299
|
+
const day = String(date.getDate()).padStart(2, '0'); // 日(补0,2位)
|
|
300
|
+
const hours = String(date.getHours()).padStart(2, '0'); // 时(补0,2位)
|
|
301
|
+
const minutes = String(date.getMinutes()).padStart(2, '0'); // 分(补0,2位)
|
|
302
|
+
const seconds = String(date.getSeconds()).padStart(2, '0'); // 秒(补0,2位)
|
|
303
|
+
const milliseconds = String(date.getMilliseconds()).padStart(3, '0'); // 毫秒(补0,3位)
|
|
304
|
+
|
|
305
|
+
// 拼接成 yyyymmddhhmmssSSS 格式
|
|
306
|
+
return `${year}${month}${day}${hours}${minutes}${seconds}${milliseconds}`;
|
|
307
|
+
}
|
|
279
308
|
/**
|
|
280
309
|
* 流式传输聊天请求
|
|
281
310
|
* @param chatText
|
|
@@ -298,18 +327,28 @@ export default class CorzBot extends GptBase {
|
|
|
298
327
|
////如果参数中用的是Workflow,则调用对话流来输出结果
|
|
299
328
|
////否则用智能体的对话来输出结果
|
|
300
329
|
let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000), index = 0;
|
|
330
|
+
const debugStream = this.debug ? createWriteStream(join(this.debugDir, `${this.formatDateWithMs()}.log`), { flags: 'a' }) : null;
|
|
301
331
|
const params = await this.getRequestStream(client, message, callChatOption);
|
|
332
|
+
if (debugStream) {
|
|
333
|
+
debugStream.write(`callChatOption:${JSON.stringify(callChatOption)}\n`);
|
|
334
|
+
debugStream.write(`attach:${attach ? JSON.stringify(attach) : 'null'}\n`);
|
|
335
|
+
debugStream.write(`parameters:${JSON.stringify(params)}\n`);
|
|
336
|
+
}
|
|
302
337
|
const stream = this.botid ? client.chat.stream(params as StreamChatReq) :
|
|
303
338
|
(this.workflowid ? client.workflows.runs.stream(params as RunWorkflowReq) :
|
|
304
339
|
client.workflows.chat.stream(params as ChatWorkflowReq));
|
|
305
340
|
let deltaindex = 0, fullanswer: string[] = [],cardfollowup: string[] = [], followup: string[] = [], cardData: CardType[] = [];
|
|
306
341
|
let deepThinking = '', thinkingStatus = DeepThinkingStatus.None, cardResource = '', preresponseText=''; // 是否在深度思考中
|
|
307
342
|
for await (const part of stream) {
|
|
308
|
-
if (part.event === ChatEventType.ERROR)
|
|
343
|
+
if (part.event === ChatEventType.ERROR) {
|
|
344
|
+
if (debugStream) debugStream.write('chat error:');
|
|
345
|
+
return this.emit('chaterror', { successed: false, error: 'call failed' });
|
|
346
|
+
}
|
|
309
347
|
if (part.event === ChatEventType.CONVERSATION_MESSAGE_DELTA ||
|
|
310
348
|
part.event === WorkflowEventType.MESSAGE
|
|
311
349
|
) {
|
|
312
350
|
let { conversation_id, content_type, type = 'answer', role = 'assistant', content, reasoning_content: reasoning } = part.data as any;
|
|
351
|
+
if (debugStream) debugStream.write(content);
|
|
313
352
|
if (content === 'Default') continue;
|
|
314
353
|
if (!content && reasoning) {
|
|
315
354
|
this.emit('chatthinking', { text: reasoning, completed: false, action: 'deep-thinking' });
|
|
@@ -374,7 +413,10 @@ export default class CorzBot extends GptBase {
|
|
|
374
413
|
////在流式传输中,提取相关推荐问题
|
|
375
414
|
if (part.event === ChatEventType.CONVERSATION_MESSAGE_COMPLETED) {
|
|
376
415
|
const { type, content } = part.data ?? {};
|
|
377
|
-
if (type === 'follow_up')
|
|
416
|
+
if (type === 'follow_up') {
|
|
417
|
+
if (debugStream) debugStream.write('followup:' + content);
|
|
418
|
+
followup.push(content);
|
|
419
|
+
}
|
|
378
420
|
}
|
|
379
421
|
///整个对话结束
|
|
380
422
|
if (part.event === ChatEventType.CONVERSATION_CHAT_COMPLETED ||
|
|
@@ -383,6 +425,7 @@ export default class CorzBot extends GptBase {
|
|
|
383
425
|
const { conversation_id, content } = (part.data) ?? {} as any;
|
|
384
426
|
let output = { successed: true, cards: cardData.length ? cardData : null, cardfollowup: cardfollowup.filter(x=>typeof x === 'string' && x.length > 0), followup, type: 'answer', content_type: 'text', role: RoleType.Assistant, requestid, segment: null, text: content ?? fullanswer.join(''), index: index++, session_id: conversation_id };
|
|
385
427
|
if (attach) output = Object.assign({}, output, attach);
|
|
428
|
+
if (debugStream) debugStream.end('chat completed');
|
|
386
429
|
this.emit('chatdone', output)
|
|
387
430
|
}
|
|
388
431
|
}
|