Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148/** Structured OpenCodeReview input parsing and normalization. @module */
import { normalizeComment, } from './comment-normalize.ts';import { InputValidationError, } from './input-validation-error.ts';import { isRecord, } from './json-record.ts';import { parseJsonlInput, } from './jsonl.ts';import type { NormalizedFinding, NormalizedInput,} from './model.ts';
/** Reads optional resolved head from complete OCR result manifest. @param result - Validated top-level result record. @returns Resolved head string when available. @example ```ts readResolvedHead({ manifest: { input: { resolved_head: 'abc' } } }); // 'abc' ``` */function readResolvedHeadMetadata( result: Readonly<Record<string, unknown>>,): { readonly resolvedHead?: string; } { if ((!isRecord(result.manifest,)) || (!isRecord(result.manifest .input,)) || ((typeof result.manifest .input .resolved_head) !== 'string') || (result.manifest .input .resolved_head === '')) { return {}; } return { resolvedHead: result.manifest .input .resolved_head, };}
/** Normalizes one top-level comment array. @param comments - Untrusted OCR comment values. @returns Findings with one-based record positions. @example ```ts normalizeComments([]); // [] ``` */function normalizeComments(comments: readonly unknown[],): readonly NormalizedFinding[] { return comments.map(function normalizeEnvelopeComment( value, index, ): NormalizedFinding { return normalizeComment({ value, position: { kind: 'record', value: index + 1, }, },); },);}
/** Converts a parsed JSON document into supported result or comment envelope. @param parsed - Complete parsed JSON document. @returns Normalized adapter input. @throws {@link InputValidationError} when document is unsupported. @example ```ts parseJsonDocument([]); // comments envelope ``` */function parseJsonDocument(parsed: unknown,): NormalizedInput { if (Array.isArray(parsed,)) { return { inputKind: 'comments', findings: normalizeComments(parsed,), }; } if ((!isRecord(parsed,)) || ((typeof parsed.status) !== 'string') || (!Array.isArray(parsed.comments,))) { throw new InputValidationError('input is not a complete OCR result or comment array',); } /** Optional head provenance copied only from recognized manifest path. */ const resolvedHeadMetadata = readResolvedHeadMetadata(parsed,); return { inputKind: 'result', ...resolvedHeadMetadata, findings: normalizeComments(parsed.comments,), };}
/** Parses one supported OCR structured input envelope atomically. @param text - Complete JSON or JSONL text supplied by trusted transport boundary. @returns Normalized findings and available head provenance. @throws {@link InputValidationError} when JSON or recognized schema is invalid. @example ```ts parseStructuredInput({ text: '{"status":"complete","comments":[]}' }); ``` */export function parseStructuredInput({ text, }: { readonly text: string; },): NormalizedInput { try { /** Complete JSON document before envelope narrowing. */ const parsed: unknown = JSON.parse(text,); if (isRecord(parsed,) && ((typeof parsed.type) === 'string')) { return parseJsonlInput({ text, },); } return parseJsonDocument(parsed,); } catch (error: unknown) { if (error instanceof InputValidationError) { throw error; } return parseJsonlInput({ text, },); }}