forked from infiniflow/ragflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
276 lines (259 loc) · 8.05 KB
/
client.go
File metadata and controls
276 lines (259 loc) · 8.05 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package cli
import (
"fmt"
ce "ragflow/internal/cli/contextengine"
)
// PasswordPromptFunc is a function type for password input
type PasswordPromptFunc func(prompt string) (string, error)
// CurrentModel holds the current model configuration
type CurrentModel struct {
Provider string
Instance string
Model string
}
// RAGFlowClient handles API interactions with the RAGFlow server
type RAGFlowClient struct {
HTTPClient *HTTPClient
ServerType string // "admin" or "user"
PasswordPrompt PasswordPromptFunc // Function for password input
OutputFormat OutputFormat // Output format: table, plain, json
ContextEngine *ce.Engine // Context Engine for virtual filesystem
CurrentModel *CurrentModel // Current model configuration
}
// NewRAGFlowClient creates a new RAGFlow client
func NewRAGFlowClient(serverType string) *RAGFlowClient {
httpClient := NewHTTPClient()
// Set port from configuration file based on server type
if serverType == "admin" {
httpClient.Port = 9381
} else {
httpClient.Port = 9380
}
client := &RAGFlowClient{
HTTPClient: httpClient,
ServerType: serverType,
}
// Initialize Context Engine
client.initContextEngine()
return client
}
// initContextEngine initializes the Context Engine with all providers
func (c *RAGFlowClient) initContextEngine() {
engine := ce.NewEngine()
// Register providers
engine.RegisterProvider(ce.NewDatasetProvider(&httpClientAdapter{c.HTTPClient}))
c.ContextEngine = engine
}
// httpClientAdapter adapts HTTPClient to ce.HTTPClientInterface
type httpClientAdapter struct {
client *HTTPClient
}
func (a *httpClientAdapter) Request(method, path string, useAPIBase bool, authKind string, headers map[string]string, jsonBody map[string]interface{}) (*ce.HTTPResponse, error) {
// Auto-detect auth kind based on available tokens
// If authKind is "auto" or empty, determine based on token availability
if authKind == "auto" || authKind == "" {
if a.client.useAPIToken && a.client.APIToken != "" {
authKind = "api"
} else if a.client.LoginToken != "" {
authKind = "web"
} else {
authKind = "web" // default
}
}
resp, err := a.client.Request(method, path, useAPIBase, authKind, headers, jsonBody)
if err != nil {
return nil, err
}
return &ce.HTTPResponse{
StatusCode: resp.StatusCode,
Body: resp.Body,
Headers: resp.Headers,
Duration: resp.Duration,
}, nil
}
// ExecuteCommand executes a parsed command
// Returns benchmark result map for commands that support it (e.g., ping_server with iterations > 1)
func (c *RAGFlowClient) ExecuteCommand(cmd *Command) (ResponseIf, error) {
switch c.ServerType {
case "admin":
// Admin mode: execute command with admin privileges
return c.ExecuteAdminCommand(cmd)
case "user":
// User mode: execute command with user privileges
return c.ExecuteUserCommand(cmd)
default:
return nil, fmt.Errorf("invalid server type: %s", c.ServerType)
}
}
func (c *RAGFlowClient) ExecuteAdminCommand(cmd *Command) (ResponseIf, error) {
switch cmd.Type {
case "login_user":
return nil, c.LoginUser(cmd)
case "logout":
return c.Logout()
case "ping":
return c.PingAdmin(cmd)
case "benchmark":
return c.RunBenchmark(cmd)
case "list_user_datasets":
return c.ListUserDatasets(cmd)
case "list_users":
return c.ListUsers(cmd)
case "list_services":
return c.ListServices(cmd)
case "grant_admin":
return c.GrantAdmin(cmd)
case "revoke_admin":
return c.RevokeAdmin(cmd)
case "create_user":
return c.CreateUser(cmd)
case "activate_user":
return c.ActivateUser(cmd)
case "alter_user":
return c.AlterUserPassword(cmd)
case "drop_user":
return c.DropUser(cmd)
case "show_service":
return c.ShowService(cmd)
case "show_version":
return c.ShowAdminVersion(cmd)
case "show_user":
return c.ShowUser(cmd)
case "list_datasets":
return c.ListDatasets(cmd)
case "list_agents":
return c.ListAgents(cmd)
case "generate_token":
return c.GenerateAdminToken(cmd)
case "list_tokens":
return c.ListAdminTokens(cmd)
case "drop_token":
return c.DropAdminToken(cmd)
case "list_available_providers":
return c.ListAvailableProviders(cmd)
case "show_provider":
return c.ShowProvider(cmd)
case "list_provider_models":
return c.ListModels(cmd)
case "list_instance_models":
return c.ListInstanceModels(cmd)
case "show_model":
return c.ShowModel(cmd)
// TODO: Implement other commands
default:
return nil, fmt.Errorf("command '%s' would be executed with API", cmd.Type)
}
}
func (c *RAGFlowClient) ExecuteUserCommand(cmd *Command) (ResponseIf, error) {
switch cmd.Type {
case "register_user":
return c.RegisterUser(cmd)
case "login_user":
return nil, c.LoginUser(cmd)
case "logout":
return c.Logout()
case "ping":
return c.PingServer(cmd)
case "benchmark":
return c.RunBenchmark(cmd)
case "list_user_datasets":
return c.ListUserDatasets(cmd)
case "search_on_datasets":
return c.SearchOnDatasets(cmd)
case "create_token":
return c.CreateToken(cmd)
case "list_tokens":
return c.ListTokens(cmd)
case "drop_token":
return c.DropToken(cmd)
case "set_token":
return c.SetToken(cmd)
case "show_token":
return c.ShowToken(cmd)
case "unset_token":
return c.UnsetToken(cmd)
case "show_version":
return c.ShowServerVersion(cmd)
case "create_index":
return c.CreateIndex(cmd)
case "drop_index":
return c.DropIndex(cmd)
case "create_doc_meta_index":
return c.CreateDocMetaIndex(cmd)
case "drop_doc_meta_index":
return c.DropDocMetaIndex(cmd)
case "list_available_providers":
return c.ListAvailableProviders(cmd)
case "show_provider":
return c.ShowProvider(cmd)
case "list_provider_models":
return c.ListModels(cmd)
case "list_instance_models":
return c.ListInstanceModels(cmd)
case "show_model":
return c.ShowModel(cmd)
// Provider commands
case "add_provider":
return c.AddProvider(cmd)
case "list_providers":
return c.ListProviders(cmd)
case "delete_provider":
return c.DeleteProvider(cmd)
// Provider instance commands
case "create_provider_instance":
return c.CreateProviderInstance(cmd)
case "list_provider_instances":
return c.ListProviderInstances(cmd)
case "show_provider_instance":
return c.ShowProviderInstance(cmd)
case "alter_provider_instance":
return c.AlterProviderInstance(cmd)
case "drop_provider_instance":
return c.DropProviderInstance(cmd)
case "enable_model":
return c.EnableOrDisableModel(cmd, "enable")
case "disable_model":
return c.EnableOrDisableModel(cmd, "disable")
case "chat_to_model":
return c.ChatToModel(cmd)
case "use_model":
return c.UseModel(cmd)
case "show_current_model":
return c.ShowCurrentModel(cmd)
// ContextEngine commands
case "ce_ls":
return c.CEList(cmd)
case "ce_search":
return c.CESearch(cmd)
case "insert_dataset_from_file":
return c.InsertDatasetFromFile(cmd)
case "insert_metadata_from_file":
return c.InsertMetadataFromFile(cmd)
// TODO: Implement other commands
default:
return nil, fmt.Errorf("command '%s' would be executed with API", cmd.Type)
}
}
// ShowCurrentUser shows the current logged-in user information
// TODO: Implement showing current user information when API is available
func (c *RAGFlowClient) ShowCurrentUser(cmd *Command) (map[string]interface{}, error) {
// TODO: Call the appropriate API to get current user information
// Currently there is no /admin/user/info or /user/info API available
// The /admin/auth API only verifies authorization, does not return user info
return nil, fmt.Errorf("command 'SHOW CURRENT USER' is not yet implemented")
}