callAiWithTimeout function ✓ 100.0%

Last updated: 2026-03-04T23:21:38.413Z

Metrics

LOC: 27 Complexity: 3 Params: 1 Coverage: 100.0% (8/8 lines, 9x executed)

Signature

callAiWithTimeout( fn: (field: FormField) => Promise<string>, field: FormField, context: string, timeoutMs = DEFAULT_AI_TIMEOUT_MS, ): : Promise<string>

Summary

Wraps an AI call with a hard timeout so it never blocks indefinitely.

Source Code

async function callAiWithTimeout(
  fn: (field: FormField) => Promise<string>,
  field: FormField,
  context: string,
  timeoutMs = DEFAULT_AI_TIMEOUT_MS,
): Promise<string> {
  const label = field.label ?? field.id ?? field.selector;
  log.info(
    `🤖 AI gerando valor para: "${label}" (${context}, timeout ${timeoutMs}ms)...`,
  );
  const start = Date.now();

  const result = await Promise.race([
    fn(field),
    new Promise<string>((_, reject) =>
      setTimeout(
        () => reject(new Error(`AI timeout (${timeoutMs}ms)`)),
        timeoutMs,
      ),
    ),
  ]);

  log.info(
    `✅ AI concluiu em ${Date.now() - start}ms: "${result.slice(0, 60)}"`,
  );
  return result;
}

No outgoing dependencies.

Impact (Incoming)

graph LR callAiWithTimeout["callAiWithTimeout"] resolveFieldValue["resolveFieldValue"] resolveFieldValue -->|calls| callAiWithTimeout style callAiWithTimeout fill:#dbeafe,stroke:#2563eb,stroke-width:2px click callAiWithTimeout "4ef7f4ef5ff6b7b3.html" click resolveFieldValue "b0ea06e6c355d586.html"
SourceType
resolveFieldValue calls