doomiaichat 7.1.5 → 7.1.6

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.js CHANGED
@@ -24,12 +24,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
24
24
  */
25
25
  const api_1 = require("@coze/api");
26
26
  const gptbase_1 = __importDefault(require("./gptbase"));
27
+ const querystring_1 = require("querystring");
28
+ // 定义深度思考的动作
29
+ var DeepThinkingAction;
30
+ (function (DeepThinkingAction) {
31
+ DeepThinkingAction["process"] = "process_msg";
32
+ DeepThinkingAction["content"] = "reasoning_content";
33
+ DeepThinkingAction["card"] = "card_resource";
34
+ })(DeepThinkingAction || (DeepThinkingAction = {}));
35
+ // 定义深度思考的状态
27
36
  var DeepThinkingStatus;
28
37
  (function (DeepThinkingStatus) {
29
38
  DeepThinkingStatus[DeepThinkingStatus["None"] = 0] = "None";
30
39
  DeepThinkingStatus[DeepThinkingStatus["Thinking"] = 1] = "Thinking";
31
40
  DeepThinkingStatus[DeepThinkingStatus["ReasonOutput"] = 2] = "ReasonOutput";
32
- DeepThinkingStatus[DeepThinkingStatus["ThinkingOver"] = 3] = "ThinkingOver";
41
+ DeepThinkingStatus[DeepThinkingStatus["CardResource"] = 3] = "CardResource";
42
+ DeepThinkingStatus[DeepThinkingStatus["ThinkingOver"] = 4] = "ThinkingOver";
33
43
  })(DeepThinkingStatus || (DeepThinkingStatus = {}));
