pi-gemini-app 0.1.4 → 0.1.7
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 +2 -0
- package/extensions/pi/stream.js +21 -3
- package/extensions/pi/transcript.js +10 -8
- package/native-wire/oauth-loopback.mjs +2 -1
- package/native-wire/robin-current.mjs +2 -1
- package/package.json +3 -1
- package/src/gateway/image-download.mjs +1 -1
- package/src/gateway/image-upload.mjs +135 -26
- package/src/gateway/native-diagnostic.mjs +2 -0
- package/src/gateway/native-transport.mjs +28 -1
- package/src/gateway/outbound-proxy.mjs +320 -0
- package/src/gateway/quota-transport.mjs +2 -2
- package/src/gateway/terms.mjs +2 -2
- package/src/image-input.mjs +5 -4
- package/src/native-provider.mjs +163 -14
- package/src/openai-contract.mjs +19 -5
- package/src/prompt-tool-schema.mjs +47 -12
- package/src/responses-tool-namespaces.mjs +5 -3
- package/src/tool-protocol.mjs +2 -1
package/src/openai-contract.mjs
CHANGED
|
@@ -9,7 +9,7 @@ import { plainObject, protocolError, parseBoundedJson, compileToolPolicy, valida
|
|
|
9
9
|
import { stripSchemaAnnotations } from './prompt-tool-schema.mjs';
|
|
10
10
|
import { normalizeChatResponseFormat, normalizeResponsesText, buildStructuredOutputInstruction } from './gateway/structured-output.mjs';
|
|
11
11
|
import { isSafeWebUrl } from './gateway/native-search-metadata.mjs';
|
|
12
|
-
import { MAX_IMAGES, MAX_IMAGE_URL_BYTES, normalizeImageUrl, collectImageInputs } from './image-input.mjs';
|
|
12
|
+
import { MAX_IMAGES, MAX_IMAGE_URL_BYTES, MAX_TOTAL_IMAGE_URL_BYTES, normalizeImageUrl, collectImageInputs } from './image-input.mjs';
|
|
13
13
|
import { RESPONSE_TOOL_IDENTITIES, normalizeResponsesTools, responsesCallFields } from './responses-tool-namespaces.mjs';
|
|
14
14
|
import { toResponsesUsage } from './openai-usage.mjs';
|
|
15
15
|
|
|
@@ -83,8 +83,11 @@ function requestBudget(root) {
|
|
|
83
83
|
const visit = (value, depth, image = false) => {
|
|
84
84
|
if (++nodes > 32768 || depth > 32) fail('REQUEST_TOO_LARGE', 413);
|
|
85
85
|
if (typeof value === 'string') {
|
|
86
|
-
if (image)
|
|
87
|
-
|
|
86
|
+
if (image) {
|
|
87
|
+
const len = Buffer.byteLength(value);
|
|
88
|
+
if (len > MAX_IMAGE_URL_BYTES) fail('IMAGE_TOO_LARGE', 413);
|
|
89
|
+
imageBytes += len;
|
|
90
|
+
} else bytes += Buffer.byteLength(value);
|
|
88
91
|
}
|
|
89
92
|
else if (value === null || value === undefined || typeof value === 'boolean') bytes += 4;
|
|
90
93
|
else if (typeof value === 'number' && Number.isFinite(value)) bytes += 8;
|
|
@@ -105,7 +108,7 @@ function requestBudget(root) {
|
|
|
105
108
|
}
|
|
106
109
|
ancestors.delete(value);
|
|
107
110
|
}
|
|
108
|
-
if (bytes > REQUEST_MAX_NON_IMAGE_BYTES || imageBytes >
|
|
111
|
+
if (bytes > REQUEST_MAX_NON_IMAGE_BYTES || imageBytes > MAX_TOTAL_IMAGE_URL_BYTES) fail('REQUEST_TOO_LARGE', 413);
|
|
109
112
|
};
|
|
110
113
|
visit(root, 0);
|
|
111
114
|
}
|
|
@@ -335,7 +338,7 @@ export function normalizeChatRequest(payload, { allowAutoModel = false } = {}) {
|
|
|
335
338
|
if (!Array.isArray(payload.messages) || !payload.messages.length || payload.messages.length > MAX_MESSAGES) fail('MESSAGES_REQUIRED');
|
|
336
339
|
const pending = new Set(), seen = new Set();
|
|
337
340
|
const messages = payload.messages.map(message => {
|
|
338
|
-
keys(message, ['role', 'content', 'name', 'tool_calls', 'tool_call_id', 'refusal', 'parsed', 'annotations'], 'messages');
|
|
341
|
+
keys(message, ['role', 'content', 'name', 'tool_calls', 'tool_call_id', 'refusal', 'parsed', 'annotations', 'reasoning_content', 'reasoning'], 'messages');
|
|
339
342
|
const role = message.role;
|
|
340
343
|
if (!['system', 'developer', 'user', 'assistant', 'tool'].includes(role)) fail('UNSUPPORTED_ROLE');
|
|
341
344
|
if (message.name !== undefined && message.name !== null) {
|
|
@@ -346,6 +349,17 @@ export function normalizeChatRequest(payload, { allowAutoModel = false } = {}) {
|
|
|
346
349
|
// SDK stream accumulators attach null parsing/refusal metadata on replay.
|
|
347
350
|
// Only inert null is accepted; structured/refusal content is unsupported.
|
|
348
351
|
for (const key of ['refusal', 'parsed']) if (message[key] !== undefined && (role !== 'assistant' || message[key] !== null)) fail('UNSUPPORTED_PARAMETER', 400, `messages.${key}`);
|
|
352
|
+
// OpenAI-compatible reasoning clients (OpenClaw, DeepSeek/LiteLLM, Pi) replay
|
|
353
|
+
// assistant reasoning_content/reasoning strings or null on historical turns.
|
|
354
|
+
// Validate UTF-8/bounds and strip before canonical prompt construction.
|
|
355
|
+
for (const key of ['reasoning_content', 'reasoning']) {
|
|
356
|
+
if (message[key] !== undefined) {
|
|
357
|
+
if (role !== 'assistant' || (message[key] !== null && typeof message[key] !== 'string')) {
|
|
358
|
+
fail('UNSUPPORTED_PARAMETER', 400, `messages.${key}`);
|
|
359
|
+
}
|
|
360
|
+
if (typeof message[key] === 'string') text(message[key]);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
349
363
|
if (message.annotations !== undefined) {
|
|
350
364
|
if (role !== 'assistant') fail('UNSUPPORTED_ANNOTATIONS');
|
|
351
365
|
validateReplayAnnotations(message.annotations, { chat: true });
|
|
@@ -1,14 +1,37 @@
|
|
|
1
|
-
const
|
|
1
|
+
export const PROMPT_TOOL_LIMITS = Object.freeze({
|
|
2
|
+
bytes: 256 * 1024,
|
|
3
|
+
declarationBytes: 512 * 1024,
|
|
4
|
+
depth: 16,
|
|
5
|
+
nodes: 4096,
|
|
6
|
+
declarationNodes: 16384,
|
|
7
|
+
tools: 256,
|
|
8
|
+
work: 65536,
|
|
9
|
+
});
|
|
10
|
+
const LIMITS = PROMPT_TOOL_LIMITS;
|
|
2
11
|
|
|
3
|
-
function error(code, message = code, status = 400) {
|
|
4
|
-
const e = new Error(message); e.code = code; e.status = status;
|
|
12
|
+
function error(code, message = code, status = 400, param = null) {
|
|
13
|
+
const e = new Error(message); e.code = code; e.status = status;
|
|
14
|
+
if (param !== null) e.param = param;
|
|
15
|
+
return e;
|
|
5
16
|
}
|
|
6
17
|
const declaration = (message) => error('INVALID_TOOL_DECLARATION', message);
|
|
7
18
|
const badSchema = (message) => error('UNSUPPORTED_TOOL_SCHEMA', message);
|
|
8
19
|
|
|
9
|
-
function jsonTree(
|
|
10
|
-
|
|
11
|
-
|
|
20
|
+
function jsonTree(
|
|
21
|
+
value,
|
|
22
|
+
state,
|
|
23
|
+
depth = 0,
|
|
24
|
+
seen = new Set(),
|
|
25
|
+
maxDepth = LIMITS.depth,
|
|
26
|
+
limits = { maxNodes: LIMITS.nodes, maxBytes: LIMITS.bytes, paramPrefix: 'tools' },
|
|
27
|
+
) {
|
|
28
|
+
const { maxNodes = LIMITS.nodes, maxBytes = LIMITS.bytes, paramPrefix = 'tools' } = limits;
|
|
29
|
+
if (++state.nodes > maxNodes) throw error('TOOL_SIZE_LIMIT', 'TOOL_SIZE_LIMIT', 400, paramPrefix ? `${paramPrefix}.nodes` : null);
|
|
30
|
+
if (depth > maxDepth) throw error('TOOL_SIZE_LIMIT', 'TOOL_SIZE_LIMIT', 400, paramPrefix ? `${paramPrefix}.depth` : null);
|
|
31
|
+
const add = n => {
|
|
32
|
+
state.bytes = (state.bytes || 0) + n;
|
|
33
|
+
if (state.bytes > maxBytes) throw error('TOOL_SIZE_LIMIT', 'TOOL_SIZE_LIMIT', 400, paramPrefix ? `${paramPrefix}.bytes` : null);
|
|
34
|
+
};
|
|
12
35
|
if (typeof value === 'string') { if (Buffer.from(value).toString('utf8') !== value) throw error('INVALID_TOOL_STRING'); add(Buffer.byteLength(value)); return; }
|
|
13
36
|
if (value === null || typeof value === 'boolean') { add(4); return; }
|
|
14
37
|
if (typeof value === 'number') { if (!Number.isFinite(value)) throw declaration('Non-finite JSON number'); add(8); return; }
|
|
@@ -17,8 +40,8 @@ function jsonTree(value, state, depth = 0, seen = new Set(), maxDepth = LIMITS.d
|
|
|
17
40
|
if (!Array.isArray(value) && Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) throw declaration('Non-plain JSON object');
|
|
18
41
|
seen.add(value);
|
|
19
42
|
if (Array.isArray(value)) {
|
|
20
|
-
for (let i = 0; i < value.length; i++) { if (!Object.hasOwn(value, i)) throw declaration('Sparse array'); jsonTree(value[i], state, depth + 1, seen, maxDepth); }
|
|
21
|
-
} else for (const key of Object.keys(value)) { add(Buffer.byteLength(key)); jsonTree(value[key], state, depth + 1, seen, maxDepth); }
|
|
43
|
+
for (let i = 0; i < value.length; i++) { if (!Object.hasOwn(value, i)) throw declaration('Sparse array'); jsonTree(value[i], state, depth + 1, seen, maxDepth, limits); }
|
|
44
|
+
} else for (const key of Object.keys(value)) { add(Buffer.byteLength(key)); jsonTree(value[key], state, depth + 1, seen, maxDepth, limits); }
|
|
22
45
|
seen.delete(value);
|
|
23
46
|
}
|
|
24
47
|
function clone(v) { return Array.isArray(v) ? v.map(clone) : v && typeof v === 'object' ? Object.fromEntries(Object.entries(v).map(([k, x]) => [k, clone(x)])) : v; }
|
|
@@ -144,7 +167,7 @@ function checkSchema(root, { rejectUnsupportedKeywords = false } = {}) {
|
|
|
144
167
|
const defs = new Map();
|
|
145
168
|
const walked = new WeakSet();
|
|
146
169
|
function walk(s, depth=0, visited=new Set()) {
|
|
147
|
-
if (depth > LIMITS.depth) throw error('TOOL_SIZE_LIMIT');
|
|
170
|
+
if (depth > LIMITS.depth) throw error('TOOL_SIZE_LIMIT', 'TOOL_SIZE_LIMIT', 400, 'tools.depth');
|
|
148
171
|
if (typeof s === 'boolean') return;
|
|
149
172
|
if (!s || typeof s !== 'object' || Array.isArray(s)) throw badSchema('Schema must be an object');
|
|
150
173
|
if (visited.has(s)) throw badSchema('Cyclic schema'); visited.add(s); walked.add(s);
|
|
@@ -206,10 +229,17 @@ function checkSchema(root, { rejectUnsupportedKeywords = false } = {}) {
|
|
|
206
229
|
// constraints above are checked strictly. Arbitrary regex/pattern hints are never executed.
|
|
207
230
|
export function compilePromptTools(tools = []) {
|
|
208
231
|
if (!Array.isArray(tools)) throw declaration('Tools must be an array');
|
|
209
|
-
if (tools.length > LIMITS.tools) throw error('TOOL_SIZE_LIMIT');
|
|
210
|
-
const state={nodes:
|
|
232
|
+
if (tools.length > LIMITS.tools) throw error('TOOL_SIZE_LIMIT', 'TOOL_SIZE_LIMIT', 400, 'tools.count');
|
|
233
|
+
const state = { nodes: 0 };
|
|
234
|
+
jsonTree(tools, state, 0, new Set(), LIMITS.depth * 3 + 4, {
|
|
235
|
+
maxNodes: LIMITS.declarationNodes,
|
|
236
|
+
maxBytes: LIMITS.declarationBytes,
|
|
237
|
+
paramPrefix: 'tools',
|
|
238
|
+
});
|
|
211
239
|
let text; try { text = JSON.stringify(tools); } catch { throw declaration('Tools are not JSON serializable'); }
|
|
212
|
-
if (typeof text !== 'string' || Buffer.byteLength(text, 'utf8') > LIMITS.
|
|
240
|
+
if (typeof text !== 'string' || Buffer.byteLength(text, 'utf8') > LIMITS.declarationBytes) {
|
|
241
|
+
throw error('TOOL_SIZE_LIMIT', 'TOOL_SIZE_LIMIT', 400, 'tools.bytes');
|
|
242
|
+
}
|
|
213
243
|
const out = new Map();
|
|
214
244
|
for (const tool of tools) {
|
|
215
245
|
if (!tool || typeof tool !== 'object' || Array.isArray(tool) || tool.type !== 'function') throw error('UNSUPPORTED_TOOL_TYPE');
|
|
@@ -222,6 +252,11 @@ export function compilePromptTools(tools = []) {
|
|
|
222
252
|
if(own(fn,'description')&&typeof fn.description!=='string') throw declaration('Invalid description');
|
|
223
253
|
if(own(fn,'strict')&&typeof fn.strict!=='boolean') throw declaration('Invalid strict hint');
|
|
224
254
|
const parameters=own(fn,'parameters')?fn.parameters:{type:'object'};
|
|
255
|
+
jsonTree(parameters, { nodes: 0 }, 0, new Set(), LIMITS.depth * 3 + 4, {
|
|
256
|
+
maxNodes: LIMITS.nodes,
|
|
257
|
+
maxBytes: LIMITS.bytes,
|
|
258
|
+
paramPrefix: 'tools',
|
|
259
|
+
});
|
|
225
260
|
checkSchema(parameters);
|
|
226
261
|
out.set(fn.name,{name:fn.name,parameters:clone(parameters)});
|
|
227
262
|
}
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
// Codex 0.148.0 ResponseItem uses function_call { namespace?, name, ... }.
|
|
3
3
|
// Internal aliases never appear on the Responses wire. Symbols cannot be supplied
|
|
4
4
|
// by JSON clients and survive canonical-body/result object spreads, not JSON output.
|
|
5
|
+
import { PROMPT_TOOL_LIMITS } from './prompt-tool-schema.mjs';
|
|
5
6
|
import { plainObject, protocolError } from './tool-protocol.mjs';
|
|
6
7
|
export const RESPONSE_TOOL_IDENTITIES = Symbol('responseToolIdentities');
|
|
7
|
-
const fail = code => { throw protocolError(code, 400); };
|
|
8
|
+
const fail = (code, param = null) => { throw protocolError(code, 400, param); };
|
|
8
9
|
const keys = (value, allowed) => {
|
|
9
10
|
if (!plainObject(value)) fail('INVALID_TOOL_DECLARATION');
|
|
10
11
|
if (Object.keys(value).some(key => !allowed.includes(key))) fail('UNSUPPORTED_PARAMETER');
|
|
@@ -17,14 +18,15 @@ const name = value => {
|
|
|
17
18
|
export function normalizeResponsesTools(payload) {
|
|
18
19
|
const source = payload.tools === undefined ? [] : payload.tools;
|
|
19
20
|
if (!Array.isArray(source)) fail('INVALID_TOOL_DECLARATION');
|
|
20
|
-
if (source.length >
|
|
21
|
+
if (source.length > PROMPT_TOOL_LIMITS.tools) fail('TOOL_SIZE_LIMIT', 'tools.count');
|
|
22
|
+
if (Buffer.byteLength(JSON.stringify(source)) > PROMPT_TOOL_LIMITS.declarationBytes) fail('TOOL_SIZE_LIMIT', 'tools.bytes');
|
|
21
23
|
const declarations = [], namespaces = new Set(), reserved = new Set(), identities = new Map(), aliases = new Map();
|
|
22
24
|
const add = (tool, namespace, description) => {
|
|
23
25
|
keys(tool, ['type', 'name', 'description', 'parameters', 'strict']);
|
|
24
26
|
if (tool.type !== 'function') fail('UNSUPPORTED_TOOL_TYPE');
|
|
25
27
|
name(tool.name);
|
|
26
28
|
if (tool.description !== undefined && typeof tool.description !== 'string') fail('INVALID_TOOL_DECLARATION');
|
|
27
|
-
if (declarations.length >=
|
|
29
|
+
if (declarations.length >= PROMPT_TOOL_LIMITS.tools) fail('TOOL_SIZE_LIMIT', 'tools.count');
|
|
28
30
|
declarations.push({ tool, namespace, description });
|
|
29
31
|
if (namespace === undefined) reserved.add(tool.name);
|
|
30
32
|
};
|
package/src/tool-protocol.mjs
CHANGED
|
@@ -86,7 +86,7 @@ export function parseBoundedJson(text, { status = 400 } = {}) {
|
|
|
86
86
|
export function compileToolPolicy(tools = [], choice = 'auto') {
|
|
87
87
|
let registry;
|
|
88
88
|
try { registry = compilePromptTools(tools); }
|
|
89
|
-
catch (error) { throw protocolError(error.code || 'INVALID_TOOLS', 400); }
|
|
89
|
+
catch (error) { throw protocolError(error.code || 'INVALID_TOOLS', 400, error.param ?? null); }
|
|
90
90
|
let forced = null;
|
|
91
91
|
if (plainObject(choice)) {
|
|
92
92
|
if (Object.keys(choice).some(k => !['type', 'function'].includes(k)) || choice.type !== 'function' || !plainObject(choice.function) || Object.keys(choice.function).some(k => k !== 'name') || typeof choice.function.name !== 'string') fail('UNSUPPORTED_TOOL_CHOICE');
|
|
@@ -343,5 +343,6 @@ export function createToolCallSplitter({ newId = () => 'call_' + crypto.randomBy
|
|
|
343
343
|
return safe;
|
|
344
344
|
},
|
|
345
345
|
get toolCallCount() { return seen; },
|
|
346
|
+
get suppressed() { return suppressed; },
|
|
346
347
|
};
|
|
347
348
|
}
|