@react-grab/codex 0.0.81 → 0.0.83

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 CHANGED
@@ -1,27 +1,23 @@
1
- // src/constants.ts
2
- var DEFAULT_PORT = 7567;
1
+ // ../utils/dist/client.js
3
2
  var CONNECTION_CHECK_TTL_MS = 5e3;
4
-
5
- // src/client.ts
6
- var DEFAULT_SERVER_URL = `http://localhost:${DEFAULT_PORT}`;
7
3
  var STORAGE_KEY = "react-grab:agent-sessions";
8
- var parseServerSentEvent = (eventStringBlock) => {
4
+ var parseSSEEvent = (eventBlock) => {
9
5
  let eventType = "";
10
6
  let data = "";
11
- for (const line of eventStringBlock.split("\n")) {
7
+ for (const line of eventBlock.split("\n")) {
12
8
  if (line.startsWith("event:")) eventType = line.slice(6).trim();
13
9
  else if (line.startsWith("data:")) data = line.slice(5).trim();
14
10
  }
15
11
  return { eventType, data };
16
12
  };
17
- var streamSSE = async function* (stream, signal) {
18
- const streamReader = stream.getReader();
19
- const textDecoder = new TextDecoder();
20
- let textBuffer = "";
13
+ async function* streamSSE(stream, signal) {
14
+ const reader = stream.getReader();
15
+ const decoder = new TextDecoder();
16
+ let buffer = "";
21
17
  let aborted = false;
22
18
  const onAbort = () => {
23
19
  aborted = true;
24
- streamReader.cancel().catch(() => {
20
+ reader.cancel().catch(() => {
25
21
  });
26
22
  };
27
23
  signal.addEventListener("abort", onAbort);
@@ -30,18 +26,16 @@ var streamSSE = async function* (stream, signal) {
30
26
  throw new DOMException("Aborted", "AbortError");
31
27
  }
32
28
  while (true) {
33
- const result = await streamReader.read();
29
+ const result = await reader.read();
34
30
  if (aborted || signal.aborted) {
35
31
  throw new DOMException("Aborted", "AbortError");
36
32
  }
37
33
  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);
34
+ if (value) buffer += decoder.decode(value, { stream: true });
35
+ let boundary;
36
+ while ((boundary = buffer.indexOf("\n\n")) !== -1) {
37
+ const { eventType, data } = parseSSEEvent(buffer.slice(0, boundary));
38
+ buffer = buffer.slice(boundary + 2);
45
39
  if (eventType === "done") return;
46
40
  if (eventType === "error") throw new Error(data || "Agent error");
47
41
  if (data) yield data;
@@ -51,12 +45,19 @@ var streamSSE = async function* (stream, signal) {
51
45
  } finally {
52
46
  signal.removeEventListener("abort", onAbort);
53
47
  try {
54
- streamReader.releaseLock();
48
+ reader.releaseLock();
55
49
  } catch {
56
50
  }
57
51
  }
58
- };
52
+ }
53
+
54
+ // src/constants.ts
55
+ var DEFAULT_PORT = 7567;
56
+
57
+ // src/client.ts
58
+ var DEFAULT_SERVER_URL = `http://localhost:${DEFAULT_PORT}`;
59
59
  var streamFromServer = async function* (serverUrl, context, signal) {
60
+ const startTime = Date.now();
60
61
  const sessionId = context.sessionId;
61
62
  const handleAbort = () => {
62
63
  if (sessionId) {
@@ -80,7 +81,37 @@ var streamFromServer = async function* (serverUrl, context, signal) {
80
81
  if (!response.body) {
81
82
  throw new Error("No response body");
82
83
  }
83
- yield* streamSSE(response.body, signal);
84
+ const iterator = streamSSE(response.body, signal)[Symbol.asyncIterator]();
85
+ let done = false;
86
+ let pendingNext = iterator.next();
87
+ while (!done) {
88
+ const result = await Promise.race([
89
+ pendingNext.then((iteratorResult) => ({
90
+ type: "status",
91
+ iteratorResult
92
+ })),
93
+ new Promise(
94
+ (resolve) => setTimeout(() => resolve({ type: "timeout" }), 100)
95
+ )
96
+ ]);
97
+ if (result.type === "timeout") {
98
+ const elapsedSeconds = (Date.now() - startTime) / 1e3;
99
+ yield `Working\u2026 ${elapsedSeconds.toFixed(1)}s`;
100
+ } else {
101
+ const iteratorResult = result.iteratorResult;
102
+ done = iteratorResult.done ?? false;
103
+ if (!done && iteratorResult.value) {
104
+ const status = iteratorResult.value;
105
+ if (status === "Completed successfully") {
106
+ const totalSeconds = ((Date.now() - startTime) / 1e3).toFixed(1);
107
+ yield `Completed in ${totalSeconds}s`;
108
+ } else {
109
+ yield status;
110
+ }
111
+ pendingNext = iterator.next();
112
+ }
113
+ }
114
+ }
84
115
  } finally {
85
116
  signal.removeEventListener("abort", handleAbort);
86
117
  }