Before

…lines 1–4 unchanged (4 lines)
5package mozilla.components.lib.llm.mlpa
6
7import kotlinx.coroutines.flow.MutableStateFlow
8import kotlinx.coroutines.flow.StateFlow
9import mozilla.components.concept.llm.CloudLlmProvider
10import mozilla.components.concept.llm.CloudLlmProvider.State
11import mozilla.components.concept.llm.LlmProvider
12import mozilla.components.lib.llm.mlpa.service.ChatService
13import mozilla.components.lib.llm.mlpa.service.ChatServiceError
14import mozilla.components.lib.llm.mlpa.service.ChatServiceException
15import mozilla.components.lib.llm.mlpa.service.MlpaService
16
17/**
18 * [CloudLlmProvider] implementation backed by MLPA services.
19 *
20 * This provider is responsible for:
…lines 21–28 unchanged (8 lines)
29 *
30 * @property tokenProvider Responsible for fetching the MLPA authentication token.
31 * @property mlpaService Service used to construct the [MlpaLlm] instance once authenticated.
32 */
33class MlpaLlmProvider(
34 val tokenProvider: MlpaTokenProvider,
35 val storage: MlpaTokenStorage,
36 val mlpaService: MlpaService,
37) : CloudLlmProvider {
38 override val info = LlmProvider.Info(nameRes = R.string.mlpa_llm_provider_name, iconRes = R.drawable.firefox_icon)
39 private val _state = MutableStateFlow<State>(State.Available)
40
41 /**
42 * The current state.
43 */
44 override val state: StateFlow<State> = _state
45
46 /**
47 * Prepares the provider for use.
48 *
49 * This function attempts to fetch an authentication token using [tokenProvider].
50 *
51 * - On success, updates [state] to [State.Ready] with a newly created [MlpaLlm].
52 * - On failure, updates [state] to [State.Unavailable].
53 */
54 override suspend fun prepare() {
55 tokenProvider.fetchToken()
56 .onSuccess { _state.value = State.Ready(MlpaLlm(chatService, it)) }
57 .onFailure { _state.value = State.Unavailable }
58 }
59
60 /**
61 * Wraps the [ChatService]
62 */
63 private val chatService = ChatService { token, request ->
64 val response = mlpaService.completion(token, request)
65 response.onFailure {
66 if (it.isRetryable) {
67 storage.clear()
68 _state.value = State.Available
69 } else {
70 _state.value = State.Unavailable
71 }
72 }
73 return@ChatService response
74 }
75}
76
77private val Throwable.isRetryable get() = (this as? ChatServiceException)?.error is ChatServiceError.InvalidToken
78

After

…lines 1–4 unchanged (4 lines)
5package mozilla.components.lib.llm.mlpa
6
7import kotlinx.coroutines.flow.MutableStateFlow
8import kotlinx.coroutines.flow.StateFlow
9import mozilla.components.concept.llm.CloudLlmProvider
10import mozilla.components.concept.llm.CloudLlmProvider.State
11import mozilla.components.concept.llm.CloudLlmProvider.State.Ready
12import mozilla.components.concept.llm.CloudLlmProvider.State.Unavailable
13import mozilla.components.concept.llm.LlmProvider
14import mozilla.components.lib.llm.mlpa.service.MlpaService
15
16/**
17 * [CloudLlmProvider] implementation backed by MLPA services.
18 *
19 * This provider is responsible for:
20 * - Fetching an authentication token via [MlpaTokenProvider].
…lines 21–27 unchanged (7 lines)
28 *
29 * @property tokenProvider Responsible for fetching the MLPA authentication token.
30 * @property mlpaService Service used to construct the [MlpaLlm] instance once authenticated.
31 */
32class MlpaLlmProvider(
33 val tokenProvider: MlpaTokenProvider,
34 val mlpaService: MlpaService,
35) : CloudLlmProvider {
36 override val info = LlmProvider.Info(nameRes = R.string.mlpa_llm_provider_name, iconRes = R.drawable.firefox_icon)
37 private val _state = MutableStateFlow<State>(State.Available)
38
39 /**
40 * The current state.
41 */
42 override val state: StateFlow<State> = _state
43
44 /**
45 * Prepares the provider for use.
46 *
47 * This function attempts to fetch an authentication token using [tokenProvider].
48 *
49 * - On success, updates [state] to [Ready] with a newly created [MlpaLlm].
50 * - On failure, updates [state] to [Unavailable].
51 */
52 override suspend fun prepare() {
53 tokenProvider.fetchToken()
54 .onSuccess { _state.value = Ready(MlpaLlm(mlpaService, it)) }
55 .onFailure { _state.value = Unavailable }
56 }
57}
58

