-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbizyair_tools.js
More file actions
104 lines (97 loc) · 3.24 KB
/
bizyair_tools.js
File metadata and controls
104 lines (97 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { app, ComfyApp } from "../../scripts/app.js";
import { api } from "../../../scripts/api.js";
async function handleFile(json_data) {
const jsonContent = json_data
await app.loadGraphData(
jsonContent,
true,
false,
"convert_test"
);
}
// 检查是否为服务器模式
async function isServerMode() {
const serverModeResponse = await fetch("/bizyair/server_mode");
const serverModeData = await serverModeResponse.json();
return serverModeData.data.server_mode === true;
}
async function convert(){
const p2 = await app.graphToPrompt();
const json = JSON.stringify(p2["workflow"], null, 2);
await api.fetchApi("/bizyair/node_converter", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: json
}).then(response => response.json())
.then(data => handleFile(data))
.catch(error => console.error("Error:", error));
}
// 全局变量,用于节流控制
let lastConvertTime = 0;
const MIN_CONVERT_INTERVAL = 3000; // 最小间隔3秒
// 节流函数确保convert不会被频繁调用
async function throttledConvert() {
const serverMode = await isServerMode();
if (!serverMode) {
return;
}
const now = Date.now();
if (now - lastConvertTime > MIN_CONVERT_INTERVAL) {
lastConvertTime = now;;
convert();
}
}
app.registerExtension({
name: "bizyair.tool",
setup() {
console.log('BizyAir Tools extension setup');
// 添加菜单选项
const orig = LGraphCanvas.prototype.getCanvasMenuOptions;
LGraphCanvas.prototype.getCanvasMenuOptions = function () {
const options = orig.apply(this, arguments);
options.push(null, {
content: "BizyAir Tools",
submenu: {
options: [
{
content: "convert to bizyair node",
callback: async () => {
await convert()
},
},
],
},
});
return options;
};
// 监听导入
const origLoadGraphData = app.loadGraphData;
if (origLoadGraphData) {
app.loadGraphData = async function() {
const result = origLoadGraphData.apply(this, arguments);
const serverMode = await isServerMode();
if (serverMode) {
setTimeout(() => {
throttledConvert();
}, 500);
}
return result;
};
}
},
// 添加init钩子,在ComfyUI初始化后调用convert函数
init() {
console.log('BizyAir Tools initializing...');
lastConvertTime = Date.now(); // 记录初始化时间
// 检查是否为服务器模式
setTimeout(async () => {
const serverMode = await isServerMode();
if (serverMode) {
console.log('服务器模式,页面初始化完成,调用convert');
convert();
}
}, 500);
}
});