Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

8.7 KB
Raw Blame
import type { StoreApi } from 'zustand';
import { shallow } from 'zustand/shallow';
import { createWithEqualityFn, type UseBoundStoreWithEqualityFn } from 'zustand/traditional';

import { isHtmlFile } from '@/components/HtmlPreview/fileType';
import {
  cacheScope,
  createReplicaSlice,
  createReplicaState,
  recordLens,
  type ReplicaState,
  type ReplicaSyncResult,
} from '@/libs/replica';
import { cloudSandboxService } from '@/services/cloudSandbox';
import { isTerminalLocalFileError } from '@/services/electron/localFileService';
import { type LocalFilePreview, projectFileService } from '@/services/projectFile';

import {
  localFilePreviewKey,
  localFilePreviewResource,
  type LocalFilePreviewTarget,
  localFilePreviewTargetFromKey,
  toPersistedLocalFilePreview,
} from './projection';

export {
  isPersistableLocalFilePreviewKey,
  localFilePreviewKey,
  localFilePreviewResource,
  type LocalFilePreviewTarget,
  MAX_PERSISTED_PREVIEW_CHARS,
  MAX_PERSISTED_PREVIEWS,
  toPersistedLocalFilePreview,
} from './projection';

/**
 * Live-read a text file from the topic's cloud sandbox via the `readLocalFile`
 * sandbox tool. There is no durable copy anywhere β€” a recycled sandbox (or any
 * tool failure) rejects, surfacing the sandbox-unavailable empty state.
 */
const fetchSandboxFilePreview = async (
  path: string,
  topicId: string,
): Promise<LocalFilePreview> => {
  // `readLocalFile` defaults an omitted range to the first 200 lines β€” always
  // request the whole file for previews so long files don't render truncated.
  const result = await cloudSandboxService.callTool(
    'readLocalFile',
    { fullContent: true, path },
    { topicId },
  );
  if (!result.success || typeof result.result?.content !== 'string')
    throw new Error(result.error?.message || 'Failed to read sandbox file');

  return {
    content: result.result.content,
    contentType: result.result.mimeType || 'text/plain',
    type: 'text',
  };
};

const fetchLocalFilePreview = (key: string): Promise<LocalFilePreview> => {
  const {
    accept,
    allowExternalFile,
    deviceId,
    filePath,
    sandboxTopicId,
    topicId,
    workingDirectory,
  } = localFilePreviewTargetFromKey(key);
  if (sandboxTopicId) return fetchSandboxFilePreview(filePath, sandboxTopicId);

  // A local HTML file resolves its relative URLs against sibling files.
  const resourceScope = !deviceId && isHtmlFile({ path: filePath }) ? 'workspace' : undefined;
  return projectFileService.getLocalFilePreview({
    ...(accept && { accept }),
    allowExternalFile,
    deviceId,
    path: filePath,
    ...(resourceScope && { resourceScope }),
    ...(topicId && { topicId }),
    workingDirectory,
  });
};

/** What `useFetchPreview` reports; the preview itself is read with `useLocalFilePreview`. */
export interface LocalFilePreviewSyncResult extends ReplicaSyncResult {
  /** No preview yet (neither persisted nor fetched) and no error. */
  isLoading: boolean;
}

export interface LocalFilePreviewStoreState {
  /** Previews by {@link localFilePreviewKey}. */
  previewMap: Record<string, LocalFilePreview>;
  /** Replica bookkeeping for `previewMap`. */
  previewReplica: ReplicaState<LocalFilePreview>;
}

interface LocalFilePreviewStore extends LocalFilePreviewStoreState {
  /**
   * Start a save of `target`: captures the identity (user / workspace scope) the
   * save runs under and returns the commit that folds the saved text body into
   * the preview, so a reload paints the saved file. The commit is a no-op once
   * another identity is active β€” the body belongs to the scope that wrote it.
   */
  beginSave: (target: LocalFilePreviewTarget) => (content: string) => void;
  /** Re-read the file from its device / sandbox. */
  refreshPreview: (target: LocalFilePreviewTarget) => Promise<unknown>;
  /** Fetch orchestration only; read the preview with `useLocalFilePreview`. */
  useFetchPreview: (
    target: LocalFilePreviewTarget | undefined,
    enabled?: boolean,
  ) => LocalFilePreviewSyncResult;
}

export const initialLocalFilePreviewState: LocalFilePreviewStoreState = {
  previewMap: {},
  previewReplica: createReplicaState(),
};

