detectAsync method ✓ 100.0%

Last updated: 2026-03-01T23:25:47.102Z

Metrics

LOC: 48 Complexity: 9 Params: 1 Coverage: 100.0% (17/17 lines, 12x executed)

Signature

detectAsync(field: FormField): : Promise<ClassifierResult | null>

Summary

Async path — sends field HTML context to the background service worker which prompts Gemini Nano and parses the JSON reply. On success, persists the classification to the learning store so TF.js prototype vectors are updated for future page loads. Returns null on unavailability, timeout, or parse failure.

Source Code

  async detectAsync(field: FormField): Promise<ClassifierResult | null> {
    const fieldLabel =
      field.label ??
      field.contextSignals ??
      field.name ??
      field.id ??
      field.selector;
    log.debug(`[detectAsync] Iniciando classificação para "${fieldLabel}"`);

    try {
      const input = buildClassifierInput(field);
      const result = await classifyFieldViaProxy(input);

      if (!result) {
        log.debug(
          `[detectAsync] Classificação retornou null para "${fieldLabel}"`,
        );
        return null;
      }

      log.debug(
        `"${fieldLabel}" → ${result.fieldType} (generator: ${result.generatorType}, ${(result.confidence * 100).toFixed(0)}%)`,
      );

      // ── Persist to dataset + learning store ───────────────────────────
      const signals = field.contextSignals ?? "";
      if (signals) {
        addDatasetEntry({
          signals,
          type: result.fieldType,
          source: "auto",
          difficulty: "easy",
        }).catch(() => {
          /* non-critical */
        });
        storeLearnedEntry(signals, result.fieldType, result.generatorType)
          .then(() => invalidateClassifier())
          .catch(() => {
            /* non-critical — ignore storage errors */
          });
      }

      return { type: result.fieldType, confidence: result.confidence };
    } catch (err) {
      log.warn("Erro na classificação via proxy:", (err as Error).message);
      return null;
    }
  },

Dependencies (Outgoing)

graph LR detectAsync["detectAsync"] buildClassifierInput["buildClassifierInput"] invalidateClassifier["invalidateClassifier"] detectAsync -->|calls| buildClassifierInput detectAsync -->|calls| invalidateClassifier style detectAsync fill:#dbeafe,stroke:#2563eb,stroke-width:2px click detectAsync "25ab12b02ee66faf.html" click buildClassifierInput "48b9b13d289a7b63.html" click invalidateClassifier "a97a4f5efc9940ea.html"

No incoming dependencies.