@realtimex/folio 0.1.11 → 0.1.12
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/api/src/services/IngestionService.ts +111 -47
- package/api/src/services/ModelCapabilityService.ts +194 -54
- package/api/src/services/PolicyEngine.ts +48 -22
- package/api/src/services/RAGService.ts +2 -2
- package/dist/api/src/services/IngestionService.js +103 -41
- package/dist/api/src/services/ModelCapabilityService.js +148 -52
- package/dist/api/src/services/PolicyEngine.js +38 -22
- package/dist/api/src/services/RAGService.js +2 -2
- package/dist/assets/{index-nxHX9No5.js → index-tVGLBfz6.js} +37 -37
- package/dist/index.html +1 -1
- package/package.json +1 -1
|
@@ -5,6 +5,7 @@ import { SDKService } from "./SDKService.js";
|
|
|
5
5
|
const logger = createLogger("ModelCapabilityService");
|
|
6
6
|
|
|
7
7
|
export type VisionCapabilityState = "supported" | "unsupported" | "unknown";
|
|
8
|
+
export type VisionCapabilityModality = "image" | "pdf";
|
|
8
9
|
type StoredVisionCapabilityState = "supported" | "unsupported" | "pending_unsupported";
|
|
9
10
|
|
|
10
11
|
interface StoredVisionCapability {
|
|
@@ -28,6 +29,7 @@ interface SettingsLike {
|
|
|
28
29
|
export interface VisionResolution {
|
|
29
30
|
provider: string;
|
|
30
31
|
model: string;
|
|
32
|
+
modality: VisionCapabilityModality;
|
|
31
33
|
state: VisionCapabilityState;
|
|
32
34
|
shouldAttempt: boolean;
|
|
33
35
|
}
|
|
@@ -53,21 +55,30 @@ export class ModelCapabilityService {
|
|
|
53
55
|
private static readonly UNSUPPORTED_CONFIRMATION_FAILURES = 2;
|
|
54
56
|
private static readonly UNSUPPORTED_SCORE_THRESHOLD = 3;
|
|
55
57
|
|
|
56
|
-
static resolveVisionSupport(
|
|
58
|
+
static resolveVisionSupport(
|
|
59
|
+
settingsRow: SettingsLike | null | undefined,
|
|
60
|
+
modality: VisionCapabilityModality = "image"
|
|
61
|
+
): VisionResolution {
|
|
57
62
|
const provider = (settingsRow?.llm_provider || SDKService.DEFAULT_LLM_PROVIDER).trim();
|
|
58
63
|
const model = (settingsRow?.llm_model || SDKService.DEFAULT_LLM_MODEL).trim();
|
|
59
|
-
const state = this.getVisionState(settingsRow?.vision_model_capabilities, provider, model);
|
|
64
|
+
const state = this.getVisionState(settingsRow?.vision_model_capabilities, provider, model, modality);
|
|
60
65
|
return {
|
|
61
66
|
provider,
|
|
62
67
|
model,
|
|
68
|
+
modality,
|
|
63
69
|
state,
|
|
64
70
|
shouldAttempt: state !== "unsupported",
|
|
65
71
|
};
|
|
66
72
|
}
|
|
67
73
|
|
|
68
|
-
static getVisionState(
|
|
74
|
+
static getVisionState(
|
|
75
|
+
rawMap: unknown,
|
|
76
|
+
provider: string,
|
|
77
|
+
model: string,
|
|
78
|
+
modality: VisionCapabilityModality = "image"
|
|
79
|
+
): VisionCapabilityState {
|
|
69
80
|
const map = this.normalizeCapabilityMap(rawMap);
|
|
70
|
-
const entry = map[this.capabilityKey(provider, model)];
|
|
81
|
+
const entry = map[this.capabilityKey(provider, model, modality)];
|
|
71
82
|
if (!entry || this.isExpired(entry)) return "unknown";
|
|
72
83
|
if (entry.state === "pending_unsupported") return "unknown";
|
|
73
84
|
return entry.state;
|
|
@@ -78,9 +89,11 @@ export class ModelCapabilityService {
|
|
|
78
89
|
userId: string;
|
|
79
90
|
provider: string;
|
|
80
91
|
model: string;
|
|
92
|
+
modality?: VisionCapabilityModality;
|
|
81
93
|
}): Promise<void> {
|
|
82
94
|
await this.writeCapability({
|
|
83
95
|
...opts,
|
|
96
|
+
modality: opts.modality ?? "image",
|
|
84
97
|
state: "supported",
|
|
85
98
|
reason: "vision_request_succeeded",
|
|
86
99
|
ttlMs: this.SUPPORTED_TTL_MS,
|
|
@@ -93,18 +106,24 @@ export class ModelCapabilityService {
|
|
|
93
106
|
provider: string;
|
|
94
107
|
model: string;
|
|
95
108
|
error: unknown;
|
|
109
|
+
modality?: VisionCapabilityModality;
|
|
96
110
|
}): Promise<VisionCapabilityState> {
|
|
111
|
+
const modality = opts.modality ?? "image";
|
|
97
112
|
const classification = this.classifyVisionFailure({
|
|
98
113
|
error: opts.error,
|
|
99
114
|
provider: opts.provider,
|
|
115
|
+
modality,
|
|
100
116
|
});
|
|
101
117
|
|
|
102
118
|
if (!classification.isCapabilityError) {
|
|
103
|
-
logger.info(
|
|
119
|
+
logger.info(
|
|
120
|
+
`Vision failure for ${opts.provider}/${opts.model} (${modality}) treated as non-capability; leaving capability unknown`,
|
|
121
|
+
{
|
|
104
122
|
reason: classification.reason,
|
|
105
123
|
score: classification.score,
|
|
106
124
|
evidence: classification.evidence,
|
|
107
|
-
|
|
125
|
+
}
|
|
126
|
+
);
|
|
108
127
|
return "unknown";
|
|
109
128
|
}
|
|
110
129
|
|
|
@@ -113,7 +132,7 @@ export class ModelCapabilityService {
|
|
|
113
132
|
return "unknown";
|
|
114
133
|
}
|
|
115
134
|
|
|
116
|
-
const key = this.capabilityKey(opts.provider, opts.model);
|
|
135
|
+
const key = this.capabilityKey(opts.provider, opts.model, modality);
|
|
117
136
|
const now = new Date();
|
|
118
137
|
const failureCount = this.nextFailureCount(map[key], now.getTime());
|
|
119
138
|
|
|
@@ -123,6 +142,7 @@ export class ModelCapabilityService {
|
|
|
123
142
|
userId: opts.userId,
|
|
124
143
|
provider: opts.provider,
|
|
125
144
|
model: opts.model,
|
|
145
|
+
modality,
|
|
126
146
|
state: "pending_unsupported",
|
|
127
147
|
reason: "capability_signal_pending_confirmation",
|
|
128
148
|
ttlMs: this.PENDING_UNSUPPORTED_TTL_MS,
|
|
@@ -139,6 +159,7 @@ export class ModelCapabilityService {
|
|
|
139
159
|
userId: opts.userId,
|
|
140
160
|
provider: opts.provider,
|
|
141
161
|
model: opts.model,
|
|
162
|
+
modality,
|
|
142
163
|
state: "unsupported",
|
|
143
164
|
reason: classification.reason,
|
|
144
165
|
ttlMs: this.UNSUPPORTED_TTL_MS,
|
|
@@ -190,6 +211,7 @@ export class ModelCapabilityService {
|
|
|
190
211
|
userId: string;
|
|
191
212
|
provider: string;
|
|
192
213
|
model: string;
|
|
214
|
+
modality: VisionCapabilityModality;
|
|
193
215
|
state: StoredVisionCapabilityState;
|
|
194
216
|
reason: string;
|
|
195
217
|
ttlMs: number;
|
|
@@ -203,6 +225,7 @@ export class ModelCapabilityService {
|
|
|
203
225
|
userId,
|
|
204
226
|
provider,
|
|
205
227
|
model,
|
|
228
|
+
modality,
|
|
206
229
|
state,
|
|
207
230
|
reason,
|
|
208
231
|
ttlMs,
|
|
@@ -218,7 +241,7 @@ export class ModelCapabilityService {
|
|
|
218
241
|
}
|
|
219
242
|
|
|
220
243
|
const now = new Date();
|
|
221
|
-
const key = this.capabilityKey(provider, model);
|
|
244
|
+
const key = this.capabilityKey(provider, model, modality);
|
|
222
245
|
|
|
223
246
|
const nextEntry: StoredVisionCapability = {
|
|
224
247
|
state,
|
|
@@ -246,7 +269,7 @@ export class ModelCapabilityService {
|
|
|
246
269
|
return;
|
|
247
270
|
}
|
|
248
271
|
|
|
249
|
-
logger.info(`Updated model capability for ${provider}/${model}: ${state}`, {
|
|
272
|
+
logger.info(`Updated model capability for ${provider}/${model} (${modality}): ${state}`, {
|
|
250
273
|
reason,
|
|
251
274
|
ttlMs,
|
|
252
275
|
failureCount,
|
|
@@ -308,10 +331,16 @@ export class ModelCapabilityService {
|
|
|
308
331
|
return normalized;
|
|
309
332
|
}
|
|
310
333
|
|
|
311
|
-
private static
|
|
334
|
+
private static capabilityBaseKey(provider: string, model: string): string {
|
|
312
335
|
return `${provider.toLowerCase().trim()}:${model.toLowerCase().trim()}`;
|
|
313
336
|
}
|
|
314
337
|
|
|
338
|
+
private static capabilityKey(provider: string, model: string, modality: VisionCapabilityModality = "image"): string {
|
|
339
|
+
const base = this.capabilityBaseKey(provider, model);
|
|
340
|
+
if (modality === "image") return base;
|
|
341
|
+
return `${base}:${modality}`;
|
|
342
|
+
}
|
|
343
|
+
|
|
315
344
|
private static isExpired(entry: StoredVisionCapability): boolean {
|
|
316
345
|
if (!entry.expires_at) return false;
|
|
317
346
|
const expiryTs = Date.parse(entry.expires_at);
|
|
@@ -339,7 +368,11 @@ export class ModelCapabilityService {
|
|
|
339
368
|
return currentCount + 1;
|
|
340
369
|
}
|
|
341
370
|
|
|
342
|
-
private static classifyVisionFailure(opts: {
|
|
371
|
+
private static classifyVisionFailure(opts: {
|
|
372
|
+
error: unknown;
|
|
373
|
+
provider: string;
|
|
374
|
+
modality: VisionCapabilityModality;
|
|
375
|
+
}): VisionFailureClassification {
|
|
343
376
|
const signal = this.extractVisionFailureSignal(opts.error);
|
|
344
377
|
if (!signal.message && signal.codes.size === 0 && signal.statusCodes.size === 0) {
|
|
345
378
|
return { isCapabilityError: false, reason: "empty_error", score: 0, evidence: [] };
|
|
@@ -355,7 +388,7 @@ export class ModelCapabilityService {
|
|
|
355
388
|
};
|
|
356
389
|
}
|
|
357
390
|
|
|
358
|
-
const documentEvidence = this.matchDocumentSpecific(signal);
|
|
391
|
+
const documentEvidence = this.matchDocumentSpecific(signal, opts.modality);
|
|
359
392
|
if (documentEvidence.length > 0) {
|
|
360
393
|
return {
|
|
361
394
|
isCapabilityError: false,
|
|
@@ -365,7 +398,7 @@ export class ModelCapabilityService {
|
|
|
365
398
|
};
|
|
366
399
|
}
|
|
367
400
|
|
|
368
|
-
const capability = this.scoreCapabilitySignal(signal, opts.provider);
|
|
401
|
+
const capability = this.scoreCapabilitySignal(signal, opts.provider, opts.modality);
|
|
369
402
|
if (capability.score >= this.UNSUPPORTED_SCORE_THRESHOLD) {
|
|
370
403
|
return {
|
|
371
404
|
isCapabilityError: true,
|
|
@@ -516,8 +549,8 @@ export class ModelCapabilityService {
|
|
|
516
549
|
];
|
|
517
550
|
}
|
|
518
551
|
|
|
519
|
-
private static matchDocumentSpecific(signal: VisionFailureSignal): string[] {
|
|
520
|
-
const
|
|
552
|
+
private static matchDocumentSpecific(signal: VisionFailureSignal, modality: VisionCapabilityModality): string[] {
|
|
553
|
+
const imageCodeHints = [
|
|
521
554
|
"image_too_large",
|
|
522
555
|
"invalid_base64",
|
|
523
556
|
"invalid_image",
|
|
@@ -525,9 +558,8 @@ export class ModelCapabilityService {
|
|
|
525
558
|
"malformed_image",
|
|
526
559
|
"invalid_image_url",
|
|
527
560
|
"image_decode_failed",
|
|
528
|
-
]
|
|
529
|
-
|
|
530
|
-
const messageMatches = this.matchMessage(signal.message, [
|
|
561
|
+
];
|
|
562
|
+
const imageMessageHints = [
|
|
531
563
|
"image too large",
|
|
532
564
|
"invalid base64",
|
|
533
565
|
"malformed image",
|
|
@@ -535,7 +567,37 @@ export class ModelCapabilityService {
|
|
|
535
567
|
"unable to decode image",
|
|
536
568
|
"failed to decode image",
|
|
537
569
|
"invalid image url",
|
|
538
|
-
]
|
|
570
|
+
];
|
|
571
|
+
const pdfCodeHints = [
|
|
572
|
+
"invalid_pdf",
|
|
573
|
+
"malformed_pdf",
|
|
574
|
+
"corrupt_pdf",
|
|
575
|
+
"encrypted_pdf",
|
|
576
|
+
"password_protected_pdf",
|
|
577
|
+
"pdf_parse_error",
|
|
578
|
+
"file_too_large",
|
|
579
|
+
];
|
|
580
|
+
const pdfMessageHints = [
|
|
581
|
+
"invalid pdf",
|
|
582
|
+
"malformed pdf",
|
|
583
|
+
"corrupt pdf",
|
|
584
|
+
"encrypted pdf",
|
|
585
|
+
"password protected pdf",
|
|
586
|
+
"failed to parse pdf",
|
|
587
|
+
"unable to parse pdf",
|
|
588
|
+
"pdf is corrupted",
|
|
589
|
+
"pdf too large",
|
|
590
|
+
"file too large",
|
|
591
|
+
];
|
|
592
|
+
|
|
593
|
+
const codeMatches = this.matchCodes(
|
|
594
|
+
signal.codes,
|
|
595
|
+
modality === "pdf" ? pdfCodeHints : imageCodeHints
|
|
596
|
+
);
|
|
597
|
+
const messageMatches = this.matchMessage(
|
|
598
|
+
signal.message,
|
|
599
|
+
modality === "pdf" ? pdfMessageHints : imageMessageHints
|
|
600
|
+
);
|
|
539
601
|
|
|
540
602
|
const statusMatches = Array.from(signal.statusCodes).filter((status) => {
|
|
541
603
|
if (status === 413) return true;
|
|
@@ -552,60 +614,109 @@ export class ModelCapabilityService {
|
|
|
552
614
|
];
|
|
553
615
|
}
|
|
554
616
|
|
|
555
|
-
private static scoreCapabilitySignal(
|
|
617
|
+
private static scoreCapabilitySignal(
|
|
618
|
+
signal: VisionFailureSignal,
|
|
619
|
+
provider: string,
|
|
620
|
+
modality: VisionCapabilityModality
|
|
621
|
+
): { score: number; evidence: string[] } {
|
|
556
622
|
const evidence: string[] = [];
|
|
557
623
|
let score = 0;
|
|
558
624
|
|
|
559
|
-
const explicitCapabilityCodes = this.matchCodes(
|
|
560
|
-
|
|
561
|
-
"
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
625
|
+
const explicitCapabilityCodes = this.matchCodes(
|
|
626
|
+
signal.codes,
|
|
627
|
+
modality === "pdf"
|
|
628
|
+
? [
|
|
629
|
+
"pdf_not_supported",
|
|
630
|
+
"unsupported_pdf_input",
|
|
631
|
+
"unsupported_document_input",
|
|
632
|
+
"unsupported_file_input",
|
|
633
|
+
"input_file_not_supported",
|
|
634
|
+
"unsupported_file_type",
|
|
635
|
+
"model_not_document_capable",
|
|
636
|
+
]
|
|
637
|
+
: [
|
|
638
|
+
"vision_not_supported",
|
|
639
|
+
"unsupported_vision",
|
|
640
|
+
"model_not_vision_capable",
|
|
641
|
+
"image_not_supported",
|
|
642
|
+
"unsupported_message_content",
|
|
643
|
+
"unsupported_content_type_for_model",
|
|
644
|
+
"unsupported_image_input",
|
|
645
|
+
"invalid_model_for_vision",
|
|
646
|
+
]
|
|
647
|
+
);
|
|
569
648
|
|
|
570
649
|
if (explicitCapabilityCodes.length > 0) {
|
|
571
650
|
score += 3;
|
|
572
651
|
evidence.push(...explicitCapabilityCodes.map((match) => `code:${match}`));
|
|
573
652
|
}
|
|
574
653
|
|
|
575
|
-
const highPrecisionMessageMatches = this.matchMessage(
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
654
|
+
const highPrecisionMessageMatches = this.matchMessage(
|
|
655
|
+
signal.message,
|
|
656
|
+
modality === "pdf"
|
|
657
|
+
? [
|
|
658
|
+
"this model does not support pdf",
|
|
659
|
+
"model does not support pdf",
|
|
660
|
+
"pdf is not supported for this model",
|
|
661
|
+
"file input is not supported for this model",
|
|
662
|
+
"input_file is not supported",
|
|
663
|
+
"unsupported file type: application/pdf",
|
|
664
|
+
"application/pdf is not supported for this model",
|
|
665
|
+
]
|
|
666
|
+
: [
|
|
667
|
+
"does not support images",
|
|
668
|
+
"does not support image inputs",
|
|
669
|
+
"model does not support image",
|
|
670
|
+
"this model cannot process images",
|
|
671
|
+
"text-only model",
|
|
672
|
+
"images are not supported for this model",
|
|
673
|
+
"vision is not supported for this model",
|
|
674
|
+
"vision is not supported",
|
|
675
|
+
"vision not supported",
|
|
676
|
+
"image_url is only supported by certain models",
|
|
677
|
+
]
|
|
678
|
+
);
|
|
587
679
|
|
|
588
680
|
if (highPrecisionMessageMatches.length > 0) {
|
|
589
681
|
score += 3;
|
|
590
682
|
evidence.push(...highPrecisionMessageMatches.map((match) => `msg:${match}`));
|
|
591
683
|
}
|
|
592
684
|
|
|
593
|
-
const providerSpecificMatches = this.matchMessage(
|
|
685
|
+
const providerSpecificMatches = this.matchMessage(
|
|
686
|
+
signal.message,
|
|
687
|
+
this.providerCapabilityHints(provider, modality)
|
|
688
|
+
);
|
|
594
689
|
if (providerSpecificMatches.length > 0) {
|
|
595
690
|
score += 2;
|
|
596
691
|
evidence.push(...providerSpecificMatches.map((match) => `provider:${match}`));
|
|
597
692
|
}
|
|
598
693
|
|
|
599
|
-
const weakCapabilityHints = this.matchMessage(
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
694
|
+
const weakCapabilityHints = this.matchMessage(
|
|
695
|
+
signal.message,
|
|
696
|
+
modality === "pdf"
|
|
697
|
+
? [
|
|
698
|
+
"pdf input",
|
|
699
|
+
"pdf support",
|
|
700
|
+
"pdf not supported",
|
|
701
|
+
"application/pdf",
|
|
702
|
+
"input_file",
|
|
703
|
+
"file input",
|
|
704
|
+
"document input",
|
|
705
|
+
"unsupported file type",
|
|
706
|
+
"unsupported content type",
|
|
707
|
+
"invalid content type",
|
|
708
|
+
]
|
|
709
|
+
: [
|
|
710
|
+
"vision",
|
|
711
|
+
"unsupported content type",
|
|
712
|
+
"unsupported message content",
|
|
713
|
+
"invalid content type",
|
|
714
|
+
"unrecognized content type",
|
|
715
|
+
"image_url",
|
|
716
|
+
"multimodal",
|
|
717
|
+
"multi-modal",
|
|
718
|
+
]
|
|
719
|
+
);
|
|
609
720
|
|
|
610
721
|
const hasClientValidationStatus = Array.from(signal.statusCodes).some((status) => [400, 415, 422].includes(status));
|
|
611
722
|
if (weakCapabilityHints.length > 0 && hasClientValidationStatus) {
|
|
@@ -624,9 +735,38 @@ export class ModelCapabilityService {
|
|
|
624
735
|
};
|
|
625
736
|
}
|
|
626
737
|
|
|
627
|
-
private static providerCapabilityHints(provider: string): string[] {
|
|
738
|
+
private static providerCapabilityHints(provider: string, modality: VisionCapabilityModality): string[] {
|
|
628
739
|
const normalized = provider.toLowerCase().trim();
|
|
629
740
|
|
|
741
|
+
if (modality === "pdf") {
|
|
742
|
+
if (normalized.includes("openai")) {
|
|
743
|
+
return [
|
|
744
|
+
"input_file is not supported",
|
|
745
|
+
"unsupported file type: application/pdf",
|
|
746
|
+
"application/pdf is not supported for this model",
|
|
747
|
+
];
|
|
748
|
+
}
|
|
749
|
+
if (normalized.includes("anthropic")) {
|
|
750
|
+
return [
|
|
751
|
+
"pdf is not supported for this model",
|
|
752
|
+
"file input is not supported for this model",
|
|
753
|
+
];
|
|
754
|
+
}
|
|
755
|
+
if (normalized.includes("google") || normalized.includes("gemini")) {
|
|
756
|
+
return [
|
|
757
|
+
"unsupported document input",
|
|
758
|
+
"pdf input is not supported",
|
|
759
|
+
];
|
|
760
|
+
}
|
|
761
|
+
if (normalized.includes("realtimex")) {
|
|
762
|
+
return [
|
|
763
|
+
"unsupported file input",
|
|
764
|
+
"invalid model",
|
|
765
|
+
];
|
|
766
|
+
}
|
|
767
|
+
return [];
|
|
768
|
+
}
|
|
769
|
+
|
|
630
770
|
if (normalized.includes("openai")) {
|
|
631
771
|
return [
|
|
632
772
|
"image_url is only supported by certain models",
|
|
@@ -49,21 +49,42 @@ type ProcessWithPoliciesOptions = {
|
|
|
49
49
|
allowLearnedFallback?: boolean;
|
|
50
50
|
};
|
|
51
51
|
|
|
52
|
+
type VlmPayload = {
|
|
53
|
+
kind: "image" | "pdf";
|
|
54
|
+
dataUrl: string;
|
|
55
|
+
supplementalText: string;
|
|
56
|
+
};
|
|
57
|
+
|
|
52
58
|
/**
|
|
53
|
-
* Helper to build LLM message content. If the text contains
|
|
54
|
-
* generated by IngestionService, it casts the payload to
|
|
55
|
-
* Vision array structure so the underlying SDK bridge can transmit the image.
|
|
59
|
+
* Helper to build LLM message content. If the text contains a VLM marker
|
|
60
|
+
* generated by IngestionService, it casts the payload to multimodal blocks.
|
|
56
61
|
*/
|
|
57
|
-
function extractVlmPayload(text: string):
|
|
58
|
-
const
|
|
59
|
-
if (
|
|
62
|
+
function extractVlmPayload(text: string): VlmPayload | null {
|
|
63
|
+
const imageMarker = text.match(/\[VLM_IMAGE_DATA:(data:[^;]+;base64,[^\]]+)\]/);
|
|
64
|
+
if (imageMarker) {
|
|
65
|
+
const markerText = imageMarker[0];
|
|
66
|
+
return {
|
|
67
|
+
kind: "image",
|
|
68
|
+
dataUrl: imageMarker[1],
|
|
69
|
+
supplementalText: text.replace(markerText, "").trim().slice(0, 4000),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
60
72
|
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
73
|
+
const pdfMarker = text.match(/\[VLM_PDF_DATA:(data:[^;]+;base64,[^\]]+)\]/);
|
|
74
|
+
if (pdfMarker) {
|
|
75
|
+
const markerText = pdfMarker[0];
|
|
76
|
+
return {
|
|
77
|
+
kind: "pdf",
|
|
78
|
+
dataUrl: pdfMarker[1],
|
|
79
|
+
supplementalText: text.replace(markerText, "").trim().slice(0, 4000),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function hasVlmPayload(text: string): boolean {
|
|
87
|
+
return text.includes("[VLM_IMAGE_DATA:") || text.includes("[VLM_PDF_DATA:");
|
|
67
88
|
}
|
|
68
89
|
|
|
69
90
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -73,10 +94,13 @@ function buildMessageContent(prompt: string, text: string, textFirst = false): a
|
|
|
73
94
|
const textPrompt = vlmPayload.supplementalText
|
|
74
95
|
? `${prompt}\n\nSupplemental extracted fields:\n${vlmPayload.supplementalText}`
|
|
75
96
|
: prompt;
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
97
|
+
// `input_file` is not provider-agnostic (e.g. Anthropic-style block); providers
|
|
98
|
+
// that don't accept it will fail, and IngestionService will learn unsupported pdf modality.
|
|
99
|
+
const assetBlock = vlmPayload.kind === "pdf"
|
|
100
|
+
? { type: "input_file", file_url: vlmPayload.dataUrl }
|
|
101
|
+
: { type: "image_url", image_url: { url: vlmPayload.dataUrl } };
|
|
102
|
+
|
|
103
|
+
return [{ type: "text", text: textPrompt }, assetBlock];
|
|
80
104
|
}
|
|
81
105
|
// Standard text payload
|
|
82
106
|
return textFirst
|
|
@@ -444,7 +468,7 @@ async function evaluateCondition(condition: MatchCondition, doc: DocumentObject,
|
|
|
444
468
|
model,
|
|
445
469
|
condition_type: condition.type,
|
|
446
470
|
prompt_preview: prompt.slice(0, 180),
|
|
447
|
-
vision_payload: doc.text
|
|
471
|
+
vision_payload: hasVlmPayload(doc.text)
|
|
448
472
|
}
|
|
449
473
|
});
|
|
450
474
|
Actuator.logEvent(doc.ingestionId, doc.userId, "analysis", "Policy Matching", {
|
|
@@ -453,7 +477,7 @@ async function evaluateCondition(condition: MatchCondition, doc: DocumentObject,
|
|
|
453
477
|
model,
|
|
454
478
|
condition_type: condition.type,
|
|
455
479
|
prompt_preview: prompt.slice(0, 180),
|
|
456
|
-
vision_payload: doc.text
|
|
480
|
+
vision_payload: hasVlmPayload(doc.text)
|
|
457
481
|
}, doc.supabase);
|
|
458
482
|
const result = await sdk.llm.chat(
|
|
459
483
|
[
|
|
@@ -563,7 +587,7 @@ Fields to extract:
|
|
|
563
587
|
${fieldDescriptions}`;
|
|
564
588
|
|
|
565
589
|
try {
|
|
566
|
-
const isVlmPayload = doc.text
|
|
590
|
+
const isVlmPayload = hasVlmPayload(doc.text);
|
|
567
591
|
const mixedPrompt = isVlmPayload
|
|
568
592
|
? `You are a precise data extraction engine. Return only valid JSON.\n\n${prompt}`
|
|
569
593
|
: prompt;
|
|
@@ -722,7 +746,7 @@ Rules:
|
|
|
722
746
|
known_fields_count: Object.keys(contractData).length,
|
|
723
747
|
}, doc.supabase);
|
|
724
748
|
|
|
725
|
-
const isVlmPayload = doc.text
|
|
749
|
+
const isVlmPayload = hasVlmPayload(doc.text);
|
|
726
750
|
const mixedPrompt = isVlmPayload
|
|
727
751
|
? `You are a precise data extraction engine. Return only valid JSON.\n\n${prompt}`
|
|
728
752
|
: prompt;
|
|
@@ -1004,7 +1028,9 @@ export class PolicyEngine {
|
|
|
1004
1028
|
const allowLearnedFallback = opts.allowLearnedFallback !== false && !forcedPolicyId;
|
|
1005
1029
|
if (allowLearnedFallback && doc.supabase && policies.length > 0) {
|
|
1006
1030
|
try {
|
|
1007
|
-
const learningText = doc.text
|
|
1031
|
+
const learningText = doc.text
|
|
1032
|
+
.replace(/\[VLM_IMAGE_DATA:[^\]]+\]/g, "")
|
|
1033
|
+
.replace(/\[VLM_PDF_DATA:[^\]]+\]/g, "");
|
|
1008
1034
|
const learned = await PolicyLearningService.resolveLearnedCandidate({
|
|
1009
1035
|
supabase: doc.supabase,
|
|
1010
1036
|
userId: doc.userId,
|
|
@@ -1118,7 +1144,7 @@ export class PolicyEngine {
|
|
|
1118
1144
|
`No markdown, no explanation — only the JSON object.`;
|
|
1119
1145
|
|
|
1120
1146
|
const userPrompt = `Extract the following fields from the document:\n${fieldList}`;
|
|
1121
|
-
const isVlmPayload = doc.text
|
|
1147
|
+
const isVlmPayload = hasVlmPayload(doc.text);
|
|
1122
1148
|
const mixedPrompt = isVlmPayload ? `${systemPrompt}\n\n${userPrompt}` : userPrompt;
|
|
1123
1149
|
|
|
1124
1150
|
try {
|
|
@@ -135,8 +135,8 @@ export class RAGService {
|
|
|
135
135
|
supabase: SupabaseClient,
|
|
136
136
|
settings?: EmbeddingSettings
|
|
137
137
|
): Promise<void> {
|
|
138
|
-
if (
|
|
139
|
-
logger.info(`Skipping chunking and embedding for VLM base64
|
|
138
|
+
if (/^\[VLM_(IMAGE|PDF)_DATA:/.test(rawText)) {
|
|
139
|
+
logger.info(`Skipping chunking and embedding for VLM base64 multimodal data (Ingestion: ${ingestionId})`);
|
|
140
140
|
return;
|
|
141
141
|
}
|
|
142
142
|
|