@abhishek944/pi-classifier 0.3.0 → 0.4.0
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 +15 -0
- package/COMPUTER_USE.md +117 -0
- package/README.md +82 -6
- package/dist/box-tool.d.ts +26 -0
- package/dist/box-tool.js +109 -0
- package/dist/box.d.ts +32 -0
- package/dist/box.js +148 -0
- package/dist/classify.d.ts +3 -0
- package/dist/classify.js +1 -1
- package/dist/computer-use-state.d.ts +9 -0
- package/dist/computer-use-state.js +160 -0
- package/dist/computer-use-tool.d.ts +52 -0
- package/dist/computer-use-tool.js +78 -0
- package/dist/computer-use-types.d.ts +132 -0
- package/dist/computer-use-types.js +1 -0
- package/dist/computer-use.d.ts +22 -0
- package/dist/computer-use.js +223 -0
- package/dist/config.js +4 -0
- package/dist/extension.d.ts +38 -10
- package/dist/extension.js +36 -11
- package/dist/image-content.d.ts +12 -0
- package/dist/image-content.js +90 -0
- package/dist/index.d.ts +8 -1
- package/dist/index.js +2 -0
- package/dist/inputs.d.ts +9 -2
- package/dist/inputs.js +154 -130
- package/dist/providers.js +90 -87
- package/dist/types.d.ts +30 -12
- package/dist/validation.js +63 -38
- package/package.json +2 -1
- package/src/box-tool.ts +145 -0
- package/src/box.ts +253 -0
- package/src/choices.ts +2 -1
- package/src/classify.ts +4 -2
- package/src/computer-use-state.ts +145 -0
- package/src/computer-use-tool.ts +76 -0
- package/src/computer-use-types.ts +127 -0
- package/src/computer-use.ts +216 -0
- package/src/config.ts +4 -0
- package/src/extension.ts +39 -12
- package/src/image-content.ts +77 -0
- package/src/index.ts +10 -0
- package/src/inputs.ts +163 -185
- package/src/providers.ts +92 -105
- package/src/types.ts +20 -9
- package/src/validation.ts +62 -94
package/src/providers.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { imageContentParts, imageReferences, imageUrl, mapImageReferences, namedImage, questionImageReferences } from "./image-content.js";
|
|
2
3
|
import { postJson } from "./http.js";
|
|
3
4
|
import { estimateCost, tokenCount } from "./cost.js";
|
|
4
5
|
import {
|
|
@@ -6,7 +7,7 @@ import {
|
|
|
6
7
|
isRecord,
|
|
7
8
|
type Answer,
|
|
8
9
|
type ClassifierResult,
|
|
9
|
-
type
|
|
10
|
+
type QuestionContent,
|
|
10
11
|
type PreparedInput,
|
|
11
12
|
type Question,
|
|
12
13
|
type ResolvedModel,
|
|
@@ -129,8 +130,16 @@ function finish(
|
|
|
129
130
|
return result;
|
|
130
131
|
}
|
|
131
132
|
|
|
132
|
-
function
|
|
133
|
-
|
|
133
|
+
function textContent(value: QuestionContent): string {
|
|
134
|
+
if (typeof value === "string") return value;
|
|
135
|
+
if (value.some((part) => typeof part !== "string"))
|
|
136
|
+
throw new ClassifierError("capability", "This content placement does not support images on the selected route.");
|
|
137
|
+
return value.join("");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function openAiContent(value: unknown, input: PreparedInput): unknown[] {
|
|
141
|
+
return imageContentParts(value, (id) => ({ type: "input_image", image_url: imageUrl(namedImage(input.images, id)) }))
|
|
142
|
+
.map((part) => typeof part === "string" ? { type: "input_text", text: part } : part);
|
|
134
143
|
}
|
|
135
144
|
|
|
136
145
|
/* Answer parsing for map-shaped answers (Perplexity, System One, Clef) --- */
|
|
@@ -380,66 +389,42 @@ function parseTypedAnswers(
|
|
|
380
389
|
|
|
381
390
|
/* OpenAI Decisions -------------------------------------------------------- */
|
|
382
391
|
|
|
383
|
-
function predicateInstructions(
|
|
384
|
-
question: Extract<Question, { type: "bool" }>,
|
|
385
|
-
): string {
|
|
392
|
+
function predicateInstructions(question: Extract<Question, { type: "bool" }>): QuestionContent {
|
|
386
393
|
const meanings: string[] = [];
|
|
387
|
-
if (question.criteria?.true)
|
|
388
|
-
|
|
389
|
-
if (question.
|
|
390
|
-
|
|
391
|
-
return
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
id:
|
|
398
|
-
question
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
if (question.type === "choice") {
|
|
407
|
-
return {
|
|
408
|
-
type: "choice",
|
|
409
|
-
name: id,
|
|
410
|
-
instructions: question.instructions,
|
|
411
|
-
choices: Object.entries(question.criteria).map(
|
|
412
|
-
([value, description]) => ({ value, description }),
|
|
413
|
-
),
|
|
414
|
-
};
|
|
415
|
-
}
|
|
416
|
-
if (question.type === "score") {
|
|
417
|
-
return {
|
|
418
|
-
type: "score",
|
|
419
|
-
name: id,
|
|
420
|
-
instructions: question.instructions,
|
|
421
|
-
levels: question.criteria.map((label) => ({ label })),
|
|
422
|
-
};
|
|
423
|
-
}
|
|
394
|
+
if (question.criteria?.true) meanings.push("True means: " + textContent(question.criteria.true));
|
|
395
|
+
if (question.criteria?.false) meanings.push("False means: " + textContent(question.criteria.false));
|
|
396
|
+
if (meanings.length === 0) return question.instructions;
|
|
397
|
+
const suffix = "\n" + meanings.join("\n");
|
|
398
|
+
return typeof question.instructions === "string" ? question.instructions + suffix : [...question.instructions, suffix];
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function openAiQuestion(id: string, question: Question, input: PreparedInput): Record<string, unknown> {
|
|
402
|
+
const instructions = question.type === "bool" ? predicateInstructions(question) : question.instructions;
|
|
403
|
+
const wireInstructions = typeof instructions === "string" ? instructions : openAiContent(instructions, input);
|
|
404
|
+
if (question.type === "bool") return { type: "predicate", name: id, instructions: wireInstructions };
|
|
405
|
+
if (question.type === "choice") return {
|
|
406
|
+
type: "choice", name: id, instructions: wireInstructions,
|
|
407
|
+
choices: Object.entries(question.criteria).map(([value, description]) => ({ value, description: textContent(description) })),
|
|
408
|
+
};
|
|
409
|
+
if (question.type === "score") return {
|
|
410
|
+
type: "score", name: id, instructions: wireInstructions,
|
|
411
|
+
levels: question.criteria.map((label) => ({ label: textContent(label) })),
|
|
412
|
+
};
|
|
424
413
|
throw unsupportedBox();
|
|
425
414
|
}
|
|
426
415
|
|
|
427
416
|
function openAiInput(input: PreparedInput): unknown {
|
|
428
|
-
const text =
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
return
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
})),
|
|
440
|
-
],
|
|
441
|
-
},
|
|
442
|
-
];
|
|
417
|
+
const text = typeof input.state === "string" ? input.state : JSON.stringify(input.state);
|
|
418
|
+
const shared = input.images.filter((image) => image.id === undefined);
|
|
419
|
+
const references = imageReferences(input.state);
|
|
420
|
+
if (shared.length === 0 && references.length === 0) return text;
|
|
421
|
+
return [{
|
|
422
|
+
role: "user",
|
|
423
|
+
content: [
|
|
424
|
+
...(references.length ? openAiContent(input.state, input) : [{ type: "input_text", text }]),
|
|
425
|
+
...shared.map((image) => ({ type: "input_image", image_url: imageUrl(image) })),
|
|
426
|
+
],
|
|
427
|
+
}];
|
|
443
428
|
}
|
|
444
429
|
|
|
445
430
|
function openAiChoiceProbabilities(
|
|
@@ -597,7 +582,7 @@ async function classifyOpenAi(
|
|
|
597
582
|
signal?: AbortSignal,
|
|
598
583
|
): Promise<ClassifierResult> {
|
|
599
584
|
const questions = Object.entries(input.questions).map(([id, question]) =>
|
|
600
|
-
openAiQuestion(id, question),
|
|
585
|
+
openAiQuestion(id, question, input),
|
|
601
586
|
);
|
|
602
587
|
const { body, requestId } = await postJson(
|
|
603
588
|
route,
|
|
@@ -624,50 +609,41 @@ async function classifyOpenAi(
|
|
|
624
609
|
function systemOneQuestions(
|
|
625
610
|
questions: Record<string, Question>,
|
|
626
611
|
allowBox: boolean,
|
|
612
|
+
content: (value: QuestionContent) => unknown = textContent,
|
|
627
613
|
): Record<string, unknown> {
|
|
628
|
-
// Question IDs may be "__proto__", so the wire map is collected without a prototype.
|
|
629
614
|
const result = Object.create(null) as Record<string, unknown>;
|
|
630
615
|
for (const [id, question] of Object.entries(questions)) {
|
|
631
|
-
if (question.type === "
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
};
|
|
636
|
-
if (question.criteria !== undefined) wire.criteria = question.criteria;
|
|
637
|
-
result[id] = wire;
|
|
638
|
-
} else if (question.type === "choice") {
|
|
639
|
-
result[id] = {
|
|
640
|
-
type: "choice",
|
|
641
|
-
instructions: question.instructions,
|
|
642
|
-
criteria: question.criteria,
|
|
643
|
-
};
|
|
644
|
-
} else if (question.type === "score") {
|
|
645
|
-
result[id] = {
|
|
646
|
-
type: "score",
|
|
647
|
-
instructions: question.instructions,
|
|
648
|
-
criteria: question.criteria,
|
|
649
|
-
};
|
|
650
|
-
} else if (allowBox) {
|
|
651
|
-
result[id] = { type: "box", instructions: question.instructions };
|
|
652
|
-
} else {
|
|
653
|
-
throw unsupportedBox();
|
|
616
|
+
if (question.type === "box") {
|
|
617
|
+
if (!allowBox) throw unsupportedBox();
|
|
618
|
+
result[id] = { type: "box", instructions: content(question.instructions) };
|
|
619
|
+
continue;
|
|
654
620
|
}
|
|
621
|
+
const wire: Record<string, unknown> = {
|
|
622
|
+
type: question.type === "bool" ? "noul" : question.type,
|
|
623
|
+
instructions: content(question.instructions),
|
|
624
|
+
};
|
|
625
|
+
if (question.type === "score") wire.criteria = question.criteria.map(content);
|
|
626
|
+
else if (question.criteria !== undefined) wire.criteria = Object.fromEntries(
|
|
627
|
+
Object.entries(question.criteria).map(([key, value]) => [key, content(value)]),
|
|
628
|
+
);
|
|
629
|
+
result[id] = wire;
|
|
655
630
|
}
|
|
656
631
|
return result;
|
|
657
632
|
}
|
|
658
633
|
|
|
659
634
|
function perplexityState(input: PreparedInput): unknown {
|
|
660
|
-
//
|
|
661
|
-
|
|
662
|
-
const
|
|
663
|
-
|
|
664
|
-
if (
|
|
635
|
+
// Only explicit references become native image parts; other JSON stays text.
|
|
636
|
+
const text = typeof input.state === "string" ? input.state : JSON.stringify(input.state);
|
|
637
|
+
const shared = input.images.filter((image) => image.id === undefined);
|
|
638
|
+
const references = imageReferences(input.state);
|
|
639
|
+
if (shared.length + references.length > 8)
|
|
640
|
+
throw new ClassifierError("input", "Perplexity accepts at most eight image occurrences per question.");
|
|
641
|
+
if (shared.length === 0 && references.length === 0) return text;
|
|
665
642
|
return [
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
})),
|
|
643
|
+
...(references.length ? imageContentParts(input.state, (id) => ({
|
|
644
|
+
type: "image_url", image_url: { url: imageUrl(namedImage(input.images, id)) },
|
|
645
|
+
})) : [text]),
|
|
646
|
+
...shared.map((image) => ({ type: "image_url", image_url: { url: imageUrl(image) } })),
|
|
671
647
|
];
|
|
672
648
|
}
|
|
673
649
|
|
|
@@ -775,24 +751,35 @@ async function classifySystemOne(
|
|
|
775
751
|
signal?: AbortSignal,
|
|
776
752
|
): Promise<ClassifierResult> {
|
|
777
753
|
const assets = Object.create(null) as Record<string, unknown>;
|
|
778
|
-
const
|
|
754
|
+
const sharedRefs: string[] = [];
|
|
755
|
+
const namedRefs = new Map<string, string>();
|
|
756
|
+
const used = new Set([...imageReferences(input.state), ...questionImageReferences(input.questions)]);
|
|
779
757
|
for (const image of input.images) {
|
|
758
|
+
// Batching can remove a question or a criterion. Jev rejects unused assets.
|
|
759
|
+
if (image.id !== undefined && !used.has(image.id)) continue;
|
|
760
|
+
if ("url" in image) throw new ClassifierError("capability", "Jev assets require local images.");
|
|
780
761
|
const ref = randomUUID();
|
|
781
|
-
|
|
762
|
+
if (image.id === undefined) sharedRefs.push(ref);
|
|
763
|
+
else namedRefs.set(image.id, ref);
|
|
782
764
|
assets[ref] = {
|
|
783
|
-
type: "image",
|
|
784
|
-
format: image.mimeType === "image/png" ? "png" : "jpg",
|
|
765
|
+
type: "image", format: image.mimeType === "image/png" ? "png" : "jpg",
|
|
785
766
|
data_base64: image.data,
|
|
786
767
|
};
|
|
787
768
|
}
|
|
769
|
+
const reference = (id: string): string => {
|
|
770
|
+
const ref = namedRefs.get(id);
|
|
771
|
+
if (!ref) throw new ClassifierError("input", "Unknown named image reference.");
|
|
772
|
+
return ref;
|
|
773
|
+
};
|
|
774
|
+
const state = mapImageReferences(input.state, reference) as PreparedInput["state"];
|
|
788
775
|
const payload: Record<string, unknown> = {
|
|
789
776
|
model: route.model,
|
|
790
|
-
state:
|
|
791
|
-
questions: systemOneQuestions(input.questions, true),
|
|
777
|
+
state: sharedRefs.length ? withImageRefs(state, sharedRefs) : state,
|
|
778
|
+
questions: systemOneQuestions(input.questions, true, (value) => mapImageReferences(value, reference)),
|
|
792
779
|
};
|
|
793
|
-
if (
|
|
780
|
+
if (Object.keys(assets).length > 0) payload.assets = assets;
|
|
794
781
|
const experimentalHeader =
|
|
795
|
-
|
|
782
|
+
Object.keys(assets).length > 0 ||
|
|
796
783
|
Object.values(input.questions).some((question) => question.type === "box");
|
|
797
784
|
const extraHeaders = experimentalHeader
|
|
798
785
|
? { "TypeSafe-Danger-Zone-Images": "true" }
|
|
@@ -861,10 +848,10 @@ async function classifyCloudflare(
|
|
|
861
848
|
questions: systemOneQuestions(input.questions, false),
|
|
862
849
|
};
|
|
863
850
|
if (input.images.length > 0) {
|
|
864
|
-
payload.images = input.images.map((image) =>
|
|
865
|
-
|
|
866
|
-
base64: image.data
|
|
867
|
-
})
|
|
851
|
+
payload.images = input.images.map((image) => {
|
|
852
|
+
if ("url" in image) throw new ClassifierError("capability", "Clef requires local images.");
|
|
853
|
+
return { content_type: image.mimeType, base64: image.data };
|
|
854
|
+
});
|
|
868
855
|
}
|
|
869
856
|
path = "run/" + route.model;
|
|
870
857
|
} else {
|
package/src/types.ts
CHANGED
|
@@ -5,15 +5,23 @@ export type ApiStyle =
|
|
|
5
5
|
| "cloudflare-system-one"
|
|
6
6
|
| "microsoft-system-one";
|
|
7
7
|
|
|
8
|
+
/** Explicit references are resolved only from the request's named images. */
|
|
9
|
+
export type ImageReference = { $image: string };
|
|
10
|
+
export type QuestionContent = string | (string | ImageReference)[];
|
|
11
|
+
export type ImageInput =
|
|
12
|
+
| string
|
|
13
|
+
| { id: string; path: string }
|
|
14
|
+
| { id: string; url: string };
|
|
15
|
+
|
|
8
16
|
export type Question =
|
|
9
17
|
| {
|
|
10
18
|
type: "bool";
|
|
11
|
-
instructions:
|
|
12
|
-
criteria?: { true?:
|
|
19
|
+
instructions: QuestionContent;
|
|
20
|
+
criteria?: { true?: QuestionContent; false?: QuestionContent };
|
|
13
21
|
}
|
|
14
|
-
| { type: "choice"; instructions:
|
|
15
|
-
| { type: "score"; instructions:
|
|
16
|
-
| { type: "box"; instructions:
|
|
22
|
+
| { type: "choice"; instructions: QuestionContent; criteria: Record<string, QuestionContent> }
|
|
23
|
+
| { type: "score"; instructions: QuestionContent; criteria: QuestionContent[] }
|
|
24
|
+
| { type: "box"; instructions: QuestionContent };
|
|
17
25
|
|
|
18
26
|
export type Answer =
|
|
19
27
|
| { type: "bool"; probability: number }
|
|
@@ -44,8 +52,8 @@ export type Answer =
|
|
|
44
52
|
export interface ClassifyParams {
|
|
45
53
|
state: string | Record<string, unknown> | unknown[];
|
|
46
54
|
questions: Record<string, Question>;
|
|
47
|
-
/**
|
|
48
|
-
images?:
|
|
55
|
+
/** Legacy paths are shared in order. Named images require explicit {$image:id} references. */
|
|
56
|
+
images?: ImageInput[];
|
|
49
57
|
}
|
|
50
58
|
|
|
51
59
|
export type ClassificationTreeNode = {
|
|
@@ -60,7 +68,7 @@ export type ClassifyTreeParams = {
|
|
|
60
68
|
state: ClassifyParams["state"];
|
|
61
69
|
tree: string | ClassificationTree;
|
|
62
70
|
instructions?: string;
|
|
63
|
-
images?:
|
|
71
|
+
images?: ImageInput[];
|
|
64
72
|
};
|
|
65
73
|
|
|
66
74
|
export type TreeStep = {
|
|
@@ -152,6 +160,7 @@ export type ClassifierRoute = {
|
|
|
152
160
|
};
|
|
153
161
|
|
|
154
162
|
export interface ClassifierSettings {
|
|
163
|
+
computerUse?: import("./computer-use-types.js").ComputerUseSettings;
|
|
155
164
|
none_of_above_choice?: boolean;
|
|
156
165
|
remove_choice_limits?: boolean;
|
|
157
166
|
default?: ClassifierRoute;
|
|
@@ -182,13 +191,15 @@ export interface ResolvedModel {
|
|
|
182
191
|
maxChoices?: number;
|
|
183
192
|
}
|
|
184
193
|
|
|
185
|
-
export interface
|
|
194
|
+
export interface PreparedLocalImage {
|
|
186
195
|
data: string;
|
|
187
196
|
mimeType: "image/png" | "image/jpeg" | "image/webp";
|
|
188
197
|
width: number;
|
|
189
198
|
height: number;
|
|
190
199
|
}
|
|
191
200
|
|
|
201
|
+
export type PreparedImage = (PreparedLocalImage | { url: string }) & { id?: string };
|
|
202
|
+
|
|
192
203
|
export interface PreparedInput {
|
|
193
204
|
state: ClassifyParams["state"];
|
|
194
205
|
questions: Record<string, Question>;
|
package/src/validation.ts
CHANGED
|
@@ -4,133 +4,101 @@ import {
|
|
|
4
4
|
type ClassifyParams,
|
|
5
5
|
type Question,
|
|
6
6
|
} from "./types.js";
|
|
7
|
+
import { imageReferences, isImageReference, questionImageReferences } from "./image-content.js";
|
|
7
8
|
|
|
8
9
|
function invalid(message: string): never {
|
|
9
10
|
throw new ClassifierError("input", message);
|
|
10
11
|
}
|
|
12
|
+
const imageId = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
13
|
+
|
|
14
|
+
function content(value: unknown, maxLength: number, nonempty = false): boolean {
|
|
15
|
+
if (typeof value === "string") return value.length <= maxLength && (!nonempty || Boolean(value.trim()));
|
|
16
|
+
if (!Array.isArray(value) || value.length < 1 || value.length > 128) return false;
|
|
17
|
+
if (!value.every((part) => typeof part === "string" || isImageReference(part))) return false;
|
|
18
|
+
validateJson(value, 0);
|
|
19
|
+
return Buffer.byteLength(JSON.stringify(value)) <= maxLength &&
|
|
20
|
+
(!nonempty || value.some((part) => typeof part !== "string" || Boolean(part.trim())));
|
|
21
|
+
}
|
|
11
22
|
|
|
12
23
|
export function validateParams(value: unknown): ClassifyParams {
|
|
13
|
-
if (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
(key) => !["state", "questions", "images"].includes(key),
|
|
17
|
-
)
|
|
18
|
-
)
|
|
19
|
-
invalid(
|
|
20
|
-
"Expected state, questions and optional images; provider/model are selected in settings.",
|
|
21
|
-
);
|
|
22
|
-
if (
|
|
23
|
-
!(
|
|
24
|
-
typeof value.state === "string" ||
|
|
25
|
-
Array.isArray(value.state) ||
|
|
26
|
-
isRecord(value.state)
|
|
27
|
-
)
|
|
28
|
-
)
|
|
24
|
+
if (!isRecord(value) || Object.keys(value).some((key) => !["state", "questions", "images"].includes(key)))
|
|
25
|
+
invalid("Expected state, questions and optional images; provider/model are selected in settings.");
|
|
26
|
+
if (!(typeof value.state === "string" || Array.isArray(value.state) || isRecord(value.state)))
|
|
29
27
|
invalid("state must be text, a JSON object or an array.");
|
|
30
28
|
validateJson(value.state, 0);
|
|
31
|
-
if (!isRecord(value.questions))
|
|
32
|
-
invalid("questions must be a map keyed by question ID.");
|
|
29
|
+
if (!isRecord(value.questions)) invalid("questions must be a map keyed by question ID.");
|
|
33
30
|
const entries = Object.entries(value.questions);
|
|
34
|
-
if (entries.length < 1 || entries.length > 32)
|
|
35
|
-
invalid("Supply between 1 and 32 questions.");
|
|
31
|
+
if (entries.length < 1 || entries.length > 32) invalid("Supply between 1 and 32 questions.");
|
|
36
32
|
for (const [id, question] of entries) {
|
|
37
33
|
if (!/^[a-zA-Z0-9_-]{1,64}$/.test(id) || !isRecord(question))
|
|
38
|
-
invalid(
|
|
39
|
-
|
|
40
|
-
);
|
|
41
|
-
if (
|
|
42
|
-
Object.keys(question).some(
|
|
43
|
-
(key) => !["type", "instructions", "criteria"].includes(key),
|
|
44
|
-
)
|
|
45
|
-
)
|
|
34
|
+
invalid("Question IDs must be 1–64 letters, digits, underscores or hyphens.");
|
|
35
|
+
if (Object.keys(question).some((key) => !["type", "instructions", "criteria"].includes(key)))
|
|
46
36
|
invalid("Unknown question field.");
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
!question.instructions.trim() ||
|
|
50
|
-
question.instructions.length > 16_384
|
|
51
|
-
)
|
|
52
|
-
invalid(
|
|
53
|
-
"Each question needs non-empty instructions, at most 16384 characters.",
|
|
54
|
-
);
|
|
37
|
+
if (!content(question.instructions, 16_384, true))
|
|
38
|
+
invalid("Each question needs non-empty text or ordered text/image-reference instructions, at most 16384 characters/bytes.");
|
|
55
39
|
if (question.type === "bool") {
|
|
56
|
-
if (
|
|
57
|
-
question.criteria
|
|
58
|
-
(
|
|
59
|
-
Object.entries(question.criteria).some(
|
|
60
|
-
([key, entry]) =>
|
|
61
|
-
!["true", "false"].includes(key) ||
|
|
62
|
-
typeof entry !== "string" ||
|
|
63
|
-
entry.length > 4096,
|
|
64
|
-
))
|
|
65
|
-
)
|
|
66
|
-
invalid("bool criteria must contain optional true/false descriptions.");
|
|
40
|
+
if (question.criteria !== undefined && (!isRecord(question.criteria) ||
|
|
41
|
+
Object.entries(question.criteria).some(([key, entry]) => !["true", "false"].includes(key) || !content(entry, 4096))))
|
|
42
|
+
invalid("bool criteria must contain optional true/false descriptions or image content.");
|
|
67
43
|
} else if (question.type === "choice") {
|
|
68
|
-
if (!isRecord(question.criteria))
|
|
69
|
-
invalid("choice criteria must map labels to descriptions.");
|
|
44
|
+
if (!isRecord(question.criteria)) invalid("choice criteria must map labels to descriptions.");
|
|
70
45
|
const criteria = Object.entries(question.criteria);
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
criteria.some(
|
|
74
|
-
([key, entry]) =>
|
|
75
|
-
!key.trim() ||
|
|
76
|
-
key.length > 128 ||
|
|
77
|
-
typeof entry !== "string" ||
|
|
78
|
-
entry.length > 4096,
|
|
79
|
-
)
|
|
80
|
-
)
|
|
81
|
-
invalid("choice requires at least one label with a string description.");
|
|
46
|
+
if (criteria.length < 1 || criteria.some(([key, entry]) => !key.trim() || key.length > 128 || !content(entry, 4096)))
|
|
47
|
+
invalid("choice requires at least one label with a text or image-content description.");
|
|
82
48
|
} else if (question.type === "score") {
|
|
83
|
-
if (
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
question.criteria.length > 10 ||
|
|
87
|
-
question.criteria.some(
|
|
88
|
-
(entry) =>
|
|
89
|
-
typeof entry !== "string" || !entry.trim() || entry.length > 4096,
|
|
90
|
-
)
|
|
91
|
-
)
|
|
92
|
-
invalid(
|
|
93
|
-
"score criteria must contain 2–10 non-empty ordered descriptions.",
|
|
94
|
-
);
|
|
49
|
+
if (!Array.isArray(question.criteria) || question.criteria.length < 2 || question.criteria.length > 10 ||
|
|
50
|
+
question.criteria.some((entry) => !content(entry, 4096, true)))
|
|
51
|
+
invalid("score criteria must contain 2–10 non-empty ordered descriptions or image content.");
|
|
95
52
|
} else if (question.type === "box") {
|
|
96
|
-
if (question.criteria !== undefined)
|
|
97
|
-
invalid("box does not accept criteria.");
|
|
53
|
+
if (question.criteria !== undefined) invalid("box does not accept criteria.");
|
|
98
54
|
} else invalid("Question type must be bool, choice, score or box.");
|
|
99
55
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
(!Array.isArray(value.images) ||
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
56
|
+
const ids = new Set<string>();
|
|
57
|
+
if (value.images !== undefined) {
|
|
58
|
+
if (!Array.isArray(value.images) || value.images.length > 8)
|
|
59
|
+
invalid("images must contain at most eight local paths or named path/url descriptors; route-specific limits also apply.");
|
|
60
|
+
for (const image of value.images) {
|
|
61
|
+
if (typeof image === "string") {
|
|
62
|
+
if (!image.trim() || image.length > 4096) invalid("Image paths must be non-empty, at most 4096 characters.");
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (!isRecord(image) || typeof image.id !== "string" || !imageId.test(image.id) ||
|
|
66
|
+
Object.keys(image).some((key) => !["id", "path", "url"].includes(key)) ||
|
|
67
|
+
(Object.hasOwn(image, "path") === Object.hasOwn(image, "url")))
|
|
68
|
+
invalid("Named images require a unique id and exactly one path or url.");
|
|
69
|
+
const source = image.path ?? image.url;
|
|
70
|
+
if (typeof source !== "string" || !source.trim() || source.length > 4096)
|
|
71
|
+
invalid("Image path/url must be non-empty, at most 4096 characters.");
|
|
72
|
+
if (ids.has(image.id)) invalid("Named image IDs must be unique.");
|
|
73
|
+
ids.add(image.id);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const references = [...imageReferences(value.state), ...questionImageReferences(value.questions as Record<string, Question>)];
|
|
77
|
+
for (const id of references) if (!ids.has(id)) invalid("Every image reference must name an image descriptor in images.");
|
|
78
|
+
for (const id of ids) if (!references.includes(id)) invalid("Every named image must be referenced in state, instructions or criteria.");
|
|
79
|
+
if (Buffer.byteLength(JSON.stringify(value)) > 2 * 1024 * 1024)
|
|
112
80
|
invalid("Classification text and questions must be at most 2 MiB.");
|
|
113
81
|
return value as unknown as ClassifyParams;
|
|
114
82
|
}
|
|
115
83
|
|
|
116
84
|
function validateJson(value: unknown, depth: number): void {
|
|
117
|
-
if (depth > 32) invalid("
|
|
118
|
-
if (value
|
|
85
|
+
if (depth > 32) invalid("JSON content must be at most 32 levels deep.");
|
|
86
|
+
if (isImageReference(value)) {
|
|
87
|
+
if (Object.keys(value).length !== 1 || typeof value.$image !== "string" || !imageId.test(value.$image))
|
|
88
|
+
invalid("An image reference must be exactly {$image: id} with a 1–64 character image ID.");
|
|
119
89
|
return;
|
|
90
|
+
}
|
|
91
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") return;
|
|
120
92
|
if (typeof value === "number" && Number.isFinite(value)) return;
|
|
121
93
|
if (Array.isArray(value)) {
|
|
122
94
|
for (const entry of value) validateJson(entry, depth + 1);
|
|
123
95
|
return;
|
|
124
96
|
}
|
|
125
|
-
if (
|
|
126
|
-
isRecord(value) &&
|
|
127
|
-
(Object.getPrototypeOf(value) === Object.prototype ||
|
|
128
|
-
Object.getPrototypeOf(value) === null)
|
|
129
|
-
) {
|
|
97
|
+
if (isRecord(value) && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)) {
|
|
130
98
|
for (const entry of Object.values(value)) validateJson(entry, depth + 1);
|
|
131
99
|
return;
|
|
132
100
|
}
|
|
133
|
-
invalid("
|
|
101
|
+
invalid("Content must contain only finite JSON values.");
|
|
134
102
|
}
|
|
135
103
|
|
|
136
104
|
export function hasBox(questions: Record<string, Question>): boolean {
|