| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752 |
- /**
- * ChannelPlugin — multi-channel plugin architecture type definitions.
- *
- * Replaces the BaseChannelPlugin inheritance model with a composition-based
- * adapter pattern. Each channel platform implements ChannelPlugin with
- * 4 required + 6 optional adapters.
- *
- * Design references:
- * - Composition over inheritance
- * - Dual-layer separation: channel messages via adapters, complex ops via CLI
- * - OpenClaw ChannelPlugin patterns (flat adapter composition)
- */
- import type { ChannelPlatform } from './channel-route.js';
- import type { ChatType } from './channel-context.js';
- import type { AskQuestionnaireRequest, AskQuestionnaireReplyPayload } from './questionnaire.js';
- // ============================================================================
- // Core Types
- // ============================================================================
- /**
- * Unified parsed event from any channel platform.
- * Produced by ChannelInboundAdapter.parseEvent() from raw platform events.
- */
- export interface ChannelParsedEvent {
- /** Event type discriminator */
- kind: 'message' | 'card_action' | 'reaction';
- /** Source platform */
- platform: ChannelPlatform;
- /** Chat/conversation type */
- chatType: ChatType;
- /** Chat/conversation unique ID */
- chatId: string;
- /** Platform message ID */
- messageId: string;
- /** Sender user ID */
- senderId: string;
- /** Sender display name (optional, for logging) */
- senderName?: string;
- /**
- * Client/user language tag reported by the platform (e.g. 'en' / 'zh-hans').
- * Filled best-effort by the inbound adapter; consumed to localize outbound
- * copy. Absent → outbound falls back to the channel default locale.
- */
- locale?: string;
- /** Extracted text content */
- text: string;
- /** Whether the bot was @mentioned */
- hasMention: boolean;
- /** Attached files/images/audio */
- attachments: ChannelAttachment[];
- /** Original unprocessed platform event */
- rawEvent: unknown;
- /** Event timestamp (Unix ms) */
- timestamp: number;
- /**
- * Parent message ID when this message is a reply.
- * Used to fetch the original message for context injection.
- */
- parentMessageId?: string;
- /**
- * Quoted message content when this message is a reply.
- * Populated by the inbound adapter's fetchParentMessage().
- */
- quotedMessage?: {
- /** Extracted text content of the quoted message */
- text: string;
- /** Sender display name (if available) */
- senderName?: string;
- };
- }
- /** Attachment descriptor from inbound messages. */
- export interface ChannelAttachment {
- /** Attachment type */
- type: 'image' | 'file' | 'audio' | 'video';
- /** Platform-specific resource key */
- key: string;
- /** Platform message ID that owns this resource, if different from the event message. */
- sourceMessageId?: string;
- /** Original filename (if known) */
- name?: string;
- /** MIME type (if known) */
- mimeType?: string;
- /** File size in bytes (if known) */
- size?: number;
- }
- /**
- * Context passed to outbound adapters for sending messages.
- * Constructed from ChannelContext by ChannelPluginRunner.
- */
- export interface ChannelOutboundContext {
- /** Target chat/conversation ID */
- chatId: string;
- /** Message ID to reply to (for threading) */
- replyToMessageId?: string;
- /** Thread/topic ID (platform-specific) */
- threadId?: string;
- /** Agent session ID (for state correlation) */
- sessionId?: string;
- /** Agent display name */
- agentName?: string;
- /** Channel client instance name */
- clientName: string;
- }
- /** Result of a send operation. */
- export interface ChannelSendResult {
- /** Whether the send was successful */
- success: boolean;
- /** Platform message ID of the sent message */
- messageId?: string;
- /** Error message if failed */
- error?: string;
- }
- /** Media payload for sending media messages. */
- export interface ChannelMediaPayload {
- /** Media type */
- type: 'image' | 'file' | 'audio' | 'video';
- /** File path or URL */
- source: string;
- /** MIME type */
- mimeType?: string;
- /** Caption text */
- caption?: string;
- }
- /** Tool call step for rich reply card rendering. */
- export interface ChannelToolStep {
- /** Tool name */
- name: string;
- /** Human-readable detail (file path, URL, command, etc.) */
- detail?: string;
- /** Tool call status */
- status: 'running' | 'finished' | 'failed';
- /** Tool call ID */
- callId: string;
- /** Raw tool arguments (JSON string) */
- args?: string;
- }
- /** Permission request for approval flow. */
- export interface ChannelPermissionRequest {
- /** Unique request ID */
- requestId: string;
- /** Agent session ID */
- sessionId: string;
- /** Agent name */
- agentName: string;
- /** Human-readable description of what is being requested */
- description: string;
- /** Tool name that triggered the request */
- toolName?: string;
- /** Tool arguments (JSON) */
- toolArgs?: string;
- /** AI-generated tool description (only present for tools that provide it, e.g. bash) */
- toolDescription?: string;
- /** Human-readable tool input (command, file path, etc.) */
- toolInput?: string;
- }
- /** User's decision on a permission request. */
- export interface ChannelPermissionDecision {
- /** Request ID being decided */
- requestId: string;
- /** User's decision */
- action: 'allow' | 'deny' | 'allow-always';
- /** User ID who made the decision */
- userId: string;
- }
- /** Connection probe result. */
- export interface ChannelProbeResult {
- /** Whether the connection is healthy */
- healthy: boolean;
- /** Bot display name (if retrieved) */
- botName?: string;
- /** Bot platform ID (e.g., open_id for Feishu) */
- botId?: string;
- /** Auth status */
- authStatus: 'valid' | 'expired' | 'unknown' | 'ok' | 'failed';
- /** Error details if unhealthy */
- error?: string;
- }
- // ============================================================================
- // Account Configuration
- // ============================================================================
- /** Platform-agnostic account configuration. */
- export interface ChannelAccountConfig {
- /** Account instance name (e.g., "main-bot", "test-bot") */
- name: string;
- /** Whether this account is enabled */
- enabled: boolean;
- /** Platform identifier */
- platform: ChannelPlatform;
- /** Platform-specific credentials */
- credentials: Record<string, string>;
- /** Connection mode */
- mode?: string;
- /** Additional platform-specific options */
- options?: Record<string, unknown>;
- }
- // ============================================================================
- // Capability Declaration
- // ============================================================================
- /**
- * Static capability declaration for a channel platform.
- * ChannelPluginRunner uses this to decide behavior paths instead of
- * platform-specific if/else checks.
- */
- export interface ChannelCapabilities {
- /** Supported chat types */
- chatTypes: ChatType[];
- /** Supports interactive card messages */
- interactiveCards: boolean;
- /** Supports in-place card content update (streaming) */
- streamingUpdate: boolean;
- /** Supports emoji reactions */
- reactions: boolean;
- /** Supports editing sent messages */
- editMessage: boolean;
- /** Supports native threads/topics */
- threads: boolean;
- /** Supports media messages (images, files) */
- media: boolean;
- /** Supports audio messages and transcription */
- audio: boolean;
- /** Supports @mention detection */
- mentions: boolean;
- /**
- * Whether this channel requires the per-sender owner-on-first-DM gate
- * in `InboundEventHandler`.
- *
- * Public bots (Telegram, Feishu) have no platform-level per-bot
- * authentication: anyone who knows the bot's @username can DM it, so
- * the application layer must restrict each bot to a single owner
- * sender. These plugins should leave this at the default `true`.
- *
- * Bots whose platform enforces per-bot authentication (WeChat iLink
- * Bot — bound to a single authed user via QR) authenticate the user
- * at the platform level and should set this to `false` to skip the
- * owner gate entirely. Skipping the gate also avoids persisting a
- * meaningless `wechat:<agentName>` row to `channel-owner.yaml`.
- *
- * Default: `true` (preserve existing gate behavior for plugins that
- * have not yet opted in).
- */
- requiresOwnerGate?: boolean;
- }
- /** Plugin metadata for display/registration. */
- export interface ChannelMeta {
- /** Human-readable platform name */
- label: string;
- /** Platform description */
- description?: string;
- /** Platform icon URL or emoji */
- icon?: string;
- /** Sort order for UI display */
- order?: number;
- }
- // ============================================================================
- // Required Adapters (4)
- // ============================================================================
- /**
- * Config Adapter — account configuration management.
- * Responsible for listing and validating accounts from the application config.
- */
- export interface ChannelConfigAdapter {
- /**
- * List all account configurations for this platform.
- * @param cfg - Raw application config object
- */
- listAccounts(cfg: Record<string, unknown>): ChannelAccountConfig[];
- /**
- * Validate an account configuration.
- * Returns validation result with potential error messages.
- */
- validateAccount(
- account: ChannelAccountConfig,
- ): { valid: true } | { valid: false; errors: string[] };
- /** Optional JSON schema for platform-specific configuration. */
- configSchema?(): Record<string, unknown>;
- }
- /**
- * Gateway Adapter — connection lifecycle management.
- * Manages the connection to the channel platform (WebSocket, polling, etc.).
- */
- export interface ChannelGatewayAdapter {
- /**
- * Start the connection and begin receiving events.
- * Events are delivered via ctx.onRawEvent callback.
- */
- start(ctx: ChannelGatewayContext): Promise<void>;
- /**
- * Stop the connection gracefully.
- */
- stop(ctx: ChannelGatewayContext): Promise<void>;
- /**
- * Optional health probe — check connection status and credentials.
- */
- probe?(ctx: ChannelGatewayContext): Promise<ChannelProbeResult>;
- }
- /** Runtime context provided to the gateway adapter. */
- export interface ChannelGatewayContext {
- /** Account configuration */
- account: ChannelAccountConfig;
- /** Client instance name */
- clientName: string;
- /** Data directory for persistent storage */
- dataDir: string;
- /** Callback for delivering raw platform events to ChannelPluginRunner */
- onRawEvent: (rawEvent: unknown) => Promise<void>;
- /** Signal for graceful shutdown */
- abortSignal: AbortSignal;
- }
- /**
- * Inbound Adapter — incoming event parsing.
- * Converts raw platform events into the unified ChannelParsedEvent format.
- */
- export interface ChannelInboundAdapter {
- /**
- * Parse a raw platform event into a unified ChannelParsedEvent.
- * Returns null if the event should be ignored (unsupported type, etc.).
- *
- * May be sync or async — async is required when the adapter needs to fetch
- * additional content from the platform (e.g. expanding Feishu merge_forward
- * by calling GET /messages/{id}). Existing sync implementations remain valid.
- */
- parseEvent(
- rawEvent: unknown,
- ctx: ChannelInboundContext,
- ): (ChannelParsedEvent | null) | Promise<ChannelParsedEvent | null>;
- /**
- * Download an attachment from the platform.
- * Returns the local file path of the downloaded file.
- */
- downloadAttachment?(
- event: ChannelParsedEvent,
- attachment: ChannelAttachment,
- ctx: ChannelInboundContext,
- ): Promise<string>;
- /**
- * Transcribe an audio file to text.
- * Returns the transcription text.
- */
- transcribeAudio?(audioPath: string, ctx: ChannelInboundContext): Promise<string>;
- /**
- * Fetch the content of a parent message (for reply context).
- * Returns the text content and optional sender name, or null if not available.
- */
- fetchParentMessage?(
- parentMessageId: string,
- ctx: ChannelInboundContext,
- ): Promise<{ text: string; senderName?: string } | null>;
- }
- /** Context provided to inbound adapter methods. */
- export interface ChannelInboundContext {
- /** Client instance name */
- clientName: string;
- /** Account configuration */
- account: ChannelAccountConfig;
- /** Data directory */
- dataDir: string;
- /** Bot's platform user ID (for @mention detection) */
- botId?: string;
- }
- /**
- * Outbound Adapter — message sending.
- * Handles sending messages to the channel platform.
- */
- export interface ChannelOutboundAdapter {
- /**
- * Send a text message to a chat.
- */
- sendText(ctx: ChannelOutboundContext, text: string): Promise<ChannelSendResult>;
- /**
- * Reply to a specific message (for threading/quoting).
- */
- replyTo?(
- ctx: ChannelOutboundContext,
- messageId: string,
- text: string,
- ): Promise<ChannelSendResult>;
- /**
- * Send a media message (image, file, audio, video).
- */
- sendMedia?(ctx: ChannelOutboundContext, media: ChannelMediaPayload): Promise<ChannelSendResult>;
- /** Maximum text length for a single message. */
- textLimit: number;
- /**
- * Split text into chunks respecting the platform's message length limit.
- * If not provided, ChannelPluginRunner uses a default paragraph-aware splitter.
- */
- chunkText?(text: string, limit: number): string[];
- }
- // ============================================================================
- // Optional Adapters (6)
- // ============================================================================
- /**
- * RichReply Adapter — interactive card message lifecycle.
- * Implements the Thinking -> Working -> Done card state machine.
- * Only used when capabilities.interactiveCards is true.
- */
- export interface ChannelRichReplyAdapter {
- /**
- * Send an initial queued card/message. Returns the card/message ID.
- * Used when a session already has work ahead of this inbound message.
- */
- sendQueued?(
- ctx: ChannelOutboundContext,
- userText: string,
- queueInfo: ChannelQueueInfo,
- ): Promise<string>;
- /**
- * Update an existing queued card/message to the initial thinking state.
- */
- updateThinking?(ctx: ChannelOutboundContext, cardMsgId: string, userText: string): Promise<void>;
- /**
- * Send initial "thinking" card. Returns the card message ID.
- */
- sendThinking(ctx: ChannelOutboundContext, userText: string): Promise<string>;
- /**
- * Update the card to "working" state with tool call progress.
- */
- updateWorking(
- ctx: ChannelOutboundContext,
- cardMsgId: string,
- steps: ChannelToolStep[],
- currentStep: string,
- elapsed: number,
- ): Promise<void>;
- /**
- * Update the card to "done" state with the final response.
- */
- updateDone(
- ctx: ChannelOutboundContext,
- cardMsgId: string,
- text: string,
- steps: ChannelToolStep[],
- ): Promise<void>;
- /**
- * Update the card to show an error state.
- */
- updateError?(
- ctx: ChannelOutboundContext,
- cardMsgId: string,
- error: string,
- info?: ChannelSessionErrorInfo,
- ): Promise<void>;
- /**
- * Send a standalone error card/message (no prior thinking card needed).
- * Used for delayed follow-up failures.
- */
- sendError?(
- ctx: ChannelOutboundContext,
- error: string,
- info?: ChannelSessionErrorInfo,
- ): Promise<string>;
- /**
- * Send a standalone "done" card (no prior thinking card needed).
- * Used for follow-up messages that should appear as cards, not plain text.
- */
- sendDone?(ctx: ChannelOutboundContext, text: string): Promise<string>;
- /**
- * Send a standalone "BTW" (peek / side-channel) reply card.
- * Carries a BTW tag so users can tell it apart from the main reply.
- * Optional — text-only channels fall back to a text prefix.
- */
- sendPeek?(ctx: ChannelOutboundContext, text: string): Promise<string>;
- }
- export interface ChannelSessionErrorInfo {
- message: string;
- errorCode?: number;
- actionText?: string;
- actionUrl?: string;
- }
- export interface ChannelQueueInfo {
- /** 1-based position among queued messages, excluding the currently running turn. */
- position: number;
- /** Queued messages before this item. */
- queuedAhead: number;
- /** Currently running turn ahead of this item, if any. */
- activeAhead: number;
- /** Total messages/turns ahead from the user's perspective. */
- totalAhead: number;
- }
- /**
- * Auth Adapter — read-side access to the current user token.
- *
- * The platform-specific OAuth/device-code flow that *acquires* the user
- * token lives outside this adapter — for Feishu, it is delegated to
- * `lark-cli auth login --recommend` driven by the daemon's onboard
- * route. The adapter exists so the channel-bridge runtime can read the
- * current UAT (refreshing on the fly if needed) without knowing the
- * platform-specific store layout.
- */
- export interface ChannelAuthAdapter {
- /**
- * Get the current user token, or null if not authenticated.
- */
- getUserToken(accountId: string): Promise<string | null>;
- }
- /**
- * Actions Adapter — message-level operations.
- * Handles reactions, message editing, deletion, etc.
- */
- export interface ChannelActionsAdapter {
- /**
- * Add an emoji reaction to a message.
- */
- addReaction?(ctx: ChannelOutboundContext, messageId: string, emoji: string): Promise<void>;
- /**
- * Remove an emoji reaction from a message.
- */
- removeReaction?(ctx: ChannelOutboundContext, messageId: string, emoji: string): Promise<void>;
- /**
- * Edit a previously sent message.
- */
- editMessage?(ctx: ChannelOutboundContext, messageId: string, newText: string): Promise<void>;
- /**
- * Delete a message.
- */
- deleteMessage?(ctx: ChannelOutboundContext, messageId: string): Promise<void>;
- /**
- * Start a typing/processing indicator. Returns a cleanup function.
- * Called by PluginRunner on inbound message; cleanup called in finally block.
- */
- startTypingIndicator?(messageId: string): () => void;
- }
- /**
- * Approval Adapter — permission request/approval flow.
- * Handles interactive permission cards and user approval decisions.
- */
- export interface ChannelApprovalAdapter {
- /**
- * Send a permission request card. Returns the card message ID.
- */
- sendPermissionAsk(
- ctx: ChannelOutboundContext,
- request: ChannelPermissionRequest,
- ): Promise<string>;
- /**
- * Embed permission action buttons into a "done" card payload.
- */
- embedInDoneCard?(cardPayload: unknown, requests: ChannelPermissionRequest[]): unknown;
- /**
- * Parse a raw event into a permission decision, or null if not a permission action.
- */
- parsePermissionAction(rawEvent: unknown): ChannelPermissionDecision | null;
- }
- /**
- * Result of parsing a questionnaire card-action submit event. Carries the
- * structured reply (already mapped from the card's `form_value`) plus the
- * routing context embedded in the submit button's `value` so the handler can
- * dispatch the reply to the right session without an extra store lookup.
- */
- export interface ChannelQuestionnaireSubmit {
- requestId: string;
- sessionId?: string;
- reply: AskQuestionnaireReplyPayload;
- }
- /**
- * Questionnaire Adapter — interactive `ask_user` (V2 questionnaire) flow for
- * card-capable channels (Feishu). Mirrors {@link ChannelApprovalAdapter}:
- * card-capable channels implement it to render a form card and parse the
- * submit callback; text-only channels (WeChat) omit it and the handler falls
- * back to a numbered-text round-trip via the outbound adapter.
- */
- export interface ChannelQuestionnaireAdapter {
- /**
- * Send a questionnaire form card. Returns the card message ID (empty on
- * failure so the handler can retry).
- */
- sendQuestionnaireAsk(
- ctx: ChannelOutboundContext,
- request: AskQuestionnaireRequest,
- ): Promise<string>;
- /**
- * Parse a raw card-action submit event into a structured reply, or null
- * when the event is not a questionnaire submit.
- */
- parseQuestionnaireSubmit(rawEvent: unknown): ChannelQuestionnaireSubmit | null;
- /**
- * Best-effort: update the card to a "submitted" state once the answers
- * have been forwarded to the agent.
- */
- updateCardAsSubmitted?(messageId: string, request: AskQuestionnaireRequest): Promise<void>;
- }
- /**
- * Threading Adapter — thread/topic management.
- * Handles platform-specific thread semantics.
- */
- export interface ChannelThreadingAdapter {
- /**
- * Resolve the thread ID for a reply.
- * Different platforms have different threading models.
- */
- resolveThreadId?(event: ChannelParsedEvent): string | undefined;
- /**
- * Create a new thread from a message.
- */
- createThread?(ctx: ChannelOutboundContext, messageId: string, title?: string): Promise<string>;
- /**
- * Send a message into an existing thread.
- */
- sendToThread?(
- ctx: ChannelOutboundContext,
- threadId: string,
- text: string,
- ): Promise<ChannelSendResult>;
- }
- /**
- * Directory Adapter — user/group directory queries.
- * Provides lookup capabilities for platform users and groups.
- */
- export interface ChannelDirectoryAdapter {
- /**
- * Get the bot's own identity on the platform.
- */
- self?(ctx: ChannelInboundContext): Promise<ChannelDirectoryEntry | null>;
- /**
- * List available peer users (DM targets).
- */
- listPeers?(ctx: ChannelInboundContext): Promise<ChannelDirectoryEntry[]>;
- /**
- * List available groups/channels.
- */
- listGroups?(ctx: ChannelInboundContext): Promise<ChannelDirectoryEntry[]>;
- /**
- * List members of a specific group.
- */
- listGroupMembers?(ctx: ChannelInboundContext, groupId: string): Promise<ChannelDirectoryEntry[]>;
- }
- /** Directory entry representing a user or group. */
- export interface ChannelDirectoryEntry {
- /** Platform user/group ID */
- id: string;
- /** Display name */
- name: string;
- /** Entry type */
- type: 'user' | 'group' | 'bot';
- /** Avatar URL */
- avatarUrl?: string;
- }
- // ============================================================================
- // Plugin Main Contract
- // ============================================================================
- /**
- * ChannelPlugin — the main plugin interface for a channel platform.
- *
- * Each channel platform (Feishu, Telegram, Slack, etc.) implements this
- * interface with 4 required adapters and any applicable optional adapters.
- *
- * ChannelPluginRunner consumes this interface to drive message dispatch,
- * using capabilities + adapter presence to determine behavior paths.
- * No platform-specific logic exists in the Runner.
- */
- export interface ChannelPlugin {
- /** Platform identifier (unique across all plugins) */
- readonly id: ChannelPlatform;
- /** Plugin metadata for display */
- readonly meta: ChannelMeta;
- /** Static capability declaration */
- readonly capabilities: ChannelCapabilities;
- // --- Required Adapters ---
- /** Account configuration management */
- readonly config: ChannelConfigAdapter;
- /** Connection lifecycle (WebSocket, polling, etc.) */
- readonly gateway: ChannelGatewayAdapter;
- /** Incoming event parsing */
- readonly inbound: ChannelInboundAdapter;
- /** Message sending */
- readonly outbound: ChannelOutboundAdapter;
- // --- Optional Adapters ---
- /** Interactive card message lifecycle (Thinking -> Working -> Done) */
- readonly richReply?: ChannelRichReplyAdapter;
- /** User authentication flow */
- readonly auth?: ChannelAuthAdapter;
- /** Message-level operations (reactions, edit, delete) */
- readonly actions?: ChannelActionsAdapter;
- /** Permission request/approval flow */
- readonly approval?: ChannelApprovalAdapter;
- /** Interactive questionnaire (ask_user / V2) flow for card-capable channels */
- readonly questionnaire?: ChannelQuestionnaireAdapter;
- /** Thread/topic management */
- readonly threading?: ChannelThreadingAdapter;
- /** User/group directory queries */
- readonly directory?: ChannelDirectoryAdapter;
- }
|