Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

2.7 KB
Raw Blame
import type { ChatTopicBotContext } from '@lobechat/types';

export interface BuildBotContextParams {
  applicationId: string;
  /** Platform-assigned ID of the inbound message author (e.g. Discord/Slack `user.id`). */
  authorUserId: string | undefined;
  /**
   * User IDs, besides the owner, the owner trusts with their devices
   * (`extractDeviceAllowlist(settings)`). Omitted or empty: owner only.
   */
  deviceAllowlist?: string[];
  /**
   * Whether this conversation sits in a channel whose members may all use the
   * owner's devices (`isDeviceChannelGranted`). Omitted: no channel grant.
   */
  deviceChannelGranted?: boolean;
  /**
   * Whether this conversation is outside the channels the owner limited device
   * use to (`isDeviceChannelRestricted`). Omitted: no channel restriction.
   */
  deviceChannelRestricted?: boolean;
  /**
   * Configured owner ID for this bot install (e.g. `agent_bot_providers.settings.userId`
   * or messenger link's `platformUserId`). Owner identity is decided by
   * exact equality with `authorUserId` — never inferred, never defaulted.
   */
  operatorUserId: string | undefined;
  platform: string;
  platformThreadId: string;
}

/**
 * Build the per-turn `ChatTopicBotContext` for an inbound bot message.
 *
 * This helper is the **single** place that decides `isOwner`. Downstream
 * policy (`resolveDeviceAccessPolicy`, audit log, agent runtime gates) reads
 * `isOwner` directly and never recomputes — keeping the rule one-place-only
 * is what prevents the fail-closed default from regressing as new routers
 * (Bot, Messenger, future platforms) are added.
 *
 * Fail-closed contract: when `operatorUserId` is missing the result is
 * `isOwner: false`, regardless of `authorUserId`. Treating absent owner
 * configuration as "trusted" would silently grant device access to every
 * sender on unconfigured installs.
 */
export const buildBotContext = (params: BuildBotContextParams): ChatTopicBotContext => {
  const senderExternalUserId = params.authorUserId ?? '';
  const isOwner = !!params.operatorUserId && senderExternalUserId === params.operatorUserId;
  // Same fail-closed rule as `isOwner`: an unknown sender is never trusted.
  const isDeviceTrusted =
    !isOwner && !!senderExternalUserId && !!params.deviceAllowlist?.includes(senderExternalUserId);
  const isDeviceChannelGranted =
    !isOwner && !!senderExternalUserId && params.deviceChannelGranted === true;
  return {
    applicationId: params.applicationId,
    isOwner,
    ...(isDeviceChannelGranted && { isDeviceChannelGranted }),
    ...(params.deviceChannelRestricted && { isDeviceChannelRestricted: true }),
    ...(isDeviceTrusted && { isDeviceTrusted }),
    platform: params.platform,
    platformThreadId: params.platformThreadId,
    senderExternalUserId,
  };
};