@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/dist/box-tool.js CHANGED
@@ -1,44 +1,14 @@
1
1
  import { defineTool, } from "@earendil-works/pi-coding-agent";
2
- import { Type } from "typebox";
3
2
  import { renderClassifierBox, } from "./box.js";
3
+ import { CLASSIFIER_BOX_TOOL, CLASSIFIER_BOX_TOOL_DESCRIPTION, boxOutput, boxParameters, } from "./tool-contracts.js";
4
4
  import { ClassifierError, userError } from "./types.js";
5
- export const boxParameters = Type.Object({
6
- image: Type.String({
7
- minLength: 1,
8
- maxLength: 4096,
9
- description: "Local PNG, JPEG or WebP file inside cwd. No URLs, symlinks or inline base64.",
10
- }),
11
- box: Type.Object({
12
- top: Type.Number({ minimum: 0, maximum: 1 }),
13
- bottom: Type.Number({ minimum: 0, maximum: 1 }),
14
- left: Type.Number({ minimum: 0, maximum: 1 }),
15
- right: Type.Number({ minimum: 0, maximum: 1 }),
16
- }, {
17
- additionalProperties: false,
18
- description: "Normalized edges between 0 and 1, with top < bottom and left < right.",
19
- }),
20
- mode: Type.Optional(Type.Union([Type.Literal("overlay"), Type.Literal("crop")], {
21
- description: "overlay draws the box on the image (default); crop returns only the boxed region.",
22
- })),
23
- }, { additionalProperties: false });
24
- export const boxOutput = Type.Object({
25
- mode: Type.Union([Type.Literal("overlay"), Type.Literal("crop")]),
26
- mimeType: Type.Literal("image/png"),
27
- width: Type.Integer({ minimum: 1 }),
28
- height: Type.Integer({ minimum: 1 }),
29
- pixelBox: Type.Object({
30
- left: Type.Integer({ minimum: 0 }),
31
- top: Type.Integer({ minimum: 0 }),
32
- width: Type.Integer({ minimum: 1 }),
33
- height: Type.Integer({ minimum: 1 }),
34
- }),
35
- byteLength: Type.Integer({ minimum: 1 }),
36
- });
5
+ // Re-exported so existing deep imports from "./box-tool.js" keep working.
6
+ export { boxOutput, boxParameters };
37
7
  export function registerBoxTool(pi) {
38
8
  pi.registerTool(defineTool({
39
- name: "classifier_box_image",
9
+ name: CLASSIFIER_BOX_TOOL,
40
10
  label: "Box image",
41
- description: "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.",
11
+ description: CLASSIFIER_BOX_TOOL_DESCRIPTION,
42
12
  promptSnippet: "Render or crop a local image using normalized box coordinates.",
43
13
  promptGuidelines: [
44
14
  "Use classifier_box_image only for local images inside cwd and normalized boxes between 0 and 1.",
package/dist/choices.js CHANGED
@@ -12,6 +12,28 @@ export function choicePolicy(settings) {
12
12
  return { none, batch };
13
13
  }
14
14
  const NONE_DESCRIPTION = "None of the other options in this question applies to the supplied state. Select this only when every listed real option is unsuitable.";
15
+ /**
16
+ * Each provider call returns its own web evidence. `searchResult` indexes that call's array,
17
+ * so spans must be rebased onto the accumulated evidence assembled by this orchestrator.
18
+ */
19
+ function remapGroundedAnswers(answers, offset) {
20
+ if (offset === 0)
21
+ return answers;
22
+ const mapped = Object.create(null);
23
+ for (const [id, answer] of Object.entries(answers)) {
24
+ if (answer.type !== "locate" || !answer.spans.some((span) => span.searchResult !== undefined)) {
25
+ mapped[id] = answer;
26
+ continue;
27
+ }
28
+ mapped[id] = {
29
+ ...answer,
30
+ spans: answer.spans.map((span) => span.searchResult === undefined
31
+ ? span
32
+ : { ...span, searchResult: span.searchResult + offset }),
33
+ };
34
+ }
35
+ return mapped;
36
+ }
15
37
  /** Only this operation owns batching. Transport, images, credentials and routes stay with the caller. */
16
38
  export async function executeChoices(input, route, policy, call, signal) {
17
39
  const plans = new Map();
@@ -42,6 +64,7 @@ export async function executeChoices(input, route, policy, call, signal) {
42
64
  let last;
43
65
  let usage;
44
66
  let cost;
67
+ let webGrounding;
45
68
  while (plans.size > 0) {
46
69
  if (signal?.aborted)
47
70
  throw new ClassifierError("aborted", "Classification was cancelled.");
@@ -79,9 +102,18 @@ export async function executeChoices(input, route, policy, call, signal) {
79
102
  }
80
103
  usage = calls === 0 ? result.usage : sumUsage(usage, result.usage);
81
104
  cost = cost === undefined ? result.cost : sumCost(cost, result.cost);
105
+ const callAnswers = remapGroundedAnswers(result.answers, webGrounding?.searchResults.length ?? 0);
106
+ if (result.webGrounding !== undefined) {
107
+ webGrounding ??= { searchResults: [] };
108
+ webGrounding.searchResults.push(...result.webGrounding.searchResults);
109
+ if (result.webGrounding.fetchedResults?.length) {
110
+ webGrounding.fetchedResults ??= [];
111
+ webGrounding.fetchedResults.push(...result.webGrounding.fetchedResults);
112
+ }
113
+ }
82
114
  calls++;
83
115
  last = result;
84
- for (const [id, answer] of Object.entries(result.answers)) {
116
+ for (const [id, answer] of Object.entries(callAnswers)) {
85
117
  const plan = plans.get(id);
86
118
  if (!plan) {
87
119
  answers[id] = answer;
@@ -125,6 +157,8 @@ export async function executeChoices(input, route, policy, call, signal) {
125
157
  };
126
158
  if (usage !== undefined)
127
159
  combined.usage = usage;
160
+ if (webGrounding !== undefined)
161
+ combined.webGrounding = webGrounding;
128
162
  if (calls === 1 && last.requestId !== undefined)
129
163
  combined.requestId = last.requestId;
130
164
  return combined;
@@ -1,9 +1,12 @@
1
+ import { type CredentialSource } from "./auth.js";
1
2
  import { type MemoryImage } from "./inputs.js";
2
3
  import { type ClassifierComparisonResult, type ClassifierResult, type ClassifierSettings } from "./types.js";
3
4
  export type ClassifyOptions = {
4
5
  settings: ClassifierSettings;
5
6
  cwd: string;
6
7
  signal?: AbortSignal;
8
+ /** Credential origin. Desktop keeps the default; the native Durable factory passes "explicit". */
9
+ credentialSource?: CredentialSource;
7
10
  /** Trusted programmatic ingress; all bytes still pass normal image validation. */
8
11
  memoryImages?: readonly MemoryImage[];
9
12
  };
package/dist/classify.js CHANGED
@@ -20,7 +20,7 @@ async function classifyOnce(input, route, options) {
20
20
  checkCancelled(options.signal);
21
21
  const prepared = await prepareInput(input, route, options.cwd, options.signal, options.memoryImages);
22
22
  checkCancelled(options.signal);
23
- const key = resolveApiKey(route);
23
+ const key = resolveApiKey(route, options.credentialSource ?? "pi-store");
24
24
  const result = await executeChoices(prepared, route, choicePolicy(options.settings), (questions) => classifyWithProvider(route, { ...prepared, questions }, key, options.signal), options.signal);
25
25
  if (Buffer.byteLength(JSON.stringify(result, null, 2)) > 40 * 1024)
26
26
  throw new ClassifierError("response", "Classifier result exceeds the 40 KiB tool output limit. Ask fewer questions.");
@@ -44,7 +44,7 @@ export async function classify(params, options) {
44
44
  let lastError;
45
45
  for (const candidate of candidates) {
46
46
  checkCancelled(options.signal);
47
- const route = resolveModel(options.settings, candidate);
47
+ const route = resolveModel(options.settings, candidate, options.credentialSource ?? "pi-store");
48
48
  const key = routeKey(route);
49
49
  if (seen.has(key))
50
50
  continue;
@@ -85,7 +85,7 @@ export async function compareClassifiers(params, options) {
85
85
  const start = performance.now();
86
86
  let selected = candidate;
87
87
  try {
88
- const route = resolveModel(options.settings, candidate);
88
+ const route = resolveModel(options.settings, candidate, options.credentialSource ?? "pi-store");
89
89
  selected = routePair(route);
90
90
  const key = routeKey(route);
91
91
  if (seen.has(key))
package/dist/config.d.ts CHANGED
@@ -1,6 +1,40 @@
1
+ import { type CredentialSource } from "./auth.js";
1
2
  import { type ClassifierSettings, type ClassifierRoute, type ResolvedModel } from "./types.js";
2
3
  export declare const SETTINGS_KEY = "pi-classifier";
3
4
  export declare const BUILT_INS: ({
5
+ id: string;
6
+ route: string;
7
+ api: "local-system-one";
8
+ baseUrl: string;
9
+ credentialProvider: string;
10
+ envVar: string;
11
+ models: {
12
+ id: string;
13
+ input: ("audio" | "video" | "text" | "image")[];
14
+ }[];
15
+ } | {
16
+ id: string;
17
+ route: string;
18
+ api: "openrouter-extractions";
19
+ baseUrl: string;
20
+ credentialProvider: string;
21
+ envVar: string;
22
+ models: {
23
+ id: string;
24
+ input: ("text" | "image")[];
25
+ }[];
26
+ } | {
27
+ id: string;
28
+ route: string;
29
+ api: "litellm-extraction";
30
+ baseUrl: string;
31
+ credentialProvider: string;
32
+ envVar: string;
33
+ models: {
34
+ id: string;
35
+ input: ("text" | "image")[];
36
+ }[];
37
+ } | {
4
38
  id: string;
5
39
  route: string;
6
40
  api: "microsoft-system-one";
@@ -9,7 +43,18 @@ export declare const BUILT_INS: ({
9
43
  envVar: string;
10
44
  models: {
11
45
  id: string;
12
- input: "text"[];
46
+ input: ("text" | "image")[];
47
+ }[];
48
+ } | {
49
+ id: string;
50
+ route: string;
51
+ api: "sarvam-system-one";
52
+ baseUrl: string;
53
+ credentialProvider: string;
54
+ envVar: string;
55
+ models: {
56
+ id: string;
57
+ input: ("text" | "image")[];
13
58
  }[];
14
59
  } | {
15
60
  id: string;
@@ -22,6 +67,39 @@ export declare const BUILT_INS: ({
22
67
  id: string;
23
68
  input: ("text" | "image")[];
24
69
  }[];
70
+ } | {
71
+ id: string;
72
+ route: string;
73
+ api: "gemini-decisions";
74
+ baseUrl: string;
75
+ credentialProvider: string;
76
+ envVar: string;
77
+ models: {
78
+ id: string;
79
+ input: ("audio" | "video" | "text" | "image")[];
80
+ }[];
81
+ } | {
82
+ id: string;
83
+ route: string;
84
+ api: "inception-decisions";
85
+ baseUrl: string;
86
+ credentialProvider: string;
87
+ envVar: string;
88
+ models: {
89
+ id: string;
90
+ input: ("text" | "image")[];
91
+ }[];
92
+ } | {
93
+ id: string;
94
+ route: string;
95
+ api: "parallel-decisions";
96
+ baseUrl: string;
97
+ credentialProvider: string;
98
+ envVar: string;
99
+ models: {
100
+ id: string;
101
+ input: "text"[];
102
+ }[];
25
103
  } | {
26
104
  id: string;
27
105
  route: string;
@@ -33,6 +111,18 @@ export declare const BUILT_INS: ({
33
111
  id: string;
34
112
  input: ("text" | "image")[];
35
113
  }[];
114
+ } | {
115
+ id: string;
116
+ route: string;
117
+ api: "liquid-system-one";
118
+ baseUrl: string;
119
+ credentialProvider: string;
120
+ envVar: string;
121
+ models: {
122
+ id: string;
123
+ input: ("audio" | "text" | "image")[];
124
+ box: true;
125
+ }[];
36
126
  } | {
37
127
  id: string;
38
128
  route: string;
@@ -58,11 +148,22 @@ export declare const BUILT_INS: ({
58
148
  envVar: string;
59
149
  models: ({
60
150
  id: string;
61
- input: ("text" | "image")[];
151
+ input: ("audio" | "video" | "text" | "image")[];
62
152
  } | {
63
153
  id: string;
64
154
  input?: never;
65
155
  })[];
156
+ } | {
157
+ id: string;
158
+ route: string;
159
+ api: "cloudflare-clairic";
160
+ baseUrl: string;
161
+ credentialProvider: string;
162
+ envVar: string;
163
+ models: {
164
+ id: string;
165
+ input: ("text" | "image")[];
166
+ }[];
66
167
  } | {
67
168
  id: string;
68
169
  route: string;
@@ -73,10 +174,46 @@ export declare const BUILT_INS: ({
73
174
  models: {
74
175
  id: string;
75
176
  }[];
177
+ } | {
178
+ id: string;
179
+ route: string;
180
+ api: "openrouter-decisions";
181
+ baseUrl: string;
182
+ credentialProvider: string;
183
+ envVar: string;
184
+ models: ({
185
+ id: string;
186
+ input: ("text" | "image")[];
187
+ box: true;
188
+ } | {
189
+ id: string;
190
+ input: ("audio" | "video" | "text" | "image")[];
191
+ box?: never;
192
+ })[];
76
193
  })[];
194
+ export type OpenRouterExtractionCapabilities = {
195
+ fieldTypes: string[];
196
+ maxFields: number;
197
+ maxImages: number;
198
+ maxDocuments: number;
199
+ maxAssets: number;
200
+ referenceDate: boolean;
201
+ };
202
+ /** Field tiers from OpenRouter's Extractions guide; unknown models default to text-only. */
203
+ export declare function openRouterExtractionCapabilities(model: string): OpenRouterExtractionCapabilities;
204
+ export type OpenRouterDecisionCapabilities = {
205
+ flexibleImagePlacement: boolean;
206
+ box: boolean;
207
+ remoteImageUrls: boolean;
208
+ images: number;
209
+ audio: number;
210
+ video: number;
211
+ };
212
+ /** Defaults come from OpenRouter's published Decisions adapter registry and multimodal limits. */
213
+ export declare function openRouterDecisionCapabilities(model: string): OpenRouterDecisionCapabilities;
77
214
  export declare function loadClassifierSettings(cwd: string, projectTrusted?: boolean): ClassifierSettings;
78
215
  export declare function validateBaseUrl(raw: string): string;
79
216
  export declare function configuredDefault(settings: ClassifierSettings): ClassifierRoute;
80
217
  export declare function configuredRoutes(settings: ClassifierSettings, field: "fallback" | "compare"): ClassifierRoute[];
81
- export declare function resolveModel(settings: ClassifierSettings, selected?: ClassifierRoute): ResolvedModel;
218
+ export declare function resolveModel(settings: ClassifierSettings, selected?: ClassifierRoute, source?: CredentialSource): ResolvedModel;
82
219
  export declare function listModels(settings: ClassifierSettings): string;