forked from r.muellner/velum-raycast
fix: stream large project texts and scope API capabilities
Analyze UTF-8 text files as bounded batches of complete lines and reassemble only approved spans without loading entire files. Preserve line endings, BOM, Unicode offsets and source-change checks. Scope capability checks to selected file types, report missing features and HTTP failures, and retain the required Office image-policy handling. Cover large files, chunk boundaries, mappings and failure cleanup with regression tests.
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import { hashProjectFile, inspectTextFile } from "./project-text-stream";
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { constants as fsConstants, promises as fs } from "node:fs";
|
||||
import { basename, dirname, extname, join, relative, resolve, sep } from "node:path";
|
||||
import { environment } from "@raycast/api";
|
||||
import type { EntityType, MappingEntry, PlaceholderMapping } from "../types";
|
||||
import { whitelistKey } from "./project-whitelist";
|
||||
import type { DocumentImageReport, ImagePolicy } from "./image-policy";
|
||||
import type { ProjectWhitelistEntry } from "./project-whitelist";
|
||||
import { restoreOriginalCase } from "./restore-case";
|
||||
import { mappingForRestoration } from "./mapping-groups";
|
||||
@@ -14,7 +16,7 @@ export type { ProjectWhitelistEntry } from "./project-whitelist";
|
||||
export type ProjectRule = { type: "BEGRIFF" | "PERSON" | "ORG" | "ORT" | "FIRMA" | "KUNDE"; value: string };
|
||||
export type ProjectFile = { relPath: string; absPath: string; kind: "text" | "document"; size: number };
|
||||
export type SkippedProjectFile = { path: string; reason: string };
|
||||
export type RunFile = { source: string; output: string; kind: ProjectFile["kind"] };
|
||||
export type RunFile = { source: string; output: string; kind: ProjectFile["kind"]; imageReport?: DocumentImageReport };
|
||||
export type ProjectRun = {
|
||||
version: 2;
|
||||
origin?: "direct" | "imported";
|
||||
@@ -25,6 +27,8 @@ export type ProjectRun = {
|
||||
mapping: PlaceholderMapping;
|
||||
/** Categories actually used to analyze the files in this snapshot. */
|
||||
entityTypes?: EntityType[];
|
||||
/** Explicit image handling applied to documents in this snapshot. Unknown means reprocess. */
|
||||
imagePolicy?: ImagePolicy;
|
||||
/** Hash of each live project file and its immutable output in this run. */
|
||||
sourceState?: Record<string, { hash: string; output: string }>;
|
||||
/** Generic replacement mode for placeholders embedded in code identifiers. */
|
||||
@@ -231,7 +235,7 @@ export function forceMasks(rules: ProjectRule[]): Record<string, string[]> {
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function scanProjectFiles(root: string, maxSize = 20 * 1024 * 1024,
|
||||
export async function scanProjectFiles(root: string, maxSize?: number,
|
||||
validateText = true): Promise<{
|
||||
files: ProjectFile[]; skipped: SkippedProjectFile[];
|
||||
}> {
|
||||
@@ -258,15 +262,16 @@ export async function scanProjectFiles(root: string, maxSize = 20 * 1024 * 1024,
|
||||
continue;
|
||||
}
|
||||
const stat = await fs.stat(path);
|
||||
if (stat.size > maxSize) { skip(relPath, "Datei zu groß"); continue; }
|
||||
const kind = DOCUMENTS.has(extname(path).toLowerCase()) ? "document" : "text";
|
||||
const limit = maxSize ?? (kind === "document" ? 20 * 1024 * 1024 : Number.POSITIVE_INFINITY);
|
||||
if (stat.size > limit) { skip(relPath, `Datei zu groß (Grenze: ${limit / 1024 / 1024} MiB)`); continue; }
|
||||
if (kind === "document" && extname(path).toLowerCase() !== ".pdf" && !(await isOfficeArchive(path))) {
|
||||
skip(relPath, "Kein lesbares Office-ZIP-Archiv (beschädigt, verschlüsselt oder falsche Endung)");
|
||||
continue;
|
||||
}
|
||||
if (kind === "text" && validateText) {
|
||||
if (await isBinary(path)) { skip(relPath, "Binärdatei"); continue; }
|
||||
try { new TextDecoder("utf-8", { fatal: true }).decode(await fs.readFile(path)); }
|
||||
try { await inspectTextFile(path); }
|
||||
catch { skip(relPath, "Keine gültige UTF-8-Textdatei"); continue; }
|
||||
}
|
||||
files.push({ relPath, absPath: path, kind, size: stat.size });
|
||||
@@ -589,8 +594,7 @@ export async function listRuns(projectPath: string, support = environment.suppor
|
||||
/** An unchanged output from an in-place run can be carried into the next snapshot. */
|
||||
export async function matchesPreviousOutput(directory: string, relPath: string, currentHash: string): Promise<boolean> {
|
||||
try {
|
||||
const previous = await fs.readFile(resolveWithin(join(directory, "files"), relPath));
|
||||
return digest(previous) === currentHash;
|
||||
return await hashProjectFile(resolveWithin(join(directory, "files"), relPath)) === currentHash;
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
|
||||
throw error;
|
||||
@@ -728,7 +732,7 @@ export async function replaceProjectFilesInPlace(projectPath: string, runDirecto
|
||||
try {
|
||||
for (const file of files) {
|
||||
const source = resolveWithin(root, file.source);
|
||||
if (expectedHashes && digest(await fs.readFile(source)) !== expectedHashes.get(file.source)) {
|
||||
if (expectedHashes && await hashProjectFile(source) !== expectedHashes.get(file.source)) {
|
||||
throw new Error(`Projektdatei wurde vor dem Ersetzen erneut geändert: ${file.source}`);
|
||||
}
|
||||
await fs.unlink(source);
|
||||
|
||||
Reference in New Issue
Block a user