All files / src/apps/user/pages/knowledge generationStore.ts

96.42% Statements 27/28
88.88% Branches 16/18
100% Functions 5/5
96.29% Lines 26/27

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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                                11x     11x                       11x 73x   11x 73x 73x           11x           23x 23x                         11x 17x 17x           11x         33x 33x 33x   1x   32x 7x 7x       7x   33x           4x                     3x 3x    
/**
 * The knowledge generation a wizard started, kept in this tab's
 * sessionStorage so closing the modal or reloading the page comes back to
 * it: the run goes on in the background either way (svc-core keeps it,
 * proposal included), and this only remembers which run to ask about.
 *
 * Per tab, per workspace and per entry point (a repository or loose
 * sources), and only a convenience: storage may be blocked, full or
 * cleared, so every access is guarded and the wizard works without it. One
 * older than a day is not offered; one the code cannot read is dropped.
 * Modelled on `agents/setup/draftStore.ts`.
 */
 
import { KnowledgeCategoryKey, knowledgeCategory } from "./categories";
 
/** Bumped when what is stored changes shape, so an old copy is ignored. */
export const GENERATION_STORE_VERSION = 1;
 
/** How long a stored run is offered back. */
export const GENERATION_TTL_MS = 24 * 60 * 60 * 1000;
 
export interface StoredGeneration {
  generationId: string;
  description: string;
  modelId: string;
  /** The kind of knowledge it was asked for; absent on a copy from before. */
  category?: KnowledgeCategoryKey;
  /** When it was saved, ms since the epoch. */
  savedAt: number;
}
 
const keyFor = (workspaceKey: string, mode: string): string =>
  `chainr.knowledgeSetup.${mode}.${workspaceKey}`;
 
const storage = (): Storage | null => {
  try {
    return window.sessionStorage;
  } catch {
    return null;
  }
};
 
export const saveGeneration = (
  workspaceKey: string,
  mode: string,
  run: Omit<StoredGeneration, "savedAt">,
  now: number = Date.now(),
): void => {
  try {
    storage()?.setItem(
      keyFor(workspaceKey, mode),
      JSON.stringify({
        version: GENERATION_STORE_VERSION,
        ...run,
        savedAt: now,
      }),
    );
  } catch {
    // Full or blocked: the wizard carries on, only without a copy.
  }
};
 
export const clearGeneration = (workspaceKey: string, mode: string): void => {
  try {
    storage()?.removeItem(keyFor(workspaceKey, mode));
  } catch {
    // Nothing to clear where storage cannot be reached.
  }
};
 
export const loadGeneration = (
  workspaceKey: string,
  mode: string,
  now: number = Date.now(),
): StoredGeneration | null => {
  let raw: string | null = null;
  try {
    raw = storage()?.getItem(keyFor(workspaceKey, mode)) ?? null;
  } catch {
    return null;
  }
  if (!raw) return null;
  try {
    const parsed = JSON.parse(raw) as Partial<StoredGeneration> & {
      version?: number;
    };
    const fresh =
      typeof parsed.savedAt === "number" &&
      now - parsed.savedAt < GENERATION_TTL_MS;
    if (
      parsed.version === GENERATION_STORE_VERSION &&
      fresh &&
      typeof parsed.generationId === "string" &&
      parsed.generationId
    ) {
      return {
        generationId: parsed.generationId,
        description: String(parsed.description ?? ""),
        modelId: String(parsed.modelId ?? ""),
        category: knowledgeCategory(parsed.category).key,
        savedAt: parsed.savedAt as number,
      };
    }
  } catch {
    // Unreadable: dropped below.
  }
  clearGeneration(workspaceKey, mode);
  return null;
};