Merge origin/main into chore/nanoclaw-recursive-room
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Cross-platform detection utilities for NanoClaw setup.
|
||||
* Cross-platform detection utilities for EJClaw setup.
|
||||
*/
|
||||
import { execSync } from 'child_process';
|
||||
import fs from 'fs';
|
||||
|
||||
@@ -12,14 +12,18 @@ import Database from 'better-sqlite3';
|
||||
function createTestDb(): Database.Database {
|
||||
const db = new Database(':memory:');
|
||||
db.exec(`CREATE TABLE IF NOT EXISTS registered_groups (
|
||||
jid TEXT PRIMARY KEY,
|
||||
jid TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
folder TEXT NOT NULL UNIQUE,
|
||||
folder TEXT NOT NULL,
|
||||
trigger_pattern TEXT NOT NULL,
|
||||
added_at TEXT NOT NULL,
|
||||
agent_config TEXT,
|
||||
requires_trigger INTEGER DEFAULT 1,
|
||||
is_main INTEGER DEFAULT 0
|
||||
is_main INTEGER DEFAULT 0,
|
||||
agent_type TEXT NOT NULL DEFAULT 'claude-code',
|
||||
work_dir TEXT,
|
||||
PRIMARY KEY (jid, agent_type),
|
||||
UNIQUE (folder, agent_type)
|
||||
)`);
|
||||
return db;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ function generatePlist(
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>com.nanoclaw</string>
|
||||
<string>com.ejclaw</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>${nodePath}</string>
|
||||
@@ -39,9 +39,9 @@ function generatePlist(
|
||||
<string>${homeDir}</string>
|
||||
</dict>
|
||||
<key>StandardOutPath</key>
|
||||
<string>${projectRoot}/logs/nanoclaw.log</string>
|
||||
<string>${projectRoot}/logs/ejclaw.log</string>
|
||||
<key>StandardErrorPath</key>
|
||||
<string>${projectRoot}/logs/nanoclaw.error.log</string>
|
||||
<string>${projectRoot}/logs/ejclaw.error.log</string>
|
||||
</dict>
|
||||
</plist>`;
|
||||
}
|
||||
@@ -53,7 +53,7 @@ function generateSystemdUnit(
|
||||
isSystem: boolean,
|
||||
): string {
|
||||
return `[Unit]
|
||||
Description=NanoClaw Personal Assistant
|
||||
Description=EJClaw Personal Assistant
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
@@ -64,8 +64,8 @@ Restart=always
|
||||
RestartSec=5
|
||||
Environment=HOME=${homeDir}
|
||||
Environment=PATH=/usr/local/bin:/usr/bin:/bin:${homeDir}/.local/bin
|
||||
StandardOutput=append:${projectRoot}/logs/nanoclaw.log
|
||||
StandardError=append:${projectRoot}/logs/nanoclaw.error.log
|
||||
StandardOutput=append:${projectRoot}/logs/ejclaw.log
|
||||
StandardError=append:${projectRoot}/logs/ejclaw.error.log
|
||||
|
||||
[Install]
|
||||
WantedBy=${isSystem ? 'multi-user.target' : 'default.target'}`;
|
||||
@@ -75,16 +75,16 @@ describe('plist generation', () => {
|
||||
it('contains the correct label', () => {
|
||||
const plist = generatePlist(
|
||||
'/usr/local/bin/node',
|
||||
'/home/user/nanoclaw',
|
||||
'/home/user/ejclaw',
|
||||
'/home/user',
|
||||
);
|
||||
expect(plist).toContain('<string>com.nanoclaw</string>');
|
||||
expect(plist).toContain('<string>com.ejclaw</string>');
|
||||
});
|
||||
|
||||
it('uses the correct node path', () => {
|
||||
const plist = generatePlist(
|
||||
'/opt/node/bin/node',
|
||||
'/home/user/nanoclaw',
|
||||
'/home/user/ejclaw',
|
||||
'/home/user',
|
||||
);
|
||||
expect(plist).toContain('<string>/opt/node/bin/node</string>');
|
||||
@@ -93,20 +93,20 @@ describe('plist generation', () => {
|
||||
it('points to dist/index.js', () => {
|
||||
const plist = generatePlist(
|
||||
'/usr/local/bin/node',
|
||||
'/home/user/nanoclaw',
|
||||
'/home/user/ejclaw',
|
||||
'/home/user',
|
||||
);
|
||||
expect(plist).toContain('/home/user/nanoclaw/dist/index.js');
|
||||
expect(plist).toContain('/home/user/ejclaw/dist/index.js');
|
||||
});
|
||||
|
||||
it('sets log paths', () => {
|
||||
const plist = generatePlist(
|
||||
'/usr/local/bin/node',
|
||||
'/home/user/nanoclaw',
|
||||
'/home/user/ejclaw',
|
||||
'/home/user',
|
||||
);
|
||||
expect(plist).toContain('nanoclaw.log');
|
||||
expect(plist).toContain('nanoclaw.error.log');
|
||||
expect(plist).toContain('ejclaw.log');
|
||||
expect(plist).toContain('ejclaw.error.log');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -114,7 +114,7 @@ describe('systemd unit generation', () => {
|
||||
it('user unit uses default.target', () => {
|
||||
const unit = generateSystemdUnit(
|
||||
'/usr/bin/node',
|
||||
'/home/user/nanoclaw',
|
||||
'/home/user/ejclaw',
|
||||
'/home/user',
|
||||
false,
|
||||
);
|
||||
@@ -124,7 +124,7 @@ describe('systemd unit generation', () => {
|
||||
it('system unit uses multi-user.target', () => {
|
||||
const unit = generateSystemdUnit(
|
||||
'/usr/bin/node',
|
||||
'/home/user/nanoclaw',
|
||||
'/home/user/ejclaw',
|
||||
'/home/user',
|
||||
true,
|
||||
);
|
||||
@@ -134,7 +134,7 @@ describe('systemd unit generation', () => {
|
||||
it('contains restart policy', () => {
|
||||
const unit = generateSystemdUnit(
|
||||
'/usr/bin/node',
|
||||
'/home/user/nanoclaw',
|
||||
'/home/user/ejclaw',
|
||||
'/home/user',
|
||||
false,
|
||||
);
|
||||
@@ -145,32 +145,32 @@ describe('systemd unit generation', () => {
|
||||
it('sets correct ExecStart', () => {
|
||||
const unit = generateSystemdUnit(
|
||||
'/usr/bin/node',
|
||||
'/srv/nanoclaw',
|
||||
'/srv/ejclaw',
|
||||
'/home/user',
|
||||
false,
|
||||
);
|
||||
expect(unit).toContain(
|
||||
'ExecStart=/usr/bin/node /srv/nanoclaw/dist/index.js',
|
||||
'ExecStart=/usr/bin/node /srv/ejclaw/dist/index.js',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('WSL nohup fallback', () => {
|
||||
it('generates a valid wrapper script', () => {
|
||||
const projectRoot = '/home/user/nanoclaw';
|
||||
const projectRoot = '/home/user/ejclaw';
|
||||
const nodePath = '/usr/bin/node';
|
||||
const pidFile = path.join(projectRoot, 'nanoclaw.pid');
|
||||
const pidFile = path.join(projectRoot, 'ejclaw.pid');
|
||||
|
||||
// Simulate what service.ts generates
|
||||
const wrapper = `#!/bin/bash
|
||||
set -euo pipefail
|
||||
cd ${JSON.stringify(projectRoot)}
|
||||
nohup ${JSON.stringify(nodePath)} ${JSON.stringify(projectRoot)}/dist/index.js >> ${JSON.stringify(projectRoot)}/logs/nanoclaw.log 2>> ${JSON.stringify(projectRoot)}/logs/nanoclaw.error.log &
|
||||
nohup ${JSON.stringify(nodePath)} ${JSON.stringify(projectRoot)}/dist/index.js >> ${JSON.stringify(projectRoot)}/logs/ejclaw.log 2>> ${JSON.stringify(projectRoot)}/logs/ejclaw.error.log &
|
||||
echo $! > ${JSON.stringify(pidFile)}`;
|
||||
|
||||
expect(wrapper).toContain('#!/bin/bash');
|
||||
expect(wrapper).toContain('nohup');
|
||||
expect(wrapper).toContain(nodePath);
|
||||
expect(wrapper).toContain('nanoclaw.pid');
|
||||
expect(wrapper).toContain('ejclaw.pid');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,6 +20,9 @@ import {
|
||||
} from './platform.js';
|
||||
import { emitStatus } from './status.js';
|
||||
|
||||
const EJCLAW_SERVICE_NAME = 'ejclaw';
|
||||
const EJCLAW_LAUNCHD_LABEL = 'com.ejclaw';
|
||||
|
||||
export async function run(_args: string[]): Promise<void> {
|
||||
const projectRoot = process.cwd();
|
||||
const platform = getPlatform();
|
||||
@@ -77,7 +80,7 @@ function setupLaunchd(
|
||||
homeDir,
|
||||
'Library',
|
||||
'LaunchAgents',
|
||||
'com.nanoclaw.plist',
|
||||
`${EJCLAW_LAUNCHD_LABEL}.plist`,
|
||||
);
|
||||
fs.mkdirSync(path.dirname(plistPath), { recursive: true });
|
||||
|
||||
@@ -86,7 +89,7 @@ function setupLaunchd(
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>com.nanoclaw</string>
|
||||
<string>${EJCLAW_LAUNCHD_LABEL}</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>${nodePath}</string>
|
||||
@@ -106,9 +109,9 @@ function setupLaunchd(
|
||||
<string>${homeDir}</string>
|
||||
</dict>
|
||||
<key>StandardOutPath</key>
|
||||
<string>${projectRoot}/logs/nanoclaw.log</string>
|
||||
<string>${projectRoot}/logs/ejclaw.log</string>
|
||||
<key>StandardErrorPath</key>
|
||||
<string>${projectRoot}/logs/nanoclaw.error.log</string>
|
||||
<string>${projectRoot}/logs/ejclaw.error.log</string>
|
||||
</dict>
|
||||
</plist>`;
|
||||
|
||||
@@ -128,7 +131,7 @@ function setupLaunchd(
|
||||
let serviceLoaded = false;
|
||||
try {
|
||||
const output = execSync('launchctl list', { encoding: 'utf-8' });
|
||||
serviceLoaded = output.includes('com.nanoclaw');
|
||||
serviceLoaded = output.includes(EJCLAW_LAUNCHD_LABEL);
|
||||
} catch {
|
||||
// launchctl list failed
|
||||
}
|
||||
@@ -160,7 +163,7 @@ function setupLinux(
|
||||
}
|
||||
|
||||
/**
|
||||
* Kill any orphaned nanoclaw node processes left from previous runs or debugging.
|
||||
* Kill any orphaned ejclaw node processes left from previous runs or debugging.
|
||||
* Prevents connection conflicts when two instances connect to the same channel simultaneously.
|
||||
*/
|
||||
function killOrphanedProcesses(projectRoot: string): void {
|
||||
@@ -168,7 +171,7 @@ function killOrphanedProcesses(projectRoot: string): void {
|
||||
execSync(`pkill -f '${projectRoot}/dist/index\\.js' || true`, {
|
||||
stdio: 'ignore',
|
||||
});
|
||||
logger.info('Stopped any orphaned nanoclaw processes');
|
||||
logger.info('Stopped any orphaned ejclaw processes');
|
||||
} catch {
|
||||
// pkill not available or no orphans
|
||||
}
|
||||
@@ -186,7 +189,7 @@ function setupSystemd(
|
||||
let systemctlPrefix: string;
|
||||
|
||||
if (runningAsRoot) {
|
||||
unitPath = '/etc/systemd/system/nanoclaw.service';
|
||||
unitPath = `/etc/systemd/system/${EJCLAW_SERVICE_NAME}.service`;
|
||||
systemctlPrefix = 'systemctl';
|
||||
logger.info('Running as root — installing system-level systemd unit');
|
||||
} else {
|
||||
@@ -202,12 +205,12 @@ function setupSystemd(
|
||||
}
|
||||
const unitDir = path.join(homeDir, '.config', 'systemd', 'user');
|
||||
fs.mkdirSync(unitDir, { recursive: true });
|
||||
unitPath = path.join(unitDir, 'nanoclaw.service');
|
||||
unitPath = path.join(unitDir, `${EJCLAW_SERVICE_NAME}.service`);
|
||||
systemctlPrefix = 'systemctl --user';
|
||||
}
|
||||
|
||||
const unit = `[Unit]
|
||||
Description=NanoClaw Personal Assistant
|
||||
Description=EJClaw Personal Assistant
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
@@ -218,8 +221,8 @@ Restart=always
|
||||
RestartSec=5
|
||||
Environment=HOME=${homeDir}
|
||||
Environment=PATH=${path.dirname(nodePath)}:/usr/local/bin:/usr/bin:/bin:${homeDir}/.local/bin:${homeDir}/.npm-global/bin
|
||||
StandardOutput=append:${projectRoot}/logs/nanoclaw.log
|
||||
StandardError=append:${projectRoot}/logs/nanoclaw.error.log
|
||||
StandardOutput=append:${projectRoot}/logs/ejclaw.log
|
||||
StandardError=append:${projectRoot}/logs/ejclaw.error.log
|
||||
|
||||
[Install]
|
||||
WantedBy=${runningAsRoot ? 'multi-user.target' : 'default.target'}`;
|
||||
@@ -227,7 +230,7 @@ WantedBy=${runningAsRoot ? 'multi-user.target' : 'default.target'}`;
|
||||
fs.writeFileSync(unitPath, unit);
|
||||
logger.info({ unitPath }, 'Wrote systemd unit');
|
||||
|
||||
// Kill orphaned nanoclaw processes to avoid channel connection conflicts
|
||||
// Kill orphaned ejclaw processes to avoid channel connection conflicts
|
||||
killOrphanedProcesses(projectRoot);
|
||||
|
||||
// Enable and start
|
||||
@@ -238,13 +241,17 @@ WantedBy=${runningAsRoot ? 'multi-user.target' : 'default.target'}`;
|
||||
}
|
||||
|
||||
try {
|
||||
execSync(`${systemctlPrefix} enable nanoclaw`, { stdio: 'ignore' });
|
||||
execSync(`${systemctlPrefix} enable ${EJCLAW_SERVICE_NAME}`, {
|
||||
stdio: 'ignore',
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error({ err }, 'systemctl enable failed');
|
||||
}
|
||||
|
||||
try {
|
||||
execSync(`${systemctlPrefix} start nanoclaw`, { stdio: 'ignore' });
|
||||
execSync(`${systemctlPrefix} start ${EJCLAW_SERVICE_NAME}`, {
|
||||
stdio: 'ignore',
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error({ err }, 'systemctl start failed');
|
||||
}
|
||||
@@ -252,7 +259,9 @@ WantedBy=${runningAsRoot ? 'multi-user.target' : 'default.target'}`;
|
||||
// Verify
|
||||
let serviceLoaded = false;
|
||||
try {
|
||||
execSync(`${systemctlPrefix} is-active nanoclaw`, { stdio: 'ignore' });
|
||||
execSync(`${systemctlPrefix} is-active ${EJCLAW_SERVICE_NAME}`, {
|
||||
stdio: 'ignore',
|
||||
});
|
||||
serviceLoaded = true;
|
||||
} catch {
|
||||
// Not active
|
||||
@@ -276,12 +285,12 @@ function setupNohupFallback(
|
||||
): void {
|
||||
logger.warn('No systemd detected — generating nohup wrapper script');
|
||||
|
||||
const wrapperPath = path.join(projectRoot, 'start-nanoclaw.sh');
|
||||
const pidFile = path.join(projectRoot, 'nanoclaw.pid');
|
||||
const wrapperPath = path.join(projectRoot, 'start-ejclaw.sh');
|
||||
const pidFile = path.join(projectRoot, 'ejclaw.pid');
|
||||
|
||||
const lines = [
|
||||
'#!/bin/bash',
|
||||
'# start-nanoclaw.sh — Start NanoClaw without systemd',
|
||||
'# start-ejclaw.sh — Start EJClaw without systemd',
|
||||
`# To stop: kill \\$(cat ${pidFile})`,
|
||||
'',
|
||||
'set -euo pipefail',
|
||||
@@ -292,20 +301,20 @@ function setupNohupFallback(
|
||||
`if [ -f ${JSON.stringify(pidFile)} ]; then`,
|
||||
` OLD_PID=$(cat ${JSON.stringify(pidFile)} 2>/dev/null || echo "")`,
|
||||
' if [ -n "$OLD_PID" ] && kill -0 "$OLD_PID" 2>/dev/null; then',
|
||||
' echo "Stopping existing NanoClaw (PID $OLD_PID)..."',
|
||||
' echo "Stopping existing EJClaw (PID $OLD_PID)..."',
|
||||
' kill "$OLD_PID" 2>/dev/null || true',
|
||||
' sleep 2',
|
||||
' fi',
|
||||
'fi',
|
||||
'',
|
||||
'echo "Starting NanoClaw..."',
|
||||
'echo "Starting EJClaw..."',
|
||||
`nohup ${JSON.stringify(nodePath)} ${JSON.stringify(projectRoot + '/dist/index.js')} \\`,
|
||||
` >> ${JSON.stringify(projectRoot + '/logs/nanoclaw.log')} \\`,
|
||||
` 2>> ${JSON.stringify(projectRoot + '/logs/nanoclaw.error.log')} &`,
|
||||
` >> ${JSON.stringify(projectRoot + '/logs/ejclaw.log')} \\`,
|
||||
` 2>> ${JSON.stringify(projectRoot + '/logs/ejclaw.error.log')} &`,
|
||||
'',
|
||||
`echo $! > ${JSON.stringify(pidFile)}`,
|
||||
'echo "NanoClaw started (PID $!)"',
|
||||
`echo "Logs: tail -f ${projectRoot}/logs/nanoclaw.log"`,
|
||||
'echo "EJClaw started (PID $!)"',
|
||||
`echo "Logs: tail -f ${projectRoot}/logs/ejclaw.log"`,
|
||||
];
|
||||
const wrapper = lines.join('\n') + '\n';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user