n8n
Version:
n8n Workflow Automation Tool
152 lines (151 loc) • 8.08 kB
TypeScript
import type { CredentialProvider, StreamChunk, ToolDescriptor } from '@n8n/agents';
import { type AgentSkill, type AgentSkillMutationResponse, type ChatIntegrationDescriptor } from '@n8n/api-types';
import * as agents from '@n8n/agents';
import { Logger } from '@n8n/backend-common';
import { AgentsConfig, GlobalConfig } from '@n8n/config';
import { ExecutionRepository, ProjectRelationRepository, UserRepository, WorkflowRepository } from '@n8n/db';
import { type ExecuteAgentData } from 'n8n-workflow';
import { ActiveExecutions } from '../../active-executions';
import { EphemeralNodeExecutor } from '../../node-execution';
import type { PubSubCommandMap } from '../../scaling/pubsub/pubsub.event-map';
import { Publisher } from '../../scaling/pubsub/publisher.service';
import { UrlService } from '../../services/url.service';
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 type { AgentJsonConfig } from './json-config/agent-json-config';
import { AgentPublishedVersionRepository } from './repositories/agent-published-version.repository';
import { AgentRepository } from './repositories/agent.repository';
import { AgentSecureRuntime } from './runtime/agent-secure-runtime';
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 ExecuteForSchedulePublishedConfig {
agentId: string;
projectId: string;
message: string;
memory: AgentMemoryScope;
}
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 agentPublishedVersionRepository;
private readonly agentSkillsService;
private readonly publisher;
private readonly agentsConfig;
private readonly globalConfig;
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, agentPublishedVersionRepository: AgentPublishedVersionRepository, agentSkillsService: AgentSkillsService, publisher: Publisher, agentsConfig: AgentsConfig, globalConfig: GlobalConfig);
private isNodeToolsModuleEnabled;
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, userId: string): Promise<Agent>;
unpublishAgent(agentId: string, projectId: string): Promise<Agent>;
revertToPublishedAgent(agentId: string, projectId: string): Promise<Agent>;
delete(agentId: string, projectId: string): Promise<boolean>;
getChatMessages(threadId: string): Promise<agents.AgentDbMessage[]>;
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[];
}>;
executeForChat(config: ExecuteForChatConfig): AsyncGenerator<StreamChunk>;
getTestChatMessages(agentId: string, userId: string): Promise<agents.AgentDbMessage[]>;
clearTestChatMessages(agentId: string, userId: string): Promise<void>;
clearAllTestChatMessages(agentId: string): Promise<void>;
executeForChatPublished(config: ExecuteForChatPublishedConfig): AsyncGenerator<StreamChunk>;
executeForSchedulePublished(config: ExecuteForSchedulePublishedConfig): AsyncGenerator<StreamChunk>;
private streamChatResponse;
private compileIsolated;
executeForWorkflow(agentId: string, message: string, executionId: string, threadId: string, userId: string, projectId: string): Promise<ExecuteAgentData>;
getConfig(agentId: string, projectId: string): Promise<AgentJsonConfig>;
validateConfig(raw: unknown): Promise<{
valid: true;
config: AgentJsonConfig;
} | {
valid: false;
error: string;
}>;
private validateNodeToolExpressions;
private healIntegrationCredentialNames;
updateConfig(agentId: string, projectId: string, config: unknown): Promise<{
config: AgentJsonConfig;
updatedAt: string;
versionId: string | null;
}>;
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 reconstructFromConfig;
}