cc-haha/adapters/common/config.ts
程序员阿江(Relakkes) 88e7e14598 feat(pairing): extend type definitions with pairing and pairedUsers fields
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-08 20:06:37 +08:00

97 lines
2.5 KiB
TypeScript

/**
* 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<string, any> {
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,
},
}
}