@cognigy/rest-api-client
Version:
Cognigy REST-Client
180 lines • 7.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AI_AGENT_JOB_CALL_MCP_TOOL = void 0;
/* Custom modules */
const createNodeDescriptor_1 = require("../../../createNodeDescriptor");
const transcripts_1 = require("../../../../interfaces/transcripts/transcripts");
exports.AI_AGENT_JOB_CALL_MCP_TOOL = (0, createNodeDescriptor_1.createNodeDescriptor)({
type: "aiAgentJobCallMCPTool",
defaultLabel: "Call MCP Tool",
summary: "UI__NODE_EDITOR__SERVICE__AI_AGENT_CALL_MCP_TOOL__SUMMARY",
parentType: "",
tokens: [
{
type: "input",
label: "MCP Tool Result",
script: "input.aiAgent.toolResult",
},
],
fields: [
{
key: "storeLocation",
type: "select",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STORE_LOCATION__LABEL",
defaultValue: "input",
params: {
options: [
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STORE_LOCATION__OPTIONS__INPUT__LABEL",
value: "input",
},
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STORE_LOCATION__OPTIONS__CONTEXT__LABEL",
value: "context",
},
],
},
},
{
key: "inputKey",
type: "cognigyText",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__INPUT_KEY__LABEL",
defaultValue: "aiAgent.toolResult",
condition: {
key: "storeLocation",
value: "input",
},
},
{
key: "contextKey",
type: "cognigyText",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__CONTEXT_KEY__LABEL",
defaultValue: "aiAgent.toolResult",
condition: {
key: "storeLocation",
value: "context",
},
},
{
key: "resolveImmediately",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_CALL_MCP_TOOL__FIELDS__RESOLVE_IMMEDIATELY__LABEL",
type: "toggle",
description: "UI__NODE_EDITOR__SERVICE__AI_AGENT_CALL_MCP_TOOL__FIELDS__RESOLVE_IMMEDIATELY__DESCRIPTION",
defaultValue: false,
},
{
key: "debugToolResult",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_CALL_MCP_TOOL__FIELDS__DEBUG_TOOL_RESULT__LABEL",
type: "toggle",
description: "UI__NODE_EDITOR__SERVICE__AI_AGENT_CALL_MCP_TOOL__FIELDS__DEBUG_TOOL_RESULT__DESCRIPTION",
defaultValue: false,
},
],
sections: [
{
key: "storage",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_CALL_MCP_TOOL__SECTIONS__STORAGE__LABEL",
defaultCollapsed: false,
fields: ["storeLocation", "inputKey", "contextKey"],
},
{
key: "debugging",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__SECTIONS__DEBUG_SETTINGS__LABEL",
defaultCollapsed: true,
fields: ["debugToolResult"],
},
],
form: [
{ type: "field", key: "resolveImmediately" },
{ type: "section", key: "storage" },
{ type: "section", key: "debugging" },
],
appearance: {
color: "#252525",
},
tags: ["ai", "aiAgent"],
function: async ({ cognigy, config }) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const { api } = cognigy;
const { storeLocation, contextKey, inputKey, resolveImmediately, debugToolResult } = config;
const sessionState = await api.loadSessionState();
const toolCall = (_a = sessionState.lastToolCall) === null || _a === void 0 ? void 0 : _a.toolCall;
const aiAgentJobNode = (_b = sessionState.lastToolCall) === null || _b === void 0 ? void 0 : _b.aiAgentJobNode;
const mcpServerUrl = (_c = sessionState.lastToolCall) === null || _c === void 0 ? void 0 : _c.mcpServerUrl;
const timeout = (_d = sessionState.lastToolCall) === null || _d === void 0 ? void 0 : _d.timeout;
if (!(toolCall === null || toolCall === void 0 ? void 0 : toolCall.id)) {
(_e = api.logDebugError) === null || _e === void 0 ? void 0 : _e.call(api, "UI__DEBUG_MODE__AI_AGENT_ANSWER__ERROR__MESSAGE");
}
if (toolCall && aiAgentJobNode && mcpServerUrl && timeout) {
let toolResult = null;
let fullResult = null;
try {
toolResult = await api.executeMcpTool({
mcpServerUrl,
toolName: toolCall.function.name,
toolArgs: toolCall.function.arguments,
timeout,
});
fullResult = JSON.stringify(toolResult, null, 2);
if (debugToolResult) {
(_f = api.logDebugMessage) === null || _f === void 0 ? void 0 : _f.call(api, `Tool <b>${(_g = toolCall === null || toolCall === void 0 ? void 0 : toolCall.function) === null || _g === void 0 ? void 0 : _g.name}</b> called successfully.<br><br><b>Result:</b><br>${fullResult}`);
}
}
catch (error) {
const errorDetails = error instanceof Error
? {
name: error.name,
message: error.message,
}
: error;
(_h = api.logDebugError) === null || _h === void 0 ? void 0 : _h.call(api, `Failed to execute MCP Tool ${(_j = toolCall === null || toolCall === void 0 ? void 0 : toolCall.function) === null || _j === void 0 ? void 0 : _j.name}:<br>${JSON.stringify(errorDetails, null, 2)}`);
}
// Add result to Cognigy Input/Context for further usage
if (storeLocation === "context") {
(_k = api.addToContext) === null || _k === void 0 ? void 0 : _k.call(api, contextKey, toolResult, "simple");
}
else if (storeLocation === "input") {
api.addToInput(inputKey, toolResult);
}
const { flow, node } = aiAgentJobNode;
if (resolveImmediately && fullResult && flow && node) {
// Add Tool Call Message to Transcript
const toolCallTranscriptStep = {
role: transcripts_1.TranscriptRole.ASSISTANT,
type: transcripts_1.TranscriptEntryType.TOOL_CALL,
source: "system",
payload: {
name: toolCall.function.name,
id: toolCall.id,
input: toolCall.function.arguments,
},
};
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,
content: fullResult,
},
};
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,
});
}
}
},
});
//# sourceMappingURL=aiAgentJobCallMCPTool.js.map