Skip to content

Commit cfdfdbc

Browse files
author
Ubuntu
committed
feat: add automatic peer extraction via message_received hook
- Register message_received hook in LCM plugin - Auto-extract peer info from inbound metadata - DM: peer is sender (user:xxx) - Group: peer is chat/group - Automatically populates contacts table on message receipt This enables automatic contact creation without manual calls to lcm_update_peer.
1 parent 67df16b commit cfdfdbc

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

index.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,49 @@ const lcmPlugin = {
13311331
}),
13321332
);
13331333

1334+
// Register hook to auto-extract peer info from inbound messages
1335+
api.registerHook(["message_received"], async (event) => {
1336+
const meta = event.context?.inboundMeta;
1337+
if (!meta) return;
1338+
1339+
const sessionId = event.sessionId;
1340+
if (!sessionId) return;
1341+
1342+
// Extract peer info from inbound metadata
1343+
const chatId = meta.chat_id;
1344+
const channel = meta.channel;
1345+
const chatType = meta.chat_type;
1346+
1347+
// For DMs, the peer is the sender
1348+
// For groups, the peer is the chat/group
1349+
let peerId: string | undefined;
1350+
let peerName: string | undefined;
1351+
1352+
if (chatType === "dm" && meta.sender_id) {
1353+
peerId = `user:${meta.sender_id}`;
1354+
peerName = meta.sender_name || meta.sender;
1355+
} else if (chatType === "group" && chatId) {
1356+
peerId = chatId;
1357+
peerName = meta.chat_name || meta.group_name;
1358+
}
1359+
1360+
if (!peerId) return;
1361+
1362+
// Update conversation with peer info
1363+
try {
1364+
await lcm.updateConversationPeer({
1365+
sessionId,
1366+
peerId,
1367+
peerName,
1368+
channel,
1369+
chatType,
1370+
});
1371+
deps.log.info(`[lcm] Auto-extracted peer: ${peerId} (${peerName || "unknown"}) for session ${sessionId}`);
1372+
} catch (error) {
1373+
deps.log.warn(`[lcm] Failed to update peer info: ${error}`);
1374+
}
1375+
});
1376+
13341377
api.logger.info(
13351378
`[lcm] Plugin loaded (enabled=${deps.config.enabled}, db=${deps.config.databasePath}, threshold=${deps.config.contextThreshold})`,
13361379
);

0 commit comments

Comments
 (0)