/
conn.go
232 lines (203 loc) · 6.19 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package chipper
import (
"context"
"fmt"
"time"
pb "github.com/digital-dream-labs/api/go/chipperpb"
"github.com/digital-dream-labs/opus-go/opus"
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
"github.com/opentracing/opentracing-go"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// Stream represents an open stream of various possible types (intent, knowledge graph) to Chipper
type Stream interface {
SendAudio(audioData []byte) error
WaitForResponse() (interface{}, error)
Close() error
CloseSend() error
}
// StreamOpts defines options common to all possible stream types
type StreamOpts struct {
CompressOpts
Timeout time.Duration
Language pb.LanguageCode
SaveAudio bool
NoDas bool
}
// IntentOpts extends StreamOpts with options unique to intent streams
type IntentOpts struct {
StreamOpts
Handler pb.IntentService
Mode pb.RobotMode
SpeechOnly bool
}
// ConnectOpts extends StreamOpts with options unique to connection check streams
type ConnectOpts struct {
StreamOpts
TotalAudioMs uint32
AudioPerRequestMs uint32
}
// KGOpts extends StreamOpts with options unique to knowledge graph streams
type KGOpts struct {
StreamOpts
Timezone string
}
// IntentGraphOpts extends StreamOpts with options of both Intent and KG
type IntentGraphOpts struct {
StreamOpts
Handler pb.IntentService
Mode pb.RobotMode
SpeechOnly bool
// Doesnt seem to be passing this in
Timezone string
}
// CompressOpts specifies whether compression should be used and, if so, allows
// the specification of parameters related to it
type CompressOpts struct {
Bitrate uint
Complexity uint
FrameSize float32
PreEncoded bool
Compress bool
}
// Conn represents an underlying GRPC connection to the Chipper server
type Conn struct {
conn *grpc.ClientConn
client pb.ChipperGrpcClient
appKey string
opts *connOpts
}
// NewConn creates a new connection to the given GRPC server
func NewConn(ctx context.Context, serverURL string, appKey string, options ...ConnOpt) (*Conn, error) {
// create default options and apply user provided overrides
opts := connOpts{deviceID: "device-id"}
for _, opt := range options {
opt(&opts)
}
dialOpts := opts.grpcOpts
if opts.insecure {
dialOpts = append(dialOpts, grpc.WithInsecure())
} else {
dialOpts = append(
dialOpts,
grpc.WithTransportCredentials(
credentials.NewClientTLSFromCert(getTLSCerts(), ""),
),
)
}
if metadata := getMetadata(appKey, &opts); metadata != nil {
dialOpts = append(dialOpts, grpc.WithPerRPCCredentials(metadata))
}
dialOpts = append(dialOpts, grpc.WithUnaryInterceptor(otgrpc.OpenTracingClientInterceptor(opentracing.GlobalTracer())))
dialOpts = append(dialOpts, grpc.WithStreamInterceptor(otgrpc.OpenTracingStreamClientInterceptor(opentracing.GlobalTracer())))
rpcConn, err := grpc.DialContext(ctx, serverURL, dialOpts...)
if err != nil {
return nil, err
}
rpcClient := pb.NewChipperGrpcClient(rpcConn)
conn := &Conn{
conn: rpcConn,
client: rpcClient,
appKey: appKey,
opts: &opts}
return conn, nil
}
func (c *Conn) newStream(opts *StreamOpts, client grpc.ClientStream, cancel func()) baseStream {
var encoder *opus.OggStream
if opts.Compress {
encoder = &opus.OggStream{
SampleRate: 16000,
Channels: 1,
FrameSize: opts.FrameSize,
Bitrate: opts.Bitrate,
Complexity: opts.Complexity}
}
return baseStream{
conn: c,
client: client,
opts: opts,
encoder: encoder,
cancel: cancel,
}
}
// NewIntentStream opens a new stream on the given connection to stream audio for the purpose of getting
// an intent response
func (c *Conn) NewIntentStream(ctx context.Context, opts IntentOpts) (Stream, error) {
ctx, cancel := getContext(ctx, &opts.StreamOpts)
client, err := c.client.StreamingIntent(ctx)
if err != nil {
fmt.Println("GRPC Stream creation error:", err)
return nil, err
}
return &intentStream{
baseStream: c.newStream(&opts.StreamOpts, client, cancel),
opts: &opts,
}, nil
}
// NewKGStream opens a new stream on the given connection to stream audio for the purpose of getting
// a knowledge graph response
func (c *Conn) NewKGStream(ctx context.Context, opts KGOpts) (Stream, error) {
ctx, cancel := getContext(ctx, &opts.StreamOpts)
client, err := c.client.StreamingKnowledgeGraph(ctx)
if err != nil {
fmt.Println("GRPC Stream creation error:", err)
return nil, err
}
return &kgStream{
baseStream: c.newStream(&opts.StreamOpts, client, cancel),
opts: &opts,
}, nil
}
// NewIntentGraphStream opens a new stream on the given connection to stream audio for the purpose of getting
// a knowledge graph response
func (c *Conn) NewIntentGraphStream(ctx context.Context, opts IntentGraphOpts) (Stream, error) {
ctx, cancel := getContext(ctx, &opts.StreamOpts)
client, err := c.client.StreamingIntentGraph(ctx)
if err != nil {
return nil, err
}
return &intentGraphStream{
baseStream: c.newStream(&opts.StreamOpts, client, cancel),
opts: &opts,
}, nil
}
// NewConnectionStream opens a stream for doing connection checks
func (c *Conn) NewConnectionStream(ctx context.Context, opts ConnectOpts) (Stream, error) {
ctx, cancel := getContext(ctx, &opts.StreamOpts)
client, err := c.client.StreamingConnectionCheck(ctx)
if err != nil {
fmt.Println("GRPC Stream creation error:", err)
return nil, err
}
return &connectionStream{
baseStream: c.newStream(&opts.StreamOpts, client, cancel),
opts: &opts,
}, nil
}
// Close closes the underlying connection
func (c *Conn) Close() error {
return c.conn.Close()
}
// SendText performs an intent request with a text string instead of voice data
func (c *Conn) SendText(ctx context.Context, text, session, device string,
service pb.IntentService) *pb.IntentResult {
r := &pb.TextRequest{
TextInput: text,
DeviceId: device,
Session: session,
LanguageCode: pb.LanguageCode_ENGLISH_US,
IntentService: service,
FirmwareVersion: c.opts.firmware,
}
res, err := c.client.TextIntent(ctx, r)
if err != nil {
fmt.Println("Text intent error:", err)
return nil
}
fmt.Printf("Text_intent=\"%s\" query=\"%s\", param=\"%v\"",
res.IntentResult.Action,
res.IntentResult.QueryText,
res.IntentResult.Parameters)
return res.IntentResult
}