client-finder/scripts/run.ts

104 lines
2.8 KiB
TypeScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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);
});