@benjamolina/pi-antigravity-guard 0.5.0 → 0.6.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.
@@ -8,14 +8,48 @@ export function hasToolContext(context) {
8
8
  if (Array.isArray(tools) && tools.length > 0)
9
9
  return true;
10
10
  const messages = value(context, "messages");
11
- return Array.isArray(messages) && messages.some((message) => {
11
+ if (!Array.isArray(messages))
12
+ return false;
13
+ const activeTools = new Map();
14
+ for (let index = 0; index < messages.length; index++) {
15
+ const descriptor = Object.getOwnPropertyDescriptor(messages, String(index));
16
+ if (!descriptor || !("value" in descriptor))
17
+ continue;
18
+ const message = descriptor.value;
12
19
  if (!isRecord(message))
13
- return false;
14
- if (value(message, "role") === "toolResult")
20
+ continue;
21
+ const role = value(message, "role");
22
+ if (role === "toolResult")
15
23
  return true;
24
+ if (role === "system") {
25
+ const removed = value(message, "toolsRemoved");
26
+ if (Array.isArray(removed)) {
27
+ for (let r = 0; r < removed.length; r++) {
28
+ const rd = Object.getOwnPropertyDescriptor(removed, String(r));
29
+ const item = rd && "value" in rd ? rd.value : undefined;
30
+ const name = isRecord(item) ? value(item, "name") : undefined;
31
+ if (typeof name === "string" && name)
32
+ activeTools.delete(name);
33
+ }
34
+ }
35
+ const added = value(message, "toolsAdded");
36
+ if (Array.isArray(added)) {
37
+ for (let a = 0; a < added.length; a++) {
38
+ const ad = Object.getOwnPropertyDescriptor(added, String(a));
39
+ if (!ad || !("value" in ad))
40
+ continue;
41
+ const item = ad.value;
42
+ const name = isRecord(item) ? value(item, "name") : undefined;
43
+ activeTools.set(typeof name === "string" && name ? name : Symbol("tool"), item);
44
+ }
45
+ }
46
+ continue;
47
+ }
16
48
  const content = value(message, "content");
17
- return Array.isArray(content) && content.some((part) => isRecord(part) && value(part, "type") === "toolCall");
18
- });
49
+ if (Array.isArray(content) && content.some((part) => isRecord(part) && value(part, "type") === "toolCall"))
50
+ return true;
51
+ }
52
+ return activeTools.size > 0;
19
53
  }
