@sable-ai/voice-agent
v0.1.0
Published
Voice agent implementation for Sable AI
Readme
@sable-ai/voice-agent
A TypeScript library for integrating with Sable's voice agent, providing a simple interface for voice agent interactions.
Installation
npm install @sable-ai/voice-agent
# or
yarn add @sable-ai/voice-agentPrerequisites
- Node.js 18.0.0 or higher
@sable-ai/corepeer dependency
Quick Start
import { VoiceAgent, ToolRegistry } from '@sable-ai/voice-agent';
// Initialize the tool registry
const toolRegistry = new ToolRegistry();
// Create a voice agent instance
const agent = new VoiceAgent({
debug: process.env.NODE_ENV !== 'production',
autoConnect: false,
onStatusChange: (status) => console.log('Status:', status),
onTranscript: (transcript) => console.log('Transcript:', transcript),
onError: (error) => console.error('Error:', error)
}, toolRegistry);
// Start a call
async function startCall() {
try {
await agent.startCall({
systemPrompt: 'You are a helpful assistant',
model: 'gpt-4',
voice: 'en-US-Studio-O'
});
// Send a text message
agent.sendText({
text: 'Hello, how can I help you today?',
deferResponse: false
});
// End the call after 30 seconds
setTimeout(() => agent.endCall(), 30000);
} catch (error) {
console.error('Failed to start call:', error);
}
}
startCall();API Reference
VoiceAgent
Constructor
new VoiceAgent(options: VoiceAgentOptions, toolRegistry: ToolRegistry)Methods
startCall(callConfig: CallConfig): Promise<void>- Initiates a new voice callendCall(): Promise<void>- Ends the current callsendText(options: SendTextOptions): void- Sends a text messagetoggleMute(role: Role): void- Toggles mute for mic or speaker
ToolRegistry
Methods
registerTool(name: string, implementation: Function): void- Registers a new toolgetTools(): Map<string, Function>- Returns all registered tools
Tool Integration
You can extend the voice agent's capabilities by registering custom tools:
// Define a tool
function getWeather(location: string): Promise<string> {
// Implementation to fetch weather
return Promise.resolve(`The weather in ${location} is sunny.`);
}
// Register the tool
toolRegistry.registerTool('get_weather', getWeather);
// The agent can now use this tool when appropriateError Handling
The VoiceAgent emits errors through the onError callback. Always implement this to handle potential issues:
const agent = new VoiceAgent({
onError: (error) => {
console.error('Voice Agent Error:', error);
// Handle error appropriately
}
}, toolRegistry);TypeScript Support
This package includes TypeScript type definitions. All types are exported for your convenience:
import type {
VoiceAgentOptions,
CallConfig,
JoinUrlResponse,
SendTextOptions
} from '@sable-ai/voice-agent';