@ray-js/t-agent-plugin-aistream 0.2.0-beta-14 → 0.2.0-beta-15

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.
@@ -120,7 +120,7 @@ export declare class AIStreamEvent {
120
120
  }): Promise<void>;
121
121
  abort(options?: {
122
122
  userData?: Attribute[];
123
- }): Promise<void>;
123
+ }): void;
124
124
  on(name: 'close', callback: () => void): void;
125
125
  on(name: 'finish', callback: () => void): void;
126
126
  on(name: 'error', callback: (error: any) => void): void;
@@ -374,6 +374,7 @@ export class AIStreamEvent {
374
374
  if (this.streams[dataChannel]) {
375
375
  throw new AIStreamEventError("".concat(dataChannel, " stream already exists"), AIStreamErrorCode.AIStreamError);
376
376
  }
377
+ let startPromise = null;
377
378
  const stream = {
378
379
  dataChannel,
379
380
  type: source.type,
@@ -392,11 +393,12 @@ export class AIStreamEvent {
392
393
  count: source.amplitudeCount
393
394
  });
394
395
  }
395
- await startRecordAndSendAudioData({
396
+ startPromise = startRecordAndSendAudioData({
396
397
  sessionId: this.sessionId,
397
398
  dataChannel,
398
399
  userData: source.userData
399
400
  });
401
+ await startPromise;
400
402
  } else if (source.type === 'video') {
401
403
  // const cameraType = source.cameraType || VideoCameraType.BACK;
402
404
  // await startRecordAndSendVideoData({
@@ -410,13 +412,22 @@ export class AIStreamEvent {
410
412
  if (!stream.started || this.closed) {
411
413
  return;
412
414
  }
415
+ // 一定要等录音开始,才能结束
416
+ if (startPromise) {
417
+ try {
418
+ await startPromise;
419
+ } catch (e) {
420
+ // pass
421
+ }
422
+ startPromise = null;
423
+ }
413
424
  if (source.type === 'audio') {
414
425
  if (source.amplitudeCount) {
415
426
  unregisterVoiceAmplitudes({
416
427
  count: source.amplitudeCount
417
428
  });
418
429
  }
419
- await stopRecordAndSendAudioData({
430
+ stopRecordAndSendAudioData({
420
431
  sessionId: this.sessionId,
421
432
  dataChannel,
422
433
  userData: source.userData
@@ -452,7 +463,7 @@ export class AIStreamEvent {
452
463
  userData: options === null || options === void 0 ? void 0 : options.userData
453
464
  });
454
465
  }
455
- async abort(options) {
466
+ abort(options) {
456
467
  if (this.closed) {
457
468
  return;
458
469
  }
@@ -124,7 +124,7 @@ mock.hooks.hook('createSession', context => {
124
124
  // 用于测试断开连接;
125
125
  // setTimeout(() => {
126
126
  // dispatch('onConnectStateChanged', {
127
- // connectionId: getCurrentConnection().connectionId,
127
+ // connectionId: '',
128
128
  // connectState: ConnectState.DISCONNECTED,
129
129
  // code: 200,
130
130
  // });
@@ -134,7 +134,7 @@ mock.hooks.hook('createSession', context => {
134
134
  // s.closed = true;
135
135
  // });
136
136
  // mock.data.set('sessionMap', new Map());
137
- // }, 3000);
137
+ // }, 5000);
138
138
 
139
139
  // 用于测试断开会话
140
140
  // setTimeout(() => {
@@ -60,14 +60,14 @@ export function sendBlocksToAIStream(params) {
60
60
  controller.enqueue(part);
61
61
  };
62
62
  let aborting = false;
63
- const abort = async () => {
63
+ const abort = () => {
64
64
  if (aborting || canceled || closed) {
65
65
  return;
66
66
  }
67
67
  aborting = true;
68
68
  canceled = true;
69
69
  if (event) {
70
- await event.abort();
70
+ event.abort();
71
71
  }
72
72
  aborting = false;
73
73
  };
@@ -80,15 +80,6 @@ export function sendBlocksToAIStream(params) {
80
80
  }
81
81
  }
82
82
  };
83
-
84
- // signal?.addEventListener('abort', async () => {
85
- // logger.debug('sendBlocksToAIStream signal aborted');
86
- // abortPromise = abort();
87
- // await abortPromise;
88
- // logger.debug('sendBlocksToAIStream signal aborted done');
89
- // close();
90
- // });
91
-
92
83
  let pendingCancel = false;
93
84
  if (audioEmitter) {
94
85
  audioEmitter.addEventListener('confirm', () => {
@@ -133,7 +124,7 @@ export function sendBlocksToAIStream(params) {
133
124
  }
134
125
  if (signal !== null && signal !== void 0 && signal.aborted || pendingCancel) {
135
126
  logger.debug('sendBlocksToAIStream pendingCancel aborted');
136
- await abort();
127
+ abort();
137
128
  close();
138
129
  return;
139
130
  }
@@ -330,21 +321,21 @@ export function sendBlocksToAIStream(params) {
330
321
  await s.stop();
331
322
  }
332
323
  logger.debug('sendBlocksToAIStream audio cancel aborted');
333
- await abort();
324
+ abort();
334
325
  resolve();
335
326
  });
336
327
  s.start();
337
328
  });
338
329
  if (signal !== null && signal !== void 0 && signal.aborted) {
339
330
  logger.debug('sendBlocksToAIStream after audio aborted');
340
- await abort();
331
+ abort();
341
332
  close();
342
333
  return;
343
334
  }
344
335
  }
345
336
  await event.end();
346
337
  if (signal !== null && signal !== void 0 && signal.aborted) {
347
- await abort();
338
+ abort();
348
339
  close();
349
340
  }
350
341
  },
@@ -353,7 +344,7 @@ export function sendBlocksToAIStream(params) {
353
344
  var _event;
354
345
  canceled = true;
355
346
  logger.debug('sendBlocksToAIStream stream canceled', reason);
356
- await ((_event = event) === null || _event === void 0 ? void 0 : _event.abort());
347
+ (_event = event) === null || _event === void 0 || _event.abort();
357
348
  }
358
349
  }
359
350
  });
@@ -235,6 +235,7 @@ export function withAIStream() {
235
235
  let end = false;
236
236
  audioPromise = new Promise((resolve, reject) => {
237
237
  // 当确认发送时,展示 loading
238
+ let userMsgShow = false;
238
239
  const onConfirm = async () => {
239
240
  if (end) {
240
241
  return;
@@ -243,6 +244,7 @@ export function withAIStream() {
243
244
  status: ChatMessageStatus.START
244
245
  });
245
246
  userMsg.bubble.setStatus(BubbleTileStatus.NORMAL);
247
+ userMsgShow = true; // show 会延迟变更 isShow 状态,所以使用 userMsgShow
246
248
  await userMsg.show();
247
249
  audioEmitter.removeEventListener('confirm', onConfirm);
248
250
  };
@@ -253,10 +255,8 @@ export function withAIStream() {
253
255
  if (end) {
254
256
  return;
255
257
  }
256
- if (!event.detail.text) {
257
- if (userMsg.isShow) {
258
- await userMsg.remove();
259
- }
258
+ if (!event.detail.text && userMsgShow) {
259
+ await userMsg.remove();
260
260
  reject(new Error('No text found in audio event'));
261
261
  return;
262
262
  }
@@ -274,21 +274,20 @@ export function withAIStream() {
274
274
  audioEmitter.addEventListener('finish', onFinished);
275
275
  const onCancel = async () => {
276
276
  end = true;
277
- if (!response.started) {
278
- if (userMsg.isShow) {
279
- await userMsg.remove();
280
- }
277
+ if (!response.started && userMsgShow) {
278
+ await userMsg.remove();
281
279
  }
282
280
  audioEmitter.removeEventListener('cancel', onCancel);
283
281
  reject(new Error('User cancel'));
284
282
  };
285
283
  audioEmitter.addEventListener('cancel', onCancel);
286
284
  signal.addEventListener('abort', () => {
285
+ logger.log('withAIStream chat agent.chat audioEmitter onAbort');
287
286
  audioEmitter.dispatchEvent(new EmitterEvent('cancel'));
288
287
  });
289
288
  const onError = async () => {
290
289
  end = true;
291
- if (userMsg.isShow && userMsg.status !== ChatMessageStatus.FINISH) {
290
+ if (userMsgShow && userMsg.status !== ChatMessageStatus.FINISH) {
292
291
  await userMsg.remove();
293
292
  }
294
293
  audioEmitter.removeEventListener('error', onError);
@@ -419,7 +418,7 @@ export function withAIStream() {
419
418
  await hooks.callHook('onTTTAction', tile, result);
420
419
  if (result.action) {
421
420
  await runTTTAction(result.action, {
422
- onSendSkills: async () => {
421
+ onSendSkills: async (block, options) => {
423
422
  throw new Error('send skill not support');
424
423
  },
425
424
  onSendMessages: async (blocks, sendImmediately) => {
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-14",
3
+ "version": "0.2.0-beta-15",
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": "2fdaf9e5abc36632f003b7060ada22390cda132b"
38
+ "gitHead": "f6fabb33ac8ef9cb264fbde8bec12dacc1d63ed7"
39
39
  }