forked from acheong08/ChatGPT-API-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
109 lines (92 loc) · 2.2 KB
/
types.go
File metadata and controls
109 lines (92 loc) · 2.2 KB
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
package types
import (
"sync"
"time"
"github.com/gorilla/websocket"
)
type Message struct {
Id string `json:"id"`
Message string `json:"message"`
Data string `json:"data"`
}
type ChatGptResponse struct {
Id string `json:"id"`
ResponseId string `json:"response_id"`
ConversationId string `json:"conversation_id"`
Content string `json:"content"`
Error string `json:"error"`
}
type ChatGptRequest struct {
MessageId string `json:"message_id"`
ConversationId string `json:"conversation_id"`
ParentId string `json:"parent_id"`
Content string `json:"content"`
}
type Connection struct {
// The websocket connection.
Ws *websocket.Conn
// The connecton id.
Id string
// Last heartbeat time.
Heartbeat time.Time
// Last message time.
LastMessageTime time.Time
}
type ConnectionPool struct {
Connections map[string]*Connection
Mu sync.RWMutex
}
func (p *ConnectionPool) Get(id string) (*Connection, bool) {
p.Mu.RLock()
defer p.Mu.RUnlock()
conn, ok := p.Connections[id]
if conn == nil {
ok = false
}
return conn, ok
}
func (p *ConnectionPool) Set(conn *Connection) {
p.Mu.Lock()
defer p.Mu.Unlock()
p.Connections[conn.Id] = conn
}
func (p *ConnectionPool) Delete(id string) error {
p.Mu.Lock()
defer p.Mu.Unlock()
delete(p.Connections, id)
return nil
}
func NewConnectionPool() *ConnectionPool {
return &ConnectionPool{
Connections: make(map[string]*Connection),
}
}
type Conversation struct {
Id string `json:"id"`
ConnectionId string `json:"connection_id"`
}
type ConversationPool struct {
Conversations map[string]*Conversation
Mu sync.RWMutex
}
func (p *ConversationPool) Get(id string) (*Conversation, bool) {
p.Mu.RLock()
defer p.Mu.RUnlock()
conversation, ok := p.Conversations[id]
return conversation, ok
}
func (p *ConversationPool) Set(conversation *Conversation) {
p.Mu.Lock()
defer p.Mu.Unlock()
p.Conversations[conversation.Id] = conversation
}
func (p *ConversationPool) Delete(id string) {
p.Mu.Lock()
defer p.Mu.Unlock()
delete(p.Conversations, id)
}
func NewConversationPool() *ConversationPool {
return &ConversationPool{
Conversations: make(map[string]*Conversation),
}
}