Skip to content

Commit

Permalink
Don't crash when editing the title of a conversation not in-memory
Browse files Browse the repository at this point in the history
  • Loading branch information
petemill committed Dec 12, 2024
1 parent e000c4e commit 16e68d6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
32 changes: 16 additions & 16 deletions components/ai_chat/core/browser/ai_chat_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ constexpr auto kAllowedSchemes = base::MakeFixedFlatSet<std::string_view>(
{url::kHttpsScheme, url::kHttpScheme, url::kFileScheme, url::kDataScheme});

std::vector<mojom::Conversation*> FilterVisibleConversations(
std::map<std::string, mojom::ConversationPtr>& conversations_map) {
std::map<std::string, mojom::ConversationPtr, std::less<>>&
conversations_map) {
std::vector<mojom::Conversation*> conversations;
for (const auto& kv : conversations_map) {
auto& conversation = kv.second;
Expand Down Expand Up @@ -598,14 +599,8 @@ void AIChatService::DeleteConversation(const std::string& id) {

void AIChatService::RenameConversation(const std::string& id,
const std::string& new_name) {
ConversationHandler* conversation_handler =
conversation_handlers_.at(id).get();
if (!conversation_handler) {
return;
}

DVLOG(1) << "Renamed conversation " << id << " to '" << new_name << "'";
OnConversationTitleChanged(conversation_handler, new_name);
;
OnConversationTitleChanged(id, new_name);
}

void AIChatService::OnPremiumStatusReceived(GetPremiumStatusCallback callback,
Expand Down Expand Up @@ -809,20 +804,25 @@ void AIChatService::OnClientConnectionChanged(ConversationHandler* handler) {
MaybeUnloadConversation(handler);
}

void AIChatService::OnConversationTitleChanged(ConversationHandler* handler,
std::string title) {
auto conversation_it = conversations_.find(handler->get_conversation_uuid());
CHECK(conversation_it != conversations_.end());
auto& conversation = conversation_it->second;
conversation->title = title;
void AIChatService::OnConversationTitleChanged(
const std::string& conversation_uuid,
const std::string& new_title) {
auto conversation_it = conversations_.find(conversation_uuid);
if (conversation_it == conversations_.end()) {
DLOG(ERROR) << "Conversation not found for title change";
return;
}

auto& conversation_metadata = conversation_it->second;
conversation_metadata->title = new_title;

OnConversationListChanged();

// Persist the change
if (ai_chat_db_) {
ai_chat_db_
.AsyncCall(base::IgnoreResult(&AIChatDatabase::UpdateConversationTitle))
.WithArgs(handler->get_conversation_uuid(), std::move(title));
.WithArgs(conversation_uuid, new_title);
}
}

Expand Down
7 changes: 4 additions & 3 deletions components/ai_chat/core/browser/ai_chat_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ class AIChatService : public KeyedService,
void OnConversationEntryRemoved(ConversationHandler* handler,
std::string entry_uuid) override;
void OnClientConnectionChanged(ConversationHandler* handler) override;
void OnConversationTitleChanged(ConversationHandler* handler,
std::string title) override;
void OnConversationTitleChanged(const std::string& conversation_uuid,
const std::string& title) override;
void OnAssociatedContentDestroyed(ConversationHandler* handler,
int content_id) override;

Expand Down Expand Up @@ -174,7 +174,8 @@ class AIChatService : public KeyedService,

private:
// Key is uuid
using ConversationMap = std::map<std::string, mojom::ConversationPtr>;
using ConversationMap =
std::map<std::string, mojom::ConversationPtr, std::less<>>;
using ConversationMapCallback = base::OnceCallback<void(ConversationMap&)>;

void MaybeInitStorage();
Expand Down
2 changes: 1 addition & 1 deletion components/ai_chat/core/browser/conversation_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1685,7 +1685,7 @@ void ConversationHandler::OnClientConnectionChanged() {

void ConversationHandler::OnConversationTitleChanged(std::string title) {
for (auto& observer : observers_) {
observer.OnConversationTitleChanged(this, title);
observer.OnConversationTitleChanged(metadata_->uuid, title);
}
}

Expand Down
5 changes: 3 additions & 2 deletions components/ai_chat/core/browser/conversation_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ class ConversationHandler : public mojom::ConversationHandler,

// Called when a mojo client connects or disconnects
virtual void OnClientConnectionChanged(ConversationHandler* handler) {}
virtual void OnConversationTitleChanged(ConversationHandler* handler,
std::string title) {}
virtual void OnConversationTitleChanged(
const std::string& conversation_uuid,
const std::string& title) {}
virtual void OnSelectedLanguageChanged(
ConversationHandler* handler,
const std::string& selected_language) {}
Expand Down

0 comments on commit 16e68d6

Please sign in to comment.