@cognigy/rest-api-client
Version:
Cognigy REST-Client
274 lines • 11.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EXECUTE_WORKFLOW_TOOL = void 0;
/* Custom modules */
const createNodeDescriptor_1 = require("../../../createNodeDescriptor");
const transcripts_1 = require("../../../../interfaces/transcripts/transcripts");
exports.EXECUTE_WORKFLOW_TOOL = (0, createNodeDescriptor_1.createNodeDescriptor)({
type: "executeWorkflowTool",
defaultLabel: "Execute Workflow 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: "execute_workflow",
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__EXECUTE_WORKFLOW_TOOL__FIELDS__DESCRIPTION__DESCRIPTION",
type: "cognigyLLMText",
params: {
required: true,
rows: 5,
multiline: true,
placeholder: `Describe the action or functionality that the target Flow handles.\nFor example: "Update a booking", "Check order status", or "Start onboarding workflow". Be specific so the AI Agent knows exactly when to execute this Flow.`
}
},
{
key: "parameters",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_TOOL__FIELDS__PARAMETERS__LABEL",
description: "UI__NODE_EDITOR__SERVICE__AI_AGENT_TOOL__FIELDS__PARAMETERS__DESCRIPTION",
type: "toolParameters",
defaultValue: `{
"type": "object",
"properties": {
"execution_reason": {
"type": "string",
"description": "The reason for executing this workflow, including context from the conversation."
}
},
"required": ["execution_reason"],
"additionalProperties": false
}`,
params: {
required: false,
},
},
{
key: "flowNode",
type: "flowNode",
label: "UI__NODE_EDITOR__SERVICE__EXECUTE_WORKFLOW_TOOL__FIELDS__FLOW_NODE__LABEL",
params: {
required: true,
}
},
{
key: "parseIntents",
type: "toggle",
label: "UI__NODE_EDITOR__EXECUTE_FLOW__PARSE_INTENTS__LABEL",
description: "UI__NODE_EDITOR__EXECUTE_FLOW__PARSE_INTENTS__DESCRIPTION",
defaultValue: false
},
{
key: "parseKeyphrases",
type: "toggle",
label: "UI__NODE_EDITOR__EXECUTE_FLOW__PARSE_KEYPHRASES__LABEL",
description: "UI__NODE_EDITOR__EXECUTE_FLOW__PARSE_KEYPHRASES__DESCRIPTION",
defaultValue: false
},
{
key: "absorbContext",
type: "toggle",
label: "UI__NODE_EDITOR__EXECUTE_FLOW__ABSORB_CONTEXT__LABEL",
description: "UI__NODE_EDITOR__EXECUTE_FLOW__ABSORB_CONTEXT__DESCRIPTION",
defaultValue: 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: [
"condition",
"parseIntents",
"parseKeyphrases",
"absorbContext",
],
},
],
form: [
{ type: "field", key: "toolId" },
{ type: "field", key: "description" },
{ type: "field", key: "flowNode" },
{ type: "section", key: "debugging" },
{ type: "section", key: "advanced" },
],
appearance: {
color: "white",
textColor: "#252525",
variant: "mini",
},
function: async ({ cognigy, config, nodeId: thisNodeId }) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
const { api, input } = cognigy;
const { debugMessage, toolId, flowNode: targetFlowNode, parseIntents, parseKeyphrases, absorbContext, } = 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) {
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 {
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 (!(targetFlowNode === null || targetFlowNode === void 0 ? void 0 : targetFlowNode.flow)) {
throw new Error("Flow is required for Execute Workflow Tool");
}
if (toolCall && (aiAgentJobNode || isMcpToolCall)) {
if (!((_h = api.checkThink) === null || _h === void 0 ? void 0 : _h.call(api, thisNodeId))) {
const executionReason = ((_k = (_j = toolCall === null || toolCall === void 0 ? void 0 : toolCall.function) === null || _j === void 0 ? void 0 : _j.arguments) === null || _k === void 0 ? void 0 : _k.execution_reason) || "No reason provided";
// Optional Debug Message
if (debugMessage) {
(_l = api.logDebugMessage) === null || _l === void 0 ? void 0 : _l.call(api, JSON.stringify({
execution_reason: executionReason,
target_flow: targetFlowNode.flow,
target_node: targetFlowNode.node,
}, null, 2));
}
api.resetNextNodes();
if (isMcpToolCall) {
// For MCP: execute the target flow then output the result directly
await api.executeFlow({
flowNode: {
flow: targetFlowNode.flow,
node: targetFlowNode.node
},
absorbContext,
parseIntents,
parseKeyphrases
});
await ((_m = api.output) === null || _m === void 0 ? void 0 : _m.call(api, "Workflow executed", {
_cognigy: {
_preventTranscript: true
}
}));
}
else {
// For regular AI Agent: return to the AI Agent Job node first, then execute target flow
const { flow: thisFlow, node: thisNode } = aiAgentJobNode;
if (thisFlow && thisNode) {
await api.executeFlow({
flowNode: {
flow: thisFlow,
node: thisNode,
},
absorbContext: true,
});
}
else {
throw new Error("AI Agent Job Node is required for Execute Workflow Tool");
}
// Execute the target flow
await api.executeFlow({
flowNode: {
flow: targetFlowNode.flow,
node: targetFlowNode.node
},
absorbContext,
parseIntents,
parseKeyphrases
});
// 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: "Workflow executed",
}
};
await api.addTranscriptStep(toolAnswer);
// Clear the last tool call from session state
api.updateSessionStateValues({
lastToolCall: undefined
});
}
}
else {
throw new Error("Infinite Loop Detected");
}
}
}
});
//# sourceMappingURL=executeWorkflowTool.js.map