`import { useState, useEffect, useRef } from “react”;
import { ConvaiClient } from “convai-web-sdk”;
export default function App() {
const [apiKey] = useState(“”);
const [characterId] = useState(“”);
// Direct ConvaiClient usage
const clientRef = useRef<ConvaiClient | null>(null);
const [isTyping, setIsTyping] = useState(false);
// Initialize client
useEffect(() => {
if (!characterId || !apiKey) return;
// Create client
clientRef.current = new ConvaiClient({
apiKey,
characterId,
enableAudio: true,
languageCode: "en-US",
});
// Set up callbacks
const client = clientRef.current;
// Error callback
client.setErrorCallback?.((type: string, statusMessage: string) => {
console.error("Convai error:", type, statusMessage);
});
// Response callback
client.setResponseCallback?.((response: any) => {
console.log("Response received:", response);
});
return () => {
clientRef.current = null;
};
}, [characterId, apiKey]);
// Test sendTextStream functionality
const testSendTextStream = () => {
if (!clientRef.current) {
console.log(“Client not initialized”);
return;
}
clientRef.current.sendTextStream?.(“Hello, this is a test message!”);
};
const testInvokeTrigger = () => {
if (!clientRef.current) {
console.log(“Client not initialized”);
return;
}
console.log("Invoking trigger...");
try {
clientRef.current.invokeTrigger?.("score goal");
} catch (error) {
console.error("Error invoking trigger:", error);
}
};
return (
Convai Client - Simple Test
<div style={{ marginTop: '20px' }}>
<button
onClick={testSendTextStream}
style={{
padding: '10px 20px',
backgroundColor: '4CAF50_1',
color: 'white',
border: 'none',
borderRadius: '5px',
marginRight: '10px'
}}
>
Test sendTextStream
</button>
<button
onClick={testInvokeTrigger}
style={{
padding: '10px 20px',
backgroundColor: '2196F3_1',
color: 'white',
border: 'none',
borderRadius: '5px'
}}
>
Test invokeTrigger
</button>
</div>
<div style={{ marginTop: '20px', fontSize: '14px', color: '#666' }}>
<div>Typing: {isTyping ? 'Yes' : 'No'}</div>
<div>Check the browser console for logs and responses.</div>
</div>
</div>
);
}`