Skip to content

TypeScript Support

Klleon SDK is provided as a UMD bundle, so you need to add a type definition file to use it in TypeScript projects.

Type Definition File

Create a KlleonSDK.d.ts file in your project root.

typescript
/** SDK connection status */
type SDKStatus =
  | "IDLE"
  | "CONNECTING"
  | "CONNECTING_FAILED"
  | "SOCKET_CONNECTED"
  | "SOCKET_FAILED"
  | "STREAMING_CONNECTED"
  | "STREAMING_FAILED"
  | "CONNECTED_FINISH";

type LogLevel = "debug" | "info" | "warn" | "error" | "silent";
type LanguageCode = "ko_kr" | "en_us" | "ja_jp" | "id_id";

/** init() options */
interface InitOption {
  sdk_key: string;
  avatar_id: string;
  log_level?: LogLevel;
  language?: LanguageCode;
  enable_microphone?: boolean;
  auto_send?: boolean;
  stt_only?: boolean;
}

/** onSignal callback parameter */
interface IncomingSignalData {
  signal: string;
  payload?: Record<string, unknown>;
  id: string;
  type: "SESSION" | "INPUT" | "OUTPUT";
}

/** onError callback parameter */
interface ErrorData {
  code: string;
  message?: string;
}

/** SDK interface */
interface KlleonSDK {
  // Lifecycle
  init: (option: InitOption) => Promise<void>;
  destroy: () => Promise<void>;
  // Events
  onSignal: (cb: (data: IncomingSignalData) => void) => void;
  onStatus: (cb: (status: SDKStatus) => void) => void;
  onError: (cb: (error: ErrorData) => void) => void;

  // Text
  sendMessage: (text: string) => void;
  speak: (text: string) => void;

  // Audio echo
  sendSpeakAudio: (audio: string) => void;
  endSpeakAudio: () => void;

  // Voice
  startListening: () => void;
  endListening: () => void;
  cancelListening: () => void;

  // Avatar control
  stopSpeaking: () => void;

  // Media
  setVolume: (volume: number) => void;

  // Status query
  getStatus: () => SDKStatus;
  clearMessages: () => void;
}

declare global {
  interface Window {
    KlleonSDK: KlleonSDK;
  }

  // Prevent type errors when using Web Components in React
  namespace JSX {
    interface IntrinsicElements {
      "avatar-container": React.DetailedHTMLProps<
        React.HTMLAttributes<HTMLElement> & {
          volume?: number;
          fit?: "cover" | "contain" | "fill";
        },
        HTMLElement
      >;
      "chat-container": React.DetailedHTMLProps<
        React.HTMLAttributes<HTMLElement> & {
          type?: "text" | "voice";
          delay?: number;
          isShowCount?: boolean;
          scrollbar?: boolean;
        },
        HTMLElement
      >;
    }
  }
}

export {};

tsconfig.json Configuration

Add the type definition file to include so TypeScript recognizes it.

json
{
  "compilerOptions": {
    "jsx": "react-jsx"
  },
  "include": ["src", "KlleonSDK.d.ts"]
}

Usage Example

After adding the type definitions, autocomplete and type checking will work for window.KlleonSDK.

tsx
const SDK = window.KlleonSDK;

SDK.onStatus((status) => console.log(status));
SDK.onSignal((data) => console.log(data.signal, data.payload));

SDK.init({
  sdk_key: 'YOUR_SDK_KEY',
  avatar_id: 'YOUR_AVATAR_ID',
}).catch((e) => console.error(e.message));