@cognigy/rest-api-client
Version:
Cognigy REST-Client
316 lines • 13.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SEND_EMAIL_TOOL = void 0;
/* Custom modules */
const createNodeDescriptor_1 = require("../../../createNodeDescriptor");
const transcripts_1 = require("../../../../interfaces/transcripts/transcripts");
exports.SEND_EMAIL_TOOL = (0, createNodeDescriptor_1.createNodeDescriptor)({
type: "sendEmailTool",
defaultLabel: "Send Email Tool",
parentType: ["aiAgentJob", "llmPromptV2"],
constraints: {
editable: true,
deletable: true,
collapsable: true,
creatable: true,
movable: true,
placement: {
predecessor: {
whitelist: []
}
},
childFlowCreatable: false
},
behavior: {
stopping: true
},
preview: {
type: "text",
key: "toolId"
},
fields: [
{
key: "toolId",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_TOOL__FIELDS__TOOL_ID__LABEL",
description: "UI__NODE_EDITOR__SERVICE__AI_AGENT_TOOL__FIELDS__TOOL_ID__DESCRIPTION",
type: "cognigyLLMText",
defaultValue: "send_email",
params: {
required: true,
rows: 1,
multiline: false,
maxLength: 64,
}
},
{
key: "description",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_TOOL__FIELDS__DESCRIPTION__LABEL",
description: "UI__NODE_EDITOR__SERVICE__AI_AGENT_TOOL__FIELDS__DESCRIPTION__DESCRIPTION",
type: "cognigyLLMText",
defaultValue: "Create and send a new email message.",
params: {
required: true,
rows: 5,
multiline: true
}
},
{
key: "recipient",
label: "UI__NODE_EDITOR__SMTP__EMAIL_NOTIFICATION__RECIPIENT__LABEL",
description: "UI__NODE_EDITOR__SMTP__EMAIL_NOTIFICATION__RECIPIENT__DESCRIPTION",
type: "cognigyText",
params: {
required: true,
rows: 1,
multiline: false,
maxLength: 255,
},
},
{
type: "description",
key: "aiAgentInfo",
label: " ",
params: {
text: "UI__NODE_EDITOR__SERVICE__SEND_EMAIL_TOOL__FIELDS__AI_AGENT_INFO__TEXT",
},
},
{
key: "cc",
type: "cognigyText",
label: "UI__NODE_EDITOR__SMTP__EMAIL_NOTIFICATION__CC__LABEL",
description: "UI__NODE_EDITOR__SMTP__EMAIL_NOTIFICATION__CC__DESCRIPTION",
},
{
key: "bcc",
type: "cognigyText",
label: "UI__NODE_EDITOR__SMTP__EMAIL_NOTIFICATION__BCC__LABEL",
description: "UI__NODE_EDITOR__SMTP__EMAIL_NOTIFICATION__BCC__DESCRIPTION",
},
{
key: "priority",
type: "select",
label: "UI__NODE_EDITOR__SMTP__EMAIL_NOTIFICATION__PRIORITY__LABEL",
defaultValue: "normal",
params: {
options: [
{ label: "UI__NODE_EDITOR__SMTP__EMAIL_NOTIFICATION__PRIORITY__HIGH__LABEL", value: "high" },
{ label: "UI__NODE_EDITOR__SMTP__EMAIL_NOTIFICATION__PRIORITY__NORMAL__LABEL", value: "normal" },
{ label: "UI__NODE_EDITOR__SMTP__EMAIL_NOTIFICATION__PRIORITY__LOW__LABEL", value: "low" }
]
}
},
{
key: "parameters",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_TOOL__FIELDS__PARAMETERS__LABEL",
type: "toolParameters",
defaultValue: `{
"type": "object",
"properties": {
"subject": {
"type": "string",
"description": "The subject of the email."
},
"message": {
"type": "string",
"description": "The body of the email."
}
},
"required": ["subject", "message"],
"additionalProperties": false
}`,
},
{
key: "debugMessage",
type: "toggle",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_TOOL__FIELDS__DEBUG_MESSAGE__LABEL",
description: "UI__NODE_EDITOR__SERVICE__AI_AGENT_TOOL__FIELDS__DEBUG_MESSAGE__DESCRIPTION",
defaultValue: true,
},
{
key: "condition",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_TOOL__FIELDS__CONDITION__LABEL",
description: "UI__NODE_EDITOR__SERVICE__AI_AGENT_TOOL__FIELDS__CONDITION__DESCRIPTION",
type: "cognigyText",
defaultValue: "",
},
],
sections: [
{
key: "debugging",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__SECTIONS__DEBUG_SETTINGS__LABEL",
defaultCollapsed: true,
fields: [
"debugMessage",
],
},
{
key: "advanced",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__SECTIONS__ADVANCED__LABEL",
defaultCollapsed: true,
fields: [
"cc",
"bcc",
"priority",
"condition"
]
},
],
form: [
{ type: "field", key: "aiAgentInfo" },
{ type: "field", key: "toolId" },
{ type: "field", key: "description" },
{ type: "field", key: "recipient" },
{ type: "section", key: "debugging" },
{ type: "section", key: "advanced" },
],
appearance: {
color: "white",
textColor: "#252525",
variant: "mini",
},
function: async ({ cognigy, config, nodeId, nodeType, organisationId, projectId, childConfigs }) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
const { api, context, input } = cognigy;
const { debugMessage, recipient, cc, bcc, priority, toolId, } = config;
const sessionState = await api.loadSessionState();
// Check if this is an MCP direct tool call
const mcpData = (_a = input.data) === null || _a === void 0 ? void 0 : _a._mcp;
const isMcpToolCall = (mcpData === null || mcpData === void 0 ? void 0 : mcpData.method) === "tools/call" && ((_b = mcpData === null || mcpData === void 0 ? void 0 : mcpData.params) === null || _b === void 0 ? void 0 : _b.name);
// For MCP calls, synthesize a toolCall object from input.data._mcp
// For regular AI Agent calls, use sessionState.lastToolCall
let toolCall;
let aiAgentJobNode;
if (isMcpToolCall) {
// MCP tool call - create synthetic toolCall from MCP data
toolCall = {
id: String(mcpData.id),
type: "function",
index: 0,
function: {
name: ((_c = mcpData.params) === null || _c === void 0 ? void 0 : _c.name) || toolId,
arguments: ((_d = mcpData.params) === null || _d === void 0 ? void 0 : _d.arguments) || {}
}
};
aiAgentJobNode = mcpData.aiAgentJobNode;
}
else {
// Regular AI Agent tool call
toolCall = (_e = sessionState.lastToolCall) === null || _e === void 0 ? void 0 : _e.toolCall;
aiAgentJobNode = (_f = sessionState.lastToolCall) === null || _f === void 0 ? void 0 : _f.aiAgentJobNode;
}
if (!(toolCall === null || toolCall === void 0 ? void 0 : toolCall.id) && !isMcpToolCall) {
(_g = api.logDebugError) === null || _g === void 0 ? void 0 : _g.call(api, "UI__DEBUG_MODE__AI_AGENT_ANSWER__ERROR__MESSAGE");
}
if (toolCall && (aiAgentJobNode || isMcpToolCall)) {
if (!((_h = api.checkThink) === null || _h === void 0 ? void 0 : _h.call(api, nodeId))) {
const { subject, message } = toolCall.function.arguments;
let result;
try {
result = await ((_j = api === null || api === void 0 ? void 0 : api.emailNotification) === null || _j === void 0 ? void 0 : _j.call(api, {
cognigy,
config: {
recipient,
subject,
message,
cc: cc || "",
bcc: bcc || "",
priority: priority || "normal",
async: false,
storeLocation: "none",
inputKey: "",
contextKey: "",
stopOnError: false,
senderName: "",
},
nodeId,
nodeType,
organisationId,
projectId,
childConfigs,
}));
}
catch (error) {
// Log error message to debug mode
const errorMessage = error instanceof Error ? error.message : String(error);
(_k = api.logDebugError) === null || _k === void 0 ? void 0 : _k.call(api, errorMessage, "UI__DEBUG_MODE__AI_AGENT__SEND_EMAIL_TOOL__ERROR");
// Pass error message to LLM as result
result = errorMessage;
}
// Optional Debug Message of Email sending and response
if (debugMessage) {
const messageLines = [];
if (recipient) {
messageLines.push(`\n<b>UI__DEBUG_MODE__AI_AGENT__SEND_EMAIL_TOOL__RECIPIENT</b> ${recipient}`);
}
if (subject) {
messageLines.push(`\n<b>UI__DEBUG_MODE__AI_AGENT__SEND_EMAIL_TOOL__SUBJECT</b> ${subject}`);
}
if (message) {
messageLines.push(`\n<b>UI__DEBUG_MODE__AI_AGENT__SEND_EMAIL_TOOL__MESSAGE</b> ${message}`);
}
if (result) {
const resultString = typeof result === 'string' ? result : JSON.stringify(result, null, 2);
messageLines.push(`\n<b>UI__DEBUG_MODE__AI_AGENT__SEND_EMAIL_TOOL__RESULT</b>\n${resultString}`);
}
(_l = api.logDebugMessage) === null || _l === void 0 ? void 0 : _l.call(api, messageLines.join("\n"), "UI__DEBUG_MODE__AI_AGENT__SEND_EMAIL_TOOL__HEADER");
}
// For MCP tool calls, output the result directly - it will be captured by handleFinalPing
// For regular AI Agent calls, we need to return to the parent AI Agent node
const resultString = typeof result === 'string' ? result : JSON.stringify(result);
if (isMcpToolCall) {
// Clear the execution stack so the flow doesn't continue to other nodes
// (e.g. the flow's first node) after outputting the MCP result
api.resetNextNodes();
// Output the result as the tool response
// This will be captured by handleFinalPing and sent back to the MCP client
await ((_m = api.output) === null || _m === void 0 ? void 0 : _m.call(api, resultString, {
_cognigy: {
_preventTranscript: true
}
}));
}
else if (aiAgentJobNode) {
const { flow, node } = aiAgentJobNode;
if (flow && node) {
// Add Tool Call Message to Transcript
const toolCallTranscriptStep = {
role: transcripts_1.TranscriptRole.ASSISTANT,
type: transcripts_1.TranscriptEntryType.TOOL_CALL,
source: "system",
payload: Object.assign({ name: toolCall.function.name, id: toolCall.id, input: toolCall.function.arguments }, (toolCall.thoughtSignature && { thoughtSignature: toolCall.thoughtSignature }))
};
await api.addTranscriptStep(toolCallTranscriptStep);
// Add Tool Answer Message to Transcript
const toolAnswer = {
role: transcripts_1.TranscriptRole.TOOL,
type: transcripts_1.TranscriptEntryType.TOOL_ANSWER,
source: "system",
payload: {
toolCallId: toolCall.id,
name: toolCall.function.name,
content: `Send email result:\n${resultString}`,
}
};
await api.addTranscriptStep(toolAnswer);
api.resetNextNodes();
// remove the call from the session state, because the call has been answered
api.updateSessionStateValues({
lastToolCall: undefined
});
await api.executeFlow({
flowNode: {
flow,
node,
},
absorbContext: true,
});
}
}
}
else {
throw new Error("Infinite Loop Detected");
}
}
}
});
//# sourceMappingURL=sendEmailTool.js.map