@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.
Files changed (77) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/COMPUTER_USE.md +117 -0
  3. package/README.md +344 -30
  4. package/dist/auth.d.ts +4 -2
  5. package/dist/auth.js +18 -2
  6. package/dist/box-tool.d.ts +4 -0
  7. package/dist/box-tool.js +79 -0
  8. package/dist/box.d.ts +32 -0
  9. package/dist/box.js +148 -0
  10. package/dist/choices.js +35 -1
  11. package/dist/classify.d.ts +6 -0
  12. package/dist/classify.js +4 -4
  13. package/dist/computer-use-state.d.ts +9 -0
  14. package/dist/computer-use-state.js +160 -0
  15. package/dist/computer-use-tool.d.ts +52 -0
  16. package/dist/computer-use-tool.js +78 -0
  17. package/dist/computer-use-types.d.ts +132 -0
  18. package/dist/computer-use-types.js +1 -0
  19. package/dist/computer-use.d.ts +22 -0
  20. package/dist/computer-use.js +223 -0
  21. package/dist/config.d.ts +140 -3
  22. package/dist/config.js +405 -20
  23. package/dist/cost.js +42 -4
  24. package/dist/durable.d.ts +34 -0
  25. package/dist/durable.js +237 -0
  26. package/dist/extension.d.ts +2 -247
  27. package/dist/extension.js +53 -190
  28. package/dist/extract.d.ts +11 -0
  29. package/dist/extract.js +191 -0
  30. package/dist/http.d.ts +6 -1
  31. package/dist/http.js +64 -28
  32. package/dist/image-content.d.ts +12 -0
  33. package/dist/image-content.js +90 -0
  34. package/dist/index.d.ts +10 -1
  35. package/dist/index.js +3 -0
  36. package/dist/inputs.d.ts +24 -2
  37. package/dist/inputs.js +565 -134
  38. package/dist/mcp-cli.d.ts +2 -0
  39. package/dist/mcp-cli.js +39 -0
  40. package/dist/mcp-server.d.ts +28 -0
  41. package/dist/mcp-server.js +121 -0
  42. package/dist/mcp-tool-definitions.d.ts +19 -0
  43. package/dist/mcp-tool-definitions.js +64 -0
  44. package/dist/providers.d.ts +1 -0
  45. package/dist/providers.js +1218 -124
  46. package/dist/tool-contracts.d.ts +785 -0
  47. package/dist/tool-contracts.js +530 -0
  48. package/dist/tree.js +1 -1
  49. package/dist/types.d.ts +314 -20
  50. package/dist/validation.js +181 -44
  51. package/package.json +33 -5
  52. package/src/auth.ts +26 -2
  53. package/src/box-tool.ts +110 -0
  54. package/src/box.ts +253 -0
  55. package/src/choices.ts +44 -2
  56. package/src/classify.ts +10 -6
  57. package/src/computer-use-state.ts +145 -0
  58. package/src/computer-use-tool.ts +76 -0
  59. package/src/computer-use-types.ts +127 -0
  60. package/src/computer-use.ts +216 -0
  61. package/src/config.ts +454 -23
  62. package/src/cost.ts +42 -4
  63. package/src/durable.ts +298 -0
  64. package/src/extension.ts +79 -265
  65. package/src/extract.ts +228 -0
  66. package/src/http.ts +63 -32
  67. package/src/image-content.ts +77 -0
  68. package/src/index.ts +25 -0
  69. package/src/inputs.ts +603 -184
  70. package/src/mcp-cli.ts +44 -0
  71. package/src/mcp-server.ts +179 -0
  72. package/src/mcp-tool-definitions.ts +104 -0
  73. package/src/providers.ts +1398 -144
  74. package/src/tool-contracts.ts +785 -0
  75. package/src/tree.ts +1 -1
  76. package/src/types.ts +248 -11
  77. package/src/validation.ts +169 -96
