enable coding agents if they are available by default

This commit is contained in:
Arjun 2026-05-28 22:33:32 +05:30
parent e7c7d0e90f
commit c213274723

View file

@ -2,6 +2,7 @@ import fs from 'fs/promises';
import path from 'path'; import path from 'path';
import { WorkDir } from '../config/config.js'; import { WorkDir } from '../config/config.js';
import { CodeModeConfig } from './types.js'; import { CodeModeConfig } from './types.js';
import { checkCodeModeAgentStatus } from './status.js';
export interface ICodeModeConfigRepo { export interface ICodeModeConfigRepo {
getConfig(): Promise<CodeModeConfig>; getConfig(): Promise<CodeModeConfig>;
@ -10,27 +11,31 @@ export interface ICodeModeConfigRepo {
export class FSCodeModeConfigRepo implements ICodeModeConfigRepo { export class FSCodeModeConfigRepo implements ICodeModeConfigRepo {
private readonly configPath = path.join(WorkDir, 'config', 'code-mode.json'); private readonly configPath = path.join(WorkDir, 'config', 'code-mode.json');
private readonly defaultConfig: CodeModeConfig = { enabled: false }; private agentReadyPromise: Promise<boolean> | null = null;
constructor() { // Reuse the existing agent check (Claude Code / Codex installed + signed in),
this.ensureConfigFile(); // cached for the process lifetime so we probe (shell + keychain) at most once
} // per session rather than on every getConfig call.
private agentReady(): Promise<boolean> {
private async ensureConfigFile(): Promise<void> { if (!this.agentReadyPromise) {
try { this.agentReadyPromise = checkCodeModeAgentStatus()
await fs.access(this.configPath); .then((s) =>
} catch { (s.claude.installed && s.claude.signedIn)
await fs.mkdir(path.dirname(this.configPath), { recursive: true }); || (s.codex.installed && s.codex.signedIn))
await fs.writeFile(this.configPath, JSON.stringify(this.defaultConfig, null, 2)); .catch(() => false);
} }
return this.agentReadyPromise;
} }
async getConfig(): Promise<CodeModeConfig> { async getConfig(): Promise<CodeModeConfig> {
try { try {
// The file only exists once the user has explicitly toggled code mode
// in settings — always honor that choice.
const content = await fs.readFile(this.configPath, 'utf8'); const content = await fs.readFile(this.configPath, 'utf8');
return CodeModeConfig.parse(JSON.parse(content)); return CodeModeConfig.parse(JSON.parse(content));
} catch { } catch {
return this.defaultConfig; // No explicit choice yet: enable automatically when a coding agent is ready.
return { enabled: await this.agentReady() };
} }
} }