UNPKG

n8n

Version:

n8n Workflow Automation Tool

197 lines (196 loc) • 10.5 kB
import type { CredentialProvider, StreamChunk, ToolDescriptor } from '@n8n/agents'; import { type AgentIntegrationConfig, type AgentJsonConfig, type AgentSkill, type AgentSkillMutationResponse, type AgentVersionListItemDto, type ChatIntegrationDescriptor, AgentPersistedMessageDto } from '@n8n/api-types'; import { Logger } from '@n8n/backend-common'; import { AgentsConfig, GlobalConfig } from '@n8n/config'; import { ExecutionRepository, ProjectRelationRepository, User, UserRepository, WorkflowRepository } from '@n8n/db'; import { type ExecuteAgentData } from 'n8n-workflow'; import { ActiveExecutions } from '../../active-executions'; import { EphemeralNodeExecutor } from '../../node-execution'; import { OauthService } from '../../oauth/oauth.service'; import type { PubSubCommandMap } from '../../scaling/pubsub/pubsub.event-map'; import { Publisher } from '../../scaling/pubsub/publisher.service'; import { UrlService } from '../../services/url.service'; import { Telemetry } from '../../telemetry'; import { WorkflowRunner } from '../../workflow-runner'; import { WorkflowFinderService } from '../../workflows/workflow-finder.service'; import { AgentExecutionService } from './agent-execution.service'; import { AgentSkillsService } from './agent-skills.service'; import { AgentsToolsService } from './agents-tools.service'; import { Agent } from './entities/agent.entity'; import { N8NCheckpointStorage } from './integrations/n8n-checkpoint-storage'; import { N8nMemory } from './integrations/n8n-memory'; import { AgentHistoryRepository } from './repositories/agent-history.repository'; import { AgentTaskSnapshotRepository } from './repositories/agent-task-snapshot.repository'; import { AgentTaskRepository } from './repositories/agent-task.repository'; import { AgentRepository } from './repositories/agent.repository'; import { AgentSecureRuntime } from './runtime/agent-secure-runtime'; import { ChatIntegrationService } from './integrations/chat-integration.service'; import { AgentKnowledgeCommandService } from './agent-knowledge-command.service'; import { AgentKnowledgeService } from './agent-knowledge.service'; export declare function chatThreadId(agentId: string, userId?: string): string; export interface AgentMemoryScope { threadId: string; resourceId: string; } export interface ExecuteForChatConfig { agentId: string; projectId: string; message: string; userId: string; memory: AgentMemoryScope; } export interface ExecuteForChatPublishedConfig { agentId: string; projectId: string; message: string; memory: AgentMemoryScope; integrationType?: string; } export interface ResumeForChatConfig { agentId: string; projectId: string; runId: string; toolCallId: string; resumeData: unknown; integrationType?: string; } export interface ExecuteForTaskPublishedConfig { agentId: string; projectId: string; message: string; memory: AgentMemoryScope; taskId: string; taskVersionId: string; } export interface ExecuteForTaskNowConfig { agentId: string; projectId: string; userId: string; message: string; memory: AgentMemoryScope; taskId: string; } interface PublishAgentOptions { syncIntegrations?: boolean; } interface SaveCredentialIntegrationOptions { broadcast?: boolean; } export declare class AgentsService { private readonly logger; private readonly agentRepository; private readonly projectRelationRepository; private readonly workflowRunner; private readonly activeExecutions; private readonly executionRepository; private readonly workflowRepository; private readonly userRepository; private readonly workflowFinderService; private readonly urlService; private readonly n8nCheckpointStorage; private readonly secureRuntime; private readonly ephemeralNodeExecutor; private readonly agentsToolsService; private readonly n8nMemory; private readonly agentExecutionService; private readonly agentHistoryRepository; private readonly agentSkillsService; private readonly agentTaskRepository; private readonly agentTaskSnapshotRepository; private readonly publisher; private readonly agentsConfig; private readonly globalConfig; private readonly telemetry; private readonly chatIntegrationService; private readonly agentKnowledgeService; private readonly agentKnowledgeCommandService; private readonly oauthService; private readonly runtimes; private computeRuntimeCacheKey; private clearRuntimes; handleAgentConfigChanged(payload: PubSubCommandMap['agent-config-changed']): void; constructor(logger: Logger, agentRepository: AgentRepository, projectRelationRepository: ProjectRelationRepository, workflowRunner: WorkflowRunner, activeExecutions: ActiveExecutions, executionRepository: ExecutionRepository, workflowRepository: WorkflowRepository, userRepository: UserRepository, workflowFinderService: WorkflowFinderService, urlService: UrlService, n8nCheckpointStorage: N8NCheckpointStorage, secureRuntime: AgentSecureRuntime, ephemeralNodeExecutor: EphemeralNodeExecutor, agentsToolsService: AgentsToolsService, n8nMemory: N8nMemory, agentExecutionService: AgentExecutionService, agentHistoryRepository: AgentHistoryRepository, agentSkillsService: AgentSkillsService, agentTaskRepository: AgentTaskRepository, agentTaskSnapshotRepository: AgentTaskSnapshotRepository, publisher: Publisher, agentsConfig: AgentsConfig, globalConfig: GlobalConfig, telemetry: Telemetry, chatIntegrationService: ChatIntegrationService, agentKnowledgeService: AgentKnowledgeService, agentKnowledgeCommandService: AgentKnowledgeCommandService, oauthService: OauthService); private isNodeToolsModuleEnabled; isKnowledgeBaseModuleEnabled(): boolean; private closeAgentResources; private createAgentExecutionCounter; private shouldAttachNodeTools; listChatIntegrations(): ChatIntegrationDescriptor[]; create(projectId: string, name: string): Promise<Agent>; findByProjectId(projectId: string): Promise<Agent[]>; findById(agentId: string, projectId: string): Promise<Agent | null>; updateName(agentId: string, projectId: string, name: string): Promise<Agent | null>; updateDescription(agentId: string, projectId: string, description: string, updatedAt?: string): Promise<Agent | null>; findByUser(userId: string): Promise<Agent[]>; findPublishedByUser(userId: string): Promise<Agent[]>; publishAgent(agentId: string, projectId: string, user: User, versionId?: string, options?: PublishAgentOptions): Promise<Agent>; unpublishAgent(agentId: string, projectId: string): Promise<Agent>; revertToPublishedAgent(agentId: string, projectId: string): Promise<Agent>; revertToVersion(agentId: string, projectId: string, versionId: string): Promise<Agent>; hasPublishHistory(agentId: string): Promise<boolean>; listPublishHistory(agentId: string, projectId: string, take: number, skip: number): Promise<AgentVersionListItemDto[]>; delete(agentId: string, projectId: string): Promise<boolean>; getConversationHistory(params: { threadId: string; projectId: string; agentId: string; }): Promise<AgentPersistedMessageDto[] | null>; private getMemoryFactory; private createCredentialProvider; private getRuntime; private makeToolResolver; private injectRuntimeDependencies; private attachNodeToolChain; resumeForChat(config: ResumeForChatConfig): AsyncGenerator<StreamChunk>; validateAgentIsRunnable(agentId: string, projectId: string, credentialProvider: CredentialProvider): Promise<{ missing: string[]; }>; private validateMemoryWorkerModel; private workerCredentialSupportsModel; executeForChat(config: ExecuteForChatConfig): AsyncGenerator<StreamChunk>; getTestChatMessages(agentId: string, userId: string): Promise<import("@n8n/agents").AgentDbMessage[]>; clearTestChatMessages(agentId: string, userId: string): Promise<void>; clearAllTestChatMessages(agentId: string): Promise<void>; executeForChatPublished(config: ExecuteForChatPublishedConfig): AsyncGenerator<StreamChunk>; executeForTaskPublished(config: ExecuteForTaskPublishedConfig): AsyncGenerator<StreamChunk>; executeForTaskNow(config: ExecuteForTaskNowConfig): AsyncGenerator<StreamChunk>; private streamChatResponse; private compileIsolated; executeForWorkflow(agentId: string, message: string, executionId: string, threadId: string, userId: string, projectId: string, telemetryUserId?: string): Promise<ExecuteAgentData>; getConfig(agentId: string, projectId: string): Promise<AgentJsonConfig>; validateConfig(raw: unknown): Promise<{ valid: true; config: AgentJsonConfig; } | { valid: false; error: string; }>; private validateNodeToolExpressions; updateConfig(agentId: string, projectId: string, config: unknown): Promise<{ config: AgentJsonConfig; updatedAt: string; versionId: string | null; }>; saveCredentialIntegration(agent: Agent, integration: AgentIntegrationConfig, options?: SaveCredentialIntegrationOptions): Promise<Agent>; removeCredentialIntegration(agent: Agent, type: string, credentialId: string): Promise<Agent>; buildCustomTool(agentId: string, projectId: string, code: string, descriptor: ToolDescriptor): Promise<{ ok: boolean; id: string; descriptor: ToolDescriptor; }>; listSkills(agentId: string, projectId: string): Promise<Record<string, AgentSkill>>; getSkill(agentId: string, projectId: string, skillId: string): Promise<AgentSkill>; createSkill(agentId: string, projectId: string, skill: AgentSkill): Promise<AgentSkillMutationResponse>; createAndAttachSkill(agentId: string, projectId: string, skill: AgentSkill): Promise<AgentSkillMutationResponse>; updateSkill(agentId: string, projectId: string, skillId: string, updates: Partial<AgentSkill>): Promise<AgentSkillMutationResponse>; deleteCustomTool(agentId: string, projectId: string, toolId: string): Promise<void>; deleteSkill(agentId: string, projectId: string, skillId: string): Promise<void>; private validateNodeToolConfigs; private validateConfigRefs; private getMissingCustomToolIds; private snapshotConfiguredTools; private snapshotConfiguredTasks; private restoreTasksFromSnapshot; private reconstructFromConfig; } export {};