Extract config and types into separate files, clean up index.ts

- src/config.ts: configuration constants
- src/types.ts: TypeScript interfaces
- src/index.ts: remove section comments, streamline code

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
gavrielc
2026-01-31 19:17:40 +02:00
parent fe5ae974a3
commit 78426c764d
3 changed files with 84 additions and 209 deletions

8
src/config.ts Normal file
View File

@@ -0,0 +1,8 @@
export const ASSISTANT_NAME = process.env.ASSISTANT_NAME || 'Andy';
export const POLL_INTERVAL = 2000;
export const STORE_DIR = './store';
export const GROUPS_DIR = './groups';
export const DATA_DIR = './data';
export const TRIGGER_PATTERN = new RegExp(`^@${ASSISTANT_NAME}\\b`, 'i');
export const CLEAR_COMMAND = '/clear';

View File

@@ -1,13 +1,3 @@
/**
* NanoClaw - Unified Node.js Implementation
*
* Single process that handles:
* - WhatsApp connection (baileys)
* - Message routing
* - Claude Agent SDK queries
* - Response sending
*/
import makeWASocket, { import makeWASocket, {
useMultiFileAuthState, useMultiFileAuthState,
DisconnectReason, DisconnectReason,
@@ -22,49 +12,32 @@ import { exec } from 'child_process';
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
// === CONFIGURATION === import {
ASSISTANT_NAME,
const CONFIG = { POLL_INTERVAL,
assistantName: process.env.ASSISTANT_NAME || 'Andy', STORE_DIR,
pollInterval: 2000, // ms GROUPS_DIR,
storeDir: './store', DATA_DIR,
groupsDir: './groups', TRIGGER_PATTERN,
dataDir: './data', CLEAR_COMMAND
}; } from './config.js';
import { RegisteredGroup, Session, NewMessage } from './types.js';
const TRIGGER_PATTERN = new RegExp(`^@${CONFIG.assistantName}\\b`, 'i');
const CLEAR_COMMAND = '/clear';
// === TYPES ===
interface RegisteredGroup {
name: string;
folder: string;
trigger: string;
added_at: string;
}
interface Session {
[folder: string]: string; // folder -> session_id
}
// === LOGGING ===
const logger = pino({ const logger = pino({
level: process.env.LOG_LEVEL || 'info', level: process.env.LOG_LEVEL || 'info',
transport: { transport: { target: 'pino-pretty', options: { colorize: true } }
target: 'pino-pretty',
options: { colorize: true }
}
}); });
// === DATABASE === let db: Database.Database;
let sock: WASocket;
let lastTimestamp = '';
let sessions: Session = {};
let registeredGroups: Record<string, RegisteredGroup> = {};
function initDatabase(dbPath: string): Database.Database { function initDatabase(dbPath: string): Database.Database {
fs.mkdirSync(path.dirname(dbPath), { recursive: true }); fs.mkdirSync(path.dirname(dbPath), { recursive: true });
const database = new Database(dbPath);
const db = new Database(dbPath); database.exec(`
db.exec(`
CREATE TABLE IF NOT EXISTS chats ( CREATE TABLE IF NOT EXISTS chats (
jid TEXT PRIMARY KEY, jid TEXT PRIMARY KEY,
name TEXT, name TEXT,
@@ -82,12 +55,9 @@ function initDatabase(dbPath: string): Database.Database {
); );
CREATE INDEX IF NOT EXISTS idx_timestamp ON messages(timestamp); CREATE INDEX IF NOT EXISTS idx_timestamp ON messages(timestamp);
`); `);
return database;
return db;
} }
// === FILE HELPERS ===
function loadJson<T>(filePath: string, defaultValue: T): T { function loadJson<T>(filePath: string, defaultValue: T): T {
try { try {
if (fs.existsSync(filePath)) { if (fs.existsSync(filePath)) {
@@ -104,40 +74,21 @@ function saveJson(filePath: string, data: unknown): void {
fs.writeFileSync(filePath, JSON.stringify(data, null, 2)); fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
} }
// === STATE ===
let db: Database.Database;
let sock: WASocket;
let lastTimestamp = '';
let sessions: Session = {};
let registeredGroups: Record<string, RegisteredGroup> = {};
function loadState(): void { function loadState(): void {
const statePath = path.join(CONFIG.dataDir, 'router_state.json'); const statePath = path.join(DATA_DIR, 'router_state.json');
const state = loadJson<{ last_timestamp?: string }>(statePath, {}); const state = loadJson<{ last_timestamp?: string }>(statePath, {});
lastTimestamp = state.last_timestamp || ''; lastTimestamp = state.last_timestamp || '';
sessions = loadJson(path.join(DATA_DIR, 'sessions.json'), {});
sessions = loadJson(path.join(CONFIG.dataDir, 'sessions.json'), {}); registeredGroups = loadJson(path.join(DATA_DIR, 'registered_groups.json'), {});
registeredGroups = loadJson(path.join(CONFIG.dataDir, 'registered_groups.json'), {}); logger.info({ groupCount: Object.keys(registeredGroups).length }, 'State loaded');
logger.info({
groupCount: Object.keys(registeredGroups).length,
lastTimestamp: lastTimestamp || '(start)'
}, 'State loaded');
} }
function saveState(): void { function saveState(): void {
saveJson(path.join(CONFIG.dataDir, 'router_state.json'), { last_timestamp: lastTimestamp }); saveJson(path.join(DATA_DIR, 'router_state.json'), { last_timestamp: lastTimestamp });
saveJson(path.join(CONFIG.dataDir, 'sessions.json'), sessions); saveJson(path.join(DATA_DIR, 'sessions.json'), sessions);
} }
// === MESSAGE STORAGE === function storeMessage(msg: proto.IWebMessageInfo, chatJid: string, isFromMe: boolean): void {
function storeMessage(
msg: proto.IWebMessageInfo,
chatJid: string,
isFromMe: boolean
): void {
if (!msg.key) return; if (!msg.key) return;
const content = const content =
@@ -152,59 +103,30 @@ function storeMessage(
const msgId = msg.key.id || ''; const msgId = msg.key.id || '';
try { try {
// Ensure chat exists first db.prepare(`INSERT OR REPLACE INTO chats (jid, name, last_message_time) VALUES (?, ?, ?)`).run(chatJid, chatJid, timestamp);
db.prepare(` db.prepare(`INSERT OR REPLACE INTO messages (id, chat_jid, sender, content, timestamp, is_from_me) VALUES (?, ?, ?, ?, ?, ?)`).run(msgId, chatJid, sender, content, timestamp, isFromMe ? 1 : 0);
INSERT OR REPLACE INTO chats (jid, name, last_message_time)
VALUES (?, ?, ?)
`).run(chatJid, chatJid, timestamp);
// Store message
db.prepare(`
INSERT OR REPLACE INTO messages (id, chat_jid, sender, content, timestamp, is_from_me)
VALUES (?, ?, ?, ?, ?, ?)
`).run(msgId, chatJid, sender, content, timestamp, isFromMe ? 1 : 0);
logger.debug({ chatJid, msgId }, 'Message stored'); logger.debug({ chatJid, msgId }, 'Message stored');
} catch (err) { } catch (err) {
logger.error({ err, msgId }, 'Failed to store message'); logger.error({ err, msgId }, 'Failed to store message');
} }
} }
// === MESSAGE PROCESSING ===
interface NewMessage {
id: string;
chat_jid: string;
sender: string;
content: string;
timestamp: string;
}
function getNewMessages(): NewMessage[] { function getNewMessages(): NewMessage[] {
const jids = Object.keys(registeredGroups); const jids = Object.keys(registeredGroups);
if (jids.length === 0) { if (jids.length === 0) return [];
logger.debug('No registered groups');
return [];
}
const placeholders = jids.map(() => '?').join(','); const placeholders = jids.map(() => '?').join(',');
const query = ` const sql = `
SELECT id, chat_jid, sender, content, timestamp SELECT id, chat_jid, sender, content, timestamp
FROM messages FROM messages
WHERE timestamp > ? AND chat_jid IN (${placeholders}) WHERE timestamp > ? AND chat_jid IN (${placeholders})
ORDER BY timestamp ORDER BY timestamp
`; `;
logger.debug({ lastTimestamp, jids }, 'Querying messages'); const rows = db.prepare(sql).all(lastTimestamp, ...jids) as NewMessage[];
const rows = db.prepare(query).all(lastTimestamp, ...jids) as NewMessage[];
for (const row of rows) { for (const row of rows) {
if (row.timestamp > lastTimestamp) { if (row.timestamp > lastTimestamp) lastTimestamp = row.timestamp;
lastTimestamp = row.timestamp;
} }
}
return rows; return rows;
} }
@@ -214,63 +136,41 @@ async function processMessage(msg: NewMessage): Promise<void> {
const content = msg.content.trim(); const content = msg.content.trim();
// Handle /clear command
if (content.toLowerCase() === CLEAR_COMMAND) { if (content.toLowerCase() === CLEAR_COMMAND) {
if (sessions[group.folder]) { if (sessions[group.folder]) {
// Archive old session
const archived = loadJson<Record<string, Array<{ session_id: string; cleared_at: string }>>>( const archived = loadJson<Record<string, Array<{ session_id: string; cleared_at: string }>>>(
path.join(CONFIG.dataDir, 'archived_sessions.json'), path.join(DATA_DIR, 'archived_sessions.json'), {}
{}
); );
if (!archived[group.folder]) archived[group.folder] = []; if (!archived[group.folder]) archived[group.folder] = [];
archived[group.folder].push({ archived[group.folder].push({ session_id: sessions[group.folder], cleared_at: new Date().toISOString() });
session_id: sessions[group.folder], saveJson(path.join(DATA_DIR, 'archived_sessions.json'), archived);
cleared_at: new Date().toISOString()
});
saveJson(path.join(CONFIG.dataDir, 'archived_sessions.json'), archived);
delete sessions[group.folder]; delete sessions[group.folder];
saveJson(path.join(CONFIG.dataDir, 'sessions.json'), sessions); saveJson(path.join(DATA_DIR, 'sessions.json'), sessions);
} }
logger.info({ group: group.name }, 'Session cleared'); logger.info({ group: group.name }, 'Session cleared');
await sendMessage(msg.chat_jid, `${CONFIG.assistantName}: Conversation cleared. Starting fresh!`); await sendMessage(msg.chat_jid, `${ASSISTANT_NAME}: Conversation cleared. Starting fresh!`);
return; return;
} }
// Check trigger pattern
if (!TRIGGER_PATTERN.test(content)) return; if (!TRIGGER_PATTERN.test(content)) return;
// Strip trigger from message
const prompt = content.replace(TRIGGER_PATTERN, '').trim(); const prompt = content.replace(TRIGGER_PATTERN, '').trim();
if (!prompt) return; if (!prompt) return;
logger.info({ group: group.name, prompt: prompt.slice(0, 50) }, 'Processing message'); logger.info({ group: group.name, prompt: prompt.slice(0, 50) }, 'Processing message');
// Run agent
const response = await runAgent(group, prompt, msg.chat_jid); const response = await runAgent(group, prompt, msg.chat_jid);
if (response) await sendMessage(msg.chat_jid, response);
if (response) {
await sendMessage(msg.chat_jid, response);
}
} }
async function runAgent( async function runAgent(group: RegisteredGroup, prompt: string, chatJid: string): Promise<string | null> {
group: RegisteredGroup,
prompt: string,
chatJid: string
): Promise<string | null> {
const isMain = group.folder === 'main'; const isMain = group.folder === 'main';
const groupDir = path.join(CONFIG.groupsDir, group.folder); const groupDir = path.join(GROUPS_DIR, group.folder);
// Ensure group directory exists
fs.mkdirSync(path.join(groupDir, 'logs'), { recursive: true }); fs.mkdirSync(path.join(groupDir, 'logs'), { recursive: true });
// Build context
const context = `[WhatsApp message from group: ${group.name}] const context = `[WhatsApp message from group: ${group.name}]
[Reply to chat_jid: ${chatJid}] [Reply to chat_jid: ${chatJid}]
[Can write to global memory (../CLAUDE.md): ${isMain}] [Can write to global memory (../CLAUDE.md): ${isMain}]
[Prefix your responses with "${CONFIG.assistantName}:"] [Prefix your responses with "${ASSISTANT_NAME}:"]
User message: ${prompt}`; User message: ${prompt}`;
@@ -284,10 +184,7 @@ User message: ${prompt}`;
options: { options: {
cwd: groupDir, cwd: groupDir,
resume: sessionId, resume: sessionId,
allowedTools: [ allowedTools: ['Read', 'Write', 'Edit', 'Glob', 'Grep', 'WebSearch', 'WebFetch'],
'Read', 'Write', 'Edit', 'Glob', 'Grep',
'WebSearch', 'WebFetch'
],
permissionMode: 'bypassPermissions', permissionMode: 'bypassPermissions',
settingSources: ['project'], settingSources: ['project'],
mcpServers: { mcpServers: {
@@ -296,31 +193,24 @@ User message: ${prompt}`;
} }
} }
})) { })) {
// Capture session ID from init message
if (message.type === 'system' && message.subtype === 'init') { if (message.type === 'system' && message.subtype === 'init') {
newSessionId = message.session_id; newSessionId = message.session_id;
} }
// Capture final result
if ('result' in message && message.result) { if ('result' in message && message.result) {
result = message.result as string; result = message.result as string;
} }
} }
} catch (err) { } catch (err) {
logger.error({ group: group.name, err }, 'Agent error'); logger.error({ group: group.name, err }, 'Agent error');
return `${CONFIG.assistantName}: Sorry, I encountered an error. Please try again.`; return `${ASSISTANT_NAME}: Sorry, I encountered an error. Please try again.`;
} }
// Save session
if (newSessionId) { if (newSessionId) {
sessions[group.folder] = newSessionId; sessions[group.folder] = newSessionId;
saveJson(path.join(CONFIG.dataDir, 'sessions.json'), sessions); saveJson(path.join(DATA_DIR, 'sessions.json'), sessions);
}
if (result) {
logger.info({ group: group.name, result: result.slice(0, 100) }, 'Agent response');
} }
if (result) logger.info({ group: group.name, result: result.slice(0, 100) }, 'Agent response');
return result; return result;
} }
@@ -333,129 +223,88 @@ async function sendMessage(jid: string, text: string): Promise<void> {
} }
} }
// === WHATSAPP CONNECTION ===
async function connectWhatsApp(): Promise<void> { async function connectWhatsApp(): Promise<void> {
const authDir = path.join(CONFIG.storeDir, 'auth'); const authDir = path.join(STORE_DIR, 'auth');
fs.mkdirSync(authDir, { recursive: true }); fs.mkdirSync(authDir, { recursive: true });
const { state, saveCreds } = await useMultiFileAuthState(authDir); const { state, saveCreds } = await useMultiFileAuthState(authDir);
sock = makeWASocket({ sock = makeWASocket({
auth: { auth: { creds: state.creds, keys: makeCacheableSignalKeyStore(state.keys, logger) },
creds: state.creds,
keys: makeCacheableSignalKeyStore(state.keys, logger)
},
printQRInTerminal: false, printQRInTerminal: false,
logger, logger,
browser: ['NanoClaw', 'Chrome', '1.0.0'] browser: ['NanoClaw', 'Chrome', '1.0.0']
}); });
// Handle connection updates
sock.ev.on('connection.update', (update) => { sock.ev.on('connection.update', (update) => {
const { connection, lastDisconnect, qr } = update; const { connection, lastDisconnect, qr } = update;
if (qr) { if (qr) {
// Auth needed - notify user and exit
// This shouldn't happen during normal operation; auth is done during setup
const msg = 'WhatsApp authentication required. Run /setup in Claude Code.'; const msg = 'WhatsApp authentication required. Run /setup in Claude Code.';
logger.error(msg); logger.error(msg);
// Send macOS notification so user sees it
exec(`osascript -e 'display notification "${msg}" with title "NanoClaw" sound name "Basso"'`); exec(`osascript -e 'display notification "${msg}" with title "NanoClaw" sound name "Basso"'`);
// Give notification time to display, then exit
setTimeout(() => process.exit(1), 1000); setTimeout(() => process.exit(1), 1000);
} }
if (connection === 'close') { if (connection === 'close') {
const reason = (lastDisconnect?.error as any)?.output?.statusCode; const reason = (lastDisconnect?.error as any)?.output?.statusCode;
const shouldReconnect = reason !== DisconnectReason.loggedOut; const shouldReconnect = reason !== DisconnectReason.loggedOut;
logger.info({ reason, shouldReconnect }, 'Connection closed'); logger.info({ reason, shouldReconnect }, 'Connection closed');
if (shouldReconnect) { if (shouldReconnect) {
logger.info('Reconnecting...'); logger.info('Reconnecting...');
connectWhatsApp(); connectWhatsApp();
} else { } else {
logger.info('Logged out. Delete store/auth folder and restart to re-authenticate.'); logger.info('Logged out. Run /setup to re-authenticate.');
process.exit(0); process.exit(0);
} }
} else if (connection === 'open') { } else if (connection === 'open') {
console.log('\n✓ Connected to WhatsApp!\n'); logger.info('Connected to WhatsApp');
logger.info('WhatsApp connection established');
startMessageLoop(); startMessageLoop();
} }
}); });
// Save credentials on update
sock.ev.on('creds.update', saveCreds); sock.ev.on('creds.update', saveCreds);
// Handle incoming messages (store them)
sock.ev.on('messages.upsert', ({ messages }) => { sock.ev.on('messages.upsert', ({ messages }) => {
for (const msg of messages) { for (const msg of messages) {
if (!msg.message) continue; if (!msg.message) continue;
const chatJid = msg.key.remoteJid; const chatJid = msg.key.remoteJid;
if (!chatJid || chatJid === 'status@broadcast') continue; if (!chatJid || chatJid === 'status@broadcast') continue;
storeMessage(msg, chatJid, msg.key.fromMe || false); storeMessage(msg, chatJid, msg.key.fromMe || false);
} }
}); });
} }
// === MAIN LOOP ===
async function startMessageLoop(): Promise<void> { async function startMessageLoop(): Promise<void> {
logger.info(`NanoClaw running (trigger: @${CONFIG.assistantName})`); logger.info(`NanoClaw running (trigger: @${ASSISTANT_NAME})`);
while (true) { while (true) {
try { try {
const messages = getNewMessages(); const messages = getNewMessages();
if (messages.length > 0) logger.info({ count: messages.length }, 'New messages');
if (messages.length > 0) { for (const msg of messages) await processMessage(msg);
logger.info({ count: messages.length }, 'Found new messages');
}
for (const msg of messages) {
await processMessage(msg);
}
saveState(); saveState();
} catch (err) { } catch (err) {
logger.error({ err }, 'Error in message loop'); logger.error({ err }, 'Error in message loop');
} }
await new Promise(resolve => setTimeout(resolve, POLL_INTERVAL));
await new Promise(resolve => setTimeout(resolve, CONFIG.pollInterval));
} }
} }
// === ENTRY POINT ===
async function main(): Promise<void> { async function main(): Promise<void> {
// Initialize database db = initDatabase(path.join(STORE_DIR, 'messages.db'));
const dbPath = path.join(CONFIG.storeDir, 'messages.db');
db = initDatabase(dbPath);
logger.info('Database initialized'); logger.info('Database initialized');
// Load state
loadState(); loadState();
// Connect to WhatsApp
await connectWhatsApp(); await connectWhatsApp();
// Handle graceful shutdown const shutdown = () => {
process.on('SIGINT', () => {
logger.info('Shutting down...'); logger.info('Shutting down...');
db.close(); db.close();
process.exit(0); process.exit(0);
}); };
process.on('SIGINT', shutdown);
process.on('SIGTERM', () => { process.on('SIGTERM', shutdown);
logger.info('Shutting down...');
db.close();
process.exit(0);
});
} }
main().catch(err => { main().catch(err => {

18
src/types.ts Normal file
View File

@@ -0,0 +1,18 @@
export interface RegisteredGroup {
name: string;
folder: string;
trigger: string;
added_at: string;
}
export interface Session {
[folder: string]: string;
}
export interface NewMessage {
id: string;
chat_jid: string;
sender: string;
content: string;
timestamp: string;
}