@@ -81,38 +81,6 @@ final class OnboardingAISetupModel {
8181 private( set) var manualError : Failure ?
8282 var showManualEntry = false
8383
84- var selectedManualProvider : ManualProvider ? {
85- self . manualProviders . first { $0 . id == self . manualProviderID }
86- }
87-
88- var prepareOptions : [ PrepareOption ] {
89- guard self . prepareAvailable else { return [ ] }
90- return Self . prepareOptions (
91- candidates : self . candidates ,
92- advertisedOptions : self . detectedPrepareOptions )
93- }
94-
95- var isPreparingModel : Bool {
96- self . providerWizardKind == . prepare
97- }
98-
99- var connected : Bool {
100- self . phase == . connected
101- }
102-
103- var isBusy : Bool {
104- self . phase == . detecting || self . phase == . testing || self . manualTesting || self . authBusy ||
105- self . pendingActivationVerification
106- }
107-
108- /// Once setup starts changing inference, its successful result belongs to
109- /// OpenClaw rather than the existing-Gateway onboarding bypass.
110- var ownsInferenceTransition : Bool {
111- ( self . phase == . detecting && self . configuredGatewayBlocker == nil ) ||
112- self . phase == . testing || self . manualTesting || self . authBusy || self . connected ||
113- self . pendingActivationVerification
114- }
115-
11684 /// Called when a candidate connects so the page can advance.
11785 var onConnected : ( ( ) -> Void ) ?
11886 /// Called whenever setup enters the read-only wait for an ambiguous
@@ -140,28 +108,6 @@ final class OnboardingAISetupModel {
140108 /// Only a just-completed provider flow may trust setupComplete without re-probing.
141109 @ ObservationIgnored private var providerAuthReconciliationPending = false
142110
143- /// Internal (not private): the persisted-transition helper lives in
144- /// OnboardingAISetupSupport.swift with the other pure statics.
145- struct PersistedActivationState : Equatable {
146- let setupComplete : Bool
147- let configuredModel : String ?
148- }
149-
150- private struct AttemptContext : Equatable {
151- let token : UUID
152- let routeIdentity : String
153- }
154-
155- private struct PendingVerification {
156- let context : AttemptContext
157- let task : Task < PendingVerificationOutcome , Never >
158- }
159-
160- private struct CompletedHandoff {
161- let routeIdentity : String
162- let activationOwner : OnboardingSystemAgentResumeStore . ActivationOwner ?
163- }
164-
165111 init (
166112 gateway : GatewayConnection = . shared ,
167113 defaults : UserDefaults = . standard ,
@@ -178,44 +124,6 @@ final class OnboardingAISetupModel {
178124 self . connectionModeProvider = connectionModeProvider
179125 }
180126
181- private struct DetectResult : Decodable {
182- struct DetectedCandidate : Decodable {
183- let icon : String ?
184- let website : String ?
185- let kind : String
186- let label : String
187- let detail : String
188- let modelRef : String
189- let credentials : Bool ?
190- }
191-
192- let candidates : [ DetectedCandidate ]
193- let unavailableCandidates : [ UnavailableCandidate ] ?
194- let manualProviders : [ ManualProvider ] ?
195- let authOptions : [ AuthOption ] ?
196- let prepareOptions : [ PrepareOption ] ?
197- let recommendedInstalls : [ RecommendedInstall ] ?
198- let configuredModel : String ?
199- let setupComplete : Bool ?
200-
201- var persistedActivationState : PersistedActivationState ? {
202- self . setupComplete . map {
203- PersistedActivationState (
204- setupComplete : $0 ,
205- configuredModel : self . configuredModel )
206- }
207- }
208- }
209-
210- struct ActivateResult : Decodable {
211- let ok : Bool
212- let modelRef : String ?
213- let latencyMs : Double ?
214- let lines : [ String ] ?
215- let status : String ?
216- let error : String ?
217- }
218-
219127 func startIfNeeded ( ) {
220128 if self . waitingForPendingActivationDeadline {
221129 self . resetForGatewayChange ( clearPendingHandoff : false )
@@ -805,7 +713,7 @@ extension OnboardingAISetupModel {
805713 if let preparedChoiceID {
806714 // Detection kinds encode the provider-auth choice ID, while
807715 // PrepareOption.brandId owns the model-ref namespace.
808- let preparedKind = " provider-auto: \( preparedChoiceID ) "
716+ let preparedKind = Self . providerAutoSetupKind ( choiceID : preparedChoiceID )
809717 if let prepared = candidates . first ( where : {
810718 $0 . kind == preparedKind && $0 . credentials != false
811719 } ) {
@@ -907,8 +815,29 @@ extension OnboardingAISetupModel {
907815
908816 private func activate ( kind : String , context : AttemptContext ) async {
909817 guard self . isCurrentAttempt ( context ) , ! Task . isCancelled else { return }
910- guard let candidate = candidates . first ( where : { $0 . kind == kind } ) ,
911- let lease = serverLease ,
818+ guard let candidate = candidates . first ( where : { $0 . kind == kind } )
819+ else {
820+ requireFreshDetection ( after : Self . transportFailure (
821+ " The Gateway connection changed. Check for AI accounts again. " ) )
822+ return
823+ }
824+ await self . activate (
825+ kind : kind ,
826+ modelRef : candidate . modelRef ,
827+ label : candidate . label ,
828+ tryNextCandidateOnFailure : true ,
829+ context : context )
830+ }
831+
832+ private func activate (
833+ kind : String ,
834+ modelRef : String ,
835+ label : String ,
836+ tryNextCandidateOnFailure : Bool ,
837+ context : AttemptContext ) async
838+ {
839+ guard self . isCurrentAttempt ( context ) , ! Task . isCancelled else { return }
840+ guard let lease = serverLease ,
912841 await gateway . isCurrentServerLease ( lease )
913842 else {
914843 requireFreshDetection ( after : Self . transportFailure (
@@ -934,15 +863,17 @@ extension OnboardingAISetupModel {
934863 guard let routeFingerprint = await gateway . activationOwnershipFingerprint (
935864 ifCurrentServerLease : lease )
936865 else {
937- self . statuses [ kind ] = . failed ( Self . transportFailure (
938- " Secure storage is unavailable, so OpenClaw cannot safely resume this AI setup. " ) )
866+ let failure = Self . transportFailure (
867+ " Secure storage is unavailable, so OpenClaw cannot safely resume this AI setup. " )
868+ self . statuses [ kind ] = . failed ( failure )
869+ self . exposeActivationFailure ( failure , whenTerminal : ! tryNextCandidateOnFailure )
939870 self . phase = . ready
940871 return
941872 }
942873 guard self . isCurrentAttempt ( context ) , ! Task . isCancelled else { return }
943874 let params = Self . activationParams (
944875 kind : kind ,
945- modelRef : candidate . modelRef ,
876+ modelRef : modelRef ,
946877 supportsExactModel : supportsExactModel )
947878 let activationOwner = OnboardingSystemAgentResumeStore . ActivationOwner (
948879 id : UUID ( ) . uuidString ,
@@ -957,8 +888,10 @@ extension OnboardingAISetupModel {
957888 activationTimeoutMs : requestTimeoutMs ,
958889 defaults : defaults )
959890 else {
960- self . statuses [ kind ] = . failed ( Self . transportFailure (
961- " No Gateway is selected. Select a Gateway, then try again. " ) )
891+ let failure = Self . transportFailure (
892+ " No Gateway is selected. Select a Gateway, then try again. " )
893+ self . statuses [ kind ] = . failed ( failure )
894+ self . exposeActivationFailure ( failure , whenTerminal : ! tryNextCandidateOnFailure )
962895 self . phase = . ready
963896 return
964897 }
@@ -999,11 +932,18 @@ extension OnboardingAISetupModel {
999932 } else {
1000933 self . pendingActivationVerification = false
1001934 self . clearPendingHandoff ( ifOwnedBy : context , activationOwner : activationOwner )
1002- self . statuses [ kind ] = . failed ( Self . failure (
1003- label : self . candidates . first { $0 . kind == kind } ? . label ?? kind ,
935+ let failure = Self . failure (
936+ label : label ,
1004937 status : result . status ,
1005- error : result . error ) )
1006- await tryNextAfterFailure ( of : kind , context : context )
938+ error : result . error )
939+ self . statuses [ kind ] = . failed ( failure )
940+ if tryNextCandidateOnFailure {
941+ await tryNextAfterFailure ( of : kind , context : context )
942+ } else {
943+ self . phase = . ready
944+ self . detectError = failure
945+ self . showManualEntry = ! self . manualProviders . isEmpty
946+ }
1007947 }
1008948 } catch {
1009949 guard self . isCurrentAttempt ( context ) else { return }
@@ -1012,6 +952,7 @@ extension OnboardingAISetupModel {
1012952 // this activation and its credential mutation completed safely.
1013953 let failure = Self . transportFailure ( error . localizedDescription )
1014954 self . statuses [ kind ] = . failed ( failure )
955+ self . exposeActivationFailure ( failure , whenTerminal : ! tryNextCandidateOnFailure )
1015956 if Self . activationFailureIsDefinitive ( error ) {
1016957 self . pendingActivationVerification = false
1017958 self . clearPendingHandoff ( ifOwnedBy : context , activationOwner : activationOwner )
@@ -1028,6 +969,7 @@ extension OnboardingAISetupModel {
1028969 await ! ( self . gateway . isCurrentServerLease ( lease ) ) ,
1029970 await self . reconcileActivationAfterGatewayRestart (
1030971 kind : kind ,
972+ expectedModel : modelRef ,
1031973 context : context ,
1032974 activationOwner : activationOwner ,
1033975 before : persistedStateBeforeActivation ,
@@ -1045,8 +987,14 @@ extension OnboardingAISetupModel {
1045987 }
1046988 }
1047989
990+ private func exposeActivationFailure ( _ failure : Failure , whenTerminal : Bool ) {
991+ guard whenTerminal else { return }
992+ self . detectError = failure
993+ }
994+
1048995 private func reconcileActivationAfterGatewayRestart (
1049996 kind : String ,
997+ expectedModel : String ,
1050998 context : AttemptContext ,
1051999 activationOwner : OnboardingSystemAgentResumeStore . ActivationOwner ,
10521000 before : PersistedActivationState ? ,
@@ -1067,6 +1015,7 @@ extension OnboardingAISetupModel {
10671015 timeoutMs : Double ( leaseTimeoutMs ) ) ,
10681016 await reconcilePersistedActivation (
10691017 kind : kind ,
1018+ expectedModel : expectedModel ,
10701019 context : context ,
10711020 activationOwner : activationOwner ,
10721021 before : before ,
@@ -1096,21 +1045,21 @@ extension OnboardingAISetupModel {
10961045
10971046 private func reconcilePersistedActivation (
10981047 kind : String ,
1048+ expectedModel : String ,
10991049 context : AttemptContext ,
11001050 activationOwner : OnboardingSystemAgentResumeStore . ActivationOwner ,
11011051 before : PersistedActivationState ? ,
11021052 serverLease : GatewayConnection . ServerLease ,
11031053 timeoutMs : Int ) async -> Bool
11041054 {
11051055 guard timeoutMs > 0 ,
1106- let expectedModel = candidates . first ( where : { $0 . kind == kind } ) ? . modelRef ,
1107- isCurrentAttempt ( context ) ,
1056+ self . isCurrentAttempt ( context ) ,
11081057 ! Task . isCancelled ,
11091058 OnboardingSystemAgentResumeStore . isOwned (
11101059 by : activationOwner ,
11111060 for : context . routeIdentity ,
1112- defaults : defaults ) ,
1113- await gateway . activationOwnershipFingerprint ( ifCurrentServerLease : serverLease ) ==
1061+ defaults : self . defaults ) ,
1062+ await self . gateway . activationOwnershipFingerprint ( ifCurrentServerLease : serverLease ) ==
11141063 activationOwner . routeFingerprint
11151064 else { return false }
11161065 guard let detectData = try? await gateway . request (
@@ -1215,7 +1164,8 @@ extension OnboardingAISetupModel {
12151164 done : result . done ,
12161165 step : result . step ,
12171166 status : wizardStatusString ( result . status ) ,
1218- error : result . error )
1167+ error : result . error ,
1168+ preparedModelRef : result . preparedmodelref )
12191169 } catch {
12201170 // The Gateway session survives socket loss; cancel by its known
12211171 // id before reporting failure so it cannot persist config later.
@@ -1322,7 +1272,8 @@ extension OnboardingAISetupModel {
13221272 done : result . done ,
13231273 step : result . step ,
13241274 status : wizardStatusString ( result . status ) ,
1325- error : result . error )
1275+ error : result . error ,
1276+ preparedModelRef : result . preparedmodelref )
13261277 } catch {
13271278 let cancellation = await self . gateway . cancelWizardSession ( sessionID , on : serverLease )
13281279 guard token == self . attemptToken , authAttemptID == self . authAttemptID else { return }
@@ -1347,7 +1298,8 @@ extension OnboardingAISetupModel {
13471298 done : Bool ,
13481299 step : WizardStep ? ,
13491300 status : String ? ,
1350- error : String ? )
1301+ error : String ? ,
1302+ preparedModelRef : String ? )
13511303 {
13521304 self . authBusy = false
13531305 let validationError = ! done && status == " running " && error ? . isEmpty == false
@@ -1371,8 +1323,30 @@ extension OnboardingAISetupModel {
13711323 let preparedProvider = self . providerWizardKind == . prepare
13721324 ? self . activeAuthOption . map { ( id : $0 . id , label : $0 . label ) }
13731325 : nil
1326+ let preparedModel = preparedModelRef ?
1327+ . trimmingCharacters ( in : . whitespacesAndNewlines )
13741328 self . providerAuthReconciliationPending = self . providerWizardKind == . auth
13751329 self . clearProviderAuth ( )
1330+ if let preparedProvider ,
1331+ let preparedModel ,
1332+ ! preparedModel . isEmpty
1333+ {
1334+ guard let context = self . captureAttemptContext ( ) else {
1335+ self . failDetectionForMissingRoute ( )
1336+ return
1337+ }
1338+ let kind = Self . providerAutoSetupKind ( choiceID : preparedProvider . id )
1339+ self . statuses [ kind ] = . untried
1340+ Task {
1341+ await self . activate (
1342+ kind : kind ,
1343+ modelRef : preparedModel ,
1344+ label : preparedProvider . label ,
1345+ tryNextCandidateOnFailure : false ,
1346+ context : context )
1347+ }
1348+ return
1349+ }
13761350 self . scheduleDetection (
13771351 preparedChoiceID : preparedProvider ? . id ,
13781352 preparedProviderLabel : preparedProvider ? . label )
@@ -1462,8 +1436,18 @@ extension OnboardingAISetupModel {
14621436 self . authBusy = true
14631437 }
14641438
1465- func _test_applyAuthWizardResult ( done : Bool , status : String ? , error : String ? ) {
1466- self . applyAuthWizardResult ( done : done , step : nil , status : status , error : error )
1439+ func _test_applyAuthWizardResult (
1440+ done : Bool ,
1441+ status : String ? ,
1442+ error : String ? ,
1443+ preparedModelRef : String ? = nil )
1444+ {
1445+ self . applyAuthWizardResult (
1446+ done : done ,
1447+ step : nil ,
1448+ status : status ,
1449+ error : error ,
1450+ preparedModelRef : preparedModelRef )
14671451 }
14681452
14691453 var _test_authSessionID : String ? {
0 commit comments