8000 [Bug] Chat name being generate even when option is disabled · Issue #107 · Renset/macai · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[Bug] Chat name being generate even when option is disabled #107

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

Open
regismesquita opened this issue May 13, 2025 · 1 comment
Open

[Bug] Chat name being generate even when option is disabled #107

regismesquita opened this issue May 13, 2025 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@regismesquita
Copy link
Contributor

Describe the bug
In the latest version 2.1.0, when a user selects the option to not generate a chat name, the system still sends requests to generate chat names in the background. This behavior is unexpected and can lead to unnecessary costs, especially when dealing with long contexts.

To Reproduce
Steps to reproduce the behavior:

  1. Go to the settings or configuration section where chat name generation options are available.
  2. Select the option to not generate a chat name.
  3. Initiate a chat or conversation.
  4. Observe the system logs or network requests (e.g., in LiteLLM) to see that requests for generating chat names are still being sent.

Expected behavior
When the user selects the option to not generate a chat name, no requests for generating chat names should be sent. This ensures that unnecessary costs are avoided, especially for long contexts.

Screenshots

Image

Additional context
This issue can be particularly problematic for users with large contexts or high usage volumes, as the unnecessary requests can significantly increase costs. A fix should ensure that the system respects the user's preference to disable chat name generation entirely.

@regismesquita regismesquita added the bug Something isn't working label May 13, 2025
@regismesquita regismesquita changed the title [Bug] [Bug] Chat name being generate even when option is disabled May 13, 2025
@regismesquita
Copy link
Contributor Author
regismesquita commented May 13, 2025

For reference: AI Generated patch that I am using in a local build locally and seems to works.

diff --git a/macai/Utilities/MessageManager.swift b/macai/Utilities/MessageManager.swift
index d9b7f09..c45bb6a 100644
--- a/macai/Utilities/MessageManager.swift
+++ b/macai/Utilities/MessageManager.swift
@@ -99,19 +99,47 @@ class MessageManager: ObservableObject {
     }

     func generateChatNameIfNeeded(chat: ChatEntity, force: Bool = false) {
-        guard force || chat.name == "", chat.messages.count > 0 else {
+        // Condition 1: API service must exist and allow name generation
+        guard let serviceEntity = chat.apiService else {
             #if DEBUG
-                print("Chat name not needed, skipping generation")
+            print("MessageManager: Chat name generation skipped - API service entity is nil for chat \(chat.id.uuidString).")
             #endif
             return
         }
+        guard serviceEntity.generateChatNames else {
+            #if DEBUG
+            // Use serviceEntity here, which is APIServiceEntity and has a name property
+            print("MessageManager: Chat name generation skipped - 'generateChatNames' is false for API service '\(serviceEntity.name ?? "Unnamed")' (chat \(chat.id.uuidString)).")
+            #endif
+            return
+        }
+
+        // Condition 2: Either force generation, or chat name is empty and there are messages
+        let needsGenerationDueToEmptyName = chat.name == "" && chat.messages.count > 0
+        guard force || needsGenerationDueToEmptyName else {
+            #if DEBUG
+            if !force && chat.name != "" {
+                print("MessageManager: Chat name generation skipped - chat '\(chat.name)' already has a name and force is false (chat \(chat.id.uuidString)).")
+            } else if !force && chat.messages.count == 0 && chat.name == "" {
+                 print("MessageManager: Chat name generation skipped - chat has no messages, name is empty, and force is false (chat \(chat.id.uuidString)).")
+            } else {
+                print("MessageManager: Chat name generation skipped - conditions not met (force=\(force), nameEmpty=\(chat.name == ""), messagesCount=\(chat.messages.count)) for chat \(chat.id.uuidString).")
+            }
+            #endif
+            return
+        }
+
+        #if DEBUG
+        print("MessageManager: Proceeding with chat name generation for chat \(chat.id.uuidString) (force: \(force), current name: '\(chat.name)', message count: \(chat.messages.count)).")
+        #endif

         let requestMessages = prepareRequestMessages(
             userMessage: AppConstants.chatGptGenerateChatInstruction,
             chat: chat,
             contextSize: 3
         )
-        apiService.sendMessage(requestMessages, temperature: AppConstants.defaultTemperatureForChatNameGeneration) {
+        // Use self.apiService (the protocol instance) to send messages, not serviceEntity (the CoreData entity)
+        self.apiService.sendMessage(requestMessages, temperature: AppConstants.defaultTemperatureForChatNameGeneration) {
             [weak self] result in
             guard let self = self else { return }

@@ -252,4 +280,4 @@ class MessageManager: ObservableObject {

         return messages
     }
-}
+}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants
0