session

package
v0.0.0-...-f578b72 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultSystemPrompt = `` /* 6537-byte string literal not displayed */

DefaultSystemPrompt is the default system instructions for the AI assistant.

Variables

View Source
var ErrBufferFull = errors.New("audio buffer full")

ErrBufferFull is returned when the buffer exceeds its maximum size

Functions

func PcmToMuLawByte

func PcmToMuLawByte(pcm int16) byte

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

func NewManager(cfg *config.Config) (*Manager, error)

NewManager creates a session manager with Redis connection

func (*Manager) CleanupInactiveSessions

func (sm *Manager) CleanupInactiveSessions(ctx context.Context)

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

func (sm *Manager) GetActiveSessionCount() int

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

func (sm *Manager) RemoveSession(ctx context.Context, sessionID string) error

RemoveSession cleans up and removes a session

func (*Manager) Shutdown

func (sm *Manager) Shutdown()

Shutdown closes all sessions

func (*Manager) StartCleanupRoutine

func (sm *Manager) StartCleanupRoutine(ctx context.Context)

StartCleanupRoutine starts periodic cleanup of inactive sessions

type Provider

type Provider string

Provider identifies which AI backend a session uses.

const (
	// ProviderGemini indicates the session uses Google's Gemini AI.
	ProviderGemini Provider = "gemini"
	// ProviderOpenAI indicates the session uses OpenAI's Realtime API.
	ProviderOpenAI Provider = "openai"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL