@abhishek944/pi-classifier 0.3.0 → 0.5.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 +48 -0
- package/COMPUTER_USE.md +117 -0
- package/README.md +344 -30
- package/dist/auth.d.ts +4 -2
- package/dist/auth.js +18 -2
- package/dist/box-tool.d.ts +4 -0
- package/dist/box-tool.js +79 -0
- package/dist/box.d.ts +32 -0
- package/dist/box.js +148 -0
- package/dist/choices.js +35 -1
- package/dist/classify.d.ts +6 -0
- package/dist/classify.js +4 -4
- 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.d.ts +140 -3
- package/dist/config.js +405 -20
- package/dist/cost.js +42 -4
- package/dist/durable.d.ts +34 -0
- package/dist/durable.js +237 -0
- package/dist/extension.d.ts +2 -247
- package/dist/extension.js +53 -190
- package/dist/extract.d.ts +11 -0
- package/dist/extract.js +191 -0
- package/dist/http.d.ts +6 -1
- package/dist/http.js +64 -28
- package/dist/image-content.d.ts +12 -0
- package/dist/image-content.js +90 -0
- package/dist/index.d.ts +10 -1
- package/dist/index.js +3 -0
- package/dist/inputs.d.ts +24 -2
- package/dist/inputs.js +565 -134
- package/dist/mcp-cli.d.ts +2 -0
- package/dist/mcp-cli.js +39 -0
- package/dist/mcp-server.d.ts +28 -0
- package/dist/mcp-server.js +121 -0
- package/dist/mcp-tool-definitions.d.ts +19 -0
- package/dist/mcp-tool-definitions.js +64 -0
- package/dist/providers.d.ts +1 -0
- package/dist/providers.js +1218 -124
- package/dist/tool-contracts.d.ts +785 -0
- package/dist/tool-contracts.js +530 -0
- package/dist/tree.js +1 -1
- package/dist/types.d.ts +314 -20
- package/dist/validation.js +181 -44
- package/package.json +33 -5
- package/src/auth.ts +26 -2
- package/src/box-tool.ts +110 -0
- package/src/box.ts +253 -0
- package/src/choices.ts +44 -2
- package/src/classify.ts +10 -6
- 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 +454 -23
- package/src/cost.ts +42 -4
- package/src/durable.ts +298 -0
- package/src/extension.ts +79 -265
- package/src/extract.ts +228 -0
- package/src/http.ts +63 -32
- package/src/image-content.ts +77 -0
- package/src/index.ts +25 -0
- package/src/inputs.ts +603 -184
- package/src/mcp-cli.ts +44 -0
- package/src/mcp-server.ts +179 -0
- package/src/mcp-tool-definitions.ts +104 -0
- package/src/providers.ts +1398 -144
- package/src/tool-contracts.ts +785 -0
- package/src/tree.ts +1 -1
- package/src/types.ts +248 -11
- package/src/validation.ts +169 -96
package/src/validation.ts
CHANGED
|
@@ -4,133 +4,206 @@ 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", "media", "mode", "conversation", "imageDetail", "promptCache"].includes(key)))
|
|
25
|
+
invalid("Expected state, questions and optional images, media, mode, conversation, imageDetail or promptCache; 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 >
|
|
35
|
-
invalid("Supply between 1 and 32 questions.");
|
|
31
|
+
if (entries.length < 1 || entries.length > 1000) invalid("Supply between 1 and 1000 questions; the selected route enforces its own lower limit.");
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
34
|
+
invalid("Question IDs must be 1–64 letters, digits, underscores or hyphens.");
|
|
35
|
+
const allowed = question.type === "number" || question.type === "estimate"
|
|
36
|
+
? ["type", "instructions", "unit", "range", "step"]
|
|
37
|
+
: question.type === "spans" || question.type === "boxes"
|
|
38
|
+
? ["type", "instructions", "max"]
|
|
39
|
+
: question.type === "choice"
|
|
40
|
+
? ["type", "instructions", "criteria", "booleanValues"]
|
|
41
|
+
: question.type === "score"
|
|
42
|
+
? ["type", "instructions", "criteria", "descriptions"]
|
|
43
|
+
: question.type === "extract"
|
|
44
|
+
? ["type", "instructions", "criteria", "source", "threshold", "snapToWords", "debug"]
|
|
45
|
+
: ["type", "instructions", "criteria"];
|
|
46
|
+
if (Object.keys(question).some((key) => !allowed.includes(key)))
|
|
47
|
+
invalid("Unknown question field for " + String(question.type) + ".");
|
|
48
|
+
if (question.type !== "extract" && !content(question.instructions, 16_384, true))
|
|
49
|
+
invalid("Each question needs non-empty text or ordered text/image-reference instructions, at most 16384 characters/bytes.");
|
|
50
|
+
if (question.type === "extract" && question.instructions !== undefined && !content(question.instructions, 16_384))
|
|
51
|
+
invalid("Extract instructions must be text or ordered text/image-reference content, at most 16384 characters/bytes.");
|
|
55
52
|
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.");
|
|
53
|
+
if (question.criteria !== undefined && (!isRecord(question.criteria) ||
|
|
54
|
+
Object.entries(question.criteria).some(([key, entry]) => !["true", "false"].includes(key) || !content(entry, 4096))))
|
|
55
|
+
invalid("bool criteria must contain optional true/false descriptions or image content.");
|
|
67
56
|
} else if (question.type === "choice") {
|
|
68
|
-
if (!isRecord(question.criteria))
|
|
69
|
-
invalid("choice criteria must map labels to descriptions.");
|
|
57
|
+
if (!isRecord(question.criteria)) invalid("choice criteria must map labels to descriptions.");
|
|
70
58
|
const criteria = Object.entries(question.criteria);
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
)
|
|
80
|
-
)
|
|
81
|
-
invalid("choice requires at least one label with a string description.");
|
|
59
|
+
if (criteria.length < 1 || criteria.some(([key, entry]) => !key.trim() || key.length > 128 || !content(entry, 4096)))
|
|
60
|
+
invalid("choice requires at least one label with a text or image-content description.");
|
|
61
|
+
if (question.booleanValues !== undefined) {
|
|
62
|
+
if (typeof question.booleanValues !== "boolean")
|
|
63
|
+
invalid("choice.booleanValues must be a boolean.");
|
|
64
|
+
if (question.booleanValues && criteria.some(([key]) => key !== "true" && key !== "false"))
|
|
65
|
+
invalid("Boolean-valued choices must use the labels true and false.");
|
|
66
|
+
}
|
|
82
67
|
} else if (question.type === "score") {
|
|
83
|
-
if (
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
)
|
|
91
|
-
)
|
|
92
|
-
invalid(
|
|
93
|
-
"score criteria must contain 2–10 non-empty ordered descriptions.",
|
|
94
|
-
);
|
|
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.");
|
|
95
75
|
} else if (question.type === "box") {
|
|
96
|
-
if (question.criteria !== undefined)
|
|
97
|
-
|
|
98
|
-
|
|
76
|
+
if (question.criteria !== undefined) invalid("box does not accept criteria.");
|
|
77
|
+
} else if (question.type === "locate" || question.type === "text" || question.type === "date") {
|
|
78
|
+
if (question.criteria !== undefined) invalid(question.type + " does not accept criteria.");
|
|
79
|
+
} else if (question.type === "spans" || question.type === "boxes") {
|
|
80
|
+
if (question.criteria !== undefined) invalid(question.type + " does not accept criteria.");
|
|
81
|
+
if (question.max !== undefined &&
|
|
82
|
+
(!Number.isSafeInteger(question.max) || (question.max as number) < 1 || (question.max as number) > 64))
|
|
83
|
+
invalid(question.type + ".max must be an integer from 1 to 64.");
|
|
84
|
+
} else if (question.type === "number" || question.type === "estimate") {
|
|
85
|
+
if (question.unit !== undefined && (typeof question.unit !== "string" || question.unit.length > 128))
|
|
86
|
+
invalid("unit must be a string of at most 128 characters.");
|
|
87
|
+
if (question.step !== undefined && (typeof question.step !== "number" || !Number.isFinite(question.step) || question.step <= 0))
|
|
88
|
+
invalid("step must be a positive finite number.");
|
|
89
|
+
const range = question.range;
|
|
90
|
+
if (question.type === "estimate" && !Array.isArray(range))
|
|
91
|
+
invalid("estimate requires a [low, high] range.");
|
|
92
|
+
if (range !== undefined && (!Array.isArray(range) || range.length !== 2 ||
|
|
93
|
+
range.some((entry) => typeof entry !== "number" || !Number.isFinite(entry)) ||
|
|
94
|
+
(range[0] as number) >= (range[1] as number)))
|
|
95
|
+
invalid("range must be a finite [low, high] pair with low below high.");
|
|
96
|
+
} else if (question.type === "extract") {
|
|
97
|
+
if (!isRecord(question.criteria))
|
|
98
|
+
invalid("extract criteria must map field names to descriptions.");
|
|
99
|
+
const criteria = Object.entries(question.criteria);
|
|
100
|
+
if (criteria.length < 1 || criteria.length > 26 || criteria.some(([key, entry]) => !key.trim() || key.length > 128 || !content(entry, 4096)))
|
|
101
|
+
invalid("extract requires 1–26 fields with text descriptions.");
|
|
102
|
+
if (question.source !== undefined && (typeof question.source !== "string" || !question.source.trim() || question.source.length > 4096))
|
|
103
|
+
invalid("extract.source must be a non-empty string of at most 4096 characters.");
|
|
104
|
+
if (question.threshold !== undefined && (typeof question.threshold !== "number" || !Number.isFinite(question.threshold) || question.threshold < 0 || question.threshold > 1))
|
|
105
|
+
invalid("extract.threshold must be a number between 0 and 1.");
|
|
106
|
+
if (question.snapToWords !== undefined && typeof question.snapToWords !== "boolean")
|
|
107
|
+
invalid("extract.snapToWords must be a boolean.");
|
|
108
|
+
if (question.debug !== undefined && typeof question.debug !== "boolean")
|
|
109
|
+
invalid("extract.debug must be a boolean.");
|
|
110
|
+
} else if (question.type === "multi_choice") {
|
|
111
|
+
if (!isRecord(question.criteria))
|
|
112
|
+
invalid("multi_choice criteria must map labels to descriptions.");
|
|
113
|
+
const criteria = Object.entries(question.criteria);
|
|
114
|
+
if (criteria.length < 2 || criteria.some(([key, entry]) => !key.trim() || key.length > 128 || !content(entry, 4096)))
|
|
115
|
+
invalid("multi_choice requires at least two labels with text or image-content descriptions.");
|
|
116
|
+
} else invalid("Question type must be a supported typed decision or read.");
|
|
117
|
+
}
|
|
118
|
+
if (value.media !== undefined) {
|
|
119
|
+
if (!Array.isArray(value.media))
|
|
120
|
+
invalid("media must be an array of audio/video descriptors.");
|
|
121
|
+
for (const item of value.media) {
|
|
122
|
+
if (!isRecord(item) || (item.type !== "audio" && item.type !== "video") ||
|
|
123
|
+
Object.keys(item).some((key) => key !== "type" && key !== "path") ||
|
|
124
|
+
typeof item.path !== "string" || !item.path.trim() || item.path.length > 4096)
|
|
125
|
+
invalid("Each media descriptor needs a type of audio or video and a local path.");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (value.conversation !== undefined) {
|
|
129
|
+
if (!Array.isArray(value.conversation))
|
|
130
|
+
invalid("conversation must be an array of OpenAI Decisions input items.");
|
|
131
|
+
const types = ["message", "function_call", "function_call_output", "custom_tool_call", "custom_tool_call_output"];
|
|
132
|
+
for (const item of value.conversation) {
|
|
133
|
+
if (!isRecord(item) || typeof item.type !== "string" || !types.includes(item.type))
|
|
134
|
+
invalid("Each conversation item needs a documented OpenAI Decisions input type.");
|
|
135
|
+
}
|
|
99
136
|
}
|
|
100
|
-
if (
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
value.
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
137
|
+
if (value.mode !== undefined && value.mode !== "web_grounding")
|
|
138
|
+
invalid("mode must be omitted or \"web_grounding\".");
|
|
139
|
+
if (value.imageDetail !== undefined &&
|
|
140
|
+
(typeof value.imageDetail !== "string" || !["auto", "low", "high", "original"].includes(value.imageDetail)))
|
|
141
|
+
invalid("imageDetail must be auto, low, high or original.");
|
|
142
|
+
if (value.promptCache !== undefined) {
|
|
143
|
+
if (!isRecord(value.promptCache) ||
|
|
144
|
+
Object.keys(value.promptCache).some((key) => !["key", "mode", "ttl", "breakpoints"].includes(key)))
|
|
145
|
+
invalid("promptCache may only set key, mode, ttl and breakpoints.");
|
|
146
|
+
if (value.promptCache.key !== undefined &&
|
|
147
|
+
(typeof value.promptCache.key !== "string" || value.promptCache.key.length < 1 || value.promptCache.key.length > 64))
|
|
148
|
+
invalid("promptCache.key must be 1–64 characters.");
|
|
149
|
+
if (value.promptCache.mode !== undefined &&
|
|
150
|
+
(typeof value.promptCache.mode !== "string" || !["implicit", "explicit"].includes(value.promptCache.mode)))
|
|
151
|
+
invalid("promptCache.mode must be implicit or explicit.");
|
|
152
|
+
if (value.promptCache.ttl !== undefined && value.promptCache.ttl !== "30m")
|
|
153
|
+
invalid("promptCache.ttl may only be 30m.");
|
|
154
|
+
if (value.promptCache.breakpoints !== undefined && typeof value.promptCache.breakpoints !== "boolean")
|
|
155
|
+
invalid("promptCache.breakpoints must be a boolean.");
|
|
156
|
+
}
|
|
157
|
+
const ids = new Set<string>();
|
|
158
|
+
if (value.images !== undefined) {
|
|
159
|
+
if (!Array.isArray(value.images) || value.images.length > 128)
|
|
160
|
+
invalid("images must contain at most 128 local paths or named path/url descriptors; route-specific limits also apply.");
|
|
161
|
+
for (const image of value.images) {
|
|
162
|
+
if (typeof image === "string") {
|
|
163
|
+
if (!image.trim() || image.length > 4096) invalid("Image paths must be non-empty, at most 4096 characters.");
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (!isRecord(image) || typeof image.id !== "string" || !imageId.test(image.id) ||
|
|
167
|
+
Object.keys(image).some((key) => !["id", "path", "url"].includes(key)) ||
|
|
168
|
+
(Object.hasOwn(image, "path") === Object.hasOwn(image, "url")))
|
|
169
|
+
invalid("Named images require a unique id and exactly one path or url.");
|
|
170
|
+
const source = image.path ?? image.url;
|
|
171
|
+
if (typeof source !== "string" || !source.trim() || source.length > 4096)
|
|
172
|
+
invalid("Image path/url must be non-empty, at most 4096 characters.");
|
|
173
|
+
if (ids.has(image.id)) invalid("Named image IDs must be unique.");
|
|
174
|
+
ids.add(image.id);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const references = [...imageReferences(value.state), ...questionImageReferences(value.questions as Record<string, Question>)];
|
|
178
|
+
for (const id of references) if (!ids.has(id)) invalid("Every image reference must name an image descriptor in images.");
|
|
179
|
+
for (const id of ids) if (!references.includes(id)) invalid("Every named image must be referenced in state, instructions or criteria.");
|
|
180
|
+
if (value.mode === "web_grounding" &&
|
|
181
|
+
Object.values(value.questions).some((question) => isRecord(question) && question.type === "locate") &&
|
|
182
|
+
typeof value.state !== "string")
|
|
183
|
+
invalid("web grounding with locate requires a string state.");
|
|
184
|
+
if (Buffer.byteLength(JSON.stringify(value)) > 2 * 1024 * 1024)
|
|
112
185
|
invalid("Classification text and questions must be at most 2 MiB.");
|
|
113
186
|
return value as unknown as ClassifyParams;
|
|
114
187
|
}
|
|
115
188
|
|
|
116
189
|
function validateJson(value: unknown, depth: number): void {
|
|
117
|
-
if (depth > 32) invalid("
|
|
118
|
-
if (value
|
|
190
|
+
if (depth > 32) invalid("JSON content must be at most 32 levels deep.");
|
|
191
|
+
if (isImageReference(value)) {
|
|
192
|
+
if (Object.keys(value).length !== 1 || typeof value.$image !== "string" || !imageId.test(value.$image))
|
|
193
|
+
invalid("An image reference must be exactly {$image: id} with a 1–64 character image ID.");
|
|
119
194
|
return;
|
|
195
|
+
}
|
|
196
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") return;
|
|
120
197
|
if (typeof value === "number" && Number.isFinite(value)) return;
|
|
121
198
|
if (Array.isArray(value)) {
|
|
122
199
|
for (const entry of value) validateJson(entry, depth + 1);
|
|
123
200
|
return;
|
|
124
201
|
}
|
|
125
|
-
if (
|
|
126
|
-
isRecord(value) &&
|
|
127
|
-
(Object.getPrototypeOf(value) === Object.prototype ||
|
|
128
|
-
Object.getPrototypeOf(value) === null)
|
|
129
|
-
) {
|
|
202
|
+
if (isRecord(value) && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)) {
|
|
130
203
|
for (const entry of Object.values(value)) validateJson(entry, depth + 1);
|
|
131
204
|
return;
|
|
132
205
|
}
|
|
133
|
-
invalid("
|
|
206
|
+
invalid("Content must contain only finite JSON values.");
|
|
134
207
|
}
|
|
135
208
|
|
|
136
209
|
export function hasBox(questions: Record<string, Question>): boolean {
|