Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

5.5 KB
Raw Blame
import type { ToolAfterCallContext } from '@lobechat/types';
import { BaseExecutor } from '@lobechat/types';
import { isRecord } from '@lobechat/utils/object';

import {
  recordGitCommandEffects,
  recordWorktreeEnter,
  recordWorktreeExit,
} from './worktreeDetection';

/**
 * Hook-only executor for a heterogeneous CLI agent's tool identifier
 * (`amp` / `antigravity` / `claude-code` / `codebuddy` / `codex` / `cursor` / `droid` / `grok-build` /
 * `kimi-code` / `opencode` / `pi` / `qoder` / `trae` β€” set by the adapters in
 * `packages/heterogeneous-agents/src/adapters/*`). These agents run their OWN
 * tools, so this executor is NEVER invoked: `apiEnum` is empty β†’ `hasApi()` is
 * always false β†’ the client-tool dispatch (`hasExecutor`) never routes to
 * `invoke`, and no phantom tool is exposed to the model.
 *
 * It exists solely so the `tool_end` dispatcher
 * (`gatewayEventHandler.dispatchOnAfterCall`, which resolves the executor by the
 * tool's `identifier`) can reach `onAfterCall` and observe the CLI's shell tool
 * results renderer-side β€” the same seam idiomatic builtin tools use to react to
 * their own mutations.
 */
const EMPTY_API_ENUM = {} as Record<string, string>;

/**
 * Pull the command out of a shell tool's parsed params. Only reads `command`, `cmd`,
 * or Antigravity's `CommandLine` β€” never `content` β€” so file writes aren't commands.
 * Returns the raw value (string OR the argv-array form) so the detector can keep
 * argument boundaries.
 */
const readShellCommand = (params: unknown): string | string[] | undefined => {
  if (!isRecord(params)) return undefined;
  const raw = params.command ?? params.cmd ?? params.CommandLine;
  if (typeof raw === 'string') return raw;
  if (Array.isArray(raw) && raw.every((t) => typeof t === 'string')) return raw as string[];
  return undefined;
};

/**
 * The CLI's own worktree tools, when it has any. Claude Code moves its session with
 * `EnterWorktree` / `ExitWorktree` and never shells out, so the `git worktree add`
 * sniffing in `recordGitCommandEffects` cannot observe those moves. Codex has no
 * equivalent tool and so passes `undefined`.
 */
interface WorktreeApiNames {
  enter: string;
  exit: string;
}

class HeteroCliExecutor extends BaseExecutor<typeof EMPTY_API_ENUM> {
  protected readonly apiEnum = EMPTY_API_ENUM;

  /**
   * @param identifier The CLI adapter's tool identifier.
   * @param shellApiNames The adapter's shell / run-command tool api name(s). Side
   *   effects are constrained to THIS tool first, then handled uniformly β€” we don't
   *   sniff every tool call's params.
   * @param worktreeApiNames The adapter's native enter/exit worktree tools, if any.
   */
  constructor(
    readonly identifier: string,
    private readonly shellApiNames: ReadonlySet<string>,
    private readonly worktreeApiNames?: WorktreeApiNames,
  ) {
    super();
  }

  onAfterCall = async ({
    apiName,
    params,
    result,
    toolCallId,
    topicId,
  }: ToolAfterCallContext): Promise<void> => {
    // Constrain to a SUCCESSFUL call bound to a run topic. A refused `ExitWorktree`
    // (dirty worktree, no active session) fails validation and lands here with
    // `success: false` β€” it must not clear the topic's worktree.
    if (!result.success || !topicId) return;

    if (apiName === this.worktreeApiNames?.enter) {
      await recordWorktreeEnter({ content: result.content, topicId });
      return;
    }
    if (apiName === this.worktreeApiNames?.exit) {
      await recordWorktreeExit({ content: result.content, topicId });
      return;
    }

    if (!this.shellApiNames.has(apiName)) return;

    const command = readShellCommand(params);
    if (command === undefined) return;

    await recordGitCommandEffects({
      command,
      resultContent: result.content,
      toolCallId,
      topicId,
    });
  };
}

// AMP's shell tool is `shell_command`; CC, CodeBuddy, and Qoder use `Bash`; Codex's is
// `command_execution`; Cursor's is `shellToolCall`; Grok Build uses ACP's stable `execute`
// kind; Kimi Code uses `Shell`; OpenCode and Pi both use `bash`.
export const ampExecutor = new HeteroCliExecutor('amp', new Set(['shell_command']));
export const antigravityExecutor = new HeteroCliExecutor('antigravity', new Set(['run_command']));
export const claudeCodeExecutor = new HeteroCliExecutor('claude-code', new Set(['Bash']), {
  enter: 'EnterWorktree',
  exit: 'ExitWorktree',
});
export const codeBuddyExecutor = new HeteroCliExecutor('codebuddy', new Set(['Bash']));
export const codexExecutor = new HeteroCliExecutor('codex', new Set(['command_execution']));
export const copilotExecutor = new HeteroCliExecutor('copilot', new Set(['execute']));
export const cursorExecutor = new HeteroCliExecutor('cursor', new Set(['shellToolCall']));
// Droid's ACP shell tool name is not a documented protocol guarantee. Keep this
// hook inert rather than guessing and recording unrelated tool calls.
export const droidExecutor = new HeteroCliExecutor('droid', new Set());
export const devinExecutor = new HeteroCliExecutor('devin', new Set(['exec']));
export const grokBuildExecutor = new HeteroCliExecutor('grok-build', new Set(['execute']));
export const kimiCodeExecutor = new HeteroCliExecutor('kimi-code', new Set(['Shell']));
export const openCodeExecutor = new HeteroCliExecutor('opencode', new Set(['bash']));
export const piExecutor = new HeteroCliExecutor('pi', new Set(['bash']));
export const qoderExecutor = new HeteroCliExecutor('qoder', new Set(['Bash']));
export const traeExecutor = new HeteroCliExecutor('trae', new Set());