// Group split files ("model-00001-of-00003.gguf", …) into one entry for display and selection. const SPLIT = /^(.*)-(\d{5})-of-(\d{5})(\.[^./]+)$/; export type SplitInfo = { base: string; part: number; total: number }; /** "BF16/X-BF16-00001-of-00002.gguf" → { base: "BF16/X-BF16.gguf", part: 1, total: 2 }. */ export function splitInfo(path: string): SplitInfo | null { const m = path.match(SPLIT); if (!m) return null; return { base: `${m[1]}${m[4]}`, part: Number(m[2]), total: Number(m[3]) }; } export type FileGroup = { /** Display path: the file itself, or the shared name of a split file without the part suffix. */ path: string; /** Files in this entry, in part order. */ parts: T[]; size: number; /** Number of parts the split file has in total (1 for normal files). */ totalParts: number; }; /** Merge split parts into one entry; other files stay as they are. Keeps first-seen order. */ export function groupSplitFiles(files: T[]): FileGroup[] { const groups: FileGroup[] = []; const byBase = new Map>(); for (const f of files) { const info = splitInfo(f.path); if (!info) { groups.push({ path: f.path, parts: [f], size: f.size, totalParts: 1 }); continue; } const key = `${info.base}#${info.total}`; let g = byBase.get(key); if (!g) { g = { path: info.base, parts: [], size: 0, totalParts: info.total }; byBase.set(key, g); groups.push(g); } g.parts.push(f); g.size += f.size; } for (const g of groups) { if (g.parts.length > 1) g.parts.sort((a, b) => (splitInfo(a.path)?.part ?? 0) - (splitInfo(b.path)?.part ?? 0)); } return groups; } export type JobFileEntry = { path: string; expectedBytes: number; doneBytes: number; failed: boolean; parts: number }; /** Job progress per entry: split parts summed into one line. */ export function groupJobFiles(files: { path: string; expectedBytes: number; doneBytes: number; state: string }[]): JobFileEntry[] { return groupSplitFiles(files.map((f) => ({ ...f, size: f.expectedBytes }))).map((g) => ({ path: g.path, expectedBytes: g.size, doneBytes: g.parts.reduce((s, p) => s + p.doneBytes, 0), failed: g.parts.some((p) => p.state === "failed"), parts: g.totalParts, })); }