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.

24.2 KB
Raw Blame
// ==================== Identifier ====================

export const MessageToolIdentifier = 'lobe-message';

// ==================== Supported Platforms ====================

export const MessagePlatform = {
  dingtalk: 'dingtalk',
  discord: 'discord',
  feishu: 'feishu',
  imessage: 'imessage',
  lark: 'lark',
  qq: 'qq',
  slack: 'slack',
  telegram: 'telegram',
  wechat: 'wechat',
} as const;

export type MessagePlatformType = (typeof MessagePlatform)[keyof typeof MessagePlatform];

// ==================== API Names ====================

export const MessageApiName = {
  // Core message operations (cross-platform)
  deleteMessage: 'deleteMessage',
  editMessage: 'editMessage',
  getReactions: 'getReactions',
  listPins: 'listPins',
  pinMessage: 'pinMessage',
  reactToMessage: 'reactToMessage',
  readDocument: 'readDocument',
  readMessages: 'readMessages',
  searchMessages: 'searchMessages',
  sendMessage: 'sendMessage',
  unpinMessage: 'unpinMessage',

  // Channel management
  getChannelInfo: 'getChannelInfo',
  listChannels: 'listChannels',

  // Member information
  getMemberInfo: 'getMemberInfo',

  // Thread operations
  createThread: 'createThread',
  listThreads: 'listThreads',
  replyToThread: 'replyToThread',

  // Platform-specific
  createPoll: 'createPoll',

  // Direct messaging
  sendDirectMessage: 'sendDirectMessage',

  // Bot management
  connectBot: 'connectBot',
  createBot: 'createBot',
  deleteBot: 'deleteBot',
  getBotDetail: 'getBotDetail',
  listBots: 'listBots',
  listPlatforms: 'listPlatforms',
  toggleBot: 'toggleBot',
  updateBot: 'updateBot',

  // ==================== System Bot Messenger Management ====================
  // Operates on `messenger_installations` (workspace-scoped OAuth installs)
  // and `messenger_account_links` (per-user routing decisions). Mirrors the
  // per-agent bot CRUD surface but for the LobeHub System Bot, which can't
  // be created via tool calls (OAuth requires browser flow).
  /** List the current user's System Bot installations across workspaces. */
  listMessengers: 'listMessengers',
  /** Get one System Bot connection's detail by installationId. */
  getMessengerDetail: 'getMessengerDetail',
  /** Revoke a workspace install (cascades to all users in that workspace). */
  uninstallMessenger: 'uninstallMessenger',
  /** List the platforms where the user can install the LobeHub System Bot. */
  listMessengerPlatforms: 'listMessengerPlatforms',
  /** List the user's account links β€” one row per (platform, tenant). */
  listMessengerLinks: 'listMessengerLinks',
  /** Change which agent receives inbound IM on a specific link. */
  setMessengerActiveAgent: 'setMessengerActiveAgent',
  /** Remove the user's account link for a platform (does not uninstall). */
  unlinkMessenger: 'unlinkMessenger',
  /** Proactively push a message to the current user's own linked messenger DM. */
  sendMessengerPush: 'sendMessengerPush',
} as const;

export type MessageApiNameType = (typeof MessageApiName)[keyof typeof MessageApiName];

// ==================== Common Types ====================

export interface MessageTarget {
  /** Channel / conversation / room ID within the platform */
  channelId: string;
  /** Platform identifier */
  platform: MessagePlatformType;
}

// ==================== Parameter Types ====================

// --- Rich embeds (Discord cards) ---

/** A single key/value block inside a Discord embed. */
export interface SendMessageEmbedField {
  /** Render side-by-side with neighbouring inline fields (up to 3 per row). */
  inline?: boolean;
  /** Field label (max 256 chars). */
  name: string;
  /** Field body, markdown allowed (max 1024 chars). */
  value: string;
}

/**
 * JSON-safe outbound "card" for platforms with native rich embeds. Modelled
 * on the Discord embed object β€” every property is optional but at least one
 * visible block (title / description / fields / footer / author / image) is
 * required for the card to render. Platforms without an embed concept drop
 * these silently so the text `content` still ships.
 *
 * @see https://discord.com/developers/docs/resources/message#embed-object
 */
