@abhishek944/pi-classifier 0.5.1 → 0.7.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 +14 -0
- package/README.md +150 -41
- package/dist/choices.js +1 -1
- package/dist/classify.d.ts +9 -1
- package/dist/classify.js +24 -10
- package/dist/config.d.ts +48 -2
- package/dist/config.js +90 -6
- package/dist/cost.js +23 -2
- package/dist/extension.js +7 -5
- package/dist/extract.d.ts +5 -1
- package/dist/extract.js +190 -3
- package/dist/index.d.ts +1 -1
- package/dist/inputs.d.ts +2 -2
- package/dist/inputs.js +47 -14
- package/dist/providers.js +62 -0
- package/dist/tool-contracts.d.ts +101 -226
- package/dist/tool-contracts.js +61 -116
- package/dist/types.d.ts +42 -8
- package/dist/validation.d.ts +8 -1
- package/dist/validation.js +158 -125
- package/package.json +3 -2
- package/src/choices.ts +1 -1
- package/src/classify.ts +44 -15
- package/src/config.ts +93 -6
- package/src/cost.ts +24 -2
- package/src/extension.ts +7 -5
- package/src/extract.ts +194 -5
- package/src/index.ts +3 -0
- package/src/inputs.ts +51 -18
- package/src/providers.ts +108 -0
- package/src/tool-contracts.ts +107 -150
- package/src/types.ts +46 -7
- package/src/validation.ts +142 -105
package/dist/validation.js
CHANGED
|
@@ -15,6 +15,128 @@ function content(value, maxLength, nonempty = false) {
|
|
|
15
15
|
return Buffer.byteLength(JSON.stringify(value)) <= maxLength &&
|
|
16
16
|
(!nonempty || value.some((part) => typeof part !== "string" || Boolean(part.trim())));
|
|
17
17
|
}
|
|
18
|
+
const CLASSIFICATION_QUESTION_TYPES = new Set(["bool", "choice", "score", "multi_choice"]);
|
|
19
|
+
const EXTRACTION_QUESTION_TYPES = new Set(["extract", "box", "locate", "text", "number", "date", "spans", "boxes", "estimate"]);
|
|
20
|
+
/** Per-type field rules; both tools validate against the same definitions. */
|
|
21
|
+
function validateQuestion(question) {
|
|
22
|
+
const allowed = question.type === "number" || question.type === "estimate"
|
|
23
|
+
? ["type", "instructions", "unit", "range", "step"]
|
|
24
|
+
: question.type === "spans" || question.type === "boxes"
|
|
25
|
+
? ["type", "instructions", "max"]
|
|
26
|
+
: question.type === "choice"
|
|
27
|
+
? ["type", "instructions", "criteria", "booleanValues"]
|
|
28
|
+
: question.type === "score"
|
|
29
|
+
? ["type", "instructions", "criteria", "descriptions"]
|
|
30
|
+
: question.type === "extract"
|
|
31
|
+
? ["type", "instructions", "criteria", "source", "threshold", "snapToWords", "debug"]
|
|
32
|
+
: ["type", "instructions", "criteria"];
|
|
33
|
+
if (Object.keys(question).some((key) => !allowed.includes(key)))
|
|
34
|
+
invalid("Unknown question field for " + String(question.type) + ".");
|
|
35
|
+
if (question.type !== "extract" && !content(question.instructions, 16_384, true))
|
|
36
|
+
invalid("Each question needs non-empty text or ordered text/image-reference instructions, at most 16384 characters/bytes.");
|
|
37
|
+
if (question.type === "extract" && question.instructions !== undefined && !content(question.instructions, 16_384))
|
|
38
|
+
invalid("Extract instructions must be text or ordered text/image-reference content, at most 16384 characters/bytes.");
|
|
39
|
+
if (question.type === "bool") {
|
|
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.");
|
|
43
|
+
}
|
|
44
|
+
else if (question.type === "choice") {
|
|
45
|
+
if (!isRecord(question.criteria))
|
|
46
|
+
invalid("choice criteria must map labels to descriptions.");
|
|
47
|
+
const criteria = Object.entries(question.criteria);
|
|
48
|
+
if (criteria.length < 1 || criteria.some(([key, entry]) => !key.trim() || key.length > 128 || !content(entry, 4096)))
|
|
49
|
+
invalid("choice requires at least one label with a text or image-content description.");
|
|
50
|
+
if (question.booleanValues !== undefined) {
|
|
51
|
+
if (typeof question.booleanValues !== "boolean")
|
|
52
|
+
invalid("choice.booleanValues must be a boolean.");
|
|
53
|
+
if (question.booleanValues && criteria.some(([key]) => key !== "true" && key !== "false"))
|
|
54
|
+
invalid("Boolean-valued choices must use the labels true and false.");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else if (question.type === "score") {
|
|
58
|
+
if (!Array.isArray(question.criteria) || question.criteria.length < 2 || question.criteria.length > 10 ||
|
|
59
|
+
question.criteria.some((entry) => !content(entry, 4096, true)))
|
|
60
|
+
invalid("score criteria must contain 2–10 non-empty ordered descriptions or image content.");
|
|
61
|
+
if (question.descriptions !== undefined &&
|
|
62
|
+
(!Array.isArray(question.descriptions) || question.descriptions.length !== question.criteria.length ||
|
|
63
|
+
question.descriptions.some((entry) => typeof entry !== "string" || entry.length > 4096)))
|
|
64
|
+
invalid("score descriptions must contain one string per criterion, at most 4096 characters each.");
|
|
65
|
+
}
|
|
66
|
+
else if (question.type === "box") {
|
|
67
|
+
if (question.criteria !== undefined)
|
|
68
|
+
invalid("box does not accept criteria.");
|
|
69
|
+
}
|
|
70
|
+
else if (question.type === "locate" || question.type === "text" || question.type === "date") {
|
|
71
|
+
if (question.criteria !== undefined)
|
|
72
|
+
invalid(question.type + " does not accept criteria.");
|
|
73
|
+
}
|
|
74
|
+
else if (question.type === "spans" || question.type === "boxes") {
|
|
75
|
+
if (question.criteria !== undefined)
|
|
76
|
+
invalid(question.type + " does not accept criteria.");
|
|
77
|
+
if (question.max !== undefined &&
|
|
78
|
+
(!Number.isSafeInteger(question.max) || question.max < 1 || question.max > 64))
|
|
79
|
+
invalid(question.type + ".max must be an integer from 1 to 64.");
|
|
80
|
+
}
|
|
81
|
+
else if (question.type === "number" || question.type === "estimate") {
|
|
82
|
+
if (question.unit !== undefined && (typeof question.unit !== "string" || question.unit.length > 128))
|
|
83
|
+
invalid("unit must be a string of at most 128 characters.");
|
|
84
|
+
if (question.step !== undefined && (typeof question.step !== "number" || !Number.isFinite(question.step) || question.step <= 0))
|
|
85
|
+
invalid("step must be a positive finite number.");
|
|
86
|
+
const range = question.range;
|
|
87
|
+
if (question.type === "estimate" && !Array.isArray(range))
|
|
88
|
+
invalid("estimate requires a [low, high] range.");
|
|
89
|
+
if (range !== undefined && (!Array.isArray(range) || range.length !== 2 ||
|
|
90
|
+
range.some((entry) => typeof entry !== "number" || !Number.isFinite(entry)) ||
|
|
91
|
+
range[0] >= range[1]))
|
|
92
|
+
invalid("range must be a finite [low, high] pair with low below high.");
|
|
93
|
+
}
|
|
94
|
+
else if (question.type === "extract") {
|
|
95
|
+
if (!isRecord(question.criteria))
|
|
96
|
+
invalid("extract criteria must map field names to descriptions.");
|
|
97
|
+
const criteria = Object.entries(question.criteria);
|
|
98
|
+
if (criteria.length < 1 || criteria.length > 26 || criteria.some(([key, entry]) => !key.trim() || key.length > 128 || !content(entry, 4096)))
|
|
99
|
+
invalid("extract requires 1–26 fields with text descriptions.");
|
|
100
|
+
if (question.source !== undefined && (typeof question.source !== "string" || !question.source.trim() || question.source.length > 4096))
|
|
101
|
+
invalid("extract.source must be a non-empty string of at most 4096 characters.");
|
|
102
|
+
if (question.threshold !== undefined && (typeof question.threshold !== "number" || !Number.isFinite(question.threshold) || question.threshold < 0 || question.threshold > 1))
|
|
103
|
+
invalid("extract.threshold must be a number between 0 and 1.");
|
|
104
|
+
if (question.snapToWords !== undefined && typeof question.snapToWords !== "boolean")
|
|
105
|
+
invalid("extract.snapToWords must be a boolean.");
|
|
106
|
+
if (question.debug !== undefined && typeof question.debug !== "boolean")
|
|
107
|
+
invalid("extract.debug must be a boolean.");
|
|
108
|
+
}
|
|
109
|
+
else if (question.type === "multi_choice") {
|
|
110
|
+
if (!isRecord(question.criteria))
|
|
111
|
+
invalid("multi_choice criteria must map labels to descriptions.");
|
|
112
|
+
const criteria = Object.entries(question.criteria);
|
|
113
|
+
if (criteria.length < 2 || criteria.some(([key, entry]) => !key.trim() || key.length > 128 || !content(entry, 4096)))
|
|
114
|
+
invalid("multi_choice requires at least two labels with text or image-content descriptions.");
|
|
115
|
+
}
|
|
116
|
+
else
|
|
117
|
+
invalid("Question type must be a supported classification or extraction question type.");
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* `classify_extract` question mode. Extraction question types only, validated against the same
|
|
121
|
+
* per-type rules the provider adapters rely on. Route capability checks stay in prepareInput.
|
|
122
|
+
*/
|
|
123
|
+
export function validateExtractionQuestions(value) {
|
|
124
|
+
if (!isRecord(value))
|
|
125
|
+
invalid("questions must be a map keyed by question ID.");
|
|
126
|
+
const entries = Object.entries(value);
|
|
127
|
+
if (entries.length < 1 || entries.length > 1000)
|
|
128
|
+
invalid("Supply between 1 and 1000 extraction questions; the selected route enforces its own lower limit.");
|
|
129
|
+
for (const [id, question] of entries) {
|
|
130
|
+
if (!/^[a-zA-Z0-9_-]{1,64}$/.test(id) || !isRecord(question))
|
|
131
|
+
invalid("Question IDs must be 1–64 letters, digits, underscores or hyphens.");
|
|
132
|
+
if (typeof question.type === "string" && CLASSIFICATION_QUESTION_TYPES.has(question.type))
|
|
133
|
+
invalid("classify_extract reads values, not classification decisions. " + question.type + " belongs in classify.");
|
|
134
|
+
validateQuestion(question);
|
|
135
|
+
}
|
|
136
|
+
if (Buffer.byteLength(JSON.stringify(value)) > 2 * 1024 * 1024)
|
|
137
|
+
invalid("Extraction questions must be at most 2 MiB.");
|
|
138
|
+
return value;
|
|
139
|
+
}
|
|
18
140
|
export function validateParams(value) {
|
|
19
141
|
if (!isRecord(value) || Object.keys(value).some((key) => !["state", "questions", "images", "media", "mode", "conversation", "imageDetail", "promptCache"].includes(key)))
|
|
20
142
|
invalid("Expected state, questions and optional images, media, mode, conversation, imageDetail or promptCache; provider/model are selected in settings.");
|
|
@@ -29,102 +151,9 @@ export function validateParams(value) {
|
|
|
29
151
|
for (const [id, question] of entries) {
|
|
30
152
|
if (!/^[a-zA-Z0-9_-]{1,64}$/.test(id) || !isRecord(question))
|
|
31
153
|
invalid("Question IDs must be 1–64 letters, digits, underscores or hyphens.");
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
? ["type", "instructions", "max"]
|
|
36
|
-
: question.type === "choice"
|
|
37
|
-
? ["type", "instructions", "criteria", "booleanValues"]
|
|
38
|
-
: question.type === "score"
|
|
39
|
-
? ["type", "instructions", "criteria", "descriptions"]
|
|
40
|
-
: question.type === "extract"
|
|
41
|
-
? ["type", "instructions", "criteria", "source", "threshold", "snapToWords", "debug"]
|
|
42
|
-
: ["type", "instructions", "criteria"];
|
|
43
|
-
if (Object.keys(question).some((key) => !allowed.includes(key)))
|
|
44
|
-
invalid("Unknown question field for " + String(question.type) + ".");
|
|
45
|
-
if (question.type !== "extract" && !content(question.instructions, 16_384, true))
|
|
46
|
-
invalid("Each question needs non-empty text or ordered text/image-reference instructions, at most 16384 characters/bytes.");
|
|
47
|
-
if (question.type === "extract" && question.instructions !== undefined && !content(question.instructions, 16_384))
|
|
48
|
-
invalid("Extract instructions must be text or ordered text/image-reference content, at most 16384 characters/bytes.");
|
|
49
|
-
if (question.type === "bool") {
|
|
50
|
-
if (question.criteria !== undefined && (!isRecord(question.criteria) ||
|
|
51
|
-
Object.entries(question.criteria).some(([key, entry]) => !["true", "false"].includes(key) || !content(entry, 4096))))
|
|
52
|
-
invalid("bool criteria must contain optional true/false descriptions or image content.");
|
|
53
|
-
}
|
|
54
|
-
else if (question.type === "choice") {
|
|
55
|
-
if (!isRecord(question.criteria))
|
|
56
|
-
invalid("choice criteria must map labels to descriptions.");
|
|
57
|
-
const criteria = Object.entries(question.criteria);
|
|
58
|
-
if (criteria.length < 1 || criteria.some(([key, entry]) => !key.trim() || key.length > 128 || !content(entry, 4096)))
|
|
59
|
-
invalid("choice requires at least one label with a text or image-content description.");
|
|
60
|
-
if (question.booleanValues !== undefined) {
|
|
61
|
-
if (typeof question.booleanValues !== "boolean")
|
|
62
|
-
invalid("choice.booleanValues must be a boolean.");
|
|
63
|
-
if (question.booleanValues && criteria.some(([key]) => key !== "true" && key !== "false"))
|
|
64
|
-
invalid("Boolean-valued choices must use the labels true and false.");
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
else if (question.type === "score") {
|
|
68
|
-
if (!Array.isArray(question.criteria) || question.criteria.length < 2 || question.criteria.length > 10 ||
|
|
69
|
-
question.criteria.some((entry) => !content(entry, 4096, true)))
|
|
70
|
-
invalid("score criteria must contain 2–10 non-empty ordered descriptions or image content.");
|
|
71
|
-
if (question.descriptions !== undefined &&
|
|
72
|
-
(!Array.isArray(question.descriptions) || question.descriptions.length !== question.criteria.length ||
|
|
73
|
-
question.descriptions.some((entry) => typeof entry !== "string" || entry.length > 4096)))
|
|
74
|
-
invalid("score descriptions must contain one string per criterion, at most 4096 characters each.");
|
|
75
|
-
}
|
|
76
|
-
else if (question.type === "box") {
|
|
77
|
-
if (question.criteria !== undefined)
|
|
78
|
-
invalid("box does not accept criteria.");
|
|
79
|
-
}
|
|
80
|
-
else if (question.type === "locate" || question.type === "text" || question.type === "date") {
|
|
81
|
-
if (question.criteria !== undefined)
|
|
82
|
-
invalid(question.type + " does not accept criteria.");
|
|
83
|
-
}
|
|
84
|
-
else if (question.type === "spans" || question.type === "boxes") {
|
|
85
|
-
if (question.criteria !== undefined)
|
|
86
|
-
invalid(question.type + " does not accept criteria.");
|
|
87
|
-
if (question.max !== undefined &&
|
|
88
|
-
(!Number.isSafeInteger(question.max) || question.max < 1 || question.max > 64))
|
|
89
|
-
invalid(question.type + ".max must be an integer from 1 to 64.");
|
|
90
|
-
}
|
|
91
|
-
else if (question.type === "number" || question.type === "estimate") {
|
|
92
|
-
if (question.unit !== undefined && (typeof question.unit !== "string" || question.unit.length > 128))
|
|
93
|
-
invalid("unit must be a string of at most 128 characters.");
|
|
94
|
-
if (question.step !== undefined && (typeof question.step !== "number" || !Number.isFinite(question.step) || question.step <= 0))
|
|
95
|
-
invalid("step must be a positive finite number.");
|
|
96
|
-
const range = question.range;
|
|
97
|
-
if (question.type === "estimate" && !Array.isArray(range))
|
|
98
|
-
invalid("estimate requires a [low, high] range.");
|
|
99
|
-
if (range !== undefined && (!Array.isArray(range) || range.length !== 2 ||
|
|
100
|
-
range.some((entry) => typeof entry !== "number" || !Number.isFinite(entry)) ||
|
|
101
|
-
range[0] >= range[1]))
|
|
102
|
-
invalid("range must be a finite [low, high] pair with low below high.");
|
|
103
|
-
}
|
|
104
|
-
else if (question.type === "extract") {
|
|
105
|
-
if (!isRecord(question.criteria))
|
|
106
|
-
invalid("extract criteria must map field names to descriptions.");
|
|
107
|
-
const criteria = Object.entries(question.criteria);
|
|
108
|
-
if (criteria.length < 1 || criteria.length > 26 || criteria.some(([key, entry]) => !key.trim() || key.length > 128 || !content(entry, 4096)))
|
|
109
|
-
invalid("extract requires 1–26 fields with text descriptions.");
|
|
110
|
-
if (question.source !== undefined && (typeof question.source !== "string" || !question.source.trim() || question.source.length > 4096))
|
|
111
|
-
invalid("extract.source must be a non-empty string of at most 4096 characters.");
|
|
112
|
-
if (question.threshold !== undefined && (typeof question.threshold !== "number" || !Number.isFinite(question.threshold) || question.threshold < 0 || question.threshold > 1))
|
|
113
|
-
invalid("extract.threshold must be a number between 0 and 1.");
|
|
114
|
-
if (question.snapToWords !== undefined && typeof question.snapToWords !== "boolean")
|
|
115
|
-
invalid("extract.snapToWords must be a boolean.");
|
|
116
|
-
if (question.debug !== undefined && typeof question.debug !== "boolean")
|
|
117
|
-
invalid("extract.debug must be a boolean.");
|
|
118
|
-
}
|
|
119
|
-
else if (question.type === "multi_choice") {
|
|
120
|
-
if (!isRecord(question.criteria))
|
|
121
|
-
invalid("multi_choice criteria must map labels to descriptions.");
|
|
122
|
-
const criteria = Object.entries(question.criteria);
|
|
123
|
-
if (criteria.length < 2 || criteria.some(([key, entry]) => !key.trim() || key.length > 128 || !content(entry, 4096)))
|
|
124
|
-
invalid("multi_choice requires at least two labels with text or image-content descriptions.");
|
|
125
|
-
}
|
|
126
|
-
else
|
|
127
|
-
invalid("Question type must be a supported typed decision or read.");
|
|
154
|
+
if (typeof question.type === "string" && EXTRACTION_QUESTION_TYPES.has(question.type))
|
|
155
|
+
invalid("classify answers classification decisions only. " + question.type + " is an extraction read; send the same question to classify_extract instead.");
|
|
156
|
+
validateQuestion(question);
|
|
128
157
|
}
|
|
129
158
|
if (value.media !== undefined) {
|
|
130
159
|
if (!Array.isArray(value.media))
|
|
@@ -165,35 +194,7 @@ export function validateParams(value) {
|
|
|
165
194
|
if (value.promptCache.breakpoints !== undefined && typeof value.promptCache.breakpoints !== "boolean")
|
|
166
195
|
invalid("promptCache.breakpoints must be a boolean.");
|
|
167
196
|
}
|
|
168
|
-
|
|
169
|
-
if (value.images !== undefined) {
|
|
170
|
-
if (!Array.isArray(value.images) || value.images.length > 128)
|
|
171
|
-
invalid("images must contain at most 128 local paths or named path/url descriptors; route-specific limits also apply.");
|
|
172
|
-
for (const image of value.images) {
|
|
173
|
-
if (typeof image === "string") {
|
|
174
|
-
if (!image.trim() || image.length > 4096)
|
|
175
|
-
invalid("Image paths must be non-empty, at most 4096 characters.");
|
|
176
|
-
continue;
|
|
177
|
-
}
|
|
178
|
-
if (!isRecord(image) || typeof image.id !== "string" || !imageId.test(image.id) ||
|
|
179
|
-
Object.keys(image).some((key) => !["id", "path", "url"].includes(key)) ||
|
|
180
|
-
(Object.hasOwn(image, "path") === Object.hasOwn(image, "url")))
|
|
181
|
-
invalid("Named images require a unique id and exactly one path or url.");
|
|
182
|
-
const source = image.path ?? image.url;
|
|
183
|
-
if (typeof source !== "string" || !source.trim() || source.length > 4096)
|
|
184
|
-
invalid("Image path/url must be non-empty, at most 4096 characters.");
|
|
185
|
-
if (ids.has(image.id))
|
|
186
|
-
invalid("Named image IDs must be unique.");
|
|
187
|
-
ids.add(image.id);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
const references = [...imageReferences(value.state), ...questionImageReferences(value.questions)];
|
|
191
|
-
for (const id of references)
|
|
192
|
-
if (!ids.has(id))
|
|
193
|
-
invalid("Every image reference must name an image descriptor in images.");
|
|
194
|
-
for (const id of ids)
|
|
195
|
-
if (!references.includes(id))
|
|
196
|
-
invalid("Every named image must be referenced in state, instructions or criteria.");
|
|
197
|
+
validateImageDescriptors(value.images, value.state, value.questions);
|
|
197
198
|
if (value.mode === "web_grounding" &&
|
|
198
199
|
Object.values(value.questions).some((question) => isRecord(question) && question.type === "locate") &&
|
|
199
200
|
typeof value.state !== "string")
|
|
@@ -226,6 +227,38 @@ function validateJson(value, depth) {
|
|
|
226
227
|
}
|
|
227
228
|
invalid("Content must contain only finite JSON values.");
|
|
228
229
|
}
|
|
230
|
+
/** Named/shared image descriptors and {$image:id} resolution, shared by both tools. */
|
|
231
|
+
export function validateImageDescriptors(images, state, questions) {
|
|
232
|
+
const ids = new Set();
|
|
233
|
+
if (images !== undefined) {
|
|
234
|
+
if (!Array.isArray(images) || images.length > 128)
|
|
235
|
+
invalid("images must contain at most 128 local paths or named path/url descriptors; route-specific limits also apply.");
|
|
236
|
+
for (const image of images) {
|
|
237
|
+
if (typeof image === "string") {
|
|
238
|
+
if (!image.trim() || image.length > 4096)
|
|
239
|
+
invalid("Image paths must be non-empty, at most 4096 characters.");
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (!isRecord(image) || typeof image.id !== "string" || !imageId.test(image.id) ||
|
|
243
|
+
Object.keys(image).some((key) => !["id", "path", "url"].includes(key)) ||
|
|
244
|
+
(Object.hasOwn(image, "path") === Object.hasOwn(image, "url")))
|
|
245
|
+
invalid("Named images require a unique id and exactly one path or url.");
|
|
246
|
+
const source = image.path ?? image.url;
|
|
247
|
+
if (typeof source !== "string" || !source.trim() || source.length > 4096)
|
|
248
|
+
invalid("Image path/url must be non-empty, at most 4096 characters.");
|
|
249
|
+
if (ids.has(image.id))
|
|
250
|
+
invalid("Named image IDs must be unique.");
|
|
251
|
+
ids.add(image.id);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const references = [...imageReferences(state), ...questionImageReferences(questions)];
|
|
255
|
+
for (const id of references)
|
|
256
|
+
if (!ids.has(id))
|
|
257
|
+
invalid("Every image reference must name an image descriptor in images.");
|
|
258
|
+
for (const id of ids)
|
|
259
|
+
if (!references.includes(id))
|
|
260
|
+
invalid("Every named image must be referenced in state, instructions or criteria.");
|
|
261
|
+
}
|
|
229
262
|
export function hasBox(questions) {
|
|
230
263
|
return Object.values(questions).some((question) => question.type === "box");
|
|
231
264
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abhishek944/pi-classifier",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Config-driven typed classification and extraction for Pi via Microsoft Foundry, OpenAI Decisions, Perplexity, Sarvam, TypeSafe Jev, Liquid, Cloudflare Clef and Clairic, Gemini, Inception, Parallel, OpenRouter, Vercel and local System One runtimes.",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "Config-driven typed classification and extraction for Pi via Microsoft Foundry, OpenAI Decisions, Perplexity, Sarvam, TypeSafe Jev, Liquid, Cloudflare Clef and Clairic, Gemini, OpenCode Go, Inception, Parallel, OpenRouter, Vercel and local System One runtimes.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
7
7
|
"pi",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"decisions",
|
|
10
10
|
"sarvam",
|
|
11
11
|
"gemini",
|
|
12
|
+
"opencode-go",
|
|
12
13
|
"inception",
|
|
13
14
|
"parallel",
|
|
14
15
|
"openrouter",
|
package/src/choices.ts
CHANGED
|
@@ -113,7 +113,7 @@ export async function executeChoices(
|
|
|
113
113
|
if (signal?.aborted)
|
|
114
114
|
throw new ClassifierError("aborted", "Classification was cancelled.");
|
|
115
115
|
const questions = Object.create(null) as Record<string, Question>;
|
|
116
|
-
// A bool/score
|
|
116
|
+
// A bool/score is evaluated once, not charged again for later choice batches.
|
|
117
117
|
if (calls === 0) {
|
|
118
118
|
for (const [id, question] of Object.entries(input.questions))
|
|
119
119
|
if (question.type !== "choice") questions[id] = question;
|
package/src/classify.ts
CHANGED
|
@@ -12,6 +12,8 @@ import {
|
|
|
12
12
|
type ClassifierRoute,
|
|
13
13
|
type ClassifierSettings,
|
|
14
14
|
type ClassifyParams,
|
|
15
|
+
type DecisionInput,
|
|
16
|
+
type PreparedInput,
|
|
15
17
|
type ResolvedModel,
|
|
16
18
|
} from "./types.js";
|
|
17
19
|
import { validateParams } from "./validation.js";
|
|
@@ -39,21 +41,18 @@ function routePair(route: ResolvedModel): ClassifierRoute {
|
|
|
39
41
|
return { provider: route.route, model: route.model };
|
|
40
42
|
}
|
|
41
43
|
|
|
42
|
-
async function
|
|
43
|
-
|
|
44
|
+
async function decideWithRoute(
|
|
45
|
+
prepared: PreparedInput,
|
|
44
46
|
route: ResolvedModel,
|
|
47
|
+
apiKey: string,
|
|
45
48
|
options: ClassifyOptions,
|
|
46
49
|
): Promise<ClassifierResult> {
|
|
47
|
-
checkCancelled(options.signal);
|
|
48
|
-
const prepared = await prepareInput(input, route, options.cwd, options.signal, options.memoryImages);
|
|
49
|
-
checkCancelled(options.signal);
|
|
50
|
-
const key = resolveApiKey(route, options.credentialSource ?? "pi-store");
|
|
51
50
|
const result = await executeChoices(
|
|
52
51
|
prepared,
|
|
53
52
|
route,
|
|
54
53
|
choicePolicy(options.settings),
|
|
55
54
|
(questions) =>
|
|
56
|
-
classifyWithProvider(route, { ...prepared, questions },
|
|
55
|
+
classifyWithProvider(route, { ...prepared, questions }, apiKey, options.signal),
|
|
57
56
|
options.signal,
|
|
58
57
|
);
|
|
59
58
|
if (Buffer.byteLength(JSON.stringify(result, null, 2)) > 40 * 1024)
|
|
@@ -64,6 +63,18 @@ async function classifyOnce(
|
|
|
64
63
|
return result;
|
|
65
64
|
}
|
|
66
65
|
|
|
66
|
+
async function classifyOnce(
|
|
67
|
+
input: ClassifyParams,
|
|
68
|
+
route: ResolvedModel,
|
|
69
|
+
options: ClassifyOptions,
|
|
70
|
+
): Promise<ClassifierResult> {
|
|
71
|
+
checkCancelled(options.signal);
|
|
72
|
+
const prepared = await prepareInput(input, route, options.cwd, options.signal, options.memoryImages);
|
|
73
|
+
checkCancelled(options.signal);
|
|
74
|
+
const key = resolveApiKey(route, options.credentialSource ?? "pi-store");
|
|
75
|
+
return decideWithRoute(prepared, route, key, options);
|
|
76
|
+
}
|
|
77
|
+
|
|
67
78
|
function permitsFallback(error: unknown): boolean {
|
|
68
79
|
return (
|
|
69
80
|
error instanceof ClassifierError &&
|
|
@@ -72,13 +83,15 @@ function permitsFallback(error: unknown): boolean {
|
|
|
72
83
|
);
|
|
73
84
|
}
|
|
74
85
|
|
|
75
|
-
/**
|
|
76
|
-
|
|
77
|
-
|
|
86
|
+
/**
|
|
87
|
+
* Shared default-then-configured-fallback orchestration for typed-question requests. Classification
|
|
88
|
+
* and typed extraction both use it so a moved question keeps the same route behavior.
|
|
89
|
+
*/
|
|
90
|
+
export async function runDecisionRequest(
|
|
91
|
+
input: DecisionInput,
|
|
78
92
|
options: ClassifyOptions,
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const input = validateParams(params);
|
|
93
|
+
execute: (prepared: PreparedInput, route: ResolvedModel, apiKey: string) => Promise<ClassifierResult>,
|
|
94
|
+
): Promise<{ result: ClassifierResult; route: ResolvedModel }> {
|
|
82
95
|
const candidates = [
|
|
83
96
|
configuredDefault(options.settings),
|
|
84
97
|
...configuredRoutes(options.settings, "fallback"),
|
|
@@ -94,9 +107,12 @@ export async function classify(
|
|
|
94
107
|
seen.add(key);
|
|
95
108
|
attemptedRoutes.push(routePair(route));
|
|
96
109
|
try {
|
|
97
|
-
const
|
|
110
|
+
const prepared = await prepareInput(input, route, options.cwd, options.signal, options.memoryImages);
|
|
111
|
+
checkCancelled(options.signal);
|
|
112
|
+
const apiKey = resolveApiKey(route, options.credentialSource ?? "pi-store");
|
|
113
|
+
const result = await execute(prepared, route, apiKey);
|
|
98
114
|
if (attemptedRoutes.length > 1) result.attemptedRoutes = attemptedRoutes;
|
|
99
|
-
return result;
|
|
115
|
+
return { result, route };
|
|
100
116
|
} catch (error) {
|
|
101
117
|
checkCancelled(options.signal);
|
|
102
118
|
if (!permitsFallback(error)) throw error;
|
|
@@ -106,6 +122,19 @@ export async function classify(
|
|
|
106
122
|
throw lastError;
|
|
107
123
|
}
|
|
108
124
|
|
|
125
|
+
/** Default plus explicitly configured, selective fallback. Never retries a route. */
|
|
126
|
+
export async function classify(
|
|
127
|
+
params: unknown,
|
|
128
|
+
options: ClassifyOptions,
|
|
129
|
+
): Promise<ClassifierResult> {
|
|
130
|
+
choicePolicy(options.settings);
|
|
131
|
+
const input = validateParams(params);
|
|
132
|
+
const { result } = await runDecisionRequest(input, options, (prepared, route, apiKey) =>
|
|
133
|
+
decideWithRoute(prepared, route, apiKey, options),
|
|
134
|
+
);
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
|
|
109
138
|
/** Explicit comparisons only: configured entries, no default substitution or fallback. */
|
|
110
139
|
export async function compareClassifiers(
|
|
111
140
|
params: unknown,
|
package/src/config.ts
CHANGED
|
@@ -87,7 +87,7 @@ export const BUILT_INS = [
|
|
|
87
87
|
envVar: "OPENROUTER_API_KEY",
|
|
88
88
|
models: [
|
|
89
89
|
{ id: "cloudflare/clairic", input: ["text", "image"] },
|
|
90
|
-
{ id: "inception/mercury-decide", input: ["text"] },
|
|
90
|
+
{ id: "inception/mercury-decide", input: ["text"], shutdown: "2026-12-28" },
|
|
91
91
|
{ id: "liquid/d1.2", input: ["text", "image"] },
|
|
92
92
|
{ id: "liquid/d1.2-ultra", input: ["text", "image"] },
|
|
93
93
|
{ id: "perplexity/pplx-decider-v1.3", input: ["text"] },
|
|
@@ -104,6 +104,16 @@ export const BUILT_INS = [
|
|
|
104
104
|
envVar: "LITELLM_API_KEY",
|
|
105
105
|
models: [{ id: "clairic", input: ["text", "image"] }],
|
|
106
106
|
},
|
|
107
|
+
{
|
|
108
|
+
id: "litellm",
|
|
109
|
+
route: "litellm-decisions-api",
|
|
110
|
+
api: "openai-decisions",
|
|
111
|
+
baseUrl: "",
|
|
112
|
+
credentialProvider: "litellm",
|
|
113
|
+
envVar: "LITELLM_API_KEY",
|
|
114
|
+
// The declared alias is LiteLLM's own documentation example; any proxy model_name resolves.
|
|
115
|
+
models: [{ id: "luna", input: ["text"] }],
|
|
116
|
+
},
|
|
107
117
|
{
|
|
108
118
|
id: "microsoft",
|
|
109
119
|
route: "microsoft-api",
|
|
@@ -160,6 +170,19 @@ export const BUILT_INS = [
|
|
|
160
170
|
{ id: "gemini-decision-1", input: ["text", "image", "audio", "video"] },
|
|
161
171
|
],
|
|
162
172
|
},
|
|
173
|
+
{
|
|
174
|
+
id: "opencode-go",
|
|
175
|
+
route: "opencode-go-api",
|
|
176
|
+
api: "system-one",
|
|
177
|
+
baseUrl: "https://opencode.ai/inference/go/systemone/v1",
|
|
178
|
+
credentialProvider: "opencode-go",
|
|
179
|
+
envVar: "OPENCODE_API_KEY",
|
|
180
|
+
models: [
|
|
181
|
+
{ id: "jev-1.14", input: ["text"] },
|
|
182
|
+
{ id: "gemini-decision-1", input: ["text"] },
|
|
183
|
+
{ id: "gpt-6-luna-decision", input: ["text"] },
|
|
184
|
+
],
|
|
185
|
+
},
|
|
163
186
|
{
|
|
164
187
|
id: "inception",
|
|
165
188
|
route: "inception-api",
|
|
@@ -167,7 +190,7 @@ export const BUILT_INS = [
|
|
|
167
190
|
baseUrl: "https://api.inceptionlabs.ai/v1",
|
|
168
191
|
credentialProvider: "inception",
|
|
169
192
|
envVar: "INCEPTION_API_KEY",
|
|
170
|
-
models: [{ id: "mercury-decide", input: ["text", "image"] }],
|
|
193
|
+
models: [{ id: "mercury-decide", input: ["text", "image"], shutdown: "2026-12-28" }],
|
|
171
194
|
},
|
|
172
195
|
{
|
|
173
196
|
id: "parallel",
|
|
@@ -196,6 +219,37 @@ export const BUILT_INS = [
|
|
|
196
219
|
{ id: "pplx-decider-v1-27b", input: ["text", "image"] },
|
|
197
220
|
],
|
|
198
221
|
},
|
|
222
|
+
{
|
|
223
|
+
id: "subq",
|
|
224
|
+
route: "subq-api",
|
|
225
|
+
api: "subq-system-one",
|
|
226
|
+
baseUrl: "https://api.subq.ai",
|
|
227
|
+
credentialProvider: "subq",
|
|
228
|
+
envVar: "SUBQ_API_KEY",
|
|
229
|
+
models: [
|
|
230
|
+
{ id: "subq-snap-1.2", input: ["text", "image"] },
|
|
231
|
+
{ id: "subq-snap-latest", input: ["text", "image"] },
|
|
232
|
+
],
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
id: "dashscope",
|
|
236
|
+
route: "alibaba-api",
|
|
237
|
+
api: "alibaba-system-one",
|
|
238
|
+
// Decision Model endpoints are workspace- and region-scoped; the operator supplies the gateway host.
|
|
239
|
+
baseUrl: "",
|
|
240
|
+
credentialProvider: "dashscope",
|
|
241
|
+
envVar: "DASHSCOPE_API_KEY",
|
|
242
|
+
models: [{ id: "decision-model", input: ["text", "image"] }],
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
id: "fireworks",
|
|
246
|
+
route: "fireworks-api",
|
|
247
|
+
api: "fireworks-system-one",
|
|
248
|
+
baseUrl: "https://api.fireworks.ai/inference/v1",
|
|
249
|
+
credentialProvider: "fireworks",
|
|
250
|
+
envVar: "FIREWORKS_API_KEY",
|
|
251
|
+
models: [{ id: "accounts/fireworks/models/rune", input: ["text"] }],
|
|
252
|
+
},
|
|
199
253
|
{
|
|
200
254
|
id: "liquid",
|
|
201
255
|
route: "liquid-api",
|
|
@@ -268,7 +322,7 @@ export const BUILT_INS = [
|
|
|
268
322
|
{ id: "microsoft/microsoft-decision-1.1", input: ["text", "image"] },
|
|
269
323
|
{ id: "cloudflare/clef-flash", input: ["text", "image"] },
|
|
270
324
|
{ id: "cloudflare/clef-omni", input: ["text", "image", "audio", "video"] },
|
|
271
|
-
{ id: "inception/mercury-decide", input: ["text", "image"] },
|
|
325
|
+
{ id: "inception/mercury-decide", input: ["text", "image"], shutdown: "2026-12-28" },
|
|
272
326
|
{ id: "liquid/d1.2", input: ["text", "image"] },
|
|
273
327
|
{ id: "liquid/d1.2-ultra", input: ["text", "image"] },
|
|
274
328
|
{ id: "openai/gpt-6-luna-decisions", input: ["text", "image"] },
|
|
@@ -368,8 +422,17 @@ export function openRouterDecisionCapabilities(model: string): OpenRouterDecisio
|
|
|
368
422
|
}
|
|
369
423
|
|
|
370
424
|
function builtInLimits(api: ApiStyle, provider: string | undefined, model: string): RouteLimits {
|
|
371
|
-
if (api === "openai-decisions")
|
|
425
|
+
if (api === "openai-decisions") {
|
|
426
|
+
// LiteLLM validates 1-128 questions on both decision routes before calling a provider.
|
|
427
|
+
if (provider === "litellm") return { ...DEFAULT_LIMITS, maxQuestions: 128 };
|
|
372
428
|
return { ...DEFAULT_LIMITS, maxQuestions: 400 };
|
|
429
|
+
}
|
|
430
|
+
if (api === "subq-system-one")
|
|
431
|
+
return { ...DEFAULT_LIMITS, maxQuestions: 128, maxImages: 4, maxAudio: 0, maxVideo: 0 };
|
|
432
|
+
if (api === "alibaba-system-one")
|
|
433
|
+
return { ...DEFAULT_LIMITS, maxQuestions: 16, maxImages: 16, maxAudio: 0, maxVideo: 0 };
|
|
434
|
+
if (api === "fireworks-system-one")
|
|
435
|
+
return { ...DEFAULT_LIMITS, maxQuestions: 256, maxImages: 0, maxAudio: 0, maxVideo: 0 };
|
|
373
436
|
if (api === "openrouter-extractions") {
|
|
374
437
|
const caps = openRouterExtractionCapabilities(model);
|
|
375
438
|
return { ...DEFAULT_LIMITS, maxImages: caps.maxImages, maxAudio: 0, maxVideo: 0 };
|
|
@@ -647,6 +710,9 @@ export function resolveModel(
|
|
|
647
710
|
"openrouter-extractions",
|
|
648
711
|
"litellm-extraction",
|
|
649
712
|
"microsoft-system-one",
|
|
713
|
+
"subq-system-one",
|
|
714
|
+
"alibaba-system-one",
|
|
715
|
+
"fireworks-system-one",
|
|
650
716
|
].includes(api)
|
|
651
717
|
)
|
|
652
718
|
return configuration("Unsupported classifier API protocol.");
|
|
@@ -707,7 +773,7 @@ export function resolveModel(
|
|
|
707
773
|
"/ai";
|
|
708
774
|
if (!baseUrl)
|
|
709
775
|
return configuration(
|
|
710
|
-
"The selected provider requires a baseUrl. Microsoft uses your Foundry resource endpoint
|
|
776
|
+
"The selected provider requires a baseUrl. Microsoft uses your Foundry resource endpoint, Alibaba uses your Model Studio workspace gateway host, and local runtimes use your server URL.",
|
|
711
777
|
);
|
|
712
778
|
const timeout = settings.requestTimeoutMs ?? 30_000;
|
|
713
779
|
if (!Number.isInteger(timeout) || timeout < 100 || timeout > 300_000)
|
|
@@ -856,12 +922,33 @@ function knownMaxChoices(route: ResolvedModel): number | undefined {
|
|
|
856
922
|
route.provider === "liquid" &&
|
|
857
923
|
route.baseUrl === "https://api.liquid.ai/decisions/v1" &&
|
|
858
924
|
["d1.2", "d1.2-ultra"].includes(route.model);
|
|
925
|
+
const subq =
|
|
926
|
+
route.provider === "subq" &&
|
|
927
|
+
route.baseUrl === "https://api.subq.ai" &&
|
|
928
|
+
["subq-snap-1.2", "subq-snap-latest"].includes(route.model);
|
|
929
|
+
const alibaba =
|
|
930
|
+
route.api === "alibaba-system-one" &&
|
|
931
|
+
route.provider === "dashscope" &&
|
|
932
|
+
route.model === "decision-model";
|
|
933
|
+
const fireworks =
|
|
934
|
+
route.provider === "fireworks" &&
|
|
935
|
+
route.baseUrl === "https://api.fireworks.ai/inference/v1" &&
|
|
936
|
+
route.model === "accounts/fireworks/models/rune";
|
|
937
|
+
const litellm = route.route === "litellm-decisions-api";
|
|
938
|
+
if (subq || alibaba || litellm) return 255;
|
|
939
|
+
if (fireworks) return 208;
|
|
859
940
|
return directJev || sarvam || openai || perplexity || clef || liquid ? 255 : undefined;
|
|
860
941
|
}
|
|
861
942
|
|
|
862
943
|
export function listModels(settings: ClassifierSettings): string {
|
|
863
944
|
const lines = BUILT_INS.flatMap((provider) =>
|
|
864
|
-
provider.models.map(
|
|
945
|
+
provider.models.map(
|
|
946
|
+
(model: ModelDefinition) =>
|
|
947
|
+
provider.route +
|
|
948
|
+
" " +
|
|
949
|
+
model.id +
|
|
950
|
+
(model.shutdown ? " (shuts down " + model.shutdown + ")" : ""),
|
|
951
|
+
),
|
|
865
952
|
);
|
|
866
953
|
for (const [id, provider] of Object.entries(settings.customProviders ?? {})) {
|
|
867
954
|
if (Array.isArray(provider?.models))
|