UNPKG

@inworld/web-core

Version:
79 lines (78 loc) 3.97 kB
import { AudioSessionStartPayloadMicrophoneMode, AudioSessionStartPayloadUnderstandingMode, ControlEventAction, ConversationEventPayloadConversationEventType, } from '../../../proto/ai/inworld/packets/packets.pb.js'; import { InworlControlAction, InworldConversationEventType, MicrophoneMode, UnderstandingMode, } from '../../common/data_structures/index.js'; import { Actor } from './routing.entity.js'; export class ControlEvent { constructor({ action, audioSessionStart, description, conversation, }) { this.action = action; if (audioSessionStart) { this.audioSessionStart = audioSessionStart; } if (description) { this.description = description; } if (conversation) { this.conversation = conversation; } } static fromProto(proto) { var _a, _b, _c; const conversation = (_a = proto.conversationUpdate) !== null && _a !== void 0 ? _a : proto.conversationEvent; return new ControlEvent(Object.assign(Object.assign(Object.assign({ action: this.getControlType(proto) }, (proto.audioSessionStart && { audioSessionStart: { mode: ControlEvent.getMicrophoneMode(proto.audioSessionStart), understandingMode: ControlEvent.getUnderstandingMode(proto.audioSessionStart), }, })), { description: proto.description }), (conversation && { conversation: Object.assign(Object.assign({}, (proto.conversationEvent && { type: this.getConversationType(proto), })), { participants: (_c = (_b = conversation.participants) === null || _b === void 0 ? void 0 : _b.map((participant) => Actor.fromProto(participant))) !== null && _c !== void 0 ? _c : [] }), }))); } static getControlType(proto) { switch (proto.action) { case ControlEventAction.AUDIO_SESSION_START: return InworlControlAction.AUDIO_SESSION_START; case ControlEventAction.AUDIO_SESSION_END: return InworlControlAction.AUDIO_SESSION_END; case ControlEventAction.INTERACTION_END: return InworlControlAction.INTERACTION_END; case ControlEventAction.TTS_PLAYBACK_MUTE: return InworlControlAction.TTS_PLAYBACK_MUTE; case ControlEventAction.TTS_PLAYBACK_UNMUTE: return InworlControlAction.TTS_PLAYBACK_UNMUTE; case ControlEventAction.WARNING: return InworlControlAction.WARNING; case ControlEventAction.CONVERSATION_UPDATE: return InworlControlAction.CONVERSATION_UPDATE; case ControlEventAction.CONVERSATION_EVENT: return InworlControlAction.CONVERSATION_EVENT; default: return InworlControlAction.UNKNOWN; } } static getConversationType(proto) { switch (proto.conversationEvent.eventType) { case ConversationEventPayloadConversationEventType.STARTED: return InworldConversationEventType.STARTED; case ConversationEventPayloadConversationEventType.UPDATED: return InworldConversationEventType.UPDATED; case ConversationEventPayloadConversationEventType.EVICTED: return InworldConversationEventType.EVICTED; default: return InworldConversationEventType.UNKNOWN; } } static getMicrophoneMode(proto) { if (proto.mode === AudioSessionStartPayloadMicrophoneMode.EXPECT_AUDIO_END) { return MicrophoneMode.EXPECT_AUDIO_END; } return MicrophoneMode.OPEN_MIC; } static getUnderstandingMode(proto) { if (proto.understandingMode === AudioSessionStartPayloadUnderstandingMode.SPEECH_RECOGNITION_ONLY) { return UnderstandingMode.SPEECH_RECOGNITION_ONLY; } return UnderstandingMode.FULL; } }