client-finder/scripts/run.ts

104 lines
2.8 KiB
TypeScript
Raw Normal View History

2026-03-11 23:36:43 +00:00
#!/usr/bin/env bun
import { runClientFinder } from '../src/index.js';
/**
* v2.0 .env.local
* ~/.openclaw/.env
*
* skill skill
*
*
* cp ~/.openclaw/.env.example ~/.openclaw/.env
* vi ~/.openclaw/.env # CLIENT_KEY
*/
/**
* Print usage information
*/
function printUsage(): void {
console.error(`Usage:
bun run scripts/run.ts [--client-key=<sk_xxx.yyy>] [--auth-base=<url>] "<query>" [country] [--dry-run]
Environment:
~/.openclaw/.env
CLI args take precedence over global config.
Examples:
bun run scripts/run.ts "find electronics suppliers in China"
bun run scripts/run.ts "find electronics suppliers in China" China --dry-run
bun run scripts/run.ts --client-key=sk_xxx "find suppliers" US
Configuration:
Global config: ~/.openclaw/.env
CLI args override global config
`);
}
type CliArgs = {
query: string;
country?: string;
dryRun: boolean;
clientKey?: string;
authBase?: string;
};
function parseArgs(argv: string[]): CliArgs | null {
const positionals: string[] = [];
let dryRun = false;
let clientKey: string | undefined;
let authBase: string | undefined;
for (const arg of argv) {
if (arg === '--dry-run') {
dryRun = true;
} else if (arg.startsWith('--client-key=')) {
clientKey = arg.slice('--client-key='.length).trim();
} else if (arg.startsWith('--auth-base=')) {
authBase = arg.slice('--auth-base='.length).trim().replace(/\/$/, '');
} else if (arg === '-h' || arg === '--help') {
printUsage();
process.exit(0);
} else if (!arg.startsWith('--')) {
positionals.push(arg);
}
}
if (positionals.length < 1) {
return null;
}
const query = positionals[0];
const country = positionals.length > 1 ? positionals[1] : undefined;
return { query, country, dryRun, clientKey, authBase };
}
async function main(): Promise<void> {
// 不再加载 .env.local直接使用全局配置 ~/.openclaw/.env
// auth-runtime 会自动加载全局配置
const parsed = parseArgs(process.argv.slice(2));
if (!parsed) {
printUsage();
process.exit(1);
}
// 命令行参数覆盖全局配置
if (parsed.clientKey) process.env.CLIENT_KEY = parsed.clientKey;
if (parsed.authBase) process.env.AUTH_BASE = parsed.authBase;
const result = await runClientFinder(parsed.query, parsed.country, parsed.dryRun);
console.log(JSON.stringify(result, null, 2));
}
main().catch((error) => {
console.error(JSON.stringify({
status: 'failed',
error: error instanceof Error ? error.message : String(error),
query: '',
dryRun: false,
}, null, 2));
process.exit(1);
});