/** * Adapter 配置加载 * * 优先级:环境变量 > ~/.claude/adapters.json > 默认值 */ import * as fs from 'node:fs' import * as os from 'node:os' import * as path from 'node:path' export type PairedUser = { userId: string | number displayName: string pairedAt: number } export type PairingState = { code: string | null expiresAt: number | null createdAt: number | null } export type TelegramConfig = { botToken: string allowedUsers: number[] pairedUsers: PairedUser[] defaultWorkDir: string } export type FeishuConfig = { appId: string appSecret: string encryptKey: string verificationToken: string allowedUsers: string[] pairedUsers: PairedUser[] defaultWorkDir: string streamingCard: boolean } export type AdapterConfig = { serverUrl: string defaultProjectDir: string pairing: PairingState telegram: TelegramConfig feishu: FeishuConfig } function getConfigPath(): string { const configDir = process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), '.claude') return path.join(configDir, 'adapters.json') } function loadFile(): Record { try { return JSON.parse(fs.readFileSync(getConfigPath(), 'utf-8')) } catch (err: any) { if (err?.code !== 'ENOENT') { console.warn(`[Config] Failed to parse ${getConfigPath()}, using defaults`) } return {} } } export function loadConfig(): AdapterConfig { const file = loadFile() const tg = file.telegram ?? {} const fs_ = file.feishu ?? {} const pairing = file.pairing ?? {} return { serverUrl: process.env.ADAPTER_SERVER_URL || file.serverUrl || 'ws://127.0.0.1:3456', defaultProjectDir: file.defaultProjectDir || '', pairing: { code: pairing.code ?? null, expiresAt: pairing.expiresAt ?? null, createdAt: pairing.createdAt ?? null, }, telegram: { botToken: process.env.TELEGRAM_BOT_TOKEN || tg.botToken || '', allowedUsers: tg.allowedUsers ?? [], pairedUsers: tg.pairedUsers ?? [], defaultWorkDir: tg.defaultWorkDir || process.cwd(), }, feishu: { appId: process.env.FEISHU_APP_ID || fs_.appId || '', appSecret: process.env.FEISHU_APP_SECRET || fs_.appSecret || '', encryptKey: process.env.FEISHU_ENCRYPT_KEY || fs_.encryptKey || '', verificationToken: process.env.FEISHU_VERIFICATION_TOKEN || fs_.verificationToken || '', allowedUsers: fs_.allowedUsers ?? [], pairedUsers: fs_.pairedUsers ?? [], defaultWorkDir: fs_.defaultWorkDir || process.cwd(), streamingCard: fs_.streamingCard ?? false, }, } }