-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AI Chat SERP integration (#5528)
Task/Issue URL: https://app.asana.com/0/488551667048375/1209228531591905/f ### Description - Uses the C-S-S navigatorInterface + messageBridge to link directly from SERP into the dedicated AI Chat WebView. - Tapping the "Ask AI Chat" button in SERP will auto-prompt the dedicated WebView with the pre-filled prompt (Also works for "DuckAssist"). - The functionality is disabled when aiChat is disabled in the config. ### Steps to test this PR _Apply the patch linked in the task_ - [ ] Search for “bread recipe” (You may need to log in) - [ ] Tap the “Ask AI Chat” button - [ ] Verify that the dedicated WebView is opened with “bread recipe” auto-prompted - [ ] Go to settings in SERP > AI features - [ ] Change the setting to show DuckAssist often - [ ] Change the language to English(US) - [ ] Search for “how tall is a giraffe” (You should see “DuckAssist”) - [ ] Tap the “Ask AI Chat” button - [ ] Verify that the dedicated WebView is opened - [ ] Verify that you can scroll up to reveal info above “Tell me more” _Disable `aiChat` in the config linked in the task and load it up_ - [ ] Search for “bread recipe” - [ ] Tap the “Ask AI Chat” button - [ ] Verify that AI Chat is **not** opened in the dedicated WebView
- Loading branch information
Showing
10 changed files
with
606 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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
84 changes: 84 additions & 0 deletions
84
app/src/main/java/com/duckduckgo/app/browser/duckchat/DuckChatJSHelper.kt
This file contains 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,84 @@ | ||
/* | ||
* 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.app.browser.duckchat | ||
|
||
import com.duckduckgo.app.browser.commands.Command | ||
import com.duckduckgo.app.browser.commands.Command.SendResponseToJs | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.duckchat.api.DuckChat | ||
import com.duckduckgo.js.messaging.api.JsCallbackData | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import javax.inject.Inject | ||
import org.json.JSONObject | ||
|
||
interface DuckChatJSHelper { | ||
suspend fun processJsCallbackMessage( | ||
featureName: String, | ||
method: String, | ||
id: String?, | ||
data: JSONObject?, | ||
): Command? | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
class RealDuckChatJSHelper @Inject constructor( | ||
private val duckChat: DuckChat, | ||
private val preferencesStore: DuckChatPreferencesStore, | ||
) : DuckChatJSHelper { | ||
private fun getUserValues(featureName: String, method: String, id: String): JsCallbackData { | ||
val jsonPayload = JSONObject().apply { | ||
put(PLATFORM, ANDROID) | ||
put(IS_HANDOFF_ENABLED, duckChat.isEnabled()) | ||
put(PAYLOAD, preferencesStore.fetchAndClearUserPreferences()) | ||
} | ||
return JsCallbackData(jsonPayload, featureName, method, id) | ||
} | ||
|
||
override suspend fun processJsCallbackMessage( | ||
featureName: String, | ||
method: String, | ||
id: String?, | ||
data: JSONObject?, | ||
): Command? = when (method) { | ||
METHOD_GET_USER_VALUES -> id?.let { | ||
SendResponseToJs(getUserValues(featureName, method, it)) | ||
} | ||
METHOD_OPEN_AI_CHAT -> { | ||
val payload = extractPayload(data) | ||
preferencesStore.updateUserPreferences(payload) | ||
duckChat.openDuckChat() | ||
null | ||
} | ||
else -> null | ||
} | ||
|
||
private fun extractPayload(data: JSONObject?): String? { | ||
return data?.takeIf { | ||
it.opt(PAYLOAD) != JSONObject.NULL | ||
}?.optString(PAYLOAD) | ||
} | ||
|
||
companion object { | ||
const val DUCK_CHAT_FEATURE_NAME = "aiChat" | ||
private const val METHOD_GET_USER_VALUES = "getUserValues" | ||
private const val METHOD_OPEN_AI_CHAT = "openAIChat" | ||
private const val PAYLOAD = "aiChatPayload" | ||
private const val IS_HANDOFF_ENABLED = "isAIChatHandoffEnabled" | ||
private const val PLATFORM = "platform" | ||
private const val ANDROID = "android" | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/duckduckgo/app/browser/duckchat/DuckChatPreferencesStore.kt
This file contains 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,54 @@ | ||
/* | ||
* 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.app.browser.duckchat | ||
|
||
import androidx.core.content.edit | ||
import com.duckduckgo.data.store.api.SharedPreferencesProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import javax.inject.Inject | ||
|
||
interface DuckChatPreferencesStore { | ||
fun fetchAndClearUserPreferences(): String? | ||
fun updateUserPreferences(userPreferences: String?) | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
class RealDuckChatPreferencesStore @Inject constructor( | ||
private val sharedPreferencesProvider: SharedPreferencesProvider, | ||
) : DuckChatPreferencesStore { | ||
|
||
private val preferences by lazy { | ||
sharedPreferencesProvider.getSharedPreferences(FILENAME) | ||
} | ||
|
||
override fun fetchAndClearUserPreferences(): String? = | ||
preferences.getString(USER_PREFERENCES, null).also { | ||
preferences.edit { remove(USER_PREFERENCES) } | ||
} | ||
|
||
override fun updateUserPreferences(userPreferences: String?) { | ||
preferences.edit { | ||
putString(USER_PREFERENCES, userPreferences) | ||
} | ||
} | ||
|
||
companion object { | ||
const val FILENAME = "com.duckduckgo.app.duckchat" | ||
const val USER_PREFERENCES = "DUCK_CHAT_USER_PREFERENCES" | ||
} | ||
} |
This file contains 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
Oops, something went wrong.