Skip to content

Commit

Permalink
Merge pull request #147 from boyswan/main
Browse files Browse the repository at this point in the history
Add TypeUtteranceEndResponse & handle case in live.router
  • Loading branch information
davidvonthenen authored Dec 21, 2023
2 parents 1e72d2d + 3e7950e commit 843e1e0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/api/live/v1/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ func (dch DefaultCallbackHandler) Metadata(md *interfaces.MetadataResponse) erro
return nil
}

func (dch DefaultCallbackHandler) UtteranceEnd(ur *interfaces.UtteranceEndResponse) error {
fmt.Printf("\nUtteranceEnd \n")
return nil
}

func (dch DefaultCallbackHandler) Error(er *interfaces.ErrorResponse) error {
var debugStr string
if v := os.Getenv("DEEPGRAM_DEBUG"); v != "" {
Expand Down
5 changes: 3 additions & 2 deletions pkg/api/live/v1/interfaces/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ package interfaces
// These are the message types that can be received from the live API
const (
// message types
TypeMessageResponse string = "Results"
TypeMetadataResponse string = "Metadata"
TypeMessageResponse string = "Results"
TypeMetadataResponse string = "Metadata"
TypeUtteranceEndResponse string = "UtteranceEnd"

// Error type
TypeErrorResponse string = "Error"
Expand Down
1 change: 1 addition & 0 deletions pkg/api/live/v1/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package interfaces
type LiveMessageCallback interface {
Message(mr *MessageResponse) error
Metadata(md *MetadataResponse) error
UtteranceEnd(ur *UtteranceEndResponse) error
Error(er *ErrorResponse) error
// TODO: implement other conversation insights
}
5 changes: 5 additions & 0 deletions pkg/api/live/v1/interfaces/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ type MetadataResponse struct {
Type string `json:"type,omitempty"`
}

// UtteranceEndResponse is the response from a live transcription
type UtteranceEndResponse struct {
Type string `json:"type,omitempty"`
}

// ErrorResponse is the response from a live transcription
type ErrorResponse struct {
Description string `json:"description"`
Expand Down
30 changes: 30 additions & 0 deletions pkg/api/live/v1/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func (r *MessageRouter) Message(byMsg []byte) error {
return r.MessageResponse(byMsg)
case interfaces.TypeMetadataResponse:
return r.MetadataResponse(byMsg)
case interfaces.TypeUtteranceEndResponse:
return r.UtteranceEndResponse(byMsg)
default:
return r.UnhandledMessage(byMsg)
}
Expand Down Expand Up @@ -121,6 +123,34 @@ func (r *MessageRouter) MetadataResponse(byMsg []byte) error {
return nil
}

func (r *MessageRouter) UtteranceEndResponse(byMsg []byte) error {
klog.V(6).Infof("router.UtteranceEnd ENTER\n")

// trace debugging
r.printDebugMessages(5, "UtteranceEnd", byMsg)

var ur interfaces.UtteranceEndResponse
err := json.Unmarshal(byMsg, &ur)
if err != nil {
klog.V(1).Infof("UtteranceEnd json.Unmarshal failed. Err: %v\n", err)
klog.V(6).Infof("router.UtteranceEnd LEAVE\n")
return err
}

if r.callback != nil {
err := r.callback.UtteranceEnd(&ur)
if err != nil {
klog.V(1).Infof("callback.UtteranceEnd failed. Err: %v\n", err)
} else {
klog.V(5).Infof("callback.UtteranceEnd succeeded\n")
}
klog.V(6).Infof("router.UtteranceEnd LEAVE\n")
return err
}

return nil
}

func (r *MessageRouter) ErrorResponse(byMsg []byte) error {
klog.V(6).Infof("router.ErrorResponse ENTER\n")

Expand Down

0 comments on commit 843e1e0

Please sign in to comment.