Go CLI: admin model framework (#16252)

This commit is contained in:
Jin Hai
2026-06-23 16:57:05 +08:00
committed by GitHub
parent 2362210caf
commit dec2ce4a60
12 changed files with 1279 additions and 355 deletions

View File

@@ -103,7 +103,7 @@ func (p *Parser) parseAdminListCommands() (*Command, error) {
case TokenAvailable:
return p.parseListAvailableProviders()
case TokenProvider:
return p.parseAdminListProviderModels()
return p.parseAdminListProviderCommands()
case TokenProviders:
return p.parseAdminListProviders()
case TokenModels:
@@ -244,7 +244,7 @@ func (p *Parser) parseAdminListIngestionTasks() (*Command, error) {
}
// LIST PROVIDER 'provider_name' MODELS;
func (p *Parser) parseAdminListProviderModels() (*Command, error) {
func (p *Parser) parseAdminListProviderCommands() (*Command, error) {
p.nextToken() // consume PROVIDER
providerName, err := p.parseQuotedString()
@@ -253,10 +253,59 @@ func (p *Parser) parseAdminListProviderModels() (*Command, error) {
}
p.nextToken()
switch p.curToken.Type {
case TokenInstances:
return p.parseAdminListProviderInstances(providerName)
case TokenInstance:
return p.parseAdminListProviderInstance(providerName)
case TokenModels:
return p.parseAdminListProviderModels(providerName)
default:
return nil, fmt.Errorf("unknown LIST target: %s", p.curToken.Value)
}
}
// LIST PROVIDER 'provider_name' INSTANCE 'instance_name' MODELS;
func (p *Parser) parseAdminListProviderInstance(providerName string) (*Command, error) {
p.nextToken() // consume INSTANCE
instanceName, err := p.parseQuotedString()
if err != nil {
return nil, err
}
p.nextToken()
if p.curToken.Type != TokenModels {
return nil, fmt.Errorf("expected MODELS")
}
p.nextToken()
cmd := NewCommand("admin_list_provider_instance_models")
cmd.Params["provider_name"] = providerName
cmd.Params["instance_name"] = instanceName
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
// LIST PROVIDER 'provider_name' INSTANCES;
func (p *Parser) parseAdminListProviderInstances(providerName string) (*Command, error) {
p.nextToken() // consume INSTANCES
cmd := NewCommand("admin_list_provider_instances")
cmd.Params["provider_name"] = providerName
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
func (p *Parser) parseAdminListProviderModels(providerName string) (*Command, error) {
p.nextToken() // consume MODELS
cmd := NewCommand("admin_list_provider_models")
cmd.Params["provider_name"] = providerName
@@ -266,7 +315,7 @@ func (p *Parser) parseAdminListProviderModels() (*Command, error) {
return cmd, nil
}
// parseAdminListProviders parses LIST PROVIDERS command
// LIST PROVIDERS
func (p *Parser) parseAdminListProviders() (*Command, error) {
p.nextToken() // consume PROVIDERS
// Semicolon is optional
@@ -657,13 +706,12 @@ func (p *Parser) parseAdminShowProvider() (*Command, error) {
}
p.nextToken()
if p.curToken.Type == TokenModel {
// SHOW PROVIDER 'provider_name' MODEL 'model_name'
switch p.curToken.Type {
case TokenInstance:
return p.parseAdminShowProviderInstance(providerName)
case TokenModel:
return p.parseAdminShowProviderModel(providerName)
}
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
default:
p.nextToken()
}
@@ -672,6 +720,46 @@ func (p *Parser) parseAdminShowProvider() (*Command, error) {
return cmd, nil
}
// SHOW PROVIDER 'provider_name' INSTANCE 'instance_name';
func (p *Parser) parseAdminShowProviderInstance(providerName string) (*Command, error) {
p.nextToken() // consume INSTANCE
instanceName, err := p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected instance name: %w", err)
}
p.nextToken() // consume instance_name
if p.curToken.Type == TokenBalance {
return p.parseAdminShowProviderInstanceBalance(providerName, instanceName)
}
cmd := NewCommand("admin_show_provider_instance")
cmd.Params["instance_name"] = instanceName
cmd.Params["provider_name"] = providerName
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
// SHOW PROVIDER 'provider_name' INSTANCE 'instance_name' BALANCE;
func (p *Parser) parseAdminShowProviderInstanceBalance(providerName, instanceName string) (*Command, error) {
p.nextToken() // consume BALANCE
cmd := NewCommand("admin_show_provider_instance_balance")
cmd.Params["instance_name"] = instanceName
cmd.Params["provider_name"] = providerName
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
// SHOW PROVIDER 'provider_name' MODEL 'model_name';
func (p *Parser) parseAdminShowProviderModel(providerName string) (*Command, error) {
p.nextToken() // consume MODEL
@@ -760,17 +848,22 @@ func (p *Parser) parseCommonShowPoolModel() (*Command, error) {
// endregion SHOW commands
// parseAdminCheck
// CHECK LICENSE
// CHECK PROVIDER 'provider_name' REGION 'region_name' KEY 'api_key' [URL 'base_url'];
// CHECK PROVIDER 'provider_name' INSTANCE 'instance_name';
func (p *Parser) parseAdminCheck() (*Command, error) {
p.nextToken() // consume CHECK
switch p.curToken.Type {
case TokenLicense:
return p.parseAdminCheckLicense()
case TokenProvider:
return p.parseAdminCheckProvider()
default:
return nil, fmt.Errorf("unknown CHECK target: %s", p.curToken.Value)
}
}
// CHECK LICENSE;
func (p *Parser) parseAdminCheckLicense() (*Command, error) {
p.nextToken() // consume LICENSE
cmd := NewCommand("admin_check_license")
@@ -782,6 +875,90 @@ func (p *Parser) parseAdminCheckLicense() (*Command, error) {
return cmd, nil
}
// CHECK PROVIDER 'provider_name' REGION 'region_name' KEY 'api_key' [URL 'base_url'];
// CHECK PROVIDER 'provider_name' INSTANCE 'instance_name';
func (p *Parser) parseAdminCheckProvider() (*Command, error) {
if p.curToken.Type != TokenProvider {
return nil, fmt.Errorf("expected PROVIDER after CHECK")
}
p.nextToken()
if p.curToken.Type != TokenQuotedString {
return nil, fmt.Errorf("expected provider name after PROVIDER")
}
providerName := p.curToken.Value
p.nextToken()
if p.curToken.Type == TokenInstance {
return p.parseAdminCheckProviderInstance(providerName)
}
if p.curToken.Type != TokenRegion {
return nil, fmt.Errorf("expected REGION after provider name")
}
p.nextToken()
if p.curToken.Type != TokenQuotedString {
return nil, fmt.Errorf("expected region name after REGION")
}
regionName := p.curToken.Value
p.nextToken()
if p.curToken.Type != TokenKey {
return nil, fmt.Errorf("expected KEY after region name")
}
p.nextToken()
if p.curToken.Type != TokenQuotedString {
return nil, fmt.Errorf("expected API key after KEY")
}
apiKey := p.curToken.Value
p.nextToken()
baseURL := ""
if p.curToken.Type == TokenURL {
p.nextToken()
if p.curToken.Type != TokenQuotedString {
return nil, fmt.Errorf("expected base URL after URL")
}
baseURL = p.curToken.Value
p.nextToken()
}
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
if p.curToken.Type != TokenEOF {
return nil, fmt.Errorf("unexpected token: %s", p.curToken.Value)
}
cmd := NewCommand("admin_check_provider_with_key")
cmd.Params["provider_name"] = providerName
cmd.Params["region"] = regionName
cmd.Params["api_key"] = apiKey
if baseURL != "" {
cmd.Params["base_url"] = baseURL
}
return cmd, nil
}
func (p *Parser) parseAdminCheckProviderInstance(providerName string) (*Command, error) {
if p.curToken.Type != TokenInstance {
return nil, fmt.Errorf("expected PROVIDER after CHECK")
}
p.nextToken()
instanceName, err := p.parseQuotedString()
if err != nil {
return nil, err
}
cmd := NewCommand("admin_check_provider_instance")
cmd.Params["provider_name"] = providerName
cmd.Params["instance_name"] = instanceName
return cmd, nil
}
// STOP INGESTION TASKS 'task_id1 task_id2';
func (p *Parser) parseAdminStopIngestionTasks() (*Command, error) {
p.nextToken() // consume STOP
@@ -1067,6 +1244,8 @@ func (p *Parser) parseAdminAlterCommands() (*Command, error) {
return p.parseAdminAlterUser()
case TokenRole:
return p.parseAdminAlterRole()
case TokenProvider:
return p.parseAdminAlterProvider()
default:
return nil, fmt.Errorf("unknown ALTER target: %s", p.curToken.Value)
}
@@ -1199,6 +1378,67 @@ func (p *Parser) parseAdminAlterRole() (*Command, error) {
return cmd, nil
}
func (p *Parser) parseAdminAlterProvider() (*Command, error) {
p.nextToken() // consume PROVIDER
providerName, err := p.parseQuotedString()
if err != nil {
return nil, err
}
p.nextToken()
if p.curToken.Type != TokenInstance {
return nil, fmt.Errorf("expected INSTANCE")
}
p.nextToken()
instanceName, err := p.parseQuotedString()
if err != nil {
return nil, err
}
p.nextToken()
newModelName := ""
newAPIKey := ""
optionsLoop:
for {
switch p.curToken.Type {
case TokenName:
p.nextToken()
newModelName, err = p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected model name: %w", err)
}
p.nextToken()
case TokenKey:
p.nextToken()
newAPIKey, err = p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected API key: %w", err)
}
p.nextToken()
default:
break optionsLoop
}
}
if newModelName == "" && newAPIKey == "" {
return nil, fmt.Errorf("expected NAME or KEY after INSTANCE")
}
cmd := NewCommand("admin_alter_provider_instance")
cmd.Params["provider_name"] = providerName
cmd.Params["instance_name"] = instanceName
cmd.Params["new_model_name"] = newModelName
cmd.Params["new_api_key"] = newAPIKey
p.nextToken()
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
// endregion ALTER commands
func (p *Parser) parseAdminGrantCommands() (*Command, error) {
@@ -1896,6 +2136,112 @@ func (p *Parser) parseAdminDeleteModel(providerName, instanceName string) (*Comm
return cmd, nil
}
func (p *Parser) parseAdminEnableCommand() (*Command, error) {
p.nextToken() // consume ENABLE
switch p.curToken.Type {
case TokenProvider:
return p.parseAdminEnableModel()
default:
return nil, fmt.Errorf("unknown ENABLE target: %s", p.curToken.Value)
}
}
func (p *Parser) parseAdminEnableModel() (*Command, error) {
p.nextToken() // consume PROVIDER
providerName, err := p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected provider name: %w", err)
}
p.nextToken()
if p.curToken.Type != TokenInstance {
return nil, fmt.Errorf("expected INSTANCE")
}
p.nextToken()
instanceName, err := p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected model instance name: %w", err)
}
p.nextToken()
if p.curToken.Type != TokenModel {
return nil, fmt.Errorf("expected MODEL")
}
p.nextToken()
modelName, err := p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected model name: %w", err)
}
p.nextToken()
cmd := NewCommand("admin_enable_model")
cmd.Params["provider_name"] = providerName
cmd.Params["instance_name"] = instanceName
cmd.Params["model_name"] = modelName
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
func (p *Parser) parseAdminDisableCommand() (*Command, error) {
p.nextToken() // consume DISABLE
switch p.curToken.Type {
case TokenProvider:
return p.parseAdminDisableModel()
default:
return nil, fmt.Errorf("unknown DISABLE target: %s", p.curToken.Value)
}
}
func (p *Parser) parseAdminDisableModel() (*Command, error) {
p.nextToken() // consume PROVIDER
providerName, err := p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected provider name: %w", err)
}
p.nextToken()
if p.curToken.Type != TokenInstance {
return nil, fmt.Errorf("expected INSTANCE")
}
p.nextToken()
instanceName, err := p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected model instance name: %w", err)
}
p.nextToken()
if p.curToken.Type != TokenModel {
return nil, fmt.Errorf("expected MODEL")
}
p.nextToken()
modelName, err := p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected model name: %w", err)
}
p.nextToken()
cmd := NewCommand("admin_disable_model")
cmd.Params["provider_name"] = providerName
cmd.Params["instance_name"] = instanceName
cmd.Params["model_name"] = modelName
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
func (p *Parser) parseAdminSaveCommand() (*Command, error) {
p.nextToken() // consume SAVE
switch p.curToken.Type {

View File

@@ -75,6 +75,8 @@ func (c *CLI) ExecuteAdminCommand(cmd *Command) (ResponseIf, error) {
return c.AdminAlterUserPassword(cmd)
case "admin_alter_role":
return c.AdminAlterRole(cmd)
case "admin_alter_provider_instance":
return c.CommonAlterProviderInstanceCommand(cmd)
case "admin_drop_user":
return c.AdminDropUserCommand(cmd)
case "admin_drop_user_api_key":
@@ -115,14 +117,20 @@ func (c *CLI) ExecuteAdminCommand(cmd *Command) (ResponseIf, error) {
return c.CommonAvailableProvidersCommand(cmd)
case "admin_show_provider":
return c.CommonShowProviderCommand(cmd)
case "admin_show_provider_instance":
return c.CommonShowProviderInstanceCommand(cmd)
case "admin_show_provider_instance_balance":
return c.CommonShowProviderInstanceBalanceCommand(cmd)
case "admin_show_provider_model":
return c.CommonShowProviderModelCommand(cmd)
case "admin_list_provider_models":
return c.CommonListModelsCommand(cmd)
case "admin_list_provider_instance_models":
return c.CommonListInstanceModels(cmd)
case "admin_list_provider_instances":
return c.CommonListProviderInstances(cmd)
case "list_supported_models":
return c.ListSupportedModels(cmd)
case "list_instance_models":
return c.ListInstanceModels(cmd)
case "admin_show_model":
return c.CommonShowModel(cmd)
case "admin_list_providers":
@@ -151,6 +159,10 @@ func (c *CLI) ExecuteAdminCommand(cmd *Command) (ResponseIf, error) {
return c.UserShowMessageQueueCommand(cmd)
case "admin_check_license":
return c.AdminCheckLicenseCommand(cmd)
case "admin_check_provider_with_key":
return c.CommonCheckProviderWithKey(cmd)
case "admin_check_provider_instance":
return c.CommonCheckProviderConnection(cmd)
case "admin_show_fingerprint":
return c.AdminShowFingerprintCommand(cmd)
case "admin_show_license":
@@ -241,6 +253,10 @@ func (c *CLI) ExecuteAdminCommand(cmd *Command) (ResponseIf, error) {
return c.AdminDeleteInstancesCommand(cmd)
case "admin_delete_model":
return c.AdminDeleteModelsCommand(cmd)
case "admin_enable_model":
return c.CommonEnableOrDisableModel(cmd, "enable")
case "admin_disable_model":
return c.CommonEnableOrDisableModel(cmd, "disable")
// TODO: Implement other commands
case "show_admin_server":
return c.ShowAdminServer(cmd)
@@ -317,7 +333,7 @@ func (c *CLI) ExecuteUserCommand(cmd *Command) (ResponseIf, error) {
case "list_supported_models":
return c.ListSupportedModels(cmd)
case "list_instance_models":
return c.ListInstanceModels(cmd)
return c.CommonListInstanceModels(cmd)
case "show_provider_model":
return c.CommonShowProviderModelCommand(cmd)
case "show_model":
@@ -335,21 +351,21 @@ func (c *CLI) ExecuteUserCommand(cmd *Command) (ResponseIf, error) {
case "create_provider_instance":
return c.CreateProviderInstance(cmd)
case "list_provider_instances":
return c.ListProviderInstances(cmd)
return c.CommonListProviderInstances(cmd)
case "show_provider_instance":
return c.ShowProviderInstance(cmd)
return c.CommonShowProviderInstanceCommand(cmd)
case "show_instance_balance":
return c.ShowInstanceBalance(cmd)
case "alter_provider_instance":
return c.AlterProviderInstance(cmd)
return c.CommonAlterProviderInstanceCommand(cmd)
case "drop_provider_instance":
return c.DropProviderInstance(cmd)
case "drop_instance_model":
return c.DropInstanceModel(cmd)
case "enable_model":
return c.EnableOrDisableModel(cmd, "enable")
return c.CommonEnableOrDisableModel(cmd, "enable")
case "disable_model":
return c.EnableOrDisableModel(cmd, "disable")
return c.CommonEnableOrDisableModel(cmd, "disable")
case "add_custom_model":
return c.AddCustomModel(cmd)
case "chat_to_model":
@@ -374,9 +390,9 @@ func (c *CLI) ExecuteUserCommand(cmd *Command) (ResponseIf, error) {
case "parse_file_user_command":
return c.ParseFileUserCommand(cmd)
case "check_provider_connection":
return c.CheckProviderConnection(cmd)
return c.CommonCheckProviderConnection(cmd)
case "check_provider_with_key":
return c.CheckProviderWithKey(cmd)
return c.CommonCheckProviderWithKey(cmd)
case "use_model":
return c.UseModel(cmd)
case "use_api_server":

View File

@@ -323,6 +323,190 @@ func (c *CLI) CommonShowProviderCommand(cmd *Command) (ResponseIf, error) {
return &result, nil
}
// CommonShowProviderInstanceCommand shows details of a specific instance
func (c *CLI) CommonShowProviderInstanceCommand(cmd *Command) (ResponseIf, error) {
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance name not provided")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider name not provided")
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s", providerName, instanceName)
resp, err = c.AdminServerClient.Request("GET", endPoint, "web", nil, nil)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/instances/%s", providerName, instanceName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("GET", endPoint, "web", nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to show instance: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to show instance: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("failed to show instance: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// CommonShowProviderInstanceBalanceCommand shows balance of a specific instance
func (c *CLI) CommonShowProviderInstanceBalanceCommand(cmd *Command) (ResponseIf, error) {
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance name not provided")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider name not provided")
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s/balance", providerName, instanceName)
resp, err = c.AdminServerClient.Request("GET", endPoint, "web", nil, nil)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/instances/%s/balance", providerName, instanceName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("GET", endPoint, "web", nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to show instance balance: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to show instance balance: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("failed to show instance balance: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// CommonListProviderInstances lists all instances of a provider
// LIST INSTANCES FROM PROVIDER <name>
func (c *CLI) CommonListProviderInstances(cmd *Command) (ResponseIf, error) {
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider_name not provided")
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/instances", providerName)
resp, err = c.AdminServerClient.Request("GET", endPoint, "web", nil, nil)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/instances", providerName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("GET", endPoint, "web", nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to list instances: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list instances: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("list instances failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
func (c *CLI) CommonListInstanceModels(cmd *Command) (ResponseIf, error) {
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider_name not provided")
}
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance_name not provided")
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s/models", providerName, instanceName)
resp, err = c.AdminServerClient.Request("GET", endPoint, "web", nil, nil)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/instances/%s/models", providerName, instanceName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("GET", endPoint, "web", nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to list instance models: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list instance models: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("failed to list instance models: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
func (c *CLI) CommonListModelsCommand(cmd *Command) (ResponseIf, error) {
providerName, ok := cmd.Params["provider_name"].(string)
@@ -452,6 +636,284 @@ func (c *CLI) CommonShowProviderModelCommand(cmd *Command) (ResponseIf, error) {
return &result, nil
}
func (c *CLI) CommonCheckProviderWithKey(cmd *Command) (ResponseIf, error) {
providerName, ok := cmd.Params["provider_name"].(string)
if !ok || providerName == "" {
return nil, fmt.Errorf("provider name not provided")
}
region, ok := cmd.Params["region"].(string)
if !ok || region == "" {
return nil, fmt.Errorf("region not provided")
}
apiKey, ok := cmd.Params["api_key"].(string)
if !ok {
return nil, fmt.Errorf("api_key not provided")
}
baseURL, _ := cmd.Params["base_url"].(string)
var apiKeyValue interface{}
if apiKey != "" {
apiKeyValue = apiKey
} else {
apiKeyValue = nil
}
payload := map[string]interface{}{
"region": region,
"api_key": apiKeyValue,
}
if baseURL != "" {
payload["base_url"] = baseURL
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/connection", providerName)
resp, err = c.AdminServerClient.Request("POST", endPoint, "web", nil, payload)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/connection", providerName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("POST", endPoint, "web", nil, payload)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to check provider connection with key: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to check provider connection: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
switch c.Config.CLIMode {
case AdminMode:
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
case APIMode:
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
default:
return nil, fmt.Errorf("invalid server type")
}
}
func (c *CLI) CommonCheckProviderConnection(cmd *Command) (ResponseIf, error) {
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance name not provided")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider name not provided")
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s/connection", providerName, instanceName)
resp, err = c.AdminServerClient.Request("POST", endPoint, "web", nil, nil)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/instances/%s/connection", providerName, instanceName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("POST", endPoint, "web", nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to check provider connection: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to check provider connection: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
switch c.Config.CLIMode {
case AdminMode:
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
case APIMode:
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
default:
return nil, fmt.Errorf("invalid server type")
}
}
// AlterProviderInstanceCommand alters a provider instance
func (c *CLI) CommonAlterProviderInstanceCommand(cmd *Command) (ResponseIf, error) {
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider name not provided")
}
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance name not provided")
}
payload := map[string]interface{}{}
newName, ok := cmd.Params["new_model_name"].(string)
if ok {
payload["model_name"] = newName
}
newAPIKey, ok := cmd.Params["new_api_key"].(string)
if ok {
payload["api_key"] = newAPIKey
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s", providerName, instanceName)
resp, err = c.AdminServerClient.Request("PUT", endPoint, "web", nil, payload)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/instances/%s", providerName, instanceName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("PUT", endPoint, "web", nil, payload)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to alter instance: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to alter instance: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
switch c.Config.CLIMode {
case AdminMode:
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
case APIMode:
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
default:
return nil, fmt.Errorf("invalid server type")
}
}
func (c *CLI) CommonEnableOrDisableModel(cmd *Command, status string) (ResponseIf, error) {
modelName, ok := cmd.Params["model_name"].(string)
if !ok {
return nil, fmt.Errorf("model name not provided")
}
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance name not provided")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider name not provided")
}
payload := map[string]interface{}{
"status": status,
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s/models/%s", providerName, instanceName, modelName)
resp, err = c.AdminServerClient.Request("PATCH", endPoint, "web", nil, payload)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/instances/%s/models/%s", providerName, instanceName, modelName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("PATCH", endPoint, "web", nil, payload)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to enable/disable model: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to enable/disable model: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
switch c.Config.CLIMode {
case AdminMode:
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
case APIMode:
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
default:
return nil, fmt.Errorf("invalid server type")
}
}
func (c *CLI) SetDefaultModel(cmd *Command) (ResponseIf, error) {
modelType, ok := cmd.Params["model_type"].(string)

View File

@@ -124,6 +124,10 @@ func (p *Parser) parseAdminCommand() (*Command, error) {
return p.parseAdminAddCommand()
case TokenDelete:
return p.parseAdminDeleteCommands()
case TokenEnable:
return p.parseAdminEnableCommand()
case TokenDisable:
return p.parseAdminDisableCommand()
case TokenSave:
return p.parseAdminSaveCommand()
case TokenUse:

View File

@@ -1275,83 +1275,6 @@ func (c *CLI) CreateProviderInstance(cmd *Command) (ResponseIf, error) {
return &result, nil
}
// ListProviderInstances lists all instances of a provider
// LIST INSTANCES FROM PROVIDER <name>
func (c *CLI) ListProviderInstances(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != APIMode {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider name not provided")
}
url := fmt.Sprintf("/providers/%s/instances", providerName)
resp, err := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("GET", url, "web", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list instances: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list instances: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("list instances failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// ShowProviderInstance shows details of a specific instance
// SHOW INSTANCE <name> FROM PROVIDER <name>
func (c *CLI) ShowProviderInstance(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != APIMode {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance name not provided")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider name not provided")
}
url := fmt.Sprintf("/providers/%s/instances/%s", providerName, instanceName)
resp, err := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("GET", url, "web", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to show instance: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to show instance: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("show instance failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// ShowInstanceBalance shows balance of a specific instance
// SHOW BALANCE FROM PROVIDER <provider_name> <instance_name>
func (c *CLI) ShowInstanceBalance(cmd *Command) (ResponseIf, error) {
@@ -1393,56 +1316,6 @@ func (c *CLI) ShowInstanceBalance(cmd *Command) (ResponseIf, error) {
return &result, nil
}
// AlterProviderInstance renames a provider instance
// ALTER INSTANCE <name> NAME <new_name> FROM PROVIDER <name>
func (c *CLI) AlterProviderInstance(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != APIMode {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance name not provided")
}
newName, ok := cmd.Params["new_name"].(string)
if !ok {
return nil, fmt.Errorf("new name not provided")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider name not provided")
}
url := fmt.Sprintf("/providers/%s/instances/%s", providerName, instanceName)
payload := map[string]interface{}{
"llm_name": newName,
}
resp, err := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("PUT", url, "web", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to alter instance: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to alter instance: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("alter instance failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// DropProviderInstance deletes a provider instance
// DROP INSTANCE <name> FROM PROVIDER <name>
func (c *CLI) DropProviderInstance(cmd *Command) (ResponseIf, error) {
@@ -1538,87 +1411,6 @@ func (c *CLI) DropInstanceModel(cmd *Command) (ResponseIf, error) {
return &result, nil
}
func (c *CLI) ListInstanceModels(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != APIMode {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider_name not provided")
}
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance_name not provided")
}
var endPoint string
endPoint = fmt.Sprintf("/providers/%s/instances/%s/models", providerName, instanceName)
resp, err := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("GET", endPoint, "web", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list instance models: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list instance models: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("failed to list instance models: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
func (c *CLI) EnableOrDisableModel(cmd *Command, status string) (ResponseIf, error) {
if c.Config.CLIMode != APIMode {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
modelName, ok := cmd.Params["model_name"].(string)
if !ok {
return nil, fmt.Errorf("model name not provided")
}
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance name not provided")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider name not provided")
}
url := fmt.Sprintf("/providers/%s/instances/%s/models/%s", providerName, instanceName, modelName)
payload := map[string]interface{}{
"status": status,
}
resp, err := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("PATCH", url, "web", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to enable/disable model: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to enable/disable model: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("enable/disable model failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
func isValidURL(str string) bool {
u, err := netUrl.Parse(str)
if err != nil {
@@ -2668,103 +2460,6 @@ func (c *CLI) ShowTaskUserCommand(cmd *Command) (ResponseIf, error) {
return &result, nil
}
func (c *CLI) CheckProviderConnection(cmd *Command) (ResponseIf, error) {
if c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].APIToken == nil && c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].LoginToken == nil {
return nil, fmt.Errorf("API token not set. Please login first")
}
if c.Config.CLIMode != APIMode {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance name not provided")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider name not provided")
}
url := fmt.Sprintf("/providers/%s/instances/%s/connection", providerName, instanceName)
resp, err := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("GET", url, "web", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to check provider connection: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to check provider connection: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
func (c *CLI) CheckProviderWithKey(cmd *Command) (ResponseIf, error) {
if c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].APIToken == nil && c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].LoginToken == nil {
return nil, fmt.Errorf("API token not set. Please login first")
}
if c.Config.CLIMode != APIMode {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok || providerName == "" {
return nil, fmt.Errorf("provider name not provided")
}
region, ok := cmd.Params["region"].(string)
if !ok || region == "" {
return nil, fmt.Errorf("region not provided")
}
apiKey, ok := cmd.Params["api_key"].(string)
if !ok {
return nil, fmt.Errorf("api_key not provided")
}
baseURL, _ := cmd.Params["base_url"].(string)
var apiKeyValue interface{}
if apiKey != "" {
apiKeyValue = apiKey
} else {
apiKeyValue = nil
}
url := fmt.Sprintf("/providers/%s/connection", providerName)
payload := map[string]interface{}{
"region": region,
"api_key": apiKeyValue,
}
if baseURL != "" {
payload["base_url"] = baseURL
}
resp, err := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("GET", url, "api", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to check provider connection with key: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to check provider connection: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// UseModel sets the current model for chat
func (c *CLI) UseModel(cmd *Command) (ResponseIf, error) {
if c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].APIToken == nil && c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].LoginToken == nil {

View File

@@ -1710,19 +1710,36 @@ func (p *Parser) parseAlterInstance() (*Command, error) {
if err != nil {
return nil, fmt.Errorf("expected instance name: %w", err)
}
p.nextToken()
if p.curToken.Type != TokenName {
return nil, fmt.Errorf("expected NAME")
}
p.nextToken()
newName, err := p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected new instance name: %w", err)
newModelName := ""
newAPIKey := ""
optionsLoop:
for {
switch p.curToken.Type {
case TokenName:
p.nextToken()
newModelName, err = p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected model name: %w", err)
}
p.nextToken()
case TokenKey:
p.nextToken()
newAPIKey, err = p.parseQuotedString()
if err != nil {
return nil, fmt.Errorf("expected API key: %w", err)
}
p.nextToken()
default:
break optionsLoop
}
}
if newModelName == "" && newAPIKey == "" {
return nil, fmt.Errorf("expected NAME or KEY after INSTANCE")
}
p.nextToken()
if p.curToken.Type != TokenFrom {
return nil, fmt.Errorf("expected FROM")
}
@@ -1739,9 +1756,10 @@ func (p *Parser) parseAlterInstance() (*Command, error) {
}
cmd := NewCommand("alter_provider_instance")
cmd.Params["instance_name"] = instanceName
cmd.Params["new_name"] = newName
cmd.Params["provider_name"] = providerName
cmd.Params["instance_name"] = instanceName
cmd.Params["new_model_name"] = newModelName
cmd.Params["new_api_key"] = newAPIKey
p.nextToken()
// Semicolon is optional
@@ -2543,8 +2561,9 @@ func (p *Parser) parseListModelsOfProvider() (*Command, error) {
// If so, format is: LIST MODELS FROM <instance_name> <provider_name>
// If not, format is: LIST MODELS FROM <provider_name>
if p.curToken.Type == TokenQuotedString {
var instanceName string
// Two arguments: instance_name and provider_name
instanceName, err := p.parseQuotedString()
instanceName, err = p.parseQuotedString()
if err != nil {
return nil, err
}