Skip to content

Commit

Permalink
Merge pull request #26424 from brave/fix_opt_in_case
Browse files Browse the repository at this point in the history
Fetch staged entries from Brave Search before opt-in to Leo
  • Loading branch information
yrliou authored Nov 7, 2024
2 parents 6cb2b70 + fb7a18b commit ba95b0a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 20 deletions.
5 changes: 2 additions & 3 deletions components/ai_chat/core/browser/conversation_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1047,8 +1047,7 @@ void ConversationHandler::MaybeFetchOrClearContentStagedConversation() {
}

const bool can_check_for_staged_conversation =
ai_chat_service_->HasUserOptedIn() && IsContentAssociationPossible() &&
should_send_page_contents_;
IsContentAssociationPossible() && should_send_page_contents_;
if (!can_check_for_staged_conversation) {
// Clear any staged conversation entries since user might have unassociated
// content with this conversation
Expand All @@ -1071,7 +1070,7 @@ void ConversationHandler::OnGetStagedEntriesFromContent(
const std::optional<std::vector<SearchQuerySummary>>& entries) {
// Check if all requirements are still met.
if (is_request_in_progress_ || !entries || !IsContentAssociationPossible() ||
!should_send_page_contents_ || !ai_chat_service_->HasUserOptedIn()) {
!should_send_page_contents_) {
return;
}

Expand Down
21 changes: 5 additions & 16 deletions components/ai_chat/core/browser/conversation_handler_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1093,13 +1093,6 @@ TEST_F(ConversationHandlerUnitTest,
conversation_handler_->OnGetStagedEntriesFromContent(entries);
task_environment_.RunUntilIdle();
EXPECT_EQ(conversation_handler_->GetConversationHistory().size(), 0u);

// No staged entries if user opt-out.
conversation_handler_->SetShouldSendPageContents(true);
EmulateUserOptedOut();
conversation_handler_->OnGetStagedEntriesFromContent(entries);
task_environment_.RunUntilIdle();
EXPECT_EQ(conversation_handler_->GetConversationHistory().size(), 0u);
}

TEST_F(ConversationHandlerUnitTest, OnGetStagedEntriesFromContent) {
Expand Down Expand Up @@ -1135,19 +1128,15 @@ TEST_F(ConversationHandlerUnitTest, OnGetStagedEntriesFromContent) {
EXPECT_EQ(history[5]->text, "summary2");
}

TEST_F(ConversationHandlerUnitTest_OptedOut,
TEST_F(ConversationHandlerUnitTest,
MaybeFetchOrClearSearchQuerySummary_NotOptedIn) {
// Content will have staged entries, but we want to make sure that
// ConversationHandler won't ask for them when not opted-in yet.
// Staged entries could be retrieved before user opts in.
SetAssociatedContentStagedEntries(/*empty=*/false);
EXPECT_CALL(*associated_content_, GetStagedEntriesFromContent).Times(0);
// Modifying whether page contents should be sent should trigger content
// staging.
EXPECT_CALL(*associated_content_, GetStagedEntriesFromContent).Times(1);
// Don't get a false positive because no client is automatically connected.
// Connecting a client will trigger content staging.
NiceMock<MockConversationHandlerClient> client(conversation_handler_.get());
EXPECT_TRUE(conversation_handler_->IsAnyClientConnected());
conversation_handler_->SetShouldSendPageContents(false);
conversation_handler_->SetShouldSendPageContents(true);
conversation_handler_->GetAssociatedContentInfo(base::BindLambdaForTesting(
[&](mojom::SiteInfoPtr site_info, bool should_send_page_contents) {
EXPECT_TRUE(should_send_page_contents);
Expand All @@ -1156,7 +1145,7 @@ TEST_F(ConversationHandlerUnitTest_OptedOut,
task_environment_.RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(conversation_handler_.get());

EXPECT_TRUE(conversation_handler_->GetConversationHistory().empty());
EXPECT_FALSE(conversation_handler_->GetConversationHistory().empty());
}

TEST_F(ConversationHandlerUnitTest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ function Main() {
<div className={styles.promptContainer}>
<LongConversationInfo />
</div>}
{!aiChatContext.hasAcceptedAgreement && <WelcomeGuide />}
{!aiChatContext.hasAcceptedAgreement && !conversationContext.conversationHistory.length && <WelcomeGuide />}
</div>
</div>
<div className={styles.input}>
Expand Down

0 comments on commit ba95b0a

Please sign in to comment.