@abhishek944/pi-classifier 0.4.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.
Files changed (58) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +269 -31
  3. package/dist/auth.d.ts +4 -2
  4. package/dist/auth.js +18 -2
  5. package/dist/box-tool.d.ts +2 -24
  6. package/dist/box-tool.js +5 -35
  7. package/dist/choices.js +35 -1
  8. package/dist/classify.d.ts +3 -0
  9. package/dist/classify.js +3 -3
  10. package/dist/config.d.ts +140 -3
  11. package/dist/config.js +401 -20
  12. package/dist/cost.js +42 -4
  13. package/dist/durable.d.ts +34 -0
  14. package/dist/durable.js +237 -0
  15. package/dist/extension.d.ts +2 -275
  16. package/dist/extension.js +49 -211
  17. package/dist/extract.d.ts +11 -0
  18. package/dist/extract.js +191 -0
  19. package/dist/http.d.ts +6 -1
  20. package/dist/http.js +64 -28
  21. package/dist/index.d.ts +3 -1
  22. package/dist/index.js +1 -0
  23. package/dist/inputs.d.ts +16 -1
  24. package/dist/inputs.js +421 -14
  25. package/dist/mcp-cli.d.ts +2 -0
  26. package/dist/mcp-cli.js +39 -0
  27. package/dist/mcp-server.d.ts +28 -0
  28. package/dist/mcp-server.js +121 -0
  29. package/dist/mcp-tool-definitions.d.ts +19 -0
  30. package/dist/mcp-tool-definitions.js +64 -0
  31. package/dist/providers.d.ts +1 -0
  32. package/dist/providers.js +1144 -53
  33. package/dist/tool-contracts.d.ts +785 -0
  34. package/dist/tool-contracts.js +530 -0
  35. package/dist/tree.js +1 -1
  36. package/dist/types.d.ts +284 -8
  37. package/dist/validation.js +122 -10
  38. package/package.json +32 -5
  39. package/src/auth.ts +26 -2
  40. package/src/box-tool.ts +12 -47
  41. package/src/choices.ts +42 -1
  42. package/src/classify.ts +6 -4
  43. package/src/config.ts +450 -23
  44. package/src/cost.ts +42 -4
  45. package/src/durable.ts +298 -0
  46. package/src/extension.ts +75 -288
  47. package/src/extract.ts +228 -0
  48. package/src/http.ts +63 -32
  49. package/src/index.ts +15 -0
  50. package/src/inputs.ts +454 -13
  51. package/src/mcp-cli.ts +44 -0
  52. package/src/mcp-server.ts +179 -0
  53. package/src/mcp-tool-definitions.ts +104 -0
  54. package/src/providers.ts +1323 -56
  55. package/src/tool-contracts.ts +785 -0
  56. package/src/tree.ts +1 -1
  57. package/src/types.ts +231 -5
  58. package/src/validation.ts +114 -9
package/src/http.ts CHANGED
@@ -1,5 +1,34 @@
1
1
  import { ClassifierError, type ResolvedModel } from "./types.js";
2
2
 
