@cognigy/rest-api-client
Version:
Cognigy REST-Client
922 lines • 42.7 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LLM_PROMPT_V2 = void 0;
/* Custom modules */
const createNodeDescriptor_1 = require("../../../createNodeDescriptor");
const logic_1 = require("../../logic");
const crypto_1 = require("crypto");
const createToolDefinitions_1 = require("../aiAgent/helpers/createToolDefinitions");
const prompt_1 = require("../../nlu/generativeSlotFiller/prompt");
const errors_1 = require("../../../../errors");
const transcripts_1 = require("../../../../interfaces/transcripts/transcripts");
exports.LLM_PROMPT_V2 = (0, createNodeDescriptor_1.createNodeDescriptor)({
type: "llmPromptV2",
defaultLabel: "LLM Prompt",
summary: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__DESCRIPTION",
constraints: {
collapsable: true,
placement: {
children: {
whitelist: ["llmPromptDefault", "llmPromptTool", "llmPromptMCPTool"],
},
},
},
behavior: {
entrypoint: true
},
dependencies: {
children: ["llmPromptDefault", "llmPromptTool"]
},
preview: {
type: "text",
key: "prompt",
},
fields: [
{
key: "llmProviderReferenceId",
type: "llmSelect",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__LLM_SELECT__LABEL",
defaultValue: "default",
params: {
required: true
}
},
{
key: "prompt",
label: "UI__NODE_EDITOR__SERVICE__LLM_PROMPT__FIELDS__PROMPT__LABEL",
type: "cognigyLLMText",
description: "UI__NODE_EDITOR__SERVICE__LLM_PROMPT__FIELDS__PROMPT__DESCRIPTION",
params: {
multiline: true,
rows: 5,
required: false
},
defaultValue: ""
},
{
key: "chatTranscriptSteps",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TRANSCRIPT_STEPS__LABEL",
type: "slider",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TRANSCRIPT_STEPS__DESCRIPTION",
defaultValue: 50,
params: {
min: 0,
max: 50,
step: 1
},
condition: {
key: "usePromptMode",
value: false,
}
},
{
key: "usePromptMode",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__USE_PROMPT__LABEL",
type: "toggle",
params: {
required: true
},
defaultValue: false
},
{
key: "samplingMethod",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__SAMPLING_METHOD__LABEL",
type: "select",
defaultValue: "temperature",
params: {
options: [
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__SAMPLING_METHOD__OPTIONS__TEMPERATURE__LABEL",
value: "temperature"
},
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__SAMPLING_METHOD__OPTIONS__TOP_PERCENTATGE__LABEL",
value: "topP"
}
]
}
},
{
key: "temperature",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TEMPERATURE__LABEL",
type: "slider",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TEMPERATURE__DESCRIPTION",
defaultValue: 0.7,
params: {
min: 0,
max: 1,
step: 0.1
},
condition: {
key: "samplingMethod",
value: "temperature",
}
},
{
key: "topP",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TOP_P__LABEL",
type: "slider",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TOP_P__DESCRIPTION",
defaultValue: 1,
params: {
min: 0,
max: 1,
step: 0.1
},
condition: {
key: "samplingMethod",
value: "topP",
}
},
{
key: "maxTokens",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__MAX_TOKENS__LABEL",
type: "slider",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__MAX_TOKENS__DESCRIPTION",
defaultValue: 1000,
params: {
min: 100,
max: 16000,
step: 100
}
},
{
key: "frequencyPenalty",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__FREQUENCY_PENALTY__LABEL",
type: "slider",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__FREQUENCY_PENALTY__DESCRIPTION",
defaultValue: 0,
params: {
min: -2,
max: 2,
step: 0.1
}
},
{
key: "presencePenalty",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__PRESENCE_PENALTY__LABEL",
type: "slider",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__PRESENCE_PENALTY__DESCRIPTION",
defaultValue: 0,
params: {
min: -2,
max: 2,
step: 0.1
}
},
{
key: "useStop",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__USE_STOP__LABEL",
type: "toggle",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__USE_STOP__DESCRIPTION",
defaultValue: false
},
{
key: "stop",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STOP__LABEL",
type: "textArray",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STOP__DESCRIPTION",
condition: {
key: "useStop",
value: true
}
},
{
key: "timeout",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TIMEOUT__LABEL",
defaultValue: 8000,
type: "number",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__TIMEOUT__DESCRIPTION",
},
{
key: "storeLocation",
type: "select",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STORE_LOCATION__LABEL",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STORE_LOCATION__DESCRIPTION",
defaultValue: "stream",
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"
},
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STORE_LOCATION__OPTIONS__STREAM__LABEL",
value: "stream"
}
],
required: true
},
},
{
key: "immediateOutput",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__IMMEDIATEOUTPUT__LABEL",
type: "toggle",
defaultValue: true,
condition: {
or: [
{
key: "storeLocation",
value: "input",
},
{
key: "storeLocation",
value: "context",
}
]
}
},
{
key: "inputKey",
type: "cognigyText",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__INPUT_KEY__LABEL",
defaultValue: "promptResult",
condition: {
or: [
{
key: "storeLocation",
value: "input",
},
{
and: [
{
key: "storeLocation",
value: "stream",
},
{
key: "streamStoreCopyInInput",
value: true,
}
]
}
]
}
},
{
key: "contextKey",
type: "cognigyText",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__CONTEXT_KEY__LABEL",
defaultValue: "promptResult",
condition: {
key: "storeLocation",
value: "context",
}
},
{
key: "streamStopTokens",
type: "textArray",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STREAM_STOP_TOKENS__LABEL",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STREAM_STOP_TOKENS__DESCRIPTION",
defaultValue: [".", "!", "?", "\\n"],
condition: {
key: "storeLocation",
value: "stream",
}
},
{
key: "streamStopTokenOverrides",
type: "textArray",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STREAM_STOP_TOKEN_OVERRIDES__LABEL",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STREAM_STOP_TOKEN_OVERRIDES__DESCRIPTION",
defaultValue: ["\d+\."],
condition: {
key: "storeLocation",
value: "stream",
}
},
{
key: "streamDescription",
type: "description",
label: " ",
params: {
text: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STREAM_DESCRIPTION__TEXT"
},
condition: {
key: "storeLocation",
value: "stream",
}
},
{
key: "streamStoreCopyInInput",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STREAM_STORE_COPY__LABEL",
type: "toggle",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__STREAM_STORE_COPY__DESCRIPTION",
defaultValue: false,
condition: {
key: "storeLocation",
value: "stream",
}
},
{
key: "debugLogTokenCount",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__DEBUGLOGTOKENCOUNT__LABEL",
type: "toggle",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__DEBUGLOGTOKENCOUNT__DESCRIPTION",
defaultValue: false
},
{
key: "debugLogRequestAndCompletion",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__DEBUGLOGREQUESTANDCOMPLETION__LABEL",
type: "toggle",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__DEBUGLOGREQUESTANDCOMPLETION__DESCRIPTION",
defaultValue: false
},
{
key: "debugDescription",
type: "description",
label: " ",
params: {
text: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__DEBUG_DESCRIPTION__TEXT"
}
},
{
key: "responseFormat",
type: "select",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__RESPONSE_FORMAT__LABEL",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__RESPONSE_FORMAT__DESCRIPTION",
defaultValue: "default",
params: {
options: [
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__RESPONSE_FORMAT__OPTIONS__DEFAULT__LABEL",
value: "default"
},
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__RESPONSE_FORMAT__OPTIONS__TEXT__LABEL",
value: "text"
},
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__RESPONSE_FORMAT__OPTIONS__JSON__LABEL",
value: "json_object"
}
]
},
},
{
key: "seed",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__SEED__LABEL",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__SEED__DESCRIPTION",
type: "cognigyText",
defaultValue: ""
},
{
key: "jsonStreamWarning",
type: "description",
label: " ",
params: {
text: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__JSONSTREAMWARNING__PARAM"
},
condition: {
and: [
{
key: "responseFormat",
value: "json_object"
},
{
key: "storeLocation",
value: "stream",
}
]
}
},
{
key: "customModelOptions",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__CUSTOM_MODEL_OPTIONS__LABEL",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__CUSTOM_MODEL_OPTIONS__DESCRIPTION",
type: "json",
defaultValue: {}
},
{
key: "customRequestOptions",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__CUSTOM_REQUEST_OPTIONS__LABEL",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__CUSTOM_REQUEST_OPTIONS__DESCRIPTION",
type: "json",
defaultValue: {}
},
{
key: "logErrorToSystem",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__LOG_ERROR_TO_SYSTEM__LABEL",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__LOG_ERROR_TO_SYSTEM__DESCRIPTION",
type: "toggle",
defaultValue: false,
},
{
key: "errorHandling",
type: "select",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__HANDLE_SERVICE_ERROR__LABEL",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__HANDLE_SERVICE_ERROR__DESCRIPTION",
defaultValue: "continue",
params: {
options: [
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__HANDLE_SERVICE_ERROR__OPTIONS__STOP__LABEL",
value: "stop"
},
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__HANDLE_SERVICE_ERROR__OPTIONS__CONTINUE__LABEL",
value: "continue"
},
{
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__HANDLE_SERVICE_ERROR__OPTIONS__GOTO__LABEL",
value: "goto"
},
]
}
},
{
key: "errorMessage",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__ERROR_MESSAGE__LABEL",
type: "cognigyText",
description: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__ERROR_MESSAGE__DESCRIPTION",
defaultValue: "",
condition: {
key: "errorHandling",
value: "continue"
}
},
{
key: "errorHandlingGotoTarget",
type: "flowNode",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__FIELDS__ERROR__GOTO_NODE__LABEL",
condition: {
key: "errorHandling",
value: "goto"
}
},
{
key: "toolChoice",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__TOOL_CHOICE__LABEL",
description: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__TOOL_CHOICE__DESCRIPTION",
type: "select",
defaultValue: "auto",
params: {
options: [
{
label: 'UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__TOOL_CHOICE__OPTIONS__AUTO__LABEL',
value: 'auto'
},
{
label: 'UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__TOOL_CHOICE__OPTIONS__REQUIRED__LABEL',
value: 'required'
},
{
label: 'UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__TOOL_CHOICE__OPTIONS__NONE__LABEL',
value: 'none'
}
]
}
},
{
key: "useStrict",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__USE_STRICT__LABEL",
description: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__USE_STRICT__DESCRIPTION",
type: "toggle",
defaultValue: false
},
{
key: "processImages",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__PROCESS_IMAGES__LABEL",
description: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__PROCESS_IMAGES__DESCRIPTION",
type: "toggle",
defaultValue: false
},
{
key: "transcriptImageHandling",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__TRANSCRIPT_IMAGES__LABEL",
description: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__TRANSCRIPT_IMAGES__DESCRIPTION",
type: "select",
defaultValue: "minify",
params: {
options: [
{
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__TRANSCRIPT_IMAGES__OPTIONS__MINIFY__LABEL",
value: "minify"
},
{
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__TRANSCRIPT_IMAGES__OPTIONS__DROP__LABEL",
value: "drop"
},
{
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__TRANSCRIPT_IMAGES__OPTIONS__KEEP__LABEL",
value: "keep"
}
],
},
},
{
key: "debugLogToolDefinitions",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__LOG_TOOL_DEFINITIONS__LABEL",
type: "toggle",
description: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__FIELDS__LOG_TOOL_DEFINITIONS__DESCRIPTION",
defaultValue: false
},
],
sections: [
{
key: "advanced",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__SECTIONS__ADVANCED__LABEL",
defaultCollapsed: true,
fields: [
"maxTokens",
"usePromptMode",
"chatTranscriptSteps",
"responseFormat",
"jsonStreamWarning",
"timeout",
"samplingMethod",
"temperature",
"topP",
"presencePenalty",
"frequencyPenalty",
"useStop",
"stop",
"seed"
]
},
{
key: "storage",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__SECTIONS__STORAGE__LABEL",
defaultCollapsed: true,
fields: [
"storeLocation",
"jsonStreamWarning",
"streamDescription",
"inputKey",
"contextKey",
"immediateOutput",
"streamStopTokens",
"streamStopTokenOverrides",
"streamStoreCopyInInput"
]
},
{
key: "errorHandling",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__SECTIONS__ERROR_HANDLING__LABEL",
defaultCollapsed: true,
fields: [
"logErrorToSystem",
"errorHandling",
"errorMessage",
"errorHandlingGotoTarget",
]
},
{
key: "customOptions",
label: "UI__NODE_EDITOR__SERVICE__GPT_PROMPT__SECTIONS__CUSTOM_OPTIONS__LABEL",
defaultCollapsed: true,
fields: [
"customModelOptions",
"customRequestOptions"
]
},
{
key: "debugging",
label: "UI__NODE_EDITOR__SECTIONS__DEBUG_SETTINGS__LABEL",
defaultCollapsed: true,
fields: [
"debugDescription",
"debugLogTokenCount",
"debugLogRequestAndCompletion",
"debugLogToolDefinitions"
]
},
{
key: "toolSettings",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__SECTIONS__TOOL_SETTINGS__LABEL",
defaultCollapsed: true,
fields: [
"toolChoice",
"useStrict",
],
},
{
key: "imageHandling",
label: "UI__NODE_EDITOR__SERVICE__AI_AGENT_JOB__SECTIONS__IMAGE_HANDLING__LABEL",
defaultCollapsed: true,
fields: [
"processImages",
"transcriptImageHandling"
]
},
],
form: [
{ type: "field", key: "llmProviderReferenceId" },
{ type: "field", key: "prompt" },
{ type: "section", key: "advanced" },
{ type: "section", key: "storage" },
{ type: "section", key: "toolSettings" },
{ type: "section", key: "imageHandling" },
{ type: "section", key: "errorHandling" },
{ type: "section", key: "customOptions" },
{ type: "section", key: "debugging" },
],
appearance: {
color: "#252525",
},
tags: ["ai", "llm", "gpt", "generative ai", "openai", "azure", "prompt"],
function: async ({ cognigy, config, childConfigs, nodeId }) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
const { api, input, flowReferenceId } = cognigy;
const { temperature, maxTokens, topP, presencePenalty, frequencyPenalty, useStop, stop, storeLocation, contextKey, inputKey, timeout, streamStopTokens, streamStopTokenOverrides, debugLogTokenCount, debugLogRequestAndCompletion, debugLogToolDefinitions, llmProviderReferenceId, usePromptMode, chatTranscriptSteps, responseFormat, streamStoreCopyInInput, seed, immediateOutput, customModelOptions, customRequestOptions, errorHandling = "continue", // default behavior for LLM Prompt node was, continue its execution even though an error occurred (deviating it from the SEO node) & do not output an error message on UI explicitly. However, error is always stored in the input or context object. We can use an extra "say" node to output it.
errorHandlingGotoTarget, errorMessage, logErrorToSystem, processImages, transcriptImageHandling, toolChoice, useStrict } = config;
let prompt = config.prompt || "";
const { traceId } = input;
// check if custom variables are used and if they have a length modifier
// works only for a single variable per prompt
if (prompt.includes("@cognigyRecentConversation")) {
let turnLimit;
if (prompt.match(/@cognigyRecentConversation:(\d+)/)) {
// @cognigyRecentConversation has a length modifier (e.g. @cognigyRecentConversation:5), so we just want to return the top 5 turns
turnLimit = Number((_a = prompt.match(/@cognigyRecentConversation:(\d+)/)) === null || _a === void 0 ? void 0 : _a[1]);
}
const recentConversation = (0, prompt_1.createLastConverationString)(cognigy.lastConversationEntries, turnLimit) + "\n";
prompt = prompt.replace(/@cognigyRecentConversation(:\d+)?/, recentConversation);
}
if (prompt.includes("@cognigyRecentUserInputs")) {
let turnLimit;
if (prompt.match(/@cognigyRecentUserInputs:(\d+)/)) {
// @cognigyRecentUserInputs has a length modifier (e.g. @cognigyRecentUserInputs:5), so we just want to return the top 5 entries
turnLimit = Number((_b = prompt.match(/@cognigyRecentUserInputs:(\d+)/)) === null || _b === void 0 ? void 0 : _b[1]);
}
const recentUserInputs = (0, prompt_1.createLastUserInputString)(cognigy.lastConversationEntries, turnLimit) + "\n";
prompt = prompt.replace(/@cognigyRecentUserInputs(:\d+)?/, recentUserInputs);
}
// handle errors from external services, depending on the settings
const handleServiceError = async (error) => {
var _a, _b, _c, _d, _e, _f;
const compactError = {
name: error === null || error === void 0 ? void 0 : error.name,
code: error === null || error === void 0 ? void 0 : error.code,
message: (error === null || error === void 0 ? void 0 : error.message) || error
};
// return the requestId if it exist in the error obj.
if ((_a = error === null || error === void 0 ? void 0 : error.meta) === null || _a === void 0 ? void 0 : _a.requestId) {
compactError["requestId"] = (_b = error === null || error === void 0 ? void 0 : error.meta) === null || _b === void 0 ? void 0 : _b.requestId;
}
if ((_c = error === null || error === void 0 ? void 0 : error.originalErrorDetails) === null || _c === void 0 ? void 0 : _c.code) {
compactError.code = error.originalErrorDetails.code;
}
const errorResponse = {
error: compactError,
};
// add error to context or input
switch (storeLocation) {
case "context":
(_d = api.addToContext) === null || _d === void 0 ? void 0 : _d.call(api, contextKey, errorResponse, "simple");
break;
default:
api.addToInput(inputKey, errorResponse);
}
if (errorHandling === "continue") {
// output the timeout message
if (errorMessage) {
await ((_e = api.output) === null || _e === void 0 ? void 0 : _e.call(api, errorMessage, null));
}
// Continue with default node as next node
const defaultChild = childConfigs === null || childConfigs === void 0 ? void 0 : childConfigs.find(child => child.type === "llmPromptDefault");
if (defaultChild) {
api.setNextNode(defaultChild.id);
}
}
else if (errorHandling === "goto") {
if (!errorHandlingGotoTarget) {
throw new Error("GoTo Target is required");
}
const gotoParams = {
cognigy,
childConfigs: [],
nodeId,
config: {
flowNode: {
flow: errorHandlingGotoTarget.flow,
node: errorHandlingGotoTarget.node,
},
injectedText: input.text,
injectedData: input.data,
executionMode: "continue",
absorbContext: false
}
};
await ((_f = logic_1.GO_TO.function) === null || _f === void 0 ? void 0 : _f.call(logic_1.GO_TO, gotoParams));
}
else {
throw new errors_1.InternalServerError(error === null || error === void 0 ? void 0 : error.message, { traceId });
}
};
try {
const isStreamingChannel = input.channel === "webchat3" || input.channel === "adminconsole";
const _messageId = (0, crypto_1.randomUUID)();
/**
* Retrieve the tool definitions from the child nodes
*/
const { toolIds, toolNames, toolMap, tools } = await (0, createToolDefinitions_1.createToolDefinitions)(childConfigs, api, useStrict);
/**
* Generate Prompt Options
*/
const llmPromptOptions = Object.assign(Object.assign(Object.assign({ prompt,
temperature,
maxTokens,
topP,
presencePenalty,
frequencyPenalty, timeoutInMs: timeout, useCase: "promptNode", stream: storeLocation === "stream", streamOnDataHandler: (text) => {
var _a;
text = isStreamingChannel ? text : text.trim();
if (text) {
// if we got text, we output it, but prevent it from being added to the transcript
(_a = api.output) === null || _a === void 0 ? void 0 : _a.call(api, text, {
_cognigy: {
_preventTranscript: true,
_messageId,
}
});
}
}, streamStopTokens: streamStopTokens || [".", "!", "?", "\\n"], streamStopTokenOverrides, preventNewLineRemoval: isStreamingChannel ? true : false,
// set to true in order to get token usage
detailedResults: true, seed: Number(seed) ? Number(seed) : undefined }, (tools.length > 0 && { tools })), (tools.length > 0 && { toolChoice: toolChoice })), { customModelOptions,
customRequestOptions });
if (useStop) {
llmPromptOptions["stop"] = stop;
}
// llmProviderReferenceId `default` value is not a responseFormat, rather it is LLM Model default selection.
if (llmProviderReferenceId && llmProviderReferenceId !== "default") {
llmPromptOptions["llmProviderReferenceId"] = llmProviderReferenceId;
}
if (processImages) {
llmPromptOptions["imageOptions"] = {
processImages,
transcriptImageHandling
};
}
if (responseFormat && responseFormat !== "default") {
llmPromptOptions["responseFormat"] = responseFormat;
}
let debugPrompt = prompt;
// if we're not using prompt mode, we need to add the system message and the transcript
// this is the equivalent of the old "useChat" mode
if (!usePromptMode) {
const transcript = await api.getTranscript({
limit: chatTranscriptSteps || 50,
rolesWhiteList: [transcripts_1.TranscriptRole.USER, transcripts_1.TranscriptRole.ASSISTANT, transcripts_1.TranscriptRole.TOOL],
excludeDataOnlyMessagesFilter: [transcripts_1.TranscriptRole.ASSISTANT]
});
llmPromptOptions["transcript"] = transcript;
llmPromptOptions["chat"] = [{
role: "system",
content: prompt
}];
}
// Run the LLM Query
const fullLlmResult = await api.runGenerativeAIPrompt(llmPromptOptions, "gptPromptNode");
const { messages } = fullLlmResult, llmResult = __rest(fullLlmResult, ["messages"]);
const isFollowSessionActive = api.getMetadata().isFollowSessionActive;
if (debugLogToolDefinitions) {
(_c = api.logDebugMessage) === null || _c === void 0 ? void 0 : _c.call(api, tools, "UI__DEBUG_MODE__AI_AGENT_JOB__TOOL_DEFINITIONS");
}
// if we're in adminconsole or following a session, process debugging options
(input.endpointType === "adminconsole" || isFollowSessionActive) && (0, prompt_1.writeLLMDebugLogs)("LLM Prompt", debugPrompt, llmResult, debugLogTokenCount, debugLogRequestAndCompletion, cognigy);
if (llmResult.finishReason === "tool_calls" && llmResult.toolCalls.length > 0) {
const mainToolCall = llmResult.toolCalls[0];
let isMcpToolCall = false;
// Find the child node with the toolId of the tool call
let toolChild = childConfigs.find(child => { var _a, _b; return child.type === "llmPromptTool" && ((_a = child.config) === null || _a === void 0 ? void 0 : _a.toolId) && api.parseCognigyScriptText((_b = child.config) === null || _b === void 0 ? void 0 : _b.toolId) === mainToolCall.function.name; });
if (!toolChild && toolMap.has(mainToolCall.function.name)) {
// If the tool call is from an MCP tool, set the next node to the corresponding child node
toolChild = childConfigs.find(child => child.id === toolMap.get(mainToolCall.function.name));
isMcpToolCall = true;
}
if (mainToolCall.function.name !== "retrieve_knowledge" && toolChild === undefined) {
(_d = api.logDebugError) === null || _d === void 0 ? void 0 : _d.call(api, `UI__DEBUG_MODE__AI_AGENT_JOB__TOOL_CALL__ERROR__BODY <b>${mainToolCall.function.name}</b>`, "UI__DEBUG_MODE__AI_AGENT_JOB__TOOL_CALL__ERROR__HEADER");
}
// Add last tool call to session state for loading it from Tool Answer Node
api.updateSessionStateValues({
lastToolCall: Object.assign(Object.assign({ llmProvider: fullLlmResult.llmProvider, aiAgentJobNode: {
flow: flowReferenceId,
node: nodeId,
} }, (isMcpToolCall && {
mcpServerUrl: (_e = toolChild === null || toolChild === void 0 ? void 0 : toolChild.config) === null || _e === void 0 ? void 0 : _e.mcpServerUrl,
timeout: (_f = toolChild === null || toolChild === void 0 ? void 0 : toolChild.config) === null || _f === void 0 ? void 0 : _f.timeout,
mcpToolNode: toolChild === null || toolChild === void 0 ? void 0 : toolChild.id,
})), { toolCall: mainToolCall }),
});
// if there are any parameters/arguments, add them to the input slots
if (mainToolCall.function.arguments) {
input.llmPrompt = Object.assign(Object.assign({}, input.llmPrompt), { toolArgs: Object.assign(Object.assign({}, (_h = (_g = input.llmPrompt) === null || _g === void 0 ? void 0 : _g.toolArgs) !== null && _h !== void 0 ? _h : {}), mainToolCall.function.arguments) });
}
// Debug Message for Tool Calls, configured in the Tool Node
if ((_j = toolChild === null || toolChild === void 0 ? void 0 : toolChild.config) === null || _j === void 0 ? void 0 : _j.debugMessage) {
const messageLines = [`<b>UI__DEBUG_MODE__AI_AGENT_JOB__TOOL_CALL__DEBUG_MESSAGE__HEADER:</b> ${api.parseCognigyScriptText(toolChild.config.toolId)}`];
// Arguments / Parameters Slots
const slots = ((_k = mainToolCall === null || mainToolCall === void 0 ? void 0 : mainToolCall.function) === null || _k === void 0 ? void 0 : _k.arguments) && Object.keys(mainToolCall.function.arguments);
const hasSlots = slots && slots.length > 0;
messageLines.push(`<b>UI__DEBUG_MODE__AI_AGENT_JOB__TOOL_CALL__DEBUG_MESSAGE__SLOTS</b>${hasSlots ? "" : " -"}`);
if (hasSlots) {
slots.forEach(slot => {
let slotValueAsString = mainToolCall.function.arguments[slot];
if (typeof slotValueAsString === "object" && slotValueAsString !== null) {
slotValueAsString = JSON.stringify(slotValueAsString, null, 2);
}
else {
String(slotValueAsString);
}
messageLines.push(`- ${slot}: ${slotValueAsString}`);
});
}
(_l = api.logDebugMessage) === null || _l === void 0 ? void 0 : _l.call(api, messageLines.join("\n"), "UI__DEBUG_MODE__AI_AGENT_JOB__TOOL_CALL__DEBUG_MESSAGE__HEADER");
}
if (toolChild) {
api.setNextNode(toolChild.id);
}
else {
const defaultChild = childConfigs === null || childConfigs === void 0 ? void 0 : childConfigs.find(child => child.type === "llmPromptDefault");
if (defaultChild) {
api.setNextNode(defaultChild.id);
}
}
}
else {
// Default case
const defaultChild = childConfigs === null || childConfigs === void 0 ? void 0 : childConfigs.find(child => child.type === "llmPromptDefault");
if (defaultChild) {
api.setNextNode(defaultChild.id);
}
}
// Optionally output the result immediately
// This will also store it into the output
if (llmResult.result && immediateOutput && !llmPromptOptions.stream) {
// we stringify objects (e.g. results coming from JSON Mode)
// so that the transcript only contains text
const resultToOutput = typeof ((llmResult === null || llmResult === void 0 ? void 0 : llmResult.result) || llmResult) === "object" ? JSON.stringify((llmResult === null || llmResult === void 0 ? void 0 : llmResult.result) || llmResult, undefined, 2) : (llmResult === null || llmResult === void 0 ? void 0 : llmResult.result) || llmResult;
await ((_m = api.output) === null || _m === void 0 ? void 0 : _m.call(api, resultToOutput, {}));
}
else if (llmResult.finishReason && llmPromptOptions.stream) {
// send the finishReason as last output for a stream
(_o = api.output) === null || _o === void 0 ? void 0 : _o.call(api, "", {
_cognigy: {
_preventTranscript: true,
_messageId,
_finishReason: llmResult.finishReason,
}
});
}
// If we are streaming and we got a result, also store it into the transcript, since streamed chunks are not stored there
if (llmResult.result && llmPromptOptions.stream) {
const transcriptContent = {
role: transcripts_1.TranscriptRole.ASSISTANT,
type: transcripts_1.TranscriptEntryType.OUTPUT,
source: "assistant",
payload: {
text: ((llmResult === null || llmResult === void 0 ? void 0 : llmResult.result) || llmResult),
data: {},
}
};
await api.addTranscriptStep(transcriptContent);
}
// Add response to Cognigy Input/Context for further usage
if (storeLocation === "context") {
(_p = api.addToContext) === null || _p === void 0 ? void 0 : _p.call(api, contextKey, llmResult, "simple");
}
else if (storeLocation === "input") {
api.addToInput(inputKey, llmResult);
}
else if (storeLocation === "stream" && streamStoreCopyInInput) {
api.addToInput(inputKey, llmResult);
}
}
catch (error) {
const errorDetailsBase = {
name: error === null || error === void 0 ? void 0 : error.name,
code: (error === null || error === void 0 ? void 0 : error.code) || (error === null || error === void 0 ? void 0 : error.httpStatusCode),
message: (error === null || error === void 0 ? void 0 : error.message) || ((_q = error.originalErrorDetails) === null || _q === void 0 ? void 0 : _q.message),
};
const errorDetails = Object.assign(Object.assign({}, errorDetailsBase), { originalErrorDetails: error === null || error === void 0 ? void 0 : error.originalErrorDetails });
// return the requestId if it exist in the error obj.
if ((_r = error.meta) === null || _r === void 0 ? void 0 : _r.requestId) {
errorDetails["meta"] = {
requestId: (_s = error.meta) === null || _s === void 0 ? void 0 : _s.requestId
};
}
if (logErrorToSystem) {
(_t = api.log) === null || _t === void 0 ? void 0 : _t.call(api, "error", JSON.stringify(errorDetailsBase));
}
(_u = api.logDebugError) === null || _u === void 0 ? void 0 : _u.call(api, errorDetailsBase, "UI__DEBUG_MODE__LLM_PROMPT__ERROR");
await handleServiceError(errorDetails);
return;
}
}
});
//# sourceMappingURL=LLMPromptV2.js.map