Skip to content

Commit f397d8f

Browse files
committed
Merge branch 'master' into fix/btnStyle
2 parents 61f012f + d20a061 commit f397d8f

4 files changed

Lines changed: 141 additions & 71 deletions

File tree

bizyui/js/bizyair_tools.js

Lines changed: 79 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,60 @@
11
import { app, ComfyApp } from "../../scripts/app.js";
22
import { api } from "../../../scripts/api.js";
33

4-
app.registerExtension({
5-
name: "bizyair.tool",
6-
setup() {
4+
async function handleFile(json_data) {
5+
const jsonContent = json_data
76

8-
async function handleFile(json_data) {
9-
const jsonContent = json_data
7+
await app.loadGraphData(
8+
jsonContent,
9+
true,
10+
false,
11+
"convert_test"
12+
);
13+
}
1014

11-
await app.loadGraphData(
12-
jsonContent,
13-
true,
14-
false,
15-
"convert_test"
16-
);
15+
// 检查是否为服务器模式
16+
async function isServerMode() {
17+
const serverModeResponse = await fetch("/bizyair/server_mode");
18+
const serverModeData = await serverModeResponse.json();
19+
return serverModeData.data.server_mode === true;
20+
}
1721

18-
}
19-
async function convert(){
20-
const p2 = await app.graphToPrompt();
21-
const json = JSON.stringify(p2["workflow"], null, 2);
22+
async function convert(){
23+
const p2 = await app.graphToPrompt();
24+
const json = JSON.stringify(p2["workflow"], null, 2);
25+
await api.fetchApi("/bizyair/node_converter", {
26+
method: "POST",
27+
headers: {
28+
"Content-Type": "application/json",
29+
},
30+
body: json
31+
}).then(response => response.json())
32+
.then(data => handleFile(data))
33+
.catch(error => console.error("Error:", error));
34+
}
2235

23-
await api.fetchApi("/bizyair/node_converter", {
24-
method: "POST",
25-
headers: {
26-
"Content-Type": "application/json",
27-
},
28-
body: json
29-
}).then(response => response.json())
30-
.then(data => handleFile(data))
31-
.catch(error => console.error("Error:", error));
32-
}
33-
// Add canvas menu options
36+
// 全局变量,用于节流控制
37+
let lastConvertTime = 0;
38+
const MIN_CONVERT_INTERVAL = 3000; // 最小间隔3秒
39+
40+
// 节流函数确保convert不会被频繁调用
41+
async function throttledConvert() {
42+
const serverMode = await isServerMode();
43+
if (!serverMode) {
44+
return;
45+
}
46+
const now = Date.now();
47+
if (now - lastConvertTime > MIN_CONVERT_INTERVAL) {
48+
lastConvertTime = now;;
49+
convert();
50+
}
51+
}
52+
53+
app.registerExtension({
54+
name: "bizyair.tool",
55+
setup() {
56+
console.log('BizyAir Tools extension setup');
57+
// 添加菜单选项
3458
const orig = LGraphCanvas.prototype.getCanvasMenuOptions;
3559
LGraphCanvas.prototype.getCanvasMenuOptions = function () {
3660
const options = orig.apply(this, arguments);
@@ -48,7 +72,33 @@ app.registerExtension({
4872
},
4973
});
5074
return options;
51-
};
52-
53-
},
75+
};
76+
// 监听导入
77+
const origLoadGraphData = app.loadGraphData;
78+
if (origLoadGraphData) {
79+
app.loadGraphData = async function() {
80+
const result = origLoadGraphData.apply(this, arguments);
81+
const serverMode = await isServerMode();
82+
if (serverMode) {
83+
setTimeout(() => {
84+
throttledConvert();
85+
}, 500);
86+
}
87+
return result;
88+
};
89+
}
90+
},
91+
// 添加init钩子,在ComfyUI初始化后调用convert函数
92+
init() {
93+
console.log('BizyAir Tools initializing...');
94+
lastConvertTime = Date.now(); // 记录初始化时间
95+
// 检查是否为服务器模式
96+
setTimeout(async () => {
97+
const serverMode = await isServerMode();
98+
if (serverMode) {
99+
console.log('服务器模式,页面初始化完成,调用convert');
100+
convert();
101+
}
102+
}, 500);
103+
}
54104
});

