Documentation
¶
Index ¶
- Constants
- Variables
- func PcmToMuLawByte(pcm int16) byte
- type AudioBuffer
- type ClientSession
- func NewClientSession(ctx context.Context, id string, clientConn *websocket.Conn, geminiKey string, ...) (*ClientSession, error)
- func NewOpenAIClientSession(ctx context.Context, id string, clientConn *websocket.Conn, openaiKey string, ...) (*ClientSession, error)
- func NewOpenAITwilioClientSession(ctx context.Context, id string, clientConn *websocket.Conn, openaiKey string, ...) (*ClientSession, error)
- func NewTwilioClientSession(ctx context.Context, id string, clientConn *websocket.Conn, geminiKey string, ...) (*ClientSession, error)
- func (cs *ClientSession) Close() error
- func (cs *ClientSession) IsClosed() bool
- func (cs *ClientSession) MuLawByteToPCMBytes(b byte) []byte
- func (cs *ClientSession) SendToClient(msg *messages.ServerMessage) error
- func (cs *ClientSession) Start()
- func (cs *ClientSession) StartOpenAI()
- func (cs *ClientSession) StartOpenAITwilio()
- func (cs *ClientSession) StartTwilio()
- type Manager
- func (sm *Manager) CleanupInactiveSessions(ctx context.Context)
- func (sm *Manager) CreateSession(ctx context.Context, clientConn *websocket.Conn) (*ClientSession, error)
- func (sm *Manager) CreateTwilioSession(ctx context.Context, clientConn *websocket.Conn) (*ClientSession, error)
- func (sm *Manager) GetActiveSessionCount() int
- func (sm *Manager) GetSession(sessionID string) (*ClientSession, bool)
- func (sm *Manager) RemoveSession(ctx context.Context, sessionID string) error
- func (sm *Manager) Shutdown()
- func (sm *Manager) StartCleanupRoutine(ctx context.Context)
- type Provider
Constants ¶
const DefaultSystemPrompt = `` /* 6537-byte string literal not displayed */
DefaultSystemPrompt is the default system instructions for the AI assistant.
Variables ¶
var ErrBufferFull = errors.New("audio buffer full")
ErrBufferFull is returned when the buffer exceeds its maximum size
Functions ¶
func PcmToMuLawByte ¶
PcmToMuLawByte converts a 16-bit PCM sample to a mu-law encoded byte.
Types ¶
type AudioBuffer ¶
type AudioBuffer struct {
// contains filtered or unexported fields
}
AudioBuffer accumulates audio chunks until flushed
func NewAudioBuffer ¶
func NewAudioBuffer(maxSize int) *AudioBuffer
NewAudioBuffer creates a buffer with the specified maximum size in bytes
func (*AudioBuffer) Append ¶
func (ab *AudioBuffer) Append(chunk []byte) error
Append adds an audio chunk to the buffer Returns ErrBufferFull if adding the chunk would exceed maxSize
func (*AudioBuffer) ChunkCount ¶
func (ab *AudioBuffer) ChunkCount() int
ChunkCount returns the number of chunks in the buffer
func (*AudioBuffer) Clear ¶
func (ab *AudioBuffer) Clear()
Clear empties the buffer without returning data
func (*AudioBuffer) Flush ¶
func (ab *AudioBuffer) Flush() []byte
Flush concatenates all chunks in order and clears the buffer Returns the complete audio data
func (*AudioBuffer) IsEmpty ¶
func (ab *AudioBuffer) IsEmpty() bool
IsEmpty returns true if no chunks are buffered
func (*AudioBuffer) MaxSize ¶
func (ab *AudioBuffer) MaxSize() int
MaxSize returns the maximum buffer size
func (*AudioBuffer) Size ¶
func (ab *AudioBuffer) Size() int
Size returns the current total buffered bytes
type ClientSession ¶
type ClientSession struct {
ID string
Provider Provider // Which AI backend: "gemini" or "openai"
IsTwilio bool // Whether this is a Twilio voice call session
StreamSid string // Twilio stream SID (set on "start" event)
ClientConn *websocket.Conn
GeminiProxy *gemini.Proxy
OpenAIProxy *openai.Proxy
AudioBuffer *AudioBuffer // Buffer for incoming audio chunks
CreatedAt time.Time
LastActivity time.Time
CloseChan chan struct{}
// contains filtered or unexported fields
}
ClientSession represents a single user's connection
func NewClientSession ¶
func NewClientSession(ctx context.Context, id string, clientConn *websocket.Conn, geminiKey string, model string, voice string, systemPrompt string, maxBufferSize int, tools []*genai.Tool) (*ClientSession, error)
NewClientSession creates a session with Gemini connection
func NewOpenAIClientSession ¶
func NewOpenAIClientSession(ctx context.Context, id string, clientConn *websocket.Conn, openaiKey string, model string, voice string, systemPrompt string, maxBufferSize int) (*ClientSession, error)
NewOpenAIClientSession creates a session with OpenAI Realtime connection (PCM 24kHz audio)
func NewOpenAITwilioClientSession ¶
func NewOpenAITwilioClientSession(ctx context.Context, id string, clientConn *websocket.Conn, openaiKey string, model string, voice string, systemPrompt string, maxBufferSize int) (*ClientSession, error)
NewOpenAITwilioClientSession creates an OpenAI session for Twilio voice calls (mu-law audio)
func NewTwilioClientSession ¶
func NewTwilioClientSession(ctx context.Context, id string, clientConn *websocket.Conn, geminiKey string, model string, voice string, systemPrompt string, maxBufferSize int, tools []*genai.Tool) (*ClientSession, error)
NewTwilioClientSession creates a Gemini session for Twilio voice calls
func (*ClientSession) Close ¶
func (cs *ClientSession) Close() error
Close terminates the session and cleans up resources
func (*ClientSession) IsClosed ¶
func (cs *ClientSession) IsClosed() bool
IsClosed returns whether the session is closed
func (*ClientSession) MuLawByteToPCMBytes ¶
func (cs *ClientSession) MuLawByteToPCMBytes(b byte) []byte
MuLawByteToPCMBytes converts a single mu-law byte to its 16-bit PCM equivalent in little-endian bytes.
func (*ClientSession) SendToClient ¶
func (cs *ClientSession) SendToClient(msg *messages.ServerMessage) error
SendToClient sends a message to the frontend client (legacy, use queueMessage)
func (*ClientSession) Start ¶
func (cs *ClientSession) Start()
Start begins the bidirectional message handling for standard WebSocket clients
func (*ClientSession) StartOpenAI ¶
func (cs *ClientSession) StartOpenAI()
StartOpenAI begins the bidirectional message handling for OpenAI WebSocket clients (PCM audio)
func (*ClientSession) StartOpenAITwilio ¶
func (cs *ClientSession) StartOpenAITwilio()
StartOpenAITwilio begins the bidirectional message handling for OpenAI + Twilio (mu-law audio)
func (*ClientSession) StartTwilio ¶
func (cs *ClientSession) StartTwilio()
StartTwilio begins the bidirectional message handling for Twilio voice calls
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager manages all client sessions
func NewManager ¶
NewManager creates a session manager with Redis connection
func (*Manager) CleanupInactiveSessions ¶
CleanupInactiveSessions removes sessions that have been inactive
func (*Manager) CreateSession ¶
func (sm *Manager) CreateSession(ctx context.Context, clientConn *websocket.Conn) (*ClientSession, error)
CreateSession creates a new client session using the configured provider
func (*Manager) CreateTwilioSession ¶
func (sm *Manager) CreateTwilioSession(ctx context.Context, clientConn *websocket.Conn) (*ClientSession, error)
CreateTwilioSession creates a new Twilio voice call session using the configured provider
func (*Manager) GetActiveSessionCount ¶
GetActiveSessionCount returns current session count
func (*Manager) GetSession ¶
func (sm *Manager) GetSession(sessionID string) (*ClientSession, bool)
GetSession retrieves a session by ID
func (*Manager) RemoveSession ¶
RemoveSession cleans up and removes a session
func (*Manager) StartCleanupRoutine ¶
StartCleanupRoutine starts periodic cleanup of inactive sessions