All files / tools coercePattern.ts

100% Statements 55/55
93.93% Branches 31/33
100% Functions 3/3
100% Lines 55/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73        156x 156x 156x     1x         1x 26x 26x 26x 156x 156x 150x 150x 156x 7x 5x 5x 5x 2x 2x 7x 1x 1x 7x 6x 26x 26x         1x 17x 17x 17x 12x 5x   17x 66x 66x 10x 10x 66x 2x 2x 2x 2x 2x 2x 2x 2x 66x   17x 25x 25x 1x 1x 25x   4x 4x  
/**
 * @param value - Valor candidato
 * @returns `true` se for um objeto plano (não array).
 */
function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}
 
/** Chaves onde o modelo/API aninha o payload real (ex.: `function.arguments`). */
const NEST_KEYS = ["arguments", "params", "input", "args", "data", "tool_input"] as const;
 
/**
 * Copia chaves de objetos aninhados para o nível superior sem sobrescrever chaves já definidas.
 */
export function mergeNestedPatternArgs(raw: Record<string, unknown>): Record<string, unknown> {
  const merged: Record<string, unknown> = { ...raw };
  const fillableKeys = new Set(["pattern", "Pattern", "query", "glob", "path", "regex", "regexp", "search", "q"]);
  for (const nestKey of NEST_KEYS) {
    const inner = raw[nestKey];
    if (!isRecord(inner)) {
      continue;
    }
    for (const [key, value] of Object.entries(inner)) {
      if (merged[key] === undefined) {
        merged[key] = value;
        continue;
      }
      const current = merged[key];
      const currentEmpty = typeof current === "string" && current.trim().length === 0;
      if (currentEmpty && fillableKeys.has(key) && (typeof value === "string" || typeof value === "number")) {
        merged[key] = value;
      }
    }
  }
  return merged;
}
 
/**
 * Obtém o texto de pesquisa para Grep/Glob quando o modelo usa chaves alternativas ou tipos soltos.
 */
export function coerceSearchPattern(raw: Record<string, unknown>, tool: "grep" | "glob"): string {
  const flat = mergeNestedPatternArgs(raw);
  const keys =
    tool === "grep"
      ? (["pattern", "Pattern", "regex", "regexp", "query", "search", "q"] as const)
      : (["pattern", "Pattern", "glob", "query", "search"] as const);
 
  for (const key of keys) {
    const value = flat[key];
    if (typeof value === "string" && value.trim().length > 0) {
      return value.trim();
    }
    if (Array.isArray(value) && value.length > 0) {
      const parts = value
        .map((entry) => (typeof entry === "string" ? entry : String(entry)))
        .map((entry) => entry.trim())
        .filter((entry) => entry.length > 0);
      if (parts.length > 0) {
        return parts.join("|");
      }
    }
  }
 
  for (const key of keys) {
    const value = flat[key];
    if (typeof value === "number" && Number.isFinite(value)) {
      return String(value);
    }
  }
 
  return "";
}