UNPKG

@airsurfer09/web-handsfree

Version:

A React package for integrating Convai's AI-powered voice assistants with LiveKit for real-time audio/video conversations

61 lines 2.2 kB
import { useCallback } from "react"; import { logger } from "../utils/logger"; /** * Hook for sending user text messages to Convai. * * Sends programmatic text messages that are processed by the AI assistant * but not displayed in the chat interface. * * @param {Room} room - LiveKit room instance * @param {string} participantSid - Participant session ID * @returns {Object} Object containing sendUserTextMessage function * * @example * ```tsx * function ChatComponent() { * const { sendUserTextMessage } = useUserTextMessageSender(room, participantSid); * * const handleSendMessage = () => { * sendUserTextMessage("Hello, how are you?"); * }; * * return <button onClick={handleSendMessage}>Send Message</button>; * } * ``` */ export const useUserTextMessageSender = (room, participantSid) => { const sendUserTextMessage = useCallback((text) => { // Check if room is connected and localParticipant exists if (!room || room.state === 'disconnected' || !room.localParticipant) { logger.warn("Cannot send message: room is disconnected or localParticipant is unavailable"); return; } if (!text || !text.trim()) { logger.warn("Cannot send message: text is empty"); return; } try { const message = { type: "user_text_message", data: { text: text.trim(), participant_sid: participantSid || room.localParticipant.sid, }, }; const encodedData = new TextEncoder().encode(JSON.stringify(message)); room.localParticipant.publishData(encodedData, { reliable: true, }); logger.log("💬 User text message sent:", text.trim(), "SID:", participantSid || room.localParticipant.sid); } catch (error) { logger.error("Failed to send user text message:", error); // Re-throw to allow caller to handle throw error; } }, [room, participantSid]); return { sendUserTextMessage, }; }; //# sourceMappingURL=useUserTextMessageSender.js.map