@n8n/n8n-nodes-langchain
Version:
114 lines • 5.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.apiRequest = apiRequest;
exports.pollVideoTask = pollVideoTask;
exports.pollVideoTaskV2 = pollVideoTaskV2;
exports.getVideoDownloadUrl = getVideoDownloadUrl;
exports.generateVideo = generateVideo;
const sleep_1 = require("@n8n/utils/sleep");
const n8n_workflow_1 = require("n8n-workflow");
async function apiRequest(method, endpoint, parameters) {
const { body, qs, option, headers } = parameters ?? {};
const credentials = await this.getCredentials('minimaxApi');
const versionedBaseUrl = credentials.url ?? 'https://api.minimax.io/v1';
const baseUrl = versionedBaseUrl.replace(/\/v1\/?$/, '');
const url = `${baseUrl}${endpoint}`;
const options = {
headers: headers ?? {},
method,
body,
qs,
url,
json: true,
};
if (option && Object.keys(option).length !== 0) {
Object.assign(options, option);
}
return await this.helpers.httpRequestWithAuthentication.call(this, 'minimaxApi', options);
}
const VIDEO_TERMINAL_STATUSES = ['Success', 'Fail'];
const VIDEO_V2_TERMINAL_STATUSES = ['succeeded', 'failed', 'cancelled'];
const DEFAULT_POLL_INTERVAL_MS = 15_000;
const MAX_POLL_ATTEMPTS = 60;
async function pollVideoTask(taskId, pollIntervalMs = DEFAULT_POLL_INTERVAL_MS) {
for (let attempt = 0; attempt < MAX_POLL_ATTEMPTS; attempt++) {
const response = await apiRequest.call(this, 'GET', '/v1/query/video_generation', {
qs: { task_id: taskId },
});
const status = response?.status;
if (VIDEO_TERMINAL_STATUSES.includes(status)) {
if (status === 'Fail') {
const errorCode = response?.base_resp?.status_code || 'UNKNOWN';
const errorMessage = response?.base_resp?.status_msg || 'Video generation task failed';
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Task failed: [${errorCode}] ${errorMessage}`);
}
const fileId = response?.file_id;
if (!fileId) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Video generation succeeded but no file_id was returned');
}
return { fileId, status };
}
await (0, sleep_1.sleep)(pollIntervalMs);
}
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Video task ${taskId} did not complete within the maximum polling time. You can query the task manually using the task ID.`);
}
async function pollVideoTaskV2(taskId, pollIntervalMs = DEFAULT_POLL_INTERVAL_MS) {
const abortSignal = this.getExecutionCancelSignal();
for (let attempt = 0; attempt < MAX_POLL_ATTEMPTS; attempt++) {
abortSignal?.throwIfAborted();
const response = (await apiRequest.call(this, 'GET', `/v2/query/video_generation/${taskId}`));
const task = response.task;
const status = task?.status;
if (status && VIDEO_V2_TERMINAL_STATUSES.includes(status)) {
if (status !== 'succeeded') {
const errorCode = task.error?.code ?? 'UNKNOWN';
const errorMessage = task.error?.message ?? `Video generation task was ${status}`;
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Task failed: [${errorCode}] ${errorMessage}`);
}
const videoUrl = task.content?.url;
if (!videoUrl) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Video generation succeeded but no video URL was returned');
}
return { videoUrl, status };
}
await (0, sleep_1.sleep)(pollIntervalMs, abortSignal);
}
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Video task ${taskId} did not complete within the maximum polling time. You can query the task manually using the task ID.`);
}
async function getVideoDownloadUrl(fileId) {
const response = await apiRequest.call(this, 'GET', '/v1/files/retrieve', {
qs: { file_id: fileId },
});
const downloadUrl = response?.file?.download_url;
if (!downloadUrl) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to retrieve download URL for file ${fileId}`);
}
return downloadUrl;
}
async function generateVideo(apiVersion, body) {
if (apiVersion === 'v2') {
const response = (await apiRequest.call(this, 'POST', '/v2/video_generation', {
body,
}));
const taskId = response.task_id;
if (!taskId) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No task_id returned from video generation request');
}
const { videoUrl } = await pollVideoTaskV2.call(this, taskId);
return { videoUrl, taskId };
}
const response = (await apiRequest.call(this, 'POST', '/v1/video_generation', {
body,
}));
if (response.base_resp?.status_code !== 0) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to create video task: ${response.base_resp?.status_msg || 'Unknown error'}`);
}
const taskId = response.task_id;
if (!taskId) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No task_id returned from video generation request');
}
const { fileId } = await pollVideoTask.call(this, taskId);
const videoUrl = await getVideoDownloadUrl.call(this, fileId);
return { videoUrl, taskId, fileId };
}
//# sourceMappingURL=index.js.map