Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

10.8 KB
Raw Blame
import type {
  AdvancedSettingKind,
  AdvancedSettings,
  SwitchLaunchProfile,
  SwitchLaunchSpecialization,
} from '@switch-console/core/agents/plugins';
import { LAUNCH_PROFILE_HOME_PLACEHOLDER } from '@switch-console/core/agents/plugins';

/**
 * The agent an OpenCode session runs as by default, and therefore the one a
 * per-agent setting has to land on.
 *
 * OpenCode's per-agent settings live under `agent.<name>`, and a fresh `opencode`
 * with no `--agent` runs `build`. Overriding `build` rather than defining a new
 * agent keeps the launch command unchanged — there is no name to pass — and
 * leaves the agent list the user sees as it was. Every key set here is one
 * OpenCode's own schema declares for an agent, so the override is a narrowing of
 * the built-in rather than a replacement of it.
 */
const OPENCODE_TARGET_AGENT = 'build';

/**
 * Where an agent's OpenCode config and instructions live, under the same
 * `.config/opencode` directory as the global config they layer over. Kept in
 * their own `switch/` subdirectory so it is obvious which files Switch Console
 * owns, and so removing an agent cannot take a user's file with it.
 */
const OPENCODE_PROFILE_DIR = '.config/opencode/switch';

/**
 * The profile name an agent maps to, used as the config filename stem.
 *
 * A Switch agent name is unique only *within a location*: two agents can share a
 * name in different directories, so the name alone would collide onto one file
 * and whichever launched second would overwrite the first's settings. The name
 * therefore carries a digest of `(workingDir, slug)` — the same `(dir, slug)` key
 * the Codex profile uses — which also keeps `a.b` and
 * `a-b` distinct.
 */
export function opencodeProfileName(slug: string, workingDir: string): string {
  const plain = slug.replace(/[^A-Za-z0-9_-]/g, '-');
  return `${plain}-${profileDigest(workingDir, slug)}`;
}

/** FNV-1a over `workingDir\0slug`, base36 — short, stable, and dependency-free. */
function profileDigest(workingDir: string, slug: string): string {
  let hash = 0x811c9dc5;
  const input = `${workingDir} ${slug}`;
  for (let i = 0; i < input.length; i++) {
    hash = Math.imul(hash ^ input.charCodeAt(i), 0x01000193) >>> 0;
  }
  return hash.toString(36);
}

/** Home-relative path of an agent's OpenCode config. */
function opencodeConfigRelativePath(slug: string, workingDir: string): string {
  return `${OPENCODE_PROFILE_DIR}/${opencodeProfileName(slug, workingDir)}.json`;
}

/** Home-relative path of an agent's instructions file, when it has one. */
function opencodeInstructionsRelativePath(slug: string, workingDir: string): string {
  return `${OPENCODE_PROFILE_DIR}/${opencodeProfileName(slug, workingDir)}.instructions.md`;
}

/** Home-relative files an agent's OpenCode profile occupies, for delete/rename teardown. */
export function opencodeProfilePaths(params: { slug: string; workingDir: string }): string[] {
  return [
    opencodeConfigRelativePath(params.slug, params.workingDir),
    opencodeInstructionsRelativePath(params.slug, params.workingDir),
  ];
}

/**
 * Values OpenCode accepts for a tool permission. Only the two ends are offered
 * per setting below; `ask` would stall an unattended session on a prompt nobody
 * is there to answer.
 */
const ALLOW = 'allow';
const DENY = 'deny';

/**
 * Where each per-agent setting goes in the config, by the key it is stored and
 * collected under. The fields a person fills them in with are the server's.
 *
 * `path` is a dotted path, absent for a setting handled specially
 * (`instructions`, which is a file rather than a value). `scope` says what it is
 * relative to: the agent's own object (`agent.build.…`) for most, or the config
 * root for the few OpenCode keys that have no per-agent form. Every value
 * reaches the writer as a string, and blank means the key is omitted so the
 * user's own `opencode.json` decides — which is not the same as writing a
 * default-looking value over it.
 */
type OpencodeSetting = {
  key: string;
  /**
   * The shape of value the setting takes as an advanced setting, or null for a
   * main attribute of the agent (its model, its instructions). Those are still
   * rendered into the profile — OpenCode only reads its own files — but they
   * are collected once, provider-agnostically, rather than as advanced settings.
   */
  advanced: AdvancedSettingKind | null;
  path?: string;
  scope?: 'agent' | 'config';
  toValue?: (raw: string) => unknown;
};

/**
 * Parse a numeric setting, dropping anything that is not a finite number.
 *
 * OpenCode ignores config keys it does not recognise and does not complain about
 * a value it cannot use, so a junk number would be silently inert. Leaving the
 * key out instead is the same outcome the user gets from a blank field, and does
 * not put `null` into a config that claims to be a number.
 */
const numeric = (raw: string): unknown => {
  const value = Number(raw);
  return Number.isFinite(value) ? value : undefined;
};

