botConfigHelpers.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import {
  2. normalizeBotReplyGranularity,
  3. type BotActor,
  4. type BotCommandPolicy,
  5. type BotConfig,
  6. type BotProvider,
  7. type BotsConfigFile,
  8. } from "@zcode/shared";
  9. import { normalizeBotCommandPolicy, normalizeBotCurrentOptions } from "./config.js";
  10. import { normalizeAllowedWorkspaces } from "./workspaceHelpers.js";
  11. function isRecord(value: unknown): value is Record<string, unknown> {
  12. return typeof value === "object" && value !== null;
  13. }
  14. export function getContextKey(bot: Pick<BotConfig, "id">): string {
  15. return bot.id;
  16. }
  17. export function findBot(config: BotsConfigFile, botId: string): BotConfig | null {
  18. return config.bots.find((bot) => bot.id === botId) ?? null;
  19. }
  20. export function findCallbackBot(
  21. config: BotsConfigFile,
  22. provider: BotProvider,
  23. payload: unknown,
  24. ): BotConfig | null {
  25. if (!isRecord(payload)) {
  26. return null;
  27. }
  28. const botId = typeof payload.botId === "string" ? payload.botId : "";
  29. if (botId) {
  30. return findBot(config, botId);
  31. }
  32. return config.bots.filter((bot) => bot.provider === provider && bot.enabled).at(0) ?? null;
  33. }
  34. export function findAuthorizedBot(config: BotsConfigFile, actor: BotActor): BotConfig | null {
  35. if (actor.chatType === "group" && (actor.provider === "feishu" || actor.provider === "lark")) {
  36. return (
  37. config.bots.find(
  38. (bot) => bot.enabled && bot.id === actor.botId && bot.provider === actor.provider,
  39. ) ?? null
  40. );
  41. }
  42. if (actor.provider === "weixin") {
  43. return (
  44. config.bots.find(
  45. (bot) => bot.enabled && bot.provider === "weixin" && bot.id === actor.botId,
  46. ) ?? null
  47. );
  48. }
  49. return (
  50. config.bots.find(
  51. (bot) =>
  52. bot.enabled &&
  53. bot.provider === actor.provider &&
  54. bot.providerUserId === actor.providerUserId,
  55. ) ?? null
  56. );
  57. }
  58. export function findBoundUser(bot: BotConfig, actor: BotActor): BotConfig | null {
  59. return actor.provider === "weixin" || bot.providerUserId === actor.providerUserId ? bot : null;
  60. }
  61. export function normalizeBotConfig(bot: BotConfig): BotConfig {
  62. const normalized = {
  63. ...bot,
  64. // Bot 配置化后 allowedWorkspaces 是唯一 workspace 权限边界;空数组统一落成 "*"。
  65. allowedWorkspaces: normalizeAllowedWorkspaces(bot.allowedWorkspaces),
  66. allowedCommands: normalizeBotCommandPolicy(bot.allowedCommands),
  67. currentOptions: normalizeBotCurrentOptions(bot.currentOptions),
  68. // Bugfix: Feishu/Lark 的回复颗粒度依赖 Card JSON 2.0 单卡更新,旧配置不能继续保留普通消息模式。
  69. replyMode: normalizeBotReplyGranularity(bot.provider, bot.replyMode),
  70. };
  71. if (normalized.provider !== "weixin") {
  72. return normalized;
  73. }
  74. // 微信使用内置 iLink 地址,保存时清理 webhookUrl,避免把其他 provider 的出站字段残留到微信配置。
  75. delete normalized.webhookUrl;
  76. return normalized;
  77. }
  78. function getUserCommandPolicy(bot: BotConfig): BotCommandPolicy {
  79. return normalizeBotCommandPolicy(bot.allowedCommands);
  80. }
  81. export function isUserCommandAllowed(
  82. bot: BotConfig,
  83. requestedCommand:
  84. | "help"
  85. | "status"
  86. | "new"
  87. | "reconnect"
  88. | "workspace"
  89. | "model"
  90. | "mode"
  91. | "thoughtLevel"
  92. | "task"
  93. | "reply"
  94. | "stop"
  95. | "message"
  96. | "approve",
  97. ): boolean {
  98. if (
  99. requestedCommand === "help" ||
  100. requestedCommand === "message" ||
  101. requestedCommand === "approve" ||
  102. requestedCommand === "task" ||
  103. requestedCommand === "stop"
  104. ) {
  105. return true;
  106. }
  107. if (requestedCommand === "reconnect") {
  108. return getUserCommandPolicy(bot).workspace !== false;
  109. }
  110. return getUserCommandPolicy(bot)[requestedCommand] !== false;
  111. }
  112. export function normalizeConfigBots(config: BotsConfigFile): BotsConfigFile {
  113. return {
  114. ...config,
  115. bots: config.bots.map(normalizeBotConfig),
  116. };
  117. }