@@ -9,72 +9,79 @@ const BIZYAIR_MODEL_TYPE_KEY = "bizyair_model_type";
99 * @returns {boolean } 如果设置开启则返回 true,否则返回 false
1010 */
1111function shouldShowApiPricingBadge ( ) {
12- try {
13- const app = document . querySelector ( "#vue-app" ) . __vue_app__ ;
14- const pinia = app . config . globalProperties . $pinia ;
15- const settingStore = pinia . _s . get ( "setting" ) ;
16- const showApiPricing = settingStore . get ( "Comfy.NodeBadge.ShowApiPricing" ) ;
17- return showApiPricing !== false ;
18- } catch ( error ) {
19- // 如果无法获取 store 或出错,默认返回 true(保持原有行为)
12+
2013 return true ;
21- }
14+
2215}
2316
2417// 获取node 的模型配置input
2518export async function applyBadgeToNode ( _this , forceRefresh = false ) {
26- const shouldShowBadge = shouldShowApiPricingBadge ( ) ;
27-
28- // 如果不是强制刷新,优先从节点属性中恢复模型类型(用于工作流加载场景)
29- if ( ! forceRefresh && _this . properties && _this . properties [ BIZYAIR_MODEL_TYPE_KEY ] ) {
30- const modelType = _this . properties [ BIZYAIR_MODEL_TYPE_KEY ] ;
31- if ( shouldShowBadge ) {
32- await addPriceBadgeToNode ( _this , modelType ) ;
33- }
34- return ;
35- }
19+ const shouldShowBadge = shouldShowApiPricingBadge ( ) ;
3620
37- // 为包含model输入的节点添加价格徽章
38- const hiddenOutput = hasModelInput ( _this ) ;
39-
40- if ( hiddenOutput ) {
41- if ( ! hiddenOutput . type ) {
42- console . error ( `[modelPrice] error finding model type` ) ;
43- return ;
44- }
45-
46- if ( typeof hiddenOutput . type !== "string" ) {
21+ // 如果不是强制刷新,优先从节点属性中恢复模型类型(用于工作流加载场景)
22+ if ( ! forceRefresh && _this . properties && _this . properties [ BIZYAIR_MODEL_TYPE_KEY ] ) {
23+ const modelType = _this . properties [ BIZYAIR_MODEL_TYPE_KEY ] ;
24+ // if (shouldShowBadge) {
25+ await addPriceBadgeToNode ( _this , modelType ) ;
26+ // }
4727 return ;
4828 }
4929
50- // 解析模型类型(传入节点对象以获取用户选择的model)
51- const modelType = getModelTypeFromHiddenInput ( hiddenOutput , _this ) ;
30+ // 为包含model输入的节点添加价格徽章
31+ const hiddenOutput = hasModelInput ( _this ) ;
5232
53- // 将hiddenOutput临时存储在节点上,方便后续使用
54- _this . _bizyairHiddenOutput = hiddenOutput ;
33+ if ( hiddenOutput ) {
34+ if ( ! hiddenOutput . type ) {
35+ console . error ( `[modelPrice] error finding model type` ) ;
36+ return ;
37+ }
5538
56- // 将模型类型存储到节点属性中,确保保存工作流时能被序列化
57- if ( ! _this . properties ) {
58- _this . properties = { } ;
59- }
60- _this . properties [ BIZYAIR_MODEL_TYPE_KEY ] = modelType ;
39+ if ( typeof hiddenOutput . type !== "string" ) {
40+ return ;
41+ }
6142
62- // 只有在设置开启时才添加badge
63- if ( shouldShowBadge ) {
64- await addPriceBadgeToNode ( _this , modelType ) ;
65- }
43+ // 解析模型类型(传入节点对象以获取用户选择的model)
44+ const modelType = getModelTypeFromHiddenInput ( hiddenOutput , _this ) ;
45+
46+ // 将hiddenOutput临时存储在节点上,方便后续使用
47+ _this . _bizyairHiddenOutput = hiddenOutput ;
48+
49+ // 将模型类型存储到节点属性中,确保保存工作流时能被序列化
50+ if ( ! _this . properties ) {
51+ _this . properties = { } ;
52+ }
53+ _this . properties [ BIZYAIR_MODEL_TYPE_KEY ] = modelType ;
6654
67- // 无论是否添加badge,都要删除无用的outputs
68- _this . outputs = _this . outputs . filter (
69- ( output ) => output . name !== hiddenOutput . name
70- ) ;
55+ // 只有在设置开启时才添加badge
56+ if ( shouldShowBadge ) {
57+ await addPriceBadgeToNode ( _this , modelType ) ;
58+ }
59+
60+ // 无论是否添加badge,都要删除无用的outputs
61+ _this . outputs = _this . outputs . filter (
62+ ( output ) => output . name !== hiddenOutput . name
63+ ) ;
7164 }
7265}
7366
7467// 从hiddenInput中获取模型类型
7568function getModelTypeFromHiddenInput ( hiddenOutput , node ) {
76- const modelJson = JSON . parse ( hiddenOutput . type ) ;
69+ let modelJson ;
70+ try {
71+ modelJson = JSON . parse ( hiddenOutput . type ) ;
72+ } catch ( e ) {
73+ return hiddenOutput . type ;
74+ }
75+ // 从 mode widget 获取 key.当 modelJson 包含 "official" 或 "third-party" 键时,才启用 mode 优先逻辑
76+ const isModeMap = modelJson && ( modelJson . hasOwnProperty ( "official" ) || modelJson . hasOwnProperty ( "third-party" ) ) ;
7777
78+ if ( isModeMap && node . widgets ) {
79+ const modeWidget = node . widgets . find ( w => w . name === "mode" ) ;
80+ if ( modeWidget && modeWidget . value && modelJson [ modeWidget . value ] ) {
81+ return modelJson [ modeWidget . value ] ;
82+ }
83+ }
84+
7885 // 从节点的widgets中获取用户选择的model
7986 let selectedModel = null ;
8087 const possibleWidgetNames = [ "model" , "model_name" ] ;
@@ -94,8 +101,14 @@ function getModelTypeFromHiddenInput(hiddenOutput, node){
94101 }
95102
96103 // 如果没找到或key不存在,回退到使用第一个键(兼容旧逻辑)
97- const modelsList = Object . keys ( modelJson ) ;
98- return modelJson [ modelsList [ 0 ] ] ;
104+ if ( typeof modelJson === 'object' && modelJson !== null ) {
105+ const modelsList = Object . keys ( modelJson ) ;
106+ if ( modelsList . length > 0 ) {
107+ return modelJson [ modelsList [ 0 ] ] ;
108+ }
109+ }
110+ // 如果是字符串或者其他情况,直接返回
111+ return hiddenOutput . type ;
99112}
100113
101114// 自定义节点创建处理函数
0 commit comments