UNPKG

n8n

Version:

n8n Workflow Automation Tool

129 lines • 6.22 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.IntegrationMessageContextService = void 0; exports.isIntegrationMessageContext = isIntegrationMessageContext; const di_1 = require("@n8n/di"); const n8n_workflow_1 = require("n8n-workflow"); const agent_resource_repository_1 = require("../repositories/agent-resource.repository"); const agent_thread_repository_1 = require("../repositories/agent-thread.repository"); const MESSAGE_CONTEXT_METADATA_KEY = 'currentMessageContext'; let IntegrationMessageContextService = class IntegrationMessageContextService { constructor(threadRepository, resourceRepository) { this.threadRepository = threadRepository; this.resourceRepository = resourceRepository; } async getLatest(threadId) { const thread = await this.threadRepository.findOneBy({ id: threadId }); const value = this.parseMetadata(thread?.metadata)[MESSAGE_CONTEXT_METADATA_KEY]; return isIntegrationMessageContext(value) ? value : null; } async setLatest(threadId, resourceId, context) { const existing = await this.threadRepository.findOneBy({ id: threadId }); const metadata = { ...this.parseMetadata(existing?.metadata), [MESSAGE_CONTEXT_METADATA_KEY]: context, }; if (existing) { existing.metadata = JSON.stringify(metadata); await this.threadRepository.save(existing); return; } await this.ensureResource(resourceId); await this.threadRepository.save(this.threadRepository.create({ id: threadId, resourceId, title: null, metadata: JSON.stringify(metadata), })); } async ensureResource(resourceId) { const exists = await this.resourceRepository.existsBy({ id: resourceId }); if (!exists) { await this.resourceRepository.save(this.resourceRepository.create({ id: resourceId, metadata: null })); } } parseMetadata(value) { if (!value) return {}; try { const parsed = (0, n8n_workflow_1.jsonParse)(value); return isRecord(parsed) ? parsed : {}; } catch { return {}; } } }; exports.IntegrationMessageContextService = IntegrationMessageContextService; exports.IntegrationMessageContextService = IntegrationMessageContextService = __decorate([ (0, di_1.Service)(), __metadata("design:paramtypes", [agent_thread_repository_1.AgentThreadRepository, agent_resource_repository_1.AgentResourceRepository]) ], IntegrationMessageContextService); function isRecord(value) { return value !== null && typeof value === 'object' && !Array.isArray(value); } function isIntegrationMessageContext(value) { if (!value || typeof value !== 'object') return false; const context = value; return (typeof context.integrationConnectionId === 'string' && typeof context.platform === 'string' && isIntegrationMessageTarget(context.target) && (context.messageId === undefined || typeof context.messageId === 'string') && (context.interactingUserId === undefined || typeof context.interactingUserId === 'string') && (context.agentUserId === undefined || typeof context.agentUserId === 'string') && (context.subject === undefined || isIntegrationMessageSubject(context.subject)) && typeof context.updatedAt === 'string'); } function isIntegrationMessageSubject(value) { if (!value || typeof value !== 'object') return false; const subject = value; return (typeof subject.type === 'string' && typeof subject.id === 'string' && (subject.title === undefined || typeof subject.title === 'string') && (subject.description === undefined || typeof subject.description === 'string') && (subject.url === undefined || typeof subject.url === 'string') && (subject.status === undefined || typeof subject.status === 'string') && (subject.labels === undefined || (Array.isArray(subject.labels) && subject.labels.every((label) => typeof label === 'string'))) && (subject.assignee === undefined || isIntegrationSubjectPerson(subject.assignee)) && (subject.author === undefined || isIntegrationSubjectPerson(subject.author))); } function isIntegrationSubjectPerson(value) { if (!value || typeof value !== 'object') return false; const person = value; return typeof person.id === 'string' && typeof person.name === 'string'; } function isIntegrationMessageTarget(value) { if (!value || typeof value !== 'object') return false; const target = value; if (target.type === 'thread') { return (typeof target.threadId === 'string' && (target.channelId === undefined || typeof target.channelId === 'string') && (target.userId === undefined || typeof target.userId === 'string')); } if (target.type === 'channel') { return (typeof target.channelId === 'string' && (target.threadId === undefined || typeof target.threadId === 'string')); } if (target.type === 'dm') { return (typeof target.userId === 'string' && (target.threadId === undefined || typeof target.threadId === 'string')); } return false; } //# sourceMappingURL=integration-message-context.service.js.map