Svelte SDK

Svelte SDK

Add real-time voice interactions to your Svelte app with a single component.

Installation

npm install @itannix/svelte

Quick Start

Import the VoiceAssistant component and bind to its methods:

App.svelte
<script>
  import { VoiceAssistant } from '@itannix/svelte';

  let assistant;
  let status = 'disconnected';
  let messages = [];

  function handleConnect() {
    assistant.connect();
  }

  function handleDisconnect() {
    assistant.disconnect();
  }
</script>

<VoiceAssistant
  clientId="your-client-id"
  clientSecret="your-client-secret"
  bind:this={assistant}
  on:statusChange={(e) => status = e.detail}
  on:transcript={(e) => {
    messages = [...messages, { role: 'user', text: e.detail }];
  }}
  on:assistantMessage={(e) => {
    if (e.detail.done) {
      messages = [...messages, { role: 'assistant', text: e.detail.text }];
    }
  }}
/>

<p>Status: {status}</p>

<button on:click={handleConnect} disabled={status !== 'disconnected'}>
  Connect
</button>
<button on:click={handleDisconnect} disabled={status === 'disconnected'}>
  Disconnect
</button>

{#each messages as msg}
  <p><strong>{msg.role}:</strong> {msg.text}</p>
{/each}

Props

PropTypeDefaultDescription
clientIdstringrequiredYour ItanniX client ID from the dashboard
clientSecretstringrequiredYour client secret (32+ character random string)
serverUrlstring'https://api.itannix.com'API server URL (for self-hosted deployments)

Events

EventDetail TypeDescription
statusChange'connecting' | 'connected' | 'disconnected'Fired when connection status changes
transcriptstringUser's speech transcription (completed)
assistantMessage{ text: string, done: boolean }Assistant response. done=true indicates final message.
functionCall{ name, args, callId }Function call from the assistant. Respond with sendFunctionResult().
errorErrorConnection or runtime errors

Methods

Access component methods via bind:this:

<VoiceAssistant bind:this={assistant} ... />

<script>
  let assistant;

  // Connect to voice service
  await assistant.connect();

  // Disconnect
  assistant.disconnect();

  // Send function result back to assistant
  assistant.sendFunctionResult(callId, { success: true, data: result });
</script>

connect(): Promise<void>

Establishes WebRTC connection to the voice service. Requests microphone permission and starts streaming audio.

disconnect(): void

Closes the connection and stops all audio streams. Safe to call even if not connected.

sendFunctionResult(callId: string, result: object): void

Sends a function call result back to the assistant. Use the callId from the functionCall event.

Handling Function Calls

When the assistant invokes a function, handle it and return the result:

Function Call Handler
<VoiceAssistant
  bind:this={assistant}
  on:functionCall={async (e) => {
    const { name, args, callId } = e.detail;

    if (name === 'get_weather') {
      const weather = await fetchWeather(args.location);
      assistant.sendFunctionResult(callId, { 
        temperature: weather.temp,
        conditions: weather.conditions
      });
    }
  }}
  ...
/>

TypeScript Support

The SDK is written in TypeScript and exports all types:

import { 
  VoiceAssistant,
  VoiceClient,
  type ConnectionStatus,
  type FunctionCallEvent,
  type AssistantMessageEvent
} from '@itannix/svelte';

Next Steps