Skip to content

Fast, accurate OCR with correctness assessment.

7.1 KB
Raw Blame
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
import { imageParseSourceUnavailable, type ImageParseSource } from '@/api/map/imageParseSource';

import type { ImageParseDraft } from './useImageParseDraft';
import type { ImageParseLayoutBlock } from '../utils/imageParseSample';
import { imageParseBlockId } from '../utils/imageParseBlocks';
import { imageParseDraftRanges } from '../utils/imageParseDraftRanges';
import { imageParseBlockCanRegenerate } from '../utils/imageParseQuality';
import { imageParseBlockGenerator } from '../utils/imageParseRegenerationRequest';
import {
  imageParseRegenerationErrorKey,
  imageParseRegenerationIds,
  regenerateMappedImageParseBlock,
  type ImageParseRegeneration,
} from '../utils/imageParseRegeneration';

interface RegenerationInput {
  resultKey: string;
  /** Document-parsing model ID visible to the current logged-in user. */
  model: string;
  /** Plaintext secret of the currently selected API Key; passed in memory only and not persisted to disk. */
  secret: string;
  blocks: readonly ImageParseLayoutBlock[];
  draft: ImageParseDraft;
  documentDataUrl: string;
  mime: string;
  pageIndex: number;
  loading: boolean;
  source?: ImageParseSource;
  /** Refresh Playground free-trial state after a request reaches a terminal state. */
  refreshTrial?: () => void;
}

/** Orchestrate active block optimization: limit the budget, protect manual edits, and abort old tasks that cross documents or page numbers. */
export function useImageParseRegeneration(input: RegenerationInput) {
  const { resultKey, blocks } = input;
  const { refreshTrial } = input;
  const latest = useRef(input);
  useLayoutEffect(() => {
    latest.current = input;
  }, [input]);
  const active = useRef<AbortController | null>(null);
  const [state, setState] = useState<{
    key: string;
    histories: Record<string, Record<string, ImageParseRegeneration>>;
    pending: ReadonlySet<string>;
  }>({ key: resultKey, histories: {}, pending: new Set() });
  const results = useMemo(() => state.histories[resultKey] ?? {}, [resultKey, state.histories]);
  const pending = state.key === resultKey ? state.pending : new Set<string>();
  const unavailableReason = imageParseSourceUnavailable(input.source) ??
    (input.secret ? undefined : 'imageParseAuthFailed');
  const available = unavailableReason === undefined;

  const stop = useCallback(() => {
    active.current?.abort();
    active.current = null;
    setState((previous) => ({ ...previous, pending: new Set() }));
  }, []);
  useEffect(() => stop, [resultKey, stop]);

  const reset = useCallback(() => {
    stop();
    setState((previous) => ({
      ...previous,
      key: resultKey,
      histories: { ...previous.histories, [resultKey]: {} },
      pending: new Set(),
    }));
  }, [resultKey, stop]);

  const run = useCallback(
    async (ids: readonly string[]) => {
      const eligibleIds = ids.filter((id) => !results[id]);
      if (active.current || input.loading || !available || !eligibleIds.length) return;
      const controller = new AbortController();
      active.current = controller;
      const signal = controller.signal;
      const isCurrent = () => !signal.aborted && latest.current.resultKey === resultKey;
      setState((previous) => ({
        key: resultKey,
        histories: previous.histories,
        pending: new Set(eligibleIds),
      }));
      // Call the batch in order, so the number of low-score blocks does not amplify service concurrency; one block failing on its own does not clear results already finished.
      for (const id of eligibleIds) {
        if (!isCurrent()) break;
        const original = blocks.find((block, index) => imageParseBlockId(block, index) === id);
        if (!original || !imageParseBlockCanRegenerate(original)) continue;
        const previous = results[id];
        const currentBest = previous?.best ?? original;
        let record: ImageParseRegeneration = previous ?? {
          original,
          best: original,
          rounds: 0,
          improved: false,
        };
        try {
          record = await regenerateMappedImageParseBlock({
            id,
            original,
            currentBest,
            signal,
            getDraft: () => {
              if (!isCurrent()) throw new DOMException('Cancelled', 'AbortError');
              return latest.current.draft;
            },
            generate: imageParseBlockGenerator(
              {
                source: input.source,
                model: input.model,
                secret: input.secret,
                documentDataUrl: input.documentDataUrl,
                mime: input.mime,
                pageIndex: input.pageIndex,
              },
              original,
              id,
              signal,
            ),
            onApply: (next, range) => {
              const draft = latest.current.draft;
              draft.setValue(next, range, 'model');
              // React may batch the whole commit; the next block must synchronously obtain the updated text and offsets.
              latest.current = {
                ...latest.current,
                draft: {
                  ...draft,
                  value: next,
                  ranges: imageParseDraftRanges(draft.value, next, draft.ranges, range),
                },
              };
            },
          });
        } catch (failure) {
          if (controller.signal.aborted || latest.current.resultKey !== resultKey) break;
          record = {
            ...record,
            best: currentBest,
            improved: false,
            error: imageParseRegenerationErrorKey(failure),
          };
        }
        if (controller.signal.aborted || latest.current.resultKey !== resultKey) break;
        setState((previous) => ({
          key: resultKey,
          histories: {
            ...previous.histories,
            [resultKey]: { ...previous.histories[resultKey], [id]: record },
          },
          pending: new Set([...previous.pending].filter((item) => item !== id)),
        }));
      }
      // Each block optimization is a real L2 request; refresh the free count after the batch ends.
      refreshTrial?.();
      if (active.current === controller) {
        active.current = null;
        setState((previous) => ({ ...previous, pending: new Set() }));
      }
    },
    [
      available,
      blocks,
      input.documentDataUrl,
      input.loading,
      input.mime,
      input.model,
      input.pageIndex,
      input.secret,
      input.source,
      refreshTrial,
      resultKey,
      results,
    ],
  );

  const layout = useMemo(
    () =>
      blocks.map((block, index) => {
        const record = results[imageParseBlockId(block, index)];
        return record ? record.best : block;
      }),
    [blocks, results],
  );
  const eligibleIds = imageParseRegenerationIds(layout, input.draft).filter((id) => !results[id]);

  return {
    ...({ unavailableReason } as { unavailableReason?: string }),
    available,
    results,
    pending,
    layout,
    eligibleLowCount: eligibleIds.length,
    busy: pending.size > 0,
    reset,
    stop,
    regenerateBlock: (id: string) => run([id]),
    regenerateLowBlocks: () => run(eligibleIds),
  };
}