shuzhiren-comfyui/任务队列后端/outside/outPlatforms/JimuAI.js

190 lines
4.2 KiB
JavaScript

const API_BASE_URL = process.env.JIMUAI_API_BASE_URL || 'https://api.xueai.art';
// 获取生成接口URL
export function getGenerateUrl() {
return `${API_BASE_URL}/workProgresses`;
}
// 获取生成接口请求头
export function getGenerateHeader(apikey) {
const headers = {
'Content-Type': 'application/json'
};
if (apikey) {
headers['WXCZ-ACCESS-KEY'] = apikey;
}
return headers;
}
// 获取生成接口请求体
export function getGenerateBody(task) {
const payload = task.payload;
const apikey = task.apikey;
const posts = {
plat: 'comfyui',
private: true,
stepEvent: true,
standaloneMode: true,
channelName: 'magicps',
workflow: parseInt(payload.workflowId),
params: {
isFullJson: true,
workflowName: 'workflow-#' + payload.workflowId,
resultUpload: {
storageDays: 7,
category: 'generated'
},
imageCreation: {
byModel: 'workflow-#' + payload.workflowId
},
valuesMap: {}
}
};
const params = posts.params;
if (payload.inputs && Array.isArray(payload.inputs)) {
for (const input of payload.inputs) {
const res = {};
switch(input.type) {
case 'image':
case 'video':
case 'audio':
if (input.value && typeof input.value === 'object') {
res.url = input.value.url;
res.fileSize = input.value.fileSize;
}
break;
case 'text':
case 'string':
if (typeof input.value === 'string') {
res.value = input.value;
}
break;
case 'boolean':
if (typeof input.value === 'boolean') {
res.value = input.value;
}
break;
case 'number':
if (typeof input.value === 'number') {
res.value = input.value;
}
break;
}
for (const key of ['id', 'up', 'type']) {
if (input[key] !== undefined) {
res[key] = input[key];
}
}
params.valuesMap[input.id] = res;
}
}
return JSON.stringify(posts);
}
// 获取查询接口URL
export function getQueryUrl(remoteTaskId) {
return `${API_BASE_URL}/workProgresses/${remoteTaskId}/event`;
}
// 获取查询 接口请求头
export function getQueryHeader(apikey) {
const headers = {
'Content-Type': 'application/json'
};
if (apikey) {
headers['WXCZ-ACCESS-KEY'] = apikey;
}
return headers;
}
export function getTaskStatus(response) {
const data = response.data;
if (data && data.status) {
switch(data.status) {
case 'ended':
return true;
case 'error':
case 'timeout':
case 'aborted':
return false;
default:
return null;
}
}
return null;
}
export async function getSuccessTasks(response) {
const res = await response.json();
console.log('积木AI提交任务响应:', res);
if (res.success && res.data && res.data.id) {
return res.data.id;
} else {
return { message: res.message || '任务提交失败', type: 2 };
}
}
export async function getTaskResult(response) {
const res = await response.json();
console.log('积木AI任务结果:', res);
if (res.success && res.data) {
const data = res.data;
const files = [];
if (data.resourceIds) {
const resourceIds = data.resourceIds.split(',');
const apiUrl = API_BASE_URL;
for (const rId of resourceIds) {
files.push(`${apiUrl}/resources/download/${rId}`);
}
}
if (files.length > 0) {
return { files: files.length === 1 ? files[0] : files, type: 1 };
} else {
return { message: '未找到生成结果', type: 2 };
}
} else {
return { message: res.message || '获取任务结果失败', type: 2 };
}
}
export function getStopUrl(taskId) {
return `${API_BASE_URL}/workProgresses/${taskId}`;
}
export function getStopHeader(apikey) {
const headers = {
'Content-Type': 'application/json'
};
if (apikey) {
headers['WXCZ-ACCESS-KEY'] = apikey;
}
return headers;
}
export function getStopBody() {
return JSON.stringify({ status: 'abort' });
}