const OPENCODE_SETTINGS: OpencodeSetting[] = [
  { key: 'model', advanced: null, path: 'model' },
  { key: 'variant', advanced: 'text', path: 'variant' },
  { key: 'temperature', advanced: 'number', path: 'temperature', toValue: numeric },
  { key: 'topP', advanced: 'number', path: 'top_p', toValue: numeric },
  { key: 'maxSteps', advanced: 'number', path: 'maxSteps', toValue: numeric },
  {
    key: 'webSearch',
    advanced: 'text',
    // Web search is not a setting in OpenCode, it is a tool the agent is allowed
    // or denied. Written as a permission rather than under `tools`, which
    // OpenCode normalises into exactly this.
    path: 'permission.websearch',
    toValue: (raw) => (raw === 'true' ? ALLOW : DENY),
  },
  {
    key: 'smallModel',
    advanced: 'text',
    path: 'small_model',
    // Top-level: OpenCode has no per-agent utility model, so this is the one
    // setting here that applies to the session rather than to the agent. The
    // config file is per-agent, so writing it at the root is still per-agent.
    scope: 'config',
  },
  { key: 'instructions', advanced: null },
];

/** The advanced settings an OpenCode profile carries, beside the model and instructions. */
export function opencodeLaunchProfileSettings(): AdvancedSettings {
  return Object.fromEntries(
    OPENCODE_SETTINGS.flatMap((setting) =>
      setting.advanced ? [[setting.key, setting.advanced]] : []
    )
  );
}

/** Set a dotted path within an object, creating the intermediate objects. */
function assign(target: Record<string, unknown>, path: string, value: unknown): void {
  const segments = path.split('.');
  const leaf = segments.pop() as string;
  let cursor = target;
  for (const segment of segments) {
    if (typeof cursor[segment] !== 'object' || cursor[segment] === null) cursor[segment] = {};
    cursor = cursor[segment] as Record<string, unknown>;
  }
  cursor[leaf] = value;
}

/**
 * Build an agent's OpenCode config.
 *
 * The result is layered over the user's global `~/.config/opencode/opencode.json`
 * rather than replacing it: OpenCode deep-merges what `OPENCODE_CONFIG` names
 * onto what it already loaded, so the Switch MCP server registered there survives
 * without being restated here. That is also why nothing in this file registers
 * it — restating it would put a second copy of the runtime pin somewhere it could
 * drift.
 *
 * `instructionsPath`, when the agent has an instructions body, is the absolute
 * path of the file holding it. It is absolute because the config is read from the
 * home directory while the session runs in the repo, so a relative path would
 * resolve against the wrong root.
 *
 * Returns `null` when nothing is set, so an agent on the defaults gets no config
 * file and no `OPENCODE_CONFIG` pointing at one.
 */
export function buildOpencodeConfig(
  values: SwitchLaunchSpecialization,
  instructionsPath: string | null
): string | null {
  const agent: Record<string, unknown> = {};
  const config: Record<string, unknown> = {};

  // Driven off OpenCode's own settings rather than the collected keys, so a value
  // stored under a key OpenCode does not declare is left out rather than written.
  // That matters more here than elsewhere: OpenCode drops a config section
  // containing a key it does not recognise, silently and whole.
  for (const setting of OPENCODE_SETTINGS) {
    if (!setting.path) continue;
    const raw = values[setting.key];
    if (typeof raw !== 'string' || raw.trim() === '') continue;

    const value = setting.toValue ? setting.toValue(raw.trim()) : raw.trim();
    if (value === undefined) continue;
    assign(setting.scope === 'config' ? config : agent, setting.path, value);
  }

  if (instructionsPath) config.instructions = [instructionsPath];
  if (Object.keys(agent).length > 0) config.agent = { [OPENCODE_TARGET_AGENT]: agent };
  if (Object.keys(config).length === 0) return null;

  // OpenCode adds `$schema` to a config that lacks one; writing it avoids that
  // showing up as a change to a file Switch Console owns.
  return `${JSON.stringify({ $schema: 'https://opencode.ai/config.json', ...config }, null, 2)}\n`;
}

/**
 * Compute an agent's OpenCode launch profile: the files to write under the home
 * directory and the environment that loads them.
 *
 * Unlike Codex there is no argv to return. OpenCode has no `--profile` or
 * `--config` flag; the only way to add a config file to a session is
 * `OPENCODE_CONFIG`, so the profile's whole contribution is an environment
 * variable naming the file it just wrote. The value has to be absolute and this
 * function is pure, so the home directory arrives as a placeholder the launch
 * surface fills in.
 *
 * An instructions body is a second file rather than a value in the config.
 * OpenCode's per-agent `prompt` takes text directly, but it *supplies* that
 * agent's system prompt rather than adding to it, which for a coding agent means
 * losing the operating instructions it needs to work. Top-level `instructions` is
 * the additive path — the same one an AGENTS.md takes — and it names files.
 */
export function opencodeLaunchProfile(params: {
  slug: string;
  workingDir: string;
  values: SwitchLaunchSpecialization;
}): SwitchLaunchProfile | null {
  const { slug, workingDir, values } = params;

  const instructions = values.instructions?.trim() ? values.instructions : null;
  const instructionsRelativePath = opencodeInstructionsRelativePath(slug, workingDir);
  const instructionsPath = instructions
    ? `${LAUNCH_PROFILE_HOME_PLACEHOLDER}/${instructionsRelativePath}`
    : null;

  const content = buildOpencodeConfig(values, instructionsPath);
  if (!content) return null;

  const configRelativePath = opencodeConfigRelativePath(slug, workingDir);
  return {
    files: [
      { relativePath: configRelativePath, content },
      ...(instructions ? [{ relativePath: instructionsRelativePath, content: instructions }] : []),
    ],
    args: [],
    env: { OPENCODE_CONFIG: `${LAUNCH_PROFILE_HOME_PLACEHOLDER}/${configRelativePath}` },
  };
}