DSH OpenCode Go Key Broker

dsh-opencode-go-key-broker Plugin: Automatic Key Rotation for OpenCode Go in DSH

OpenCode Go in DSH typically only allows configuring a single API Key. When the quota window (rolling/weekly/monthly) for that key is exhausted, requests will fail with errors, requiring manual replacement. The dsh-opencode-go-key-broker plugin solves this problem by maintaining a pool of keys and automatically switching them based on quota status.

This plugin is maintained by Gdenich, belongs to the model inference category, and is licensed under MIT.

Core Features

  • OpenCode Go API-key Pool Management: Supports storage and rotation of multiple API keys.
  • Quota-based Automatic Switching: Automatically switches keys based on rolling, weekly, and monthly quota usage rates.
  • Proactive Polling & Reactive Switching: Actively polls quota status periodically or switches reactively upon errors.
  • Settings Page Management: Provides a graphical interface to view key status, manually switch keys, and view logs.
  • Composer Quota Badge: Displays quota usage in the editor.
  • HTTP Routing: Provides management interfaces such as snapshot, activate, refresh, and diag.

Installation and Enablement

Install from the npm registry:

dsh plugin --profile desktop add dsh-opencode-go-key-broker

After installation, DSH Desktop must be restarted to properly load the plugin list and the startup manifest.

How it Works

Key Pool Structure

The Key pool is managed via environment variables, following this convention (which is also how the plugin reads and stores keys):

OPENCODE_GO_API_KEY: sk-…        # The value actually read by the route (currently active Key)
OPENCODE_GO_KEY_ACTIVE: go2      # Marks the currently active Key (displayed with ★)
OPENCODE_GO_KEY_go1: sk-…
OPENCODE_GO_KEY_go2: sk-…
OPENCODE_GO_KEY_go3: sk-…

Switching Strategy

The plugin includes two switching logics:

  1. Proactive Polling: Periodically queries the /v1/usage status for each key according to the refreshMs configuration. If the usage rate of the currently active key exceeds a threshold (default 98%), it is marked as inactive, and the plugin switches to the next key with an “ok” status.
  2. Reactive Switching: Listens for the agent/request-error waterfall event. Upon receiving a QUOTA or INVALID_CREDENTIAL error, it immediately switches the key and returns { kind: 'retry' }. The Agent will then re-prepare the request using the new key.

Limitations

  • Switch Frequency: A single key can be switched at most 3 times per minute to prevent infinite loops when all keys have exhausted their quotas.
  • Error Filtering: Network errors or timeout errors do not trigger key switching, avoiding switches caused by temporary network fluctuations.
  • Cache Miss: Changing keys alters the account context. OpenCode Go’s prompt-cache is bound to the key, so the first request after a key switch will not utilize the cache.

Configuration

Configuration items are located in cordis.patch.yml (or profile override):

Config Item Default Value Description
enabled true Whether to enable the plugin
dryRun true Only logs decisions without actually switching keys; recommended for initial use
refreshMs 60000 Interval for proactive polling (milliseconds)
timeoutMs 15000 Timeout for querying the usage endpoint
thresholds.rolling 98 Quota threshold (percentage) that triggers a switch
thresholds.weekly 98 Same as above, for weekly quota
thresholds.monthly 98 Same as above, for monthly quota
warnPercent 80 Threshold for displaying “near exhaustion” warning
sticky true Keep the new key active after switching until it turns “red”
prefer first Selection strategy during switch: first or headroom (maximum remaining space)
switchOnError true Whether to trigger reactive switching on errors
keyNames auto Specify a list of key names, or automatically scan environment variables

It is recommended to observe behavior in dryRun: true mode for a period of time. Once the switching logic in the logs meets expectations, set it to false.

HTTP Routes

The plugin provides the following Host interfaces for external calls:

  • GET /plugins/dsh-opencode-go-key-broker/snapshot?force=1: Get the current Key pool status and logs.
  • POST /plugins/dsh-opencode-go-key-broker/activate {name}: Manually specify the active Key name.
  • POST /plugins/dsh-opencode-go-key-broker/refresh: Immediately trigger a quota query.
  • POST /plugins/dsh-opencode-go-key-broker/diag: Output diagnostic information to the log file.

Notes

  • Keys must be valid. Invalid keys (e.g., 401) are treated as “red” status.
  • Switching keys results in the loss of prompt-cache.
  • DSH Desktop must be restarted after installation.

Conclusion

By automating key rotation, this plugin reduces the operational costs for OpenCode Go in multi-key scenarios. For more details, please refer to the GitHub Repository.

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye