@react-grab/gemini 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/dist/client.global.js +2 -449
- package/package.json +4 -4
package/dist/client.global.js
CHANGED
|
@@ -1,449 +1,2 @@
|
|
|
1
|
-
var ReactGrabGemini = (function (
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
// ../relay/dist/client.js
|
|
5
|
-
var DEFAULT_RELAY_PORT = 4722;
|
|
6
|
-
var DEFAULT_RECONNECT_INTERVAL_MS = 3e3;
|
|
7
|
-
var RELAY_TOKEN_PARAM = "token";
|
|
8
|
-
var createRelayClient = (options = {}) => {
|
|
9
|
-
const serverUrl = options.serverUrl ?? `ws://localhost:${DEFAULT_RELAY_PORT}`;
|
|
10
|
-
const autoReconnect = options.autoReconnect ?? true;
|
|
11
|
-
const reconnectIntervalMs = options.reconnectIntervalMs ?? DEFAULT_RECONNECT_INTERVAL_MS;
|
|
12
|
-
const token = options.token;
|
|
13
|
-
let webSocketConnection = null;
|
|
14
|
-
let isConnectedState = false;
|
|
15
|
-
let availableHandlers = [];
|
|
16
|
-
let reconnectTimeoutId = null;
|
|
17
|
-
let pendingConnectionPromise = null;
|
|
18
|
-
let pendingConnectionReject = null;
|
|
19
|
-
let isIntentionalDisconnect = false;
|
|
20
|
-
const messageCallbacks = /* @__PURE__ */ new Set();
|
|
21
|
-
const handlersChangeCallbacks = /* @__PURE__ */ new Set();
|
|
22
|
-
const connectionChangeCallbacks = /* @__PURE__ */ new Set();
|
|
23
|
-
const scheduleReconnect = () => {
|
|
24
|
-
if (!autoReconnect || reconnectTimeoutId || isIntentionalDisconnect) return;
|
|
25
|
-
reconnectTimeoutId = setTimeout(() => {
|
|
26
|
-
reconnectTimeoutId = null;
|
|
27
|
-
connect().catch(() => {
|
|
28
|
-
});
|
|
29
|
-
}, reconnectIntervalMs);
|
|
30
|
-
};
|
|
31
|
-
const handleMessage = (event) => {
|
|
32
|
-
try {
|
|
33
|
-
const message = JSON.parse(event.data);
|
|
34
|
-
if (message.type === "handlers" && message.handlers) {
|
|
35
|
-
availableHandlers = message.handlers;
|
|
36
|
-
for (const callback of handlersChangeCallbacks) {
|
|
37
|
-
callback(availableHandlers);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
for (const callback of messageCallbacks) {
|
|
41
|
-
callback(message);
|
|
42
|
-
}
|
|
43
|
-
} catch {
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
const connect = () => {
|
|
47
|
-
if (webSocketConnection?.readyState === WebSocket.OPEN) {
|
|
48
|
-
return Promise.resolve();
|
|
49
|
-
}
|
|
50
|
-
if (pendingConnectionPromise) {
|
|
51
|
-
return pendingConnectionPromise;
|
|
52
|
-
}
|
|
53
|
-
isIntentionalDisconnect = false;
|
|
54
|
-
pendingConnectionPromise = new Promise((resolve, reject) => {
|
|
55
|
-
pendingConnectionReject = reject;
|
|
56
|
-
const connectionUrl = token ? `${serverUrl}?${RELAY_TOKEN_PARAM}=${encodeURIComponent(token)}` : serverUrl;
|
|
57
|
-
webSocketConnection = new WebSocket(connectionUrl);
|
|
58
|
-
webSocketConnection.onopen = () => {
|
|
59
|
-
pendingConnectionPromise = null;
|
|
60
|
-
pendingConnectionReject = null;
|
|
61
|
-
isConnectedState = true;
|
|
62
|
-
for (const callback of connectionChangeCallbacks) {
|
|
63
|
-
callback(true);
|
|
64
|
-
}
|
|
65
|
-
resolve();
|
|
66
|
-
};
|
|
67
|
-
webSocketConnection.onmessage = handleMessage;
|
|
68
|
-
webSocketConnection.onclose = () => {
|
|
69
|
-
if (pendingConnectionReject) {
|
|
70
|
-
pendingConnectionReject(new Error("WebSocket connection closed"));
|
|
71
|
-
pendingConnectionReject = null;
|
|
72
|
-
}
|
|
73
|
-
pendingConnectionPromise = null;
|
|
74
|
-
isConnectedState = false;
|
|
75
|
-
availableHandlers = [];
|
|
76
|
-
for (const callback of handlersChangeCallbacks) {
|
|
77
|
-
callback(availableHandlers);
|
|
78
|
-
}
|
|
79
|
-
for (const callback of connectionChangeCallbacks) {
|
|
80
|
-
callback(false);
|
|
81
|
-
}
|
|
82
|
-
scheduleReconnect();
|
|
83
|
-
};
|
|
84
|
-
webSocketConnection.onerror = () => {
|
|
85
|
-
pendingConnectionPromise = null;
|
|
86
|
-
pendingConnectionReject = null;
|
|
87
|
-
isConnectedState = false;
|
|
88
|
-
reject(new Error("WebSocket connection failed"));
|
|
89
|
-
};
|
|
90
|
-
});
|
|
91
|
-
return pendingConnectionPromise;
|
|
92
|
-
};
|
|
93
|
-
const disconnect = () => {
|
|
94
|
-
isIntentionalDisconnect = true;
|
|
95
|
-
if (reconnectTimeoutId) {
|
|
96
|
-
clearTimeout(reconnectTimeoutId);
|
|
97
|
-
reconnectTimeoutId = null;
|
|
98
|
-
}
|
|
99
|
-
if (pendingConnectionReject) {
|
|
100
|
-
pendingConnectionReject(new Error("Connection aborted"));
|
|
101
|
-
pendingConnectionReject = null;
|
|
102
|
-
}
|
|
103
|
-
pendingConnectionPromise = null;
|
|
104
|
-
webSocketConnection?.close();
|
|
105
|
-
webSocketConnection = null;
|
|
106
|
-
isConnectedState = false;
|
|
107
|
-
availableHandlers = [];
|
|
108
|
-
};
|
|
109
|
-
const isConnected = () => isConnectedState;
|
|
110
|
-
const sendMessage = (message) => {
|
|
111
|
-
if (webSocketConnection?.readyState === WebSocket.OPEN) {
|
|
112
|
-
webSocketConnection.send(JSON.stringify(message));
|
|
113
|
-
return true;
|
|
114
|
-
}
|
|
115
|
-
return false;
|
|
116
|
-
};
|
|
117
|
-
const sendAgentRequest = (agentId, context) => {
|
|
118
|
-
return sendMessage({
|
|
119
|
-
type: "agent-request",
|
|
120
|
-
agentId,
|
|
121
|
-
sessionId: context.sessionId,
|
|
122
|
-
context
|
|
123
|
-
});
|
|
124
|
-
};
|
|
125
|
-
const abortAgent = (agentId, sessionId) => {
|
|
126
|
-
sendMessage({
|
|
127
|
-
type: "agent-abort",
|
|
128
|
-
agentId,
|
|
129
|
-
sessionId
|
|
130
|
-
});
|
|
131
|
-
};
|
|
132
|
-
const undoAgent = (agentId, sessionId) => {
|
|
133
|
-
return sendMessage({
|
|
134
|
-
type: "agent-undo",
|
|
135
|
-
agentId,
|
|
136
|
-
sessionId
|
|
137
|
-
});
|
|
138
|
-
};
|
|
139
|
-
const redoAgent = (agentId, sessionId) => {
|
|
140
|
-
return sendMessage({
|
|
141
|
-
type: "agent-redo",
|
|
142
|
-
agentId,
|
|
143
|
-
sessionId
|
|
144
|
-
});
|
|
145
|
-
};
|
|
146
|
-
const onMessage = (callback) => {
|
|
147
|
-
messageCallbacks.add(callback);
|
|
148
|
-
return () => messageCallbacks.delete(callback);
|
|
149
|
-
};
|
|
150
|
-
const onHandlersChange = (callback) => {
|
|
151
|
-
handlersChangeCallbacks.add(callback);
|
|
152
|
-
return () => handlersChangeCallbacks.delete(callback);
|
|
153
|
-
};
|
|
154
|
-
const onConnectionChange = (callback) => {
|
|
155
|
-
connectionChangeCallbacks.add(callback);
|
|
156
|
-
queueMicrotask(() => {
|
|
157
|
-
if (connectionChangeCallbacks.has(callback)) {
|
|
158
|
-
callback(isConnectedState);
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
return () => connectionChangeCallbacks.delete(callback);
|
|
162
|
-
};
|
|
163
|
-
const getAvailableHandlers = () => availableHandlers;
|
|
164
|
-
return {
|
|
165
|
-
connect,
|
|
166
|
-
disconnect,
|
|
167
|
-
isConnected,
|
|
168
|
-
sendAgentRequest,
|
|
169
|
-
abortAgent,
|
|
170
|
-
undoAgent,
|
|
171
|
-
redoAgent,
|
|
172
|
-
onMessage,
|
|
173
|
-
onHandlersChange,
|
|
174
|
-
onConnectionChange,
|
|
175
|
-
getAvailableHandlers
|
|
176
|
-
};
|
|
177
|
-
};
|
|
178
|
-
var createRelayAgentProvider = (options) => {
|
|
179
|
-
const { relayClient, agentId } = options;
|
|
180
|
-
const checkConnection = async () => {
|
|
181
|
-
if (!relayClient.isConnected()) {
|
|
182
|
-
try {
|
|
183
|
-
await relayClient.connect();
|
|
184
|
-
} catch {
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return relayClient.getAvailableHandlers().includes(agentId);
|
|
189
|
-
};
|
|
190
|
-
const send = async function* (context, signal) {
|
|
191
|
-
if (signal.aborted) {
|
|
192
|
-
throw new DOMException("Aborted", "AbortError");
|
|
193
|
-
}
|
|
194
|
-
yield "Connecting\u2026";
|
|
195
|
-
const sessionId = context.sessionId ?? `session-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
196
|
-
const contextWithSession = {
|
|
197
|
-
...context,
|
|
198
|
-
sessionId
|
|
199
|
-
};
|
|
200
|
-
const messageQueue = [];
|
|
201
|
-
let resolveNextMessage = null;
|
|
202
|
-
let rejectNextMessage = null;
|
|
203
|
-
let isDone = false;
|
|
204
|
-
let errorMessage = null;
|
|
205
|
-
const handleAbort = () => {
|
|
206
|
-
relayClient.abortAgent(agentId, sessionId);
|
|
207
|
-
isDone = true;
|
|
208
|
-
if (resolveNextMessage) {
|
|
209
|
-
resolveNextMessage({ value: void 0, done: true });
|
|
210
|
-
resolveNextMessage = null;
|
|
211
|
-
rejectNextMessage = null;
|
|
212
|
-
}
|
|
213
|
-
};
|
|
214
|
-
signal.addEventListener("abort", handleAbort, { once: true });
|
|
215
|
-
const handleConnectionChange = (connected) => {
|
|
216
|
-
if (!connected && !isDone) {
|
|
217
|
-
errorMessage = "Relay connection lost";
|
|
218
|
-
isDone = true;
|
|
219
|
-
if (rejectNextMessage) {
|
|
220
|
-
rejectNextMessage(new Error(errorMessage));
|
|
221
|
-
resolveNextMessage = null;
|
|
222
|
-
rejectNextMessage = null;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
};
|
|
226
|
-
const unsubscribeConnection = relayClient.onConnectionChange(
|
|
227
|
-
handleConnectionChange
|
|
228
|
-
);
|
|
229
|
-
const unsubscribeMessage = relayClient.onMessage((message) => {
|
|
230
|
-
if (message.sessionId !== sessionId) return;
|
|
231
|
-
if (message.type === "agent-status" && message.content) {
|
|
232
|
-
messageQueue.push(message.content);
|
|
233
|
-
if (resolveNextMessage) {
|
|
234
|
-
const nextMessage = messageQueue.shift();
|
|
235
|
-
if (nextMessage !== void 0) {
|
|
236
|
-
resolveNextMessage({ value: nextMessage, done: false });
|
|
237
|
-
resolveNextMessage = null;
|
|
238
|
-
rejectNextMessage = null;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
} else if (message.type === "agent-done") {
|
|
242
|
-
isDone = true;
|
|
243
|
-
if (resolveNextMessage) {
|
|
244
|
-
resolveNextMessage({ value: void 0, done: true });
|
|
245
|
-
resolveNextMessage = null;
|
|
246
|
-
rejectNextMessage = null;
|
|
247
|
-
}
|
|
248
|
-
} else if (message.type === "agent-error") {
|
|
249
|
-
errorMessage = message.content ?? "Unknown error";
|
|
250
|
-
isDone = true;
|
|
251
|
-
if (rejectNextMessage) {
|
|
252
|
-
rejectNextMessage(new Error(errorMessage));
|
|
253
|
-
resolveNextMessage = null;
|
|
254
|
-
rejectNextMessage = null;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
if (!relayClient.isConnected()) {
|
|
259
|
-
unsubscribeConnection();
|
|
260
|
-
unsubscribeMessage();
|
|
261
|
-
signal.removeEventListener("abort", handleAbort);
|
|
262
|
-
throw new Error("Relay connection is not open");
|
|
263
|
-
}
|
|
264
|
-
const didSendRequest = relayClient.sendAgentRequest(
|
|
265
|
-
agentId,
|
|
266
|
-
contextWithSession
|
|
267
|
-
);
|
|
268
|
-
if (!didSendRequest) {
|
|
269
|
-
unsubscribeConnection();
|
|
270
|
-
unsubscribeMessage();
|
|
271
|
-
signal.removeEventListener("abort", handleAbort);
|
|
272
|
-
throw new Error("Failed to send agent request: connection not open");
|
|
273
|
-
}
|
|
274
|
-
try {
|
|
275
|
-
while (true) {
|
|
276
|
-
if (messageQueue.length > 0) {
|
|
277
|
-
const next = messageQueue.shift();
|
|
278
|
-
if (next !== void 0) {
|
|
279
|
-
yield next;
|
|
280
|
-
}
|
|
281
|
-
continue;
|
|
282
|
-
}
|
|
283
|
-
if (isDone || signal.aborted) {
|
|
284
|
-
break;
|
|
285
|
-
}
|
|
286
|
-
const result = await new Promise(
|
|
287
|
-
(resolve, reject) => {
|
|
288
|
-
resolveNextMessage = resolve;
|
|
289
|
-
rejectNextMessage = reject;
|
|
290
|
-
}
|
|
291
|
-
);
|
|
292
|
-
if (result.done) break;
|
|
293
|
-
yield result.value;
|
|
294
|
-
}
|
|
295
|
-
if (errorMessage) {
|
|
296
|
-
throw new Error(errorMessage);
|
|
297
|
-
}
|
|
298
|
-
} finally {
|
|
299
|
-
signal.removeEventListener("abort", handleAbort);
|
|
300
|
-
unsubscribeConnection();
|
|
301
|
-
unsubscribeMessage();
|
|
302
|
-
}
|
|
303
|
-
};
|
|
304
|
-
const abort = async (sessionId) => {
|
|
305
|
-
relayClient.abortAgent(agentId, sessionId);
|
|
306
|
-
};
|
|
307
|
-
const waitForOperationResponse = (sessionId) => {
|
|
308
|
-
return new Promise((resolve, reject) => {
|
|
309
|
-
let didCleanup = false;
|
|
310
|
-
const cleanup = () => {
|
|
311
|
-
if (didCleanup) return;
|
|
312
|
-
didCleanup = true;
|
|
313
|
-
unsubscribeMessage();
|
|
314
|
-
unsubscribeConnection();
|
|
315
|
-
};
|
|
316
|
-
const unsubscribeMessage = relayClient.onMessage((message) => {
|
|
317
|
-
if (message.sessionId !== sessionId) return;
|
|
318
|
-
cleanup();
|
|
319
|
-
if (message.type === "agent-done") {
|
|
320
|
-
resolve();
|
|
321
|
-
} else if (message.type === "agent-error") {
|
|
322
|
-
reject(new Error(message.content ?? "Operation failed"));
|
|
323
|
-
}
|
|
324
|
-
});
|
|
325
|
-
const unsubscribeConnection = relayClient.onConnectionChange(
|
|
326
|
-
(connected) => {
|
|
327
|
-
if (!connected) {
|
|
328
|
-
cleanup();
|
|
329
|
-
reject(
|
|
330
|
-
new Error("Connection lost while waiting for operation response")
|
|
331
|
-
);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
);
|
|
335
|
-
});
|
|
336
|
-
};
|
|
337
|
-
const undo = async () => {
|
|
338
|
-
const sessionId = `undo-${agentId}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
339
|
-
const didSend = relayClient.undoAgent(agentId, sessionId);
|
|
340
|
-
if (!didSend) {
|
|
341
|
-
throw new Error("Failed to send undo request: connection not open");
|
|
342
|
-
}
|
|
343
|
-
return waitForOperationResponse(sessionId);
|
|
344
|
-
};
|
|
345
|
-
const redo = async () => {
|
|
346
|
-
const sessionId = `redo-${agentId}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
347
|
-
const didSend = relayClient.redoAgent(agentId, sessionId);
|
|
348
|
-
if (!didSend) {
|
|
349
|
-
throw new Error("Failed to send redo request: connection not open");
|
|
350
|
-
}
|
|
351
|
-
return waitForOperationResponse(sessionId);
|
|
352
|
-
};
|
|
353
|
-
return {
|
|
354
|
-
send,
|
|
355
|
-
abort,
|
|
356
|
-
undo,
|
|
357
|
-
redo,
|
|
358
|
-
checkConnection,
|
|
359
|
-
supportsResume: true,
|
|
360
|
-
supportsFollowUp: true
|
|
361
|
-
};
|
|
362
|
-
};
|
|
363
|
-
var defaultRelayClient = null;
|
|
364
|
-
var getDefaultRelayClient = () => {
|
|
365
|
-
if (typeof window === "undefined") {
|
|
366
|
-
return null;
|
|
367
|
-
}
|
|
368
|
-
if (window.__REACT_GRAB_RELAY__) {
|
|
369
|
-
defaultRelayClient = window.__REACT_GRAB_RELAY__;
|
|
370
|
-
return defaultRelayClient;
|
|
371
|
-
}
|
|
372
|
-
if (!defaultRelayClient) {
|
|
373
|
-
defaultRelayClient = createRelayClient();
|
|
374
|
-
window.__REACT_GRAB_RELAY__ = defaultRelayClient;
|
|
375
|
-
}
|
|
376
|
-
return defaultRelayClient;
|
|
377
|
-
};
|
|
378
|
-
|
|
379
|
-
// src/client.ts
|
|
380
|
-
var AGENT_ID = "gemini";
|
|
381
|
-
var isReactGrabApi = (value) => typeof value === "object" && value !== null && "registerPlugin" in value;
|
|
382
|
-
var createGeminiAgentProvider = (providerOptions = {}) => {
|
|
383
|
-
const relayClient = providerOptions.relayClient ?? getDefaultRelayClient();
|
|
384
|
-
if (!relayClient) {
|
|
385
|
-
throw new Error("RelayClient is required in browser environments");
|
|
386
|
-
}
|
|
387
|
-
return createRelayAgentProvider({
|
|
388
|
-
relayClient,
|
|
389
|
-
agentId: AGENT_ID
|
|
390
|
-
});
|
|
391
|
-
};
|
|
392
|
-
var attachAgent = async () => {
|
|
393
|
-
if (typeof window === "undefined") return;
|
|
394
|
-
const relayClient = getDefaultRelayClient();
|
|
395
|
-
if (!relayClient) return;
|
|
396
|
-
try {
|
|
397
|
-
await relayClient.connect();
|
|
398
|
-
} catch {
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
|
-
const provider = createRelayAgentProvider({
|
|
402
|
-
relayClient,
|
|
403
|
-
agentId: AGENT_ID
|
|
404
|
-
});
|
|
405
|
-
const attach = (api) => {
|
|
406
|
-
const agent = { provider, storage: sessionStorage };
|
|
407
|
-
const plugin = {
|
|
408
|
-
name: "gemini-agent",
|
|
409
|
-
actions: [
|
|
410
|
-
{
|
|
411
|
-
id: "edit-with-gemini",
|
|
412
|
-
label: "Edit with Gemini",
|
|
413
|
-
shortcut: "Enter",
|
|
414
|
-
onAction: (actionContext) => {
|
|
415
|
-
actionContext.enterPromptMode?.(agent);
|
|
416
|
-
},
|
|
417
|
-
agent
|
|
418
|
-
}
|
|
419
|
-
]
|
|
420
|
-
};
|
|
421
|
-
api.registerPlugin(plugin);
|
|
422
|
-
};
|
|
423
|
-
const existingApi = window.__REACT_GRAB__;
|
|
424
|
-
if (isReactGrabApi(existingApi)) {
|
|
425
|
-
attach(existingApi);
|
|
426
|
-
return;
|
|
427
|
-
}
|
|
428
|
-
window.addEventListener(
|
|
429
|
-
"react-grab:init",
|
|
430
|
-
(event) => {
|
|
431
|
-
if (!(event instanceof CustomEvent)) return;
|
|
432
|
-
if (!isReactGrabApi(event.detail)) return;
|
|
433
|
-
attach(event.detail);
|
|
434
|
-
},
|
|
435
|
-
{ once: true }
|
|
436
|
-
);
|
|
437
|
-
const apiAfterListener = window.__REACT_GRAB__;
|
|
438
|
-
if (isReactGrabApi(apiAfterListener)) {
|
|
439
|
-
attach(apiAfterListener);
|
|
440
|
-
}
|
|
441
|
-
};
|
|
442
|
-
attachAgent();
|
|
443
|
-
|
|
444
|
-
exports.attachAgent = attachAgent;
|
|
445
|
-
exports.createGeminiAgentProvider = createGeminiAgentProvider;
|
|
446
|
-
|
|
447
|
-
return exports;
|
|
448
|
-
|
|
449
|
-
})({});
|
|
1
|
+
var ReactGrabGemini=(function(exports){'use strict';var $=4722,q=3e3,x="token",D=(s={})=>{let n=s.serverUrl??`ws://localhost:${$}`,c=s.autoReconnect??true,E=s.reconnectIntervalMs??q,h=s.token,i=null,f=false,A=[],C=null,t=null,o=null,w=false,y=new Set,g=new Set,r=new Set,d=()=>{!c||C||w||(C=setTimeout(()=>{C=null,p().catch(()=>{});},E));},l=e=>{try{let a=JSON.parse(e.data);if(a.type==="handlers"&&a.handlers){A=a.handlers;for(let m of g)m(A);}for(let m of y)m(a);}catch{}},p=()=>i?.readyState===WebSocket.OPEN?Promise.resolve():t||(w=false,t=new Promise((e,a)=>{o=a;let m=h?`${n}?${x}=${encodeURIComponent(h)}`:n;i=new WebSocket(m),i.onopen=()=>{t=null,o=null,f=true;for(let S of r)S(true);e();},i.onmessage=l,i.onclose=()=>{o&&(o(new Error("WebSocket connection closed")),o=null),t=null,f=false,A=[];for(let S of g)S(A);for(let S of r)S(false);d();},i.onerror=()=>{t=null,o=null,f=false,a(new Error("WebSocket connection failed"));};}),t),v=()=>{w=true,C&&(clearTimeout(C),C=null),o&&(o(new Error("Connection aborted")),o=null),t=null,i?.close(),i=null,f=false,A=[];},M=()=>f,R=e=>i?.readyState===WebSocket.OPEN?(i.send(JSON.stringify(e)),true):false;return {connect:p,disconnect:v,isConnected:M,sendAgentRequest:(e,a)=>R({type:"agent-request",agentId:e,sessionId:a.sessionId,context:a}),abortAgent:(e,a)=>{R({type:"agent-abort",agentId:e,sessionId:a});},undoAgent:(e,a)=>R({type:"agent-undo",agentId:e,sessionId:a}),redoAgent:(e,a)=>R({type:"agent-redo",agentId:e,sessionId:a}),onMessage:e=>(y.add(e),()=>y.delete(e)),onHandlersChange:e=>(g.add(e),()=>g.delete(e)),onConnectionChange:e=>(r.add(e),queueMicrotask(()=>{r.has(e)&&e(f);}),()=>r.delete(e)),getAvailableHandlers:()=>A}},I=s=>{let{relayClient:n,agentId:c}=s,E=async()=>{if(!n.isConnected())try{await n.connect();}catch{return false}return n.getAvailableHandlers().includes(c)},h=async function*(t,o){if(o.aborted)throw new DOMException("Aborted","AbortError");yield "Connecting\u2026";let w=t.sessionId??`session-${Date.now()}-${Math.random().toString(36).slice(2)}`,y={...t,sessionId:w},g=[],r=null,d=null,l=false,p=null,v=()=>{n.abortAgent(c,w),l=true,r&&(r({value:void 0,done:true}),r=null,d=null);};o.addEventListener("abort",v,{once:true});let M=u=>{!u&&!l&&(p="Relay connection lost",l=true,d&&(d(new Error(p)),r=null,d=null));},R=n.onConnectionChange(M),P=n.onMessage(u=>{if(u.sessionId===w)if(u.type==="agent-status"&&u.content){if(g.push(u.content),r){let b=g.shift();b!==void 0&&(r({value:b,done:false}),r=null,d=null);}}else u.type==="agent-done"?(l=true,r&&(r({value:void 0,done:true}),r=null,d=null)):u.type==="agent-error"&&(p=u.content??"Unknown error",l=true,d&&(d(new Error(p)),r=null,d=null));});if(!n.isConnected())throw R(),P(),o.removeEventListener("abort",v),new Error("Relay connection is not open");if(!n.sendAgentRequest(c,y))throw R(),P(),o.removeEventListener("abort",v),new Error("Failed to send agent request: connection not open");try{for(;;){if(g.length>0){let b=g.shift();b!==void 0&&(yield b);continue}if(l||o.aborted)break;let u=await new Promise((b,G)=>{r=b,d=G;});if(u.done)break;yield u.value;}if(p)throw new Error(p)}finally{o.removeEventListener("abort",v),R(),P();}},i=async t=>{n.abortAgent(c,t);},f=t=>new Promise((o,w)=>{let y=false,g=()=>{y||(y=true,r(),d());},r=n.onMessage(l=>{l.sessionId===t&&(g(),l.type==="agent-done"?o():l.type==="agent-error"&&w(new Error(l.content??"Operation failed")));}),d=n.onConnectionChange(l=>{l||(g(),w(new Error("Connection lost while waiting for operation response")));});});return {send:h,abort:i,undo:async()=>{let t=`undo-${c}-${Date.now()}-${Math.random().toString(36).slice(2)}`;if(!n.undoAgent(c,t))throw new Error("Failed to send undo request: connection not open");return f(t)},redo:async()=>{let t=`redo-${c}-${Date.now()}-${Math.random().toString(36).slice(2)}`;if(!n.redoAgent(c,t))throw new Error("Failed to send redo request: connection not open");return f(t)},checkConnection:E,supportsResume:true,supportsFollowUp:true}},_=null,k=()=>typeof window>"u"?null:window.__REACT_GRAB_RELAY__?(_=window.__REACT_GRAB_RELAY__,_):(_||(_=D(),window.__REACT_GRAB_RELAY__=_),_);var L="gemini",T=s=>typeof s=="object"&&s!==null&&"registerPlugin"in s,Y=(s={})=>{let n=s.relayClient??k();if(!n)throw new Error("RelayClient is required in browser environments");return I({relayClient:n,agentId:L})},N=async()=>{if(typeof window>"u")return;let s=k();if(!s)return;try{await s.connect();}catch{return}let n=I({relayClient:s,agentId:L}),c=i=>{let f={provider:n,storage:sessionStorage},A={name:"gemini-agent",actions:[{id:"edit-with-gemini",label:"Edit with Gemini",shortcut:"Enter",onAction:C=>{C.enterPromptMode?.(f);},agent:f}]};i.registerPlugin(A);},E=window.__REACT_GRAB__;if(T(E)){c(E);return}window.addEventListener("react-grab:init",i=>{i instanceof CustomEvent&&T(i.detail)&&c(i.detail);},{once:true});let h=window.__REACT_GRAB__;T(h)&&c(h);};N();
|
|
2
|
+
exports.attachAgent=N;exports.createGeminiAgentProvider=Y;return exports;})({});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-grab/gemini",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"bin": {
|
|
5
5
|
"react-grab-gemini": "./dist/cli.cjs"
|
|
6
6
|
},
|
|
@@ -25,13 +25,13 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"execa": "^9.6.0",
|
|
28
|
-
"react-grab": "0.1.
|
|
29
|
-
"
|
|
28
|
+
"@react-grab/relay": "0.1.1",
|
|
29
|
+
"react-grab": "0.1.1"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/node": "^22.10.7",
|
|
33
33
|
"tsup": "^8.4.0",
|
|
34
|
-
"@react-grab/utils": "0.1.
|
|
34
|
+
"@react-grab/utils": "0.1.1"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"dev": "tsup --watch",
|