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.47 kB
import { useState, useEffect } from "react"; import { logger } from "../utils/logger"; /** * Hook to fetch character information from Convai API * * @param characterId - The character ID to fetch information for * @param apiKey - The Convai API key * @returns Character information including name and image URL */ export const useCharacterInfo = (characterId, apiKey) => { const [characterInfo, setCharacterInfo] = useState({ name: "Convi", image: null, }); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const fetchCharacterInfo = async () => { if (!characterId || !apiKey) { setIsLoading(false); return; } try { setIsLoading(true); setError(null); const response = await fetch("https://api.convai.com/character/get", { method: "POST", headers: { "Content-Type": "application/json", "CONVAI-API-KEY": apiKey, }, body: JSON.stringify({ charID: characterId }), }); if (!response.ok) { throw new Error(`Failed to fetch character info: ${response.status}`); } const data = await response.json(); // Extract character name and image with fallbacks const name = data.character_name || "Convi"; const image = data.model_details?.METAHUMAN?.avatar_image_square || data.model_details?.METAHUMAN?.avatar_image || data.model_details?.modelPlaceholder || null; setCharacterInfo({ name, image }); logger.log("Character info loaded:", { name, hasImage: !!image }); } catch (err) { const error = err instanceof Error ? err : new Error("Unknown error"); logger.error("Failed to fetch character info:", error); setError(error); // Keep default values on error } finally { setIsLoading(false); } }; fetchCharacterInfo(); }, [characterId, apiKey]); return { ...characterInfo, isLoading, error }; }; //# sourceMappingURL=useCharacterInfo.js.map