n8n-nodes-gigachat
Version:
A user-friendly GigaChat AI (Sber) nodes for n8n
148 lines • 5.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.prepareGigaTools = prepareGigaTools;
exports.executeGigaTool = executeGigaTool;
exports.formatGigaToolResult = formatGigaToolResult;
async function prepareGigaTools(ctx) {
let tools = [];
try {
const toolsData = await ctx.getInputConnectionData('ai_tool', 0);
if (toolsData) {
tools = Array.isArray(toolsData) ? toolsData : [toolsData];
}
}
catch (error) { }
const functions = [];
if (tools.length > 0) {
for (const tool of tools) {
const toolParams = tool.parameters;
let properties = {};
let required = [];
if (toolParams) {
if (typeof toolParams === 'object') {
properties = toolParams.properties || {};
required = toolParams.required || [];
}
else if (typeof toolParams === 'string') {
try {
const parsed = JSON.parse(toolParams);
properties = parsed.properties || {};
required = parsed.required || [];
}
catch (e) {
properties = {};
required = [];
}
}
}
if (Object.keys(properties).length === 0 && tool.schema) {
const toolSchema = tool.schema;
if (toolSchema && typeof toolSchema === 'object' && toolSchema.properties) {
properties = toolSchema.properties;
required = toolSchema.required || [];
}
}
if (Object.keys(properties).length === 0) {
properties = {
input: {
type: 'string',
description: 'Input for the tool',
},
};
required = ['input'];
}
functions.push({
name: tool.name,
description: tool.description || '',
parameters: {
type: 'object',
properties: properties,
required: required,
},
});
}
}
return { functions, tools };
}
async function executeGigaTool(tool, functionCall) {
var _a;
let functionArgs;
try {
functionArgs =
typeof functionCall.arguments === 'string'
? JSON.parse(functionCall.arguments)
: functionCall.arguments || {};
}
catch (e) {
functionArgs = {};
}
let toolResult;
let toolInput = functionArgs;
if (((_a = tool.constructor) === null || _a === void 0 ? void 0 : _a.name) === 'DynamicTool' || (tool.call && !tool.execute)) {
if (typeof functionArgs === 'object') {
const firstKey = Object.keys(functionArgs)[0];
if (firstKey && typeof functionArgs[firstKey] === 'string') {
toolInput = functionArgs[firstKey];
}
else {
toolInput = JSON.stringify(functionArgs);
}
}
}
if (typeof tool.execute === 'function') {
toolResult = await tool.execute(toolInput);
}
else if (typeof tool.call === 'function') {
toolResult = await tool.call(toolInput);
}
else if (typeof tool.func === 'function') {
toolResult = await tool.func(toolInput);
}
else {
toolResult = { error: 'Tool execution method not found' };
}
return toolResult;
}
function formatGigaToolResult(toolResult, toolInput) {
let parsedResult = toolResult;
if (typeof toolResult === 'string') {
try {
parsedResult = JSON.parse(toolResult);
}
catch (e) {
}
}
let functionResponseContent;
if (Array.isArray(parsedResult) && parsedResult.length === 0) {
functionResponseContent = JSON.stringify({
status: 'no_results',
message: 'No results found in the knowledge base for query: "' + toolInput + '"',
});
}
else if (parsedResult === '[]' || parsedResult === '') {
functionResponseContent = JSON.stringify({
status: 'no_results',
message: 'No results found in the knowledge base for query: "' + toolInput + '"',
});
}
else if (typeof parsedResult === 'string') {
try {
JSON.parse(parsedResult);
functionResponseContent = parsedResult;
}
catch (e) {
functionResponseContent = JSON.stringify({
status: 'success',
result: parsedResult,
});
}
}
else {
functionResponseContent = JSON.stringify({
status: 'success',
result: parsedResult,
});
}
return functionResponseContent;
}
//# sourceMappingURL=Tools.js.map