/**
 * Annotated for the same reason as `useProjectDirectoryStore`: `useFetchPreview`
 * reads this store inside its initializer, which would otherwise make the
 * equality-fn overload infer its type circularly.
 */
export const useLocalFilePreviewStore: UseBoundStoreWithEqualityFn<
  StoreApi<LocalFilePreviewStore>
> = createWithEqualityFn<LocalFilePreviewStore>()((set, get) => {
  /**
   * Saves folded into each preview, by identity (cache scope) and key. A read
   * started before a save may still hold the pre-save file; once the save lands
   * it must not replace the confirmed content β€” the same guard as the document
   * detail's adoptions. A save under another identity never vouches for a read
   * of this one.
   */
  const saveGenerations = new Map<string, number>();
  const generationKey = (scope: string, key: string) => JSON.stringify([scope, key]);

  const previews = createReplicaSlice(localFilePreviewResource, {
    actionPrefix: 'localFilePreview/previews',
    fetcher: async (key) => {
      const scope = cacheScope.get();
      const generation = generationKey(scope, key);
      const savedBefore = saveGenerations.get(generation) ?? 0;
      // `previewMap` shows the active identity, so the saved body is only
      // readable while the read's identity is still the active one.
      const savedSince = () =>
        cacheScope.get() === scope && (saveGenerations.get(generation) ?? 0) !== savedBefore;
      let preview: LocalFilePreview;
      try {
        preview = await fetchLocalFilePreview(key);
      } catch (error) {
        // A save landed while this read was in flight: its failure (e.g. the
        // 404 of a file the save then recreated) describes the pre-save file,
        // so it must neither surface nor evict the confirmed saved body.
        const saved = get().previewMap[key];
        if (savedSince() && saved) return saved;
        throw error;
      }
      const current = get().previewMap[key];
      if (!savedSince() || !current) return preview;
      // A save landed while this read was in flight: keep the saved body, but
      // take the rest of the fresh read β€” notably the `resourceBaseUrl` an HTML
      // preview needs for its relative assets (never persisted, so the cached
      // copy has none).
      return preview.type === 'text' && current.type === 'text'
        ? { ...preview, content: current.content }
        : current;
    },
    effects: {
      // The file was deleted or may no longer be read (desktop protocol 404 /
      // 403): drop the cached text β€” on screen and in IndexedDB β€” so the portal
      // reports it instead of rehydrating the obsolete body after a reload.
      // Transient failures keep it (stale-while-revalidate).
      onError: (error, _key, { key, scope }) => {
        if (key && isTerminalLocalFileError(error)) previews.remove(key, scope);
      },
    },
    get,
    set,
    stateKey: 'previewReplica',
    toPersisted: toPersistedLocalFilePreview,
    view: recordLens<LocalFilePreviewStore, LocalFilePreview>('previewMap'),
  });

  return {
    ...initialLocalFilePreviewState,

    beginSave: (target) => {
      const key = localFilePreviewKey(target);
      const scope = cacheScope.get();
      return (content) => {
        // The write settled after an account / workspace switch: `update` writes
        // the active scope, which would plant this file body in another identity.
        if (cacheScope.get() !== scope) return;
        const generation = generationKey(scope, key);
        saveGenerations.set(generation, (saveGenerations.get(generation) ?? 0) + 1);
        previews.update(key, (preview) =>
          preview?.type === 'text' ? { ...preview, content } : preview,
        );
      };
    },
    refreshPreview: (target) => previews.revalidate(localFilePreviewKey(target)),
    useFetchPreview: (target, enabled = true) => {
      const key = target ? localFilePreviewKey(target) : undefined;
      // The file's content is not something the user expects to change on a
      // tab refocus; reloads are explicit (toolbar / `…` menu).
      const sync = previews.useSync(key, { enabled, revalidateOnFocus: false });
      const hasPreview = useLocalFilePreviewStore((state) =>
        key ? state.previewMap[key] !== undefined : false,
      );
      return { ...sync, isLoading: enabled && !!key && !hasPreview && sync.error == null };
    },
  };
}, shallow);

/** The preview of one file, or `undefined` while nothing is loaded for it. */
export const useLocalFilePreview = (target: LocalFilePreviewTarget | undefined) =>
  useLocalFilePreviewStore((state) =>
    target ? state.previewMap[localFilePreviewKey(target)] : undefined,
  );