@fonixtree/magic-design 2.0.77 → 2.0.78

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.
@@ -5,7 +5,7 @@ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" =
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports["default"] = SearchCondition;
8
+ exports["default"] = void 0;
9
9
 
10
10
  var _react = _interopRequireWildcard(require("react"));
11
11
 
@@ -23,23 +23,37 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
23
23
 
24
24
  var toneList = ['Concise', 'Formal', 'Youthful', 'Innovative', 'Enticing'];
25
25
 
26
- function SearchCondition() {
27
- var _a = (0, _react.useState)('Concise'),
28
- tone = _a[0],
29
- setTone = _a[1];
26
+ function SearchCondition(_a) {
27
+ var onRef = _a.onRef;
30
28
 
31
- var _b = (0, _react.useState)('30'),
32
- count = _b[0],
33
- setCount = _b[1];
29
+ var _b = (0, _react.useState)('Concise'),
30
+ tone = _b[0],
31
+ setTone = _b[1];
34
32
 
35
- var _c = (0, _react.useState)(''),
36
- features = _c[0],
37
- setFeatures = _c[1];
33
+ var _c = (0, _react.useState)('30'),
34
+ count = _c[0],
35
+ setCount = _c[1];
38
36
 
39
37
  var _d = (0, _react.useState)(''),
40
- key = _d[0],
41
- setKey = _d[1];
38
+ features = _d[0],
39
+ setFeatures = _d[1];
42
40
 
41
+ var _e = (0, _react.useState)(''),
42
+ keyDetail = _e[0],
43
+ setKeyDetail = _e[1];
44
+
45
+ (0, _react.useImperativeHandle)(onRef, function () {
46
+ return {
47
+ getValues: function getValues() {
48
+ return {
49
+ tone: tone,
50
+ count: count,
51
+ features: features,
52
+ keyDetail: keyDetail
53
+ };
54
+ }
55
+ };
56
+ });
43
57
  return /*#__PURE__*/_react["default"].createElement("div", {
44
58
  className: "condition_wrap"
45
59
  }, /*#__PURE__*/_react["default"].createElement("div", {
@@ -103,8 +117,12 @@ function SearchCondition() {
103
117
  className: "cond_value"
104
118
  }, /*#__PURE__*/_react["default"].createElement(_antd.Input, {
105
119
  onChange: function onChange(e) {
106
- return setKey(e.target.value);
120
+ return setKeyDetail(e.target.value);
107
121
  },
108
- value: key
122
+ value: keyDetail
109
123
  }))));
110
- }
124
+ }
125
+
126
+ var _default = /*#__PURE__*/(0, _react.forwardRef)(SearchCondition);
127
+
128
+ exports["default"] = _default;
@@ -71,6 +71,7 @@ function (_super) {
71
71
  function ChatRobot(props) {
72
72
  var _this = _super.call(this, props) || this;
73
73
 
74
+ _this.condRef = /*#__PURE__*/_react["default"].createRef();
74
75
  _this.state = {
75
76
  open: false,
76
77
  message: '',
@@ -96,15 +97,57 @@ function (_super) {
96
97
  return marked.parse(text);
97
98
  };
98
99
 
100
+ _this.getMessageObj = function (msg) {
101
+ var showCondition = _this.state.showCondition;
102
+
103
+ if (!showCondition) {
104
+ return {
105
+ content: msg,
106
+ showContent: msg
107
+ };
108
+ }
109
+
110
+ var newMsg = msg;
111
+
112
+ if (msg.endsWith('.')) {
113
+ newMsg += '.';
114
+ }
115
+
116
+ var cond = _this.condRef.current.getValues();
117
+
118
+ if (cond.tone) {
119
+ newMsg += " The tone should be " + cond.tone + ".";
120
+ }
121
+
122
+ if (cond.count) {
123
+ newMsg += " Within " + cond.count + " words.";
124
+ }
125
+
126
+ if (cond.features) {
127
+ newMsg += " The description should cover key features " + cond.features + ".";
128
+ }
129
+
130
+ if (cond.keyDetail) {
131
+ newMsg += " Emphasize " + cond.keyDetail + ".";
132
+ }
133
+
134
+ return {
135
+ content: newMsg,
136
+ showContent: msg
137
+ };
138
+ };
139
+
99
140
  _this.onEnter = function () {
100
141
  var message = _this.state.message;
101
142
 
102
143
  if (!message) {
103
144
  return;
104
- } // chat
145
+ }
146
+
147
+ var messageObj = _this.getMessageObj(message); // chat
105
148
 
106
149
 
107
- _this.chatGpt.sendMessage(message);
150
+ _this.chatGpt.sendMessage(messageObj);
108
151
 
109
152
  var context = _this.chatGpt.getContext();
110
153
 
@@ -312,7 +355,7 @@ function (_super) {
312
355
  }, /*#__PURE__*/_react["default"].createElement("div", {
313
356
  className: "messageContentWrap",
314
357
  dangerouslySetInnerHTML: {
315
- __html: _this.getMarkText(ctxNode.content)
358
+ __html: _this.getMarkText(ctxNode.showContent || ctxNode.content)
316
359
  }
317
360
  }), ctxNode.role === 'assistant' && /*#__PURE__*/_react["default"].createElement("div", {
318
361
  className: "msgIconWrap"
@@ -343,7 +386,9 @@ function (_super) {
343
386
  className: (0, _classnames["default"])('more_condition', {
344
387
  more_condition_hide: !showCondition
345
388
  })
346
- }, /*#__PURE__*/_react["default"].createElement(_SearchCondition["default"], null)))), /*#__PURE__*/_react["default"].createElement(_AiImageGenerator["default"], {
389
+ }, /*#__PURE__*/_react["default"].createElement(_SearchCondition["default"], {
390
+ onRef: this.condRef
391
+ })))), /*#__PURE__*/_react["default"].createElement(_AiImageGenerator["default"], {
347
392
  onClose: function onClose() {
348
393
  return _this.setState({
349
394
  aiImageVisible: false
@@ -11,6 +11,165 @@ var _commonUtil = require("../../../utils/commonUtil");
11
11
 
12
12
  var _storeUtil = require("../../../utils/storeUtil");
13
13
 
14
+ var __assign = void 0 && (void 0).__assign || function () {
15
+ __assign = Object.assign || function (t) {
16
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
17
+ s = arguments[i];
18
+
19
+ for (var p in s) {
20
+ if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
21
+ }
22
+ }
23
+
24
+ return t;
25
+ };
26
+
27
+ return __assign.apply(this, arguments);
28
+ };
29
+
30
+ var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
31
+ function adopt(value) {
32
+ return value instanceof P ? value : new P(function (resolve) {
33
+ resolve(value);
34
+ });
35
+ }
36
+
37
+ return new (P || (P = Promise))(function (resolve, reject) {
38
+ function fulfilled(value) {
39
+ try {
40
+ step(generator.next(value));
41
+ } catch (e) {
42
+ reject(e);
43
+ }
44
+ }
45
+
46
+ function rejected(value) {
47
+ try {
48
+ step(generator["throw"](value));
49
+ } catch (e) {
50
+ reject(e);
51
+ }
52
+ }
53
+
54
+ function step(result) {
55
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
56
+ }
57
+
58
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
59
+ });
60
+ };
61
+
62
+ var __generator = void 0 && (void 0).__generator || function (thisArg, body) {
63
+ var _ = {
64
+ label: 0,
65
+ sent: function sent() {
66
+ if (t[0] & 1) throw t[1];
67
+ return t[1];
68
+ },
69
+ trys: [],
70
+ ops: []
71
+ },
72
+ f,
73
+ y,
74
+ t,
75
+ g;
76
+ return g = {
77
+ next: verb(0),
78
+ "throw": verb(1),
79
+ "return": verb(2)
80
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
81
+ return this;
82
+ }), g;
83
+
84
+ function verb(n) {
85
+ return function (v) {
86
+ return step([n, v]);
87
+ };
88
+ }
89
+
90
+ function step(op) {
91
+ if (f) throw new TypeError("Generator is already executing.");
92
+
93
+ while (_) {
94
+ try {
95
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
96
+ if (y = 0, t) op = [op[0] & 2, t.value];
97
+
98
+ switch (op[0]) {
99
+ case 0:
100
+ case 1:
101
+ t = op;
102
+ break;
103
+
104
+ case 4:
105
+ _.label++;
106
+ return {
107
+ value: op[1],
108
+ done: false
109
+ };
110
+
111
+ case 5:
112
+ _.label++;
113
+ y = op[1];
114
+ op = [0];
115
+ continue;
116
+
117
+ case 7:
118
+ op = _.ops.pop();
119
+
120
+ _.trys.pop();
121
+
122
+ continue;
123
+
124
+ default:
125
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
126
+ _ = 0;
127
+ continue;
128
+ }
129
+
130
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
131
+ _.label = op[1];
132
+ break;
133
+ }
134
+
135
+ if (op[0] === 6 && _.label < t[1]) {
136
+ _.label = t[1];
137
+ t = op;
138
+ break;
139
+ }
140
+
141
+ if (t && _.label < t[2]) {
142
+ _.label = t[2];
143
+
144
+ _.ops.push(op);
145
+
146
+ break;
147
+ }
148
+
149
+ if (t[2]) _.ops.pop();
150
+
151
+ _.trys.pop();
152
+
153
+ continue;
154
+ }
155
+
156
+ op = body.call(thisArg, _);
157
+ } catch (e) {
158
+ op = [6, e];
159
+ y = 0;
160
+ } finally {
161
+ f = t = 0;
162
+ }
163
+ }
164
+
165
+ if (op[0] & 5) throw op[1];
166
+ return {
167
+ value: op[0] ? op[1] : void 0,
168
+ done: true
169
+ };
170
+ }
171
+ };
172
+
14
173
  // 角色
