forked from r.muellner/velum-raycast
fix: isolate failing project analysis batches
This commit is contained in:
66
src/lib/project-analysis.ts
Normal file
66
src/lib/project-analysis.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import type {
|
||||
EntityType,
|
||||
PlaceholderMapping,
|
||||
PseudonymizeResponse,
|
||||
} from "../types";
|
||||
import { makeTextBatches } from "./project-review";
|
||||
import type { TextBatch } from "./project-review";
|
||||
|
||||
type Analyze = (
|
||||
text: string,
|
||||
mapping: PlaceholderMapping,
|
||||
entityTypes: EntityType[],
|
||||
forceMasks: Record<string, string[]>,
|
||||
) => Promise<PseudonymizeResponse>;
|
||||
|
||||
/** Retry a failing server batch in smaller groups, without losing file boundaries. */
|
||||
export async function analyzeProjectBatch(
|
||||
batch: TextBatch,
|
||||
mapping: PlaceholderMapping,
|
||||
entityTypes: EntityType[],
|
||||
forceMasks: Record<string, string[]>,
|
||||
analyze: Analyze,
|
||||
phase: string,
|
||||
): Promise<{
|
||||
mapping: PlaceholderMapping;
|
||||
results: Array<{ batch: TextBatch; result: PseudonymizeResponse }>;
|
||||
}> {
|
||||
try {
|
||||
const result = await analyze(batch.text, mapping, entityTypes, forceMasks);
|
||||
return { mapping: result.mapping, results: [{ batch, result }] };
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
if (!/request failed: 5\d\d\b/i.test(message)) throw error;
|
||||
if (batch.segments.length < 2) {
|
||||
throw new Error(
|
||||
`${phase}: ${batch.segments[0]?.key ?? "unbekannte Datei"}: ${message}`,
|
||||
{ cause: error },
|
||||
);
|
||||
}
|
||||
const middle = Math.floor(batch.segments.length / 2);
|
||||
const halves = [
|
||||
batch.segments.slice(0, middle),
|
||||
batch.segments.slice(middle),
|
||||
];
|
||||
let current = mapping;
|
||||
const results: Array<{ batch: TextBatch; result: PseudonymizeResponse }> =
|
||||
[];
|
||||
for (const segments of halves) {
|
||||
const [part] = makeTextBatches(
|
||||
segments.map(({ key, text }) => ({ key, text })),
|
||||
Number.POSITIVE_INFINITY,
|
||||
);
|
||||
const analyzed = await analyzeProjectBatch(
|
||||
part,
|
||||
current,
|
||||
entityTypes,
|
||||
forceMasks,
|
||||
analyze,
|
||||
phase,
|
||||
);
|
||||
current = analyzed.mapping;
|
||||
results.push(...analyzed.results);
|
||||
}
|
||||
return { mapping: current, results };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user