@juspay/neurolink 10.10.7 → 10.10.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +37 -8
- package/dist/browser/neurolink.min.js +315 -315
- package/dist/cli/factories/commandFactory.js +8 -4
- package/dist/lib/processors/media/VideoProcessor.d.ts +13 -3
- package/dist/lib/processors/media/VideoProcessor.js +53 -12
- package/dist/lib/types/file.d.ts +37 -0
- package/dist/lib/types/generate.d.ts +4 -0
- package/dist/lib/types/stream.d.ts +4 -0
- package/dist/lib/utils/errorHandling.d.ts +21 -0
- package/dist/lib/utils/errorHandling.js +53 -0
- package/dist/lib/utils/fileDetector.js +9 -6
- package/dist/lib/utils/messageBuilder.js +111 -36
- package/dist/lib/utils/pdfProcessor.d.ts +11 -0
- package/dist/lib/utils/pdfProcessor.js +17 -0
- package/dist/processors/media/VideoProcessor.d.ts +13 -3
- package/dist/processors/media/VideoProcessor.js +53 -12
- package/dist/types/file.d.ts +37 -0
- package/dist/types/generate.d.ts +4 -0
- package/dist/types/stream.d.ts +4 -0
- package/dist/utils/errorHandling.d.ts +21 -0
- package/dist/utils/errorHandling.js +53 -0
- package/dist/utils/fileDetector.js +9 -6
- package/dist/utils/messageBuilder.js +111 -36
- package/dist/utils/pdfProcessor.d.ts +11 -0
- package/dist/utils/pdfProcessor.js +17 -0
- package/package.json +1 -1
|
@@ -166,15 +166,19 @@ export class CLICommandFactory {
|
|
|
166
166
|
type: "string",
|
|
167
167
|
description: "Add video file for analysis (can be used multiple times) (MP4, WebM, MOV, AVI, MKV)",
|
|
168
168
|
},
|
|
169
|
+
// No yargs `default:` on these two. They used to declare 8 / 85 while the
|
|
170
|
+
// processor actually picks a duration-based frame count (up to 100) and
|
|
171
|
+
// encodes at quality 80 — harmless only because the values were never read
|
|
172
|
+
// (#478). Now that they reach the encoder, a default here would silently
|
|
173
|
+
// re-cap every existing CLI video at 8 frames. Unset means "let the
|
|
174
|
+
// processor choose", which is what callers have always effectively had.
|
|
169
175
|
"video-frames": {
|
|
170
176
|
type: "number",
|
|
171
|
-
default:
|
|
172
|
-
description: "Number of frames to extract (default: 8)",
|
|
177
|
+
description: "Number of frames to extract (default: chosen from video duration, max 100)",
|
|
173
178
|
},
|
|
174
179
|
"video-quality": {
|
|
175
180
|
type: "number",
|
|
176
|
-
default:
|
|
177
|
-
description: "Frame quality 0-100 (default: 85)",
|
|
181
|
+
description: "Frame quality 1-100 (default: 80)",
|
|
178
182
|
},
|
|
179
183
|
"video-format": {
|
|
180
184
|
type: "string",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
* ```
|
|
45
45
|
*/
|
|
46
46
|
import { BaseFileProcessor } from "../base/BaseFileProcessor.js";
|
|
47
|
-
import type { FileInfo, ProcessedVideo, ProcessorFileProcessingResult, ProcessOptions } from "../../types/index.js";
|
|
47
|
+
import type { FileInfo, ProcessedVideo, ProcessorFileProcessingResult, ProcessOptions, VideoProcessorOptions } from "../../types/index.js";
|
|
48
48
|
/**
|
|
49
49
|
* Narrow a loaded `fluent-ffmpeg` export to the shape this file actually uses:
|
|
50
50
|
* a callable carrying the `ffprobe` and `setFfmpegPath` statics.
|
|
@@ -116,7 +116,7 @@ export declare class VideoProcessor extends BaseFileProcessor<ProcessedVideo> {
|
|
|
116
116
|
* @param options - Optional processing options
|
|
117
117
|
* @returns Processing result with extracted video data or error
|
|
118
118
|
*/
|
|
119
|
-
processFile(fileInfo: FileInfo, options?: ProcessOptions): Promise<ProcessorFileProcessingResult<ProcessedVideo>>;
|
|
119
|
+
processFile(fileInfo: FileInfo, options?: ProcessOptions & VideoProcessorOptions): Promise<ProcessorFileProcessingResult<ProcessedVideo>>;
|
|
120
120
|
/**
|
|
121
121
|
* Probe a video file to extract metadata using ffprobe.
|
|
122
122
|
*
|
|
@@ -137,6 +137,11 @@ export declare class VideoProcessor extends BaseFileProcessor<ProcessedVideo> {
|
|
|
137
137
|
* @returns Structured video metadata
|
|
138
138
|
*/
|
|
139
139
|
private buildMetadata;
|
|
140
|
+
/**
|
|
141
|
+
* Clamp a caller-supplied frame quality into sharp's valid 1-100 range,
|
|
142
|
+
* falling back to the default when absent or non-numeric (#478).
|
|
143
|
+
*/
|
|
144
|
+
private static resolveFrameQuality;
|
|
140
145
|
/**
|
|
141
146
|
* Extract keyframes from a video at calculated intervals.
|
|
142
147
|
*
|
|
@@ -153,10 +158,15 @@ export declare class VideoProcessor extends BaseFileProcessor<ProcessedVideo> {
|
|
|
153
158
|
* The interval is adaptive: if the tier interval would exceed MAX_FRAMES,
|
|
154
159
|
* the interval widens to duration/MAX_FRAMES for full-video coverage.
|
|
155
160
|
*
|
|
161
|
+
* A caller-supplied `options.frames` overrides the tier schedule entirely:
|
|
162
|
+
* that many frames are spread evenly across the clip, still capped at
|
|
163
|
+
* MAX_FRAMES. `options.quality` and `options.format` reach the encoder (#478).
|
|
164
|
+
*
|
|
156
165
|
* @param videoPath - Path to the video file
|
|
157
166
|
* @param tempDir - Temp directory for frame output
|
|
158
167
|
* @param durationSec - Video duration in seconds
|
|
159
|
-
* @
|
|
168
|
+
* @param options - Caller frame budget / encoder settings
|
|
169
|
+
* @returns Array of encoded frame buffers (JPEG unless png was requested)
|
|
160
170
|
*/
|
|
161
171
|
private extractKeyframes;
|
|
162
172
|
/**
|
|
@@ -313,7 +313,10 @@ export class VideoProcessor extends BaseFileProcessor {
|
|
|
313
313
|
* @param options - Optional processing options
|
|
314
314
|
* @returns Processing result with extracted video data or error
|
|
315
315
|
*/
|
|
316
|
-
async processFile(fileInfo,
|
|
316
|
+
async processFile(fileInfo,
|
|
317
|
+
// #478: widened with the keyframe knobs so `--video-frames`/`-quality`/
|
|
318
|
+
// `-format` can reach the encoder instead of being silently discarded.
|
|
319
|
+
options) {
|
|
317
320
|
const filename = this.getFilename(fileInfo);
|
|
318
321
|
const sizeBytes = fileInfo.size || fileInfo.buffer?.length || 0;
|
|
319
322
|
return withSpan({
|
|
@@ -438,7 +441,7 @@ export class VideoProcessor extends BaseFileProcessor {
|
|
|
438
441
|
// Step 5: Extract keyframes
|
|
439
442
|
let keyframes = [];
|
|
440
443
|
try {
|
|
441
|
-
keyframes = await this.extractKeyframes(tempVideoPath, tempDir, metadata.duration);
|
|
444
|
+
keyframes = await this.extractKeyframes(tempVideoPath, tempDir, metadata.duration, options);
|
|
442
445
|
}
|
|
443
446
|
catch {
|
|
444
447
|
// Non-fatal: continue without keyframes if extraction fails
|
|
@@ -642,6 +645,16 @@ export class VideoProcessor extends BaseFileProcessor {
|
|
|
642
645
|
// ===========================================================================
|
|
643
646
|
// KEYFRAME EXTRACTION
|
|
644
647
|
// ===========================================================================
|
|
648
|
+
/**
|
|
649
|
+
* Clamp a caller-supplied frame quality into sharp's valid 1-100 range,
|
|
650
|
+
* falling back to the default when absent or non-numeric (#478).
|
|
651
|
+
*/
|
|
652
|
+
static resolveFrameQuality(quality) {
|
|
653
|
+
if (typeof quality !== "number" || !Number.isFinite(quality)) {
|
|
654
|
+
return VIDEO_CONFIG.FRAME_JPEG_QUALITY;
|
|
655
|
+
}
|
|
656
|
+
return Math.min(100, Math.max(1, Math.round(quality)));
|
|
657
|
+
}
|
|
645
658
|
/**
|
|
646
659
|
* Extract keyframes from a video at calculated intervals.
|
|
647
660
|
*
|
|
@@ -658,20 +671,45 @@ export class VideoProcessor extends BaseFileProcessor {
|
|
|
658
671
|
* The interval is adaptive: if the tier interval would exceed MAX_FRAMES,
|
|
659
672
|
* the interval widens to duration/MAX_FRAMES for full-video coverage.
|
|
660
673
|
*
|
|
674
|
+
* A caller-supplied `options.frames` overrides the tier schedule entirely:
|
|
675
|
+
* that many frames are spread evenly across the clip, still capped at
|
|
676
|
+
* MAX_FRAMES. `options.quality` and `options.format` reach the encoder (#478).
|
|
677
|
+
*
|
|
661
678
|
* @param videoPath - Path to the video file
|
|
662
679
|
* @param tempDir - Temp directory for frame output
|
|
663
680
|
* @param durationSec - Video duration in seconds
|
|
664
|
-
* @
|
|
681
|
+
* @param options - Caller frame budget / encoder settings
|
|
682
|
+
* @returns Array of encoded frame buffers (JPEG unless png was requested)
|
|
665
683
|
*/
|
|
666
|
-
async extractKeyframes(videoPath, tempDir, durationSec) {
|
|
684
|
+
async extractKeyframes(videoPath, tempDir, durationSec, options) {
|
|
667
685
|
if (durationSec <= 0) {
|
|
668
686
|
return [];
|
|
669
687
|
}
|
|
670
|
-
//
|
|
671
|
-
|
|
688
|
+
// #478: honor the caller's frame budget, still bounded by MAX_FRAMES so a
|
|
689
|
+
// CLI flag can lower the cost but never raise it past the processor's own
|
|
690
|
+
// ceiling. A non-positive/non-finite request falls back to the default.
|
|
691
|
+
const requestedFrames = options?.frames;
|
|
692
|
+
const hasExplicitBudget = typeof requestedFrames === "number" &&
|
|
693
|
+
Number.isFinite(requestedFrames) &&
|
|
694
|
+
requestedFrames > 0;
|
|
695
|
+
const frameBudget = hasExplicitBudget
|
|
696
|
+
? Math.min(Math.floor(requestedFrames), VIDEO_CONFIG.MAX_FRAMES)
|
|
697
|
+
: VIDEO_CONFIG.MAX_FRAMES;
|
|
698
|
+
// Determine extraction interval based on duration. When the caller asked
|
|
699
|
+
// for a specific frame count, spread that many evenly across the whole
|
|
700
|
+
// video instead of using the duration tier — otherwise a short interval
|
|
701
|
+
// would hit the budget early and only cover the opening seconds.
|
|
702
|
+
//
|
|
703
|
+
// Keyed on whether a budget was REQUESTED, not on whether it happens to be
|
|
704
|
+
// below MAX_FRAMES: asking for exactly MAX_FRAMES is still an explicit
|
|
705
|
+
// request and must produce that many frames, not silently fall back to the
|
|
706
|
+
// tier schedule (which yields far fewer on a short clip).
|
|
707
|
+
const intervalSec = hasExplicitBudget
|
|
708
|
+
? Math.max(durationSec / frameBudget, Number.EPSILON)
|
|
709
|
+
: this.getFrameInterval(durationSec);
|
|
672
710
|
// Calculate timestamps to extract
|
|
673
711
|
const timestamps = [];
|
|
674
|
-
for (let t = 0; t < durationSec && timestamps.length <
|
|
712
|
+
for (let t = 0; t < durationSec && timestamps.length < frameBudget; t += intervalSec) {
|
|
675
713
|
timestamps.push(t);
|
|
676
714
|
}
|
|
677
715
|
if (timestamps.length === 0) {
|
|
@@ -691,13 +729,16 @@ export class VideoProcessor extends BaseFileProcessor {
|
|
|
691
729
|
const rawFrame = await fs.readFile(framePath);
|
|
692
730
|
// Resize to fit within max dimension while preserving aspect ratio
|
|
693
731
|
const sharp = (await import("sharp")).default;
|
|
694
|
-
const
|
|
695
|
-
.resize(VIDEO_CONFIG.FRAME_MAX_DIMENSION, VIDEO_CONFIG.FRAME_MAX_DIMENSION, {
|
|
732
|
+
const pipeline = sharp(rawFrame).resize(VIDEO_CONFIG.FRAME_MAX_DIMENSION, VIDEO_CONFIG.FRAME_MAX_DIMENSION, {
|
|
696
733
|
fit: "inside",
|
|
697
734
|
withoutEnlargement: true,
|
|
698
|
-
})
|
|
699
|
-
|
|
700
|
-
|
|
735
|
+
});
|
|
736
|
+
// #478: `--video-quality` / `--video-format` were accepted by the CLI
|
|
737
|
+
// and then dropped on the floor; both now reach the encoder.
|
|
738
|
+
const quality = VideoProcessor.resolveFrameQuality(options?.quality);
|
|
739
|
+
const resized = await (options?.format === "png"
|
|
740
|
+
? pipeline.png({ quality })
|
|
741
|
+
: pipeline.jpeg({ quality })).toBuffer();
|
|
701
742
|
keyframes.push(resized);
|
|
702
743
|
}
|
|
703
744
|
catch {
|
package/dist/lib/types/file.d.ts
CHANGED
|
@@ -302,6 +302,21 @@ export type AudioProcessorOptions = {
|
|
|
302
302
|
/** Maximum file size in megabytes */
|
|
303
303
|
maxSizeMB?: number;
|
|
304
304
|
};
|
|
305
|
+
/**
|
|
306
|
+
* Keyframe-extraction knobs for an attached video (#478).
|
|
307
|
+
*
|
|
308
|
+
* These back the `--video-frames` / `--video-quality` / `--video-format` CLI
|
|
309
|
+
* flags and `GenerateOptions.videoOptions`. Each is clamped to the processor's
|
|
310
|
+
* own ceiling — a caller cannot raise `frames` above VIDEO_CONFIG.MAX_FRAMES.
|
|
311
|
+
*/
|
|
312
|
+
export type VideoProcessorOptions = {
|
|
313
|
+
/** Max keyframes to extract. Clamped to the processor's MAX_FRAMES ceiling. */
|
|
314
|
+
frames?: number;
|
|
315
|
+
/** Encoder quality 1-100 for the extracted frames. */
|
|
316
|
+
quality?: number;
|
|
317
|
+
/** Frame encoding. Defaults to jpeg. */
|
|
318
|
+
format?: "jpeg" | "png";
|
|
319
|
+
};
|
|
305
320
|
/**
|
|
306
321
|
* Office processor options for Word, PowerPoint, and Excel documents
|
|
307
322
|
*
|
|
@@ -366,6 +381,7 @@ export type FileDetectorOptions = {
|
|
|
366
381
|
audioOptions?: AudioProcessorOptions;
|
|
367
382
|
csvOptions?: CSVProcessorOptions;
|
|
368
383
|
officeOptions?: OfficeProcessorOptions;
|
|
384
|
+
videoOptions?: VideoProcessorOptions;
|
|
369
385
|
confidenceThreshold?: number;
|
|
370
386
|
provider?: string;
|
|
371
387
|
/** Maximum number of retry attempts for network requests (default: 3) */
|
|
@@ -438,6 +454,27 @@ export type PDFImagePage = {
|
|
|
438
454
|
/** Populated when this page failed to render (#294). */
|
|
439
455
|
error?: string;
|
|
440
456
|
};
|
|
457
|
+
/**
|
|
458
|
+
* A single PDF queued for multimodal message building, normalised from either
|
|
459
|
+
* submission surface — `input.pdfFiles` or `input.content` with `type: "pdf"`
|
|
460
|
+
* — so both can share the aggregate page/size guard (#309).
|
|
461
|
+
*/
|
|
462
|
+
export type MultimodalPdfEntry = {
|
|
463
|
+
/** Raw PDF bytes. */
|
|
464
|
+
buffer: Buffer;
|
|
465
|
+
/** Display name; may be a full path, so log only its basename. */
|
|
466
|
+
filename: string;
|
|
467
|
+
/**
|
|
468
|
+
* Page count when known. Null/undefined on the `input.content` path whenever
|
|
469
|
+
* the caller omitted `metadata.pages`; the aggregate guard resolves those
|
|
470
|
+
* from `buffer` rather than treating them as zero.
|
|
471
|
+
*/
|
|
472
|
+
pageCount?: number | null;
|
|
473
|
+
/** Password for an encrypted PDF (#258). */
|
|
474
|
+
password?: string;
|
|
475
|
+
/** Per-page pixel ceiling for the image fallback (#260). */
|
|
476
|
+
maxCanvasPixels?: number;
|
|
477
|
+
};
|
|
441
478
|
/** Result of PDF to image conversion. */
|
|
442
479
|
export type PDFImageConversionResult = {
|
|
443
480
|
/** Array of base64-encoded PNG images (one per successfully converted page) */
|
|
@@ -135,9 +135,13 @@ export type GenerateOptions = {
|
|
|
135
135
|
maxCanvasPixels?: number;
|
|
136
136
|
};
|
|
137
137
|
videoOptions?: {
|
|
138
|
+
/** Frames to extract. Unset lets VideoProcessor pick from the clip's duration; clamped to 100. */
|
|
138
139
|
frames?: number;
|
|
140
|
+
/** Frame encoder quality, clamped to 1-100. Default 80. */
|
|
139
141
|
quality?: number;
|
|
142
|
+
/** Frame encoding. Default jpeg. */
|
|
140
143
|
format?: "jpeg" | "png";
|
|
144
|
+
/** Not implemented yet (#433) — warns rather than silently doing nothing. */
|
|
141
145
|
transcribeAudio?: boolean;
|
|
142
146
|
};
|
|
143
147
|
/**
|
|
@@ -227,9 +227,13 @@ export type StreamOptions = {
|
|
|
227
227
|
maxCanvasPixels?: number;
|
|
228
228
|
};
|
|
229
229
|
videoOptions?: {
|
|
230
|
+
/** Frames to extract. Unset lets VideoProcessor pick from the clip's duration; clamped to 100. */
|
|
230
231
|
frames?: number;
|
|
232
|
+
/** Frame encoder quality, clamped to 1-100. Default 80. */
|
|
231
233
|
quality?: number;
|
|
234
|
+
/** Frame encoding. Default jpeg. */
|
|
232
235
|
format?: "jpeg" | "png";
|
|
236
|
+
/** Not implemented yet (#433) — warns rather than silently doing nothing. */
|
|
233
237
|
transcribeAudio?: boolean;
|
|
234
238
|
};
|
|
235
239
|
/**
|
|
@@ -39,6 +39,9 @@ export declare const ERROR_CODES: {
|
|
|
39
39
|
readonly FILE_PROCESSING_FAILED: "FILE_PROCESSING_FAILED";
|
|
40
40
|
readonly CSV_PROCESSING_FAILED: "CSV_PROCESSING_FAILED";
|
|
41
41
|
readonly PDF_PAGE_LIMIT_EXCEEDED: "PDF_PAGE_LIMIT_EXCEEDED";
|
|
42
|
+
readonly PDF_AGGREGATE_PAGE_LIMIT_EXCEEDED: "PDF_AGGREGATE_PAGE_LIMIT_EXCEEDED";
|
|
43
|
+
readonly PDF_AGGREGATE_SIZE_LIMIT_EXCEEDED: "PDF_AGGREGATE_SIZE_LIMIT_EXCEEDED";
|
|
44
|
+
readonly PDF_PAGE_COUNT_UNVERIFIABLE: "PDF_PAGE_COUNT_UNVERIFIABLE";
|
|
42
45
|
readonly PDF_PASSWORD_REQUIRED: "PDF_PASSWORD_REQUIRED";
|
|
43
46
|
readonly PDF_INCORRECT_PASSWORD: "PDF_INCORRECT_PASSWORD";
|
|
44
47
|
readonly RATE_LIMITER_QUEUE_FULL: "RATE_LIMITER_QUEUE_FULL";
|
|
@@ -203,6 +206,24 @@ export declare class ErrorFactory {
|
|
|
203
206
|
* Create a PDF page limit exceeded error
|
|
204
207
|
*/
|
|
205
208
|
static pdfPageLimitExceeded(estimatedPages: number, maxPages: number, provider: string): NeuroLinkError;
|
|
209
|
+
/**
|
|
210
|
+
* The combined page count across every PDF in one request exceeds what the
|
|
211
|
+
* provider accepts (#309). Distinct from `pdfPageLimitExceeded`, which is
|
|
212
|
+
* per-file — here each document can be individually legal.
|
|
213
|
+
*/
|
|
214
|
+
static pdfAggregatePageLimitExceeded(fileCount: number, totalPages: number, maxPages: number, provider: string): NeuroLinkError;
|
|
215
|
+
/**
|
|
216
|
+
* The combined byte size across every PDF in one request exceeds what the
|
|
217
|
+
* provider accepts (#309).
|
|
218
|
+
*/
|
|
219
|
+
static pdfAggregateSizeLimitExceeded(fileCount: number, totalMB: number, maxSizeMB: number, provider: string): NeuroLinkError;
|
|
220
|
+
/**
|
|
221
|
+
* A PDF supplied through the untrusted `input.content` surface could not be
|
|
222
|
+
* parsed for a page count (#309). Caller-supplied `metadata.pages` is not
|
|
223
|
+
* authoritative there, so an unreadable document is rejected rather than
|
|
224
|
+
* admitted with an assumed count of zero.
|
|
225
|
+
*/
|
|
226
|
+
static pdfPageCountUnverifiable(filenames: string[], provider: string): NeuroLinkError;
|
|
206
227
|
/**
|
|
207
228
|
* The PDF is encrypted and no password was supplied (#258).
|
|
208
229
|
*/
|
|
@@ -52,6 +52,9 @@ export const ERROR_CODES = {
|
|
|
52
52
|
CSV_PROCESSING_FAILED: "CSV_PROCESSING_FAILED",
|
|
53
53
|
// PDF validation errors
|
|
54
54
|
PDF_PAGE_LIMIT_EXCEEDED: "PDF_PAGE_LIMIT_EXCEEDED",
|
|
55
|
+
PDF_AGGREGATE_PAGE_LIMIT_EXCEEDED: "PDF_AGGREGATE_PAGE_LIMIT_EXCEEDED",
|
|
56
|
+
PDF_AGGREGATE_SIZE_LIMIT_EXCEEDED: "PDF_AGGREGATE_SIZE_LIMIT_EXCEEDED",
|
|
57
|
+
PDF_PAGE_COUNT_UNVERIFIABLE: "PDF_PAGE_COUNT_UNVERIFIABLE",
|
|
55
58
|
PDF_PASSWORD_REQUIRED: "PDF_PASSWORD_REQUIRED",
|
|
56
59
|
PDF_INCORRECT_PASSWORD: "PDF_INCORRECT_PASSWORD",
|
|
57
60
|
// Rate limiter errors
|
|
@@ -557,6 +560,56 @@ export class ErrorFactory {
|
|
|
557
560
|
},
|
|
558
561
|
});
|
|
559
562
|
}
|
|
563
|
+
/**
|
|
564
|
+
* The combined page count across every PDF in one request exceeds what the
|
|
565
|
+
* provider accepts (#309). Distinct from `pdfPageLimitExceeded`, which is
|
|
566
|
+
* per-file — here each document can be individually legal.
|
|
567
|
+
*/
|
|
568
|
+
static pdfAggregatePageLimitExceeded(fileCount, totalPages, maxPages, provider) {
|
|
569
|
+
return new NeuroLinkError({
|
|
570
|
+
code: ERROR_CODES.PDF_AGGREGATE_PAGE_LIMIT_EXCEEDED,
|
|
571
|
+
message: `[PDF] Combined page count across ${fileCount} PDF(s) (${totalPages}) exceeds the ` +
|
|
572
|
+
`${maxPages}-page limit for ${provider}. ` +
|
|
573
|
+
`Split the request or reduce the number of PDFs.`,
|
|
574
|
+
category: ErrorCategory.VALIDATION,
|
|
575
|
+
severity: ErrorSeverity.MEDIUM,
|
|
576
|
+
retriable: false,
|
|
577
|
+
context: { fileCount, totalPages, maxPages, provider },
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* The combined byte size across every PDF in one request exceeds what the
|
|
582
|
+
* provider accepts (#309).
|
|
583
|
+
*/
|
|
584
|
+
static pdfAggregateSizeLimitExceeded(fileCount, totalMB, maxSizeMB, provider) {
|
|
585
|
+
return new NeuroLinkError({
|
|
586
|
+
code: ERROR_CODES.PDF_AGGREGATE_SIZE_LIMIT_EXCEEDED,
|
|
587
|
+
message: `[PDF] Combined size across ${fileCount} PDF(s) (${totalMB.toFixed(2)}MB) exceeds the ` +
|
|
588
|
+
`${maxSizeMB}MB limit for ${provider}.`,
|
|
589
|
+
category: ErrorCategory.VALIDATION,
|
|
590
|
+
severity: ErrorSeverity.MEDIUM,
|
|
591
|
+
retriable: false,
|
|
592
|
+
context: { fileCount, totalMB, maxSizeMB, provider },
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* A PDF supplied through the untrusted `input.content` surface could not be
|
|
597
|
+
* parsed for a page count (#309). Caller-supplied `metadata.pages` is not
|
|
598
|
+
* authoritative there, so an unreadable document is rejected rather than
|
|
599
|
+
* admitted with an assumed count of zero.
|
|
600
|
+
*/
|
|
601
|
+
static pdfPageCountUnverifiable(filenames, provider) {
|
|
602
|
+
return new NeuroLinkError({
|
|
603
|
+
code: ERROR_CODES.PDF_PAGE_COUNT_UNVERIFIABLE,
|
|
604
|
+
message: `[PDF] Cannot verify the page count for ${filenames.length} PDF(s) supplied via ` +
|
|
605
|
+
`input.content (${filenames.join(", ")}). Provide readable PDFs, or submit them ` +
|
|
606
|
+
`via input.pdfFiles where page counts are derived during detection.`,
|
|
607
|
+
category: ErrorCategory.VALIDATION,
|
|
608
|
+
severity: ErrorSeverity.MEDIUM,
|
|
609
|
+
retriable: false,
|
|
610
|
+
context: { filenames, provider },
|
|
611
|
+
});
|
|
612
|
+
}
|
|
560
613
|
/**
|
|
561
614
|
* The PDF is encrypted and no password was supplied (#258).
|
|
562
615
|
*/
|
|
@@ -344,13 +344,13 @@ export class FileDetector {
|
|
|
344
344
|
logger.warn(`[FileDetector] All fallback parsing failed for type "${detection.type}". ` +
|
|
345
345
|
`Attempted: ${options.allowedTypes.join(", ")}. Falling through to universal handler.`);
|
|
346
346
|
const csvOptions = options?.csvOptions;
|
|
347
|
-
const result = await FileDetector.processFile(content, detection, csvOptions, options?.provider);
|
|
347
|
+
const result = await FileDetector.processFile(content, detection, csvOptions, options?.provider, options?.videoOptions);
|
|
348
348
|
FileDetector.setFileResultSpanAttributes(span, result, inputFilename, detection.type);
|
|
349
349
|
return result;
|
|
350
350
|
}
|
|
351
351
|
const content = await FileDetector.loadContent(input, detection, options);
|
|
352
352
|
const csvOptions = options?.csvOptions;
|
|
353
|
-
const result = await FileDetector.processFile(content, detection, csvOptions, options?.provider);
|
|
353
|
+
const result = await FileDetector.processFile(content, detection, csvOptions, options?.provider, options?.videoOptions);
|
|
354
354
|
FileDetector.setFileResultSpanAttributes(span, result, inputFilename, detection.type);
|
|
355
355
|
return result;
|
|
356
356
|
});
|
|
@@ -967,7 +967,7 @@ export class FileDetector {
|
|
|
967
967
|
/**
|
|
968
968
|
* Route to appropriate processor
|
|
969
969
|
*/
|
|
970
|
-
static async processFile(content, detection, options, provider) {
|
|
970
|
+
static async processFile(content, detection, options, provider, videoOptions) {
|
|
971
971
|
switch (detection.type) {
|
|
972
972
|
case "csv":
|
|
973
973
|
// Pass original extension through to CSV processor; if detection has none,
|
|
@@ -985,7 +985,7 @@ export class FileDetector {
|
|
|
985
985
|
// AI providers don't support SVG as image format, so we extract text content
|
|
986
986
|
return await FileDetector.processSvgAsText(content, detection);
|
|
987
987
|
case "video":
|
|
988
|
-
return await FileDetector.processVideoFile(content, detection);
|
|
988
|
+
return await FileDetector.processVideoFile(content, detection, videoOptions);
|
|
989
989
|
case "audio":
|
|
990
990
|
return await FileDetector.processAudioFile(content, detection);
|
|
991
991
|
case "archive":
|
|
@@ -1031,7 +1031,7 @@ export class FileDetector {
|
|
|
1031
1031
|
/**
|
|
1032
1032
|
* Process video file: extract metadata, keyframes, and subtitles via VideoProcessor
|
|
1033
1033
|
*/
|
|
1034
|
-
static async processVideoFile(content, detection) {
|
|
1034
|
+
static async processVideoFile(content, detection, videoOptions) {
|
|
1035
1035
|
const videoFilename = detection.metadata.filename || "video";
|
|
1036
1036
|
try {
|
|
1037
1037
|
const videoResult = await (await getVideoProcessor()).processFile({
|
|
@@ -1040,7 +1040,10 @@ export class FileDetector {
|
|
|
1040
1040
|
mimetype: detection.mimeType || "video/mp4",
|
|
1041
1041
|
size: content.length,
|
|
1042
1042
|
buffer: content,
|
|
1043
|
-
}
|
|
1043
|
+
},
|
|
1044
|
+
// #478: carry the caller's keyframe budget/quality/format through to
|
|
1045
|
+
// the processor; previously these stopped at the CLI layer.
|
|
1046
|
+
videoOptions);
|
|
1044
1047
|
if (videoResult.success && videoResult.data) {
|
|
1045
1048
|
return {
|
|
1046
1049
|
type: "video",
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { existsSync, readFileSync, statSync } from "fs";
|
|
2
2
|
import { readFile as readFileAsync, stat as statAsync } from "fs/promises";
|
|
3
|
-
import { basename } from "path";
|
|
4
3
|
import { getGlobalDispatcher, interceptors, request } from "undici";
|
|
5
4
|
import { MultimodalLogger, ProviderImageAdapter, } from "../adapters/providerImageAdapter.js";
|
|
6
5
|
import { CONVERSATION_INSTRUCTIONS, STRUCTURED_OUTPUT_INSTRUCTIONS, } from "../config/conversationMemory.js";
|
|
@@ -809,6 +808,20 @@ export function mergeMediaFileAliases(input) {
|
|
|
809
808
|
input.audioFiles = undefined;
|
|
810
809
|
input.videoFiles = undefined;
|
|
811
810
|
}
|
|
811
|
+
/**
|
|
812
|
+
* #478: `transcribeAudio` (CLI `--transcribe-audio`) is accepted by the options
|
|
813
|
+
* surface but no video-audio transcription exists yet — VideoProcessor extracts
|
|
814
|
+
* keyframes and embedded subtitle tracks only, and the transcription step is
|
|
815
|
+
* still open as #433. Say so once per request rather than letting the caller
|
|
816
|
+
* believe a transcript was produced and silently omitted.
|
|
817
|
+
*/
|
|
818
|
+
function warnIfVideoTranscriptionRequested(videoOptions) {
|
|
819
|
+
if (videoOptions?.transcribeAudio) {
|
|
820
|
+
logger.warn("[NEUROLINK] Video audio transcription was requested but is not implemented yet " +
|
|
821
|
+
"(tracked as #433). Keyframes and any embedded subtitle tracks are still extracted; " +
|
|
822
|
+
"spoken audio will not be transcribed.");
|
|
823
|
+
}
|
|
824
|
+
}
|
|
812
825
|
/**
|
|
813
826
|
* Process the unified files array with auto-detection.
|
|
814
827
|
* Handles lazy file registration, full processing, and preview injection.
|
|
@@ -825,6 +838,7 @@ export async function processUnifiedFilesArray(options, maxSize, provider) {
|
|
|
825
838
|
}
|
|
826
839
|
const totalFiles = options.input.files.length;
|
|
827
840
|
const files = options.input.files;
|
|
841
|
+
warnIfVideoTranscriptionRequested(options.videoOptions);
|
|
828
842
|
return withSpan({
|
|
829
843
|
name: "neurolink.file.process_all",
|
|
830
844
|
tracer: tracers.file,
|
|
@@ -882,6 +896,15 @@ export async function processUnifiedFilesArray(options, maxSize, provider) {
|
|
|
882
896
|
"unknown",
|
|
883
897
|
],
|
|
884
898
|
csvOptions: options.csvOptions,
|
|
899
|
+
// #478: videos arrive through this unified `files` path, so this is
|
|
900
|
+
// where the CLI's frame/quality/format request has to be handed on.
|
|
901
|
+
videoOptions: options.videoOptions
|
|
902
|
+
? {
|
|
903
|
+
frames: options.videoOptions.frames,
|
|
904
|
+
quality: options.videoOptions.quality,
|
|
905
|
+
format: options.videoOptions.format,
|
|
906
|
+
}
|
|
907
|
+
: undefined,
|
|
885
908
|
provider: provider,
|
|
886
909
|
mimetypeHint: fileMimetypeHint,
|
|
887
910
|
});
|
|
@@ -1020,6 +1043,83 @@ function enforcePostProcessingBudget(options, provider, model) {
|
|
|
1020
1043
|
`budget=${contextWindow.toLocaleString()} tokens, ` +
|
|
1021
1044
|
`utilization=${contextWindow > 0 ? ((totalContentTokens / contextWindow) * 100).toFixed(1) : "N/A"}%`);
|
|
1022
1045
|
}
|
|
1046
|
+
/**
|
|
1047
|
+
* #309: enforce the provider's page/size ceilings across ALL PDFs in a request,
|
|
1048
|
+
* not just per-file. N files each just under the single-file limit can still
|
|
1049
|
+
* blow past it in aggregate (e.g. three 40-page PDFs → 120 pages for a
|
|
1050
|
+
* 100-page API).
|
|
1051
|
+
*
|
|
1052
|
+
* Shared by both PDF submission surfaces: `input.pdfFiles` (via
|
|
1053
|
+
* `processExplicitPdfFiles`) and `input.content` with `type: "pdf"` (via
|
|
1054
|
+
* `convertContentToProviderFormat`). The latter previously built its own
|
|
1055
|
+
* `pdfFiles` array and reached the provider without ever calling this guard,
|
|
1056
|
+
* so the limit was bypassable by moving the same payload to `input.content`.
|
|
1057
|
+
*/
|
|
1058
|
+
/**
|
|
1059
|
+
* Basename that strips BOTH separators regardless of host platform.
|
|
1060
|
+
*
|
|
1061
|
+
* `path.basename` only understands the host's separator, so on a POSIX server
|
|
1062
|
+
* a Windows-style filename (`C:\Users\alice\q3-merger.pdf`) comes back
|
|
1063
|
+
* completely unchanged — defeating the point of trimming it before it reaches
|
|
1064
|
+
* a log line, since caller-controlled paths can carry usernames and internal
|
|
1065
|
+
* directory structure.
|
|
1066
|
+
*/
|
|
1067
|
+
function safeBasename(filename) {
|
|
1068
|
+
const lastSeparator = Math.max(filename.lastIndexOf("/"), filename.lastIndexOf("\\"));
|
|
1069
|
+
const trimmed = lastSeparator === -1 ? filename : filename.slice(lastSeparator + 1);
|
|
1070
|
+
return trimmed || "<unnamed file>";
|
|
1071
|
+
}
|
|
1072
|
+
async function enforceAggregatePdfLimits(pdfFiles, provider, { trustSuppliedPageCounts }) {
|
|
1073
|
+
const aggregateConfig = PDFProcessor.getProviderConfig(provider);
|
|
1074
|
+
// Only an empty set is exempt. A single PDF must still be checked: on the
|
|
1075
|
+
// `input.content` path nothing else validates it (that path never goes
|
|
1076
|
+
// through FileDetector.detectAndProcess / PDFProcessor.process), so bailing
|
|
1077
|
+
// at length <= 1 let one 200-page document through untouched.
|
|
1078
|
+
if (!aggregateConfig || pdfFiles.length === 0) {
|
|
1079
|
+
return;
|
|
1080
|
+
}
|
|
1081
|
+
// Byte total is free to compute — enforce it BEFORE parsing anything, so an
|
|
1082
|
+
// oversized request is rejected without first spending parser CPU/memory on
|
|
1083
|
+
// every document in it.
|
|
1084
|
+
const totalMB = pdfFiles.reduce((sum, f) => sum + f.buffer.length, 0) / (1024 * 1024);
|
|
1085
|
+
if (totalMB > aggregateConfig.maxSizeMB) {
|
|
1086
|
+
throw ErrorFactory.pdfAggregateSizeLimitExceeded(pdfFiles.length, totalMB, aggregateConfig.maxSizeMB, provider);
|
|
1087
|
+
}
|
|
1088
|
+
// `trustSuppliedPageCounts` is the difference between the two surfaces.
|
|
1089
|
+
// On `input.pdfFiles` the count comes from FileDetector's own detection, so
|
|
1090
|
+
// it is authoritative. On `input.content` it is `metadata.pages` — plain
|
|
1091
|
+
// caller input — and trusting it lets a request declare `pages: 1` for each
|
|
1092
|
+
// of three 40-page PDFs and sail past the ceiling. There, the count is
|
|
1093
|
+
// always re-derived from the bytes and the supplied value is ignored.
|
|
1094
|
+
const pageCounts = await Promise.all(pdfFiles.map(async (f) => trustSuppliedPageCounts && typeof f.pageCount === "number"
|
|
1095
|
+
? f.pageCount
|
|
1096
|
+
: await PDFProcessor.resolvePageCount(f.buffer)));
|
|
1097
|
+
// Filenames are caller-controlled and may be full paths carrying
|
|
1098
|
+
// PII/internal directory segments — surface only the basename, stripping
|
|
1099
|
+
// both separators so a Windows path is trimmed on a POSIX host too.
|
|
1100
|
+
const unknownFileNames = pdfFiles
|
|
1101
|
+
.filter((_, i) => typeof pageCounts[i] !== "number")
|
|
1102
|
+
.map((f) => (f.filename ? safeBasename(f.filename) : "<unnamed file>"));
|
|
1103
|
+
const totalPages = pageCounts.reduce((sum, p) => sum + (typeof p === "number" ? p : 0), 0);
|
|
1104
|
+
if (unknownFileNames.length > 0) {
|
|
1105
|
+
if (!trustSuppliedPageCounts) {
|
|
1106
|
+
// Untrusted surface: an unreadable count is indistinguishable from an
|
|
1107
|
+
// evasion attempt, and counting it as zero is precisely the hole. Fail
|
|
1108
|
+
// closed rather than admit an unverifiable document.
|
|
1109
|
+
throw ErrorFactory.pdfPageCountUnverifiable(unknownFileNames, provider);
|
|
1110
|
+
}
|
|
1111
|
+
// Trusted surface: detection already vetted these, so one unreadable
|
|
1112
|
+
// count must not fail an otherwise valid request — but it must not be
|
|
1113
|
+
// silent either, since the known sum may undercount the true total.
|
|
1114
|
+
logger.warn(`[PDF] Aggregate page-limit check across ${pdfFiles.length} PDFs could only be ` +
|
|
1115
|
+
`partially verified: ${unknownFileNames.length} file(s) have an unknown ` +
|
|
1116
|
+
`page count (${unknownFileNames.join(", ")}), so the known total (${totalPages}) may ` +
|
|
1117
|
+
`undercount the true combined page count.`);
|
|
1118
|
+
}
|
|
1119
|
+
if (totalPages > aggregateConfig.maxPages) {
|
|
1120
|
+
throw ErrorFactory.pdfAggregatePageLimitExceeded(pdfFiles.length, totalPages, aggregateConfig.maxPages, provider);
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1023
1123
|
/**
|
|
1024
1124
|
* Process explicit PDF files and return structured PDF entries for multimodal processing.
|
|
1025
1125
|
*/
|
|
@@ -1059,41 +1159,10 @@ async function processExplicitPdfFiles(options, maxSize, provider) {
|
|
|
1059
1159
|
throw error;
|
|
1060
1160
|
}
|
|
1061
1161
|
}
|
|
1062
|
-
//
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
if (aggregateConfig && pdfFiles.length > 1) {
|
|
1067
|
-
// A null pageCount (accurate count unavailable — see
|
|
1068
|
-
// PDFProcessor.getAccuratePageCount) is treated as 0 in the sum below,
|
|
1069
|
-
// which can undercount the aggregate and let a combined request over
|
|
1070
|
-
// the provider's page limit slip through silently. Enforcement still
|
|
1071
|
-
// runs against the known sum — a PDF with an unknown count must not
|
|
1072
|
-
// fail the request outright — but the gap itself must not be silent.
|
|
1073
|
-
const unknownPageCountFiles = pdfFiles.filter((f) => f.pageCount === null || f.pageCount === undefined);
|
|
1074
|
-
const totalPages = pdfFiles.reduce((sum, f) => sum + (f.pageCount ?? 0), 0);
|
|
1075
|
-
const totalMB = pdfFiles.reduce((sum, f) => sum + f.buffer.length, 0) / (1024 * 1024);
|
|
1076
|
-
if (unknownPageCountFiles.length > 0) {
|
|
1077
|
-
// Filenames are caller-controlled and may be full paths carrying
|
|
1078
|
-
// PII/internal directory segments — log only the basename.
|
|
1079
|
-
const unknownFileNames = unknownPageCountFiles
|
|
1080
|
-
.map((f) => (f.filename ? basename(f.filename) : "<unnamed file>"))
|
|
1081
|
-
.join(", ");
|
|
1082
|
-
logger.warn(`[PDF] Aggregate page-limit check across ${pdfFiles.length} PDFs could only be ` +
|
|
1083
|
-
`partially verified: ${unknownPageCountFiles.length} file(s) have an unknown ` +
|
|
1084
|
-
`page count (${unknownFileNames}), so the known total (${totalPages}) may ` +
|
|
1085
|
-
`undercount the true combined page count.`);
|
|
1086
|
-
}
|
|
1087
|
-
if (totalPages > aggregateConfig.maxPages) {
|
|
1088
|
-
throw new Error(`[PDF] Combined page count across ${pdfFiles.length} PDFs (${totalPages}) exceeds the ` +
|
|
1089
|
-
`${aggregateConfig.maxPages}-page limit for ${provider}. ` +
|
|
1090
|
-
`Split the request or reduce the number of PDFs.`);
|
|
1091
|
-
}
|
|
1092
|
-
if (totalMB > aggregateConfig.maxSizeMB) {
|
|
1093
|
-
throw new Error(`[PDF] Combined size across ${pdfFiles.length} PDFs (${totalMB.toFixed(2)}MB) exceeds the ` +
|
|
1094
|
-
`${aggregateConfig.maxSizeMB}MB limit for ${provider}.`);
|
|
1095
|
-
}
|
|
1096
|
-
}
|
|
1162
|
+
// Counts here come from FileDetector's detection, so they are authoritative.
|
|
1163
|
+
await enforceAggregatePdfLimits(pdfFiles, provider, {
|
|
1164
|
+
trustSuppliedPageCounts: true,
|
|
1165
|
+
});
|
|
1097
1166
|
return pdfFiles;
|
|
1098
1167
|
}
|
|
1099
1168
|
/**
|
|
@@ -1390,6 +1459,12 @@ async function convertContentToProviderFormat(content, provider, _model, pdfOpti
|
|
|
1390
1459
|
password: pdfOptions?.password,
|
|
1391
1460
|
maxCanvasPixels: pdfOptions?.maxCanvasPixels,
|
|
1392
1461
|
}));
|
|
1462
|
+
// #309: same aggregate ceiling as `input.pdfFiles`. Without this, moving an
|
|
1463
|
+
// over-limit payload from `input.pdfFiles` to `input.content` skipped the
|
|
1464
|
+
// check entirely and the request went straight to the provider.
|
|
1465
|
+
await enforceAggregatePdfLimits(pdfFiles, provider, {
|
|
1466
|
+
trustSuppliedPageCounts: false,
|
|
1467
|
+
});
|
|
1393
1468
|
return await convertMultimodalToProviderFormat(text, images, pdfFiles, provider, _model);
|
|
1394
1469
|
}
|
|
1395
1470
|
/**
|
|
@@ -18,6 +18,17 @@ export declare class PDFProcessor {
|
|
|
18
18
|
*/
|
|
19
19
|
static supportsNativePDF(provider: string): boolean;
|
|
20
20
|
static getProviderConfig(provider: string): PDFProviderConfig | null;
|
|
21
|
+
/**
|
|
22
|
+
* Best-effort page count for a PDF whose count the caller did not supply
|
|
23
|
+
* (#309). Mirrors what `process()` derives for the `input.pdfFiles` path:
|
|
24
|
+
* the accurate pdfjs count when the document parses, otherwise the header
|
|
25
|
+
* regex estimate. Returns null when neither can determine a count.
|
|
26
|
+
*
|
|
27
|
+
* Exists so the aggregate page-limit guard can enforce against PDFs handed
|
|
28
|
+
* in via `input.content`, where `metadata.pages` is optional and routinely
|
|
29
|
+
* omitted — without it, an absent count silently sums as zero.
|
|
30
|
+
*/
|
|
31
|
+
static resolvePageCount(buffer: Buffer): Promise<number | null>;
|
|
21
32
|
private static isValidPDF;
|
|
22
33
|
private static extractBasicMetadata;
|
|
23
34
|
/**
|