doomiaichat 7.1.5 → 7.1.7
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 +1 -1
- package/dist/corzbot.js +69 -22
- package/dist/gptbase.d.ts +5 -0
- package/dist/gptbase.js +18 -0
- package/package.json +1 -1
- package/src/corzbot.ts +65 -21
- package/src/gptbase.ts +10 -1
package/dist/corzbot.d.ts
CHANGED
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["
|
|
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;
|
|
@@ -63,10 +73,15 @@ class CorzBot extends gptbase_1.default {
|
|
|
63
73
|
*/
|
|
64
74
|
createCoversation(client) {
|
|
65
75
|
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
76
|
+
try {
|
|
77
|
+
const czApi = client !== null && client !== void 0 ? client : yield this.createClient();
|
|
78
|
+
const result = yield czApi.conversations.create({ bot_id: this.botid });
|
|
79
|
+
return result.id;
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
console.error('createCoversation error in coze api');
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
70
85
|
});
|
|
71
86
|
}
|
|
72
87
|
/**
|
|
@@ -158,28 +173,40 @@ class CorzBot extends gptbase_1.default {
|
|
|
158
173
|
* @returns
|
|
159
174
|
*/
|
|
160
175
|
parseDeepThinkingJson(content, status) {
|
|
176
|
+
var _a;
|
|
161
177
|
const thinkingEndIndex = content.indexOf("\"}");
|
|
162
|
-
const thinkingStartIndex = status === DeepThinkingStatus.Thinking ? content.indexOf("{\"process_msg") :
|
|
178
|
+
// const thinkingStartIndex = status === DeepThinkingStatus.Thinking ? content.indexOf("{\"process_msg") :
|
|
179
|
+
// (status===DeepThinkingStatus.ReasonOutput ? content.indexOf("{\"reasoning_content") : content.indexOf("{\"card_resource")) ;
|
|
180
|
+
const thinkingStartIndex = (_a = [content.indexOf("{\"process_msg"),
|
|
181
|
+
content.indexOf("{\"reasoning_content"),
|
|
182
|
+
content.indexOf("{\"card_resource")].find(x => x >= 0)) !== null && _a !== void 0 ? _a : -1;
|
|
183
|
+
// 如果没有找到相关的JSON内容,则直接返回
|
|
184
|
+
if (thinkingStartIndex < 0)
|
|
185
|
+
return { successed: true, content, status: DeepThinkingStatus.ThinkingOver };
|
|
163
186
|
let jsonStr = null;
|
|
164
187
|
try {
|
|
165
188
|
jsonStr = content.substring(thinkingStartIndex, thinkingEndIndex >= 0 ? thinkingEndIndex : undefined) + "\"}";
|
|
166
189
|
// 转换JSON的时候需要将非法字符过滤掉
|
|
167
190
|
const thinkingObject = JSON.parse(jsonStr.replace(/[\x00-\x1F\x7F]/g, ''));
|
|
191
|
+
const thinkingAction = Object.keys(thinkingObject)[0];
|
|
168
192
|
// 需要将内容的原文返回
|
|
169
193
|
const originalContent = jsonStr.split('":"')[1];
|
|
170
194
|
// 如果正确的解析JSON,并且当前有包含JSON结束的字符,则把当前思考的JSON内容替换掉
|
|
171
195
|
if (thinkingEndIndex >= 0)
|
|
172
196
|
content = content.substring(thinkingEndIndex + 2);
|
|
173
|
-
//
|
|
174
|
-
if (status === DeepThinkingStatus.
|
|
197
|
+
// 如果正常输出了卡片资源,并且json结束 card_resource 只有一个,所以一旦结束,则整个思考过程完毕
|
|
198
|
+
if (status === DeepThinkingStatus.CardResource && thinkingEndIndex >= 0)
|
|
175
199
|
status = DeepThinkingStatus.ThinkingOver;
|
|
200
|
+
// 判断整个思考过程是否到达等待卡片资源(有可能智能体输出不了卡片资源) reasoning_content 只有一个,所以一旦结束,则进入下一个阶段 card_resource
|
|
201
|
+
if (status === DeepThinkingStatus.ReasonOutput && thinkingEndIndex >= 0)
|
|
202
|
+
status = DeepThinkingStatus.CardResource;
|
|
176
203
|
// 判断当前是否从前期思考切换到思考内容输出
|
|
177
|
-
if (status === DeepThinkingStatus.Thinking &&
|
|
204
|
+
if (status === DeepThinkingStatus.Thinking && thinkingAction === DeepThinkingAction.content)
|
|
178
205
|
status = DeepThinkingStatus.ReasonOutput;
|
|
206
|
+
//if (status === DeepThinkingStatus.Thinking && content.indexOf("{\"reasoning_content") >= 0) status=DeepThinkingStatus.ReasonOutput;
|
|
179
207
|
//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);
|
|
208
|
+
let thinkingContent = originalContent === null || originalContent === void 0 ? void 0 : originalContent.substring(0, originalContent.length - 2);
|
|
181
209
|
const outputLength = (thinkingContent === null || thinkingContent === void 0 ? void 0 : thinkingContent.length) || 0;
|
|
182
|
-
const thinkingAction = Object.keys(thinkingObject)[0];
|
|
183
210
|
// 用lastThinkingMessage来记录上一次的思考内容,避免重复输出内容,导致的网络传输内容过多
|
|
184
211
|
if (this.lastThinkingMessage.action === thinkingAction && this.lastThinkingMessage.textposition) {
|
|
185
212
|
thinkingContent = thinkingContent.substring(this.lastThinkingMessage.textposition);
|
|
@@ -187,11 +214,17 @@ class CorzBot extends gptbase_1.default {
|
|
|
187
214
|
this.lastThinkingMessage = thinkingEndIndex >= 0 ? { action: thinkingAction, textposition: 0 } : { action: thinkingAction, textposition: outputLength };
|
|
188
215
|
return { successed: true, thinking: {
|
|
189
216
|
action: thinkingAction,
|
|
190
|
-
text: thinkingContent
|
|
217
|
+
text: thinkingContent,
|
|
218
|
+
completed: thinkingEndIndex >= 0
|
|
191
219
|
}, content, status };
|
|
192
220
|
}
|
|
193
221
|
catch (error) {
|
|
194
|
-
|
|
222
|
+
// 如果在content之后发生了错误,可能该智能体输出不了卡片类型的数据,则输出的内容已经是正文了
|
|
223
|
+
if (status === DeepThinkingStatus.CardResource) {
|
|
224
|
+
status = DeepThinkingStatus.ThinkingOver;
|
|
225
|
+
return { successed: true, content, status };
|
|
226
|
+
}
|
|
227
|
+
console.error('解析JSON出错了:', jsonStr, status);
|
|
195
228
|
// 当前解析出错,等待下一次解析
|
|
196
229
|
return { successed: false, content, status };
|
|
197
230
|
}
|
|
@@ -206,6 +239,7 @@ class CorzBot extends gptbase_1.default {
|
|
|
206
239
|
*/
|
|
207
240
|
chatRequestInStream(message, callChatOption = {}, attach, _axiosOption) {
|
|
208
241
|
var _a, e_1, _b, _c;
|
|
242
|
+
var _d, _e;
|
|
209
243
|
return __awaiter(this, void 0, void 0, function* () {
|
|
210
244
|
if (!message)
|
|
211
245
|
this.emit('chaterror', { successed: false, error: 'no message in chat' });
|
|
@@ -225,11 +259,11 @@ class CorzBot extends gptbase_1.default {
|
|
|
225
259
|
const params = yield this.getRequestStream(client, message, callChatOption);
|
|
226
260
|
const stream = !callChatOption.workflowid ? client.chat.stream(params) : client.workflows.chat.stream(params);
|
|
227
261
|
let deltaindex = 0, fullanswer = [], followup = [];
|
|
228
|
-
let deepThinking = '', thinkingStatus = DeepThinkingStatus.None; // 是否在深度思考中
|
|
262
|
+
let deepThinking = '', thinkingStatus = DeepThinkingStatus.None, cardResource = ''; // 是否在深度思考中
|
|
229
263
|
try {
|
|
230
|
-
for (var
|
|
264
|
+
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
265
|
_c = stream_1_1.value;
|
|
232
|
-
|
|
266
|
+
_f = false;
|
|
233
267
|
try {
|
|
234
268
|
const part = _c;
|
|
235
269
|
if (part.event === api_1.ChatEventType.ERROR)
|
|
@@ -242,17 +276,30 @@ class CorzBot extends gptbase_1.default {
|
|
|
242
276
|
deltaindex++;
|
|
243
277
|
}
|
|
244
278
|
// 如果在深度思考中,则不输出消息
|
|
245
|
-
if (thinkingStatus !== DeepThinkingStatus.None
|
|
246
|
-
thinkingStatus !== DeepThinkingStatus.ThinkingOver) {
|
|
279
|
+
if (thinkingStatus !== DeepThinkingStatus.None
|
|
280
|
+
&& thinkingStatus !== DeepThinkingStatus.ThinkingOver) {
|
|
247
281
|
deepThinking += content;
|
|
248
282
|
const result = this.parseDeepThinkingJson(deepThinking, thinkingStatus);
|
|
249
283
|
if (result.successed) {
|
|
250
|
-
|
|
251
|
-
|
|
284
|
+
// 如果不是卡片资源,则发送thinking事件,并更新thinkingStatus
|
|
285
|
+
if (thinkingStatus !== DeepThinkingStatus.CardResource) {
|
|
286
|
+
deepThinking = result.content;
|
|
287
|
+
this.emit('chatthinking', result.thinking);
|
|
288
|
+
}
|
|
289
|
+
else if (thinkingStatus === DeepThinkingStatus.CardResource) {
|
|
290
|
+
cardResource += (_d = result.thinking) === null || _d === void 0 ? void 0 : _d.text;
|
|
291
|
+
// 卡片流结束,解析卡片资源数据
|
|
292
|
+
if ((_e = result.thinking) === null || _e === void 0 ? void 0 : _e.completed) {
|
|
293
|
+
const cardData = cardResource.replace(/[\x00-\x1F\x7F]/g, '').split('|').map((item) => (0, querystring_1.parse)(item));
|
|
294
|
+
// 将卡片资源返回给客户端
|
|
295
|
+
this.emit('chatcard', cardData);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
252
298
|
thinkingStatus = result.status;
|
|
253
299
|
}
|
|
254
300
|
if (thinkingStatus != DeepThinkingStatus.ThinkingOver)
|
|
255
301
|
continue;
|
|
302
|
+
// thinkingStatus = DeepThinkingStatus.None;
|
|
256
303
|
// 将排除了thinking之后的消息内容要带下去成为正式的输出内容
|
|
257
304
|
content = deepThinking;
|
|
258
305
|
}
|
|
@@ -278,14 +325,14 @@ class CorzBot extends gptbase_1.default {
|
|
|
278
325
|
}
|
|
279
326
|
}
|
|
280
327
|
finally {
|
|
281
|
-
|
|
328
|
+
_f = true;
|
|
282
329
|
}
|
|
283
330
|
}
|
|
284
331
|
}
|
|
285
332
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
286
333
|
finally {
|
|
287
334
|
try {
|
|
288
|
-
if (!
|
|
335
|
+
if (!_f && !_a && (_b = stream_1.return)) yield _b.call(stream_1);
|
|
289
336
|
}
|
|
290
337
|
finally { if (e_1) throw e_1.error; }
|
|
291
338
|
}
|
package/dist/gptbase.d.ts
CHANGED
|
@@ -18,6 +18,11 @@ export default abstract class GptBase extends EventEmitter {
|
|
|
18
18
|
* @param axiosOption
|
|
19
19
|
*/
|
|
20
20
|
abstract chatRequest(chatText: string | Array<any>, _paramOption: any, axiosOption?: any): Promise<ApiResult>;
|
|
21
|
+
/**
|
|
22
|
+
* 创建一个会话主题id
|
|
23
|
+
* @returns
|
|
24
|
+
*/
|
|
25
|
+
createCoversation(_client?: any): Promise<string | null>;
|
|
21
26
|
/**
|
|
22
27
|
* 流式的聊天模式
|
|
23
28
|
* @param chatText
|
package/dist/gptbase.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
const events_1 = require("events");
|
|
4
13
|
class GptBase extends events_1.EventEmitter {
|
|
@@ -13,6 +22,15 @@ class GptBase extends events_1.EventEmitter {
|
|
|
13
22
|
* @param text
|
|
14
23
|
*/
|
|
15
24
|
getTextEmbedding(_text, _axiosOption) { return null; }
|
|
25
|
+
/**
|
|
26
|
+
* 创建一个会话主题id
|
|
27
|
+
* @returns
|
|
28
|
+
*/
|
|
29
|
+
createCoversation(_client) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
return null;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
16
34
|
/**
|
|
17
35
|
* 流式的聊天模式
|
|
18
36
|
* @param chatText
|
package/package.json
CHANGED
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;
|
|
@@ -45,11 +57,15 @@ export default class CorzBot extends GptBase {
|
|
|
45
57
|
/**
|
|
46
58
|
* 发起一次会话
|
|
47
59
|
*/
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
60
|
+
override async createCoversation(client?: CozeAPI):Promise<string|null>{
|
|
61
|
+
try{
|
|
62
|
+
const czApi = client?? await this.createClient();
|
|
63
|
+
const result = await czApi.conversations.create({ bot_id: this.botid})
|
|
64
|
+
return result.id;
|
|
65
|
+
}catch(error){
|
|
66
|
+
console.error('createCoversation error in coze api');
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
53
69
|
}
|
|
54
70
|
/**
|
|
55
71
|
* 组装请求参数
|
|
@@ -99,7 +115,7 @@ export default class CorzBot extends GptBase {
|
|
|
99
115
|
const client = await this.createClient();
|
|
100
116
|
////如果参数中用的是Workflow,则调用对话流来输出结果
|
|
101
117
|
////否则用智能体的对话来输出结果
|
|
102
|
-
const params =await this.getRequestStream(client,
|
|
118
|
+
const params = await this.getRequestStream(client,message, callChatOption);
|
|
103
119
|
const response = await client.chat.createAndPoll(params as CreateChatReq);
|
|
104
120
|
if (response.chat.status === ChatStatus.COMPLETED && response.messages) {
|
|
105
121
|
const message = response.messages.filter(x=>x.type==='answer').map(item => ({
|
|
@@ -134,24 +150,34 @@ export default class CorzBot extends GptBase {
|
|
|
134
150
|
*/
|
|
135
151
|
private parseDeepThinkingJson(content: string,status: DeepThinkingStatus) {
|
|
136
152
|
const thinkingEndIndex = content.indexOf("\"}");
|
|
137
|
-
const thinkingStartIndex = status === DeepThinkingStatus.Thinking ? content.indexOf("{\"process_msg") :
|
|
153
|
+
// const thinkingStartIndex = status === DeepThinkingStatus.Thinking ? content.indexOf("{\"process_msg") :
|
|
154
|
+
// (status===DeepThinkingStatus.ReasonOutput ? content.indexOf("{\"reasoning_content") : content.indexOf("{\"card_resource")) ;
|
|
155
|
+
const thinkingStartIndex = [content.indexOf("{\"process_msg"),
|
|
156
|
+
content.indexOf("{\"reasoning_content"),
|
|
157
|
+
content.indexOf("{\"card_resource")].find(x=>x>=0)??-1;
|
|
158
|
+
// 如果没有找到相关的JSON内容,则直接返回
|
|
159
|
+
if (thinkingStartIndex < 0) return { successed: true, content, status: DeepThinkingStatus.ThinkingOver};
|
|
138
160
|
let jsonStr = null;
|
|
139
161
|
try{
|
|
140
162
|
jsonStr = content.substring(thinkingStartIndex, thinkingEndIndex >= 0 ? thinkingEndIndex : undefined) +"\"}";
|
|
141
163
|
// 转换JSON的时候需要将非法字符过滤掉
|
|
142
164
|
const thinkingObject = JSON.parse(jsonStr.replace(/[\x00-\x1F\x7F]/g, ''));
|
|
165
|
+
const thinkingAction = Object.keys(thinkingObject)[0];
|
|
143
166
|
// 需要将内容的原文返回
|
|
144
167
|
const originalContent = jsonStr.split('":"')[1];
|
|
145
168
|
// 如果正确的解析JSON,并且当前有包含JSON结束的字符,则把当前思考的JSON内容替换掉
|
|
146
169
|
if (thinkingEndIndex >= 0) content = content.substring(thinkingEndIndex+2);
|
|
147
|
-
//
|
|
148
|
-
if (status === DeepThinkingStatus.
|
|
170
|
+
// 如果正常输出了卡片资源,并且json结束 card_resource 只有一个,所以一旦结束,则整个思考过程完毕
|
|
171
|
+
if (status === DeepThinkingStatus.CardResource && thinkingEndIndex >= 0) status = DeepThinkingStatus.ThinkingOver;
|
|
172
|
+
// 判断整个思考过程是否到达等待卡片资源(有可能智能体输出不了卡片资源) reasoning_content 只有一个,所以一旦结束,则进入下一个阶段 card_resource
|
|
173
|
+
if (status === DeepThinkingStatus.ReasonOutput && thinkingEndIndex >= 0) status = DeepThinkingStatus.CardResource;
|
|
149
174
|
// 判断当前是否从前期思考切换到思考内容输出
|
|
150
|
-
if (status === DeepThinkingStatus.Thinking &&
|
|
175
|
+
if (status === DeepThinkingStatus.Thinking && thinkingAction === DeepThinkingAction.content) status = DeepThinkingStatus.ReasonOutput;
|
|
176
|
+
//if (status === DeepThinkingStatus.Thinking && content.indexOf("{\"reasoning_content") >= 0) status=DeepThinkingStatus.ReasonOutput;
|
|
151
177
|
//const removeOutputContent = content.replace(originalContent!.substring(1, originalContent!.length - 2),"")
|
|
152
|
-
let thinkingContent = originalContent?.substring(0, originalContent.length - 2)
|
|
178
|
+
let thinkingContent = originalContent?.substring(0, originalContent.length - 2)
|
|
153
179
|
const outputLength = thinkingContent?.length || 0;
|
|
154
|
-
|
|
180
|
+
|
|
155
181
|
// 用lastThinkingMessage来记录上一次的思考内容,避免重复输出内容,导致的网络传输内容过多
|
|
156
182
|
if (this.lastThinkingMessage.action === thinkingAction && this.lastThinkingMessage.textposition){
|
|
157
183
|
thinkingContent = thinkingContent!.substring(this.lastThinkingMessage.textposition)
|
|
@@ -160,11 +186,17 @@ export default class CorzBot extends GptBase {
|
|
|
160
186
|
|
|
161
187
|
return { successed: true,thinking:{
|
|
162
188
|
action: thinkingAction,
|
|
163
|
-
text: thinkingContent
|
|
189
|
+
text: thinkingContent,//Object.values(thinkingObject)[0]
|
|
190
|
+
completed: thinkingEndIndex >= 0
|
|
164
191
|
}, content, status}
|
|
165
192
|
}
|
|
166
193
|
catch(error){
|
|
167
|
-
|
|
194
|
+
// 如果在content之后发生了错误,可能该智能体输出不了卡片类型的数据,则输出的内容已经是正文了
|
|
195
|
+
if (status === DeepThinkingStatus.CardResource) {
|
|
196
|
+
status = DeepThinkingStatus.ThinkingOver;
|
|
197
|
+
return {successed: true, content, status};
|
|
198
|
+
}
|
|
199
|
+
console.error('解析JSON出错了:', jsonStr, status)
|
|
168
200
|
// 当前解析出错,等待下一次解析
|
|
169
201
|
return { successed: false, content, status }
|
|
170
202
|
}
|
|
@@ -191,10 +223,10 @@ export default class CorzBot extends GptBase {
|
|
|
191
223
|
////如果参数中用的是Workflow,则调用对话流来输出结果
|
|
192
224
|
////否则用智能体的对话来输出结果
|
|
193
225
|
let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000), index=0;
|
|
194
|
-
const params =await this.getRequestStream(client,
|
|
226
|
+
const params = await this.getRequestStream(client,message, callChatOption);
|
|
195
227
|
const stream = !callChatOption.workflowid? client.chat.stream(params as StreamChatReq) : client.workflows.chat.stream(params as ChatWorkflowReq);
|
|
196
228
|
let deltaindex = 0, fullanswer: string[] = [], followup: string[] = [];
|
|
197
|
-
let deepThinking = '', thinkingStatus = DeepThinkingStatus.None; // 是否在深度思考中
|
|
229
|
+
let deepThinking = '', thinkingStatus = DeepThinkingStatus.None,cardResource=''; // 是否在深度思考中
|
|
198
230
|
for await (const part of stream) {
|
|
199
231
|
if (part.event === ChatEventType.ERROR) return this.emit('chaterror', { successed: false, error: 'call failed' });
|
|
200
232
|
if (part.event === ChatEventType.CONVERSATION_MESSAGE_DELTA) {
|
|
@@ -205,17 +237,29 @@ export default class CorzBot extends GptBase {
|
|
|
205
237
|
deltaindex++;
|
|
206
238
|
}
|
|
207
239
|
// 如果在深度思考中,则不输出消息
|
|
208
|
-
if (thinkingStatus !== DeepThinkingStatus.None
|
|
209
|
-
thinkingStatus !== DeepThinkingStatus.ThinkingOver
|
|
240
|
+
if (thinkingStatus !== DeepThinkingStatus.None
|
|
241
|
+
&& thinkingStatus !== DeepThinkingStatus.ThinkingOver
|
|
210
242
|
){
|
|
211
243
|
deepThinking += content;
|
|
212
244
|
const result = this.parseDeepThinkingJson(deepThinking, thinkingStatus)
|
|
213
|
-
if (result.successed){
|
|
214
|
-
|
|
215
|
-
|
|
245
|
+
if (result.successed ){
|
|
246
|
+
// 如果不是卡片资源,则发送thinking事件,并更新thinkingStatus
|
|
247
|
+
if(thinkingStatus !== DeepThinkingStatus.CardResource){
|
|
248
|
+
deepThinking = result.content;
|
|
249
|
+
this.emit('chatthinking', result.thinking);
|
|
250
|
+
} else if (thinkingStatus === DeepThinkingStatus.CardResource) {
|
|
251
|
+
cardResource += result.thinking?.text;
|
|
252
|
+
// 卡片流结束,解析卡片资源数据
|
|
253
|
+
if (result.thinking?.completed){
|
|
254
|
+
const cardData:any[] = cardResource.replace(/[\x00-\x1F\x7F]/g, '').split('|').map((item:string)=>parse(item));
|
|
255
|
+
// 将卡片资源返回给客户端
|
|
256
|
+
this.emit('chatcard', cardData);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
216
259
|
thinkingStatus = result.status;
|
|
217
260
|
}
|
|
218
261
|
if (thinkingStatus != DeepThinkingStatus.ThinkingOver) continue;
|
|
262
|
+
// thinkingStatus = DeepThinkingStatus.None;
|
|
219
263
|
// 将排除了thinking之后的消息内容要带下去成为正式的输出内容
|
|
220
264
|
content = deepThinking;
|
|
221
265
|
}
|
package/src/gptbase.ts
CHANGED
|
@@ -20,6 +20,14 @@ export default abstract class GptBase extends EventEmitter {
|
|
|
20
20
|
* @param axiosOption
|
|
21
21
|
*/
|
|
22
22
|
abstract chatRequest(chatText: string | Array<any>, _paramOption: any, axiosOption?: any): Promise<ApiResult>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 创建一个会话主题id
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
async createCoversation(_client?:any):Promise<string|null> {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
23
31
|
/**
|
|
24
32
|
* 流式的聊天模式
|
|
25
33
|
* @param chatText
|
|
@@ -27,4 +35,5 @@ export default abstract class GptBase extends EventEmitter {
|
|
|
27
35
|
* @param axiosOption
|
|
28
36
|
*/
|
|
29
37
|
chatRequestInStream(_chatText: string | Array<any>, _paramOption: any, _attach?: any, _axiosOption?: any): any { return null; }
|
|
30
|
-
}
|
|
38
|
+
}
|
|
39
|
+
|