mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-07-15 12:53:31 +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>
61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
import * as fs from 'node:fs'
|
|
import * as path from 'node:path'
|
|
import * as os from 'node:os'
|
|
|
|
export type SessionEntry = {
|
|
sessionId: string
|
|
workDir: string
|
|
updatedAt: number
|
|
}
|
|
|
|
type StoreData = Record<string, SessionEntry>
|
|
|
|
function getDefaultPath(): string {
|
|
const configDir = process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), '.claude')
|
|
return path.join(configDir, 'adapter-sessions.json')
|
|
}
|
|
|
|
export class SessionStore {
|
|
private data: StoreData
|
|
private filePath: string
|
|
|
|
constructor(filePath?: string) {
|
|
this.filePath = filePath ?? getDefaultPath()
|
|
this.data = this.load()
|
|
}
|
|
|
|
get(chatId: string): SessionEntry | null {
|
|
return this.data[chatId] ?? null
|
|
}
|
|
|
|
set(chatId: string, sessionId: string, workDir: string): void {
|
|
this.data[chatId] = { sessionId, workDir, updatedAt: Date.now() }
|
|
this.save()
|
|
}
|
|
|
|
delete(chatId: string): void {
|
|
delete this.data[chatId]
|
|
this.save()
|
|
}
|
|
|
|
listAll(): Array<{ chatId: string } & SessionEntry> {
|
|
return Object.entries(this.data).map(([chatId, entry]) => ({ chatId, ...entry }))
|
|
}
|
|
|
|
private load(): StoreData {
|
|
try {
|
|
return JSON.parse(fs.readFileSync(this.filePath, 'utf-8'))
|
|
} catch {
|
|
return {}
|
|
}
|
|
}
|
|
|
|
private save(): void {
|
|
const dir = path.dirname(this.filePath)
|
|
fs.mkdirSync(dir, { recursive: true })
|
|
const tmp = `${this.filePath}.tmp.${Date.now()}`
|
|
fs.writeFileSync(tmp, JSON.stringify(this.data, null, 2) + '\n')
|
|
fs.renameSync(tmp, this.filePath)
|
|
}
|
|
}
|