Skip to content

Commit

Permalink
fixes failed tests and code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jpvajda committed Jan 31, 2025
1 parent ede21b1 commit 9d0bacf
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 18 deletions.
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,28 +196,32 @@ There are examples for **every*- API call in this SDK. You can find all of these

These examples provide:

Speech-to-Text - Live Audio / WebSocket:
### Agent

- Agent Simple - [examples/agent/simple](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/agent/simple/main.go)

### Speech-to-Text - Live Audio / WebSocket:

- From a Microphone - [examples/speech-to-text/websocket/microphone](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/speech-to-text/websocket/microphone/main.go)
- From an HTTP Endpoint - [examples/speech-to-text/websocket/http](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/speech-to-text/websocket/http/main.go)

Speech-to-Text - PreRecorded / REST:
### Speech-to-Text - PreRecorded / REST:

- From an Audio File - [examples/speech-to-text/rest/file](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/speech-to-text/rest/file/main.go)
- From an URL - [examples/speech-to-text/rest/url](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/speech-to-text/rest/url/main.go)
- From an Audio Stream - [examples/speech-to-text/rest/stream](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/speech-to-text/rest/stream/main.go)

Speech-to-Text - Live Audio:
### Speech-to-Text - Live Audio:

- From a Microphone - [examples/speech-to-text/websocket/microphone](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/speech-to-text/websocket/microphone/main.go)
- From an HTTP Endpoint - [examples/speech-to-text/websocket/http](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/speech-to-text/websocket/http/main.go)

Text-to-Speech - WebSocket
### Text-to-Speech - WebSocket

- Websocket Simple Example - [examples/text-to-speech/websocket/simple](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/text-to-speech/websocket/simple/main.go)
- Interactive Websocket - [examples/text-to-speech/websocket/interactive](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/text-to-speech/websocket/interactive/main.go)

Text-to-Speech - REST
### Text-to-Speech - REST

