@react-grab/claude-code 0.0.98 → 0.1.0-beta.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/cli.cjs +19713 -6307
- package/dist/cli.js +19684 -6298
- package/dist/client.cjs +383 -210
- package/dist/client.d.cts +4 -13
- package/dist/client.d.ts +4 -13
- package/dist/client.global.js +2 -6
- package/dist/client.js +383 -210
- package/dist/{server.cjs → handler.cjs} +17090 -15920
- package/dist/handler.d.cts +8 -0
- package/dist/handler.d.ts +8 -0
- package/dist/{server.js → handler.js} +9571 -8398
- package/package.json +8 -12
- package/dist/server.d.cts +0 -12
- package/dist/server.d.ts +0 -12
package/dist/client.cjs
CHANGED
|
@@ -1,243 +1,416 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
// ../
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
let
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
3
|
+
// ../relay/dist/client.js
|
|
4
|
+
var DEFAULT_RELAY_PORT = 4722;
|
|
5
|
+
var DEFAULT_RECONNECT_INTERVAL_MS = 3e3;
|
|
6
|
+
var RELAY_TOKEN_PARAM = "token";
|
|
7
|
+
var createRelayClient = (options = {}) => {
|
|
8
|
+
const serverUrl = options.serverUrl ?? `ws://localhost:${DEFAULT_RELAY_PORT}`;
|
|
9
|
+
const autoReconnect = options.autoReconnect ?? true;
|
|
10
|
+
const reconnectIntervalMs = options.reconnectIntervalMs ?? DEFAULT_RECONNECT_INTERVAL_MS;
|
|
11
|
+
const token = options.token;
|
|
12
|
+
let webSocketConnection = null;
|
|
13
|
+
let isConnectedState = false;
|
|
14
|
+
let availableHandlers = [];
|
|
15
|
+
let reconnectTimeoutId = null;
|
|
16
|
+
let pendingConnectionPromise = null;
|
|
17
|
+
let pendingConnectionReject = null;
|
|
18
|
+
let isIntentionalDisconnect = false;
|
|
19
|
+
const messageCallbacks = /* @__PURE__ */ new Set();
|
|
20
|
+
const handlersChangeCallbacks = /* @__PURE__ */ new Set();
|
|
21
|
+
const connectionChangeCallbacks = /* @__PURE__ */ new Set();
|
|
22
|
+
const scheduleReconnect = () => {
|
|
23
|
+
if (!autoReconnect || reconnectTimeoutId || isIntentionalDisconnect) return;
|
|
24
|
+
reconnectTimeoutId = setTimeout(() => {
|
|
25
|
+
reconnectTimeoutId = null;
|
|
26
|
+
connect().catch(() => {
|
|
27
|
+
});
|
|
28
|
+
}, reconnectIntervalMs);
|
|
24
29
|
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
throw new DOMException("Aborted", "AbortError");
|
|
30
|
+
const handleMessage = (event) => {
|
|
31
|
+
try {
|
|
32
|
+
const message = JSON.parse(event.data);
|
|
33
|
+
if (message.type === "handlers" && message.handlers) {
|
|
34
|
+
availableHandlers = message.handlers;
|
|
35
|
+
for (const callback of handlersChangeCallbacks) {
|
|
36
|
+
callback(availableHandlers);
|
|
37
|
+
}
|
|
34
38
|
}
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
let boundary;
|
|
38
|
-
while ((boundary = buffer.indexOf("\n\n")) !== -1) {
|
|
39
|
-
const { eventType, data } = parseSSEEvent(buffer.slice(0, boundary));
|
|
40
|
-
buffer = buffer.slice(boundary + 2);
|
|
41
|
-
if (eventType === "done") return;
|
|
42
|
-
if (eventType === "error") throw new Error(data || "Agent error");
|
|
43
|
-
if (data) yield data;
|
|
39
|
+
for (const callback of messageCallbacks) {
|
|
40
|
+
callback(message);
|
|
44
41
|
}
|
|
45
|
-
if (done) break;
|
|
46
|
-
}
|
|
47
|
-
} finally {
|
|
48
|
-
signal.removeEventListener("abort", onAbort);
|
|
49
|
-
try {
|
|
50
|
-
reader.releaseLock();
|
|
51
42
|
} catch {
|
|
52
43
|
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
44
|
+
};
|
|
45
|
+
const connect = () => {
|
|
46
|
+
if (webSocketConnection?.readyState === WebSocket.OPEN) {
|
|
47
|
+
return Promise.resolve();
|
|
48
|
+
}
|
|
49
|
+
if (pendingConnectionPromise) {
|
|
50
|
+
return pendingConnectionPromise;
|
|
51
|
+
}
|
|
52
|
+
isIntentionalDisconnect = false;
|
|
53
|
+
pendingConnectionPromise = new Promise((resolve, reject) => {
|
|
54
|
+
pendingConnectionReject = reject;
|
|
55
|
+
const connectionUrl = token ? `${serverUrl}?${RELAY_TOKEN_PARAM}=${encodeURIComponent(token)}` : serverUrl;
|
|
56
|
+
webSocketConnection = new WebSocket(connectionUrl);
|
|
57
|
+
webSocketConnection.onopen = () => {
|
|
58
|
+
pendingConnectionPromise = null;
|
|
59
|
+
pendingConnectionReject = null;
|
|
60
|
+
isConnectedState = true;
|
|
61
|
+
for (const callback of connectionChangeCallbacks) {
|
|
62
|
+
callback(true);
|
|
63
|
+
}
|
|
64
|
+
resolve();
|
|
65
|
+
};
|
|
66
|
+
webSocketConnection.onmessage = handleMessage;
|
|
67
|
+
webSocketConnection.onclose = () => {
|
|
68
|
+
if (pendingConnectionReject) {
|
|
69
|
+
pendingConnectionReject(new Error("WebSocket connection closed"));
|
|
70
|
+
pendingConnectionReject = null;
|
|
71
|
+
}
|
|
72
|
+
pendingConnectionPromise = null;
|
|
73
|
+
isConnectedState = false;
|
|
74
|
+
availableHandlers = [];
|
|
75
|
+
for (const callback of handlersChangeCallbacks) {
|
|
76
|
+
callback(availableHandlers);
|
|
77
|
+
}
|
|
78
|
+
for (const callback of connectionChangeCallbacks) {
|
|
79
|
+
callback(false);
|
|
80
|
+
}
|
|
81
|
+
scheduleReconnect();
|
|
82
|
+
};
|
|
83
|
+
webSocketConnection.onerror = () => {
|
|
84
|
+
pendingConnectionPromise = null;
|
|
85
|
+
pendingConnectionReject = null;
|
|
86
|
+
isConnectedState = false;
|
|
87
|
+
reject(new Error("WebSocket connection failed"));
|
|
88
|
+
};
|
|
89
|
+
});
|
|
90
|
+
return pendingConnectionPromise;
|
|
91
|
+
};
|
|
92
|
+
const disconnect = () => {
|
|
93
|
+
isIntentionalDisconnect = true;
|
|
94
|
+
if (reconnectTimeoutId) {
|
|
95
|
+
clearTimeout(reconnectTimeoutId);
|
|
96
|
+
reconnectTimeoutId = null;
|
|
97
|
+
}
|
|
98
|
+
if (pendingConnectionReject) {
|
|
99
|
+
pendingConnectionReject(new Error("Connection aborted"));
|
|
100
|
+
pendingConnectionReject = null;
|
|
101
|
+
}
|
|
102
|
+
pendingConnectionPromise = null;
|
|
103
|
+
webSocketConnection?.close();
|
|
104
|
+
webSocketConnection = null;
|
|
105
|
+
isConnectedState = false;
|
|
106
|
+
availableHandlers = [];
|
|
107
|
+
};
|
|
108
|
+
const isConnected = () => isConnectedState;
|
|
109
|
+
const sendMessage = (message) => {
|
|
110
|
+
if (webSocketConnection?.readyState === WebSocket.OPEN) {
|
|
111
|
+
webSocketConnection.send(JSON.stringify(message));
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
};
|
|
116
|
+
const sendAgentRequest = (agentId, context) => {
|
|
117
|
+
return sendMessage({
|
|
118
|
+
type: "agent-request",
|
|
119
|
+
agentId,
|
|
120
|
+
sessionId: context.sessionId,
|
|
121
|
+
context
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
const abortAgent = (agentId, sessionId) => {
|
|
125
|
+
sendMessage({
|
|
126
|
+
type: "agent-abort",
|
|
127
|
+
agentId,
|
|
128
|
+
sessionId
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
const undoAgent = (agentId, sessionId) => {
|
|
132
|
+
return sendMessage({
|
|
133
|
+
type: "agent-undo",
|
|
134
|
+
agentId,
|
|
135
|
+
sessionId
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
const redoAgent = (agentId, sessionId) => {
|
|
139
|
+
return sendMessage({
|
|
140
|
+
type: "agent-redo",
|
|
141
|
+
agentId,
|
|
142
|
+
sessionId
|
|
143
|
+
});
|
|
144
|
+
};
|
|
145
|
+
const onMessage = (callback) => {
|
|
146
|
+
messageCallbacks.add(callback);
|
|
147
|
+
return () => messageCallbacks.delete(callback);
|
|
148
|
+
};
|
|
149
|
+
const onHandlersChange = (callback) => {
|
|
150
|
+
handlersChangeCallbacks.add(callback);
|
|
151
|
+
return () => handlersChangeCallbacks.delete(callback);
|
|
152
|
+
};
|
|
153
|
+
const onConnectionChange = (callback) => {
|
|
154
|
+
connectionChangeCallbacks.add(callback);
|
|
155
|
+
queueMicrotask(() => {
|
|
156
|
+
if (connectionChangeCallbacks.has(callback)) {
|
|
157
|
+
callback(isConnectedState);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
return () => connectionChangeCallbacks.delete(callback);
|
|
161
|
+
};
|
|
162
|
+
const getAvailableHandlers = () => availableHandlers;
|
|
79
163
|
return {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
164
|
+
connect,
|
|
165
|
+
disconnect,
|
|
166
|
+
isConnected,
|
|
167
|
+
sendAgentRequest,
|
|
168
|
+
abortAgent,
|
|
169
|
+
undoAgent,
|
|
170
|
+
redoAgent,
|
|
171
|
+
onMessage,
|
|
172
|
+
onHandlersChange,
|
|
173
|
+
onConnectionChange,
|
|
174
|
+
getAvailableHandlers
|
|
84
175
|
};
|
|
85
176
|
};
|
|
86
|
-
var
|
|
87
|
-
const
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
fetch(`${options.serverUrl}${abortPath}`, { method: "POST" }).catch(
|
|
95
|
-
() => {
|
|
177
|
+
var createRelayAgentProvider = (options) => {
|
|
178
|
+
const { relayClient, agentId } = options;
|
|
179
|
+
const checkConnection = async () => {
|
|
180
|
+
if (!relayClient.isConnected()) {
|
|
181
|
+
try {
|
|
182
|
+
await relayClient.connect();
|
|
183
|
+
} catch {
|
|
184
|
+
return false;
|
|
96
185
|
}
|
|
97
|
-
);
|
|
98
|
-
};
|
|
99
|
-
signal.addEventListener("abort", handleAbort);
|
|
100
|
-
try {
|
|
101
|
-
const response = await fetch(agentUrl, {
|
|
102
|
-
method: "POST",
|
|
103
|
-
headers: { "Content-Type": "application/json" },
|
|
104
|
-
body: JSON.stringify(context),
|
|
105
|
-
signal
|
|
106
|
-
});
|
|
107
|
-
if (!response.ok) {
|
|
108
|
-
throw new Error(`Server error: ${response.status}`);
|
|
109
186
|
}
|
|
110
|
-
|
|
111
|
-
|
|
187
|
+
return relayClient.getAvailableHandlers().includes(agentId);
|
|
188
|
+
};
|
|
189
|
+
const send = async function* (context, signal) {
|
|
190
|
+
if (signal.aborted) {
|
|
191
|
+
throw new DOMException("Aborted", "AbortError");
|
|
112
192
|
}
|
|
113
|
-
|
|
193
|
+
yield "Connecting\u2026";
|
|
194
|
+
const sessionId = context.sessionId ?? `session-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
195
|
+
const contextWithSession = {
|
|
196
|
+
...context,
|
|
197
|
+
sessionId
|
|
198
|
+
};
|
|
199
|
+
const messageQueue = [];
|
|
200
|
+
let resolveNextMessage = null;
|
|
201
|
+
let rejectNextMessage = null;
|
|
114
202
|
let isDone = false;
|
|
115
|
-
let
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
203
|
+
let errorMessage = null;
|
|
204
|
+
const handleAbort = () => {
|
|
205
|
+
relayClient.abortAgent(agentId, sessionId);
|
|
206
|
+
isDone = true;
|
|
207
|
+
if (resolveNextMessage) {
|
|
208
|
+
resolveNextMessage({ value: void 0, done: true });
|
|
209
|
+
resolveNextMessage = null;
|
|
210
|
+
rejectNextMessage = null;
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
signal.addEventListener("abort", handleAbort);
|
|
214
|
+
const handleConnectionChange = (connected) => {
|
|
215
|
+
if (!connected && !isDone) {
|
|
216
|
+
errorMessage = "Relay connection lost";
|
|
217
|
+
isDone = true;
|
|
218
|
+
if (rejectNextMessage) {
|
|
219
|
+
rejectNextMessage(new Error(errorMessage));
|
|
220
|
+
resolveNextMessage = null;
|
|
221
|
+
rejectNextMessage = null;
|
|
134
222
|
}
|
|
135
223
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
224
|
+
};
|
|
225
|
+
const unsubscribeConnection = relayClient.onConnectionChange(
|
|
226
|
+
handleConnectionChange
|
|
227
|
+
);
|
|
228
|
+
const unsubscribeMessage = relayClient.onMessage((message) => {
|
|
229
|
+
if (message.sessionId !== sessionId) return;
|
|
230
|
+
if (message.type === "agent-status" && message.content) {
|
|
231
|
+
messageQueue.push(message.content);
|
|
232
|
+
if (resolveNextMessage) {
|
|
233
|
+
const nextMessage = messageQueue.shift();
|
|
234
|
+
if (nextMessage !== void 0) {
|
|
235
|
+
resolveNextMessage({ value: nextMessage, done: false });
|
|
236
|
+
resolveNextMessage = null;
|
|
237
|
+
rejectNextMessage = null;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
} else if (message.type === "agent-done") {
|
|
241
|
+
isDone = true;
|
|
242
|
+
if (resolveNextMessage) {
|
|
243
|
+
resolveNextMessage({ value: void 0, done: true });
|
|
244
|
+
resolveNextMessage = null;
|
|
245
|
+
rejectNextMessage = null;
|
|
246
|
+
}
|
|
247
|
+
} else if (message.type === "agent-error") {
|
|
248
|
+
errorMessage = message.content ?? "Unknown error";
|
|
249
|
+
isDone = true;
|
|
250
|
+
if (rejectNextMessage) {
|
|
251
|
+
rejectNextMessage(new Error(errorMessage));
|
|
252
|
+
resolveNextMessage = null;
|
|
253
|
+
rejectNextMessage = null;
|
|
254
|
+
}
|
|
142
255
|
}
|
|
256
|
+
});
|
|
257
|
+
if (!relayClient.isConnected()) {
|
|
258
|
+
unsubscribeConnection();
|
|
259
|
+
unsubscribeMessage();
|
|
260
|
+
signal.removeEventListener("abort", handleAbort);
|
|
261
|
+
throw new Error("Relay connection is not open");
|
|
262
|
+
}
|
|
263
|
+
const didSendRequest = relayClient.sendAgentRequest(agentId, contextWithSession);
|
|
264
|
+
if (!didSendRequest) {
|
|
265
|
+
unsubscribeConnection();
|
|
266
|
+
unsubscribeMessage();
|
|
267
|
+
signal.removeEventListener("abort", handleAbort);
|
|
268
|
+
throw new Error("Failed to send agent request: connection not open");
|
|
143
269
|
}
|
|
144
|
-
} finally {
|
|
145
|
-
signal.removeEventListener("abort", handleAbort);
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
var createCachedConnectionChecker = (checkConnection, ttlMs = CONNECTION_CHECK_TTL_MS) => {
|
|
149
|
-
let cache = null;
|
|
150
|
-
return async () => {
|
|
151
|
-
const now = Date.now();
|
|
152
|
-
if (cache && now - cache.timestamp < ttlMs) return cache.result;
|
|
153
270
|
try {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
271
|
+
while (true) {
|
|
272
|
+
if (messageQueue.length > 0) {
|
|
273
|
+
const next = messageQueue.shift();
|
|
274
|
+
if (next !== void 0) {
|
|
275
|
+
yield next;
|
|
276
|
+
}
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
if (isDone || signal.aborted) {
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
const result = await new Promise(
|
|
283
|
+
(resolve, reject) => {
|
|
284
|
+
resolveNextMessage = resolve;
|
|
285
|
+
rejectNextMessage = reject;
|
|
286
|
+
}
|
|
287
|
+
);
|
|
288
|
+
if (result.done) break;
|
|
289
|
+
yield result.value;
|
|
290
|
+
}
|
|
291
|
+
if (errorMessage) {
|
|
292
|
+
throw new Error(errorMessage);
|
|
293
|
+
}
|
|
294
|
+
} finally {
|
|
295
|
+
signal.removeEventListener("abort", handleAbort);
|
|
296
|
+
unsubscribeConnection();
|
|
297
|
+
unsubscribeMessage();
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
const abort = async (sessionId) => {
|
|
301
|
+
relayClient.abortAgent(agentId, sessionId);
|
|
302
|
+
};
|
|
303
|
+
const waitForOperationResponse = (sessionId) => {
|
|
304
|
+
return new Promise((resolve, reject) => {
|
|
305
|
+
let didCleanup = false;
|
|
306
|
+
const cleanup = () => {
|
|
307
|
+
if (didCleanup) return;
|
|
308
|
+
didCleanup = true;
|
|
309
|
+
unsubscribeMessage();
|
|
310
|
+
unsubscribeConnection();
|
|
311
|
+
};
|
|
312
|
+
const unsubscribeMessage = relayClient.onMessage((message) => {
|
|
313
|
+
if (message.sessionId !== sessionId) return;
|
|
314
|
+
cleanup();
|
|
315
|
+
if (message.type === "agent-done") {
|
|
316
|
+
resolve();
|
|
317
|
+
} else if (message.type === "agent-error") {
|
|
318
|
+
reject(new Error(message.content ?? "Operation failed"));
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
const unsubscribeConnection = relayClient.onConnectionChange((connected) => {
|
|
322
|
+
if (!connected) {
|
|
323
|
+
cleanup();
|
|
324
|
+
reject(new Error("Connection lost while waiting for operation response"));
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
};
|
|
329
|
+
const undo = async () => {
|
|
330
|
+
const sessionId = `undo-${agentId}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
331
|
+
const didSend = relayClient.undoAgent(agentId, sessionId);
|
|
332
|
+
if (!didSend) {
|
|
333
|
+
throw new Error("Failed to send undo request: connection not open");
|
|
334
|
+
}
|
|
335
|
+
return waitForOperationResponse(sessionId);
|
|
336
|
+
};
|
|
337
|
+
const redo = async () => {
|
|
338
|
+
const sessionId = `redo-${agentId}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
339
|
+
const didSend = relayClient.redoAgent(agentId, sessionId);
|
|
340
|
+
if (!didSend) {
|
|
341
|
+
throw new Error("Failed to send redo request: connection not open");
|
|
160
342
|
}
|
|
343
|
+
return waitForOperationResponse(sessionId);
|
|
344
|
+
};
|
|
345
|
+
return {
|
|
346
|
+
send,
|
|
347
|
+
abort,
|
|
348
|
+
undo,
|
|
349
|
+
redo,
|
|
350
|
+
checkConnection,
|
|
351
|
+
supportsResume: true,
|
|
352
|
+
supportsFollowUp: true
|
|
161
353
|
};
|
|
162
354
|
};
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
355
|
+
var defaultRelayClient = null;
|
|
356
|
+
var getDefaultRelayClient = () => {
|
|
357
|
+
if (typeof window === "undefined") {
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
if (window.__REACT_GRAB_RELAY__) {
|
|
361
|
+
defaultRelayClient = window.__REACT_GRAB_RELAY__;
|
|
362
|
+
return defaultRelayClient;
|
|
363
|
+
}
|
|
364
|
+
if (!defaultRelayClient) {
|
|
365
|
+
defaultRelayClient = createRelayClient();
|
|
366
|
+
window.__REACT_GRAB_RELAY__ = defaultRelayClient;
|
|
367
|
+
}
|
|
368
|
+
return defaultRelayClient;
|
|
369
|
+
};
|
|
167
370
|
|
|
168
371
|
// src/client.ts
|
|
169
|
-
var
|
|
170
|
-
var
|
|
171
|
-
systemPrompt: {
|
|
172
|
-
type: "preset",
|
|
173
|
-
preset: "claude_code",
|
|
174
|
-
append: `You are helping a user make changes to a React component based on a selected element.
|
|
175
|
-
The user has selected an element from their UI and wants you to help modify it.
|
|
176
|
-
Provide clear, concise status updates as you work.`
|
|
177
|
-
},
|
|
178
|
-
model: "haiku",
|
|
179
|
-
permissionMode: "bypassPermissions",
|
|
180
|
-
maxTurns: 10
|
|
181
|
-
};
|
|
182
|
-
var isReactGrabApi = (value) => typeof value === "object" && value !== null && "setAgent" in value;
|
|
372
|
+
var AGENT_ID = "claude-code";
|
|
373
|
+
var isReactGrabApi = (value) => typeof value === "object" && value !== null && "registerPlugin" in value;
|
|
183
374
|
var createClaudeAgentProvider = (providerOptions = {}) => {
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
375
|
+
const relayClient = providerOptions.relayClient ?? getDefaultRelayClient();
|
|
376
|
+
if (!relayClient) {
|
|
377
|
+
throw new Error("RelayClient is required in browser environments");
|
|
378
|
+
}
|
|
379
|
+
return createRelayAgentProvider({
|
|
380
|
+
relayClient,
|
|
381
|
+
agentId: AGENT_ID
|
|
189
382
|
});
|
|
190
|
-
const checkConnection = createCachedConnectionChecker(async () => {
|
|
191
|
-
const response = await fetch(`${serverUrl}/health`, { method: "GET" });
|
|
192
|
-
return response.ok;
|
|
193
|
-
}, CONNECTION_CHECK_TTL_MS);
|
|
194
|
-
return {
|
|
195
|
-
send: async function* (context, signal) {
|
|
196
|
-
const mergedContext = {
|
|
197
|
-
...context,
|
|
198
|
-
options: mergeOptions(context.options)
|
|
199
|
-
};
|
|
200
|
-
yield* streamAgentStatusFromServer(
|
|
201
|
-
{ serverUrl, completedStatus: COMPLETED_STATUS },
|
|
202
|
-
mergedContext,
|
|
203
|
-
signal
|
|
204
|
-
);
|
|
205
|
-
},
|
|
206
|
-
resume: async function* (sessionId, signal, storage) {
|
|
207
|
-
const storedContext = getStoredAgentContext(storage, sessionId);
|
|
208
|
-
const context = {
|
|
209
|
-
content: storedContext.content,
|
|
210
|
-
prompt: storedContext.prompt,
|
|
211
|
-
options: storedContext.options,
|
|
212
|
-
sessionId: storedContext.sessionId ?? sessionId
|
|
213
|
-
};
|
|
214
|
-
const mergedContext = {
|
|
215
|
-
...context,
|
|
216
|
-
options: mergeOptions(context.options)
|
|
217
|
-
};
|
|
218
|
-
yield "Resuming...";
|
|
219
|
-
yield* streamAgentStatusFromServer(
|
|
220
|
-
{ serverUrl, completedStatus: COMPLETED_STATUS },
|
|
221
|
-
mergedContext,
|
|
222
|
-
signal
|
|
223
|
-
);
|
|
224
|
-
},
|
|
225
|
-
supportsResume: true,
|
|
226
|
-
supportsFollowUp: true,
|
|
227
|
-
checkConnection,
|
|
228
|
-
undo: async () => {
|
|
229
|
-
try {
|
|
230
|
-
await fetch(`${serverUrl}/undo`, { method: "POST" });
|
|
231
|
-
} catch {
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
};
|
|
235
383
|
};
|
|
236
384
|
var attachAgent = async () => {
|
|
237
385
|
if (typeof window === "undefined") return;
|
|
238
|
-
const
|
|
386
|
+
const relayClient = getDefaultRelayClient();
|
|
387
|
+
if (!relayClient) return;
|
|
388
|
+
try {
|
|
389
|
+
await relayClient.connect();
|
|
390
|
+
} catch {
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
const provider = createRelayAgentProvider({
|
|
394
|
+
relayClient,
|
|
395
|
+
agentId: AGENT_ID
|
|
396
|
+
});
|
|
239
397
|
const attach = (api) => {
|
|
240
|
-
|
|
398
|
+
const agent = { provider, storage: sessionStorage };
|
|
399
|
+
const plugin = {
|
|
400
|
+
name: "claude-code-agent",
|
|
401
|
+
actions: [
|
|
402
|
+
{
|
|
403
|
+
id: "edit-with-claude-code",
|
|
404
|
+
label: "Edit with Claude",
|
|
405
|
+
shortcut: "Enter",
|
|
406
|
+
onAction: (actionContext) => {
|
|
407
|
+
actionContext.enterPromptMode?.(agent);
|
|
408
|
+
},
|
|
409
|
+
agent
|
|
410
|
+
}
|
|
411
|
+
]
|
|
412
|
+
};
|
|
413
|
+
api.registerPlugin(plugin);
|
|
241
414
|
};
|
|
242
415
|
const existingApi = window.__REACT_GRAB__;
|
|
243
416
|
if (isReactGrabApi(existingApi)) {
|
package/dist/client.d.cts
CHANGED
|
@@ -1,20 +1,11 @@
|
|
|
1
|
-
import { init
|
|
1
|
+
import { init } from 'react-grab/core';
|
|
2
2
|
export { AgentCompleteResult } from 'react-grab/core';
|
|
3
|
-
import {
|
|
3
|
+
import { RelayClient, AgentProvider } from '@react-grab/relay/client';
|
|
4
4
|
|
|
5
|
-
type ClaudeAgentContext = AgentContext<Options>;
|
|
6
5
|
interface ClaudeAgentProviderOptions {
|
|
7
|
-
|
|
8
|
-
getOptions?: () => Partial<Options>;
|
|
6
|
+
relayClient?: RelayClient;
|
|
9
7
|
}
|
|
10
|
-
declare const createClaudeAgentProvider: (providerOptions?: ClaudeAgentProviderOptions) =>
|
|
11
|
-
send: (context: ClaudeAgentContext, signal: AbortSignal) => AsyncGenerator<string, void, unknown>;
|
|
12
|
-
resume: (sessionId: string, signal: AbortSignal, storage: AgentSessionStorage) => AsyncGenerator<string, void, unknown>;
|
|
13
|
-
supportsResume: boolean;
|
|
14
|
-
supportsFollowUp: boolean;
|
|
15
|
-
checkConnection: () => Promise<boolean>;
|
|
16
|
-
undo: () => Promise<void>;
|
|
17
|
-
};
|
|
8
|
+
declare const createClaudeAgentProvider: (providerOptions?: ClaudeAgentProviderOptions) => AgentProvider;
|
|
18
9
|
declare global {
|
|
19
10
|
interface Window {
|
|
20
11
|
__REACT_GRAB__?: ReturnType<typeof init>;
|
package/dist/client.d.ts
CHANGED
|
@@ -1,20 +1,11 @@
|
|
|
1
|
-
import { init
|
|
1
|
+
import { init } from 'react-grab/core';
|
|
2
2
|
export { AgentCompleteResult } from 'react-grab/core';
|
|
3
|
-
import {
|
|
3
|
+
import { RelayClient, AgentProvider } from '@react-grab/relay/client';
|
|
4
4
|
|
|
5
|
-
type ClaudeAgentContext = AgentContext<Options>;
|
|
6
5
|
interface ClaudeAgentProviderOptions {
|
|
7
|
-
|
|
8
|
-
getOptions?: () => Partial<Options>;
|
|
6
|
+
relayClient?: RelayClient;
|
|
9
7
|
}
|
|
10
|
-
declare const createClaudeAgentProvider: (providerOptions?: ClaudeAgentProviderOptions) =>
|
|
11
|
-
send: (context: ClaudeAgentContext, signal: AbortSignal) => AsyncGenerator<string, void, unknown>;
|
|
12
|
-
resume: (sessionId: string, signal: AbortSignal, storage: AgentSessionStorage) => AsyncGenerator<string, void, unknown>;
|
|
13
|
-
supportsResume: boolean;
|
|
14
|
-
supportsFollowUp: boolean;
|
|
15
|
-
checkConnection: () => Promise<boolean>;
|
|
16
|
-
undo: () => Promise<void>;
|
|
17
|
-
};
|
|
8
|
+
declare const createClaudeAgentProvider: (providerOptions?: ClaudeAgentProviderOptions) => AgentProvider;
|
|
18
9
|
declare global {
|
|
19
10
|
interface Window {
|
|
20
11
|
__REACT_GRAB__?: ReturnType<typeof init>;
|