UNPKG

n8n

Version:

n8n Workflow Automation Tool

112 lines 4.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AgentChatMessageContextBridge = void 0; const integration_tools_1 = require("./integration-tools"); class AgentChatMessageContextBridge { constructor(messageContextStore, integration, agentId, logger) { this.messageContextStore = messageContextStore; this.integration = integration; this.agentId = agentId; this.logger = logger; } async updateLatest(threadId, resourceId, thread, options = {}) { if (!this.messageContextStore) return undefined; const integrationConnectionId = (0, integration_tools_1.buildIntegrationConnectionId)(this.integration); const previousContext = await this.getPreviousContext(threadId, integrationConnectionId); const agentUserId = options.agentUserId ?? previousContext?.agentUserId; const target = { type: 'thread', threadId: thread.id, channelId: thread.channelId, }; const context = { integrationConnectionId, platform: this.integration.type, target, ...(options.messageId ? { messageId: options.messageId } : {}), ...(options.interactingUserId ? { interactingUserId: options.interactingUserId } : {}), ...(agentUserId ? { agentUserId } : {}), ...(options.subject ? { subject: options.subject } : {}), ...(!options.subject && previousContext?.subject ? { subject: previousContext.subject } : {}), ...(options.replyExpectation ? { replyExpectation: options.replyExpectation, replyTarget: target, ...(options.messageId ? { replyMessageId: options.messageId } : {}), } : {}), updatedAt: new Date().toISOString(), }; try { await this.messageContextStore.setLatest(threadId, resourceId, context); return context; } catch (error) { this.logger.warn('[AgentChatBridge] Failed to update latest message context', { agentId: this.agentId, threadId, error: error instanceof Error ? error.message : String(error), }); return undefined; } } async resolveSubject(message) { try { return toIntegrationMessageSubject(await message.subject); } catch (error) { this.logger.debug(`[AgentChatBridge] Failed to fetch message subject: ${error instanceof Error ? error.message : String(error)}`); return undefined; } } async getPreviousContext(threadId, integrationConnectionId) { if (!this.messageContextStore) return undefined; try { const previousContext = await this.messageContextStore.getLatest(threadId); if (previousContext?.integrationConnectionId !== integrationConnectionId) { return undefined; } return previousContext; } catch (error) { this.logger.warn('[AgentChatBridge] Failed to read previous message context', { agentId: this.agentId, threadId, error: error instanceof Error ? error.message : String(error), }); return undefined; } } } exports.AgentChatMessageContextBridge = AgentChatMessageContextBridge; function toIntegrationMessageSubject(subject) { if (!subject || typeof subject.type !== 'string' || typeof subject.id !== 'string') { return undefined; } const assignee = toIntegrationSubjectPerson(subject.assignee); const author = toIntegrationSubjectPerson(subject.author); const labels = subject.labels?.filter((label) => typeof label === 'string'); return { type: subject.type, id: subject.id, ...(typeof subject.title === 'string' ? { title: subject.title } : {}), ...(typeof subject.description === 'string' ? { description: subject.description } : {}), ...(typeof subject.url === 'string' ? { url: subject.url } : {}), ...(typeof subject.status === 'string' ? { status: subject.status } : {}), ...(labels && labels.length > 0 ? { labels } : {}), ...(assignee ? { assignee } : {}), ...(author ? { author } : {}), }; } function toIntegrationSubjectPerson(person) { if (!person || typeof person.id !== 'string' || typeof person.name !== 'string') { return undefined; } return { id: person.id, name: person.name, }; } //# sourceMappingURL=agent-chat-message-context.js.map