@abhishek944/pi-classifier 0.0.0-stage → 0.2.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 +25 -0
- package/LICENSE +201 -0
- package/README.md +241 -2
- package/THIRD_PARTY_NOTICES.md +48 -0
- package/dist/auth.d.ts +5 -0
- package/dist/auth.js +56 -0
- package/dist/classify.d.ts +11 -0
- package/dist/classify.js +117 -0
- package/dist/config.d.ts +82 -0
- package/dist/config.js +298 -0
- package/dist/extension.d.ts +122 -0
- package/dist/extension.js +255 -0
- package/dist/http.d.ts +5 -0
- package/dist/http.js +85 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/inputs.d.ts +2 -0
- package/dist/inputs.js +184 -0
- package/dist/providers.d.ts +2 -0
- package/dist/providers.js +614 -0
- package/dist/settings-write.d.ts +10 -0
- package/dist/settings-write.js +110 -0
- package/dist/types.d.ts +144 -0
- package/dist/types.js +18 -0
- package/dist/validation.d.ts +3 -0
- package/dist/validation.js +95 -0
- package/package.json +72 -4
- package/src/auth.ts +74 -0
- package/src/classify.ts +157 -0
- package/src/config.ts +391 -0
- package/src/extension.ts +362 -0
- package/src/http.ts +108 -0
- package/src/index.ts +17 -0
- package/src/inputs.ts +239 -0
- package/src/providers.ts +898 -0
- package/src/settings-write.ts +145 -0
- package/src/types.ts +152 -0
- package/src/validation.ts +139 -0
package/src/extension.ts
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { isProjectTrusted } from "@amaster.ai/pi-shared/settings";
|
|
2
|
+
import {
|
|
3
|
+
defineTool,
|
|
4
|
+
type ExtensionAPI,
|
|
5
|
+
type ExtensionContext,
|
|
6
|
+
} from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { Type } from "typebox";
|
|
8
|
+
import { classify, compareClassifiers } from "./classify.js";
|
|
9
|
+
import {
|
|
10
|
+
configuredRoutes,
|
|
11
|
+
listModels,
|
|
12
|
+
loadClassifierSettings,
|
|
13
|
+
resolveModel,
|
|
14
|
+
} from "./config.js";
|
|
15
|
+
import { updateProjectClassifierSettings } from "./settings-write.js";
|
|
16
|
+
import { ClassifierError, userError } from "./types.js";
|
|
17
|
+
|
|
18
|
+
const shortText = Type.String({ maxLength: 4096 });
|
|
19
|
+
const question = Type.Union([
|
|
20
|
+
Type.Object(
|
|
21
|
+
{
|
|
22
|
+
type: Type.Literal("bool"),
|
|
23
|
+
instructions: Type.String({ minLength: 1, maxLength: 16384 }),
|
|
24
|
+
criteria: Type.Optional(
|
|
25
|
+
Type.Object(
|
|
26
|
+
{ true: Type.Optional(shortText), false: Type.Optional(shortText) },
|
|
27
|
+
{ additionalProperties: false },
|
|
28
|
+
),
|
|
29
|
+
),
|
|
30
|
+
},
|
|
31
|
+
{ additionalProperties: false },
|
|
32
|
+
),
|
|
33
|
+
Type.Object(
|
|
34
|
+
{
|
|
35
|
+
type: Type.Literal("choice"),
|
|
36
|
+
instructions: Type.String({ minLength: 1, maxLength: 16384 }),
|
|
37
|
+
criteria: Type.Record(
|
|
38
|
+
Type.String({ minLength: 1, maxLength: 128 }),
|
|
39
|
+
shortText,
|
|
40
|
+
{ minProperties: 1, maxProperties: 64 },
|
|
41
|
+
),
|
|
42
|
+
},
|
|
43
|
+
{ additionalProperties: false },
|
|
44
|
+
),
|
|
45
|
+
Type.Object(
|
|
46
|
+
{
|
|
47
|
+
type: Type.Literal("score"),
|
|
48
|
+
instructions: Type.String({ minLength: 1, maxLength: 16384 }),
|
|
49
|
+
criteria: Type.Array(Type.String({ minLength: 1, maxLength: 4096 }), {
|
|
50
|
+
minItems: 2,
|
|
51
|
+
maxItems: 10,
|
|
52
|
+
}),
|
|
53
|
+
},
|
|
54
|
+
{ additionalProperties: false },
|
|
55
|
+
),
|
|
56
|
+
Type.Object(
|
|
57
|
+
{
|
|
58
|
+
type: Type.Literal("box"),
|
|
59
|
+
instructions: Type.String({ minLength: 1, maxLength: 16384 }),
|
|
60
|
+
},
|
|
61
|
+
{ additionalProperties: false },
|
|
62
|
+
),
|
|
63
|
+
]);
|
|
64
|
+
|
|
65
|
+
export const classifyParameters = Type.Object(
|
|
66
|
+
{
|
|
67
|
+
state: Type.Union(
|
|
68
|
+
[
|
|
69
|
+
Type.String(),
|
|
70
|
+
Type.Record(Type.String(), Type.Unknown()),
|
|
71
|
+
Type.Array(Type.Unknown()),
|
|
72
|
+
],
|
|
73
|
+
{
|
|
74
|
+
description:
|
|
75
|
+
"Text or JSON data to classify. All questions refer to this same state.",
|
|
76
|
+
},
|
|
77
|
+
),
|
|
78
|
+
questions: Type.Record(
|
|
79
|
+
Type.String({ pattern: "^[a-zA-Z0-9_-]{1,64}$" }),
|
|
80
|
+
question,
|
|
81
|
+
{ minProperties: 1, maxProperties: 32 },
|
|
82
|
+
),
|
|
83
|
+
images: Type.Optional(
|
|
84
|
+
Type.Array(Type.String({ minLength: 1, maxLength: 4096 }), {
|
|
85
|
+
maxItems: 5,
|
|
86
|
+
description:
|
|
87
|
+
"Local PNG/JPEG/WebP files inside cwd, in order. No URLs, symlinks or inline base64. Requires an image-capable model.",
|
|
88
|
+
}),
|
|
89
|
+
),
|
|
90
|
+
},
|
|
91
|
+
{ additionalProperties: false },
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
export const classifyOutput = Type.Object({
|
|
95
|
+
provider: Type.String(),
|
|
96
|
+
model: Type.String(),
|
|
97
|
+
answers: Type.Record(
|
|
98
|
+
Type.String(),
|
|
99
|
+
Type.Union([
|
|
100
|
+
Type.Object({
|
|
101
|
+
type: Type.Literal("bool"),
|
|
102
|
+
probability: Type.Number({ minimum: 0, maximum: 1 }),
|
|
103
|
+
}),
|
|
104
|
+
Type.Object({
|
|
105
|
+
type: Type.Literal("choice"),
|
|
106
|
+
choice: Type.String(),
|
|
107
|
+
probabilities: Type.Record(
|
|
108
|
+
Type.String(),
|
|
109
|
+
Type.Number({ minimum: 0, maximum: 1 }),
|
|
110
|
+
),
|
|
111
|
+
confidence: Type.Number({ minimum: 0, maximum: 1 }),
|
|
112
|
+
}),
|
|
113
|
+
Type.Object({
|
|
114
|
+
type: Type.Literal("score"),
|
|
115
|
+
score: Type.Number(),
|
|
116
|
+
confidence: Type.Number({ minimum: 0, maximum: 1 }),
|
|
117
|
+
probabilities: Type.Optional(
|
|
118
|
+
Type.Record(Type.String(), Type.Number({ minimum: 0, maximum: 1 })),
|
|
119
|
+
),
|
|
120
|
+
}),
|
|
121
|
+
Type.Object({
|
|
122
|
+
type: Type.Literal("box"),
|
|
123
|
+
box: Type.Object({
|
|
124
|
+
top: Type.Number(),
|
|
125
|
+
bottom: Type.Number(),
|
|
126
|
+
left: Type.Number(),
|
|
127
|
+
right: Type.Number(),
|
|
128
|
+
}),
|
|
129
|
+
confidence: Type.Number({ minimum: 0, maximum: 1 }),
|
|
130
|
+
}),
|
|
131
|
+
Type.Object({ type: Type.Literal("refusal") }),
|
|
132
|
+
]),
|
|
133
|
+
),
|
|
134
|
+
usage: Type.Optional(
|
|
135
|
+
Type.Object({
|
|
136
|
+
inputTokens: Type.Number(),
|
|
137
|
+
outputTokens: Type.Number(),
|
|
138
|
+
totalTokens: Type.Number(),
|
|
139
|
+
}),
|
|
140
|
+
),
|
|
141
|
+
requestId: Type.Optional(Type.String()),
|
|
142
|
+
attemptedRoutes: Type.Optional(
|
|
143
|
+
Type.Array(Type.Object({ provider: Type.String(), model: Type.String() })),
|
|
144
|
+
),
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
export const classifyCompareOutput = Type.Object({
|
|
148
|
+
cancelled: Type.Boolean(),
|
|
149
|
+
results: Type.Array(
|
|
150
|
+
Type.Union([
|
|
151
|
+
Type.Object({
|
|
152
|
+
provider: Type.String(),
|
|
153
|
+
model: Type.String(),
|
|
154
|
+
durationMs: Type.Number(),
|
|
155
|
+
status: Type.Literal("success"),
|
|
156
|
+
result: classifyOutput,
|
|
157
|
+
}),
|
|
158
|
+
Type.Object({
|
|
159
|
+
provider: Type.String(),
|
|
160
|
+
model: Type.String(),
|
|
161
|
+
durationMs: Type.Number(),
|
|
162
|
+
status: Type.Literal("error"),
|
|
163
|
+
error: Type.Object({ code: Type.String(), message: Type.String() }),
|
|
164
|
+
}),
|
|
165
|
+
]),
|
|
166
|
+
),
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
export default function piClassifier(pi: ExtensionAPI): void {
|
|
170
|
+
pi.registerTool(
|
|
171
|
+
defineTool({
|
|
172
|
+
name: "classify",
|
|
173
|
+
label: "Classify",
|
|
174
|
+
description:
|
|
175
|
+
"Ask a decision model typed questions about text, JSON and supported local images. Returns bool probabilities, choices with probabilities, or scores. Experimental box answers require explicit Jev configuration. Provider/model are fixed by pi-classifier.default (or legacy defaultProvider/defaultModel); this tool cannot override them. API-key authentication is independent of Codex chat login. Only explicitly configured fallback routes may be used after a missing key or HTTP 401/404/429; never after uncertainty, refusals, timeouts or network failures. Use /classifier list or status to inspect configuration.",
|
|
176
|
+
promptSnippet:
|
|
177
|
+
"Classify text, JSON or supported images into typed probabilistic decisions.",
|
|
178
|
+
promptGuidelines: [
|
|
179
|
+
"Use for bounded judgments, classification and rubric scoring, not open-ended prose.",
|
|
180
|
+
"Treat low confidence as uncertainty, not permission to switch models or retry.",
|
|
181
|
+
"Send only data the user intends to share with the configured provider.",
|
|
182
|
+
"Ask /classifier status for the active model. Do not change provider settings without user direction.",
|
|
183
|
+
],
|
|
184
|
+
parameters: classifyParameters,
|
|
185
|
+
outputSchema: classifyOutput,
|
|
186
|
+
annotations: {
|
|
187
|
+
readOnlyHint: true,
|
|
188
|
+
destructiveHint: false,
|
|
189
|
+
openWorldHint: true,
|
|
190
|
+
},
|
|
191
|
+
async execute(_id, params, signal, _onUpdate, ctx) {
|
|
192
|
+
try {
|
|
193
|
+
const result = await classify(params, {
|
|
194
|
+
settings: loadClassifierSettings(ctx.cwd, isProjectTrusted(ctx)),
|
|
195
|
+
cwd: ctx.cwd,
|
|
196
|
+
...(signal ? { signal } : {}),
|
|
197
|
+
});
|
|
198
|
+
return {
|
|
199
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
200
|
+
details: result,
|
|
201
|
+
structuredContent: { ...result },
|
|
202
|
+
};
|
|
203
|
+
} catch (error) {
|
|
204
|
+
console.error(
|
|
205
|
+
"[pi-classifier] classify failed: " +
|
|
206
|
+
(error instanceof ClassifierError ? error.code : "unexpected"),
|
|
207
|
+
);
|
|
208
|
+
throw new Error(userError(error));
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
}),
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
pi.registerTool(
|
|
215
|
+
defineTool({
|
|
216
|
+
name: "classify_compare",
|
|
217
|
+
label: "Compare classifiers",
|
|
218
|
+
description:
|
|
219
|
+
"Explicitly compare the same typed questions and state using exactly pi-classifier.compare provider/model pairs. Each entry is a separate potentially billable request. No fallback, default substitution, provider/model overrides, or default changes. Returns labeled results, duration, usage and safe per-entry failures; cancellation retains completed results and prevents later requests.",
|
|
220
|
+
promptSnippet:
|
|
221
|
+
"Compare configured decision models only when the user explicitly requests a model comparison.",
|
|
222
|
+
promptGuidelines: [
|
|
223
|
+
"Use only when the user explicitly asks to compare classifier models; never use automatically for low confidence or refusals.",
|
|
224
|
+
"Each configured comparison route receives the submitted data and may incur a charge.",
|
|
225
|
+
],
|
|
226
|
+
parameters: classifyParameters,
|
|
227
|
+
outputSchema: classifyCompareOutput,
|
|
228
|
+
annotations: {
|
|
229
|
+
readOnlyHint: true,
|
|
230
|
+
destructiveHint: false,
|
|
231
|
+
openWorldHint: true,
|
|
232
|
+
},
|
|
233
|
+
async execute(_id, params, signal, _onUpdate, ctx) {
|
|
234
|
+
try {
|
|
235
|
+
const result = await compareClassifiers(params, {
|
|
236
|
+
settings: loadClassifierSettings(ctx.cwd, isProjectTrusted(ctx)),
|
|
237
|
+
cwd: ctx.cwd,
|
|
238
|
+
...(signal ? { signal } : {}),
|
|
239
|
+
});
|
|
240
|
+
return {
|
|
241
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
242
|
+
details: result,
|
|
243
|
+
structuredContent: { ...result },
|
|
244
|
+
};
|
|
245
|
+
} catch (error) {
|
|
246
|
+
throw new Error(userError(error));
|
|
247
|
+
}
|
|
248
|
+
},
|
|
249
|
+
}),
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
pi.registerCommand("classifier", {
|
|
253
|
+
description:
|
|
254
|
+
"Classifier: status, list, reload, use <route> <model>, compare <JSON params>. Use writes trusted project defaults; compare makes potentially billable calls.",
|
|
255
|
+
handler: async (args, ctx) => {
|
|
256
|
+
try {
|
|
257
|
+
const parts = args.trim().split(/\s+/).filter(Boolean);
|
|
258
|
+
const command = parts[0] ?? "status";
|
|
259
|
+
const settings = loadClassifierSettings(ctx.cwd, isProjectTrusted(ctx));
|
|
260
|
+
if (command === "compare") {
|
|
261
|
+
let params: unknown;
|
|
262
|
+
try {
|
|
263
|
+
params = JSON.parse(args.trim().slice("compare".length).trim());
|
|
264
|
+
} catch {
|
|
265
|
+
throw new ClassifierError(
|
|
266
|
+
"input",
|
|
267
|
+
"Usage: /classifier compare <JSON with state, questions and optional images>.",
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
const result = await compareClassifiers(params, {
|
|
271
|
+
settings,
|
|
272
|
+
cwd: ctx.cwd,
|
|
273
|
+
...(ctx.signal ? { signal: ctx.signal } : {}),
|
|
274
|
+
});
|
|
275
|
+
reply(pi, ctx, JSON.stringify(result, null, 2));
|
|
276
|
+
} else if (command === "list" && parts.length === 1) {
|
|
277
|
+
reply(
|
|
278
|
+
pi,
|
|
279
|
+
ctx,
|
|
280
|
+
listModels(settings) +
|
|
281
|
+
"\n\nFallback: " +
|
|
282
|
+
JSON.stringify(configuredRoutes(settings, "fallback")) +
|
|
283
|
+
"\nCompare: " +
|
|
284
|
+
JSON.stringify(configuredRoutes(settings, "compare")),
|
|
285
|
+
);
|
|
286
|
+
} else if (
|
|
287
|
+
(command === "status" || command === "reload") &&
|
|
288
|
+
parts.length <= 1
|
|
289
|
+
) {
|
|
290
|
+
const route = resolveModel(settings);
|
|
291
|
+
reply(
|
|
292
|
+
pi,
|
|
293
|
+
ctx,
|
|
294
|
+
JSON.stringify(
|
|
295
|
+
{
|
|
296
|
+
provider: route.route,
|
|
297
|
+
model: route.model,
|
|
298
|
+
images: route.images,
|
|
299
|
+
box: route.box,
|
|
300
|
+
requestTimeoutMs: route.requestTimeoutMs,
|
|
301
|
+
authentication: "API key only; independent of chat login",
|
|
302
|
+
fallback: configuredRoutes(settings, "fallback"),
|
|
303
|
+
compare: configuredRoutes(settings, "compare"),
|
|
304
|
+
},
|
|
305
|
+
null,
|
|
306
|
+
2,
|
|
307
|
+
),
|
|
308
|
+
);
|
|
309
|
+
} else if (command === "use" && parts.length === 3) {
|
|
310
|
+
const selected = {
|
|
311
|
+
...settings,
|
|
312
|
+
default: { provider: parts[1]!, model: parts[2]! },
|
|
313
|
+
};
|
|
314
|
+
const route = resolveModel(selected);
|
|
315
|
+
await updateProjectClassifierSettings(
|
|
316
|
+
ctx.cwd,
|
|
317
|
+
isProjectTrusted(ctx),
|
|
318
|
+
{ default: { provider: route.route, model: route.model } },
|
|
319
|
+
);
|
|
320
|
+
reply(
|
|
321
|
+
pi,
|
|
322
|
+
ctx,
|
|
323
|
+
"Selected " +
|
|
324
|
+
route.route +
|
|
325
|
+
" / " +
|
|
326
|
+
route.model +
|
|
327
|
+
" in project settings. Chat login is unchanged.",
|
|
328
|
+
);
|
|
329
|
+
} else {
|
|
330
|
+
reply(
|
|
331
|
+
pi,
|
|
332
|
+
ctx,
|
|
333
|
+
"Usage: /classifier status | list | reload | use <route> <model> | compare <JSON params>",
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
} catch (error) {
|
|
337
|
+
console.error(
|
|
338
|
+
"[pi-classifier] command failed: " +
|
|
339
|
+
(error instanceof ClassifierError ? error.code : "configuration"),
|
|
340
|
+
);
|
|
341
|
+
reply(
|
|
342
|
+
pi,
|
|
343
|
+
ctx,
|
|
344
|
+
error instanceof ClassifierError
|
|
345
|
+
? error.message
|
|
346
|
+
: "Could not update classifier settings. Check project trust and settings JSON.",
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function reply(pi: ExtensionAPI, ctx: ExtensionContext, text: string): void {
|
|
354
|
+
const bounded =
|
|
355
|
+
text.length > 16000 ? text.slice(0, 16000) + "\n[truncated]" : text;
|
|
356
|
+
if (ctx.hasUI) ctx.ui.notify(bounded, "info");
|
|
357
|
+
else
|
|
358
|
+
pi.sendMessage(
|
|
359
|
+
{ customType: "pi-classifier", content: bounded, display: true },
|
|
360
|
+
{ triggerTurn: false },
|
|
361
|
+
);
|
|
362
|
+
}
|
package/src/http.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { ClassifierError, type ResolvedModel } from "./types.js";
|
|
2
|
+
|
|
3
|
+
export async function postJson(
|
|
4
|
+
route: ResolvedModel,
|
|
5
|
+
path: string,
|
|
6
|
+
body: unknown,
|
|
7
|
+
apiKey: string,
|
|
8
|
+
signal?: AbortSignal,
|
|
9
|
+
extraHeaders?: Record<string, string>,
|
|
10
|
+
): Promise<{ body: unknown; requestId?: string }> {
|
|
11
|
+
if (signal?.aborted)
|
|
12
|
+
throw new ClassifierError("aborted", "Classification was cancelled.");
|
|
13
|
+
const encoded = JSON.stringify(body);
|
|
14
|
+
if (Buffer.byteLength(encoded) > 20 * 1024 * 1024)
|
|
15
|
+
throw new ClassifierError(
|
|
16
|
+
"input",
|
|
17
|
+
"Encoded classifier request exceeds 20 MiB.",
|
|
18
|
+
);
|
|
19
|
+
const controller = new AbortController();
|
|
20
|
+
const abort = () => controller.abort();
|
|
21
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
22
|
+
const timer = setTimeout(() => controller.abort(), route.requestTimeoutMs);
|
|
23
|
+
try {
|
|
24
|
+
const headers = new Headers(route.headers);
|
|
25
|
+
for (const [key, value] of Object.entries(extraHeaders ?? {}))
|
|
26
|
+
headers.set(key, value);
|
|
27
|
+
if (route.api === "microsoft-system-one") {
|
|
28
|
+
headers.set("api-key", apiKey);
|
|
29
|
+
} else {
|
|
30
|
+
headers.set("authorization", "Bearer " + apiKey);
|
|
31
|
+
}
|
|
32
|
+
headers.set("content-type", "application/json");
|
|
33
|
+
headers.set("accept", "application/json");
|
|
34
|
+
const response = await fetch(route.baseUrl + "/" + path, {
|
|
35
|
+
method: "POST",
|
|
36
|
+
headers,
|
|
37
|
+
body: encoded,
|
|
38
|
+
signal: controller.signal,
|
|
39
|
+
redirect: "error",
|
|
40
|
+
});
|
|
41
|
+
if (!response.ok) {
|
|
42
|
+
await response.body?.cancel();
|
|
43
|
+
throw new ClassifierError(
|
|
44
|
+
"http",
|
|
45
|
+
"Classifier request failed (HTTP " +
|
|
46
|
+
response.status +
|
|
47
|
+
"). Check credentials, model, limits and provider availability.",
|
|
48
|
+
response.status,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
const reader = response.body?.getReader();
|
|
52
|
+
if (!reader)
|
|
53
|
+
throw new ClassifierError(
|
|
54
|
+
"response",
|
|
55
|
+
"Classifier returned an empty response.",
|
|
56
|
+
);
|
|
57
|
+
const chunks: Uint8Array[] = [];
|
|
58
|
+
let size = 0;
|
|
59
|
+
try {
|
|
60
|
+
while (true) {
|
|
61
|
+
const next = await reader.read();
|
|
62
|
+
if (next.done) break;
|
|
63
|
+
size += next.value.length;
|
|
64
|
+
if (size > 1024 * 1024) {
|
|
65
|
+
await reader.cancel();
|
|
66
|
+
throw new ClassifierError(
|
|
67
|
+
"response",
|
|
68
|
+
"Classifier response exceeds 1 MiB.",
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
chunks.push(next.value);
|
|
72
|
+
}
|
|
73
|
+
} finally {
|
|
74
|
+
reader.releaseLock();
|
|
75
|
+
}
|
|
76
|
+
let parsed: unknown;
|
|
77
|
+
try {
|
|
78
|
+
parsed = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
79
|
+
} catch {
|
|
80
|
+
throw new ClassifierError(
|
|
81
|
+
"response",
|
|
82
|
+
"Classifier returned invalid JSON.",
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
const result: { body: unknown; requestId?: string } = { body: parsed };
|
|
86
|
+
const id =
|
|
87
|
+
response.headers.get("x-request-id") ??
|
|
88
|
+
response.headers.get("request-id");
|
|
89
|
+
if (id && /^[a-zA-Z0-9_.:-]{1,128}$/.test(id)) result.requestId = id;
|
|
90
|
+
return result;
|
|
91
|
+
} catch (error) {
|
|
92
|
+
if (controller.signal.aborted)
|
|
93
|
+
throw new ClassifierError(
|
|
94
|
+
signal?.aborted ? "aborted" : "timeout",
|
|
95
|
+
signal?.aborted
|
|
96
|
+
? "Classification was cancelled."
|
|
97
|
+
: "Classifier request timed out. This request was not retried.",
|
|
98
|
+
);
|
|
99
|
+
if (error instanceof ClassifierError) throw error;
|
|
100
|
+
throw new ClassifierError(
|
|
101
|
+
"network",
|
|
102
|
+
"Classifier service could not be reached. This request was not retried.",
|
|
103
|
+
);
|
|
104
|
+
} finally {
|
|
105
|
+
clearTimeout(timer);
|
|
106
|
+
signal?.removeEventListener("abort", abort);
|
|
107
|
+
}
|
|
108
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { classify, compareClassifiers } from "./classify.js";
|
|
2
|
+
export { loadClassifierSettings, resolveModel, listModels } from "./config.js";
|
|
3
|
+
export type {
|
|
4
|
+
Answer,
|
|
5
|
+
Question,
|
|
6
|
+
ClassifierResult,
|
|
7
|
+
ClassifierRoute,
|
|
8
|
+
ClassifierComparisonEntry,
|
|
9
|
+
ClassifierComparisonResult,
|
|
10
|
+
ClassifierSettings,
|
|
11
|
+
ClassifyParams,
|
|
12
|
+
CustomProviderSettings,
|
|
13
|
+
ModelDefinition,
|
|
14
|
+
ProviderSettings,
|
|
15
|
+
ApiStyle,
|
|
16
|
+
} from "./types.js";
|
|
17
|
+
export { ClassifierError } from "./types.js";
|