package/src/tree.ts CHANGED
@@ -20,7 +20,7 @@ export async function classifyTree(
20
20
  ): Promise<ClassifierTreeResult> {
21
21
  choicePolicy(options.settings);
22
22
  const input = await loadTreeInput(params, options.cwd, options.signal);
23
- const initial = resolveModel(options.settings);
23
+ const initial = resolveModel(options.settings, undefined, options.credentialSource ?? "pi-store");
24
24
  const result: ClassifierTreeResult = {
25
25
  provider: initial.route,
26
26
  model: initial.model,
package/src/types.ts CHANGED
@@ -1,19 +1,101 @@
1
1
  export type ApiStyle =
2
2
  | "openai-decisions"
3
+ | "openrouter-decisions"
4
+ | "gemini-decisions"
5
+ | "inception-decisions"
6
+ | "parallel-decisions"
7
+ | "local-system-one"
3
8
  | "perplexity-decisions"
9
+ | "sarvam-system-one"
4
10
  | "system-one"
11
+ | "liquid-system-one"
5
12
  | "cloudflare-system-one"
13
+ | "cloudflare-clairic"
14
+ | "openrouter-extractions"
15
+ | "litellm-extraction"
6
16
  | "microsoft-system-one";
7
17
 
18
+ /** Explicit references are resolved only from the request's named images. */
19
+ export type ImageReference = { $image: string };
20
+ export type QuestionContent = string | (string | ImageReference)[];
21
+ export type ImageInput =
22
+ | string
23
+ | { id: string; path: string }
24
+ | { id: string; url: string };
25
+
26
+ export type MediaInput = { type: "audio" | "video"; path: string };
27
+ export type CharSpan = [number, number];
28
+ export type BoxEdges = { top: number; bottom: number; left: number; right: number };
29
+
8
30
  export type Question =
9
31
  | {
10
32
  type: "bool";
11
- instructions: string;
12
- criteria?: { true?: string; false?: string };
33
+ instructions: QuestionContent;
34
+ criteria?: { true?: QuestionContent; false?: QuestionContent };
13
35
  }
14
- | { type: "choice"; instructions: string; criteria: Record<string, string> }
15
- | { type: "score"; instructions: string; criteria: string[] }
16
- | { type: "box"; instructions: string };
36
+ | { type: "choice"; instructions: QuestionContent; criteria: Record<string, QuestionContent>; booleanValues?: boolean }
37
+ | { type: "score"; instructions: QuestionContent; criteria: QuestionContent[]; descriptions?: string[] }
38
+ | { type: "extract"; instructions?: QuestionContent; criteria: Record<string, QuestionContent>; source?: string; threshold?: number; snapToWords?: boolean; debug?: boolean }
39
+ | { type: "box"; instructions: QuestionContent }
40
+ | { type: "locate"; instructions: QuestionContent }
41
+ | { type: "text"; instructions: QuestionContent }
42
+ | { type: "number"; instructions: QuestionContent; unit?: string; range?: CharSpan; step?: number }
43
+ | { type: "date"; instructions: QuestionContent }
44
+ | { type: "spans"; instructions: QuestionContent; max?: number }
45
+ | { type: "boxes"; instructions: QuestionContent; max?: number }
46
+ | { type: "estimate"; instructions: QuestionContent; range: CharSpan; step?: number; unit?: string }
47
+ | { type: "multi_choice"; instructions: QuestionContent; criteria: Record<string, QuestionContent> };
48
+
49
+ export type TextSpan = {
50
+ start: number;
51
+ end: number;
52
+ text: string;
53
+ score: number;
54
+ /** Index into webGrounding.searchResults when the span came from search evidence. */
55
+ searchResult?: number;
56
+ };
57
+
58
+ export type WebSearchResult = {
59
+ title: string;
60
+ url: string;
61
+ snippet: string;
62
+ date?: string;
63
+ lastUpdated?: string;
64
+ };
65
+
66
+ export type WebFetchedResult = {
67
+ title: string;
68
+ url: string;
69
+ snippet: string;
70
+ };
71
+
72
+ export type DecisionCitation = {
73
+ url: string;
74
+ title?: string;
75
+ excerpts?: string[];
76
+ };
77
+
78
+ export type NonTokenCost = {
79
+ currency: "USD";
80
+ webGrounding: {
81
+ status: "estimated_maximum";
82
+ /** One search is requested per question; duplicate queries and failed searches may not be billed. */
83
+ searchesRequested: number;
84
+ searchFeeMaximum: number;
85
+ /** A successful URL fetch is reported by the provider and is billable. */
86
+ successfulFetches: number;
87
+ fetchFee: number;
88
+ totalMaximum: number;
89
+ note: string;
90
+ };
91
+ };
92
+
93
+ export type ProviderCost = {
94
+ currency: "USD";
95
+ total: number;
96
+ source: "provider" | "gateway";
97
+ note?: string;
98
+ };
17
99
 
18
100
  export type Answer =
19
101
  | { type: "bool"; probability: number }
@@ -34,9 +116,31 @@ export type Answer =
34
116
  }
35
117
  | {
36
118
  type: "box";
37
- box: { top: number; bottom: number; left: number; right: number };
119
+ box: BoxEdges;
38
120
  confidence: number;
121
+ found?: number;
122
+ }
123
+ | { type: "locate"; spans: TextSpan[] }
124
+ | { type: "extract"; spans: Record<string, TextSpan[]>; notStated: Record<string, number> }
125
+ | { type: "text"; text: string; span?: CharSpan | null; found?: number; path?: string }
126
+ | { type: "number"; value: number | null; unit?: string | null; text?: string | null; span?: CharSpan | null; found?: number; path?: string }
127
+ | { type: "date"; date: string | null; text?: string | null; span?: CharSpan | null; found?: number; path?: string }
128
+ | {
129
+ type: "spans";
130
+ spans: { text: string; span: CharSpan; found: number }[];
131
+ count: number;
132
+ atLeast: boolean;
133
+ presence: number[];
134
+ }
135
+ | {
136
+ type: "boxes";
137
+ boxes: { box: BoxEdges; found: number; confidence: number }[];
138
+ count: number;
139
+ atLeast: boolean;
140
+ presence: number[];
39
141
  }
142
+ | { type: "estimate"; value: number; interval: CharSpan; unit?: string | null }
143
+ | { type: "multi_choice"; choices: string[]; probabilities: Record<string, number> }
40
144
  | { type: "refusal" }
41
145
  | { type: "no_match" }
42
146
  | { type: "ambiguous" };
@@ -44,8 +148,23 @@ export type Answer =
44
148
  export interface ClassifyParams {
45
149
  state: string | Record<string, unknown> | unknown[];
46
150
  questions: Record<string, Question>;
47
- /** Regular PNG/JPEG/WebP files inside cwd; attached in this order. */
48
- images?: string[];
151
+ /** Legacy paths are shared in order. Named images require explicit {$image:id} references. */
152
+ images?: ImageInput[];
153
+ /** Shared audio/video inputs for providers that document media support. */
154
+ media?: MediaInput[];
155
+ /** OpenAI Decisions input items: messages, function calls/outputs and custom tool calls/outputs. */
156
+ conversation?: unknown[];
157
+ /** Explicit opt-in to Perplexity web search and page fetching. This can upload state text and incur per-search fees. */
158
+ mode?: "web_grounding";
159
+ /** Image detail hint for adapters that publish one; ignored by routes without a documented field. */
160
+ imageDetail?: "auto" | "low" | "high" | "original";
161
+ /** OpenAI-compatible prompt-cache controls; rejected by routes without a documented contract. */
162
+ promptCache?: {
163
+ key?: string;
164
+ mode?: "implicit" | "explicit";
165
+ ttl?: "30m";
166
+ breakpoints?: boolean;
167
+ };
49
168
  }
50
169
 
51
170
  export type ClassificationTreeNode = {
@@ -60,7 +179,7 @@ export type ClassifyTreeParams = {
60
179
  state: ClassifyParams["state"];
61
180
  tree: string | ClassificationTree;
62
181
  instructions?: string;
63
- images?: string[];
182
+ images?: ImageInput[];
64
183
  };
65
184
 
66
185
  export type TreeStep = {
@@ -87,7 +206,7 @@ export type ClassifierTreeResult = {
87
206
  export interface ModelDefinition {
88
207
  id: string;
89
208
  name?: string;
90
- input?: ("text" | "image")[];
209
+ input?: ("text" | "image" | "audio" | "video")[];
91
210
  box?: boolean;
92
211
  }
93
212
 
@@ -108,6 +227,7 @@ export type ClassifierCost =
108
227
  total: number;
109
228
  source: "builtin" | "configured";
110
229
  rates: TokenPricing;
230
+ nonToken?: NonTokenCost;
111
231
  }
112
232
  | {
113
233
  status: "estimated";
@@ -116,6 +236,7 @@ export type ClassifierCost =
116
236
  output: number;
117
237
  total: number;
118
238
  source: "aggregated";
239
+ nonToken?: NonTokenCost;
119
240
  }
120
241
  | {
121
242
  status: "unavailable";
@@ -128,17 +249,32 @@ export type ClassifierCost =
128
249
  | "calculation_overflow";
129
250
  };
130
251
 
252
+ export type RouteLimits = {
253
+ maxQuestions: number;
254
+ maxImages: number;
255
+ maxAudio: number;
256
+ maxVideo: number;
257
+ };
258
+
131
259
  export interface ProviderSettings {
132
260
  apiKey?: string;
133
261
  baseUrl?: string;
134
262
  headers?: Record<string, string>;
135
263
  accountId?: string;
264
+ /** Cloudflare AI Gateway selection; emitted as `cf-aig-gateway-id`. */
265
+ gatewayId?: string;
136
266
  experimentalImages?: boolean;
137
267
  experimentalBox?: boolean;
268
+ /** Microsoft Foundry accepts API-key or Entra bearer credentials on the same endpoint. */
269
+ authMode?: "api-key" | "bearer" | "google-api-key" | "parallel-api-key" | "none";
270
+ /** TypeSafe Jev long-context opt-in; sends `TypeSafe-Danger-Zone-Long-Context: true`. */
271
+ longContext?: boolean;
138
272
  /** Model ID (or Azure deployment name) to explicit token rates. */
139
273
  pricing?: Record<string, TokenPricing>;
140
274
  /** Verified wire choice limit; needed for batching on endpoints without a known limit. */
141
275
  maxChoices?: number;
276
+ /** Verified per-request envelope overrides for custom or account-specific endpoints. */
277
+ limits?: Partial<RouteLimits>;
142
278
  }
143
279
 
144
280
  export interface CustomProviderSettings extends ProviderSettings {
@@ -152,6 +288,7 @@ export type ClassifierRoute = {
152
288
  };
153
289
 
154
290
  export interface ClassifierSettings {
291
+ computerUse?: import("./computer-use-types.js").ComputerUseSettings;
155
292
  none_of_above_choice?: boolean;
156
293
  remove_choice_limits?: boolean;
157
294
  default?: ClassifierRoute;
@@ -169,30 +306,52 @@ export interface ResolvedModel {
169
306
  provider: string;
170
307
  model: string;
171
308
  api: ApiStyle;
309
+ /** Provider wire format used by the selected route. */
310
+ mediaFormat: "jev-assets" | "base64-object" | "openrouter-parts";
172
311
  baseUrl: string;
173
312
  headers: Record<string, string>;
313
+ gatewayId?: string;
174
314
  configuredApiKey?: string;
175
315
  credentialProvider: string;
176
316
  envVar?: string;
177
317
  images: boolean;
318
+ audio: boolean;
319
+ video: boolean;
178
320
  box: boolean;
179
321
  experimentalImages: boolean;
322
+ authMode: "api-key" | "bearer" | "subscription-key" | "google-api-key" | "parallel-api-key" | "none";
323
+ longContext: boolean;
180
324
  requestTimeoutMs: number;
325
+ /** Envelope used before a request; values come from verified route contracts or explicit settings. */
326
+ limits: RouteLimits;
181
327
  pricing?: { rates: TokenPricing; source: "builtin" | "configured" };
182
328
  maxChoices?: number;
183
329
  }
184
330
 
185
- export interface PreparedImage {
331
+ export interface PreparedLocalImage {
186
332
  data: string;
187
333
  mimeType: "image/png" | "image/jpeg" | "image/webp";
188
334
  width: number;
189
335
  height: number;
190
336
  }
191
337
 
338
+ export type PreparedImage = (PreparedLocalImage | { url: string }) & { id?: string };
339
+
340
+ export type PreparedMedia = {
341
+ type: "audio" | "video";
342
+ mimeType: string;
343
+ data: string;
344
+ };
345
+
192
346
  export interface PreparedInput {
193
347
  state: ClassifyParams["state"];
194
348
  questions: Record<string, Question>;
195
349
  images: PreparedImage[];
350
+ media: PreparedMedia[];
351
+ mode?: ClassifyParams["mode"];
352
+ conversation?: ClassifyParams["conversation"];
353
+ imageDetail?: ClassifyParams["imageDetail"];
354
+ promptCache?: ClassifyParams["promptCache"];
196
355
  }
197
356
 
198
357
  export type ClassifierResult = {
@@ -210,7 +369,16 @@ export type ClassifierResult = {
210
369
  providerCalls?: number;
211
370
  /** Estimate for completed requests in this result, not an invoice or total of failed attempts. */
212
371
  cost: ClassifierCost;
372
+ /** Gateway/provider-reported charge when the API exposes one; never substituted for the token estimate. */
373
+ providerCost?: ProviderCost;
374
+ /** Request-wide web evidence returned by grounded Decision APIs such as Parallel. */
375
+ citations?: DecisionCitation[];
213
376
  requestId?: string;
377
+ /** Evidence returned by Perplexity web grounding, with the same order and counts as the provider response. */
378
+ webGrounding?: {
379
+ searchResults: WebSearchResult[];
380
+ fetchedResults?: WebFetchedResult[];
381
+ };
214
382
  attemptedRoutes?: ClassifierRoute[];
215
383
  };
216
384
 
@@ -246,3 +414,72 @@ export function userError(error: unknown): string {
246
414
  ? error.message
247
415
  : "Classification failed unexpectedly. Check the selected provider configuration.";
248
416
  }
417
+
418
+ /** Cloudflare Clairic field specifications. Keys use the provider's documented names. */
419
+ export type ExtractionField = {
420
+ type:
421
+ | "text" | "number" | "compute" | "date" | "time" | "datetime" | "duration"
422
+ | "boolean" | "enum" | "object" | "list" | "estimate" | "forecast";
423
+ instructions?: string;
424
+ nullable?: boolean;
425
+ unit?: string;
426
+ integer?: boolean;
427
+ decimals?: number;
428
+ minimum?: number;
429
+ maximum?: number;
430
+ exclusiveMinimum?: number;
431
+ exclusiveMaximum?: number;
432
+ minLength?: number;
433
+ maxLength?: number;
434
+ pattern?: string;
435
+ enum?: unknown[];
436
+ knowledge?: boolean;
437
+ source_only?: boolean;
438
+ null_threshold?: number;
439
+ properties?: Record<string, ExtractionField>;
440
+ items?: "text" | "number" | "date" | { type: "object"; properties: Record<string, ExtractionField> };
441
+ max_items?: number;
442
+ min?: number | string;
443
+ max?: number | string;
444
+ step?:
445
+ | number
446
+ | "minute" | "hour" | "day" | "week" | "month" | "quarter" | "year";
447
+ scale?: "linear" | "log";
448
+ horizon?: number;
449
+ interval?: "step" | "minute" | "hour" | "day" | "week" | "month" | "quarter" | "year";
450
+ };
451
+
452
+ export type ExtractParams = {
453
+ state?: ClassifyParams["state"];
454
+ fields: Record<string, ExtractionField>;
455
+ images?: (string | { path: string; rotation?: 0 | 90 | 180 | 270 })[];
456
+ documents?: string[];
457
+ referenceDate?: string;
458
+ options?: { includeQuantiles?: boolean; returnStateText?: boolean };
459
+ };
460
+
461
+ export type JsonLike =
462
+ | string
463
+ | number
464
+ | boolean
465
+ | null
466
+ | JsonLike[]
467
+ | { [key: string]: JsonLike };
468
+
469
+ export type ExtractionAnswer = {
470
+ type: string;
471
+ value: JsonLike;
472
+ [key: string]: JsonLike;
473
+ };
474
+
475
+ export type ExtractResult = {
476
+ provider: string;
477
+ model: string;
478
+ answers: Record<string, ExtractionAnswer>;
479
+ truncated: boolean;
480
+ usage?: NonNullable<ClassifierResult["usage"]>;
481
+ cost: ClassifierCost;
482
+ providerCost?: ProviderCost;
483
+ requestId?: string;
484
+ stateText?: string;
485
+ };