@midscene/shared 0.26.7-beta-20250821134240.0 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,7 +16,12 @@ const allConfigFromEnv = ()=>ENV_KEYS.reduce((p, name)=>({
16
16
  class GlobalConfigManager {
17
17
  reset() {
18
18
  this.override = void 0;
19
- this.modelConfigFn = void 0;
19
+ this.modelConfigByIntent = {
20
+ VQA: void 0,
21
+ default: void 0,
22
+ grounding: void 0,
23
+ planning: void 0
24
+ };
20
25
  }
21
26
  getAllConfig() {
22
27
  const envConfig = allConfigFromEnv();
@@ -69,28 +74,34 @@ class GlobalConfigManager {
69
74
  extendMode
70
75
  };
71
76
  }
72
- getModelConfig(intent) {
73
- if (this.modelConfigFn) {
74
- const result = this.modelConfigFn({
75
- intent
76
- });
77
- return {
78
- hasModelConfigFn: true,
79
- result
80
- };
81
- }
82
- return {
83
- hasModelConfigFn: false,
84
- result: void 0
85
- };
77
+ getModelConfigFromFn(intent) {
78
+ return this.modelConfigByIntent[intent];
86
79
  }
87
80
  registerModelConfigFn(modelConfigFn) {
88
81
  if ('function' != typeof modelConfigFn) throw new Error(`modelConfigFn must be a function when registerModelConfigFn, but got with type ${typeof modelConfigFn}`);
89
- this.modelConfigFn = modelConfigFn;
82
+ const intents = [
83
+ 'VQA',
84
+ 'default',
85
+ 'grounding',
86
+ 'planning'
87
+ ];
88
+ for (const i of intents){
89
+ const result = modelConfigFn({
90
+ intent: i
91
+ });
92
+ if (!result) throw new Error(`The agent has an option named modelConfig is a function, but it return ${result} when call with intent ${i}, which should be a object.`);
93
+ this.modelConfigByIntent[i] = result;
94
+ }
90
95
  }
91
96
  constructor(){
92
97
  _define_property(this, "override", void 0);
93
- _define_property(this, "modelConfigFn", void 0);
98
+ _define_property(this, "modelConfigByIntent", void 0);
99
+ this.modelConfigByIntent = {
100
+ VQA: void 0,
101
+ default: void 0,
102
+ grounding: void 0,
103
+ planning: void 0
104
+ };
94
105
  }
95
106
  }
96
107
  const globalConfigManger = new GlobalConfigManager();
@@ -0,0 +1,52 @@
1
+ import { enableDebug } from "../logger.mjs";
2
+ import { assert } from "../utils.mjs";
3
+ import { globalConfigManger } from "./global-config.mjs";
4
+ import { MIDSCENE_DEBUG_AI_PROFILE, MIDSCENE_DEBUG_AI_RESPONSE } from "./types.mjs";
5
+ const maskKey = (key, maskChar = '*')=>{
6
+ if ('string' != typeof key || 0 === key.length) return key;
7
+ const prefixLen = 3;
8
+ const suffixLen = 3;
9
+ const keepLength = prefixLen + suffixLen;
10
+ if (key.length <= keepLength) return key;
11
+ const prefix = key.substring(0, prefixLen);
12
+ const suffix = key.substring(key.length - suffixLen);
13
+ const maskLength = key.length - keepLength;
14
+ const mask = maskChar.repeat(maskLength);
15
+ return `${prefix}${mask}${suffix}`;
16
+ };
17
+ const maskConfig = (config)=>Object.fromEntries(Object.entries(config).map(([key, value])=>[
18
+ key,
19
+ [
20
+ 'openaiApiKey',
21
+ 'azureOpenaiKey',
22
+ 'anthropicApiKey'
23
+ ].includes(key) ? maskKey(value) : value
24
+ ]));
25
+ const initDebugConfig = ()=>{
26
+ const shouldPrintTiming = globalConfigManger.getConfigValueInBoolean(MIDSCENE_DEBUG_AI_PROFILE);
27
+ let debugConfig = '';
28
+ if (shouldPrintTiming) {
29
+ console.warn('MIDSCENE_DEBUG_AI_PROFILE is deprecated, use DEBUG=midscene:ai:profile instead');
30
+ debugConfig = 'ai:profile';
31
+ }
32
+ const shouldPrintAIResponse = globalConfigManger.getConfigValueInBoolean(MIDSCENE_DEBUG_AI_RESPONSE);
33
+ if (shouldPrintAIResponse) {
34
+ console.warn('MIDSCENE_DEBUG_AI_RESPONSE is deprecated, use DEBUG=midscene:ai:response instead');
35
+ debugConfig = debugConfig ? 'ai:*' : 'ai:call';
36
+ }
37
+ if (debugConfig) enableDebug(debugConfig);
38
+ };
39
+ const parseJson = (key, value)=>{
40
+ if (void 0 !== value) try {
41
+ return JSON.parse(value);
42
+ } catch (e) {
43
+ throw new Error(`Failed to parse ${key} as a JSON. ${e.message}`, {
44
+ cause: e
45
+ });
46
+ }
47
+ };
48
+ const createAssert = (modelNameKey, provider, modelName)=>(value, key, modelVendorFlag)=>{
49
+ if (modelName) modelVendorFlag ? assert(value, `The ${key} must be a non-empty string because of the ${modelNameKey} is declared as ${modelName} and ${modelVendorFlag} has also been specified in ${provider}, but got: ${value}. Please check your config.`) : assert(value, `The ${key} must be a non-empty string because of the ${modelNameKey} is declared as ${modelName} in ${provider}, but got: ${value}. Please check your config.`);
50
+ else assert(value, `The ${key} must be a non-empty string, but got: ${value}. Please check your config.`);
51
+ };
52
+ export { createAssert, initDebugConfig, maskConfig, parseJson };
@@ -1,5 +1,6 @@
1
1
  import { globalConfigManger } from "./global-config.mjs";
2
2
  import { decideModelConfig } from "./model-config.mjs";
3
+ import { UITarsModelVersion } from "./parse.mjs";
3
4
  export * from "./utils.mjs";
4
5
  export * from "./types.mjs";
5
- export { decideModelConfig, globalConfigManger };
6
+ export { UITarsModelVersion, decideModelConfig, globalConfigManger };
@@ -1,35 +1,9 @@
1
1
  import { globalConfigManger } from "./global-config.mjs";
2
- import { MIDSCENE_DEBUG_AI_PROFILE, MIDSCENE_DEBUG_AI_RESPONSE, MIDSCENE_USE_DOUBAO_VISION, MIDSCENE_USE_GEMINI, MIDSCENE_USE_QWEN_VL, MIDSCENE_USE_VLM_UI_TARS } from "./types.mjs";
3
2
  import { DEFAULT_MODEL_CONFIG_KEYS, DEFAULT_MODEL_CONFIG_KEYS_LEGACY, GROUNDING_MODEL_CONFIG_KEYS, PLANNING_MODEL_CONFIG_KEYS, VQA_MODEL_CONFIG_KEYS } from "./constants.mjs";
4
- import { enableDebug, getDebug } from "../logger.mjs";
3
+ import { getDebug } from "../logger.mjs";
5
4
  import { assert } from "../utils.mjs";
6
- const initDebugConfig = ()=>{
7
- const shouldPrintTiming = globalConfigManger.getConfigValueInBoolean(MIDSCENE_DEBUG_AI_PROFILE);
8
- let debugConfig = '';
9
- if (shouldPrintTiming) {
10
- console.warn('MIDSCENE_DEBUG_AI_PROFILE is deprecated, use DEBUG=midscene:ai:profile instead');
11
- debugConfig = 'ai:profile';
12
- }
13
- const shouldPrintAIResponse = globalConfigManger.getConfigValueInBoolean(MIDSCENE_DEBUG_AI_RESPONSE);
14
- if (shouldPrintAIResponse) {
15
- console.warn('MIDSCENE_DEBUG_AI_RESPONSE is deprecated, use DEBUG=midscene:ai:response instead');
16
- debugConfig = debugConfig ? 'ai:*' : 'ai:call';
17
- }
18
- if (debugConfig) enableDebug(debugConfig);
19
- };
20
- const createAssert = (modelNameKey, provider, modelName)=>(value, key, modelVendorFlag)=>{
21
- if (modelName) modelVendorFlag ? assert(value, `The ${key} must be a non-empty string because of the ${modelNameKey} is declared as ${modelName} and ${modelVendorFlag} has also been specified in ${provider}, but got: ${value}. Please check your config.`) : assert(value, `The ${key} must be a non-empty string because of the ${modelNameKey} is declared as ${modelName} in ${provider}, but got: ${value}. Please check your config.`);
22
- else assert(value, `The ${key} must be a non-empty string, but got: ${value}. Please check your config.`);
23
- };
24
- const parseJson = (key, value)=>{
25
- if (void 0 !== value) try {
26
- return JSON.parse(value);
27
- } catch (e) {
28
- throw new Error(`Failed to parse ${key} as a JSON. ${e.message}`, {
29
- cause: e
30
- });
31
- }
32
- };
5
+ import { createAssert, initDebugConfig, maskConfig, parseJson } from "./helper.mjs";
6
+ import { parseVlModeAndUiTarsFromGlobalConfig, parseVlModeAndUiTarsFromRaw } from "./parse.mjs";
33
7
  const KEYS_MAP = {
34
8
  VQA: VQA_MODEL_CONFIG_KEYS,
35
9
  grounding: GROUNDING_MODEL_CONFIG_KEYS,
@@ -37,11 +11,12 @@ const KEYS_MAP = {
37
11
  default: DEFAULT_MODEL_CONFIG_KEYS
38
12
  };
39
13
  const decideOpenaiSdkConfig = ({ keys, provider, valueAssert })=>{
14
+ initDebugConfig();
15
+ const debugLog = getDebug('ai:decideModel');
40
16
  const socksProxy = provider[keys.socksProxy];
41
17
  const httpProxy = provider[keys.httpProxy];
42
18
  const vlMode = provider[keys.vlMode];
43
- const debugLog = getDebug('ai:decideModel');
44
- debugLog('enter decideOpenaiSdkConfig', provider, keys);
19
+ debugLog('enter decideOpenaiSdkConfig with keys:', keys);
45
20
  if (provider[keys.openaiUseAzureDeprecated]) {
46
21
  debugLog(`provider has ${keys.openaiUseAzureDeprecated} with value${provider[keys.openaiUseAzureDeprecated]}`);
47
22
  const openaiBaseURL = provider[keys.openaiBaseURL];
@@ -52,7 +27,7 @@ const decideOpenaiSdkConfig = ({ keys, provider, valueAssert })=>{
52
27
  return {
53
28
  socksProxy,
54
29
  httpProxy,
55
- vlMode,
30
+ vlModeRaw: vlMode,
56
31
  openaiUseAzureDeprecated: true,
57
32
  openaiApiKey,
58
33
  openaiBaseURL,
@@ -72,7 +47,7 @@ const decideOpenaiSdkConfig = ({ keys, provider, valueAssert })=>{
72
47
  return {
73
48
  socksProxy,
74
49
  httpProxy,
75
- vlMode,
50
+ vlModeRaw: vlMode,
76
51
  useAzureOpenai: true,
77
52
  azureOpenaiScope,
78
53
  azureOpenaiKey,
@@ -104,41 +79,27 @@ const decideOpenaiSdkConfig = ({ keys, provider, valueAssert })=>{
104
79
  return {
105
80
  socksProxy,
106
81
  httpProxy,
107
- vlMode,
82
+ vlModeRaw: vlMode,
108
83
  openaiBaseURL,
109
84
  openaiApiKey,
110
85
  openaiExtraConfig
111
86
  };
112
87
  }
113
88
  };
114
- const decideVlModelValueFromGlobalConfig = ()=>{
115
- const isDoubao = globalConfigManger.getConfigValueInBoolean(MIDSCENE_USE_DOUBAO_VISION);
116
- const isQwen = globalConfigManger.getConfigValueInBoolean(MIDSCENE_USE_QWEN_VL);
117
- const isUiTars = globalConfigManger.getConfigValueInBoolean(MIDSCENE_USE_VLM_UI_TARS);
118
- const isGemini = globalConfigManger.getConfigValueInBoolean(MIDSCENE_USE_GEMINI);
119
- const enabledModes = [
120
- isDoubao && MIDSCENE_USE_DOUBAO_VISION,
121
- isQwen && MIDSCENE_USE_QWEN_VL,
122
- isUiTars && MIDSCENE_USE_VLM_UI_TARS,
123
- isGemini && MIDSCENE_USE_GEMINI
124
- ].filter(Boolean);
125
- if (enabledModes.length > 1) throw new Error(`Only one vision mode can be enabled at a time. Currently enabled modes: ${enabledModes.join(', ')}. Please disable all but one mode.`);
126
- if (isQwen) return 'qwen-vl';
127
- if (isDoubao) return 'doubao-vision';
128
- if (isGemini) return 'gemini';
129
- if (isUiTars) return 'vlm-ui-tars';
89
+ const getModelDescription = (vlMode, uiTarsVersion)=>{
90
+ if (vlMode) if (uiTarsVersion) return `UI-TARS=${uiTarsVersion}`;
91
+ else return `${vlMode} mode`;
92
+ return '';
130
93
  };
131
94
  const decideModelConfig = (modelPreferences, withAssert)=>{
132
95
  initDebugConfig();
133
96
  const debugLog = getDebug('ai:decideModel');
134
97
  debugLog('modelPreferences', modelPreferences);
135
98
  const { intent } = modelPreferences;
136
- const { hasModelConfigFn, result: modelConfigFromFn } = globalConfigManger.getModelConfig(intent);
137
- debugLog('The return value of agent.modelConfig()', modelConfigFromFn);
138
- if (hasModelConfigFn) {
139
- if (!modelConfigFromFn) throw new Error(`The agent has an option named modelConfig is a function, but it return ${modelConfigFromFn} when call with intent ${intent}, which should be a object.`);
99
+ const modelConfigFromFn = globalConfigManger.getModelConfigFromFn(intent);
100
+ if (modelConfigFromFn) {
101
+ debugLog('decideModelConfig base on agent.modelConfig()');
140
102
  const keysForFn = KEYS_MAP[intent];
141
- debugLog('ChooseOpenaiSdkConfig base on agent.modelConfig()');
142
103
  const candidateModelNameFromConfig = modelConfigFromFn[keysForFn.modelName];
143
104
  debugLog('Got modelName from modelConfigFn', candidateModelNameFromConfig);
144
105
  const chosenKeys = (()=>{
@@ -155,49 +116,61 @@ const decideModelConfig = (modelPreferences, withAssert)=>{
155
116
  provider: modelConfigFromFn,
156
117
  valueAssert: withAssert ? createAssert(chosenKeys.modelName, 'modelConfig', candidateModelNameFromConfig) : ()=>{}
157
118
  });
119
+ const { vlMode, uiTarsVersion } = parseVlModeAndUiTarsFromRaw(result.vlModeRaw);
120
+ const modelDescription = getModelDescription(vlMode, uiTarsVersion);
158
121
  const finalResult = {
159
122
  ...result,
160
123
  modelName: modelConfigFromFn[chosenKeys.modelName],
124
+ vlMode,
125
+ uiTarsVersion,
126
+ modelDescription,
161
127
  from: 'modelConfig'
162
128
  };
163
- debugLog('ChooseOpenaiSdkConfig result:', finalResult);
129
+ debugLog('decideModelConfig result by agent.modelConfig():', maskConfig(finalResult));
164
130
  return finalResult;
165
131
  }
166
132
  const allConfig = globalConfigManger.getAllConfig();
167
133
  const keysForEnv = 'default' === intent ? DEFAULT_MODEL_CONFIG_KEYS_LEGACY : KEYS_MAP[intent];
168
134
  const candidateModelNameFromEnv = allConfig[keysForEnv.modelName];
169
135
  debugLog(`Get value of ${keysForEnv.modelName} from globalConfig`, candidateModelNameFromEnv);
170
- if (candidateModelNameFromEnv) {
171
- debugLog('ChooseOpenaiSdkConfig base on global env config with intent.');
136
+ if ('default' !== intent && allConfig[keysForEnv.modelName]) {
137
+ const modelName = allConfig[keysForEnv.modelName];
138
+ debugLog(`Got intent ${intent} corresponding modelName ${modelName} by key ${keysForEnv.modelName} from globalConfig, will get other config by intent.`);
172
139
  const result = decideOpenaiSdkConfig({
173
140
  keys: keysForEnv,
174
141
  provider: allConfig,
175
- valueAssert: withAssert ? createAssert(keysForEnv.modelName, 'process.env', candidateModelNameFromEnv) : ()=>{}
142
+ valueAssert: withAssert ? createAssert(keysForEnv.modelName, 'process.env', modelName) : ()=>{}
176
143
  });
144
+ const { vlMode, uiTarsVersion } = parseVlModeAndUiTarsFromRaw(result.vlModeRaw);
145
+ const modelDescription = getModelDescription(vlMode, uiTarsVersion);
177
146
  const finalResult = {
178
147
  ...result,
179
- modelName: candidateModelNameFromEnv,
180
- from: 'env'
181
- };
182
- debugLog('ChooseOpenaiSdkConfig result:', finalResult);
183
- return finalResult;
184
- }
185
- {
186
- debugLog('ChooseOpenaiSdkConfig base on global env config without intent.');
187
- const result = decideOpenaiSdkConfig({
188
- keys: DEFAULT_MODEL_CONFIG_KEYS_LEGACY,
189
- provider: allConfig,
190
- valueAssert: withAssert ? createAssert(DEFAULT_MODEL_CONFIG_KEYS_LEGACY.modelName, 'process.env') : ()=>{}
191
- });
192
- const vlMode = decideVlModelValueFromGlobalConfig();
193
- const finalResult = {
194
- ...result,
195
- modelName: allConfig[DEFAULT_MODEL_CONFIG_KEYS_LEGACY.modelName] || 'gpt-4o',
148
+ modelName,
196
149
  vlMode,
197
- from: 'legacy-env'
150
+ uiTarsVersion,
151
+ modelDescription,
152
+ from: 'env'
198
153
  };
199
- debugLog('ChooseOpenaiSdkConfig result:', finalResult);
154
+ debugLog('decideModelConfig result by process.env with intent:', maskConfig(finalResult));
200
155
  return finalResult;
201
156
  }
157
+ debugLog(`decideModelConfig as legacy logic with intent ${intent}.`);
158
+ const result = decideOpenaiSdkConfig({
159
+ keys: DEFAULT_MODEL_CONFIG_KEYS_LEGACY,
160
+ provider: allConfig,
161
+ valueAssert: withAssert ? createAssert(DEFAULT_MODEL_CONFIG_KEYS_LEGACY.modelName, 'process.env') : ()=>{}
162
+ });
163
+ const { vlMode, uiTarsVersion } = parseVlModeAndUiTarsFromGlobalConfig(allConfig);
164
+ const modelDescription = getModelDescription(vlMode, uiTarsVersion);
165
+ const finalResult = {
166
+ ...result,
167
+ modelName: allConfig[DEFAULT_MODEL_CONFIG_KEYS_LEGACY.modelName] || 'gpt-4o',
168
+ vlMode,
169
+ uiTarsVersion,
170
+ modelDescription,
171
+ from: 'legacy-env'
172
+ };
173
+ debugLog('decideModelConfig result by legacy logic:', maskConfig(finalResult));
174
+ return finalResult;
202
175
  };
203
- export { createAssert, decideModelConfig, decideOpenaiSdkConfig, parseJson };
176
+ export { decideModelConfig, decideOpenaiSdkConfig };
@@ -0,0 +1,78 @@
1
+ import { MIDSCENE_USE_DOUBAO_VISION, MIDSCENE_USE_GEMINI, MIDSCENE_USE_QWEN_VL, MIDSCENE_USE_VLM_UI_TARS } from "./types.mjs";
2
+ var parse_UITarsModelVersion = /*#__PURE__*/ function(UITarsModelVersion) {
3
+ UITarsModelVersion["V1_0"] = "1.0";
4
+ UITarsModelVersion["V1_5"] = "1.5";
5
+ UITarsModelVersion["DOUBAO_1_5_15B"] = "doubao-1.5-15B";
6
+ UITarsModelVersion["DOUBAO_1_5_20B"] = "doubao-1.5-20B";
7
+ return UITarsModelVersion;
8
+ }({});
9
+ const vlModeRawValidValues = [
10
+ 'doubao-vision',
11
+ 'gemini',
12
+ 'qwen-vl',
13
+ 'vlm-ui-tars',
14
+ 'vlm-ui-tars-doubao',
15
+ 'vlm-ui-tars-doubao-1.5'
16
+ ];
17
+ const parseVlModeAndUiTarsFromRaw = (vlModeRaw)=>{
18
+ if (!vlModeRaw) return {
19
+ vlMode: void 0,
20
+ uiTarsVersion: void 0
21
+ };
22
+ if (!vlModeRawValidValues.includes(vlModeRaw)) throw new Error(`the value ${vlModeRaw} is not a valid VL_MODE value, must be one of ${vlModeRawValidValues}`);
23
+ const raw = vlModeRaw;
24
+ if ('vlm-ui-tars' === raw) return {
25
+ vlMode: 'vlm-ui-tars',
26
+ uiTarsVersion: "1.0"
27
+ };
28
+ if ('vlm-ui-tars-doubao' === raw || 'vlm-ui-tars-doubao-1.5' === raw) return {
29
+ vlMode: 'vlm-ui-tars',
30
+ uiTarsVersion: "doubao-1.5-20B"
31
+ };
32
+ return {
33
+ vlMode: raw,
34
+ uiTarsVersion: void 0
35
+ };
36
+ };
37
+ const parseVlModeAndUiTarsFromGlobalConfig = (provider)=>{
38
+ const isDoubao = provider[MIDSCENE_USE_DOUBAO_VISION];
39
+ const isQwen = provider[MIDSCENE_USE_QWEN_VL];
40
+ const isUiTars = provider[MIDSCENE_USE_VLM_UI_TARS];
41
+ const isGemini = provider[MIDSCENE_USE_GEMINI];
42
+ const enabledModes = [
43
+ isDoubao && MIDSCENE_USE_DOUBAO_VISION,
44
+ isQwen && MIDSCENE_USE_QWEN_VL,
45
+ isUiTars && MIDSCENE_USE_VLM_UI_TARS,
46
+ isGemini && MIDSCENE_USE_GEMINI
47
+ ].filter(Boolean);
48
+ if (enabledModes.length > 1) throw new Error(`Only one vision mode can be enabled at a time. Currently enabled modes: ${enabledModes.join(', ')}. Please disable all but one mode.`);
49
+ if (isQwen) return {
50
+ vlMode: 'qwen-vl',
51
+ uiTarsVersion: void 0
52
+ };
53
+ if (isDoubao) return {
54
+ vlMode: 'doubao-vision',
55
+ uiTarsVersion: void 0
56
+ };
57
+ if (isGemini) return {
58
+ vlMode: 'gemini',
59
+ uiTarsVersion: void 0
60
+ };
61
+ if (isUiTars) if ('1' === isUiTars) return {
62
+ vlMode: 'vlm-ui-tars',
63
+ uiTarsVersion: "1.0"
64
+ };
65
+ else if ('DOUBAO' === isUiTars || 'DOUBAO-1.5' === isUiTars) return {
66
+ vlMode: 'vlm-ui-tars',
67
+ uiTarsVersion: "doubao-1.5-20B"
68
+ };
69
+ else return {
70
+ vlMode: 'vlm-ui-tars',
71
+ uiTarsVersion: `${isUiTars}`
72
+ };
73
+ return {
74
+ vlMode: void 0,
75
+ uiTarsVersion: void 0
76
+ };
77
+ };
78
+ export { parse_UITarsModelVersion as UITarsModelVersion, parseVlModeAndUiTarsFromGlobalConfig, parseVlModeAndUiTarsFromRaw };
@@ -1,41 +1,12 @@
1
1
  import { globalConfigManger } from "./global-config.mjs";
2
2
  import { decideModelConfig } from "./model-config.mjs";
3
- import { MIDSCENE_OPENAI_INIT_CONFIG_JSON, MIDSCENE_PREFERRED_LANGUAGE, MIDSCENE_USE_VLM_UI_TARS } from "./types.mjs";
4
- var utils_UITarsModelVersion = /*#__PURE__*/ function(UITarsModelVersion) {
5
- UITarsModelVersion["V1_0"] = "1.0";
6
- UITarsModelVersion["V1_5"] = "1.5";
7
- UITarsModelVersion["DOUBAO_1_5_15B"] = "doubao-1.5-15B";
8
- UITarsModelVersion["DOUBAO_1_5_20B"] = "doubao-1.5-20B";
9
- return UITarsModelVersion;
10
- }({});
3
+ import { MIDSCENE_OPENAI_INIT_CONFIG_JSON, MIDSCENE_PREFERRED_LANGUAGE } from "./types.mjs";
11
4
  const uiTarsModelVersion = (modelPreferences)=>{
12
- if ('vlm-ui-tars' !== vlLocateMode(modelPreferences)) return false;
13
- const modelConfig = decideModelConfig(modelPreferences, false);
14
- const { vlMode } = modelConfig;
15
- if ('legacy-env' === modelConfig.from) {
16
- const versionConfig = globalConfigManger.getConfigValue(MIDSCENE_USE_VLM_UI_TARS);
17
- if ('1' === versionConfig) return "1.0";
18
- if ('DOUBAO' === versionConfig || 'DOUBAO-1.5' === versionConfig) return "doubao-1.5-20B";
19
- return `${versionConfig}`;
20
- }
21
- if ('vlm-ui-tars' === vlMode) return "1.0";
22
- if ('vlm-ui-tars-doubao' === vlMode || 'vlm-ui-tars-doubao-1.5' === vlMode) return "doubao-1.5-20B";
23
- throw new Error(`vlMode ${vlMode} is not a expected value.`);
5
+ const { uiTarsVersion } = decideModelConfig(modelPreferences, false);
6
+ return uiTarsVersion;
24
7
  };
25
- const validModeNames = [
26
- 'doubao-vision',
27
- 'gemini',
28
- 'qwen-vl',
29
- 'vlm-ui-tars',
30
- 'vlm-ui-tars-doubao',
31
- 'vlm-ui-tars-doubao-1.5'
32
- ];
33
8
  const vlLocateMode = (modelPreferences)=>{
34
- const modelConfig = decideModelConfig(modelPreferences, false);
35
- if (void 0 === modelConfig.vlMode) return false;
36
- const { vlMode } = modelConfig;
37
- if (!validModeNames.includes(vlMode)) throw new Error(`VL_MODE value ${vlMode} is not a valid VL_MODE value, must be one of ${validModeNames}`);
38
- if ('vlm-ui-tars-doubao' === vlMode || 'vlm-ui-tars-doubao-1.5' === vlMode) return 'vlm-ui-tars';
9
+ const { vlMode } = decideModelConfig(modelPreferences, false);
39
10
  return vlMode;
40
11
  };
41
12
  const getIsUseQwenVl = (modelPreferences)=>{
@@ -69,4 +40,4 @@ const overrideAIConfig = (newConfig, extendMode = false)=>{
69
40
  }
70
41
  globalConfigManger.registerOverride(newConfig, extendMode);
71
42
  };
72
- export { utils_UITarsModelVersion as UITarsModelVersion, getAIConfig, getAIConfigInBoolean, getAIConfigInNumber, getIsUseQwenVl, getModelName, getPreferredLanguage, getUploadTestServerUrl, overrideAIConfig, uiTarsModelVersion, vlLocateMode };
43
+ export { getAIConfig, getAIConfigInBoolean, getAIConfigInNumber, getIsUseQwenVl, getModelName, getPreferredLanguage, getUploadTestServerUrl, overrideAIConfig, uiTarsModelVersion, vlLocateMode };
@@ -44,7 +44,12 @@ const allConfigFromEnv = ()=>external_types_js_namespaceObject.ENV_KEYS.reduce((
44
44
  class GlobalConfigManager {
45
45
  reset() {
46
46
  this.override = void 0;
47
- this.modelConfigFn = void 0;
47
+ this.modelConfigByIntent = {
48
+ VQA: void 0,
49
+ default: void 0,
50
+ grounding: void 0,
51
+ planning: void 0
52
+ };
48
53
  }
49
54
  getAllConfig() {
50
55
  const envConfig = allConfigFromEnv();
@@ -97,28 +102,34 @@ class GlobalConfigManager {
97
102
  extendMode
98
103
  };
99
104
  }
100
- getModelConfig(intent) {
101
- if (this.modelConfigFn) {
102
- const result = this.modelConfigFn({
103
- intent
104
- });
105
- return {
106
- hasModelConfigFn: true,
107
- result
108
- };
109
- }
110
- return {
111
- hasModelConfigFn: false,
112
- result: void 0
113
- };
105
+ getModelConfigFromFn(intent) {
106
+ return this.modelConfigByIntent[intent];
114
107
  }
115
108
  registerModelConfigFn(modelConfigFn) {
116
109
  if ('function' != typeof modelConfigFn) throw new Error(`modelConfigFn must be a function when registerModelConfigFn, but got with type ${typeof modelConfigFn}`);
117
- this.modelConfigFn = modelConfigFn;
110
+ const intents = [
111
+ 'VQA',
112
+ 'default',
113
+ 'grounding',
114
+ 'planning'
115
+ ];
116
+ for (const i of intents){
117
+ const result = modelConfigFn({
118
+ intent: i
119
+ });
120
+ if (!result) throw new Error(`The agent has an option named modelConfig is a function, but it return ${result} when call with intent ${i}, which should be a object.`);
121
+ this.modelConfigByIntent[i] = result;
122
+ }
118
123
  }
119
124
  constructor(){
120
125
  _define_property(this, "override", void 0);
121
- _define_property(this, "modelConfigFn", void 0);
126
+ _define_property(this, "modelConfigByIntent", void 0);
127
+ this.modelConfigByIntent = {
128
+ VQA: void 0,
129
+ default: void 0,
130
+ grounding: void 0,
131
+ planning: void 0
132
+ };
122
133
  }
123
134
  }
124
135
  const globalConfigManger = new GlobalConfigManager();
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ initDebugConfig: ()=>initDebugConfig,
28
+ maskConfig: ()=>maskConfig,
29
+ createAssert: ()=>createAssert,
30
+ parseJson: ()=>parseJson
31
+ });
32
+ const external_logger_js_namespaceObject = require("../logger.js");
33
+ const external_utils_js_namespaceObject = require("../utils.js");
34
+ const external_global_config_js_namespaceObject = require("./global-config.js");
35
+ const external_types_js_namespaceObject = require("./types.js");
36
+ const maskKey = (key, maskChar = '*')=>{
37
+ if ('string' != typeof key || 0 === key.length) return key;
38
+ const prefixLen = 3;
39
+ const suffixLen = 3;
40
+ const keepLength = prefixLen + suffixLen;
41
+ if (key.length <= keepLength) return key;
42
+ const prefix = key.substring(0, prefixLen);
43
+ const suffix = key.substring(key.length - suffixLen);
44
+ const maskLength = key.length - keepLength;
45
+ const mask = maskChar.repeat(maskLength);
46
+ return `${prefix}${mask}${suffix}`;
47
+ };
48
+ const maskConfig = (config)=>Object.fromEntries(Object.entries(config).map(([key, value])=>[
49
+ key,
50
+ [
51
+ 'openaiApiKey',
52
+ 'azureOpenaiKey',
53
+ 'anthropicApiKey'
54
+ ].includes(key) ? maskKey(value) : value
55
+ ]));
56
+ const initDebugConfig = ()=>{
57
+ const shouldPrintTiming = external_global_config_js_namespaceObject.globalConfigManger.getConfigValueInBoolean(external_types_js_namespaceObject.MIDSCENE_DEBUG_AI_PROFILE);
58
+ let debugConfig = '';
59
+ if (shouldPrintTiming) {
60
+ console.warn('MIDSCENE_DEBUG_AI_PROFILE is deprecated, use DEBUG=midscene:ai:profile instead');
61
+ debugConfig = 'ai:profile';
62
+ }
63
+ const shouldPrintAIResponse = external_global_config_js_namespaceObject.globalConfigManger.getConfigValueInBoolean(external_types_js_namespaceObject.MIDSCENE_DEBUG_AI_RESPONSE);
64
+ if (shouldPrintAIResponse) {
65
+ console.warn('MIDSCENE_DEBUG_AI_RESPONSE is deprecated, use DEBUG=midscene:ai:response instead');
66
+ debugConfig = debugConfig ? 'ai:*' : 'ai:call';
67
+ }
68
+ if (debugConfig) (0, external_logger_js_namespaceObject.enableDebug)(debugConfig);
69
+ };
70
+ const parseJson = (key, value)=>{
71
+ if (void 0 !== value) try {
72
+ return JSON.parse(value);
73
+ } catch (e) {
74
+ throw new Error(`Failed to parse ${key} as a JSON. ${e.message}`, {
75
+ cause: e
76
+ });
77
+ }
78
+ };
79
+ const createAssert = (modelNameKey, provider, modelName)=>(value, key, modelVendorFlag)=>{
80
+ if (modelName) if (modelVendorFlag) (0, external_utils_js_namespaceObject.assert)(value, `The ${key} must be a non-empty string because of the ${modelNameKey} is declared as ${modelName} and ${modelVendorFlag} has also been specified in ${provider}, but got: ${value}. Please check your config.`);
81
+ else (0, external_utils_js_namespaceObject.assert)(value, `The ${key} must be a non-empty string because of the ${modelNameKey} is declared as ${modelName} in ${provider}, but got: ${value}. Please check your config.`);
82
+ else (0, external_utils_js_namespaceObject.assert)(value, `The ${key} must be a non-empty string, but got: ${value}. Please check your config.`);
83
+ };
84
+ exports.createAssert = __webpack_exports__.createAssert;
85
+ exports.initDebugConfig = __webpack_exports__.initDebugConfig;
86
+ exports.maskConfig = __webpack_exports__.maskConfig;
87
+ exports.parseJson = __webpack_exports__.parseJson;
88
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
89
+ "createAssert",
90
+ "initDebugConfig",
91
+ "maskConfig",
92
+ "parseJson"
93
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
94
+ Object.defineProperty(exports, '__esModule', {
95
+ value: true
96
+ });