Line data Source code
1 : import { defineStore } from 'pinia'
2 : import { computed, ref } from 'vue'
3 :
4 : import type { ServerConfig } from '@/constants/serverConfig'
5 : import type { ServerConfigValue } from '@comfyorg/settings-core/types'
6 :
7 : type ServerConfigWithValue<T> = ServerConfig<T> & {
8 : /**
9 : * Current value.
10 : */
11 : value: T
12 : /**
13 : * Initial value loaded from settings.
14 : */
15 : initialValue: T
16 : }
17 :
18 2355 : export const useServerConfigStore = defineStore('serverConfig', () => {
19 2301 : const serverConfigById = ref<
20 : Record<string, ServerConfigWithValue<ServerConfigValue>>
21 : >({})
22 2301 : const serverConfigs = computed(() => {
23 0 : return Object.values(serverConfigById.value)
24 : })
25 2301 : const modifiedConfigs = computed<ServerConfigWithValue<ServerConfigValue>[]>(
26 : () => {
27 0 : return serverConfigs.value.filter((config) => {
28 0 : return config.initialValue !== config.value
29 : })
30 : }
31 : )
32 2301 : const revertChanges = () => {
33 0 : for (const config of modifiedConfigs.value) {
34 0 : config.value = config.initialValue
35 : }
36 : }
37 2301 : const serverConfigsByCategory = computed<
38 : Record<string, ServerConfigWithValue<ServerConfigValue>[]>
39 : >(() => {
40 0 : const categories = new Map<
41 : string,
42 : ServerConfigWithValue<ServerConfigValue>[]
43 : >()
44 0 : for (const config of serverConfigs.value) {
45 0 : const category = config.category?.[0] ?? 'General'
46 0 : const configs = categories.get(category) ?? []
47 0 : configs.push(config)
48 0 : categories.set(category, configs)
49 : }
50 0 : return Object.fromEntries(categories)
51 : })
52 2301 : const serverConfigValues = computed<Record<string, ServerConfigValue>>(() => {
53 0 : return Object.fromEntries(
54 : serverConfigs.value.map((config) => {
55 0 : return [
56 : config.id,
57 : config.value === config.defaultValue ||
58 : config.value === null ||
59 : config.value === undefined
60 : ? undefined
61 : : config.value
62 : ]
63 : })
64 : )
65 : })
66 2301 : const launchArgs = computed<Record<string, string>>(() => {
67 0 : const args: Record<
68 : string,
69 : Omit<ServerConfigValue, 'undefined' | 'null'>
70 : > = Object.assign(
71 : {},
72 : ...serverConfigs.value.map((config) => {
73 : // Filter out configs that have the default value or undefined | null value
74 0 : if (
75 : config.value === config.defaultValue ||
76 : config.value === null ||
77 : config.value === undefined
78 : ) {
79 0 : return {}
80 : }
81 0 : return config.getValue
82 : ? config.getValue(config.value)
83 : : { [config.id]: config.value }
84 : })
85 : )
86 :
87 : // Convert true to empty string
88 : // Convert number to string
89 0 : return Object.fromEntries(
90 : Object.entries(args).map(([key, value]) => {
91 0 : if (value === true) {
92 0 : return [key, '']
93 : }
94 0 : return [key, value.toString()]
95 : })
96 : )
97 : })
98 2301 : const commandLineArgs = computed<string>(() => {
99 0 : return Object.entries(launchArgs.value)
100 : .map(([key, value]) => [`--${key}`, value])
101 : .flat()
102 : .filter((arg: string) => arg !== '')
103 : .join(' ')
104 : })
105 :
106 2301 : function loadServerConfig(
107 : configs: ServerConfig<ServerConfigValue>[],
108 : values: Record<string, ServerConfigValue>
109 : ) {
110 2301 : for (const config of configs) {
111 85137 : const value = values[config.id] ?? config.defaultValue
112 85137 : serverConfigById.value[config.id] = {
113 : ...config,
114 : value,
115 : initialValue: value
116 : }
117 : }
118 : }
119 :
120 2301 : return {
121 : serverConfigById,
122 : serverConfigs,
123 : modifiedConfigs,
124 : serverConfigsByCategory,
125 : serverConfigValues,
126 : launchArgs,
127 : commandLineArgs,
128 : revertChanges,
129 : loadServerConfig
130 : }
131 : })
|