All files / agent toolArgumentRecovery.ts

100% Statements 88/88
100% Branches 26/26
100% Functions 4/4
100% Lines 88/88

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117        1x 1x         8x 8x 8x 7x 7x 1x 1x 1x 1x 1x         8x 8x 8x 8x 8x 1x 1x 7x 7x         11x 11x 1x 1x 10x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 9x 9x 1x 1x             11x 11x 11x 11x 11x 11x 11x 11x 11x 11x   11x 11x 11x 11x 11x 11x 11x 11x 11x 11x   11x 11x 11x 11x 11x 11x 11x 11x 11x 11x   11x 11x 11x 11x   10x 11x 2x 2x   8x 11x 1x 1x   7x 7x 11x 2x 2x 11x  
import OpenAI from "openai";
 
import type { ToolJsonSchema } from "./types";
 
const MAX_RAW_SNIPPET = 6_000;
const MAX_SCHEMA_SNIPPET = 2_000;
 
/**
 * Remove cercas Markdown do texto devolvido pelo modelo.
 */
function stripCodeFence(text: string): string {
  const normalized = text.trim();
  if (!normalized.startsWith("```")) {
    return normalized;
  }
  return normalized
    .replace(/^```[a-zA-Z]*\s*/u, "")
    .replace(/\s*```$/u, "")
    .trim();
}
 
/**
 * Extrai o primeiro fragmento que parece um objeto JSON.
 */
function extractFirstJsonObject(text: string): string | null {
  const stripped = stripCodeFence(text);
  const start = stripped.indexOf("{");
  const end = stripped.lastIndexOf("}");
  if (start < 0 || end <= start) {
    return null;
  }
  return stripped.slice(start, end + 1).trim();
}
 
/**
 * Resume o schema da tool para o prompt de recuperação (evita enviar ficheiros enormes).
 */
function summarizeParameters(parameters: ToolJsonSchema | undefined): string {
  if (!parameters?.properties || typeof parameters.properties !== "object") {
    return "(schema não disponível)";
  }
  const keys = Object.keys(parameters.properties);
  const required = Array.isArray(parameters.required) ? parameters.required : [];
  const raw = JSON.stringify(
    {
      required,
      properties: parameters.properties,
    },
    null,
    2,
  );
  if (raw.length <= MAX_SCHEMA_SNIPPET) {
    return raw;
  }
  return `${raw.slice(0, MAX_SCHEMA_SNIPPET)}…\n(truncado)`;
}
 
/**
 * Um pedido LLM curto e determinístico para corrigir argumentos JSON de uma tool quando o parse local falhou.
 *
 * @returns Texto JSON válido para voltar a passar por `parseToolArguments`, ou `null` se não for possível.
 */
export async function recoverToolArgumentsWithLlm(
  client: OpenAI,
  model: string,
  toolName: string,
  rawArgumentsText: string,
  parseErrorMessage: string,
  parameters: ToolJsonSchema | undefined,
): Promise<string | null> {
  const raw = rawArgumentsText.length > MAX_RAW_SNIPPET ? `${rawArgumentsText.slice(0, MAX_RAW_SNIPPET)}…` : rawArgumentsText;
  const schemaHint = summarizeParameters(parameters);
 
  const userPrompt = [
    `Tool name: ${toolName}`,
    `Parse error: ${parseErrorMessage}`,
    "",
    "Expected JSON schema (reference):",
    schemaHint,
    "",
    "Broken / raw arguments from the model (fix into one JSON object only):",
    raw,
  ].join("\n");
 
  try {
    const response = await client.chat.completions.create({
      model,
      temperature: 0.1,
      max_tokens: 2_048,
      messages: [
        {
          role: "system",
          content:
            "You fix malformed tool-call arguments. Reply with a single JSON object only — no markdown fences, no commentary. " +
            "Keys must match the tool schema. Use ASCII double quotes for JSON strings.",
        },
        { role: "user", content: userPrompt },
      ],
    });
 
    const text = response.choices[0]?.message?.content;
    if (typeof text !== "string" || text.trim().length === 0) {
      return null;
    }
 
    const candidate = extractFirstJsonObject(text);
    if (!candidate) {
      return null;
    }
 
    JSON.parse(candidate);
    return candidate;
  } catch {
    return null;
  }
}