Skip to content

Commit 939e2cf

Browse files
authored
变更生命周期 ,不使用nodeType.prototype.onNodeCreated了 (#149)
* 变更lm_api获取方式 * fix
1 parent ad5bc8f commit 939e2cf

2 files changed

Lines changed: 131 additions & 109 deletions

File tree

bizyui/js/siliconcloud_llm_api.js

Lines changed: 130 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,146 @@
11
import { app } from "../../scripts/app.js";
22
import { getCookie, getIsServerMode } from "./subassembly/tools.js";
33

4-
const createModelFetchExtension = (nodeName, endpoint) => {
5-
return {
6-
name: `bizyair.siliconcloud.${nodeName.toLowerCase()}.api.model_fetch`,
7-
async beforeRegisterNodeDef(nodeType, nodeData, app) {
8-
if (nodeData.name === nodeName) {
9-
const originalNodeCreated = nodeType.prototype.onNodeCreated;
10-
nodeType.prototype.onNodeCreated = async function () {
11-
if (originalNodeCreated) {
12-
originalNodeCreated.apply(this, arguments);
13-
}
14-
15-
const modelWidget = this.widgets.find((w) => w.name === "model");
16-
17-
const fetchModels = async () => {
18-
try {
19-
// 首先检查服务器模式
20-
const isServerMode = await getIsServerMode();
21-
22-
if (isServerMode) {
23-
// 服务器模式,需要token
24-
return new Promise((resolve) => {
25-
const checkToken = () => {
26-
const token = getCookie("auth_token") || getCookie("bizy_token");
27-
if (token) {
28-
clearInterval(timer);
29-
fetchWithToken(token).then(resolve);
30-
}
31-
};
32-
33-
const timer = setInterval(checkToken, 300);
34-
checkToken(); // 立即执行一次检查
35-
});
36-
} else {
37-
// 非服务器模式,直接发送请求
38-
return fetchWithToken();
39-
}
40-
} catch (error) {
41-
console.error(`Error fetching ${nodeName} models`, error);
42-
return [];
43-
}
44-
};
4+
const createModelFetchExtension = (endpoint, app, __this) => {
5+
const modelWidget = __this.widgets.find((w) => w.name === "model");
456

46-
const fetchWithToken = async (token) => {
47-
try {
48-
const headers = {
49-
"Content-Type": "application/json"
50-
};
51-
52-
// 只有在token存在时才添加Authorization头
53-
if (token) {
54-
headers["Authorization"] = token;
55-
}
56-
57-
const response = await fetch(endpoint, {
58-
method: "POST",
59-
headers: headers,
60-
body: JSON.stringify({}),
61-
});
62-
63-
if (response.ok) {
64-
const models = await response.json();
65-
console.debug(`Fetched ${nodeName} models:`, models);
66-
return models;
67-
} else {
68-
console.error(`Failed to fetch ${nodeName} models: ${response.status}`);
69-
return [];
70-
}
71-
} catch (error) {
72-
console.error(`Error fetching ${nodeName} models`, error);
73-
return [];
7+
const fetchModels = async () => {
8+
try {
9+
// 首先检查服务器模式
10+
const isServerMode = await getIsServerMode();
11+
12+
if (isServerMode) {
13+
// 服务器模式,需要token
14+
return new Promise((resolve) => {
15+
const checkToken = () => {
16+
const token = getCookie("auth_token") || getCookie("bizy_token");
17+
if (token) {
18+
clearInterval(timer);
19+
fetchWithToken(token).then(resolve);
7420
}
7521
};
22+
23+
const timer = setInterval(checkToken, 300);
24+
checkToken(); // 立即执行一次检查
25+
});
26+
} else {
27+
// 非服务器模式,直接发送请求
28+
return fetchWithToken();
29+
}
30+
} catch (error) {
31+
console.error(`Error fetching models`, error);
32+
return [];
33+
}
34+
};
35+
36+
const fetchWithToken = async (token) => {
37+
try {
38+
const headers = {
39+
"Content-Type": "application/json"
40+
};
41+
42+
// 只有在token存在时才添加Authorization头
43+
if (token) {
44+
headers["Authorization"] = token;
45+
}
7646

77-
const updateModels = async () => {
78-
const prevValue = modelWidget.value;
79-
modelWidget.value = "";
80-
modelWidget.options.values = [];
47+
const response = await fetch(endpoint, {
48+
method: "POST",
49+
headers: headers,
50+
body: JSON.stringify({}),
51+
});
8152

82-
const models = await fetchModels();
53+
if (response.ok) {
54+
const models = await response.json();
55+
56+
return models;
57+
} else {
58+
59+
return [];
60+
}
61+
} catch (error) {
62+
console.error(`Error fetching models`, error);
63+
return [];
64+
}
65+
};
8366

84-
modelWidget.options.values = models;
85-
console.debug(`Updated ${nodeName} modelWidget.options.values:`, modelWidget.options.values);
67+
const updateModels = async () => {
68+
const prevValue = modelWidget.value;
69+
modelWidget.value = "";
70+
modelWidget.options.values = [];
8671

87-
if (models.includes(prevValue)) {
88-
modelWidget.value = prevValue; // stay on current.
89-
} else if (models.length > 0) {
90-
modelWidget.value = models[0]; // set first as default.
91-
}
72+
const models = await fetchModels();
9273

93-
console.debug(`Updated ${nodeName} modelWidget.value:`, modelWidget.value);
94-
app.graph.setDirtyCanvas(true);
95-
};
74+
modelWidget.options.values = models;
75+
9676

97-
const dummy = async () => {
98-
// calling async method will update the widgets with actual value from the browser and not the default from Node definition.
99-
};
77+
if (models.includes(prevValue)) {
78+
modelWidget.value = prevValue; // stay on current.
79+
} else if (models.length > 0) {
80+
modelWidget.value = models[0]; // set first as default.
81+
}
10082

101-
// Initial update
102-
await dummy(); // this will cause the widgets to obtain the actual value from web page.
103-
await updateModels();
104-
};
105-
}
106-
},
83+
84+
app.graph.setDirtyCanvas(true);
10785
};
86+
87+
const dummy = async () => {
88+
// calling async method will update the widgets with actual value from the browser and not the default from Node definition.
89+
};
90+
91+
// Initial update
92+
dummy(); // this will cause the widgets to obtain the actual value from web page.
93+
updateModels();
10894
};
10995

110-
// LLM Extension
111-
app.registerExtension(
112-
createModelFetchExtension(
113-
"BizyAirSiliconCloudLLMAPI",
114-
"/bizyair/get_silicon_cloud_llm_models"
115-
)
116-
);
117-
118-
// VLM Extension
119-
app.registerExtension(
120-
createModelFetchExtension(
121-
"BizyAirSiliconCloudVLMAPI",
122-
"/bizyair/get_silicon_cloud_vlm_models"
123-
)
124-
);
96+
// ========== Silicon Cloud Models Extension ==========
97+
// 配置映射:统一管理 LLM 和 VLM 的 API 端点
98+
const SILICONCLOUD_MODELS_CONFIG = {
99+
'BizyAirSiliconCloudLLMAPI': {
100+
endpoint: '/bizyair/get_silicon_cloud_llm_models',
101+
name: 'LLM'
102+
},
103+
'BizyAirSiliconCloudVLMAPI': {
104+
endpoint: '/bizyair/get_silicon_cloud_vlm_models',
105+
name: 'VLM'
106+
}
107+
};
108+
109+
// 统一的 Extension - 使用 nodeCreated 钩子避免原型覆盖冲突
110+
app.registerExtension({
111+
name: 'bizyair.siliconcloud.models.unified',
112+
113+
async nodeCreated(node, app) {
114+
const config = SILICONCLOUD_MODELS_CONFIG[node.comfyClass];
115+
116+
if (config && !node._bizyair_model_initialized) {
117+
node._bizyair_model_initialized = true;
118+
119+
// 使用 setTimeout 确保 widgets 已经完全初始化
120+
setTimeout(() => {
121+
try {
122+
createModelFetchExtension(config.endpoint, app, node);
123+
} catch (error) {
124+
console.error(`[BizyAir ${config.name}] Failed to create model fetch extension:`, error);
125+
}
126+
}, 0);
127+
}
128+
},
129+
130+
async loadedGraphNode(node, app) {
131+
// 处理从工作流加载的节点
132+
const config = SILICONCLOUD_MODELS_CONFIG[node.comfyClass];
133+
134+
if (config && !node._bizyair_model_initialized) {
135+
node._bizyair_model_initialized = true;
136+
137+
setTimeout(() => {
138+
try {
139+
createModelFetchExtension(config.endpoint, app, node);
140+
} catch (error) {
141+
console.error(`[BizyAir ${config.name}] Failed to load model fetch extension:`, error);
142+
}
143+
}, 0);
144+
}
145+
}
146+
});

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.2.79
1+
1.2.80

0 commit comments

Comments
 (0)