File size: 2,036 Bytes
7e7bccd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// The last batch run of a dataset, kept in this browser so the batch page
// survives a trip into an episode and back (the run itself cannot: leaving
// the page stops it). The staged atoms live in the per-episode local copies
// (localAtoms.ts); this holds only the report.
import type { BatchReport } from "./batchAnnotate";
import { browserStorage, type StorageLike } from "./localAtoms";

export const BATCH_STORE_PREFIX = "lerobot-batch-report:v1:";

export interface StoredBatch {
  report: BatchReport;
  /** set once this run's staged files went to the Hub */
  committedAt: string | null;
}

export function batchStoreKey(repoId: string): string {
  return `${BATCH_STORE_PREFIX}${repoId}`;
}

export function saveStoredBatch(
  repoId: string,
  stored: StoredBatch,
  storage: StorageLike | null = browserStorage(),
): boolean {
  if (!storage) return false;
  try {
    storage.setItem(batchStoreKey(repoId), JSON.stringify(stored));
    return true;
  } catch {
    return false;
  }
}

export function loadStoredBatch(
  repoId: string,
  storage: StorageLike | null = browserStorage(),
): StoredBatch | null {
  if (!storage) return null;
  try {
    const raw = storage.getItem(batchStoreKey(repoId));
    if (!raw) return null;
    const parsed = JSON.parse(raw) as Partial<StoredBatch>;
    const report = parsed.report;
    if (
      !report ||
      report.schema !== "batch-report/1" ||
      !Array.isArray(report.episodes)
    )
      return null;
    // a report from before the parallel runner: nothing owed, one lane
    if (!Array.isArray(report.requested))
      report.requested = report.episodes.map((r) => r.episode);
    if (typeof report.concurrency !== "number") report.concurrency = 1;
    return { report, committedAt: parsed.committedAt ?? null };
  } catch {
    return null;
  }
}

export function clearStoredBatch(
  repoId: string,
  storage: StorageLike | null = browserStorage(),
): void {
  try {
    storage?.removeItem(batchStoreKey(repoId));
  } catch {
    /* nothing to clear */
  }
}