@ray-js/t-agent-plugin-aistream 0.2.6 → 0.2.7-beta.10

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.
@@ -14,6 +14,8 @@ import { BizCode, ConnectClientType } from './AIStreamTypes';
14
14
  import { DEFAULT_TOKEN_API, DEFAULT_TOKEN_API_VERSION, globalAIStreamClient } from './global';
15
15
  import logger from './utils/logger';
16
16
  import { ChatHistoryLocalStore } from './ChatHistoryLocalStore';
17
+ import { deepMerge } from './utils/object';
18
+ import { trackEvent } from './utils/track';
17
19
  export function withAIStream() {
18
20
  let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
19
21
  const hooks = createHooks();
@@ -60,6 +62,10 @@ export function withAIStream() {
60
62
  if (clientType === ConnectClientType.APP && deviceId) {
61
63
  throw new Error('deviceId is not allowed when clientType is app');
62
64
  }
65
+ trackEvent(agentId, 'on_agent_start', {
66
+ clientType,
67
+ tokenApi: "".concat(tokenOptions.api, "_").concat(tokenOptions.version)
68
+ });
63
69
  let homeId = options.homeId;
64
70
  if (clientType === ConnectClientType.DEVELOPER) {
65
71
  homeId = 1;
@@ -88,7 +94,14 @@ export function withAIStream() {
88
94
  extParams: _objectSpread({
89
95
  needTts: !!options.enableTts,
90
96
  deviceId
91
- }, tokenOptions.extParams)
97
+ }, tokenOptions.extParams),
98
+ getSessionUserData: async () => {
99
+ const result = {
100
+ userData: {}
101
+ };
102
+ await hooks.callHook('onUserDataRead', 'create-session', {}, result);
103
+ return result.userData;
104
+ }
92
105
  });
93
106
  await session.set('AIStream.streamSession', streamSession);
94
107
  if (options.earlyStart) {
@@ -221,14 +234,25 @@ export function withAIStream() {
221
234
  };
222
235
  }
223
236
  });
224
- const send = (blocks, signal, extraOptions) => {
237
+ const send = (blocks, signal, eventUserData) => {
225
238
  const streamSession = session.get('AIStream.streamSession');
226
239
  const result = sendBlocksToAIStream({
227
240
  blocks,
228
241
  session: streamSession,
229
242
  signal,
230
243
  eventIdPrefix: options.eventIdPrefix,
231
- attribute: _objectSpread({}, extraOptions)
244
+ getUserData: async () => {
245
+ const userDataResult = {
246
+ userData: {}
247
+ };
248
+ await hooks.callHook('onUserDataRead', 'start-event', {
249
+ blocks
250
+ }, userDataResult);
251
+ const userData = {};
252
+ deepMerge(userData, userDataResult.userData);
253
+ deepMerge(userData, eventUserData);
254
+ return userData;
255
+ }
232
256
  });
233
257
  signal === null || signal === void 0 || signal.addEventListener('abort', event => {
234
258
  logger.debug('withAIStream signal aborted, response.started:', result.response.started);
@@ -245,10 +269,11 @@ export function withAIStream() {
245
269
  const {
246
270
  sendBy = 'user',
247
271
  responseBy = 'assistant',
248
- extraOptions
272
+ userData
249
273
  } = options || {};
250
274
  let audioEmitter = null;
251
- for (const block of blocks) {
275
+ for (let i = 0; i < blocks.length; i++) {
276
+ const block = blocks[i];
252
277
  if (block.type === 'audio') {
253
278
  if (audioEmitter) {
254
279
  throw new Error('only one audio emitter is allowed');
@@ -345,7 +370,7 @@ export function withAIStream() {
345
370
  const {
346
371
  response,
347
372
  metaPromise
348
- } = send(blocks, signal, extraOptions);
373
+ } = send(blocks, signal, userData);
349
374
  if (audioPromise) {
350
375
  try {
351
376
  await audioPromise;
@@ -399,7 +424,8 @@ export function withAIStream() {
399
424
  if (valid) {
400
425
  await message.persist();
401
426
  const [_, ...rest] = result.messages;
402
- for (const m of rest) {
427
+ for (let i = 0; i < rest.length; i++) {
428
+ const m = rest[i];
403
429
  await m.show();
404
430
  await m.persist();
405
431
  }
@@ -531,8 +557,9 @@ export function withAIStream() {
531
557
  };
532
558
  onSkillsEnd(async (skills, respMsg, result) => {
533
559
  const cards = [];
534
- for (const skill of skills) {
560
+ for (let i = 0; i < skills.length; i++) {
535
561
  var _skill$custom, _skill$general;
562
+ const skill = skills[i];
536
563
  if ((_skill$custom = skill.custom) !== null && _skill$custom !== void 0 && (_skill$custom = _skill$custom.data) !== null && _skill$custom !== void 0 && _skill$custom.aiCards) {
537
564
  for (const card of skill.custom.data.aiCards) {
538
565
  cards.push(card);
@@ -548,7 +575,8 @@ export function withAIStream() {
548
575
  cards: cards
549
576
  };
550
577
  await hooks.callHook('onCardsReceived', skills, r);
551
- for (const card of cards) {
578
+ for (let i = 0; i < cards.length; i++) {
579
+ const card = cards[i];
552
580
  const m = createMessage({
553
581
  role: respMsg.role,
554
582
  status: ChatMessageStatus.FINISH,
@@ -585,6 +613,9 @@ export function withAIStream() {
585
613
  onCardsReceived: fn => {
586
614
  return hooks.hook('onCardsReceived', fn);
587
615
  },
616
+ onUserDataRead: fn => {
617
+ return hooks.hook('onUserDataRead', fn);
618
+ },
588
619
  onTTTAction: fn => {
589
620
  return hooks.hook('onTTTAction', fn);
590
621
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/t-agent-plugin-aistream",
3
- "version": "0.2.6",
3
+ "version": "0.2.7-beta.10",
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": "6fc9a8195706d72c2bab10479e8d65a26854bdde"
38
+ "gitHead": "4fa71cb1278f9ef5d4a171f81ff87dbbc8040e4d"
39
39
  }