-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathcsgbot_svc_client.go
More file actions
246 lines (212 loc) · 8.65 KB
/
csgbot_svc_client.go
File metadata and controls
246 lines (212 loc) · 8.65 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package rpc
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"log/slog"
"net/http"
"strconv"
"time"
"opencsg.com/csghub-server/common/errorx"
"opencsg.com/csghub-server/common/types"
)
type CsgbotSvcClient interface {
DeleteWorkspaceFiles(ctx context.Context, userUUID string, username string, token string, agentName string) error
CreateKnowledgeBase(ctx context.Context, userUUID string, username string, token string, req *CreateKnowledgeBaseRequest) (*CreateKnowledgeBaseResponse, error)
DeleteKnowledgeBase(ctx context.Context, userUUID string, username string, token string, contentID string) error
UpdateKnowledgeBase(ctx context.Context, userUUID string, username string, token string, contentID string, req *types.UpdateAgentKnowledgeBaseRequest) error
}
type CreateKnowledgeBaseRequest struct {
Name string `json:"name"`
Description string `json:"description"`
}
type CreateKnowledgeBaseResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Data json.RawMessage `json:"data"`
IsComponent bool `json:"is_component"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Webhook bool `json:"webhook"`
Tags []string `json:"tags"`
Locked bool `json:"locked"`
McpEnabled bool `json:"mcp_enabled"`
AccessType string `json:"access_type"`
UserUUID string `json:"user_id"` // user uuid
FolderID string `json:"folder_id"`
Model string `json:"model"`
}
type DeleteKnowledgeBaseRequest struct {
IDs []string `json:"ids"`
}
type DeleteKnowledgeBaseResponse struct {
IDs []string `json:"ids"`
Total int `json:"total"`
}
type CsgbotSvcHttpClientImpl struct {
hc *HttpClient
}
func NewCsgbotSvcHttpClient(endpoint string, opts ...RequestOption) CsgbotSvcClient {
return &CsgbotSvcHttpClientImpl{
hc: NewHttpClient(endpoint, opts...),
}
}
// Delete workspace files for a code agent
// DELETE /api/v1/csgbot/codeAgent/{agent_name}
func (c *CsgbotSvcHttpClientImpl) DeleteWorkspaceFiles(ctx context.Context, userUUID string, username string, token string, agentName string) error {
rpcErrorCtx := map[string]any{
"user_uuid": userUUID,
"service": "csgbot",
"api": "DELETE /api/v1/csgbot/codeAgent/{agent_name}",
}
path := c.hc.endpoint + "/api/v1/csgbot/codeAgent/" + agentName
hreq, err := http.NewRequestWithContext(ctx, http.MethodDelete, path, nil)
if err != nil {
return errorx.InternalServerError(err, rpcErrorCtx)
}
hreq.Header.Set("Content-Type", "application/json")
hreq.Header.Set("user_uuid", userUUID)
hreq.Header.Set("user_name", username)
hreq.Header.Set("user_token", token)
hresp, err := c.hc.Do(hreq)
if err != nil {
slog.ErrorContext(ctx, "failed to delete workspace files for code agent", "error", err, "rpc_error_ctx", rpcErrorCtx)
return errorx.RemoteSvcFail(errors.New("failed to delete workspace files for code agent"), rpcErrorCtx)
}
defer hresp.Body.Close()
if hresp.StatusCode != http.StatusOK {
slog.ErrorContext(ctx, "failed to delete workspace files for code agent", "status_code", hresp.StatusCode, "rpc_error_ctx", rpcErrorCtx)
return errorx.RemoteSvcFail(errors.New("failed to delete workspace files for code agent"), rpcErrorCtx)
}
return nil
}
// Create knowledge base
// POST /api/v1/csgbot/langflow/flows/rag
func (c *CsgbotSvcHttpClientImpl) CreateKnowledgeBase(ctx context.Context, userUUID string, username string, token string, req *CreateKnowledgeBaseRequest) (*CreateKnowledgeBaseResponse, error) {
if req == nil {
return nil, errorx.BadRequest(errors.New("create knowledge base request is nil"), nil)
}
rpcErrorCtx := map[string]any{
"user_uuid": userUUID,
"service": "csgbot",
"api": "POST /api/v1/csgbot/langflow/flows/rag",
}
jsonData, err := json.Marshal(req)
if err != nil {
return nil, errorx.InternalServerError(err, rpcErrorCtx)
}
buf := bytes.NewBuffer(jsonData)
path := c.hc.endpoint + "/api/v1/csgbot/langflow/flows/rag"
hreq, err := http.NewRequestWithContext(ctx, http.MethodPost, path, buf)
if err != nil {
return nil, errorx.InternalServerError(err, rpcErrorCtx)
}
hreq.Header.Set("Content-Type", "application/json")
hreq.Header.Set("user_uuid", userUUID)
hreq.Header.Set("user_name", username)
hreq.Header.Set("user_token", token)
hresp, err := c.hc.Do(hreq)
if err != nil {
slog.ErrorContext(ctx, "failed to create knowledge base in csgbot service", "error", err, "rpc_error_ctx", rpcErrorCtx)
return nil, errorx.RemoteSvcFail(errors.New("failed to create knowledge base in csgbot service"), rpcErrorCtx)
}
defer hresp.Body.Close()
if hresp.StatusCode != http.StatusOK {
slog.ErrorContext(ctx, "failed to create knowledge base in csgbot service", "status_code", hresp.StatusCode, "rpc_error_ctx", rpcErrorCtx)
return nil, errorx.RemoteSvcFail(errors.New("failed to create knowledge base in csgbot service"), rpcErrorCtx)
}
body, err := io.ReadAll(hresp.Body)
if err != nil {
slog.ErrorContext(ctx, "failed to create knowledge base in csgbot service", "error", err, "rpc_error_ctx", rpcErrorCtx)
return nil, errorx.InternalServerError(err, rpcErrorCtx)
}
var resp CreateKnowledgeBaseResponse
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, errorx.InternalServerError(err, rpcErrorCtx)
}
return &resp, nil
}
// Delete knowledge base
// POST /api/v1/csgbot/langflow/flows/rag/delete
func (c *CsgbotSvcHttpClientImpl) DeleteKnowledgeBase(ctx context.Context, userUUID string, username string, token string, contentID string) error {
rpcErrorCtx := map[string]any{
"user_uuid": userUUID,
"content_id": contentID,
"service": "csgbot",
"api": "POST /api/v1/csgbot/langflow/flows/rag/delete",
}
var resp DeleteKnowledgeBaseResponse
req := DeleteKnowledgeBaseRequest{
IDs: []string{contentID},
}
jsonData, err := json.Marshal(req)
if err != nil {
return errorx.InternalServerError(err, rpcErrorCtx)
}
buf := bytes.NewBuffer(jsonData)
path := c.hc.endpoint + "/api/v1/csgbot/langflow/flows/rag/delete"
hreq, err := http.NewRequestWithContext(ctx, http.MethodPost, path, buf)
if err != nil {
return errorx.InternalServerError(err, rpcErrorCtx)
}
hreq.Header.Set("Content-Type", "application/json")
hreq.Header.Set("user_uuid", userUUID)
hreq.Header.Set("user_name", username)
hreq.Header.Set("user_token", token)
hresp, err := c.hc.Do(hreq)
if err != nil {
slog.ErrorContext(ctx, "failed to delete knowledge base in csgbot service", "error", err, "rpc_error_ctx", rpcErrorCtx)
return errorx.RemoteSvcFail(errors.New("failed to delete knowledge base in csgbot service"), rpcErrorCtx)
}
defer hresp.Body.Close()
if hresp.StatusCode != http.StatusOK {
return errorx.RemoteSvcFail(errors.New("failed to delete knowledge base in csgbot service, status code: "+strconv.Itoa(hresp.StatusCode)), rpcErrorCtx)
}
body, err := io.ReadAll(hresp.Body)
if err != nil {
return errorx.InternalServerError(err, rpcErrorCtx)
}
if err := json.Unmarshal(body, &resp); err != nil {
return errorx.RemoteSvcFail(errors.New("failed to delete knowledge base in csgbot service, unmarshal response error: "+err.Error()), rpcErrorCtx)
}
if resp.Total != 1 {
return errorx.RemoteSvcFail(errors.New("failed to delete knowledge base in csgbot service, total: "+strconv.Itoa(resp.Total)), rpcErrorCtx)
}
if len(resp.IDs) == 0 {
return errorx.RemoteSvcFail(errors.New("failed to delete knowledge base in csgbot service, response IDs is empty"), rpcErrorCtx)
}
if resp.IDs[0] != contentID {
return errorx.RemoteSvcFail(errors.New("failed to delete knowledge base in csgbot service, content ID mismatch: "+contentID+" != "+resp.IDs[0]), rpcErrorCtx)
}
return nil
}
// Update knowledge base
// PUT /api/v1/csgbot/langflow/flows/rag/{content_id}
func (c *CsgbotSvcHttpClientImpl) UpdateKnowledgeBase(_ context.Context, _ string, _ string, _ string, _ string, _ *types.UpdateAgentKnowledgeBaseRequest) error {
return nil
}
func NewCsgbotSvcHttpClientBuilder(endpoint string, opts ...RequestOption) CsgbotSvcClientBuilder {
return &CsgbotSvcHttpClientImpl{
hc: NewHttpClient(endpoint, opts...),
}
}
type CsgbotSvcClientBuilder interface {
WithRetry(attempts uint) CsgbotSvcClientBuilder
WithDelay(delay time.Duration) CsgbotSvcClientBuilder
Build() CsgbotSvcClient
}
func (c *CsgbotSvcHttpClientImpl) WithRetry(attempts uint) CsgbotSvcClientBuilder {
c.hc = c.hc.WithRetry(attempts)
return c
}
func (c *CsgbotSvcHttpClientImpl) WithDelay(delay time.Duration) CsgbotSvcClientBuilder {
c.hc = c.hc.WithDelay(delay)
return c
}
func (c *CsgbotSvcHttpClientImpl) Build() CsgbotSvcClient {
return c
}