@cognigy/rest-api-client
Version:
Cognigy REST-Client
221 lines • 9.53 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HANDOVER_TO_AI_AGENT_TOOL = void 0;
/* Custom modules */
const createNodeDescriptor_1 = require("../../../createNodeDescriptor");
const transcripts_1 = require("../../../../interfaces/transcripts/transcripts");
exports.HANDOVER_TO_AI_AGENT_TOOL = (0, createNodeDescriptor_1.createNodeDescriptor)({
type: "handoverToAiAgentTool",
defaultLabel: "Handover to AI Agent 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: "handover_to_ai_agent",
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",
params: {
required: true,
rows: 5,
multiline: true,
placeholder: "UI__NODE_EDITOR__SERVICE__HANDOVER_TO_AI_AGENT_TOOL__FIELDS__DESCRIPTION__PLACEHOLDER"
}
},
{
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": {
"handover_reason": {
"type": "string",
"description": "The reason for handing over to the AI Agent, including context from the conversation."
}
},
"required": ["handover_reason"],
"additionalProperties": false
}`,
params: {
required: false,
},
},
{
key: "flowNode",
type: "flowNode",
label: "UI__NODE_EDITOR__SERVICE__HANDOVER_TO_AI_AGENT_TOOL__FIELDS__FLOW_NODE__LABEL",
params: {
required: true,
nodeTypeFilter: ["aiAgentJob"]
}
},
{
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",
],
},
],
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;
const { api, input } = cognigy;
const { debugMessage, flowNode, 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;
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) || {}
}
};
}
else {
// Regular AI Agent tool call
toolCall = (_e = sessionState.lastToolCall) === null || _e === void 0 ? void 0 : _e.toolCall;
}
if (!(toolCall === null || toolCall === void 0 ? void 0 : toolCall.id) && !isMcpToolCall) {
(_f = api.logDebugError) === null || _f === void 0 ? void 0 : _f.call(api, "UI__DEBUG_MODE__AI_AGENT_ANSWER__ERROR__MESSAGE");
}
if (!(flowNode === null || flowNode === void 0 ? void 0 : flowNode.flow) || !(flowNode === null || flowNode === void 0 ? void 0 : flowNode.node)) {
throw new Error("Flow Node is required for Handover to AI Agent Tool");
}
if (toolCall) {
if (!((_g = api.checkThink) === null || _g === void 0 ? void 0 : _g.call(api, thisNodeId))) {
const handoverReason = ((_j = (_h = toolCall === null || toolCall === void 0 ? void 0 : toolCall.function) === null || _h === void 0 ? void 0 : _h.arguments) === null || _j === void 0 ? void 0 : _j.handover_reason) || "Handover requested";
// Optional Debug Message
if (debugMessage) {
(_k = api.logDebugMessage) === null || _k === void 0 ? void 0 : _k.call(api, JSON.stringify({ handover_reason: handoverReason, target_flow: flowNode.flow, target_node: flowNode.node }, null, 2));
}
// For MCP tool calls, output a result and then execute the target flow
// For regular AI Agent calls, add transcript entries and execute flow
if (isMcpToolCall) {
// Output confirmation message - will be captured by handleFinalPing
await ((_l = api.output) === null || _l === void 0 ? void 0 : _l.call(api, `Handover to AI Agent initiated. Reason: ${handoverReason}`, {
_cognigy: {
_preventTranscript: true
}
}));
}
else {
// 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: `Handover to AI Agent initiated. Reason: ${handoverReason}`,
}
};
await api.addTranscriptStep(toolAnswer);
}
api.resetNextNodes();
api.updateSessionStateValues({
lastToolCall: undefined
});
await api.executeFlow({
flowNode: {
flow: flowNode.flow,
node: flowNode.node,
},
absorbContext: true,
});
}
else {
throw new Error("Infinite Loop Detected");
}
}
}
});
//# sourceMappingURL=handoverToAiAgentTool.js.map