Before

source_file
block_comment "/* This Source Code Form is subject to the terms of the Mozi"...
package_header
package "package"
qualified_identifier
identifier "mozilla"
. "."
identifier "components"
. "."
identifier "lib"
. "."
identifier "llm"
. "."
identifier "mlpa"
import
import "import"
qualified_identifier
identifier "kotlinx"
. "."
identifier "coroutines"
. "."
identifier "flow"
. "."
identifier "MutableStateFlow"
import
import "import"
qualified_identifier
identifier "kotlinx"
. "."
identifier "coroutines"
. "."
identifier "flow"
. "."
identifier "StateFlow"
import
import "import"
qualified_identifier
identifier "mozilla"
. "."
identifier "components"
. "."
identifier "concept"
. "."
identifier "llm"
. "."
identifier "CloudLlmProvider"
import
import "import"
qualified_identifier
identifier "mozilla"
. "."
identifier "components"
. "."
identifier "concept"
. "."
identifier "llm"
. "."
identifier "CloudLlmProvider"
. "."
identifier "State"
import
import "import"
qualified_identifier
identifier "mozilla"
. "."
identifier "components"
. "."
identifier "concept"
. "."
identifier "llm"
. "."
identifier "LlmProvider"
import
import "import"
qualified_identifier
identifier "mozilla"
. "."
identifier "components"
. "."
identifier "lib"
. "."
identifier "llm"
. "."
identifier "mlpa"
. "."
identifier "service"
. "."
identifier "ChatService"
import
import "import"
qualified_identifier
identifier "mozilla"
. "."
identifier "components"
. "."
identifier "lib"
. "."
identifier "llm"
. "."
identifier "mlpa"
. "."
identifier "service"
. "."
identifier "ChatServiceError"
import
import "import"
qualified_identifier
identifier "mozilla"
. "."
identifier "components"
. "."
identifier "lib"
. "."
identifier "llm"
. "."
identifier "mlpa"
. "."
identifier "service"
. "."
identifier "ChatServiceException"
import
import "import"
qualified_identifier
identifier "mozilla"
. "."
identifier "components"
. "."
identifier "lib"
. "."
identifier "llm"
. "."
identifier "mlpa"
. "."
identifier "service"
. "."
identifier "MlpaService"
block_comment "/**\n * [CloudLlmProvider] implementation backed by MLPA serv"...
class_declaration
class "class"
identifier "MlpaLlmProvider"
primary_constructor
class_parameters
( "("
class_parameter
val "val"
identifier "tokenProvider"
: ":"
user_type
identifier "MlpaTokenProvider"
, ","
class_parameter
val "val"
identifier "storage"
: ":"
user_type
identifier "MlpaTokenStorage"
, ","
class_parameter
val "val"
identifier "mlpaService"
: ":"
user_type
identifier "MlpaService"
, ","
) ")"
: ":"
delegation_specifiers
delegation_specifier
user_type
identifier "CloudLlmProvider"
class_body
{ "{"
property_declaration (+37 nodes collapsed)
property_declaration (+21 nodes collapsed)
block_comment "/**\n * The current state.\n */"
property_declaration
modifiers
member_modifier
override "override"
val "val"
variable_declaration
identifier "state"
: ":"
user_type
identifier "StateFlow"
type_arguments
< "<"
type_projection
user_type
identifier "State"
> ">"
= "="
identifier "_state"
block_comment "/**\n * Prepares the provider for use.\n *\n * This"...
function_declaration
modifiers
member_modifier
override "override"
function_modifier
suspend "suspend"
fun "fun"
identifier "prepare"
function_value_parameters
( "("
) ")"
function_body
block
{ "{"
call_expression
navigation_expression
call_expression
navigation_expression
call_expression
navigation_expression
identifier "tokenProvider"
. "."
identifier "fetchToken"
value_arguments
( "("
) ")"
. "."
identifier "onSuccess"
annotated_lambda
lambda_literal
{ "{"
assignment
navigation_expression
identifier "_state"
. "."
identifier "value"
= "="
call_expression
navigation_expression
identifier "State"
. "."
identifier "Ready"
value_arguments
( "("
value_argument
call_expression
identifier "MlpaLlm"
value_arguments
( "("
value_argument
identifier "chatService"
, ","
value_argument
identifier "it"
) ")"
) ")"
} "}"
. "."
identifier "onFailure"
annotated_lambda
lambda_literal
{ "{"
assignment
navigation_expression
identifier "_state"
. "."
identifier "value"
= "="
navigation_expression
identifier "State"
. "."
identifier "Unavailable"
} "}"
} "}"
block_comment "/**\n * Wraps the [ChatService]\n */"
property_declaration
modifiers
visibility_modifier
private "private"
val "val"
variable_declaration
identifier "chatService"
= "="
call_expression
identifier "ChatService"
annotated_lambda
lambda_literal
{ "{"
lambda_parameters
variable_declaration
identifier "token"
, ","
variable_declaration
identifier "request"
-> "->"
property_declaration
val "val"
variable_declaration
identifier "response"
= "="
call_expression
navigation_expression
identifier "mlpaService"
. "."
identifier "completion"
value_arguments
( "("
value_argument
identifier "token"
, ","
value_argument
identifier "request"
) ")"
call_expression
navigation_expression
identifier "response"
. "."
identifier "onFailure"
annotated_lambda
lambda_literal
{ "{"
if_expression
if "if"
( "("
navigation_expression
identifier "it"
. "."
identifier "isRetryable"
) ")"
block
{ "{"
call_expression
navigation_expression
identifier "storage"
. "."
identifier "clear"
value_arguments
( "("
) ")"
assignment
navigation_expression
identifier "_state"
. "."
identifier "value"
= "="
navigation_expression
identifier "State"
. "."
identifier "Available"
} "}"
else "else"
block
{ "{"
assignment
navigation_expression
identifier "_state"
. "."
identifier "value"
= "="
navigation_expression
identifier "State"
. "."
identifier "Unavailable"
} "}"
} "}"
return_expression
return@ "return@"
identifier "ChatService"
identifier "response"
} "}"
} "}"
property_declaration
modifiers
visibility_modifier
private "private"
val "val"
user_type
identifier "Throwable"
. "."
variable_declaration
identifier "isRetryable"
getter
get "get"
( "("
) ")"
function_body
= "="
is_expression
navigation_expression
parenthesized_expression
( "("
as_expression
this_expression
this "this"
as? "as?"
user_type
identifier "ChatServiceException"
) ")"
?. "?."
identifier "error"
is "is"
user_type
identifier "ChatServiceError"
. "."
identifier "InvalidToken"

