-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconversations.go
179 lines (152 loc) · 5.21 KB
/
conversations.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
package coze
import (
"context"
"fmt"
"net/http"
"strconv"
)
func (r *conversations) List(ctx context.Context, req *ListConversationsReq) (NumberPaged[Conversation], error) {
if req.PageSize == 0 {
req.PageSize = 20
}
if req.PageNum == 0 {
req.PageNum = 1
}
return NewNumberPaged[Conversation](
func(request *pageRequest) (*pageResponse[Conversation], error) {
uri := "/v1/conversations"
resp := &listConversationsResp{}
err := r.client.Request(ctx, http.MethodGet, uri, nil, resp,
withHTTPQuery("bot_id", req.BotID),
withHTTPQuery("page_num", strconv.Itoa(request.PageNum)),
withHTTPQuery("page_size", strconv.Itoa(request.PageSize)))
if err != nil {
return nil, err
}
return &pageResponse[Conversation]{
HasMore: resp.Data.HasMore,
Data: resp.Data.Conversations,
LogID: resp.HTTPResponse.LogID(),
}, nil
}, req.PageSize, req.PageNum)
}
func (r *conversations) Create(ctx context.Context, req *CreateConversationsReq) (*CreateConversationsResp, error) {
uri := "/v1/conversation/create"
resp := &createConversationsResp{}
err := r.client.Request(ctx, http.MethodPost, uri, req, resp)
if err != nil {
return nil, err
}
resp.Conversation.setHTTPResponse(resp.HTTPResponse)
return resp.Conversation, nil
}
func (r *conversations) Retrieve(ctx context.Context, req *RetrieveConversationsReq) (*RetrieveConversationsResp, error) {
uri := "/v1/conversation/retrieve"
resp := &retrieveConversationsResp{}
err := r.client.Request(ctx, http.MethodGet, uri, nil, resp, withHTTPQuery("conversation_id", req.ConversationID))
if err != nil {
return nil, err
}
resp.Conversation.setHTTPResponse(resp.HTTPResponse)
return resp.Conversation, nil
}
func (r *conversations) Clear(ctx context.Context, req *ClearConversationsReq) (*ClearConversationsResp, error) {
uri := fmt.Sprintf("/v1/conversations/%s/clear", req.ConversationID)
resp := &clearConversationsResp{}
err := r.client.Request(ctx, http.MethodPost, uri, nil, resp)
if err != nil {
return nil, err
}
resp.Data.setHTTPResponse(resp.HTTPResponse)
return resp.Data, nil
}
type conversations struct {
client *core
Messages *conversationsMessages
}
func newConversations(core *core) *conversations {
return &conversations{
client: core,
Messages: newConversationMessage(core),
}
}
// Conversation represents conversation information
type Conversation struct {
// The ID of the conversation
ID string `json:"id"`
// Indicates the create time of the conversation. The value format is Unix timestamp in seconds.
CreatedAt int `json:"created_at"`
// Additional information when creating a message, and this additional information will also be
// returned when retrieving messages.
MetaData map[string]string `json:"meta_data,omitempty"`
// section_id is used to distinguish the context sections of the session history.
// The same section is one context.
LastSectionID string `json:"last_section_id"`
}
// CreateConversationsReq represents request for creating conversation
type CreateConversationsReq struct {
// Messages in the conversation. For more information, see EnterMessage object.
Messages []*Message `json:"messages,omitempty"`
// Additional information when creating a message, and this additional information will also be
// returned when retrieving messages.
MetaData map[string]string `json:"meta_data,omitempty"`
// Bind and isolate conversation on different bots.
BotID string `json:"bot_id,omitempty"`
}
// ListConversationsReq represents request for listing conversations
type ListConversationsReq struct {
// The ID of the bot.
BotID string `json:"bot_id"`
// The page number.
PageNum int `json:"page_num,omitempty"`
// The page size.
PageSize int `json:"page_size,omitempty"`
}
// RetrieveConversationsReq represents request for retrieving conversation
type RetrieveConversationsReq struct {
// The ID of the conversation.
ConversationID string `json:"conversation_id"`
}
// ClearConversationsReq represents request for clearing conversation
type ClearConversationsReq struct {
// The ID of the conversation.
ConversationID string `json:"conversation_id"`
}
// CreateConversationsResp represents response for creating conversation
type createConversationsResp struct {
baseResponse
Conversation *CreateConversationsResp `json:"data"`
}
type CreateConversationsResp struct {
baseModel
Conversation
}
// listConversationsResp represents response for listing conversations
type listConversationsResp struct {
baseResponse
Data *ListConversationsResp `json:"data"`
}
// ListConversationsResp represents response for listing conversations
type ListConversationsResp struct {
baseModel
HasMore bool `json:"has_more"`
Conversations []*Conversation `json:"conversations"`
}
// RetrieveConversationsResp represents response for retrieving conversation
type retrieveConversationsResp struct {
baseResponse
Conversation *RetrieveConversationsResp `json:"data"`
}
type RetrieveConversationsResp struct {
baseModel
Conversation
}
// ClearConversationsResp represents response for clearing conversation
type clearConversationsResp struct {
baseResponse
Data *ClearConversationsResp `json:"data"`
}
type ClearConversationsResp struct {
baseModel
ConversationID string `json:"conversation_id"`
}