template-skill/scripts/run.ts

67 lines
2.0 KiB
TypeScript

#!/usr/bin/env bun
import type { Command } from '../src/index.ts';
import { run } from '../src/index.ts';
const SKILL_NAME = 'my-skill'; // TODO: replace with actual skill name
function printUsage(): void {
console.error(`Usage:
bun scripts/run.ts [--api-base=<url>] <command> [args...] [--dry-run]
Commands:
run <arg>
Config: ~/.openclaw/.env (CLIENT_KEY)
`);
}
function reportTelemetry(payload: object): void {
const endpoint = process.env.TELEMETRY_ENDPOINT;
if (!endpoint) return;
fetch(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
}).catch(() => {});
}
async function main(): Promise<void> {
const positionals: string[] = [];
let dryRun = false;
for (const arg of process.argv.slice(2)) {
if (arg === '--dry-run') {
dryRun = true;
} else if (arg.startsWith('--api-base=')) {
process.env.API_BASE = arg.slice('--api-base='.length).trim();
} else if (arg === '-h' || arg === '--help') {
printUsage(); process.exit(0);
} else {
positionals.push(arg);
}
}
if (positionals.length < 1) { printUsage(); process.exit(1); }
const command = positionals[0] as Command;
const startMs = Date.now();
let result: Awaited<ReturnType<typeof run>>;
try {
result = await run(command, positionals.slice(1), dryRun);
} catch (err) {
const error = err instanceof Error ? err.message : String(err);
console.log(JSON.stringify({ status: 'failed', command, dryRun, error }, null, 2));
if (!dryRun) reportTelemetry({ skill: SKILL_NAME, command, status: 'failed', durationMs: Date.now() - startMs, error });
process.exit(1);
}
console.log(JSON.stringify(result, null, 2));
if (!dryRun) reportTelemetry({ skill: SKILL_NAME, command, status: result.status, durationMs: Date.now() - startMs, error: (result as any).error });
}
main().catch((err) => {
console.error(JSON.stringify({ status: 'failed', error: err instanceof Error ? err.message : String(err) }, null, 2));
process.exit(1);
});