export interface SendMessageEmbed {
  /** Small header line above the title. */
  author?: { icon_url?: string; name: string; url?: string };
  /** Left accent colour β€” integer (0xRRGGBB) or `#RRGGBB` string. */
  color?: number | string;
  /** Body text, markdown allowed (max 4096 chars). */
  description?: string;
  /** Key/value blocks below the description (max 25). */
  fields?: SendMessageEmbedField[];
  /** Small footer line at the bottom of the card. */
  footer?: { icon_url?: string; text: string };
  /** Large image rendered below the fields. */
  image?: { url: string };
  /** Small image rendered top-right of the card. */
  thumbnail?: { url: string };
  /** ISO-8601 timestamp shown next to the footer. */
  timestamp?: string;
  /** Card heading (max 256 chars). */
  title?: string;
  /** Makes the title a hyperlink. */
  url?: string;
}

// --- Direct Messaging ---

/**
 * Which connection a send goes out through. Both are optional: when neither is
 * set and the call targets the IM conversation this run is replying in, the
 * server runtime sends through the same connection the conversation arrived
 * on (per-agent bot or System Bot messenger). Never set both.
 */
export interface MessageSendRoute {
  /** Per-agent bot id from `listBots`. */
  botId?: string;
  /** System Bot connection id from `listMessengers`. */
  messengerInstallationId?: string;
}

export interface SendDirectMessageParams extends MessageSendRoute {
  /**
   * Optional: outbound media attachments (images / files / video / audio).
   * Same shape as `SendMessageParams.attachments` β€” see `SendMessageAttachment`.
   */
  attachments?: SendMessageAttachment[];
  /** Message content */
  content: string;
  /**
   * Optional: rich embeds / cards. Same shape as `SendMessageParams.embeds`.
   * Only Discord renders these today; other platforms ignore them.
   */
  embeds?: SendMessageEmbed[];
  /** Platform */
  platform: MessagePlatformType;
  /** Target user ID on the platform */
  userId: string;
}

/**
 * One outbound attachment that did NOT reach the user, and why. Mirrors the
 * server's `AttachmentFailure` so the tool result can name the file and the
 * cause instead of the model assuming every attachment landed.
 */
export interface SendAttachmentFailure {
  /** Loader error or platform error text, when there is one. */
  detail?: string;
  name?: string;
  reason: 'over-budget-no-link' | 'source-unavailable' | 'upload-failed';
  type: SendMessageAttachment['type'];
}

/**
 * Attachment outcome shared by every send state. Both fields are absent when
 * the send carried no attachments; `attachmentFailures` is absent when every
 * attachment landed.
 */
export interface SendAttachmentsOutcome {
  attachmentFailures?: SendAttachmentFailure[];
  attachmentsDelivered?: number;
}

export interface SendDirectMessageState extends SendAttachmentsOutcome {
  channelId?: string;
  messageId?: string;
  platform?: string;
}

// --- Core Message Operations ---

/**
 * JSON-safe outbound attachment for `sendMessage`. Either `data` (base64) or
 * `fetchUrl` (remote URL) must be set. Prefer `fetchUrl` to keep payload size
 * small when the binary already lives somewhere reachable.
 *
 * Mirrors `BotMessageAttachment` on the bot-reply callback path so the agent
 * runtime, callback service, and Messager tool/CLI all speak the same shape.
 */
export interface SendMessageAttachment {
  /** Base64-encoded bytes. Used when no fetchable URL exists. */
  data?: string;
  /** Remote URL the platform server can GET to retrieve the bytes. */
  fetchUrl?: string;
  mimeType?: string;
  name?: string;
  type: 'image' | 'file' | 'video' | 'audio';
}

