Skip to content

Commit d8eda45

Browse files
committed
Bug 2021177 - Adds FxA implementation of the MlpaTokenProvider
1 parent 4bade96 commit d8eda45

9 files changed

Lines changed: 111 additions & 38 deletions

File tree

mobile/android/android-components/components/lib/llm-mlpa/src/main/java/mozilla/components/lib/llm/mlpa/service/FetchClientMlpaService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class FetchClientMlpaService(
7474
url = "${config.baseUrl}/v1/chat/completions",
7575
method = Request.Method.POST,
7676
headers = MutableHeaders(
77-
"authorization" to authorizationToken.value,
77+
"authorization" to "Bearer ${authorizationToken.value}",
7878
"content-type" to "application/json",
7979
"service-type" to "s2s",
8080
),

mobile/android/fenix/app/src/main/java/org/mozilla/fenix/components/Components.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import org.mozilla.fenix.components.appstate.AppAction
4949
import org.mozilla.fenix.components.appstate.AppState
5050
import org.mozilla.fenix.components.appstate.setup.checklist.SetupChecklistState
5151
import org.mozilla.fenix.components.appstate.setup.checklist.getSetupChecklistCollection
52+
import org.mozilla.fenix.components.llm.Llm
53+
import org.mozilla.fenix.components.llm.ext.accessTokenProvider
5254
import org.mozilla.fenix.components.metrics.MetricsMiddleware
5355
import org.mozilla.fenix.crashes.CrashReportingAppMiddleware
5456
import org.mozilla.fenix.crashes.SettingsCrashReportCache
@@ -410,7 +412,12 @@ class Components(private val context: Context) {
410412
}
411413

412414
val llm: Llm by lazyMonitored {
413-
Llm(core.client, integrityClient, clientUUID)
415+
Llm(
416+
client = core.client,
417+
fxaTokenProvider = backgroundServices.accountManager.accessTokenProvider,
418+
integrityClient = integrityClient,
419+
userIdProvider = clientUUID,
420+
)
414421
}
415422

416423
private val clientUUID by lazyMonitored { ClientUUID.build(context) }

mobile/android/fenix/app/src/main/java/org/mozilla/fenix/components/Llm.kt renamed to mobile/android/fenix/app/src/main/java/org/mozilla/fenix/components/llm/FenixMlpaService.kt

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
package org.mozilla.fenix.components
5+
package org.mozilla.fenix.components.llm
66

77
import mozilla.components.concept.fetch.Client
8-
import mozilla.components.concept.integrity.IntegrityClient
9-
import mozilla.components.lib.llm.mlpa.MlpaLlmProvider
10-
import mozilla.components.lib.llm.mlpa.MlpaTokenProvider
11-
import mozilla.components.lib.llm.mlpa.UserIdProvider
128
import mozilla.components.lib.llm.mlpa.service.AuthenticationService
139
import mozilla.components.lib.llm.mlpa.service.AuthorizationToken
1410
import mozilla.components.lib.llm.mlpa.service.ChatService
1511
import mozilla.components.lib.llm.mlpa.service.FetchClientMlpaService
1612
import mozilla.components.lib.llm.mlpa.service.MlpaConfig
1713
import mozilla.components.lib.llm.mlpa.service.MlpaService
18-
import mozilla.components.lib.llm.mlpa.service.PackageName
19-
import org.mozilla.fenix.BuildConfig
20-
import org.mozilla.fenix.perf.lazyMonitored
2114

2215
/**
2316
* Temporary class for toggling between prod and nonprod MLPA environment
@@ -36,26 +29,3 @@ class FenixMlpaService(
3629
request: ChatService.Request,
3730
) = service.completion(authorizationToken, request)
3831
}
39-
40-
/**
41-
* Component group for LLM services.
42-
*/
43-
class Llm(
44-
private val client: Client,
45-
private val integrityClient: IntegrityClient,
46-
private val userIdProvider: UserIdProvider,
47-
) {
48-
49-
val fenixMlpaService by lazyMonitored { FenixMlpaService(client) }
50-
val mlpaProvider: MlpaLlmProvider by lazyMonitored {
51-
MlpaLlmProvider(
52-
MlpaTokenProvider.mlpaIntegrityHandshake(
53-
integrityClient = integrityClient,
54-
authenticationService = fenixMlpaService,
55-
userIdProvider = userIdProvider,
56-
packageName = PackageName(BuildConfig.APPLICATION_ID),
57-
),
58-
fenixMlpaService,
59-
)
60-
}
61-
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
package org.mozilla.fenix.components.llm
6+
7+
import mozilla.components.concept.fetch.Client
8+
import mozilla.components.concept.integrity.IntegrityClient
9+
import mozilla.components.lib.llm.mlpa.MlpaLlmProvider
10+
import mozilla.components.lib.llm.mlpa.MlpaTokenProvider
11+
import mozilla.components.lib.llm.mlpa.UserIdProvider
12+
import mozilla.components.lib.llm.mlpa.service.PackageName
13+
import org.mozilla.fenix.BuildConfig
14+
import org.mozilla.fenix.components.llm.ext.FxaAccessTokenProvider
15+
import org.mozilla.fenix.components.llm.ext.choose
16+
import org.mozilla.fenix.components.llm.ext.fxaTokenProvider
17+
import org.mozilla.fenix.perf.lazyMonitored
18+
19+
/**
20+
* Component group for LLM services.
21+
*/
22+
class Llm(
23+
private val client: Client,
24+
private val fxaTokenProvider: FxaAccessTokenProvider,
25+
private val integrityClient: IntegrityClient,
26+
private val userIdProvider: UserIdProvider,
27+
) {
28+
29+
val fenixMlpaService by lazyMonitored { FenixMlpaService(client) }
30+
31+
val mlpaProvider: MlpaLlmProvider by lazyMonitored {
32+
MlpaLlmProvider(
33+
MlpaTokenProvider.choose(
34+
MlpaTokenProvider.fxaTokenProvider(fxaTokenProvider),
35+
MlpaTokenProvider.mlpaIntegrityHandshake(
36+
integrityClient = integrityClient,
37+
authenticationService = fenixMlpaService,
38+
userIdProvider = userIdProvider,
39+
packageName = PackageName(BuildConfig.APPLICATION_ID),
40+
),
41+
),
42+
fenixMlpaService,
43+
)
44+
}
45+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
package org.mozilla.fenix.components.llm.ext
6+
7+
import mozilla.components.service.fxa.manager.FxaAccountManager
8+
import mozilla.components.service.fxa.manager.SCOPE_PROFILE
9+
10+
internal val FxaAccountManager.accessTokenProvider get() = FxaAccessTokenProvider {
11+
authenticatedAccount()
12+
?.getAccessToken(SCOPE_PROFILE)
13+
?.token
14+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
package org.mozilla.fenix.components.llm.ext
6+
7+
import mozilla.components.lib.llm.mlpa.MlpaTokenProvider
8+
import mozilla.components.lib.llm.mlpa.service.AuthorizationToken
9+
10+
internal class AllTokenProvidersFailed : IllegalStateException("All token providers failed to retrieve a token.")
11+
internal class FxaMissingAccessToken : IllegalStateException("Unable to get access token from FxaAccessTokenProvider")
12+
13+
/** Convenience interface for getting an fxa access token. */
14+
fun interface FxaAccessTokenProvider {
15+
/** Returns an access token or null */
16+
suspend fun provide(): String?
17+
}
18+
19+
/** Implementation of [MlpaTokenProvider] that takes the first successful token it receives.
20+
* @param tokenProviders a list of [MlpaTokenProvider].
21+
* @return an [MlpaTokenProvider].
22+
*/
23+
fun MlpaTokenProvider.Companion.choose(vararg tokenProviders: MlpaTokenProvider) = MlpaTokenProvider {
24+
tokenProviders.firstNotNullOfOrNull { provider ->
25+
provider.fetchToken().takeIf { it.isSuccess }
26+
} ?: Result.failure(AllTokenProvidersFailed())
27+
}
28+
29+
/** Implementation of [MlpaTokenProvider] that tries to fetch an fxa access token.
30+
* @param tokenProvider a list of [FxaAccessTokenProvider].
31+
* @return an [MlpaTokenProvider].
32+
*/
33+
fun MlpaTokenProvider.Companion.fxaTokenProvider(tokenProvider: FxaAccessTokenProvider) = MlpaTokenProvider {
34+
tokenProvider.provide()?.let {
35+
Result.success(AuthorizationToken(it))
36+
} ?: Result.failure(FxaMissingAccessToken())
37+
}

mobile/android/fenix/app/src/main/java/org/mozilla/fenix/debugsettings/llm/LlmTools.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import mozilla.components.concept.llm.Llm
2525
import mozilla.components.concept.llm.Prompt
2626
import mozilla.components.lib.llm.mlpa.MlpaLlmProvider
2727
import org.mozilla.fenix.R
28-
import org.mozilla.fenix.components.Llm as LlmComponent
28+
import org.mozilla.fenix.components.llm.Llm as LlmComponent
2929

3030
/**
3131
* Debug drawer view to test an [IntegrityClient].

mobile/android/fenix/app/src/main/java/org/mozilla/fenix/debugsettings/navigation/DebugDrawerRoute.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import mozilla.components.concept.integrity.IntegrityClient
1212
import mozilla.components.concept.storage.CreditCardsAddressesStorage
1313
import mozilla.components.concept.storage.LoginsStorage
1414
import org.mozilla.fenix.R
15-
import org.mozilla.fenix.components.Llm
15+
import org.mozilla.fenix.components.llm.Llm
1616
import org.mozilla.fenix.debugsettings.addons.ui.AddonsDebugToolsScreen
1717
import org.mozilla.fenix.debugsettings.addresses.AddressesDebugRegionRepository
1818
import org.mozilla.fenix.debugsettings.addresses.AddressesTools

mobile/android/fenix/app/src/main/java/org/mozilla/fenix/debugsettings/ui/FenixOverlay.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import mozilla.components.concept.storage.CreditCardsAddressesStorage
2828
import mozilla.components.concept.storage.LoginsStorage
2929
import mozilla.telemetry.glean.Glean
3030
import org.mozilla.fenix.R
31-
import org.mozilla.fenix.components.Llm
31+
import org.mozilla.fenix.components.llm.Llm
3232
import org.mozilla.fenix.debugsettings.addresses.AddressesDebugRegionRepository
3333
import org.mozilla.fenix.debugsettings.addresses.AddressesTools
3434
import org.mozilla.fenix.debugsettings.addresses.FakeAddressesDebugRegionRepository
@@ -222,6 +222,6 @@ private fun FenixOverlayPreview() {
222222
addressesDebugRegionRepository = FakeAddressesDebugRegionRepository(),
223223
creditCardsAddressesStorage = FakeCreditCardsAddressesStorage(),
224224
integrityClient = IntegrityClient.testSuccess,
225-
llm = Llm(FakeClient(), FakeIntegrityClient(), FakeUserIdProvider()),
225+
llm = Llm(FakeClient(), { null }, FakeIntegrityClient(), FakeUserIdProvider()),
226226
)
227-
}
227+
}

0 commit comments

Comments
 (0)