@cognigy/rest-api-client
Version:
Cognigy REST-Client
186 lines • 8.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AI_AGENT_TOOL_ANSWER = void 0;
/* Custom modules */
const createNodeDescriptor_1 = require("../../../createNodeDescriptor");
const transcripts_1 = require("../../../../interfaces/transcripts/transcripts");
/**
* This is now being referred to as "Resolve Tool Action" in the UI
*/
exports.AI_AGENT_TOOL_ANSWER = (0, createNodeDescriptor_1.createNodeDescriptor)({
type: "aiAgentToolAnswer",
defaultLabel: "Resolve Tool Action",
summary: "UI__NODE_EDITOR__SERVICE__AI_AGENT_ANSWER__SUMMARY",
parentType: "",
preview: {
type: "text",
key: "answer"
},
behavior: {
stopping: true
},
fields: [
{
key: "answer",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_ANSWER__FIELDS__ANSWER_TEXT__LABEL",
description: "UI__NODE_EDITOR__SERVICE__AI_AGENT_ANSWER__FIELDS__ANSWER_TEXT__DESCRIPTION",
type: "cognigyText",
params: {
required: true,
rows: 5,
multiline: true
}
},
{
key: "debugToolAnswer",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_ANSWER__FIELDS__DEBUG_MESSAGE__LABEL",
type: "toggle",
description: "UI__NODE_EDITOR__SERVICE__AI_AGENT_ANSWER__FIELDS__DEBUG_MESSAGE__DESCRIPTION",
defaultValue: false
},
{
key: "maxLoops",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_ANSWER__FIELDS__MAX_LOOPS__LABEL",
type: "slider",
description: "UI__NODE_EDITOR__SERVICE__AI_AGENT_ANSWER__FIELDS__MAX_LOOPS__DESCRIPTION",
defaultValue: 4,
fallbackValue: 4,
params: {
min: 1,
max: 50,
step: 1
}
},
],
sections: [
{
key: "advanced",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_ANSWER__SECTIONS__ADVANCED__LABEL",
defaultCollapsed: true,
fields: [
"maxLoops",
],
},
{
key: "debugging",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__SECTIONS__DEBUG_SETTINGS__LABEL",
defaultCollapsed: true,
fields: [
"debugToolAnswer",
],
}
],
form: [
{ type: "field", key: "answer" },
{ type: "section", key: "debugging" },
{ type: "section", key: "advanced" }
],
appearance: {
color: "#252525",
},
tags: ["ai", "aiAgent"],
function: async ({ cognigy, config, nodeId: thisNodeId }) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const { api, context, input } = cognigy;
const { answer, debugToolAnswer, maxLoops, } = 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;
// Check if this is an MCP Server endpoint - if so, return the answer directly
// without re-executing the AI Agent Job flow
const isMcpServerEndpoint = input.channel === "mcpServer";
if (!(toolCall === null || toolCall === void 0 ? void 0 : toolCall.id) && !isMcpServerEndpoint) {
(_c = api.logDebugError) === null || _c === void 0 ? void 0 : _c.call(api, "UI__DEBUG_MODE__AI_AGENT_ANSWER__ERROR__MESSAGE");
}
if (isMcpServerEndpoint) {
// For MCP Server endpoints, output the answer directly as the tool response
if (answer) {
// Add Tool Call Message to Transcript
if (toolCall) {
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: answer,
}
};
await api.addTranscriptStep(toolAnswer);
}
if (debugToolAnswer) {
(_d = api.logDebugMessage) === null || _d === void 0 ? void 0 : _d.call(api, `<b>UI__DEBUG_MODE__AI_AGENT_ANSWER__DEBUG_ANSWER__MESSAGE</b><br>${answer}`, ((_e = toolCall === null || toolCall === void 0 ? void 0 : toolCall.function) === null || _e === void 0 ? void 0 : _e.name) || "MCP Tool");
}
// Output the answer as text for MCP Server to return as tool response
(_f = api.say) === null || _f === void 0 ? void 0 : _f.call(api, answer);
// Clear the tool call from session state
api.updateSessionStateValues({
lastToolCall: undefined
});
api.resetNextNodes();
}
else {
(_g = api.logDebugError) === null || _g === void 0 ? void 0 : _g.call(api, "UI__DEBUG_MODE__AI_AGENT_ANSWER__MISSING_ANSWER");
}
return;
}
if (toolCall && aiAgentJobNode) {
if (!((_h = api.checkThink) === null || _h === void 0 ? void 0 : _h.call(api, thisNodeId, maxLoops))) {
const { flow, node } = aiAgentJobNode;
if (answer && 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: answer,
}
};
await api.addTranscriptStep(toolAnswer);
if (debugToolAnswer) {
(_j = api.logDebugMessage) === null || _j === void 0 ? void 0 : _j.call(api, `<b>UI__DEBUG_MODE__AI_AGENT_ANSWER__DEBUG_ANSWER__MESSAGE</b><br>${answer}`, `${toolCall.function.name}`);
}
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 if (!answer) {
(_k = api.logDebugError) === null || _k === void 0 ? void 0 : _k.call(api, "UI__DEBUG_MODE__AI_AGENT_ANSWER__MISSING_ANSWER");
}
}
else {
throw new Error("Infinite Loop Detected");
}
}
}
});
//# sourceMappingURL=aiAgentToolAnswer.js.map