@ray-js/t-agent-plugin-aistream 0.2.0-beta.0 → 0.2.0-beta.18

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.
@@ -628,7 +628,9 @@ export declare enum EventType {
628
628
  /** 聊天中断 */
629
629
  CHAT_BREAK = 4,
630
630
  /** 服务端 VAD */
631
- SERVER_VAD = 5
631
+ SERVER_VAD = 5,
632
+ /** 服务端错误 */
633
+ SERVER_ERROR = 6
632
634
  }
633
635
  export declare enum StreamFlag {
634
636
  /** 仅一包 */
@@ -718,7 +720,9 @@ export type BizConfig = {
718
720
  revData: string[];
719
721
  };
720
722
  export declare enum AIStreamAttributeType {
721
- AI_CHAT = 1003
723
+ AI_CHAT = 1003,
724
+ ERROR_MESSAGE = 1006,
725
+ ERROR_CODE = 1007
722
726
  }
723
727
  export declare enum AIStreamAttributePayloadType {
724
728
  U_INT8 = 1,
@@ -728,17 +732,9 @@ export declare enum AIStreamAttributePayloadType {
728
732
  BYTES = 5,
729
733
  STRING = 6
730
734
  }
731
- export declare enum AIStreamChatSysWorkflow {
732
- ASR_LLM_TTS = "asr-llm-tts",
733
- ASR_LLM = "asr-llm",
734
- ASR = "asr",
735
- LLM = "llm",
736
- LLM_TTS = "llm-tts"
737
- }
738
735
  export interface AIStreamChatAttribute {
739
736
  'asr.enableVad'?: 'true' | 'false';
740
737
  'processing.interrupt'?: 'true' | 'false';
741
- 'sys.workflow'?: AIStreamChatSysWorkflow;
742
738
  [key: string]: string;
743
739
  }
744
740
  export type StartPlayAudioParams = {
@@ -1607,15 +1603,31 @@ export type ReceivedSearchKnowledgeSkill = ReceivedTextSkillPacketBody<BuildInSk
1607
1603
  export interface ReceivedSmartHomeSkillDevice {
1608
1604
  devId: string;
1609
1605
  devIcon: string;
1610
- dps: Record<string, string>;
1606
+ dps?: Record<string, string>;
1611
1607
  devName: string;
1608
+ room: string;
1609
+ success: boolean;
1610
+ }
1611
+ export interface ReceivedSmartHomeSkillScene {
1612
+ sceneId: string;
1613
+ sceneName: string;
1614
+ sceneIcon: string;
1615
+ type: number;
1616
+ valid: boolean;
1617
+ displayColor: string;
1618
+ enable?: boolean;
1619
+ success?: boolean;
1612
1620
  }
1613
1621
  export declare enum ReceivedSmartHomeSkillAction {
1614
- CONTROL_DEVICE = "control_device"
1622
+ QUERY_DEVICE = "query_device",
1623
+ CONTROL_DEVICE = "control_device",
1624
+ QUERY_SCENE = "query_scene",
1625
+ CONTROL_SCENE = "control_scene"
1615
1626
  }
1616
1627
  export type ReceivedSmartHomeSkill = ReceivedTextSkillPacketBody<BuildInSkillCode.SEARCH_KNOWLEDGE, {
1617
1628
  data: {
1618
1629
  devices: ReceivedSmartHomeSkillDevice[];
1630
+ scenes: ReceivedSmartHomeSkillScene[];
1619
1631
  };
1620
1632
  action: ReceivedSmartHomeSkillAction;
1621
1633
  }, any>;
@@ -99,6 +99,7 @@ export let EventType = /*#__PURE__*/function (EventType) {
99
99
  EventType[EventType["ONE_SHOT"] = 3] = "ONE_SHOT";
100
100
  EventType[EventType["CHAT_BREAK"] = 4] = "CHAT_BREAK";
101
101
  EventType[EventType["SERVER_VAD"] = 5] = "SERVER_VAD";
102
+ EventType[EventType["SERVER_ERROR"] = 6] = "SERVER_ERROR";
102
103
  return EventType;
103
104
  }({});
104
105
  export let StreamFlag = /*#__PURE__*/function (StreamFlag) {
@@ -173,6 +174,8 @@ export let BizTag = /*#__PURE__*/function (BizTag) {
173
174
  }({});
174
175
  export let AIStreamAttributeType = /*#__PURE__*/function (AIStreamAttributeType) {
175
176
  AIStreamAttributeType[AIStreamAttributeType["AI_CHAT"] = 1003] = "AI_CHAT";
177
+ AIStreamAttributeType[AIStreamAttributeType["ERROR_MESSAGE"] = 1006] = "ERROR_MESSAGE";
178
+ AIStreamAttributeType[AIStreamAttributeType["ERROR_CODE"] = 1007] = "ERROR_CODE";
176
179
  return AIStreamAttributeType;
177
180
  }({});
178
181
  export let AIStreamAttributePayloadType = /*#__PURE__*/function (AIStreamAttributePayloadType) {
@@ -184,14 +187,6 @@ export let AIStreamAttributePayloadType = /*#__PURE__*/function (AIStreamAttribu
184
187
  AIStreamAttributePayloadType[AIStreamAttributePayloadType["STRING"] = 6] = "STRING";
185
188
  return AIStreamAttributePayloadType;
186
189
  }({});
187
- export let AIStreamChatSysWorkflow = /*#__PURE__*/function (AIStreamChatSysWorkflow) {
188
- AIStreamChatSysWorkflow["ASR_LLM_TTS"] = "asr-llm-tts";
189
- AIStreamChatSysWorkflow["ASR_LLM"] = "asr-llm";
190
- AIStreamChatSysWorkflow["ASR"] = "asr";
191
- AIStreamChatSysWorkflow["LLM"] = "llm";
192
- AIStreamChatSysWorkflow["LLM_TTS"] = "llm-tts";
193
- return AIStreamChatSysWorkflow;
194
- }({});
195
190
  export let NetworkType = /*#__PURE__*/function (NetworkType) {
196
191
  NetworkType["NONE"] = "none";
197
192
  NetworkType["CELL_2G"] = "2g";
@@ -260,6 +255,9 @@ export let BuildInSkillCode = /*#__PURE__*/function (BuildInSkillCode) {
260
255
  return BuildInSkillCode;
261
256
  }({});
262
257
  export let ReceivedSmartHomeSkillAction = /*#__PURE__*/function (ReceivedSmartHomeSkillAction) {
258
+ ReceivedSmartHomeSkillAction["QUERY_DEVICE"] = "query_device";
263
259
  ReceivedSmartHomeSkillAction["CONTROL_DEVICE"] = "control_device";
260
+ ReceivedSmartHomeSkillAction["QUERY_SCENE"] = "query_scene";
261
+ ReceivedSmartHomeSkillAction["CONTROL_SCENE"] = "control_scene";
264
262
  return ReceivedSmartHomeSkillAction;
265
263
  }({});
@@ -71,9 +71,6 @@ export function withBuildIn() {
71
71
  });
72
72
  })();
73
73
  (() => {
74
- const intentMap = {
75
- [ReceivedSmartHomeSkillAction.CONTROL_DEVICE]: 'controlDevice'
76
- };
77
74
  onSkillsEnd((skills, responseMessage) => {
78
75
  if (!responseMessage) {
79
76
  return;
@@ -84,23 +81,58 @@ export function withBuildIn() {
84
81
  changeInfo: []
85
82
  };
86
83
  for (const skill of skills) {
87
- var _content$general;
88
84
  if (skill.code !== BuildInSkillCode.SMART_HOME) {
89
85
  continue;
90
86
  }
91
87
  const content = skill;
92
- if (!(content !== null && content !== void 0 && (_content$general = content.general) !== null && _content$general !== void 0 && (_content$general = _content$general.data) !== null && _content$general !== void 0 && (_content$general = _content$general.devices) !== null && _content$general !== void 0 && _content$general.length)) {
93
- continue;
94
- }
95
- const intent = intentMap[content.general.action] || 'controlDevice';
96
- for (const dev of content.general.data.devices) {
97
- data.deviceInfo.push({
98
- icon: dev.devIcon,
99
- name: dev.devName,
100
- deviceId: dev.devId,
101
- intent,
102
- success: null
103
- });
88
+ if (content.general.action === ReceivedSmartHomeSkillAction.QUERY_SCENE) {
89
+ for (const scene of content.general.data.scenes) {
90
+ data.sceneInfo.push({
91
+ type: scene.type,
92
+ enabled: scene.enable,
93
+ icon: scene.sceneIcon,
94
+ name: scene.sceneName,
95
+ sceneId: scene.sceneId,
96
+ intent: 'queryScene',
97
+ valid: scene.valid,
98
+ displayColor: scene.displayColor,
99
+ success: null
100
+ });
101
+ }
102
+ } else if (content.general.action === ReceivedSmartHomeSkillAction.CONTROL_SCENE) {
103
+ for (const scene of content.general.data.scenes) {
104
+ data.sceneInfo.push({
105
+ type: scene.type,
106
+ icon: scene.sceneIcon,
107
+ name: scene.sceneName,
108
+ sceneId: scene.sceneId,
109
+ intent: 'controlScene',
110
+ valid: scene.valid,
111
+ displayColor: scene.displayColor,
112
+ success: scene.success
113
+ });
114
+ }
115
+ } else if (content.general.action === ReceivedSmartHomeSkillAction.QUERY_DEVICE) {
116
+ for (const dev of content.general.data.devices) {
117
+ data.deviceInfo.push({
118
+ icon: dev.devIcon,
119
+ name: dev.devName,
120
+ deviceId: dev.devId,
121
+ intent: 'queryDevice',
122
+ room: dev.room,
123
+ success: dev.success
124
+ });
125
+ }
126
+ } else if (content.general.action === ReceivedSmartHomeSkillAction.CONTROL_DEVICE) {
127
+ for (const dev of content.general.data.devices) {
128
+ data.deviceInfo.push({
129
+ icon: dev.devIcon,
130
+ name: dev.devName,
131
+ deviceId: dev.devId,
132
+ intent: 'controlDevice',
133
+ success: null
134
+ });
135
+ }
104
136
  }
105
137
  }
106
138
  if (data.deviceInfo.length) {
package/dist/global.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { AIStreamClient } from './utils';
2
2
  export declare const globalAIStreamClient: AIStreamClient;
3
- export declare const DEFAULT_TOKEN_API = "m.thing.aigc.basic.server.token";
3
+ export declare const DEFAULT_TOKEN_API = "m.life.ai.token.get";
4
4
  export declare const DEFAULT_TOKEN_API_VERSION = "1.0";
package/dist/global.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import { AIStreamClient } from './utils';
2
2
  export const globalAIStreamClient = new AIStreamClient();
3
- export const DEFAULT_TOKEN_API = 'm.thing.aigc.basic.server.token';
3
+ export const DEFAULT_TOKEN_API = 'm.life.ai.token.get';
4
4
  export const DEFAULT_TOKEN_API_VERSION = '1.0';
@@ -7,7 +7,7 @@ import "core-js/modules/esnext.iterator.find.js";
7
7
  import "core-js/modules/esnext.iterator.for-each.js";
8
8
  import "core-js/modules/esnext.iterator.map.js";
9
9
  import "core-js/modules/web.dom-collections.iterator.js";
10
- import { AIStreamErrorCode, AIStreamServerErrorCode, BizTag, ConnectClientType, ConnectState, EventType, NetworkType, SessionState } from '../AIStreamTypes';
10
+ import { AIStreamAttributeType, AIStreamErrorCode, AIStreamServerErrorCode, BizTag, ConnectClientType, ConnectState, EventType, NetworkType, SessionState } from '../AIStreamTypes';
11
11
  import { closeSession, connect, createSession, disconnect, getCurrentHomeInfo, getNetworkType, isConnected, queryAgentToken, registerRecordAmplitudes, sendEventChatBreak, sendEventEnd, sendEventPayloadEnd, sendEventStart, sendImageData, sendTextData, startRecordAndSendAudioData, stopRecordAndSendAudioData, unregisterVoiceAmplitudes } from './ttt';
12
12
  import { AIStreamObserver, AIStreamObserverPool } from './observer';
13
13
  import { isAbortError } from '@ray-js/t-agent';
@@ -178,12 +178,27 @@ export class AIStreamSession {
178
178
  (_this$activeEvent2 = this.activeEvent) === null || _this$activeEvent2 === void 0 || _this$activeEvent2.emit('data', entry);
179
179
  if (entry.type === 'event' && entry.body.eventId === this.activeEvent.eventId) {
180
180
  const {
181
- eventType
181
+ eventType,
182
+ userData
182
183
  } = entry.body;
183
184
  if (eventType === EventType.EVENT_END || eventType === EventType.CHAT_BREAK || eventType === EventType.ONE_SHOT) {
184
185
  var _this$activeEvent3;
185
186
  (_this$activeEvent3 = this.activeEvent) === null || _this$activeEvent3 === void 0 || _this$activeEvent3.emit('finish');
186
187
  this.cleanupEvent();
188
+ } else if (eventType === EventType.SERVER_ERROR) {
189
+ var _this$activeEvent4;
190
+ let code = '';
191
+ let message = '';
192
+ for (const item of userData) {
193
+ if (item.type === AIStreamAttributeType.ERROR_CODE) {
194
+ code = item.value;
195
+ } else if (item.type === AIStreamAttributeType.ERROR_MESSAGE) {
196
+ message = item.value;
197
+ }
198
+ }
199
+ const error = new AIStreamError(message || 'Event error occurred', code || AIStreamErrorCode.UNKNOWN_ERROR);
200
+ (_this$activeEvent4 = this.activeEvent) === null || _this$activeEvent4 === void 0 || _this$activeEvent4.emit('error', error);
201
+ this.cleanupEvent();
187
202
  }
188
203
  }
189
204
  });
@@ -320,11 +335,11 @@ export class AIStreamSession {
320
335
  return this.activeEvent;
321
336
  }
322
337
  cleanupEvent() {
323
- var _this$activeObserver, _this$activeEvent4;
338
+ var _this$activeObserver, _this$activeEvent5;
324
339
  logger.debug('AIStreamSession cleanupEvent');
325
340
  (_this$activeObserver = this.activeObserver) === null || _this$activeObserver === void 0 || _this$activeObserver.disconnect();
326
341
  this.activeObserver = null;
327
- (_this$activeEvent4 = this.activeEvent) === null || _this$activeEvent4 === void 0 || _this$activeEvent4.emit('close');
342
+ (_this$activeEvent5 = this.activeEvent) === null || _this$activeEvent5 === void 0 || _this$activeEvent5.emit('close');
328
343
  this.activeEvent = null;
329
344
  }
330
345
  cleanup() {
@@ -49,7 +49,7 @@ declare class AsrAgent {
49
49
  * @example
50
50
  * const asrAgent = createAsrAgent({
51
51
  * agentId: 'asr-agent',
52
- * tokenApi: 'm.thing.aigc.basic.server.token',
52
+ * tokenApi: 'xxxx',
53
53
  * tokenApiVersion: '1.0',
54
54
  * clientType: ConnectClientType.APP,
55
55
  * deviceId: 'deviceId',
@@ -2,7 +2,7 @@ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
2
  import "core-js/modules/es.json.stringify.js";
3
3
  import { authorize } from './ttt';
4
4
  import { DEFAULT_TOKEN_API, DEFAULT_TOKEN_API_VERSION, globalAIStreamClient } from '../global';
5
- import { ConnectClientType, AIStreamAttributePayloadType, AIStreamAttributeType, AIStreamChatSysWorkflow } from '../AIStreamTypes';
5
+ import { ConnectClientType, AIStreamAttributePayloadType, AIStreamAttributeType } from '../AIStreamTypes';
6
6
  import logger from './logger';
7
7
  class AsrAgent {
8
8
  constructor(options) {
@@ -51,7 +51,10 @@ class AsrAgent {
51
51
  const activeSession = streamConn.createSession({
52
52
  api: tokenApi || DEFAULT_TOKEN_API,
53
53
  apiVersion: tokenApiVersion || DEFAULT_TOKEN_API_VERSION,
54
- solutionCode: agentId
54
+ solutionCode: agentId,
55
+ extParams: {
56
+ onlyAsr: true
57
+ }
55
58
  });
56
59
  this.activeSession = activeSession;
57
60
  return this.activeSession;
@@ -81,17 +84,15 @@ class AsrAgent {
81
84
  onFinish,
82
85
  onError
83
86
  } = this.options || {};
84
- const activeSession = await this.createSession();
85
- const attribute = {
86
- 'processing.interrupt': 'false',
87
- 'asr.enableVad': 'false',
88
- 'sys.workflow': AIStreamChatSysWorkflow.ASR
89
- };
87
+ const activeSession = this.createSession();
90
88
  const activeEvent = await activeSession.startEvent({
91
89
  userData: [{
92
90
  type: AIStreamAttributeType.AI_CHAT,
93
91
  payloadType: AIStreamAttributePayloadType.STRING,
94
- value: JSON.stringify(attribute)
92
+ value: JSON.stringify({
93
+ 'processing.interrupt': 'false',
94
+ 'asr.enableVad': 'false'
95
+ })
95
96
  }]
96
97
  });
97
98
  this.activeEvent = activeEvent;
@@ -165,7 +166,7 @@ class AsrAgent {
165
166
  * @example
166
167
  * const asrAgent = createAsrAgent({
167
168
  * agentId: 'asr-agent',
168
- * tokenApi: 'm.thing.aigc.basic.server.token',
169
+ * tokenApi: 'xxxx',
169
170
  * tokenApiVersion: '1.0',
170
171
  * clientType: ConnectClientType.APP,
171
172
  * deviceId: 'deviceId',
@@ -7,6 +7,7 @@ export interface SendBlocksToAIStreamParams {
7
7
  session: AIStreamSession;
8
8
  attribute?: AIStreamChatAttribute;
9
9
  signal?: AbortSignal;
10
+ enableTts?: boolean;
10
11
  }
11
12
  export declare function sendBlocksToAIStream(params: SendBlocksToAIStreamParams): {
12
13
  response: StreamResponse;
@@ -3,7 +3,7 @@ import "core-js/modules/es.json.stringify.js";
3
3
  import "core-js/modules/web.dom-collections.iterator.js";
4
4
  import '../polyfill';
5
5
  import { ReadableStream } from 'web-streams-polyfill';
6
- import { AIStreamAttributePayloadType, AIStreamAttributeType, AIStreamChatSysWorkflow, AIStreamErrorCode, ConnectState, FileFormat, ReceivedTextPacketEof, ReceivedTextPacketType, SessionState, StreamFlag } from '../AIStreamTypes';
6
+ import { AIStreamAttributePayloadType, AIStreamAttributeType, AIStreamErrorCode, ConnectState, FileFormat, ReceivedTextPacketEof, ReceivedTextPacketType, SessionState, StreamFlag } from '../AIStreamTypes';
7
7
  import { EmitterEvent, generateId, safeParseJSON, StreamResponse } from '@ray-js/t-agent';
8
8
  import { tryCatch } from './misc';
9
9
  import { AIStreamError, transformErrorCode } from './errors';
@@ -34,8 +34,7 @@ export function sendBlocksToAIStream(params) {
34
34
  }
35
35
  const attribute = _objectSpread({
36
36
  'processing.interrupt': 'false',
37
- 'asr.enableVad': 'false',
38
- 'sys.workflow': audioEmitter ? AIStreamChatSysWorkflow.ASR_LLM : AIStreamChatSysWorkflow.LLM
37
+ 'asr.enableVad': 'false'
39
38
  }, params.attribute);
40
39
  let canceled = false;
41
40
  let closed = false;
@@ -26,6 +26,8 @@ export interface AIStreamOptions {
26
26
  earlyStart?: boolean;
27
27
  /** 自定义消息存储, 返回的实例需要实现 ChatHistoryLocalStore 接口, 返回null则不存储历史聊天记录 */
28
28
  createChatHistoryStore?: (agent: ChatAgent) => ChatHistoryLocalStore | null;
29
+ /** 是否开启音频合成 */
30
+ enableTts?: boolean;
29
31
  }
30
32
  export type AIStreamPlugin = GetChatPluginHandler<typeof withAIStream>;
31
33
  export interface AIStreamHooks {
@@ -43,7 +45,7 @@ export declare function withAIStream(options?: AIStreamOptions): (agent: ChatAge
43
45
  hooks: import("hookable").Hookable<any, string>;
44
46
  aiStream: {
45
47
  send: (blocks: InputBlock[], signal?: AbortSignal, extraOptions?: Record<string, any>) => {
46
- response: import("@ray-js/t-agent").StreamResponse; /** 是否将输入块传递给智能体,默认为 true,设置为 false 时,需要你自己编写 onInputBlocksPush Hook 来处理输入块 */
48
+ response: import("@ray-js/t-agent").StreamResponse;
47
49
  metaPromise: Promise<Record<string, any>>;
48
50
  };
49
51
  chat: (blocks: InputBlock[], signal?: AbortSignal, options?: {
@@ -1,5 +1,5 @@
1
- import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
1
  import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
2
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3
3
  const _excluded = ["message"];
4
4
  import "core-js/modules/es.array.flat.js";
5
5
  import "core-js/modules/es.array.reverse.js";
@@ -98,7 +98,10 @@ export function withAIStream() {
98
98
  api: tokenOptions.api,
99
99
  apiVersion: tokenOptions.version,
100
100
  solutionCode: agentId,
101
- extParams: tokenOptions.extParams
101
+ extParams: _objectSpread(_objectSpread({}, tokenOptions.extParams), {}, {
102
+ needTts: !!options.enableTts,
103
+ deviceId: options.deviceId || undefined
104
+ })
102
105
  });
103
106
  await session.set('AIStream.streamSession', streamSession);
104
107
  if (options.earlyStart) {
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.0",
3
+ "version": "0.2.0-beta.18",
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": "0b5d552289721444563828b03a51a6e8695779db"
38
+ "gitHead": "3826cdeac61109618670dcbbeafd4103113f04d7"
39
39
  }