@activadee/n8n-nodes-gradio-client
Version:
n8n node for connecting to Gradio Spaces
144 lines (143 loc) • 5.81 kB
JavaScript
;
// Node Operations - Business Logic
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeGetSpaceInfo = executeGetSpaceInfo;
exports.executeCallFunction = executeCallFunction;
const n8n_workflow_1 = require("n8n-workflow");
const client_1 = require("./client");
const errors_1 = require("./errors");
const utils_1 = require("./utils");
async function executeGetSpaceInfo(executeFunctions, i, cleanedUrl, headers) {
try {
// Try to get config endpoint
const configUrl = `${cleanedUrl}/gradio_api/config`;
console.log('Getting space info from:', configUrl);
const configResponse = await executeFunctions.helpers.httpRequest({
method: 'GET',
url: configUrl,
headers,
returnFullResponse: true,
ignoreHttpStatusErrors: true,
});
console.log('Config response status:', configResponse.statusCode);
if (configResponse.statusCode === 200) {
return {
json: {
spaceUrl: cleanedUrl,
config: configResponse.body,
available: true,
},
pairedItem: { item: i },
};
}
else {
return {
json: {
spaceUrl: cleanedUrl,
available: false,
error: `HTTP ${configResponse.statusCode}: ${configResponse.body}`,
},
pairedItem: { item: i },
};
}
}
catch (error) {
return {
json: {
spaceUrl: cleanedUrl,
available: false,
error: error instanceof Error ? error.message : String(error),
},
pairedItem: { item: i },
};
}
}
async function executeCallFunction(executeFunctions, i, cleanedUrl, headers) {
const items = executeFunctions.getInputData();
const apiSelection = executeFunctions.getNodeParameter('apiSelection', i);
const apiName = apiSelection === 'manual'
? executeFunctions.getNodeParameter('apiNameManual', i)
: executeFunctions.getNodeParameter('apiName', i);
const inputParametersRaw = executeFunctions.getNodeParameter('inputParameters', i);
const advancedOptions = executeFunctions.getNodeParameter('advancedOptions', i);
const fileOptions = executeFunctions.getNodeParameter('fileOptions', i);
// Parse input parameters - must be array format
let inputParameters;
try {
const parsedData = JSON.parse(inputParametersRaw);
if (!Array.isArray(parsedData)) {
throw new Error('Input parameters must be a JSON array (e.g., ["param1", "param2"])');
}
inputParameters = parsedData;
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Invalid input parameters: ${error instanceof Error ? error.message : String(error)}`);
}
// Handle file upload if enabled
if (fileOptions.enableFileUpload) {
const parameterIndex = fileOptions.parameterIndex;
const filePropertyName = fileOptions.filePropertyName || 'data';
const customFilename = fileOptions.filename;
if (items[i].binary && items[i].binary[filePropertyName]) {
const binaryData = items[i].binary[filePropertyName];
const filename = customFilename || binaryData.fileName || 'file.bin';
const fileData = await (0, utils_1.convertToGradioFile)(executeFunctions, filePropertyName, filename);
// Replace parameter at specified index with file data
if (parameterIndex >= 0 && parameterIndex < inputParameters.length) {
inputParameters[parameterIndex] = fileData;
}
}
}
// Enhanced configuration with adaptive settings
const timeout = advancedOptions.timeout || 120;
const returnFullResponse = advancedOptions.returnFullResponse || false;
const retryAttempts = Math.min(advancedOptions.retryAttempts || 3, 5); // Max 5 retries
const debugMode = advancedOptions.debugMode || false;
const startTime = Date.now();
if (debugMode) {
console.log('🔍 Debug Mode: Enhanced Gradio Client Configuration', {
spaceUrl: cleanedUrl,
apiName,
inputParameters,
timeout,
retryAttempts,
headers: Object.keys(headers)
});
}
// Use the new predict() method with enhanced error handling
let resultData;
try {
resultData = await client_1.GradioClient.predict(executeFunctions, cleanedUrl, apiName, inputParameters, headers, timeout, retryAttempts);
}
catch (error) {
// Enhanced error handling with specific messages
if (error instanceof errors_1.GradioError) {
const contextualMessage = `${error.message} (Space: ${cleanedUrl}, API: ${apiName})`;
throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), contextualMessage);
}
else {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Unexpected error: ${errorMessage}`);
}
}
const duration = Date.now() - startTime;
if (returnFullResponse) {
return {
json: {
data: resultData,
success: true,
duration,
apiName,
spaceUrl: cleanedUrl,
},
pairedItem: { item: i },
};
}
else {
// Return just the data
return {
json: resultData,
pairedItem: { item: i },
};
}
}