/
do.go
86 lines (78 loc) · 2.3 KB
/
do.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
package infrastructure
import (
"database/sql"
"encoding/json"
"time"
"github.com/go-jimu/components/mediator"
"github.com/jacexh/chatgpt-bot/internal/chat/domain"
)
type (
Conversation struct {
ID sql.NullInt32 `db:"id"`
ChatID sql.NullString `db:"chat_id"`
Prompt sql.NullString `db:"prompt"`
Completion sql.NullString `db:"completion"`
ChannelMessageID sql.NullString `db:"channel_message_id"`
CTime sql.NullTime `db:"ctime"`
MTime sql.NullTime `db:"mtime"`
}
Chat struct {
ID string `db:"id"`
Counts int `db:"counts"`
Current string `db:"current"`
Channel int `db:"channel"`
ChannelUserID string `db:"channel_user_id"`
Version int `db:"version"`
CTime time.Time `db:"ctime"`
MTime time.Time `db:"mtime"`
Deleted int `db:"deleted"`
}
)
func ConverDO(ch *Chat, cs ...*Conversation) (*domain.Chat, error) {
c := &domain.Chat{
ID: ch.ID,
From: domain.From{Channel: domain.Channel(ch.Channel), ChannelUserID: domain.ChannelUserID(ch.ChannelUserID)},
Version: ch.Version,
Counts: ch.Counts,
Conversations: make([]*domain.Conversation, len(cs)),
Status: domain.StatusReady,
CreatedAt: ch.CTime,
Event: mediator.NewEventCollection(),
}
if ch.Current != "" && ch.Current != "{}" {
cu := new(domain.Conversation)
if err := json.Unmarshal([]byte(ch.Current), cu); err != nil {
return nil, err
}
c.Current = cu
}
for index, con := range cs {
c.Conversations[index] = &domain.Conversation{
MessageID: domain.ChannelMessageID(con.ChannelMessageID.String),
Prompt: con.Prompt.String,
Completion: con.Completion.String,
}
}
return c, nil
}
func ConvertEntityChat(entity *domain.Chat) (*Chat, error) {
c := &Chat{
ID: entity.ID,
Counts: entity.Counts,
Channel: int(entity.From.Channel),
ChannelUserID: string(entity.From.ChannelUserID),
Version: entity.Version,
Deleted: int(entity.Status),
}
if entity.Current != nil {
data, err := json.Marshal(entity.Current)
if err != nil {
return nil, err
}
str := string(data)
if str != "" && str != "{}" {
c.Current = str
}
}
return c, nil
}