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.

5.4 KB
Raw Blame
/** Default debounce window (ms) for message batching. */
export const DEFAULT_BOT_DEBOUNCE_MS = 2000;

/** Maximum debounce window (ms) allowed across all platforms. */
export const MAX_BOT_DEBOUNCE_MS = 30_000;

/** Default number of messages to read from channel history. */
export const DEFAULT_BOT_HISTORY_LIMIT = 50;

/**
 * Maximum number of messages allowed at the interface layer.
 * This is the upper bound across all platforms (Slack supports up to 999).
 * Each platform service clamps to its own API limit.
 */
export const MAX_BOT_HISTORY_LIMIT = 999;

/** Minimum number of messages allowed for history limit. */
export const MIN_BOT_HISTORY_LIMIT = 1;

/**
 * Per-platform attachment size budgets for the proactive messenger push path.
 *
 * Shared between the server (which enforces them: over-budget images are
 * recompressed, files that cannot fit degrade to a download-link message β€”
 * see apps/server/src/services/bot/platforms/attachmentBudget.ts) and the
 * client (which uses them to warn the user in the push modal BEFORE sending).
 *
 * Values are deliberately conservative:
 * - telegram: 5MB photo / 20MB file β€” Bot API caps for URL-sourced media.
 * - discord: 10MB β€” default upload cap for bots (DMs never get guild boosts).
 * - slack: 50MB β€” `files.upload` v2 itself allows 1GB, but every sender
 *   materializes the file in memory to upload it, so the real ceiling is
 *   MAX_IN_MEMORY_ATTACHMENT_BYTES. Budgeting at 1GB made the 50MB–1GB band
 *   pass the budget pass as an upload and then fail during materialization,
 *   dropping the attachment with neither a file nor a download link.
 * - wechat: 2MB image β€” empirical: iLink silently drops larger images
 *   (every API call returns 200 yet the message never renders); 20MB file
 *   as a best-effort cap, the iLink protocol documents no explicit number.
 * - linq: 10MB β€” Linq's documented ceiling for media it fetches by URL.
 *
 * `textMaxChars` is the single-message character cap used to batch the
 * download-link fallbacks: Discord rejects a message over 2000 chars,
 * Telegram truncates at 4096 (which would cut a URL in half), Slack's
 * practical `chat.postMessage` limit is ~3000, and iLink documents none β€”
 * so WeChat reuses Discord's conservative number.
 */
export interface MessengerAttachmentBudget {
  fileMaxBytes: number;
  imageMaxBytes: number;
  /**
   * Character cap for a single outbound text message. Used to batch
   * download-link fallbacks: a message over the cap is either rejected
   * (Discord) or truncated mid-URL (Telegram), both of which lose the link.
   */
  textMaxChars: number;
}

const MB = 1024 * 1024;

export const MESSENGER_ATTACHMENT_BUDGETS: Record<
  'discord' | 'slack' | 'telegram' | 'wechat' | 'linq',
  MessengerAttachmentBudget
> = {
  discord: { fileMaxBytes: 10 * MB, imageMaxBytes: 10 * MB, textMaxChars: 2000 },
  linq: { fileMaxBytes: 10 * MB, imageMaxBytes: 10 * MB, textMaxChars: 2000 },
  slack: { fileMaxBytes: 50 * MB, imageMaxBytes: 50 * MB, textMaxChars: 3000 },
  telegram: { fileMaxBytes: 20 * MB, imageMaxBytes: 5 * MB, textMaxChars: 4096 },
  wechat: { fileMaxBytes: 20 * MB, imageMaxBytes: 2 * MB, textMaxChars: 2000 },
};

/**
 * What to do with an image that does not fit the platform's `imageMaxBytes`.
 *
 * `compress` re-encodes it under the budget so the recipient gets an inline
 * picture; `link` skips the re-encode and sends the original as a download
 * link instead. The choice is the sender's because the trade-off is theirs:
 * a chat screenshot survives recompression fine, while a design mock or a
 * photo the recipient is meant to inspect does not.
 */
export type MessengerOversizeImageStrategy = 'compress' | 'link';

/** Preserves the pre-choice behavior for every caller that sends none. */
export const DEFAULT_OVERSIZE_IMAGE_STRATEGY: MessengerOversizeImageStrategy = 'compress';

/**
 * Hard ceiling on the source an image may be recompressed from.
 *
 * Buffering the original into memory is what recompression costs, so past this
 * the server refuses and sends a download link instead β€” whatever the sender
 * picked. It lives here, beside the budget table and for the same reason: the
 * push modal must not offer "compress it" for a file the server will never
 * compress, or the consequence it spells out is simply untrue.
 */
export const MESSENGER_MAX_COMPRESSION_SOURCE_BYTES = 100 * MB;

/**
 * Stand-in the server returns instead of a bot credential it will not disclose.
 *
 * Shared with the client because a form that reads a credential back cannot
 * tell a secret from a placeholder by looking: helper actions that spend the
 * value (LINE's bot-info fetch, Feishu's owner lookup) have to recognise it as
 * "not available" rather than send it upstream and fail authentication.
 *
 * One fixed sentinel rather than a partial reveal: surviving characters are
 * still entropy, and an exact value is the only thing the write path can
 * reliably match on the way back in.
 */
export const BOT_CREDENTIAL_MASK = 'β€’β€’β€’β€’β€’β€’β€’β€’';

/**
 * Whether a form field holds the placeholder rather than a usable credential.
 *
 * Anything that spends a credential β€” a helper that calls the platform API, a
 * bridge that persists it locally β€” must ask this before using the value, or it
 * sends the placeholder upstream and reads the resulting auth failure as a bad
 * secret.
 */
export const isMaskedBotCredential = (value: string | null | undefined): boolean =>
  value?.trim() === BOT_CREDENTIAL_MASK;