@@ -23,17 +23,18 @@ import { buildOpenCodeFamilyPermissionEnv } from '@/backends/opencode/utils/open
2323/**
2424 * Get the platform-specific path to the OpenCode configuration file.
2525 *
26- * Follows Node.js app config conventions:
27- * - Linux/macOS: ~/.config/ opencode/opencode.json (XDG config dir )
26+ * Follows platform-specific conventions:
27+ * - Linux/macOS: $XDG_CONFIG_HOME/ opencode/opencode.json (defaults to ~/. config when unset )
2828 * - Windows: %APPDATA%\opencode\opencode.json
2929 */
3030function getOpenCodeConfigPath ( ) : string {
3131 if ( process . platform === 'win32' ) {
3232 const appData = process . env . APPDATA || join ( homedir ( ) , 'AppData' , 'Roaming' ) ;
3333 return join ( appData , 'opencode' , 'opencode.json' ) ;
3434 }
35- // Linux, macOS, and others use XDG config directory
36- return join ( homedir ( ) , '.config' , 'opencode' , 'opencode.json' ) ;
35+ // Linux, macOS, and others respect XDG_CONFIG_HOME
36+ const xdgConfigHome = process . env . XDG_CONFIG_HOME || join ( homedir ( ) , '.config' ) ;
37+ return join ( xdgConfigHome , 'opencode' , 'opencode.json' ) ;
3738}
3839
3940/**
@@ -44,14 +45,23 @@ function getOpenCodeConfigPath(): string {
4445 * This function bridges that gap by reading the user's config file and formatting
4546 * it for the ACP backend.
4647 *
47- * Respects existing OPENCODE_CONFIG_CONTENT if already set (user override).
48+ * Precedence (highest to lowest):
49+ * 1. process.env.OPENCODE_CONFIG_CONTENT (user's shell-level override)
50+ * 2. optionsEnv.OPENCODE_CONFIG_CONTENT (runtime-provided config)
51+ * 3. ~/.config/opencode/opencode.json (user's config file)
4852 */
49- function readOpenCodeConfig ( ) : string | undefined {
50- // Respect explicit override
53+ function readOpenCodeConfig ( optionsEnv ?: Record < string , string > ) : string | undefined {
54+ // Highest priority: process-level env var (user's shell override)
5155 if ( process . env . OPENCODE_CONFIG_CONTENT ) {
5256 return process . env . OPENCODE_CONFIG_CONTENT ;
5357 }
5458
59+ // Second priority: options env (runtime-provided config)
60+ if ( optionsEnv ?. OPENCODE_CONFIG_CONTENT ) {
61+ return optionsEnv . OPENCODE_CONFIG_CONTENT ;
62+ }
63+
64+ // Lowest priority: read from config file
5565 const configPath = getOpenCodeConfigPath ( ) ;
5666 if ( ! existsSync ( configPath ) ) {
5767 return undefined ;
@@ -78,7 +88,7 @@ export interface OpenCodeBackendOptions extends AgentFactoryOptions {
7888}
7989
8090export function createOpenCodeBackend ( options : OpenCodeBackendOptions ) : AgentBackend {
81- const openCodeConfig = readOpenCodeConfig ( ) ;
91+ const openCodeConfig = readOpenCodeConfig ( options . env ) ;
8292
8393 const backendOptions : AcpBackendOptions = {
8494 agentName : 'opencode' ,
0 commit comments