This commit is contained in:
126
packages/launcher/src/cli-core.ts
Normal file
126
packages/launcher/src/cli-core.ts
Normal file
@@ -0,0 +1,126 @@
|
||||
export interface LauncherArgs {
|
||||
port: number;
|
||||
skipLiteLLM: boolean;
|
||||
noBrowser: boolean;
|
||||
help: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface StartupSuccessOptions {
|
||||
url: string;
|
||||
llmProvider: string;
|
||||
llmHealth: string;
|
||||
dataDir: string;
|
||||
noBrowser: boolean;
|
||||
}
|
||||
|
||||
const DEFAULT_PORT = 3333;
|
||||
const MAX_PORT = 65535;
|
||||
|
||||
export function parseArgs(argv: string[]): LauncherArgs {
|
||||
const args = argv.slice(2);
|
||||
const result: LauncherArgs = {
|
||||
port: DEFAULT_PORT,
|
||||
skipLiteLLM: false,
|
||||
noBrowser: false,
|
||||
help: false,
|
||||
};
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const arg = args[i];
|
||||
|
||||
if (arg === '--port' || arg === '-p') {
|
||||
const value = args[++i];
|
||||
if (value === undefined) {
|
||||
result.error = 'Missing value for --port. Use a number between 1 and 65535.';
|
||||
return result;
|
||||
}
|
||||
|
||||
const port = Number(value);
|
||||
if (!Number.isInteger(port) || port < 1 || port > MAX_PORT) {
|
||||
result.error = `Invalid port: ${value}. Use a number between 1 and 65535.`;
|
||||
return result;
|
||||
}
|
||||
|
||||
result.port = port;
|
||||
} else if (arg === '--skip-litellm') {
|
||||
result.skipLiteLLM = true;
|
||||
} else if (arg === '--no-open') {
|
||||
result.noBrowser = true;
|
||||
} else if (arg === '--help' || arg === '-h') {
|
||||
result.help = true;
|
||||
} else if (arg.startsWith('-')) {
|
||||
result.error = `Unknown option: ${arg}`;
|
||||
return result;
|
||||
} else {
|
||||
result.error = `Unknown argument: ${arg}`;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function formatHelp(): string {
|
||||
return `
|
||||
Waggle - Your personal AI agent swarm
|
||||
|
||||
Usage:
|
||||
npx waggle [options]
|
||||
|
||||
Options:
|
||||
--port, -p <number> Server port (default: 3333)
|
||||
--skip-litellm Use built-in Anthropic proxy instead of LiteLLM
|
||||
--no-open Do not open browser automatically
|
||||
--help, -h Show this help message
|
||||
|
||||
Data directory: ~/.waggle/
|
||||
Config: ~/.waggle/config.json
|
||||
`;
|
||||
}
|
||||
|
||||
export function formatStartupFailure(message: string, port: number): string {
|
||||
const firstLine = message.split(/\r?\n/)[0] || message;
|
||||
const lines = [
|
||||
'',
|
||||
` Failed to start Waggle: ${firstLine}`,
|
||||
'',
|
||||
];
|
||||
|
||||
if (/port\s+\d+\s+is already in use/i.test(firstLine)) {
|
||||
lines.push(
|
||||
` Another app is already using port ${port}.`,
|
||||
` Try: npx waggle --port ${port + 1}`,
|
||||
' Or close the other Waggle instance and run the command again.',
|
||||
'',
|
||||
);
|
||||
} else {
|
||||
lines.push(
|
||||
' Check the message above, fix the startup problem, and run the command again.',
|
||||
'',
|
||||
);
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
export function formatStartupSuccess(options: StartupSuccessOptions): string {
|
||||
const lines = [
|
||||
'',
|
||||
` Server: ${options.url}`,
|
||||
` LLM: ${options.llmProvider} (${options.llmHealth})`,
|
||||
` Data: ${options.dataDir}`,
|
||||
];
|
||||
|
||||
if (options.noBrowser) {
|
||||
lines.push(
|
||||
' Browser: not opened (--no-open)',
|
||||
` Open manually: ${options.url}`,
|
||||
);
|
||||
} else {
|
||||
lines.push(' Browser: opening default browser');
|
||||
}
|
||||
|
||||
lines.push('', ' Press Ctrl+C to stop', '');
|
||||
return lines.join('\n');
|
||||
}
|
||||
117
packages/launcher/src/cli.ts
Normal file
117
packages/launcher/src/cli.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* Waggle CLI Launcher - `npx waggle`
|
||||
*
|
||||
* Starts the Waggle server and opens the frontend in the default browser.
|
||||
*
|
||||
* Usage:
|
||||
* npx waggle # Start on default port 3333
|
||||
* npx waggle --port 4000 # Start on custom port
|
||||
* npx waggle --skip-litellm # Skip LiteLLM proxy
|
||||
* npx waggle --no-open # Do not open browser automatically
|
||||
*/
|
||||
|
||||
import { execFile } from 'node:child_process';
|
||||
import os from 'node:os';
|
||||
import {
|
||||
formatHelp,
|
||||
formatStartupFailure,
|
||||
formatStartupSuccess,
|
||||
parseArgs,
|
||||
} from './cli-core.js';
|
||||
|
||||
function openBrowser(url: string): void {
|
||||
const platform = os.platform();
|
||||
const onOpenError = (err: Error | null): void => {
|
||||
if (err) console.log(` Open manually: ${url}`);
|
||||
};
|
||||
|
||||
if (platform === 'win32') {
|
||||
execFile('cmd', ['/c', 'start', '', url], onOpenError);
|
||||
} else if (platform === 'darwin') {
|
||||
execFile('open', [url], onOpenError);
|
||||
} else {
|
||||
execFile('xdg-open', [url], onOpenError);
|
||||
}
|
||||
}
|
||||
|
||||
function checkNodeVersion(): boolean {
|
||||
const [major] = process.versions.node.split('.').map(Number);
|
||||
if (major < 18) {
|
||||
console.error(`\n Waggle requires Node.js >= 18. You have ${process.versions.node}.`);
|
||||
console.error(' Install a newer version: https://nodejs.org\n');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const { port, skipLiteLLM, noBrowser, help, error } = parseArgs(process.argv);
|
||||
|
||||
if (help) {
|
||||
console.log(formatHelp());
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
console.error();
|
||||
console.error(` ${error}`);
|
||||
console.error(formatHelp());
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (!checkNodeVersion()) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log();
|
||||
console.log(' Waggle - AI Agent Swarm');
|
||||
console.log(' ------------------------');
|
||||
|
||||
const { startService, isFirstRun } = await import('@waggle/server/local/service');
|
||||
const dataDir = process.env.WAGGLE_DATA_DIR || undefined;
|
||||
const displayDataDir = dataDir ?? `${os.homedir()}/.waggle`;
|
||||
const firstRun = isFirstRun(displayDataDir);
|
||||
|
||||
if (firstRun) {
|
||||
console.log(' Welcome! Setting up for the first time...');
|
||||
console.log();
|
||||
}
|
||||
|
||||
try {
|
||||
const { server } = await startService({
|
||||
port,
|
||||
skipLiteLLM,
|
||||
dataDir,
|
||||
onProgress: (event) => {
|
||||
const pct = Math.round(event.progress * 100);
|
||||
process.stdout.write(`\r [${pct.toString().padStart(3)}%] ${event.message}`);
|
||||
if (event.phase === 'ready') {
|
||||
process.stdout.write('\n');
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const addr = server.server.address();
|
||||
const actualPort = typeof addr === 'object' && addr ? addr.port : port;
|
||||
const url = `http://localhost:${actualPort}`;
|
||||
const llm = server.agentState.llmProvider;
|
||||
|
||||
console.log(formatStartupSuccess({
|
||||
url,
|
||||
llmProvider: llm.provider,
|
||||
llmHealth: llm.health,
|
||||
dataDir: displayDataDir,
|
||||
noBrowser,
|
||||
}));
|
||||
|
||||
if (!noBrowser) {
|
||||
openBrowser(url);
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
console.error(formatStartupFailure(message, port));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user