export interface SendMessageParams extends MessageSendRoute {
  /**
   * Optional: outbound media attachments (images / files / video / audio).
   * Platforms that don't support outbound media silently drop these so the
   * text leg still ships.
   */
  attachments?: SendMessageAttachment[];
  /** Channel / conversation / room ID */
  channelId: string;
  /** Message content (text, markdown depending on platform support) */
  content: string;
  /**
   * Optional: rich embeds / cards rendered natively by the platform. Only
   * Discord renders these today (as Discord embeds); other platforms ignore
   * them so the text `content` still ships. See `SendMessageEmbed`.
   */
  embeds?: SendMessageEmbed[];
  /** Platform to send on */
  platform: MessagePlatformType;
  /** Optional: reply to a specific message */
  replyTo?: string;
}

export interface SendMessageState extends SendAttachmentsOutcome {
  channelId?: string;
  messageId?: string;
  platform?: string;
}

export interface ReadMessagesParams {
  /** Optional: read messages after this message ID */
  after?: string;
  /** Optional: read messages before this message ID */
  before?: string;
  /** Channel / conversation / room ID */
  channelId: string;
  /** Pagination cursor from a previous response (Feishu/Lark pageToken) */
  cursor?: string;
  /** End time as Unix second timestamp (Feishu/Lark only) */
  endTime?: string;
  /** Max number of messages to fetch */
  limit?: number;
  /** Platform to read from */
  platform: MessagePlatformType;
  /** Start time as Unix second timestamp (Feishu/Lark only) */
  startTime?: string;
}

export interface ReadMessagesState {
  channelId?: string;
  /** Whether more messages are available */
  hasMore?: boolean;
  messages?: MessageItem[];
  /** Cursor for fetching the next page */
  nextCursor?: string;
  platform?: string;
  totalFetched?: number;
}

export interface MessageItemAttachment {
  /** MIME type when the platform reports it (Discord `content_type`). */
  contentType?: string;
  name: string;
  /** Bytes, when the platform reports it. */
  size?: number;
  url: string;
}

export interface MessageItem {
  attachments?: MessageItemAttachment[];
  author: { id: string; name: string };
  content: string;
  id: string;
  replyTo?: string;
  timestamp: string;
}

// --- Documents ---

export interface ReadDocumentParams {
  /**
   * Platform document ID, for callers that already hold one (e.g. a Feishu
   * docx token). Either this or `url` is required; `url` wins when both given.
   */
  documentId?: string;
  /** Platform to read from */
  platform: MessagePlatformType;
  /** Document URL as it appeared in the chat (e.g. `https://x.feishu.cn/docx/<token>`) */
  url?: string;
}

export interface ReadDocumentState {
  /** Plain-text body of the document */
  content?: string;
  /** Resolved document ID on the platform */
  documentId?: string;
  /** Document kind on the platform (e.g. `docx`, `wiki`) */
  kind?: string;
  platform?: string;
  title?: string;
  /** True when the body was cut to fit the tool result */
  truncated?: boolean;
  /** Canonical URL of the document, when known */
  url?: string;
}

export interface EditMessageParams {
  /** Channel ID where the message is */
  channelId: string;
  /** New content */
  content: string;
  /** Message ID to edit */
  messageId: string;
  /** Platform */
  platform: MessagePlatformType;
}

export interface EditMessageState {
  messageId?: string;
  success?: boolean;
}

export interface DeleteMessageParams {
  /** Channel ID where the message is */
  channelId: string;
  /** Message ID to delete */
  messageId: string;
  /** Platform */
  platform: MessagePlatformType;
}

export interface DeleteMessageState {
  messageId?: string;
  success?: boolean;
}

export interface SearchMessagesParams {
  /** Optional: filter by author */
  authorId?: string;
  /** Channel ID to search in */
  channelId: string;
  /** Max results (default: 25) */
  limit?: number;
  /** Platform */
  platform: MessagePlatformType;
  /** Search query */
  query: string;
}

export interface SearchMessagesState {
  messages?: MessageItem[];
  query?: string;
  totalFound?: number;
}

export interface ReactToMessageParams {
  /** Channel ID */
  channelId: string;
  /** Emoji to react with (unicode emoji or platform-specific format) */
  emoji: string;
  /** Message ID to react to */
  messageId: string;
  /** Platform */
  platform: MessagePlatformType;
}

export interface ReactToMessageState {
  messageId?: string;
  success?: boolean;
}

