Skip to content

Conversation

m-k8s
Copy link
Contributor

@m-k8s m-k8s commented Jul 26, 2025

Fix chat message ancestor lookup to handle intermediate messages

Summary

This PR fixes a bug where assistant messages were not being found for specific user messages due to direct parent comparison failing when intermediate messages exist.

Bug Description

The original code used direct parent comparison (data[key].parent == userMessage.id) to find assistant messages that respond to specific user messages. However, this approach fails when there are system messages between the user message and the assistant response, causing assistant messages to not be found at all for those user messages.

Fix Details

Replaced the direct parent comparison with a full message tree traversal function isUserMessageAncestor that correctly identifies which user message an assistant message responds to, regardless of intermediate messages in the conversation tree.

Impact

  • Before: Assistant messages not found when intermediate messages exist between user and assistant
  • After: Assistant messages correctly associated with their corresponding user messages

Files Changed

  • chatgpt.js - Fixed chat message ancestor lookup to handle intermediate messages

m-k8s added 2 commits July 26, 2025 21:54
Replace direct parent comparison with full message tree traversal.
Fixes issue where assistant messages were not found when system messages exist between user and assistant messages.
- Fixes SonarQube duplication warning
- Improves code readability
Copy link

@adamlui adamlui merged commit deebd39 into KudoAI:main Jul 28, 2025
6 checks passed
adamlui pushed a commit that referenced this pull request Jul 28, 2025
fix: traverse full message tree to find correct user ancestor ↞ [auto-sync from https://github.com/KudoAI/chatgpt.js]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants