@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/config.ts
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
loadPiSettings,
|
|
6
|
+
resolveConfigDir,
|
|
7
|
+
} from "@amaster.ai/pi-shared/settings";
|
|
8
|
+
import { cloudflareAccountId } from "./auth.js";
|
|
9
|
+
import {
|
|
10
|
+
ClassifierError,
|
|
11
|
+
isRecord,
|
|
12
|
+
type ApiStyle,
|
|
13
|
+
type ClassifierSettings,
|
|
14
|
+
type ClassifierRoute,
|
|
15
|
+
type ModelDefinition,
|
|
16
|
+
type ProviderSettings,
|
|
17
|
+
type ResolvedModel,
|
|
18
|
+
} from "./types.js";
|
|
19
|
+
|
|
20
|
+
export const SETTINGS_KEY = "pi-classifier";
|
|
21
|
+
export const BUILT_INS = [
|
|
22
|
+
{
|
|
23
|
+
id: "microsoft",
|
|
24
|
+
route: "microsoft-api",
|
|
25
|
+
api: "microsoft-system-one",
|
|
26
|
+
baseUrl: "",
|
|
27
|
+
credentialProvider: "microsoft",
|
|
28
|
+
envVar: "AZURE_API_KEY",
|
|
29
|
+
models: [{ id: "Microsoft-Decision-1", input: ["text"] }],
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
id: "openai",
|
|
33
|
+
route: "openai-api",
|
|
34
|
+
api: "openai-decisions",
|
|
35
|
+
baseUrl: "https://api.openai.com/v1",
|
|
36
|
+
credentialProvider: "openai",
|
|
37
|
+
envVar: "OPENAI_API_KEY",
|
|
38
|
+
models: [{ id: "gpt-6-luna", input: ["text", "image"] }],
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
id: "perplexity",
|
|
42
|
+
route: "perplexity-api",
|
|
43
|
+
api: "perplexity-decisions",
|
|
44
|
+
baseUrl: "https://api.perplexity.ai/v1",
|
|
45
|
+
credentialProvider: "perplexity",
|
|
46
|
+
envVar: "PERPLEXITY_API_KEY",
|
|
47
|
+
models: [
|
|
48
|
+
{ id: "pplx-decider-v1.1-27b", input: ["text", "image"] },
|
|
49
|
+
{ id: "pplx-decider-v1-27b", input: ["text", "image"] },
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
id: "typesafe",
|
|
54
|
+
route: "typesafe-api",
|
|
55
|
+
api: "system-one",
|
|
56
|
+
baseUrl: "https://api.typesafe.ai/v1",
|
|
57
|
+
credentialProvider: "typesafe",
|
|
58
|
+
envVar: "TYPESAFE_API_KEY",
|
|
59
|
+
models: [
|
|
60
|
+
{ id: "jev-latest", input: ["text", "image"] },
|
|
61
|
+
{ id: "jev-1.13.0", input: ["text", "image"] },
|
|
62
|
+
{ id: "jev-preview", input: ["text", "image"], box: true },
|
|
63
|
+
{ id: "jev-1.14.0", input: ["text", "image"], box: true },
|
|
64
|
+
],
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
id: "cloudflare",
|
|
68
|
+
route: "cloudflare-api",
|
|
69
|
+
api: "cloudflare-system-one",
|
|
70
|
+
baseUrl: "",
|
|
71
|
+
credentialProvider: "cloudflare-workers-ai",
|
|
72
|
+
envVar: "CLOUDFLARE_API_KEY",
|
|
73
|
+
models: [
|
|
74
|
+
{ id: "@cf/cloudflare/clef", input: ["text", "image"] },
|
|
75
|
+
{ id: "@cf/cloudflare/clef-flash", input: ["text", "image"] },
|
|
76
|
+
{ id: "typesafe/jev" },
|
|
77
|
+
],
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
id: "openrouter",
|
|
81
|
+
route: "openrouter-api",
|
|
82
|
+
api: "system-one",
|
|
83
|
+
baseUrl: "https://openrouter.ai/api/v1",
|
|
84
|
+
credentialProvider: "openrouter",
|
|
85
|
+
envVar: "OPENROUTER_API_KEY",
|
|
86
|
+
models: [{ id: "typesafe/jev-1.13" }, { id: "~typesafe/jev-latest" }],
|
|
87
|
+
},
|
|
88
|
+
] satisfies Array<{
|
|
89
|
+
id: string;
|
|
90
|
+
route: string;
|
|
91
|
+
api: ApiStyle;
|
|
92
|
+
baseUrl: string;
|
|
93
|
+
credentialProvider: string;
|
|
94
|
+
envVar: string;
|
|
95
|
+
models: ModelDefinition[];
|
|
96
|
+
}>;
|
|
97
|
+
|
|
98
|
+
export function loadClassifierSettings(
|
|
99
|
+
cwd: string,
|
|
100
|
+
projectTrusted = false,
|
|
101
|
+
): ClassifierSettings {
|
|
102
|
+
try {
|
|
103
|
+
// The shared loader is forgiving; malformed layers must not silently select a different route.
|
|
104
|
+
const paths = new Set([
|
|
105
|
+
join(resolveConfigDir(join(homedir(), ".pi", "agent")), "settings.json"),
|
|
106
|
+
join(resolveConfigDir(), "settings.json"),
|
|
107
|
+
...(projectTrusted ? [join(cwd, ".pi", "settings.json")] : []),
|
|
108
|
+
]);
|
|
109
|
+
for (const path of paths) validateSettingsLayer(path);
|
|
110
|
+
const settings = loadPiSettings<ClassifierSettings>(SETTINGS_KEY, {
|
|
111
|
+
cwd,
|
|
112
|
+
projectTrusted,
|
|
113
|
+
strictProjectSettings: true,
|
|
114
|
+
});
|
|
115
|
+
if (!isRecord(settings)) throw new Error();
|
|
116
|
+
return settings;
|
|
117
|
+
} catch (error) {
|
|
118
|
+
if (error instanceof ClassifierError) throw error;
|
|
119
|
+
throw new ClassifierError(
|
|
120
|
+
"configuration",
|
|
121
|
+
"Could not load pi-classifier settings. Check that the namespace contains a valid JSON object.",
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function validateSettingsLayer(path: string): void {
|
|
127
|
+
let text: string;
|
|
128
|
+
try {
|
|
129
|
+
text = readFileSync(path, "utf8");
|
|
130
|
+
} catch (error) {
|
|
131
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return;
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
if (text.charCodeAt(0) === 0xfeff)
|
|
135
|
+
configuration(
|
|
136
|
+
"pi-classifier settings require UTF-8 without a BOM. Remove the BOM from settings.json; no alternate configuration was used.",
|
|
137
|
+
);
|
|
138
|
+
const root: unknown = JSON.parse(text);
|
|
139
|
+
if (
|
|
140
|
+
!isRecord(root) ||
|
|
141
|
+
(Object.hasOwn(root, SETTINGS_KEY) && !isRecord(root[SETTINGS_KEY]))
|
|
142
|
+
)
|
|
143
|
+
configuration(
|
|
144
|
+
"Settings and the pi-classifier namespace must be JSON objects, not null or arrays.",
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function configuration(message: string): never {
|
|
149
|
+
throw new ClassifierError("configuration", message);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function validateBaseUrl(raw: string): string {
|
|
153
|
+
try {
|
|
154
|
+
const url = new URL(raw);
|
|
155
|
+
const loopback = ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname);
|
|
156
|
+
if (
|
|
157
|
+
(url.protocol !== "https:" && !(url.protocol === "http:" && loopback)) ||
|
|
158
|
+
url.username ||
|
|
159
|
+
url.password ||
|
|
160
|
+
url.search ||
|
|
161
|
+
url.hash
|
|
162
|
+
)
|
|
163
|
+
throw new Error();
|
|
164
|
+
return url.toString().replace(/\/+$/, "");
|
|
165
|
+
} catch {
|
|
166
|
+
return configuration(
|
|
167
|
+
"Provider baseUrl must be HTTPS (or loopback HTTP), without credentials, query or fragment.",
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function optionalString(value: unknown, field: string): string | undefined {
|
|
173
|
+
if (value === undefined) return undefined;
|
|
174
|
+
if (typeof value !== "string" || !value.trim())
|
|
175
|
+
return configuration(field + " must be a non-empty string.");
|
|
176
|
+
return value.trim();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function headers(value: unknown): Record<string, string> {
|
|
180
|
+
if (value === undefined) return {};
|
|
181
|
+
if (
|
|
182
|
+
!isRecord(value) ||
|
|
183
|
+
Object.entries(value).some(
|
|
184
|
+
([key, entry]) =>
|
|
185
|
+
!/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/.test(key) ||
|
|
186
|
+
typeof entry !== "string" ||
|
|
187
|
+
/[\r\n]/.test(entry),
|
|
188
|
+
)
|
|
189
|
+
) {
|
|
190
|
+
return configuration(
|
|
191
|
+
"Provider headers must be a string map with valid HTTP header names and values.",
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
const protectedNames = [
|
|
195
|
+
"authorization",
|
|
196
|
+
"api-key",
|
|
197
|
+
"content-type",
|
|
198
|
+
"content-length",
|
|
199
|
+
"host",
|
|
200
|
+
"cookie",
|
|
201
|
+
"proxy-authorization",
|
|
202
|
+
"transfer-encoding",
|
|
203
|
+
"connection",
|
|
204
|
+
];
|
|
205
|
+
if (
|
|
206
|
+
Object.values(value).some(
|
|
207
|
+
(entry) => typeof entry === "string" && /\$\{?[A-Z_]/.test(entry),
|
|
208
|
+
)
|
|
209
|
+
)
|
|
210
|
+
return configuration(
|
|
211
|
+
"Resolve header environment references in global settings, not project settings.",
|
|
212
|
+
);
|
|
213
|
+
if (
|
|
214
|
+
Object.keys(value).some((key) => protectedNames.includes(key.toLowerCase()))
|
|
215
|
+
)
|
|
216
|
+
return configuration(
|
|
217
|
+
"Authentication and transport headers cannot be overridden.",
|
|
218
|
+
);
|
|
219
|
+
return value as Record<string, string>;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function configuredDefault(settings: ClassifierSettings): ClassifierRoute {
|
|
223
|
+
if (settings.default !== undefined)
|
|
224
|
+
return routePair(settings.default, "default");
|
|
225
|
+
return routePair(
|
|
226
|
+
{ provider: settings.defaultProvider, model: settings.defaultModel },
|
|
227
|
+
"defaultProvider/defaultModel",
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function routePair(value: unknown, field: string): ClassifierRoute {
|
|
232
|
+
if (!isRecord(value))
|
|
233
|
+
return configuration(field + " must be a provider/model pair.");
|
|
234
|
+
const provider = optionalString(value.provider, field + ".provider");
|
|
235
|
+
const model = optionalString(value.model, field + ".model");
|
|
236
|
+
if (!provider || !model)
|
|
237
|
+
return configuration(field + " requires provider and model.");
|
|
238
|
+
return { provider, model };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function configuredRoutes(
|
|
242
|
+
settings: ClassifierSettings,
|
|
243
|
+
field: "fallback" | "compare",
|
|
244
|
+
): ClassifierRoute[] {
|
|
245
|
+
const value = settings[field];
|
|
246
|
+
if (value === undefined) return [];
|
|
247
|
+
if (!Array.isArray(value))
|
|
248
|
+
return configuration(field + " must be an array of provider/model pairs.");
|
|
249
|
+
return value.map((entry, index) => routePair(entry, field + "[" + index + "]"));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function resolveModel(
|
|
253
|
+
settings: ClassifierSettings,
|
|
254
|
+
selected: ClassifierRoute = configuredDefault(settings),
|
|
255
|
+
): ResolvedModel {
|
|
256
|
+
const { provider: routeId, model: modelId } = routePair(selected, "route");
|
|
257
|
+
if (
|
|
258
|
+
!/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/.test(routeId) ||
|
|
259
|
+
modelId.length > 256 ||
|
|
260
|
+
/[\u0000-\u001f\u007f]/.test(modelId)
|
|
261
|
+
)
|
|
262
|
+
return configuration(
|
|
263
|
+
"Provider routes must be short identifiers and model IDs must be at most 256 characters without control characters.",
|
|
264
|
+
);
|
|
265
|
+
const builtIn = BUILT_INS.find(
|
|
266
|
+
(entry) => entry.route === routeId || entry.id === routeId,
|
|
267
|
+
);
|
|
268
|
+
if (settings.providers !== undefined && !isRecord(settings.providers))
|
|
269
|
+
return configuration("providers must be an object.");
|
|
270
|
+
if (
|
|
271
|
+
settings.customProviders !== undefined &&
|
|
272
|
+
!isRecord(settings.customProviders)
|
|
273
|
+
)
|
|
274
|
+
return configuration("customProviders must be an object.");
|
|
275
|
+
for (const name of Object.keys(settings.customProviders ?? {})) {
|
|
276
|
+
if (BUILT_INS.some((entry) => entry.route === name || entry.id === name))
|
|
277
|
+
return configuration("Custom providers cannot shadow built-in routes.");
|
|
278
|
+
}
|
|
279
|
+
const custom = builtIn ? undefined : settings.customProviders?.[routeId];
|
|
280
|
+
if (!builtIn && !custom)
|
|
281
|
+
return configuration("Unknown classifier provider route.");
|
|
282
|
+
const raw: ProviderSettings = builtIn
|
|
283
|
+
? (settings.providers?.[builtIn.id] ?? {})
|
|
284
|
+
: custom!;
|
|
285
|
+
if (!isRecord(raw))
|
|
286
|
+
return configuration("Provider settings must be an object.");
|
|
287
|
+
const api = builtIn?.api ?? custom!.api;
|
|
288
|
+
if (
|
|
289
|
+
![
|
|
290
|
+
"openai-decisions",
|
|
291
|
+
"perplexity-decisions",
|
|
292
|
+
"system-one",
|
|
293
|
+
"cloudflare-system-one",
|
|
294
|
+
"microsoft-system-one",
|
|
295
|
+
].includes(api)
|
|
296
|
+
)
|
|
297
|
+
return configuration("Unsupported classifier API protocol.");
|
|
298
|
+
const models: ModelDefinition[] = builtIn?.models ?? custom!.models;
|
|
299
|
+
if (
|
|
300
|
+
!Array.isArray(models) ||
|
|
301
|
+
models.length === 0 ||
|
|
302
|
+
models.some(
|
|
303
|
+
(m) =>
|
|
304
|
+
!isRecord(m) ||
|
|
305
|
+
typeof m.id !== "string" ||
|
|
306
|
+
!m.id.trim() ||
|
|
307
|
+
(m.input !== undefined &&
|
|
308
|
+
(!Array.isArray(m.input) ||
|
|
309
|
+
m.input.some((x) => x !== "text" && x !== "image"))) ||
|
|
310
|
+
(m.box !== undefined && typeof m.box !== "boolean"),
|
|
311
|
+
)
|
|
312
|
+
) {
|
|
313
|
+
return configuration(
|
|
314
|
+
"Provider models must declare an id and optional text/image input capabilities.",
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
const definition = models.find((entry) => entry.id === modelId);
|
|
318
|
+
// Built-in API routes allow new text model IDs without a package release. Images require known capabilities.
|
|
319
|
+
if (!definition && !builtIn)
|
|
320
|
+
return configuration(
|
|
321
|
+
"The selected model is not declared in customProviders.<id>.models.",
|
|
322
|
+
);
|
|
323
|
+
for (const flag of ["experimentalImages", "experimentalBox"] as const) {
|
|
324
|
+
if (raw[flag] !== undefined && typeof raw[flag] !== "boolean")
|
|
325
|
+
return configuration(flag + " must be a boolean.");
|
|
326
|
+
}
|
|
327
|
+
let baseUrl = optionalString(raw.baseUrl, "baseUrl") ?? builtIn?.baseUrl;
|
|
328
|
+
if (api === "cloudflare-system-one" && !baseUrl)
|
|
329
|
+
baseUrl =
|
|
330
|
+
"https://api.cloudflare.com/client/v4/accounts/" +
|
|
331
|
+
cloudflareAccountId(optionalString(raw.accountId, "accountId")) +
|
|
332
|
+
"/ai";
|
|
333
|
+
if (!baseUrl)
|
|
334
|
+
return configuration(
|
|
335
|
+
"The selected provider requires a baseUrl. Microsoft uses your Foundry resource endpoint.",
|
|
336
|
+
);
|
|
337
|
+
const timeout = settings.requestTimeoutMs ?? 30_000;
|
|
338
|
+
if (!Number.isInteger(timeout) || timeout < 100 || timeout > 300_000)
|
|
339
|
+
return configuration("requestTimeoutMs must be between 100 and 300000.");
|
|
340
|
+
if (raw.apiKey !== undefined && typeof raw.apiKey !== "string")
|
|
341
|
+
return configuration("apiKey must be a string.");
|
|
342
|
+
const configuredApiKey =
|
|
343
|
+
typeof raw.apiKey === "string" ? raw.apiKey.trim() || undefined : undefined;
|
|
344
|
+
if (configuredApiKey && /\$\{?|^!/.test(configuredApiKey))
|
|
345
|
+
return configuration(
|
|
346
|
+
"API-key references must be resolved in global settings; project settings cannot expand environment variables or execute commands.",
|
|
347
|
+
);
|
|
348
|
+
const isJevDirect = builtIn?.id === "typesafe";
|
|
349
|
+
const result: ResolvedModel = {
|
|
350
|
+
route: builtIn?.route ?? routeId,
|
|
351
|
+
provider: builtIn?.id ?? routeId,
|
|
352
|
+
model: modelId,
|
|
353
|
+
api,
|
|
354
|
+
baseUrl: validateBaseUrl(baseUrl),
|
|
355
|
+
headers: headers(raw.headers),
|
|
356
|
+
credentialProvider: builtIn?.credentialProvider ?? routeId,
|
|
357
|
+
images:
|
|
358
|
+
api === "microsoft-system-one"
|
|
359
|
+
? false
|
|
360
|
+
: api === "system-one"
|
|
361
|
+
? Boolean(
|
|
362
|
+
raw.experimentalImages && definition?.input?.includes("image"),
|
|
363
|
+
)
|
|
364
|
+
: Boolean(definition?.input?.includes("image")),
|
|
365
|
+
box: Boolean(
|
|
366
|
+
(isJevDirect || custom?.api === "system-one") &&
|
|
367
|
+
definition?.box &&
|
|
368
|
+
definition?.input?.includes("image") &&
|
|
369
|
+
raw.experimentalBox &&
|
|
370
|
+
raw.experimentalImages,
|
|
371
|
+
),
|
|
372
|
+
experimentalImages: raw.experimentalImages === true,
|
|
373
|
+
requestTimeoutMs: timeout,
|
|
374
|
+
};
|
|
375
|
+
if (configuredApiKey) result.configuredApiKey = configuredApiKey;
|
|
376
|
+
if (builtIn?.envVar) result.envVar = builtIn.envVar;
|
|
377
|
+
return result;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export function listModels(settings: ClassifierSettings): string {
|
|
381
|
+
const lines = BUILT_INS.flatMap((provider) =>
|
|
382
|
+
provider.models.map((model) => provider.route + " " + model.id),
|
|
383
|
+
);
|
|
384
|
+
for (const [id, provider] of Object.entries(settings.customProviders ?? {})) {
|
|
385
|
+
if (Array.isArray(provider?.models))
|
|
386
|
+
for (const model of provider.models)
|
|
387
|
+
if (typeof model?.id === "string")
|
|
388
|
+
lines.push(JSON.stringify(id) + " " + JSON.stringify(model.id));
|
|
389
|
+
}
|
|
390
|
+
return lines.join("\n");
|
|
391
|
+
}
|