20
54
  export function prepareToolContext(tools, choice, schemaProfile) {
21
55
  if (schemaProfile !== GEMINI_PARAMETERS_JSON_SCHEMA_PROFILE && schemaProfile !== CLAUDE_CUSTOM_PARAMETERS_PROFILE)
@@ -94,33 +128,58 @@ export function replayToolHistory(messages, serializePart, onDiagnostics, policy
94
128
  if (value(current, "addedToolNames") !== undefined && dense(value(current, "addedToolNames")).length)
95
129
  history("PI_TOOL_CALL_INVALID");
96
130
  const textParts = [];
97
- const imageParts = [];
131
+ const mediaParts = [];
98
132
  for (const item of dense(value(current, "content"))) {
99
133
  const part = record(item);
100
- if (value(part, "type") === "text" && typeof value(part, "text") === "string") {
134
+ const partType = value(part, "type");
135
+ if (partType === "text" && typeof value(part, "text") === "string") {
101
136
  textParts.push(value(part, "text"));
102
137
  }
103
- else if (value(part, "type") === "image") {
104
- const rawData = value(part, "data") ?? (isRecord(value(part, "source")) ? value(record(value(part, "source")), "data") : undefined);
105
- if (typeof rawData === "string" && rawData) {
106
- const rawMime = value(part, "mimeType") ?? (isRecord(value(part, "source")) ? value(record(value(part, "source")), "media_type") : undefined);
107
- const mimeType = typeof rawMime === "string" && rawMime ? rawMime : "image/jpeg";
108
- const cleanedData = rawData.replace(/^data:[^;]+;base64,/s, "").trim();
109
- imageParts.push({ inlineData: { mimeType, data: cleanedData } });
138
+ else if (partType === "image" || partType === "document" || partType === "file") {
139
+ if (policy?.allowMultimodal === false) {
140
+ history("PI_TOOL_RESULT_MEDIA_UNSUPPORTED");
141
+ }
142
+ const source = isRecord(value(part, "source")) ? record(value(part, "source")) : undefined;
143
+ const rawData = value(part, "data") ?? (source ? value(source, "data") : undefined);
144
+ if (typeof rawData !== "string" || !rawData) {
145
+ history("PI_TOOL_RESULT_MEDIA_UNSUPPORTED");
146
+ }
147
+ const dataUriMatch = /^data:([^;,]+);base64,/is.exec(rawData);
148
+ const rawMime = value(part, "mimeType") ?? (source ? value(source, "media_type") ?? value(source, "mimeType") : undefined) ?? dataUriMatch?.[1];
149
+ const defaultMime = partType === "image" ? "image/jpeg" : "application/pdf";
150
+ const mimeType = typeof rawMime === "string" && rawMime.trim() ? rawMime.trim() : defaultMime;
151
+ const lowerMime = mimeType.toLowerCase();
152
+ const isPdf = lowerMime === "application/pdf";
153
+ const isImage = lowerMime.startsWith("image/");
154
+ if (!isPdf && !isImage) {
155
+ history("PI_TOOL_RESULT_MEDIA_UNSUPPORTED");
156
+ }
157
+ if ((partType === "document" || partType === "file") && !isPdf) {
158
+ history("PI_TOOL_RESULT_MEDIA_UNSUPPORTED");
159
+ }
160
+ const cleanedData = rawData.replace(/^data:[^;]+;base64,/is, "").replace(/\s+/g, "");
161
+ if (!cleanedData || cleanedData.length % 4 !== 0 || !/^[A-Za-z0-9+/]+={0,2}$/.test(cleanedData)) {
162
+ history("PI_TOOL_RESULT_MEDIA_UNSUPPORTED");
110
163
  }
164
+ mediaParts.push({ inlineData: { mimeType, data: cleanedData } });
111
165
  }
112
166
  else {
113
167
  history("PI_TOOL_RESULT_MEDIA_UNSUPPORTED");
114
168
  }
115
169
  }
116
170
  const text = textParts.join("\n\n");
117
- const outputText = text || (imageParts.length > 0 ? "[Image content]" : "");
171
+ const fallbackMediaLabel = mediaParts.length > 0
172
+ ? mediaParts.every((p) => p.inlineData.mimeType.toLowerCase() === "application/pdf")
173
+ ? "[PDF content]"
174
+ : "[Image content]"
175
+ : "";
176
+ const outputText = text || fallbackMediaLabel;
118
177
  const response = value(current, "isError") === true ? { error: outputText } : { output: outputText };
119
178
  pending.results.set(id, pending.observations ? observation(call, outputText) : { functionResponse: { ...(policy?.includeToolCallId ? { id: sanitizeToolCallId(call.id, call.name) } : {}), name: call.name, response } });
120
- if (imageParts.length > 0) {
179
+ if (mediaParts.length > 0) {
121
180
  if (!pending.extraParts)
122
181
  pending.extraParts = [];
123
- pending.extraParts.push(...imageParts);
182
+ pending.extraParts.push(...mediaParts);
124
183
  }
125
184
  results.add(id);
126
185
  continue;
@@ -149,7 +208,7 @@ export function replayToolHistory(messages, serializePart, onDiagnostics, policy
149
208
  if (value(current, "stopReason") !== "toolUse")
150
209
  history("PI_TOOL_CALL_INVALID");
151
210
  const group = [];
152
- const parts = content.flatMap((item) => {
211
+ const parts = content.flatMap((item, contentIndex) => {
153
212
  const part = record(item);
154
213
  if (value(part, "type") !== "toolCall") {
155
214
  if (isEmptyUnsignedText(part))
@@ -165,9 +224,10 @@ export function replayToolHistory(messages, serializePart, onDiagnostics, policy
165
224
  if (typeof args !== "object" || Array.isArray(args) || args === null)
166
225
  history("PI_TOOL_CALL_INVALID");
167
226
  calls.add(id);
227
+ const callIndex = group.length;
168
228
  const call = { id, name, args };
169
229
  group.push(call);
170
- return [{ call, signature: policy?.toolCallSignature(part, current), signaturePresent: value(part, "thoughtSignature") !== undefined }];
230
+ return [{ call, signature: policy?.toolCallSignature(part, current, callIndex, contentIndex), signaturePresent: value(part, "thoughtSignature") !== undefined, callIndex }];
171
231
  });
172
232
  const toolParts = parts.filter((part) => "call" in part);
173
233
  const callsAreSigned = policy?.requireSignedToolCalls === true && policy.isSameModel(current) && Boolean(toolParts[0]?.signature) && toolParts.every((part) => !part.signaturePresent || Boolean(part.signature));
@@ -1,4 +1,6 @@
1
+ import { cleanJSONSchemaForAntigravity, toGeminiSchema } from "@benjamolina/antigravity-guard-core";
1
2
  import { type JsonObject } from "./tool-contract.ts";
3
+ export { cleanJSONSchemaForAntigravity, toGeminiSchema };
2
4
  export interface ToolDeclaration {
3
5
  readonly name: string;
4
6
  readonly description: string;
@@ -1,4 +1,6 @@
1
+ import { EMPTY_SCHEMA_PLACEHOLDER_DESCRIPTION, EMPTY_SCHEMA_PLACEHOLDER_NAME, cleanJSONSchemaForAntigravity, toGeminiSchema, } from "@benjamolina/antigravity-guard-core";
1
2
  import { denseArray, fail, freeze, ownData, plainObject } from "./tool-contract.js";
3
+ export { cleanJSONSchemaForAntigravity, toGeminiSchema };
2
4
  const MAX_SCHEMA_DEPTH = 32;
3
5
  const MAX_SCHEMA_NODES = 2048;
4
6
  const MAX_SCHEMA_BYTES = 256 * 1024;
@@ -32,7 +34,7 @@ function normalizeDeclaration(value, index, names) {
32
34
  if (typeof description !== "string" || !description.trim())
33
35
  fail("PI_TOOL_DECLARATION_INVALID", name, `${path}.description`);
34
36
  const constrainedSampling = ownData(tool, "constrainedSampling", name, path);
35
- if (constrainedSampling !== undefined && constrainedSampling !== false)
37
+ if (!isAdmittedConstrainedSampling(constrainedSampling, name, `${path}.constrainedSampling`))
36
38
  fail("PI_TOOL_CONSTRAINED_SAMPLING_UNSUPPORTED", name, `${path}.constrainedSampling`);
37
39
  const root = plainObject(ownData(tool, "parameters", name, path), name, "$");
38
40
  if (ownData(root, "type", name, "$") !== "object")
@@ -47,6 +49,14 @@ function normalizeDeclaration(value, index, names) {
47
49
  fail("PI_TOOL_SCHEMA_LIMIT", name, "$");
48
50
  return freeze({ name, description, parameters });
49
51
  }
52
+ function isAdmittedConstrainedSampling(value, declaration, path) {
53
+ if (value === undefined || value === false)
54
+ return true;
55
+ if (typeof value !== "object" || value === null || Array.isArray(value))
56
+ return false;
57
+ const config = plainObject(value, declaration, path);
58
+ return ownData(config, "type", declaration, path) === "json_schema" && ownData(config, "strict", declaration, path) === "prefer";
59
+ }
50
60
  function snapshotJson(value, declaration, path, state, depth = 0) {
51
61
  if (depth > MAX_SCHEMA_DEPTH || ++state.nodes > MAX_SCHEMA_NODES)
52
62
  fail("PI_TOOL_SCHEMA_LIMIT", declaration, path);
@@ -210,34 +220,26 @@ function isJsonObject(value) {
210
220
  function define(target, key, value) {
211
221
  Object.defineProperty(target, key, { value, enumerable: true, configurable: false, writable: false });
212
222
  }
213
- const CUSTOM_TOOL_SCHEMA_ALLOW = new Set([
214
- "type",
215
- "description",
216
- "properties",
217
- "required",
218
- "items",
219
- "enum",
220
- ]);
221
223
  export function normalizeCustomToolSchema(schema) {
222
224
  if (!schema || typeof schema !== "object" || Array.isArray(schema))
223
225
  return {};
224
- const out = {};
225
- for (const [key, value] of Object.entries(schema)) {
226
- if (!CUSTOM_TOOL_SCHEMA_ALLOW.has(key))
227
- continue;
228
- if (key === "properties" && value && typeof value === "object" && !Array.isArray(value)) {
229
- const props = {};
230
- for (const [propName, propSchema] of Object.entries(value)) {
231
- props[propName] = normalizeCustomToolSchema(propSchema);
232
- }
233
- out.properties = props;
234
- continue;
235
- }
236
- if (key === "items") {
237
- out.items = normalizeCustomToolSchema(value);
238
- continue;
239
- }
240
- out[key] = value;
226
+ const cleaned = cleanJSONSchemaForAntigravity(schema);
227
+ if (!cleaned || typeof cleaned !== "object" || Array.isArray(cleaned))
228
+ return {};
229
+ const hasProperties = cleaned.properties !== null &&
230
+ typeof cleaned.properties === "object" &&
231
+ !Array.isArray(cleaned.properties) &&
232
+ Object.keys(cleaned.properties).length > 0;
233
+ if (cleaned.type === "object" && !hasProperties) {
234
+ cleaned.properties = {
235
+ [EMPTY_SCHEMA_PLACEHOLDER_NAME]: {
236
+ type: "boolean",
237
+ description: EMPTY_SCHEMA_PLACEHOLDER_DESCRIPTION,
238
+ },
239
+ };
240
+ cleaned.required = Array.isArray(cleaned.required)
241
+ ? Array.from(new Set([...cleaned.required, EMPTY_SCHEMA_PLACEHOLDER_NAME]))
242
+ : [EMPTY_SCHEMA_PLACEHOLDER_NAME];
241
243
  }
242
- return out;
244
+ return cleaned;
243
245
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@benjamolina/pi-antigravity-guard",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Pi adapter for Antigravity Guard",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -29,7 +29,7 @@
29
29
  ]
30
30
  },
31
31
  "dependencies": {
32
- "@benjamolina/antigravity-guard-core": "0.2.0"
32
+ "@benjamolina/antigravity-guard-core": "0.3.0"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "@earendil-works/pi-ai": "*",