n8n-nodes-sap-ai-core
Version:
n8n nodes for SAP AI Core LLM and embeddings integration
117 lines • 4.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatToSapAiCoreFunction = formatToSapAiCoreFunction;
exports.formatToSapAiCoreTool = formatToSapAiCoreTool;
exports.formatToSapAiCoreAssistantTool = formatToSapAiCoreAssistantTool;
exports.validateSapAiCoreTool = validateSapAiCoreTool;
exports.convertToolsToSapAiCoreFormat = convertToolsToSapAiCoreFormat;
exports.findToolByName = findToolByName;
exports.extractToolNames = extractToolNames;
exports.prepareSapAiCoreToolDescription = prepareSapAiCoreToolDescription;
const zod_to_json_schema_1 = require("zod-to-json-schema");
/**
* Format a tool for SAP AI Core Function format
*/
function formatToSapAiCoreFunction(tool) {
return {
name: tool.name,
description: tool.description,
parameters: (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema || {})
};
}
/**
* Format a tool for SAP AI Core Tool format
* Main format used by SAP AI Core for tool calling
*/
function formatToSapAiCoreTool(tool) {
const schema = (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema || {});
return {
type: "function",
function: {
name: tool.name,
description: tool.description,
parameters: schema
}
};
}
/**
* Format a tool for SAP AI Core Assistant Tool format
* Used specifically for assistant-style interactions
*/
function formatToSapAiCoreAssistantTool(tool) {
return {
type: "function",
function: {
name: tool.name,
description: tool.description,
parameters: (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema || {})
}
};
}
/**
* Validate tool schema for SAP AI Core compatibility
*/
function validateSapAiCoreTool(tool) {
try {
if (!tool.name || typeof tool.name !== 'string') {
return false;
}
if (!tool.description || typeof tool.description !== 'string') {
return false;
}
// Check if schema exists and is valid
const schema = tool.schema;
if (schema) {
(0, zod_to_json_schema_1.zodToJsonSchema)(schema);
}
return true;
}
catch (error) {
console.warn(`Tool validation failed for ${tool.name}:`, error);
return false;
}
}
/**
* Convert tools array to SAP AI Core format
*/
function convertToolsToSapAiCoreFormat(tools) {
return tools
.filter(validateSapAiCoreTool)
.map(formatToSapAiCoreTool);
}
/**
* Get tool by name from formatted tools array
*/
function findToolByName(tools, name) {
return tools.find(tool => { var _a; return ((_a = tool.function) === null || _a === void 0 ? void 0 : _a.name) === name || tool.name === name; });
}
/**
* Extract tool names from formatted tools array
*/
function extractToolNames(tools) {
return tools.map(tool => { var _a; return ((_a = tool.function) === null || _a === void 0 ? void 0 : _a.name) || tool.name; }).filter(Boolean);
}
/**
* Prepare tool description with SAP AI Core specific formatting
*/
function prepareSapAiCoreToolDescription(toolDescription, schema, additionalContext) {
let description = toolDescription;
if (additionalContext) {
description += `\n\nContext: ${additionalContext}`;
}
if (schema && schema.properties) {
const parameters = Object.entries(schema.properties);
if (parameters.length > 0) {
description += `\n\nParameters:`;
parameters.forEach(([name, paramSchema]) => {
var _a;
const required = ((_a = schema.required) === null || _a === void 0 ? void 0 : _a.includes(name)) ? ' (required)' : ' (optional)';
const type = paramSchema.type || 'any';
const paramDesc = paramSchema.description || '';
description += `\n- ${name}${required}: ${type} - ${paramDesc}`;
});
}
}
return description;
}
//# sourceMappingURL=sapAiCoreUtils.js.map