# Trillet AI Web Calls SDK

The Trillet AI Web Calls SDK enables seamless integration of AI-powered voice and text interactions into web applications. This SDK provides a simple interface to connect with Trillet AI agents for real-time communication.

## Features

- 🎙️ Real-time voice interactions with AI agents
- 💬 Text-based conversations
- 🔒 Secure authentication options
- 📊 Audio visualization capabilities
- 🎯 Event-driven architecture
- 🔄 Real-time transcription
- 🌐 Browser compatibility checks

## Installation

```bash
npm install @trillet-ai/web-sdk
# or
yarn add @trillet-ai/web-sdk
```

## Secure call flow (recommended)

Keep your API key on your server. Mint the room on your backend with `createCall`
(from the Node-safe `@trillet-ai/web-sdk/server` entry point), return the
`connection` object to the browser, and join with `joinRoom` — the API key, workspace
ID, and agent ID never reach the browser.

```typescript
// --- Backend (Node, API key stays here) ---
import { createCall } from '@trillet-ai/web-sdk/server';

const roomDetails = await createCall({
  apiKey: process.env.TRILLET_API_KEY!, // server-side only — never ship to the browser
  agentId: 'your-agent-id',
  mode: 'voice',
});
// Send roomDetails (or just roomDetails.connection) to your frontend over HTTPS.
```

```typescript
// --- Frontend (browser, no secret) ---
import { TrilletAgent } from '@trillet-ai/web-sdk';

const agent = new TrilletAgent({ agentId: 'your-agent-id', mode: 'voice' });

// roomDetails fetched from your backend above
await agent.joinRoom(roomDetails); // connects to LiveKit; makes no Trillet API call
```

> `startCall()` and `createCall()` are **server-side only** — they require an API key
> and must not be called from the browser.

## Quick Start

```typescript
import { TrilletAgent } from '@trillet-ai/web-sdk';

// Initialize the agent with API Key (Standard Method)
const agent = new TrilletAgent({
  apiKey: 'your-api-key',
  workspaceId: 'your-workspace-id',
  agentId: 'your-agent-id',
  mode: 'voice', // 'voice' or 'text'

  // Optional parameters
  variables: {
    customVar1: 'value1',
    customVar2: 'value2'
  },
  callbackUrl: 'https://your-callback-url.com'
});

// Start a regular call
await agent.startCall();

// Or start a public call
await agent.startPublicCall();

// Listen for events
agent.on('connected', (details) => {
  console.log('Connected to call:', details);
});

agent.on('transcriptionReceived', (transcript) => {
  console.log('New voice transcript:', transcript);
});

agent.on('message', (message) => {
    console.log('New text message from agent:', message);
});

agent.on('transcriptUpdate', (transcripts) => {
    console.log('Transcript updated:', transcripts);
});

// Control the call
agent.toggleMicrophone(true); // Enable/disable microphone in voice mode
await agent.setMuted(true); // Mute (awaitable); agent.isMuted reflects state
agent.on('muteChanged', (muted) => console.log('muted?', muted));
agent.sendTextMessage("Hello, agent!"); // Send a text message in text mode
agent.endCall(); // End the call
```

## Authentication

The SDK supports two authentication methods:

1. **API Key Authentication (Standard Method)**
   ```typescript
   const agent = new TrilletAgent({
     apiKey: 'your-api-key',
     workspaceId: 'your-workspace-id',
     agentId: 'your-agent-id'
   });
   ```

2. **Public Access**
   - For public-facing applications
   - Uses `startPublicCall()` method
   ```typescript
   const agent = new TrilletAgent({
     workspaceId: 'your-workspace-id',
     agentId: 'your-agent-id',
     mode: 'text' // Or 'voice'
   });
   await agent.startPublicCall();
   ```

## Call Types

The SDK supports two types of calls:

1. **Regular Call** - Using `startCall()`
   - Requires full authentication with API Key
   - Suitable for authenticated users

2. **Public Call** - Using `startPublicCall()`
   - Requires workspace ID and agent ID
   - Suitable for public-facing applications
   - No API key needed in client-side code
   - Example:
     ```typescript
     const agent = new TrilletAgent({
       workspaceId: 'your-workspace-id',
       agentId: 'your-agent-id',  // Required for both public and regular calls
       mode: 'text' // Or 'voice'
     });
     await agent.startPublicCall();
     ```

## Gating a call (`onBeforeCall`)

`onBeforeCall` is awaited once at the top of every call-start path — `startCall()`,
`startPublicCall()` and `joinRoom()` — before any network request or LiveKit connection.
Throw to abort: nothing is minted and nothing connects.

```typescript
const agent = new TrilletAgent({
  agentId: 'your-agent-id',
  workspaceId: 'your-workspace-id',
  onBeforeCall: async () => {
    // Your own challenge, login check, or quota lookup.
    const ok = await fetch('/api/verify-human').then((r) => r.json());
    if (!ok.allowed) throw new Error('Verification failed');
  },
});

await agent.startPublicCall(); // never runs if the hook throws
```

This is the right place for a challenge you actually verify on your own backend. (The
`<trillet-widget>` `recaptcha-site-key` attribute is deliberately *not* that — its token
is never verified, so it is friction only.)

Note that a public call is additionally subject to server-side controls you configure in
the portal: your workspace's domain allowlist (Settings → Domains) and rate limits on the
public endpoint.

## Events

The SDK emits various events that you can listen to:

```typescript
agent.on('connected', (details) => {
  // Called when successfully connected to a call
});

agent.on('disconnected', () => {
  // Called when the call ends
});

agent.on('error', (error) => {
  // Called when an error occurs
});

agent.on('transcriptionReceived', (segments, participant) => {
  // Called when new transcription is available (voice mode)
});

agent.on('assistantStartedSpeaking', () => {
  // Called when the AI agent starts speaking (voice mode)
});

agent.on('assistantStoppedSpeaking', () => {
  // Called when the AI agent stops speaking (voice mode)
});

agent.on('audioData', (data) => {
  // Raw audio data for visualization (voice mode)
});

agent.on('message', (message) => {
    // Called when a text message is received from the agent (text mode)
});

agent.on('transcriptUpdate', (transcripts) => {
    // Called whenever the transcript is updated (voice or text)
});
```

## Voice Mode Features

When using voice mode, the SDK provides additional audio-related features:

```typescript
// Check if the assistant is currently speaking
const isSpeaking = agent.isAssistantSpeaking;

// Access transcripts
const allTranscripts = agent.getTranscripts();
const currentTranscript = agent.getCurrentTranscript();
```

## Text Mode Features

When using text mode, you can send and receive text messages.

```typescript
// Send a message to the agent
agent.sendTextMessage("Hello, how are you?");

// Listen for messages from the agent
agent.on('message', (message) => {
  console.log('Agent says:', message.text);
});

// The full conversation transcript is available for both modes
const fullConversation = agent.getTranscripts();
console.log(fullConversation);
```

## Requirements

- Modern web browser with secure context (HTTPS or localhost)
- WebRTC support
- Microphone access (for voice mode)

## Browser Support

The SDK is compatible with modern browsers that support WebRTC:
- Chrome (recommended)
- Firefox
- Safari
- Edge

## Error Handling

The SDK includes comprehensive error handling:

```typescript
agent.on('error', (error) => {
  console.error('Trillet SDK Error:', error);
});
```

## License

[License details here]

## Support

For support, please contact [support contact information].
