@abhishek944/pi-classifier 0.2.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 +27 -0
- package/COMPUTER_USE.md +117 -0
- package/README.md +268 -8
- 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/choices.d.ts +8 -0
- package/dist/choices.js +131 -0
- package/dist/classification-tree.d.ts +4 -0
- package/dist/classification-tree.js +143 -0
- package/dist/classify.d.ts +4 -2
- package/dist/classify.js +5 -2
- 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 +44 -0
- package/dist/cost.d.ts +8 -0
- package/dist/cost.js +202 -0
- package/dist/extension.d.ts +164 -9
- package/dist/extension.js +164 -14
- package/dist/image-content.d.ts +12 -0
- package/dist/image-content.js +90 -0
- package/dist/index.d.ts +9 -1
- package/dist/index.js +3 -0
- package/dist/inputs.d.ts +11 -2
- package/dist/inputs.js +155 -131
- package/dist/providers.js +113 -100
- package/dist/tree.d.ts +4 -0
- package/dist/tree.js +136 -0
- package/dist/types.d.ts +113 -11
- package/dist/validation.js +63 -39
- package/package.json +2 -1
- package/src/box-tool.ts +145 -0
- package/src/box.ts +253 -0
- package/src/choices.ts +174 -0
- package/src/classification-tree.ts +181 -0
- package/src/classify.ts +16 -4
- 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 +55 -0
- package/src/cost.ts +250 -0
- package/src/extension.ts +191 -15
- package/src/image-content.ts +77 -0
- package/src/index.ts +18 -0
- package/src/inputs.ts +163 -185
- package/src/providers.ts +113 -119
- package/src/tree.ts +157 -0
- package/src/types.ts +117 -10
- package/src/validation.ts +62 -95
package/dist/types.d.ts
CHANGED
|
@@ -1,22 +1,34 @@
|
|
|
1
1
|
export type ApiStyle = "openai-decisions" | "perplexity-decisions" | "system-one" | "cloudflare-system-one" | "microsoft-system-one";
|
|
2
|
+
/** Explicit references are resolved only from the request's named images. */
|
|
3
|
+
export type ImageReference = {
|
|
4
|
+
$image: string;
|
|
5
|
+
};
|
|
6
|
+
export type QuestionContent = string | (string | ImageReference)[];
|
|
7
|
+
export type ImageInput = string | {
|
|
8
|
+
id: string;
|
|
9
|
+
path: string;
|
|
10
|
+
} | {
|
|
11
|
+
id: string;
|
|
12
|
+
url: string;
|
|
13
|
+
};
|
|
2
14
|
export type Question = {
|
|
3
15
|
type: "bool";
|
|
4
|
-
instructions:
|
|
16
|
+
instructions: QuestionContent;
|
|
5
17
|
criteria?: {
|
|
6
|
-
true?:
|
|
7
|
-
false?:
|
|
18
|
+
true?: QuestionContent;
|
|
19
|
+
false?: QuestionContent;
|
|
8
20
|
};
|
|
9
21
|
} | {
|
|
10
22
|
type: "choice";
|
|
11
|
-
instructions:
|
|
12
|
-
criteria: Record<string,
|
|
23
|
+
instructions: QuestionContent;
|
|
24
|
+
criteria: Record<string, QuestionContent>;
|
|
13
25
|
} | {
|
|
14
26
|
type: "score";
|
|
15
|
-
instructions:
|
|
16
|
-
criteria:
|
|
27
|
+
instructions: QuestionContent;
|
|
28
|
+
criteria: QuestionContent[];
|
|
17
29
|
} | {
|
|
18
30
|
type: "box";
|
|
19
|
-
instructions:
|
|
31
|
+
instructions: QuestionContent;
|
|
20
32
|
};
|
|
21
33
|
export type Answer = {
|
|
22
34
|
type: "bool";
|
|
@@ -26,6 +38,9 @@ export type Answer = {
|
|
|
26
38
|
choice: string;
|
|
27
39
|
probabilities: Record<string, number>;
|
|
28
40
|
confidence: number;
|
|
41
|
+
/** Present when probabilities cover one internally processed batch, not the full list. */
|
|
42
|
+
probabilityScope?: "batch";
|
|
43
|
+
noneOfAboveProbability?: number;
|
|
29
44
|
} | {
|
|
30
45
|
type: "score";
|
|
31
46
|
score: number;
|
|
@@ -42,19 +57,83 @@ export type Answer = {
|
|
|
42
57
|
confidence: number;
|
|
43
58
|
} | {
|
|
44
59
|
type: "refusal";
|
|
60
|
+
} | {
|
|
61
|
+
type: "no_match";
|
|
62
|
+
} | {
|
|
63
|
+
type: "ambiguous";
|
|
45
64
|
};
|
|
46
65
|
export interface ClassifyParams {
|
|
47
66
|
state: string | Record<string, unknown> | unknown[];
|
|
48
67
|
questions: Record<string, Question>;
|
|
49
|
-
/**
|
|
50
|
-
images?:
|
|
68
|
+
/** Legacy paths are shared in order. Named images require explicit {$image:id} references. */
|
|
69
|
+
images?: ImageInput[];
|
|
51
70
|
}
|
|
71
|
+
export type ClassificationTreeNode = {
|
|
72
|
+
id: string;
|
|
73
|
+
description?: string;
|
|
74
|
+
children?: ClassificationTreeNode[];
|
|
75
|
+
};
|
|
76
|
+
export type ClassificationTree = {
|
|
77
|
+
roots: ClassificationTreeNode[];
|
|
78
|
+
};
|
|
79
|
+
export type ClassifyTreeParams = {
|
|
80
|
+
state: ClassifyParams["state"];
|
|
81
|
+
tree: string | ClassificationTree;
|
|
82
|
+
instructions?: string;
|
|
83
|
+
images?: ImageInput[];
|
|
84
|
+
};
|
|
85
|
+
export type TreeStep = {
|
|
86
|
+
level: number;
|
|
87
|
+
id: string;
|
|
88
|
+
source: "automatic" | "model";
|
|
89
|
+
confidence?: number;
|
|
90
|
+
probabilityScope?: "siblings" | "batch";
|
|
91
|
+
noneOfAboveProbability?: number;
|
|
92
|
+
};
|
|
93
|
+
export type ClassifierTreeResult = {
|
|
94
|
+
provider: string;
|
|
95
|
+
model: string;
|
|
96
|
+
status: "complete" | "no_match" | "ambiguous" | "refusal";
|
|
97
|
+
levels: Record<string, string>;
|
|
98
|
+
steps: TreeStep[];
|
|
99
|
+
providerCalls: number;
|
|
100
|
+
cost: ClassifierCost;
|
|
101
|
+
usage?: NonNullable<ClassifierResult["usage"]>;
|
|
102
|
+
attemptedRoutes?: ClassifierRoute[];
|
|
103
|
+
};
|
|
52
104
|
export interface ModelDefinition {
|
|
53
105
|
id: string;
|
|
54
106
|
name?: string;
|
|
55
107
|
input?: ("text" | "image")[];
|
|
56
108
|
box?: boolean;
|
|
57
109
|
}
|
|
110
|
+
/** USD per million tokens. Cache counts, when reported, are part of input tokens. */
|
|
111
|
+
export type TokenPricing = {
|
|
112
|
+
inputPerMillion: number;
|
|
113
|
+
outputPerMillion: number;
|
|
114
|
+
cachedInputPerMillion?: number;
|
|
115
|
+
cacheWritePerMillion?: number;
|
|
116
|
+
};
|
|
117
|
+
export type ClassifierCost = {
|
|
118
|
+
status: "estimated";
|
|
119
|
+
currency: "USD";
|
|
120
|
+
input: number;
|
|
121
|
+
output: number;
|
|
122
|
+
total: number;
|
|
123
|
+
source: "builtin" | "configured";
|
|
124
|
+
rates: TokenPricing;
|
|
125
|
+
} | {
|
|
126
|
+
status: "estimated";
|
|
127
|
+
currency: "USD";
|
|
128
|
+
input: number;
|
|
129
|
+
output: number;
|
|
130
|
+
total: number;
|
|
131
|
+
source: "aggregated";
|
|
132
|
+
} | {
|
|
133
|
+
status: "unavailable";
|
|
134
|
+
currency: "USD";
|
|
135
|
+
reason: "unknown_pricing" | "missing_usage" | "invalid_usage" | "missing_cache_pricing" | "calculation_overflow";
|
|
136
|
+
};
|
|
58
137
|
export interface ProviderSettings {
|
|
59
138
|
apiKey?: string;
|
|
60
139
|
baseUrl?: string;
|
|
@@ -62,6 +141,10 @@ export interface ProviderSettings {
|
|
|
62
141
|
accountId?: string;
|
|
63
142
|
experimentalImages?: boolean;
|
|
64
143
|
experimentalBox?: boolean;
|
|
144
|
+
/** Model ID (or Azure deployment name) to explicit token rates. */
|
|
145
|
+
pricing?: Record<string, TokenPricing>;
|
|
146
|
+
/** Verified wire choice limit; needed for batching on endpoints without a known limit. */
|
|
147
|
+
maxChoices?: number;
|
|
65
148
|
}
|
|
66
149
|
export interface CustomProviderSettings extends ProviderSettings {
|
|
67
150
|
api: ApiStyle;
|
|
@@ -72,6 +155,9 @@ export type ClassifierRoute = {
|
|
|
72
155
|
model: string;
|
|
73
156
|
};
|
|
74
157
|
export interface ClassifierSettings {
|
|
158
|
+
computerUse?: import("./computer-use-types.js").ComputerUseSettings;
|
|
159
|
+
none_of_above_choice?: boolean;
|
|
160
|
+
remove_choice_limits?: boolean;
|
|
75
161
|
default?: ClassifierRoute;
|
|
76
162
|
fallback?: ClassifierRoute[];
|
|
77
163
|
compare?: ClassifierRoute[];
|
|
@@ -95,13 +181,23 @@ export interface ResolvedModel {
|
|
|
95
181
|
box: boolean;
|
|
96
182
|
experimentalImages: boolean;
|
|
97
183
|
requestTimeoutMs: number;
|
|
184
|
+
pricing?: {
|
|
185
|
+
rates: TokenPricing;
|
|
186
|
+
source: "builtin" | "configured";
|
|
187
|
+
};
|
|
188
|
+
maxChoices?: number;
|
|
98
189
|
}
|
|
99
|
-
export interface
|
|
190
|
+
export interface PreparedLocalImage {
|
|
100
191
|
data: string;
|
|
101
192
|
mimeType: "image/png" | "image/jpeg" | "image/webp";
|
|
102
193
|
width: number;
|
|
103
194
|
height: number;
|
|
104
195
|
}
|
|
196
|
+
export type PreparedImage = (PreparedLocalImage | {
|
|
197
|
+
url: string;
|
|
198
|
+
}) & {
|
|
199
|
+
id?: string;
|
|
200
|
+
};
|
|
105
201
|
export interface PreparedInput {
|
|
106
202
|
state: ClassifyParams["state"];
|
|
107
203
|
questions: Record<string, Question>;
|
|
@@ -115,7 +211,13 @@ export type ClassifierResult = {
|
|
|
115
211
|
inputTokens: number;
|
|
116
212
|
outputTokens: number;
|
|
117
213
|
totalTokens: number;
|
|
214
|
+
cachedInputTokens?: number;
|
|
215
|
+
cacheWriteTokens?: number;
|
|
118
216
|
};
|
|
217
|
+
/** Successful provider requests made by automatic choice processing. */
|
|
218
|
+
providerCalls?: number;
|
|
219
|
+
/** Estimate for completed requests in this result, not an invoice or total of failed attempts. */
|
|
220
|
+
cost: ClassifierCost;
|
|
119
221
|
requestId?: string;
|
|
120
222
|
attemptedRoutes?: ClassifierRoute[];
|
|
121
223
|
};
|
package/dist/validation.js
CHANGED
|
@@ -1,14 +1,24 @@
|
|
|
1
1
|
import { ClassifierError, isRecord, } from "./types.js";
|
|
2
|
+
import { imageReferences, isImageReference, questionImageReferences } from "./image-content.js";
|
|
2
3
|
function invalid(message) {
|
|
3
4
|
throw new ClassifierError("input", message);
|
|
4
5
|
}
|
|
6
|
+
const imageId = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
7
|
+
function content(value, maxLength, nonempty = false) {
|
|
8
|
+
if (typeof value === "string")
|
|
9
|
+
return value.length <= maxLength && (!nonempty || Boolean(value.trim()));
|
|
10
|
+
if (!Array.isArray(value) || value.length < 1 || value.length > 128)
|
|
11
|
+
return false;
|
|
12
|
+
if (!value.every((part) => typeof part === "string" || isImageReference(part)))
|
|
13
|
+
return false;
|
|
14
|
+
validateJson(value, 0);
|
|
15
|
+
return Buffer.byteLength(JSON.stringify(value)) <= maxLength &&
|
|
16
|
+
(!nonempty || value.some((part) => typeof part !== "string" || Boolean(part.trim())));
|
|
17
|
+
}
|
|
5
18
|
export function validateParams(value) {
|
|
6
|
-
if (!isRecord(value) ||
|
|
7
|
-
Object.keys(value).some((key) => !["state", "questions", "images"].includes(key)))
|
|
19
|
+
if (!isRecord(value) || Object.keys(value).some((key) => !["state", "questions", "images"].includes(key)))
|
|
8
20
|
invalid("Expected state, questions and optional images; provider/model are selected in settings.");
|
|
9
|
-
if (!(typeof value.state === "string" ||
|
|
10
|
-
Array.isArray(value.state) ||
|
|
11
|
-
isRecord(value.state)))
|
|
21
|
+
if (!(typeof value.state === "string" || Array.isArray(value.state) || isRecord(value.state)))
|
|
12
22
|
invalid("state must be text, a JSON object or an array.");
|
|
13
23
|
validateJson(value.state, 0);
|
|
14
24
|
if (!isRecord(value.questions))
|
|
@@ -21,36 +31,24 @@ export function validateParams(value) {
|
|
|
21
31
|
invalid("Question IDs must be 1–64 letters, digits, underscores or hyphens.");
|
|
22
32
|
if (Object.keys(question).some((key) => !["type", "instructions", "criteria"].includes(key)))
|
|
23
33
|
invalid("Unknown question field.");
|
|
24
|
-
if (
|
|
25
|
-
|
|
26
|
-
question.instructions.length > 16_384)
|
|
27
|
-
invalid("Each question needs non-empty instructions, at most 16384 characters.");
|
|
34
|
+
if (!content(question.instructions, 16_384, true))
|
|
35
|
+
invalid("Each question needs non-empty text or ordered text/image-reference instructions, at most 16384 characters/bytes.");
|
|
28
36
|
if (question.type === "bool") {
|
|
29
|
-
if (question.criteria !== undefined &&
|
|
30
|
-
(
|
|
31
|
-
|
|
32
|
-
typeof entry !== "string" ||
|
|
33
|
-
entry.length > 4096)))
|
|
34
|
-
invalid("bool criteria must contain optional true/false descriptions.");
|
|
37
|
+
if (question.criteria !== undefined && (!isRecord(question.criteria) ||
|
|
38
|
+
Object.entries(question.criteria).some(([key, entry]) => !["true", "false"].includes(key) || !content(entry, 4096))))
|
|
39
|
+
invalid("bool criteria must contain optional true/false descriptions or image content.");
|
|
35
40
|
}
|
|
36
41
|
else if (question.type === "choice") {
|
|
37
42
|
if (!isRecord(question.criteria))
|
|
38
43
|
invalid("choice criteria must map labels to descriptions.");
|
|
39
44
|
const criteria = Object.entries(question.criteria);
|
|
40
|
-
if (criteria.length < 1 ||
|
|
41
|
-
|
|
42
|
-
criteria.some(([key, entry]) => !key.trim() ||
|
|
43
|
-
key.length > 128 ||
|
|
44
|
-
typeof entry !== "string" ||
|
|
45
|
-
entry.length > 4096))
|
|
46
|
-
invalid("choice requires 1–64 labels with string descriptions.");
|
|
45
|
+
if (criteria.length < 1 || criteria.some(([key, entry]) => !key.trim() || key.length > 128 || !content(entry, 4096)))
|
|
46
|
+
invalid("choice requires at least one label with a text or image-content description.");
|
|
47
47
|
}
|
|
48
48
|
else if (question.type === "score") {
|
|
49
|
-
if (!Array.isArray(question.criteria) ||
|
|
50
|
-
question.criteria.
|
|
51
|
-
|
|
52
|
-
question.criteria.some((entry) => typeof entry !== "string" || !entry.trim() || entry.length > 4096))
|
|
53
|
-
invalid("score criteria must contain 2–10 non-empty ordered descriptions.");
|
|
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.");
|
|
54
52
|
}
|
|
55
53
|
else if (question.type === "box") {
|
|
56
54
|
if (question.criteria !== undefined)
|
|
@@ -59,19 +57,47 @@ export function validateParams(value) {
|
|
|
59
57
|
else
|
|
60
58
|
invalid("Question type must be bool, choice, score or box.");
|
|
61
59
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
60
|
+
const ids = new Set();
|
|
61
|
+
if (value.images !== undefined) {
|
|
62
|
+
if (!Array.isArray(value.images) || value.images.length > 8)
|
|
63
|
+
invalid("images must contain at most eight local paths or named path/url descriptors; route-specific limits also apply.");
|
|
64
|
+
for (const image of value.images) {
|
|
65
|
+
if (typeof image === "string") {
|
|
66
|
+
if (!image.trim() || image.length > 4096)
|
|
67
|
+
invalid("Image paths must be non-empty, at most 4096 characters.");
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (!isRecord(image) || typeof image.id !== "string" || !imageId.test(image.id) ||
|
|
71
|
+
Object.keys(image).some((key) => !["id", "path", "url"].includes(key)) ||
|
|
72
|
+
(Object.hasOwn(image, "path") === Object.hasOwn(image, "url")))
|
|
73
|
+
invalid("Named images require a unique id and exactly one path or url.");
|
|
74
|
+
const source = image.path ?? image.url;
|
|
75
|
+
if (typeof source !== "string" || !source.trim() || source.length > 4096)
|
|
76
|
+
invalid("Image path/url must be non-empty, at most 4096 characters.");
|
|
77
|
+
if (ids.has(image.id))
|
|
78
|
+
invalid("Named image IDs must be unique.");
|
|
79
|
+
ids.add(image.id);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const references = [...imageReferences(value.state), ...questionImageReferences(value.questions)];
|
|
83
|
+
for (const id of references)
|
|
84
|
+
if (!ids.has(id))
|
|
85
|
+
invalid("Every image reference must name an image descriptor in images.");
|
|
86
|
+
for (const id of ids)
|
|
87
|
+
if (!references.includes(id))
|
|
88
|
+
invalid("Every named image must be referenced in state, instructions or criteria.");
|
|
89
|
+
if (Buffer.byteLength(JSON.stringify(value)) > 2 * 1024 * 1024)
|
|
69
90
|
invalid("Classification text and questions must be at most 2 MiB.");
|
|
70
91
|
return value;
|
|
71
92
|
}
|
|
72
93
|
function validateJson(value, depth) {
|
|
73
94
|
if (depth > 32)
|
|
74
|
-
invalid("
|
|
95
|
+
invalid("JSON content must be at most 32 levels deep.");
|
|
96
|
+
if (isImageReference(value)) {
|
|
97
|
+
if (Object.keys(value).length !== 1 || typeof value.$image !== "string" || !imageId.test(value.$image))
|
|
98
|
+
invalid("An image reference must be exactly {$image: id} with a 1–64 character image ID.");
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
75
101
|
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
76
102
|
return;
|
|
77
103
|
if (typeof value === "number" && Number.isFinite(value))
|
|
@@ -81,14 +107,12 @@ function validateJson(value, depth) {
|
|
|
81
107
|
validateJson(entry, depth + 1);
|
|
82
108
|
return;
|
|
83
109
|
}
|
|
84
|
-
if (isRecord(value) &&
|
|
85
|
-
(Object.getPrototypeOf(value) === Object.prototype ||
|
|
86
|
-
Object.getPrototypeOf(value) === null)) {
|
|
110
|
+
if (isRecord(value) && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)) {
|
|
87
111
|
for (const entry of Object.values(value))
|
|
88
112
|
validateJson(entry, depth + 1);
|
|
89
113
|
return;
|
|
90
114
|
}
|
|
91
|
-
invalid("
|
|
115
|
+
invalid("Content must contain only finite JSON values.");
|
|
92
116
|
}
|
|
93
117
|
export function hasBox(questions) {
|
|
94
118
|
return Object.values(questions).some((question) => question.type === "box");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abhishek944/pi-classifier",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Config-driven typed classification for Pi via Microsoft Decision-1, OpenAI Decisions, Perplexity, TypeSafe Jev and Cloudflare Clef, with selective fallback and explicit comparisons.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"src",
|
|
42
42
|
"CHANGELOG.md",
|
|
43
43
|
"README.md",
|
|
44
|
+
"COMPUTER_USE.md",
|
|
44
45
|
"LICENSE",
|
|
45
46
|
"THIRD_PARTY_NOTICES.md"
|
|
46
47
|
],
|
package/src/box-tool.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineTool,
|
|
3
|
+
type ExtensionAPI,
|
|
4
|
+
} from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { Type } from "typebox";
|
|
6
|
+
import {
|
|
7
|
+
renderClassifierBox,
|
|
8
|
+
type ClassifierBoxMode,
|
|
9
|
+
type ClassifierBoxPixelBox,
|
|
10
|
+
} from "./box.js";
|
|
11
|
+
import { ClassifierError, userError } from "./types.js";
|
|
12
|
+
|
|
13
|
+
export const boxParameters = Type.Object(
|
|
14
|
+
{
|
|
15
|
+
image: Type.String({
|
|
16
|
+
minLength: 1,
|
|
17
|
+
maxLength: 4096,
|
|
18
|
+
description:
|
|
19
|
+
"Local PNG, JPEG or WebP file inside cwd. No URLs, symlinks or inline base64.",
|
|
20
|
+
}),
|
|
21
|
+
box: Type.Object(
|
|
22
|
+
{
|
|
23
|
+
top: Type.Number({ minimum: 0, maximum: 1 }),
|
|
24
|
+
bottom: Type.Number({ minimum: 0, maximum: 1 }),
|
|
25
|
+
left: Type.Number({ minimum: 0, maximum: 1 }),
|
|
26
|
+
right: Type.Number({ minimum: 0, maximum: 1 }),
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
additionalProperties: false,
|
|
30
|
+
description:
|
|
31
|
+
"Normalized edges between 0 and 1, with top < bottom and left < right.",
|
|
32
|
+
},
|
|
33
|
+
),
|
|
34
|
+
mode: Type.Optional(
|
|
35
|
+
Type.Union([Type.Literal("overlay"), Type.Literal("crop")], {
|
|
36
|
+
description:
|
|
37
|
+
"overlay draws the box on the image (default); crop returns only the boxed region.",
|
|
38
|
+
}),
|
|
39
|
+
),
|
|
40
|
+
},
|
|
41
|
+
{ additionalProperties: false },
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
export const boxOutput = Type.Object({
|
|
45
|
+
mode: Type.Union([Type.Literal("overlay"), Type.Literal("crop")]),
|
|
46
|
+
mimeType: Type.Literal("image/png"),
|
|
47
|
+
width: Type.Integer({ minimum: 1 }),
|
|
48
|
+
height: Type.Integer({ minimum: 1 }),
|
|
49
|
+
pixelBox: Type.Object({
|
|
50
|
+
left: Type.Integer({ minimum: 0 }),
|
|
51
|
+
top: Type.Integer({ minimum: 0 }),
|
|
52
|
+
width: Type.Integer({ minimum: 1 }),
|
|
53
|
+
height: Type.Integer({ minimum: 1 }),
|
|
54
|
+
}),
|
|
55
|
+
byteLength: Type.Integer({ minimum: 1 }),
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
type BoxToolDetails = {
|
|
59
|
+
mode: ClassifierBoxMode;
|
|
60
|
+
mimeType: "image/png";
|
|
61
|
+
width: number;
|
|
62
|
+
height: number;
|
|
63
|
+
pixelBox: ClassifierBoxPixelBox;
|
|
64
|
+
byteLength: number;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export function registerBoxTool(pi: ExtensionAPI): void {
|
|
68
|
+
pi.registerTool(
|
|
69
|
+
defineTool<typeof boxParameters, BoxToolDetails>({
|
|
70
|
+
name: "classifier_box_image",
|
|
71
|
+
label: "Box image",
|
|
72
|
+
description:
|
|
73
|
+
"Render a normalized bounding box on a local image, or crop to it, entirely locally. Use to inspect a box answer from classify. overlay keeps the image and draws a rectangle; crop returns only the clamped region. Returns a PNG plus exact source-pixel box metadata. No provider request, upload, resizing or disk write. A box is a localization hint, not proof that the object exists.",
|
|
74
|
+
promptSnippet:
|
|
75
|
+
"Render or crop a local image using normalized box coordinates.",
|
|
76
|
+
promptGuidelines: [
|
|
77
|
+
"Use classifier_box_image only for local images inside cwd and normalized boxes between 0 and 1.",
|
|
78
|
+
"A returned box does not by itself confirm that the object exists.",
|
|
79
|
+
],
|
|
80
|
+
parameters: boxParameters,
|
|
81
|
+
outputSchema: boxOutput,
|
|
82
|
+
annotations: {
|
|
83
|
+
readOnlyHint: true,
|
|
84
|
+
destructiveHint: false,
|
|
85
|
+
openWorldHint: false,
|
|
86
|
+
},
|
|
87
|
+
async execute(_id, params, signal, _onUpdate, ctx) {
|
|
88
|
+
try {
|
|
89
|
+
const result = await renderClassifierBox(params, {
|
|
90
|
+
cwd: ctx.cwd,
|
|
91
|
+
...(signal ? { signal } : {}),
|
|
92
|
+
});
|
|
93
|
+
const byteLength = Buffer.from(result.data, "base64").byteLength;
|
|
94
|
+
const details: BoxToolDetails = {
|
|
95
|
+
mode: result.mode,
|
|
96
|
+
mimeType: result.mimeType,
|
|
97
|
+
width: result.width,
|
|
98
|
+
height: result.height,
|
|
99
|
+
pixelBox: result.pixelBox,
|
|
100
|
+
byteLength,
|
|
101
|
+
};
|
|
102
|
+
const box = result.pixelBox;
|
|
103
|
+
return {
|
|
104
|
+
content: [
|
|
105
|
+
{
|
|
106
|
+
type: "text",
|
|
107
|
+
text:
|
|
108
|
+
"Rendered " +
|
|
109
|
+
result.mode +
|
|
110
|
+
" box as a " +
|
|
111
|
+
result.width +
|
|
112
|
+
"×" +
|
|
113
|
+
result.height +
|
|
114
|
+
" PNG (" +
|
|
115
|
+
byteLength +
|
|
116
|
+
" bytes). Source pixel box: left=" +
|
|
117
|
+
box.left +
|
|
118
|
+
", top=" +
|
|
119
|
+
box.top +
|
|
120
|
+
", width=" +
|
|
121
|
+
box.width +
|
|
122
|
+
", height=" +
|
|
123
|
+
box.height +
|
|
124
|
+
". A box is a localization hint, not proof that the object exists.",
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
type: "image",
|
|
128
|
+
data: result.data,
|
|
129
|
+
mimeType: result.mimeType,
|
|
130
|
+
},
|
|
131
|
+
],
|
|
132
|
+
details,
|
|
133
|
+
structuredContent: details,
|
|
134
|
+
};
|
|
135
|
+
} catch (error) {
|
|
136
|
+
console.error(
|
|
137
|
+
"[pi-classifier] box image failed: " +
|
|
138
|
+
(error instanceof ClassifierError ? error.code : "unexpected"),
|
|
139
|
+
);
|
|
140
|
+
throw new Error(userError(error));
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
}
|