export interface GetReactionsParams {
  /** Channel ID */
  channelId: string;
  /** Message ID */
  messageId: string;
  /** Platform */
  platform: MessagePlatformType;
}

export interface GetReactionsState {
  messageId?: string;
  reactions?: { count: number; emoji: string; users?: string[] }[];
}

export interface PinMessageParams {
  /** Channel ID */
  channelId: string;
  /** Message ID to pin */
  messageId: string;
  /** Platform */
  platform: MessagePlatformType;
}

export interface PinMessageState {
  messageId?: string;
  success?: boolean;
}

export interface UnpinMessageParams {
  /** Channel ID */
  channelId: string;
  /** Message ID to unpin */
  messageId: string;
  /** Platform */
  platform: MessagePlatformType;
}

export interface UnpinMessageState {
  messageId?: string;
  success?: boolean;
}

export interface ListPinsParams {
  /** Channel ID */
  channelId: string;
  /** Platform */
  platform: MessagePlatformType;
}

export interface ListPinsState {
  messages?: MessageItem[];
}

// --- Channel Management ---

export interface GetChannelInfoParams {
  /** Channel ID */
  channelId: string;
  /** Platform */
  platform: MessagePlatformType;
}

export interface GetChannelInfoState {
  description?: string;
  id?: string;
  memberCount?: number;
  name?: string;
  type?: string;
}

export interface ListChannelsParams {
  /** Optional: filter by category or type */
  filter?: string;
  /** Platform */
  platform: MessagePlatformType;
  /** Server / workspace / group ID (required for platforms with multi-server) */
  serverId?: string;
}

export interface ListChannelsState {
  channels?: { id: string; name: string; type?: string }[];
}

// --- Member Information ---

export interface GetMemberInfoParams {
  /** Member / user ID */
  memberId: string;
  /** Platform */
  platform: MessagePlatformType;
  /** Server / workspace ID (required for some platforms) */
  serverId?: string;
}

export interface GetMemberInfoState {
  avatar?: string;
  displayName?: string;
  id?: string;
  roles?: string[];
  status?: string;
  username?: string;
}

// --- Thread Operations ---

export interface CreateThreadParams {
  /** Channel ID to create thread in */
  channelId: string;
  /** Optional: initial message content */
  content?: string;
  /** Optional: message ID to start thread from */
  messageId?: string;
  /** Thread name */
  name: string;
  /** Platform */
  platform: MessagePlatformType;
}

export interface CreateThreadState {
  threadId?: string;
}

export interface ListThreadsParams {
  /** Channel ID */
  channelId: string;
  /** Platform */
  platform: MessagePlatformType;
}

export interface ListThreadsState {
  threads?: { id: string; messageCount?: number; name: string }[];
}

export interface ReplyToThreadParams extends MessageSendRoute {
  /**
   * Optional: outbound media attachments (images / files / video / audio).
   * Same shape as `SendMessageParams.attachments` β€” see `SendMessageAttachment`.
   */
  attachments?: SendMessageAttachment[];
  /** Reply content */
  content: string;
  /**
   * Optional: rich embeds / cards. Same shape as `SendMessageParams.embeds`.
   * Only Discord renders these today; other platforms ignore them.
   */
  embeds?: SendMessageEmbed[];
  /** Platform */
  platform: MessagePlatformType;
  /** Thread ID */
  threadId: string;
}

export interface ReplyToThreadState extends SendAttachmentsOutcome {
  messageId?: string;
  threadId?: string;
}

// --- Platform-Specific: Polls ---

export interface CreatePollParams {
  /** Channel ID */
  channelId: string;
  /** Duration in hours (platform-specific limits) */
  duration?: number;
  /** Allow multiple answers */
  multipleAnswers?: boolean;
  /** Poll options */
  options: string[];
  /** Platform */
  platform: MessagePlatformType;
  /** Poll question */
  question: string;
}

export interface CreatePollState {
  messageId?: string;
  pollId?: string;
}

// --- Bot Management ---

export interface ListPlatformsParams {
  /** No parameters needed */
}

export interface PlatformInfo {
  credentialFields: { key: string; label: string; required: boolean; type: string }[];
  id: string;
  name: string;
}

