pi-gemini-app 0.1.0 → 0.1.1
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/extensions/pi/index.js +6 -12
- package/extensions/pi/provider.js +41 -3
- package/extensions/pi/stream.js +85 -1
- package/extensions/pi/transcript.js +96 -4
- package/package.json +1 -1
package/extensions/pi/index.js
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
createProvider,
|
|
3
|
-
createAssistantMessageEventStream,
|
|
4
|
-
getCurrentSystemPrompt,
|
|
5
|
-
getCurrentTools,
|
|
6
|
-
toToolDeclaration,
|
|
7
|
-
} from '@earendil-works/pi-ai';
|
|
1
|
+
import * as piAi from '@earendil-works/pi-ai';
|
|
8
2
|
import { registerGeminiAppExtension } from './provider.js';
|
|
9
3
|
|
|
10
4
|
export default function (pi) {
|
|
11
5
|
registerGeminiAppExtension(pi, {
|
|
12
|
-
createProvider,
|
|
13
|
-
createAssistantMessageEventStream,
|
|
14
|
-
getCurrentSystemPrompt,
|
|
15
|
-
getCurrentTools,
|
|
16
|
-
toToolDeclaration,
|
|
6
|
+
createProvider: piAi.createProvider,
|
|
7
|
+
createAssistantMessageEventStream: piAi.createAssistantMessageEventStream,
|
|
8
|
+
getCurrentSystemPrompt: piAi.getCurrentSystemPrompt,
|
|
9
|
+
getCurrentTools: piAi.getCurrentTools,
|
|
10
|
+
toToolDeclaration: piAi.toToolDeclaration,
|
|
17
11
|
});
|
|
18
12
|
}
|
|
@@ -121,9 +121,47 @@ export function createGeminiAppProvider({
|
|
|
121
121
|
});
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
export function registerGeminiAppExtension(pi, dependencies) {
|
|
125
|
-
|
|
126
|
-
|
|
124
|
+
export function registerGeminiAppExtension(pi, dependencies = {}) {
|
|
125
|
+
if (dependencies?.createProvider === undefined) {
|
|
126
|
+
const {
|
|
127
|
+
createProvider: _ignoredCreateProvider,
|
|
128
|
+
auth = createGeminiOAuthAuth(),
|
|
129
|
+
fetchGoogleUsageInfo: _ignoredFetchUsage,
|
|
130
|
+
decodeUsageInfo: _ignoredDecodeUsage,
|
|
131
|
+
...streamDependencies
|
|
132
|
+
} = dependencies;
|
|
133
|
+
const streamHandler = createGeminiStreamHandler(streamDependencies);
|
|
134
|
+
pi.registerProvider(PROVIDER_ID, {
|
|
135
|
+
name: 'Google Gemini App',
|
|
136
|
+
baseUrl: BASE_URL,
|
|
137
|
+
api: API_ID,
|
|
138
|
+
models: MODELS,
|
|
139
|
+
streamSimple: streamHandler.streamSimple,
|
|
140
|
+
oauth: {
|
|
141
|
+
name: auth.name,
|
|
142
|
+
login: (callbacks) =>
|
|
143
|
+
auth.login({
|
|
144
|
+
signal: callbacks?.signal,
|
|
145
|
+
notify: (ev) => {
|
|
146
|
+
if (ev.type === 'auth_url') {
|
|
147
|
+
callbacks?.onAuth?.({ url: ev.url, instructions: ev.instructions });
|
|
148
|
+
} else if (ev.message) {
|
|
149
|
+
callbacks?.onProgress?.(ev.message);
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
prompt: (p) =>
|
|
153
|
+
p.type === 'manual_code' && typeof callbacks?.onManualCodeInput === 'function'
|
|
154
|
+
? callbacks.onManualCodeInput()
|
|
155
|
+
: callbacks?.onPrompt?.({ message: p.message, placeholder: p.placeholder }),
|
|
156
|
+
}),
|
|
157
|
+
refreshToken: (cred, signal) => auth.refresh(cred, signal),
|
|
158
|
+
getApiKey: (cred) => cred.access,
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
} else {
|
|
162
|
+
const provider = createGeminiAppProvider(dependencies);
|
|
163
|
+
pi.registerProvider(provider);
|
|
164
|
+
}
|
|
127
165
|
|
|
128
166
|
const fetchUsage = dependencies?.fetchGoogleUsageInfo || defaultFetchGoogleUsageInfo;
|
|
129
167
|
const decodeUsage = dependencies?.decodeUsageInfo || defaultDecodeUsageInfo;
|
package/extensions/pi/stream.js
CHANGED
|
@@ -88,8 +88,92 @@ function contentChars(content) {
|
|
|
88
88
|
return 0;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
class DefaultAssistantMessageEventStream {
|
|
92
|
+
#queue = [];
|
|
93
|
+
#waiting = [];
|
|
94
|
+
#done = false;
|
|
95
|
+
#startedAt = Date.now();
|
|
96
|
+
#startedAtMonotonic = performance.now();
|
|
97
|
+
#finalResultPromise;
|
|
98
|
+
#resolveFinalResult;
|
|
99
|
+
|
|
100
|
+
constructor() {
|
|
101
|
+
this.#finalResultPromise = new Promise((resolve) => {
|
|
102
|
+
this.#resolveFinalResult = resolve;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
#time(message) {
|
|
107
|
+
if (
|
|
108
|
+
this.#done ||
|
|
109
|
+
!message ||
|
|
110
|
+
typeof message !== 'object' ||
|
|
111
|
+
message.durationMs !== undefined ||
|
|
112
|
+
message.timestamp < this.#startedAt
|
|
113
|
+
) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
message.durationMs = Math.max(0, Math.round(performance.now() - this.#startedAtMonotonic));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
push(event) {
|
|
120
|
+
if (this.#done) return;
|
|
121
|
+
if (event?.type === 'done') {
|
|
122
|
+
this.#time(event.message);
|
|
123
|
+
this.#done = true;
|
|
124
|
+
this.#resolveFinalResult(event.message);
|
|
125
|
+
} else if (event?.type === 'error') {
|
|
126
|
+
this.#time(event.error);
|
|
127
|
+
this.#done = true;
|
|
128
|
+
this.#resolveFinalResult(event.error);
|
|
129
|
+
}
|
|
130
|
+
const waiter = this.#waiting.shift();
|
|
131
|
+
if (waiter) {
|
|
132
|
+
waiter({ value: event, done: false });
|
|
133
|
+
} else {
|
|
134
|
+
this.#queue.push(event);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
end(result) {
|
|
139
|
+
if (result !== undefined) {
|
|
140
|
+
this.#time(result);
|
|
141
|
+
this.#done = true;
|
|
142
|
+
this.#resolveFinalResult(result);
|
|
143
|
+
} else {
|
|
144
|
+
this.#done = true;
|
|
145
|
+
}
|
|
146
|
+
while (this.#waiting.length > 0) {
|
|
147
|
+
const waiter = this.#waiting.shift();
|
|
148
|
+
waiter({ value: undefined, done: true });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async *[Symbol.asyncIterator]() {
|
|
153
|
+
while (true) {
|
|
154
|
+
if (this.#queue.length > 0) {
|
|
155
|
+
yield this.#queue.shift();
|
|
156
|
+
} else if (this.#done) {
|
|
157
|
+
return;
|
|
158
|
+
} else {
|
|
159
|
+
const next = await new Promise((resolve) => this.#waiting.push(resolve));
|
|
160
|
+
if (next.done) return;
|
|
161
|
+
yield next.value;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
result() {
|
|
167
|
+
return this.#finalResultPromise;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function defaultCreateAssistantMessageEventStream() {
|
|
172
|
+
return new DefaultAssistantMessageEventStream();
|
|
173
|
+
}
|
|
174
|
+
|
|
91
175
|
export function createGeminiStreamHandler({
|
|
92
|
-
createAssistantMessageEventStream,
|
|
176
|
+
createAssistantMessageEventStream = defaultCreateAssistantMessageEventStream,
|
|
93
177
|
getCurrentSystemPrompt,
|
|
94
178
|
getCurrentTools,
|
|
95
179
|
toToolDeclaration,
|
|
@@ -226,14 +226,106 @@ function serializeToolArguments(block) {
|
|
|
226
226
|
}
|
|
227
227
|
}
|
|
228
228
|
|
|
229
|
+
function defaultContentText(content, separator = '\n') {
|
|
230
|
+
if (typeof content === 'string') return content;
|
|
231
|
+
if (!Array.isArray(content)) return '';
|
|
232
|
+
return content
|
|
233
|
+
.filter((block) => block && block.type === 'text' && typeof block.text === 'string')
|
|
234
|
+
.map((block) => block.text)
|
|
235
|
+
.join(separator);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function defaultGetCurrentTools(messages) {
|
|
239
|
+
const tools = new Map();
|
|
240
|
+
if (!Array.isArray(messages)) return [];
|
|
241
|
+
for (const message of messages) {
|
|
242
|
+
if (!message || message.role !== 'system') continue;
|
|
243
|
+
for (const removed of message.toolsRemoved ?? []) {
|
|
244
|
+
if (removed && typeof removed.name === 'string') {
|
|
245
|
+
tools.delete(removed.name);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
for (const added of message.toolsAdded ?? []) {
|
|
249
|
+
if (added && typeof added.name === 'string') {
|
|
250
|
+
tools.set(added.name, added);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return [...tools.values()];
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function defaultGetCurrentSystemPrompt(messages) {
|
|
258
|
+
if (!Array.isArray(messages)) return '';
|
|
259
|
+
const contentParts = [];
|
|
260
|
+
const sections = new Map();
|
|
261
|
+
let sawSystemMessage = false;
|
|
262
|
+
for (const message of messages) {
|
|
263
|
+
if (!message || message.role !== 'system') continue;
|
|
264
|
+
sawSystemMessage = true;
|
|
265
|
+
const text = defaultContentText(message.content);
|
|
266
|
+
if (text.length > 0) contentParts.push(text);
|
|
267
|
+
if (message.sections && typeof message.sections === 'object') {
|
|
268
|
+
for (const [name, value] of Object.entries(message.sections)) {
|
|
269
|
+
if (value === null) {
|
|
270
|
+
sections.delete(name);
|
|
271
|
+
} else if (typeof value === 'string') {
|
|
272
|
+
sections.set(name, value);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (!sawSystemMessage) return '';
|
|
278
|
+
const parts = [];
|
|
279
|
+
const base = contentParts.join('\n\n');
|
|
280
|
+
if (base.length > 0) parts.push(base);
|
|
281
|
+
for (const sectionText of sections.values()) {
|
|
282
|
+
if (sectionText.length > 0) parts.push(sectionText);
|
|
283
|
+
}
|
|
284
|
+
return parts.join('\n\n');
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function defaultToToolDeclaration(tool) {
|
|
288
|
+
const parameters =
|
|
289
|
+
tool?.parameters !== undefined
|
|
290
|
+
? JSON.parse(JSON.stringify(tool.parameters))
|
|
291
|
+
: { type: 'object' };
|
|
292
|
+
return {
|
|
293
|
+
name: tool?.name,
|
|
294
|
+
description: tool?.description,
|
|
295
|
+
parameters,
|
|
296
|
+
...(tool?.constrainedSampling === undefined
|
|
297
|
+
? {}
|
|
298
|
+
: { constrainedSampling: tool.constrainedSampling }),
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function normalizeContextMessages(context) {
|
|
303
|
+
const baseMessages = Array.isArray(context?.messages) ? context.messages : [];
|
|
304
|
+
const hasSystemPrompt =
|
|
305
|
+
typeof context?.systemPrompt === 'string' && context.systemPrompt.length > 0;
|
|
306
|
+
const hasTools = Array.isArray(context?.tools) && context.tools.length > 0;
|
|
307
|
+
if (!hasSystemPrompt && !hasTools) {
|
|
308
|
+
return baseMessages;
|
|
309
|
+
}
|
|
310
|
+
return [
|
|
311
|
+
{
|
|
312
|
+
role: 'system',
|
|
313
|
+
content: hasSystemPrompt ? context.systemPrompt : '',
|
|
314
|
+
...(hasTools ? { toolsAdded: context.tools } : {}),
|
|
315
|
+
timestamp: 0,
|
|
316
|
+
},
|
|
317
|
+
...baseMessages,
|
|
318
|
+
];
|
|
319
|
+
}
|
|
320
|
+
|
|
229
321
|
export async function buildChatPayload(
|
|
230
322
|
model,
|
|
231
323
|
context,
|
|
232
324
|
options = {},
|
|
233
325
|
{
|
|
234
|
-
getCurrentSystemPrompt,
|
|
235
|
-
getCurrentTools,
|
|
236
|
-
toToolDeclaration,
|
|
326
|
+
getCurrentSystemPrompt = defaultGetCurrentSystemPrompt,
|
|
327
|
+
getCurrentTools = defaultGetCurrentTools,
|
|
328
|
+
toToolDeclaration = defaultToToolDeclaration,
|
|
237
329
|
simple = false,
|
|
238
330
|
} = {},
|
|
239
331
|
) {
|
|
@@ -241,7 +333,7 @@ export async function buildChatPayload(
|
|
|
241
333
|
throw new TypeError('Transcript helpers getCurrentSystemPrompt, getCurrentTools, and toToolDeclaration are required');
|
|
242
334
|
}
|
|
243
335
|
|
|
244
|
-
const rawMessages =
|
|
336
|
+
const rawMessages = normalizeContextMessages(context);
|
|
245
337
|
const codec = createToolCodec();
|
|
246
338
|
|
|
247
339
|
const rawTools = getCurrentTools(rawMessages) ?? [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-gemini-app",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
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": [
|