gemi 0.242.3 → 0.243.0

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.
@@ -1137,6 +1137,18 @@ var listed = (flight) => flight.clientRunId !== void 0 || flight.seen === true;
1137
1137
  var sameRuns = (a, b) => a.length === b.length && a.every((run, i) => run.id === b[i].id && run.runId === b[i].runId && run.turn === b[i].turn && run.status === b[i].status);
1138
1138
  /** How often a run announced mid-stream checks whether the chat is free to attach it. */
1139
1139
  var WATCH_RECHECK_MS = 100;
1140
+ /**
1141
+ * How long the requests in flight outlive the hook's effects (#1391).
1142
+ *
1143
+ * React runs effect cleanups without unmounting: an `<Activity>` that hides
1144
+ * the chat, and in development a `<StrictMode>` that reconnects effects when a
1145
+ * re-suspended `<Suspense>` boundary shows its content again. The state
1146
+ * survives and the effects run again on show, so a reply that is streaming
1147
+ * must survive too. A cleanup only starts this timer; the effect running again
1148
+ * cancels it. A chat still gone when it fires is taken as unmounted, and its
1149
+ * requests are closed.
1150
+ */
1151
+ var DISCONNECT_GRACE_MS = 3e4;
1140
1152
  /** The first wait before `watch` follows the thread again after a dropped stream. */
1141
1153
  var WATCH_RETRY_MS = 1e3;
1142
1154
  var WATCH_RETRY_MAX_MS = 3e4;
@@ -1217,14 +1229,39 @@ function useChat(path, params = {}) {
1217
1229
  extraBody,
1218
1230
  ifBusy: hookIfBusy
1219
1231
  };
1232
+ const disconnectRef = useRef(null);
1220
1233
  useEffect(() => {
1221
- mountedRef.current = true;
1234
+ if (disconnectRef.current !== null) {
1235
+ clearTimeout(disconnectRef.current);
1236
+ disconnectRef.current = null;
1237
+ }
1238
+ if (!mountedRef.current) {
1239
+ mountedRef.current = true;
1240
+ setState(stateRef.current);
1241
+ setPhase(phaseRef.current);
1242
+ syncRuns();
1243
+ }
1222
1244
  return () => {
1223
- mountedRef.current = false;
1224
- for (const flight of flightsRef.current) flight.controller.abort();
1225
- flightsRef.current.clear();
1245
+ disconnectRef.current = setTimeout(() => {
1246
+ disconnectRef.current = null;
1247
+ mountedRef.current = false;
1248
+ if (flightsRef.current.size === 0) return;
1249
+ for (const flight of flightsRef.current) flight.controller.abort();
1250
+ flightsRef.current.clear();
1251
+ phaseRef.current = "idle";
1252
+ const cut = markAllAborted(stateRef.current);
1253
+ stateRef.current = {
1254
+ ...cut,
1255
+ pending: withAnswering(cut.pending, new Set(cut.pending.map((call) => call.toolCallId)), false),
1256
+ error: {
1257
+ code: "aborted",
1258
+ message: "The chat was hidden while a reply streamed, and the request was closed.",
1259
+ retryable: true
1260
+ }
1261
+ };
1262
+ }, DISCONNECT_GRACE_MS);
1226
1263
  };
1227
- }, []);
1264
+ }, [syncRuns]);
1228
1265
  const commit = useCallback((next) => {
1229
1266
  stateRef.current = next;
1230
1267
  if (mountedRef.current) setState(next);