phi-code-ai 0.56.4 → 0.74.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 +258 -73
- package/dist/api-registry.d.ts.map +1 -1
- package/dist/api-registry.js.map +1 -1
- package/dist/bedrock-provider.d.ts.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +1 -1
- package/dist/cli.js.map +1 -1
- package/dist/env-api-keys.d.ts +9 -0
- package/dist/env-api-keys.d.ts.map +1 -1
- package/dist/env-api-keys.js +96 -30
- package/dist/env-api-keys.js.map +1 -1
- package/dist/image-models.d.ts +10 -0
- package/dist/image-models.d.ts.map +1 -0
- package/dist/image-models.generated.d.ts +305 -0
- package/dist/image-models.generated.d.ts.map +1 -0
- package/dist/image-models.generated.js +307 -0
- package/dist/image-models.generated.js.map +1 -0
- package/dist/image-models.js +23 -0
- package/dist/image-models.js.map +1 -0
- package/dist/images-api-registry.d.ts +14 -0
- package/dist/images-api-registry.d.ts.map +1 -0
- package/dist/images-api-registry.js +22 -0
- package/dist/images-api-registry.js.map +1 -0
- package/dist/images.d.ts +4 -0
- package/dist/images.d.ts.map +1 -0
- package/dist/images.js +14 -0
- package/dist/images.js.map +1 -0
- package/dist/index.d.ts +20 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -9
- package/dist/index.js.map +1 -1
- package/dist/models.d.ts +3 -9
- package/dist/models.d.ts.map +1 -1
- package/dist/models.generated.d.ts +6525 -2231
- package/dist/models.generated.d.ts.map +1 -1
- package/dist/models.generated.js +8992 -5524
- package/dist/models.generated.js.map +1 -1
- package/dist/models.js +28 -12
- package/dist/models.js.map +1 -1
- package/dist/oauth.d.ts.map +1 -1
- package/dist/providers/amazon-bedrock.d.ts +23 -0
- package/dist/providers/amazon-bedrock.d.ts.map +1 -1
- package/dist/providers/amazon-bedrock.js +206 -44
- package/dist/providers/amazon-bedrock.js.map +1 -1
- package/dist/providers/anthropic.d.ts +23 -2
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +294 -63
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/azure-openai-responses.d.ts.map +1 -1
- package/dist/providers/azure-openai-responses.js +47 -23
- package/dist/providers/azure-openai-responses.js.map +1 -1
- package/dist/providers/cloudflare.d.ts +13 -0
- package/dist/providers/cloudflare.d.ts.map +1 -0
- package/dist/providers/cloudflare.js +26 -0
- package/dist/providers/cloudflare.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 +368 -0
- package/dist/providers/faux.js.map +1 -0
- package/dist/providers/github-copilot-headers.d.ts.map +1 -1
- package/dist/providers/github-copilot-headers.js.map +1 -1
- package/dist/providers/google-shared.d.ts +7 -2
- package/dist/providers/google-shared.d.ts.map +1 -1
- package/dist/providers/google-shared.js +53 -24
- package/dist/providers/google-shared.js.map +1 -1
- package/dist/providers/google-vertex.d.ts +1 -1
- package/dist/providers/google-vertex.d.ts.map +1 -1
- package/dist/providers/google-vertex.js +87 -16
- package/dist/providers/google-vertex.js.map +1 -1
- package/dist/providers/google.d.ts +1 -1
- package/dist/providers/google.d.ts.map +1 -1
- package/dist/providers/google.js +57 -9
- package/dist/providers/google.js.map +1 -1
- package/dist/providers/images/openrouter.d.ts +3 -0
- package/dist/providers/images/openrouter.d.ts.map +1 -0
- package/dist/providers/images/openrouter.js +129 -0
- package/dist/providers/images/openrouter.js.map +1 -0
- package/dist/providers/images/register-builtins.d.ts +4 -0
- package/dist/providers/images/register-builtins.d.ts.map +1 -0
- package/dist/providers/images/register-builtins.js +34 -0
- package/dist/providers/images/register-builtins.js.map +1 -0
- package/dist/providers/mistral.d.ts +3 -0
- package/dist/providers/mistral.d.ts.map +1 -1
- package/dist/providers/mistral.js +49 -9
- package/dist/providers/mistral.js.map +1 -1
- package/dist/providers/openai-codex-responses.d.ts +21 -0
- package/dist/providers/openai-codex-responses.d.ts.map +1 -1
- package/dist/providers/openai-codex-responses.js +443 -86
- package/dist/providers/openai-codex-responses.js.map +1 -1
- package/dist/providers/openai-completions.d.ts +5 -1
- package/dist/providers/openai-completions.d.ts.map +1 -1
- package/dist/providers/openai-completions.js +459 -225
- package/dist/providers/openai-completions.js.map +1 -1
- package/dist/providers/openai-responses-shared.d.ts +1 -0
- package/dist/providers/openai-responses-shared.d.ts.map +1 -1
- package/dist/providers/openai-responses-shared.js +95 -45
- package/dist/providers/openai-responses-shared.js.map +1 -1
- package/dist/providers/openai-responses.d.ts.map +1 -1
- package/dist/providers/openai-responses.js +66 -44
- package/dist/providers/openai-responses.js.map +1 -1
- package/dist/providers/register-builtins.d.ts +27 -2
- package/dist/providers/register-builtins.d.ts.map +1 -1
- package/dist/providers/register-builtins.js +157 -52
- package/dist/providers/register-builtins.js.map +1 -1
- package/dist/providers/simple-options.d.ts.map +1 -1
- package/dist/providers/simple-options.js +5 -1
- package/dist/providers/simple-options.js.map +1 -1
- package/dist/providers/transform-messages.d.ts.map +1 -1
- package/dist/providers/transform-messages.js +63 -34
- package/dist/providers/transform-messages.js.map +1 -1
- package/dist/session-resources.d.ts +4 -0
- package/dist/session-resources.d.ts.map +1 -0
- package/dist/session-resources.js +22 -0
- package/dist/session-resources.js.map +1 -0
- package/dist/stream.d.ts.map +1 -1
- package/dist/stream.js.map +1 -1
- package/dist/types.d.ts +219 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/diagnostics.d.ts +19 -0
- package/dist/utils/diagnostics.d.ts.map +1 -0
- package/dist/utils/diagnostics.js +25 -0
- package/dist/utils/diagnostics.js.map +1 -0
- package/dist/utils/event-stream.d.ts.map +1 -1
- package/dist/utils/event-stream.js +7 -3
- package/dist/utils/event-stream.js.map +1 -1
- package/dist/utils/hash.d.ts.map +1 -1
- package/dist/utils/hash.js.map +1 -1
- package/dist/utils/headers.d.ts +2 -0
- package/dist/utils/headers.d.ts.map +1 -0
- package/dist/utils/headers.js +8 -0
- package/dist/utils/headers.js.map +1 -0
- package/dist/utils/json-parse.d.ts +8 -1
- package/dist/utils/json-parse.d.ts.map +1 -1
- package/dist/utils/json-parse.js +89 -5
- package/dist/utils/json-parse.js.map +1 -1
- package/dist/utils/oauth/anthropic.d.ts +14 -6
- package/dist/utils/oauth/anthropic.d.ts.map +1 -1
- package/dist/utils/oauth/anthropic.js +288 -57
- package/dist/utils/oauth/anthropic.js.map +1 -1
- package/dist/utils/oauth/github-copilot.d.ts.map +1 -1
- package/dist/utils/oauth/github-copilot.js +23 -12
- package/dist/utils/oauth/github-copilot.js.map +1 -1
- package/dist/utils/oauth/index.d.ts +0 -4
- package/dist/utils/oauth/index.d.ts.map +1 -1
- package/dist/utils/oauth/index.js +0 -10
- package/dist/utils/oauth/index.js.map +1 -1
- 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.map +1 -1
- package/dist/utils/oauth/openai-codex.js +51 -46
- package/dist/utils/oauth/openai-codex.js.map +1 -1
- package/dist/utils/oauth/pkce.d.ts.map +1 -1
- package/dist/utils/oauth/pkce.js.map +1 -1
- package/dist/utils/oauth/types.d.ts +10 -0
- package/dist/utils/oauth/types.d.ts.map +1 -1
- package/dist/utils/oauth/types.js.map +1 -1
- package/dist/utils/overflow.d.ts +7 -3
- package/dist/utils/overflow.d.ts.map +1 -1
- package/dist/utils/overflow.js +46 -13
- package/dist/utils/overflow.js.map +1 -1
- package/dist/utils/sanitize-unicode.d.ts.map +1 -1
- package/dist/utils/sanitize-unicode.js.map +1 -1
- package/dist/utils/typebox-helpers.d.ts +1 -1
- package/dist/utils/typebox-helpers.d.ts.map +1 -1
- package/dist/utils/typebox-helpers.js +1 -1
- package/dist/utils/typebox-helpers.js.map +1 -1
- package/dist/utils/validation.d.ts.map +1 -1
- package/dist/utils/validation.js +247 -38
- package/dist/utils/validation.js.map +1 -1
- package/package.json +43 -13
- package/bedrock-provider.d.ts +0 -1
- package/bedrock-provider.js +0 -1
- package/dist/providers/google-gemini-cli.d.ts +0 -74
- package/dist/providers/google-gemini-cli.d.ts.map +0 -1
- package/dist/providers/google-gemini-cli.js +0 -754
- package/dist/providers/google-gemini-cli.js.map +0 -1
- package/dist/utils/oauth/google-antigravity.d.ts +0 -26
- package/dist/utils/oauth/google-antigravity.d.ts.map +0 -1
- package/dist/utils/oauth/google-antigravity.js +0 -373
- package/dist/utils/oauth/google-antigravity.js.map +0 -1
- package/dist/utils/oauth/google-gemini-cli.d.ts +0 -26
- package/dist/utils/oauth/google-gemini-cli.d.ts.map +0 -1
- package/dist/utils/oauth/google-gemini-cli.js +0 -478
- package/dist/utils/oauth/google-gemini-cli.js.map +0 -1
package/dist/utils/validation.js
CHANGED
|
@@ -1,27 +1,233 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import { Compile } from "typebox/compile";
|
|
2
|
+
import { Value } from "typebox/value";
|
|
3
|
+
const validatorCache = new WeakMap();
|
|
4
|
+
const TYPEBOX_KIND = Symbol.for("TypeBox.Kind");
|
|
5
|
+
function isRecord(value) {
|
|
6
|
+
return typeof value === "object" && value !== null;
|
|
7
|
+
}
|
|
8
|
+
function isJsonSchemaObject(value) {
|
|
9
|
+
return isRecord(value);
|
|
10
|
+
}
|
|
11
|
+
function hasTypeBoxMetadata(schema) {
|
|
12
|
+
return isRecord(schema) && Object.getOwnPropertySymbols(schema).includes(TYPEBOX_KIND);
|
|
13
|
+
}
|
|
14
|
+
function getSchemaTypes(schema) {
|
|
15
|
+
if (typeof schema.type === "string") {
|
|
16
|
+
return [schema.type];
|
|
17
|
+
}
|
|
18
|
+
if (Array.isArray(schema.type)) {
|
|
19
|
+
return schema.type.filter((type) => typeof type === "string");
|
|
20
|
+
}
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
function matchesJsonType(value, type) {
|
|
24
|
+
switch (type) {
|
|
25
|
+
case "number":
|
|
26
|
+
return typeof value === "number";
|
|
27
|
+
case "integer":
|
|
28
|
+
return typeof value === "number" && Number.isInteger(value);
|
|
29
|
+
case "boolean":
|
|
30
|
+
return typeof value === "boolean";
|
|
31
|
+
case "string":
|
|
32
|
+
return typeof value === "string";
|
|
33
|
+
case "null":
|
|
34
|
+
return value === null;
|
|
35
|
+
case "array":
|
|
36
|
+
return Array.isArray(value);
|
|
37
|
+
case "object":
|
|
38
|
+
return isRecord(value) && !Array.isArray(value);
|
|
39
|
+
default:
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function isValidatorSchema(value) {
|
|
44
|
+
return isRecord(value);
|
|
45
|
+
}
|
|
46
|
+
function getSubSchemaValidator(schema) {
|
|
47
|
+
if (!isValidatorSchema(schema)) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
13
50
|
try {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
51
|
+
return getValidator(schema);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function coercePrimitiveByType(value, type) {
|
|
58
|
+
switch (type) {
|
|
59
|
+
case "number": {
|
|
60
|
+
if (value === null) {
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
64
|
+
const parsed = Number(value);
|
|
65
|
+
if (Number.isFinite(parsed)) {
|
|
66
|
+
return parsed;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (typeof value === "boolean") {
|
|
70
|
+
return value ? 1 : 0;
|
|
71
|
+
}
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
case "integer": {
|
|
75
|
+
if (value === null) {
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
79
|
+
const parsed = Number(value);
|
|
80
|
+
if (Number.isInteger(parsed)) {
|
|
81
|
+
return parsed;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (typeof value === "boolean") {
|
|
85
|
+
return value ? 1 : 0;
|
|
86
|
+
}
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
case "boolean": {
|
|
90
|
+
if (value === null) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
if (typeof value === "string") {
|
|
94
|
+
if (value === "true") {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
if (value === "false") {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (typeof value === "number") {
|
|
102
|
+
if (value === 1) {
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
if (value === 0) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return value;
|
|
110
|
+
}
|
|
111
|
+
case "string": {
|
|
112
|
+
if (value === null) {
|
|
113
|
+
return "";
|
|
114
|
+
}
|
|
115
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
116
|
+
return String(value);
|
|
117
|
+
}
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
case "null": {
|
|
121
|
+
if (value === "" || value === 0 || value === false) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
return value;
|
|
125
|
+
}
|
|
126
|
+
default:
|
|
127
|
+
return value;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function applySchemaObjectCoercion(value, schema) {
|
|
131
|
+
const properties = schema.properties;
|
|
132
|
+
const definedKeys = new Set(properties ? Object.keys(properties) : []);
|
|
133
|
+
if (properties) {
|
|
134
|
+
for (const [key, propertySchema] of Object.entries(properties)) {
|
|
135
|
+
if (!(key in value)) {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
value[key] = coerceWithJsonSchema(value[key], propertySchema);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (schema.additionalProperties && isJsonSchemaObject(schema.additionalProperties)) {
|
|
142
|
+
for (const [key, propertyValue] of Object.entries(value)) {
|
|
143
|
+
if (definedKeys.has(key)) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
value[key] = coerceWithJsonSchema(propertyValue, schema.additionalProperties);
|
|
147
|
+
}
|
|
20
148
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
149
|
+
}
|
|
150
|
+
function applySchemaArrayCoercion(value, schema) {
|
|
151
|
+
if (Array.isArray(schema.items)) {
|
|
152
|
+
for (let index = 0; index < value.length; index++) {
|
|
153
|
+
const itemSchema = schema.items[index];
|
|
154
|
+
if (!itemSchema) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
value[index] = coerceWithJsonSchema(value[index], itemSchema);
|
|
158
|
+
}
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (isJsonSchemaObject(schema.items)) {
|
|
162
|
+
for (let index = 0; index < value.length; index++) {
|
|
163
|
+
value[index] = coerceWithJsonSchema(value[index], schema.items);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function coerceWithUnionSchema(value, schemas) {
|
|
168
|
+
for (const schema of schemas) {
|
|
169
|
+
const candidate = structuredClone(value);
|
|
170
|
+
const coerced = coerceWithJsonSchema(candidate, schema);
|
|
171
|
+
const validator = getSubSchemaValidator(schema);
|
|
172
|
+
if (validator?.Check(coerced)) {
|
|
173
|
+
return coerced;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return value;
|
|
177
|
+
}
|
|
178
|
+
function coerceWithJsonSchema(value, schema) {
|
|
179
|
+
let nextValue = value;
|
|
180
|
+
if (Array.isArray(schema.allOf)) {
|
|
181
|
+
for (const nested of schema.allOf) {
|
|
182
|
+
nextValue = coerceWithJsonSchema(nextValue, nested);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (Array.isArray(schema.anyOf)) {
|
|
186
|
+
nextValue = coerceWithUnionSchema(nextValue, schema.anyOf);
|
|
187
|
+
}
|
|
188
|
+
if (Array.isArray(schema.oneOf)) {
|
|
189
|
+
nextValue = coerceWithUnionSchema(nextValue, schema.oneOf);
|
|
24
190
|
}
|
|
191
|
+
const schemaTypes = getSchemaTypes(schema);
|
|
192
|
+
const matchesUnionMember = schemaTypes.length > 1 && schemaTypes.some((schemaType) => matchesJsonType(nextValue, schemaType));
|
|
193
|
+
if (schemaTypes.length > 0 && !matchesUnionMember) {
|
|
194
|
+
for (const schemaType of schemaTypes) {
|
|
195
|
+
const candidate = coercePrimitiveByType(nextValue, schemaType);
|
|
196
|
+
if (candidate !== nextValue) {
|
|
197
|
+
nextValue = candidate;
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (schemaTypes.includes("object") && isRecord(nextValue) && !Array.isArray(nextValue)) {
|
|
203
|
+
applySchemaObjectCoercion(nextValue, schema);
|
|
204
|
+
}
|
|
205
|
+
if (schemaTypes.includes("array") && Array.isArray(nextValue)) {
|
|
206
|
+
applySchemaArrayCoercion(nextValue, schema);
|
|
207
|
+
}
|
|
208
|
+
return nextValue;
|
|
209
|
+
}
|
|
210
|
+
function getValidator(schema) {
|
|
211
|
+
const key = schema;
|
|
212
|
+
const cached = validatorCache.get(key);
|
|
213
|
+
if (cached) {
|
|
214
|
+
return cached;
|
|
215
|
+
}
|
|
216
|
+
const validator = Compile(schema);
|
|
217
|
+
validatorCache.set(key, validator);
|
|
218
|
+
return validator;
|
|
219
|
+
}
|
|
220
|
+
function formatValidationPath(error) {
|
|
221
|
+
if (error.keyword === "required") {
|
|
222
|
+
const requiredProperties = error.params.requiredProperties;
|
|
223
|
+
const requiredProperty = requiredProperties?.[0];
|
|
224
|
+
if (requiredProperty) {
|
|
225
|
+
const basePath = error.instancePath.replace(/^\//, "").replace(/\//g, ".");
|
|
226
|
+
return basePath ? `${basePath}.${requiredProperty}` : requiredProperty;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const path = error.instancePath.replace(/^\//, "").replace(/\//g, ".");
|
|
230
|
+
return path || "root";
|
|
25
231
|
}
|
|
26
232
|
/**
|
|
27
233
|
* Finds a tool by name and validates the tool call arguments against its TypeBox schema
|
|
@@ -45,26 +251,29 @@ export function validateToolCall(tools, toolCall) {
|
|
|
45
251
|
* @throws Error with formatted message if validation fails
|
|
46
252
|
*/
|
|
47
253
|
export function validateToolArguments(tool, toolCall) {
|
|
48
|
-
// Skip validation in browser extension environment (CSP restrictions prevent AJV from working)
|
|
49
|
-
if (!ajv || isBrowserExtension) {
|
|
50
|
-
// Trust the LLM's output without validation
|
|
51
|
-
// Browser extensions can't use AJV due to Manifest V3 CSP restrictions
|
|
52
|
-
return toolCall.arguments;
|
|
53
|
-
}
|
|
54
|
-
// Compile the schema
|
|
55
|
-
const validate = ajv.compile(tool.parameters);
|
|
56
|
-
// Clone arguments so AJV can safely mutate for type coercion
|
|
57
254
|
const args = structuredClone(toolCall.arguments);
|
|
58
|
-
|
|
59
|
-
|
|
255
|
+
Value.Convert(tool.parameters, args);
|
|
256
|
+
const validator = getValidator(tool.parameters);
|
|
257
|
+
if (!hasTypeBoxMetadata(tool.parameters) && isJsonSchemaObject(tool.parameters)) {
|
|
258
|
+
const coerced = coerceWithJsonSchema(args, tool.parameters);
|
|
259
|
+
if (coerced !== args) {
|
|
260
|
+
if (isRecord(args) && isRecord(coerced)) {
|
|
261
|
+
for (const key of Object.keys(args)) {
|
|
262
|
+
delete args[key];
|
|
263
|
+
}
|
|
264
|
+
Object.assign(args, coerced);
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
return validator.Check(coerced) ? coerced : args;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (validator.Check(args)) {
|
|
60
272
|
return args;
|
|
61
273
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const path = err.instancePath ? err.instancePath.substring(1) : err.params.missingProperty || "root";
|
|
66
|
-
return ` - ${path}: ${err.message}`;
|
|
67
|
-
})
|
|
274
|
+
const errors = validator
|
|
275
|
+
.Errors(args)
|
|
276
|
+
.map((error) => ` - ${formatValidationPath(error)}: ${error.message}`)
|
|
68
277
|
.join("\n") || "Unknown validation error";
|
|
69
278
|
const errorMessage = `Validation failed for tool "${toolCall.name}":\n${errors}\n\nReceived arguments:\n${JSON.stringify(toolCall.arguments, null, 2)}`;
|
|
70
279
|
throw new Error(errorMessage);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/utils/validation.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,KAAK,CAAC;AAC5B,OAAO,gBAAgB,MAAM,aAAa,CAAC;AAE3C,wCAAwC;AACxC,MAAM,GAAG,GAAI,SAAiB,CAAC,OAAO,IAAI,SAAS,CAAC;AACpD,MAAM,UAAU,GAAI,gBAAwB,CAAC,OAAO,IAAI,gBAAgB,CAAC;AAIzE,qEAAqE;AACrE,2EAA2E;AAC3E,MAAM,kBAAkB,GAAG,OAAO,UAAU,KAAK,WAAW,IAAK,UAAkB,CAAC,MAAM,EAAE,OAAO,EAAE,EAAE,KAAK,SAAS,CAAC;AAEtH,kFAAkF;AAClF,qEAAqE;AACrE,IAAI,GAAG,GAAQ,IAAI,CAAC;AACpB,IAAI,CAAC,kBAAkB,EAAE,CAAC;IACzB,IAAI,CAAC;QACJ,GAAG,GAAG,IAAI,GAAG,CAAC;YACb,SAAS,EAAE,IAAI;YACf,MAAM,EAAE,KAAK;YACb,WAAW,EAAE,IAAI;SACjB,CA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AAY,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,eAAe,IAAI,MAAM,CAAC;QACrG,OAAO,OAAO,IAAI,KAAK,GAAG,CAAC,OAAO,EAAE,CAAC;IACtC,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,IAAI,0BAA0B,CAAC;IAE5C,MAAM,YAAY,GAAG,+BAA+B,QAAQ,CAAC,IAAI,OAAO,MAAM,4BAA4B,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;IAExJ,MAAM,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC;AAC/B,CAAC","sourcesContent":["import AjvModule from \"ajv\";\nimport addFormatsModule from \"ajv-formats\";\n\n// Handle both default and named exports\nconst Ajv = (AjvModule as any).default || AjvModule;\nconst addFormats = (addFormatsModule as any).default || addFormatsModule;\n\nimport type { Tool, ToolCall } from \"../types.js\";\n\n// Detect if we're in a browser extension environment with strict CSP\n// Chrome extensions with Manifest V3 don't allow eval/Function constructor\nconst isBrowserExtension = typeof globalThis !== \"undefined\" && (globalThis as any).chrome?.runtime?.id !== undefined;\n\n// Create a singleton AJV instance with formats (only if not in browser extension)\n// AJV requires 'unsafe-eval' CSP which is not allowed in Manifest V3\nlet ajv: any = null;\nif (!isBrowserExtension) {\n\ttry {\n\t\tajv = new Ajv({\n\t\t\tallErrors: true,\n\t\t\tstrict: false,\n\t\t\tcoerceTypes: true,\n\t\t});\n\t\taddFormats(ajv);\n\t} catch (_e) {\n\t\t// AJV initialization failed (likely CSP restriction)\n\t\tconsole.warn(\"AJV validation disabled due to CSP restrictions\");\n\t}\n}\n\n/**\n * Finds a tool by name and validates the tool call arguments against its TypeBox schema\n * @param tools Array of tool definitions\n * @param toolCall The tool call from the LLM\n * @returns The validated arguments\n * @throws Error if tool is not found or validation fails\n */\nexport function validateToolCall(tools: Tool[], toolCall: ToolCall): any {\n\tconst tool = tools.find((t) => t.name === toolCall.name);\n\tif (!tool) {\n\t\tthrow new Error(`Tool \"${toolCall.name}\" not found`);\n\t}\n\treturn validateToolArguments(tool, toolCall);\n}\n\n/**\n * Validates tool call arguments against the tool's TypeBox schema\n * @param tool The tool definition with TypeBox schema\n * @param toolCall The tool call from the LLM\n * @returns The validated (and potentially coerced) arguments\n * @throws Error with formatted message if validation fails\n */\nexport function validateToolArguments(tool: Tool, toolCall: ToolCall): any {\n\t// Skip validation in browser extension environment (CSP restrictions prevent AJV from working)\n\tif (!ajv || isBrowserExtension) {\n\t\t// Trust the LLM's output without validation\n\t\t// Browser extensions can't use AJV due to Manifest V3 CSP restrictions\n\t\treturn toolCall.arguments;\n\t}\n\n\t// Compile the schema\n\tconst validate = ajv.compile(tool.parameters);\n\n\t// Clone arguments so AJV can safely mutate for type coercion\n\tconst args = structuredClone(toolCall.arguments);\n\n\t// Validate the arguments (AJV mutates args in-place for type coercion)\n\tif (validate(args)) {\n\t\treturn args;\n\t}\n\n\t// Format validation errors nicely\n\tconst errors =\n\t\tvalidate.errors\n\t\t\t?.map((err: any) => {\n\t\t\t\tconst path = err.instancePath ? err.instancePath.substring(1) : err.params.missingProperty || \"root\";\n\t\t\t\treturn ` - ${path}: ${err.message}`;\n\t\t\t})\n\t\t\t.join(\"\\n\") || \"Unknown validation error\";\n\n\tconst errorMessage = `Validation failed for tool \"${toolCall.name}\":\\n${errors}\\n\\nReceived arguments:\\n${JSON.stringify(toolCall.arguments, null, 2)}`;\n\n\tthrow new Error(errorMessage);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/utils/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAGtC,MAAM,cAAc,GAAG,IAAI,OAAO,EAAsC,CAAC;AACzE,MAAM,YAAY,GAAG,MAAM,CAAC,GA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AE,CAAC;gBACzC,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;oBACrC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;gBAClB,CAAC;gBACD,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;YAC9B,CAAC;iBAAM,CAAC;gBACP,OAAO,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;YAClD,CAAC;QACF,CAAC;IACF,CAAC;IAED,IAAI,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;QAC3B,OAAO,IAAI,CAAC;IACb,CAAC;IAED,MAAM,MAAM,GACX,SAAS;SACP,MAAM,CAAC,IAAI,CAAC;SACZ,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,oBAAoB,CAAC,KAAK,CAAC,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC;SACtE,IAAI,CAAC,IAAI,CAAC,IAAI,0BAA0B,CAAC;IAE5C,MAAM,YAAY,GAAG,+BAA+B,QAAQ,CAAC,IAAI,OAAO,MAAM,4BAA4B,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;IAExJ,MAAM,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC;AAAA,CAC9B","sourcesContent":["import { Compile } from \"typebox/compile\";\nimport type { TLocalizedValidationError } from \"typebox/error\";\nimport { Value } from \"typebox/value\";\nimport type { Tool, ToolCall } from \"../types.js\";\n\nconst validatorCache = new WeakMap<object, ReturnType<typeof Compile>>();\nconst TYPEBOX_KIND = Symbol.for(\"TypeBox.Kind\");\n\ninterface JsonSchemaObject {\n\ttype?: string | string[];\n\tproperties?: Record<string, JsonSchemaObject>;\n\titems?: JsonSchemaObject | JsonSchemaObject[];\n\tadditionalProperties?: boolean | JsonSchemaObject;\n\tallOf?: JsonSchemaObject[];\n\tanyOf?: JsonSchemaObject[];\n\toneOf?: JsonSchemaObject[];\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null;\n}\n\nfunction isJsonSchemaObject(value: unknown): value is JsonSchemaObject {\n\treturn isRecord(value);\n}\n\nfunction hasTypeBoxMetadata(schema: unknown): boolean {\n\treturn isRecord(schema) && Object.getOwnPropertySymbols(schema).includes(TYPEBOX_KIND);\n}\n\nfunction getSchemaTypes(schema: JsonSchemaObject): string[] {\n\tif (typeof schema.type === \"string\") {\n\t\treturn [schema.type];\n\t}\n\tif (Array.isArray(schema.type)) {\n\t\treturn schema.type.filter((type): type is string => typeof type === \"string\");\n\t}\n\treturn [];\n}\n\nfunction matchesJsonType(value: unknown, type: string): boolean {\n\tswitch (type) {\n\t\tcase \"number\":\n\t\t\treturn typeof value === \"number\";\n\t\tcase \"integer\":\n\t\t\treturn typeof value === \"number\" && Number.isInteger(value);\n\t\tcase \"boolean\":\n\t\t\treturn typeof value === \"boolean\";\n\t\tcase \"string\":\n\t\t\treturn typeof value === \"string\";\n\t\tcase \"null\":\n\t\t\treturn value === null;\n\t\tcase \"array\":\n\t\t\treturn Array.isArray(value);\n\t\tcase \"object\":\n\t\t\treturn isRecord(value) && !Array.isArray(value);\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}\n\nfunction isValidatorSchema(value: unknown): value is Tool[\"parameters\"] {\n\treturn isRecord(value);\n}\n\nfunction getSubSchemaValidator(schema: JsonSchemaObject): ReturnType<typeof Compile> | undefined {\n\tif (!isValidatorSchema(schema)) {\n\t\treturn undefined;\n\t}\n\ttry {\n\t\treturn getValidator(schema);\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction coercePrimitiveByType(value: unknown, type: string): unknown {\n\tswitch (type) {\n\t\tcase \"number\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\tif (typeof value === \"string\" && value.trim() !== \"\") {\n\t\t\t\tconst parsed = Number(value);\n\t\t\t\tif (Number.isFinite(parsed)) {\n\t\t\t\t\treturn parsed;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (typeof value === \"boolean\") {\n\t\t\t\treturn value ? 1 : 0;\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"integer\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\tif (typeof value === \"string\" && value.trim() !== \"\") {\n\t\t\t\tconst parsed = Number(value);\n\t\t\t\tif (Number.isInteger(parsed)) {\n\t\t\t\t\treturn parsed;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (typeof value === \"boolean\") {\n\t\t\t\treturn value ? 1 : 0;\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"boolean\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tif (typeof value === \"string\") {\n\t\t\t\tif (value === \"true\") {\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tif (value === \"false\") {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (typeof value === \"number\") {\n\t\t\t\tif (value === 1) {\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tif (value === 0) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"string\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn \"\";\n\t\t\t}\n\t\t\tif (typeof value === \"number\" || typeof value === \"boolean\") {\n\t\t\t\treturn String(value);\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"null\": {\n\t\t\tif (value === \"\" || value === 0 || value === false) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tdefault:\n\t\t\treturn value;\n\t}\n}\n\nfunction applySchemaObjectCoercion(value: Record<string, unknown>, schema: JsonSchemaObject): void {\n\tconst properties = schema.properties;\n\tconst definedKeys = new Set<string>(properties ? Object.keys(properties) : []);\n\n\tif (properties) {\n\t\tfor (const [key, propertySchema] of Object.entries(properties)) {\n\t\t\tif (!(key in value)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvalue[key] = coerceWithJsonSchema(value[key], propertySchema);\n\t\t}\n\t}\n\n\tif (schema.additionalProperties && isJsonSchemaObject(schema.additionalProperties)) {\n\t\tfor (const [key, propertyValue] of Object.entries(value)) {\n\t\t\tif (definedKeys.has(key)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvalue[key] = coerceWithJsonSchema(propertyValue, schema.additionalProperties);\n\t\t}\n\t}\n}\n\nfunction applySchemaArrayCoercion(value: unknown[], schema: JsonSchemaObject): void {\n\tif (Array.isArray(schema.items)) {\n\t\tfor (let index = 0; index < value.length; index++) {\n\t\t\tconst itemSchema = schema.items[index];\n\t\t\tif (!itemSchema) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvalue[index] = coerceWithJsonSchema(value[index], itemSchema);\n\t\t}\n\t\treturn;\n\t}\n\n\tif (isJsonSchemaObject(schema.items)) {\n\t\tfor (let index = 0; index < value.length; index++) {\n\t\t\tvalue[index] = coerceWithJsonSchema(value[index], schema.items);\n\t\t}\n\t}\n}\n\nfunction coerceWithUnionSchema(value: unknown, schemas: JsonSchemaObject[]): unknown {\n\tfor (const schema of schemas) {\n\t\tconst candidate = structuredClone(value);\n\t\tconst coerced = coerceWithJsonSchema(candidate, schema);\n\t\tconst validator = getSubSchemaValidator(schema);\n\t\tif (validator?.Check(coerced)) {\n\t\t\treturn coerced;\n\t\t}\n\t}\n\treturn value;\n}\n\nfunction coerceWithJsonSchema(value: unknown, schema: JsonSchemaObject): unknown {\n\tlet nextValue = value;\n\n\tif (Array.isArray(schema.allOf)) {\n\t\tfor (const nested of schema.allOf) {\n\t\t\tnextValue = coerceWithJsonSchema(nextValue, nested);\n\t\t}\n\t}\n\n\tif (Array.isArray(schema.anyOf)) {\n\t\tnextValue = coerceWithUnionSchema(nextValue, schema.anyOf);\n\t}\n\n\tif (Array.isArray(schema.oneOf)) {\n\t\tnextValue = coerceWithUnionSchema(nextValue, schema.oneOf);\n\t}\n\n\tconst schemaTypes = getSchemaTypes(schema);\n\tconst matchesUnionMember =\n\t\tschemaTypes.length > 1 && schemaTypes.some((schemaType) => matchesJsonType(nextValue, schemaType));\n\tif (schemaTypes.length > 0 && !matchesUnionMember) {\n\t\tfor (const schemaType of schemaTypes) {\n\t\t\tconst candidate = coercePrimitiveByType(nextValue, schemaType);\n\t\t\tif (candidate !== nextValue) {\n\t\t\t\tnextValue = candidate;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (schemaTypes.includes(\"object\") && isRecord(nextValue) && !Array.isArray(nextValue)) {\n\t\tapplySchemaObjectCoercion(nextValue, schema);\n\t}\n\n\tif (schemaTypes.includes(\"array\") && Array.isArray(nextValue)) {\n\t\tapplySchemaArrayCoercion(nextValue, schema);\n\t}\n\n\treturn nextValue;\n}\n\nfunction getValidator(schema: Tool[\"parameters\"]): ReturnType<typeof Compile> {\n\tconst key = schema as object;\n\tconst cached = validatorCache.get(key);\n\tif (cached) {\n\t\treturn cached;\n\t}\n\tconst validator = Compile(schema);\n\tvalidatorCache.set(key, validator);\n\treturn validator;\n}\n\nfunction formatValidationPath(error: TLocalizedValidationError): string {\n\tif (error.keyword === \"required\") {\n\t\tconst requiredProperties = (error.params as { requiredProperties?: string[] }).requiredProperties;\n\t\tconst requiredProperty = requiredProperties?.[0];\n\t\tif (requiredProperty) {\n\t\t\tconst basePath = error.instancePath.replace(/^\\//, \"\").replace(/\\//g, \".\");\n\t\t\treturn basePath ? `${basePath}.${requiredProperty}` : requiredProperty;\n\t\t}\n\t}\n\tconst path = error.instancePath.replace(/^\\//, \"\").replace(/\\//g, \".\");\n\treturn path || \"root\";\n}\n\n/**\n * Finds a tool by name and validates the tool call arguments against its TypeBox schema\n * @param tools Array of tool definitions\n * @param toolCall The tool call from the LLM\n * @returns The validated arguments\n * @throws Error if tool is not found or validation fails\n */\nexport function validateToolCall(tools: Tool[], toolCall: ToolCall): any {\n\tconst tool = tools.find((t) => t.name === toolCall.name);\n\tif (!tool) {\n\t\tthrow new Error(`Tool \"${toolCall.name}\" not found`);\n\t}\n\treturn validateToolArguments(tool, toolCall);\n}\n\n/**\n * Validates tool call arguments against the tool's TypeBox schema\n * @param tool The tool definition with TypeBox schema\n * @param toolCall The tool call from the LLM\n * @returns The validated (and potentially coerced) arguments\n * @throws Error with formatted message if validation fails\n */\nexport function validateToolArguments(tool: Tool, toolCall: ToolCall): any {\n\tconst args = structuredClone(toolCall.arguments);\n\tValue.Convert(tool.parameters, args);\n\n\tconst validator = getValidator(tool.parameters);\n\tif (!hasTypeBoxMetadata(tool.parameters) && isJsonSchemaObject(tool.parameters)) {\n\t\tconst coerced = coerceWithJsonSchema(args, tool.parameters);\n\t\tif (coerced !== args) {\n\t\t\tif (isRecord(args) && isRecord(coerced)) {\n\t\t\t\tfor (const key of Object.keys(args)) {\n\t\t\t\t\tdelete args[key];\n\t\t\t\t}\n\t\t\t\tObject.assign(args, coerced);\n\t\t\t} else {\n\t\t\t\treturn validator.Check(coerced) ? coerced : args;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (validator.Check(args)) {\n\t\treturn args;\n\t}\n\n\tconst errors =\n\t\tvalidator\n\t\t\t.Errors(args)\n\t\t\t.map((error) => ` - ${formatValidationPath(error)}: ${error.message}`)\n\t\t\t.join(\"\\n\") || \"Unknown validation error\";\n\n\tconst errorMessage = `Validation failed for tool \"${toolCall.name}\":\\n${errors}\\n\\nReceived arguments:\\n${JSON.stringify(toolCall.arguments, null, 2)}`;\n\n\tthrow new Error(errorMessage);\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "phi-code-ai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.74.0",
|
|
4
4
|
"description": "Unified LLM API with automatic model discovery and provider configuration",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -10,13 +10,45 @@
|
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"import": "./dist/index.js"
|
|
12
12
|
},
|
|
13
|
+
"./anthropic": {
|
|
14
|
+
"types": "./dist/providers/anthropic.d.ts",
|
|
15
|
+
"import": "./dist/providers/anthropic.js"
|
|
16
|
+
},
|
|
17
|
+
"./azure-openai-responses": {
|
|
18
|
+
"types": "./dist/providers/azure-openai-responses.d.ts",
|
|
19
|
+
"import": "./dist/providers/azure-openai-responses.js"
|
|
20
|
+
},
|
|
21
|
+
"./google": {
|
|
22
|
+
"types": "./dist/providers/google.d.ts",
|
|
23
|
+
"import": "./dist/providers/google.js"
|
|
24
|
+
},
|
|
25
|
+
"./google-vertex": {
|
|
26
|
+
"types": "./dist/providers/google-vertex.d.ts",
|
|
27
|
+
"import": "./dist/providers/google-vertex.js"
|
|
28
|
+
},
|
|
29
|
+
"./mistral": {
|
|
30
|
+
"types": "./dist/providers/mistral.d.ts",
|
|
31
|
+
"import": "./dist/providers/mistral.js"
|
|
32
|
+
},
|
|
33
|
+
"./openai-codex-responses": {
|
|
34
|
+
"types": "./dist/providers/openai-codex-responses.d.ts",
|
|
35
|
+
"import": "./dist/providers/openai-codex-responses.js"
|
|
36
|
+
},
|
|
37
|
+
"./openai-completions": {
|
|
38
|
+
"types": "./dist/providers/openai-completions.d.ts",
|
|
39
|
+
"import": "./dist/providers/openai-completions.js"
|
|
40
|
+
},
|
|
41
|
+
"./openai-responses": {
|
|
42
|
+
"types": "./dist/providers/openai-responses.d.ts",
|
|
43
|
+
"import": "./dist/providers/openai-responses.js"
|
|
44
|
+
},
|
|
13
45
|
"./oauth": {
|
|
14
46
|
"types": "./dist/oauth.d.ts",
|
|
15
47
|
"import": "./dist/oauth.js"
|
|
16
48
|
},
|
|
17
49
|
"./bedrock-provider": {
|
|
18
|
-
"types": "./bedrock-provider.d.ts",
|
|
19
|
-
"import": "./bedrock-provider.js"
|
|
50
|
+
"types": "./dist/bedrock-provider.d.ts",
|
|
51
|
+
"import": "./dist/bedrock-provider.js"
|
|
20
52
|
}
|
|
21
53
|
},
|
|
22
54
|
"bin": {
|
|
@@ -24,31 +56,29 @@
|
|
|
24
56
|
},
|
|
25
57
|
"files": [
|
|
26
58
|
"dist",
|
|
27
|
-
"bedrock-provider.js",
|
|
28
|
-
"bedrock-provider.d.ts",
|
|
29
59
|
"README.md"
|
|
30
60
|
],
|
|
31
61
|
"scripts": {
|
|
32
62
|
"clean": "shx rm -rf dist",
|
|
33
63
|
"generate-models": "npx tsx scripts/generate-models.ts",
|
|
34
|
-
"
|
|
64
|
+
"generate-image-models": "npx tsx scripts/generate-image-models.ts",
|
|
65
|
+
"build": "npm run generate-models && npm run generate-image-models && tsgo -p tsconfig.build.json",
|
|
35
66
|
"dev": "tsgo -p tsconfig.build.json --watch --preserveWatchOutput",
|
|
36
67
|
"dev:tsc": "tsgo -p tsconfig.build.json --watch --preserveWatchOutput",
|
|
37
68
|
"test": "vitest --run",
|
|
38
69
|
"prepublishOnly": "npm run clean && npm run build"
|
|
39
70
|
},
|
|
40
71
|
"dependencies": {
|
|
41
|
-
"@anthropic-ai/sdk": "^0.
|
|
42
|
-
"@aws-sdk/client-bedrock-runtime": "^3.
|
|
72
|
+
"@anthropic-ai/sdk": "^0.91.1",
|
|
73
|
+
"@aws-sdk/client-bedrock-runtime": "^3.1030.0",
|
|
43
74
|
"@google/genai": "^1.40.0",
|
|
44
|
-
"@mistralai/mistralai": "
|
|
45
|
-
"@sinclair/typebox": "^0.34.41",
|
|
46
|
-
"ajv": "^8.17.1",
|
|
47
|
-
"ajv-formats": "^3.0.1",
|
|
75
|
+
"@mistralai/mistralai": "^2.2.0",
|
|
48
76
|
"chalk": "^5.6.2",
|
|
49
77
|
"openai": "6.26.0",
|
|
50
78
|
"partial-json": "^0.1.7",
|
|
51
79
|
"proxy-agent": "^6.5.0",
|
|
80
|
+
"proxy-from-env": "^1.1.0",
|
|
81
|
+
"typebox": "^1.1.24",
|
|
52
82
|
"undici": "^7.19.1",
|
|
53
83
|
"zod-to-json-schema": "^3.24.6"
|
|
54
84
|
},
|
|
@@ -66,7 +96,7 @@
|
|
|
66
96
|
"license": "MIT",
|
|
67
97
|
"repository": {
|
|
68
98
|
"type": "git",
|
|
69
|
-
"url": "git+https://github.com/
|
|
99
|
+
"url": "git+https://github.com/earendil-works/pi-mono.git",
|
|
70
100
|
"directory": "packages/ai"
|
|
71
101
|
},
|
|
72
102
|
"engines": {
|
package/bedrock-provider.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./dist/bedrock-provider.js";
|
package/bedrock-provider.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./dist/bedrock-provider.js";
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Google Gemini CLI / Antigravity provider.
|
|
3
|
-
* Shared implementation for both google-gemini-cli and google-antigravity providers.
|
|
4
|
-
* Uses the Cloud Code Assist API endpoint to access Gemini and Claude models.
|
|
5
|
-
*/
|
|
6
|
-
import type { Content, ThinkingConfig } from "@google/genai";
|
|
7
|
-
import type { Context, Model, SimpleStreamOptions, StreamFunction, StreamOptions } from "../types.js";
|
|
8
|
-
import { convertTools, mapToolChoice } from "./google-shared.js";
|
|
9
|
-
/**
|
|
10
|
-
* Thinking level for Gemini 3 models.
|
|
11
|
-
* Mirrors Google's ThinkingLevel enum values.
|
|
12
|
-
*/
|
|
13
|
-
export type GoogleThinkingLevel = "THINKING_LEVEL_UNSPECIFIED" | "MINIMAL" | "LOW" | "MEDIUM" | "HIGH";
|
|
14
|
-
export interface GoogleGeminiCliOptions extends StreamOptions {
|
|
15
|
-
toolChoice?: "auto" | "none" | "any";
|
|
16
|
-
/**
|
|
17
|
-
* Thinking/reasoning configuration.
|
|
18
|
-
* - Gemini 2.x models: use `budgetTokens` to set the thinking budget
|
|
19
|
-
* - Gemini 3 models (gemini-3-pro-*, gemini-3-flash-*): use `level` instead
|
|
20
|
-
*
|
|
21
|
-
* When using `streamSimple`, this is handled automatically based on the model.
|
|
22
|
-
*/
|
|
23
|
-
thinking?: {
|
|
24
|
-
enabled: boolean;
|
|
25
|
-
/** Thinking budget in tokens. Use for Gemini 2.x models. */
|
|
26
|
-
budgetTokens?: number;
|
|
27
|
-
/** Thinking level. Use for Gemini 3 models (LOW/HIGH for Pro, MINIMAL/LOW/MEDIUM/HIGH for Flash). */
|
|
28
|
-
level?: GoogleThinkingLevel;
|
|
29
|
-
};
|
|
30
|
-
projectId?: string;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Extract retry delay from Gemini error response (in milliseconds).
|
|
34
|
-
* Checks headers first (Retry-After, x-ratelimit-reset, x-ratelimit-reset-after),
|
|
35
|
-
* then parses body patterns like:
|
|
36
|
-
* - "Your quota will reset after 39s"
|
|
37
|
-
* - "Your quota will reset after 18h31m10s"
|
|
38
|
-
* - "Please retry in Xs" or "Please retry in Xms"
|
|
39
|
-
* - "retryDelay": "34.074824224s" (JSON field)
|
|
40
|
-
*/
|
|
41
|
-
export declare function extractRetryDelay(errorText: string, response?: Response | Headers): number | undefined;
|
|
42
|
-
interface CloudCodeAssistRequest {
|
|
43
|
-
project: string;
|
|
44
|
-
model: string;
|
|
45
|
-
request: {
|
|
46
|
-
contents: Content[];
|
|
47
|
-
sessionId?: string;
|
|
48
|
-
systemInstruction?: {
|
|
49
|
-
role?: string;
|
|
50
|
-
parts: {
|
|
51
|
-
text: string;
|
|
52
|
-
}[];
|
|
53
|
-
};
|
|
54
|
-
generationConfig?: {
|
|
55
|
-
maxOutputTokens?: number;
|
|
56
|
-
temperature?: number;
|
|
57
|
-
thinkingConfig?: ThinkingConfig;
|
|
58
|
-
};
|
|
59
|
-
tools?: ReturnType<typeof convertTools>;
|
|
60
|
-
toolConfig?: {
|
|
61
|
-
functionCallingConfig: {
|
|
62
|
-
mode: ReturnType<typeof mapToolChoice>;
|
|
63
|
-
};
|
|
64
|
-
};
|
|
65
|
-
};
|
|
66
|
-
requestType?: string;
|
|
67
|
-
userAgent?: string;
|
|
68
|
-
requestId?: string;
|
|
69
|
-
}
|
|
70
|
-
export declare const streamGoogleGeminiCli: StreamFunction<"google-gemini-cli", GoogleGeminiCliOptions>;
|
|
71
|
-
export declare const streamSimpleGoogleGeminiCli: StreamFunction<"google-gemini-cli", SimpleStreamOptions>;
|
|
72
|
-
export declare function buildRequest(model: Model<"google-gemini-cli">, context: Context, projectId: string, options?: GoogleGeminiCliOptions, isAntigravity?: boolean): CloudCodeAssistRequest;
|
|
73
|
-
export {};
|
|
74
|
-
//# sourceMappingURL=google-gemini-cli.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"google-gemini-cli.d.ts","sourceRoot":"","sources":["../../src/providers/google-gemini-cli.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE7D,OAAO,KAAK,EAGX,OAAO,EACP,KAAK,EACL,mBAAmB,EACnB,cAAc,EACd,aAAa,EAMb,MAAM,aAAa,CAAC;AAGrB,OAAO,EAEN,YAAY,EAGZ,aAAa,EAEb,MAAM,oBAAoB,CAAC;AAG5B;;;GAGG;AACH,MAAM,MAAM,mBAAmB,GAAG,4BAA4B,GAAG,SAAS,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;AAEvG,MAAM,WAAW,sBAAuB,SAAQ,aAAa;IAC5D,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;IACrC;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE;QACV,OAAO,EAAE,OAAO,CAAC;QACjB,4DAA4D;QAC5D,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,qGAAqG;QACrG,KAAK,CAAC,E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