Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

19.5 KB
Raw Blame
// DeepSeek Harness support — DeepSeek's `dsh` (github.com/deepseek-ai/deepseek-harness)
// over ACP stdio (`dsh --profile acp`). The generic protocol runtime lives in
// acp/core.ts; this file is only the per-harness quirks. Verified against
// @deepseek-ai/dsh 0.2.1-rc.2 on 2026-10-22:
//
//   - initialize answers protocolVersion 1, authMethods [] (no sign-in step:
//     the DeepSeek key is read from DEEPSEEK_API_KEY or dsh's own
//     $DSH_HOME/.credentials.yaml), promptCapabilities.image false,
//     sessionCapabilities {close, list, resume} — resume is session/resume,
//     there is no session/load.
//   - session/new advertises two config options: `model` (category "model",
//     values are JSON pairs such as ["deepseek-official","deepseek-v4-flash"],
//     grouped per provider) and `reasoning_effort` (category "thought_level":
//     off | low | high | max). Both are set with session/set_config_option,
//     which answers with the full configOptions so the core can confirm the
//     change. That is exactly the OpenCode-shaped path (selectModel +
//     modelVariants), so model and effort need no driver code of their own.
//   - Warm boot to initialize is 1.6 s; the first boot after an install
//     creates $DSH_HOME/profiles/acp (a cold `npx` boot took ~120 s because
//     it downloaded the dependency tree, so the catalog is only probed on an
//     explicit Refresh, never at app startup).
//
// Approve for me requires dsh 0.2.2-rc.2 to register the bot's memory directory
// with the workspace-write sandbox. The session model reviews sandbox escalations.
// Verified with dsh 0.2.2-rc.2, 0.2.2-rc.5 and 0.2.3-alpha.2 on 2026-11-24.
//
// Picker ids are `<provider>/<model>` so bot settings, badges and config.json
// stay readable; resolveTurnModel turns them into dsh's wire pair just before
// the core selects the model.
import { mkdirSync, readFileSync, realpathSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { parse as parseYaml, stringify } from "yaml";

import { writeFileAtomicIfChanged } from "../../atomic.ts";
import { cliVersionProbe } from "../../cli-version-probe.ts";
import { DATA_DIR } from "../../config.ts";
import type { ModelCatalog } from "../../contracts.ts";
import { harnessHome } from "../../env-path.ts";
import { killCliTree, resolveCli, spawnCli } from "../../procs.ts";
import { createAcpDriver, type AcpSupport } from "./core.ts";
import { acpVariantOption } from "./config-options.ts";
import { DSH_APPROVAL_PLUGIN } from "./dsh-approval-plugin.ts";
import { semVerAtLeast } from "./version-probe.ts";

export const DEEPSEEK_MODELS: ModelCatalog = {
  default: "deepseek-official/deepseek-v4-flash",
  options: [
    { id: "deepseek-official/deepseek-v4-flash", label: "DeepSeek-V4-Flash" },
    { id: "deepseek-official/deepseek-flash", label: "DeepSeek-V4.1-Flash" },
    { id: "deepseek-official/deepseek-v4-pro", label: "DeepSeek-V4-Pro" },
  ],
};

/** `provider/model` → dsh's `["provider","model"]`. An id with no provider
 * is handed to dsh as-is so its own error names the problem. */
export function deepseekWireValue(pickerId: string): string {
  const slash = pickerId.indexOf("/");
  if (slash <= 0 || slash === pickerId.length - 1) return pickerId;
  return JSON.stringify([pickerId.slice(0, slash), pickerId.slice(slash + 1)]);
}

/** Inverse of deepseekWireValue; null for anything but a two-string pair. */
export function deepseekPickerId(wireValue: string): string | null {
  let parsed: unknown;
  try {
    parsed = JSON.parse(wireValue);
  } catch {
    return null;
  }
  if (!Array.isArray(parsed) || parsed.length !== 2) return null;
  const [provider, model] = parsed;
  if (typeof provider !== "string" || typeof model !== "string" || !provider || !model) return null;
  return `${provider}/${model}`;
}

export function dshHome(env: Record<string, string | undefined>): string {
  return env.DSH_HOME || harnessHome("dsh", env);
}

const nonEmpty = (value: unknown): boolean => typeof value === "string" && value.trim() !== "";

/** dsh 0.2.1 writes `version: 1` with the key under `refs:`; it also migrates
 * the pre-release flat layout (the key at the top level), so both count. An
 * empty or quoted-empty value is "no key": dsh treats it as a boot error. */
function storedCredentialPresent(path: string): boolean {
  let doc: unknown;
  try {
    doc = parseYaml(readFileSync(path, "utf8"));
  } catch {
    return false;
  }
  if (!doc || typeof doc !== "object") return false;
  const root = doc as Record<string, unknown>;
  const refs = root.refs && typeof root.refs === "object" ? root.refs as Record<string, unknown> : {};
  return nonEmpty(refs.DEEPSEEK_API_KEY) || nonEmpty(root.DEEPSEEK_API_KEY);
}

const DOTENV_LINE = /^[ \t]*(?:export[ \t]+)?DEEPSEEK_API_KEY[ \t]*=(.*)$/;

/** One dotenv assignment with a non-empty value, quotes and a trailing comment stripped. */
function dotenvValuePresent(path: string): boolean {
  let text: string;
  try {
    text = readFileSync(path, "utf8");
  } catch {
    return false;
  }
  for (const line of text.split(/\r?\n/)) {
    const match = DOTENV_LINE.exec(line);
    if (!match) continue;
    let value = match[1]!.trim();
    const quoted = /^(["'])(.*)\1(?:[ \t]+#.*)?$/.exec(value);
    if (quoted) value = quoted[2]!;
    else value = value.replace(/[ \t]+#.*$/, "").trim();
    if (value !== "") return true;
  }
  return false;
}

/** A DeepSeek key dsh will find: the launch env, its stored credential file,
 * or its home .env (the layers dsh documents, read-only here). The stored
 * file outranks the env inside dsh; for "is there a key at all" any layer counts. */
export function hasDeepSeekKey(env: Record<string, string | undefined>): boolean {
  if (nonEmpty(env.DEEPSEEK_API_KEY)) return true;
  const home = dshHome(env);
  return storedCredentialPresent(join(home, ".credentials.yaml")) || dotenvValuePresent(join(home, ".env"));
}

/** Every `value` string under a select option, descending into groups. */
function offeredWireValues(sessionConfig: unknown, configId: string): { values: string[]; current: string | null } {
  const options = (sessionConfig as { configOptions?: unknown[] } | undefined)?.configOptions;
  const option = (Array.isArray(options) ? options : []).find(
    (entry: any) => entry?.id === configId,
  ) as { currentValue?: unknown; options?: unknown } | undefined;
  const values: string[] = [];
  const collect = (entries: unknown) => {
    if (!Array.isArray(entries)) return;
    for (const entry of entries) {
      if (typeof entry?.value === "string") values.push(entry.value);
      else if (Array.isArray(entry?.options)) collect(entry.options);
    }
  };
  collect(option?.options);
  return { values, current: typeof option?.currentValue === "string" ? option.currentValue : null };
}

/** The `model` config option of a session/new answer, as a picker catalog.
 * Groups carry dsh's provider display name, kept as the option's provider
 * badge so a gateway's copy of a model stays distinguishable. */
export function parseDeepSeekCatalog(sessionResult: unknown): ModelCatalog {
  const options = (sessionResult as { configOptions?: unknown[] } | undefined)?.configOptions;
  const option = (Array.isArray(options) ? options : []).find((entry: any) => entry?.id === "model") as
    { currentValue?: unknown; options?: unknown } | undefined;
  const out: ModelCatalog["options"] = [];
  const collect = (entries: unknown, provider: string | undefined) => {
    if (!Array.isArray(entries)) return;
    for (const entry of entries) {
      if (Array.isArray(entry?.options)) {
        const name = typeof entry.name === "string" && entry.name.trim() ? entry.name.trim()
          : typeof entry.group === "string" ? entry.group : provider;
        collect(entry.options, name);
        continue;
      }
      if (typeof entry?.value !== "string") continue;
      const id = deepseekPickerId(entry.value);
      if (!id || out.some((row) => row.id === id)) continue;
      const label = typeof entry.name === "string" && entry.name.trim() ? entry.name.trim() : id;
      out.push({ id, label, ...(provider ? { provider } : {}) });
    }
  };
  collect(option?.options, undefined);
  if (!out.length) throw new Error("dsh advertised no models");
  const current = typeof option?.currentValue === "string" ? deepseekPickerId(option.currentValue) : null;
  const selected = out.find((model) => model.id === current);
  const variants = acpVariantOption(sessionResult)?.options;
  if (selected && variants?.length) selected.variants = variants;
  return {
    default: current && out.some((row) => row.id === current) ? current : out[0]!.id,
    options: out,
  };
}

export const DEEPSEEK_MODELS_TIMEOUT_ENV = "OPENMAUS_DEEPSEEK_MODELS_TIMEOUT_MS";
/** A warm `dsh --profile acp` answers session/new in ~2 s; the first boot
 * after an install also creates the profile, so leave real headroom. */
export const DEEPSEEK_MODELS_DEFAULT_TIMEOUT_MS = 60_000;

function catalogTimeoutMs(env: Record<string, string | undefined>): number {
  const raw = env[DEEPSEEK_MODELS_TIMEOUT_ENV];
  if (raw === undefined) return DEEPSEEK_MODELS_DEFAULT_TIMEOUT_MS;
  const parsed = Number(raw);
  return Number.isInteger(parsed) && parsed > 0 && parsed <= 2_147_483_647 ? parsed : DEEPSEEK_MODELS_DEFAULT_TIMEOUT_MS;
}

const VARIANT_DISCOVERY_MODEL_LIMIT = 128;

/** Ask the installed dsh what it offers: initialize, session/new in `cwd`,
 * read each model's thinking choices (a model dsh won't switch to keeps none), session/close.
 * Rejects on any other failure so the core keeps the last usable catalog; never resolves to an
 * empty or guessed list. */
export function fetchDeepSeekCatalog(
  cli: string,
  env: Record<string, string | undefined>,
  cwd: string,
  allowEnv: readonly string[] = [],
): Promise<ModelCatalog> {
  return new Promise((resolve, reject) => {
    let child: ReturnType<typeof spawnCli>;
    try {
      // Security: while the bot box is on, dsh runs in it, as a bot's turn does: it reads ~/.dsh, which a bot can write.
      const resolved = resolveCli(cli, ["--profile", "acp"], env as NodeJS.ProcessEnv);
      child = spawnCli(resolved.command, resolved.args, { stdio: ["pipe", "pipe", "ignore"], env: { ...env, ...resolved.env }, box: { allowEnv, engine: "deepseekAgent" } });
    } catch (error) {
      return reject(error instanceof Error ? error : new Error(String(error)));
    }
    let settled = false;
    let timer: ReturnType<typeof setTimeout> | undefined;
    const finish = (outcome: { catalog: ModelCatalog } | { error: Error }) => {
      if (settled) return;
      settled = true;
      if (timer) clearTimeout(timer);
      try {
        child.stdin?.end();
      } catch {}
      // dsh's own children (its MCP servers) go too: spawnCli started it in a group of its own.
      void killCliTree(child, 1_000);
      if ("catalog" in outcome) resolve(outcome.catalog);
      else reject(outcome.error);
    };
    timer = setTimeout(() => finish({ error: new Error(`dsh timed out after ${catalogTimeoutMs(env)} ms without answering session/new`) }), catalogTimeoutMs(env));
    child.once("error", (error) => finish({ error }));
    // A child that already exited makes stdin emit EPIPE, and an unhandled stream 'error' crashes the server.
    child.stdin?.on("error", (error) => finish({ error }));
    child.once("close", (code, signal) => finish({ error: new Error(`dsh exited (${signal ?? code}) before answering session/new`) }));

    let buf = "";
    let nextId = 0;
    const send = (method: string, params: unknown): number => {
      nextId += 1;
      try {
        child.stdin!.write(`${JSON.stringify({ jsonrpc: "2.0", id: nextId, method, params })}\n`, (error) => {
          if (error) finish({ error });
        });
      } catch (error) {
        finish({ error: error instanceof Error ? error : new Error(String(error)) });
      }
      return nextId;
    };
    let initId = 0;
    let sessionReqId = 0;
    let closeId = 0;
    let catalog: ModelCatalog | null = null;
    let sessionId: string | null = null;
    let selectId = 0;
    let selectedModel: ModelCatalog["options"][number] | undefined;
    const pendingModels: ModelCatalog["options"] = [];
    const nextModel = () => {
      selectedModel = pendingModels.shift();
      if (selectedModel) {
        selectId = send("session/set_config_option", { sessionId, configId: "model", value: deepseekWireValue(selectedModel.id) });
      } else {
        closeId = send("session/close", { sessionId });
      }
    };
    child.stdout?.on("data", (chunk) => {
      buf += String(chunk);
      let nl: number;
      while ((nl = buf.indexOf("\n")) >= 0) {
        const line = buf.slice(0, nl);
        buf = buf.slice(nl + 1);
        let msg: any;
        try {
          msg = JSON.parse(line);
        } catch {
          continue;
        }
        if (msg?.id === initId) {
          if (!msg.result) return finish({ error: new Error(`dsh rejected initialize: ${JSON.stringify(msg.error ?? msg)}`) });
          sessionReqId = send("session/new", { cwd, mcpServers: [] });
        } else if (sessionReqId && msg?.id === sessionReqId) {
          sessionId = typeof msg.result?.sessionId === "string" ? msg.result.sessionId : null;
          try {
            catalog = parseDeepSeekCatalog(msg.result);
          } catch (error) {
            // Best effort: the close is sent, but an unusable answer does not wait on it.
            if (sessionId) send("session/close", { sessionId });
            return finish({ error: error instanceof Error ? error : new Error(String(error)) });
          }
          if (!sessionId) return finish({ catalog });
          if (catalog.options.length <= VARIANT_DISCOVERY_MODEL_LIMIT) {
            const current = offeredWireValues(msg.result, "model").current;
            pendingModels.push(...catalog.options.filter((model) => deepseekWireValue(model.id) !== current));
          }
          nextModel();
        } else if (selectId && msg?.id === selectId && selectedModel) {
          if (offeredWireValues(msg.result, "model").current === deepseekWireValue(selectedModel.id)) {
            const variants = acpVariantOption(msg.result)?.options;
            if (variants?.length) selectedModel.variants = variants;
          }
          nextModel();
        } else if (closeId && msg?.id === closeId) {
          finish({ catalog: catalog! });
        }
      }
    });
    initId = send("initialize", {
      protocolVersion: 1,
      clientCapabilities: { fs: { readTextFile: false, writeTextFile: false } },
      clientInfo: { name: "openmausbot", version: "catalog" },
    });
  });
}

// dsh first exposes ctx.sandboxPolicy.registerWritableRoot in 0.2.2-rc.2.
const MIN_APPROVAL_VERSION = "0.2.2-rc.2";
const versionProbes = new Map<string, ReturnType<typeof cliVersionProbe>>();
const clisWithApprovalPatch = new Set<string>();
const approvalDir = resolve(DATA_DIR, "engines", "dsh");
const approvalPlugin = join(approvalDir, "approval-preset.mjs");
const reviewPatch = join(approvalDir, "approval-review.patch.yml");
const sandboxPatch = join(approvalDir, "approval-sandbox.patch.yml");

/** The data folder as a conversation's folder is named (its real path), so the plugin finds the bot's memory from it. */
function approvalDataDir(): string {
  try { return realpathSync(DATA_DIR); } catch { return DATA_DIR; }
}

function approvalPatch(review: boolean): string {
  const patch = review ? reviewPatch : sandboxPatch;
  mkdirSync(approvalDir, { recursive: true });
  writeFileAtomicIfChanged(approvalPlugin, DSH_APPROVAL_PLUGIN);
  writeFileAtomicIfChanged(patch, stringify([{ insert: [
    { id: "openmausbot-approval-preset", name: approvalPlugin, config: { review, dataDir: approvalDataDir() } },
  ] }]));
  return patch;
}

export const deepseekSupport: AcpSupport = {
  driverKind: "deepseekAgent",
  displayName: "DeepSeek Harness",
  // @deepseek-ai/dsh declares engines.node ">=24.9.0"; on older Node it quits before answering.
  minNodeVersion: "24.9.0",
  models: DEEPSEEK_MODELS,
  defaultCli: "dsh",
  nativeSource: "deepseek.acp",
  loginNote: "DeepSeek Harness has no DeepSeek API key — paste one under Settings → AI → Providers, or set DEEPSEEK_API_KEY",

  install: {
    command: {
      darwin: "npm install -g @deepseek-ai/dsh",
      linux: "npm install -g @deepseek-ai/dsh",
      win32: "npm install -g @deepseek-ai/dsh",
    },
    docsUrl: "https://github.com/deepseek-ai/deepseek-harness",
    needsNode: true,
    // Keyless is "paste a key in Settings", not a terminal sign-in.
    settings: "connections",
  },

  resolveCommand: async (env, { cli }) => {
    let probe = versionProbes.get(cli);
    if (!probe) {
      probe = cliVersionProbe(cli);
      versionProbes.set(cli, probe);
    }
    const result = await probe(env);
    if ("version" in result && semVerAtLeast(result.version, MIN_APPROVAL_VERSION)) {
      clisWithApprovalPatch.add(cli);
      return { command: cli, ro: [approvalPlugin, reviewPatch, sandboxPatch] };
    }
    clisWithApprovalPatch.delete(cli);
    return { command: cli };
  },
  spawnArgs: ({ cli }, turn) => {
    const args = ["--profile", "acp"];
    if (clisWithApprovalPatch.has(cli)) args.push("--patch", approvalPatch(turn.approvalMode === "auto"));
    return args;
  },
  updateFilter: (calls) => (update) => {
    const { sessionUpdate, toolCallId, status } = update;
    if ((sessionUpdate === "tool_call" || sessionUpdate === "tool_call_update") && toolCallId) {
      if (status === "completed" || status === "failed") calls.delete(toolCallId);
      else calls.set(toolCallId, { ...calls.get(toolCallId), ...update });
    }
    return update;
  },
  permissionRequest: (params, calls) => {
    // dsh sends the call details in session/update and only its ID in session/request_permission.
    const call = calls.get(params.toolCall.toolCallId);
    if (!call) return params;
    const { title, kind, rawInput, locations } = call;
    const isCommand = typeof rawInput?.command === "string";
    const path = rawInput?.path ?? rawInput?.file_path;
    return { ...params, toolCall: {
      title: !isCommand && typeof path === "string" ? `${title} ${path}` : title,
      kind: isCommand ? "execute" : kind, rawInput, locations, ...params.toolCall,
    } };
  },
  credentialEnv: ["DEEPSEEK_API_KEY"],
  // Model and reasoning effort are session config options (see header).
  selectModel: { configId: "model" },
  modelVariants: true,
  resumeMethod: "resume",
  // dsh 0.2 rejects session/resume of a session still open in the same process
  // ("session is already active"), so a turn with new tools needs a new process.
  restartOnMcpChange: true,
  // Refresh only: a fresh profile boot can take long, and startup must not wait on it.
  resolveModels: (env, config, _instanceId, _fresh, allowEnv) => fetchDeepSeekCatalog(config.cli || "dsh", env, tmpdir(), allowEnv),
  resolveModelsOnCreate: false,

  // initialize advertises no auth methods; the key rides in the child env or
  // dsh's own credential file, so there is nothing to call authenticate for.
  pickAuthMethod: () => null,
  authFailure: "continue",
  isAuthenticated: (env) => hasDeepSeekKey(env),

  resolveTurnModel: (model) => (model ? deepseekWireValue(model) : model),
  fallbackModel: (sessionConfig, catalogDefault) => {
    const { values, current } = offeredWireValues(sessionConfig, "model");
    const wanted = catalogDefault ? deepseekWireValue(catalogDefault) : "";
    if (wanted && values.includes(wanted)) return wanted;
    if (current && values.includes(current)) return current;
    return null;
  },

  // House convention for ACP harnesses: the persona rides in the prompt text.
  buildPromptText: (turn) => (turn.system ? `${turn.system}\n\n${turn.text}` : turn.text),
};

export const DeepSeekAgentDriver = createAcpDriver(deepseekSupport);