@ray-js/t-agent-plugin-aistream 0.2.3-beta-3 → 0.2.3-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.
@@ -791,6 +791,18 @@ export type StartPlayAudioParams = {
791
791
  }) => void;
792
792
  complete?: () => void;
793
793
  };
794
+ export type StopPlayAudioParams = {
795
+ success?: (params: null) => void;
796
+ fail?: (params: {
797
+ errorMsg: string;
798
+ errorCode: string | number;
799
+ innerError: {
800
+ errorCode: string | number;
801
+ errorMsg: string;
802
+ };
803
+ }) => void;
804
+ complete?: () => void;
805
+ };
794
806
  export type Attribute = {
795
807
  /** Attribute 类型 */
796
808
  type: AIStreamAttributeType;
@@ -862,6 +874,67 @@ export type AudioBody = {
862
874
  */
863
875
  bitDepth?: number;
864
876
  };
877
+ export declare enum AudioPlayChangedState {
878
+ /** 播放开始 */
879
+ START = 1,
880
+ /** 缓冲中 */
881
+ BUFFERING = 2,
882
+ /** 缓存完成继续播放 */
883
+ BUFFERED = 3,
884
+ /** 播放完成 */
885
+ COMPLETED = 4,
886
+ /** 播放异常 */
887
+ ERROR = 5
888
+ }
889
+ export type AudioPlayChangedBody = {
890
+ path: string;
891
+ /** 播放状态: 1-播放开始, 2-缓冲中, 3-缓存完成 继续播放, 4-播放完成, 5-播放异常 */
892
+ state: AudioPlayChangedState;
893
+ /** 播放异常码: 1-主动停止播放, 2-播放被异常中断, ... */
894
+ code?: number;
895
+ };
896
+ export interface AIStreamAudioFile {
897
+ /** 音频缓存路径 */
898
+ path: string;
899
+ /**
900
+ * 音频编码类型,表示音频数据的编码格式
901
+ * - 100: ADPCM
902
+ * - 101: PCM
903
+ * - 102: AACRaw
904
+ * - 103: AACADTS
905
+ * - 104: AACLATM
906
+ * - 105: G711U
907
+ * - 106: G711A
908
+ * - 107: G726
909
+ * - 108: SPEEX
910
+ * - 109: MP3
911
+ * - 110: G722
912
+ * - 111: Opus
913
+ */
914
+ codecType: number;
915
+ /**
916
+ * 音频采样率,单位Hz
917
+ * 表示每秒采样次数,常见值:8000, 16000, 44100等
918
+ */
919
+ sampleRate: number;
920
+ /**
921
+ * 音频位深,表示每个采样点的位数
922
+ * 常见值:8, 16, 24, 32等
923
+ */
924
+ bitDepth: number;
925
+ /**
926
+ * 音频通道数
927
+ * - 0: 单声道
928
+ * - 1: 立体声
929
+ */
930
+ channels: number;
931
+ }
932
+ export type RecordAndSendAudioFailBody = {
933
+ /**
934
+ * 录制发送异常: 1-发送数据异常, 2-录制音频异常
935
+ */
936
+ code: 0 | 1;
937
+ };
865
938
  export type VideoBody = {
866
939
  /** 接收数据通道 */
867
940
  dataChannel: string;
@@ -1217,6 +1290,10 @@ export type SendEventChatBreakParams = {
1217
1290
  *@description 开始录制并发送音频数据
1218
1291
  */
1219
1292
  export type StartRecordAndSendAudioDataParams = {
1293
+ saveFile?: boolean;
1294
+ recordInitParams?: {
1295
+ sampleRate?: number;
1296
+ };
1220
1297
  /** 会话 id */
1221
1298
  sessionId: string;
1222
1299
  /** 下发数据通道,当音频只有单路的时候,可以不传 */
@@ -1241,9 +1318,7 @@ export type StopRecordAndSendAudioDataParams = {
1241
1318
  dataChannel?: string;
1242
1319
  /** 扩展属性 */
1243
1320
  userData?: Attribute[];
1244
- success?: (params: {
1245
- filePath: string;
1246
- } | null) => void;
1321
+ success?: (params: AIStreamAudioFile | null) => void;
1247
1322
  fail?: (params: {
1248
1323
  errorMsg: string;
1249
1324
  errorCode: string | number;
@@ -1345,6 +1420,7 @@ export type SendTextDataParams = {
1345
1420
  complete?: () => void;
1346
1421
  };
1347
1422
  export type InitAudioRecorderParams = {
1423
+ sampleRate?: number;
1348
1424
  success?: (params: null) => void;
1349
1425
  fail?: (params: {
1350
1426
  errorMsg: string;
@@ -187,6 +187,14 @@ export let AIStreamAttributePayloadType = /*#__PURE__*/function (AIStreamAttribu
187
187
  AIStreamAttributePayloadType[AIStreamAttributePayloadType["STRING"] = 6] = "STRING";
188
188
  return AIStreamAttributePayloadType;
189
189
  }({});
190
+ export let AudioPlayChangedState = /*#__PURE__*/function (AudioPlayChangedState) {
191
+ AudioPlayChangedState[AudioPlayChangedState["START"] = 1] = "START";
192
+ AudioPlayChangedState[AudioPlayChangedState["BUFFERING"] = 2] = "BUFFERING";
193
+ AudioPlayChangedState[AudioPlayChangedState["BUFFERED"] = 3] = "BUFFERED";
194
+ AudioPlayChangedState[AudioPlayChangedState["COMPLETED"] = 4] = "COMPLETED";
195
+ AudioPlayChangedState[AudioPlayChangedState["ERROR"] = 5] = "ERROR";
196
+ return AudioPlayChangedState;
197
+ }({});
190
198
  export let NetworkType = /*#__PURE__*/function (NetworkType) {
191
199
  NetworkType["NONE"] = "none";
192
200
  NetworkType["CELL_2G"] = "2g";
@@ -1,4 +1,4 @@
1
- import { ConnectClientType } from '../AIStreamTypes';
1
+ import { AIStreamAudioFile, ConnectClientType } from '../AIStreamTypes';
2
2
  export interface AsrAgentOptions {
3
3
  agentId: string;
4
4
  /** 获取 agent token 的参数 */
@@ -13,9 +13,11 @@ export interface AsrAgentOptions {
13
13
  clientType?: ConnectClientType;
14
14
  deviceId?: string;
15
15
  onMessage?: (message: {
16
+ type: 'text';
16
17
  text: string;
17
18
  } | {
18
- filePath: string;
19
+ type: 'file';
20
+ file: AIStreamAudioFile;
19
21
  }) => void;
20
22
  onFinish?: () => void;
21
23
  onError?: (error: any) => void;
@@ -23,7 +25,7 @@ export interface AsrAgentOptions {
23
25
  onAbort?: () => void;
24
26
  recordingOptions?: {
25
27
  /** 是否需要保存音频 */
26
- needSave?: boolean;
28
+ saveFile?: boolean;
27
29
  /** 保存的音频采样率,单位:hz */
28
30
  sampleRate?: number;
29
31
  /** 最长录制时长,单位:毫秒 */
@@ -14,9 +14,15 @@ export class AsrAgent {
14
14
  /** 录音时长定时器 */
15
15
  _defineProperty(this, "recordDurationTimer", null);
16
16
  this.options = options;
17
- if (this.options.earlyStart) {
17
+ if (options.earlyStart) {
18
+ const {
19
+ recordingOptions
20
+ } = options;
21
+ const sampleRate = recordingOptions === null || recordingOptions === void 0 ? void 0 : recordingOptions.sampleRate;
18
22
  // 如果需要提前启动,直接初始化和创建连接
19
- this.createSession().then(() => initAudioRecorder()).catch(error => {
23
+ this.createSession().then(() => initAudioRecorder(sampleRate ? {
24
+ sampleRate
25
+ } : {})).catch(error => {
20
26
  logger.error('EarlyStart Failed to create ASR session:', error);
21
27
  });
22
28
  }
@@ -113,9 +119,15 @@ export class AsrAgent {
113
119
  const {
114
120
  recordingOptions
115
121
  } = this.options || {};
116
- const audioStream = activeEvent.stream(_objectSpread({
117
- type: 'audio'
118
- }, recordingOptions));
122
+ const {
123
+ sampleRate,
124
+ saveFile
125
+ } = recordingOptions || {};
126
+ const audioStream = activeEvent.stream({
127
+ type: 'audio',
128
+ sampleRate,
129
+ saveFile
130
+ });
119
131
  this.audioStream = audioStream;
120
132
  activeEvent.on('data', entry => {
121
133
  if (entry.type === 'text') {
@@ -126,7 +138,10 @@ export class AsrAgent {
126
138
  if (packet.bizType !== ReceivedTextPacketType.ASR) {
127
139
  return;
128
140
  }
129
- typeof onMessage === 'function' && onMessage(packet.data);
141
+ typeof onMessage === 'function' && onMessage({
142
+ type: 'text',
143
+ text: packet.data.text
144
+ });
130
145
  } else if (entry.type === 'connectionState') {
131
146
  if (entry.body.connectState === ConnectState.DISCONNECTED || entry.body.connectState === ConnectState.CLOSED) {
132
147
  this.stop();
@@ -168,10 +183,12 @@ export class AsrAgent {
168
183
  this.recordDurationTimer = null;
169
184
  }
170
185
  if (this.audioStream) {
171
- const result = await this.audioStream.stop();
172
- if (result !== null && result !== void 0 && result.filePath) {
186
+ const file = await this.audioStream.stop();
187
+ console.log('Audio file result:', file);
188
+ if (file !== null && file !== void 0 && file.path) {
173
189
  this.options.onMessage({
174
- filePath: result.filePath
190
+ type: 'file',
191
+ file
175
192
  });
176
193
  }
177
194
  this.audioStream = null;
@@ -0,0 +1,3 @@
1
+ import { AsrAgent } from './AsrAgent';
2
+ import { createAsrAgent } from './createAsrAgent';
3
+ export { AsrAgent, createAsrAgent };
@@ -0,0 +1,3 @@
1
+ import { AsrAgent } from './AsrAgent';
2
+ import { createAsrAgent } from './createAsrAgent';
3
+ export { AsrAgent, createAsrAgent };
package/dist/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export * from './withAIStream';
4
4
  export * from './ChatHistoryStore';
5
5
  export * from './buildIn';
6
6
  export * from './ChatHistoryLocalStore';
7
+ export * from './asr';
package/dist/index.js CHANGED
@@ -3,4 +3,5 @@ export * from './AIStreamTypes';
3
3
  export * from './withAIStream';
4
4
  export * from './ChatHistoryStore';
5
5
  export * from './buildIn';
6
- export * from './ChatHistoryLocalStore';
6
+ export * from './ChatHistoryLocalStore';
7
+ export * from './asr';
@@ -1,4 +1,4 @@
1
- import { Attribute, BizTag, ConnectClientType, ConnectState, FileFormat, VideoCameraType } from '../AIStreamTypes';
1
+ import { AIStreamAudioFile, Attribute, BizTag, ConnectClientType, ConnectState, FileFormat, VideoCameraType } from '../AIStreamTypes';
2
2
  import { AIStreamDataEntry, AIStreamObserverPool } from './observer';
3
3
  import { AIStreamError } from './errors';
4
4
  interface AIStreamConnectionOptions {
@@ -87,7 +87,7 @@ type AIStreamEventSource = {
87
87
  type: 'audio';
88
88
  dataChannel?: string;
89
89
  userData?: Attribute[];
90
- needSave?: boolean;
90
+ saveFile?: boolean;
91
91
  sampleRate?: number;
92
92
  } | {
93
93
  type: 'video';
@@ -100,9 +100,7 @@ export interface AIStreamEventStream {
100
100
  dataChannel: string;
101
101
  started: boolean;
102
102
  start: () => Promise<void>;
103
- stop: () => Promise<{
104
- filePath?: string;
105
- } | null>;
103
+ stop: () => Promise<AIStreamAudioFile | null>;
106
104
  }
107
105
  export declare class AIStreamEvent {
108
106
  readonly eventId: string;
@@ -459,7 +459,11 @@ export class AIStreamEvent {
459
459
  startPromise = startRecordAndSendAudioData({
460
460
  sessionId: this.sessionId,
461
461
  dataChannel,
462
- userData: source.userData
462
+ userData: source.userData,
463
+ recordInitParams: source.sampleRate ? {
464
+ sampleRate: source.sampleRate
465
+ } : undefined,
466
+ saveFile: source.saveFile
463
467
  });
464
468
  await startPromise;
465
469
  } else if (source.type === 'video') {
@@ -480,9 +484,9 @@ export class AIStreamEvent {
480
484
  await tryCatchTTT(() => startPromise);
481
485
  startPromise = null;
482
486
  }
483
- let result = null;
487
+ let file = null;
484
488
  if (source.type === 'audio') {
485
- result = await stopRecordAndSendAudioData({
489
+ file = await stopRecordAndSendAudioData({
486
490
  sessionId: this.sessionId,
487
491
  dataChannel,
488
492
  userData: source.userData
@@ -503,7 +507,7 @@ export class AIStreamEvent {
503
507
  });
504
508
  delete this.streams[dataChannel];
505
509
  stream.started = false;
506
- return result;
510
+ return file;
507
511
  }
508
512
  };
509
513
  this.streams[dataChannel] = stream;
@@ -470,7 +470,7 @@ mock.hooks.hook('stopRecordAndSendAudioData', async context => {
470
470
  throw new TTTError('stopRecordAndSendAudioData event not exists', AIStreamAppErrorCode.SESSION_ID_INVALID);
471
471
  }
472
472
  session.currentEvent.asr.finishController.abort(new Error('stopRecordAndSendAudioData'));
473
- context.result = true;
473
+ context.result = null;
474
474
  await mock.sleep(100);
475
475
  });
476
476
  mock.hooks.hook('startRecordAndSendVideoData', context => {
@@ -9,6 +9,5 @@ export * from './AIStream';
9
9
  export * from './observer';
10
10
  export * from './sendMessage';
11
11
  export * from './version';
12
- export * from '../asr/createAsrAgent';
13
12
  export * from './actions';
14
13
  export { mock };
@@ -9,6 +9,5 @@ export * from './AIStream';
9
9
  export * from './observer';
10
10
  export * from './sendMessage';
11
11
  export * from './version';
12
- export * from '../asr/createAsrAgent';
13
12
  export * from './actions';
14
13
  export { mock };
@@ -1,4 +1,4 @@
1
- import { ApiRequestByAtopParams, ApiRequestByHighwayParams, AudioBody, AuthorizeParams, AuthorizePolicyStatusParams, CanIUseRouterParams, CloseSessionParams, ConnectParams, ConnectStateBody, CreateSessionParams, DeleteRecordListParams, DisconnectParams, EventBody, EventChannelMessageParams, GetAppInfoParams, GetCurrentHomeInfoParams, GetMiniAppConfigParams, GetAccountInfoParams, ImageBody, InsertRecordParams, NavigateToMiniProgramParams, OpenInnerH5Params, OpenMiniWidgetParams, QueryAgentTokenParams, QueryRecordListParams, RecordAmplitudesBody, RegisterChannelParams, RouterParams, SendEventChatBreakParams, SendEventEndParams, SendEventPayloadEndParams, SendEventStartParams, SendImageDataParams, SendTextDataParams, SessionStateBody, StartRecordAndSendAudioDataParams, StopRecordAndSendAudioDataParams, TextBody, UpdateRecordParams, IsConnectedParams, GetNetworkTypeParams, StartPlayAudioParams, InitAudioRecorderParams } from '../AIStreamTypes';
1
+ import { ApiRequestByAtopParams, ApiRequestByHighwayParams, AudioBody, AuthorizeParams, AuthorizePolicyStatusParams, CanIUseRouterParams, CloseSessionParams, ConnectParams, ConnectStateBody, CreateSessionParams, DeleteRecordListParams, DisconnectParams, EventBody, EventChannelMessageParams, GetAppInfoParams, GetCurrentHomeInfoParams, GetMiniAppConfigParams, GetAccountInfoParams, ImageBody, InsertRecordParams, NavigateToMiniProgramParams, OpenInnerH5Params, OpenMiniWidgetParams, QueryAgentTokenParams, QueryRecordListParams, RecordAmplitudesBody, RegisterChannelParams, RouterParams, SendEventChatBreakParams, SendEventEndParams, SendEventPayloadEndParams, SendEventStartParams, SendImageDataParams, SendTextDataParams, SessionStateBody, StartRecordAndSendAudioDataParams, StopRecordAndSendAudioDataParams, TextBody, UpdateRecordParams, IsConnectedParams, GetNetworkTypeParams, StartPlayAudioParams, InitAudioRecorderParams, StopPlayAudioParams, AudioPlayChangedBody } from '../AIStreamTypes';
2
2
  export declare const getMiniAppConfig: (options?: Omit<GetMiniAppConfigParams, "success" | "fail"> | undefined) => Promise<{
3
3
  config: any;
4
4
  }>;
@@ -79,9 +79,7 @@ export declare const sendEventPayloadEnd: (options?: Omit<SendEventPayloadEndPar
79
79
  export declare const sendEventEnd: (options?: Omit<SendEventEndParams, "success" | "fail"> | undefined) => Promise<null>;
80
80
  export declare const sendEventChatBreak: (options?: Omit<SendEventChatBreakParams, "success" | "fail"> | undefined) => Promise<null>;
81
81
  export declare const startRecordAndSendAudioData: (options?: Omit<StartRecordAndSendAudioDataParams, "success" | "fail"> | undefined) => Promise<null>;
82
- export declare const stopRecordAndSendAudioData: (options?: Omit<StopRecordAndSendAudioDataParams, "success" | "fail"> | undefined) => Promise<{
83
- filePath: string;
84
- } | null>;
82
+ export declare const stopRecordAndSendAudioData: (options?: Omit<StopRecordAndSendAudioDataParams, "success" | "fail"> | undefined) => Promise<import("../AIStreamTypes").AIStreamAudioFile | null>;
85
83
  export declare const sendImageData: (options?: Omit<SendImageDataParams, "success" | "fail"> | undefined) => Promise<null>;
86
84
  export declare const sendTextData: (options?: Omit<SendTextDataParams, "success" | "fail"> | undefined) => Promise<null>;
87
85
  export declare const registerRecordAmplitudes: (options?: Omit<{
@@ -90,10 +88,9 @@ export declare const registerRecordAmplitudes: (options?: Omit<{
90
88
  export declare const unregisterVoiceAmplitudes: (options?: Omit<any, "success" | "fail"> | undefined) => Promise<unknown>;
91
89
  export declare const initAudioRecorder: (options?: Omit<InitAudioRecorderParams, "success" | "fail"> | undefined) => Promise<null>;
92
90
  export declare const startPlayAudio: (options?: Omit<StartPlayAudioParams, "success" | "fail"> | undefined) => Promise<null>;
91
+ export declare const stopPlayAudio: (options?: Omit<StopPlayAudioParams, "success" | "fail"> | undefined) => Promise<null>;
93
92
  export declare const listenEventReceived: (listener: (params: EventBody) => void) => () => void;
94
- export declare const listenAudioPlayEnd: (listener: (params: {
95
- path: string;
96
- }) => void) => () => void;
93
+ export declare const listenAudioPlayChanged: (listener: (params: AudioPlayChangedBody) => void) => () => void;
97
94
  export declare const listenAudioReceived: (listener: (params: AudioBody) => void) => () => void;
98
95
  export declare const listenTextReceived: (listener: (params: TextBody) => void) => () => void;
99
96
  export declare const listenImageReceived: (listener: (params: ImageBody) => void) => () => void;
package/dist/utils/ttt.js CHANGED
@@ -58,11 +58,12 @@ export const registerRecordAmplitudes = promisify(ty.aistream.registerRecordAmpl
58
58
  export const unregisterVoiceAmplitudes = promisify(ty.aistream.unregisterVoiceAmplitudes, true);
59
59
  export const initAudioRecorder = promisify(ty.aistream.initAudioRecorder, true);
60
60
  export const startPlayAudio = promisify(ty.aistream.startPlayAudio, true);
61
+ export const stopPlayAudio = promisify(ty.aistream.stopPlayAudio, true);
61
62
 
62
63
  // export const sendFileData = promisify<SendFileDataParams>(ty.aistream.sendFileData);
63
64
 
64
65
  export const listenEventReceived = listening(ty.aistream.onEventReceived, ty.aistream.offEventReceived, true);
65
- export const listenAudioPlayEnd = listening(ty.aistream.onAudioPlayEnd, ty.aistream.onAudioPlayEnd, true);
66
+ export const listenAudioPlayChanged = listening(ty.aistream.onAudioPlayChanged, ty.aistream.onAudioPlayChanged, true);
66
67
  export const listenAudioReceived = listening(ty.aistream.onAudioReceived, ty.aistream.offAudioReceived, true);
67
68
 
68
69
  // export const listenVideoReceived = listening<VideoBody>(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/t-agent-plugin-aistream",
3
- "version": "0.2.3-beta-3",
3
+ "version": "0.2.3-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": "84e4e7b2ec8f91fe56b3b8499d549175893f9fe3"
38
+ "gitHead": "952d287025737224227ead3ecf6dd4c432fec6a2"
39
39
  }