@ray-js/t-agent-plugin-aistream 0.2.0-beta-3 → 0.2.0-beta-4

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.
@@ -36,8 +36,11 @@ export interface ChatHistoryStore<Key = number> {
36
36
  }>;
37
37
  /** 更新消息 */
38
38
  update(id: Key, body: ChatMessageObject): Promise<void>;
39
+ /** 删除消息 */
39
40
  remove(id: Key): Promise<void>;
40
- removeAll(): Promise<void>;
41
+ /** 批量删除消息 */
42
+ removeAll(ids?: Key[]): Promise<void>;
43
+ /** 插入消息 */
41
44
  insert(body: ChatMessageObject): Promise<{
42
45
  id: Key;
43
46
  }>;
@@ -57,7 +60,7 @@ export declare class ChatHistoryLocalStore implements ChatHistoryStore {
57
60
  }>;
58
61
  update(id: number, body: ChatMessageObject): Promise<void>;
59
62
  remove(id: number): Promise<void>;
60
- removeAll(): Promise<void>;
63
+ removeAll(ids?: number[]): Promise<void>;
61
64
  insert(message: ChatMessageObject): Promise<{
62
65
  id: number;
63
66
  }>;
@@ -150,8 +150,20 @@ export class ChatHistoryLocalStore {
150
150
  id: [id]
151
151
  });
152
152
  }
