Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:2aronS Duel Agents patchOpenClawConfig

From Leeroopedia
Knowledge Sources
Domains Configuration, Developer_Tools
Last Updated 2026-10-27 14:00 GMT

Overview

Concrete tool for safely patching an OpenClaw gateway's JSON5 config to route through Duel while preserving existing settings, backing up the original, and isolating the API key, provided by @duel-agents/core.

Description

The patchOpenClawConfig function is the most complex single function in the core package. It:

  1. Validates the key via requireApiKey
  2. Builds the patch via buildOpenClawPatch — the patch uses ${DUEL_API_KEY} placeholder, never plaintext
  3. Reads the existing config — if the file is missing (ENOENT), starts from {}; if present but unparseable, throws with a descriptive error
  4. Creates the parent directory if needed
  5. Backs up the existing file to ${configPath}.bak (silently skipped if source does not exist)
  6. Preserves the user's default model — if the existing config already sets agents.defaults.model.primary, removes primary from the patch so re-install does not clobber the user's choice
  7. Deep-merges the patch into the existing config via deepMerge
  8. Writes the result as 2-space-indented JSON5 with a trailing newline

The key itself is never written to openclaw.json; the caller writes it to ~/.openclaw/.env via mergeEnvFile.

Usage

Import this function when you need to:

  • Patch an OpenClaw config to add the Duel provider
  • Re-install Duel into an existing OpenClaw setup without losing user customizations
  • Understand the safe-patching pipeline (backup → parse → preserve → merge → write)

Code Reference

Source Location

  • Repository: Duel-Agents
  • File: packages/core/src/index.ts (lines 140-196)

Signature

export async function patchOpenClawConfig(
  configPath: string,
  apiKey: string,
  proxyUrl?: string,           // default: getProxyUrl()
): Promise<void>;

Import

import { patchOpenClawConfig } from "@duel-agents/core";

I/O Contract

Inputs

Name Type Required Description
configPath string Yes Path to openclaw.json
apiKey string Yes Validated Duel key (re-validated)
proxyUrl string No Proxy URL (default getProxyUrl())

Outputs

Name Type Description
side effect filesystem Patched openclaw.json; backup at .bak
throws Error If key invalid, config unparseable, or backup fails (non-ENOENT)

Usage Examples

Patch OpenClaw Config

import { patchOpenClawConfig, resolveOpenClawConfigPath, mergeEnvFile, resolveOpenClawEnvPath } from "@duel-agents/core";

const configPath = resolveOpenClawConfigPath();
const envPath = resolveOpenClawEnvPath();

// 1. Patch the config (uses ${DUEL_API_KEY} placeholder)
await patchOpenClawConfig(configPath, apiKey);

// 2. Write the actual key to ~/.openclaw/.env (never to openclaw.json)
const warnings = await mergeEnvFile(envPath, {
  DUEL_API_KEY: apiKey,
  DUEL_PROXY_URL: "https://duelagents.com/v1",
});
for (const w of warnings) console.warn(`⚠ ${w}`);

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment