Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

Add Claude subscription token handling in auth flow

Dane Schneider16466436b546
diff --git a/app/cli/lib/model_credentials.go b/app/cli/lib/model_credentials.go
index 4b266ab6..3bdc8029 100644
--- a/app/cli/lib/model_credentials.go
+++ b/app/cli/lib/model_credentials.go
@@ -168,15 +168,15 @@ func checkProviderCredentialStatus(cfg *shared.ModelProviderConfigSchema, authVa
}
}
-func MustVerifyAuthVars() map[string]string {
- return mustVerifyAuthVars(false)
+func MustVerifyAuthVars(integratedModels bool) map[string]string {
+ return mustVerifyAuthVars(integratedModels, false)
}
-func MustVerifyAuthVarsSilent() map[string]string {
- return mustVerifyAuthVars(true)
+func MustVerifyAuthVarsSilent(integratedModels bool) map[string]string {
+ return mustVerifyAuthVars(integratedModels, true)
}
-func mustVerifyAuthVars(silent bool) map[string]string {
+func mustVerifyAuthVars(integratedModels, silent bool) map[string]string {
if !silent {
term.StartSpinner("")
}
@@ -203,6 +203,24 @@ func mustVerifyAuthVars(silent bool) map[string]string {
}
}
+ // For IntegratedModelsMode on Cloud, we only send the connected Claude subscription api key—nothing else
+ // If we're in IntegratedModelsMode and there's no connected Claude sub, return nil
+ if integratedModels {
+ if orgUserConfig.UseClaudeSubscription {
+ creds, err := GetAccountCredentials()
+ if err != nil {
+ term.OutputErrorAndExit("Error getting Claude subscription credentials: %v", err)
+ }
+
+ if creds != nil && creds.ClaudeMax != nil {
+ return map[string]string{
+ shared.AnthropicClaudeMaxTokenEnvVar: creds.ClaudeMax.AccessToken,
+ }
+ }
+ }
+ return nil
+ }
+
checkResult, err := CheckCredentialStatus(opts, orgUserConfig.UseClaudeSubscription)
if err != nil {
term.OutputErrorAndExit("Error checking API keys/credentials: %v", err)
diff --git a/app/server/handlers/client_helper.go b/app/server/handlers/client_helper.go
index 0823a94a..30384a79 100644
--- a/app/server/handlers/client_helper.go
+++ b/app/server/handlers/client_helper.go
@@ -35,7 +35,7 @@ func initClients(params initClientsParams) initClientsResult {
settings := params.settings
orgUserConfig := params.orgUserConfig
- var authVars map[string]string
+ authVars := map[string]string{}
if params.authVars != nil {
authVars = params.authVars
} else if params.apiKeys != nil {
@@ -60,7 +60,14 @@ func initClients(params initClientsParams) initClientsResult {
}
if hookResult.GetIntegratedModelsResult != nil && hookResult.GetIntegratedModelsResult.IntegratedModelsMode {
- authVars = hookResult.GetIntegratedModelsResult.AuthVars
+ merged := map[string]string{}
+ for k, v := range hookResult.GetIntegratedModelsResult.AuthVars {
+ merged[k] = v
+ }
+ if authVars[shared.AnthropicClaudeMaxTokenEnvVar] != "" {
+ merged[shared.AnthropicClaudeMaxTokenEnvVar] = authVars[shared.AnthropicClaudeMaxTokenEnvVar]
+ }
+ authVars = merged
}
if len(authVars) == 0 && os.Getenv("IS_CLOUD") != "" {
log.Println("No api keys/credentials provided for models")
diff --git a/app/server/model/client.go b/app/server/model/client.go
index f201a3c1..08f87a5d 100644
--- a/app/server/model/client.go
+++ b/app/server/model/client.go
@@ -152,9 +152,24 @@ func CreateChatCompletionStream(
}
return withStreamingRetries(ctx, func(numTotalRetry int, didProviderFallback bool, modelErr *shared.ModelError) (*ExtendedChatCompletionStream, shared.FallbackResult, error) {
- handleClaudeMaxRateLimitedIfNeeded(modelErr, modelConfig, authVars, settings, orgUserConfig, currentOrgId, currentUserId)
-
- fallbackRes := modelConfig.GetFallbackForModelError(numTotalRetry, didProviderFallback, modelErr, authVars, settings, orgUserConfig)
+ handleClaudeMaxRateLimitedIfNeeded(
+ modelErr,
+ modelConfig,
+ authVars,
+ settings,
+ orgUserConfig,
+ currentOrgId,
+ currentUserId,
+ )
+
+ fallbackRes := modelConfig.GetFallbackForModelError(
+ numTotalRetry,
+ didProviderFallback,
+ modelErr,
+ authVars,
+ settings,
+ orgUserConfig,
+ )
resolvedModelConfig := fallbackRes.ModelRoleConfig
if resolvedModelConfig == nil {
diff --git a/app/server/model/client_stream.go b/app/server/model/client_stream.go
index dccdee6a..c6575658 100644
--- a/app/server/model/client_stream.go
+++ b/app/server/model/client_stream.go
@@ -290,7 +290,7 @@ func withStreamingRetries[T any](
log.Printf("Error in streaming operation: %v, isFallback: %t, numTotalRetry: %d, numFallbackRetry: %d, numRetry: %d, compareRetries: %d, maxRetries: %d\n", err, isFallback, numTotalRetry, numFallbackRetry, numRetry, compareRetries, maxRetries)
- classifyRes := classifyBasicError(err)
+ classifyRes := classifyBasicError(err, fallbackRes.BaseModelConfig.HasClaudeMaxAuth)
modelErr = &classifyRes
newFallback := false
diff --git a/app/server/model/plan/tell_stream_main.go b/app/server/model/plan/tell_stream_main.go
index f6f79473..6bdba011 100644
--- a/app/server/model/plan/tell_stream_main.go
+++ b/app/server/model/plan/tell_stream_main.go
@@ -162,7 +162,8 @@ mainLoop:
if response.Error != nil {
log.Println("listenStream - stream finished with error", spew.Sdump(response.Error))
- modelErr := model.ClassifyModelError(response.Error.Code, response.Error.Message, nil)
+ baseModelConfig := state.fallbackRes.BaseModelConfig
+ modelErr := model.ClassifyModelError(response.Error.Code, response.Error.Message, nil, baseModelConfig.HasClaudeMaxAuth)
res := state.onError(onErrorParams{
streamErr: fmt.Errorf("The AI model (%s/%s) stopped streaming with error code %d: %s", modelProvider, modelName, response.Error.Code, response.Error.Message),
diff --git a/app/shared/ai_models_errors.go b/app/shared/ai_models_errors.go
index 5d67d6ac..d157b0af 100644
--- a/app/shared/ai_models_errors.go
+++ b/app/shared/ai_models_errors.go
@@ -37,6 +37,7 @@ type FallbackResult struct {
HasErrorFallback bool
IsFallback bool
FallbackType FallbackType
+ BaseModelConfig *BaseModelConfig
}
const MAX_RETRIES_BEFORE_FALLBACK = 1
@@ -52,6 +53,7 @@ func (m *ModelRoleConfig) GetFallbackForModelError(
if m == nil || modelErr == nil {
return FallbackResult{
ModelRoleConfig: m,
+ BaseModelConfig: m.GetBaseModelConfig(authVars, settings, orgUserConfig),
IsFallback: false,
}
}
@@ -59,6 +61,7 @@ func (m *ModelRoleConfig) GetFallbackForModelError(
if m.LargeContextFallback != nil {
return FallbackResult{
ModelRoleConfig: m.LargeContextFallback,
+ BaseModelConfig: m.LargeContextFallback.GetBaseModelConfig(authVars, settings, orgUserConfig),
FallbackType: FallbackTypeContext,
IsFallback: true,
}
@@ -67,6 +70,7 @@ func (m *ModelRoleConfig) GetFallbackForModelError(
if m.ErrorFallback != nil {
return FallbackResult{
ModelRoleConfig: m.ErrorFallback,
+ BaseModelConfig: m.ErrorFallback.GetBaseModelConfig(authVars, settings, orgUserConfig),
FallbackType: FallbackTypeError,
IsFallback: true,
}
@@ -82,6 +86,7 @@ func (m *ModelRoleConfig) GetFallbackForModelError(
if providerFallback != nil {
return FallbackResult{
ModelRoleConfig: providerFallback,
+ BaseModelConfig: providerFallback.GetBaseModelConfig(authVars, settings, orgUserConfig),
FallbackType: FallbackTypeProvider,
IsFallback: true,
}