Skip to content

Commit 2171f89

Browse files
committed
feat: add request timeout and retry controls
1 parent b660dbf commit 2171f89

9 files changed

Lines changed: 654 additions & 189 deletions

File tree

docs/configuration.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Deep Code 使用 `settings.json` 设置文件进行持久化配置,支持两
3131
| `thinkingEnabled` | boolean | 是否启用思考模式(DeepSeek V4 系列默认启用) |
3232
| `reasoningEffort` | string | 推理强度,可选 `"high"``"max"`(默认 `"max"`|
3333
| `debugLogEnabled` | boolean | 是否启用调试日志输出(默认 `false`|
34+
| `requestTimeoutMs` | number | 主模型请求超时时间,单位毫秒。`0` 表示不启用超时(默认 `0`|
35+
| `maxRetries` | number | 主模型请求遇到临时网络错误时的最大重试次数(默认 `2`,最大 `10`|
3436
| `notify` | string | 任务完成通知脚本的完整路径(如 Slack 通知脚本) |
3537
| `webSearchTool` | string | 自定义联网搜索脚本的完整路径 |
3638
| `mcpServers` | object | MCP 服务器配置(键为服务名,值为 McpServerConfig 对象) |
@@ -45,6 +47,8 @@ Deep Code 使用 `settings.json` 设置文件进行持久化配置,支持两
4547
| `THINKING_ENABLED` | string | 是否启用思考模式 |
4648
| `REASONING_EFFORT` | string | 推理强度 |
4749
| `DEBUG_LOG_ENABLED` | string | 是否启用调试日志输出 |
50+
| `REQUEST_TIMEOUT_MS` | string | 主模型请求超时时间,单位毫秒。`0` 表示不启用超时 |
51+
| `MAX_RETRIES` | string | 主模型请求遇到临时网络错误时的最大重试次数 |
4852
| `<其他任意KEY>` | string | 自定义环境变量 |
4953

5054
#### `thinkingEnabled` — 思考模式
@@ -130,6 +134,22 @@ MCP(Model Context Protocol)服务器配置。值是键值对,键为服务
130134

131135
设为 `true` 可让程序输出详细的调试日志(默认 `false`),用于排查 API 调用和工具执行的问题。
132136

137+
#### `requestTimeoutMs``maxRetries` — 请求超时与重试
138+
139+
用于控制主模型 Chat Completion 请求的超时和自动重试:
140+
141+
```json
142+
{
143+
"requestTimeoutMs": 300000,
144+
"maxRetries": 2
145+
}
146+
```
147+
148+
- `requestTimeoutMs` 单位为毫秒。默认 `0` 表示不启用请求超时,避免长时间思考或长输出被意外中断。
149+
- `maxRetries` 默认 `2`,最大 `10`。仅对临时错误重试,例如网络断开、请求超时、HTTP `408``409``425``429``5xx`
150+
- 用户主动中断、权限拒绝、HTTP `400``401``403``404` 等配置或请求错误不会自动重试。
151+
- 也可以通过环境变量配置:`DEEPCODE_REQUEST_TIMEOUT_MS=300000``DEEPCODE_MAX_RETRIES=2`
152+
133153
## 环境变量优先级
134154

135155
环境变量是配置应用程序的常用方式,尤其适用于敏感信息(如 api-key)或可能在不同环境之间更改的设置。

docs/configuration_en.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ The following are all the top-level fields supported in `settings.json`, along w
3131
| `thinkingEnabled` | boolean | Whether to enable thinking mode (enabled by default for DeepSeek V4 series)|
3232
| `reasoningEffort` | string | Reasoning intensity, either `"high"` or `"max"` (default `"max"`) |
3333
| `debugLogEnabled` | boolean | Enable debug log output (default `false`) |
34+
| `requestTimeoutMs` | number | Main model request timeout in milliseconds. `0` disables the timeout (default `0`) |
35+
| `maxRetries` | number | Maximum retry count for transient main model request failures (default `2`, max `10`) |
3436
| `notify` | string | Full path to a task-completion notification script (e.g., Slack notification script) |
3537
| `webSearchTool` | string | Full path to a custom web search script |
3638
| `mcpServers` | object | MCP server configurations (keys are service names, values are McpServerConfig objects) |
@@ -45,6 +47,8 @@ The following are all the top-level fields supported in `settings.json`, along w
4547
| `THINKING_ENABLED`| string | Enable thinking mode |
4648
| `REASONING_EFFORT`| string | Reasoning intensity |
4749
| `DEBUG_LOG_ENABLED`| string| Enable debug log output |
50+
| `REQUEST_TIMEOUT_MS`| string | Main model request timeout in milliseconds. `0` disables the timeout |
51+
| `MAX_RETRIES` | string | Maximum retry count for transient main model request failures |
4852
| `<any other KEY>` | string | Custom environment variable |
4953

5054
#### `thinkingEnabled` — Thinking Mode
@@ -129,6 +133,22 @@ For detailed MCP usage instructions, refer to [mcp.md](mcp.md).
129133

130134
Set to `true` to enable detailed debug logging (default `false`), useful for troubleshooting API calls and tool execution.
131135

136+
#### `requestTimeoutMs` and `maxRetries` — Request Timeout and Retries
137+
138+
Controls timeout and automatic retries for main model Chat Completion requests:
139+
140+
```json
141+
{
142+
"requestTimeoutMs": 300000,
143+
"maxRetries": 2
144+
}
145+
```
146+
147+
- `requestTimeoutMs` is in milliseconds. The default `0` disables request timeout so long thinking or long outputs are not interrupted unexpectedly.
148+
- `maxRetries` defaults to `2` and is capped at `10`. Retries apply only to transient failures, such as network disconnects, request timeouts, HTTP `408`, `409`, `425`, `429`, and `5xx`.
149+
- User interrupts, permission denials, HTTP `400`, `401`, `403`, `404`, and other request/configuration errors are not retried.
150+
- You can also configure these through environment variables: `DEEPCODE_REQUEST_TIMEOUT_MS=300000`, `DEEPCODE_MAX_RETRIES=2`.
151+
132152
## Environment Variable Priority
133153

134154
Environment variables are a common way to configure applications, especially for sensitive information (such as api-key) or settings that may change between environments.
@@ -181,4 +201,4 @@ Applied in the following priority order (lower-numbered overridden by higher-num
181201
2. User-level settings.json: `{"env": {"MCP_GITHUB_PERSONAL_ACCESS_TOKEN": "..."}}`
182202
3. Project-level settings.json: `{"mcpServers":{"github":{"env":{"GITHUB_PERSONAL_ACCESS_TOKEN":"..."}}}}`
183203
4. Project-level settings.json: `{"env": {"MCP_GITHUB_PERSONAL_ACCESS_TOKEN": "..."}}`
184-
5. System environment variable: `DEEPCODE_MCP_GITHUB_PERSONAL_ACCESS_TOKEN=... deepcode`
204+
5. System environment variable: `DEEPCODE_MCP_GITHUB_PERSONAL_ACCESS_TOKEN=... deepcode`

src/common/openai-client.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export function createOpenAIClient(projectRoot: string = process.cwd()): {
2626
thinkingEnabled: boolean;
2727
reasoningEffort: "high" | "max";
2828
debugLogEnabled: boolean;
29+
requestTimeoutMs: number;
30+
maxRetries: number;
2931
notify?: string;
3032
webSearchTool?: string;
3133
env: Record<string, string>;
@@ -40,6 +42,8 @@ export function createOpenAIClient(projectRoot: string = process.cwd()): {
4042
thinkingEnabled: settings.thinkingEnabled,
4143
reasoningEffort: settings.reasoningEffort,
4244
debugLogEnabled: settings.debugLogEnabled,
45+
requestTimeoutMs: settings.requestTimeoutMs,
46+
maxRetries: settings.maxRetries,
4347
notify: settings.notify,
4448
webSearchTool: settings.webSearchTool,
4549
env: settings.env,
@@ -56,6 +60,8 @@ export function createOpenAIClient(projectRoot: string = process.cwd()): {
5660
thinkingEnabled: settings.thinkingEnabled,
5761
reasoningEffort: settings.reasoningEffort,
5862
debugLogEnabled: settings.debugLogEnabled,
63+
requestTimeoutMs: settings.requestTimeoutMs,
64+
maxRetries: settings.maxRetries,
5965
notify: settings.notify,
6066
webSearchTool: settings.webSearchTool,
6167
env: settings.env,
@@ -91,6 +97,8 @@ export function createOpenAIClient(projectRoot: string = process.cwd()): {
9197
thinkingEnabled: settings.thinkingEnabled,
9298
reasoningEffort: settings.reasoningEffort,
9399
debugLogEnabled: settings.debugLogEnabled,
100+
requestTimeoutMs: settings.requestTimeoutMs,
101+
maxRetries: settings.maxRetries,
94102
notify: settings.notify,
95103
webSearchTool: settings.webSearchTool,
96104
env: settings.env,

0 commit comments

Comments
 (0)