import { AgentIntegrationConfig, type AgentIntegrationSettings } from '@n8n/api-types'; import { Logger } from '@n8n/backend-common'; import { GlobalConfig } from '@n8n/config'; import { type PubSubCommandMap } from '@n8n/decorators'; import type { Channel, Thread, UserInfo } from 'chat'; import { InstanceSettings } from 'n8n-core'; import { CredentialsService } from '../../../credentials/credentials.service'; import { UrlService } from '@n8n/backend-services'; import { AgentChatBridge } from './agent-chat-bridge'; import { ChatIntegrationRegistry } from './agent-chat-integration'; import { AgentChatSubscriptionStateService } from './agent-chat-subscription-state.service'; import type { CallbackMetadata } from './callback-store'; import { type ShortenCallback } from './component-mapper'; import { LeaderChannelRelayService } from './leader-channel-relay.service'; import { type ProjectAgent } from '../entities/agent.entity'; import { AgentChangePublisher } from '../agent-change-publisher.service'; import { type AgentChannelRef, type IntegrationRef } from '../utils/agent-channel'; import { AgentRepository } from '../repositories/agent.repository'; import { AgentChannelStatusReporter } from './agent-channel-status-reporter'; type WebhookHandler = (request: Request, options?: { waitUntil?: (task: Promise) => void; }) => Promise; export interface ChatInstance { initialize(): Promise; shutdown(): Promise; webhooks: Record; onNewMention: (handler: unknown) => void; onSubscribedMessage: (handler: unknown) => void; onAction: (handler: unknown) => void; getAdapter(name: string): unknown; openDM(user: string): Promise; thread(threadId: string): Thread; channel(channelId: string): Channel; getUser(user: string): Promise; } interface ConnectOptions { ingressEnabled?: boolean; skipExternalHooks?: boolean; settings?: AgentIntegrationSettings; } interface DisconnectOptions { skipExternalHooks?: boolean; } interface DisconnectChannelOptions { deleteSubscriptions?: boolean; } export declare class ChatIntegrationService { private readonly logger; private readonly agentRepository; private readonly credentialsService; private readonly urlService; private readonly integrationRegistry; private readonly instanceSettings; private readonly changePublisher; private readonly globalConfig; private readonly chatSubscriptionStateService; private readonly statusReporter; private readonly leaderChannelRelay; private readonly connections; private readonly outboundConnections; private readonly outboundConnectionInitializations; private readonly leaderOperations; private shuttingDown; constructor(logger: Logger, agentRepository: AgentRepository, credentialsService: CredentialsService, urlService: UrlService, integrationRegistry: ChatIntegrationRegistry, instanceSettings: InstanceSettings, changePublisher: AgentChangePublisher, globalConfig: GlobalConfig, chatSubscriptionStateService: AgentChatSubscriptionStateService, statusReporter: AgentChannelStatusReporter, leaderChannelRelay: LeaderChannelRelayService); broadcastIntegrationChange(agentId: string, integration: AgentIntegrationConfig, action: 'connect' | 'disconnect'): Promise; private integrationFromConnectionKey; assertStartupPreconditions(agentId: string, integration: AgentIntegrationConfig, projectId: string): Promise; validateBeforeConnect(agentId: string, integration: AgentIntegrationConfig, projectId: string): Promise; connect(agentId: string, integration: AgentIntegrationConfig, projectId: string, options?: ConnectOptions): Promise; private connectLocal; private establishConnection; disconnect(agentId: string, integration?: IntegrationRef, options?: DisconnectOptions): Promise; releaseChannelLocally(agentId: string, integration: IntegrationRef): Promise; disconnectChannel(agentId: string, integration: AgentIntegrationConfig, options?: DisconnectChannelOptions): Promise; disconnectAll(): Promise; private localRuntimeKeys; disconnectLeaderOnlyIntegrations(): Promise; syncToConfig(agent: ProjectAgent, previous: AgentIntegrationConfig[], next: AgentIntegrationConfig[]): Promise; getChatInstance(agentId: string, integration?: IntegrationRef): ChatInstance | undefined; getBridge(agentId: string, integrationType: string, credentialId?: string): AgentChatBridge | undefined; acquireQueueBridge(agentId: string, integrationType: string, credentialId: string): { bridge: AgentChatBridge; release: () => void; } | undefined; private findConnection; isChannelLive(agentId: string, integration: IntegrationRef): boolean; getChatInstanceForTools(agentId: string, integration: IntegrationRef): Promise; getShortenCallback(agentId: string, integration: IntegrationRef, metadata?: CallbackMetadata): ShortenCallback | undefined; getWebhookHandler(agentId: string, platform: string, connectionSelector?: string): WebhookHandler | undefined; listLiveChannels(): AgentChannelRef[]; hasLiveChannel(ref: AgentChannelRef): boolean; startChannel(agent: ProjectAgent, integration: AgentIntegrationConfig): Promise; handleIntegrationChanged(payload: PubSubCommandMap['agent-chat-integration-changed']): Promise; handleLeaderChannelRequest(payload: PubSubCommandMap['agent-chat-leader-channel-request']): Promise; private shouldRouteToLeader; private runLeaderOperation; private settleLeaderOperations; private disconnectLocal; private disconnectOutboundOne; private disposeOutboundConnection; private disconnectOne; private runDisconnectedHook; private decryptCredentialForProject; private buildWebhookUrl; private connectOptionsFor; private createConnectionContext; private initializeConnection; private createChatBridge; private cleanupFailedConnection; private runConnectedHook; private reportOutboundError; private initializeOutboundConnection; private applyLeaderChannelRequest; private runBeforeDisconnectHook; } export {};