export interface ListPlatformsState {
  platforms: PlatformInfo[];
}

export interface ListBotsParams {
  /** No parameters needed β€” returns all bots for the current agent */
}

export interface ConfiguredBotInfo {
  applicationId: string;
  enabled: boolean;
  id: string;
  platform: string;
  runtimeStatus?: string;
  /** Default server/guild/workspace ID (for listing channels) */
  serverId?: string;
  /** Owner's user ID on the platform (for sending DMs) */
  userId?: string;
}

export interface ListBotsState {
  bots: ConfiguredBotInfo[];
}

export interface GetBotDetailParams {
  botId: string;
}

export interface GetBotDetailState {
  applicationId: string;
  enabled: boolean;
  id: string;
  platform: string;
  runtimeStatus?: string;
  settings?: Record<string, unknown>;
}

export interface CreateBotParams {
  /** Agent ID to attach the bot to */
  agentId: string;
  /** Application ID for webhook routing */
  applicationId: string;
  /** Credential key-value pairs (platform-specific) */
  credentials: Record<string, string>;
  /** Target platform */
  platform: string;
  /**
   * Optional initial settings (DM policy, allowlist, server/user IDs, etc.).
   * Same shape as `UpdateBotParams.settings`. Omit to use schema defaults.
   */
  settings?: Record<string, unknown>;
}

export interface CreateBotState {
  id: string;
  platform: string;
}

export interface UpdateBotParams {
  botId: string;
  credentials?: Record<string, string>;
  settings?: Record<string, unknown>;
}

export interface UpdateBotState {
  success: boolean;
}

export interface DeleteBotParams {
  botId: string;
}

export interface DeleteBotState {
  success: boolean;
}

export interface ToggleBotParams {
  botId: string;
  enabled: boolean;
}

export interface ToggleBotState {
  enabled: boolean;
  success: boolean;
}

export interface ConnectBotParams {
  botId: string;
}

export interface ConnectBotState {
  status: string;
}

// --- System Bot Messenger Management ---

/**
 * Summary of a System Bot installation surfaced to the LLM / caller. Mirrors
 * the safe metadata shape returned by `messenger.listMyInstallations` β€” never
 * the credentials.
 */
export interface MessengerInfo {
  /** Platform application/bot id (Slack appId, Discord applicationId, …). */
  applicationId: string;
  /** Slack-only: enterprise grid id when this is an enterprise install. */
  enterpriseId?: string | null;
  /** Stable installation id β€” pass back on `uninstallMessenger` / `getMessengerDetail`. */
  id: string;
  /** ISO timestamp of when the install was created (or Date instance). */
  installedAt?: string | Date;
  /** Slack-only: whether this install is at the enterprise (org-wide) level. */
  isEnterpriseInstall?: boolean;
  /** Messaging platform (slack / discord / telegram / …). */
  platform: string;
  /** OAuth scope string granted at install time (Slack-only typically). */
  scope?: string;
  /** Tenant identifier β€” Slack workspace, Discord guild, WeChat user, … (empty for Telegram). */
  tenantId: string;
  /** Optional human-friendly tenant label (workspace / guild name). */
  tenantName?: string;
}

/**
 * Summary of a user-platform account link. One row per (userId, platform,
 * tenantId) β€” controls which agent the user's inbound IM messages route to.
 */
export interface MessengerLinkInfo {
  /** The agent currently set as active for inbound messages (null = unset). */
  activeAgentId: string | null;
  /** When the link was created. */
  createdAt?: string | Date;
  platform: string;
  /** Platform-side user id (Slack/Discord user id, Telegram chat id, WeChat user id). */
  platformUserId?: string;
  /** Display name surfaced when verify-im completed. */
  platformUsername?: string;
  /** Tenant scope for the link β€” empty for single-link platforms (Telegram / WeChat). */
  tenantId?: string;
}

/**
 * Subset of `messenger.availablePlatforms` payload surfaced to the LLM.
 * Includes the deep-link fields the verify-im flow uses to direct the user
 * to the right install URL.
 */
