@wix/legends-platform-sdk 1.0.0 → 1.2.0

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.
Files changed (59) hide show
  1. package/dist/cjs/conversation/types.js.map +1 -1
  2. package/dist/cjs/hooks/useConversation/livekit/types.js.map +1 -1
  3. package/dist/cjs/hooks/useConversation/livekit/useConversation.js +32 -18
  4. package/dist/cjs/hooks/useConversation/livekit/useConversation.js.map +1 -1
  5. package/dist/cjs/hooks/useConversation/livekit/utils/dataChannel.js +4 -0
  6. package/dist/cjs/hooks/useConversation/livekit/utils/dataChannel.js.map +1 -1
  7. package/dist/cjs/hooks/useConversationEvents/types.js.map +1 -1
  8. package/dist/cjs/hooks/useCreateConversation/useCreateConversation.js.map +1 -1
  9. package/dist/cjs/hooks/useTextChat/helpers.js +37 -0
  10. package/dist/cjs/hooks/useTextChat/helpers.js.map +1 -0
  11. package/dist/cjs/hooks/useTextChat/useTextChat.js.map +1 -1
  12. package/dist/cjs/hooks/useTextChat/useToolAwareSend.js +34 -8
  13. package/dist/cjs/hooks/useTextChat/useToolAwareSend.js.map +1 -1
  14. package/dist/cjs/services/FetchPersonaChatService.js +13 -3
  15. package/dist/cjs/services/FetchPersonaChatService.js.map +1 -1
  16. package/dist/cjs/types/index.js.map +1 -1
  17. package/dist/cjs/types/service.js.map +1 -1
  18. package/dist/esm/conversation/types.js.map +1 -1
  19. package/dist/esm/hooks/useConversation/livekit/types.js.map +1 -1
  20. package/dist/esm/hooks/useConversation/livekit/useConversation.js +33 -19
  21. package/dist/esm/hooks/useConversation/livekit/useConversation.js.map +1 -1
  22. package/dist/esm/hooks/useConversation/livekit/utils/dataChannel.js +3 -0
  23. package/dist/esm/hooks/useConversation/livekit/utils/dataChannel.js.map +1 -1
  24. package/dist/esm/hooks/useConversationEvents/types.js.map +1 -1
  25. package/dist/esm/hooks/useCreateConversation/useCreateConversation.js.map +1 -1
  26. package/dist/esm/hooks/useTextChat/helpers.js +35 -0
  27. package/dist/esm/hooks/useTextChat/helpers.js.map +1 -0
  28. package/dist/esm/hooks/useTextChat/useTextChat.js.map +1 -1
  29. package/dist/esm/hooks/useTextChat/useToolAwareSend.js +33 -7
  30. package/dist/esm/hooks/useTextChat/useToolAwareSend.js.map +1 -1
  31. package/dist/esm/services/FetchPersonaChatService.js +13 -3
  32. package/dist/esm/services/FetchPersonaChatService.js.map +1 -1
  33. package/dist/esm/types/index.js.map +1 -1
  34. package/dist/esm/types/service.js.map +1 -1
  35. package/dist/types/conversation/types.d.ts +1 -1
  36. package/dist/types/conversation/types.d.ts.map +1 -1
  37. package/dist/types/hooks/useConversation/livekit/types.d.ts +3 -0
  38. package/dist/types/hooks/useConversation/livekit/types.d.ts.map +1 -1
  39. package/dist/types/hooks/useConversation/livekit/useConversation.d.ts.map +1 -1
  40. package/dist/types/hooks/useConversation/livekit/utils/dataChannel.d.ts +2 -1
  41. package/dist/types/hooks/useConversation/livekit/utils/dataChannel.d.ts.map +1 -1
  42. package/dist/types/hooks/useConversationEvents/types.d.ts +4 -0
  43. package/dist/types/hooks/useConversationEvents/types.d.ts.map +1 -1
  44. package/dist/types/hooks/useCreateConversation/useCreateConversation.d.ts +1 -1
  45. package/dist/types/hooks/useCreateConversation/useCreateConversation.d.ts.map +1 -1
  46. package/dist/types/hooks/useTextChat/helpers.d.ts +7 -0
  47. package/dist/types/hooks/useTextChat/helpers.d.ts.map +1 -0
  48. package/dist/types/hooks/useTextChat/useTextChat.d.ts +3 -1
  49. package/dist/types/hooks/useTextChat/useTextChat.d.ts.map +1 -1
  50. package/dist/types/hooks/useTextChat/useToolAwareSend.d.ts +3 -1
  51. package/dist/types/hooks/useTextChat/useToolAwareSend.d.ts.map +1 -1
  52. package/dist/types/services/FetchPersonaChatService.d.ts +2 -1
  53. package/dist/types/services/FetchPersonaChatService.d.ts.map +1 -1
  54. package/dist/types/types/index.d.ts +1 -1
  55. package/dist/types/types/index.d.ts.map +1 -1
  56. package/dist/types/types/service.d.ts +11 -1
  57. package/dist/types/types/service.d.ts.map +1 -1
  58. package/package.json +3 -4
  59. package/livekit-client.patch +0 -19
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["../../../src/conversation/types.ts"],"sourcesContent":["export type ConversationType = 'video' | 'audio';\n\nexport type ConversationParticipantRole = 'user' | 'agent';\n\nexport type ConversationVendor =\n | 'LIVEKIT'\n | 'DAILY'\n | 'HUME'\n | 'ELEVEN_LABS'\n | 'UNKNOWN_VENDOR';\n\nexport type AgentConnectionState =\n | 'joined' // agent is connected to the conversation\n | 'joining' // agent is connecting to the conversation\n | 'left' // agent left the conversation\n | 'idle'; // agent is not connected to the conversation\n\nexport type UserConnectionState =\n | 'joined' // user is connected to the conversation\n | 'joining' // user is connecting to the conversation\n | 'idle' // user is not connected to the conversation\n | 'leaving' // user is leaving the conversation\n | 'join-error'; // failed to join the conversation\n\nexport type ActivityState = 'idle' | 'speaking' | 'listening';\n\nexport type ConversationStatus =\n // Initial state\n | 'pending' // conversation is not created yet\n // States\n | 'created' // conversation was created\n | 'destroying' // destroying the conversation\n | 'destroyed' // conversation was destroyed\n | 'exceeded' // conversation limit is exceeded\n // Errors\n | 'voice-not-found' // failed to find a voice\n | 'initialization-error' // failed to create conversation\n | 'error'; // unknown error\n\nexport type Conversation = {\n id: string;\n url: string;\n limit?: number;\n type: ConversationType;\n};\n\nexport type ConversationTranscription = {\n message: string;\n source: ConversationParticipantRole;\n id: string;\n};\n\nexport type ConversationDevice = {\n enabled: boolean;\n deviceId: string;\n};\n\nexport type ConversationAudio = {\n // Range between `0.0` and `1.0`.\n volume: number;\n muted: boolean;\n};\n"],"mappings":"","ignoreList":[]}
1
+ {"version":3,"names":[],"sources":["../../../src/conversation/types.ts"],"sourcesContent":["export type ConversationType = 'video' | 'audio';\n\nexport type ConversationParticipantRole = 'user' | 'agent';\n\nexport type ConversationVendor =\n | 'LIVEKIT'\n | 'DAILY'\n | 'HUME'\n | 'ELEVEN_LABS'\n | 'UNKNOWN_VENDOR';\n\nexport type AgentConnectionState =\n | 'joined' // agent is connected to the conversation\n | 'joining' // agent is connecting to the conversation\n | 'left' // agent left the conversation\n | 'idle'; // agent is not connected to the conversation\n\nexport type UserConnectionState =\n | 'joined' // user is connected to the conversation\n | 'joining' // user is connecting to the conversation\n | 'idle' // user is not connected to the conversation\n | 'leaving' // user is leaving the conversation\n | 'join-error'; // failed to join the conversation\n\nexport type ActivityState = 'idle' | 'speaking' | 'listening';\n\nexport type ConversationStatus =\n // Initial state\n | 'pending' // conversation is not created yet\n // States\n | 'created' // conversation was created\n | 'destroying' // destroying the conversation\n | 'destroyed' // conversation was destroyed\n | 'exceeded' // conversation limit is exceeded\n | 'inactivity-timeout' // conversation ended due to inactivity\n // Errors\n | 'voice-not-found' // failed to find a voice\n | 'initialization-error' // failed to create conversation\n | 'error'; // unknown error\n\nexport type Conversation = {\n id: string;\n url: string;\n limit?: number;\n type: ConversationType;\n};\n\nexport type ConversationTranscription = {\n message: string;\n source: ConversationParticipantRole;\n id: string;\n};\n\nexport type ConversationDevice = {\n enabled: boolean;\n deviceId: string;\n};\n\nexport type ConversationAudio = {\n // Range between `0.0` and `1.0`.\n volume: number;\n muted: boolean;\n};\n"],"mappings":"","ignoreList":[]}
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["../../../../../src/hooks/useConversation/livekit/types.ts"],"sourcesContent":["import type { FunctionCall, FunctionCallResult } from '../../../types/service';\nimport type {\n ToolCallEnvelope,\n ToolRuntimeAdapter,\n} from '../../useTools/runtime/types';\n\nexport type InactivityWarningData = {\n type: 'inactivity_warning';\n grace_period_sec: number;\n};\n\nexport type ToolRuntimeLike = {\n handleIncoming(\n envelope: ToolCallEnvelope,\n adapter: ToolRuntimeAdapter,\n ): Promise<void>;\n};\n\nexport type TurnDetectionData = {\n createdAt: number;\n participantIdentity: string;\n sequence: number;\n source: string;\n state: 'listening' | 'speaking';\n turnDetectionStrategy: 'livekit_turn_detector' | 'silero_vad';\n type: string;\n version: number;\n participantKind: 'STANDARD' | 'AGENT';\n speechId?: string;\n};\n\nexport type ToolCallsData = {\n type: 'tool_calls';\n requestId: string;\n functionCalls: FunctionCall[];\n};\n\nexport type ToolResultsData = {\n type: 'tool_results';\n requestId: string;\n functionCallResults: FunctionCallResult[];\n};\n"],"mappings":"","ignoreList":[]}
1
+ {"version":3,"names":[],"sources":["../../../../../src/hooks/useConversation/livekit/types.ts"],"sourcesContent":["import type { FunctionCall, FunctionCallResult } from '../../../types/service';\nimport type {\n ToolCallEnvelope,\n ToolRuntimeAdapter,\n} from '../../useTools/runtime/types';\n\nexport type InactivityWarningData = {\n type: 'inactivity_warning';\n grace_period_sec: number;\n};\n\nexport type InactivityTimeoutData = {\n type: 'inactivity_timeout';\n};\n\nexport type ToolRuntimeLike = {\n handleIncoming(\n envelope: ToolCallEnvelope,\n adapter: ToolRuntimeAdapter,\n ): Promise<void>;\n};\n\nexport type TurnDetectionData = {\n createdAt: number;\n participantIdentity: string;\n sequence: number;\n source: string;\n state: 'listening' | 'speaking';\n turnDetectionStrategy: 'livekit_turn_detector' | 'silero_vad';\n type: string;\n version: number;\n participantKind: 'STANDARD' | 'AGENT';\n speechId?: string;\n};\n\nexport type ToolCallsData = {\n type: 'tool_calls';\n requestId: string;\n functionCalls: FunctionCall[];\n};\n\nexport type ToolResultsData = {\n type: 'tool_results';\n requestId: string;\n functionCallResults: FunctionCallResult[];\n};\n"],"mappings":"","ignoreList":[]}
@@ -63,6 +63,7 @@ function useConversation({
63
63
  const setUserActivityState = (0, _jotai.useSetAtom)(_conversation.state.userActivityState);
64
64
  const emitEvent = (0, _useEmitConversationEvent.useEmitConversationEvent)();
65
65
  const userStopRef = (0, _react.useRef)(null);
66
+ const inactivityTimeoutRef = (0, _react.useRef)(false);
66
67
  const timer = (0, _reactTimerHook.useTimer)({
67
68
  autoStart: false,
68
69
  onExpire: handleExpire,
@@ -211,6 +212,13 @@ function useConversation({
211
212
  gracePeriodSec: data.grace_period_sec
212
213
  });
213
214
  }
215
+ function handleInactivityTimeoutEvent(_data) {
216
+ var _conversationRef$curr3;
217
+ inactivityTimeoutRef.current = true;
218
+ emitEvent('inactivityTimeout', {
219
+ conversationId: (_conversationRef$curr3 = conversationRef.current) == null ? void 0 : _conversationRef$curr3.id
220
+ });
221
+ }
214
222
  async function handleToolCallsEvent(data) {
215
223
  await toolRuntimeRef.current.resolveIncomingToolCalls(data, toolAdapter);
216
224
  }
@@ -223,9 +231,10 @@ function useConversation({
223
231
  }, {
224
232
  guard: _utils.checkIsInactivityWarningData,
225
233
  handle: handleInactivityWarningEvent
226
- }
227
- // ... other data channel handlers
228
- ];
234
+ }, {
235
+ guard: _utils.checkIsInactivityTimeoutData,
236
+ handle: handleInactivityTimeoutEvent
237
+ }];
229
238
  function handleDataReceivedEvent(payload) {
230
239
  const decoded = (0, _utils.decodeDataPayload)(payload);
231
240
  if (decoded === null) {
@@ -244,11 +253,11 @@ function useConversation({
244
253
  }
245
254
  }
246
255
  function handleConnectedEvent() {
247
- var _conversationRef$curr3;
256
+ var _conversationRef$curr4;
248
257
  setUserConnectionState('joined');
249
258
  setAgentConnectionState('joining');
250
259
  emitEvent('callStarted', {
251
- conversationId: (_conversationRef$curr3 = conversationRef.current) == null ? void 0 : _conversationRef$curr3.id
260
+ conversationId: (_conversationRef$curr4 = conversationRef.current) == null ? void 0 : _conversationRef$curr4.id
252
261
  });
253
262
  }
254
263
  async function handleEncryptionErrorEvent(error) {
@@ -261,6 +270,11 @@ function useConversation({
261
270
  setAgentConnectionState('idle');
262
271
  setUserActivityState('idle');
263
272
  setUserConnectionState('idle');
273
+ if (inactivityTimeoutRef.current) {
274
+ inactivityTimeoutRef.current = false;
275
+ setConversationStatus('inactivity-timeout');
276
+ return;
277
+ }
264
278
  switch (reason) {
265
279
  case _livekitClient.DisconnectReason.CLIENT_INITIATED:
266
280
  break;
@@ -279,18 +293,18 @@ function useConversation({
279
293
  }
280
294
  }
281
295
  function handleAgentJoin(participant) {
282
- var _conversationRef$curr4;
296
+ var _conversationRef$curr5;
283
297
  setAgentConnectionState('joined');
284
298
  emitEvent('agentJoined', {
285
- conversationId: (_conversationRef$curr4 = conversationRef.current) == null ? void 0 : _conversationRef$curr4.id,
299
+ conversationId: (_conversationRef$curr5 = conversationRef.current) == null ? void 0 : _conversationRef$curr5.id,
286
300
  participant
287
301
  });
288
302
  }
289
303
  function handleParticipantDisconnectedEvent(participant) {
290
- var _conversationRef$curr5;
304
+ var _conversationRef$curr6;
291
305
  setAgentConnectionState('left');
292
306
  emitEvent('agentLeft', {
293
- conversationId: (_conversationRef$curr5 = conversationRef.current) == null ? void 0 : _conversationRef$curr5.id,
307
+ conversationId: (_conversationRef$curr6 = conversationRef.current) == null ? void 0 : _conversationRef$curr6.id,
294
308
  participant
295
309
  });
296
310
  }
@@ -299,13 +313,13 @@ function useConversation({
299
313
  return;
300
314
  }
301
315
  try {
302
- var _conversationRef$curr6;
316
+ var _conversationRef$curr7;
303
317
  setUserConnectionState('leaving');
304
318
  await (room == null ? void 0 : room.disconnect(true));
305
319
  setConversationStatus('destroyed');
306
320
  setJoinedAt(undefined);
307
321
  emitEvent('callEnded', {
308
- conversationId: (_conversationRef$curr6 = conversationRef.current) == null ? void 0 : _conversationRef$curr6.id
322
+ conversationId: (_conversationRef$curr7 = conversationRef.current) == null ? void 0 : _conversationRef$curr7.id
309
323
  });
310
324
  } catch (error) {
311
325
  console.error('Failed to leave', error);
@@ -313,18 +327,18 @@ function useConversation({
313
327
  }
314
328
  }
315
329
  async function handleExpire() {
316
- var _conversationRef$curr7, _conversationRef$curr8;
330
+ var _conversationRef$curr8, _conversationRef$curr9;
317
331
  emitEvent('timeExceeded', {
318
- conversationId: (_conversationRef$curr7 = conversationRef.current) == null ? void 0 : _conversationRef$curr7.id,
319
- limit: (_conversationRef$curr8 = conversationRef.current) == null ? void 0 : _conversationRef$curr8.limit
332
+ conversationId: (_conversationRef$curr8 = conversationRef.current) == null ? void 0 : _conversationRef$curr8.id,
333
+ limit: (_conversationRef$curr9 = conversationRef.current) == null ? void 0 : _conversationRef$curr9.limit
320
334
  });
321
335
  await leave();
322
336
  setConversationStatus('exceeded');
323
337
  }
324
338
  async function interrupt() {
325
- var _conversationRef$curr9;
339
+ var _conversationRef$curr0;
326
340
  emitEvent('interrupt', {
327
- conversationId: (_conversationRef$curr9 = conversationRef.current) == null ? void 0 : _conversationRef$curr9.id
341
+ conversationId: (_conversationRef$curr0 = conversationRef.current) == null ? void 0 : _conversationRef$curr0.id
328
342
  });
329
343
  await (0, _utils.publishConversationDataMessage)(room, {}, 'interrupt');
330
344
  }
@@ -334,7 +348,7 @@ function useConversation({
334
348
  }, 'inactivity_dismiss');
335
349
  }
336
350
  async function send(message, attachments = []) {
337
- var _conversationRef$curr0;
351
+ var _conversationRef$curr1;
338
352
  await (room == null ? void 0 : room.localParticipant.sendText(message, {
339
353
  topic: 'lk.chat',
340
354
  attributes: {
@@ -342,7 +356,7 @@ function useConversation({
342
356
  }
343
357
  }));
344
358
  emitEvent('userSendMessage', {
345
- conversationId: (_conversationRef$curr0 = conversationRef.current) == null ? void 0 : _conversationRef$curr0.id,
359
+ conversationId: (_conversationRef$curr1 = conversationRef.current) == null ? void 0 : _conversationRef$curr1.id,
346
360
  message
347
361
  });
348
362
  }
@@ -1 +1 @@
1
- {"version":3,"names":["_react","require","_livekitClient","_invariant","_interopRequireDefault","_reactTimerHook","_reactUse","_componentsReact","_jotai","_conversation","_useCreateConversation","_useEmitConversationEvent","_useToolCallingRuntime","_utils","useConversation","autoJoin","type","config","startWithMicrophone","startWithCamera","_conversation$data2","setLogLevel","LogLevel","silent","conversation","useCreateConversation","room","useEnsureRoom","speaker","useAtomValue","state","microphone","setMicrophone","useAtom","camera","setCamera","toolRuntime","useToolCallingRuntime","toolAdapter","createLivekitToolAdapter","conversationRef","useLatest","data","toolRuntimeRef","conversationStatus","setConversationStatus","userConnectionState","setUserConnectionState","joinedAt","setJoinedAt","setActiveMediaDevice","setMicrophoneDeviceId","useMediaDeviceSelect","kind","setSpeakerDeviceId","setCameraDeviceId","requestPermissions","enabled","setTranscriptions","useSetAtom","transcriptions","agentConnectionState","setAgentConnectionState","setAgentActivityState","agentActivityState","setUserActivityState","userActivityState","emitEvent","useEmitConversationEvent","userStopRef","useRef","timer","useTimer","autoStart","onExpire","handleExpire","expiryTimestamp","Date","useEffect","registerRoomEvents","unregisterRoomEvents","useUpdateEffect","deviceId","localParticipant","setMicrophoneEnabled","setCameraEnabled","resolution","width","height","join","_conversation$data","limit","undefined","restart","getTime","_conversation$data3","invariant","url","URL","accessToken","searchParams","get","connect","remoteParticipant","findJoinedAgentParticipant","handleAgentJoin","enableMediaDevices","error","console","handleTranscriptionReceivedEvent","segments","participant","map","s","id","message","text","source","ParticipantKind","AGENT","old","mergeTranscriptions","handleTurnDetectionEvent","_conversationRef$curr","conversationId","current","participantKind","eventState","ts","performance","now","turnDetectionStrategy","sequence","speechId","latencyMs","handleInactivityWarningEvent","_conversationRef$curr2","gracePeriodSec","grace_period_sec","handleToolCallsEvent","resolveIncomingToolCalls","dataChannelHandlers","guard","checkIsToolCallsData","handle","checkIsTurnDetectionData","checkIsInactivityWarningData","handleDataReceivedEvent","payload","decoded","decodeDataPayload","Promise","resolve","catch","handleConnectedEvent","_conversationRef$curr3","handleEncryptionErrorEvent","handleDisconnectedEvent","reason","DisconnectReason","CLIENT_INITIATED","ROOM_CLOSED","handleParticipantConnectedEvent","shouldHandleParticipantConnected","_conversationRef$curr4","handleParticipantDisconnectedEvent","_conversationRef$curr5","leave","_conversationRef$curr6","disconnect","_conversationRef$curr7","_conversationRef$curr8","interrupt","_conversationRef$curr9","publishConversationDataMessage","dismissInactivity","send","attachments","_conversationRef$curr0","sendText","topic","attributes","safeStringify","on","off","status"],"sources":["../../../../../src/hooks/useConversation/livekit/useConversation.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\nimport {\n DisconnectReason,\n Participant,\n setLogLevel,\n ParticipantKind,\n RemoteParticipant,\n TranscriptionSegment,\n LogLevel,\n} from 'livekit-client';\nimport invariant from 'invariant';\nimport { useTimer } from 'react-timer-hook';\nimport { useLatest, useUpdateEffect } from 'react-use';\nimport { useEnsureRoom, useMediaDeviceSelect } from '@livekit/components-react';\nimport { useAtom, useAtomValue, useSetAtom } from 'jotai';\nimport {\n Conversation,\n state,\n type ConversationTranscription,\n} from '../../../conversation';\nimport type {\n UseConversationResult,\n UseConversationOptions,\n Attachment,\n} from '../types';\nimport type {\n InactivityWarningData,\n TurnDetectionData,\n ToolCallsData,\n} from './types';\nimport { useCreateConversation } from '../../useCreateConversation';\n\nimport { useEmitConversationEvent } from '../../useConversationEvents/useEmitConversationEvent';\nimport { useToolCallingRuntime } from '../../useTools/runtime/useToolCallingRuntime';\nimport {\n checkIsTurnDetectionData,\n checkIsInactivityWarningData,\n checkIsToolCallsData,\n createLivekitToolAdapter,\n decodeDataPayload,\n findJoinedAgentParticipant,\n mergeTranscriptions,\n publishConversationDataMessage,\n safeStringify,\n shouldHandleParticipantConnected,\n} from './utils';\n\nexport function useConversation({\n autoJoin = true,\n type,\n config,\n startWithMicrophone = false,\n startWithCamera = false,\n}: UseConversationOptions): UseConversationResult {\n setLogLevel(LogLevel.silent);\n\n const conversation = useCreateConversation({ type, config });\n const room = useEnsureRoom();\n const speaker = useAtomValue(state.speaker);\n const [microphone, setMicrophone] = useAtom(state.microphone);\n const [camera, setCamera] = useAtom(state.camera);\n const toolRuntime = useToolCallingRuntime();\n const toolAdapter = createLivekitToolAdapter(room);\n\n // for use in register/unregister room events\n const conversationRef = useLatest<Conversation | undefined>(\n conversation.data,\n );\n const toolRuntimeRef = useLatest(toolRuntime);\n\n const [conversationStatus, setConversationStatus] = useAtom(\n state.conversationStatus,\n );\n\n const [userConnectionState, setUserConnectionState] = useAtom(\n state.userConnectionState,\n );\n\n const [joinedAt, setJoinedAt] = useAtom(state.joinedAt);\n\n const { setActiveMediaDevice: setMicrophoneDeviceId } = useMediaDeviceSelect({\n kind: 'audioinput',\n });\n\n const { setActiveMediaDevice: setSpeakerDeviceId } = useMediaDeviceSelect({\n kind: 'audiooutput',\n });\n\n const { setActiveMediaDevice: setCameraDeviceId } = useMediaDeviceSelect({\n kind: 'videoinput',\n requestPermissions: camera.enabled,\n });\n\n const setTranscriptions = useSetAtom(state.transcriptions);\n const [agentConnectionState, setAgentConnectionState] = useAtom(\n state.agentConnectionState,\n );\n const setAgentActivityState = useSetAtom(state.agentActivityState);\n const setUserActivityState = useSetAtom(state.userActivityState);\n const emitEvent = useEmitConversationEvent();\n\n const userStopRef = useRef<{\n ts: number;\n turnDetectionStrategy: string;\n sequence?: number;\n speechId?: string;\n } | null>(null);\n\n const timer = useTimer({\n autoStart: false,\n onExpire: handleExpire,\n expiryTimestamp: new Date(),\n });\n\n useEffect(() => {\n if (!room) {\n return;\n }\n registerRoomEvents();\n\n return () => {\n unregisterRoomEvents();\n };\n }, [room]);\n\n useUpdateEffect(() => {\n setMicrophoneDeviceId(microphone.deviceId);\n }, [microphone.deviceId]);\n\n useUpdateEffect(() => {\n setSpeakerDeviceId(speaker.deviceId);\n }, [speaker.deviceId]);\n\n useUpdateEffect(() => {\n setCameraDeviceId(camera.deviceId);\n }, [camera.deviceId]);\n\n useUpdateEffect(() => {\n if (agentConnectionState !== 'joined') {\n return;\n }\n room.localParticipant.setMicrophoneEnabled(microphone.enabled);\n }, [microphone.enabled, agentConnectionState]);\n\n useUpdateEffect(() => {\n if (agentConnectionState !== 'joined') {\n return;\n }\n room.localParticipant.setCameraEnabled(camera.enabled, {\n resolution: { width: 1920, height: 1080 },\n });\n }, [camera.enabled, agentConnectionState]);\n\n useEffect(() => {\n if (!autoJoin) {\n return;\n }\n\n if (conversationStatus === 'created') {\n void join();\n }\n }, [conversationStatus, autoJoin]);\n\n useEffect(() => {\n if (!joinedAt || conversation.data?.limit === undefined) {\n return;\n }\n\n timer.restart(new Date(joinedAt.getTime() + conversation.data!.limit!));\n }, [joinedAt, conversation.data?.limit]);\n\n async function join() {\n invariant(userConnectionState === 'idle', 'Already joined');\n invariant(conversation.data, 'Conversation is not ready');\n const url = new URL(conversation?.data?.url || '');\n const accessToken = url.searchParams.get('token');\n try {\n setUserConnectionState('joining');\n await room?.connect(conversation.data.url, accessToken!, {});\n\n // sometimes the agent is already joined before the user joins the conversation\n const remoteParticipant = findJoinedAgentParticipant(room, type);\n if (remoteParticipant && agentConnectionState !== 'joined') {\n handleAgentJoin(remoteParticipant);\n }\n\n enableMediaDevices();\n } catch (error) {\n console.error('Failed to join', error);\n setUserConnectionState('join-error');\n }\n }\n\n function enableMediaDevices() {\n if (startWithMicrophone) {\n setMicrophone({ ...microphone, enabled: true });\n }\n if (startWithCamera && type === 'video') {\n setCamera({ ...camera, enabled: true });\n }\n }\n\n function handleTranscriptionReceivedEvent(\n segments: TranscriptionSegment[],\n participant?: Participant,\n ) {\n const transcriptions: ConversationTranscription[] = segments.map((s) => ({\n id: s.id,\n message: s.text,\n source: participant?.kind === ParticipantKind.AGENT ? 'agent' : 'user',\n }));\n\n setTranscriptions((old) => mergeTranscriptions(transcriptions, old));\n }\n\n function handleTurnDetectionEvent(data: TurnDetectionData) {\n const conversationId = conversationRef.current?.id;\n const { participantKind, state: eventState } = data;\n switch (participantKind) {\n case 'STANDARD':\n if (eventState === 'listening') {\n userStopRef.current = {\n ts: performance.now(),\n turnDetectionStrategy: data.turnDetectionStrategy,\n sequence: data.sequence,\n speechId: data.speechId,\n };\n } else {\n userStopRef.current = null;\n }\n emitEvent(\n eventState === 'speaking' ? 'userSpeakStart' : 'userSpeakStop',\n { conversationId: conversationId! },\n );\n setUserActivityState(eventState);\n break;\n case 'AGENT':\n if (eventState === 'speaking' && userStopRef.current) {\n const latencyMs = performance.now() - userStopRef.current.ts;\n emitEvent('responseLatency', {\n conversationId: conversationId!,\n latencyMs,\n turnDetectionStrategy: userStopRef.current.turnDetectionStrategy,\n sequence: userStopRef.current.sequence,\n speechId: data.speechId ?? userStopRef.current.speechId,\n });\n userStopRef.current = null;\n }\n emitEvent(\n eventState === 'speaking' ? 'agentSpeakStart' : 'agentSpeakStop',\n { conversationId: conversationId! },\n );\n setAgentActivityState(eventState);\n break;\n }\n }\n\n function handleInactivityWarningEvent(data: InactivityWarningData) {\n emitEvent('inactivityWarning', {\n conversationId: conversationRef.current?.id!,\n gracePeriodSec: data.grace_period_sec,\n });\n }\n\n async function handleToolCallsEvent(data: ToolCallsData) {\n await toolRuntimeRef.current.resolveIncomingToolCalls(data, toolAdapter);\n }\n\n const dataChannelHandlers: {\n guard: (data: unknown) => boolean;\n handle: (data: unknown) => void | Promise<void>;\n }[] = [\n {\n guard: checkIsToolCallsData,\n handle: handleToolCallsEvent as (data: unknown) => Promise<void>,\n },\n {\n guard: checkIsTurnDetectionData,\n handle: handleTurnDetectionEvent as (data: unknown) => void,\n },\n {\n guard: checkIsInactivityWarningData,\n handle: handleInactivityWarningEvent as (data: unknown) => void,\n },\n // ... other data channel handlers\n ];\n\n function handleDataReceivedEvent(payload: Uint8Array) {\n const decoded = decodeDataPayload(payload);\n if (decoded === null) {\n return;\n }\n for (const { guard, handle } of dataChannelHandlers) {\n if (guard(decoded)) {\n void Promise.resolve(handle(decoded)).catch((error) => {\n console.error('Failed to handle data channel payload', error);\n });\n return;\n }\n }\n }\n\n function handleConnectedEvent() {\n setUserConnectionState('joined');\n setAgentConnectionState('joining');\n emitEvent('callStarted', {\n conversationId: conversationRef.current?.id!,\n });\n }\n\n async function handleEncryptionErrorEvent(error: Error) {\n console.error('Encryption error', error);\n setConversationStatus('error');\n }\n\n function handleDisconnectedEvent(reason?: DisconnectReason) {\n userStopRef.current = null;\n setAgentActivityState('idle');\n setAgentConnectionState('idle');\n setUserActivityState('idle');\n setUserConnectionState('idle');\n\n switch (reason) {\n case DisconnectReason.CLIENT_INITIATED:\n break;\n case DisconnectReason.ROOM_CLOSED:\n setConversationStatus('destroyed');\n break;\n default:\n console.error('Conversation disconnected with reason', reason);\n setConversationStatus('error');\n break;\n }\n }\n\n function handleParticipantConnectedEvent(participant: RemoteParticipant) {\n if (shouldHandleParticipantConnected(participant, type)) {\n handleAgentJoin(participant);\n }\n }\n\n function handleAgentJoin(participant: RemoteParticipant) {\n setAgentConnectionState('joined');\n emitEvent('agentJoined', {\n conversationId: conversationRef.current?.id!,\n participant,\n });\n }\n\n function handleParticipantDisconnectedEvent(participant: RemoteParticipant) {\n setAgentConnectionState('left');\n emitEvent('agentLeft', {\n conversationId: conversationRef.current?.id!,\n participant,\n });\n }\n\n async function leave() {\n if (userConnectionState !== 'joined' && userConnectionState !== 'joining') {\n return;\n }\n\n try {\n setUserConnectionState('leaving');\n await room?.disconnect(true);\n setConversationStatus('destroyed');\n setJoinedAt(undefined);\n emitEvent('callEnded', {\n conversationId: conversationRef.current?.id!,\n });\n } catch (error) {\n console.error('Failed to leave', error);\n setConversationStatus('error');\n }\n }\n\n async function handleExpire() {\n emitEvent('timeExceeded', {\n conversationId: conversationRef.current?.id!,\n limit: conversationRef.current?.limit!,\n });\n await leave();\n setConversationStatus('exceeded');\n }\n\n async function interrupt() {\n emitEvent('interrupt', {\n conversationId: conversationRef.current?.id!,\n });\n\n await publishConversationDataMessage(room, {}, 'interrupt');\n }\n\n async function dismissInactivity() {\n await publishConversationDataMessage(\n room,\n { type: 'inactivity_dismiss' },\n 'inactivity_dismiss',\n );\n }\n\n async function send(message: string, attachments: Attachment[] = []) {\n await room?.localParticipant.sendText(message, {\n topic: 'lk.chat',\n attributes: {\n attachments: safeStringify(attachments),\n },\n });\n emitEvent('userSendMessage', {\n conversationId: conversationRef.current?.id!,\n message,\n });\n }\n\n function registerRoomEvents() {\n room\n .on('connected', handleConnectedEvent)\n .on('encryptionError', handleEncryptionErrorEvent)\n .on('disconnected', handleDisconnectedEvent)\n .on('transcriptionReceived', handleTranscriptionReceivedEvent)\n .on('participantConnected', handleParticipantConnectedEvent)\n .on('participantDisconnected', handleParticipantDisconnectedEvent)\n .on('dataReceived', handleDataReceivedEvent);\n }\n\n function unregisterRoomEvents() {\n room.off('connected', handleConnectedEvent);\n room.off('encryptionError', handleEncryptionErrorEvent);\n room.off('disconnected', handleDisconnectedEvent);\n room.off('transcriptionReceived', handleTranscriptionReceivedEvent);\n room.off('participantConnected', handleParticipantConnectedEvent);\n room.off('participantDisconnected', handleParticipantDisconnectedEvent);\n room.off('dataReceived', handleDataReceivedEvent);\n }\n\n return {\n status: conversationStatus,\n conversation: conversation.data,\n join,\n send,\n interrupt,\n dismissInactivity,\n leave,\n };\n}\n"],"mappings":";;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,cAAA,GAAAD,OAAA;AASA,IAAAE,UAAA,GAAAC,sBAAA,CAAAH,OAAA;AACA,IAAAI,eAAA,GAAAJ,OAAA;AACA,IAAAK,SAAA,GAAAL,OAAA;AACA,IAAAM,gBAAA,GAAAN,OAAA;AACA,IAAAO,MAAA,GAAAP,OAAA;AACA,IAAAQ,aAAA,GAAAR,OAAA;AAeA,IAAAS,sBAAA,GAAAT,OAAA;AAEA,IAAAU,yBAAA,GAAAV,OAAA;AACA,IAAAW,sBAAA,GAAAX,OAAA;AACA,IAAAY,MAAA,GAAAZ,OAAA;AAaO,SAASa,eAAeA,CAAC;EAC9BC,QAAQ,GAAG,IAAI;EACfC,IAAI;EACJC,MAAM;EACNC,mBAAmB,GAAG,KAAK;EAC3BC,eAAe,GAAG;AACI,CAAC,EAAyB;EAAA,IAAAC,mBAAA;EAChD,IAAAC,0BAAW,EAACC,uBAAQ,CAACC,MAAM,CAAC;EAE5B,MAAMC,YAAY,GAAG,IAAAC,4CAAqB,EAAC;IAAET,IAAI;IAAEC;EAAO,CAAC,CAAC;EAC5D,MAAMS,IAAI,GAAG,IAAAC,8BAAa,EAAC,CAAC;EAC5B,MAAMC,OAAO,GAAG,IAAAC,mBAAY,EAACC,mBAAK,CAACF,OAAO,CAAC;EAC3C,MAAM,CAACG,UAAU,EAAEC,aAAa,CAAC,GAAG,IAAAC,cAAO,EAACH,mBAAK,CAACC,UAAU,CAAC;EAC7D,MAAM,CAACG,MAAM,E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CAAC,WAAW,EAAE7B,oBAAoB,CAAC,CACrC6B,EAAE,CAAC,iBAAiB,EAAE3B,0BAA0B,CAAC,CACjD2B,EAAE,CAAC,cAAc,EAAE1B,uBAAuB,CAAC,CAC3C0B,EAAE,CAAC,uBAAuB,EAAE1E,gCAAgC,CAAC,CAC7D0E,EAAE,CAAC,sBAAsB,EAAErB,+BAA+B,CAAC,CAC3DqB,EAAE,CAAC,yBAAyB,EAAElB,kCAAkC,CAAC,CACjEkB,EAAE,CAAC,cAAc,EAAEpC,uBAAuB,CAAC;EAChD;EAEA,SAASnE,oBAAoBA,CAAA,EAAG;IAC9BtD,IAAI,CAAC8J,GAAG,CAAC,WAAW,EAAE9B,oBAAoB,CAAC;IAC3ChI,IAAI,CAAC8J,GAAG,CAAC,iBAAiB,EAAE5B,0BAA0B,CAAC;IACvDlI,IAAI,CAAC8J,GAAG,CAAC,cAAc,EAAE3B,uBAAuB,CAAC;IACjDnI,IAAI,CAAC8J,GAAG,CAAC,uBAAuB,EAAE3E,gCAAgC,CAAC;IACnEnF,IAAI,CAAC8J,GAAG,CAAC,sBAAsB,EAAEtB,+BAA+B,CAAC;IACjExI,IAAI,CAAC8J,GAAG,CAAC,yBAAyB,EAAEnB,kCAAkC,CAAC;IACvE3I,IAAI,CAAC8J,GAAG,CAAC,cAAc,EAAErC,uBAAuB,CAAC;EACnD;EAEA,OAAO;IACLsC,MAAM,EAAE7I,kBAAkB;IAC1BpB,YAAY,EAAEA,YAAY,CAACkB,IAAI;IAC/B+C,IAAI;IACJuF,IAAI;IACJJ,SAAS;IACTG,iBAAiB;IACjBR;EACF,CAAC;AACH","ignoreList":[]}
1
+ {"version":3,"names":["_react","require","_livekitClient","_invariant","_interopRequireDefault","_reactTimerHook","_reactUse","_componentsReact","_jotai","_conversation","_useCreateConversation","_useEmitConversationEvent","_useToolCallingRuntime","_utils","useConversation","autoJoin","type","config","startWithMicrophone","startWithCamera","_conversation$data2","setLogLevel","LogLevel","silent","conversation","useCreateConversation","room","useEnsureRoom","speaker","useAtomValue","state","microphone","setMicrophone","useAtom","camera","setCamera","toolRuntime","useToolCallingRuntime","toolAdapter","createLivekitToolAdapter","conversationRef","useLatest","data","toolRuntimeRef","conversationStatus","setConversationStatus","userConnectionState","setUserConnectionState","joinedAt","setJoinedAt","setActiveMediaDevice","setMicrophoneDeviceId","useMediaDeviceSelect","kind","setSpeakerDeviceId","setCameraDeviceId","requestPermissions","enabled","setTranscriptions","useSetAtom","transcriptions","agentConnectionState","setAgentConnectionState","setAgentActivityState","agentActivityState","setUserActivityState","userActivityState","emitEvent","useEmitConversationEvent","userStopRef","useRef","inactivityTimeoutRef","timer","useTimer","autoStart","onExpire","handleExpire","expiryTimestamp","Date","useEffect","registerRoomEvents","unregisterRoomEvents","useUpdateEffect","deviceId","localParticipant","setMicrophoneEnabled","setCameraEnabled","resolution","width","height","join","_conversation$data","limit","undefined","restart","getTime","_conversation$data3","invariant","url","URL","accessToken","searchParams","get","connect","remoteParticipant","findJoinedAgentParticipant","handleAgentJoin","enableMediaDevices","error","console","handleTranscriptionReceivedEvent","segments","participant","map","s","id","message","text","source","ParticipantKind","AGENT","old","mergeTranscriptions","handleTurnDetectionEvent","_conversationRef$curr","conversationId","current","participantKind","eventState","ts","performance","now","turnDetectionStrategy","sequence","speechId","latencyMs","handleInactivityWarningEvent","_conversationRef$curr2","gracePeriodSec","grace_period_sec","handleInactivityTimeoutEvent","_data","_conversationRef$curr3","handleToolCallsEvent","resolveIncomingToolCalls","dataChannelHandlers","guard","checkIsToolCallsData","handle","checkIsTurnDetectionData","checkIsInactivityWarningData","checkIsInactivityTimeoutData","handleDataReceivedEvent","payload","decoded","decodeDataPayload","Promise","resolve","catch","handleConnectedEvent","_conversationRef$curr4","handleEncryptionErrorEvent","handleDisconnectedEvent","reason","DisconnectReason","CLIENT_INITIATED","ROOM_CLOSED","handleParticipantConnectedEvent","shouldHandleParticipantConnected","_conversationRef$curr5","handleParticipantDisconnectedEvent","_conversationRef$curr6","leave","_conversationRef$curr7","disconnect","_conversationRef$curr8","_conversationRef$curr9","interrupt","_conversationRef$curr0","publishConversationDataMessage","dismissInactivity","send","attachments","_conversationRef$curr1","sendText","topic","attributes","safeStringify","on","off","status"],"sources":["../../../../../src/hooks/useConversation/livekit/useConversation.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\nimport {\n DisconnectReason,\n Participant,\n setLogLevel,\n ParticipantKind,\n RemoteParticipant,\n TranscriptionSegment,\n LogLevel,\n} from 'livekit-client';\nimport invariant from 'invariant';\nimport { useTimer } from 'react-timer-hook';\nimport { useLatest, useUpdateEffect } from 'react-use';\nimport { useEnsureRoom, useMediaDeviceSelect } from '@livekit/components-react';\nimport { useAtom, useAtomValue, useSetAtom } from 'jotai';\nimport {\n Conversation,\n state,\n type ConversationTranscription,\n} from '../../../conversation';\nimport type {\n UseConversationResult,\n UseConversationOptions,\n Attachment,\n} from '../types';\nimport type {\n InactivityWarningData,\n InactivityTimeoutData,\n TurnDetectionData,\n ToolCallsData,\n} from './types';\nimport { useCreateConversation } from '../../useCreateConversation';\n\nimport { useEmitConversationEvent } from '../../useConversationEvents/useEmitConversationEvent';\nimport { useToolCallingRuntime } from '../../useTools/runtime/useToolCallingRuntime';\nimport {\n checkIsTurnDetectionData,\n checkIsInactivityWarningData,\n checkIsInactivityTimeoutData,\n checkIsToolCallsData,\n createLivekitToolAdapter,\n decodeDataPayload,\n findJoinedAgentParticipant,\n mergeTranscriptions,\n publishConversationDataMessage,\n safeStringify,\n shouldHandleParticipantConnected,\n} from './utils';\n\nexport function useConversation({\n autoJoin = true,\n type,\n config,\n startWithMicrophone = false,\n startWithCamera = false,\n}: UseConversationOptions): UseConversationResult {\n setLogLevel(LogLevel.silent);\n\n const conversation = useCreateConversation({ type, config });\n const room = useEnsureRoom();\n const speaker = useAtomValue(state.speaker);\n const [microphone, setMicrophone] = useAtom(state.microphone);\n const [camera, setCamera] = useAtom(state.camera);\n const toolRuntime = useToolCallingRuntime();\n const toolAdapter = createLivekitToolAdapter(room);\n\n // for use in register/unregister room events\n const conversationRef = useLatest<Conversation | undefined>(\n conversation.data,\n );\n const toolRuntimeRef = useLatest(toolRuntime);\n\n const [conversationStatus, setConversationStatus] = useAtom(\n state.conversationStatus,\n );\n\n const [userConnectionState, setUserConnectionState] = useAtom(\n state.userConnectionState,\n );\n\n const [joinedAt, setJoinedAt] = useAtom(state.joinedAt);\n\n const { setActiveMediaDevice: setMicrophoneDeviceId } = useMediaDeviceSelect({\n kind: 'audioinput',\n });\n\n const { setActiveMediaDevice: setSpeakerDeviceId } = useMediaDeviceSelect({\n kind: 'audiooutput',\n });\n\n const { setActiveMediaDevice: setCameraDeviceId } = useMediaDeviceSelect({\n kind: 'videoinput',\n requestPermissions: camera.enabled,\n });\n\n const setTranscriptions = useSetAtom(state.transcriptions);\n const [agentConnectionState, setAgentConnectionState] = useAtom(\n state.agentConnectionState,\n );\n const setAgentActivityState = useSetAtom(state.agentActivityState);\n const setUserActivityState = useSetAtom(state.userActivityState);\n const emitEvent = useEmitConversationEvent();\n\n const userStopRef = useRef<{\n ts: number;\n turnDetectionStrategy: string;\n sequence?: number;\n speechId?: string;\n } | null>(null);\n const inactivityTimeoutRef = useRef(false);\n\n const timer = useTimer({\n autoStart: false,\n onExpire: handleExpire,\n expiryTimestamp: new Date(),\n });\n\n useEffect(() => {\n if (!room) {\n return;\n }\n registerRoomEvents();\n\n return () => {\n unregisterRoomEvents();\n };\n }, [room]);\n\n useUpdateEffect(() => {\n setMicrophoneDeviceId(microphone.deviceId);\n }, [microphone.deviceId]);\n\n useUpdateEffect(() => {\n setSpeakerDeviceId(speaker.deviceId);\n }, [speaker.deviceId]);\n\n useUpdateEffect(() => {\n setCameraDeviceId(camera.deviceId);\n }, [camera.deviceId]);\n\n useUpdateEffect(() => {\n if (agentConnectionState !== 'joined') {\n return;\n }\n room.localParticipant.setMicrophoneEnabled(microphone.enabled);\n }, [microphone.enabled, agentConnectionState]);\n\n useUpdateEffect(() => {\n if (agentConnectionState !== 'joined') {\n return;\n }\n room.localParticipant.setCameraEnabled(camera.enabled, {\n resolution: { width: 1920, height: 1080 },\n });\n }, [camera.enabled, agentConnectionState]);\n\n useEffect(() => {\n if (!autoJoin) {\n return;\n }\n\n if (conversationStatus === 'created') {\n void join();\n }\n }, [conversationStatus, autoJoin]);\n\n useEffect(() => {\n if (!joinedAt || conversation.data?.limit === undefined) {\n return;\n }\n\n timer.restart(new Date(joinedAt.getTime() + conversation.data!.limit!));\n }, [joinedAt, conversation.data?.limit]);\n\n async function join() {\n invariant(userConnectionState === 'idle', 'Already joined');\n invariant(conversation.data, 'Conversation is not ready');\n const url = new URL(conversation?.data?.url || '');\n const accessToken = url.searchParams.get('token');\n try {\n setUserConnectionState('joining');\n await room?.connect(conversation.data.url, accessToken!, {});\n\n // sometimes the agent is already joined before the user joins the conversation\n const remoteParticipant = findJoinedAgentParticipant(room, type);\n if (remoteParticipant && agentConnectionState !== 'joined') {\n handleAgentJoin(remoteParticipant);\n }\n\n enableMediaDevices();\n } catch (error) {\n console.error('Failed to join', error);\n setUserConnectionState('join-error');\n }\n }\n\n function enableMediaDevices() {\n if (startWithMicrophone) {\n setMicrophone({ ...microphone, enabled: true });\n }\n if (startWithCamera && type === 'video') {\n setCamera({ ...camera, enabled: true });\n }\n }\n\n function handleTranscriptionReceivedEvent(\n segments: TranscriptionSegment[],\n participant?: Participant,\n ) {\n const transcriptions: ConversationTranscription[] = segments.map((s) => ({\n id: s.id,\n message: s.text,\n source: participant?.kind === ParticipantKind.AGENT ? 'agent' : 'user',\n }));\n\n setTranscriptions((old) => mergeTranscriptions(transcriptions, old));\n }\n\n function handleTurnDetectionEvent(data: TurnDetectionData) {\n const conversationId = conversationRef.current?.id;\n const { participantKind, state: eventState } = data;\n switch (participantKind) {\n case 'STANDARD':\n if (eventState === 'listening') {\n userStopRef.current = {\n ts: performance.now(),\n turnDetectionStrategy: data.turnDetectionStrategy,\n sequence: data.sequence,\n speechId: data.speechId,\n };\n } else {\n userStopRef.current = null;\n }\n emitEvent(\n eventState === 'speaking' ? 'userSpeakStart' : 'userSpeakStop',\n { conversationId: conversationId! },\n );\n setUserActivityState(eventState);\n break;\n case 'AGENT':\n if (eventState === 'speaking' && userStopRef.current) {\n const latencyMs = performance.now() - userStopRef.current.ts;\n emitEvent('responseLatency', {\n conversationId: conversationId!,\n latencyMs,\n turnDetectionStrategy: userStopRef.current.turnDetectionStrategy,\n sequence: userStopRef.current.sequence,\n speechId: data.speechId ?? userStopRef.current.speechId,\n });\n userStopRef.current = null;\n }\n emitEvent(\n eventState === 'speaking' ? 'agentSpeakStart' : 'agentSpeakStop',\n { conversationId: conversationId! },\n );\n setAgentActivityState(eventState);\n break;\n }\n }\n\n function handleInactivityWarningEvent(data: InactivityWarningData) {\n emitEvent('inactivityWarning', {\n conversationId: conversationRef.current?.id!,\n gracePeriodSec: data.grace_period_sec,\n });\n }\n\n function handleInactivityTimeoutEvent(_data: InactivityTimeoutData) {\n inactivityTimeoutRef.current = true;\n emitEvent('inactivityTimeout', {\n conversationId: conversationRef.current?.id!,\n });\n }\n\n async function handleToolCallsEvent(data: ToolCallsData) {\n await toolRuntimeRef.current.resolveIncomingToolCalls(data, toolAdapter);\n }\n\n const dataChannelHandlers: {\n guard: (data: unknown) => boolean;\n handle: (data: unknown) => void | Promise<void>;\n }[] = [\n {\n guard: checkIsToolCallsData,\n handle: handleToolCallsEvent as (data: unknown) => Promise<void>,\n },\n {\n guard: checkIsTurnDetectionData,\n handle: handleTurnDetectionEvent as (data: unknown) => void,\n },\n {\n guard: checkIsInactivityWarningData,\n handle: handleInactivityWarningEvent as (data: unknown) => void,\n },\n {\n guard: checkIsInactivityTimeoutData,\n handle: handleInactivityTimeoutEvent as (data: unknown) => void,\n },\n ];\n\n function handleDataReceivedEvent(payload: Uint8Array) {\n const decoded = decodeDataPayload(payload);\n if (decoded === null) {\n return;\n }\n for (const { guard, handle } of dataChannelHandlers) {\n if (guard(decoded)) {\n void Promise.resolve(handle(decoded)).catch((error) => {\n console.error('Failed to handle data channel payload', error);\n });\n return;\n }\n }\n }\n\n function handleConnectedEvent() {\n setUserConnectionState('joined');\n setAgentConnectionState('joining');\n emitEvent('callStarted', {\n conversationId: conversationRef.current?.id!,\n });\n }\n\n async function handleEncryptionErrorEvent(error: Error) {\n console.error('Encryption error', error);\n setConversationStatus('error');\n }\n\n function handleDisconnectedEvent(reason?: DisconnectReason) {\n userStopRef.current = null;\n setAgentActivityState('idle');\n setAgentConnectionState('idle');\n setUserActivityState('idle');\n setUserConnectionState('idle');\n\n if (inactivityTimeoutRef.current) {\n inactivityTimeoutRef.current = false;\n setConversationStatus('inactivity-timeout');\n return;\n }\n\n switch (reason) {\n case DisconnectReason.CLIENT_INITIATED:\n break;\n case DisconnectReason.ROOM_CLOSED:\n setConversationStatus('destroyed');\n break;\n default:\n console.error('Conversation disconnected with reason', reason);\n setConversationStatus('error');\n break;\n }\n }\n\n function handleParticipantConnectedEvent(participant: RemoteParticipant) {\n if (shouldHandleParticipantConnected(participant, type)) {\n handleAgentJoin(participant);\n }\n }\n\n function handleAgentJoin(participant: RemoteParticipant) {\n setAgentConnectionState('joined');\n emitEvent('agentJoined', {\n conversationId: conversationRef.current?.id!,\n participant,\n });\n }\n\n function handleParticipantDisconnectedEvent(participant: RemoteParticipant) {\n setAgentConnectionState('left');\n emitEvent('agentLeft', {\n conversationId: conversationRef.current?.id!,\n participant,\n });\n }\n\n async function leave() {\n if (userConnectionState !== 'joined' && userConnectionState !== 'joining') {\n return;\n }\n\n try {\n setUserConnectionState('leaving');\n await room?.disconnect(true);\n setConversationStatus('destroyed');\n setJoinedAt(undefined);\n emitEvent('callEnded', {\n conversationId: conversationRef.current?.id!,\n });\n } catch (error) {\n console.error('Failed to leave', error);\n setConversationStatus('error');\n }\n }\n\n async function handleExpire() {\n emitEvent('timeExceeded', {\n conversationId: conversationRef.current?.id!,\n limit: conversationRef.current?.limit!,\n });\n await leave();\n setConversationStatus('exceeded');\n }\n\n async function interrupt() {\n emitEvent('interrupt', {\n conversationId: conversationRef.current?.id!,\n });\n\n await publishConversationDataMessage(room, {}, 'interrupt');\n }\n\n async function dismissInactivity() {\n await publishConversationDataMessage(\n room,\n { type: 'inactivity_dismiss' },\n 'inactivity_dismiss',\n );\n }\n\n async function send(message: string, attachments: Attachment[] = []) {\n await room?.localParticipant.sendText(message, {\n topic: 'lk.chat',\n attributes: {\n attachments: safeStringify(attachments),\n },\n });\n emitEvent('userSendMessage', {\n conversationId: conversationRef.current?.id!,\n message,\n });\n }\n\n function registerRoomEvents() {\n room\n .on('connected', handleConnectedEvent)\n .on('encryptionError', handleEncryptionErrorEvent)\n .on('disconnected', handleDisconnectedEvent)\n .on('transcriptionReceived', handleTranscriptionReceivedEvent)\n .on('participantConnected', handleParticipantConnectedEvent)\n .on('participantDisconnected', handleParticipantDisconnectedEvent)\n .on('dataReceived', handleDataReceivedEvent);\n }\n\n function unregisterRoomEvents() {\n room.off('connected', handleConnectedEvent);\n room.off('encryptionError', handleEncryptionErrorEvent);\n room.off('disconnected', handleDisconnectedEvent);\n room.off('transcriptionReceived', handleTranscriptionReceivedEvent);\n room.off('participantConnected', handleParticipantConnectedEvent);\n room.off('participantDisconnected', handleParticipantDisconnectedEvent);\n room.off('dataReceived', handleDataReceivedEvent);\n }\n\n return {\n status: conversationStatus,\n conversation: conversation.data,\n join,\n send,\n interrupt,\n dismissInactivity,\n leave,\n };\n}\n"],"mappings":";;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,cAAA,GAAAD,OAAA;AASA,IAAAE,UAAA,GAAAC,sBAAA,CAAAH,OAAA;AACA,IAAAI,eAAA,GAAAJ,OAAA;AACA,IAAAK,SAAA,GAAAL,OAAA;AACA,IAAAM,gBAAA,GAAAN,OAAA;AACA,IAAAO,MAAA,GAAAP,OAAA;AACA,IAAAQ,aAAA,GAAAR,OAAA;AAgBA,IAAAS,sBAAA,GAAAT,OAAA;AAEA,IAAAU,yBAAA,GAAAV,OAAA;AACA,I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,EAAE1B,uBAAuB,CAAC,CAC3C0B,EAAE,CAAC,uBAAuB,EAAE9E,gCAAgC,CAAC,CAC7D8E,EAAE,CAAC,sBAAsB,EAAErB,+BAA+B,CAAC,CAC3DqB,EAAE,CAAC,yBAAyB,EAAElB,kCAAkC,CAAC,CACjEkB,EAAE,CAAC,cAAc,EAAEpC,uBAAuB,CAAC;EAChD;EAEA,SAASvE,oBAAoBA,CAAA,EAAG;IAC9BvD,IAAI,CAACmK,GAAG,CAAC,WAAW,EAAE9B,oBAAoB,CAAC;IAC3CrI,IAAI,CAACmK,GAAG,CAAC,iBAAiB,EAAE5B,0BAA0B,CAAC;IACvDvI,IAAI,CAACmK,GAAG,CAAC,cAAc,EAAE3B,uBAAuB,CAAC;IACjDxI,IAAI,CAACmK,GAAG,CAAC,uBAAuB,EAAE/E,gCAAgC,CAAC;IACnEpF,IAAI,CAACmK,GAAG,CAAC,sBAAsB,EAAEtB,+BAA+B,CAAC;IACjE7I,IAAI,CAACmK,GAAG,CAAC,yBAAyB,EAAEnB,kCAAkC,CAAC;IACvEhJ,IAAI,CAACmK,GAAG,CAAC,cAAc,EAAErC,uBAAuB,CAAC;EACnD;EAEA,OAAO;IACLsC,MAAM,EAAElJ,kBAAkB;IAC1BpB,YAAY,EAAEA,YAAY,CAACkB,IAAI;IAC/BgD,IAAI;IACJ2F,IAAI;IACJJ,SAAS;IACTG,iBAAiB;IACjBR;EACF,CAAC;AACH","ignoreList":[]}
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
+ exports.checkIsInactivityTimeoutData = checkIsInactivityTimeoutData;
4
5
  exports.checkIsInactivityWarningData = checkIsInactivityWarningData;
5
6
  exports.checkIsToolCallsData = checkIsToolCallsData;
6
7
  exports.checkIsTurnDetectionData = checkIsTurnDetectionData;
@@ -19,6 +20,9 @@ function checkIsTurnDetectionData(data) {
19
20
  function checkIsInactivityWarningData(data) {
20
21
  return typeof data === 'object' && data !== null && 'type' in data && data.type === 'inactivity_warning' && 'grace_period_sec' in data && typeof data.grace_period_sec === 'number';
21
22
  }
23
+ function checkIsInactivityTimeoutData(data) {
24
+ return typeof data === 'object' && data !== null && 'type' in data && data.type === 'inactivity_timeout';
25
+ }
22
26
  function checkIsToolCallsData(data) {
23
27
  return typeof data === 'object' && data !== null && 'type' in data && data.type === 'tool_calls' && 'requestId' in data && typeof data.requestId === 'string' && 'functionCalls' in data && Array.isArray(data.functionCalls);
24
28
  }
@@ -1 +1 @@
1
- {"version":3,"names":["decodeDataPayload","data","JSON","parse","TextDecoder","decode","error","console","checkIsTurnDetectionData","checkIsInactivityWarningData","type","grace_period_sec","checkIsToolCallsData","requestId","Array","isArray","functionCalls"],"sources":["../../../../../../src/hooks/useConversation/livekit/utils/dataChannel.ts"],"sourcesContent":["import type {\n InactivityWarningData,\n ToolCallsData,\n TurnDetectionData,\n} from '../types';\n\nexport function decodeDataPayload(data: Uint8Array): unknown {\n try {\n return JSON.parse(new TextDecoder().decode(data));\n } catch (error) {\n console.error('Failed to decode data received payload', error);\n return null;\n }\n}\n\nexport function checkIsTurnDetectionData(\n data: unknown,\n): data is TurnDetectionData {\n return (\n typeof data === 'object' &&\n data !== null &&\n 'participantKind' in data &&\n 'state' in data\n );\n}\n\nexport function checkIsInactivityWarningData(\n data: unknown,\n): data is InactivityWarningData {\n return (\n typeof data === 'object' &&\n data !== null &&\n 'type' in data &&\n (data as Record<string, unknown>).type === 'inactivity_warning' &&\n 'grace_period_sec' in data &&\n typeof (data as Record<string, unknown>).grace_period_sec === 'number'\n );\n}\n\nexport function checkIsToolCallsData(data: unknown): data is ToolCallsData {\n return (\n typeof data === 'object' &&\n data !== null &&\n 'type' in data &&\n (data as Record<string, unknown>).type === 'tool_calls' &&\n 'requestId' in data &&\n typeof (data as Record<string, unknown>).requestId === 'string' &&\n 'functionCalls' in data &&\n Array.isArray((data as Record<string, unknown>).functionCalls)\n );\n}\n"],"mappings":";;;;;;;AAMO,SAASA,iBAAiBA,CAACC,IAAgB,EAAW;EAC3D,IAAI;IACF,OAAOC,IAAI,CAACC,KAAK,CAAC,IAAIC,WAAW,CAAC,CAAC,CAACC,MAAM,CAACJ,IAAI,CAAC,CAAC;EACnD,CAAC,CAAC,OAAOK,KAAK,EAAE;IACdC,OAAO,CAACD,KAAK,CAAC,wCAAwC,EAAEA,KAAK,CAAC;IAC9D,OAAO,IAAI;EACb;AACF;AAEO,SAASE,wBAAwBA,CACtCP,IAAa,EACc;EAC3B,OACE,OAAOA,IAAI,KAAK,QAAQ,IACxBA,IAAI,KAAK,IAAI,IACb,iBAAiB,IAAIA,IAAI,IACzB,OAAO,IAAIA,IAAI;AAEnB;AAEO,SAASQ,4BAA4BA,CAC1CR,IAAa,EACkB;EAC/B,OACE,OAAOA,IAAI,KAAK,QAAQ,IACxBA,IAAI,KAAK,IAAI,IACb,MAAM,IAAIA,IAAI,IACbA,IAAI,CAA6BS,IAAI,KAAK,oBAAoB,IAC/D,kBAAkB,IAAIT,IAAI,IAC1B,OAAQA,IAAI,CAA6BU,gBAAgB,KAAK,QAAQ;AAE1E;AAEO,SAASC,oBAAoBA,CAACX,IAAa,EAAyB;EACzE,OACE,OAAOA,IAAI,KAAK,QAAQ,IACxBA,IAAI,KAAK,IAAI,IACb,MAAM,IAAIA,IAAI,IACbA,IAAI,CAA6BS,IAAI,KAAK,YAAY,IACvD,WAAW,IAAIT,IAAI,IACnB,OAAQA,IAAI,CAA6BY,SAAS,KAAK,QAAQ,IAC/D,eAAe,IAAIZ,IAAI,IACvBa,KAAK,CAACC,OAAO,CAAEd,IAAI,CAA6Be,aAAa,CAAC;AAElE","ignoreList":[]}
1
+ {"version":3,"names":["decodeDataPayload","data","JSON","parse","TextDecoder","decode","error","console","checkIsTurnDetectionData","checkIsInactivityWarningData","type","grace_period_sec","checkIsInactivityTimeoutData","checkIsToolCallsData","requestId","Array","isArray","functionCalls"],"sources":["../../../../../../src/hooks/useConversation/livekit/utils/dataChannel.ts"],"sourcesContent":["import type {\n InactivityWarningData,\n InactivityTimeoutData,\n ToolCallsData,\n TurnDetectionData,\n} from '../types';\n\nexport function decodeDataPayload(data: Uint8Array): unknown {\n try {\n return JSON.parse(new TextDecoder().decode(data));\n } catch (error) {\n console.error('Failed to decode data received payload', error);\n return null;\n }\n}\n\nexport function checkIsTurnDetectionData(\n data: unknown,\n): data is TurnDetectionData {\n return (\n typeof data === 'object' &&\n data !== null &&\n 'participantKind' in data &&\n 'state' in data\n );\n}\n\nexport function checkIsInactivityWarningData(\n data: unknown,\n): data is InactivityWarningData {\n return (\n typeof data === 'object' &&\n data !== null &&\n 'type' in data &&\n (data as Record<string, unknown>).type === 'inactivity_warning' &&\n 'grace_period_sec' in data &&\n typeof (data as Record<string, unknown>).grace_period_sec === 'number'\n );\n}\n\nexport function checkIsInactivityTimeoutData(\n data: unknown,\n): data is InactivityTimeoutData {\n return (\n typeof data === 'object' &&\n data !== null &&\n 'type' in data &&\n (data as Record<string, unknown>).type === 'inactivity_timeout'\n );\n}\n\nexport function checkIsToolCallsData(data: unknown): data is ToolCallsData {\n return (\n typeof data === 'object' &&\n data !== null &&\n 'type' in data &&\n (data as Record<string, unknown>).type === 'tool_calls' &&\n 'requestId' in data &&\n typeof (data as Record<string, unknown>).requestId === 'string' &&\n 'functionCalls' in data &&\n Array.isArray((data as Record<string, unknown>).functionCalls)\n );\n}\n"],"mappings":";;;;;;;;AAOO,SAASA,iBAAiBA,CAACC,IAAgB,EAAW;EAC3D,IAAI;IACF,OAAOC,IAAI,CAACC,KAAK,CAAC,IAAIC,WAAW,CAAC,CAAC,CAACC,MAAM,CAACJ,IAAI,CAAC,CAAC;EACnD,CAAC,CAAC,OAAOK,KAAK,EAAE;IACdC,OAAO,CAACD,KAAK,CAAC,wCAAwC,EAAEA,KAAK,CAAC;IAC9D,OAAO,IAAI;EACb;AACF;AAEO,SAASE,wBAAwBA,CACtCP,IAAa,EACc;EAC3B,OACE,OAAOA,IAAI,KAAK,QAAQ,IACxBA,IAAI,KAAK,IAAI,IACb,iBAAiB,IAAIA,IAAI,IACzB,OAAO,IAAIA,IAAI;AAEnB;AAEO,SAASQ,4BAA4BA,CAC1CR,IAAa,EACkB;EAC/B,OACE,OAAOA,IAAI,KAAK,QAAQ,IACxBA,IAAI,KAAK,IAAI,IACb,MAAM,IAAIA,IAAI,IACbA,IAAI,CAA6BS,IAAI,KAAK,oBAAoB,IAC/D,kBAAkB,IAAIT,IAAI,IAC1B,OAAQA,IAAI,CAA6BU,gBAAgB,KAAK,QAAQ;AAE1E;AAEO,SAASC,4BAA4BA,CAC1CX,IAAa,EACkB;EAC/B,OACE,OAAOA,IAAI,KAAK,QAAQ,IACxBA,IAAI,KAAK,IAAI,IACb,MAAM,IAAIA,IAAI,IACbA,IAAI,CAA6BS,IAAI,KAAK,oBAAoB;AAEnE;AAEO,SAASG,oBAAoBA,CAACZ,IAAa,EAAyB;EACzE,OACE,OAAOA,IAAI,KAAK,QAAQ,IACxBA,IAAI,KAAK,IAAI,IACb,MAAM,IAAIA,IAAI,IACbA,IAAI,CAA6BS,IAAI,KAAK,YAAY,IACvD,WAAW,IAAIT,IAAI,IACnB,OAAQA,IAAI,CAA6Ba,SAAS,KAAK,QAAQ,IAC/D,eAAe,IAAIb,IAAI,IACvBc,KAAK,CAACC,OAAO,CAAEf,IAAI,CAA6BgB,aAAa,CAAC;AAElE","ignoreList":[]}
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["../../../../src/hooks/useConversationEvents/types.tsx"],"sourcesContent":["import React from 'react';\nimport { RemoteParticipant } from 'livekit-client';\n\nexport type UserSpeakStartPayload = {\n conversationId: string;\n};\n\nexport type TimeExceededPayload = {\n conversationId: string;\n limit: number;\n};\n\nexport type UserSpeakStopPayload = {\n conversationId: string;\n};\n\nexport type AgentSpeakStartPayload = {\n conversationId: string;\n};\n\nexport type AgentSpeakStopPayload = {\n conversationId: string;\n};\n\nexport type ResponseLatencyPayload = {\n conversationId: string;\n latencyMs: number;\n turnDetectionStrategy: string;\n sequence?: number;\n speechId?: string;\n};\n\nexport type InterruptPayload = {\n conversationId: string;\n};\n\nexport type CallStartedPayload = {\n conversationId: string;\n};\n\nexport type CallEndedPayload = {\n conversationId: string;\n};\n\nexport type UserSendMessagePayload = {\n conversationId: string;\n message: string;\n};\n\nexport type AgentJoinedPayload = {\n conversationId: string;\n participant: RemoteParticipant;\n};\n\nexport type AgentLeftPayload = {\n conversationId: string;\n participant?: RemoteParticipant;\n};\n\nexport type AgentVideoLoadedPayload = {\n videoRef: React.RefObject<HTMLVideoElement>;\n};\n\nexport type InactivityWarningPayload = {\n conversationId: string;\n gracePeriodSec: number;\n};\n\nexport type ConversationEventMap = {\n userSpeakStart: UserSpeakStartPayload;\n userSpeakStop: UserSpeakStopPayload;\n agentSpeakStart: AgentSpeakStartPayload;\n agentSpeakStop: AgentSpeakStopPayload;\n responseLatency: ResponseLatencyPayload;\n interrupt: InterruptPayload;\n callStarted: CallStartedPayload;\n callEnded: CallEndedPayload;\n userSendMessage: UserSendMessagePayload;\n agentJoined: AgentJoinedPayload;\n agentLeft: AgentLeftPayload;\n timeExceeded: TimeExceededPayload;\n agentVideoLoaded: AgentVideoLoadedPayload;\n inactivityWarning: InactivityWarningPayload;\n};\n\nexport type ConversationEvent = keyof ConversationEventMap;\n\nexport type EventPayload<T extends ConversationEvent> = ConversationEventMap[T];\n"],"mappings":"","ignoreList":[]}
1
+ {"version":3,"names":[],"sources":["../../../../src/hooks/useConversationEvents/types.tsx"],"sourcesContent":["import React from 'react';\nimport { RemoteParticipant } from 'livekit-client';\n\nexport type UserSpeakStartPayload = {\n conversationId: string;\n};\n\nexport type TimeExceededPayload = {\n conversationId: string;\n limit: number;\n};\n\nexport type UserSpeakStopPayload = {\n conversationId: string;\n};\n\nexport type AgentSpeakStartPayload = {\n conversationId: string;\n};\n\nexport type AgentSpeakStopPayload = {\n conversationId: string;\n};\n\nexport type ResponseLatencyPayload = {\n conversationId: string;\n latencyMs: number;\n turnDetectionStrategy: string;\n sequence?: number;\n speechId?: string;\n};\n\nexport type InterruptPayload = {\n conversationId: string;\n};\n\nexport type CallStartedPayload = {\n conversationId: string;\n};\n\nexport type CallEndedPayload = {\n conversationId: string;\n};\n\nexport type UserSendMessagePayload = {\n conversationId: string;\n message: string;\n};\n\nexport type AgentJoinedPayload = {\n conversationId: string;\n participant: RemoteParticipant;\n};\n\nexport type AgentLeftPayload = {\n conversationId: string;\n participant?: RemoteParticipant;\n};\n\nexport type AgentVideoLoadedPayload = {\n videoRef: React.RefObject<HTMLVideoElement>;\n};\n\nexport type InactivityWarningPayload = {\n conversationId: string;\n gracePeriodSec: number;\n};\n\nexport type InactivityTimeoutPayload = {\n conversationId: string;\n};\n\nexport type ConversationEventMap = {\n userSpeakStart: UserSpeakStartPayload;\n userSpeakStop: UserSpeakStopPayload;\n agentSpeakStart: AgentSpeakStartPayload;\n agentSpeakStop: AgentSpeakStopPayload;\n responseLatency: ResponseLatencyPayload;\n interrupt: InterruptPayload;\n callStarted: CallStartedPayload;\n callEnded: CallEndedPayload;\n userSendMessage: UserSendMessagePayload;\n agentJoined: AgentJoinedPayload;\n agentLeft: AgentLeftPayload;\n timeExceeded: TimeExceededPayload;\n agentVideoLoaded: AgentVideoLoadedPayload;\n inactivityWarning: InactivityWarningPayload;\n inactivityTimeout: InactivityTimeoutPayload;\n};\n\nexport type ConversationEvent = keyof ConversationEventMap;\n\nexport type EventPayload<T extends ConversationEvent> = ConversationEventMap[T];\n"],"mappings":"","ignoreList":[]}
@@ -1 +1 @@
1
- {"version":3,"names":["_invariant","_interopRequireDefault","require","_react","_jotai","_reactUse","_conversation","_sdkConsumer","_helpers","useCreateConversation","options","type","config","chatConfig","chatId","namespace","personaChatService","useSdkConsumer","setConversationStatus","useSetAtom","state","conversationStatus","conversation","setConversation","useAtom","useEffect","create","then","catch","error","_err$message","err","status","message","includes","useUnmount","destroy","limit","data","invariant","result","createInteractiveConversation","url","id","createVoiceConversation","Error","endInteractiveConversation","conversationId","undefined"],"sources":["../../../../src/hooks/useCreateConversation/useCreateConversation.ts"],"sourcesContent":["import invariant from 'invariant';\nimport { useEffect } from 'react';\nimport { useAtom, useSetAtom } from 'jotai';\nimport { useUnmount } from 'react-use';\n\nimport {\n state,\n type ConversationType,\n type Conversation,\n} from '../../conversation';\nimport { useSdkConsumer } from '../../sdk-consumer';\nimport { limit } from './helpers';\n\nexport type UseCreateConversationOptions = {\n type: ConversationType;\n config?: Record<string, unknown>;\n};\n\nexport type UseCreateConversationResult = {\n data?: Conversation;\n destroy(): Promise<void>;\n};\n\nexport function useCreateConversation(\n options: UseCreateConversationOptions,\n): UseCreateConversationResult {\n const { type, config: chatConfig } = options;\n\n const { chatId, namespace, personaChatService } = useSdkConsumer();\n\n const setConversationStatus = useSetAtom(state.conversationStatus);\n const [conversation, setConversation] = useAtom(state.conversation);\n\n useEffect(() => {\n void create()\n .then(() => setConversationStatus('created'))\n .catch((error: unknown) => {\n const err = error as { status?: number; message?: string };\n if (err.status === 404 && err.message?.includes('not found')) {\n setConversationStatus('voice-not-found');\n } else {\n setConversationStatus('initialization-error');\n }\n });\n }, [type, chatId]);\n\n useUnmount(() => {\n if (!conversation) {\n return;\n }\n void destroy();\n });\n\n useEffect(() => {\n if (!conversation) {\n return;\n }\n if (conversation.limit === 0) {\n setConversationStatus('exceeded');\n }\n }, [conversation]);\n\n return { data: conversation, destroy };\n\n async function create() {\n invariant(\n personaChatService,\n 'personaChatService is required. Pass it as a prop to LegendsPlatform.',\n );\n\n switch (type) {\n case 'video': {\n const result = await personaChatService.createInteractiveConversation({\n chatId,\n namespace,\n chatConfig,\n });\n setConversation({\n type: 'video',\n url: result.url,\n id: result.id,\n limit: limit(result.limit),\n });\n break;\n }\n case 'audio': {\n const result = await personaChatService.createVoiceConversation({\n chatId,\n namespace,\n chatConfig,\n });\n setConversation({\n type: 'audio',\n url: result.url,\n id: result.id,\n limit: limit(result.limit),\n });\n break;\n }\n default:\n throw new Error(`Unsupported conversation type: ${type}`);\n }\n }\n\n async function destroy() {\n invariant(conversation, 'Conversation is not ready');\n invariant(personaChatService, 'personaChatService is required.');\n\n setConversationStatus('destroying');\n\n await personaChatService\n .endInteractiveConversation({\n chatId,\n conversationId: conversation.id,\n namespace,\n })\n .catch(() => {});\n\n setConversationStatus('destroyed');\n setConversation(undefined);\n }\n}\n"],"mappings":";;;;;AAAA,IAAAA,UAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AACA,IAAAG,SAAA,GAAAH,OAAA;AAEA,IAAAI,aAAA,GAAAJ,OAAA;AAKA,IAAAK,YAAA,GAAAL,OAAA;AACA,IAAAM,QAAA,GAAAN,OAAA;AAYO,SAASO,qBAAqBA,CACnCC,OAAqC,EACR;EAC7B,MAAM;IAAEC,IAAI;IAAEC,MAAM,EAAEC;EAAW,CAAC,GAAGH,OAAO;EAE5C,MAAM;IAAEI,MAAM;IAAEC,SAAS;IAAEC;EAAmB,CAAC,GAAG,IAAAC,2BAAc,EAAC,CAAC;EAElE,MAAMC,qBAAqB,GAAG,IAAAC,iBAAU,EAACC,mBAAK,CAACC,kBAAkB,CAAC;EAClE,MAAM,CAACC,YAAY,EAAEC,eAAe,CAAC,GAAG,IAAAC,cAAO,EAACJ,mBAAK,CAACE,YAAY,CAAC;EAEnE,IAAAG,gBAAS,EAAC,MAAM;IACd,KAAKC,MAAM,CAAC,CAAC,CACVC,IAAI,CAAC,MAAMT,qBAAqB,CAAC,SAAS,CAAC,CAAC,CAC5CU,KAAK,CAAEC,KAAc,IAAK;MAAA,IAAAC,YAAA;MACzB,MAAMC,GAAG,GAAGF,KAA8C;MAC1D,IAAIE,GAAG,CAACC,MAAM,KAAK,GAAG,KAAAF,YAAA,GAAIC,GAAG,CAACE,OAAO,aAAXH,YAAA,CAAaI,QAAQ,CAAC,WAAW,CAAC,EAAE;QAC5DhB,qBAAqB,CAAC,iBAAiB,CAAC;MAC1C,CAAC,MAAM;QACLA,qBAAqB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,CAACH,KAAK;UAC3B,CAAC,CAAC;UACF;QACF;MACA;QACE,MAAM,IAAIQ,KAAK,CAAC,kCAAkClC,IAAI,EAAE,CAAC;IAC7D;EACF;EAEA,eAAeyB,OAAOA,CAAA,EAAG;IACvB,IAAAG,kBAAS,EAACjB,YAAY,EAAE,2BAA2B,CAAC;IACpD,IAAAiB,kBAAS,EAACvB,kBAAkB,EAAE,iCAAiC,CAAC;IAEhEE,qBAAqB,CAAC,YAAY,CAAC;IAEnC,MAAMF,kBAAkB,CACrB8B,0BAA0B,CAAC;MAC1BhC,MAAM;MACNiC,cAAc,EAAEzB,YAAY,CAACqB,EAAE;MAC/B5B;IACF,CAAC,CAAC,CACDa,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IAElBV,qBAAqB,CAAC,WAAW,CAAC;IAClCK,eAAe,CAACyB,SAAS,CAAC;EAC5B;AACF","ignoreList":[]}
1
+ {"version":3,"names":["_invariant","_interopRequireDefault","require","_react","_jotai","_reactUse","_conversation","_sdkConsumer","_helpers","useCreateConversation","options","type","config","chatConfig","chatId","namespace","personaChatService","useSdkConsumer","setConversationStatus","useSetAtom","state","conversationStatus","conversation","setConversation","useAtom","useEffect","create","then","catch","error","_err$message","err","status","message","includes","useUnmount","destroy","limit","data","invariant","result","createInteractiveConversation","url","id","createVoiceConversation","Error","endInteractiveConversation","conversationId","undefined"],"sources":["../../../../src/hooks/useCreateConversation/useCreateConversation.ts"],"sourcesContent":["import invariant from 'invariant';\nimport { useEffect } from 'react';\nimport { useAtom, useSetAtom } from 'jotai';\nimport { useUnmount } from 'react-use';\n\nimport {\n state,\n type ConversationType,\n type Conversation,\n} from '../../conversation';\nimport { useSdkConsumer } from '../../sdk-consumer';\nimport { limit } from './helpers';\n\nexport type UseCreateConversationOptions = {\n type: ConversationType;\n config?: object;\n};\n\nexport type UseCreateConversationResult = {\n data?: Conversation;\n destroy(): Promise<void>;\n};\n\nexport function useCreateConversation(\n options: UseCreateConversationOptions,\n): UseCreateConversationResult {\n const { type, config: chatConfig } = options;\n\n const { chatId, namespace, personaChatService } = useSdkConsumer();\n\n const setConversationStatus = useSetAtom(state.conversationStatus);\n const [conversation, setConversation] = useAtom(state.conversation);\n\n useEffect(() => {\n void create()\n .then(() => setConversationStatus('created'))\n .catch((error: unknown) => {\n const err = error as { status?: number; message?: string };\n if (err.status === 404 && err.message?.includes('not found')) {\n setConversationStatus('voice-not-found');\n } else {\n setConversationStatus('initialization-error');\n }\n });\n }, [type, chatId]);\n\n useUnmount(() => {\n if (!conversation) {\n return;\n }\n void destroy();\n });\n\n useEffect(() => {\n if (!conversation) {\n return;\n }\n if (conversation.limit === 0) {\n setConversationStatus('exceeded');\n }\n }, [conversation]);\n\n return { data: conversation, destroy };\n\n async function create() {\n invariant(\n personaChatService,\n 'personaChatService is required. Pass it as a prop to LegendsPlatform.',\n );\n\n switch (type) {\n case 'video': {\n const result = await personaChatService.createInteractiveConversation({\n chatId,\n namespace,\n chatConfig,\n });\n setConversation({\n type: 'video',\n url: result.url,\n id: result.id,\n limit: limit(result.limit),\n });\n break;\n }\n case 'audio': {\n const result = await personaChatService.createVoiceConversation({\n chatId,\n namespace,\n chatConfig,\n });\n setConversation({\n type: 'audio',\n url: result.url,\n id: result.id,\n limit: limit(result.limit),\n });\n break;\n }\n default:\n throw new Error(`Unsupported conversation type: ${type}`);\n }\n }\n\n async function destroy() {\n invariant(conversation, 'Conversation is not ready');\n invariant(personaChatService, 'personaChatService is required.');\n\n setConversationStatus('destroying');\n\n await personaChatService\n .endInteractiveConversation({\n chatId,\n conversationId: conversation.id,\n namespace,\n })\n .catch(() => {});\n\n setConversationStatus('destroyed');\n setConversation(undefined);\n }\n}\n"],"mappings":";;;;;AAAA,IAAAA,UAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AACA,IAAAG,SAAA,GAAAH,OAAA;AAEA,IAAAI,aAAA,GAAAJ,OAAA;AA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,YAAY,CAACqB,EAAE;MAC/B5B;IACF,CAAC,CAAC,CACDa,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IAElBV,qBAAqB,CAAC,WAAW,CAAC;IAClCK,eAAe,CAACyB,SAAS,CAAC;EAC5B;AACF","ignoreList":[]}
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.append = append;
5
+ exports.appendMany = appendMany;
6
+ function append(message) {
7
+ return data => {
8
+ if (!data) {
9
+ return {
10
+ pages: [{
11
+ messages: [message]
12
+ }],
13
+ pageParams: [undefined]
14
+ };
15
+ }
16
+ const [page, ...rest] = data.pages;
17
+ return {
18
+ ...data,
19
+ pages: [{
20
+ ...page,
21
+ messages: [message, ...((page == null ? void 0 : page.messages) ?? [])]
22
+ }, ...rest]
23
+ };
24
+ };
25
+ }
26
+ function appendMany(messages) {
27
+ return data => {
28
+ if (!data || !messages.length) {
29
+ return data ?? {
30
+ pages: [],
31
+ pageParams: []
32
+ };
33
+ }
34
+ return messages.reduceRight((acc, message) => append(message)(acc), data);
35
+ };
36
+ }
37
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["append","message","data","pages","messages","pageParams","undefined","page","rest","appendMany","length","reduceRight","acc"],"sources":["../../../../src/hooks/useTextChat/helpers.ts"],"sourcesContent":["import type { InfiniteData } from '@tanstack/react-query';\nimport type { ChatMessage, QueryMessagesResult } from '../../types';\n\ntype MessagesCache = InfiniteData<QueryMessagesResult>;\n\nexport function append(message: ChatMessage) {\n return (data: MessagesCache | undefined): MessagesCache => {\n if (!data) {\n return { pages: [{ messages: [message] }], pageParams: [undefined] };\n }\n const [page, ...rest] = data.pages;\n return {\n ...data,\n pages: [\n { ...page, messages: [message, ...(page?.messages ?? [])] },\n ...rest,\n ],\n };\n };\n}\n\nexport function appendMany(messages: ChatMessage[]) {\n return (data: MessagesCache | undefined): MessagesCache => {\n if (!data || !messages.length) {\n return data ?? { pages: [], pageParams: [] };\n }\n return messages.reduceRight(\n (acc: MessagesCache, message) => append(message)(acc),\n data,\n );\n };\n}\n"],"mappings":";;;;;AAKO,SAASA,MAAMA,CAACC,OAAoB,EAAE;EAC3C,OAAQC,IAA+B,IAAoB;IACzD,IAAI,CAACA,IAAI,EAAE;MACT,OAAO;QAAEC,KAAK,EAAE,CAAC;UAAEC,QAAQ,EAAE,CAACH,OAAO;QAAE,CAAC,CAAC;QAAEI,UAAU,EAAE,CAACC,SAAS;MAAE,CAAC;IACtE;IACA,MAAM,CAACC,IAAI,EAAE,GAAGC,IAAI,CAAC,GAAGN,IAAI,CAACC,KAAK;IAClC,OAAO;MACL,GAAGD,IAAI;MACPC,KAAK,EAAE,CACL;QAAE,GAAGI,IAAI;QAAEH,QAAQ,EAAE,CAACH,OAAO,EAAE,IAAI,CAAAM,IAAI,oBAAJA,IAAI,CAAEH,QAAQ,KAAI,EAAE,CAAC;MAAE,CAAC,EAC3D,GAAGI,IAAI;IAEX,CAAC;EACH,CAAC;AACH;AAEO,SAASC,UAAUA,CAACL,QAAuB,EAAE;EAClD,OAAQF,IAA+B,IAAoB;IACzD,IAAI,CAACA,IAAI,IAAI,CAACE,QAAQ,CAACM,MAAM,EAAE;MAC7B,OAAOR,IAAI,IAAI;QAAEC,KAAK,EAAE,EAAE;QAAEE,UAAU,EAAE;MAAG,CAAC;IAC9C;IACA,OAAOD,QAAQ,CAACO,WAAW,CACzB,CAACC,GAAkB,EAAEX,OAAO,KAAKD,MAAM,CAACC,OAAO,CAAC,CAACW,GAAG,CAAC,EACrDV,IACF,CAAC;EACH,CAAC;AACH","ignoreList":[]}
@@ -1 +1 @@
1
- {"version":3,"names":["_reactQuery","require","_sdkConsumer","_usePendingMessages","_useToolAwareSend","useTextChat","options","limit","chatId","namespace","personaChatService","useSdkConsumer","send","useToolAwareSend","pending","usePendingMessages","data","messages","status","fetchNextPage","refetch","useInfiniteQuery","queryKey","queryFn","pageParam","nextCursor","undefined","queryChatMessages","cursor","getNextPageParam","lastPage","initialPageParam","select","pages","flatMap","page","fetchNext","sendMessage","message"],"sources":["../../../../src/hooks/useTextChat/useTextChat.ts"],"sourcesContent":["import { useInfiniteQuery } from '@tanstack/react-query';\nimport type { ChatMessage } from '../../types';\nimport { useSdkConsumer } from '../../sdk-consumer';\nimport { usePendingMessages } from './usePendingMessages';\nimport { useToolAwareSend } from './useToolAwareSend';\n\ninterface UseTextChatOptions {\n limit?: number;\n}\n\nexport function useTextChat(options: UseTextChatOptions = {}) {\n const { limit = 100 } = options;\n\n const { chatId, namespace, personaChatService } = useSdkConsumer();\n\n const send = useToolAwareSend(chatId, namespace);\n const pending = usePendingMessages();\n\n const {\n data: messages = [],\n status,\n fetchNextPage,\n refetch,\n } = useInfiniteQuery({\n queryKey: ['chat-messages', chatId, namespace],\n queryFn: async ({ pageParam }: { pageParam?: string }) => {\n if (!personaChatService) {\n return { messages: [], nextCursor: undefined };\n }\n return personaChatService.queryChatMessages({\n chatId,\n namespace,\n limit,\n cursor: pageParam,\n });\n },\n getNextPageParam: (lastPage) => lastPage.nextCursor,\n initialPageParam: undefined as string | undefined,\n select: (data) =>\n data.pages.flatMap((page) => page.messages as ChatMessage[]),\n });\n\n return {\n status,\n pending,\n messages,\n fetchNext,\n refetch,\n send: sendMessage,\n };\n\n function sendMessage(message: { message?: string }) {\n return send(message);\n }\n\n function fetchNext() {\n void fetchNextPage();\n }\n}\n"],"mappings":";;;;AAAA,IAAAA,WAAA,GAAAC,OAAA;AAEA,IAAAC,YAAA,GAAAD,OAAA;AACA,IAAAE,mBAAA,GAAAF,OAAA;AACA,IAAAG,iBAAA,GAAAH,OAAA;AAMO,SAASI,WAAWA,CAACC,OAA2B,GAAG,CAAC,CAAC,EAAE;EAC5D,MAAM;IAAEC,KAAK,GAAG;EAAI,CAAC,GAAGD,OAAO;EAE/B,MAAM;IAAEE,MAAM;IAAEC,SAAS;IAAEC;EAAmB,CAAC,GAAG,IAAAC,2BAAc,EAAC,CAAC;EAElE,MAAMC,IAAI,GAAG,IAAAC,kCAAgB,EAACL,MAAM,EAAEC,SAAS,CAAC;EAChD,MAAMK,OAAO,GAAG,IAAAC,sCAAkB,EAAC,CAAC;EAEpC,MAAM;IACJC,IAAI,EAAEC,QAAQ,GAAG,EAAE;IACnBC,MAAM;IACNC,aAAa;IACbC;EACF,CAAC,GAAG,IAAAC,4BAAgB,EAAC;IACnBC,QAAQ,EAAE,CAAC,eAAe,EAAEd,MAAM,EAAEC,SAAS,CAAC;IAC9Cc,OAAO,EAAE,MAAAA,CAAO;MAAEC;IAAkC,CAAC,KAAK;MACxD,IAAI,CAACd,kBAAkB,EAAE;QACvB,OAAO;UAAEO,QAAQ,EAAE,EAAE;UAAEQ,UAAU,EAAEC;QAAU,CAAC;MAChD;MACA,OAAOhB,kBAAkB,CAACiB,iBAAiB,CAAC;QAC1CnB,MAAM;QACNC,SAAS;QACTF,KAAK;QACLqB,MAAM,EAAEJ;MACV,CAAC,CAAC;IACJ,CAAC;IACDK,gBAAgB,EAAGC,QAAQ,IAAKA,QAAQ,CAACL,UAAU;IACnDM,gBAAgB,EAAEL,SAA+B;IACjDM,MAAM,EAAGhB,IAAI,IACXA,IAAI,CAACiB,KAAK,CAACC,OAAO,CAAEC,IAAI,IAAKA,IAAI,CAAClB,QAAyB;EAC/D,CAAC,CAAC;EAEF,OAAO;IACLC,MAAM;IACNJ,OAAO;IACPG,QAAQ;IACRmB,SAAS;IACThB,OAAO;IACPR,IAAI,EAAEyB;EACR,CAAC;EAED,SAASA,WAAWA,CAACC,OAA6B,EAAE;IAClD,OAAO1B,IAAI,CAAC0B,OAAO,CAAC;EACtB;EAEA,SAASF,SAASA,CAAA,EAAG;IACnB,KAAKjB,aAAa,CAAC,CAAC;EACtB;AACF","ignoreList":[]}
1
+ {"version":3,"names":["_reactQuery","require","_sdkConsumer","_usePendingMessages","_useToolAwareSend","useTextChat","options","limit","chatId","namespace","personaChatService","useSdkConsumer","send","useToolAwareSend","pending","usePendingMessages","data","messages","status","fetchNextPage","refetch","useInfiniteQuery","queryKey","queryFn","pageParam","nextCursor","undefined","queryChatMessages","cursor","getNextPageParam","lastPage","initialPageParam","select","pages","flatMap","page","fetchNext","sendMessage","message"],"sources":["../../../../src/hooks/useTextChat/useTextChat.ts"],"sourcesContent":["import { useInfiniteQuery } from '@tanstack/react-query';\nimport type { ChatMessage, ChatMessageAttachment } from '../../types';\nimport { useSdkConsumer } from '../../sdk-consumer';\nimport { usePendingMessages } from './usePendingMessages';\nimport { useToolAwareSend } from './useToolAwareSend';\n\ninterface UseTextChatOptions {\n limit?: number;\n}\n\nexport function useTextChat(options: UseTextChatOptions = {}) {\n const { limit = 100 } = options;\n\n const { chatId, namespace, personaChatService } = useSdkConsumer();\n\n const send = useToolAwareSend(chatId, namespace);\n const pending = usePendingMessages();\n\n const {\n data: messages = [],\n status,\n fetchNextPage,\n refetch,\n } = useInfiniteQuery({\n queryKey: ['chat-messages', chatId, namespace],\n queryFn: async ({ pageParam }: { pageParam?: string }) => {\n if (!personaChatService) {\n return { messages: [], nextCursor: undefined };\n }\n return personaChatService.queryChatMessages({\n chatId,\n namespace,\n limit,\n cursor: pageParam,\n });\n },\n getNextPageParam: (lastPage) => lastPage.nextCursor,\n initialPageParam: undefined as string | undefined,\n select: (data) =>\n data.pages.flatMap((page) => page.messages as ChatMessage[]),\n });\n\n return {\n status,\n pending,\n messages,\n fetchNext,\n refetch,\n send: sendMessage,\n };\n\n function sendMessage(message: {\n message?: string;\n chatConfig?: object;\n attachments?: ChatMessageAttachment[];\n }) {\n return send(message);\n }\n\n function fetchNext() {\n void fetchNextPage();\n }\n}\n"],"mappings":";;;;AAAA,IAAAA,WAAA,GAAAC,OAAA;AAEA,IAAAC,YAAA,GAAAD,OAAA;AACA,IAAAE,mBAAA,GAAAF,OAAA;AACA,IAAAG,iBAAA,GAAAH,OAAA;AAMO,SAASI,WAAWA,CAACC,OAA2B,GAAG,CAAC,CAAC,EAAE;EAC5D,MAAM;IAAEC,KAAK,GAAG;EAA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 @@
2
2
 
3
3
  exports.__esModule = true;
4
4
  exports.useToolAwareSend = useToolAwareSend;
5
+ var _reactQuery = require("@tanstack/react-query");
5
6
  var _sdkConsumer = require("../../sdk-consumer");
6
- var _errors = require("../useTools/model/errors");
7
+ var _useTools = require("../useTools");
7
8
  var _useToolCallingRuntime = require("../useTools/runtime/useToolCallingRuntime");
9
+ var _helpers = require("./helpers");
8
10
  function useToolAwareSend(chatId, namespace) {
9
11
  const {
10
12
  personaChatService
11
13
  } = (0, _sdkConsumer.useSdkConsumer)();
12
14
  const toolRuntime = (0, _useToolCallingRuntime.useToolCallingRuntime)();
15
+ const queryClient = (0, _reactQuery.useQueryClient)();
16
+ const mutation = (0, _reactQuery.useMutation)({
17
+ mutationKey: ['pending-chat-message'],
18
+ mutationFn: params => {
19
+ if (!personaChatService) {
20
+ throw new Error('personaChatService is required to send messages.');
21
+ }
22
+ return personaChatService.sendChatMessage(params);
23
+ },
24
+ onSuccess(data, variables) {
25
+ var _data$messages;
26
+ const queryKey = ['chat-messages', chatId, namespace];
27
+ if (variables.message) {
28
+ queryClient.setQueryData(queryKey, (0, _helpers.append)({
29
+ id: Date.now().toString(),
30
+ content: variables.message,
31
+ role: 'user',
32
+ attachments: variables.attachments
33
+ }));
34
+ }
35
+ if ((_data$messages = data.messages) != null && _data$messages.length) {
36
+ queryClient.setQueryData(queryKey, (0, _helpers.appendMany)(data.messages));
37
+ }
38
+ }
39
+ });
13
40
  return async function send(message) {
14
41
  if (toolRuntime.isRunning) {
15
- throw new _errors.ToolExecutionInProgressError();
16
- }
17
- if (!personaChatService) {
18
- throw new Error('personaChatService is required to send messages.');
42
+ throw new _useTools.ToolExecutionInProgressError();
19
43
  }
20
- const initialResponse = await personaChatService.sendChatMessage({
44
+ const initialResponse = await mutation.mutateAsync({
21
45
  chatId,
22
46
  namespace,
23
- message: message.message ?? ''
47
+ message: message.message ?? '',
48
+ chatConfig: message.chatConfig,
49
+ attachments: message.attachments
24
50
  });
25
- return toolRuntime.resolveResponseToolCalls(initialResponse, functionCallResults => personaChatService.sendChatMessage({
51
+ return toolRuntime.resolveResponseToolCalls(initialResponse, functionCallResults => mutation.mutateAsync({
26
52
  chatId,
27
53
  namespace,
28
54
  message: '',
@@ -1 +1 @@
1
- {"version":3,"names":["_sdkConsumer","require","_errors","_useToolCallingRuntime","useToolAwareSend","chatId","namespace","personaChatService","useSdkConsumer","toolRuntime","useToolCallingRuntime","send","message","isRunning","ToolExecutionInProgressError","Error","initialResponse","sendChatMessage","resolveResponseToolCalls","functionCallResults"],"sources":["../../../../src/hooks/useTextChat/useToolAwareSend.ts"],"sourcesContent":["import type { FunctionCall } from '../../types/service';\nimport { useSdkConsumer } from '../../sdk-consumer';\nimport { ToolExecutionInProgressError } from '../useTools/model/errors';\nimport { useToolCallingRuntime } from '../useTools/runtime/useToolCallingRuntime';\n\nexport interface SendChatMessageResponse {\n functionCalls?: FunctionCall[];\n}\n\nexport function useToolAwareSend(chatId: string, namespace: string) {\n const { personaChatService } = useSdkConsumer();\n const toolRuntime = useToolCallingRuntime<SendChatMessageResponse>();\n\n return async function send(message: { message?: string }) {\n if (toolRuntime.isRunning) {\n throw new ToolExecutionInProgressError();\n }\n\n if (!personaChatService) {\n throw new Error('personaChatService is required to send messages.');\n }\n\n const initialResponse = await personaChatService.sendChatMessage({\n chatId,\n namespace,\n message: message.message ?? '',\n });\n\n return toolRuntime.resolveResponseToolCalls(\n initialResponse,\n (functionCallResults) =>\n personaChatService.sendChatMessage({\n chatId,\n namespace,\n message: '',\n functionCallResults,\n }),\n );\n };\n}\n"],"mappings":";;;;AACA,IAAAA,YAAA,GAAAC,OAAA;AACA,IAAAC,OAAA,GAAAD,OAAA;AACA,IAAAE,sBAAA,GAAAF,OAAA;AAMO,SAASG,gBAAgBA,CAACC,MAAc,EAAEC,SAAiB,EAAE;EAClE,MAAM;IAAEC;EAAmB,CAAC,GAAG,IAAAC,2BAAc,EAAC,CAAC;EAC/C,MAAMC,WAAW,GAAG,IAAAC,4CAAqB,EAA0B,CAAC;EAEpE,OAAO,eAAeC,IAAIA,CAACC,OAA6B,EAAE;IACxD,IAAIH,WAAW,CAACI,SAAS,EAAE;MACzB,MAAM,IAAIC,oCAA4B,CAAC,CAAC;IAC1C;IAEA,IAAI,CAACP,kBAAkB,EAAE;MACvB,MAAM,IAAIQ,KAAK,CAAC,kDAAkD,CAAC;IACrE;IAEA,MAAMC,eAAe,GAAG,MAAMT,kBAAkB,CAACU,eAAe,CAAC;MAC/DZ,MAAM;MACNC,SAAS;MACTM,OAAO,EAAEA,OAAO,CAACA,OAAO,IAAI;IAC9B,CAAC,CAAC;IAEF,OAAOH,WAAW,CAACS,wBAAwB,CACzCF,eAAe,EACdG,mBAAmB,IAClBZ,kBAAkB,CAACU,eAAe,CAAC;MACjCZ,MAAM;MACNC,SAAS;MACTM,OAAO,EAAE,EAAE;MACXO;IACF,CAAC,CACL,CAAC;EACH,CAAC;AACH","ignoreList":[]}
1
+ {"version":3,"names":["_reactQuery","require","_sdkConsumer","_useTools","_useToolCallingRuntime","_helpers","useToolAwareSend","chatId","namespace","personaChatService","useSdkConsumer","toolRuntime","useToolCallingRuntime","queryClient","useQueryClient","mutation","useMutation","mutationKey","mutationFn","params","Error","sendChatMessage","onSuccess","data","variables","_data$messages","queryKey","message","setQueryData","append","id","Date","now","toString","content","role","attachments","messages","length","appendMany","send","isRunning","ToolExecutionInProgressError","initialResponse","mutateAsync","chatConfig","resolveResponseToolCalls","functionCallResults"],"sources":["../../../../src/hooks/useTextChat/useToolAwareSend.ts"],"sourcesContent":["import { useMutation, useQueryClient } from '@tanstack/react-query';\nimport type {\n FunctionCall,\n ChatMessageAttachment,\n SendMessageParams,\n} from '../../types';\nimport { useSdkConsumer } from '../../sdk-consumer';\nimport { ToolExecutionInProgressError } from '../useTools';\nimport { useToolCallingRuntime } from '../useTools/runtime/useToolCallingRuntime';\nimport { append, appendMany } from './helpers';\n\nexport interface SendChatMessageResponse {\n functionCalls?: FunctionCall[];\n}\n\nexport function useToolAwareSend(chatId: string, namespace: string) {\n const { personaChatService } = useSdkConsumer();\n const toolRuntime = useToolCallingRuntime<SendChatMessageResponse>();\n const queryClient = useQueryClient();\n\n const mutation = useMutation({\n mutationKey: ['pending-chat-message'],\n mutationFn: (params: SendMessageParams) => {\n if (!personaChatService) {\n throw new Error('personaChatService is required to send messages.');\n }\n return personaChatService.sendChatMessage(params);\n },\n onSuccess(data, variables) {\n const queryKey = ['chat-messages', chatId, namespace];\n\n if (variables.message) {\n queryClient.setQueryData(\n queryKey,\n append({\n id: Date.now().toString(),\n content: variables.message,\n role: 'user',\n attachments: variables.attachments,\n }),\n );\n }\n\n if (data.messages?.length) {\n queryClient.setQueryData(queryKey, appendMany(data.messages));\n }\n },\n });\n\n return async function send(message: {\n message?: string;\n chatConfig?: object;\n attachments?: ChatMessageAttachment[];\n }) {\n if (toolRuntime.isRunning) {\n throw new ToolExecutionInProgressError();\n }\n\n const initialResponse = await mutation.mutateAsync({\n chatId,\n namespace,\n message: message.message ?? '',\n chatConfig: message.chatConfig,\n attachments: message.attachments,\n });\n\n return toolRuntime.resolveResponseToolCalls(\n initialResponse,\n (functionCallResults) =>\n mutation.mutateAsync({\n chatId,\n namespace,\n message: '',\n functionCallResults,\n }),\n );\n };\n}\n"],"mappings":";;;;AAAA,IAAAA,WAAA,GAAAC,OAAA;AAMA,IAAAC,YAAA,GAAAD,OAAA;AACA,IAAAE,SAAA,GAAAF,OAAA;AACA,IAAAG,sBAAA,GAAAH,OAAA;AACA,IAAAI,QAAA,GAAAJ,OAAA;AAMO,SAASK,gBAAgBA,CAACC,MAAc,EAAEC,SAAiB,EAAE;EAClE,MAAM;IAAEC;EAAmB,CAAC,GAAG,IAAAC,2BAAc,EAAC,CAAC;EAC/C,MAAMC,WAAW,GAAG,IAAAC,4CAAqB,EAA0B,CAAC;EACpE,MAAMC,WAAW,GAAG,IAAAC,0BAAc,EAAC,CAAC;EAEpC,MAAMC,QAAQ,GAAG,IAAAC,uBAAW,EAA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AEF,OAAOzB,WAAW,CAACmC,wBAAwB,CACzCH,eAAe,EACdI,mBAAmB,IAClBhC,QAAQ,CAAC6B,WAAW,CAAC;MACnBrC,MAAM;MACNC,SAAS;MACTmB,OAAO,EAAE,EAAE;MACXoB;IACF,CAAC,CACL,CAAC;EACH,CAAC;AACH","ignoreList":[]}
@@ -28,7 +28,7 @@ class FetchPersonaChatService {
28
28
  headers() {
29
29
  return {
30
30
  'Content-Type': 'application/json',
31
- Authorization: this.apiKey
31
+ Authorization: `Api-Key ${this.apiKey}`
32
32
  };
33
33
  }
34
34
  async post(path, body) {
@@ -97,12 +97,22 @@ class FetchPersonaChatService {
97
97
  chatId,
98
98
  namespace,
99
99
  message,
100
- functionCallResults
100
+ functionCallResults,
101
+ chatConfig,
102
+ attachments
101
103
  } = params;
102
104
  return this.post(`/_api/persona-chat-service/v1/chats/${chatId}/messages`, {
103
105
  namespace,
104
106
  message,
105
- functionCallResults
107
+ ...(functionCallResults ? {
108
+ functionCallResults
109
+ } : {}),
110
+ ...(chatConfig ? {
111
+ chatConfig
112
+ } : {}),
113
+ ...(attachments ? {
114
+ attachments
115
+ } : {})
106
116
  });
107
117
  }
108
118
  async queryChatMessages(params) {