import { createScopedAccountReplyToModeResolver, createTopLevelChannelReplyToModeResolver, } from "../channels/plugins/threading-helpers.js"; import type { ChannelOutboundAdapter, ChannelPairingAdapter, ChannelSecurityAdapter, } from "../channels/plugins/types.adapters.js"; import type { ChannelMessagingAdapter, ChannelOutboundSessionRoute, ChannelThreadingAdapter, } from "../channels/plugins/types.core.js"; import type { ChannelPlugin } from "../channels/plugins/types.plugin.js"; import { getChatChannelMeta } from "../channels/registry.js"; import type { OpenClawConfig } from "../config/config.js"; import type { ReplyToMode } from "../config/types.base.js"; import { buildOutboundBaseSessionKey } from "../infra/outbound/base-session-key.js"; import { emptyPluginConfigSchema } from "../plugins/config-schema.js"; import type { PluginRuntime } from "../plugins/runtime/types.js"; import type { OpenClawPluginApi, OpenClawPluginConfigSchema } from "../plugins/types.js"; import { createScopedDmSecurityResolver } from "./channel-config-helpers.js"; import { createTextPairingAdapter } from "./channel-pairing.js"; import { createAttachedChannelResultAdapter } from "./channel-send-result.js"; import { definePluginEntry } from "./plugin-entry.js"; export type { AnyAgentTool, MediaUnderstandingProviderPlugin, OpenClawPluginConfigSchema, ProviderDiscoveryContext, ProviderCatalogContext, ProviderCatalogResult, ProviderAugmentModelCatalogContext, ProviderBuiltInModelSuppressionContext, ProviderBuiltInModelSuppressionResult, ProviderBuildMissingAuthMessageContext, ProviderCacheTtlEligibilityContext, ProviderDefaultThinkingPolicyContext, ProviderFetchUsageSnapshotContext, ProviderModernModelPolicyContext, ProviderPreparedRuntimeAuth, ProviderResolvedUsageAuth, ProviderPrepareExtraParamsContext, ProviderPrepareDynamicModelContext, ProviderPrepareRuntimeAuthContext, ProviderResolveUsageAuthContext, ProviderResolveDynamicModelContext, ProviderNormalizeResolvedModelContext, ProviderRuntimeModel, SpeechProviderPlugin, ProviderThinkingPolicyContext, ProviderWrapStreamFnContext, OpenClawPluginService, OpenClawPluginServiceContext, ProviderAuthContext, ProviderAuthDoctorHintContext, ProviderAuthMethodNonInteractiveContext, ProviderAuthMethod, ProviderAuthResult, OpenClawPluginToolContext, OpenClawPluginToolFactory, OpenClawPluginCommandDefinition, OpenClawPluginDefinition, PluginCommandContext, PluginLogger, PluginInteractiveTelegramHandlerContext, } from "../plugins/types.js"; export type { OpenClawConfig } from "../config/config.js"; export { isSecretRef } from "../config/types.secrets.js"; export type { GatewayRequestHandlerOptions } from "../gateway/server-methods/types.js"; export type { ChannelOutboundSessionRoute, ChannelMessagingAdapter, } from "../channels/plugins/types.core.js"; export type { ProviderUsageSnapshot, UsageProviderId, UsageWindow, } from "../infra/provider-usage.types.js"; export type { ChannelMessageActionContext } from "../channels/plugins/types.js"; export type { ChannelPlugin } from "../channels/plugins/types.plugin.js"; export type { OpenClawPluginApi } from "../plugins/types.js"; export type { PluginRuntime } from "../plugins/runtime/types.js"; export { emptyPluginConfigSchema } from "../plugins/config-schema.js"; export { definePluginEntry } from "./plugin-entry.js"; export { delegateCompactionToRuntime } from "../context-engine/delegate.js"; export { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../routing/session-key.js"; export { buildChannelConfigSchema } from "../channels/plugins/config-schema.js"; export { applyAccountNameToChannelSection, migrateBaseNameToDefaultAccount, } from "../channels/plugins/setup-helpers.js"; export { clearAccountEntryFields, deleteAccountFromConfigSection, setAccountEnabledInConfigSection, } from "../channels/plugins/config-helpers.js"; export { formatPairingApproveHint, parseOptionalDelimitedEntries, } from "../channels/plugins/helpers.js"; export { getChatChannelMeta } from "../channels/registry.js"; export { channelTargetSchema, channelTargetsSchema, optionalStringEnum, stringEnum, } from "../agents/schema/typebox.js"; export { DEFAULT_SECRET_FILE_MAX_BYTES, loadSecretFileSync, readSecretFileSync, tryReadSecretFileSync, } from "../infra/secret-file.js"; export type { SecretFileReadOptions, SecretFileReadResult } from "../infra/secret-file.js"; export { resolveGatewayBindUrl } from "../shared/gateway-bind-url.js"; export type { GatewayBindUrlResult } from "../shared/gateway-bind-url.js"; export { normalizeAtHashSlug, normalizeHyphenSlug } from "../shared/string-normalization.js"; export { resolveTailnetHostWithRunner } from "../shared/tailscale-status.js"; export type { TailscaleStatusCommandResult, TailscaleStatusCommandRunner, } from "../shared/tailscale-status.js"; export { buildAgentSessionKey, type RoutePeer, type RoutePeerKind, } from "../routing/resolve-route.js"; export { resolveThreadSessionKeys } from "../routing/session-key.js"; export type ChannelOutboundSessionRouteParams = Parameters< NonNullable >[0]; export function stripChannelTargetPrefix(raw: string, ...providers: string[]): string { const trimmed = raw.trim(); for (const provider of providers) { const prefix = `${provider.toLowerCase()}:`; if (trimmed.toLowerCase().startsWith(prefix)) { return trimmed.slice(prefix.length).trim(); } } return trimmed; } export function stripTargetKindPrefix(raw: string): string { return raw.replace(/^(user|channel|group|conversation|room|dm):/i, "").trim(); } export function buildChannelOutboundSessionRoute(params: { cfg: OpenClawConfig; agentId: string; channel: string; accountId?: string | null; peer: { kind: "direct" | "group" | "channel"; id: string }; chatType: "direct" | "group" | "channel"; from: string; to: string; threadId?: string | number; }): ChannelOutboundSessionRoute { const baseSessionKey = buildOutboundBaseSessionKey({ cfg: params.cfg, agentId: params.agentId, channel: params.channel, accountId: params.accountId, peer: params.peer, }); return { sessionKey: baseSessionKey, baseSessionKey, peer: params.peer, chatType: params.chatType, from: params.from, to: params.to, ...(params.threadId !== undefined ? { threadId: params.threadId } : {}), }; } type DefineChannelPluginEntryOptions = { id: string; name: string; description: string; plugin: TPlugin; configSchema?: OpenClawPluginConfigSchema | (() => OpenClawPluginConfigSchema); setRuntime?: (runtime: PluginRuntime) => void; registerFull?: (api: OpenClawPluginApi) => void; }; type CreateChannelPluginBaseOptions = { id: ChannelPlugin["id"]; meta?: Partial["meta"]>>; setupWizard?: NonNullable["setupWizard"]>; capabilities?: ChannelPlugin["capabilities"]; agentPrompt?: ChannelPlugin["agentPrompt"]; streaming?: ChannelPlugin["streaming"]; reload?: ChannelPlugin["reload"]; gatewayMethods?: ChannelPlugin["gatewayMethods"]; configSchema?: ChannelPlugin["configSchema"]; config?: ChannelPlugin["config"]; security?: ChannelPlugin["security"]; setup: NonNullable["setup"]>; groups?: ChannelPlugin["groups"]; }; type CreatedChannelPluginBase = Pick< ChannelPlugin, "id" | "meta" | "setup" > & Partial< Pick< ChannelPlugin, | "setupWizard" | "capabilities" | "agentPrompt" | "streaming" | "reload" | "gatewayMethods" | "configSchema" | "config" | "security" | "groups" > >; // Shared channel-plugin entry boilerplate for bundled and third-party channels. export function defineChannelPluginEntry({ id, name, description, plugin, configSchema = emptyPluginConfigSchema, setRuntime, registerFull, }: DefineChannelPluginEntryOptions) { return definePluginEntry({ id, name, description, configSchema, register(api: OpenClawPluginApi) { setRuntime?.(api.runtime); api.registerChannel({ plugin }); if (api.registrationMode !== "full") { return; } registerFull?.(api); }, }); } // Shared setup-entry shape so bundled channels do not duplicate `{ plugin }`. export function defineSetupPluginEntry(plugin: TPlugin) { return { plugin }; } type ChatChannelPluginBase = Omit< ChannelPlugin, "security" | "pairing" | "threading" | "outbound" > & Partial< Pick< ChannelPlugin, "security" | "pairing" | "threading" | "outbound" > >; type ChatChannelSecurityOptions = { dm: { channelKey: string; resolvePolicy: (account: TResolvedAccount) => string | null | undefined; resolveAllowFrom: (account: TResolvedAccount) => Array | null | undefined; resolveFallbackAccountId?: (account: TResolvedAccount) => string | null | undefined; defaultPolicy?: string; allowFromPathSuffix?: string; policyPathSuffix?: string; approveChannelId?: string; approveHint?: string; normalizeEntry?: (raw: string) => string; }; collectWarnings?: ChannelSecurityAdapter["collectWarnings"]; }; type ChatChannelPairingOptions = { text: { idLabel: string; message: string; normalizeAllowEntry?: ChannelPairingAdapter["normalizeAllowEntry"]; notify: Parameters[0]["notify"]; }; }; type ChatChannelThreadingReplyModeOptions = | { topLevelReplyToMode: string } | { scopedAccountReplyToMode: { resolveAccount: (cfg: OpenClawConfig, accountId?: string | null) => TResolvedAccount; resolveReplyToMode: ( account: TResolvedAccount, chatType?: string | null, ) => ReplyToMode | null | undefined; fallback?: ReplyToMode; }; } | { resolveReplyToMode: NonNullable; }; type ChatChannelThreadingOptions = ChatChannelThreadingReplyModeOptions & Omit; type ChatChannelAttachedOutboundOptions = { base: Omit; attachedResults: Parameters[0]; }; function resolveChatChannelSecurity( security: | ChannelSecurityAdapter | ChatChannelSecurityOptions | undefined, ): ChannelSecurityAdapter | undefined { if (!security) { return undefined; } if (!("dm" in security)) { return security; } return { resolveDmPolicy: createScopedDmSecurityResolver(security.dm), ...(security.collectWarnings ? { collectWarnings: security.collectWarnings } : {}), }; } function resolveChatChannelPairing( pairing: ChannelPairingAdapter | ChatChannelPairingOptions | undefined, ): ChannelPairingAdapter | undefined { if (!pairing) { return undefined; } if (!("text" in pairing)) { return pairing; } return createTextPairingAdapter(pairing.text); } function resolveChatChannelThreading( threading: ChannelThreadingAdapter | ChatChannelThreadingOptions | undefined, ): ChannelThreadingAdapter | undefined { if (!threading) { return undefined; } if (!("topLevelReplyToMode" in threading) && !("scopedAccountReplyToMode" in threading)) { return threading; } let resolveReplyToMode: ChannelThreadingAdapter["resolveReplyToMode"]; if ("topLevelReplyToMode" in threading) { resolveReplyToMode = createTopLevelChannelReplyToModeResolver(threading.topLevelReplyToMode); } else { resolveReplyToMode = createScopedAccountReplyToModeResolver( threading.scopedAccountReplyToMode, ); } return { ...threading, resolveReplyToMode, }; } function resolveChatChannelOutbound( outbound: ChannelOutboundAdapter | ChatChannelAttachedOutboundOptions | undefined, ): ChannelOutboundAdapter | undefined { if (!outbound) { return undefined; } if (!("attachedResults" in outbound)) { return outbound; } return { ...outbound.base, ...createAttachedChannelResultAdapter(outbound.attachedResults), }; } // Shared higher-level builder for chat-style channels that mostly compose // scoped DM security, text pairing, reply threading, and attached send results. export function createChatChannelPlugin< TResolvedAccount extends { accountId?: string | null }, Probe = unknown, Audit = unknown, >(params: { base: ChatChannelPluginBase; security?: | ChannelSecurityAdapter | ChatChannelSecurityOptions; pairing?: ChannelPairingAdapter | ChatChannelPairingOptions; threading?: ChannelThreadingAdapter | ChatChannelThreadingOptions; outbound?: ChannelOutboundAdapter | ChatChannelAttachedOutboundOptions; }): ChannelPlugin { return { ...params.base, ...(params.security ? { security: resolveChatChannelSecurity(params.security) } : {}), ...(params.pairing ? { pairing: resolveChatChannelPairing(params.pairing) } : {}), ...(params.threading ? { threading: resolveChatChannelThreading(params.threading) } : {}), ...(params.outbound ? { outbound: resolveChatChannelOutbound(params.outbound) } : {}), } as ChannelPlugin; } // Shared base object for channel plugins that only need to override a few optional surfaces. export function createChannelPluginBase( params: CreateChannelPluginBaseOptions, ): CreatedChannelPluginBase { return { id: params.id, meta: { ...getChatChannelMeta(params.id as Parameters[0]), ...params.meta, }, ...(params.setupWizard ? { setupWizard: params.setupWizard } : {}), ...(params.capabilities ? { capabilities: params.capabilities } : {}), ...(params.agentPrompt ? { agentPrompt: params.agentPrompt } : {}), ...(params.streaming ? { streaming: params.streaming } : {}), ...(params.reload ? { reload: params.reload } : {}), ...(params.gatewayMethods ? { gatewayMethods: params.gatewayMethods } : {}), ...(params.configSchema ? { configSchema: params.configSchema } : {}), ...(params.config ? { config: params.config } : {}), ...(params.security ? { security: params.security } : {}), ...(params.groups ? { groups: params.groups } : {}), setup: params.setup, } as CreatedChannelPluginBase; }