diff --git a/internal/cli/admin_command.go b/internal/cli/admin_command.go index 52f557462d..7b23525226 100644 --- a/internal/cli/admin_command.go +++ b/internal/cli/admin_command.go @@ -44,16 +44,7 @@ func (c *CLI) PingAdmin(cmd *Command) (ResponseIf, error) { return nil, err } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to ping: 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("list users failed: invalid JSON (%w)", err) - } - result.Duration = resp.Duration - return &result, nil + return HandleSimpleResponse(resp, "ping admin") } // AdminShowVersionCommand show RAGFlow admin version @@ -63,20 +54,7 @@ func (c *CLI) AdminShowVersionCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to show admin version: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to show admin version: 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 admin version failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("show admin version")) } // AdminListResourcesCommand to list resources command (admin mode only) @@ -90,21 +68,7 @@ func (c *CLI) AdminListResourcesCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list resources: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list resources: 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("list resources failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("list resources")) } // AdminListRolesCommand to list roles command (admin mode only) @@ -163,21 +127,7 @@ func (c *CLI) AdminListProvidersCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list providers: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list providers: 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 providers failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "list providers") } // AdminCreateRoleCommand creates a new role (admin mode only) @@ -204,21 +154,7 @@ func (c *CLI) AdminCreateRoleCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to create role: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to create role: 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("create role failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("create role")) } // AdminDropRoleCommand deletes the role (admin mode only) @@ -237,21 +173,7 @@ func (c *CLI) AdminDropRoleCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to drop role: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to drop role: 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("drop role failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("drop role")) } // AdminAlterRole alters the role rights (admin mode only) @@ -278,21 +200,7 @@ func (c *CLI) AdminAlterRole(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to alter role: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to alter role: 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("alter role failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("alter role")) } // AdminGrantUserAdminCommand grants admin privileges to a user (admin mode only) @@ -314,21 +222,7 @@ func (c *CLI) AdminGrantUserAdminCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to grant admin: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to grant admin: 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("grant admin failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleSimpleResponse(resp, "grant admin") } // AdminRevokeUserAdminCommand revokes admin privileges from a user (admin mode only) @@ -350,21 +244,7 @@ func (c *CLI) AdminRevokeUserAdminCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to revoke admin: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to revoke admin: 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("revoke admin failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleSimpleResponse(resp, "revoke admin") } // AdminGrantRolePermissionCommand grants permission to role (admin mode only) @@ -400,20 +280,7 @@ func (c *CLI) AdminGrantRolePermissionCommand(cmd *Command) (ResponseIf, error) return nil, fmt.Errorf("failed to grant permission to role: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to grant permission to role: 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("grant permission to role failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("grant permission to role")) } // AdminRevokeRolePermissionCommand revokes permission from role (admin mode only) @@ -449,21 +316,7 @@ func (c *CLI) AdminRevokeRolePermissionCommand(cmd *Command) (ResponseIf, error) return nil, fmt.Errorf("failed to revoke permission from role: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to revoke permission from role: 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("revoke permission from role failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("revoke permission from role")) } // AdminShowRolePermissionCommand shows admin privileges from a user (admin mode only) @@ -484,21 +337,7 @@ func (c *CLI) AdminShowRolePermissionCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to show role permission: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to show role permission: 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 role permission failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("show role permission")) } // AdminCreateUserCommand creates a new user (admin mode only) @@ -539,21 +378,7 @@ func (c *CLI) AdminCreateUserCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to create user: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to create user: 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("create user failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleSimpleResponse(resp, "create user") } // AdminCreateUserAPIKeyCommand creates a new user API key (admin mode only) @@ -630,21 +455,7 @@ func (c *CLI) AdminActivateUser(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to update user status: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to update user status: 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("update user status failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleSimpleResponse(resp, "update user status") } // AdminAlterUserPassword changes a user's password (admin mode only) @@ -686,21 +497,7 @@ func (c *CLI) AdminAlterUserPassword(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to change user password: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to change user password: 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("change user password failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleSimpleResponse(resp, "change user password") } type listServicesResponse struct { @@ -720,21 +517,7 @@ func (c *CLI) AdminListServicesCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list services: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list services: 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 services failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "list services") } // AdminStartServiceCommand starts a service (admin mode only) @@ -752,21 +535,7 @@ func (c *CLI) AdminStartServiceCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to start service: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to start service: 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("start service failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "start service") } // AdminRestartServiceCommand restarts a service (admin mode only) @@ -784,21 +553,7 @@ func (c *CLI) AdminRestartServiceCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to restart service: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to restart service: 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("restart service failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "restart service") } // AdminShutdownServiceCommand shuts down a service (admin mode only) @@ -816,21 +571,7 @@ func (c *CLI) AdminShutdownServiceCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to shutdown service: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to shutdown service: 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("shutdown service failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "shutdown service") } // AdminShowService show service (admin mode only) @@ -848,21 +589,7 @@ func (c *CLI) AdminShowService(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to show service: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to show service: 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 service failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "show service") } func normalizeVariableRows(rows []map[string]interface{}) { @@ -944,24 +671,10 @@ func (c *CLI) AdminListEnvironmentsCommand(cmd *Command) (ResponseIf, error) { resp, err := c.AdminServerClient.Request("GET", "/admin/environments", "admin", nil, nil) if err != nil { - return nil, fmt.Errorf("failed to list configs: %w", err) + return nil, fmt.Errorf("failed to list environments: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list configs: 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 configs failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "list environments") } // AdminShowVariable shows system variables by exact name or name prefix (admin mode only). @@ -1020,20 +733,7 @@ func (c *CLI) AdminSetLicenseCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to set license: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to set license: 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("set license failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "set license") } func (c *CLI) AdminSetLicenseConfigCommand(cmd *Command) (ResponseIf, error) { @@ -1059,20 +759,7 @@ func (c *CLI) AdminSetLicenseConfigCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to set license config: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to set license config: 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("set license config failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "set license config") } // AdminSetVariableCommand updates a system variable (admin mode only). @@ -1156,21 +843,7 @@ func (c *CLI) AdminSetRoleDefaultModelsCommand(cmd *Command) (ResponseIf, error) return nil, fmt.Errorf("failed to set role default models: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to set role default models: 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("set role default models failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "set role default models") } // AdminSetLogLevelCommand set log level (admin mode only). @@ -1193,17 +866,7 @@ func (c *CLI) AdminSetLogLevelCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to change log level: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to register user: 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("change log level failed: invalid JSON (%w)", err) - } - result.Code = 0 - result.Duration = resp.Duration - return &result, nil + return HandleSimpleResponse(resp, "change log level") } // AdminResetRoleDefaultModelsCommand reset role default models (admin mode only). @@ -1233,21 +896,7 @@ func (c *CLI) AdminResetRoleDefaultModelsCommand(cmd *Command) (ResponseIf, erro return nil, fmt.Errorf("failed to reset role default models: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to reset role default models: 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("reset role default models failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "reset role default models") } // AdminDropUserCommand deletes a user (admin mode only) @@ -1269,21 +918,7 @@ func (c *CLI) AdminDropUserCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to drop user: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to drop user: 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("drop user failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleSimpleResponse(resp, "drop user") } // AdminDropUserAPIKeyCommand drops an API key for a user (admin mode only) @@ -1310,21 +945,7 @@ func (c *CLI) AdminDropUserAPIKeyCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to drop API key: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to drop API key: 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("drop API key failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "drop API key") } // ListUserDatasets lists datasets for a specific user (admin mode) @@ -1433,24 +1054,10 @@ func (c *CLI) ListAdminTasks(cmd *Command) (ResponseIf, error) { resp, err := c.AdminServerClient.Request("GET", "/admin/tasks", "admin", nil, nil) if err != nil { - return nil, fmt.Errorf("failed to drop token: %w", err) + return nil, fmt.Errorf("failed to list tasks: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to drop token: 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("drop token failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "list tasks") } func (c *CLI) ListAdminIngestors(cmd *Command) (ResponseIf, error) { @@ -1462,21 +1069,7 @@ func (c *CLI) ListAdminIngestors(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list ingestors: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list ingestors: 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 ingestors failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "list ingestors") } func (c *CLI) ListAdminIngestionTasks(cmd *Command) (ResponseIf, error) { @@ -1489,21 +1082,7 @@ func (c *CLI) ListAdminIngestionTasks(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list admin tasks: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list admin tasks: 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 admin tasks failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "list admin tasks") } func (c *CLI) AdminStopIngestionCommand(cmd *Command) (ResponseIf, error) { @@ -1521,24 +1100,10 @@ func (c *CLI) AdminStopIngestionCommand(cmd *Command) (ResponseIf, error) { resp, err := c.AdminServerClient.Request("PUT", "/admin/ingestion/tasks", "admin", nil, payload) if err != nil { - return nil, fmt.Errorf("failed to ingest file: %w", err) + return nil, fmt.Errorf("failed to stop ingestion: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to ingest file: 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("ingest file failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "stop ingestion") } func (c *CLI) AdminRemoveIngestionCommand(cmd *Command) (ResponseIf, error) { @@ -1556,24 +1121,10 @@ func (c *CLI) AdminRemoveIngestionCommand(cmd *Command) (ResponseIf, error) { resp, err := c.AdminServerClient.Request("DELETE", "/admin/ingestion/tasks", "admin", nil, payload) if err != nil { - return nil, fmt.Errorf("failed to ingest file: %w", err) + return nil, fmt.Errorf("failed to remove ingestion: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to ingest file: 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("ingest file failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "remove ingestion") } func (c *CLI) AdminShutdownIngestor(cmd *Command) (ResponseIf, error) { @@ -1594,21 +1145,7 @@ func (c *CLI) AdminShutdownIngestor(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to shutdown ingestor: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to shutdown ingestor: 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("shutdown ingestor failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "shutdown ingestor") } func (c *CLI) UserListMessageQueueCommand(cmd *Command) (ResponseIf, error) { @@ -1626,24 +1163,10 @@ func (c *CLI) UserListMessageQueueCommand(cmd *Command) (ResponseIf, error) { resp, err := c.AdminServerClient.Request("GET", "/admin/queue/messages", "admin", nil, payload) if err != nil { - return nil, fmt.Errorf("failed to list tasks in message queue: %w", err) + return nil, fmt.Errorf("failed to list message queue: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list tasks in message queue: 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 tasks in message queue failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "list message queue") } func (c *CLI) UserPublishMessageCommand(cmd *Command) (ResponseIf, error) { @@ -1705,21 +1228,7 @@ func (c *CLI) UserPullMessageCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to pull message: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to pull message: 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("pull message failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "pull message") } func (c *CLI) UserShowMessageQueueCommand(cmd *Command) (ResponseIf, error) { @@ -1732,21 +1241,7 @@ func (c *CLI) UserShowMessageQueueCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to show message queue: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to show message queue: 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 message queue failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "show message queue") } // AdminCheckLicenseCommand check license command (admin mode only) @@ -1762,21 +1257,7 @@ func (c *CLI) AdminCheckLicenseCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to check license: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to check license: 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("check license failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "check license") } // AdminShowFingerprintCommand show fingerprint command (admin mode only) @@ -1792,21 +1273,7 @@ func (c *CLI) AdminShowFingerprintCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to show fingerprint: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to show fingerprint: 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 fingerprint failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "show fingerprint") } // AdminShowLicenseCommand show license command (admin mode only) @@ -1822,21 +1289,7 @@ func (c *CLI) AdminShowLicenseCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to show license: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to show license: 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 license failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "show license") } // AdminShowUserCommand show user command (admin mode only) @@ -1858,21 +1311,7 @@ func (c *CLI) AdminShowUserCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to show user: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to show user: 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 user failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "show user") } // AdminShowRoleCommand show role command (admin mode only) @@ -1890,21 +1329,7 @@ func (c *CLI) AdminShowRoleCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to show role: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to show role: 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 role failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "show role") } // AdminShowRoleDefaultModelsCommand show role default models command (admin mode only) @@ -1922,21 +1347,7 @@ func (c *CLI) AdminShowRoleDefaultModelsCommand(cmd *Command) (ResponseIf, error return nil, fmt.Errorf("failed to show role default models: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to show role default 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("show role default models failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "show role default models") } func (c *CLI) AdminShowUserActivityCommand(cmd *Command) (ResponseIf, error) { @@ -1968,21 +1379,7 @@ func (c *CLI) AdminShowUserActivityCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to get user activity: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to get user activity: 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("get user activity failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "get user activity") } func (c *CLI) AdminShowUserSummaryCommand(cmd *Command) (ResponseIf, error) { @@ -2048,21 +1445,7 @@ func (c *CLI) AdminShowUserDatasetCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to get user dataset: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to get user dataset: 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("get user dataset failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "get user dataset") } func (c *CLI) AdminShowUserStorageCommand(cmd *Command) (ResponseIf, error) { @@ -2192,21 +1575,7 @@ func (c *CLI) AdminShowUserPermissionCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to get user permission: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to get user permission: 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("get user permission failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "get user permission") } func (c *CLI) AdminShowUsersSummaryCommand(cmd *Command) (ResponseIf, error) { @@ -2266,21 +1635,7 @@ func (c *CLI) AdminShowUsersActivityCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to get users activity: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to get users activity: 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("get users activity failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "get users activity") } func (c *CLI) AdminShowUsersPlanCommand(cmd *Command) (ResponseIf, error) { @@ -2296,21 +1651,7 @@ func (c *CLI) AdminShowUsersPlanCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to get users activity: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to get users plan: 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("get users plan failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "get users plan") } // ListUsers lists all users (admin mode only) @@ -2629,21 +1970,7 @@ func (c *CLI) AdminPurgeOrphanCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to purge orphan data: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to purge orphan data: 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("purge orphan data failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "purge orphan data") } func (c *CLI) AdminPurgeUserCommand(cmd *Command) (ResponseIf, error) { @@ -2673,21 +2000,7 @@ func (c *CLI) AdminPurgeUserCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to purge user %s: %w", userName, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to purge user %s: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body)) - } - - var result CommonDataResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("purge user %s failed: invalid JSON (%w)", userName, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("purge user %s", userName)) } func (c *CLI) AdminPurgeUsersCommand(cmd *Command) (ResponseIf, error) { @@ -2738,21 +2051,7 @@ func (c *CLI) AdminPurgeUsersCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to purge users data: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to purge users data: 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("purge users data failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "purge users data") } func (c *CLI) AdminListUserIngestionTasksCommand(cmd *Command) (ResponseIf, error) { @@ -2818,21 +2117,7 @@ func (c *CLI) AdminListUserDatasetsCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list user %s datasets: %w", userName, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list user %s datasets: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body)) - } - - var result CommonResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("list user %s datasets failed: invalid JSON (%w)", userName, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, fmt.Sprintf("list user %s datasets", userName)) } func (c *CLI) AdminListUserAgentsCommand(cmd *Command) (ResponseIf, error) { @@ -2853,21 +2138,7 @@ func (c *CLI) AdminListUserAgentsCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list user %s agents: %w", userName, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list user %s agents: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body)) - } - - var result CommonResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("list user %s agents failed: invalid JSON (%w)", userName, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, fmt.Sprintf("list user %s agents", userName)) } func (c *CLI) AdminListUserChatsCommand(cmd *Command) (ResponseIf, error) { @@ -2888,22 +2159,9 @@ func (c *CLI) AdminListUserChatsCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list user %s chats: %w", userName, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list user %s chats: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body)) - } - - var result CommonResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("list user %s chats failed: invalid JSON (%w)", userName, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, fmt.Sprintf("list user %s chats", userName)) } + func (c *CLI) AdminListUserSearchesCommand(cmd *Command) (ResponseIf, error) { if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil { @@ -2923,22 +2181,9 @@ func (c *CLI) AdminListUserSearchesCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list user %s searches: %w", userName, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list user %s searches: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body)) - } - - var result CommonResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("list user %s searches failed: invalid JSON (%w)", userName, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, fmt.Sprintf("list user %s searches", userName)) } + func (c *CLI) AdminListUserModelsCommand(cmd *Command) (ResponseIf, error) { if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil { @@ -2958,22 +2203,9 @@ func (c *CLI) AdminListUserModelsCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list user %s models: %w", userName, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list user %s models: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body)) - } - - var result CommonResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("list user %s models failed: invalid JSON (%w)", userName, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, fmt.Sprintf("list user %s models", userName)) } + func (c *CLI) AdminListUserFilesCommand(cmd *Command) (ResponseIf, error) { if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil { @@ -2993,21 +2225,7 @@ func (c *CLI) AdminListUserFilesCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list user %s files: %w", userName, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list user %s files: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body)) - } - - var result CommonResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("list user %s files failed: invalid JSON (%w)", userName, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, fmt.Sprintf("list user %s files", userName)) } func (c *CLI) AdminListUserKeysCommand(cmd *Command) (ResponseIf, error) { @@ -3074,21 +2292,7 @@ func (c *CLI) AdminListUserProvidersCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list user %s providers: %w", userName, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list user %s providers: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body)) - } - - var result CommonResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("list user %s providers failed: invalid JSON (%w)", userName, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, fmt.Sprintf("list user %s providers", userName)) } func (c *CLI) AdminListUserProviderInstancesCommand(cmd *Command) (ResponseIf, error) { @@ -3115,21 +2319,7 @@ func (c *CLI) AdminListUserProviderInstancesCommand(cmd *Command) (ResponseIf, e return nil, fmt.Errorf("failed to list user %s providers: %w", userName, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list user %s providers: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body)) - } - - var result CommonResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("list user %s providers failed: invalid JSON (%w)", userName, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, fmt.Sprintf("list user %s provider instances", userName)) } func (c *CLI) AdminListUserProviderInstanceModelsCommand(cmd *Command) (ResponseIf, error) { @@ -3159,21 +2349,7 @@ func (c *CLI) AdminListUserProviderInstanceModelsCommand(cmd *Command) (Response return nil, fmt.Errorf("failed to list user %s provider instance models: %w", userName, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list user %s provider instance models: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body)) - } - - var result CommonResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("list user %s provider instance models failed: invalid JSON (%w)", userName, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, fmt.Sprintf("list user %s provider instance models", userName)) } func (c *CLI) AdminListUserDefaultModelsCommand(cmd *Command) (ResponseIf, error) { @@ -3195,21 +2371,7 @@ func (c *CLI) AdminListUserDefaultModelsCommand(cmd *Command) (ResponseIf, error return nil, fmt.Errorf("failed to list user %s default models: %w", userName, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list user %s default models: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body)) - } - - var result CommonResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("list user %s default models failed: invalid JSON (%w)", userName, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, fmt.Sprintf("list user %s default models", userName)) } func (c *CLI) AdminStopUserIngestionTasksCommand(cmd *Command) (ResponseIf, error) { @@ -3239,21 +2401,7 @@ func (c *CLI) AdminStopUserIngestionTasksCommand(cmd *Command) (ResponseIf, erro return nil, fmt.Errorf("failed to stop user %s ingestion tasks: %w", userName, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to stop user %s ingestion tasks: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body)) - } - - var result CommonResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("stop user %s ingestion tasks failed: invalid JSON (%w)", userName, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, fmt.Sprintf("stop user %s ingestion tasks", userName)) } func (c *CLI) AdminRemoveUserIngestionTasksCommand(cmd *Command) (ResponseIf, error) { @@ -3283,21 +2431,7 @@ func (c *CLI) AdminRemoveUserIngestionTasksCommand(cmd *Command) (ResponseIf, er return nil, fmt.Errorf("failed to remove user %s ingestion tasks: %w", userName, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to remove user %s ingestion tasks: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body)) - } - - var result CommonResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("remove user %s ingestion tasks failed: invalid JSON (%w)", userName, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, fmt.Sprintf("remove user %s ingestion tasks", userName)) } // AdminAddProviderCommand add provider @@ -3323,21 +2457,7 @@ func (c *CLI) AdminAddProviderCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to add provider %s: %w", providerName, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to add provider %s: HTTP %d, body: %s", providerName, resp.StatusCode, string(resp.Body)) - } - - var result CommonDataResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("add provider %s failed: invalid JSON (%w)", providerName, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("add provider %s", providerName)) } // AdminAddModelInstanceCommand add model instance @@ -3368,21 +2488,7 @@ func (c *CLI) AdminAddModelInstanceCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to add model instance %s: %w", instanceName, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to add model instance %s: HTTP %d, body: %s", instanceName, resp.StatusCode, string(resp.Body)) - } - - var result CommonDataResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("add model instance %s failed: invalid JSON (%w)", instanceName, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("add model instance %s", instanceName)) } // AdminAddModelsCommand add models @@ -3418,21 +2524,7 @@ func (c *CLI) AdminAddModelsCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to add models %s: %w", modelNames, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to add models %s: HTTP %d, body: %s", modelNames, resp.StatusCode, string(resp.Body)) - } - - var result CommonDataResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("add models %s failed: invalid JSON (%w)", modelNames, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("add models %s", modelNames)) } // AdminDeleteProvidersCommand delete providers @@ -3458,21 +2550,7 @@ func (c *CLI) AdminDeleteProvidersCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to remove providers %s: %w", providerNames, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to remove providers %s: HTTP %d, body: %s", providerNames, resp.StatusCode, string(resp.Body)) - } - - var result CommonDataResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("remove providers %s failed: invalid JSON (%w)", providerNames, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("remove providers %s", providerNames)) } // AdminDeleteInstancesCommand delete instances @@ -3503,21 +2581,7 @@ func (c *CLI) AdminDeleteInstancesCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to remove instance %s: %w", instanceNames, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to remove instance %s: HTTP %d, body: %s", instanceNames, resp.StatusCode, string(resp.Body)) - } - - var result CommonDataResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("remove instance %s failed: invalid JSON (%w)", instanceNames, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("remove instance %s", instanceNames)) } // AdminDeleteModelsCommand delete models @@ -3553,21 +2617,7 @@ func (c *CLI) AdminDeleteModelsCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to remove model %s: %w", modelNames, err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to remove model %s: HTTP %d, body: %s", modelNames, resp.StatusCode, string(resp.Body)) - } - - var result CommonDataResponse - if err = json.Unmarshal(resp.Body, &result); err != nil { - return nil, fmt.Errorf("remove model %s failed: invalid JSON (%w)", modelNames, err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("remove model %s", modelNames)) } func (c *CLI) AdminShowLogLevelCommand(cmd *Command) (ResponseIf, error) { @@ -3581,19 +2631,5 @@ func (c *CLI) AdminShowLogLevelCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to get log level config: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to get log level config: 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("get log level config failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, fmt.Sprintf("get log level config")) } diff --git a/internal/cli/cli_http.go b/internal/cli/cli_http.go index 61a1f110b5..af53086d2e 100644 --- a/internal/cli/cli_http.go +++ b/internal/cli/cli_http.go @@ -444,7 +444,7 @@ func (c *CLI) ExecuteUserCommand(cmd *Command) (ResponseIf, error) { case "api_list_default_models": return c.APIListDefaultModelsCommand(cmd) case "api_parse_documents": - return c.APIParseDocumentsUserCommand(cmd) + return c.APIParseDocumentsCommand(cmd) case "api_start_ingestion": return c.APIStartIngestionCommand(cmd) case "api_stop_ingestion": diff --git a/internal/cli/common_command.go b/internal/cli/common_command.go index ac67130bd3..9b3fa4cdfe 100644 --- a/internal/cli/common_command.go +++ b/internal/cli/common_command.go @@ -318,20 +318,7 @@ func (c *CLI) CommonShowProviderCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to show provider: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to show provider: 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 provider: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "show provider") } // CommonShowProviderInstanceCommand shows details of a specific instance @@ -364,21 +351,7 @@ func (c *CLI) CommonShowProviderInstanceCommand(cmd *Command) (ResponseIf, error 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 + return HandleCommonDataResponse(resp, "show instance") } // CommonShowProviderInstanceBalanceCommand shows balance of a specific instance @@ -412,21 +385,7 @@ func (c *CLI) CommonShowProviderInstanceBalanceCommand(cmd *Command) (ResponseIf 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 + return HandleCommonDataResponse(resp, "show instance balance") } // CommonListProviderInstancesCommand lists all instances of a provider @@ -631,20 +590,7 @@ func (c *CLI) CommonShowProviderModelCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to show model: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to show model: 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 model: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "show model") } func (c *CLI) CommonCheckProviderWithKeyCommand(cmd *Command) (ResponseIf, error) { @@ -695,31 +641,12 @@ func (c *CLI) CommonCheckProviderWithKeyCommand(cmd *Command) (ResponseIf, error 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 + return HandleCommonDataResponse(resp, "check provider connection with key") 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 + return HandleSimpleResponse(resp, "check provider connection with key") default: return nil, fmt.Errorf("invalid server type") } @@ -754,31 +681,12 @@ func (c *CLI) CommonCheckProviderConnectionCommand(cmd *Command) (ResponseIf, er 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 + return HandleCommonDataResponse(resp, "check provider connection") 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 + return HandleSimpleResponse(resp, "check provider connection") default: return nil, fmt.Errorf("invalid server type") } @@ -827,31 +735,11 @@ func (c *CLI) CommonAlterProviderInstanceCommand(cmd *Command) (ResponseIf, erro 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 + return HandleCommonDataResponse(resp, "alter instance") 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 + return HandleSimpleResponse(resp, "alter instance") default: return nil, fmt.Errorf("invalid server type") } @@ -895,31 +783,12 @@ func (c *CLI) CommonEnableOrDisableModelCommand(cmd *Command, status string) (Re 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 + return HandleCommonDataResponse(resp, "enable/disable model") 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 + return HandleSimpleResponse(resp, "enable/disable model") default: return nil, fmt.Errorf("invalid server type") } @@ -965,20 +834,7 @@ func (c *CLI) APISetDefaultModelCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to set default model: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to set default 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("failed to set default model: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleSimpleResponse(resp, "set default model") } func (c *CLI) APIResetDefaultModelCommand(cmd *Command) (ResponseIf, error) { @@ -1007,20 +863,7 @@ func (c *CLI) APIResetDefaultModelCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to reset default model: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to reset default 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("failed to reset default model: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleSimpleResponse(resp, "reset default model") } func (c *CLI) APIListDefaultModelsCommand(cmd *Command) (ResponseIf, error) { @@ -1469,20 +1312,7 @@ func (c *CLI) CommonShowModelCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to show model: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to show model: 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 model: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "show model") } // readPassword reads password from terminal without echoing diff --git a/internal/cli/response.go b/internal/cli/response.go index e8824fc7a4..54fb9974cb 100644 --- a/internal/cli/response.go +++ b/internal/cli/response.go @@ -58,6 +58,24 @@ func (r *CommonResponse) PrintOut() { } } +func HandleCommonResponse(response *Response, command string) (ResponseIf, error) { + if response.StatusCode != 200 { + return nil, fmt.Errorf("failed to %s: HTTP %d, body: %s", command, response.StatusCode, string(response.Body)) + } + + var result CommonResponse + if err := json.Unmarshal(response.Body, &result); err != nil { + return nil, fmt.Errorf("%s failed: invalid JSON (%w)", command, err) + } + + if result.Code != 0 { + return nil, fmt.Errorf("%s", result.Message) + } + + result.Duration = response.Duration + return &result, nil +} + type ModelsResponse struct { Code int `json:"code"` Data map[string][]map[string]interface{} `json:"data"` @@ -144,6 +162,24 @@ func (r *CommonDataResponse) PrintOut() { } } +func HandleCommonDataResponse(response *Response, command string) (ResponseIf, error) { + if response.StatusCode != 200 { + return nil, fmt.Errorf("failed to %s: HTTP %d, body: %s", command, response.StatusCode, string(response.Body)) + } + + var result CommonDataResponse + if err := json.Unmarshal(response.Body, &result); err != nil { + return nil, fmt.Errorf("%s failed: invalid JSON (%w)", command, err) + } + + if result.Code != 0 { + return nil, fmt.Errorf("%s", result.Message) + } + + result.Duration = response.Duration + return &result, nil +} + type ListDocumentsResponse struct { Code int `json:"code"` Data map[string]interface{} `json:"data"` @@ -451,6 +487,10 @@ func (r *SimpleResponse) PrintOut() { } func HandleSimpleResponse(response *Response, command string) (ResponseIf, error) { + if response.StatusCode != 200 { + return nil, fmt.Errorf("failed to %s: HTTP %d, body: %s", command, response.StatusCode, string(response.Body)) + } + var result SimpleResponse if err := json.Unmarshal(response.Body, &result); err != nil { return nil, fmt.Errorf("%s failed: invalid JSON (%w)", command, err) @@ -912,7 +952,7 @@ func printReferenceChunks(raw json.RawMessage) { fmt.Println("Reference:") for i, chunk := range chunks { - id := chunkID(chunk) + id := getChunkID(chunk) content := chunkContent(chunk) docName := chunkDocName(chunk) fmt.Printf(" [ID:%d] id=%s content=%q", i, id, truncateStr(content, 120)) @@ -930,7 +970,7 @@ func printReferenceChunks(raw json.RawMessage) { } } -func chunkID(c map[string]interface{}) string { +func getChunkID(c map[string]interface{}) string { for _, key := range []string{"chunk_id", "id"} { if v, ok := c[key]; ok { return fmt.Sprint(v) diff --git a/internal/cli/user_command.go b/internal/cli/user_command.go index e74039f03c..0d9e81a0eb 100644 --- a/internal/cli/user_command.go +++ b/internal/cli/user_command.go @@ -247,10 +247,6 @@ func (c *CLI) APISetLogLevelCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to change log level: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to change log level: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) - } - return HandleSimpleResponse(resp, "change log level") } @@ -353,21 +349,7 @@ func (c *CLI) APIListDatasetsCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list datasets: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list datasets: 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 datasets failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - - return &result, nil + return HandleCommonResponse(resp, "list datasets") } func (c *CLI) APIListDatasetDocumentsCommand(cmd *Command) (ResponseIf, error) { @@ -1052,10 +1034,6 @@ func (c *CLI) APICreateDatasetCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to create dataset: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to create dataset: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) - } - return HandleSimpleResponse(resp, "create dataset") } @@ -1237,20 +1215,7 @@ func (c *CLI) APIListAPIKeysCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list keys: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list keys: 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 keys failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "list keys") } // APIDeleteAPIKeyCommand deletes an API key @@ -1269,10 +1234,6 @@ func (c *CLI) APIDeleteAPIKeyCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to delete key: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to delete key: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) - } - return HandleSimpleResponse(resp, "delete key") } @@ -1672,10 +1633,6 @@ func (c *CLI) APIAddProviderCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to add provider: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to add provider: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) - } - return HandleSimpleResponse(resp, "add provider") } @@ -1690,21 +1647,7 @@ func (c *CLI) APIListProvidersCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list providers: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list providers: 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 providers failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "list providers") } // APIDeleteProviderCommand deletes a provider @@ -1731,10 +1674,6 @@ func (c *CLI) APIDeleteProviderCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to delete provider: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to delete provider: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) - } - return HandleSimpleResponse(resp, "delete provider") } @@ -1767,14 +1706,10 @@ func (c *CLI) APIDropDatasetCommand(cmd *Command) (ResponseIf, error) { resp, err := httpClient.Request("DELETE", "/datasets", "web", nil, payload) if err != nil { - return nil, fmt.Errorf("failed to create dataset: %w", err) + return nil, fmt.Errorf("failed to drop dataset: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to create dataset: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) - } - - return HandleSimpleResponse(resp, "create provider instance") + return HandleSimpleResponse(resp, "drop dataset") } // APIDropAgentCommand DROP AGENT 'agent_name' @@ -1806,14 +1741,10 @@ func (c *CLI) APIDropAgentCommand(cmd *Command) (ResponseIf, error) { resp, err := httpClient.Request("DELETE", "/agents", "web", nil, payload) if err != nil { - return nil, fmt.Errorf("failed to create agent: %w", err) + return nil, fmt.Errorf("failed to drop agent: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to create agent: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) - } - - return HandleSimpleResponse(resp, "delete agent") + return HandleSimpleResponse(resp, "drop agent") } // APIDropChatCommand DROP CHAT 'chat_name' @@ -1845,14 +1776,10 @@ func (c *CLI) APIDropChatCommand(cmd *Command) (ResponseIf, error) { resp, err := httpClient.Request("DELETE", "/chats", "web", nil, payload) if err != nil { - return nil, fmt.Errorf("failed to create chat: %w", err) + return nil, fmt.Errorf("failed to drop chat: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to create chat: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) - } - - return HandleSimpleResponse(resp, "delete chat") + return HandleSimpleResponse(resp, "drop chat") } // APIDropSearchCommand DROP SEARCH 'search_name' @@ -1881,14 +1808,10 @@ func (c *CLI) APIDropSearchCommand(cmd *Command) (ResponseIf, error) { resp, err := httpClient.Request("DELETE", endPoint, "web", nil, nil) if err != nil { - return nil, fmt.Errorf("failed to delete search: %w", err) + return nil, fmt.Errorf("failed to drop search: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to delete search: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) - } - - return HandleSimpleResponse(resp, "delete search") + return HandleSimpleResponse(resp, "drop search") } // APIDropMemoryCommand DROP MEMORY 'memory_name' @@ -1917,14 +1840,10 @@ func (c *CLI) APIDropMemoryCommand(cmd *Command) (ResponseIf, error) { resp, err := httpClient.Request("DELETE", endPoint, "web", nil, nil) if err != nil { - return nil, fmt.Errorf("failed to delete memory: %w", err) + return nil, fmt.Errorf("failed to drop memory: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to delete memory: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) - } - - return HandleSimpleResponse(resp, "delete memory") + return HandleSimpleResponse(resp, "drop memory") } // APIAddProviderInstanceCommand creates a new provider instance @@ -1975,55 +1894,10 @@ func (c *CLI) APIAddProviderInstanceCommand(cmd *Command) (ResponseIf, error) { resp, err := httpClient.Request("POST", url, "web", nil, payload) if err != nil { - return nil, fmt.Errorf("failed to create provider instance: %w", err) + return nil, fmt.Errorf("failed to add provider instance: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to create provider instance: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) - } - - return HandleSimpleResponse(resp, "create provider instance") -} - -// ShowInstanceBalance shows balance of a specific instance -// SHOW BALANCE FROM PROVIDER -func (c *CLI) ShowInstanceBalance(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/balance", 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 + return HandleSimpleResponse(resp, "add provider instance") } // DELETE PROVIDER INSTANCE @@ -2050,14 +1924,10 @@ func (c *CLI) APIDeleteProviderInstanceCommand(cmd *Command) (ResponseIf, error) resp, err := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("DELETE", url, "web", nil, payload) if err != nil { - return nil, fmt.Errorf("failed to drop instance: %w", err) + return nil, fmt.Errorf("failed to drop provider instance: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to drop instance: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) - } - - return HandleSimpleResponse(resp, "drop instance") + return HandleSimpleResponse(resp, "drop provider instance") } // DELETE PROVIDER INSTANCE MODELS @@ -2089,14 +1959,10 @@ func (c *CLI) APIDeleteProviderInstanceModelCommand(cmd *Command) (ResponseIf, e resp, err := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("DELETE", url, "web", nil, payload) if err != nil { - return nil, fmt.Errorf("failed to drop model: %w", err) + return nil, fmt.Errorf("failed to delete model: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to drop model: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) - } - - return HandleSimpleResponse(resp, "drop model") + return HandleSimpleResponse(resp, "delete model") } func isValidURL(str string) bool { @@ -2533,18 +2399,8 @@ func (c *CLI) APIRerankUserDocumentCommand(cmd *Command) (ResponseIf, error) { if err != nil { return nil, fmt.Errorf("failed to rerank document: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to rerank document: 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("rerank document failed: invalid JSON (%w)", err) - } - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + + return HandleCommonResponse(resp, "rerank document") } func (c *CLI) APITTSUserCommand(cmd *Command) (ResponseIf, error) { @@ -2940,19 +2796,7 @@ func (c *CLI) APIOCRUserCommand(cmd *Command) (ResponseIf, error) { if err != nil { return nil, fmt.Errorf("failed to OCR document: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to OCR document: 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("OCR document failed: invalid JSON (%w)", err) - } - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - - return &result, nil + return HandleCommonDataResponse(resp, "OCR document") } func (c *CLI) APIModelParseFileCommand(cmd *Command) (ResponseIf, error) { @@ -3042,19 +2886,8 @@ func (c *CLI) APIModelParseFileCommand(cmd *Command) (ResponseIf, error) { if err != nil { return nil, fmt.Errorf("failed to PARSE document: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to PARSE document: 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("PARSE document failed: invalid JSON (%w)", err) - } - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "PARSE document") } func (c *CLI) APIListModelInstanceTasksCommand(cmd *Command) (ResponseIf, error) { @@ -3080,20 +2913,10 @@ func (c *CLI) APIListModelInstanceTasksCommand(cmd *Command) (ResponseIf, error) resp, err := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("GET", url, "web", nil, nil) if err != nil { - return nil, fmt.Errorf("failed to list tasks: %w", err) + return nil, fmt.Errorf("failed to list model parsing tasks: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list tasks: 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 tasks failed: invalid JSON (%w)", err) - } - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - result.Duration = resp.Duration - return &result, nil + + return HandleCommonResponse(resp, "list model parsing tasks") } // APIShowProviderInstanceTaskCommand shows the details of a task @@ -3220,9 +3043,6 @@ func (c *CLI) APIAddCustomModelCommand(cmd *Command) (ResponseIf, error) { if err != nil { return nil, fmt.Errorf("failed to add custom model: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to add custom model: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) - } return HandleSimpleResponse(resp, "add custom model") } @@ -3647,7 +3467,8 @@ func (c *CLI) DevRemoveChunksCommand(cmd *Command) (ResponseIf, error) { case float64: deletedCount = int64(data) case map[string]interface{}: - if count, ok := data["deleted_count"].(float64); ok { + var count float64 + if count, ok = data["deleted_count"].(float64); ok { deletedCount = int64(count) } } @@ -3659,7 +3480,7 @@ func (c *CLI) DevRemoveChunksCommand(cmd *Command) (ResponseIf, error) { return &result, nil } -func (c *CLI) APIParseDocumentsUserCommand(cmd *Command) (ResponseIf, error) { +func (c *CLI) APIParseDocumentsCommand(cmd *Command) (ResponseIf, error) { if c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].APIKey == nil && c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].LoginToken == nil { return nil, fmt.Errorf("API key not set. Please login first") } @@ -3687,14 +3508,10 @@ func (c *CLI) APIParseDocumentsUserCommand(cmd *Command) (ResponseIf, error) { // Normal mode resp, err := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("POST", url, "web", nil, payload) if err != nil { - return nil, fmt.Errorf("failed to list documents: %w", err) + return nil, fmt.Errorf("failed to parse documents: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list documents: HTTP %d, body: %s", resp.StatusCode, string(resp.Body)) - } - - return HandleSimpleResponse(resp, "list documents") + return HandleSimpleResponse(resp, "parse documents") } func (c *CLI) APIParseLocalFileCommand(cmd *Command) (ResponseIf, error) { @@ -3802,21 +3619,7 @@ func (c *CLI) APIListIngestionTasks(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list ingestion tasks: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list ingestion tasks:: 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 ingestion tasks: failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "list ingestion tasks") } // APIShowLogLevelCommand sets the log level for the system. @@ -3830,21 +3633,7 @@ func (c *CLI) APIShowLogLevelCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to get log level config: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to get log level config: 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("get log level config failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonDataResponse(resp, "get log level config") } @@ -3863,21 +3652,7 @@ func (c *CLI) APIListEnvironmentsCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list environments: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list environments: 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 environments failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "list environments") } // APIListVariablesCommand lists all system variables (api mode only). @@ -3895,21 +3670,7 @@ func (c *CLI) APIListVariablesCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to list variables: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to list variables: 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 environments failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "list variables") } func (c *CLI) APIStartIngestionCommand(cmd *Command) (ResponseIf, error) { @@ -3943,21 +3704,7 @@ func (c *CLI) APIStartIngestionCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to ingest file: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to ingest file: 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("ingest file failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "ingest file") } func (c *CLI) APIStopIngestionCommand(cmd *Command) (ResponseIf, error) { @@ -3979,24 +3726,10 @@ func (c *CLI) APIStopIngestionCommand(cmd *Command) (ResponseIf, error) { resp, err := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("PUT", "/datasets/ingestion/tasks", "web", nil, payload) if err != nil { - return nil, fmt.Errorf("failed to ingest file: %w", err) + return nil, fmt.Errorf("failed to stop ingestion: %w", err) } - if resp.StatusCode != 200 { - return nil, fmt.Errorf("failed to ingest file: 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("ingest file failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "stop ingestion") } func (c *CLI) APIRemoveTaskCommand(cmd *Command) (ResponseIf, error) { @@ -4026,17 +3759,7 @@ func (c *CLI) APIRemoveTaskCommand(cmd *Command) (ResponseIf, error) { return nil, fmt.Errorf("failed to remove tasks: 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("remove tasks failed: invalid JSON (%w)", err) - } - - if result.Code != 0 { - return nil, fmt.Errorf("%s", result.Message) - } - - result.Duration = resp.Duration - return &result, nil + return HandleCommonResponse(resp, "remove tasks") } func (c *CLI) DevChunkCommand(cmd *Command) (ResponseIf, error) {