-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
125 lines (100 loc) · 2.65 KB
/
client.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
package cosyvoice
import (
"context"
"time"
"fmt"
"net/http"
openairt "github.com/WqyJh/go-openai-realtime"
gorilla "github.com/WqyJh/go-openai-realtime/contrib/ws-gorilla"
)
type Client struct {
config ClientConfig
}
func NewClient(apiKey string) *Client {
config := DefaultConfig(apiKey)
return NewClientWithConfig(config)
}
func NewClientWithConfig(config ClientConfig) *Client {
return &Client{
config: config,
}
}
func (c *Client) getHeaders() http.Header {
header := make(http.Header)
header.Add("X-DashScope-DataInspection", "enable")
header.Add("Authorization", fmt.Sprintf("bearer %s", c.config.ApiKey))
return header
}
type synthesizerOption struct {
dialer openairt.WebSocketDialer
logger openairt.Logger
synthesizerConfig SynthesizerConfig
pingInterval int
chanSize int
}
type SynthesizerOption func(*synthesizerOption)
func WithDialer(dialer openairt.WebSocketDialer) SynthesizerOption {
return func(opts *synthesizerOption) {
opts.dialer = dialer
}
}
func WithSynthesizerConfig(config SynthesizerConfig) SynthesizerOption {
return func(opts *synthesizerOption) {
opts.synthesizerConfig = config
}
}
func WithPingInterval(interval int) SynthesizerOption {
return func(opts *synthesizerOption) {
opts.pingInterval = interval
}
}
func WithChanSize(size int) SynthesizerOption {
return func(opts *synthesizerOption) {
opts.chanSize = size
}
}
func WithLogger(logger openairt.Logger) SynthesizerOption {
return func(opts *synthesizerOption) {
opts.logger = logger
}
}
func (c *Client) AsyncSynthesizer(ctx context.Context, opts ...SynthesizerOption) (*AsyncSynthesizer, error) {
option := synthesizerOption{
pingInterval: 45,
chanSize: 32,
dialer: gorilla.NewWebSocketDialer(gorilla.WebSocketOptions{}),
logger: openairt.NopLogger{},
synthesizerConfig: DefaultSynthesizerConfig(),
}
for _, opt := range opts {
opt(&option)
}
header := c.getHeaders()
url := c.config.WsUrl
socketConn, err := option.dialer.Dial(ctx, url, header)
if err != nil {
return nil, err
}
conn := newWsConn(
ctx,
socketConn,
option.logger,
time.Duration(option.pingInterval)*time.Second,
)
go conn.handleHealthCheck()
asyncSynthesizer := AsyncSynthesizer{
conn: conn,
config: option.synthesizerConfig,
chanSize: option.chanSize,
}
return &asyncSynthesizer, nil
}
func (c *Client) SyncSynthesizer(ctx context.Context, opts ...SynthesizerOption) (*SyncSynthesizer, error) {
asyncSynthesizer, err := c.AsyncSynthesizer(ctx, opts...)
if err != nil {
return nil, err
}
return &SyncSynthesizer{
asyncSynthesizer: asyncSynthesizer,
}, nil
}