3
+ /** Optional bounded parser for provider error envelopes; receives the active key so echoed credentials can be omitted. */
4
+ export type JsonErrorDetail = (status: number, body: unknown, apiKey: string) => string | undefined;
5
+
6
+ /** Optional sanitizer for a validated response-header request ID; omitted values fall back to provider-body IDs. */
7
+ export type JsonRequestIdDetail = (value: string, apiKey: string) => string | undefined;
8
+
9
+ async function readBoundedText(response: Response): Promise<string> {
10
+ const reader = response.body?.getReader();
11
+ if (!reader)
12
+ throw new ClassifierError("response", "Classifier returned an empty response.");
13
+ const chunks: Uint8Array[] = [];
14
+ let size = 0;
15
+ try {
16
+ while (true) {
17
+ const next = await reader.read();
18
+ if (next.done) break;
19
+ size += next.value.length;
20
+ if (size > 1024 * 1024) {
21
+ await reader.cancel();
22
+ throw new ClassifierError("response", "Classifier response exceeds 1 MiB.");
23
+ }
24
+ chunks.push(next.value);
25
+ }
26
+ } finally {
27
+ reader.releaseLock();
28
+ }
29
+ return Buffer.concat(chunks).toString("utf8");
30
+ }
31
+
3
32
  export async function postJson(
4
33
  route: ResolvedModel,
5
34
  path: string,
@@ -7,7 +36,9 @@ export async function postJson(
7
36
  apiKey: string,
8
37
  signal?: AbortSignal,
9
38
  extraHeaders?: Record<string, string>,
10
- ): Promise<{ body: unknown; requestId?: string }> {
39
+ errorDetail?: JsonErrorDetail,
40
+ requestIdDetail?: JsonRequestIdDetail,
41
+ ): Promise<{ body: unknown; requestId?: string; costHeader?: string }> {
11
42
  if (signal?.aborted)
12
43
  throw new ClassifierError("aborted", "Classification was cancelled.");
13
44
  const encoded = JSON.stringify(body);
@@ -24,7 +55,15 @@ export async function postJson(
24
55
  const headers = new Headers(route.headers);
25
56
  for (const [key, value] of Object.entries(extraHeaders ?? {}))
26
57
  headers.set(key, value);
27
- if (route.api === "microsoft-system-one") {
58
+ if (route.authMode === "none") {
59
+ // Local runtimes are reached over loopback without an API key.
60
+ } else if (route.authMode === "subscription-key") {
61
+ headers.set("api-subscription-key", apiKey);
62
+ } else if (route.authMode === "google-api-key") {
63
+ headers.set("x-goog-api-key", apiKey);
64
+ } else if (route.authMode === "parallel-api-key") {
65
+ headers.set("x-api-key", apiKey);
66
+ } else if (route.authMode === "api-key") {
28
67
  headers.set("api-key", apiKey);
29
68
  } else {
30
69
  headers.set("authorization", "Bearer " + apiKey);
@@ -39,54 +78,46 @@ export async function postJson(
39
78
  redirect: "error",
40
79
  });
41
80
  if (!response.ok) {
42
- await response.body?.cancel();
81
+ let detail: string | undefined;
82
+ if (errorDetail) {
83
+ try {
84
+ const raw = await readBoundedText(response);
85
+ if (raw) detail = errorDetail(response.status, JSON.parse(raw), apiKey);
86
+ } catch {
87
+ // Keep the generic error when an error body is absent, oversized or not JSON.
88
+ }
89
+ } else {
90
+ await response.body?.cancel();
91
+ }
43
92
  throw new ClassifierError(
44
93
  "http",
45
94
  "Classifier request failed (HTTP " +
46
95
  response.status +
47
- "). Check credentials, model, limits and provider availability.",
96
+ "). Check credentials, model, limits and provider availability." +
97
+ (detail ? " " + detail : ""),
48
98
  response.status,
49
99
  );
50
100
  }
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
- }
101
+ const raw = await readBoundedText(response);
76
102
  let parsed: unknown;
77
103
  try {
78
- parsed = JSON.parse(Buffer.concat(chunks).toString("utf8"));
104
+ parsed = JSON.parse(raw);
79
105
  } catch {
80
106
  throw new ClassifierError(
81
107
  "response",
82
108
  "Classifier returned invalid JSON.",
83
109
  );
84
110
  }
85
- const result: { body: unknown; requestId?: string } = { body: parsed };
111
+ const result: { body: unknown; requestId?: string; costHeader?: string } = { body: parsed };
86
112
  const id =
87
113
  response.headers.get("x-request-id") ??
88
114
  response.headers.get("request-id");
89
- if (id && /^[a-zA-Z0-9_.:-]{1,128}$/.test(id)) result.requestId = id;
115
+ if (id && /^[a-zA-Z0-9_.:-]{1,128}$/.test(id)) {
116
+ const safeId = requestIdDetail ? requestIdDetail(id, apiKey) : id;
117
+ if (safeId !== undefined) result.requestId = safeId;
118
+ }
119
+ const cost = response.headers.get("x-litellm-response-cost");
120
+ if (cost && /^\d+(?:\.\d+)?(?:e[+-]?\d+)?$/i.test(cost)) result.costHeader = cost;
90
121
  return result;
91
122
  } catch (error) {
92
123
  if (controller.signal.aborted)
package/src/index.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export { classify, compareClassifiers } from "./classify.js";
2
+ export { extract } from "./extract.js";
3
+ export type { ExtractOptions } from "./extract.js";
2
4
  export type { ClassifyOptions } from "./classify.js";
3
5
  export type { MemoryImage } from "./inputs.js";
4
6
  export { runComputerUse } from "./computer-use.js";
@@ -31,5 +33,18 @@ export type {
31
33
  ModelDefinition,
32
34
  ProviderSettings,
33
35
  ApiStyle,
36
+ MediaInput,
37
+ PreparedMedia,
38
+ TextSpan,
39
+ BoxEdges,
40
+ CharSpan,
41
+ NonTokenCost,
42
+ WebSearchResult,
43
+ WebFetchedResult,
44
+ ExtractionField,
45
+ ExtractParams,
46
+ ExtractResult,
47
+ ExtractionAnswer,
48
+ JsonLike,
34
49
  } from "./types.js";
35
50
  export { ClassifierError } from "./types.js";