2024-11-26 16:23:04 +08:00
|
|
|
// config keys
|
|
|
|
|
export const MIDSCENE_OPENAI_INIT_CONFIG_JSON =
|
|
|
|
|
'MIDSCENE_OPENAI_INIT_CONFIG_JSON';
|
|
|
|
|
export const MIDSCENE_MODEL_NAME = 'MIDSCENE_MODEL_NAME';
|
|
|
|
|
export const MIDSCENE_LANGSMITH_DEBUG = 'MIDSCENE_LANGSMITH_DEBUG';
|
|
|
|
|
export const MIDSCENE_DEBUG_AI_PROFILE = 'MIDSCENE_DEBUG_AI_PROFILE';
|
2025-02-26 15:04:28 +08:00
|
|
|
export const MIDSCENE_DEBUG_AI_RESPONSE = 'MIDSCENE_DEBUG_AI_RESPONSE';
|
2024-11-26 16:23:04 +08:00
|
|
|
export const MIDSCENE_DANGEROUSLY_PRINT_ALL_CONFIG =
|
|
|
|
|
'MIDSCENE_DANGEROUSLY_PRINT_ALL_CONFIG';
|
|
|
|
|
export const MIDSCENE_DEBUG_MODE = 'MIDSCENE_DEBUG_MODE';
|
2025-04-22 15:53:31 +08:00
|
|
|
export const MIDSCENE_USE_BRIDGE_MODE = 'MIDSCENE_USE_BRIDGE_MODE';
|
2025-04-01 10:41:31 +08:00
|
|
|
|
|
|
|
|
export const MIDSCENE_FORCE_DEEP_THINK = 'MIDSCENE_FORCE_DEEP_THINK';
|
|
|
|
|
|
2024-12-10 09:24:21 +08:00
|
|
|
export const MIDSCENE_OPENAI_SOCKS_PROXY = 'MIDSCENE_OPENAI_SOCKS_PROXY';
|
2024-11-26 16:23:04 +08:00
|
|
|
export const OPENAI_API_KEY = 'OPENAI_API_KEY';
|
|
|
|
|
export const OPENAI_BASE_URL = 'OPENAI_BASE_URL';
|
2024-12-26 13:24:21 +08:00
|
|
|
export const OPENAI_MAX_TOKENS = 'OPENAI_MAX_TOKENS';
|
2024-12-20 15:18:52 +08:00
|
|
|
|
2024-11-26 16:23:04 +08:00
|
|
|
export const MIDSCENE_CACHE = 'MIDSCENE_CACHE';
|
2025-01-13 14:32:17 +08:00
|
|
|
export const MIDSCENE_USE_VLM_UI_TARS = 'MIDSCENE_USE_VLM_UI_TARS';
|
2025-02-21 09:56:09 +08:00
|
|
|
export const MIDSCENE_USE_QWEN_VL = 'MIDSCENE_USE_QWEN_VL';
|
2025-03-17 19:19:54 +08:00
|
|
|
export const MIDSCENE_USE_DOUBAO_VISION = 'MIDSCENE_USE_DOUBAO_VISION';
|
2025-04-22 10:26:08 +08:00
|
|
|
export const MIDSCENE_USE_GEMINI = 'MIDSCENE_USE_GEMINI';
|
2025-03-17 19:19:54 +08:00
|
|
|
export const MIDSCENE_USE_VL_MODEL = 'MIDSCENE_USE_VL_MODEL';
|
2024-11-26 16:23:04 +08:00
|
|
|
export const MATCH_BY_POSITION = 'MATCH_BY_POSITION';
|
2025-01-03 14:44:30 +08:00
|
|
|
export const MIDSCENE_API_TYPE = 'MIDSCENE-API-TYPE';
|
2024-11-26 16:23:04 +08:00
|
|
|
export const MIDSCENE_REPORT_TAG_NAME = 'MIDSCENE_REPORT_TAG_NAME';
|
|
|
|
|
|
2024-12-20 15:18:52 +08:00
|
|
|
export const MIDSCENE_USE_AZURE_OPENAI = 'MIDSCENE_USE_AZURE_OPENAI';
|
|
|
|
|
export const MIDSCENE_AZURE_OPENAI_SCOPE = 'MIDSCENE_AZURE_OPENAI_SCOPE';
|
|
|
|
|
export const MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON =
|
|
|
|
|
'MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON';
|
|
|
|
|
|
2024-12-31 18:00:20 +08:00
|
|
|
export const AZURE_OPENAI_ENDPOINT = 'AZURE_OPENAI_ENDPOINT';
|
|
|
|
|
export const AZURE_OPENAI_KEY = 'AZURE_OPENAI_KEY';
|
|
|
|
|
export const AZURE_OPENAI_API_VERSION = 'AZURE_OPENAI_API_VERSION';
|
|
|
|
|
export const AZURE_OPENAI_DEPLOYMENT = 'AZURE_OPENAI_DEPLOYMENT';
|
|
|
|
|
|
2024-12-23 12:03:05 +08:00
|
|
|
export const MIDSCENE_USE_ANTHROPIC_SDK = 'MIDSCENE_USE_ANTHROPIC_SDK';
|
|
|
|
|
export const ANTHROPIC_API_KEY = 'ANTHROPIC_API_KEY';
|
|
|
|
|
|
2024-12-20 15:18:52 +08:00
|
|
|
// @deprecated
|
|
|
|
|
export const OPENAI_USE_AZURE = 'OPENAI_USE_AZURE';
|
|
|
|
|
|
2025-04-18 10:47:48 +08:00
|
|
|
export const allConfigFromEnv = () => {
|
2024-11-26 16:23:04 +08:00
|
|
|
return {
|
|
|
|
|
[MIDSCENE_OPENAI_INIT_CONFIG_JSON]:
|
|
|
|
|
process.env[MIDSCENE_OPENAI_INIT_CONFIG_JSON] || undefined,
|
|
|
|
|
[MIDSCENE_MODEL_NAME]: process.env[MIDSCENE_MODEL_NAME] || undefined,
|
|
|
|
|
[MIDSCENE_DEBUG_MODE]: process.env[MIDSCENE_DEBUG_MODE] || undefined,
|
2025-04-01 10:41:31 +08:00
|
|
|
[MIDSCENE_FORCE_DEEP_THINK]:
|
|
|
|
|
process.env[MIDSCENE_FORCE_DEEP_THINK] || undefined,
|
2024-11-26 16:23:04 +08:00
|
|
|
[MIDSCENE_LANGSMITH_DEBUG]:
|
|
|
|
|
process.env[MIDSCENE_LANGSMITH_DEBUG] || undefined,
|
|
|
|
|
[MIDSCENE_DEBUG_AI_PROFILE]:
|
|
|
|
|
process.env[MIDSCENE_DEBUG_AI_PROFILE] || undefined,
|
2025-02-26 15:04:28 +08:00
|
|
|
[MIDSCENE_DEBUG_AI_RESPONSE]:
|
|
|
|
|
process.env[MIDSCENE_DEBUG_AI_RESPONSE] || undefined,
|
2024-11-26 16:23:04 +08:00
|
|
|
[MIDSCENE_DANGEROUSLY_PRINT_ALL_CONFIG]:
|
|
|
|
|
process.env[MIDSCENE_DANGEROUSLY_PRINT_ALL_CONFIG] || undefined,
|
|
|
|
|
[OPENAI_API_KEY]: process.env[OPENAI_API_KEY] || undefined,
|
|
|
|
|
[OPENAI_BASE_URL]: process.env[OPENAI_BASE_URL] || undefined,
|
2024-12-26 13:24:21 +08:00
|
|
|
[OPENAI_MAX_TOKENS]: process.env[OPENAI_MAX_TOKENS] || undefined,
|
2024-11-26 16:23:04 +08:00
|
|
|
[OPENAI_USE_AZURE]: process.env[OPENAI_USE_AZURE] || undefined,
|
|
|
|
|
[MIDSCENE_CACHE]: process.env[MIDSCENE_CACHE] || undefined,
|
|
|
|
|
[MATCH_BY_POSITION]: process.env[MATCH_BY_POSITION] || undefined,
|
|
|
|
|
[MIDSCENE_REPORT_TAG_NAME]:
|
|
|
|
|
process.env[MIDSCENE_REPORT_TAG_NAME] || undefined,
|
2024-12-10 09:24:21 +08:00
|
|
|
[MIDSCENE_OPENAI_SOCKS_PROXY]:
|
|
|
|
|
process.env[MIDSCENE_OPENAI_SOCKS_PROXY] || undefined,
|
2024-12-20 15:18:52 +08:00
|
|
|
[MIDSCENE_USE_AZURE_OPENAI]:
|
|
|
|
|
process.env[MIDSCENE_USE_AZURE_OPENAI] || undefined,
|
|
|
|
|
[MIDSCENE_AZURE_OPENAI_SCOPE]:
|
2024-12-31 18:00:20 +08:00
|
|
|
process.env[MIDSCENE_AZURE_OPENAI_SCOPE] || undefined,
|
2024-12-20 15:18:52 +08:00
|
|
|
[MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON]:
|
|
|
|
|
process.env[MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON] || undefined,
|
2024-12-23 12:03:05 +08:00
|
|
|
[MIDSCENE_USE_ANTHROPIC_SDK]:
|
|
|
|
|
process.env[MIDSCENE_USE_ANTHROPIC_SDK] || undefined,
|
2025-01-13 14:32:17 +08:00
|
|
|
[MIDSCENE_USE_VLM_UI_TARS]:
|
|
|
|
|
process.env[MIDSCENE_USE_VLM_UI_TARS] || undefined,
|
2025-02-21 09:56:09 +08:00
|
|
|
[MIDSCENE_USE_QWEN_VL]: process.env[MIDSCENE_USE_QWEN_VL] || undefined,
|
2025-03-17 19:19:54 +08:00
|
|
|
[MIDSCENE_USE_DOUBAO_VISION]:
|
|
|
|
|
process.env[MIDSCENE_USE_DOUBAO_VISION] || undefined,
|
2025-04-22 10:26:08 +08:00
|
|
|
[MIDSCENE_USE_GEMINI]: process.env[MIDSCENE_USE_GEMINI] || undefined,
|
2025-03-17 19:19:54 +08:00
|
|
|
[MIDSCENE_USE_VL_MODEL]: process.env[MIDSCENE_USE_VL_MODEL] || undefined,
|
2024-12-23 12:03:05 +08:00
|
|
|
[ANTHROPIC_API_KEY]: process.env[ANTHROPIC_API_KEY] || undefined,
|
2024-12-31 18:00:20 +08:00
|
|
|
[AZURE_OPENAI_ENDPOINT]: process.env[AZURE_OPENAI_ENDPOINT] || undefined,
|
|
|
|
|
[AZURE_OPENAI_KEY]: process.env[AZURE_OPENAI_KEY] || undefined,
|
|
|
|
|
[AZURE_OPENAI_API_VERSION]:
|
|
|
|
|
process.env[AZURE_OPENAI_API_VERSION] || undefined,
|
|
|
|
|
[AZURE_OPENAI_DEPLOYMENT]:
|
|
|
|
|
process.env[AZURE_OPENAI_DEPLOYMENT] || undefined,
|
2025-04-22 15:53:31 +08:00
|
|
|
[MIDSCENE_USE_BRIDGE_MODE]:
|
|
|
|
|
process.env[MIDSCENE_USE_BRIDGE_MODE] || undefined,
|
2024-11-26 16:23:04 +08:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-18 16:06:41 +08:00
|
|
|
let globalConfig: Partial<ReturnType<typeof allConfigFromEnv>> | null = null;
|
|
|
|
|
|
|
|
|
|
const getGlobalConfig = () => {
|
|
|
|
|
if (globalConfig === null) {
|
|
|
|
|
globalConfig = allConfigFromEnv();
|
|
|
|
|
}
|
|
|
|
|
return globalConfig;
|
|
|
|
|
};
|
2025-03-17 19:19:54 +08:00
|
|
|
|
|
|
|
|
export const vlLocateMode = ():
|
|
|
|
|
| 'qwen-vl'
|
|
|
|
|
| 'doubao-vision'
|
2025-04-22 10:26:08 +08:00
|
|
|
| 'gemini'
|
2025-04-16 11:12:56 +08:00
|
|
|
| 'vl-model' // not actually in use
|
|
|
|
|
| 'vlm-ui-tars'
|
2025-03-17 19:19:54 +08:00
|
|
|
| false => {
|
2025-04-16 11:12:56 +08:00
|
|
|
const enabledModes = [
|
2025-03-17 19:19:54 +08:00
|
|
|
getAIConfigInBoolean(MIDSCENE_USE_DOUBAO_VISION) &&
|
2025-04-16 11:12:56 +08:00
|
|
|
'MIDSCENE_USE_DOUBAO_VISION',
|
|
|
|
|
getAIConfigInBoolean(MIDSCENE_USE_QWEN_VL) && 'MIDSCENE_USE_QWEN_VL',
|
|
|
|
|
getAIConfigInBoolean(MIDSCENE_USE_VLM_UI_TARS) &&
|
|
|
|
|
'MIDSCENE_USE_VLM_UI_TARS',
|
2025-04-22 10:26:08 +08:00
|
|
|
getAIConfigInBoolean(MIDSCENE_USE_GEMINI) && 'MIDSCENE_USE_GEMINI',
|
2025-04-16 11:12:56 +08:00
|
|
|
].filter(Boolean);
|
|
|
|
|
|
|
|
|
|
if (enabledModes.length > 1) {
|
2025-03-17 19:19:54 +08:00
|
|
|
throw new Error(
|
2025-04-16 11:12:56 +08:00
|
|
|
`Only one vision mode can be enabled at a time. Currently enabled modes: ${enabledModes.join(', ')}. Please disable all but one mode.`,
|
2025-03-17 19:19:54 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (getAIConfigInBoolean(MIDSCENE_USE_QWEN_VL)) {
|
|
|
|
|
return 'qwen-vl';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (getAIConfigInBoolean(MIDSCENE_USE_DOUBAO_VISION)) {
|
|
|
|
|
return 'doubao-vision';
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-22 10:26:08 +08:00
|
|
|
if (getAIConfigInBoolean(MIDSCENE_USE_GEMINI)) {
|
|
|
|
|
return 'gemini';
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-17 19:19:54 +08:00
|
|
|
if (getAIConfigInBoolean(MIDSCENE_USE_VL_MODEL)) {
|
|
|
|
|
return 'vl-model';
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-16 11:12:56 +08:00
|
|
|
if (getAIConfigInBoolean(MIDSCENE_USE_VLM_UI_TARS)) {
|
|
|
|
|
return 'vlm-ui-tars';
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-17 19:19:54 +08:00
|
|
|
return false;
|
|
|
|
|
};
|
2024-11-26 16:23:04 +08:00
|
|
|
|
|
|
|
|
export const getAIConfig = (
|
2025-04-18 16:06:41 +08:00
|
|
|
configKey: keyof ReturnType<typeof allConfigFromEnv>,
|
2024-11-26 16:23:04 +08:00
|
|
|
): string | undefined => {
|
2025-02-21 09:56:09 +08:00
|
|
|
if (configKey === MATCH_BY_POSITION) {
|
2025-03-17 19:19:54 +08:00
|
|
|
throw new Error(
|
|
|
|
|
'MATCH_BY_POSITION is deprecated, use MIDSCENE_USE_VL_MODEL instead',
|
|
|
|
|
);
|
2025-02-21 09:56:09 +08:00
|
|
|
}
|
|
|
|
|
|
2025-04-18 16:06:41 +08:00
|
|
|
return getGlobalConfig()[configKey]?.trim();
|
2024-11-26 16:23:04 +08:00
|
|
|
};
|
|
|
|
|
|
2025-04-18 16:06:41 +08:00
|
|
|
export const getAIConfigInBoolean = (
|
|
|
|
|
configKey: keyof ReturnType<typeof allConfigFromEnv>,
|
|
|
|
|
) => {
|
2025-02-26 15:04:28 +08:00
|
|
|
const config = getAIConfig(configKey) || '';
|
|
|
|
|
return /^(true|1)$/i.test(config);
|
2025-02-21 09:56:09 +08:00
|
|
|
};
|
|
|
|
|
|
2025-04-18 16:06:41 +08:00
|
|
|
export const getAIConfigInJson = (
|
|
|
|
|
configKey: keyof ReturnType<typeof allConfigFromEnv>,
|
|
|
|
|
) => {
|
2024-12-10 09:24:21 +08:00
|
|
|
const config = getAIConfig(configKey);
|
|
|
|
|
try {
|
|
|
|
|
return config ? JSON.parse(config) : undefined;
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Failed to parse json config: ${configKey}. ${error.message}`,
|
|
|
|
|
{
|
|
|
|
|
cause: error,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-26 16:23:04 +08:00
|
|
|
export const overrideAIConfig = (
|
2025-03-17 19:19:54 +08:00
|
|
|
newConfig: Partial<ReturnType<typeof allConfigFromEnv>>,
|
2025-04-17 17:44:11 +08:00
|
|
|
extendMode = false, // true: merge with global config, false: override global config
|
2024-11-26 16:23:04 +08:00
|
|
|
) => {
|
2025-03-17 19:19:54 +08:00
|
|
|
for (const key in newConfig) {
|
|
|
|
|
if (typeof key !== 'string') {
|
|
|
|
|
throw new Error(`Failed to override AI config, invalid key: ${key}`);
|
|
|
|
|
}
|
|
|
|
|
if (typeof newConfig[key as keyof typeof newConfig] === 'object') {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Failed to override AI config, invalid value for key: ${key}, value: ${newConfig[key as keyof typeof newConfig]}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-18 16:06:41 +08:00
|
|
|
const currentConfig = getGlobalConfig();
|
2025-04-17 17:44:11 +08:00
|
|
|
globalConfig = extendMode
|
2025-04-18 16:06:41 +08:00
|
|
|
? { ...currentConfig, ...newConfig }
|
2025-04-17 17:44:11 +08:00
|
|
|
: { ...newConfig };
|
2024-11-26 16:23:04 +08:00
|
|
|
};
|