doomiaichat 7.1.25 → 7.1.27

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 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
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() {
@@ -135,7 +145,7 @@ class CorzBot extends gptbase_1.default {
135
145
  * @param _axiosOption
136
146
  */
137
147
  getRequestStream(client, message, callChatOption = {}) {
138
- var _a, _b, _c;
148
+ var _a, _b;
139
149
  return __awaiter(this, void 0, void 0, function* () {
140
150
  //简单的对话请求
141
151
  if (this.botid) {
@@ -155,7 +165,8 @@ class CorzBot extends gptbase_1.default {
155
165
  workflow_id: this.workflowid,
156
166
  is_async: false,
157
167
  // parameters: Object.assign({ input: message[0]?.content }, this.setting.customVariables || {}, callChatOption.customVariables || {}),
158
- parameters: Object.assign({ request_src: 1 }, callChatOption.parameters || {}, { input: (_b = message[0]) === null || _b === void 0 ? void 0 : _b.content })
168
+ // parameters: Object.assign({ request_src: 1 }, callChatOption.parameters || {}, { input: message[0]?.content })
169
+ parameters: Object.assign({ request_src: 1 }, callChatOption.parameters || {}, { input: message })
159
170
  };
160
171
  if (callChatOption.userid || callChatOption.cozeUserID)
161
172
  worflowreq.ext = Object.assign({}, worflowreq.ext || {}, { user_id: callChatOption.userid || callChatOption.cozeUserID });
@@ -163,11 +174,10 @@ class CorzBot extends gptbase_1.default {
163
174
  }
164
175
  const worflowreq = {
165
176
  additional_messages: message,
166
- ext: callChatOption.ext,
177
+ ext: { user_id: '12345678' },
167
178
  workflow_id: this.talkflowid,
168
- conversation_id: (_c = callChatOption.session_id) !== null && _c !== void 0 ? _c : yield this.createCoversation(client),
179
+ conversation_id: (_b = callChatOption.session_id) !== null && _b !== void 0 ? _b : yield this.createCoversation(client),
169
180
  parameters: Object.assign({ request_src: 1 }, callChatOption.parameters || {}) //Object.assign({}, this.setting.customVariables || {}, callChatOption.customVariables || {}),
170
- //parameters: { input: message[0]?.content }
171
181
  };
172
182
  if (callChatOption.userid || callChatOption.cozeUserID)
173
183
  worflowreq.ext = Object.assign({}, worflowreq.ext || {}, { user_id: callChatOption.userid || callChatOption.cozeUserID });
@@ -213,22 +223,25 @@ class CorzBot extends gptbase_1.default {
213
223
  }
214
224
  ];
215
225
  const client = yield this.createClient();
226
+ const requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
227
+ const debugStream = this.debug ? (0, fs_1.createWriteStream)((0, path_1.join)(this.debugDir, `sync_${requestid}.log`), { flags: 'a' }) : null;
216
228
  ////如果参数中用的是Workflow,则调用对话流来输出结果
217
229
  ////否则用智能体的对话来输出结果
218
230
  const params = yield this.getRequestStream(client, message, callChatOption);
231
+ if (debugStream) {
232
+ debugStream.write(`callChatOption:${JSON.stringify(callChatOption)}\n`);
233
+ debugStream.write(`parameters:${JSON.stringify(params)}\n`);
234
+ }
219
235
  const response = this.botid ? yield client.chat.create(params) : yield client.workflows.runs.create(params);
220
236
  if (this.workflowid) {
221
237
  const workflowResp = response;
222
238
  if (workflowResp.msg === 'Success' || (!workflowResp.msg && workflowResp.data)) {
223
239
  const resp = workflowResp.data;
240
+ debugStream && debugStream.end(resp);
224
241
  return { successed: true, message: [{ role: 'assistant', type: 'answer', content: resp }] };
225
242
  }
243
+ debugStream && debugStream.end('workflow run failed');
226
244
  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
245
  }
233
246
  if (!this.workflowid && response.conversation_id && response.id) {
234
247
  const ccd = response;
@@ -242,6 +255,7 @@ class CorzBot extends gptbase_1.default {
242
255
  return { successed: true, message, usage: chatData.usage, session_id: ccd.conversation_id };
243
256
  }
244
257
  }
258
+ debugStream && debugStream.end('聊天未完成');
245
259
  return { successed: false, error: { message: '聊天未完成' } };
246
260
  });
247
261
  }
@@ -325,7 +339,13 @@ class CorzBot extends gptbase_1.default {
325
339
  ////如果参数中用的是Workflow,则调用对话流来输出结果
326
340
  ////否则用智能体的对话来输出结果
327
341
  let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000), index = 0;
342
+ const debugStream = this.debug ? (0, fs_1.createWriteStream)((0, path_1.join)(this.debugDir, `${requestid}.log`), { flags: 'a' }) : null;
328
343
  const params = yield this.getRequestStream(client, message, callChatOption);
344
+ if (debugStream) {
345
+ debugStream.write(`callChatOption:${JSON.stringify(callChatOption)}\n`);
346
+ debugStream.write(`attach:${attach ? JSON.stringify(attach) : 'null'}\n`);
347
+ debugStream.write(`parameters:${JSON.stringify(params)}\n`);
348
+ }
329
349
  const stream = this.botid ? client.chat.stream(params) :
330
350
  (this.workflowid ? client.workflows.runs.stream(params) :
331
351
  client.workflows.chat.stream(params));
@@ -337,11 +357,16 @@ class CorzBot extends gptbase_1.default {
337
357
  _g = false;
338
358
  try {
339
359
  const part = _c;
340
- if (part.event === api_1.ChatEventType.ERROR)
360
+ if (part.event === api_1.ChatEventType.ERROR) {
361
+ if (debugStream)
362
+ debugStream.write('chat error:');
341
363
  return this.emit('chaterror', { successed: false, error: 'call failed' });
364
+ }
342
365
  if (part.event === api_1.ChatEventType.CONVERSATION_MESSAGE_DELTA ||
343
366
  part.event === api_1.WorkflowEventType.MESSAGE) {
344
367
  let { conversation_id, content_type, type = 'answer', role = 'assistant', content, reasoning_content: reasoning } = part.data;
368
+ if (debugStream)
369
+ debugStream.write(content);
345
370
  if (content === 'Default')
346
371
  continue;
347
372
  if (!content && reasoning) {
@@ -412,8 +437,11 @@ class CorzBot extends gptbase_1.default {
412
437
  ////在流式传输中,提取相关推荐问题
413
438
  if (part.event === api_1.ChatEventType.CONVERSATION_MESSAGE_COMPLETED) {
414
439
  const { type, content } = (_e = part.data) !== null && _e !== void 0 ? _e : {};
415
- if (type === 'follow_up')
440
+ if (type === 'follow_up') {
441
+ if (debugStream)
442
+ debugStream.write('followup:' + content);
416
443
  followup.push(content);
444
+ }
417
445
  }
418
446
  ///整个对话结束
419
447
  if (part.event === api_1.ChatEventType.CONVERSATION_CHAT_COMPLETED ||
@@ -422,6 +450,8 @@ class CorzBot extends gptbase_1.default {
422
450
  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
451
  if (attach)
424
452
  output = Object.assign({}, output, attach);
453
+ if (debugStream)
454
+ debugStream.end('chat completed');
425
455
  this.emit('chatdone', output);
426
456
  }
427
457
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doomiaichat",
3
- "version": "7.1.25",
3
+ "version": "7.1.27",
4
4
  "description": "Doomisoft OpenAI",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
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> {
@@ -143,18 +152,18 @@ export default class CorzBot extends GptBase {
143
152
  workflow_id: this.workflowid,//callChatOption.workflowid,
144
153
  is_async: false,
145
154
  // parameters: Object.assign({ input: message[0]?.content }, this.setting.customVariables || {}, callChatOption.customVariables || {}),
146
- parameters: Object.assign({ request_src: 1 }, callChatOption.parameters || {}, { input: message[0]?.content })
155
+ // parameters: Object.assign({ request_src: 1 }, callChatOption.parameters || {}, { input: message[0]?.content })
156
+ parameters: Object.assign({ request_src: 1 }, callChatOption.parameters || {}, { input: message})
147
157
  }
148
158
  if (callChatOption.userid || callChatOption.cozeUserID) worflowreq.ext = Object.assign({}, worflowreq.ext || {}, { user_id: callChatOption.userid || callChatOption.cozeUserID });
149
159
  return worflowreq as T;
150
160
  }
151
161
  const worflowreq: ChatWorkflowReq = {
152
162
  additional_messages: message,
153
- ext: callChatOption.ext,
163
+ ext: {user_id:'12345678'},
154
164
  workflow_id: this.talkflowid!,//callChatOption.workflowid,
155
165
  conversation_id: callChatOption.session_id ?? await this.createCoversation(client),
156
166
  parameters: Object.assign({ request_src: 1 }, callChatOption.parameters || {}) //Object.assign({}, this.setting.customVariables || {}, callChatOption.customVariables || {}),
157
- //parameters: { input: message[0]?.content }
158
167
  }
159
168
  if (callChatOption.userid || callChatOption.cozeUserID) worflowreq.ext = Object.assign({}, worflowreq.ext||{}, { user_id: callChatOption.userid || callChatOption.cozeUserID });
160
169
  return worflowreq as T;
@@ -192,23 +201,25 @@ export default class CorzBot extends GptBase {
192
201
  }
193
202
  ];
194
203
  const client = await this.createClient();
195
-
204
+ const requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
205
+ const debugStream = this.debug ? createWriteStream(join(this.debugDir, `sync_${requestid}.log`), { flags: 'a' }) : null;
196
206
  ////如果参数中用的是Workflow,则调用对话流来输出结果
197
207
  ////否则用智能体的对话来输出结果
198
208
  const params = await this.getRequestStream(client, message, callChatOption);
209
+ if (debugStream) {
210
+ debugStream.write(`callChatOption:${JSON.stringify(callChatOption)}\n`);
211
+ debugStream.write(`parameters:${JSON.stringify(params)}\n`);
212
+ }
199
213
  const response = this.botid ? await client.chat.create(params as CreateChatReq) : await client.workflows.runs.create(params as RunWorkflowReq);
200
214
  if (this.workflowid) {
201
215
  const workflowResp = response as RunWorkflowData;
202
216
  if (workflowResp.msg === 'Success' || (!workflowResp.msg && workflowResp.data)){
203
217
  const resp = workflowResp.data;
218
+ debugStream && debugStream.end(resp);
204
219
  return { successed: true, message: [{ role: 'assistant', type: 'answer', content: resp }] };
205
220
  }
221
+ debugStream && debugStream.end('workflow run failed');
206
222
  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
223
  }
213
224
  if (!this.workflowid && (response as CreateChatData).conversation_id && (response as CreateChatData).id) {
214
225
  const ccd = response as CreateChatData;
@@ -222,6 +233,7 @@ export default class CorzBot extends GptBase {
222
233
  return { successed: true, message, usage: chatData.usage, session_id: ccd.conversation_id };
223
234
  }
224
235
  }
236
+ debugStream && debugStream.end('聊天未完成');
225
237
  return { successed: false, error: { message: '聊天未完成' } };
226
238
 
227
239
  }
@@ -298,18 +310,28 @@ export default class CorzBot extends GptBase {
298
310
  ////如果参数中用的是Workflow,则调用对话流来输出结果
299
311
  ////否则用智能体的对话来输出结果
300
312
  let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000), index = 0;
313
+ const debugStream = this.debug ? createWriteStream(join(this.debugDir,`${requestid}.log`), { flags: 'a' }) : null;
301
314
  const params = await this.getRequestStream(client, message, callChatOption);
315
+ if (debugStream) {
316
+ debugStream.write(`callChatOption:${JSON.stringify(callChatOption)}\n`);
317
+ debugStream.write(`attach:${attach ? JSON.stringify(attach) : 'null'}\n`);
318
+ debugStream.write(`parameters:${JSON.stringify(params)}\n`);
319
+ }
302
320
  const stream = this.botid ? client.chat.stream(params as StreamChatReq) :
303
321
  (this.workflowid ? client.workflows.runs.stream(params as RunWorkflowReq) :
304
322
  client.workflows.chat.stream(params as ChatWorkflowReq));
305
323
  let deltaindex = 0, fullanswer: string[] = [],cardfollowup: string[] = [], followup: string[] = [], cardData: CardType[] = [];
306
324
  let deepThinking = '', thinkingStatus = DeepThinkingStatus.None, cardResource = '', preresponseText=''; // 是否在深度思考中
307
325
  for await (const part of stream) {
308
- if (part.event === ChatEventType.ERROR) return this.emit('chaterror', { successed: false, error: 'call failed' });
326
+ if (part.event === ChatEventType.ERROR) {
327
+ if (debugStream) debugStream.write('chat error:');
328
+ return this.emit('chaterror', { successed: false, error: 'call failed' });
329
+ }
309
330
  if (part.event === ChatEventType.CONVERSATION_MESSAGE_DELTA ||
310
331
  part.event === WorkflowEventType.MESSAGE
311
332
  ) {
312
333
  let { conversation_id, content_type, type = 'answer', role = 'assistant', content, reasoning_content: reasoning } = part.data as any;
334
+ if (debugStream) debugStream.write(content);
313
335
  if (content === 'Default') continue;
314
336
  if (!content && reasoning) {
315
337
  this.emit('chatthinking', { text: reasoning, completed: false, action: 'deep-thinking' });
@@ -374,7 +396,10 @@ export default class CorzBot extends GptBase {
374
396
  ////在流式传输中,提取相关推荐问题
375
397
  if (part.event === ChatEventType.CONVERSATION_MESSAGE_COMPLETED) {
376
398
  const { type, content } = part.data ?? {};
377
- if (type === 'follow_up') followup.push(content);
399
+ if (type === 'follow_up') {
400
+ if (debugStream) debugStream.write('followup:' + content);
401
+ followup.push(content);
402
+ }
378
403
  }
379
404
  ///整个对话结束
380
405
  if (part.event === ChatEventType.CONVERSATION_CHAT_COMPLETED ||
@@ -383,6 +408,7 @@ export default class CorzBot extends GptBase {
383
408
  const { conversation_id, content } = (part.data) ?? {} as any;
384
409
  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
410
  if (attach) output = Object.assign({}, output, attach);
411
+ if (debugStream) debugStream.end('chat completed');
386
412
  this.emit('chatdone', output)
387
413
  }
388
414
  }