Fix LID direct chats in Manager - #37
Conversation
Reviewer's GuideFixes Manager-only direct-chat handling by treating phone and LID JIDs as equivalent conversation identities for sidebar filtering, persisted history lookup, and realtime updates, with resilient response merging and focused identity tests. Sequence diagram for LID-aware chat history loadingsequenceDiagram
participant Chat as Chat UI
participant Query as findMessages
participant API as Evolution API
participant Identity as identity utilities
Chat->>Query: findMessages(instanceName, remoteJid)
par Primary JID lookup
Query->>API: POST findMessages where.key.remoteJid
API-->>Query: Primary response
and Alternate JID lookup
Query->>API: POST findMessages where.key.remoteJidAlt
API-->>Query: Alternate response or failure
end
Query->>Identity: messageRecordsFromResponse(primary, alternate)
Identity-->>Query: Normalized records
Query->>Identity: mergeMessagesByKeyId(groups)
Identity-->>Query: Deduplicated messages
Query-->>Chat: Combined message history
Sequence diagram for realtime messages matching either chat identitysequenceDiagram
participant Events as Realtime events
participant Messages as Messages UI
participant Identity as identity utilities
Events->>Messages: Message event with key
Messages->>Identity: messageMatchesConversation(key, selectedJid)
alt remoteJid or remoteJidAlt matches
Identity-->>Messages: true
Messages-->>Messages: Update open conversation
else Neither identity matches
Identity-->>Messages: false
Messages-->>Messages: Ignore event
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="package.json" line_range="49" />
<code_context>
"preview": "vite preview",
- "test": "echo \"No tests specified\" && exit 0",
+ "test": "npm run test:identity",
+ "test:identity": "tsc -p tests/tsconfig.json && node --test /tmp/evolution-manager-v2-identity-test/tests/chat-identity.test.js",
"format": "prettier --write \"src/**/*.{ts,tsx,js,jsx,json,css,md}\"",
"format:check": "prettier --check \"src/**/*.{ts,tsx,js,jsx,json,css,md}\""
</code_context>
<issue_to_address>
**issue (testing):** The identity test command hard-codes the compiled output directory to `/tmp/evolution-manager-v2-identity-test` and invokes that Unix-specific path directly, so `npm test` fails on Windows and in environments without a writable `/tmp` directory before the tests run.
**Triggers:** When the Manager is tested on Windows or in an environment where `/tmp` is unavailable or not writable.
**Suggested fix:** Use a repository-local temporary output directory or construct the output and test paths with a cross-platform Node script.
```suggestion
"test:identity": "tsc -p tests/tsconfig.json --outDir .tmp/evolution-manager-v2-identity-test && node --test .tmp/evolution-manager-v2-identity-test/tests/chat-identity.test.js",
```
</issue_to_address>Sourcery assessment
Approval pending. 1 finding to address first.
Blocking findings: package.json:49
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Pull request overview
This PR fixes Manager direct-chat handling when conversations can be identified by either phone-based JIDs (@s.whatsapp.net) or LID JIDs (@lid), so contacts, persisted history, and realtime updates remain consistent across both identities.
Changes:
- Add chat-identity utilities to detect direct chats, match messages to the open conversation via
remoteJid/remoteJidAlt, normalize API response shapes, and deduplicate merged history by message key id. - Update chat UI filtering and websocket event acceptance to include LID direct chats and to match events via either JID identity.
- Add a dependency-free Node test suite for the identity helpers and include VS Code editor recommendations.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/tsconfig.json | Adds a dedicated TS build config for identity tests. |
| tests/chat-identity.test.ts | Adds Node test-runner coverage for identity utilities (direct chat detection, matching, normalization, dedupe). |
| src/types/evolution.types.ts | Extends Key with optional remoteJidAlt for alternate identity matching. |
| src/pages/instance/Chat/messages.tsx | Uses identity matching helper to accept realtime events for either JID identity. |
| src/pages/instance/Chat/index.tsx | Expands Contacts filtering to include direct @lid chats. |
| src/lib/queries/chat/findMessages.ts | Merges message history fetched by primary remoteJid and alternate remoteJidAlt. |
| src/lib/chat/identity.ts | Introduces shared, dependency-free identity utilities used by UI and queries. |
| package.json | Wires npm test to run the identity test suite. |
| .vscode/settings.json | Adds recommended ESLint save-actions configuration. |
| .vscode/extensions.json | Recommends ESLint and Prettier extensions. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| "test": "npm run test:identity", | ||
| "test:identity": "tsc -p tests/tsconfig.json && node --test /tmp/evolution-manager-v2-identity-test/tests/chat-identity.test.js", |
There was a problem hiding this comment.
Accepted and fixed in 9dc5e65: the test command no longer depends on a hard-coded /tmp path.
| export const findMessages = async ({ instanceName, remoteJid }: IParams): Promise<Message[]> => { | ||
| const url = `/chat/findMessages/${instanceName}`; | ||
| const primaryRequest = api.post(url, { | ||
| where: { key: { remoteJid } }, | ||
| }); | ||
| if (response.data?.messages?.records) { | ||
| return response.data.messages.records; | ||
| } | ||
| return response.data; | ||
| const alternateRequest = api | ||
| .post(url, { | ||
| where: { key: { remoteJidAlt: remoteJid } }, | ||
| }) | ||
| .then((response) => messageRecordsFromResponse(response.data) as Message[]) | ||
| .catch(() => [] as Message[]); | ||
|
|
||
| const [primaryResponse, alternateRecords] = await Promise.all([primaryRequest, alternateRequest]); | ||
| const primaryRecords = messageRecordsFromResponse(primaryResponse.data) as Message[]; | ||
|
|
||
| return mergeMessagesByKeyId([primaryRecords, alternateRecords]); |
There was a problem hiding this comment.
Accepted and fixed in 9dc5e65: the alternate lookup now runs only for direct JIDs and its records are filtered against the selected conversation before merging.
| "types": ["node"], | ||
| "rootDir": "..", | ||
| "outDir": "/tmp/evolution-manager-v2-identity-test", | ||
| "noEmitOnError": true |
Summary
@lidchats in the Contacts sidebarkey.remoteJidandkey.remoteJidAlt, merging duplicate message IDsScope
This is a Manager-only fix. It does not change Evolution API, Baileys, database persistence, Docker images, charts, or package dependencies.
The primary history lookup remains authoritative. If an API does not support the alternate-JID filter, that lookup degrades to an empty result rather than blanking current history.
Verification
npm test— 12 passing testsnpm run lint:check— passes with 11 pre-existing warnings outside this changenpm run type-checknpm run buildwhere.key.remoteJidandwhere.key.remoteJidAltreturned HTTP 200Summary by Sourcery
Fix Manager chat identity handling so LID conversations display complete persisted and realtime message histories.
New Features:
Bug Fixes:
Enhancements:
Documentation:
Tests: