All files / src tool-result.ts

96% Statements 24/25
89.28% Branches 25/28
100% Functions 3/3
96% Lines 24/25

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    3x     14x         3x 10x 1x     9x 1x     8x 2x 2x 2x 1x   1x     6x 1x     5x 1x     4x 4x           3x 12x 10x   10x                                   2x                    
import { IsAgentReadyApiError } from "./client.js";
 
export const toTextResult = (
  text: string,
  structuredContent?: Record<string, unknown>,
) => ({
  content: [{ type: "text" as const, text }],
  ...(structuredContent ? { structuredContent } : {}),
});
 
const getRecoverySuggestion = (status: number, message: string, details: unknown): string | null => {
  if (status === 429) {
    return "Rate limit exceeded. Wait a moment and retry. If scanning, note that recently scanned domains have a 1-hour cooldown.";
  }
 
  if (status === 404) {
    return "No scan found for this domain. Use scan_website to trigger a new scan first.";
  }
 
  if (status === 400) {
    const detailStr = typeof details === "string" ? details : JSON.stringify(details ?? "");
    const lower = detailStr.toLowerCase();
    if (lower.includes("url")) {
      return "Invalid or missing URL. Ensure the URL starts with http:// or https://.";
    }
    return "Missing required parameter. Ensure the URL or domain is provided.";
  }
 
  if (status === 409) {
    return "Conflict — a scan for this domain is already in progress. Wait for it to complete, then use get_scan_results to retrieve the results.";
  }
 
  if (status === 422) {
    return "Invalid URL. The URL must start with http:// or https:// and be a valid website address.";
  }
 
  Eif (status >= 500) {
    return "IsAgentReady API server error. This is a temporary issue. Wait a moment and retry.";
  }
 
  return null;
};
 
export const toErrorResult = (error: unknown) => {
  if (error instanceof IsAgentReadyApiError) {
    const suggestion = getRecoverySuggestion(error.status, error.message, error.details);
 
    return {
      content: [
        {
          type: "text" as const,
          text: [
            `IsAgentReady API error: ${error.message}`,
            `Status: ${error.status}`,
            error.details ? `Details: ${JSON.stringify(error.details, null, 2)}` : "",
            suggestion ? `\nRecovery: ${suggestion}` : "",
          ]
            .filter(Boolean)
            .join("\n"),
        },
      ],
      isError: true,
    };
  }
 
  return {
    content: [
      {
        type: "text" as const,
        text: error instanceof Error ? error.message : String(error),
      },
    ],
    isError: true,
  };
};