import { getConfigStore, type ConfigSource, } from "../config-store.js"; import { updateString, updateBool } from "../../config.js"; export type DashboardChannelsResponse = { routing: { feishuGroupRequireMention: boolean; wechatGroupRequireMention: boolean; }; feishu: { enabled: boolean; requireMention?: boolean; appId?: string; domain?: string; encryptKey?: string; verificationToken?: string; appSecret?: string; thinkingReaction?: { enabled?: boolean; emojiType?: string; }; }; wechat: { enabled: boolean; requireMention?: boolean; implementation?: string; }; }; export type DashboardChannelsSaveRequest = { channels: DashboardChannelsResponse; }; export function readChannelsResponse(args: ConfigSource): DashboardChannelsResponse { const root = getConfigStore(args).read(); const cfg = root as { channels?: { feishu?: Record; wechat?: Record }; }; const feishuRaw = cfg.channels?.feishu ?? {}; const wechatRaw = cfg.channels?.wechat ?? {}; const feishuEnabled = Boolean((feishuRaw as { enabled?: unknown }).enabled); const wechatEnabled = Boolean((wechatRaw as { enabled?: unknown }).enabled); const feishuRequireMention = Boolean((feishuRaw as { requireMention?: unknown }).requireMention); const wechatRequireMention = Boolean((wechatRaw as { requireMention?: unknown }).requireMention); return { routing: { feishuGroupRequireMention: feishuRequireMention, wechatGroupRequireMention: wechatRequireMention, }, feishu: { enabled: feishuEnabled, requireMention: feishuRequireMention, appId: (feishuRaw as { appId?: string }).appId, domain: (feishuRaw as { domain?: string }).domain, appSecret: (feishuRaw as { appSecret?: string }).appSecret, encryptKey: (feishuRaw as { encryptKey?: string }).encryptKey, verificationToken: (feishuRaw as { verificationToken?: string }).verificationToken, thinkingReaction: (feishuRaw as { thinkingReaction?: unknown }).thinkingReaction as | { enabled?: boolean; emojiType?: string } | undefined, }, wechat: { enabled: wechatEnabled, requireMention: wechatRequireMention, implementation: (wechatRaw as { implementation?: string }).implementation, }, }; } export function saveChannelsRequest(args: ConfigSource & { body: DashboardChannelsSaveRequest }) { const { channels } = args.body; const configStore = getConfigStore(args); const root = configStore.read(); updateBool(root, ["channels", "feishu", "enabled"], channels.feishu.enabled); updateBool(root, ["channels", "feishu", "requireMention"], channels.feishu.requireMention); updateString(root, ["channels", "feishu", "appId"], channels.feishu.appId); updateString(root, ["channels", "feishu", "domain"], channels.feishu.domain); updateString(root, ["channels", "feishu", "appSecret"], channels.feishu.appSecret); updateString(root, ["channels", "feishu", "encryptKey"], channels.feishu.encryptKey); updateString(root, ["channels", "feishu", "verificationToken"], channels.feishu.verificationToken); if (channels.feishu.thinkingReaction) { updateBool( root, ["channels", "feishu", "thinkingReaction", "enabled"], channels.feishu.thinkingReaction.enabled, ); updateString( root, ["channels", "feishu", "thinkingReaction", "emojiType"], channels.feishu.thinkingReaction.emojiType, ); } updateBool(root, ["channels", "wechat", "enabled"], channels.wechat.enabled); updateBool(root, ["channels", "wechat", "requireMention"], channels.wechat.requireMention); updateString(root, ["channels", "wechat", "implementation"], channels.wechat.implementation); configStore.write(root); }