pi-gemini-app 0.1.4 → 0.1.5
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/package.json +2 -1
- package/src/prompt-tool-schema.mjs +47 -12
- package/src/responses-tool-namespaces.mjs +5 -3
- package/src/tool-protocol.mjs +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# `pi-gemini-app` — Google Gemini App Provider for Pi
|
|
2
2
|
|
|
3
|
+

|
|
4
|
+
|
|
3
5
|
Native in-process provider extension for `@earendil-works/pi-coding-agent` (`pi`) and GooeyPi. Connects directly to Gemini's native HTTP/2 endpoint (`ProcessQuery`) inside the Pi process—no separate HTTP gateway server, Docker container, or extra npm runtime dependencies required.
|
|
4
6
|
|
|
5
7
|
> **Disclaimer**: Unofficial interoperability software. Not affiliated with, endorsed by, or sponsored by Google. You must authenticate personally with your own eligible Google account.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-gemini-app",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "In-process Google Gemini App provider extension for Pi (direct native HTTP/2, /login gemini-app, /gemini-quota, zero runtime dependencies).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"node": ">=22.0.0"
|
|
34
34
|
},
|
|
35
35
|
"pi": {
|
|
36
|
+
"image": "https://raw.githubusercontent.com/ShawTim/gemini-app2api/main/docs/assets/pi-extension.jpg",
|
|
36
37
|
"extensions": [
|
|
37
38
|
"./extensions/pi/index.js"
|
|
38
39
|
]
|
|
@@ -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');
|