@ray-js/t-agent-plugin-aistream 0.2.5-beta-1 → 0.2.5-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.
@@ -683,7 +683,8 @@ export declare enum AIStreamAppErrorCode {
683
683
  INVALID_DATA_PACKET = 39008,
684
684
  FILE_DATA_READ_ERROR = 39009,
685
685
  DATA_SEND_FAILED = 39010,
686
- CONNECTION_CLOSED_BY_REMOTE = 39012
686
+ CONNECTION_CLOSED_BY_REMOTE = 39012,
687
+ AUDIO_UNEXPECTEDLY_RESET = 39014
687
688
  }
688
689
  export declare enum AIStreamServerErrorCode {
689
690
  OK = 200,
@@ -147,6 +147,7 @@ export let AIStreamAppErrorCode = /*#__PURE__*/function (AIStreamAppErrorCode) {
147
147
  AIStreamAppErrorCode[AIStreamAppErrorCode["FILE_DATA_READ_ERROR"] = 39009] = "FILE_DATA_READ_ERROR";
148
148
  AIStreamAppErrorCode[AIStreamAppErrorCode["DATA_SEND_FAILED"] = 39010] = "DATA_SEND_FAILED";
149
149
  AIStreamAppErrorCode[AIStreamAppErrorCode["CONNECTION_CLOSED_BY_REMOTE"] = 39012] = "CONNECTION_CLOSED_BY_REMOTE";
150
+ AIStreamAppErrorCode[AIStreamAppErrorCode["AUDIO_UNEXPECTEDLY_RESET"] = 39014] = "AUDIO_UNEXPECTEDLY_RESET";
150
151
  return AIStreamAppErrorCode;
151
152
  }({});
152
153
  export let AIStreamServerErrorCode = /*#__PURE__*/function (AIStreamServerErrorCode) {
@@ -66,6 +66,7 @@ export declare class AsrAgent {
66
66
  /** 开始录音时长监听 */
67
67
  private startRecordTimer;
68
68
  private _start;
69
+ private handleAudioRecorderResetError;
69
70
  private _abort;
70
71
  private _stop;
71
72
  start(): Promise<void>;
@@ -4,7 +4,7 @@ import "core-js/modules/es.json.stringify.js";
4
4
  import "core-js/modules/es.promise.finally.js";
5
5
  import "core-js/modules/web.dom-collections.iterator.js";
6
6
  import { authorize, authorizeStatus, getCurrentHomeInfo, initAudioRecorder } from '../utils';
7
- import { AIStreamAttributePayloadType, AIStreamAttributeType, ConnectClientType, ConnectState, ReceivedTextPacketType, SessionState } from '../AIStreamTypes';
7
+ import { AIStreamAppErrorCode, AIStreamAttributePayloadType, AIStreamAttributeType, ConnectClientType, ConnectState, ReceivedTextPacketType, SessionState } from '../AIStreamTypes';
8
8
  import { DEFAULT_TOKEN_API, DEFAULT_TOKEN_API_VERSION, globalAIStreamClient } from '../global';
9
9
  import { Logger, safeParseJSON } from '@ray-js/t-agent';
10
10
  import logger from '../utils/logger';
@@ -69,14 +69,17 @@ export class AsrAgent {
69
69
 
70
70
  /** 获取录音权限 */
71
71
  getRecordScope() {
72
- if (this.authorized !== null) {
72
+ if (this.authorized === true) {
73
73
  return Promise.resolve(this.authorized);
74
74
  }
75
75
  return authorize({
76
76
  scope: 'scope.record'
77
- }).then(() => true, () => false).then(authorized => {
78
- this.authorized = authorized;
79
- return authorized;
77
+ }).then(() => {
78
+ this.authorized = true;
79
+ return true;
80
+ }, () => {
81
+ this.authorized = false;
82
+ return false;
80
83
  });
81
84
  }
82
85
 
@@ -152,6 +155,9 @@ export class AsrAgent {
152
155
  if (finished) {
153
156
  return;
154
157
  }
158
+ if (this.activeEvent && error) {
159
+ this.activeEvent.abort();
160
+ }
155
161
  this.audioStream = null;
156
162
  this.activeEvent = null;
157
163
  finished = true;
@@ -233,11 +239,36 @@ export class AsrAgent {
233
239
  finish(error);
234
240
  });
235
241
  const [error] = await tryCatch(() => audioStream.start());
236
- if (error) {
237
- finish(error);
242
+ if (!error) {
243
+ this.startRecordTimer();
238
244
  return;
239
245
  }
240
- this.startRecordTimer();
246
+ const errorCode = error === null || error === void 0 ? void 0 : error.code;
247
+ if (errorCode === AIStreamAppErrorCode.AUDIO_UNEXPECTEDLY_RESET) {
248
+ const handled = await this.handleAudioRecorderResetError(audioStream, finish);
249
+ if (handled) return;
250
+ }
251
+ finish(error);
252
+ }
253
+ async handleAudioRecorderResetError(audioStream, finish) {
254
+ logger.debug('AsrAgent: Audio recorder was reset, reinitializing...');
255
+ try {
256
+ const {
257
+ recordingOptions
258
+ } = this.options || {};
259
+ const sampleRate = recordingOptions === null || recordingOptions === void 0 ? void 0 : recordingOptions.sampleRate;
260
+ await initAudioRecorder(sampleRate ? {
261
+ sampleRate
262
+ } : {});
263
+ audioStream.started = false;
264
+ await audioStream.start();
265
+ this.startRecordTimer();
266
+ return true;
267
+ } catch (reinitError) {
268
+ logger.error('AsrAgent: Failed to reinitialize audio recorder', reinitError);
269
+ finish(reinitError);
270
+ return false;
271
+ }
241
272
  }
242
273
  async _abort() {
243
274
  this.status = AsrAgentStatus.ABORTING;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/t-agent-plugin-aistream",
3
- "version": "0.2.5-beta-1",
3
+ "version": "0.2.5-beta-3",
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": "810116f5c6fc08b98db2f7d5725f2bacbedfeaf1"
38
+ "gitHead": "99cc77a0257a83aae0203fd870df4163355b1f19"
39
39
  }