After

source_file
block_comment "/* This Source Code Form is subject to the terms of the Mozi"...
package_header
package "package"
qualified_identifier
identifier "mozilla"
. "."
identifier "components"
. "."
identifier "lib"
. "."
identifier "llm"
. "."
identifier "mlpa"
import
import "import"
qualified_identifier
identifier "kotlinx"
. "."
identifier "coroutines"
. "."
identifier "flow"
. "."
identifier "MutableStateFlow"
import
import "import"
qualified_identifier
identifier "kotlinx"
. "."
identifier "coroutines"
. "."
identifier "flow"
. "."
identifier "StateFlow"
import
import "import"
qualified_identifier
identifier "mozilla"
. "."
identifier "components"
. "."
identifier "concept"
. "."
identifier "llm"
. "."
identifier "CloudLlmProvider"
import
import "import"
qualified_identifier
identifier "mozilla"
. "."
identifier "components"
. "."
identifier "concept"
. "."
identifier "llm"
. "."
identifier "CloudLlmProvider"
. "."
identifier "State"
import
import "import"
qualified_identifier
identifier "mozilla"
. "."
identifier "components"
. "."
identifier "concept"
. "."
identifier "llm"
. "."
identifier "CloudLlmProvider"
. "."
identifier "State"
. "."
identifier "Ready"
import
import "import"
qualified_identifier
identifier "mozilla"
. "."
identifier "components"
. "."
identifier "concept"
. "."
identifier "llm"
. "."
identifier "CloudLlmProvider"
. "."
identifier "State"
. "."
identifier "Unavailable"
import
import "import"
qualified_identifier
identifier "mozilla"
. "."
identifier "components"
. "."
identifier "concept"
. "."
identifier "llm"
. "."
identifier "LlmProvider"
import
import "import"
qualified_identifier
identifier "mozilla"
. "."
identifier "components"
. "."
identifier "lib"
. "."
identifier "llm"
. "."
identifier "mlpa"
. "."
identifier "service"
. "."
identifier "MlpaService"
block_comment "/**\n * [CloudLlmProvider] implementation backed by MLPA serv"...
class_declaration
class "class"
identifier "MlpaLlmProvider"
primary_constructor
class_parameters
( "("
class_parameter
val "val"
identifier "tokenProvider"
: ":"
user_type
identifier "MlpaTokenProvider"
, ","
class_parameter
val "val"
identifier "mlpaService"
: ":"
user_type
identifier "MlpaService"
, ","
) ")"
: ":"
delegation_specifiers
delegation_specifier
user_type
identifier "CloudLlmProvider"
class_body
{ "{"
property_declaration (+37 nodes collapsed)
property_declaration (+21 nodes collapsed)
block_comment "/**\n * The current state.\n */"
property_declaration
modifiers
member_modifier
override "override"
val "val"
variable_declaration
identifier "state"
: ":"
user_type
identifier "StateFlow"
type_arguments
< "<"
type_projection
user_type
identifier "State"
> ">"
= "="
identifier "_state"
block_comment "/**\n * Prepares the provider for use.\n *\n * This"...
function_declaration
modifiers
member_modifier
override "override"
function_modifier
suspend "suspend"
fun "fun"
identifier "prepare"
function_value_parameters
( "("
) ")"
function_body
block
{ "{"
call_expression
navigation_expression
call_expression
navigation_expression
call_expression
navigation_expression
identifier "tokenProvider"
. "."
identifier "fetchToken"
value_arguments
( "("
) ")"
. "."
identifier "onSuccess"
annotated_lambda
lambda_literal
{ "{"
assignment
navigation_expression
identifier "_state"
. "."
identifier "value"
= "="
call_expression
identifier "Ready"
value_arguments
( "("
value_argument
call_expression
identifier "MlpaLlm"
value_arguments
( "("
value_argument
identifier "mlpaService"
, ","
value_argument
identifier "it"
) ")"
) ")"
} "}"
. "."
identifier "onFailure"
annotated_lambda
lambda_literal
{ "{"
assignment
navigation_expression
identifier "_state"
. "."
identifier "value"
= "="
identifier "Unavailable"
} "}"
} "}"
} "}"