@ray-js/t-agent-plugin-aistream 0.2.8-beta.1 → 0.2.8-beta.3

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.
@@ -93,65 +93,79 @@ export function withAIStream() {
93
93
  clientType,
94
94
  deviceId
95
95
  });
96
- const streamSession = connection.createSession({
97
- ownerId: clientType === ConnectClientType.DEVICE ? deviceId : "".concat(homeId),
98
- api: tokenOptions.api,
99
- apiVersion: tokenOptions.version,
100
- solutionCode: agentId,
101
- extParams: _objectSpread({
102
- needTts: !!options.enableTts,
103
- deviceId
104
- }, tokenOptions.extParams),
105
- getSessionUserData: async () => {
106
- const result = {
107
- userData: {}
108
- };
109
- await hooks.callHook('onUserDataRead', 'create-session', {}, result);
110
- return result.userData;
111
- }
112
- });
113
- streamSession.on('sessionEvent', event => {
114
- hooks.callHook('onSessionEventReceived', event);
115
- });
116
- await session.set('AIStream.streamSession', streamSession);
117
- if (options.earlyStart) {
118
- // 故意异步,不阻塞消息列表加载
119
- streamSession.ensureSession().catch(error => {
120
- logger.error('earlyStart failed', error);
96
+ let streamSession = null;
97
+ try {
98
+ streamSession = connection.createSession({
99
+ ownerId: clientType === ConnectClientType.DEVICE ? deviceId : "".concat(homeId),
100
+ api: tokenOptions.api,
101
+ apiVersion: tokenOptions.version,
102
+ solutionCode: agentId,
103
+ extParams: _objectSpread({
104
+ needTts: !!options.enableTts,
105
+ deviceId
106
+ }, tokenOptions.extParams),
107
+ getSessionUserData: async () => {
108
+ const result = {
109
+ userData: {}
110
+ };
111
+ await hooks.callHook('onUserDataRead', 'create-session', {}, result);
112
+ return result.userData;
113
+ }
121
114
  });
122
- }
123
-
124
- /*
125
- * 2. 获取历史消息
126
- */
127
- let historyStore;
128
- if (typeof options.createChatHistoryStore === 'function') {
129
- historyStore = options.createChatHistoryStore(agent);
130
- } else {
131
- historyStore = new ChatHistoryLocalStore({
132
- bizCode: BizCode.CHAT,
133
- agentId,
134
- deviceId,
135
- homeId,
136
- indexId
115
+ streamSession.on('sessionEvent', event => {
116
+ hooks.callHook('onSessionEventReceived', event);
137
117
  });
138
- }
139
- if (historyStore) {
140
- await agent.session.set('AIStream.historyStore', historyStore);
141
- const {
142
- records
143
- } = await historyStore.queryAll({
144
- sort: 'desc',
145
- // id 倒序去获取最新的消息list,然后再reverse,保证最新的消息在最前面
146
- limit: options.historySize || 1000,
147
- offset: 0
118
+ await session.set('AIStream.connection', connection);
119
+ await session.set('AIStream.streamSession', streamSession);
120
+ if (options.earlyStart) {
121
+ // 故意异步,不阻塞消息列表加载
122
+ streamSession.ensureSession().catch(error => {
123
+ logger.error('earlyStart failed', error);
124
+ });
125
+ }
126
+
127
+ /*
128
+ * 2. 获取历史消息
129
+ */
130
+ let historyStore;
131
+ if (typeof options.createChatHistoryStore === 'function') {
132
+ historyStore = options.createChatHistoryStore(agent);
133
+ } else {
134
+ historyStore = new ChatHistoryLocalStore({
135
+ bizCode: BizCode.CHAT,
136
+ agentId,
137
+ deviceId,
138
+ homeId,
139
+ indexId
140
+ });
141
+ }
142
+ if (historyStore) {
143
+ await agent.session.set('AIStream.historyStore', historyStore);
144
+ const {
145
+ records
146
+ } = await historyStore.queryAll({
147
+ sort: 'desc',
148
+ // id 倒序去获取最新的消息list,然后再reverse,保证最新的消息在最前面
149
+ limit: options.historySize || 1000,
150
+ offset: 0
151
+ });
152
+ await session.set('AIStream.rawMessages', records.reverse());
153
+ session.isNewChat = records.length === 0;
154
+ } else {
155
+ session.isNewChat = true;
156
+ }
157
+ session.sessionId = indexId;
158
+ } catch (error) {
159
+ if (streamSession) {
160
+ await streamSession.close().catch(closeError => {
161
+ logger.error('withAIStream cleanup streamSession failed', closeError);
162
+ });
163
+ }
164
+ await connection.close().catch(closeError => {
165
+ logger.error('withAIStream cleanup connection failed', closeError);
148
166
  });
149
- await session.set('AIStream.rawMessages', records.reverse());
150
- session.isNewChat = records.length === 0;
151
- } else {
152
- session.isNewChat = true;
167
+ throw error;
153
168
  }
154
- session.sessionId = indexId;
155
169
  });
156
170
  const getHistoryStore = () => {
157
171
  const historyStore = session.get('AIStream.historyStore');
@@ -431,6 +445,7 @@ export function withAIStream() {
431
445
  });
432
446
  await hooks.callHook('onChatMessageSent', userMsg, message);
433
447
  const skills = [];
448
+ const attachments = [];
434
449
  logger.debug('withAIStream chat agent.flushStreamToShow');
435
450
  const result = {
436
451
  messages: await agent.flushStreamToShow(message, response, {
@@ -445,17 +460,28 @@ export function withAIStream() {
445
460
  const result = {
446
461
  messages: []
447
462
  };
463
+ await hooks.callHook('onAttachmentCompose', part, respMsg, result);
464
+ attachments.push(part);
448
465
  if (part.attachmentType === 'skill') {
449
- skills.push(part.attachment);
450
- await hooks.callHook('onSkillCompose', part.attachment, respMsg, result);
466
+ const skill = part.attachment;
467
+ skills.push(skill);
468
+ await hooks.callHook('onSkillCompose', skill, respMsg, result);
451
469
  }
452
470
  return result.messages;
453
471
  }
454
472
  })
455
473
  };
456
474
  logger.debug('withAIStream chat agent.flushStreamToShow end');
475
+ let changed = false;
457
476
  if (skills.length) {
477
+ changed = true;
458
478
  await hooks.callHook('onSkillsEnd', skills, message, result);
479
+ }
480
+ if (attachments.length) {
481
+ changed = true;
482
+ await hooks.callHook('onAttachmentsEnd', attachments, message, result);
483
+ }
484
+ if (changed) {
459
485
  await message.update();
460
486
  }
461
487
  let valid = false;
@@ -533,6 +559,7 @@ export function withAIStream() {
533
559
  });
534
560
  onAgentDispose(async () => {
535
561
  const streamSession = session.get('AIStream.streamSession');
562
+ const connection = session.get('AIStream.connection');
536
563
 
537
564
  // 比较低的概率 onAgentStart 还没跑完(卡在获取获取家庭信息)就调用了 dispose
538
565
  if (streamSession) {
@@ -541,6 +568,9 @@ export function withAIStream() {
541
568
  */
542
569
  await streamSession.close();
543
570
  }
571
+ if (connection) {
572
+ await connection.close();
573
+ }
544
574
  });
545
575
  const handleTTTAction = async (tile, tttAction) => {
546
576
  const result = {
@@ -611,7 +641,7 @@ export function withAIStream() {
611
641
  }
612
642
  }
613
643
  if ((_skill$general = skill.general) !== null && _skill$general !== void 0 && (_skill$general = _skill$general.data) !== null && _skill$general !== void 0 && _skill$general.aiCards) {
614
- for (const card of skill.custom.data.aiCards) {
644
+ for (const card of skill.general.data.aiCards) {
615
645
  cards.push(card);
616
646
  }
617
647
  }
@@ -664,6 +694,12 @@ export function withAIStream() {
664
694
  onSessionEventReceived: fn => {
665
695
  return hooks.hook('onSessionEventReceived', fn);
666
696
  },
697
+ onAttachmentCompose: fn => {
698
+ return hooks.hook('onAttachmentCompose', fn);
699
+ },
700
+ onAttachmentsEnd: fn => {
701
+ return hooks.hook('onAttachmentsEnd', fn);
702
+ },
667
703
  sendEvent: params => sendEventToAIStream(params),
668
704
  onTTTAction: fn => {
669
705
  return hooks.hook('onTTTAction', fn);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/t-agent-plugin-aistream",
3
- "version": "0.2.8-beta.1",
3
+ "version": "0.2.8-beta.3",
4
4
  "author": "Tuya.inc",
5
5
  "license": "MIT",
6
6
  "private": false,
@@ -21,10 +21,11 @@
21
21
  "scripts": {
22
22
  "dev": "ray start --type=component --output dist --emit-declaration-dev",
23
23
  "build": "ray build --type=component --output dist",
24
- "clean": "rimraf ./dist"
24
+ "clean": "rimraf ./dist",
25
+ "test": "jest --runInBand",
26
+ "test:coverage": "jest --runInBand --coverage"
25
27
  },
26
28
  "dependencies": {
27
- "dayjs": "^1.10.4",
28
29
  "url-parse": "^1.5.10",
29
30
  "web-streams-polyfill": "^4.0.0"
30
31
  },
@@ -35,5 +36,5 @@
35
36
  "devDependencies": {
36
37
  "@types/url-parse": "^1.4.11"
37
38
  },
38
- "gitHead": "040499ca11aedda3246121f880807da0f8af3cc9"
39
+ "gitHead": "938bc0c7da7d23dbbad8c3c14c97e5f4007f06f7"
39
40
  }