refactor: CI optimization, logging improvements, and codebase formatting (#456)
* fix(db): remove unique constraint on folder to support multi-channel agents * ci: implement automated skill drift detection and self-healing PRs * fix: align registration logic with Gavriel's feedback and fix build/test issues from Daniel Mi * style: conform to prettier standards for CI validation * test: fix branch naming inconsistency in CI (master vs main) * fix(ci): robust module resolution by removing file extensions in scripts * refactor(ci): simplify skill validation by removing redundant combination tests * style: conform skills-engine to prettier, unify logging in index.ts and cleanup unused imports * refactor: extract multi-channel DB changes to separate branch Move channel column, folder suffix logic, and related migrations to feat/multi-channel-db-v2 for independent review. This PR now contains only CI/CD optimizations, Prettier formatting, and logging improvements. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -84,7 +84,9 @@ vi.mock('@whiskeysockets/baileys', () => {
|
||||
timedOut: 408,
|
||||
restartRequired: 515,
|
||||
},
|
||||
fetchLatestWaWebVersion: vi.fn().mockResolvedValue({ version: [2, 3000, 0] }),
|
||||
fetchLatestWaWebVersion: vi
|
||||
.fn()
|
||||
.mockResolvedValue({ version: [2, 3000, 0] }),
|
||||
makeCacheableSignalKeyStore: vi.fn((keys: unknown) => keys),
|
||||
useMultiFileAuthState: vi.fn().mockResolvedValue({
|
||||
state: {
|
||||
@@ -101,7 +103,9 @@ import { getLastGroupSync, updateChatName, setLastGroupSync } from '../db.js';
|
||||
|
||||
// --- Test helpers ---
|
||||
|
||||
function createTestOpts(overrides?: Partial<WhatsAppChannelOpts>): WhatsAppChannelOpts {
|
||||
function createTestOpts(
|
||||
overrides?: Partial<WhatsAppChannelOpts>,
|
||||
): WhatsAppChannelOpts {
|
||||
return {
|
||||
onMessage: vi.fn(),
|
||||
onChatMetadata: vi.fn(),
|
||||
@@ -168,13 +172,17 @@ describe('WhatsAppChannel', () => {
|
||||
const channel = new WhatsAppChannel(opts);
|
||||
await connectChannel(channel);
|
||||
|
||||
const { fetchLatestWaWebVersion } = await import('@whiskeysockets/baileys');
|
||||
const { fetchLatestWaWebVersion } =
|
||||
await import('@whiskeysockets/baileys');
|
||||
expect(fetchLatestWaWebVersion).toHaveBeenCalledWith({});
|
||||
});
|
||||
|
||||
it('falls back gracefully when version fetch fails', async () => {
|
||||
const { fetchLatestWaWebVersion } = await import('@whiskeysockets/baileys');
|
||||
vi.mocked(fetchLatestWaWebVersion).mockRejectedValueOnce(new Error('network error'));
|
||||
const { fetchLatestWaWebVersion } =
|
||||
await import('@whiskeysockets/baileys');
|
||||
vi.mocked(fetchLatestWaWebVersion).mockRejectedValueOnce(
|
||||
new Error('network error'),
|
||||
);
|
||||
|
||||
const opts = createTestOpts();
|
||||
const channel = new WhatsAppChannel(opts);
|
||||
@@ -226,10 +234,9 @@ describe('WhatsAppChannel', () => {
|
||||
await (channel as any).flushOutgoingQueue();
|
||||
|
||||
// Group messages get prefixed when flushed
|
||||
expect(fakeSocket.sendMessage).toHaveBeenCalledWith(
|
||||
'test@g.us',
|
||||
{ text: 'Andy: Queued message' },
|
||||
);
|
||||
expect(fakeSocket.sendMessage).toHaveBeenCalledWith('test@g.us', {
|
||||
text: 'Andy: Queued message',
|
||||
});
|
||||
});
|
||||
|
||||
it('disconnects cleanly', async () => {
|
||||
@@ -249,7 +256,9 @@ describe('WhatsAppChannel', () => {
|
||||
describe('authentication', () => {
|
||||
it('exits process when QR code is emitted (no auth state)', async () => {
|
||||
vi.useFakeTimers();
|
||||
const mockExit = vi.spyOn(process, 'exit').mockImplementation(() => undefined as never);
|
||||
const mockExit = vi
|
||||
.spyOn(process, 'exit')
|
||||
.mockImplementation(() => undefined as never);
|
||||
|
||||
const opts = createTestOpts();
|
||||
const channel = new WhatsAppChannel(opts);
|
||||
@@ -291,7 +300,9 @@ describe('WhatsAppChannel', () => {
|
||||
});
|
||||
|
||||
it('exits on loggedOut disconnect', async () => {
|
||||
const mockExit = vi.spyOn(process, 'exit').mockImplementation(() => undefined as never);
|
||||
const mockExit = vi
|
||||
.spyOn(process, 'exit')
|
||||
.mockImplementation(() => undefined as never);
|
||||
|
||||
const opts = createTestOpts();
|
||||
const channel = new WhatsAppChannel(opts);
|
||||
@@ -477,7 +488,10 @@ describe('WhatsAppChannel', () => {
|
||||
fromMe: false,
|
||||
},
|
||||
message: {
|
||||
imageMessage: { caption: 'Check this photo', mimetype: 'image/jpeg' },
|
||||
imageMessage: {
|
||||
caption: 'Check this photo',
|
||||
mimetype: 'image/jpeg',
|
||||
},
|
||||
},
|
||||
pushName: 'Diana',
|
||||
messageTimestamp: Math.floor(Date.now() / 1000),
|
||||
@@ -684,7 +698,9 @@ describe('WhatsAppChannel', () => {
|
||||
|
||||
await channel.sendMessage('test@g.us', 'Hello');
|
||||
// Group messages get prefixed with assistant name
|
||||
expect(fakeSocket.sendMessage).toHaveBeenCalledWith('test@g.us', { text: 'Andy: Hello' });
|
||||
expect(fakeSocket.sendMessage).toHaveBeenCalledWith('test@g.us', {
|
||||
text: 'Andy: Hello',
|
||||
});
|
||||
});
|
||||
|
||||
it('prefixes direct chat messages on shared number', async () => {
|
||||
@@ -695,7 +711,10 @@ describe('WhatsAppChannel', () => {
|
||||
|
||||
await channel.sendMessage('123@s.whatsapp.net', 'Hello');
|
||||
// Shared number: DMs also get prefixed (needed for self-chat distinction)
|
||||
expect(fakeSocket.sendMessage).toHaveBeenCalledWith('123@s.whatsapp.net', { text: 'Andy: Hello' });
|
||||
expect(fakeSocket.sendMessage).toHaveBeenCalledWith(
|
||||
'123@s.whatsapp.net',
|
||||
{ text: 'Andy: Hello' },
|
||||
);
|
||||
});
|
||||
|
||||
it('queues message when disconnected', async () => {
|
||||
@@ -739,9 +758,15 @@ describe('WhatsAppChannel', () => {
|
||||
|
||||
expect(fakeSocket.sendMessage).toHaveBeenCalledTimes(3);
|
||||
// Group messages get prefixed
|
||||
expect(fakeSocket.sendMessage).toHaveBeenNthCalledWith(1, 'test@g.us', { text: 'Andy: First' });
|
||||
expect(fakeSocket.sendMessage).toHaveBeenNthCalledWith(2, 'test@g.us', { text: 'Andy: Second' });
|
||||
expect(fakeSocket.sendMessage).toHaveBeenNthCalledWith(3, 'test@g.us', { text: 'Andy: Third' });
|
||||
expect(fakeSocket.sendMessage).toHaveBeenNthCalledWith(1, 'test@g.us', {
|
||||
text: 'Andy: First',
|
||||
});
|
||||
expect(fakeSocket.sendMessage).toHaveBeenNthCalledWith(2, 'test@g.us', {
|
||||
text: 'Andy: Second',
|
||||
});
|
||||
expect(fakeSocket.sendMessage).toHaveBeenNthCalledWith(3, 'test@g.us', {
|
||||
text: 'Andy: Third',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -874,7 +899,10 @@ describe('WhatsAppChannel', () => {
|
||||
await connectChannel(channel);
|
||||
|
||||
await channel.setTyping('test@g.us', true);
|
||||
expect(fakeSocket.sendPresenceUpdate).toHaveBeenCalledWith('composing', 'test@g.us');
|
||||
expect(fakeSocket.sendPresenceUpdate).toHaveBeenCalledWith(
|
||||
'composing',
|
||||
'test@g.us',
|
||||
);
|
||||
});
|
||||
|
||||
it('sends paused presence when stopping', async () => {
|
||||
@@ -884,7 +912,10 @@ describe('WhatsAppChannel', () => {
|
||||
await connectChannel(channel);
|
||||
|
||||
await channel.setTyping('test@g.us', false);
|
||||
expect(fakeSocket.sendPresenceUpdate).toHaveBeenCalledWith('paused', 'test@g.us');
|
||||
expect(fakeSocket.sendPresenceUpdate).toHaveBeenCalledWith(
|
||||
'paused',
|
||||
'test@g.us',
|
||||
);
|
||||
});
|
||||
|
||||
it('handles typing indicator failure gracefully', async () => {
|
||||
@@ -896,7 +927,9 @@ describe('WhatsAppChannel', () => {
|
||||
fakeSocket.sendPresenceUpdate.mockRejectedValueOnce(new Error('Failed'));
|
||||
|
||||
// Should not throw
|
||||
await expect(channel.setTyping('test@g.us', true)).resolves.toBeUndefined();
|
||||
await expect(
|
||||
channel.setTyping('test@g.us', true),
|
||||
).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -11,14 +11,19 @@ import makeWASocket, {
|
||||
useMultiFileAuthState,
|
||||
} from '@whiskeysockets/baileys';
|
||||
|
||||
import { ASSISTANT_HAS_OWN_NUMBER, ASSISTANT_NAME, STORE_DIR } from '../config.js';
|
||||
import {
|
||||
getLastGroupSync,
|
||||
setLastGroupSync,
|
||||
updateChatName,
|
||||
} from '../db.js';
|
||||
ASSISTANT_HAS_OWN_NUMBER,
|
||||
ASSISTANT_NAME,
|
||||
STORE_DIR,
|
||||
} from '../config.js';
|
||||
import { getLastGroupSync, setLastGroupSync, updateChatName } from '../db.js';
|
||||
import { logger } from '../logger.js';
|
||||
import { Channel, OnInboundMessage, OnChatMetadata, RegisteredGroup } from '../types.js';
|
||||
import {
|
||||
Channel,
|
||||
OnInboundMessage,
|
||||
OnChatMetadata,
|
||||
RegisteredGroup,
|
||||
} from '../types.js';
|
||||
|
||||
const GROUP_SYNC_INTERVAL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
||||
|
||||
@@ -57,7 +62,10 @@ export class WhatsAppChannel implements Channel {
|
||||
const { state, saveCreds } = await useMultiFileAuthState(authDir);
|
||||
|
||||
const { version } = await fetchLatestWaWebVersion({}).catch((err) => {
|
||||
logger.warn({ err }, 'Failed to fetch latest WA Web version, using default');
|
||||
logger.warn(
|
||||
{ err },
|
||||
'Failed to fetch latest WA Web version, using default',
|
||||
);
|
||||
return { version: undefined };
|
||||
});
|
||||
this.sock = makeWASocket({
|
||||
@@ -86,9 +94,18 @@ export class WhatsAppChannel implements Channel {
|
||||
|
||||
if (connection === 'close') {
|
||||
this.connected = false;
|
||||
const reason = (lastDisconnect?.error as { output?: { statusCode?: number } })?.output?.statusCode;
|
||||
const reason = (
|
||||
lastDisconnect?.error as { output?: { statusCode?: number } }
|
||||
)?.output?.statusCode;
|
||||
const shouldReconnect = reason !== DisconnectReason.loggedOut;
|
||||
logger.info({ reason, shouldReconnect, queuedMessages: this.outgoingQueue.length }, 'Connection closed');
|
||||
logger.info(
|
||||
{
|
||||
reason,
|
||||
shouldReconnect,
|
||||
queuedMessages: this.outgoingQueue.length,
|
||||
},
|
||||
'Connection closed',
|
||||
);
|
||||
|
||||
if (shouldReconnect) {
|
||||
logger.info('Reconnecting...');
|
||||
@@ -167,7 +184,13 @@ export class WhatsAppChannel implements Channel {
|
||||
|
||||
// Always notify about chat metadata for group discovery
|
||||
const isGroup = chatJid.endsWith('@g.us');
|
||||
this.opts.onChatMetadata(chatJid, timestamp, undefined, 'whatsapp', isGroup);
|
||||
this.opts.onChatMetadata(
|
||||
chatJid,
|
||||
timestamp,
|
||||
undefined,
|
||||
'whatsapp',
|
||||
isGroup,
|
||||
);
|
||||
|
||||
// Only deliver full message for registered groups
|
||||
const groups = this.opts.registeredGroups();
|
||||
@@ -220,7 +243,10 @@ export class WhatsAppChannel implements Channel {
|
||||
|
||||
if (!this.connected) {
|
||||
this.outgoingQueue.push({ jid, text: prefixed });
|
||||
logger.info({ jid, length: prefixed.length, queueSize: this.outgoingQueue.length }, 'WA disconnected, message queued');
|
||||
logger.info(
|
||||
{ jid, length: prefixed.length, queueSize: this.outgoingQueue.length },
|
||||
'WA disconnected, message queued',
|
||||
);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
@@ -229,7 +255,10 @@ export class WhatsAppChannel implements Channel {
|
||||
} catch (err) {
|
||||
// If send fails, queue it for retry on reconnect
|
||||
this.outgoingQueue.push({ jid, text: prefixed });
|
||||
logger.warn({ jid, err, queueSize: this.outgoingQueue.length }, 'Failed to send, message queued');
|
||||
logger.warn(
|
||||
{ jid, err, queueSize: this.outgoingQueue.length },
|
||||
'Failed to send, message queued',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,7 +328,10 @@ export class WhatsAppChannel implements Channel {
|
||||
// Check local cache first
|
||||
const cached = this.lidToPhoneMap[lidUser];
|
||||
if (cached) {
|
||||
logger.debug({ lidJid: jid, phoneJid: cached }, 'Translated LID to phone JID (cached)');
|
||||
logger.debug(
|
||||
{ lidJid: jid, phoneJid: cached },
|
||||
'Translated LID to phone JID (cached)',
|
||||
);
|
||||
return cached;
|
||||
}
|
||||
|
||||
@@ -309,7 +341,10 @@ export class WhatsAppChannel implements Channel {
|
||||
if (pn) {
|
||||
const phoneJid = `${pn.split('@')[0].split(':')[0]}@s.whatsapp.net`;
|
||||
this.lidToPhoneMap[lidUser] = phoneJid;
|
||||
logger.info({ lidJid: jid, phoneJid }, 'Translated LID to phone JID (signalRepository)');
|
||||
logger.info(
|
||||
{ lidJid: jid, phoneJid },
|
||||
'Translated LID to phone JID (signalRepository)',
|
||||
);
|
||||
return phoneJid;
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -323,12 +358,18 @@ export class WhatsAppChannel implements Channel {
|
||||
if (this.flushing || this.outgoingQueue.length === 0) return;
|
||||
this.flushing = true;
|
||||
try {
|
||||
logger.info({ count: this.outgoingQueue.length }, 'Flushing outgoing message queue');
|
||||
logger.info(
|
||||
{ count: this.outgoingQueue.length },
|
||||
'Flushing outgoing message queue',
|
||||
);
|
||||
while (this.outgoingQueue.length > 0) {
|
||||
const item = this.outgoingQueue.shift()!;
|
||||
// Send directly — queued items are already prefixed by sendMessage
|
||||
await this.sock.sendMessage(item.jid, { text: item.text });
|
||||
logger.info({ jid: item.jid, length: item.text.length }, 'Queued message sent');
|
||||
logger.info(
|
||||
{ jid: item.jid, length: item.text.length },
|
||||
'Queued message sent',
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
this.flushing = false;
|
||||
|
||||
Reference in New Issue
Block a user