@react-grab/codex 0.0.81

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.js ADDED
@@ -0,0 +1,172 @@
1
+ // src/constants.ts
2
+ var DEFAULT_PORT = 7567;
3
+ var CONNECTION_CHECK_TTL_MS = 5e3;
4
+
5
+ // src/client.ts
6
+ var DEFAULT_SERVER_URL = `http://localhost:${DEFAULT_PORT}`;
7
+ var STORAGE_KEY = "react-grab:agent-sessions";
8
+ var parseServerSentEvent = (eventStringBlock) => {
9
+ let eventType = "";
10
+ let data = "";
11
+ for (const line of eventStringBlock.split("\n")) {
12
+ if (line.startsWith("event:")) eventType = line.slice(6).trim();
13
+ else if (line.startsWith("data:")) data = line.slice(5).trim();
14
+ }
15
+ return { eventType, data };
16
+ };
17
+ var streamSSE = async function* (stream, signal) {
18
+ const streamReader = stream.getReader();
19
+ const textDecoder = new TextDecoder();
20
+ let textBuffer = "";
21
+ let aborted = false;
22
+ const onAbort = () => {
23
+ aborted = true;
24
+ streamReader.cancel().catch(() => {
25
+ });
26
+ };
27
+ signal.addEventListener("abort", onAbort);
28
+ try {
29
+ if (signal.aborted) {
30
+ throw new DOMException("Aborted", "AbortError");
31
+ }
32
+ while (true) {
33
+ const result = await streamReader.read();
34
+ if (aborted || signal.aborted) {
35
+ throw new DOMException("Aborted", "AbortError");
36
+ }
37
+ const { done, value } = result;
38
+ if (value) textBuffer += textDecoder.decode(value, { stream: true });
39
+ let boundaryIndex;
40
+ while ((boundaryIndex = textBuffer.indexOf("\n\n")) !== -1) {
41
+ const { eventType, data } = parseServerSentEvent(
42
+ textBuffer.slice(0, boundaryIndex)
43
+ );
44
+ textBuffer = textBuffer.slice(boundaryIndex + 2);
45
+ if (eventType === "done") return;
46
+ if (eventType === "error") throw new Error(data || "Agent error");
47
+ if (data) yield data;
48
+ }
49
+ if (done) break;
50
+ }
51
+ } finally {
52
+ signal.removeEventListener("abort", onAbort);
53
+ try {
54
+ streamReader.releaseLock();
55
+ } catch {
56
+ }
57
+ }
58
+ };
59
+ var streamFromServer = async function* (serverUrl, context, signal) {
60
+ const sessionId = context.sessionId;
61
+ const handleAbort = () => {
62
+ if (sessionId) {
63
+ fetch(`${serverUrl}/abort/${sessionId}`, { method: "POST" }).catch(
64
+ () => {
65
+ }
66
+ );
67
+ }
68
+ };
69
+ signal.addEventListener("abort", handleAbort);
70
+ try {
71
+ const response = await fetch(`${serverUrl}/agent`, {
72
+ method: "POST",
73
+ headers: { "Content-Type": "application/json" },
74
+ body: JSON.stringify(context),
75
+ signal
76
+ });
77
+ if (!response.ok) {
78
+ throw new Error(`Server error: ${response.status}`);
79
+ }
80
+ if (!response.body) {
81
+ throw new Error("No response body");
82
+ }
83
+ yield* streamSSE(response.body, signal);
84
+ } finally {
85
+ signal.removeEventListener("abort", handleAbort);
86
+ }
87
+ };
88
+ var createCodexAgentProvider = (options = {}) => {
89
+ const { serverUrl = DEFAULT_SERVER_URL, getOptions } = options;
90
+ let connectionCache = null;
91
+ const mergeOptions = (contextOptions) => ({
92
+ ...getOptions?.() ?? {},
93
+ ...contextOptions ?? {}
94
+ });
95
+ return {
96
+ send: async function* (context, signal) {
97
+ const combinedContext = {
98
+ ...context,
99
+ options: mergeOptions(context.options)
100
+ };
101
+ yield* streamFromServer(serverUrl, combinedContext, signal);
102
+ },
103
+ resume: async function* (sessionId, signal, storage) {
104
+ const storedSessions = storage.getItem(STORAGE_KEY);
105
+ if (!storedSessions) {
106
+ throw new Error("No sessions to resume");
107
+ }
108
+ const parsedSessions = JSON.parse(storedSessions);
109
+ const session = parsedSessions[sessionId];
110
+ if (!session) {
111
+ throw new Error(`Session ${sessionId} not found`);
112
+ }
113
+ const context = session.context;
114
+ const combinedContext = {
115
+ ...context,
116
+ options: mergeOptions(context.options)
117
+ };
118
+ yield "Resuming...";
119
+ yield* streamFromServer(serverUrl, combinedContext, signal);
120
+ },
121
+ supportsResume: true,
122
+ supportsFollowUp: true,
123
+ checkConnection: async () => {
124
+ const now = Date.now();
125
+ if (connectionCache && now - connectionCache.timestamp < CONNECTION_CHECK_TTL_MS) {
126
+ return connectionCache.result;
127
+ }
128
+ try {
129
+ const response = await fetch(`${serverUrl}/health`, { method: "GET" });
130
+ const result = response.ok;
131
+ connectionCache = { result, timestamp: now };
132
+ return result;
133
+ } catch {
134
+ connectionCache = { result: false, timestamp: now };
135
+ return false;
136
+ }
137
+ },
138
+ undo: async () => {
139
+ try {
140
+ await fetch(`${serverUrl}/undo`, { method: "POST" });
141
+ } catch {
142
+ }
143
+ }
144
+ };
145
+ };
146
+ var attachAgent = async () => {
147
+ if (typeof window === "undefined") return;
148
+ const provider = createCodexAgentProvider();
149
+ const attach = (api2) => {
150
+ api2.setAgent({ provider, storage: sessionStorage });
151
+ };
152
+ const api = window.__REACT_GRAB__;
153
+ if (api) {
154
+ attach(api);
155
+ return;
156
+ }
157
+ window.addEventListener(
158
+ "react-grab:init",
159
+ (event) => {
160
+ const customEvent = event;
161
+ attach(customEvent.detail);
162
+ },
163
+ { once: true }
164
+ );
165
+ const apiAfterListener = window.__REACT_GRAB__;
166
+ if (apiAfterListener) {
167
+ attach(apiAfterListener);
168
+ }
169
+ };
170
+ attachAgent();
171
+
172
+ export { attachAgent, createCodexAgentProvider };