n8n
Version:
n8n Workflow Automation Tool
133 lines • 3.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.executionToMessagesDto = executionToMessagesDto;
exports.executionsToMessagesDto = executionsToMessagesDto;
function isRecord(value) {
return typeof value === 'object' && value !== null;
}
function textPart(text) {
if (!text.trim())
return null;
return { type: 'text', text };
}
function textMessageDto(id, role, text) {
const contentPart = textPart(text);
if (!contentPart)
return null;
return {
id,
role,
content: [contentPart],
};
}
function toolCallState(event) {
if (typeof event.endTime !== 'number' || event.endTime <= 0)
return undefined;
return event.success ? 'resolved' : 'rejected';
}
function toolErrorMessage(output) {
if (typeof output === 'string' && output.trim())
return output;
if (isRecord(output)) {
const message = output.message;
if (typeof message === 'string' && message.trim())
return message;
const error = output.error;
if (typeof error === 'string' && error.trim())
return error;
}
if (output === undefined || output === null)
return undefined;
try {
return JSON.stringify(output);
}
catch {
return String(output);
}
}
function timelineToolCallToPart(event) {
const state = toolCallState(event);
const base = {
type: 'tool-call',
toolName: event.name,
toolCallId: event.toolCallId,
input: event.input,
};
if (state === undefined)
return base;
if (state === 'resolved') {
return {
...base,
state,
output: event.output,
};
}
return {
...base,
state,
error: toolErrorMessage(event.output) ?? `Tool "${event.name}" failed`,
};
}
function recordedToolCallToPart(executionId, index, toolCall) {
const base = {
type: 'tool-call',
toolName: toolCall.name,
toolCallId: `${executionId}:tool:${index}`,
input: toolCall.input,
};
if (toolCall.output === undefined)
return base;
return {
...base,
output: toolCall.output,
};
}
function assistantContentFromExecution(execution) {
const content = [];
let hasTimelineText = false;
let hasTimelineToolCalls = false;
for (const event of execution.timeline ?? []) {
if (event.type === 'text') {
const part = textPart(event.content);
if (!part)
continue;
hasTimelineText = true;
content.push(part);
}
else if (event.type === 'tool-call') {
hasTimelineToolCalls = true;
content.push(timelineToolCallToPart(event));
}
}
if (!hasTimelineToolCalls) {
for (const [index, toolCall] of (execution.toolCalls ?? []).entries()) {
content.push(recordedToolCallToPart(execution.id, index, toolCall));
}
}
if (!hasTimelineText) {
const fallbackText = execution.assistantResponse || (execution.error ? `Error: ${execution.error}` : '');
const part = textPart(fallbackText);
if (part)
content.push(part);
}
return content;
}
function executionToMessagesDto(execution) {
const messages = [];
const userMessage = textMessageDto(`${execution.id}:user`, 'user', execution.userMessage);
if (userMessage)
messages.push(userMessage);
const assistantContent = assistantContentFromExecution(execution);
if (assistantContent.length > 0) {
messages.push({
id: `${execution.id}:assistant`,
role: 'assistant',
content: assistantContent,
});
}
return messages;
}
function executionsToMessagesDto(executions) {
return executions.flatMap(executionToMessagesDto);
}
//# sourceMappingURL=execution-to-message-mapper.js.map