# DSH Chat Continue

**English** | [中文](#中文)

A DSH plugin that intercepts third-party API request errors (429 Too Many Requests, 503 Service Unavailable, etc.) and automatically retries the request — preventing agent sessions from stalling when external APIs encounter issues.

---

## English

### The Problem

When using DSH to call third-party model APIs (e.g., custom providers, external AI services), those platforms often enforce **rate limits** or **other access restrictions**. When the limit is exceeded, the API returns an error (e.g., 429 Too Many Requests, `rate_limit`, `quota_exceeded`).

DSH has built-in retry logic (`dsh-llm-retry`) for DeepSeek API, but it may not cover all third-party error codes. When the built-in retry is exhausted, the agent session **stalls mid-conversation**, requiring the user to manually type "continue" to resume — a poor experience.

### The Solution

This plugin acts as a **second line of defense** after DSH's built-in retry. It:

1. **Intercepts** `agent/request-error` events after built-in retry fails
2. **Matches** the error against your configured status codes and error codes
3. **Automatically retries** or **shows a notification** for manual confirmation
4. **Resumes the conversation** seamlessly — no manual "continue" needed

### How It Works

```
Third-party API returns error (429, rate_limit, etc.)
    ↓
DSH built-in retry (`dsh-llm-retry`) handles it first
    ↓ (if built-in retry exhausted)
This plugin intercepts `agent/request-error`
    ↓
Checks against your configured retryable status codes & error codes
    ↓
Auto Mode → waits configured interval → retries automatically
Manual Mode → shows notification → you click "Retry" or "Skip"
    ↓
Retry succeeds → conversation continues without interruption
```

### Features

- **Auto Retry Mode** — automatically waits and retries on request errors, no manual intervention needed
- **Manual Confirm Mode** — shows a notification overlay at the bottom-right, click "Retry" or "Skip"
- **Configurable Error Codes** — set which HTTP status codes (e.g., `429, 503`) and error codes (e.g., `rate_limit, service_unavailable`) to intercept
- **Configurable Retry Interval** — 1–60 seconds between retries
- **Configurable Max Retries** — 1–100 retries before giving up
- **Settings Page** — **Settings → Chat Continue**

### Installation

#### Prerequisites

- DSH installed (`dsh` command available)

#### Install via npm

```bash
dsh plugin --profile web add @wuxjs/dsh-chat-continue
```

Then restart DSH. The plugin loads automatically.

### Usage

1. **Restart DSH** after installation
2. Open **Settings** (sidebar gear icon)
3. Find the **Chat Continue** tab
4. Configure your settings:

| Setting | Description | Default |
|---------|-------------|---------|
| Retry Mode | `Auto` = automatic retry, `Manual` = notification popup | Auto |
| Retry Interval | Seconds to wait between retries (1–60) | 5s |
| Max Retries | Maximum retry attempts (1–100) | 10 |
| Retry HTTP Status Codes | Comma-separated, e.g. `429, 503` | `429` |
| Retry Error Codes | Comma-separated, e.g. `rate_limit, service_unavailable` | `rate_limit` |

#### Configuration Examples

**Example 1: OpenAI-compatible API returning 429**

```
Retry HTTP Status Codes: 429
Retry Error Codes: rate_limit
```

**Example 2: Custom API returning 503 + service_unavailable**

```
Retry HTTP Status Codes: 429, 503
Retry Error Codes: rate_limit, service_unavailable
```

**Example 3: Strict rate limiting with quota exceeded**

```
Retry HTTP Status Codes: 429
Retry Error Codes: rate_limit, quota_exceeded, insufficient_quota
```

### Manual Installation (Fallback)

If auto-loading doesn't work:

1. Switch to **Cordis preset** (creative mode)
2. Use `cordis_define`:
   - **kind**: `new`
   - **idPrefix**: `rtry`
   - **name**: `Chat Continue`
   - **purpose**: `Auto-retry on API request errors, supports auto/manual mode, configurable interval`
3. Paste `lib/host.js` content into Host code
4. Paste `lib/client.js` content into Client code
5. Run `cordis_run` to start

### Uninstall

```bash
dsh plugin --profile web remove @wuxjs/dsh-chat-continue
```

Then restart DSH.

### File Structure

```
@wuxjs/dsh-chat-continue/
├── package.json           # npm package config (with dsh.bundle declaration)
├── cordis.patch.yml       # registers auto-loader into host layer
├── .gitignore
├── .npmrc                 # npm auth token (excluded from git)
├── lib/
│   ├── auto.js            # auto-loader (static plugin, registers dynamically)
│   ├── host.js            # host-side code (for manual installation)
│   └── client.js          # client-side code (for manual installation)
├── README.md
└── skills/
```

### Links

- **GitHub**: [https://github.com/Chu-m/dsh-chat-continue](https://github.com/Chu-m/dsh-chat-continue)
- **npm**: [https://www.npmjs.com/package/@wuxjs/dsh-chat-continue](https://www.npmjs.com/package/@wuxjs/dsh-chat-continue)

### License

MIT

---

## 中文

### 问题

使用 DSH 接入第三方平台的模型 API 时，这些平台通常会对调用进行**限速**或**其他限制**。当超出速率或触发其他限制时，API 会返回错误（如 429 Too Many Requests、`rate_limit`、`quota_exceeded` 等）。

DSH 官方内置了针对 DeepSeek API 的重试机制（`dsh-llm-retry`），但并不能覆盖所有第三方平台的错误码。当内置重试耗尽后，Agent 会话会**卡住中断**，需要用户手动输入"继续"才能恢复对话，体验非常差。

### 解决思路

本插件作为 DSH 内置重试的**第二道防线**：

1. **拦截** DSH 内置重试耗尽后的 `agent/request-error` 事件
2. **匹配**你配置的状态码和错误码，判断是否是可重试的错误
3. **自动重试**或**弹窗通知**让你手动确认
4. **无缝恢复对话** — 不再需要手动输入"继续"

### 工作原理

```
第三方 API 返回错误（429, rate_limit 等）
    ↓
DSH 内置重试 (`dsh-llm-retry`) 先处理
    ↓（内置重试耗尽后）
本插件拦截 `agent/request-error` 事件
    ↓
检查你配置的可重试状态码 & 错误码
    ↓
自动模式 → 等待指定秒数 → 自动重试
手动模式 → 右下角弹窗 → 点击「重试」或「放弃」
    ↓
重试成功 → 对话继续，无需手动输入
```

### 功能

- **自动重试模式**：遇到 API 请求错误时自动等待指定秒数后重试，无需人工干预
- **手动确认模式**：遇到错误时右下角弹出通知，点击「重试」或「放弃」
- **可配置错误码**：自定义要拦截的 HTTP 状态码（如 `429, 503`）和错误码（如 `rate_limit, service_unavailable`）
- **可配置重试间隔**：1–60 秒
- **可配置最大重试次数**：1–100 次
- **设置页面**：**Settings → 对话继续**

### 安装

#### 前提条件

- 已安装 DSH（`dsh` 命令可用）

#### 通过 npm 安装

```bash
dsh plugin --profile web add @wuxjs/dsh-chat-continue
```

安装后重启 DSH，插件会自动加载。

### 使用说明

1. 安装并重启 DSH
2. 打开 **Settings**（侧边栏齿轮图标）
3. 找到 **对话继续** 选项卡
4. 配置参数：

| 配置项 | 说明 | 默认值 |
|--------|------|--------|
| 重试模式 | `自动` = 自动重试，`手动` = 弹窗通知 | 自动 |
| 重试间隔 | 每次重试之间的等待时间（1–60 秒） | 5 秒 |
| 最大重试次数 | 超过此次数后停止重试（1–100 次） | 10 次 |
| 重试 HTTP 状态码 | 逗号分隔，如 `429, 503` | `429` |
| 重试错误码 | 逗号分隔，如 `rate_limit, service_unavailable` | `rate_limit` |

#### 配置示例

**示例 1：OpenAI 兼容接口返回 429**

```
重试 HTTP 状态码: 429
重试错误码: rate_limit
```

**示例 2：自定义 API 返回 503 + service_unavailable**

```
重试 HTTP 状态码: 429, 503
重试错误码: rate_limit, service_unavailable
```

**示例 3：严格限流 + quota_exceeded**

```
重试 HTTP 状态码: 429
重试错误码: rate_limit, quota_exceeded, insufficient_quota
```

### 手动安装（备用方案）

如果自动加载未生效：

1. 切换到 **Cordis preset**（创造模式）
2. 使用 `cordis_define`：
   - **kind**: `new`
   - **idPrefix**: `rtry`
   - **name**: `Chat Continue`
   - **purpose**: `自动重试 API 请求错误，支持手动/自动模式，可配置重试间隔`
3. 将 `lib/host.js` 内容粘贴到 Host 代码
4. 将 `lib/client.js` 内容粘贴到 Client 代码
5. 使用 `cordis_run` 启动

### 卸载

```bash
dsh plugin --profile web remove @wuxjs/dsh-chat-continue
```

然后重启 DSH。

### 文件结构

```
@wuxjs/dsh-chat-continue/
├── package.json           # npm 包配置（含 dsh.bundle 声明）
├── cordis.patch.yml       # 注册自动加载器到 host 层
├── .gitignore
├── .npmrc                 # npm 认证 token（已排除在 git 外）
├── lib/
│   ├── auto.js            # 自动加载器（静态插件，启动时自动注册）
│   ├── host.js            # Host 端代码（用于手动安装）
│   └── client.js          # Client 端代码（用于手动安装）
├── README.md
└── skills/
```

### 链接

- **GitHub**: [https://github.com/Chu-m/dsh-chat-continue](https://github.com/Chu-m/dsh-chat-continue)
- **npm**: [https://www.npmjs.com/package/@wuxjs/dsh-chat-continue](https://www.npmjs.com/package/@wuxjs/dsh-chat-continue)

### 许可证

MIT