34
44
  class CorzBot extends gptbase_1.default {
35
45
  // protected client: CozeAPI;
@@ -158,28 +168,40 @@ class CorzBot extends gptbase_1.default {
158
168
  * @returns
159
169
  */
160
170
  parseDeepThinkingJson(content, status) {
171
+ var _a;
161
172
  const thinkingEndIndex = content.indexOf("\"}");
162
- const thinkingStartIndex = status === DeepThinkingStatus.Thinking ? content.indexOf("{\"process_msg") : content.indexOf("{\"reasoning_content");
173
+ // const thinkingStartIndex = status === DeepThinkingStatus.Thinking ? content.indexOf("{\"process_msg") :
174
+ // (status===DeepThinkingStatus.ReasonOutput ? content.indexOf("{\"reasoning_content") : content.indexOf("{\"card_resource")) ;
175
+ const thinkingStartIndex = (_a = [content.indexOf("{\"process_msg"),
176
+ content.indexOf("{\"reasoning_content"),
177
+ content.indexOf("{\"card_resource")].find(x => x >= 0)) !== null && _a !== void 0 ? _a : -1;
178
+ // 如果没有找到相关的JSON内容,则直接返回
179
+ if (thinkingStartIndex < 0)
180
+ return { successed: true, content, status: DeepThinkingStatus.ThinkingOver };
163
181
  let jsonStr = null;
164
182
  try {
165
183
  jsonStr = content.substring(thinkingStartIndex, thinkingEndIndex >= 0 ? thinkingEndIndex : undefined) + "\"}";
166
184
  // 转换JSON的时候需要将非法字符过滤掉
167
185
  const thinkingObject = JSON.parse(jsonStr.replace(/[\x00-\x1F\x7F]/g, ''));
186
+ const thinkingAction = Object.keys(thinkingObject)[0];
168
187
  // 需要将内容的原文返回
169
188
  const originalContent = jsonStr.split('":"')[1];
170
189
  // 如果正确的解析JSON,并且当前有包含JSON结束的字符,则把当前思考的JSON内容替换掉
171
190
  if (thinkingEndIndex >= 0)
172
191
  content = content.substring(thinkingEndIndex + 2);
173
- // 判断整个思考过程是否结束
174
- if (status === DeepThinkingStatus.ReasonOutput && thinkingEndIndex >= 0)
192
+ // 如果正常输出了卡片资源,并且json结束 card_resource 只有一个,所以一旦结束,则整个思考过程完毕
193
+ if (status === DeepThinkingStatus.CardResource && thinkingEndIndex >= 0)
175
194
  status = DeepThinkingStatus.ThinkingOver;
195
+ // 判断整个思考过程是否到达等待卡片资源(有可能智能体输出不了卡片资源) reasoning_content 只有一个,所以一旦结束,则进入下一个阶段 card_resource
196
+ if (status === DeepThinkingStatus.ReasonOutput && thinkingEndIndex >= 0)
197
+ status = DeepThinkingStatus.CardResource;
176
198
  // 判断当前是否从前期思考切换到思考内容输出
177
- if (status === DeepThinkingStatus.Thinking && content.indexOf("{\"reasoning_content") >= 0)
199
+ if (status === DeepThinkingStatus.Thinking && thinkingAction === DeepThinkingAction.content)
178
200
  status = DeepThinkingStatus.ReasonOutput;
201
+ //if (status === DeepThinkingStatus.Thinking && content.indexOf("{\"reasoning_content") >= 0) status=DeepThinkingStatus.ReasonOutput;
179
202
  //const removeOutputContent = content.replace(originalContent!.substring(1, originalContent!.length - 2),"")
180
- let thinkingContent = originalContent === null || originalContent === void 0 ? void 0 : originalContent.substring(0, originalContent.length - 2); //.replace(lastContent, '');
203
+ let thinkingContent = originalContent === null || originalContent === void 0 ? void 0 : originalContent.substring(0, originalContent.length - 2);
181
204
  const outputLength = (thinkingContent === null || thinkingContent === void 0 ? void 0 : thinkingContent.length) || 0;
182
- const thinkingAction = Object.keys(thinkingObject)[0];
183
205
  // 用lastThinkingMessage来记录上一次的思考内容,避免重复输出内容,导致的网络传输内容过多
184
206
  if (this.lastThinkingMessage.action === thinkingAction && this.lastThinkingMessage.textposition) {
185
207
  thinkingContent = thinkingContent.substring(this.lastThinkingMessage.textposition);
@@ -187,11 +209,17 @@ class CorzBot extends gptbase_1.default {
187
209
  this.lastThinkingMessage = thinkingEndIndex >= 0 ? { action: thinkingAction, textposition: 0 } : { action: thinkingAction, textposition: outputLength };
188
210
  return { successed: true, thinking: {
189
211
  action: thinkingAction,
190
- text: thinkingContent //Object.values(thinkingObject)[0]
212
+ text: thinkingContent,
213
+ completed: thinkingEndIndex >= 0
191
214
  }, content, status };
192
215
  }
193
216
  catch (error) {
194
- console.error('解析JSON出错了:' + jsonStr);
217
+ // 如果在content之后发生了错误,可能该智能体输出不了卡片类型的数据,则输出的内容已经是正文了
218
+ if (status === DeepThinkingStatus.CardResource) {
219
+ status = DeepThinkingStatus.ThinkingOver;
220
+ return { successed: true, content, status };
221
+ }
222
+ console.error('解析JSON出错了:', jsonStr, status);
195
223
  // 当前解析出错,等待下一次解析
196
224
  return { successed: false, content, status };
197
225
  }
@@ -206,6 +234,7 @@ class CorzBot extends gptbase_1.default {
206
234
  */
207
235
  chatRequestInStream(message, callChatOption = {}, attach, _axiosOption) {
208
236
  var _a, e_1, _b, _c;
237
+ var _d, _e;
209
238
  return __awaiter(this, void 0, void 0, function* () {
210
239
  if (!message)
211
240
  this.emit('chaterror', { successed: false, error: 'no message in chat' });
@@ -225,11 +254,11 @@ class CorzBot extends gptbase_1.default {
225
254
  const params = yield this.getRequestStream(client, message, callChatOption);
226
255
  const stream = !callChatOption.workflowid ? client.chat.stream(params) : client.workflows.chat.stream(params);
227
256
  let deltaindex = 0, fullanswer = [], followup = [];
228
- let deepThinking = '', thinkingStatus = DeepThinkingStatus.None; // 是否在深度思考中
257
+ let deepThinking = '', thinkingStatus = DeepThinkingStatus.None, cardResource = ''; // 是否在深度思考中
229
258
  try {
230
- for (var _d = true, stream_1 = __asyncValues(stream), stream_1_1; stream_1_1 = yield stream_1.next(), _a = stream_1_1.done, !_a;) {
259
+ for (var _f = true, stream_1 = __asyncValues(stream), stream_1_1; stream_1_1 = yield stream_1.next(), _a = stream_1_1.done, !_a;) {
231
260
  _c = stream_1_1.value;
232
- _d = false;
261
+ _f = false;
233
262
  try {
234
263
  const part = _c;
235
264
  if (part.event === api_1.ChatEventType.ERROR)
@@ -242,17 +271,30 @@ class CorzBot extends gptbase_1.default {
242
271
  deltaindex++;
243
272
  }
244
273
  // 如果在深度思考中,则不输出消息
245
- if (thinkingStatus !== DeepThinkingStatus.None &&
246
- thinkingStatus !== DeepThinkingStatus.ThinkingOver) {
274
+ if (thinkingStatus !== DeepThinkingStatus.None
275
+ && thinkingStatus !== DeepThinkingStatus.ThinkingOver) {
247
276
  deepThinking += content;
248
277
  const result = this.parseDeepThinkingJson(deepThinking, thinkingStatus);
249
278
  if (result.successed) {
250
- deepThinking = result.content;
251
- this.emit('chatthinking', result.thinking);
279
+ // 如果不是卡片资源,则发送thinking事件,并更新thinkingStatus
280
+ if (thinkingStatus !== DeepThinkingStatus.CardResource) {
281
+ deepThinking = result.content;
282
+ this.emit('chatthinking', result.thinking);
283
+ }
284
+ else if (thinkingStatus === DeepThinkingStatus.CardResource) {
285
+ cardResource += (_d = result.thinking) === null || _d === void 0 ? void 0 : _d.text;
286
+ // 卡片流结束,解析卡片资源数据
287
+ if ((_e = result.thinking) === null || _e === void 0 ? void 0 : _e.completed) {
288
+ const cardData = cardResource.replace(/[\x00-\x1F\x7F]/g, '').split('|').map((item) => (0, querystring_1.parse)(item));
289
+ // 将卡片资源返回给客户端
290
+ this.emit('chatcard', cardData);
291
+ }
292
+ }
252
293
  thinkingStatus = result.status;
253
294
  }
254
295
  if (thinkingStatus != DeepThinkingStatus.ThinkingOver)
255
296
  continue;
297
+ // thinkingStatus = DeepThinkingStatus.None;
256
298
  // 将排除了thinking之后的消息内容要带下去成为正式的输出内容
257
299
  content = deepThinking;
258
300
  }
@@ -278,14 +320,14 @@ class CorzBot extends gptbase_1.default {
278
320
  }
279
321
  }
280
322
  finally {
281
- _d = true;
323
+ _f = true;
282
324
  }
283
325
  }
284
326
  }
285
327
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
286
328
  finally {
287
329
  try {
288
- if (!_d && !_a && (_b = stream_1.return)) yield _b.call(stream_1);
330
+ if (!_f && !_a && (_b = stream_1.return)) yield _b.call(stream_1);
289
331
  }
290
332
  finally { if (e_1) throw e_1.error; }
291
333
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doomiaichat",
3
- "version": "7.1.5",
3
+ "version": "7.1.6",
4
4
  "description": "Doomisoft OpenAI",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/corzbot.ts CHANGED
@@ -12,12 +12,24 @@ import { CozeAPI,
12
12
  ChatStatus} from '@coze/api';
13
13
  import GptBase from "./gptbase"
14
14
  import { CorzAuthorization } from './corzauthorization';
15
+ import { parse } from 'querystring'
16
+
17
+ // 定义深度思考的动作
18
+ enum DeepThinkingAction {
19
+ process = 'process_msg',
20
+ content ='reasoning_content',
21
+ card = 'card_resource'
22
+ }
23
+ // 定义深度思考的状态
15
24
  enum DeepThinkingStatus {
16
25
  None,
17
26
  Thinking,
18
27
  ReasonOutput,
28
+ CardResource,
19
29
  ThinkingOver
20
30
  }
31
+ // 智能体思考时输出的动作
32
+
21
33
  type TThinkingMessage = { action: string, textposition: number }
22
34
  export default class CorzBot extends GptBase {
23
35
  protected botid: string;
@@ -134,24 +146,34 @@ export default class CorzBot extends GptBase {
134
146
  */
135
147
  private parseDeepThinkingJson(content: string,status: DeepThinkingStatus) {
136
148
  const thinkingEndIndex = content.indexOf("\"}");
137
- const thinkingStartIndex = status === DeepThinkingStatus.Thinking ? content.indexOf("{\"process_msg") : content.indexOf("{\"reasoning_content");
149
+ // const thinkingStartIndex = status === DeepThinkingStatus.Thinking ? content.indexOf("{\"process_msg") :
150
+ // (status===DeepThinkingStatus.ReasonOutput ? content.indexOf("{\"reasoning_content") : content.indexOf("{\"card_resource")) ;
151
+ const thinkingStartIndex = [content.indexOf("{\"process_msg"),
152
+ content.indexOf("{\"reasoning_content"),
153
+ content.indexOf("{\"card_resource")].find(x=>x>=0)??-1;
154
+ // 如果没有找到相关的JSON内容,则直接返回
155
+ if (thinkingStartIndex < 0) return { successed: true, content, status: DeepThinkingStatus.ThinkingOver};
138
156
  let jsonStr = null;
139
157
  try{
140
158
  jsonStr = content.substring(thinkingStartIndex, thinkingEndIndex >= 0 ? thinkingEndIndex : undefined) +"\"}";
141
159
  // 转换JSON的时候需要将非法字符过滤掉
142
160
  const thinkingObject = JSON.parse(jsonStr.replace(/[\x00-\x1F\x7F]/g, ''));
161
+ const thinkingAction = Object.keys(thinkingObject)[0];
143
162
  // 需要将内容的原文返回
144
163
  const originalContent = jsonStr.split('":"')[1];
145
164
  // 如果正确的解析JSON,并且当前有包含JSON结束的字符,则把当前思考的JSON内容替换掉
146
165
  if (thinkingEndIndex >= 0) content = content.substring(thinkingEndIndex+2);
147
- // 判断整个思考过程是否结束
148
- if (status === DeepThinkingStatus.ReasonOutput && thinkingEndIndex >= 0) status = DeepThinkingStatus.ThinkingOver;
166
+ // 如果正常输出了卡片资源,并且json结束 card_resource 只有一个,所以一旦结束,则整个思考过程完毕
167
+ if (status === DeepThinkingStatus.CardResource && thinkingEndIndex >= 0) status = DeepThinkingStatus.ThinkingOver;
168
+ // 判断整个思考过程是否到达等待卡片资源(有可能智能体输出不了卡片资源) reasoning_content 只有一个,所以一旦结束,则进入下一个阶段 card_resource
169
+ if (status === DeepThinkingStatus.ReasonOutput && thinkingEndIndex >= 0) status = DeepThinkingStatus.CardResource;
149
170
  // 判断当前是否从前期思考切换到思考内容输出
150
- if (status === DeepThinkingStatus.Thinking && content.indexOf("{\"reasoning_content") >= 0) status=DeepThinkingStatus.ReasonOutput;
171
+ if (status === DeepThinkingStatus.Thinking && thinkingAction === DeepThinkingAction.content) status = DeepThinkingStatus.ReasonOutput;
172
+ //if (status === DeepThinkingStatus.Thinking && content.indexOf("{\"reasoning_content") >= 0) status=DeepThinkingStatus.ReasonOutput;
151
173
  //const removeOutputContent = content.replace(originalContent!.substring(1, originalContent!.length - 2),"")
152
- let thinkingContent = originalContent?.substring(0, originalContent.length - 2)//.replace(lastContent, '');
174
+ let thinkingContent = originalContent?.substring(0, originalContent.length - 2)
153
175
  const outputLength = thinkingContent?.length || 0;
154
- const thinkingAction = Object.keys(thinkingObject)[0];
176
+
155
177
  // 用lastThinkingMessage来记录上一次的思考内容,避免重复输出内容,导致的网络传输内容过多
156
178
  if (this.lastThinkingMessage.action === thinkingAction && this.lastThinkingMessage.textposition){
157
179
  thinkingContent = thinkingContent!.substring(this.lastThinkingMessage.textposition)
@@ -160,11 +182,17 @@ export default class CorzBot extends GptBase {
160
182
 
161
183
  return { successed: true,thinking:{
162
184
  action: thinkingAction,
163
- text: thinkingContent//Object.values(thinkingObject)[0]
185
+ text: thinkingContent,//Object.values(thinkingObject)[0]
186
+ completed: thinkingEndIndex >= 0
164
187
  }, content, status}
165
188
  }
166
189
  catch(error){
167
- console.error('解析JSON出错了:' + jsonStr)
190
+ // 如果在content之后发生了错误,可能该智能体输出不了卡片类型的数据,则输出的内容已经是正文了
191
+ if (status === DeepThinkingStatus.CardResource) {
192
+ status = DeepThinkingStatus.ThinkingOver;
193
+ return {successed: true, content, status};
194
+ }
195
+ console.error('解析JSON出错了:', jsonStr, status)
168
196
  // 当前解析出错,等待下一次解析
169
197
  return { successed: false, content, status }
170
198
  }
@@ -194,7 +222,7 @@ export default class CorzBot extends GptBase {
194
222
  const params =await this.getRequestStream(client, message, callChatOption);
195
223
  const stream = !callChatOption.workflowid? client.chat.stream(params as StreamChatReq) : client.workflows.chat.stream(params as ChatWorkflowReq);
196
224
  let deltaindex = 0, fullanswer: string[] = [], followup: string[] = [];
197
- let deepThinking = '', thinkingStatus = DeepThinkingStatus.None; // 是否在深度思考中
225
+ let deepThinking = '', thinkingStatus = DeepThinkingStatus.None,cardResource=''; // 是否在深度思考中
198
226
  for await (const part of stream) {
199
227
  if (part.event === ChatEventType.ERROR) return this.emit('chaterror', { successed: false, error: 'call failed' });
200
228
  if (part.event === ChatEventType.CONVERSATION_MESSAGE_DELTA) {
@@ -205,17 +233,29 @@ export default class CorzBot extends GptBase {
205
233
  deltaindex++;
206
234
  }
207
235
  // 如果在深度思考中,则不输出消息
208
- if (thinkingStatus !== DeepThinkingStatus.None &&
209
- thinkingStatus !== DeepThinkingStatus.ThinkingOver
236
+ if (thinkingStatus !== DeepThinkingStatus.None
237
+ && thinkingStatus !== DeepThinkingStatus.ThinkingOver
210
238
  ){
211
239
  deepThinking += content;
212
240
  const result = this.parseDeepThinkingJson(deepThinking, thinkingStatus)
213
- if (result.successed){
214
- deepThinking = result.content;
215
- this.emit('chatthinking', result.thinking);
241
+ if (result.successed ){
242
+ // 如果不是卡片资源,则发送thinking事件,并更新thinkingStatus
243
+ if(thinkingStatus !== DeepThinkingStatus.CardResource){
244
+ deepThinking = result.content;
245
+ this.emit('chatthinking', result.thinking);
246
+ } else if (thinkingStatus === DeepThinkingStatus.CardResource) {
247
+ cardResource += result.thinking?.text;
248
+ // 卡片流结束,解析卡片资源数据
249
+ if (result.thinking?.completed){
250
+ const cardData:any[] = cardResource.replace(/[\x00-\x1F\x7F]/g, '').split('|').map((item:string)=>parse(item));
251
+ // 将卡片资源返回给客户端
252
+ this.emit('chatcard', cardData);
253
+ }
254
+ }
216
255
  thinkingStatus = result.status;
217
256
  }
218
257
  if (thinkingStatus != DeepThinkingStatus.ThinkingOver) continue;
258
+ // thinkingStatus = DeepThinkingStatus.None;
219
259
  // 将排除了thinking之后的消息内容要带下去成为正式的输出内容
220
260
  content = deepThinking;
221
261
  }