Update public/script.js
Browse files- public/script.js +50 -1
public/script.js
CHANGED
|
@@ -940,9 +940,23 @@ async function generateImage(prompt) {
|
|
| 940 |
attempts++;
|
| 941 |
|
| 942 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 943 |
const historyResponse = await fetch(
|
| 944 |
-
`/api/comfyui/history/${promptId}?comfyuiUrl=${encodeURIComponent(AppState.
|
| 945 |
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 946 |
const historyData = await historyResponse.json();
|
| 947 |
|
| 948 |
if (historyData[promptId]?.outputs) {
|
|
@@ -1535,9 +1549,13 @@ ${chapter.content.substring(0, 500)}...
|
|
| 1535 |
|
| 1536 |
async function generateSingleImage(prompt) {
|
| 1537 |
try {
|
|
|
|
|
|
|
|
|
|
| 1538 |
// 应用工作流设置(包括seed)
|
| 1539 |
const workflowToUse = applyWorkflowSettingsBeforeGeneration();
|
| 1540 |
|
|
|
|
| 1541 |
const submitResponse = await fetch('/api/comfyui/prompt', {
|
| 1542 |
method: 'POST',
|
| 1543 |
headers: {
|
|
@@ -1550,8 +1568,17 @@ async function generateSingleImage(prompt) {
|
|
| 1550 |
}),
|
| 1551 |
});
|
| 1552 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1553 |
const submitData = await submitResponse.json();
|
| 1554 |
const promptId = submitData.prompt_id;
|
|
|
|
|
|
|
|
|
|
| 1555 |
|
| 1556 |
// 等待图片生成
|
| 1557 |
return new Promise((resolve) => {
|
|
@@ -1564,17 +1591,36 @@ async function generateSingleImage(prompt) {
|
|
| 1564 |
attempts++;
|
| 1565 |
|
| 1566 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1567 |
const historyResponse = await fetch(
|
| 1568 |
`/api/comfyui/history/${promptId}?comfyuiUrl=${encodeURIComponent(AppState.comfyuiUrl)}`
|
| 1569 |
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1570 |
const historyData = await historyResponse.json();
|
|
|
|
| 1571 |
|
| 1572 |
if (historyData[promptId]?.outputs) {
|
|
|
|
| 1573 |
const outputs = historyData[promptId].outputs;
|
| 1574 |
for (const nodeId in outputs) {
|
| 1575 |
const output = outputs[nodeId];
|
|
|
|
| 1576 |
if (output.images && output.images.length > 0) {
|
| 1577 |
const image = output.images[0];
|
|
|
|
| 1578 |
// 使用服务器代理获取图片
|
| 1579 |
const params = new URLSearchParams({
|
| 1580 |
comfyuiUrl: AppState.comfyuiUrl,
|
|
@@ -1583,13 +1629,16 @@ async function generateSingleImage(prompt) {
|
|
| 1583 |
type: image.type || 'output'
|
| 1584 |
});
|
| 1585 |
const imageUrl = `/api/comfyui/view?${params.toString()}`;
|
|
|
|
| 1586 |
resolve(imageUrl);
|
| 1587 |
return;
|
| 1588 |
}
|
| 1589 |
}
|
|
|
|
| 1590 |
}
|
| 1591 |
setTimeout(poll, 2000);
|
| 1592 |
} catch (error) {
|
|
|
|
| 1593 |
setTimeout(poll, 2000);
|
| 1594 |
}
|
| 1595 |
};
|
|
|
|
| 940 |
attempts++;
|
| 941 |
|
| 942 |
try {
|
| 943 |
+
if (!AppState.comfyuiUrl) {
|
| 944 |
+
console.error('ComfyUI URL未配置');
|
| 945 |
+
showNotification('ComfyUI URL未配置', 'error');
|
| 946 |
+
return;
|
| 947 |
+
}
|
| 948 |
+
|
| 949 |
const historyResponse = await fetch(
|
| 950 |
+
`/api/comfyui/history/${promptId}?comfyuiUrl=${encodeURIComponent(AppState.comfyuiUrl)}`
|
| 951 |
);
|
| 952 |
+
|
| 953 |
+
if (!historyResponse.ok) {
|
| 954 |
+
const errorData = await historyResponse.json().catch(() => ({}));
|
| 955 |
+
console.error('获取历史失败:', historyResponse.status, errorData);
|
| 956 |
+
setTimeout(poll, pollInterval);
|
| 957 |
+
return;
|
| 958 |
+
}
|
| 959 |
+
|
| 960 |
const historyData = await historyResponse.json();
|
| 961 |
|
| 962 |
if (historyData[promptId]?.outputs) {
|
|
|
|
| 1549 |
|
| 1550 |
async function generateSingleImage(prompt) {
|
| 1551 |
try {
|
| 1552 |
+
console.log('开始生成图片,ComfyUI URL:', AppState.comfyuiUrl);
|
| 1553 |
+
console.log('提示词:', prompt);
|
| 1554 |
+
|
| 1555 |
// 应用工作流设置(包括seed)
|
| 1556 |
const workflowToUse = applyWorkflowSettingsBeforeGeneration();
|
| 1557 |
|
| 1558 |
+
console.log('发送工作流到ComfyUI...');
|
| 1559 |
const submitResponse = await fetch('/api/comfyui/prompt', {
|
| 1560 |
method: 'POST',
|
| 1561 |
headers: {
|
|
|
|
| 1568 |
}),
|
| 1569 |
});
|
| 1570 |
|
| 1571 |
+
if (!submitResponse.ok) {
|
| 1572 |
+
const errorData = await submitResponse.json().catch(() => ({}));
|
| 1573 |
+
console.error('提交工作流失败:', submitResponse.status, errorData);
|
| 1574 |
+
throw new Error(errorData.error || `提交失败: ${submitResponse.status}`);
|
| 1575 |
+
}
|
| 1576 |
+
|
| 1577 |
const submitData = await submitResponse.json();
|
| 1578 |
const promptId = submitData.prompt_id;
|
| 1579 |
+
|
| 1580 |
+
console.log('工作流已提交,prompt_id:', promptId);
|
| 1581 |
+
console.log('开始轮询结果...');
|
| 1582 |
|
| 1583 |
// 等待图片生成
|
| 1584 |
return new Promise((resolve) => {
|
|
|
|
| 1591 |
attempts++;
|
| 1592 |
|
| 1593 |
try {
|
| 1594 |
+
if (!AppState.comfyuiUrl) {
|
| 1595 |
+
console.error('ComfyUI URL未配置');
|
| 1596 |
+
resolve(null);
|
| 1597 |
+
return;
|
| 1598 |
+
}
|
| 1599 |
+
|
| 1600 |
+
console.log(`轮询尝试 ${attempts}/15, prompt_id: ${promptId}`);
|
| 1601 |
+
|
| 1602 |
const historyResponse = await fetch(
|
| 1603 |
`/api/comfyui/history/${promptId}?comfyuiUrl=${encodeURIComponent(AppState.comfyuiUrl)}`
|
| 1604 |
);
|
| 1605 |
+
|
| 1606 |
+
if (!historyResponse.ok) {
|
| 1607 |
+
console.error('获取历史失败:', historyResponse.status);
|
| 1608 |
+
setTimeout(poll, 2000);
|
| 1609 |
+
return;
|
| 1610 |
+
}
|
| 1611 |
+
|
| 1612 |
const historyData = await historyResponse.json();
|
| 1613 |
+
console.log('收到历史数据:', historyData[promptId] ? '有数据' : '无数据');
|
| 1614 |
|
| 1615 |
if (historyData[promptId]?.outputs) {
|
| 1616 |
+
console.log('找到输出数据,检查图片...');
|
| 1617 |
const outputs = historyData[promptId].outputs;
|
| 1618 |
for (const nodeId in outputs) {
|
| 1619 |
const output = outputs[nodeId];
|
| 1620 |
+
console.log(`节点 ${nodeId}:`, output);
|
| 1621 |
if (output.images && output.images.length > 0) {
|
| 1622 |
const image = output.images[0];
|
| 1623 |
+
console.log('找到图片:', image);
|
| 1624 |
// 使用服务器代理获取图片
|
| 1625 |
const params = new URLSearchParams({
|
| 1626 |
comfyuiUrl: AppState.comfyuiUrl,
|
|
|
|
| 1629 |
type: image.type || 'output'
|
| 1630 |
});
|
| 1631 |
const imageUrl = `/api/comfyui/view?${params.toString()}`;
|
| 1632 |
+
console.log('生成图片URL:', imageUrl);
|
| 1633 |
resolve(imageUrl);
|
| 1634 |
return;
|
| 1635 |
}
|
| 1636 |
}
|
| 1637 |
+
console.log('输出中没有找到图片数据');
|
| 1638 |
}
|
| 1639 |
setTimeout(poll, 2000);
|
| 1640 |
} catch (error) {
|
| 1641 |
+
console.error('轮询错误:', error);
|
| 1642 |
setTimeout(poll, 2000);
|
| 1643 |
}
|
| 1644 |
};
|