-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Duck AI Migration #7149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Duck AI Migration #7149
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1f266b1
Duck.ai - support standalone migration
shakyShane c0fb8a4
linting
shakyShane 8d33349
tests
shakyShane 1e6bf74
Move things around and separate individual handlers
marcosholgado df08487
Tests
marcosholgado 10ec9f3
Flush
marcosholgado File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
duckchat/duckchat-impl/src/main/java/com/duckduckgo/duckchat/impl/DuckChatConstants.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * Copyright (c) 2025 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.duckchat.impl | ||
|
|
||
| object DuckChatConstants { | ||
| const val HOST_DUCK_AI = "duck.ai" | ||
|
|
||
| object StandaloneConstants { | ||
| const val SERIALIZED_MIGRATION_FILE = "serializedMigrationFile" | ||
| const val COUNT = "count" | ||
| const val INDEX = "index" | ||
| const val OK = "ok" | ||
| const val REASON = "reason" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
...c/main/java/com/duckduckgo/duckchat/impl/messaging/DuckChatStandaloneJsMessageHandlers.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| /* | ||
| * Copyright (c) 2025 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.duckchat.impl.messaging | ||
|
|
||
| import com.duckduckgo.common.utils.AppUrl | ||
| import com.duckduckgo.contentscopescripts.api.ContentScopeJsMessageHandlersPlugin | ||
| import com.duckduckgo.di.scopes.AppScope | ||
| import com.duckduckgo.duckchat.impl.DuckChatConstants.HOST_DUCK_AI | ||
| import com.duckduckgo.duckchat.impl.DuckChatConstants.StandaloneConstants.COUNT | ||
| import com.duckduckgo.duckchat.impl.DuckChatConstants.StandaloneConstants.INDEX | ||
| import com.duckduckgo.duckchat.impl.DuckChatConstants.StandaloneConstants.OK | ||
| import com.duckduckgo.duckchat.impl.DuckChatConstants.StandaloneConstants.REASON | ||
| import com.duckduckgo.duckchat.impl.DuckChatConstants.StandaloneConstants.SERIALIZED_MIGRATION_FILE | ||
| import com.duckduckgo.js.messaging.api.JsCallbackData | ||
| import com.duckduckgo.js.messaging.api.JsMessage | ||
| import com.duckduckgo.js.messaging.api.JsMessageCallback | ||
| import com.duckduckgo.js.messaging.api.JsMessageHandler | ||
| import com.duckduckgo.js.messaging.api.JsMessaging | ||
| import com.squareup.anvil.annotations.ContributesBinding | ||
| import com.squareup.anvil.annotations.ContributesMultibinding | ||
| import dagger.SingleInstanceIn | ||
| import org.json.JSONObject | ||
| import javax.inject.Inject | ||
|
|
||
| interface StandaloneDuckChatStore { | ||
| fun storeMigrationItem(item: String) | ||
| fun getMigrationItemByIndex(index: Int): String? | ||
| fun getMigrationItemCount(): Int | ||
| fun clearMigrationItems() | ||
| } | ||
|
|
||
| @ContributesBinding(AppScope::class) | ||
| @SingleInstanceIn(AppScope::class) | ||
| class InMemoryStandaloneStore @Inject constructor() : StandaloneDuckChatStore { | ||
| private val migrationItems = mutableListOf<String>() | ||
|
|
||
| override fun storeMigrationItem(item: String) { | ||
| migrationItems.add(item) | ||
| } | ||
|
|
||
| override fun getMigrationItemByIndex(index: Int): String? { | ||
| return migrationItems.getOrNull(index) | ||
| } | ||
|
|
||
| override fun getMigrationItemCount(): Int = migrationItems.size | ||
|
|
||
| override fun clearMigrationItems() { | ||
| migrationItems.clear() | ||
| } | ||
| } | ||
|
|
||
| @ContributesMultibinding(AppScope::class) | ||
| class StoreMigrationDataHandler @Inject constructor( | ||
| private val standaloneDuckChatStore: StandaloneDuckChatStore, | ||
| ) : ContentScopeJsMessageHandlersPlugin { | ||
| override fun getJsMessageHandler(): JsMessageHandler = | ||
| object : JsMessageHandler { | ||
| override fun process( | ||
| jsMessage: JsMessage, | ||
| jsMessaging: JsMessaging, | ||
| jsMessageCallback: JsMessageCallback?, | ||
| ) { | ||
| if (jsMessage.id.isNullOrEmpty()) return | ||
| val item = jsMessage.params.optString(SERIALIZED_MIGRATION_FILE) | ||
|
|
||
| val result = if (!item.isNullOrEmpty() && item != JSONObject.NULL.toString()) { | ||
| standaloneDuckChatStore.storeMigrationItem(item) | ||
| true | ||
| } else { | ||
| false | ||
| } | ||
|
|
||
| val payload = JSONObject().apply { | ||
| put(OK, result) | ||
| if (!result) put(REASON, "Missing or invalid serializedMigrationFile") | ||
| } | ||
|
|
||
| jsMessaging.onResponse(JsCallbackData(payload, featureName, jsMessage.method, jsMessage.id!!)) | ||
| } | ||
|
|
||
| override val allowedDomains: List<String> = | ||
| listOf( | ||
| AppUrl.Url.HOST, | ||
| HOST_DUCK_AI, | ||
| ) | ||
|
|
||
| override val featureName: String = "aiChat" | ||
| override val methods: List<String> = listOf("storeMigrationData") | ||
| } | ||
| } | ||
|
|
||
| @ContributesMultibinding(AppScope::class) | ||
| class GetMigrationInfoHandler @Inject constructor( | ||
| private val standaloneDuckChatStore: StandaloneDuckChatStore, | ||
| ) : ContentScopeJsMessageHandlersPlugin { | ||
| override fun getJsMessageHandler(): JsMessageHandler = | ||
| object : JsMessageHandler { | ||
| override fun process( | ||
| jsMessage: JsMessage, | ||
| jsMessaging: JsMessaging, | ||
| jsMessageCallback: JsMessageCallback?, | ||
| ) { | ||
| if (jsMessage.id.isNullOrEmpty()) return | ||
|
|
||
| val count = standaloneDuckChatStore.getMigrationItemCount() | ||
| val payload = JSONObject().apply { | ||
| put(OK, true) | ||
| put(COUNT, count) | ||
| } | ||
|
|
||
| jsMessaging.onResponse(JsCallbackData(payload, featureName, jsMessage.method, jsMessage.id!!)) | ||
| } | ||
|
|
||
| override val allowedDomains: List<String> = | ||
| listOf( | ||
| AppUrl.Url.HOST, | ||
| HOST_DUCK_AI, | ||
| ) | ||
|
|
||
| override val featureName: String = "aiChat" | ||
| override val methods: List<String> = listOf("getMigrationInfo") | ||
| } | ||
| } | ||
|
|
||
| @ContributesMultibinding(AppScope::class) | ||
| class GetMigrationDataByIndexHandler @Inject constructor( | ||
| private val standaloneDuckChatStore: StandaloneDuckChatStore, | ||
| ) : ContentScopeJsMessageHandlersPlugin { | ||
| override fun getJsMessageHandler(): JsMessageHandler = | ||
| object : JsMessageHandler { | ||
| override fun process( | ||
| jsMessage: JsMessage, | ||
| jsMessaging: JsMessaging, | ||
| jsMessageCallback: JsMessageCallback?, | ||
| ) { | ||
| if (jsMessage.id.isNullOrEmpty()) return | ||
| val index = jsMessage.params.optInt(INDEX, -1) | ||
| val value = standaloneDuckChatStore.getMigrationItemByIndex(index) | ||
|
|
||
| val payload = JSONObject().apply { | ||
| if (value == null) { | ||
| put(OK, false) | ||
| put(REASON, "nothing at index: $index") | ||
| } else { | ||
| put(OK, true) | ||
| put(SERIALIZED_MIGRATION_FILE, value) | ||
| } | ||
| } | ||
| jsMessaging.onResponse(JsCallbackData(payload, featureName, jsMessage.method, jsMessage.id!!)) | ||
| } | ||
|
|
||
| override val allowedDomains: List<String> = | ||
| listOf( | ||
| AppUrl.Url.HOST, | ||
| HOST_DUCK_AI, | ||
| ) | ||
|
|
||
| override val featureName: String = "aiChat" | ||
| override val methods: List<String> = listOf("getMigrationDataByIndex") | ||
| } | ||
| } | ||
|
|
||
| @ContributesMultibinding(AppScope::class) | ||
| class ClearMigrationDataHandler @Inject constructor( | ||
| private val standaloneDuckChatStore: StandaloneDuckChatStore, | ||
| ) : ContentScopeJsMessageHandlersPlugin { | ||
| override fun getJsMessageHandler(): JsMessageHandler = | ||
| object : JsMessageHandler { | ||
| override fun process( | ||
| jsMessage: JsMessage, | ||
| jsMessaging: JsMessaging, | ||
| jsMessageCallback: JsMessageCallback?, | ||
| ) { | ||
| if (jsMessage.id.isNullOrEmpty()) return | ||
| standaloneDuckChatStore.clearMigrationItems() | ||
| val jsonPayload = JSONObject().apply { put(OK, true) } | ||
| jsMessaging.onResponse(JsCallbackData(jsonPayload, featureName, jsMessage.method, jsMessage.id!!)) | ||
| } | ||
|
|
||
| override val allowedDomains: List<String> = | ||
| listOf( | ||
| AppUrl.Url.HOST, | ||
| HOST_DUCK_AI, | ||
| ) | ||
|
|
||
| override val featureName: String = "aiChat" | ||
| override val methods: List<String> = listOf("clearMigrationData") | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still have a concern that this could cause issues but we can deactivate the feature if we see an uptick in OOMs