Merge upstream/main: resolve conflicts preserving local customizations
Merge 207 upstream commits (up to v1.2.41) while keeping Apple Container runtime support and MiniMax model forwarding. Remove deleted telegram.ts and credential-proxy.ts imports, widen CONTAINER_RUNTIME_BIN type for Apple Container detection. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,5 @@
|
||||
// slack
|
||||
|
||||
// telegram
|
||||
import './telegram.js';
|
||||
|
||||
// whatsapp
|
||||
|
||||
@@ -1,949 +0,0 @@
|
||||
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
|
||||
|
||||
// --- Mocks ---
|
||||
|
||||
// Mock registry (registerChannel runs at import time)
|
||||
vi.mock('./registry.js', () => ({ registerChannel: vi.fn() }));
|
||||
|
||||
// Mock env reader (used by the factory, not needed in unit tests)
|
||||
vi.mock('../env.js', () => ({ readEnvFile: vi.fn(() => ({})) }));
|
||||
|
||||
// Mock config
|
||||
vi.mock('../config.js', () => ({
|
||||
ASSISTANT_NAME: 'Andy',
|
||||
TRIGGER_PATTERN: /^@Andy\b/i,
|
||||
}));
|
||||
|
||||
// Mock logger
|
||||
vi.mock('../logger.js', () => ({
|
||||
logger: {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
// --- Grammy mock ---
|
||||
|
||||
type Handler = (...args: any[]) => any;
|
||||
|
||||
const botRef = vi.hoisted(() => ({ current: null as any }));
|
||||
|
||||
vi.mock('grammy', () => ({
|
||||
Bot: class MockBot {
|
||||
token: string;
|
||||
commandHandlers = new Map<string, Handler>();
|
||||
filterHandlers = new Map<string, Handler[]>();
|
||||
errorHandler: Handler | null = null;
|
||||
|
||||
api = {
|
||||
sendMessage: vi.fn().mockResolvedValue(undefined),
|
||||
sendChatAction: vi.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
|
||||
constructor(token: string) {
|
||||
this.token = token;
|
||||
botRef.current = this;
|
||||
}
|
||||
|
||||
command(name: string, handler: Handler) {
|
||||
this.commandHandlers.set(name, handler);
|
||||
}
|
||||
|
||||
on(filter: string, handler: Handler) {
|
||||
const existing = this.filterHandlers.get(filter) || [];
|
||||
existing.push(handler);
|
||||
this.filterHandlers.set(filter, existing);
|
||||
}
|
||||
|
||||
catch(handler: Handler) {
|
||||
this.errorHandler = handler;
|
||||
}
|
||||
|
||||
start(opts: { onStart: (botInfo: any) => void }) {
|
||||
opts.onStart({ username: 'andy_ai_bot', id: 12345 });
|
||||
}
|
||||
|
||||
stop() {}
|
||||
},
|
||||
}));
|
||||
|
||||
import { TelegramChannel, TelegramChannelOpts } from './telegram.js';
|
||||
|
||||
// --- Test helpers ---
|
||||
|
||||
function createTestOpts(
|
||||
overrides?: Partial<TelegramChannelOpts>,
|
||||
): TelegramChannelOpts {
|
||||
return {
|
||||
onMessage: vi.fn(),
|
||||
onChatMetadata: vi.fn(),
|
||||
registeredGroups: vi.fn(() => ({
|
||||
'tg:100200300': {
|
||||
name: 'Test Group',
|
||||
folder: 'test-group',
|
||||
trigger: '@Andy',
|
||||
added_at: '2024-01-01T00:00:00.000Z',
|
||||
},
|
||||
})),
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function createTextCtx(overrides: {
|
||||
chatId?: number;
|
||||
chatType?: string;
|
||||
chatTitle?: string;
|
||||
text: string;
|
||||
fromId?: number;
|
||||
firstName?: string;
|
||||
username?: string;
|
||||
messageId?: number;
|
||||
date?: number;
|
||||
entities?: any[];
|
||||
}) {
|
||||
const chatId = overrides.chatId ?? 100200300;
|
||||
const chatType = overrides.chatType ?? 'group';
|
||||
return {
|
||||
chat: {
|
||||
id: chatId,
|
||||
type: chatType,
|
||||
title: overrides.chatTitle ?? 'Test Group',
|
||||
},
|
||||
from: {
|
||||
id: overrides.fromId ?? 99001,
|
||||
first_name: overrides.firstName ?? 'Alice',
|
||||
username: overrides.username ?? 'alice_user',
|
||||
},
|
||||
message: {
|
||||
text: overrides.text,
|
||||
date: overrides.date ?? Math.floor(Date.now() / 1000),
|
||||
message_id: overrides.messageId ?? 1,
|
||||
entities: overrides.entities ?? [],
|
||||
},
|
||||
me: { username: 'andy_ai_bot' },
|
||||
reply: vi.fn(),
|
||||
};
|
||||
}
|
||||
|
||||
function createMediaCtx(overrides: {
|
||||
chatId?: number;
|
||||
chatType?: string;
|
||||
fromId?: number;
|
||||
firstName?: string;
|
||||
date?: number;
|
||||
messageId?: number;
|
||||
caption?: string;
|
||||
extra?: Record<string, any>;
|
||||
}) {
|
||||
const chatId = overrides.chatId ?? 100200300;
|
||||
return {
|
||||
chat: {
|
||||
id: chatId,
|
||||
type: overrides.chatType ?? 'group',
|
||||
title: 'Test Group',
|
||||
},
|
||||
from: {
|
||||
id: overrides.fromId ?? 99001,
|
||||
first_name: overrides.firstName ?? 'Alice',
|
||||
username: 'alice_user',
|
||||
},
|
||||
message: {
|
||||
date: overrides.date ?? Math.floor(Date.now() / 1000),
|
||||
message_id: overrides.messageId ?? 1,
|
||||
caption: overrides.caption,
|
||||
...(overrides.extra || {}),
|
||||
},
|
||||
me: { username: 'andy_ai_bot' },
|
||||
};
|
||||
}
|
||||
|
||||
function currentBot() {
|
||||
return botRef.current;
|
||||
}
|
||||
|
||||
async function triggerTextMessage(ctx: ReturnType<typeof createTextCtx>) {
|
||||
const handlers = currentBot().filterHandlers.get('message:text') || [];
|
||||
for (const h of handlers) await h(ctx);
|
||||
}
|
||||
|
||||
async function triggerMediaMessage(
|
||||
filter: string,
|
||||
ctx: ReturnType<typeof createMediaCtx>,
|
||||
) {
|
||||
const handlers = currentBot().filterHandlers.get(filter) || [];
|
||||
for (const h of handlers) await h(ctx);
|
||||
}
|
||||
|
||||
// --- Tests ---
|
||||
|
||||
describe('TelegramChannel', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
// --- Connection lifecycle ---
|
||||
|
||||
describe('connection lifecycle', () => {
|
||||
it('resolves connect() when bot starts', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
|
||||
await channel.connect();
|
||||
|
||||
expect(channel.isConnected()).toBe(true);
|
||||
});
|
||||
|
||||
it('registers command and message handlers on connect', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
|
||||
await channel.connect();
|
||||
|
||||
expect(currentBot().commandHandlers.has('chatid')).toBe(true);
|
||||
expect(currentBot().commandHandlers.has('ping')).toBe(true);
|
||||
expect(currentBot().filterHandlers.has('message:text')).toBe(true);
|
||||
expect(currentBot().filterHandlers.has('message:photo')).toBe(true);
|
||||
expect(currentBot().filterHandlers.has('message:video')).toBe(true);
|
||||
expect(currentBot().filterHandlers.has('message:voice')).toBe(true);
|
||||
expect(currentBot().filterHandlers.has('message:audio')).toBe(true);
|
||||
expect(currentBot().filterHandlers.has('message:document')).toBe(true);
|
||||
expect(currentBot().filterHandlers.has('message:sticker')).toBe(true);
|
||||
expect(currentBot().filterHandlers.has('message:location')).toBe(true);
|
||||
expect(currentBot().filterHandlers.has('message:contact')).toBe(true);
|
||||
});
|
||||
|
||||
it('registers error handler on connect', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
|
||||
await channel.connect();
|
||||
|
||||
expect(currentBot().errorHandler).not.toBeNull();
|
||||
});
|
||||
|
||||
it('disconnects cleanly', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
|
||||
await channel.connect();
|
||||
expect(channel.isConnected()).toBe(true);
|
||||
|
||||
await channel.disconnect();
|
||||
expect(channel.isConnected()).toBe(false);
|
||||
});
|
||||
|
||||
it('isConnected() returns false before connect', () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
|
||||
expect(channel.isConnected()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// --- Text message handling ---
|
||||
|
||||
describe('text message handling', () => {
|
||||
it('delivers message for registered group', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createTextCtx({ text: 'Hello everyone' });
|
||||
await triggerTextMessage(ctx);
|
||||
|
||||
expect(opts.onChatMetadata).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.any(String),
|
||||
'Test Group',
|
||||
'telegram',
|
||||
true,
|
||||
);
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({
|
||||
id: '1',
|
||||
chat_jid: 'tg:100200300',
|
||||
sender: '99001',
|
||||
sender_name: 'Alice',
|
||||
content: 'Hello everyone',
|
||||
is_from_me: false,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('only emits metadata for unregistered chats', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createTextCtx({ chatId: 999999, text: 'Unknown chat' });
|
||||
await triggerTextMessage(ctx);
|
||||
|
||||
expect(opts.onChatMetadata).toHaveBeenCalledWith(
|
||||
'tg:999999',
|
||||
expect.any(String),
|
||||
'Test Group',
|
||||
'telegram',
|
||||
true,
|
||||
);
|
||||
expect(opts.onMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('skips bot commands (/chatid, /ping) but passes other / messages through', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
// Bot commands should be skipped
|
||||
const ctx1 = createTextCtx({ text: '/chatid' });
|
||||
await triggerTextMessage(ctx1);
|
||||
expect(opts.onMessage).not.toHaveBeenCalled();
|
||||
expect(opts.onChatMetadata).not.toHaveBeenCalled();
|
||||
|
||||
const ctx2 = createTextCtx({ text: '/ping' });
|
||||
await triggerTextMessage(ctx2);
|
||||
expect(opts.onMessage).not.toHaveBeenCalled();
|
||||
|
||||
// Non-bot /commands should flow through
|
||||
const ctx3 = createTextCtx({ text: '/remote-control' });
|
||||
await triggerTextMessage(ctx3);
|
||||
expect(opts.onMessage).toHaveBeenCalledTimes(1);
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({ content: '/remote-control' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('extracts sender name from first_name', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createTextCtx({ text: 'Hi', firstName: 'Bob' });
|
||||
await triggerTextMessage(ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({ sender_name: 'Bob' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('falls back to username when first_name missing', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createTextCtx({ text: 'Hi' });
|
||||
ctx.from.first_name = undefined as any;
|
||||
await triggerTextMessage(ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({ sender_name: 'alice_user' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('falls back to user ID when name and username missing', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createTextCtx({ text: 'Hi', fromId: 42 });
|
||||
ctx.from.first_name = undefined as any;
|
||||
ctx.from.username = undefined as any;
|
||||
await triggerTextMessage(ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({ sender_name: '42' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('uses sender name as chat name for private chats', async () => {
|
||||
const opts = createTestOpts({
|
||||
registeredGroups: vi.fn(() => ({
|
||||
'tg:100200300': {
|
||||
name: 'Private',
|
||||
folder: 'private',
|
||||
trigger: '@Andy',
|
||||
added_at: '2024-01-01T00:00:00.000Z',
|
||||
},
|
||||
})),
|
||||
});
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createTextCtx({
|
||||
text: 'Hello',
|
||||
chatType: 'private',
|
||||
firstName: 'Alice',
|
||||
});
|
||||
await triggerTextMessage(ctx);
|
||||
|
||||
expect(opts.onChatMetadata).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.any(String),
|
||||
'Alice', // Private chats use sender name
|
||||
'telegram',
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
it('uses chat title as name for group chats', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createTextCtx({
|
||||
text: 'Hello',
|
||||
chatType: 'supergroup',
|
||||
chatTitle: 'Project Team',
|
||||
});
|
||||
await triggerTextMessage(ctx);
|
||||
|
||||
expect(opts.onChatMetadata).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.any(String),
|
||||
'Project Team',
|
||||
'telegram',
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
it('converts message.date to ISO timestamp', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const unixTime = 1704067200; // 2024-01-01T00:00:00.000Z
|
||||
const ctx = createTextCtx({ text: 'Hello', date: unixTime });
|
||||
await triggerTextMessage(ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({
|
||||
timestamp: '2024-01-01T00:00:00.000Z',
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// --- @mention translation ---
|
||||
|
||||
describe('@mention translation', () => {
|
||||
it('translates @bot_username mention to trigger format', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createTextCtx({
|
||||
text: '@andy_ai_bot what time is it?',
|
||||
entities: [{ type: 'mention', offset: 0, length: 12 }],
|
||||
});
|
||||
await triggerTextMessage(ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({
|
||||
content: '@Andy @andy_ai_bot what time is it?',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('does not translate if message already matches trigger', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createTextCtx({
|
||||
text: '@Andy @andy_ai_bot hello',
|
||||
entities: [{ type: 'mention', offset: 6, length: 12 }],
|
||||
});
|
||||
await triggerTextMessage(ctx);
|
||||
|
||||
// Should NOT double-prepend — already starts with @Andy
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({
|
||||
content: '@Andy @andy_ai_bot hello',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('does not translate mentions of other bots', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createTextCtx({
|
||||
text: '@some_other_bot hi',
|
||||
entities: [{ type: 'mention', offset: 0, length: 15 }],
|
||||
});
|
||||
await triggerTextMessage(ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({
|
||||
content: '@some_other_bot hi', // No translation
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('handles mention in middle of message', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createTextCtx({
|
||||
text: 'hey @andy_ai_bot check this',
|
||||
entities: [{ type: 'mention', offset: 4, length: 12 }],
|
||||
});
|
||||
await triggerTextMessage(ctx);
|
||||
|
||||
// Bot is mentioned, message doesn't match trigger → prepend trigger
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({
|
||||
content: '@Andy hey @andy_ai_bot check this',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('handles message with no entities', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createTextCtx({ text: 'plain message' });
|
||||
await triggerTextMessage(ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({
|
||||
content: 'plain message',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('ignores non-mention entities', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createTextCtx({
|
||||
text: 'check https://example.com',
|
||||
entities: [{ type: 'url', offset: 6, length: 19 }],
|
||||
});
|
||||
await triggerTextMessage(ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({
|
||||
content: 'check https://example.com',
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// --- Non-text messages ---
|
||||
|
||||
describe('non-text messages', () => {
|
||||
it('stores photo with placeholder', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createMediaCtx({});
|
||||
await triggerMediaMessage('message:photo', ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({ content: '[Photo]' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('stores photo with caption', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createMediaCtx({ caption: 'Look at this' });
|
||||
await triggerMediaMessage('message:photo', ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({ content: '[Photo] Look at this' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('stores video with placeholder', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createMediaCtx({});
|
||||
await triggerMediaMessage('message:video', ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({ content: '[Video]' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('stores voice message with placeholder', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createMediaCtx({});
|
||||
await triggerMediaMessage('message:voice', ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({ content: '[Voice message]' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('stores audio with placeholder', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createMediaCtx({});
|
||||
await triggerMediaMessage('message:audio', ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({ content: '[Audio]' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('stores document with filename', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createMediaCtx({
|
||||
extra: { document: { file_name: 'report.pdf' } },
|
||||
});
|
||||
await triggerMediaMessage('message:document', ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({ content: '[Document: report.pdf]' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('stores document with fallback name when filename missing', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createMediaCtx({ extra: { document: {} } });
|
||||
await triggerMediaMessage('message:document', ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({ content: '[Document: file]' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('stores sticker with emoji', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createMediaCtx({
|
||||
extra: { sticker: { emoji: '😂' } },
|
||||
});
|
||||
await triggerMediaMessage('message:sticker', ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({ content: '[Sticker 😂]' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('stores location with placeholder', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createMediaCtx({});
|
||||
await triggerMediaMessage('message:location', ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({ content: '[Location]' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('stores contact with placeholder', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createMediaCtx({});
|
||||
await triggerMediaMessage('message:contact', ctx);
|
||||
|
||||
expect(opts.onMessage).toHaveBeenCalledWith(
|
||||
'tg:100200300',
|
||||
expect.objectContaining({ content: '[Contact]' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('ignores non-text messages from unregistered chats', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const ctx = createMediaCtx({ chatId: 999999 });
|
||||
await triggerMediaMessage('message:photo', ctx);
|
||||
|
||||
expect(opts.onMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
// --- sendMessage ---
|
||||
|
||||
describe('sendMessage', () => {
|
||||
it('sends message via bot API', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
await channel.sendMessage('tg:100200300', 'Hello');
|
||||
|
||||
expect(currentBot().api.sendMessage).toHaveBeenCalledWith(
|
||||
'100200300',
|
||||
'Hello',
|
||||
{ parse_mode: 'Markdown' },
|
||||
);
|
||||
});
|
||||
|
||||
it('strips tg: prefix from JID', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
await channel.sendMessage('tg:-1001234567890', 'Group message');
|
||||
|
||||
expect(currentBot().api.sendMessage).toHaveBeenCalledWith(
|
||||
'-1001234567890',
|
||||
'Group message',
|
||||
{ parse_mode: 'Markdown' },
|
||||
);
|
||||
});
|
||||
|
||||
it('splits messages exceeding 4096 characters', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const longText = 'x'.repeat(5000);
|
||||
await channel.sendMessage('tg:100200300', longText);
|
||||
|
||||
expect(currentBot().api.sendMessage).toHaveBeenCalledTimes(2);
|
||||
expect(currentBot().api.sendMessage).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'100200300',
|
||||
'x'.repeat(4096),
|
||||
{ parse_mode: 'Markdown' },
|
||||
);
|
||||
expect(currentBot().api.sendMessage).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'100200300',
|
||||
'x'.repeat(904),
|
||||
{ parse_mode: 'Markdown' },
|
||||
);
|
||||
});
|
||||
|
||||
it('sends exactly one message at 4096 characters', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const exactText = 'y'.repeat(4096);
|
||||
await channel.sendMessage('tg:100200300', exactText);
|
||||
|
||||
expect(currentBot().api.sendMessage).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('handles send failure gracefully', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
currentBot().api.sendMessage.mockRejectedValueOnce(
|
||||
new Error('Network error'),
|
||||
);
|
||||
|
||||
// Should not throw
|
||||
await expect(
|
||||
channel.sendMessage('tg:100200300', 'Will fail'),
|
||||
).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('does nothing when bot is not initialized', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
|
||||
// Don't connect — bot is null
|
||||
await channel.sendMessage('tg:100200300', 'No bot');
|
||||
|
||||
// No error, no API call
|
||||
});
|
||||
});
|
||||
|
||||
// --- ownsJid ---
|
||||
|
||||
describe('ownsJid', () => {
|
||||
it('owns tg: JIDs', () => {
|
||||
const channel = new TelegramChannel('test-token', createTestOpts());
|
||||
expect(channel.ownsJid('tg:123456')).toBe(true);
|
||||
});
|
||||
|
||||
it('owns tg: JIDs with negative IDs (groups)', () => {
|
||||
const channel = new TelegramChannel('test-token', createTestOpts());
|
||||
expect(channel.ownsJid('tg:-1001234567890')).toBe(true);
|
||||
});
|
||||
|
||||
it('does not own WhatsApp group JIDs', () => {
|
||||
const channel = new TelegramChannel('test-token', createTestOpts());
|
||||
expect(channel.ownsJid('12345@g.us')).toBe(false);
|
||||
});
|
||||
|
||||
it('does not own WhatsApp DM JIDs', () => {
|
||||
const channel = new TelegramChannel('test-token', createTestOpts());
|
||||
expect(channel.ownsJid('12345@s.whatsapp.net')).toBe(false);
|
||||
});
|
||||
|
||||
it('does not own unknown JID formats', () => {
|
||||
const channel = new TelegramChannel('test-token', createTestOpts());
|
||||
expect(channel.ownsJid('random-string')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// --- setTyping ---
|
||||
|
||||
describe('setTyping', () => {
|
||||
it('sends typing action when isTyping is true', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
await channel.setTyping('tg:100200300', true);
|
||||
|
||||
expect(currentBot().api.sendChatAction).toHaveBeenCalledWith(
|
||||
'100200300',
|
||||
'typing',
|
||||
);
|
||||
});
|
||||
|
||||
it('does nothing when isTyping is false', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
await channel.setTyping('tg:100200300', false);
|
||||
|
||||
expect(currentBot().api.sendChatAction).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does nothing when bot is not initialized', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
|
||||
// Don't connect
|
||||
await channel.setTyping('tg:100200300', true);
|
||||
|
||||
// No error, no API call
|
||||
});
|
||||
|
||||
it('handles typing indicator failure gracefully', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
currentBot().api.sendChatAction.mockRejectedValueOnce(
|
||||
new Error('Rate limited'),
|
||||
);
|
||||
|
||||
await expect(
|
||||
channel.setTyping('tg:100200300', true),
|
||||
).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// --- Bot commands ---
|
||||
|
||||
describe('bot commands', () => {
|
||||
it('/chatid replies with chat ID and metadata', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const handler = currentBot().commandHandlers.get('chatid')!;
|
||||
const ctx = {
|
||||
chat: { id: 100200300, type: 'group' as const },
|
||||
from: { first_name: 'Alice' },
|
||||
reply: vi.fn(),
|
||||
};
|
||||
|
||||
await handler(ctx);
|
||||
|
||||
expect(ctx.reply).toHaveBeenCalledWith(
|
||||
expect.stringContaining('tg:100200300'),
|
||||
expect.objectContaining({ parse_mode: 'Markdown' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('/chatid shows chat type', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const handler = currentBot().commandHandlers.get('chatid')!;
|
||||
const ctx = {
|
||||
chat: { id: 555, type: 'private' as const },
|
||||
from: { first_name: 'Bob' },
|
||||
reply: vi.fn(),
|
||||
};
|
||||
|
||||
await handler(ctx);
|
||||
|
||||
expect(ctx.reply).toHaveBeenCalledWith(
|
||||
expect.stringContaining('private'),
|
||||
expect.any(Object),
|
||||
);
|
||||
});
|
||||
|
||||
it('/ping replies with bot status', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new TelegramChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const handler = currentBot().commandHandlers.get('ping')!;
|
||||
const ctx = { reply: vi.fn() };
|
||||
|
||||
await handler(ctx);
|
||||
|
||||
expect(ctx.reply).toHaveBeenCalledWith('Andy is online.');
|
||||
});
|
||||
});
|
||||
|
||||
// --- Channel properties ---
|
||||
|
||||
describe('channel properties', () => {
|
||||
it('has name "telegram"', () => {
|
||||
const channel = new TelegramChannel('test-token', createTestOpts());
|
||||
expect(channel.name).toBe('telegram');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,396 +0,0 @@
|
||||
import https from 'https';
|
||||
import { Api, Bot } from 'grammy';
|
||||
|
||||
import { ASSISTANT_NAME, TRIGGER_PATTERN } from '../config.js';
|
||||
import { readEnvFile } from '../env.js';
|
||||
import { logger } from '../logger.js';
|
||||
import { registerChannel, ChannelOpts } from './registry.js';
|
||||
import {
|
||||
Channel,
|
||||
OnChatMetadata,
|
||||
OnInboundMessage,
|
||||
RegisteredGroup,
|
||||
} from '../types.js';
|
||||
|
||||
// Bot pool for agent teams: send-only Api instances (no polling)
|
||||
const poolApis: Api[] = [];
|
||||
// Maps "{groupFolder}:{senderName}" → pool Api index for stable assignment
|
||||
const senderBotMap = new Map<string, number>();
|
||||
let nextPoolIndex = 0;
|
||||
|
||||
/**
|
||||
* Initialize send-only Api instances for the bot pool.
|
||||
* Each pool bot can send messages but doesn't poll for updates.
|
||||
*/
|
||||
export async function initBotPool(tokens: string[]): Promise<void> {
|
||||
for (const token of tokens) {
|
||||
try {
|
||||
const api = new Api(token);
|
||||
const me = await api.getMe();
|
||||
poolApis.push(api);
|
||||
logger.info(
|
||||
{ username: me.username, id: me.id, poolSize: poolApis.length },
|
||||
'Pool bot initialized',
|
||||
);
|
||||
} catch (err) {
|
||||
logger.error({ err }, 'Failed to initialize pool bot');
|
||||
}
|
||||
}
|
||||
if (poolApis.length > 0) {
|
||||
logger.info({ count: poolApis.length }, 'Telegram bot pool ready');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message via a pool bot assigned to the given sender name.
|
||||
* Assigns bots round-robin on first use; subsequent messages from the
|
||||
* same sender in the same group always use the same bot.
|
||||
* On first assignment, renames the bot to match the sender's role.
|
||||
*/
|
||||
export async function sendPoolMessage(
|
||||
chatId: string,
|
||||
text: string,
|
||||
sender: string,
|
||||
groupFolder: string,
|
||||
): Promise<void> {
|
||||
if (poolApis.length === 0) {
|
||||
// No pool bots — fall back to main bot sendMessage path
|
||||
return;
|
||||
}
|
||||
|
||||
const key = `${groupFolder}:${sender}`;
|
||||
let idx = senderBotMap.get(key);
|
||||
if (idx === undefined) {
|
||||
idx = nextPoolIndex % poolApis.length;
|
||||
nextPoolIndex++;
|
||||
senderBotMap.set(key, idx);
|
||||
// Rename the bot to match the sender's role, then wait for Telegram to propagate
|
||||
try {
|
||||
await poolApis[idx].setMyName(sender);
|
||||
await new Promise((r) => setTimeout(r, 2000));
|
||||
logger.info(
|
||||
{ sender, groupFolder, poolIndex: idx },
|
||||
'Assigned and renamed pool bot',
|
||||
);
|
||||
} catch (err) {
|
||||
logger.warn(
|
||||
{ sender, err },
|
||||
'Failed to rename pool bot (sending anyway)',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const api = poolApis[idx];
|
||||
try {
|
||||
const numericId = chatId.replace(/^tg:/, '');
|
||||
const MAX_LENGTH = 4096;
|
||||
if (text.length <= MAX_LENGTH) {
|
||||
await sendTelegramMessage(api, numericId, text);
|
||||
} else {
|
||||
for (let i = 0; i < text.length; i += MAX_LENGTH) {
|
||||
await sendTelegramMessage(
|
||||
api,
|
||||
numericId,
|
||||
text.slice(i, i + MAX_LENGTH),
|
||||
);
|
||||
}
|
||||
}
|
||||
logger.info(
|
||||
{ chatId, sender, poolIndex: idx, length: text.length },
|
||||
'Pool message sent',
|
||||
);
|
||||
} catch (err) {
|
||||
logger.error({ chatId, sender, err }, 'Failed to send pool message');
|
||||
}
|
||||
}
|
||||
|
||||
export interface TelegramChannelOpts {
|
||||
onMessage: OnInboundMessage;
|
||||
onChatMetadata: OnChatMetadata;
|
||||
registeredGroups: () => Record<string, RegisteredGroup>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message with Telegram Markdown parse mode, falling back to plain text.
|
||||
* Claude's output naturally matches Telegram's Markdown v1 format:
|
||||
* *bold*, _italic_, `code`, ```code blocks```, [links](url)
|
||||
*/
|
||||
async function sendTelegramMessage(
|
||||
api: { sendMessage: Api['sendMessage'] },
|
||||
chatId: string | number,
|
||||
text: string,
|
||||
options: { message_thread_id?: number } = {},
|
||||
): Promise<void> {
|
||||
try {
|
||||
await api.sendMessage(chatId, text, {
|
||||
...options,
|
||||
parse_mode: 'Markdown',
|
||||
});
|
||||
} catch (err) {
|
||||
// Fallback: send as plain text if Markdown parsing fails
|
||||
logger.debug({ err }, 'Markdown send failed, falling back to plain text');
|
||||
await api.sendMessage(chatId, text, options);
|
||||
}
|
||||
}
|
||||
|
||||
export class TelegramChannel implements Channel {
|
||||
name = 'telegram';
|
||||
|
||||
private bot: Bot | null = null;
|
||||
private opts: TelegramChannelOpts;
|
||||
private botToken: string;
|
||||
|
||||
constructor(botToken: string, opts: TelegramChannelOpts) {
|
||||
this.botToken = botToken;
|
||||
this.opts = opts;
|
||||
}
|
||||
|
||||
async connect(): Promise<void> {
|
||||
this.bot = new Bot(this.botToken, {
|
||||
client: {
|
||||
baseFetchConfig: { agent: https.globalAgent, compress: true },
|
||||
},
|
||||
});
|
||||
|
||||
// Command to get chat ID (useful for registration)
|
||||
this.bot.command('chatid', (ctx) => {
|
||||
const chatId = ctx.chat.id;
|
||||
const chatType = ctx.chat.type;
|
||||
const chatName =
|
||||
chatType === 'private'
|
||||
? ctx.from?.first_name || 'Private'
|
||||
: (ctx.chat as any).title || 'Unknown';
|
||||
|
||||
ctx.reply(
|
||||
`Chat ID: \`tg:${chatId}\`\nName: ${chatName}\nType: ${chatType}`,
|
||||
{ parse_mode: 'Markdown' },
|
||||
);
|
||||
});
|
||||
|
||||
// Command to check bot status
|
||||
this.bot.command('ping', (ctx) => {
|
||||
ctx.reply(`${ASSISTANT_NAME} is online.`);
|
||||
});
|
||||
|
||||
// Telegram bot commands handled above — skip them in the general handler
|
||||
// so they don't also get stored as messages. All other /commands flow through.
|
||||
const TELEGRAM_BOT_COMMANDS = new Set(['chatid', 'ping']);
|
||||
|
||||
this.bot.on('message:text', async (ctx) => {
|
||||
if (ctx.message.text.startsWith('/')) {
|
||||
const cmd = ctx.message.text.slice(1).split(/[\s@]/)[0].toLowerCase();
|
||||
if (TELEGRAM_BOT_COMMANDS.has(cmd)) return;
|
||||
}
|
||||
|
||||
const chatJid = `tg:${ctx.chat.id}`;
|
||||
let content = ctx.message.text;
|
||||
const timestamp = new Date(ctx.message.date * 1000).toISOString();
|
||||
const senderName =
|
||||
ctx.from?.first_name ||
|
||||
ctx.from?.username ||
|
||||
ctx.from?.id.toString() ||
|
||||
'Unknown';
|
||||
const sender = ctx.from?.id.toString() || '';
|
||||
const msgId = ctx.message.message_id.toString();
|
||||
|
||||
// Determine chat name
|
||||
const chatName =
|
||||
ctx.chat.type === 'private'
|
||||
? senderName
|
||||
: (ctx.chat as any).title || chatJid;
|
||||
|
||||
// Translate Telegram @bot_username mentions into TRIGGER_PATTERN format.
|
||||
// Telegram @mentions (e.g., @andy_ai_bot) won't match TRIGGER_PATTERN
|
||||
// (e.g., ^@Andy\b), so we prepend the trigger when the bot is @mentioned.
|
||||
const botUsername = ctx.me?.username?.toLowerCase();
|
||||
if (botUsername) {
|
||||
const entities = ctx.message.entities || [];
|
||||
const isBotMentioned = entities.some((entity) => {
|
||||
if (entity.type === 'mention') {
|
||||
const mentionText = content
|
||||
.substring(entity.offset, entity.offset + entity.length)
|
||||
.toLowerCase();
|
||||
return mentionText === `@${botUsername}`;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
if (isBotMentioned && !TRIGGER_PATTERN.test(content)) {
|
||||
content = `@${ASSISTANT_NAME} ${content}`;
|
||||
}
|
||||
}
|
||||
|
||||
// Store chat metadata for discovery
|
||||
const isGroup =
|
||||
ctx.chat.type === 'group' || ctx.chat.type === 'supergroup';
|
||||
this.opts.onChatMetadata(
|
||||
chatJid,
|
||||
timestamp,
|
||||
chatName,
|
||||
'telegram',
|
||||
isGroup,
|
||||
);
|
||||
|
||||
// Only deliver full message for registered groups
|
||||
const group = this.opts.registeredGroups()[chatJid];
|
||||
if (!group) {
|
||||
logger.debug(
|
||||
{ chatJid, chatName },
|
||||
'Message from unregistered Telegram chat',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Deliver message — startMessageLoop() will pick it up
|
||||
this.opts.onMessage(chatJid, {
|
||||
id: msgId,
|
||||
chat_jid: chatJid,
|
||||
sender,
|
||||
sender_name: senderName,
|
||||
content,
|
||||
timestamp,
|
||||
is_from_me: false,
|
||||
});
|
||||
|
||||
logger.info(
|
||||
{ chatJid, chatName, sender: senderName },
|
||||
'Telegram message stored',
|
||||
);
|
||||
});
|
||||
|
||||
// Handle non-text messages with placeholders so the agent knows something was sent
|
||||
const storeNonText = (ctx: any, placeholder: string) => {
|
||||
const chatJid = `tg:${ctx.chat.id}`;
|
||||
const group = this.opts.registeredGroups()[chatJid];
|
||||
if (!group) return;
|
||||
|
||||
const timestamp = new Date(ctx.message.date * 1000).toISOString();
|
||||
const senderName =
|
||||
ctx.from?.first_name ||
|
||||
ctx.from?.username ||
|
||||
ctx.from?.id?.toString() ||
|
||||
'Unknown';
|
||||
const caption = ctx.message.caption ? ` ${ctx.message.caption}` : '';
|
||||
|
||||
const isGroup =
|
||||
ctx.chat.type === 'group' || ctx.chat.type === 'supergroup';
|
||||
this.opts.onChatMetadata(
|
||||
chatJid,
|
||||
timestamp,
|
||||
undefined,
|
||||
'telegram',
|
||||
isGroup,
|
||||
);
|
||||
this.opts.onMessage(chatJid, {
|
||||
id: ctx.message.message_id.toString(),
|
||||
chat_jid: chatJid,
|
||||
sender: ctx.from?.id?.toString() || '',
|
||||
sender_name: senderName,
|
||||
content: `${placeholder}${caption}`,
|
||||
timestamp,
|
||||
is_from_me: false,
|
||||
});
|
||||
};
|
||||
|
||||
this.bot.on('message:photo', (ctx) => storeNonText(ctx, '[Photo]'));
|
||||
this.bot.on('message:video', (ctx) => storeNonText(ctx, '[Video]'));
|
||||
this.bot.on('message:voice', (ctx) => storeNonText(ctx, '[Voice message]'));
|
||||
this.bot.on('message:audio', (ctx) => storeNonText(ctx, '[Audio]'));
|
||||
this.bot.on('message:document', (ctx) => {
|
||||
const name = ctx.message.document?.file_name || 'file';
|
||||
storeNonText(ctx, `[Document: ${name}]`);
|
||||
});
|
||||
this.bot.on('message:sticker', (ctx) => {
|
||||
const emoji = ctx.message.sticker?.emoji || '';
|
||||
storeNonText(ctx, `[Sticker ${emoji}]`);
|
||||
});
|
||||
this.bot.on('message:location', (ctx) => storeNonText(ctx, '[Location]'));
|
||||
this.bot.on('message:contact', (ctx) => storeNonText(ctx, '[Contact]'));
|
||||
|
||||
// Handle errors gracefully
|
||||
this.bot.catch((err) => {
|
||||
logger.error({ err: err.message }, 'Telegram bot error');
|
||||
});
|
||||
|
||||
// Start polling — returns a Promise that resolves when started
|
||||
return new Promise<void>((resolve) => {
|
||||
this.bot!.start({
|
||||
onStart: (botInfo) => {
|
||||
logger.info(
|
||||
{ username: botInfo.username, id: botInfo.id },
|
||||
'Telegram bot connected',
|
||||
);
|
||||
console.log(`\n Telegram bot: @${botInfo.username}`);
|
||||
console.log(
|
||||
` Send /chatid to the bot to get a chat's registration ID\n`,
|
||||
);
|
||||
resolve();
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async sendMessage(jid: string, text: string): Promise<void> {
|
||||
if (!this.bot) {
|
||||
logger.warn('Telegram bot not initialized');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const numericId = jid.replace(/^tg:/, '');
|
||||
|
||||
// Telegram has a 4096 character limit per message — split if needed
|
||||
const MAX_LENGTH = 4096;
|
||||
if (text.length <= MAX_LENGTH) {
|
||||
await sendTelegramMessage(this.bot.api, numericId, text);
|
||||
} else {
|
||||
for (let i = 0; i < text.length; i += MAX_LENGTH) {
|
||||
await sendTelegramMessage(
|
||||
this.bot.api,
|
||||
numericId,
|
||||
text.slice(i, i + MAX_LENGTH),
|
||||
);
|
||||
}
|
||||
}
|
||||
logger.info({ jid, length: text.length }, 'Telegram message sent');
|
||||
} catch (err) {
|
||||
logger.error({ jid, err }, 'Failed to send Telegram message');
|
||||
}
|
||||
}
|
||||
|
||||
isConnected(): boolean {
|
||||
return this.bot !== null;
|
||||
}
|
||||
|
||||
ownsJid(jid: string): boolean {
|
||||
return jid.startsWith('tg:');
|
||||
}
|
||||
|
||||
async disconnect(): Promise<void> {
|
||||
if (this.bot) {
|
||||
this.bot.stop();
|
||||
this.bot = null;
|
||||
logger.info('Telegram bot stopped');
|
||||
}
|
||||
}
|
||||
|
||||
async setTyping(jid: string, isTyping: boolean): Promise<void> {
|
||||
if (!this.bot || !isTyping) return;
|
||||
try {
|
||||
const numericId = jid.replace(/^tg:/, '');
|
||||
await this.bot.api.sendChatAction(numericId, 'typing');
|
||||
} catch (err) {
|
||||
logger.debug({ jid, err }, 'Failed to send Telegram typing indicator');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
registerChannel('telegram', (opts: ChannelOpts) => {
|
||||
const envVars = readEnvFile(['TELEGRAM_BOT_TOKEN']);
|
||||
const token =
|
||||
process.env.TELEGRAM_BOT_TOKEN || envVars.TELEGRAM_BOT_TOKEN || '';
|
||||
if (!token) {
|
||||
logger.warn('Telegram: TELEGRAM_BOT_TOKEN not set');
|
||||
return null;
|
||||
}
|
||||
return new TelegramChannel(token, opts);
|
||||
});
|
||||
45
src/claw-skill.test.ts
Normal file
45
src/claw-skill.test.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import { spawnSync } from 'child_process';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe('claw skill script', () => {
|
||||
it('exits zero after successful structured output even if the runtime is terminated', { timeout: 20000 }, () => {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'claw-skill-test-'));
|
||||
const binDir = path.join(tempDir, 'bin');
|
||||
fs.mkdirSync(binDir, { recursive: true });
|
||||
|
||||
const runtimePath = path.join(binDir, 'container');
|
||||
fs.writeFileSync(
|
||||
runtimePath,
|
||||
`#!/bin/sh
|
||||
cat >/dev/null
|
||||
printf '%s\n' '---NANOCLAW_OUTPUT_START---' '{"status":"success","result":"4","newSessionId":"sess-1"}' '---NANOCLAW_OUTPUT_END---'
|
||||
sleep 30
|
||||
`,
|
||||
);
|
||||
fs.chmodSync(runtimePath, 0o755);
|
||||
|
||||
const result = spawnSync(
|
||||
'python3',
|
||||
['.claude/skills/claw/scripts/claw', '-j', 'tg:123', 'What is 2+2?'],
|
||||
{
|
||||
cwd: process.cwd(),
|
||||
encoding: 'utf8',
|
||||
env: {
|
||||
...process.env,
|
||||
NANOCLAW_DIR: tempDir,
|
||||
PATH: `${binDir}:${process.env.PATH || ''}`,
|
||||
},
|
||||
timeout: 15000,
|
||||
},
|
||||
);
|
||||
|
||||
expect(result.status).toBe(0);
|
||||
expect(result.signal).toBeNull();
|
||||
expect(result.stdout).toContain('4');
|
||||
expect(result.stderr).toContain('[session: sess-1]');
|
||||
});
|
||||
});
|
||||
@@ -2,11 +2,15 @@ import os from 'os';
|
||||
import path from 'path';
|
||||
|
||||
import { readEnvFile } from './env.js';
|
||||
import { isValidTimezone } from './timezone.js';
|
||||
|
||||
// Read config values from .env (falls back to process.env).
|
||||
// Secrets (API keys, tokens) are NOT read here — they are loaded only
|
||||
// by the credential proxy (credential-proxy.ts), never exposed to containers.
|
||||
const envConfig = readEnvFile(['ASSISTANT_NAME', 'ASSISTANT_HAS_OWN_NUMBER']);
|
||||
const envConfig = readEnvFile([
|
||||
'ASSISTANT_NAME',
|
||||
'ASSISTANT_HAS_OWN_NUMBER',
|
||||
'ONECLI_URL',
|
||||
'TZ',
|
||||
]);
|
||||
|
||||
export const ASSISTANT_NAME =
|
||||
process.env.ASSISTANT_NAME || envConfig.ASSISTANT_NAME || 'Andy';
|
||||
@@ -47,9 +51,11 @@ export const CONTAINER_MAX_OUTPUT_SIZE = parseInt(
|
||||
process.env.CONTAINER_MAX_OUTPUT_SIZE || '10485760',
|
||||
10,
|
||||
); // 10MB default
|
||||
export const CREDENTIAL_PROXY_PORT = parseInt(
|
||||
process.env.CREDENTIAL_PROXY_PORT || '4800',
|
||||
10,
|
||||
export const ONECLI_URL =
|
||||
process.env.ONECLI_URL || envConfig.ONECLI_URL || 'http://localhost:10254';
|
||||
export const MAX_MESSAGES_PER_PROMPT = Math.max(
|
||||
1,
|
||||
parseInt(process.env.MAX_MESSAGES_PER_PROMPT || '10', 10) || 10,
|
||||
);
|
||||
export const IPC_POLL_INTERVAL = 1000;
|
||||
export const IDLE_TIMEOUT = parseInt(process.env.IDLE_TIMEOUT || '1800000', 10); // 30min default — how long to keep container alive after last result
|
||||
@@ -62,13 +68,33 @@ function escapeRegex(str: string): string {
|
||||
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
}
|
||||
|
||||
// Trigger pattern - currently disabled so all messages are processed
|
||||
export const TRIGGER_PATTERN = /^/;
|
||||
export function buildTriggerPattern(trigger: string): RegExp {
|
||||
return new RegExp(`^${escapeRegex(trigger.trim())}\\b`, 'i');
|
||||
}
|
||||
|
||||
// Timezone for scheduled tasks (cron expressions, etc.)
|
||||
// Uses system timezone by default
|
||||
export const TIMEZONE =
|
||||
process.env.TZ || Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||
export const DEFAULT_TRIGGER = `@${ASSISTANT_NAME}`;
|
||||
|
||||
export function getTriggerPattern(trigger?: string): RegExp {
|
||||
const normalizedTrigger = trigger?.trim();
|
||||
return buildTriggerPattern(normalizedTrigger || DEFAULT_TRIGGER);
|
||||
}
|
||||
|
||||
export const TRIGGER_PATTERN = buildTriggerPattern(DEFAULT_TRIGGER);
|
||||
|
||||
// Timezone for scheduled tasks, message formatting, etc.
|
||||
// Validates each candidate is a real IANA identifier before accepting.
|
||||
function resolveConfigTimezone(): string {
|
||||
const candidates = [
|
||||
process.env.TZ,
|
||||
envConfig.TZ,
|
||||
Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||
];
|
||||
for (const tz of candidates) {
|
||||
if (tz && isValidTimezone(tz)) return tz;
|
||||
}
|
||||
return 'UTC';
|
||||
}
|
||||
export const TIMEZONE = resolveConfigTimezone();
|
||||
|
||||
const telegramPoolEnv = readEnvFile(['TELEGRAM_BOT_POOL']);
|
||||
export const TELEGRAM_BOT_POOL = (
|
||||
|
||||
@@ -11,10 +11,10 @@ vi.mock('./config.js', () => ({
|
||||
CONTAINER_IMAGE: 'nanoclaw-agent:latest',
|
||||
CONTAINER_MAX_OUTPUT_SIZE: 10485760,
|
||||
CONTAINER_TIMEOUT: 1800000, // 30min
|
||||
CREDENTIAL_PROXY_PORT: 3001,
|
||||
DATA_DIR: '/tmp/nanoclaw-test-data',
|
||||
GROUPS_DIR: '/tmp/nanoclaw-test-groups',
|
||||
IDLE_TIMEOUT: 1800000, // 30min
|
||||
ONECLI_URL: 'http://localhost:10254',
|
||||
TIMEZONE: 'America/Los_Angeles',
|
||||
}));
|
||||
|
||||
@@ -51,6 +51,25 @@ vi.mock('./mount-security.js', () => ({
|
||||
validateAdditionalMounts: vi.fn(() => []),
|
||||
}));
|
||||
|
||||
// Mock container-runtime
|
||||
vi.mock('./container-runtime.js', () => ({
|
||||
CONTAINER_RUNTIME_BIN: 'docker',
|
||||
hostGatewayArgs: () => [],
|
||||
readonlyMountArgs: (h: string, c: string) => ['-v', `${h}:${c}:ro`],
|
||||
stopContainer: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock OneCLI SDK
|
||||
vi.mock('@onecli-sh/sdk', () => ({
|
||||
OneCLI: class {
|
||||
applyContainerConfig = vi.fn().mockResolvedValue(true);
|
||||
createAgent = vi.fn().mockResolvedValue({ id: 'test' });
|
||||
ensureAgent = vi
|
||||
.fn()
|
||||
.mockResolvedValue({ name: 'test', identifier: 'test', created: true });
|
||||
},
|
||||
}));
|
||||
|
||||
// Create a controllable fake ChildProcess
|
||||
function createFakeProcess() {
|
||||
const proc = new EventEmitter() as EventEmitter & {
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
* Container Runner for NanoClaw
|
||||
* Spawns agent execution in containers and handles IPC
|
||||
*/
|
||||
import { ChildProcess, exec, spawn } from 'child_process';
|
||||
import os from 'os';
|
||||
import { ChildProcess, spawn } from 'child_process';
|
||||
|
||||
/** Detect if running on Apple Container runtime (vs Docker) */
|
||||
const isAppleContainer = CONTAINER_RUNTIME_BIN === 'container';
|
||||
@@ -14,26 +13,27 @@ import {
|
||||
CONTAINER_IMAGE,
|
||||
CONTAINER_MAX_OUTPUT_SIZE,
|
||||
CONTAINER_TIMEOUT,
|
||||
CREDENTIAL_PROXY_PORT,
|
||||
DATA_DIR,
|
||||
GROUPS_DIR,
|
||||
IDLE_TIMEOUT,
|
||||
ONECLI_URL,
|
||||
TIMEZONE,
|
||||
} from './config.js';
|
||||
import { resolveGroupFolderPath, resolveGroupIpcPath } from './group-folder.js';
|
||||
import { logger } from './logger.js';
|
||||
import {
|
||||
CONTAINER_HOST_GATEWAY,
|
||||
CONTAINER_RUNTIME_BIN,
|
||||
hostGatewayArgs,
|
||||
readonlyMountArgs,
|
||||
stopContainer,
|
||||
} from './container-runtime.js';
|
||||
import { detectAuthMode } from './credential-proxy.js';
|
||||
import { OneCLI } from '@onecli-sh/sdk';
|
||||
import { readEnvFile } from './env.js';
|
||||
import { validateAdditionalMounts } from './mount-security.js';
|
||||
import { RegisteredGroup } from './types.js';
|
||||
|
||||
const onecli = new OneCLI({ url: ONECLI_URL });
|
||||
|
||||
// Sentinel markers for robust output parsing (must match agent-runner)
|
||||
const OUTPUT_START_MARKER = '---NANOCLAW_OUTPUT_START---';
|
||||
const OUTPUT_END_MARKER = '---NANOCLAW_OUTPUT_END---';
|
||||
@@ -46,6 +46,7 @@ export interface ContainerInput {
|
||||
isMain: boolean;
|
||||
isScheduledTask?: boolean;
|
||||
assistantName?: string;
|
||||
script?: string;
|
||||
}
|
||||
|
||||
export interface ContainerOutput {
|
||||
@@ -82,7 +83,7 @@ function buildVolumeMounts(
|
||||
});
|
||||
|
||||
// Shadow .env so the agent cannot read secrets from the mounted project root.
|
||||
// Credentials are injected by the credential proxy, never exposed to containers.
|
||||
// Credentials are injected by the OneCLI gateway, never exposed to containers.
|
||||
// Skip this mount on Apple Container since it doesn't support bind-mounting /dev/null.
|
||||
const envFile = path.join(projectRoot, '.env');
|
||||
if (fs.existsSync(envFile) && !isAppleContainer) {
|
||||
@@ -205,8 +206,17 @@ function buildVolumeMounts(
|
||||
group.folder,
|
||||
'agent-runner-src',
|
||||
);
|
||||
if (!fs.existsSync(groupAgentRunnerDir) && fs.existsSync(agentRunnerSrc)) {
|
||||
fs.cpSync(agentRunnerSrc, groupAgentRunnerDir, { recursive: true });
|
||||
if (fs.existsSync(agentRunnerSrc)) {
|
||||
const srcIndex = path.join(agentRunnerSrc, 'index.ts');
|
||||
const cachedIndex = path.join(groupAgentRunnerDir, 'index.ts');
|
||||
const needsCopy =
|
||||
!fs.existsSync(groupAgentRunnerDir) ||
|
||||
!fs.existsSync(cachedIndex) ||
|
||||
(fs.existsSync(srcIndex) &&
|
||||
fs.statSync(srcIndex).mtimeMs > fs.statSync(cachedIndex).mtimeMs);
|
||||
if (needsCopy) {
|
||||
fs.cpSync(agentRunnerSrc, groupAgentRunnerDir, { recursive: true });
|
||||
}
|
||||
}
|
||||
mounts.push({
|
||||
hostPath: groupAgentRunnerDir,
|
||||
@@ -227,11 +237,12 @@ function buildVolumeMounts(
|
||||
return mounts;
|
||||
}
|
||||
|
||||
function buildContainerArgs(
|
||||
async function buildContainerArgs(
|
||||
mounts: VolumeMount[],
|
||||
containerName: string,
|
||||
isMain: boolean,
|
||||
): string[] {
|
||||
agentIdentifier?: string,
|
||||
): Promise<string[]> {
|
||||
const args: string[] = ['run', '-i', '--rm', '--name', containerName];
|
||||
|
||||
// Pass host timezone so container's local time matches the user's
|
||||
@@ -240,21 +251,19 @@ function buildContainerArgs(
|
||||
// Prefer IPv4 for DNS resolution to avoid potential delays
|
||||
args.push('-e', 'NODE_OPTIONS=--dns-result-order=ipv4first');
|
||||
|
||||
// Route API traffic through the credential proxy (containers never see real secrets)
|
||||
args.push(
|
||||
'-e',
|
||||
`ANTHROPIC_BASE_URL=http://${CONTAINER_HOST_GATEWAY}:${CREDENTIAL_PROXY_PORT}`,
|
||||
);
|
||||
|
||||
// Mirror the host's auth method with a placeholder value.
|
||||
// API key mode: SDK sends x-api-key, proxy replaces with real key.
|
||||
// OAuth mode: SDK exchanges placeholder token for temp API key,
|
||||
// proxy injects real OAuth token on that exchange request.
|
||||
const authMode = detectAuthMode();
|
||||
if (authMode === 'api-key') {
|
||||
args.push('-e', 'ANTHROPIC_API_KEY=placeholder');
|
||||
// OneCLI gateway handles credential injection — containers never see real secrets.
|
||||
// The gateway intercepts HTTPS traffic and injects API keys or OAuth tokens.
|
||||
const onecliApplied = await onecli.applyContainerConfig(args, {
|
||||
addHostMapping: false, // Nanoclaw already handles host gateway
|
||||
agent: agentIdentifier,
|
||||
});
|
||||
if (onecliApplied) {
|
||||
logger.info({ containerName }, 'OneCLI gateway config applied');
|
||||
} else {
|
||||
args.push('-e', 'CLAUDE_CODE_OAUTH_TOKEN=placeholder');
|
||||
logger.warn(
|
||||
{ containerName },
|
||||
'OneCLI gateway not reachable — container will have no credentials',
|
||||
);
|
||||
}
|
||||
|
||||
// Forward model and SDK configuration from .env to the container.
|
||||
@@ -321,7 +330,16 @@ export async function runContainerAgent(
|
||||
const mounts = buildVolumeMounts(group, input.isMain);
|
||||
const safeName = group.folder.replace(/[^a-zA-Z0-9-]/g, '-');
|
||||
const containerName = `nanoclaw-${safeName}-${Date.now()}`;
|
||||
const containerArgs = buildContainerArgs(mounts, containerName, input.isMain);
|
||||
// Main group uses the default OneCLI agent; others use their own agent.
|
||||
const agentIdentifier = input.isMain
|
||||
? undefined
|
||||
: group.folder.toLowerCase().replace(/_/g, '-');
|
||||
const containerArgs = await buildContainerArgs(
|
||||
mounts,
|
||||
containerName,
|
||||
input.isMain,
|
||||
agentIdentifier,
|
||||
);
|
||||
|
||||
logger.debug(
|
||||
{
|
||||
@@ -456,15 +474,15 @@ export async function runContainerAgent(
|
||||
{ group: group.name, containerName },
|
||||
'Container timeout, stopping gracefully',
|
||||
);
|
||||
exec(stopContainer(containerName), { timeout: 15000 }, (err) => {
|
||||
if (err) {
|
||||
logger.warn(
|
||||
{ group: group.name, containerName, err },
|
||||
'Graceful stop failed, force killing',
|
||||
);
|
||||
container.kill('SIGKILL');
|
||||
}
|
||||
});
|
||||
try {
|
||||
stopContainer(containerName);
|
||||
} catch (err) {
|
||||
logger.warn(
|
||||
{ group: group.name, containerName, err },
|
||||
'Graceful stop failed, force killing',
|
||||
);
|
||||
container.kill('SIGKILL');
|
||||
}
|
||||
};
|
||||
|
||||
let timeout = setTimeout(killOnTimeout, timeoutMs);
|
||||
@@ -702,6 +720,7 @@ export function writeTasksSnapshot(
|
||||
id: string;
|
||||
groupFolder: string;
|
||||
prompt: string;
|
||||
script?: string | null;
|
||||
schedule_type: string;
|
||||
schedule_value: string;
|
||||
status: string;
|
||||
|
||||
@@ -42,11 +42,20 @@ describe('readonlyMountArgs', () => {
|
||||
});
|
||||
|
||||
describe('stopContainer', () => {
|
||||
it('returns stop command using CONTAINER_RUNTIME_BIN', () => {
|
||||
expect(stopContainer('nanoclaw-test-123')).toBe(
|
||||
`${CONTAINER_RUNTIME_BIN} stop nanoclaw-test-123`,
|
||||
it('calls docker stop for valid container names', () => {
|
||||
stopContainer('nanoclaw-test-123');
|
||||
expect(mockExecSync).toHaveBeenCalledWith(
|
||||
`${CONTAINER_RUNTIME_BIN} stop -t 1 nanoclaw-test-123`,
|
||||
{ stdio: 'pipe' },
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects names with shell metacharacters', () => {
|
||||
expect(() => stopContainer('foo; rm -rf /')).toThrow('Invalid container name');
|
||||
expect(() => stopContainer('foo$(whoami)')).toThrow('Invalid container name');
|
||||
expect(() => stopContainer('foo`id`')).toThrow('Invalid container name');
|
||||
expect(mockExecSync).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
// --- ensureContainerRuntimeRunning ---
|
||||
@@ -59,82 +68,57 @@ describe('ensureContainerRuntimeRunning', () => {
|
||||
|
||||
expect(mockExecSync).toHaveBeenCalledTimes(1);
|
||||
expect(mockExecSync).toHaveBeenCalledWith(
|
||||
`${CONTAINER_RUNTIME_BIN} system status`,
|
||||
`${CONTAINER_RUNTIME_BIN} info`,
|
||||
{ stdio: 'pipe' },
|
||||
);
|
||||
expect(logger.debug).toHaveBeenCalledWith(
|
||||
'Container runtime already running',
|
||||
'Docker runtime already running',
|
||||
);
|
||||
});
|
||||
|
||||
it('auto-starts when system status fails', () => {
|
||||
// First call (system status) fails
|
||||
it('throws when docker info fails', () => {
|
||||
mockExecSync.mockImplementationOnce(() => {
|
||||
throw new Error('not running');
|
||||
});
|
||||
// Second call (system start) succeeds
|
||||
mockExecSync.mockReturnValueOnce('');
|
||||
|
||||
ensureContainerRuntimeRunning();
|
||||
|
||||
expect(mockExecSync).toHaveBeenCalledTimes(2);
|
||||
expect(mockExecSync).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
`${CONTAINER_RUNTIME_BIN} system start`,
|
||||
{ stdio: 'pipe', timeout: 30000 },
|
||||
);
|
||||
expect(logger.info).toHaveBeenCalledWith('Container runtime started');
|
||||
});
|
||||
|
||||
it('throws when both status and start fail', () => {
|
||||
mockExecSync.mockImplementation(() => {
|
||||
throw new Error('failed');
|
||||
throw new Error('Cannot connect to the Docker daemon');
|
||||
});
|
||||
|
||||
expect(() => ensureContainerRuntimeRunning()).toThrow(
|
||||
'Container runtime is required but failed to start',
|
||||
);
|
||||
expect(logger.error).toHaveBeenCalled();
|
||||
expect(() => ensureContainerRuntimeRunning()).toThrow();
|
||||
expect(logger.fatal).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
// --- cleanupOrphans ---
|
||||
|
||||
describe('cleanupOrphans', () => {
|
||||
it('stops orphaned nanoclaw containers from JSON output', () => {
|
||||
// Apple Container ls returns JSON
|
||||
const lsOutput = JSON.stringify([
|
||||
{ status: 'running', configuration: { id: 'nanoclaw-group1-111' } },
|
||||
{ status: 'stopped', configuration: { id: 'nanoclaw-group2-222' } },
|
||||
{ status: 'running', configuration: { id: 'nanoclaw-group3-333' } },
|
||||
{ status: 'running', configuration: { id: 'other-container' } },
|
||||
]);
|
||||
mockExecSync.mockReturnValueOnce(lsOutput);
|
||||
it('stops orphaned nanoclaw containers', () => {
|
||||
// docker ps returns container names, one per line
|
||||
mockExecSync.mockReturnValueOnce(
|
||||
'nanoclaw-group1-111\nnanoclaw-group2-222\n',
|
||||
);
|
||||
// stop calls succeed
|
||||
mockExecSync.mockReturnValue('');
|
||||
|
||||
cleanupOrphans();
|
||||
|
||||
// ls + 2 stop calls (only running nanoclaw- containers)
|
||||
// ps + 2 stop calls
|
||||
expect(mockExecSync).toHaveBeenCalledTimes(3);
|
||||
expect(mockExecSync).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
`${CONTAINER_RUNTIME_BIN} stop nanoclaw-group1-111`,
|
||||
`${CONTAINER_RUNTIME_BIN} stop -t 1 nanoclaw-group1-111`,
|
||||
{ stdio: 'pipe' },
|
||||
);
|
||||
expect(mockExecSync).toHaveBeenNthCalledWith(
|
||||
3,
|
||||
`${CONTAINER_RUNTIME_BIN} stop nanoclaw-group3-333`,
|
||||
`${CONTAINER_RUNTIME_BIN} stop -t 1 nanoclaw-group2-222`,
|
||||
{ stdio: 'pipe' },
|
||||
);
|
||||
expect(logger.info).toHaveBeenCalledWith(
|
||||
{ count: 2, names: ['nanoclaw-group1-111', 'nanoclaw-group3-333'] },
|
||||
{ count: 2, names: ['nanoclaw-group1-111', 'nanoclaw-group2-222'] },
|
||||
'Stopped orphaned containers',
|
||||
);
|
||||
});
|
||||
|
||||
it('does nothing when no orphans exist', () => {
|
||||
mockExecSync.mockReturnValueOnce('[]');
|
||||
mockExecSync.mockReturnValueOnce('');
|
||||
|
||||
cleanupOrphans();
|
||||
|
||||
@@ -142,9 +126,9 @@ describe('cleanupOrphans', () => {
|
||||
expect(logger.info).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('warns and continues when ls fails', () => {
|
||||
it('warns and continues when ps fails', () => {
|
||||
mockExecSync.mockImplementationOnce(() => {
|
||||
throw new Error('container not available');
|
||||
throw new Error('docker not available');
|
||||
});
|
||||
|
||||
cleanupOrphans(); // should not throw
|
||||
@@ -156,11 +140,7 @@ describe('cleanupOrphans', () => {
|
||||
});
|
||||
|
||||
it('continues stopping remaining containers when one stop fails', () => {
|
||||
const lsOutput = JSON.stringify([
|
||||
{ status: 'running', configuration: { id: 'nanoclaw-a-1' } },
|
||||
{ status: 'running', configuration: { id: 'nanoclaw-b-2' } },
|
||||
]);
|
||||
mockExecSync.mockReturnValueOnce(lsOutput);
|
||||
mockExecSync.mockReturnValueOnce('nanoclaw-a-1\nnanoclaw-b-2\n');
|
||||
// First stop fails
|
||||
mockExecSync.mockImplementationOnce(() => {
|
||||
throw new Error('already stopped');
|
||||
|
||||
@@ -3,39 +3,12 @@
|
||||
* All runtime-specific logic lives here so swapping runtimes means changing one file.
|
||||
*/
|
||||
import { execSync } from 'child_process';
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
|
||||
import { logger } from './logger.js';
|
||||
|
||||
/** The container runtime binary name. Switched to docker (OrbStack) for stability. */
|
||||
export const CONTAINER_RUNTIME_BIN = 'docker';
|
||||
|
||||
/** Hostname containers use to reach the host machine. */
|
||||
export const CONTAINER_HOST_GATEWAY = 'host.docker.internal';
|
||||
|
||||
/**
|
||||
* Address the credential proxy binds to.
|
||||
* Docker Desktop/OrbStack (macOS): 127.0.0.1 — the VM routes host.docker.internal to loopback.
|
||||
*/
|
||||
export const PROXY_BIND_HOST =
|
||||
process.env.CREDENTIAL_PROXY_HOST || detectProxyBindHost();
|
||||
|
||||
function detectProxyBindHost(): string {
|
||||
if (os.platform() === 'darwin') return '127.0.0.1';
|
||||
|
||||
// WSL uses Docker Desktop (same VM routing as macOS) — loopback is correct.
|
||||
if (fs.existsSync('/proc/sys/fs/binfmt_misc/WSLInterop')) return '127.0.0.1';
|
||||
|
||||
// Bare-metal Linux: bind to the docker0 bridge IP instead of 0.0.0.0
|
||||
const ifaces = os.networkInterfaces();
|
||||
const docker0 = ifaces['docker0'];
|
||||
if (docker0) {
|
||||
const ipv4 = docker0.find((a) => a.family === 'IPv4');
|
||||
if (ipv4) return ipv4.address;
|
||||
}
|
||||
return '0.0.0.0';
|
||||
}
|
||||
export const CONTAINER_RUNTIME_BIN: string = 'docker';
|
||||
|
||||
/** CLI args needed for the container to resolve the host gateway. */
|
||||
export function hostGatewayArgs(): string[] {
|
||||
@@ -58,9 +31,12 @@ export function readonlyMountArgs(
|
||||
];
|
||||
}
|
||||
|
||||
/** Returns the shell command to stop a container by name. */
|
||||
export function stopContainer(name: string): string {
|
||||
return `${CONTAINER_RUNTIME_BIN} stop ${name}`;
|
||||
/** Stop a container by name. Uses execFileSync to avoid shell injection. */
|
||||
export function stopContainer(name: string): void {
|
||||
if (!/^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/.test(name)) {
|
||||
throw new Error(`Invalid container name: ${name}`);
|
||||
}
|
||||
execSync(`${CONTAINER_RUNTIME_BIN} stop -t 1 ${name}`, { stdio: 'pipe' });
|
||||
}
|
||||
|
||||
/** Ensure the container runtime is running, starting it if needed. */
|
||||
@@ -90,8 +66,7 @@ export function cleanupOrphans(): void {
|
||||
const orphans = output.split('\n').filter(Boolean);
|
||||
for (const name of orphans) {
|
||||
try {
|
||||
execSync(stopContainer(name), { stdio: 'pipe' });
|
||||
execSync(`${CONTAINER_RUNTIME_BIN} rm ${name}`, { stdio: 'pipe' });
|
||||
stopContainer(name);
|
||||
} catch {
|
||||
/* already stopped */
|
||||
}
|
||||
|
||||
@@ -1,192 +0,0 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import http from 'http';
|
||||
import type { AddressInfo } from 'net';
|
||||
|
||||
const mockEnv: Record<string, string> = {};
|
||||
vi.mock('./env.js', () => ({
|
||||
readEnvFile: vi.fn(() => ({ ...mockEnv })),
|
||||
}));
|
||||
|
||||
vi.mock('./logger.js', () => ({
|
||||
logger: { info: vi.fn(), error: vi.fn(), debug: vi.fn(), warn: vi.fn() },
|
||||
}));
|
||||
|
||||
import { startCredentialProxy } from './credential-proxy.js';
|
||||
|
||||
function makeRequest(
|
||||
port: number,
|
||||
options: http.RequestOptions,
|
||||
body = '',
|
||||
): Promise<{
|
||||
statusCode: number;
|
||||
body: string;
|
||||
headers: http.IncomingHttpHeaders;
|
||||
}> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = http.request(
|
||||
{ ...options, hostname: '127.0.0.1', port },
|
||||
(res) => {
|
||||
const chunks: Buffer[] = [];
|
||||
res.on('data', (c) => chunks.push(c));
|
||||
res.on('end', () => {
|
||||
resolve({
|
||||
statusCode: res.statusCode!,
|
||||
body: Buffer.concat(chunks).toString(),
|
||||
headers: res.headers,
|
||||
});
|
||||
});
|
||||
},
|
||||
);
|
||||
req.on('error', reject);
|
||||
req.write(body);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
describe('credential-proxy', () => {
|
||||
let proxyServer: http.Server;
|
||||
let upstreamServer: http.Server;
|
||||
let proxyPort: number;
|
||||
let upstreamPort: number;
|
||||
let lastUpstreamHeaders: http.IncomingHttpHeaders;
|
||||
|
||||
beforeEach(async () => {
|
||||
lastUpstreamHeaders = {};
|
||||
|
||||
upstreamServer = http.createServer((req, res) => {
|
||||
lastUpstreamHeaders = { ...req.headers };
|
||||
res.writeHead(200, { 'content-type': 'application/json' });
|
||||
res.end(JSON.stringify({ ok: true }));
|
||||
});
|
||||
await new Promise<void>((resolve) =>
|
||||
upstreamServer.listen(0, '127.0.0.1', resolve),
|
||||
);
|
||||
upstreamPort = (upstreamServer.address() as AddressInfo).port;
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await new Promise<void>((r) => proxyServer?.close(() => r()));
|
||||
await new Promise<void>((r) => upstreamServer?.close(() => r()));
|
||||
for (const key of Object.keys(mockEnv)) delete mockEnv[key];
|
||||
});
|
||||
|
||||
async function startProxy(env: Record<string, string>): Promise<number> {
|
||||
Object.assign(mockEnv, env, {
|
||||
ANTHROPIC_BASE_URL: `http://127.0.0.1:${upstreamPort}`,
|
||||
});
|
||||
proxyServer = await startCredentialProxy(0);
|
||||
return (proxyServer.address() as AddressInfo).port;
|
||||
}
|
||||
|
||||
it('API-key mode injects x-api-key and strips placeholder', async () => {
|
||||
proxyPort = await startProxy({ ANTHROPIC_API_KEY: 'sk-ant-real-key' });
|
||||
|
||||
await makeRequest(
|
||||
proxyPort,
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/v1/messages',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'x-api-key': 'placeholder',
|
||||
},
|
||||
},
|
||||
'{}',
|
||||
);
|
||||
|
||||
expect(lastUpstreamHeaders['x-api-key']).toBe('sk-ant-real-key');
|
||||
});
|
||||
|
||||
it('OAuth mode replaces Authorization when container sends one', async () => {
|
||||
proxyPort = await startProxy({
|
||||
CLAUDE_CODE_OAUTH_TOKEN: 'real-oauth-token',
|
||||
});
|
||||
|
||||
await makeRequest(
|
||||
proxyPort,
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/api/oauth/claude_cli/create_api_key',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
authorization: 'Bearer placeholder',
|
||||
},
|
||||
},
|
||||
'{}',
|
||||
);
|
||||
|
||||
expect(lastUpstreamHeaders['authorization']).toBe(
|
||||
'Bearer real-oauth-token',
|
||||
);
|
||||
});
|
||||
|
||||
it('OAuth mode does not inject Authorization when container omits it', async () => {
|
||||
proxyPort = await startProxy({
|
||||
CLAUDE_CODE_OAUTH_TOKEN: 'real-oauth-token',
|
||||
});
|
||||
|
||||
// Post-exchange: container uses x-api-key only, no Authorization header
|
||||
await makeRequest(
|
||||
proxyPort,
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/v1/messages',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'x-api-key': 'temp-key-from-exchange',
|
||||
},
|
||||
},
|
||||
'{}',
|
||||
);
|
||||
|
||||
expect(lastUpstreamHeaders['x-api-key']).toBe('temp-key-from-exchange');
|
||||
expect(lastUpstreamHeaders['authorization']).toBeUndefined();
|
||||
});
|
||||
|
||||
it('strips hop-by-hop headers', async () => {
|
||||
proxyPort = await startProxy({ ANTHROPIC_API_KEY: 'sk-ant-real-key' });
|
||||
|
||||
await makeRequest(
|
||||
proxyPort,
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/v1/messages',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
connection: 'keep-alive',
|
||||
'keep-alive': 'timeout=5',
|
||||
'transfer-encoding': 'chunked',
|
||||
},
|
||||
},
|
||||
'{}',
|
||||
);
|
||||
|
||||
// Proxy strips client hop-by-hop headers. Node's HTTP client may re-add
|
||||
// its own Connection header (standard HTTP/1.1 behavior), but the client's
|
||||
// custom keep-alive and transfer-encoding must not be forwarded.
|
||||
expect(lastUpstreamHeaders['keep-alive']).toBeUndefined();
|
||||
expect(lastUpstreamHeaders['transfer-encoding']).toBeUndefined();
|
||||
});
|
||||
|
||||
it('returns 502 when upstream is unreachable', async () => {
|
||||
Object.assign(mockEnv, {
|
||||
ANTHROPIC_API_KEY: 'sk-ant-real-key',
|
||||
ANTHROPIC_BASE_URL: 'http://127.0.0.1:59999',
|
||||
});
|
||||
proxyServer = await startCredentialProxy(0);
|
||||
proxyPort = (proxyServer.address() as AddressInfo).port;
|
||||
|
||||
const res = await makeRequest(
|
||||
proxyPort,
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/v1/messages',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
},
|
||||
'{}',
|
||||
);
|
||||
|
||||
expect(res.statusCode).toBe(502);
|
||||
expect(res.body).toBe('Bad Gateway');
|
||||
});
|
||||
});
|
||||
@@ -1,128 +0,0 @@
|
||||
/**
|
||||
* Credential proxy for container isolation.
|
||||
* Containers connect here instead of directly to the Anthropic API.
|
||||
* The proxy injects real credentials so containers never see them.
|
||||
*
|
||||
* Two auth modes:
|
||||
* API key: Proxy injects x-api-key on every request.
|
||||
* OAuth: Container CLI exchanges its placeholder token for a temp
|
||||
* API key via /api/oauth/claude_cli/create_api_key.
|
||||
* Proxy injects real OAuth token on that exchange request;
|
||||
* subsequent requests carry the temp key which is valid as-is.
|
||||
*/
|
||||
import { createServer, Server } from 'http';
|
||||
import { request as httpsRequest } from 'https';
|
||||
import { request as httpRequest, RequestOptions } from 'http';
|
||||
|
||||
import { readEnvFile } from './env.js';
|
||||
import { logger } from './logger.js';
|
||||
|
||||
export type AuthMode = 'api-key' | 'oauth';
|
||||
|
||||
export interface ProxyConfig {
|
||||
authMode: AuthMode;
|
||||
}
|
||||
|
||||
export function startCredentialProxy(
|
||||
port: number,
|
||||
host = '0.0.0.0',
|
||||
): Promise<Server> {
|
||||
const secrets = readEnvFile([
|
||||
'ANTHROPIC_API_KEY',
|
||||
'CLAUDE_CODE_OAUTH_TOKEN',
|
||||
'ANTHROPIC_AUTH_TOKEN',
|
||||
'ANTHROPIC_BASE_URL',
|
||||
]);
|
||||
|
||||
const authMode: AuthMode = secrets.ANTHROPIC_API_KEY ? 'api-key' : 'oauth';
|
||||
const oauthToken =
|
||||
secrets.CLAUDE_CODE_OAUTH_TOKEN || secrets.ANTHROPIC_AUTH_TOKEN;
|
||||
|
||||
const upstreamUrl = new URL(
|
||||
secrets.ANTHROPIC_BASE_URL || 'https://api.anthropic.com',
|
||||
);
|
||||
// Preserve the base URL's pathname prefix (e.g. /anthropic for MiniMax)
|
||||
// so requests to /v1/messages become /anthropic/v1/messages upstream.
|
||||
const basePath = upstreamUrl.pathname.replace(/\/+$/, '');
|
||||
const isHttps = upstreamUrl.protocol === 'https:';
|
||||
const makeRequest = isHttps ? httpsRequest : httpRequest;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const server = createServer((req, res) => {
|
||||
const chunks: Buffer[] = [];
|
||||
req.on('data', (c) => chunks.push(c));
|
||||
req.on('end', () => {
|
||||
const body = Buffer.concat(chunks);
|
||||
const headers: Record<string, string | number | string[] | undefined> =
|
||||
{
|
||||
...(req.headers as Record<string, string>),
|
||||
host: upstreamUrl.host,
|
||||
'content-length': body.length,
|
||||
};
|
||||
|
||||
// Strip hop-by-hop headers that must not be forwarded by proxies
|
||||
delete headers['connection'];
|
||||
delete headers['keep-alive'];
|
||||
delete headers['transfer-encoding'];
|
||||
|
||||
if (authMode === 'api-key') {
|
||||
// API key mode: inject x-api-key on every request
|
||||
delete headers['x-api-key'];
|
||||
headers['x-api-key'] = secrets.ANTHROPIC_API_KEY;
|
||||
} else {
|
||||
// OAuth mode: replace placeholder Bearer token with the real one
|
||||
// only when the container actually sends an Authorization header
|
||||
// (exchange request + auth probes). Post-exchange requests use
|
||||
// x-api-key only, so they pass through without token injection.
|
||||
if (headers['authorization']) {
|
||||
delete headers['authorization'];
|
||||
if (oauthToken) {
|
||||
headers['authorization'] = `Bearer ${oauthToken}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const upstream = makeRequest(
|
||||
{
|
||||
hostname: upstreamUrl.hostname,
|
||||
port: upstreamUrl.port || (isHttps ? 443 : 80),
|
||||
path: basePath + req.url,
|
||||
method: req.method,
|
||||
headers,
|
||||
} as RequestOptions,
|
||||
(upRes) => {
|
||||
res.writeHead(upRes.statusCode!, upRes.headers);
|
||||
upRes.pipe(res);
|
||||
},
|
||||
);
|
||||
|
||||
upstream.on('error', (err) => {
|
||||
logger.error(
|
||||
{ err, url: req.url },
|
||||
'Credential proxy upstream error',
|
||||
);
|
||||
if (!res.headersSent) {
|
||||
res.writeHead(502);
|
||||
res.end('Bad Gateway');
|
||||
}
|
||||
});
|
||||
|
||||
upstream.write(body);
|
||||
upstream.end();
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(port, host, () => {
|
||||
logger.info({ port, host, authMode }, 'Credential proxy started');
|
||||
resolve(server);
|
||||
});
|
||||
|
||||
server.on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
/** Detect which auth mode the host is configured for. */
|
||||
export function detectAuthMode(): AuthMode {
|
||||
const secrets = readEnvFile(['ANTHROPIC_API_KEY']);
|
||||
return secrets.ANTHROPIC_API_KEY ? 'api-key' : 'oauth';
|
||||
}
|
||||
67
src/db-migration.test.ts
Normal file
67
src/db-migration.test.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import Database from 'better-sqlite3';
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
describe('database migrations', () => {
|
||||
it('defaults Telegram backfill chats to direct messages', async () => {
|
||||
const repoRoot = process.cwd();
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nanoclaw-db-test-'));
|
||||
|
||||
try {
|
||||
process.chdir(tempDir);
|
||||
fs.mkdirSync(path.join(tempDir, 'store'), { recursive: true });
|
||||
|
||||
const dbPath = path.join(tempDir, 'store', 'messages.db');
|
||||
const legacyDb = new Database(dbPath);
|
||||
legacyDb.exec(`
|
||||
CREATE TABLE chats (
|
||||
jid TEXT PRIMARY KEY,
|
||||
name TEXT,
|
||||
last_message_time TEXT
|
||||
);
|
||||
`);
|
||||
legacyDb
|
||||
.prepare(
|
||||
`INSERT INTO chats (jid, name, last_message_time) VALUES (?, ?, ?)`,
|
||||
)
|
||||
.run('tg:12345', 'Telegram DM', '2024-01-01T00:00:00.000Z');
|
||||
legacyDb
|
||||
.prepare(
|
||||
`INSERT INTO chats (jid, name, last_message_time) VALUES (?, ?, ?)`,
|
||||
)
|
||||
.run('tg:-10012345', 'Telegram Group', '2024-01-01T00:00:01.000Z');
|
||||
legacyDb
|
||||
.prepare(
|
||||
`INSERT INTO chats (jid, name, last_message_time) VALUES (?, ?, ?)`,
|
||||
)
|
||||
.run('room@g.us', 'WhatsApp Group', '2024-01-01T00:00:02.000Z');
|
||||
legacyDb.close();
|
||||
|
||||
vi.resetModules();
|
||||
const { initDatabase, getAllChats, _closeDatabase } =
|
||||
await import('./db.js');
|
||||
|
||||
initDatabase();
|
||||
|
||||
const chats = getAllChats();
|
||||
expect(chats.find((chat) => chat.jid === 'tg:12345')).toMatchObject({
|
||||
channel: 'telegram',
|
||||
is_group: 0,
|
||||
});
|
||||
expect(chats.find((chat) => chat.jid === 'tg:-10012345')).toMatchObject({
|
||||
channel: 'telegram',
|
||||
is_group: 0,
|
||||
});
|
||||
expect(chats.find((chat) => chat.jid === 'room@g.us')).toMatchObject({
|
||||
channel: 'whatsapp',
|
||||
is_group: 1,
|
||||
});
|
||||
|
||||
_closeDatabase();
|
||||
} finally {
|
||||
process.chdir(repoRoot);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
deleteTask,
|
||||
getAllChats,
|
||||
getAllRegisteredGroups,
|
||||
getLastBotMessageTimestamp,
|
||||
getMessagesSince,
|
||||
getNewMessages,
|
||||
getTaskById,
|
||||
@@ -14,6 +15,7 @@ import {
|
||||
storeMessage,
|
||||
updateTask,
|
||||
} from './db.js';
|
||||
import { formatMessages } from './router.js';
|
||||
|
||||
beforeEach(() => {
|
||||
_initTestDatabase();
|
||||
@@ -208,6 +210,92 @@ describe('getMessagesSince', () => {
|
||||
expect(msgs).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('recovers cursor from last bot reply when lastAgentTimestamp is missing', () => {
|
||||
// beforeEach already inserts m3 (bot reply at 00:00:03) and m4 (user at 00:00:04)
|
||||
// Add more old history before the bot reply
|
||||
for (let i = 1; i <= 50; i++) {
|
||||
store({
|
||||
id: `history-${i}`,
|
||||
chat_jid: 'group@g.us',
|
||||
sender: 'user@s.whatsapp.net',
|
||||
sender_name: 'User',
|
||||
content: `old message ${i}`,
|
||||
timestamp: `2023-06-${String(i).padStart(2, '0')}T12:00:00.000Z`,
|
||||
});
|
||||
}
|
||||
|
||||
// New message after the bot reply (m3 at 00:00:03)
|
||||
store({
|
||||
id: 'new-1',
|
||||
chat_jid: 'group@g.us',
|
||||
sender: 'user@s.whatsapp.net',
|
||||
sender_name: 'User',
|
||||
content: 'new message after bot reply',
|
||||
timestamp: '2024-01-02T00:00:00.000Z',
|
||||
});
|
||||
|
||||
// Recover cursor from the last bot message (m3 from beforeEach)
|
||||
const recovered = getLastBotMessageTimestamp('group@g.us', 'Andy');
|
||||
expect(recovered).toBe('2024-01-01T00:00:03.000Z');
|
||||
|
||||
// Using recovered cursor: only gets messages after the bot reply
|
||||
const msgs = getMessagesSince('group@g.us', recovered!, 'Andy', 10);
|
||||
// m4 (third, 00:00:04) + new-1 — skips all 50 old messages and m1/m2
|
||||
expect(msgs).toHaveLength(2);
|
||||
expect(msgs[0].content).toBe('third');
|
||||
expect(msgs[1].content).toBe('new message after bot reply');
|
||||
});
|
||||
|
||||
it('caps messages to configured limit even with recovered cursor', () => {
|
||||
// beforeEach inserts m3 (bot at 00:00:03). Add 30 messages after it.
|
||||
for (let i = 1; i <= 30; i++) {
|
||||
store({
|
||||
id: `pending-${i}`,
|
||||
chat_jid: 'group@g.us',
|
||||
sender: 'user@s.whatsapp.net',
|
||||
sender_name: 'User',
|
||||
content: `pending message ${i}`,
|
||||
timestamp: `2024-02-${String(i).padStart(2, '0')}T12:00:00.000Z`,
|
||||
});
|
||||
}
|
||||
|
||||
const recovered = getLastBotMessageTimestamp('group@g.us', 'Andy');
|
||||
expect(recovered).toBe('2024-01-01T00:00:03.000Z');
|
||||
|
||||
// With limit=10, only the 10 most recent are returned
|
||||
const msgs = getMessagesSince('group@g.us', recovered!, 'Andy', 10);
|
||||
expect(msgs).toHaveLength(10);
|
||||
// Most recent 10: pending-21 through pending-30
|
||||
expect(msgs[0].content).toBe('pending message 21');
|
||||
expect(msgs[9].content).toBe('pending message 30');
|
||||
});
|
||||
|
||||
it('returns last N messages when no bot reply and no cursor exist', () => {
|
||||
// Use a fresh group with no bot messages
|
||||
storeChatMetadata('fresh@g.us', '2024-01-01T00:00:00.000Z');
|
||||
for (let i = 1; i <= 20; i++) {
|
||||
store({
|
||||
id: `fresh-${i}`,
|
||||
chat_jid: 'fresh@g.us',
|
||||
sender: 'user@s.whatsapp.net',
|
||||
sender_name: 'User',
|
||||
content: `message ${i}`,
|
||||
timestamp: `2024-02-${String(i).padStart(2, '0')}T12:00:00.000Z`,
|
||||
});
|
||||
}
|
||||
|
||||
const recovered = getLastBotMessageTimestamp('fresh@g.us', 'Andy');
|
||||
expect(recovered).toBeUndefined();
|
||||
|
||||
// No cursor → sinceTimestamp = '' but limit caps the result
|
||||
const msgs = getMessagesSince('fresh@g.us', '', 'Andy', 10);
|
||||
expect(msgs).toHaveLength(10);
|
||||
|
||||
const prompt = formatMessages(msgs, 'Asia/Jerusalem');
|
||||
const messageTagCount = (prompt.match(/<message /g) || []).length;
|
||||
expect(messageTagCount).toBe(10);
|
||||
});
|
||||
|
||||
it('filters pre-migration bot messages via content prefix backstop', () => {
|
||||
// Simulate a message written before migration: has prefix but is_bot_message = 0
|
||||
store({
|
||||
|
||||
43
src/db.ts
43
src/db.ts
@@ -103,6 +103,13 @@ function createSchema(database: Database.Database): void {
|
||||
/* column already exists */
|
||||
}
|
||||
|
||||
// Add script column if it doesn't exist (migration for existing DBs)
|
||||
try {
|
||||
database.exec(`ALTER TABLE scheduled_tasks ADD COLUMN script TEXT`);
|
||||
} catch {
|
||||
/* column already exists */
|
||||
}
|
||||
|
||||
// Add is_bot_message column if it doesn't exist (migration for existing DBs)
|
||||
try {
|
||||
database.exec(
|
||||
@@ -144,7 +151,7 @@ function createSchema(database: Database.Database): void {
|
||||
`UPDATE chats SET channel = 'discord', is_group = 1 WHERE jid LIKE 'dc:%'`,
|
||||
);
|
||||
database.exec(
|
||||
`UPDATE chats SET channel = 'telegram', is_group = 1 WHERE jid LIKE 'tg:%'`,
|
||||
`UPDATE chats SET channel = 'telegram', is_group = 0 WHERE jid LIKE 'tg:%'`,
|
||||
);
|
||||
} catch {
|
||||
/* columns already exist */
|
||||
@@ -168,6 +175,11 @@ export function _initTestDatabase(): void {
|
||||
createSchema(db);
|
||||
}
|
||||
|
||||
/** @internal - for tests only. */
|
||||
export function _closeDatabase(): void {
|
||||
db.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Store chat metadata only (no message content).
|
||||
* Used for all chats to enable group discovery without storing sensitive content.
|
||||
@@ -373,19 +385,33 @@ export function getMessagesSince(
|
||||
.all(chatJid, sinceTimestamp, `${botPrefix}:%`, limit) as NewMessage[];
|
||||
}
|
||||
|
||||
export function getLastBotMessageTimestamp(
|
||||
chatJid: string,
|
||||
botPrefix: string,
|
||||
): string | undefined {
|
||||
const row = db
|
||||
.prepare(
|
||||
`SELECT MAX(timestamp) as ts FROM messages
|
||||
WHERE chat_jid = ? AND (is_bot_message = 1 OR content LIKE ?)`,
|
||||
)
|
||||
.get(chatJid, `${botPrefix}:%`) as { ts: string | null } | undefined;
|
||||
return row?.ts ?? undefined;
|
||||
}
|
||||
|
||||
export function createTask(
|
||||
task: Omit<ScheduledTask, 'last_run' | 'last_result'>,
|
||||
): void {
|
||||
db.prepare(
|
||||
`
|
||||
INSERT INTO scheduled_tasks (id, group_folder, chat_jid, prompt, schedule_type, schedule_value, context_mode, next_run, status, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO scheduled_tasks (id, group_folder, chat_jid, prompt, script, schedule_type, schedule_value, context_mode, next_run, status, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`,
|
||||
).run(
|
||||
task.id,
|
||||
task.group_folder,
|
||||
task.chat_jid,
|
||||
task.prompt,
|
||||
task.script || null,
|
||||
task.schedule_type,
|
||||
task.schedule_value,
|
||||
task.context_mode || 'isolated',
|
||||
@@ -420,7 +446,12 @@ export function updateTask(
|
||||
updates: Partial<
|
||||
Pick<
|
||||
ScheduledTask,
|
||||
'prompt' | 'schedule_type' | 'schedule_value' | 'next_run' | 'status'
|
||||
| 'prompt'
|
||||
| 'script'
|
||||
| 'schedule_type'
|
||||
| 'schedule_value'
|
||||
| 'next_run'
|
||||
| 'status'
|
||||
>
|
||||
>,
|
||||
): void {
|
||||
@@ -431,6 +462,10 @@ export function updateTask(
|
||||
fields.push('prompt = ?');
|
||||
values.push(updates.prompt);
|
||||
}
|
||||
if (updates.script !== undefined) {
|
||||
fields.push('script = ?');
|
||||
values.push(updates.script || null);
|
||||
}
|
||||
if (updates.schedule_type !== undefined) {
|
||||
fields.push('schedule_type = ?');
|
||||
values.push(updates.schedule_type);
|
||||
|
||||
@@ -30,8 +30,9 @@ export function readEnvFile(keys: string[]): Record<string, string> {
|
||||
if (!wanted.has(key)) continue;
|
||||
let value = trimmed.slice(eqIdx + 1).trim();
|
||||
if (
|
||||
(value.startsWith('"') && value.endsWith('"')) ||
|
||||
(value.startsWith("'") && value.endsWith("'"))
|
||||
value.length >= 2 &&
|
||||
((value.startsWith('"') && value.endsWith('"')) ||
|
||||
(value.startsWith("'") && value.endsWith("'")))
|
||||
) {
|
||||
value = value.slice(1, -1);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
import { ASSISTANT_NAME, TRIGGER_PATTERN } from './config.js';
|
||||
import {
|
||||
ASSISTANT_NAME,
|
||||
getTriggerPattern,
|
||||
TRIGGER_PATTERN,
|
||||
} from './config.js';
|
||||
import {
|
||||
escapeXml,
|
||||
formatMessages,
|
||||
@@ -126,11 +130,60 @@ describe('formatMessages', () => {
|
||||
// --- TRIGGER_PATTERN ---
|
||||
|
||||
describe('TRIGGER_PATTERN', () => {
|
||||
// Trigger is currently disabled - all messages match
|
||||
it('matches any message (trigger disabled)', () => {
|
||||
expect(TRIGGER_PATTERN.test('hello')).toBe(true);
|
||||
expect(TRIGGER_PATTERN.test('@anything')).toBe(true);
|
||||
expect(TRIGGER_PATTERN.test('anything')).toBe(true);
|
||||
const name = ASSISTANT_NAME;
|
||||
const lower = name.toLowerCase();
|
||||
const upper = name.toUpperCase();
|
||||
|
||||
it('matches @name at start of message', () => {
|
||||
expect(TRIGGER_PATTERN.test(`@${name} hello`)).toBe(true);
|
||||
});
|
||||
|
||||
it('matches case-insensitively', () => {
|
||||
expect(TRIGGER_PATTERN.test(`@${lower} hello`)).toBe(true);
|
||||
expect(TRIGGER_PATTERN.test(`@${upper} hello`)).toBe(true);
|
||||
});
|
||||
|
||||
it('does not match when not at start of message', () => {
|
||||
expect(TRIGGER_PATTERN.test(`hello @${name}`)).toBe(false);
|
||||
});
|
||||
|
||||
it('does not match partial name like @NameExtra (word boundary)', () => {
|
||||
expect(TRIGGER_PATTERN.test(`@${name}extra hello`)).toBe(false);
|
||||
});
|
||||
|
||||
it('matches with word boundary before apostrophe', () => {
|
||||
expect(TRIGGER_PATTERN.test(`@${name}'s thing`)).toBe(true);
|
||||
});
|
||||
|
||||
it('matches @name alone (end of string is a word boundary)', () => {
|
||||
expect(TRIGGER_PATTERN.test(`@${name}`)).toBe(true);
|
||||
});
|
||||
|
||||
it('matches with leading whitespace after trim', () => {
|
||||
// The actual usage trims before testing: TRIGGER_PATTERN.test(m.content.trim())
|
||||
expect(TRIGGER_PATTERN.test(`@${name} hey`.trim())).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTriggerPattern', () => {
|
||||
it('uses the configured per-group trigger when provided', () => {
|
||||
const pattern = getTriggerPattern('@Claw');
|
||||
|
||||
expect(pattern.test('@Claw hello')).toBe(true);
|
||||
expect(pattern.test(`@${ASSISTANT_NAME} hello`)).toBe(false);
|
||||
});
|
||||
|
||||
it('falls back to the default trigger when group trigger is missing', () => {
|
||||
const pattern = getTriggerPattern(undefined);
|
||||
|
||||
expect(pattern.test(`@${ASSISTANT_NAME} hello`)).toBe(true);
|
||||
});
|
||||
|
||||
it('treats regex characters in custom triggers literally', () => {
|
||||
const pattern = getTriggerPattern('@C.L.A.U.D.E');
|
||||
|
||||
expect(pattern.test('@C.L.A.U.D.E hello')).toBe(true);
|
||||
expect(pattern.test('@CXLXAUXDXE hello')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -179,18 +232,63 @@ describe('formatOutbound', () => {
|
||||
// --- Trigger gating with requiresTrigger flag ---
|
||||
|
||||
describe('trigger gating (requiresTrigger interaction)', () => {
|
||||
// Note: TRIGGER_PATTERN is currently disabled (matches everything)
|
||||
// so all messages are processed regardless of trigger presence
|
||||
// Replicates the exact logic from processGroupMessages and startMessageLoop:
|
||||
// if (!isMainGroup && group.requiresTrigger !== false) { check group.trigger }
|
||||
function shouldRequireTrigger(
|
||||
isMainGroup: boolean,
|
||||
requiresTrigger: boolean | undefined,
|
||||
): boolean {
|
||||
return !isMainGroup && requiresTrigger !== false;
|
||||
}
|
||||
|
||||
it('all messages are processed (trigger disabled)', () => {
|
||||
function shouldProcess(
|
||||
isMainGroup: boolean,
|
||||
requiresTrigger: boolean | undefined,
|
||||
trigger: string | undefined,
|
||||
messages: NewMessage[],
|
||||
): boolean {
|
||||
if (!shouldRequireTrigger(isMainGroup, requiresTrigger)) return true;
|
||||
const triggerPattern = getTriggerPattern(trigger);
|
||||
return messages.some((m) => triggerPattern.test(m.content.trim()));
|
||||
}
|
||||
|
||||
it('main group always processes (no trigger needed)', () => {
|
||||
const msgs = [makeMsg({ content: 'hello no trigger' })];
|
||||
expect(msgs.length).toBe(1); // Sanity check
|
||||
expect(shouldProcess(true, undefined, undefined, msgs)).toBe(true);
|
||||
});
|
||||
|
||||
it('requiresTrigger=false still works', () => {
|
||||
// This test verifies the requiresTrigger flag still works when explicitly set
|
||||
// But with trigger disabled, all messages are processed anyway
|
||||
const msgs = [makeMsg({ content: 'hello' })];
|
||||
expect(msgs.length).toBe(1);
|
||||
it('main group processes even with requiresTrigger=true', () => {
|
||||
const msgs = [makeMsg({ content: 'hello no trigger' })];
|
||||
expect(shouldProcess(true, true, undefined, msgs)).toBe(true);
|
||||
});
|
||||
|
||||
it('non-main group with requiresTrigger=undefined requires trigger (defaults to true)', () => {
|
||||
const msgs = [makeMsg({ content: 'hello no trigger' })];
|
||||
expect(shouldProcess(false, undefined, undefined, msgs)).toBe(false);
|
||||
});
|
||||
|
||||
it('non-main group with requiresTrigger=true requires trigger', () => {
|
||||
const msgs = [makeMsg({ content: 'hello no trigger' })];
|
||||
expect(shouldProcess(false, true, undefined, msgs)).toBe(false);
|
||||
});
|
||||
|
||||
it('non-main group with requiresTrigger=true processes when trigger present', () => {
|
||||
const msgs = [makeMsg({ content: `@${ASSISTANT_NAME} do something` })];
|
||||
expect(shouldProcess(false, true, undefined, msgs)).toBe(true);
|
||||
});
|
||||
|
||||
it('non-main group uses its per-group trigger instead of the default trigger', () => {
|
||||
const msgs = [makeMsg({ content: '@Claw do something' })];
|
||||
expect(shouldProcess(false, true, '@Claw', msgs)).toBe(true);
|
||||
});
|
||||
|
||||
it('non-main group does not process when only the default trigger is present for a custom-trigger group', () => {
|
||||
const msgs = [makeMsg({ content: `@${ASSISTANT_NAME} do something` })];
|
||||
expect(shouldProcess(false, true, '@Claw', msgs)).toBe(false);
|
||||
});
|
||||
|
||||
it('non-main group with requiresTrigger=false always processes (no trigger needed)', () => {
|
||||
const msgs = [makeMsg({ content: 'hello no trigger' })];
|
||||
expect(shouldProcess(false, false, undefined, msgs)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
129
src/index.ts
129
src/index.ts
@@ -1,16 +1,19 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
import { OneCLI } from '@onecli-sh/sdk';
|
||||
|
||||
import {
|
||||
ASSISTANT_NAME,
|
||||
CREDENTIAL_PROXY_PORT,
|
||||
DEFAULT_TRIGGER,
|
||||
getTriggerPattern,
|
||||
GROUPS_DIR,
|
||||
IDLE_TIMEOUT,
|
||||
MAX_MESSAGES_PER_PROMPT,
|
||||
ONECLI_URL,
|
||||
POLL_INTERVAL,
|
||||
TELEGRAM_BOT_POOL,
|
||||
TIMEZONE,
|
||||
TRIGGER_PATTERN,
|
||||
} from './config.js';
|
||||
import { startCredentialProxy } from './credential-proxy.js';
|
||||
import './channels/index.js';
|
||||
import {
|
||||
getChannelFactory,
|
||||
@@ -25,13 +28,13 @@ import {
|
||||
import {
|
||||
cleanupOrphans,
|
||||
ensureContainerRuntimeRunning,
|
||||
PROXY_BIND_HOST,
|
||||
} from './container-runtime.js';
|
||||
import {
|
||||
getAllChats,
|
||||
getAllRegisteredGroups,
|
||||
getAllSessions,
|
||||
getAllTasks,
|
||||
getLastBotMessageTimestamp,
|
||||
getMessagesSince,
|
||||
getNewMessages,
|
||||
getRouterState,
|
||||
@@ -44,7 +47,6 @@ import {
|
||||
} from './db.js';
|
||||
import { GroupQueue } from './group-queue.js';
|
||||
import { resolveGroupFolderPath } from './group-folder.js';
|
||||
import { initBotPool } from './channels/telegram.js';
|
||||
import { startIpcWatcher } from './ipc.js';
|
||||
import { findChannel, formatMessages, formatOutbound } from './router.js';
|
||||
import {
|
||||
@@ -74,6 +76,27 @@ let messageLoopRunning = false;
|
||||
const channels: Channel[] = [];
|
||||
const queue = new GroupQueue();
|
||||
|
||||
const onecli = new OneCLI({ url: ONECLI_URL });
|
||||
|
||||
function ensureOneCLIAgent(jid: string, group: RegisteredGroup): void {
|
||||
if (group.isMain) return;
|
||||
const identifier = group.folder.toLowerCase().replace(/_/g, '-');
|
||||
onecli.ensureAgent({ name: group.name, identifier }).then(
|
||||
(res) => {
|
||||
logger.info(
|
||||
{ jid, identifier, created: res.created },
|
||||
'OneCLI agent ensured',
|
||||
);
|
||||
},
|
||||
(err) => {
|
||||
logger.debug(
|
||||
{ jid, identifier, err: String(err) },
|
||||
'OneCLI agent ensure skipped',
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function loadState(): void {
|
||||
lastTimestamp = getRouterState('last_timestamp') || '';
|
||||
const agentTs = getRouterState('last_agent_timestamp');
|
||||
@@ -91,6 +114,27 @@ function loadState(): void {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the message cursor for a group, recovering from the last bot reply
|
||||
* if lastAgentTimestamp is missing (new group, corrupted state, restart).
|
||||
*/
|
||||
function getOrRecoverCursor(chatJid: string): string {
|
||||
const existing = lastAgentTimestamp[chatJid];
|
||||
if (existing) return existing;
|
||||
|
||||
const botTs = getLastBotMessageTimestamp(chatJid, ASSISTANT_NAME);
|
||||
if (botTs) {
|
||||
logger.info(
|
||||
{ chatJid, recoveredFrom: botTs },
|
||||
'Recovered message cursor from last bot reply',
|
||||
);
|
||||
lastAgentTimestamp[chatJid] = botTs;
|
||||
saveState();
|
||||
return botTs;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
function saveState(): void {
|
||||
setRouterState('last_timestamp', lastTimestamp);
|
||||
setRouterState('last_agent_timestamp', JSON.stringify(lastAgentTimestamp));
|
||||
@@ -114,6 +158,29 @@ function registerGroup(jid: string, group: RegisteredGroup): void {
|
||||
// Create group folder
|
||||
fs.mkdirSync(path.join(groupDir, 'logs'), { recursive: true });
|
||||
|
||||
// Copy CLAUDE.md template into the new group folder so agents have
|
||||
// identity and instructions from the first run. (Fixes #1391)
|
||||
const groupMdFile = path.join(groupDir, 'CLAUDE.md');
|
||||
if (!fs.existsSync(groupMdFile)) {
|
||||
const templateFile = path.join(
|
||||
GROUPS_DIR,
|
||||
group.isMain ? 'main' : 'global',
|
||||
'CLAUDE.md',
|
||||
);
|
||||
if (fs.existsSync(templateFile)) {
|
||||
let content = fs.readFileSync(templateFile, 'utf-8');
|
||||
if (ASSISTANT_NAME !== 'Andy') {
|
||||
content = content.replace(/^# Andy$/m, `# ${ASSISTANT_NAME}`);
|
||||
content = content.replace(/You are Andy/g, `You are ${ASSISTANT_NAME}`);
|
||||
}
|
||||
fs.writeFileSync(groupMdFile, content);
|
||||
logger.info({ folder: group.folder }, 'Created CLAUDE.md from template');
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure a corresponding OneCLI agent exists (best-effort, non-blocking)
|
||||
ensureOneCLIAgent(jid, group);
|
||||
|
||||
logger.info(
|
||||
{ jid, name: group.name, folder: group.folder },
|
||||
'Group registered',
|
||||
@@ -161,21 +228,22 @@ async function processGroupMessages(chatJid: string): Promise<boolean> {
|
||||
|
||||
const isMainGroup = group.isMain === true;
|
||||
|
||||
const sinceTimestamp = lastAgentTimestamp[chatJid] || '';
|
||||
const missedMessages = getMessagesSince(
|
||||
chatJid,
|
||||
sinceTimestamp,
|
||||
getOrRecoverCursor(chatJid),
|
||||
ASSISTANT_NAME,
|
||||
MAX_MESSAGES_PER_PROMPT,
|
||||
);
|
||||
|
||||
if (missedMessages.length === 0) return true;
|
||||
|
||||
// Check if trigger is required and present
|
||||
if (group.requiresTrigger !== false) {
|
||||
// For non-main groups, check if trigger is required and present
|
||||
if (!isMainGroup && group.requiresTrigger !== false) {
|
||||
const triggerPattern = getTriggerPattern(group.trigger);
|
||||
const allowlistCfg = loadSenderAllowlist();
|
||||
const hasTrigger = missedMessages.some(
|
||||
(m) =>
|
||||
TRIGGER_PATTERN.test(m.content.trim()) &&
|
||||
triggerPattern.test(m.content.trim()) &&
|
||||
(m.is_from_me || isTriggerAllowed(chatJid, m.sender, allowlistCfg)),
|
||||
);
|
||||
if (!hasTrigger) return true;
|
||||
@@ -285,6 +353,7 @@ async function runAgent(
|
||||
id: t.id,
|
||||
groupFolder: t.group_folder,
|
||||
prompt: t.prompt,
|
||||
script: t.script || undefined,
|
||||
schedule_type: t.schedule_type,
|
||||
schedule_value: t.schedule_value,
|
||||
status: t.status,
|
||||
@@ -355,7 +424,7 @@ async function startMessageLoop(): Promise<void> {
|
||||
}
|
||||
messageLoopRunning = true;
|
||||
|
||||
logger.info(`NanoClaw running (trigger: @${ASSISTANT_NAME})`);
|
||||
logger.info(`NanoClaw running (default trigger: ${DEFAULT_TRIGGER})`);
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
@@ -395,16 +464,17 @@ async function startMessageLoop(): Promise<void> {
|
||||
}
|
||||
|
||||
const isMainGroup = group.isMain === true;
|
||||
const needsTrigger = group.requiresTrigger !== false;
|
||||
const needsTrigger = !isMainGroup && group.requiresTrigger !== false;
|
||||
|
||||
// For non-main groups, only act on trigger messages.
|
||||
// Non-trigger messages accumulate in DB and get pulled as
|
||||
// context when a trigger eventually arrives.
|
||||
if (needsTrigger) {
|
||||
const triggerPattern = getTriggerPattern(group.trigger);
|
||||
const allowlistCfg = loadSenderAllowlist();
|
||||
const hasTrigger = groupMessages.some(
|
||||
(m) =>
|
||||
TRIGGER_PATTERN.test(m.content.trim()) &&
|
||||
triggerPattern.test(m.content.trim()) &&
|
||||
(m.is_from_me ||
|
||||
isTriggerAllowed(chatJid, m.sender, allowlistCfg)),
|
||||
);
|
||||
@@ -415,8 +485,9 @@ async function startMessageLoop(): Promise<void> {
|
||||
// context that accumulated between triggers is included.
|
||||
const allPending = getMessagesSince(
|
||||
chatJid,
|
||||
lastAgentTimestamp[chatJid] || '',
|
||||
getOrRecoverCursor(chatJid),
|
||||
ASSISTANT_NAME,
|
||||
MAX_MESSAGES_PER_PROMPT,
|
||||
);
|
||||
const messagesToSend =
|
||||
allPending.length > 0 ? allPending : groupMessages;
|
||||
@@ -455,8 +526,12 @@ async function startMessageLoop(): Promise<void> {
|
||||
*/
|
||||
function recoverPendingMessages(): void {
|
||||
for (const [chatJid, group] of Object.entries(registeredGroups)) {
|
||||
const sinceTimestamp = lastAgentTimestamp[chatJid] || '';
|
||||
const pending = getMessagesSince(chatJid, sinceTimestamp, ASSISTANT_NAME);
|
||||
const pending = getMessagesSince(
|
||||
chatJid,
|
||||
getOrRecoverCursor(chatJid),
|
||||
ASSISTANT_NAME,
|
||||
MAX_MESSAGES_PER_PROMPT,
|
||||
);
|
||||
if (pending.length > 0) {
|
||||
logger.info(
|
||||
{ group: group.name, pendingCount: pending.length },
|
||||
@@ -477,18 +552,18 @@ async function main(): Promise<void> {
|
||||
initDatabase();
|
||||
logger.info('Database initialized');
|
||||
loadState();
|
||||
restoreRemoteControl();
|
||||
|
||||
// Start credential proxy (containers route API calls through this)
|
||||
const proxyServer = await startCredentialProxy(
|
||||
CREDENTIAL_PROXY_PORT,
|
||||
PROXY_BIND_HOST,
|
||||
);
|
||||
// Ensure OneCLI agents exist for all registered groups.
|
||||
// Recovers from missed creates (e.g. OneCLI was down at registration time).
|
||||
for (const [jid, group] of Object.entries(registeredGroups)) {
|
||||
ensureOneCLIAgent(jid, group);
|
||||
}
|
||||
|
||||
restoreRemoteControl();
|
||||
|
||||
// Graceful shutdown handlers
|
||||
const shutdown = async (signal: string) => {
|
||||
logger.info({ signal }, 'Shutdown signal received');
|
||||
proxyServer.close();
|
||||
await queue.shutdown(10000);
|
||||
for (const ch of channels) await ch.disconnect();
|
||||
process.exit(0);
|
||||
@@ -598,11 +673,6 @@ async function main(): Promise<void> {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Initialize Telegram bot pool for agent teams (swarm)
|
||||
if (TELEGRAM_BOT_POOL.length > 0) {
|
||||
await initBotPool(TELEGRAM_BOT_POOL);
|
||||
}
|
||||
|
||||
// Start subsystems (independently of connection handler)
|
||||
startSchedulerLoop({
|
||||
registeredGroups: () => registeredGroups,
|
||||
@@ -646,6 +716,7 @@ async function main(): Promise<void> {
|
||||
id: t.id,
|
||||
groupFolder: t.group_folder,
|
||||
prompt: t.prompt,
|
||||
script: t.script || undefined,
|
||||
schedule_type: t.schedule_type,
|
||||
schedule_value: t.schedule_value,
|
||||
status: t.status,
|
||||
|
||||
22
src/ipc.ts
22
src/ipc.ts
@@ -4,7 +4,7 @@ import path from 'path';
|
||||
import { CronExpressionParser } from 'cron-parser';
|
||||
|
||||
import { DATA_DIR, IPC_POLL_INTERVAL, TIMEZONE } from './config.js';
|
||||
import { sendPoolMessage } from './channels/telegram.js';
|
||||
|
||||
import { AvailableGroup } from './container-runner.js';
|
||||
import { createTask, deleteTask, getTaskById, updateTask } from './db.js';
|
||||
import { isValidGroupFolder } from './group-folder.js';
|
||||
@@ -82,16 +82,7 @@ export function startIpcWatcher(deps: IpcDeps): void {
|
||||
isMain ||
|
||||
(targetGroup && targetGroup.folder === sourceGroup)
|
||||
) {
|
||||
if (data.sender && data.chatJid.startsWith('tg:')) {
|
||||
await sendPoolMessage(
|
||||
data.chatJid,
|
||||
data.text,
|
||||
data.sender,
|
||||
sourceGroup,
|
||||
);
|
||||
} else {
|
||||
await deps.sendMessage(data.chatJid, data.text);
|
||||
}
|
||||
await deps.sendMessage(data.chatJid, data.text);
|
||||
logger.info(
|
||||
{ chatJid: data.chatJid, sourceGroup, sender: data.sender },
|
||||
'IPC message sent',
|
||||
@@ -172,6 +163,7 @@ export async function processTaskIpc(
|
||||
schedule_type?: string;
|
||||
schedule_value?: string;
|
||||
context_mode?: string;
|
||||
script?: string;
|
||||
groupFolder?: string;
|
||||
chatJid?: string;
|
||||
targetJid?: string;
|
||||
@@ -270,6 +262,7 @@ export async function processTaskIpc(
|
||||
group_folder: targetFolder,
|
||||
chat_jid: targetJid,
|
||||
prompt: data.prompt,
|
||||
script: data.script || null,
|
||||
schedule_type: scheduleType,
|
||||
schedule_value: data.schedule_value,
|
||||
context_mode: contextMode,
|
||||
@@ -362,6 +355,7 @@ export async function processTaskIpc(
|
||||
|
||||
const updates: Parameters<typeof updateTask>[1] = {};
|
||||
if (data.prompt !== undefined) updates.prompt = data.prompt;
|
||||
if (data.script !== undefined) updates.script = data.script || null;
|
||||
if (data.schedule_type !== undefined)
|
||||
updates.schedule_type = data.schedule_type as
|
||||
| 'cron'
|
||||
@@ -448,7 +442,10 @@ export async function processTaskIpc(
|
||||
);
|
||||
break;
|
||||
}
|
||||
// Defense in depth: agent cannot set isMain via IPC
|
||||
// Defense in depth: agent cannot set isMain via IPC.
|
||||
// Preserve isMain from the existing registration so IPC config
|
||||
// updates (e.g. adding additionalMounts) don't strip the flag.
|
||||
const existingGroup = registeredGroups[data.jid];
|
||||
deps.registerGroup(data.jid, {
|
||||
name: data.name,
|
||||
folder: data.folder,
|
||||
@@ -456,6 +453,7 @@ export async function processTaskIpc(
|
||||
added_at: new Date().toISOString(),
|
||||
containerConfig: data.containerConfig,
|
||||
requiresTrigger: data.requiresTrigger,
|
||||
isMain: existingGroup?.isMain,
|
||||
});
|
||||
} else {
|
||||
logger.warn(
|
||||
|
||||
@@ -1,11 +1,78 @@
|
||||
import pino from 'pino';
|
||||
const LEVELS = { debug: 20, info: 30, warn: 40, error: 50, fatal: 60 } as const;
|
||||
type Level = keyof typeof LEVELS;
|
||||
|
||||
export const logger = pino({
|
||||
level: process.env.LOG_LEVEL || 'info',
|
||||
transport: { target: 'pino-pretty', options: { colorize: true } },
|
||||
});
|
||||
const COLORS: Record<Level, string> = {
|
||||
debug: '\x1b[34m',
|
||||
info: '\x1b[32m',
|
||||
warn: '\x1b[33m',
|
||||
error: '\x1b[31m',
|
||||
fatal: '\x1b[41m\x1b[37m',
|
||||
};
|
||||
const KEY_COLOR = '\x1b[35m';
|
||||
const MSG_COLOR = '\x1b[36m';
|
||||
const RESET = '\x1b[39m';
|
||||
const FULL_RESET = '\x1b[0m';
|
||||
|
||||
// Route uncaught errors through pino so they get timestamps in stderr
|
||||
const threshold =
|
||||
LEVELS[(process.env.LOG_LEVEL as Level) || 'info'] ?? LEVELS.info;
|
||||
|
||||
function formatErr(err: unknown): string {
|
||||
if (err instanceof Error) {
|
||||
return `{\n "type": "${err.constructor.name}",\n "message": "${err.message}",\n "stack":\n ${err.stack}\n }`;
|
||||
}
|
||||
return JSON.stringify(err);
|
||||
}
|
||||
|
||||
function formatData(data: Record<string, unknown>): string {
|
||||
let out = '';
|
||||
for (const [k, v] of Object.entries(data)) {
|
||||
if (k === 'err') {
|
||||
out += `\n ${KEY_COLOR}err${RESET}: ${formatErr(v)}`;
|
||||
} else {
|
||||
out += `\n ${KEY_COLOR}${k}${RESET}: ${JSON.stringify(v)}`;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function ts(): string {
|
||||
const d = new Date();
|
||||
return `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}:${String(d.getSeconds()).padStart(2, '0')}.${String(d.getMilliseconds()).padStart(3, '0')}`;
|
||||
}
|
||||
|
||||
function log(
|
||||
level: Level,
|
||||
dataOrMsg: Record<string, unknown> | string,
|
||||
msg?: string,
|
||||
): void {
|
||||
if (LEVELS[level] < threshold) return;
|
||||
const tag = `${COLORS[level]}${level.toUpperCase()}${level === 'fatal' ? FULL_RESET : RESET}`;
|
||||
const stream = LEVELS[level] >= LEVELS.warn ? process.stderr : process.stdout;
|
||||
if (typeof dataOrMsg === 'string') {
|
||||
stream.write(
|
||||
`[${ts()}] ${tag} (${process.pid}): ${MSG_COLOR}${dataOrMsg}${RESET}\n`,
|
||||
);
|
||||
} else {
|
||||
stream.write(
|
||||
`[${ts()}] ${tag} (${process.pid}): ${MSG_COLOR}${msg}${RESET}${formatData(dataOrMsg)}\n`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export const logger = {
|
||||
debug: (dataOrMsg: Record<string, unknown> | string, msg?: string) =>
|
||||
log('debug', dataOrMsg, msg),
|
||||
info: (dataOrMsg: Record<string, unknown> | string, msg?: string) =>
|
||||
log('info', dataOrMsg, msg),
|
||||
warn: (dataOrMsg: Record<string, unknown> | string, msg?: string) =>
|
||||
log('warn', dataOrMsg, msg),
|
||||
error: (dataOrMsg: Record<string, unknown> | string, msg?: string) =>
|
||||
log('error', dataOrMsg, msg),
|
||||
fatal: (dataOrMsg: Record<string, unknown> | string, msg?: string) =>
|
||||
log('fatal', dataOrMsg, msg),
|
||||
};
|
||||
|
||||
// Route uncaught errors through logger so they get timestamps in stderr
|
||||
process.on('uncaughtException', (err) => {
|
||||
logger.fatal({ err }, 'Uncaught exception');
|
||||
process.exit(1);
|
||||
|
||||
@@ -9,16 +9,10 @@
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import pino from 'pino';
|
||||
|
||||
import { MOUNT_ALLOWLIST_PATH } from './config.js';
|
||||
import { logger } from './logger.js';
|
||||
import { AdditionalMount, AllowedRoot, MountAllowlist } from './types.js';
|
||||
|
||||
const logger = pino({
|
||||
level: process.env.LOG_LEVEL || 'info',
|
||||
transport: { target: 'pino-pretty', options: { colorize: true } },
|
||||
});
|
||||
|
||||
// Cache the allowlist in memory - only reloads on process restart
|
||||
let cachedAllowlist: MountAllowlist | null = null;
|
||||
let allowlistLoadError: string | null = null;
|
||||
@@ -63,7 +57,8 @@ export function loadMountAllowlist(): MountAllowlist | null {
|
||||
|
||||
try {
|
||||
if (!fs.existsSync(MOUNT_ALLOWLIST_PATH)) {
|
||||
allowlistLoadError = `Mount allowlist not found at ${MOUNT_ALLOWLIST_PATH}`;
|
||||
// Do NOT cache this as an error — file may be created later without restart.
|
||||
// Only parse/structural errors are permanently cached.
|
||||
logger.warn(
|
||||
{ path: MOUNT_ALLOWLIST_PATH },
|
||||
'Mount allowlist not found - additional mounts will be BLOCKED. ' +
|
||||
@@ -215,6 +210,11 @@ function isValidContainerPath(containerPath: string): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Must not contain colons — prevents Docker -v option injection (e.g., "repo:rw")
|
||||
if (containerPath.includes(':')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -139,6 +139,7 @@ async function runTask(
|
||||
id: t.id,
|
||||
groupFolder: t.group_folder,
|
||||
prompt: t.prompt,
|
||||
script: t.script,
|
||||
schedule_type: t.schedule_type,
|
||||
schedule_value: t.schedule_value,
|
||||
status: t.status,
|
||||
@@ -179,6 +180,7 @@ async function runTask(
|
||||
isMain,
|
||||
isScheduledTask: true,
|
||||
assistantName: ASSISTANT_NAME,
|
||||
script: task.script || undefined,
|
||||
},
|
||||
(proc, containerName) =>
|
||||
deps.onProcess(task.chat_jid, proc, containerName, task.group_folder),
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
import { formatLocalTime } from './timezone.js';
|
||||
import {
|
||||
formatLocalTime,
|
||||
isValidTimezone,
|
||||
resolveTimezone,
|
||||
} from './timezone.js';
|
||||
|
||||
// --- formatLocalTime ---
|
||||
|
||||
@@ -26,4 +30,44 @@ describe('formatLocalTime', () => {
|
||||
expect(ny).toContain('8:00');
|
||||
expect(tokyo).toContain('9:00');
|
||||
});
|
||||
|
||||
it('does not throw on invalid timezone, falls back to UTC', () => {
|
||||
expect(() =>
|
||||
formatLocalTime('2026-01-01T00:00:00.000Z', 'IST-2'),
|
||||
).not.toThrow();
|
||||
const result = formatLocalTime('2026-01-01T12:00:00.000Z', 'IST-2');
|
||||
// Should format as UTC (noon UTC = 12:00 PM)
|
||||
expect(result).toContain('12:00');
|
||||
expect(result).toContain('PM');
|
||||
});
|
||||
});
|
||||
|
||||
describe('isValidTimezone', () => {
|
||||
it('accepts valid IANA identifiers', () => {
|
||||
expect(isValidTimezone('America/New_York')).toBe(true);
|
||||
expect(isValidTimezone('UTC')).toBe(true);
|
||||
expect(isValidTimezone('Asia/Tokyo')).toBe(true);
|
||||
expect(isValidTimezone('Asia/Jerusalem')).toBe(true);
|
||||
});
|
||||
|
||||
it('rejects invalid timezone strings', () => {
|
||||
expect(isValidTimezone('IST-2')).toBe(false);
|
||||
expect(isValidTimezone('XYZ+3')).toBe(false);
|
||||
});
|
||||
|
||||
it('rejects empty and garbage strings', () => {
|
||||
expect(isValidTimezone('')).toBe(false);
|
||||
expect(isValidTimezone('NotATimezone')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('resolveTimezone', () => {
|
||||
it('returns the timezone if valid', () => {
|
||||
expect(resolveTimezone('America/New_York')).toBe('America/New_York');
|
||||
});
|
||||
|
||||
it('falls back to UTC for invalid timezone', () => {
|
||||
expect(resolveTimezone('IST-2')).toBe('UTC');
|
||||
expect(resolveTimezone('')).toBe('UTC');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,11 +1,32 @@
|
||||
/**
|
||||
* Check whether a timezone string is a valid IANA identifier
|
||||
* that Intl.DateTimeFormat can use.
|
||||
*/
|
||||
export function isValidTimezone(tz: string): boolean {
|
||||
try {
|
||||
Intl.DateTimeFormat(undefined, { timeZone: tz });
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the given timezone if valid IANA, otherwise fall back to UTC.
|
||||
*/
|
||||
export function resolveTimezone(tz: string): string {
|
||||
return isValidTimezone(tz) ? tz : 'UTC';
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a UTC ISO timestamp to a localized display string.
|
||||
* Uses the Intl API (no external dependencies).
|
||||
* Falls back to UTC if the timezone is invalid.
|
||||
*/
|
||||
export function formatLocalTime(utcIso: string, timezone: string): string {
|
||||
const date = new Date(utcIso);
|
||||
return date.toLocaleString('en-US', {
|
||||
timeZone: timezone,
|
||||
timeZone: resolveTimezone(timezone),
|
||||
year: 'numeric',
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
|
||||
@@ -58,6 +58,7 @@ export interface ScheduledTask {
|
||||
group_folder: string;
|
||||
chat_jid: string;
|
||||
prompt: string;
|
||||
script?: string | null;
|
||||
schedule_type: 'cron' | 'interval' | 'once';
|
||||
schedule_value: string;
|
||||
context_mode: 'group' | 'isolated';
|
||||
|
||||
Reference in New Issue
Block a user