2026-03-04 19:17:16 +08:00
|
|
|
//
|
|
|
|
|
// 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"
|
2026-04-30 12:36:03 +08:00
|
|
|
"io"
|
|
|
|
|
|
|
|
|
|
ce "ragflow/internal/cli/filesystem"
|
2026-03-04 19:17:16 +08:00
|
|
|
)
|
|
|
|
|
|
2026-03-11 19:14:18 +08:00
|
|
|
// PasswordPromptFunc is a function type for password input
|
|
|
|
|
type PasswordPromptFunc func(prompt string) (string, error)
|
|
|
|
|
|
2026-04-02 20:20:35 +08:00
|
|
|
// CurrentModel holds the current model configuration
|
|
|
|
|
type CurrentModel struct {
|
|
|
|
|
Provider string
|
|
|
|
|
Instance string
|
|
|
|
|
Model string
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 19:17:16 +08:00
|
|
|
// RAGFlowClient handles API interactions with the RAGFlow server
|
|
|
|
|
type RAGFlowClient struct {
|
2026-03-17 16:45:45 +08:00
|
|
|
HTTPClient *HTTPClient
|
|
|
|
|
ServerType string // "admin" or "user"
|
|
|
|
|
PasswordPrompt PasswordPromptFunc // Function for password input
|
2026-03-26 21:07:06 +08:00
|
|
|
OutputFormat OutputFormat // Output format: table, plain, json
|
|
|
|
|
ContextEngine *ce.Engine // Context Engine for virtual filesystem
|
2026-04-02 20:20:35 +08:00
|
|
|
CurrentModel *CurrentModel // Current model configuration
|
2026-03-04 19:17:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewRAGFlowClient(serverType string) *RAGFlowClient {
|
2026-03-12 13:33:13 +08:00
|
|
|
httpClient := NewHTTPClient()
|
|
|
|
|
// Set port from configuration file based on server type
|
|
|
|
|
if serverType == "admin" {
|
|
|
|
|
httpClient.Port = 9381
|
|
|
|
|
} else {
|
|
|
|
|
httpClient.Port = 9380
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 21:07:06 +08:00
|
|
|
client := &RAGFlowClient{
|
2026-03-12 13:33:13 +08:00
|
|
|
HTTPClient: httpClient,
|
2026-03-04 19:17:16 +08:00
|
|
|
ServerType: serverType,
|
|
|
|
|
}
|
2026-03-26 21:07:06 +08:00
|
|
|
|
|
|
|
|
// 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}))
|
2026-04-30 12:36:03 +08:00
|
|
|
engine.RegisterProvider(ce.NewFileProvider(&httpClientAdapter{c.HTTPClient}))
|
|
|
|
|
engine.RegisterProvider(ce.NewSkillProvider(&httpClientAdapter{c.HTTPClient}))
|
2026-03-26 21:07:06 +08:00
|
|
|
|
|
|
|
|
c.ContextEngine = engine
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// httpClientAdapter adapts HTTPClient to ce.HTTPClientInterface
|
|
|
|
|
type httpClientAdapter struct {
|
|
|
|
|
client *HTTPClient
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 13:56:19 +08:00
|
|
|
func (a *httpClientAdapter) Request(method, path string, authKind string, headers map[string]string, jsonBody map[string]interface{}) (*ce.HTTPResponse, error) {
|
2026-03-26 21:07:06 +08:00
|
|
|
// Auto-detect auth kind based on available tokens
|
|
|
|
|
// If authKind is "auto" or empty, determine based on token availability
|
|
|
|
|
if authKind == "auto" || authKind == "" {
|
2026-06-09 15:22:50 +08:00
|
|
|
if a.client.useAPIToken && a.client.APIToken != nil {
|
2026-03-26 21:07:06 +08:00
|
|
|
authKind = "api"
|
2026-06-09 15:22:50 +08:00
|
|
|
} else if a.client.LoginToken != nil {
|
2026-03-26 21:07:06 +08:00
|
|
|
authKind = "web"
|
|
|
|
|
} else {
|
|
|
|
|
authKind = "web" // default
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-08 13:56:19 +08:00
|
|
|
resp, err := a.client.Request(method, path, authKind, headers, jsonBody)
|
2026-03-26 21:07:06 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &ce.HTTPResponse{
|
|
|
|
|
StatusCode: resp.StatusCode,
|
|
|
|
|
Body: resp.Body,
|
|
|
|
|
Headers: resp.Headers,
|
|
|
|
|
Duration: resp.Duration,
|
|
|
|
|
}, nil
|
2026-03-04 19:17:16 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-30 12:36:03 +08:00
|
|
|
func (a *httpClientAdapter) UploadMultipart(path string, contentType string, body io.Reader) error {
|
|
|
|
|
return a.client.UploadMultipart(path, contentType, body)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 13:33:13 +08:00
|
|
|
// 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")
|
|
|
|
|
}
|