15
174
  var roleMap = {
16
175
  ASSISTANT: 'assistant',
@@ -33,6 +192,7 @@ function () {
33
192
  this.initParams = {}; // 建立连接
34
193
 
35
194
  this.initChatConnect = function () {
195
+ // return new Promise((resolve, reject) => {
36
196
  var _a = _this.initParams,
37
197
  onMessage = _a.onMessage,
38
198
  onEnd = _a.onEnd;
@@ -43,14 +203,16 @@ function () {
43
203
 
44
204
  eventSource.onopen = function (event) {
45
205
  console.log('onOpen');
46
- sse = event.target;
206
+ sse = event.target; // resolve();
47
207
  };
48
208
 
49
209
  eventSource.onmessage = function (event) {
50
210
  if (event.data == '[DONE]') {
51
211
  _this.isGenerating = false;
52
212
 
53
- _this.insertMessage2Context(text, roleMap.ASSISTANT);
213
+ _this.insertMessage2Context({
214
+ content: text
215
+ }, roleMap.ASSISTANT);
54
216
 
55
217
  text = '';
56
218
  onEnd();
@@ -77,50 +239,80 @@ function () {
77
239
  _this.isGenerating = false;
78
240
 
79
241
  _this.regenerate();
80
- };
81
- }; // 获取chat上下文
242
+ }; // });
243
+
244
+ }; // 获取全部对话上下文
82
245
 
83
246
 
84
247
  this.getContext = function () {
85
248
  var messages = (0, _storeUtil.getSessionStore)(catchKey) || [];
86
249
  return messages;
250
+ }; // 获取发送消息上下文
251
+
252
+
253
+ this.getSendContext = function () {
254
+ var context = (0, _storeUtil.getSessionStore)(catchKey) || [];
255
+ context = context.splice(context.length - 5).map(function (ctx) {
256
+ return {
257
+ role: ctx.role,
258
+ content: ctx.content
259
+ };
260
+ });
261
+ return context;
87
262
  }; // 消息插入上下文中
88
263
 
89
264
 
90
- this.insertMessage2Context = function (message, role) {
265
+ this.insertMessage2Context = function (messageObj, role) {
91
266
  if (role === void 0) {
92
267
  role = roleMap.USER;
93
268
  }
94
269
 
95
270
  var messages = (0, _storeUtil.getSessionStore)(catchKey) || [];
96
- messages.push({
97
- content: message,
271
+ messages.push(__assign({
98
272
  role: role
99
- });
273
+ }, messageObj));
100
274
  (0, _storeUtil.setSessionStore)(catchKey, messages);
101
275
  }; // 发送消息
102
276
 
103
277
 
104
- this.sendMessage = function (message) {
105
- if (!message) {
106
- return;
107
- }
108
-
109
- _this.isGenerating = true;
110
-
111
- _this.insertMessage2Context(message);
112
-
113
- var messages = _this.getContext();
114
-
115
- _this.initChatConnect();
116
-
117
- var newMessages = messages.splice(messages.length - 5);
118
- setTimeout(function () {
119
- (0, _commonUtil.commonFetch)('/designer/v1/ai/stream-chat-completion', {
120
- clientId: _this.clientId,
121
- messages: newMessages
122
- }, 'POST');
123
- }, 200);
278
+ this.sendMessage = function (messageObj) {
279
+ return __awaiter(_this, void 0, void 0, function () {
280
+ var messages, newMessages;
281
+
282
+ var _this = this;
283
+
284
+ return __generator(this, function (_a) {
285
+ switch (_a.label) {
286
+ case 0:
287
+ if (!messageObj.content) {
288
+ return [2
289
+ /*return*/
290
+ ];
291
+ }
292
+
293
+ this.isGenerating = true;
294
+ this.insertMessage2Context(messageObj);
295
+ messages = this.getSendContext();
296
+ return [4
297
+ /*yield*/
298
+ , this.initChatConnect()];
299
+
300
+ case 1:
301
+ _a.sent();
302
+
303
+ newMessages = messages.splice(messages.length - 5);
304
+ setTimeout(function () {
305
+ (0, _commonUtil.commonFetch)('/designer/v1/ai/stream-chat-completion', {
306
+ clientId: _this.clientId,
307
+ messages: newMessages
308
+ }, 'POST');
309
+ }, 200);
310
+ return [2
311
+ /*return*/
312
+ ];
313
+ }
314
+ });
315
+ });
124
316
  }; // 重新生成
125
317
 
126
318
 
@@ -139,7 +331,8 @@ function () {
139
331
 
140
332
  _this.initChatConnect();
141
333
 
142
- var newMessages = messages.splice(messages.length - 5);
334
+ var newMessages = _this.getSendContext();
335
+
143
336
  setTimeout(function () {
144
337
  (0, _commonUtil.commonFetch)('/designer/v1/ai/stream-chat-completion', {
145
338
  clientId: _this.clientId,
@@ -5,7 +5,7 @@ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" =
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports["default"] = SearchCondition;
8
+ exports["default"] = void 0;
9
9
 
10
10
  var _react = _interopRequireWildcard(require("react"));
11
11
 
@@ -23,23 +23,37 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
23
23
 
24
24
  var toneList = ['Concise', 'Formal', 'Youthful', 'Innovative', 'Enticing'];
25
25
 
26
- function SearchCondition() {
27
- var _a = (0, _react.useState)('Concise'),
28
- tone = _a[0],
29
- setTone = _a[1];
26
+ function SearchCondition(_a) {
27
+ var onRef = _a.onRef;
30
28
 
31
- var _b = (0, _react.useState)('30'),
32
- count = _b[0],
33
- setCount = _b[1];
29
+ var _b = (0, _react.useState)('Concise'),
30
+ tone = _b[0],
31
+ setTone = _b[1];
34
32
 
35
- var _c = (0, _react.useState)(''),
36
- features = _c[0],
37
- setFeatures = _c[1];
33
+ var _c = (0, _react.useState)('30'),
34
+ count = _c[0],
35
+ setCount = _c[1];
38
36
 
39
37
  var _d = (0, _react.useState)(''),
40
- key = _d[0],
41
- setKey = _d[1];
38
+ features = _d[0],
39
+ setFeatures = _d[1];
42
40
 
41
+ var _e = (0, _react.useState)(''),
42
+ keyDetail = _e[0],
43
+ setKeyDetail = _e[1];
44
+
45
+ (0, _react.useImperativeHandle)(onRef, function () {
46
+ return {
47
+ getValues: function getValues() {
48
+ return {
49
+ tone: tone,
50
+ count: count,
51
+ features: features,
52
+ keyDetail: keyDetail
53
+ };
54
+ }
55
+ };
56
+ });
43
57
  return /*#__PURE__*/_react["default"].createElement("div", {
44
58
  className: "condition_wrap"
45
59
  }, /*#__PURE__*/_react["default"].createElement("div", {
@@ -103,8 +117,12 @@ function SearchCondition() {
103
117
  className: "cond_value"
104
118
  }, /*#__PURE__*/_react["default"].createElement(_antd.Input, {
105
119
  onChange: function onChange(e) {
106
- return setKey(e.target.value);
120
+ return setKeyDetail(e.target.value);
107
121
  },
108
- value: key
122
+ value: keyDetail
109
123
  }))));
110
- }
124
+ }
125
+
126
+ var _default = /*#__PURE__*/(0, _react.forwardRef)(SearchCondition);
127
+
128
+ exports["default"] = _default;
@@ -71,6 +71,7 @@ function (_super) {
71
71
  function ChatRobot(props) {
72
72
  var _this = _super.call(this, props) || this;
73
73
 
74
+ _this.condRef = /*#__PURE__*/_react["default"].createRef();
74
75
  _this.state = {
75
76
  open: false,
76
77
  message: '',
@@ -96,15 +97,57 @@ function (_super) {
96
97
  return marked.parse(text);
97
98
  };
98
99
 
100
+ _this.getMessageObj = function (msg) {
101
+ var showCondition = _this.state.showCondition;
102
+
103
+ if (!showCondition) {
104
+ return {
105
+ content: msg,
106
+ showContent: msg
107
+ };
108
+ }
109
+
110
+ var newMsg = msg;
111
+
112
+ if (msg.endsWith('.')) {
113
+ newMsg += '.';
114
+ }
115
+
116
+ var cond = _this.condRef.current.getValues();
117
+
118
+ if (cond.tone) {
119
+ newMsg += " The tone should be " + cond.tone + ".";
120
+ }
121
+
122
+ if (cond.count) {
123
+ newMsg += " Within " + cond.count + " words.";
124
+ }
125
+
126
+ if (cond.features) {
127
+ newMsg += " The description should cover key features " + cond.features + ".";
128
+ }
129
+
130
+ if (cond.keyDetail) {
131
+ newMsg += " Emphasize " + cond.keyDetail + ".";
132
+ }
133
+
134
+ return {
135
+ content: newMsg,
136
+ showContent: msg
137
+ };
138
+ };
139
+
99
140
  _this.onEnter = function () {
100
141
  var message = _this.state.message;
101
142
 
102
143
  if (!message) {
103
144
  return;
104
- } // chat
145
+ }
146
+
147
+ var messageObj = _this.getMessageObj(message); // chat
105
148
 
106
149
 
107
- _this.chatGpt.sendMessage(message);
150
+ _this.chatGpt.sendMessage(messageObj);
108
151
 
109
152
  var context = _this.chatGpt.getContext();
110
153
 
@@ -312,7 +355,7 @@ function (_super) {
312
355
  }, /*#__PURE__*/_react["default"].createElement("div", {
313
356
  className: "messageContentWrap",
314
357
  dangerouslySetInnerHTML: {
315
- __html: _this.getMarkText(ctxNode.content)
358
+ __html: _this.getMarkText(ctxNode.showContent || ctxNode.content)
316
359
  }
317
360
  }), ctxNode.role === 'assistant' && /*#__PURE__*/_react["default"].createElement("div", {
318
361
  className: "msgIconWrap"
@@ -343,7 +386,9 @@ function (_super) {
343
386
  className: (0, _classnames["default"])('more_condition', {
344
387
  more_condition_hide: !showCondition
345
388
  })
346
- }, /*#__PURE__*/_react["default"].createElement(_SearchCondition["default"], null)))), /*#__PURE__*/_react["default"].createElement(_AiImageGenerator["default"], {
389
+ }, /*#__PURE__*/_react["default"].createElement(_SearchCondition["default"], {
390
+ onRef: this.condRef
391
+ })))), /*#__PURE__*/_react["default"].createElement(_AiImageGenerator["default"], {
347
392
  onClose: function onClose() {
348
393
  return _this.setState({
349
394
  aiImageVisible: false
@@ -11,6 +11,165 @@ var _commonUtil = require("../../../utils/commonUtil");
11
11
 
12
12
  var _storeUtil = require("../../../utils/storeUtil");
13
13
 
14
+ var __assign = void 0 && (void 0).__assign || function () {
15
+ __assign = Object.assign || function (t) {
16
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
17
+ s = arguments[i];
18
+
19
+ for (var p in s) {
20
+ if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
21
+ }
22
+ }
23
+
24
+ return t;
25
+ };
26
+
27
+ return __assign.apply(this, arguments);
28
+ };
29
+
30
+ var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
31
+ function adopt(value) {
32
+ return value instanceof P ? value : new P(function (resolve) {
33
+ resolve(value);
34
+ });
35
+ }
36
+
37
+ return new (P || (P = Promise))(function (resolve, reject) {
38
+ function fulfilled(value) {
39
+ try {
40
+ step(generator.next(value));
41
+ } catch (e) {
42
+ reject(e);
43
+ }
44
+ }
45
+
46
+ function rejected(value) {
47
+ try {
48
+ step(generator["throw"](value));
49
+ } catch (e) {
50
+ reject(e);
51
+ }
52
+ }
53
+
54
+ function step(result) {
55
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
56
+ }
57
+
58
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
59
+ });
60
+ };
61
+
62
+ var __generator = void 0 && (void 0).__generator || function (thisArg, body) {
63
+ var _ = {
64
+ label: 0,
65
+ sent: function sent() {
66
+ if (t[0] & 1) throw t[1];
67
+ return t[1];
68
+ },
69
+ trys: [],
70
+ ops: []
71
+ },
72
+ f,
73
+ y,
74
+ t,
75
+ g;
76
+ return g = {
77
+ next: verb(0),
78
+ "throw": verb(1),
79
+ "return": verb(2)
80
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
81
+ return this;
82
+ }), g;
83
+
84
+ function verb(n) {
85
+ return function (v) {
86
+ return step([n, v]);
87
+ };
88
+ }
89
+
90
+ function step(op) {
91
+ if (f) throw new TypeError("Generator is already executing.");
92
+
93
+ while (_) {
94
+ try {
95
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
96
+ if (y = 0, t) op = [op[0] & 2, t.value];
97
+
98
+ switch (op[0]) {
99
+ case 0:
100
+ case 1:
101
+ t = op;
102
+ break;
103
+
104
+ case 4:
105
+ _.label++;
106
+ return {
107
+ value: op[1],
108
+ done: false
109
+ };
110
+
111
+ case 5:
112
+ _.label++;
113
+ y = op[1];
114
+ op = [0];
115
+ continue;
116
+
117
+ case 7:
118
+ op = _.ops.pop();
119
+
120
+ _.trys.pop();
121
+
122
+ continue;
123
+
124
+ default:
125
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
126
+ _ = 0;
127
+ continue;
128
+ }
129
+
130
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
131
+ _.label = op[1];
132
+ break;
133
+ }
134
+
135
+ if (op[0] === 6 && _.label < t[1]) {
136
+ _.label = t[1];
137
+ t = op;
138
+ break;
139
+ }
140
+
141
+ if (t && _.label < t[2]) {
142
+ _.label = t[2];
143
+
144
+ _.ops.push(op);
145
+
146
+ break;
147
+ }
148
+
149
+ if (t[2]) _.ops.pop();
150
+
151
+ _.trys.pop();
152
+
153
+ continue;
154
+ }
155
+
156
+ op = body.call(thisArg, _);
157
+ } catch (e) {
158
+ op = [6, e];
159
+ y = 0;
160
+ } finally {
161
+ f = t = 0;
162
+ }
163
+ }
164
+
165
+ if (op[0] & 5) throw op[1];
166
+ return {
167
+ value: op[0] ? op[1] : void 0,
168
+ done: true
169
+ };
170
+ }
171
+ };
172
+
14
173
  // 角色
15
174
  var roleMap = {
16
175
  ASSISTANT: 'assistant',
@@ -33,6 +192,7 @@ function () {
33
192
  this.initParams = {}; // 建立连接
34
193
 
35
194
  this.initChatConnect = function () {
195
+ // return new Promise((resolve, reject) => {
36
196
  var _a = _this.initParams,
37
197
  onMessage = _a.onMessage,
38
198
  onEnd = _a.onEnd;
@@ -43,14 +203,16 @@ function () {
43
203
 
44
204
  eventSource.onopen = function (event) {
45
205
  console.log('onOpen');
46
- sse = event.target;
206
+ sse = event.target; // resolve();
47
207
  };
48
208
 
49
209
  eventSource.onmessage = function (event) {
50
210
  if (event.data == '[DONE]') {
51
211
  _this.isGenerating = false;
52
212
 
53
- _this.insertMessage2Context(text, roleMap.ASSISTANT);
213
+ _this.insertMessage2Context({
214
+ content: text
215
+ }, roleMap.ASSISTANT);
54
216
 
55
217
  text = '';
56
218
  onEnd();
@@ -77,50 +239,80 @@ function () {
77
239
  _this.isGenerating = false;
78
240
 
79
241
  _this.regenerate();
80
- };
81
- }; // 获取chat上下文
242
+ }; // });
243
+
244
+ }; // 获取全部对话上下文
82
245
 
83
246
 
84
247
  this.getContext = function () {
85
248
  var messages = (0, _storeUtil.getSessionStore)(catchKey) || [];
86
249
  return messages;
250
+ }; // 获取发送消息上下文
251
+
252
+
253
+ this.getSendContext = function () {
254
+ var context = (0, _storeUtil.getSessionStore)(catchKey) || [];
255
+ context = context.splice(context.length - 5).map(function (ctx) {
256
+ return {
257
+ role: ctx.role,
258
+ content: ctx.content
259
+ };
260
+ });
261
+ return context;
87
262
  }; // 消息插入上下文中
88
263
 
89
264
 
90
- this.insertMessage2Context = function (message, role) {
265
+ this.insertMessage2Context = function (messageObj, role) {
91
266
  if (role === void 0) {
92
267
  role = roleMap.USER;
93
268
  }
94
269
 
95
270
  var messages = (0, _storeUtil.getSessionStore)(catchKey) || [];
96
- messages.push({
97
- content: message,
271
+ messages.push(__assign({
98
272
  role: role
99
- });
273
+ }, messageObj));
100
274
  (0, _storeUtil.setSessionStore)(catchKey, messages);
101
275
  }; // 发送消息
102
276
 
103
277
 
104
- this.sendMessage = function (message) {
105
- if (!message) {
106
- return;
107
- }
108
-
109
- _this.isGenerating = true;
110
-
111
- _this.insertMessage2Context(message);
112
-
113
- var messages = _this.getContext();
114
-
115
- _this.initChatConnect();
116
-
117
- var newMessages = messages.splice(messages.length - 5);
118
- setTimeout(function () {
119
- (0, _commonUtil.commonFetch)('/designer/v1/ai/stream-chat-completion', {
120
- clientId: _this.clientId,
121
- messages: newMessages
122
- }, 'POST');
123
- }, 200);
278
+ this.sendMessage = function (messageObj) {
279
+ return __awaiter(_this, void 0, void 0, function () {
280
+ var messages, newMessages;
281
+
282
+ var _this = this;
283
+
284
+ return __generator(this, function (_a) {
285
+ switch (_a.label) {
286
+ case 0:
287
+ if (!messageObj.content) {
288
+ return [2
289
+ /*return*/
290
+ ];
291
+ }
292
+
293
+ this.isGenerating = true;
294
+ this.insertMessage2Context(messageObj);
295
+ messages = this.getSendContext();
296
+ return [4
297
+ /*yield*/
298
+ , this.initChatConnect()];
299
+
300
+ case 1:
301
+ _a.sent();
302
+
303
+ newMessages = messages.splice(messages.length - 5);
304
+ setTimeout(function () {
305
+ (0, _commonUtil.commonFetch)('/designer/v1/ai/stream-chat-completion', {
306
+ clientId: _this.clientId,
307
+ messages: newMessages
308
+ }, 'POST');
309
+ }, 200);
310
+ return [2
311
+ /*return*/
312
+ ];
313
+ }
314
+ });
315
+ });
124
316
  }; // 重新生成
125
317
 
126
318
 
@@ -139,7 +331,8 @@ function () {
139
331
 
140
332
  _this.initChatConnect();
141
333
 
142
- var newMessages = messages.splice(messages.length - 5);
334
+ var newMessages = _this.getSendContext();
335
+
143
336
  setTimeout(function () {
144
337
  (0, _commonUtil.commonFetch)('/designer/v1/ai/stream-chat-completion', {
145
338
  clientId: _this.clientId,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fonixtree/magic-design",
3
3
  "author": "Cylon Team",
4
- "version": "2.0.77",
4
+ "version": "2.0.78",
5
5
  "description": "Magic Design",
6
6
  "license": "MIT",
7
7
  "module": "es/index.js",