adam-sdk
Version:
A JavaScript SDK for integrating A.D.A.M. 3D avatars into web applications.
101 lines (100 loc) • 3.74 kB
TypeScript
import { AvatarSDKOptions, SpeakOptions, AvatarEvent, EventCallback, SpeakArg } from './types';
export * from './types';
/**
* The main class for interacting with the A.D.A.M. 3D avatar.
* This class provides methods for connecting to the avatar, making it speak, and controlling its animations and expressions.
* @example
* ```javascript
* import { AvatarSDK } from 'adam-sdk';
*
* const iframe = document.getElementById('avatar-iframe');
* const sdk = new AvatarSDK(iframe);
*
* sdk.on('ready', () => {
* console.log('SDK is ready!');
* sdk.speak('Hello, world!');
* });
*
* sdk.connect();
* ```
*/
export declare class AvatarSDK {
private iframe;
private targetOrigin;
private eventListeners;
private pendingCommands;
private isReady;
private handshakeInterval;
private fastPollInterval;
private logLevel;
/**
* Creates an instance of the AvatarSDK.
* @param iframeElement The HTMLIFrameElement that contains the avatar.
* @param options Configuration options for the SDK.
*/
constructor(iframeElement: HTMLIFrameElement, options?: AvatarSDKOptions);
/**
* Establishes a connection with the avatar iframe.
* This method must be called before any other commands can be sent to the avatar.
* It returns a promise that resolves when the connection is established.
* @returns A promise that resolves when the connection is established.
* @throws An error if the connection times out or the iframe fails to load.
*/
connect(): Promise<void>;
/**
* Makes the avatar speak.
* Supports either:
* - speak(text: string, options?: SpeakOptions)
* - speak(payload: { text: string; ...custom })
* Any additional custom key/value pairs provided will be forwarded to the iframe payload.
* @returns A promise that resolves when the command is acknowledged by the iframe.
*/
speak(text: string, options?: SpeakOptions): Promise<any>;
speak(payload: SpeakArg): Promise<any>;
/**
* Triggers a specific animation by name.
* @param name The name of the animation to play.
* @param loop Whether the animation should loop.
* @returns A promise that resolves with the command result.
*/
playAnimation(name: string, loop?: boolean): Promise<any>;
/**
* Sets the avatar's facial expression.
* @param name The name of the expression to set.
* @returns A promise that resolves with the command result.
*/
setExpression(name: string): Promise<any>;
/**
* Interrupts the current speech or action.
* @returns A promise that resolves when the interrupt is complete.
*/
interrupt(): Promise<any>;
/**
* Registers an event listener for a specific event.
* @param eventName The name of the event to listen for.
* @param callback The callback function to execute when the event is triggered.
* @example
* ```javascript
* sdk.on('speech:start', (payload) => {
* console.log('Avatar started speaking:', payload.text);
* });
* ```
*/
on(eventName: AvatarEvent, callback: EventCallback): void;
/**
* Removes an event listener for a specific event.
* @param eventName The name of the event to remove the listener from.
* @param callback The callback function to remove.
*/
off(eventName: AvatarEvent, callback: EventCallback): void;
/**
* Cleans up resources and removes event listeners.
* This should be called when the SDK is no longer needed to prevent memory leaks.
*/
destroy(): void;
private _sendCommand;
private _handleMessage;
private _postMessage;
private _log;
private _emitEvent;
}