Open source AI coding agent. Designed for large projects and real world tasks.
Go aiai-agentsai-developer-toolsai-toolsclicommand-linedeveloper-toolsgitgolanggpt-4llmopenaipolyglot-programmingterminalterminal-basedterminal-ui
Refactor auth refresh mechanism to support both token and config updates
Dane Schneiderc8a57ea3defa
diff --git a/app/cli/api/errors.go b/app/cli/api/errors.goindex ad91b76b..af1d8cd0 100644--- a/app/cli/api/errors.go+++ b/app/cli/api/errors.go@@ -2,6 +2,7 @@ package apiimport ("encoding/json"+ "fmt""log""net/http""plandex-cli/auth"@@ -31,8 +32,8 @@ func HandleApiError(r *http.Response, errBody []byte) *shared.ApiError {}}- // return error for authentication/retry- if apiError.Type == shared.ApiErrorTypeInvalidToken {+ // return error if token/auth refresh is needed+ if apiError.Type == shared.ApiErrorTypeInvalidToken || apiError.Type == shared.ApiErrorTypeAuthOutdated {return &apiError}@@ -41,13 +42,22 @@ func HandleApiError(r *http.Response, errBody []byte) *shared.ApiError {return &apiError}-func refreshTokenIfNeeded(apiErr *shared.ApiError) (bool, *shared.ApiError) {+func refreshAuthIfNeeded(apiErr *shared.ApiError) (bool, *shared.ApiError) {if apiErr.Type == shared.ApiErrorTypeInvalidToken {err := auth.RefreshInvalidToken()if err != nil {return false, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: "error refreshing invalid token"}}+ return true, nil+ } else if apiErr.Type == shared.ApiErrorTypeAuthOutdated {+ fmt.Println("refreshing auth")+ err := auth.RefreshAuth()+ if err != nil {+ return false, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: "error refreshing auth"}+ }+return true, nil}+return false, apiErr}diff --git a/app/cli/api/methods.go b/app/cli/api/methods.goindex 4e394c6c..d3abbec7 100644--- a/app/cli/api/methods.go+++ b/app/cli/api/methods.go@@ -12,6 +12,8 @@ import ("strings"shared "plandex-shared"++ "github.com/shopspring/decimal")func (a *Api) CreateCliTrialSession() (string, *shared.ApiError) {@@ -87,8 +89,8 @@ func (a *Api) CreateProject(req shared.CreateProjectRequest) (*shared.CreateProjif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.CreateProject(req)}return nil, apiErr@@ -114,8 +116,8 @@ func (a *Api) ListProjects() ([]*shared.Project, *shared.ApiError) {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.ListProjects()}return nil, apiErr@@ -152,7 +154,7 @@ func (a *Api) SetProjectPlan(projectId string, req shared.SetProjectPlanRequest)if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.SetProjectPlan(projectId, req)}@@ -185,7 +187,7 @@ func (a *Api) RenameProject(projectId string, req shared.RenameProjectRequest) *errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.RenameProject(projectId, req)}@@ -213,7 +215,7 @@ func (a *Api) ListPlans(projectIds []string) ([]*shared.Plan, *shared.ApiError)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.ListPlans(projectIds)}@@ -246,8 +248,8 @@ func (a *Api) ListArchivedPlans(projectIds []string) ([]*shared.Plan, *shared.Apif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.ListArchivedPlans(projectIds)}return nil, apiErr@@ -282,8 +284,8 @@ func (a *Api) ListPlansRunning(projectIds []string, includeRecent bool) (*sharedif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.ListPlansRunning(projectIds, includeRecent)}return nil, apiErr@@ -326,7 +328,7 @@ func (a *Api) GetCurrentBranchByPlanId(projectId string, req shared.GetCurrentBrapiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.GetCurrentBranchByPlanId(projectId, req)}@@ -358,8 +360,8 @@ func (a *Api) CreatePlan(projectId string, req shared.CreatePlanRequest) (*shareif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.CreatePlan(projectId, req)}return nil, apiErr@@ -387,8 +389,8 @@ func (a *Api) GetPlan(planId string) (*shared.Plan, *shared.ApiError) {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.GetPlan(planId)}return nil, apiErr@@ -421,7 +423,7 @@ func (a *Api) DeletePlan(planId string) *shared.ApiError {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.DeletePlan(planId)}@@ -449,7 +451,7 @@ func (a *Api) DeleteAllPlans(projectId string) *shared.ApiError {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.DeleteAllPlans(projectId)@@ -490,7 +492,7 @@ func (a *Api) TellPlan(planId, branch string, req shared.TellPlanRequest, onStreerrorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.TellPlan(planId, branch, req, onStream)@@ -543,7 +545,7 @@ func (a *Api) BuildPlan(planId, branch string, req shared.BuildPlanRequest, onSterrorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.BuildPlan(planId, branch, req, onStream)@@ -585,7 +587,7 @@ func (a *Api) RespondMissingFile(planId, branch string, req shared.RespondMissinerrorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.RespondMissingFile(planId, branch, req)@@ -614,7 +616,7 @@ func (a *Api) ConnectPlan(planId, branch string, onStream types.OnStreamPlan) *serrorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.ConnectPlan(planId, branch, onStream)@@ -645,7 +647,7 @@ func (a *Api) StopPlan(ctx context.Context, planId, branch string) *shared.ApiErif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.StopPlan(ctx, planId, branch)}@@ -680,8 +682,8 @@ func (a *Api) getCurrentPlanState(planId, branch, sha string) (*shared.CurrentPlif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.getCurrentPlanState(planId, branch, sha)}return nil, apiErr@@ -720,7 +722,7 @@ func (a *Api) ApplyPlan(planId, branch string, req shared.ApplyPlanRequest) (strerrorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.ApplyPlan(planId, branch, req)}@@ -754,7 +756,7 @@ func (a *Api) ArchivePlan(planId string) *shared.ApiError {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.ArchivePlan(planId)}@@ -782,7 +784,7 @@ func (a *Api) UnarchivePlan(planId string) *shared.ApiError {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.ArchivePlan(planId)}@@ -821,7 +823,7 @@ func (a *Api) RenamePlan(planId string, name string) *shared.ApiError {apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.RenamePlan(planId, name)}@@ -849,7 +851,7 @@ func (a *Api) RejectAllChanges(planId, branch string) *shared.ApiError {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {return a.RejectAllChanges(planId, branch)}@@ -883,7 +885,7 @@ func (a *Api) RejectFile(planId, branch, filePath string) *shared.ApiError {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {a.RejectFile(planId, branch, filePath)}@@ -917,7 +919,7 @@ func (a *Api) RejectFiles(planId, branch string, paths []string) *shared.ApiErroif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- didRefresh, apiErr := refreshTokenIfNeeded(apiErr)+ didRefresh, apiErr := refreshAuthIfNeeded(apiErr)if didRefresh {a.RejectFiles(planId, branch, paths)}@@ -944,8 +946,8 @@ func (a *Api) LoadContext(planId, branch string, req shared.LoadContextRequest)if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.LoadContext(planId, branch, req)}return nil, apiErr@@ -986,8 +988,8 @@ func (a *Api) UpdateContext(planId, branch string, req shared.UpdateContextRequeif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.UpdateContext(planId, branch, req)}return nil, apiErr@@ -1024,8 +1026,8 @@ func (a *Api) DeleteContext(planId, branch string, req shared.DeleteContextRequeif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.DeleteContext(planId, branch, req)}return nil, apiErr@@ -1052,8 +1054,8 @@ func (a *Api) ListContext(planId, branch string) ([]*shared.Context, *shared.Apiif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.ListContext(planId, branch)}return nil, apiErr@@ -1108,8 +1110,8 @@ func (a *Api) ListConvo(planId, branch string) ([]*shared.ConvoMessage, *shared.if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.ListConvo(planId, branch)}return nil, apiErr@@ -1136,8 +1138,8 @@ func (a *Api) GetPlanStatus(planId, branch string) (string, *shared.ApiError) {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.GetPlanStatus(planId, branch)}return "", apiErr@@ -1168,8 +1170,8 @@ func (a *Api) GetPlanDiffs(planId, branch string, plain bool) (string, *shared.Aif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.GetPlanDiffs(planId, branch, plain)}return "", apiErr@@ -1197,8 +1199,8 @@ func (a *Api) ListLogs(planId, branch string) (*shared.LogResponse, *shared.ApiEif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.ListLogs(planId, branch)}return nil, apiErr@@ -1236,8 +1238,8 @@ func (a *Api) RewindPlan(planId, branch string, req shared.RewindPlanRequest) (*if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.RewindPlan(planId, branch, req)}return nil, apiErr@@ -1332,8 +1334,8 @@ func (a *Api) CreateOrg(req shared.CreateOrgRequest) (*shared.CreateOrgResponse,if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.CreateOrg(req)}return nil, apiErr@@ -1359,8 +1361,8 @@ func (a *Api) GetOrgSession() (*shared.Org, *shared.ApiError) {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.GetOrgSession()}return nil, apiErr@@ -1388,8 +1390,8 @@ func (a *Api) ListOrgs() ([]*shared.Org, *shared.ApiError) {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.ListOrgs()}return nil, apiErr@@ -1420,8 +1422,8 @@ func (a *Api) DeleteUser(userId string) *shared.ApiError {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.DeleteUser(userId)}return apiErr@@ -1442,8 +1444,8 @@ func (a *Api) ListOrgRoles() ([]*shared.OrgRole, *shared.ApiError) {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.ListOrgRoles()}return nil, apiErr@@ -1475,8 +1477,8 @@ func (a *Api) InviteUser(req shared.InviteRequest) *shared.ApiError {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.InviteUser(req)}return apiErr@@ -1496,8 +1498,8 @@ func (a *Api) ListPendingInvites() ([]*shared.Invite, *shared.ApiError) {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.ListPendingInvites()}return nil, apiErr@@ -1523,8 +1525,8 @@ func (a *Api) ListAcceptedInvites() ([]*shared.Invite, *shared.ApiError) {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.ListAcceptedInvites()}return nil, apiErr@@ -1550,8 +1552,8 @@ func (a *Api) ListAllInvites() ([]*shared.Invite, *shared.ApiError) {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.ListAllInvites()}return nil, apiErr@@ -1583,8 +1585,8 @@ func (a *Api) DeleteInvite(inviteId string) *shared.ApiError {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.DeleteInvite(inviteId)}return apiErr@@ -1636,8 +1638,8 @@ func (a *Api) CreateSignInCode() (string, *shared.ApiError) {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.CreateSignInCode()}return "", apiErr@@ -1686,8 +1688,8 @@ func (a *Api) ListUsers() (*shared.ListUsersResponse, *shared.ApiError) {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.ListUsers()}return nil, apiErr@@ -1715,8 +1717,8 @@ func (a *Api) ListBranches(planId string) ([]*shared.Branch, *shared.ApiError) {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.ListBranches(planId)}return nil, apiErr@@ -1749,8 +1751,8 @@ func (a *Api) CreateBranch(planId, branch string, req shared.CreateBranchRequesterrorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.CreateBranch(planId, branch, req)}return apiErr@@ -1777,8 +1779,8 @@ func (a *Api) DeleteBranch(planId, branch string) *shared.ApiError {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.DeleteBranch(planId, branch)}return apiErr@@ -1800,8 +1802,8 @@ func (a *Api) GetSettings(planId, branch string) (*shared.PlanSettings, *shared.if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.GetSettings(planId, branch)}return nil, apiErr@@ -1842,8 +1844,8 @@ func (a *Api) UpdateSettings(planId, branch string, req shared.UpdateSettingsReqerrorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.UpdateSettings(planId, branch, req)}return nil, apiErr@@ -1872,8 +1874,8 @@ func (a *Api) GetOrgDefaultSettings() (*shared.PlanSettings, *shared.ApiError) {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.GetOrgDefaultSettings()}return nil, apiErr@@ -1914,8 +1916,8 @@ func (a *Api) UpdateOrgDefaultSettings(req shared.UpdateSettingsRequest) (*shareerrorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.UpdateOrgDefaultSettings(req)}return nil, apiErr@@ -1942,8 +1944,8 @@ func (a *Api) GetPlanConfig(planId string) (*shared.PlanConfig, *shared.ApiErrorif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.GetPlanConfig(planId)}return nil, apiErr@@ -1981,8 +1983,8 @@ func (a *Api) UpdatePlanConfig(planId string, req shared.UpdatePlanConfigRequestif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.UpdatePlanConfig(planId, req)}return apiErr@@ -2003,8 +2005,8 @@ func (a *Api) GetDefaultPlanConfig() (*shared.PlanConfig, *shared.ApiError) {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.GetDefaultPlanConfig()}return nil, apiErr@@ -2042,8 +2044,8 @@ func (a *Api) UpdateDefaultPlanConfig(req shared.UpdateDefaultPlanConfigRequest)if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.UpdateDefaultPlanConfig(req)}return apiErr@@ -2069,8 +2071,8 @@ func (a *Api) CreateCustomModel(model *shared.AvailableModel) *shared.ApiError {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.CreateCustomModel(model)}return apiErr@@ -2091,8 +2093,8 @@ func (a *Api) ListCustomModels() ([]*shared.AvailableModel, *shared.ApiError) {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.ListCustomModels()}return nil, apiErr@@ -2124,8 +2126,8 @@ func (a *Api) DeleteAvailableModel(modelId string) *shared.ApiError {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.DeleteAvailableModel(modelId)}return apiErr@@ -2156,8 +2158,8 @@ func (a *Api) UpdateCustomModel(model *shared.AvailableModel) *shared.ApiError {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.UpdateCustomModel(model)}return apiErr@@ -2183,8 +2185,8 @@ func (a *Api) CreateModelPack(set *shared.ModelPack) *shared.ApiError {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.CreateModelPack(set)}return apiErr@@ -2207,8 +2209,8 @@ func (a *Api) ListModelPacks() ([]*shared.ModelPack, *shared.ApiError) {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.ListModelPacks()}return nil, apiErr@@ -2242,8 +2244,8 @@ func (a *Api) DeleteModelPack(setId string) *shared.ApiError {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.DeleteModelPack(setId)}return apiErr@@ -2275,8 +2277,8 @@ func (a *Api) UpdateModelPack(set *shared.ModelPack) *shared.ApiError {if resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.UpdateModelPack(set)}return apiErr@@ -2303,8 +2305,8 @@ func (a *Api) GetCreditsTransactions(pageSize, pageNum int, req shared.CreditsLoerrorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.GetCreditsTransactions(pageSize, pageNum, req)}return nil, apiErr@@ -2336,8 +2338,8 @@ func (a *Api) GetCreditsSummary(req shared.CreditsLogRequest) (*shared.CreditsSuif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.GetCreditsSummary(req)}return nil, apiErr@@ -2352,6 +2354,34 @@ func (a *Api) GetCreditsSummary(req shared.CreditsLogRequest) (*shared.CreditsSureturn res, nil}+func (a *Api) GetBalance() (decimal.Decimal, *shared.ApiError) {+ serverUrl := fmt.Sprintf("%s/billing/balance", GetApiHost())++ resp, err := authenticatedFastClient.Get(serverUrl)+ if err != nil {+ return decimal.Zero, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error sending request: %v", err)}+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ apiErr := HandleApiError(resp, errorBody)+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {+ return a.GetBalance()+ }+ return decimal.Zero, apiErr+ }++ var res *shared.GetBalanceResponse+ err = json.NewDecoder(resp.Body).Decode(&res)+ if err != nil {+ return decimal.Zero, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error decoding response: %v", err)}+ }++ return res.Balance, nil+}+func (a *Api) GetFileMap(req shared.GetFileMapRequest) (*shared.GetFileMapResponse, *shared.ApiError) {serverUrl := fmt.Sprintf("%s/file_map", GetApiHost())reqBytes, err := json.Marshal(req)@@ -2368,8 +2398,8 @@ func (a *Api) GetFileMap(req shared.GetFileMapRequest) (*shared.GetFileMapResponif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.GetFileMap(req)}return nil, apiErr@@ -2396,8 +2426,8 @@ func (a *Api) GetContextBody(planId, branch, contextId string) (*shared.GetConteif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.GetContextBody(planId, branch, contextId)}return nil, apiErr@@ -2438,8 +2468,8 @@ func (a *Api) AutoLoadContext(ctx context.Context, planId, branch string, req shif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.LoadContext(planId, branch, req)}return nil, apiErr@@ -2466,8 +2496,8 @@ func (a *Api) GetBuildStatus(planId, branch string) (*shared.GetBuildStatusRespoif resp.StatusCode >= 400 {errorBody, _ := io.ReadAll(resp.Body)apiErr := HandleApiError(resp, errorBody)- tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)- if tokenRefreshed {+ authRefreshed, apiErr := refreshAuthIfNeeded(apiErr)+ if authRefreshed {return a.GetBuildStatus(planId, branch)}return nil, apiErr