-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathhub.go
190 lines (164 loc) · 3.9 KB
/
hub.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
package EdgeGPT
import (
"encoding/json"
"errors"
"github.com/gorilla/websocket"
"github.com/pavel-one/EdgeGPT-Go/config"
"github.com/pavel-one/EdgeGPT-Go/internal/Helpers"
"github.com/pavel-one/EdgeGPT-Go/responses"
"net/url"
"sync"
)
type Hub struct {
conversation *Conversation
conn *websocket.Conn
wssUrl *url.URL
headers map[string]string
InvocationId int
mu sync.Mutex
}
func NewHub(conversation *Conversation, config *config.GPT) (*Hub, error) {
if conversation == nil {
return nil, errors.New("not set conversation")
}
h := &Hub{
conversation: conversation,
conn: nil,
wssUrl: config.WssUrl,
headers: config.Headers,
}
conn, err := h.NewConnect()
if err != nil {
return nil, err
}
h.conn = conn
log.Infoln("New hub for conversation:", conversation.ConversationId)
return h, nil
}
// NewConnect create new websocket connection
func (h *Hub) NewConnect() (*websocket.Conn, error) {
conn, _, err := websocket.DefaultDialer.Dial(h.wssUrl.String(), Helpers.GetHeaders(h.headers))
if err != nil {
return nil, err
}
message := []byte("{\"protocol\": \"json\", \"version\": 1}" + Delimiter)
if err := conn.WriteMessage(websocket.TextMessage, message); err != nil {
return nil, err
}
if _, _, err := conn.ReadMessage(); err != nil { //wait initial
return nil, err
}
return conn, nil
}
// CheckAndReconnect check active connection and reconnect
func (h *Hub) CheckAndReconnect() error {
if h.conn == nil {
return errors.New("not set connection")
}
if err := h.conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
log.Infoln("Reconnection")
h.Close()
h.conn = nil
conn, err := h.NewConnect()
if err != nil {
return err
}
h.conn = conn
}
return nil
}
// send new message to websocket
func (h *Hub) send(style, message string) (*responses.MessageWrapper, error) {
if h.conn == nil {
return nil, errors.New("not set connection")
}
h.mu.Lock()
if err := h.CheckAndReconnect(); err != nil {
return nil, err
}
m, err := json.Marshal(h.getRequest(style, message))
if err != nil {
return nil, err
}
m = append(m, DelimiterByte)
if err := h.conn.WriteMessage(websocket.TextMessage, m); err != nil {
return nil, err
}
return responses.NewMessageWrapper(message, &h.mu, h.conn), nil
}
// Close hub and connection
// TODO: Use this!
func (h *Hub) Close() {
if h.conn == nil {
return
}
log.Infoln("Close connection")
h.conn.Close()
}
// getRequest generate struct for new request websocket
func (h *Hub) getRequest(style, message string) map[string]any {
switch style {
case "creative":
style = StyleCreative
break
case "balanced":
style = StyleBalanced
break
case "precise":
style = StylePrecise
break
case StyleCreative:
style = StyleCreative
break
case StyleBalanced:
style = StyleBalanced
break
case StylePrecise:
style = StylePrecise
break
default:
style = StyleBalanced
}
m := map[string]any{
"invocationId": string(rune(h.InvocationId)),
"target": "chat",
"type": 4,
"arguments": []map[string]any{
{
"source": "cib",
"optionsSets": []string{
"nlu_direct_response_filter",
"deepleo",
"disable_emoji_spoken_text",
"responsible_ai_policy_235",
"enablemm",
style,
"dtappid",
"cricinfo",
"cricinfov2",
"dv3sugg",
},
"sliceIds": []string{
"222dtappid",
"225cricinfo",
"224locals0",
},
"traceId": Helpers.RandomHex(32),
"isStartOfSession": h.InvocationId == 0,
"message": map[string]any{
"author": "user",
"inputMethod": "Keyboard",
"text": message,
"messageType": "Chat",
},
"conversationSignature": h.conversation.ConversationSignature,
"participant": map[string]any{
"id": h.conversation.ClientId,
},
"conversationId": h.conversation.ConversationId,
},
},
}
h.InvocationId++
return m
}