import type { BotAppConfig } from "../core.js"; import { createDashboardServer } from "./server.js"; import type { DashboardServer } from "./types.js"; import type { PiAgentRuntime } from "../agent.js"; import { createFileConfigStore, createMemoryConfigStore, type ConfigStore, } from "./config-store.js"; function createFallbackConfigRoot(botConfig: BotAppConfig): Record { return { agents: { defaults: { model: { primary: botConfig.agent.provider && botConfig.agent.model ? `${botConfig.agent.provider}/${botConfig.agent.model}` : "" } } }, models: { providers: {} }, channels: { feishu: { enabled: botConfig.channels.feishu?.enabled ?? false, requireMention: botConfig.routing.feishuGroupRequireMention, appId: botConfig.channels.feishu?.appId, appSecret: botConfig.channels.feishu?.appSecret, domain: botConfig.channels.feishu?.domain, encryptKey: botConfig.channels.feishu?.encryptKey, verificationToken: botConfig.channels.feishu?.verificationToken, thinkingReaction: { enabled: botConfig.channels.feishu?.thinkingReaction?.enabled ?? true, emojiType: botConfig.channels.feishu?.thinkingReaction?.emojiType } }, wechat: { enabled: botConfig.channels.wechat?.enabled ?? false, requireMention: botConfig.routing.wechatGroupRequireMention } } }; } export function createDashboard(args: { botConfig: BotAppConfig; channels: { feishu?: unknown; wechat?: unknown }; agentRuntime: PiAgentRuntime; configStore?: ConfigStore; }): DashboardServer { const configStore = args.configStore ?? (args.botConfig.agent.configPath ? createFileConfigStore(args.botConfig.agent.configPath) : createMemoryConfigStore(args.botConfig.configRoot ?? createFallbackConfigRoot(args.botConfig))); return createDashboardServer({ runtime: { appName: args.botConfig.appName, agentMode: args.botConfig.agent.mode, workspaceDir: args.botConfig.agent.cwd ?? "", agentDir: args.botConfig.agent.agentDir ?? "", enabledChannels: { feishu: Boolean(args.channels.feishu), wechat: Boolean(args.channels.wechat), }, }, agentRuntime: args.agentRuntime, configStore, }); }