bizyui/js/model_apply.js

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,53 +34,39 @@ const NodeInfoLogger = (function() {
3434
return `${baseUrl}?filename=${filename}&subfolder=&type=${type}&rand=${Math.random()}`;
3535
};
3636

37-
// 获取图片并转换为base64
37+
// 获取图片并转换为base64(根据 server_mode 判断逻辑)
3838
const getImageAsBase64 = async (filename, type) => {
3939
try {
4040
// 检查服务器模式
4141
const serverModeResponse = await fetch("/bizyair/server_mode");
4242
const serverModeData = await serverModeResponse.json();
4343

44-
let token = null;
45-
if (serverModeData.data.server_mode) {
46-
// 服务器模式,需要token
47-
token = await new Promise((resolve) => {
48-
const checkToken = () => {
49-
const token = getCookie("bizy_token");
50-
if (token) {
51-
clearInterval(timer);
52-
resolve(token);
53-
}
54-
};
55-
56-
const timer = setInterval(checkToken, 300);
57-
checkToken(); // 立即执行一次检查
58-
});
44+
// 非服务器模式下,如果 filename 已经是 base64 数据,直接返回
45+
if (!serverModeData.data.server_mode && filename.startsWith('data:')) {
46+
console.log('本地模式:filename 已经是 base64 数据,直接返回');
47+
return filename;
5948
}
60-
61-
const imageUrl = buildImageUrl(filename, type);
62-
const headers = {};
63-
if (token) {
64-
headers["Authorization"] = token;
49+
50+
let imageUrl;
51+
let headers = {};
52+
53+
if (serverModeData.data.server_mode) {
54+
// 服务器模式,改为请求后端转发接口
55+
imageUrl = `/bizyair/proxy_view?filename=${encodeURIComponent(filename)}`;
56+
} else {
57+
// 本地模式,使用原有 buildImageUrl
58+
imageUrl = buildImageUrl(filename, type);
6559
}
6660

67-
const response = await fetch(imageUrl, {
68-
headers: headers
69-
});
61+
const response = await fetch(imageUrl, { headers });
7062
if (!response.ok) {
7163
throw new Error(`获取图片失败: ${response.status} ${response.statusText}`);
7264
}
7365
const blob = await response.blob();
74-
75-
// 将blob转为File对象
66+
// 转 base64
7667
const file = new File([blob], filename, { type: blob.type });
77-
78-
// 使用formatToWebp进行压缩
7968
const { base64: compressedBase64 } = await formatToWebp(file);
80-
8169
return compressedBase64;
82-
83-
8470
} catch (error) {
8571
console.error('获取图片并转换为base64失败:', error);
8672
return null;
@@ -121,17 +107,32 @@ const NodeInfoLogger = (function() {
121107
const blob = new Blob([byteArray], { type: mimeType || base64.split(':')[1].split(';')[0] })
122108
return new File([blob], filename, { type: blob.type })
123109
}
124-
// 构建图片信息对象
125-
const buildImageInfo = (imageData) => {
110+
// 构建图片信息对象(根据 server_mode 判断逻辑)
111+
const buildImageInfo = async (imageData) => {
126112
if (!imageData?.filename) return null;
127-
128-
// 根据节点类型确定图片类型
129-
const type = imageData.type || 'temp';
113+
// 检查服务器模式
114+
const serverModeResponse = await fetch("/bizyair/server_mode");
115+
const serverModeData = await serverModeResponse.json();
116+
const type = imageData.type || 'temp';
130117
const path = `/${type}/${imageData.filename}`;
131-
118+
let url;
119+
120+
if (serverModeData.data.server_mode) {
121+
// 服务器模式,使用 view 接口
122+
url = `/view?filename=${encodeURIComponent(imageData.filename)}`;
123+
} else {
124+
// 本地模式,如果 filename 是 base64 数据,直接使用
125+
if (imageData.filename.startsWith('data:')) {
126+
url = imageData.filename;
127+
} else {
128+
// 否则使用 buildImageUrl 构建 URL
129+
url = buildImageUrl(imageData.filename, type);
130+
}
131+
}
132+
132133
return {
133134
filename: imageData.filename,
134-
url: buildImageUrl(imageData.filename, type),
135+
url: url,
135136
path: path,
136137
type: type
137138
};
@@ -252,7 +253,7 @@ const NodeInfoLogger = (function() {
252253
// 使用异步IIFE处理图片,不阻塞主流程
253254
(async () => {
254255
try {
255-
const imageInfo = buildImageInfo(this.images[0]);
256+
const imageInfo = await buildImageInfo(this.images[0]);
256257

257258
// 检查是否存在全局bizyAirLib对象及logNodeInfo函数
258259
if (typeof bizyAirLib !== 'undefined' && typeof bizyAirLib.logNodeInfo === 'function') {

bizyui/js/subassembly/tools.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const hideWidget = (node, widget_name) => {
99

1010
widget.computeSize = () => [0, -4];
1111
widget.type = "hidden";
12+
widget.hidden=true
1213
widget.options = widget.options || {};
1314
widget.show = () => {
1415
widget.computeSize = originalComputeSize;

src/components/assistant/Sidebar.vue

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
v-if="
5454
message.role === 'assistant' &&
5555
sidebarStore.nodeInfo &&
56-
canApplyToNode(sidebarStore.nodeInfo)
56+
canApplyToNode(sidebarStore.nodeInfo) &&
57+
!serverMode
5758
"
5859
class="image-actions"
5960
>
@@ -344,10 +345,13 @@
344345
}
345346
}
346347
348+
// 服务端模式
349+
const serverMode = ref(false)
350+
347351
// 生图功能
348352
const isGeneratingImage = ref(false)
349353
350-
const generateImageAction = () => {
354+
const generateImageAction = async () => {
351355
if (isLoading.value) return
352356
353357
// 在输入框中添加生成图片前缀
@@ -426,7 +430,10 @@
426430
// 生成成功后,添加带图片的助手消息
427431
const assistantMessage = {
428432
role: 'assistant' as const,
429-
content: `已为您生成图片(点击LoadImage节点可以应用)`,
433+
// 服务端模式下只展示"已为您生成图片"
434+
content: serverMode.value
435+
? '已为您生成图片'
436+
: '已为您生成图片(点击LoadImage节点可以应用)',
430437
time: getCurrentTime(),
431438
hasImage: true,
432439
image: imageUrl
@@ -835,6 +842,17 @@
835842
}
836843
837844
chatMessages.value = [welcomeMessage]
845+
846+
// 异步获取 server_mode
847+
;(async () => {
848+
try {
849+
const res = await fetch('/bizyair/server_mode')
850+
const data = await res.json()
851+
serverMode.value = !!data?.data?.server_mode
852+
} catch (e) {
853+
serverMode.value = false
854+
}
855+
})()
838856
})
839857
</script>
840858

0 commit comments

Comments
 (0)