Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

3.8 KB
Raw Blame
/**
 * CuaBot Settings Management
 */

import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs';
import { join } from 'path';
import { homedir } from 'os';

const CONFIG_DIR = join(homedir(), '.cuabot');
const SETTINGS_FILE = join(CONFIG_DIR, 'settings.json');

export interface Settings {
  defaultAgent?: string;
  telemetryEnabled?: boolean;
  aliasIgnored?: boolean;
}

export const AGENTS = {
  claude: {
    name: 'Claude Code',
    description: "Anthropic's Claude AI coding assistant",
    command: 'claude',
  },
  gemini: {
    name: 'Gemini CLI',
    description: "Google's Gemini AI assistant",
    command: 'npx @google/gemini-cli',
  },
  codex: {
    name: 'OpenAI Codex',
    description: "OpenAI's Codex coding assistant",
    command: 'codex',
  },
  aider: {
    name: 'Aider',
    description: 'AI pair programming in your terminal',
    command: 'aider',
  },
  openclaw: {
    name: 'OpenClaw',
    description: 'OpenClaw AI assistant',
    command: 'openclaw',
  },
  vibe: {
    name: 'Vibe',
    description: "Mistral's Vibe coding assistant",
    command: 'vibe',
  },
} as const;

export type AgentId = keyof typeof AGENTS;

function ensureConfigDir() {
  if (!existsSync(CONFIG_DIR)) {
    mkdirSync(CONFIG_DIR, { recursive: true });
  }
}

export function loadSettings(): Settings {
  ensureConfigDir();
  if (!existsSync(SETTINGS_FILE)) {
    return {};
  }
  try {
    return JSON.parse(readFileSync(SETTINGS_FILE, 'utf-8'));
  } catch {
    return {};
  }
}

export function saveSettings(settings: Settings): void {
  ensureConfigDir();
  writeFileSync(SETTINGS_FILE, JSON.stringify(settings, null, 2));
}

export function getDefaultAgent(): string | undefined {
  return loadSettings().defaultAgent;
}

export function setDefaultAgent(agent: string): void {
  const settings = loadSettings();
  settings.defaultAgent = agent;
  saveSettings(settings);
}

/** Global Cua opt-outs that override cuabot's own opt-in. */
function globallyOptedOut(): boolean {
  const norm = (v: string | undefined) => (v ?? '').trim().toLowerCase();
  const dnt = norm(process.env.DO_NOT_TRACK);
  if (dnt !== '' && dnt !== '0') return true;
  const off = new Set(['0', 'false', 'no', 'off']);
  if (off.has(norm(process.env.CUA_TELEMETRY))) return true;
  if (off.has(norm(process.env.CUA_TELEMETRY_ENABLED))) return true;
  if (['1', 'true', 'yes', 'on'].includes(norm(process.env.CUA_TELEMETRY_DISABLED))) return true;
  return false;
}

/**
 * cuabot telemetry (usage events and prompt sharing) is opt-in: off unless the
 * user chose "Yes" in onboarding or set CUABOT_TELEMETRY=true. DO_NOT_TRACK and
 * CUA_TELEMETRY=0 always turn it off.
 */
export function getTelemetryEnabled(): boolean {
  if (globallyOptedOut()) return false;
  const settings = loadSettings();
  const envValue = process.env.CUABOT_TELEMETRY?.toLowerCase() === 'true';
  const envFalse = process.env.CUABOT_TELEMETRY?.toLowerCase() === 'false';

  // If any source is explicitly false, return false
  if (settings.telemetryEnabled === false || envFalse) {
    return false;
  }
  // If any source is true, return true
  if (settings.telemetryEnabled === true || envValue) {
    return true;
  }
  // Default to false
  return false;
}

export function isTelemetryConfigured(): boolean {
  const settings = loadSettings();
  // Telemetry is configured if it's been explicitly set to true or false
  return settings.telemetryEnabled !== undefined;
}

export function setTelemetryEnabled(enabled: boolean): void {
  const settings = loadSettings();
  settings.telemetryEnabled = enabled;
  saveSettings(settings);
}

export function getAliasIgnored(): boolean {
  return loadSettings().aliasIgnored === true;
}

export function setAliasIgnored(ignored: boolean): void {
  const settings = loadSettings();
  settings.aliasIgnored = ignored;
  saveSettings(settings);
}