- Save audio to a Path - [examples/text-to-speech/rest/file](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/text-to-speech/rest/file/main.go)
- Save audio to a Stream/Buffer - [examples/text-to-speech/rest/stream](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/text-to-speech/rest/stream/main.go)
Expand Down Expand Up @@ -256,7 +260,21 @@ client.Init(client.InitLib{

## Testing

TBD
There are several test folders in [/tests](https://github.com/deepgram/deepgram-go-sdk/tree/main/tests) you can run:

- unit_test/ - Unit tests
- daily_test/ - Integration/daily tests
- edge_cases/ - Edge case testing
- response_data/ - Test data
- utils/ - Test utilities

To run the tests, you can use the following commands:

Run specific tests in a directory:

```bash
go run filename
```

## Backwards Compatibility

Expand All @@ -273,6 +291,6 @@ To make sure our community is safe for all, be sure to review and agree to our [
We love to hear from you so if you have questions, comments or find a bug in the
project, let us know! You can either:

- [Open an issue in this repository](https://github.com/deepgram/deepgram-dotnet-sdk/issues/new)
- [Open an issue in this repository](https://github.com/deepgram/deepgram-go-sdk/issues/new)
- [Join the Deepgram Github Discussions Community](https://github.com/orgs/deepgram/discussions)
- [Join the Deepgram Discord Community](https://discord.gg/xWRaCDBtW4)
38 changes: 37 additions & 1 deletion pkg/api/agent/v1/websocket/chan_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func NewDefaultChanHandler() *DefaultChanHandler {
functionCallingResponse: make(chan *interfaces.FunctionCallingResponse),
agentStartedSpeakingResponse: make(chan *interfaces.AgentStartedSpeakingResponse),
agentAudioDoneResponse: make(chan *interfaces.AgentAudioDoneResponse),
endOfThoughtResponse: make(chan *interfaces.EndOfThoughtResponse),
injectionRefusedResponse: make(chan *interfaces.InjectionRefusedResponse),
keepAliveResponse: make(chan *interfaces.KeepAlive),
closeChan: make(chan *interfaces.CloseResponse),
errorChan: make(chan *interfaces.ErrorResponse),
unhandledChan: make(chan *[]byte),
Expand Down Expand Up @@ -118,6 +119,16 @@ func (dch DefaultChanHandler) GetError() []*chan *interfaces.ErrorResponse {
return []*chan *interfaces.ErrorResponse{&dch.errorChan}
}

// GetInjectionRefused returns the injection refused channels
func (dch DefaultChanHandler) GetInjectionRefused() []*chan *interfaces.InjectionRefusedResponse {
return []*chan *interfaces.InjectionRefusedResponse{&dch.injectionRefusedResponse}
}

// GetKeepAlive returns the keep alive channels
func (dch DefaultChanHandler) GetKeepAlive() []*chan *interfaces.KeepAlive {
return []*chan *interfaces.KeepAlive{&dch.keepAliveResponse}
}

// GetUnhandled returns the unhandled event channels
func (dch DefaultChanHandler) GetUnhandled() []*chan *[]byte {
return []*chan *[]byte{&dch.unhandledChan}
Expand Down Expand Up @@ -385,6 +396,31 @@ func (dch DefaultChanHandler) Run() error {
}
}()

// keep alive response channel
wgReceivers.Add(1)
go func() {
defer wgReceivers.Done()

for ka := range dch.keepAliveResponse {
if dch.debugWebsocket {
data, err := json.Marshal(ka)
if err != nil {
klog.V(1).Infof("KeepAlive json.Marshal failed. Err: %v\n", err)
continue
}

prettyJSON, err := prettyjson.Format(data)
if err != nil {
klog.V(1).Infof("prettyjson.Marshal failed. Err: %v\n", err)
continue
}
klog.V(2).Infof("\n\nKeepAlive Object:\n%s\n\n", prettyJSON)
}

fmt.Printf("\n\n[KeepAliveResponse]\n\n")
}
}()

// close channel
wgReceivers.Add(1)
go func() {
Expand Down
35 changes: 35 additions & 0 deletions pkg/api/agent/v1/websocket/chan_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func NewChanRouter(chans interfaces.AgentMessageChan) *ChanRouter {
functionCallingResponse: make([]*chan *interfaces.FunctionCallingResponse, 0),
agentStartedSpeakingResponse: make([]*chan *interfaces.AgentStartedSpeakingResponse, 0),
agentAudioDoneResponse: make([]*chan *interfaces.AgentAudioDoneResponse, 0),
injectionRefusedResponse: make([]*chan *interfaces.InjectionRefusedResponse, 0),
closeChan: make([]*chan *interfaces.CloseResponse, 0),
errorChan: make([]*chan *interfaces.ErrorResponse, 0),
unhandledChan: make([]*chan *[]byte, 0),
Expand All @@ -69,6 +70,7 @@ func NewChanRouter(chans interfaces.AgentMessageChan) *ChanRouter {
router.closeChan = append(router.closeChan, chans.GetClose()...)
router.errorChan = append(router.errorChan, chans.GetError()...)
router.unhandledChan = append(router.unhandledChan, chans.GetUnhandled()...)
router.injectionRefusedResponse = append(router.injectionRefusedResponse, chans.GetInjectionRefused()...)
}

return router
Expand Down Expand Up @@ -316,6 +318,35 @@ func (r *ChanRouter) processErrorResponse(byMsg []byte) error {
return r.processGeneric(string(interfaces.TypeErrorResponse), byMsg, action)
}

func (r *ChanRouter) processInjectionRefused(byMsg []byte) error {
var response interfaces.InjectionRefusedResponse
if err := json.Unmarshal(byMsg, &response); err != nil {
return err
}

for _, ch := range r.injectionRefusedResponse {
*ch <- &response
}
return nil
}

func (r *ChanRouter) processKeepAlive(byMsg []byte) error {
action := func(data []byte) error {
var msg interfaces.KeepAlive
if err := json.Unmarshal(byMsg, &msg); err != nil {
klog.V(1).Infof("json.Unmarshal(KeepAlive) failed. Err: %v\n", err)
return err
}

for _, ch := range r.keepAliveResponse {
*ch <- &msg
}
return nil
}

return r.processGeneric(string(interfaces.TypeKeepAlive), byMsg, action)
}

// Message handles platform messages and routes them appropriately based on the MessageType
func (r *ChanRouter) Message(byMsg []byte) error {
klog.V(6).Infof("router.Message ENTER\n")
Expand Down Expand Up @@ -351,6 +382,10 @@ func (r *ChanRouter) Message(byMsg []byte) error {
err = r.processAgentAudioDone(byMsg)
case interfaces.TypeResponse(interfaces.TypeErrorResponse):
err = r.processErrorResponse(byMsg)
case interfaces.TypeInjectionRefusedResponse:
err = r.processInjectionRefused(byMsg)
case interfaces.TypeKeepAlive:
err = r.processKeepAlive(byMsg)
default:
err = r.UnhandledMessage(byMsg)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/api/agent/v1/websocket/interfaces/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ const (
TypeAgentAudioDoneResponse = "AgentAudioDone"
TypeCloseResponse = commoninterfaces.TypeCloseResponse
TypeErrorResponse = commoninterfaces.TypeErrorResponse
TypeInjectionRefusedResponse = "InjectionRefused"
)
2 changes: 2 additions & 0 deletions pkg/api/agent/v1/websocket/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ type AgentMessageChan interface {
GetClose() []*chan *CloseResponse
GetError() []*chan *ErrorResponse
GetUnhandled() []*chan *[]byte
GetInjectionRefused() []*chan *InjectionRefusedResponse
GetKeepAlive() []*chan *KeepAlive
}
6 changes: 6 additions & 0 deletions pkg/api/agent/v1/websocket/interfaces/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,9 @@ type CloseResponse = commoninterfaces.CloseResponse

// ErrorResponse is the Deepgram specific response error
type ErrorResponse = interfaces.DeepgramError

// InjectionRefusedResponse is the response when an agent message injection is refused
type InjectionRefusedResponse struct {
Type string `json:"type,omitempty"`
Message string `json:"message,omitempty"`
}
4 changes: 4 additions & 0 deletions pkg/api/agent/v1/websocket/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type DefaultChanHandler struct {
functionCallingResponse chan *interfaces.FunctionCallingResponse
agentStartedSpeakingResponse chan *interfaces.AgentStartedSpeakingResponse
agentAudioDoneResponse chan *interfaces.AgentAudioDoneResponse
injectionRefusedResponse chan *interfaces.InjectionRefusedResponse
keepAliveResponse chan *interfaces.KeepAlive
closeChan chan *interfaces.CloseResponse
errorChan chan *interfaces.ErrorResponse
unhandledChan chan *[]byte
Expand All @@ -47,6 +49,8 @@ type ChanRouter struct {
functionCallingResponse []*chan *interfaces.FunctionCallingResponse
agentStartedSpeakingResponse []*chan *interfaces.AgentStartedSpeakingResponse
agentAudioDoneResponse []*chan *interfaces.AgentAudioDoneResponse
injectionRefusedResponse []*chan *interfaces.InjectionRefusedResponse
keepAliveResponse []*chan *interfaces.KeepAlive
closeChan []*chan *interfaces.CloseResponse
errorChan []*chan *interfaces.ErrorResponse
unhandledChan []*chan *[]byte
Expand Down
4 changes: 3 additions & 1 deletion pkg/client/interfaces/v1/types-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ type Think struct {
Functions []Functions `json:"functions,omitempty"`
}
type Speak struct {
Model string `json:"model,omitempty"`
Model string `json:"model,omitempty"`
Provider string `json:"provider,omitempty"`
VoiceID string `json:"voice_id,omitempty"`
}
type Agent struct {
Listen Listen `json:"listen"`
Expand Down
2 changes: 1 addition & 1 deletion tests/daily_test/prerecorded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
)

const (
FromURLSmartFormat = "Yep. I said it before and I'll say it again. Life moves pretty fast. You don't stop and look around once in a while, you could miss it."
FromURLSmartFormat = "Yep. I said it before, and I'll say it again. Life moves pretty fast. You don't stop and look around once in a while, you could miss it."
FromURLSummarize = "Yep. I said it before, and I'll say it again. Life moves pretty fast. You don't stop and look around once in a while, you could miss it."
)

Expand Down
2 changes: 1 addition & 1 deletion tests/edge_cases/cancel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func main() {
}

// use the default callback handler which just dumps all messages to the screen
dgClient, err := client.NewWebSocketWithCancel(ctx, ctxCancel, "", cOptions, tOptions, nil)
dgClient, err := client.NewWebSocketUsingChanWithCancel(ctx, ctxCancel, "", cOptions, tOptions, nil)
if err != nil {
fmt.Println("ERROR creating LiveClient connection:", err)
return
Expand Down
2 changes: 1 addition & 1 deletion tests/edge_cases/failed_retry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
}

// use the default callback handler which just dumps all messages to the screen
dgClient, err := client.NewWebSocket(ctx, "", cOptions, tOptions, nil)
dgClient, err := client.NewWebSocketUsingChan(ctx, "", cOptions, tOptions, nil)
if err != nil {
fmt.Println("ERROR creating LiveClient connection:", err)
return
Expand Down
2 changes: 1 addition & 1 deletion tests/edge_cases/keepalive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func main() {
}

// use the default callback handler which just dumps all messages to the screen
dgClient, err := client.NewWebSocket(ctx, "", cOptions, tOptions, nil)
dgClient, err := client.NewWebSocketUsingChan(ctx, "", cOptions, tOptions, nil)
if err != nil {
fmt.Println("ERROR creating LiveClient connection:", err)
return
Expand Down
3 changes: 1 addition & 2 deletions tests/edge_cases/reconnect_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func (c MyCallback) UtteranceEnd(ur *api.UtteranceEndResponse) error {
} else {
fmt.Printf("\n[UtteranceEnd] Received\n")
}

return nil
}

Expand Down Expand Up @@ -142,7 +141,7 @@ func main() {
}

// create a Deepgram client
dgClient, err := client.NewWebSocket(ctx, "", cOptions, tOptions, callback)
dgClient, err := client.NewWebSocketUsingCallback(ctx, "", cOptions, tOptions, callback)
if err != nil {
fmt.Println("ERROR creating LiveTranscription connection:", err)
return
Expand Down
2 changes: 1 addition & 1 deletion tests/edge_cases/timeout/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
}

// use the default callback handler which just dumps all messages to the screen
dgClient, err := client.NewWebSocketWithDefaults(ctx, tOptions, nil)
dgClient, err := client.NewWSUsingChanWithDefaults(ctx, tOptions, nil)
if err != nil {
fmt.Println("ERROR creating LiveClient connection:", err)
return
Expand Down
Loading

0 comments on commit 9d0bacf

Please sign in to comment.