Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

9.1 KB
Raw Blame
import { flow } from "effect/Function";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import { TrimmedNonEmptyString } from "@akeru/contracts";
import * as Predicate from "effect/Predicate";
import { isToolLifecycleItemType } from "@akeru/contracts";
import { requestKindFromRequestType } from "./pendingRequests.ts";

// Tool data is an opaque provider object; each presentation field is decoded separately.
const RuntimeRecord = Schema.declare(Predicate.isObject);

// The activity payload contract is unknown. Web passes decoded JSON and mobile passes any
// object, so the readers accept both and decode each field they use.
type WorkLogPayload = {
  readonly data?: unknown;
  readonly detail?: unknown;
  readonly itemType?: unknown;
  readonly requestKind?: unknown;
  readonly requestType?: unknown;
};

const decodeRuntimeRecord = Schema.decodeUnknownOption(RuntimeRecord);

const decodeTrimmedString = Schema.decodeUnknownOption(TrimmedNonEmptyString);

export const asRecord = flow(decodeRuntimeRecord, Option.getOrNull);

export const asTrimmedString = flow(decodeTrimmedString, Option.getOrNull);

function trimMatchingOuterQuotes(value: string): string {
  const trimmed = value.trim();

  if (
    (trimmed.startsWith("'") && trimmed.endsWith("'")) ||
    (trimmed.startsWith('"') && trimmed.endsWith('"'))
  ) {
    const unquoted = trimmed.slice(1, -1).trim();

    return unquoted.length > 0 ? unquoted : trimmed;
  }

  return trimmed;
}

function executableBasename(value: string): string | null {
  const trimmed = trimMatchingOuterQuotes(value);

  if (trimmed.length === 0) {
    return null;
  }

  const normalized = trimmed.replace(/\\/g, "/");
  const segments = normalized.split("/");
  const last = segments.at(-1)?.trim() ?? "";

  return last.length > 0 ? last.toLowerCase() : null;
}

function splitExecutableAndRest(value: string): { executable: string; rest: string } | null {
  const trimmed = value.trim();

  if (trimmed.length === 0) {
    return null;
  }

  if (trimmed.startsWith('"') || trimmed.startsWith("'")) {
    const quote = trimmed.charAt(0);
    const closeIndex = trimmed.indexOf(quote, 1);

    if (closeIndex <= 0) {
      return null;
    }

    return {
      executable: trimmed.slice(0, closeIndex + 1),
      rest: trimmed.slice(closeIndex + 1).trim(),
    };
  }

  const firstWhitespace = trimmed.search(/\s/);

  if (firstWhitespace < 0) {
    return {
      executable: trimmed,
      rest: "",
    };
  }

  return {
    executable: trimmed.slice(0, firstWhitespace),
    rest: trimmed.slice(firstWhitespace).trim(),
  };
}

const SHELL_WRAPPER_SPECS = [
  {
    executables: ["pwsh", "pwsh.exe", "powershell", "powershell.exe"],
    wrapperFlagPattern: /(?:^|\s)-command\s+/i,
  },
  {
    executables: ["cmd", "cmd.exe"],
    wrapperFlagPattern: /(?:^|\s)\/c\s+/i,
  },
  {
    executables: ["bash", "sh", "zsh"],
    wrapperFlagPattern: /(?:^|\s)-(?:l)?c\s+/i,
  },
] as const;

function findShellWrapperSpec(shell: string) {
  return SHELL_WRAPPER_SPECS.find((spec) =>
    spec.executables.some((executable) => executable === shell),
  );
}

function unwrapCommandRemainder(value: string, wrapperFlagPattern: RegExp): string | null {
  const match = wrapperFlagPattern.exec(value);

  if (!match) {
    return null;
  }

  const command = value.slice(match.index + match[0].length).trim();

  if (command.length === 0) {
    return null;
  }

  const unwrapped = trimMatchingOuterQuotes(command);

  return unwrapped.length > 0 ? unwrapped : null;
}

function unwrapKnownShellCommandWrapper(value: string): string {
  const split = splitExecutableAndRest(value);

  if (!split || split.rest.length === 0) {
    return value;
  }

  const shell = executableBasename(split.executable);

  if (!shell) {
    return value;
  }

  const spec = findShellWrapperSpec(shell);

  if (!spec) {
    return value;
  }

  return unwrapCommandRemainder(split.rest, spec.wrapperFlagPattern) ?? value;
}

