Skip to content

Commit 4ee95a8

Browse files
quanruclaude
andauthored
feat(env): add backward compatibility for MIDSCENE_OPENAI_* environment variables (#1388)
Add backward compatibility support for legacy MIDSCENE_OPENAI_* environment variables: - MIDSCENE_OPENAI_INIT_CONFIG_JSON (now MIDSCENE_MODEL_INIT_CONFIG_JSON) - MIDSCENE_OPENAI_HTTP_PROXY (now MIDSCENE_MODEL_HTTP_PROXY) - MIDSCENE_OPENAI_SOCKS_PROXY (now MIDSCENE_MODEL_SOCKS_PROXY) Changes: - Add deprecated constants to types.ts with @deprecated tags - Add legacy variables to MODEL_ENV_KEYS for overrideAIConfig support - Update DEFAULT_MODEL_CONFIG_KEYS_LEGACY to use legacy variable names - Implement priority fallback logic in decide-model-config.ts (new variables take precedence) - Update documentation (zh/en model-provider.mdx) with deprecation notices All 139 tests pass, confirming backward compatibility works correctly. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
1 parent 9894171 commit 4ee95a8

File tree

5 files changed

+80
-18
lines changed

5 files changed

+80
-18
lines changed

apps/site/docs/en/model-provider.mdx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ Some advanced configs are also supported. Usually you don't need to use them.
4545

4646
| Name | Description |
4747
|------|-------------|
48-
| `MIDSCENE_OPENAI_INIT_CONFIG_JSON` | Optional. Custom JSON config for OpenAI SDK initialization |
49-
| `MIDSCENE_OPENAI_HTTP_PROXY` | Optional. HTTP/HTTPS proxy configuration (e.g. `http://127.0.0.1:8080` or `https://proxy.example.com:8080`). This option has higher priority than `MIDSCENE_OPENAI_SOCKS_PROXY` |
50-
| `MIDSCENE_OPENAI_SOCKS_PROXY` | Optional. SOCKS proxy configuration (e.g. "socks5://127.0.0.1:1080") |
48+
| `MIDSCENE_MODEL_INIT_CONFIG_JSON` | Optional (recommended). Custom JSON config for OpenAI SDK initialization |
49+
| `MIDSCENE_MODEL_HTTP_PROXY` | Optional (recommended). HTTP/HTTPS proxy configuration (e.g. `http://127.0.0.1:8080` or `https://proxy.example.com:8080`). This option has higher priority than `MIDSCENE_MODEL_SOCKS_PROXY` |
50+
| `MIDSCENE_MODEL_SOCKS_PROXY` | Optional (recommended). SOCKS proxy configuration (e.g. "socks5://127.0.0.1:1080") |
51+
| `MIDSCENE_OPENAI_INIT_CONFIG_JSON` | Deprecated but still compatible. Recommended to use `MIDSCENE_MODEL_INIT_CONFIG_JSON` |
52+
| `MIDSCENE_OPENAI_HTTP_PROXY` | Deprecated but still compatible. Recommended to use `MIDSCENE_MODEL_HTTP_PROXY` |
53+
| `MIDSCENE_OPENAI_SOCKS_PROXY` | Deprecated but still compatible. Recommended to use `MIDSCENE_MODEL_SOCKS_PROXY` |
5154
| `MIDSCENE_PREFERRED_LANGUAGE` | Optional. The preferred language for the model response. The default is `Chinese` if the current timezone is GMT+8 and `English` otherwise. |
5255
| `MIDSCENE_REPLANNING_CYCLE_LIMIT` | Optional. The maximum number of replanning cycles, default is 10 |
5356
| `OPENAI_MAX_TOKENS` | Optional. Maximum tokens for model response, default is 2048 |

apps/site/docs/zh/model-provider.mdx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ Midscene 默认集成了 OpenAI SDK 调用 AI 服务。使用这个 SDK 限定
4848

4949
| 名称 | 描述 |
5050
|------|-------------|
51-
| `MIDSCENE_OPENAI_INIT_CONFIG_JSON` | 可选。OpenAI SDK 的初始化配置 JSON |
52-
| `MIDSCENE_OPENAI_HTTP_PROXY` | 可选。HTTP/HTTPS 代理配置 (如 `http://127.0.0.1:8080``https://proxy.example.com:8080`)。这个选项优先级高于 `MIDSCENE_OPENAI_SOCKS_PROXY` |
53-
| `MIDSCENE_OPENAI_SOCKS_PROXY` | 可选。SOCKS 代理配置 (如 "socks5://127.0.0.1:1080") |
51+
| `MIDSCENE_MODEL_INIT_CONFIG_JSON` | 可选(推荐)。OpenAI SDK 的初始化配置 JSON |
52+
| `MIDSCENE_MODEL_HTTP_PROXY` | 可选(推荐)。HTTP/HTTPS 代理配置 (如 `http://127.0.0.1:8080``https://proxy.example.com:8080`)。这个选项优先级高于 `MIDSCENE_MODEL_SOCKS_PROXY` |
53+
| `MIDSCENE_MODEL_SOCKS_PROXY` | 可选(推荐)。SOCKS 代理配置 (如 "socks5://127.0.0.1:1080") |
54+
| `MIDSCENE_OPENAI_INIT_CONFIG_JSON` | 已弃用但仍兼容。建议使用 `MIDSCENE_MODEL_INIT_CONFIG_JSON` |
55+
| `MIDSCENE_OPENAI_HTTP_PROXY` | 已弃用但仍兼容。建议使用 `MIDSCENE_MODEL_HTTP_PROXY` |
56+
| `MIDSCENE_OPENAI_SOCKS_PROXY` | 已弃用但仍兼容。建议使用 `MIDSCENE_MODEL_SOCKS_PROXY` |
5457
| `MIDSCENE_PREFERRED_LANGUAGE` | 可选。模型响应的语言。如果当前时区是 GMT+8 则默认是 `Chinese`,否则是 `English` |
5558
| `MIDSCENE_REPLANNING_CYCLE_LIMIT` | 可选。最大重规划次数限制,默认是 10 |
5659
| `OPENAI_MAX_TOKENS` | 可选。模型响应的 max_tokens 数,默认是 2048 |

packages/shared/src/env/constants.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import {
1313
MIDSCENE_MODEL_INIT_CONFIG_JSON,
1414
MIDSCENE_MODEL_NAME,
1515
MIDSCENE_MODEL_SOCKS_PROXY,
16+
MIDSCENE_OPENAI_HTTP_PROXY,
17+
MIDSCENE_OPENAI_INIT_CONFIG_JSON,
18+
MIDSCENE_OPENAI_SOCKS_PROXY,
1619
MIDSCENE_PLANNING_LOCATOR_MODE,
1720
MIDSCENE_PLANNING_MODEL_API_KEY,
1821
MIDSCENE_PLANNING_MODEL_BASE_URL,
@@ -134,16 +137,16 @@ export const DEFAULT_MODEL_CONFIG_KEYS: IModelConfigKeys = {
134137
export const DEFAULT_MODEL_CONFIG_KEYS_LEGACY: IModelConfigKeys = {
135138
modelName: MIDSCENE_MODEL_NAME,
136139
/**
137-
* proxy
140+
* proxy - Uses legacy MIDSCENE_OPENAI_* variables for backward compatibility
138141
*/
139-
socksProxy: MIDSCENE_MODEL_SOCKS_PROXY,
140-
httpProxy: MIDSCENE_MODEL_HTTP_PROXY,
142+
socksProxy: MIDSCENE_OPENAI_SOCKS_PROXY,
143+
httpProxy: MIDSCENE_OPENAI_HTTP_PROXY,
141144
/**
142145
* Model API - Uses legacy OPENAI_* variables for backward compatibility
143146
*/
144147
openaiBaseURL: OPENAI_BASE_URL,
145148
openaiApiKey: OPENAI_API_KEY,
146-
openaiExtraConfig: MIDSCENE_MODEL_INIT_CONFIG_JSON,
149+
openaiExtraConfig: MIDSCENE_OPENAI_INIT_CONFIG_JSON,
147150
/**
148151
* Extra
149152
*/

packages/shared/src/env/decide-model-config.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ import {
1212
PLANNING_MODEL_CONFIG_KEYS,
1313
VQA_MODEL_CONFIG_KEYS,
1414
} from './constants';
15-
import { MODEL_API_KEY, MODEL_BASE_URL } from './types';
15+
import {
16+
MIDSCENE_MODEL_HTTP_PROXY,
17+
MIDSCENE_MODEL_INIT_CONFIG_JSON,
18+
MIDSCENE_MODEL_SOCKS_PROXY,
19+
MIDSCENE_OPENAI_HTTP_PROXY,
20+
MIDSCENE_OPENAI_INIT_CONFIG_JSON,
21+
MIDSCENE_OPENAI_SOCKS_PROXY,
22+
MODEL_API_KEY,
23+
MODEL_BASE_URL,
24+
} from './types';
1625

1726
import { getDebug } from '../logger';
1827
import { assert } from '../utils';
@@ -64,15 +73,16 @@ export const decideOpenaiSdkConfig = ({
6473
initDebugConfig();
6574
const debugLog = getDebug('ai:config');
6675

67-
const socksProxy = provider[keys.socksProxy];
68-
const httpProxy = provider[keys.httpProxy];
6976
const vlMode = provider[keys.vlMode];
7077

7178
debugLog('enter decideOpenaiSdkConfig with keys:', keys);
7279

73-
// Implement compatibility logic: prefer new variable names (MODEL_*), fallback to old ones (OPENAI_*)
80+
// Implement compatibility logic: prefer new variable names (MIDSCENE_MODEL_*), fallback to old ones (MIDSCENE_OPENAI_*)
7481
let openaiBaseURL: string | undefined;
7582
let openaiApiKey: string | undefined;
83+
let socksProxy: string | undefined;
84+
let httpProxy: string | undefined;
85+
let openaiExtraConfigStr: string | undefined;
7686

7787
// When using legacy keys (OPENAI_BASE_URL, OPENAI_API_KEY), check for new names first
7888
if (keys.openaiBaseURL === 'OPENAI_BASE_URL') {
@@ -89,9 +99,36 @@ export const decideOpenaiSdkConfig = ({
8999
openaiApiKey = provider[keys.openaiApiKey];
90100
}
91101

102+
// Proxy compatibility: prefer MIDSCENE_MODEL_* over MIDSCENE_OPENAI_*
103+
if (keys.socksProxy === MIDSCENE_OPENAI_SOCKS_PROXY) {
104+
// Priority: MIDSCENE_MODEL_SOCKS_PROXY > MIDSCENE_OPENAI_SOCKS_PROXY
105+
socksProxy =
106+
provider[MIDSCENE_MODEL_SOCKS_PROXY] || provider[keys.socksProxy];
107+
} else {
108+
socksProxy = provider[keys.socksProxy];
109+
}
110+
111+
if (keys.httpProxy === MIDSCENE_OPENAI_HTTP_PROXY) {
112+
// Priority: MIDSCENE_MODEL_HTTP_PROXY > MIDSCENE_OPENAI_HTTP_PROXY
113+
httpProxy =
114+
provider[MIDSCENE_MODEL_HTTP_PROXY] || provider[keys.httpProxy];
115+
} else {
116+
httpProxy = provider[keys.httpProxy];
117+
}
118+
119+
// Init config compatibility: prefer MIDSCENE_MODEL_INIT_CONFIG_JSON over MIDSCENE_OPENAI_INIT_CONFIG_JSON
120+
if (keys.openaiExtraConfig === MIDSCENE_OPENAI_INIT_CONFIG_JSON) {
121+
// Priority: MIDSCENE_MODEL_INIT_CONFIG_JSON > MIDSCENE_OPENAI_INIT_CONFIG_JSON
122+
openaiExtraConfigStr =
123+
provider[MIDSCENE_MODEL_INIT_CONFIG_JSON] ||
124+
provider[keys.openaiExtraConfig];
125+
} else {
126+
openaiExtraConfigStr = provider[keys.openaiExtraConfig];
127+
}
128+
92129
const openaiExtraConfig = parseJson(
93130
keys.openaiExtraConfig,
94-
provider[keys.openaiExtraConfig],
131+
openaiExtraConfigStr,
95132
);
96133

97134
valueAssert(openaiApiKey, keys.openaiApiKey);

packages/shared/src/env/types.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ export const OPENAI_API_KEY = 'OPENAI_API_KEY';
3030
* @deprecated Use MODEL_BASE_URL instead. This is kept for backward compatibility.
3131
*/
3232
export const OPENAI_BASE_URL = 'OPENAI_BASE_URL';
33+
/**
34+
* @deprecated Use MIDSCENE_MODEL_INIT_CONFIG_JSON instead. This is kept for backward compatibility.
35+
*/
36+
export const MIDSCENE_OPENAI_INIT_CONFIG_JSON =
37+
'MIDSCENE_OPENAI_INIT_CONFIG_JSON';
38+
/**
39+
* @deprecated Use MIDSCENE_MODEL_HTTP_PROXY instead. This is kept for backward compatibility.
40+
*/
41+
export const MIDSCENE_OPENAI_HTTP_PROXY = 'MIDSCENE_OPENAI_HTTP_PROXY';
42+
/**
43+
* @deprecated Use MIDSCENE_MODEL_SOCKS_PROXY instead. This is kept for backward compatibility.
44+
*/
45+
export const MIDSCENE_OPENAI_SOCKS_PROXY = 'MIDSCENE_OPENAI_SOCKS_PROXY';
3346
export const OPENAI_MAX_TOKENS = 'OPENAI_MAX_TOKENS';
3447

3548
export const MIDSCENE_ADB_PATH = 'MIDSCENE_ADB_PATH';
@@ -182,6 +195,9 @@ export const MODEL_ENV_KEYS = [
182195
// model default legacy
183196
OPENAI_API_KEY,
184197
OPENAI_BASE_URL,
198+
MIDSCENE_OPENAI_INIT_CONFIG_JSON,
199+
MIDSCENE_OPENAI_HTTP_PROXY,
200+
MIDSCENE_OPENAI_SOCKS_PROXY,
185201
MODEL_API_KEY,
186202
MODEL_BASE_URL,
187203
// VQA
@@ -311,12 +327,12 @@ export interface IModelConfigForDefaultLegacy {
311327
// model name
312328
[MIDSCENE_MODEL_NAME]: string;
313329
// proxy
314-
[MIDSCENE_MODEL_SOCKS_PROXY]?: string;
315-
[MIDSCENE_MODEL_HTTP_PROXY]?: string;
330+
[MIDSCENE_OPENAI_SOCKS_PROXY]?: string;
331+
[MIDSCENE_OPENAI_HTTP_PROXY]?: string;
316332
// OpenAI
317333
[OPENAI_BASE_URL]?: string;
318334
[OPENAI_API_KEY]?: string;
319-
[MIDSCENE_MODEL_INIT_CONFIG_JSON]?: string;
335+
[MIDSCENE_OPENAI_INIT_CONFIG_JSON]?: string;
320336
// extra
321337
[MIDSCENE_LOCATOR_MODE]?: TVlModeValues;
322338
}

0 commit comments

Comments
 (0)