export interface MessengerPlatformInfo {
  /** Slack appId or Discord applicationId β€” feeds deep-link URLs. */
  appId?: string;
  /** Telegram-only deep-link target (`https://t.me/<botUsername>`). */
  botUsername?: string;
  /** Platform id (slack / discord / telegram / …). */
  id: string;
  /** Display name. */
  name: string;
}

export interface ListMessengersParams {
  /** No parameters needed β€” returns all installs for the current user. */
}

export interface ListMessengersState {
  installations: MessengerInfo[];
}

export interface GetMessengerDetailParams {
  /** Stable installation id from `listMessengers`. */
  installationId: string;
}

export interface GetMessengerDetailState extends MessengerInfo {
  /** ISO timestamp of when the install was revoked (null when active). */
  revokedAt?: string | Date | null;
}

export interface UninstallMessengerParams {
  installationId: string;
}

export interface UninstallMessengerState {
  success: boolean;
}

export interface ListMessengerPlatformsParams {
  /** No parameters needed. */
}

export interface ListMessengerPlatformsState {
  platforms: MessengerPlatformInfo[];
}

export interface ListMessengerLinksParams {
  /** No parameters needed β€” returns all links for the current user. */
}

export interface ListMessengerLinksState {
  links: MessengerLinkInfo[];
}

export interface SetMessengerActiveAgentParams {
  /**
   * Agent id to route inbound messages to. Pass `null` to clear the active
   * agent (next message hits the "/agents to pick" prompt).
   */
  agentId: string | null;
  platform: string;
  /** Optional: scope to a specific workspace (Slack). Omit for global-bot platforms. */
  tenantId?: string;
}

export interface SetMessengerActiveAgentState {
  success: boolean;
}

export interface UnlinkMessengerParams {
  platform: string;
  /** Optional: scope to a specific workspace (Slack). Omit for global-bot platforms. */
  tenantId?: string;
}

export interface UnlinkMessengerState {
  success: boolean;
}

// --- Proactive Messenger Push ---

/** Platforms the System Bot proactive push supports (mirrors `MESSENGER_PUSH_PLATFORMS`). */
/**
 * Upper bound on a proactive push body. Chosen to clear the tightest platform
 * limit in the set (Discord's 2000-character message cap) so the cap fails the
 * call up front instead of at delivery, where only one platform would reject.
 *
 * Single source for the tool schema, the server runtime guard and the TRPC
 * route, so the advertised limit and the enforced one cannot drift.
 */
export const MESSENGER_PUSH_CONTENT_MAX_LENGTH = 2000;

export const MessengerPushPlatform = {
  discord: 'discord',
  linq: 'linq',
  slack: 'slack',
  telegram: 'telegram',
  wechat: 'wechat',
} as const;

export type MessengerPushPlatformType =
  (typeof MessengerPushPlatform)[keyof typeof MessengerPushPlatform];

export interface SendMessengerPushParams {
  /** Message content to deliver into the user's DM with the LobeHub System Bot. */
  content: string;
  platform: MessengerPushPlatformType;
  /**
   * Slack-only: target workspace (team id) when the user linked several.
   * Omit elsewhere β€” the runtime auto-resolves single-link platforms.
   */
  tenantId?: string;
}

/**
 * Delivery outcome of a proactive push. Mirrors the server-side
 * `MessengerPushResult` statuses, plus `needs_workspace_selection` which the
 * runtime synthesizes when a Slack push is ambiguous across workspaces.
 */
export type MessengerPushStatus =
  'sent' | 'queued' | 'unlinked' | 'unavailable' | 'needs_workspace_selection';

/** Candidate workspace surfaced when a Slack push needs disambiguation. */
export interface MessengerPushWorkspaceOption {
  tenantId: string;
  tenantName?: string;
}

export interface SendMessengerPushState {
  platform: MessengerPushPlatformType;
  /** WeChat-only: sends remaining in the current 24h window (present on `sent`). */
  remaining?: number;
  status: MessengerPushStatus;
  /** The workspace the message was routed to, when one was resolved. */
  tenantId?: string;
  /** Present on `needs_workspace_selection` β€” options to relay to the user. */
  workspaces?: MessengerPushWorkspaceOption[];
}