function formatCommandArrayPart(value: string): string {
  return /[\s"'`]/.test(value) ? `"${value.replace(/"/g, '\\"')}"` : value;
}

const CommandValue = Schema.Union([Schema.String, Schema.Array(Schema.Unknown)]);

type CommandValue = typeof CommandValue.Type;

const decodeCommandValue = Schema.decodeUnknownOption(CommandValue);

function formatDecodedCommandValue(value: CommandValue): string | null {
  const direct = asTrimmedString(value);

  if (direct) {
    return direct;
  }

  if (!Array.isArray(value)) {
    return null;
  }

  const parts: Array<string> = [];

  for (const entry of value) {
    const part = asTrimmedString(entry);

    if (part !== null) {
      parts.push(part);
    }
  }

  if (parts.length === 0) {
    return null;
  }

  return parts.map((part) => formatCommandArrayPart(part)).join(" ");
}

const formatCommandValue = flow(
  decodeCommandValue,
  Option.map(formatDecodedCommandValue),
  Option.getOrNull,
);

function normalizeCommandValue(value: Parameters<typeof formatCommandValue>[0]): string | null {
  const formatted = formatCommandValue(value);

  return formatted ? unwrapKnownShellCommandWrapper(formatted) : null;
}

function toRawToolCommand(
  value: Parameters<typeof formatCommandValue>[0],
  normalizedCommand: string | null,
): string | null {
  const formatted = formatCommandValue(value);

  if (!formatted || normalizedCommand === null) {
    return null;
  }

  return formatted === normalizedCommand ? null : formatted;
}

type ExtractToolCommandResult = {
  command: string | null;
  rawCommand: string | null;
};

export function extractToolCommand(payload: WorkLogPayload | null): ExtractToolCommandResult {
  const data = asRecord(payload?.data);
  const item = asRecord(data?.item);
  const itemResult = asRecord(item?.result);
  const itemInput = asRecord(item?.input);
  const itemType = asTrimmedString(payload?.itemType);
  const detail = asTrimmedString(payload?.detail);

  const candidates: unknown[] = [
    item?.command,
    itemInput?.command,
    itemResult?.command,
    data?.command,
    itemType === "command_execution" && detail ? stripTrailingExitCode(detail).output : null,
  ];

  for (const candidate of candidates) {
    const command = normalizeCommandValue(candidate);

    if (!command) {
      continue;
    }

    return {
      command,
      rawCommand: toRawToolCommand(candidate, command),
    };
  }

  return {
    command: null,
    rawCommand: null,
  };
}

type StripTrailingExitCodeResult = {
  output: string | null;
  exitCode?: number | undefined;
};

export function stripTrailingExitCode(value: string): StripTrailingExitCodeResult {
  const trimmed = value.trim();

  const match = /^(?<output>[\s\S]*?)(?:\s*<exited with exit code (?<code>\d+)>)\s*$/i.exec(
    trimmed,
  );

  if (!match?.groups) {
    return {
      output: trimmed.length > 0 ? trimmed : null,
    };
  }

  const exitCode = Number.parseInt(match.groups.code ?? "", 10);
  const normalizedOutput = match.groups.output?.trim() ?? "";

  return {
    output: normalizedOutput.length > 0 ? normalizedOutput : null,
    ...(Number.isInteger(exitCode) ? { exitCode } : {}),
  };
}

export function extractWorkLogItemType(payload: WorkLogPayload | null) {
  if (Predicate.isString(payload?.itemType) && isToolLifecycleItemType(payload.itemType)) {
    return payload.itemType;
  }

  return undefined;
}

export function extractWorkLogRequestKind(payload: WorkLogPayload | null) {
  if (
    payload?.requestKind === "command" ||
    payload?.requestKind === "file-read" ||
    payload?.requestKind === "file-change"
  ) {
    return payload.requestKind;
  }

  return requestKindFromRequestType(payload?.requestType) ?? undefined;
}

function pushChangedFile(
  target: string[],
  seen: Set<string>,
  value: Parameters<typeof asTrimmedString>[0],
) {
  const normalized = asTrimmedString(value);

  if (!normalized || seen.has(normalized)) {
    return;
  }

  seen.add(normalized);
  target.push(normalized);
}

function collectChangedFiles(
  value: Parameters<typeof asRecord>[0],
  target: string[],
  seen: Set<string>,
  depth: number,
) {
  if (depth > 4 || target.length >= 12) {
    return;
  }

  if (Array.isArray(value)) {
    for (const entry of value) {
      collectChangedFiles(entry, target, seen, depth + 1);

      if (target.length >= 12) {
        return;
      }
    }

    return;
  }

  const record = asRecord(value);

  if (!record) {
    return;
  }

  pushChangedFile(target, seen, record.path);
  pushChangedFile(target, seen, record.filePath);
  pushChangedFile(target, seen, record.relativePath);
  pushChangedFile(target, seen, record.filename);
  pushChangedFile(target, seen, record.newPath);
  pushChangedFile(target, seen, record.oldPath);

  for (const nestedKey of [
    "item",
    "result",
    "input",
    "data",
    "changes",
    "files",
    "edits",
    "patch",
    "patches",
    "operations",
  ]) {
    if (!(nestedKey in record)) {
      continue;
    }

    collectChangedFiles(record[nestedKey], target, seen, depth + 1);

    if (target.length >= 12) {
      return;
    }
  }
}

export function extractChangedFiles(payload: WorkLogPayload | null): string[] {
  const changedFiles: string[] = [];
  const seen = new Set<string>();
  collectChangedFiles(payload?.data, changedFiles, seen, 0);

  return changedFiles;
}