mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-07-17 13:13:35 +08:00
Implement IM adapters allowing users to chat with Claude Code from Telegram and Feishu/Lark. Includes persistent session management (chatId→sessionId mapping), project selection via /projects command, and a web UI settings page for configuring bot tokens, allowed users, and default project directory. Key changes: - adapters/: Telegram and Feishu adapter scripts with shared common modules (WsBridge, MessageBuffer, SessionStore, HttpClient, config, formatting) - Backend: adapterService + REST API (GET/PUT /api/adapters) with secret masking - Frontend: AdapterSettings page in Settings tab with i18n support - DirectoryPicker: use React Portal for dropdown to fix overflow clipping Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
74 lines
2.0 KiB
TypeScript
74 lines
2.0 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 TelegramConfig = {
|
|
botToken: string
|
|
allowedUsers: number[]
|
|
defaultWorkDir: string
|
|
}
|
|
|
|
export type FeishuConfig = {
|
|
appId: string
|
|
appSecret: string
|
|
encryptKey: string
|
|
verificationToken: string
|
|
allowedUsers: string[]
|
|
defaultWorkDir: string
|
|
streamingCard: boolean
|
|
}
|
|
|
|
export type AdapterConfig = {
|
|
serverUrl: string
|
|
defaultProjectDir: string
|
|
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 ?? {}
|
|
|
|
return {
|
|
serverUrl: process.env.ADAPTER_SERVER_URL || file.serverUrl || 'ws://127.0.0.1:3456',
|
|
defaultProjectDir: file.defaultProjectDir || '',
|
|
telegram: {
|
|
botToken: process.env.TELEGRAM_BOT_TOKEN || tg.botToken || '',
|
|
allowedUsers: tg.allowedUsers ?? [],
|
|
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 ?? [],
|
|
defaultWorkDir: fs_.defaultWorkDir || process.cwd(),
|
|
streamingCard: fs_.streamingCard ?? false,
|
|
},
|
|
}
|
|
}
|