Skip to content

Commit 920321b

Browse files
Fix/node change price (#157)
* 修复节点信息切换不加载价格问题 * 修复模型定价 改为不只是更改模型才会更改定价 * 添加版本号
1 parent d5c207b commit 920321b

4 files changed

Lines changed: 40 additions & 19 deletions

File tree

bizyui/js/handle_load_nodes.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { addPriceBadgeToNode, hasModelInput } from "./model_price.js";
55
const BIZYAIR_MODEL_TYPE_KEY = "bizyair_model_type";
66

77
// 获取node 的模型配置input
8-
export async function applyBadgeToNode(_this) {
9-
// 优先从节点属性中恢复模型类型(用于工作流加载场景)
10-
if (_this.properties && _this.properties[BIZYAIR_MODEL_TYPE_KEY]) {
8+
export async function applyBadgeToNode(_this, forceRefresh = false) {
9+
// 如果不是强制刷新,优先从节点属性中恢复模型类型(用于工作流加载场景)
10+
if (!forceRefresh && _this.properties && _this.properties[BIZYAIR_MODEL_TYPE_KEY]) {
1111
const modelType = _this.properties[BIZYAIR_MODEL_TYPE_KEY];
1212
await addPriceBadgeToNode(_this, modelType);
1313
return;
@@ -26,8 +26,8 @@ export async function applyBadgeToNode(_this) {
2626
return;
2727
}
2828

29-
// 解析模型类型
30-
const modelType = getModelTypeFromHiddenInput(hiddenOutput);
29+
// 解析模型类型(传入节点对象以获取用户选择的model)
30+
const modelType = getModelTypeFromHiddenInput(hiddenOutput, _this);
3131

3232
// 将hiddenOutput临时存储在节点上,方便后续使用
3333
_this._bizyairHiddenOutput = hiddenOutput;
@@ -48,10 +48,30 @@ export async function applyBadgeToNode(_this) {
4848
}
4949

5050
// 从hiddenInput中获取模型类型
51-
function getModelTypeFromHiddenInput(hiddenOutput){
51+
function getModelTypeFromHiddenInput(hiddenOutput, node){
5252
const modelJson = JSON.parse(hiddenOutput.type);
53+
54+
// 从节点的widgets中获取用户选择的model
55+
let selectedModel = null;
56+
const possibleWidgetNames = ["model", "model_name"];
57+
58+
if (node.widgets && Array.isArray(node.widgets)) {
59+
for (const widget of node.widgets) {
60+
if (possibleWidgetNames.includes(widget.name) && widget.value) {
61+
selectedModel = widget.value;
62+
break;
63+
}
64+
}
65+
}
66+
67+
// 如果找到了用户选择的model,使用它作为key
68+
if (selectedModel && modelJson[selectedModel]) {
69+
return modelJson[selectedModel];
70+
}
71+
72+
// 如果没找到或key不存在,回退到使用第一个键(兼容旧逻辑)
5373
const modelsList = Object.keys(modelJson);
54-
return modelJson[modelsList[0]]
74+
return modelJson[modelsList[0]];
5575
}
5676

5777
// 自定义节点创建处理函数

bizyui/js/handle_node_configure.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import { applyBadgeToNode } from "./handle_load_nodes.js";
33
app.registerExtension({
44
name: "bizyair.handle.node.configure",
55
nodeCreated(node, app) {
6-
const possibleWidgetNames = ["model_name", "model"];
6+
// const possibleWidgetNames = ["model_name", "model"];
77
// 在这里可以拿到变化之后的值,并且也可以拿到node,这时候给node切换badge即可
88
if (node && node.widgets && Array.isArray(node.widgets)) {
99
// 只有model选择的widget才注册callback函数
10+
// 不仅仅是切换model才会修改模型定价,比如切换输入参数也会修改模型定价
1011
node.widgets.forEach((widget) => {
11-
if (possibleWidgetNames.includes(widget.name)) {
12-
widget.callback = async function () {
13-
await applyBadgeToNode(node);
14-
};
15-
}
12+
widget.callback = async function () {
13+
// 用户手动修改widget时,强制刷新badge(不使用缓存的模型类型)
14+
await applyBadgeToNode(node, true);
15+
};
1616
});
1717
}
1818
},

bizyui/js/model_price.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,12 @@ export function getBadgeConfigByCoinType(coinType, priceText) {
146146

147147
/**
148148
* 为节点添加价格徽章
149-
* TODO 重新添加价格徽章的时候 节点元素不能重新render
150149
* @param {Object} node - 节点对象,包含 widgets 和其他节点信息
150+
* @param {string} modelName - 模型名称
151151
*/
152152
export async function addPriceBadgeToNode(node, modelName = "") {
153153
try {
154-
console.log(`[modelPrice] addPriceBadgeToNode`, node);
155-
// 从节点 widgets 中提取价格计算所需的数据 这个也可以封装一个函数
156-
// NODE 上的type 考虑是否也封装一个函数
154+
// 从节点 widgets 中提取价格计算所需的数据
157155
const nodeInputs = {};
158156
if (node.widgets && Array.isArray(node.widgets)) {
159157
node.widgets.forEach((widget) => {
@@ -177,11 +175,13 @@ export async function addPriceBadgeToNode(node, modelName = "") {
177175
priceResult.data.coin_type,
178176
priceResult.data.result
179177
);
178+
180179
// 添加价格徽章
181180
addCustomBadge(node, badgeConfig);
182181
}
183182
} catch (error) {
184183
console.error("添加价格徽章失败:", error);
184+
185185
// 添加错误徽章
186186
addCustomBadge(node, {
187187
text: "价格获取失败",
@@ -219,6 +219,7 @@ export async function fetchNodePrice(model, nodeInputs) {
219219
if (!nodeInputs) {
220220
throw new Error("节点输入信息为空,无法获取价格");
221221
}
222+
222223
// 调用bizyengine价格查询接口
223224
const response = await fetch(
224225
`/bizyair/trd_api_pricing?model=${encodeURIComponent(model)}`,
@@ -230,7 +231,7 @@ export async function fetchNodePrice(model, nodeInputs) {
230231
body: JSON.stringify(nodeInputs),
231232
}
232233
);
233-
234+
234235
if (!response.ok) {
235236
throw new Error(`HTTP error! status: ${response.status}`);
236237
}

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.2.84
1+
1.2.85

0 commit comments

Comments
 (0)