153
- async removeAll() {
154
- await deleteRecordList(this.getQueryCondition());
153
+ async removeAll(ids) {
154
+ if (ids) {
155
+ if (!Array.isArray(ids)) {
156
+ throw new Error('ids is not array');
157
+ }
158
+ if (ids.length === 0) {
159
+ return;
160
+ }
161
+ await deleteRecordList(_objectSpread({
162
+ id: ids
163
+ }, this.getQueryCondition()));
164
+ } else {
165
+ await deleteRecordList(this.getQueryCondition());
166
+ }
155
167
  }
156
168
  async insert(message) {
157
169
  const nowTime = Date.now();
@@ -35,19 +35,20 @@ mock.hooks.hook('getMiniAppConfig', context => {
35
35
  config: {}
36
36
  };
37
37
  });
38
+ const getCurrentConnection = () => {
39
+ return mock.data.get('currentConnection');
40
+ };
38
41
  mock.hooks.hook('connect', context => {
39
- const {
40
- options
41
- } = context;
42
- const key = "connection-".concat(options.clientType, "-").concat(options.deviceId || '');
43
- if (!mock.data.has(key)) {
44
- const connectionId = generateId();
45
- mock.data.set(key, {
46
- connectionId
47
- });
48
- mock.data.set("connection-".concat(connectionId), key);
42
+ let connection = getCurrentConnection();
43
+ if (connection) {
44
+ throw new Error('already connected');
45
+ } else {
46
+ connection = {
47
+ connectionId: generateId()
48
+ };
49
+ mock.data.set('currentConnection', connection);
49
50
  }
50
- context.result = mock.data.get(key);
51
+ context.result = connection;
51
52
  });
52
53
  mock.hooks.hook('disconnect', context => {
53
54
  const {
@@ -55,9 +56,15 @@ mock.hooks.hook('disconnect', context => {
55
56
  connectionId
56
57
  }
57
58
  } = context;
58
- const key = mock.data.get("connection-".concat(connectionId));
59
- mock.data.delete(key);
60
- mock.data.delete("connection-".concat(connectionId));
59
+ const connection = getCurrentConnection();
60
+ if (!connection) {
61
+ throw new Error('not connected');
62
+ }
63
+ if (connection.connectionId !== connectionId) {
64
+ throw new Error('connectionId mismatch');
65
+ }
66
+ mock.data.set('currentConnection', null);
67
+ mock.data.set('sessionMap', new Map());
61
68
  });
62
69
  mock.hooks.hook('queryAgentToken', context => {
63
70
  context.result = {
@@ -107,6 +114,14 @@ mock.hooks.hook('createSession', context => {
107
114
  sendDataChannels: session.sendDataChannels,
108
115
  revDataChannels: session.revDataChannels
109
116
  };
117
+
118
+ // setTimeout(() => {
119
+ // dispatch('onConnectStateChanged', {
120
+ // connectionId: getCurrentConnection().connectionId,
121
+ // connectState: ConnectState.DISCONNECTED,
122
+ // code: 200,
123
+ // });
124
+ // }, 1000);
110
125
  });
111
126
  mock.hooks.hook('closeSession', context => {
112
127
  const map = mock.data.get('sessionMap');
@@ -259,6 +274,7 @@ mock.hooks.hook('startRecordAndSendAudioData', context => {
259
274
  let text = '';
260
275
  controller.signal.addEventListener('abort', async () => {
261
276
  var _finishResolve;
277
+ // 终止识别到出完整结果的延迟
262
278
  await mock.sleep(300);
263
279
  session.replyText(StreamFlag.IN_PROGRESS, {
264
280
  bizType: ReceivedTextPacketType.ASR,
@@ -278,6 +294,8 @@ mock.hooks.hook('startRecordAndSendAudioData', context => {
278
294
  }
279
295
  (_finishResolve = finishResolve) === null || _finishResolve === void 0 || _finishResolve();
280
296
  });
297
+
298
+ // 识别 ASR 的延迟
281
299
  await mock.sleep(100);
282
300
  if (controller.signal.aborted) {
283
301
  return;
package/dist/utils/ttt.js CHANGED
@@ -2,7 +2,7 @@ import { listening, promisify } from './promisify';
2
2
  import { ConnectClientType, ConnectState } from '../AIStreamTypes';
3
3
  import { generateId } from '@ray-js/t-agent';
4
4
  export const getMiniAppConfig = promisify(ty.getMiniAppConfig, true);
5
- export const getAccountInfo = promisify(ty.getAccountInfo, true);
5
+ export const getAccountInfo = promisify(ty.getAccountInfo);
6
6
  export const isDevTools = () => {
7
7
  return ty.getSystemInfoSync().brand === 'devtools';
8
8
  };
@@ -351,10 +351,19 @@ export function withAIStream() {
351
351
  payload
352
352
  } = context;
353
353
  const message = session.messages.get(payload.messageId);
354
+ if (!message) {
355
+ throw new Error('message not found');
356
+ }
354
357
  const {
355
358
  eventId,
356
359
  sessionId
357
- } = (message === null || message === void 0 ? void 0 : message.meta) || {};
360
+ } = message.meta;
361
+ if (!eventId || !sessionId) {
362
+ context.result = {
363
+ success: true
364
+ };
365
+ return;
366
+ }
358
367
  await feedback({
359
368
  requestId: [eventId, sessionId].join('#'),
360
369
  type: payload.rate
@@ -443,9 +452,6 @@ export function withAIStream() {
443
452
  Array.from(messages).forEach(message => {
444
453
  message.remove();
445
454
  });
446
- if (options !== null && options !== void 0 && options.indexId) {
447
- return;
448
- }
449
455
  // 清空缓存的数据
450
456
  const historyStore = getHistoryStore();
451
457
  if (historyStore) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/t-agent-plugin-aistream",
3
- "version": "0.2.0-beta-3",
3
+ "version": "0.2.0-beta-4",
4
4
  "author": "Tuya.inc",
5
5
  "license": "MIT",
6
6
  "private": false,
@@ -35,5 +35,5 @@
35
35
  "devDependencies": {
36
36
  "@types/url-parse": "^1.4.11"
37
37
  },
38
- "gitHead": "03c4af7fe4cfe27069db83d99a61042c8053399e"
38
+ "gitHead": "1728f8a1cac6b2ca53e506e06171808fbf38efdc"
39
39
  }