@lenylvt/pi-ai 0.64.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/README.md +203 -0
- package/dist/api-registry.d.ts +20 -0
- package/dist/api-registry.d.ts.map +1 -0
- package/dist/api-registry.js +44 -0
- package/dist/api-registry.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +119 -0
- package/dist/cli.js.map +1 -0
- package/dist/env-api-keys.d.ts +7 -0
- package/dist/env-api-keys.d.ts.map +1 -0
- package/dist/env-api-keys.js +13 -0
- package/dist/env-api-keys.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/models.d.ts +24 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.generated.d.ts +2332 -0
- package/dist/models.generated.d.ts.map +1 -0
- package/dist/models.generated.js +2186 -0
- package/dist/models.generated.js.map +1 -0
- package/dist/models.js +60 -0
- package/dist/models.js.map +1 -0
- package/dist/oauth.d.ts +2 -0
- package/dist/oauth.d.ts.map +1 -0
- package/dist/oauth.js +2 -0
- package/dist/oauth.js.map +1 -0
- package/dist/providers/anthropic.d.ts +40 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +749 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/faux.d.ts +56 -0
- package/dist/providers/faux.d.ts.map +1 -0
- package/dist/providers/faux.js +367 -0
- package/dist/providers/faux.js.map +1 -0
- package/dist/providers/github-copilot-headers.d.ts +8 -0
- package/dist/providers/github-copilot-headers.d.ts.map +1 -0
- package/dist/providers/github-copilot-headers.js +29 -0
- package/dist/providers/github-copilot-headers.js.map +1 -0
- package/dist/providers/openai-codex-responses.d.ts +9 -0
- package/dist/providers/openai-codex-responses.d.ts.map +1 -0
- package/dist/providers/openai-codex-responses.js +741 -0
- package/dist/providers/openai-codex-responses.js.map +1 -0
- package/dist/providers/openai-completions.d.ts +15 -0
- package/dist/providers/openai-completions.d.ts.map +1 -0
- package/dist/providers/openai-completions.js +687 -0
- package/dist/providers/openai-completions.js.map +1 -0
- package/dist/providers/openai-responses-shared.d.ts +17 -0
- package/dist/providers/openai-responses-shared.d.ts.map +1 -0
- package/dist/providers/openai-responses-shared.js +458 -0
- package/dist/providers/openai-responses-shared.js.map +1 -0
- package/dist/providers/openai-responses.d.ts +13 -0
- package/dist/providers/openai-responses.d.ts.map +1 -0
- package/dist/providers/openai-responses.js +190 -0
- package/dist/providers/openai-responses.js.map +1 -0
- package/dist/providers/register-builtins.d.ts +16 -0
- package/dist/providers/register-builtins.d.ts.map +1 -0
- package/dist/providers/register-builtins.js +140 -0
- package/dist/providers/register-builtins.js.map +1 -0
- package/dist/providers/simple-options.d.ts +8 -0
- package/dist/providers/simple-options.d.ts.map +1 -0
- package/dist/providers/simple-options.js +35 -0
- package/dist/providers/simple-options.js.map +1 -0
- package/dist/providers/transform-messages.d.ts +8 -0
- package/dist/providers/transform-messages.d.ts.map +1 -0
- package/dist/providers/transform-messages.js +155 -0
- package/dist/providers/transform-messages.js.map +1 -0
- package/dist/stream.d.ts +8 -0
- package/dist/stream.d.ts.map +1 -0
- package/dist/stream.js +27 -0
- package/dist/stream.js.map +1 -0
- package/dist/types.d.ts +283 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/event-stream.d.ts +21 -0
- package/dist/utils/event-stream.d.ts.map +1 -0
- package/dist/utils/event-stream.js +81 -0
- package/dist/utils/event-stream.js.map +1 -0
- package/dist/utils/hash.d.ts +3 -0
- package/dist/utils/hash.d.ts.map +1 -0
- package/dist/utils/hash.js +14 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/json-parse.d.ts +9 -0
- package/dist/utils/json-parse.d.ts.map +1 -0
- package/dist/utils/json-parse.js +29 -0
- package/dist/utils/json-parse.js.map +1 -0
- package/dist/utils/oauth/anthropic.d.ts +25 -0
- package/dist/utils/oauth/anthropic.d.ts.map +1 -0
- package/dist/utils/oauth/anthropic.js +335 -0
- package/dist/utils/oauth/anthropic.js.map +1 -0
- package/dist/utils/oauth/github-copilot.d.ts +30 -0
- package/dist/utils/oauth/github-copilot.d.ts.map +1 -0
- package/dist/utils/oauth/github-copilot.js +292 -0
- package/dist/utils/oauth/github-copilot.js.map +1 -0
- package/dist/utils/oauth/index.d.ts +36 -0
- package/dist/utils/oauth/index.d.ts.map +1 -0
- package/dist/utils/oauth/index.js +92 -0
- package/dist/utils/oauth/index.js.map +1 -0
- package/dist/utils/oauth/oauth-page.d.ts +3 -0
- package/dist/utils/oauth/oauth-page.d.ts.map +1 -0
- package/dist/utils/oauth/oauth-page.js +105 -0
- package/dist/utils/oauth/oauth-page.js.map +1 -0
- package/dist/utils/oauth/openai-codex.d.ts +34 -0
- package/dist/utils/oauth/openai-codex.d.ts.map +1 -0
- package/dist/utils/oauth/openai-codex.js +373 -0
- package/dist/utils/oauth/openai-codex.js.map +1 -0
- package/dist/utils/oauth/pkce.d.ts +13 -0
- package/dist/utils/oauth/pkce.d.ts.map +1 -0
- package/dist/utils/oauth/pkce.js +31 -0
- package/dist/utils/oauth/pkce.js.map +1 -0
- package/dist/utils/oauth/types.d.ts +47 -0
- package/dist/utils/oauth/types.d.ts.map +1 -0
- package/dist/utils/oauth/types.js +2 -0
- package/dist/utils/oauth/types.js.map +1 -0
- package/dist/utils/overflow.d.ts +53 -0
- package/dist/utils/overflow.d.ts.map +1 -0
- package/dist/utils/overflow.js +119 -0
- package/dist/utils/overflow.js.map +1 -0
- package/dist/utils/sanitize-unicode.d.ts +22 -0
- package/dist/utils/sanitize-unicode.d.ts.map +1 -0
- package/dist/utils/sanitize-unicode.js +26 -0
- package/dist/utils/sanitize-unicode.js.map +1 -0
- package/dist/utils/typebox-helpers.d.ts +17 -0
- package/dist/utils/typebox-helpers.d.ts.map +1 -0
- package/dist/utils/typebox-helpers.js +21 -0
- package/dist/utils/typebox-helpers.js.map +1 -0
- package/dist/utils/validation.d.ts +18 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +80 -0
- package/dist/utils/validation.js.map +1 -0
- package/package.json +89 -0
- package/src/api-registry.ts +98 -0
- package/src/cli.ts +136 -0
- package/src/env-api-keys.ts +22 -0
- package/src/index.ts +29 -0
- package/src/models.generated.ts +2188 -0
- package/src/models.ts +82 -0
- package/src/oauth.ts +1 -0
- package/src/providers/anthropic.ts +905 -0
- package/src/providers/faux.ts +498 -0
- package/src/providers/github-copilot-headers.ts +37 -0
- package/src/providers/openai-codex-responses.ts +929 -0
- package/src/providers/openai-completions.ts +811 -0
- package/src/providers/openai-responses-shared.ts +513 -0
- package/src/providers/openai-responses.ts +251 -0
- package/src/providers/register-builtins.ts +232 -0
- package/src/providers/simple-options.ts +46 -0
- package/src/providers/transform-messages.ts +172 -0
- package/src/stream.ts +59 -0
- package/src/types.ts +294 -0
- package/src/utils/event-stream.ts +87 -0
- package/src/utils/hash.ts +13 -0
- package/src/utils/json-parse.ts +28 -0
- package/src/utils/oauth/anthropic.ts +402 -0
- package/src/utils/oauth/github-copilot.ts +396 -0
- package/src/utils/oauth/index.ts +123 -0
- package/src/utils/oauth/oauth-page.ts +109 -0
- package/src/utils/oauth/openai-codex.ts +450 -0
- package/src/utils/oauth/pkce.ts +34 -0
- package/src/utils/oauth/types.ts +59 -0
- package/src/utils/overflow.ts +125 -0
- package/src/utils/sanitize-unicode.ts +25 -0
- package/src/utils/typebox-helpers.ts +24 -0
- package/src/utils/validation.ts +93 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type TUnsafe, Type } from "@sinclair/typebox";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a string enum schema compatible with Google's API and other providers
|
|
5
|
+
* that don't support anyOf/const patterns.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const OperationSchema = StringEnum(["add", "subtract", "multiply", "divide"], {
|
|
9
|
+
* description: "The operation to perform"
|
|
10
|
+
* });
|
|
11
|
+
*
|
|
12
|
+
* type Operation = Static<typeof OperationSchema>; // "add" | "subtract" | "multiply" | "divide"
|
|
13
|
+
*/
|
|
14
|
+
export function StringEnum<T extends readonly string[]>(
|
|
15
|
+
values: T,
|
|
16
|
+
options?: { description?: string; default?: T[number] },
|
|
17
|
+
): TUnsafe<T[number]> {
|
|
18
|
+
return Type.Unsafe<T[number]>({
|
|
19
|
+
type: "string",
|
|
20
|
+
enum: values as any,
|
|
21
|
+
...(options?.description && { description: options.description }),
|
|
22
|
+
...(options?.default && { default: options.default }),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import AjvModule from "ajv";
|
|
2
|
+
import addFormatsModule from "ajv-formats";
|
|
3
|
+
|
|
4
|
+
// Handle both default and named exports
|
|
5
|
+
const Ajv = (AjvModule as any).default || AjvModule;
|
|
6
|
+
const addFormats = (addFormatsModule as any).default || addFormatsModule;
|
|
7
|
+
|
|
8
|
+
import type { Tool, ToolCall } from "../types.js";
|
|
9
|
+
|
|
10
|
+
// Detect if we're in a browser extension environment with strict CSP
|
|
11
|
+
// Chrome extensions with Manifest V3 don't allow eval/Function constructor
|
|
12
|
+
const isBrowserExtension = typeof globalThis !== "undefined" && (globalThis as any).chrome?.runtime?.id !== undefined;
|
|
13
|
+
|
|
14
|
+
function canUseRuntimeCodegen(): boolean {
|
|
15
|
+
if (isBrowserExtension) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
new Function("return true;");
|
|
21
|
+
return true;
|
|
22
|
+
} catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Create a singleton AJV instance with formats only when runtime code generation is available.
|
|
28
|
+
let ajv: any = null;
|
|
29
|
+
if (canUseRuntimeCodegen()) {
|
|
30
|
+
try {
|
|
31
|
+
ajv = new Ajv({
|
|
32
|
+
allErrors: true,
|
|
33
|
+
strict: false,
|
|
34
|
+
coerceTypes: true,
|
|
35
|
+
});
|
|
36
|
+
addFormats(ajv);
|
|
37
|
+
} catch (_e) {
|
|
38
|
+
console.warn("AJV validation disabled due to CSP restrictions");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Finds a tool by name and validates the tool call arguments against its TypeBox schema
|
|
44
|
+
* @param tools Array of tool definitions
|
|
45
|
+
* @param toolCall The tool call from the LLM
|
|
46
|
+
* @returns The validated arguments
|
|
47
|
+
* @throws Error if tool is not found or validation fails
|
|
48
|
+
*/
|
|
49
|
+
export function validateToolCall(tools: Tool[], toolCall: ToolCall): any {
|
|
50
|
+
const tool = tools.find((t) => t.name === toolCall.name);
|
|
51
|
+
if (!tool) {
|
|
52
|
+
throw new Error(`Tool "${toolCall.name}" not found`);
|
|
53
|
+
}
|
|
54
|
+
return validateToolArguments(tool, toolCall);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Validates tool call arguments against the tool's TypeBox schema
|
|
59
|
+
* @param tool The tool definition with TypeBox schema
|
|
60
|
+
* @param toolCall The tool call from the LLM
|
|
61
|
+
* @returns The validated (and potentially coerced) arguments
|
|
62
|
+
* @throws Error with formatted message if validation fails
|
|
63
|
+
*/
|
|
64
|
+
export function validateToolArguments(tool: Tool, toolCall: ToolCall): any {
|
|
65
|
+
// Skip validation in environments where runtime code generation is unavailable.
|
|
66
|
+
if (!ajv || !canUseRuntimeCodegen()) {
|
|
67
|
+
return toolCall.arguments;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Compile the schema.
|
|
71
|
+
const validate = ajv.compile(tool.parameters);
|
|
72
|
+
|
|
73
|
+
// Clone arguments so AJV can safely mutate for type coercion
|
|
74
|
+
const args = structuredClone(toolCall.arguments);
|
|
75
|
+
|
|
76
|
+
// Validate the arguments (AJV mutates args in-place for type coercion)
|
|
77
|
+
if (validate(args)) {
|
|
78
|
+
return args;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Format validation errors nicely
|
|
82
|
+
const errors =
|
|
83
|
+
validate.errors
|
|
84
|
+
?.map((err: any) => {
|
|
85
|
+
const path = err.instancePath ? err.instancePath.substring(1) : err.params.missingProperty || "root";
|
|
86
|
+
return ` - ${path}: ${err.message}`;
|
|
87
|
+
})
|
|
88
|
+
.join("\n") || "Unknown validation error";
|
|
89
|
+
|
|
90
|
+
const errorMessage = `Validation failed for tool "${toolCall.name}":\n${errors}\n\nReceived arguments:\n${JSON.stringify(toolCall.arguments, null, 2)}`;
|
|
91
|
+
|
|
92
|
+
throw new Error(errorMessage);
|
|
93
|
+
}
|