victor HF Staff commited on
Commit
808bcfb
·
1 Parent(s): 4326cd6

fix: enhance logging and timeout handling in BackgroundGenerationPoller component

Browse files
src/lib/components/BackgroundGenerationPoller.svelte CHANGED
@@ -13,6 +13,7 @@
13
  import type { Message } from "$lib/types/Message";
14
 
15
  const POLL_INTERVAL_MS = 1000;
 
16
 
17
  const client = useAPIClient();
18
  const pollers = new Map<string, () => void>();
@@ -28,7 +29,13 @@
28
 
29
  let destroyed = false;
30
 
31
- const stopPoller = (id: string) => {
 
 
 
 
 
 
32
  const stop = pollers.get(id);
33
  if (!stop) return;
34
 
@@ -37,15 +44,25 @@
37
  inflight.delete(id);
38
  assistantSnapshots.delete(id);
39
  failureCounts.delete(id);
 
40
  };
41
 
42
  const pollOnce = async (id: string) => {
43
  if (destroyed || inflight.has(id)) return;
44
- inflight.add(id);
45
- if (dev) {
46
- console.log("background generation poll", id);
 
 
 
 
 
 
47
  }
48
 
 
 
 
49
  try {
50
  const response = await client.conversations({ id }).get();
51
  const conversation = handleResponse(response);
@@ -90,6 +107,7 @@
90
  assistantSnapshots.delete(id);
91
  failureCounts.delete(id);
92
  shouldInvalidateConversation = true;
 
93
  await invalidate(UrlDependency.ConversationList);
94
  }
95
 
@@ -99,13 +117,14 @@
99
 
100
  failureCounts.delete(id);
101
  } catch (err) {
102
- console.error("Background generation poll failed", err);
103
  const failures = (failureCounts.get(id) ?? 0) + 1;
104
  failureCounts.set(id, failures);
105
  if (failures >= 3) {
106
  removeBackgroundGeneration(id);
107
  assistantSnapshots.delete(id);
108
  failureCounts.delete(id);
 
109
  await invalidate(UrlDependency.ConversationList);
110
  }
111
  } finally {
@@ -122,6 +141,7 @@
122
 
123
  pollers.set(entry.id, () => clearInterval(intervalId));
124
  void pollOnce(entry.id);
 
125
  };
126
 
127
  $effect(() => {
 
13
  import type { Message } from "$lib/types/Message";
14
 
15
  const POLL_INTERVAL_MS = 1000;
16
+ const MAX_POLL_DURATION_MS = 3 * 60_000;
17
 
18
  const client = useAPIClient();
19
  const pollers = new Map<string, () => void>();
 
29
 
30
  let destroyed = false;
31
 
32
+ const log = (...args: unknown[]) => {
33
+ if (dev) {
34
+ console.log("background generation", ...args);
35
+ }
36
+ };
37
+
38
+ const stopPoller = (id: string, reason?: string) => {
39
  const stop = pollers.get(id);
40
  if (!stop) return;
41
 
 
44
  inflight.delete(id);
45
  assistantSnapshots.delete(id);
46
  failureCounts.delete(id);
47
+ log("stop", id, reason);
48
  };
49
 
50
  const pollOnce = async (id: string) => {
51
  if (destroyed || inflight.has(id)) return;
52
+
53
+ const entry = backgroundGenerationEntries.find((candidate) => candidate.id === id);
54
+ if (entry && Date.now() - entry.startedAt > MAX_POLL_DURATION_MS) {
55
+ removeBackgroundGeneration(id);
56
+ stopPoller(id, "timed out");
57
+ log("timeout", id);
58
+ await invalidate(UrlDependency.ConversationList);
59
+ await invalidate(UrlDependency.Conversation);
60
+ return;
61
  }
62
 
63
+ inflight.add(id);
64
+ log("poll", id);
65
+
66
  try {
67
  const response = await client.conversations({ id }).get();
68
  const conversation = handleResponse(response);
 
107
  assistantSnapshots.delete(id);
108
  failureCounts.delete(id);
109
  shouldInvalidateConversation = true;
110
+ log("complete", id, hasFinalAnswer ? "final" : "error");
111
  await invalidate(UrlDependency.ConversationList);
112
  }
113
 
 
117
 
118
  failureCounts.delete(id);
119
  } catch (err) {
120
+ console.error("Background generation poll failed", id, err);
121
  const failures = (failureCounts.get(id) ?? 0) + 1;
122
  failureCounts.set(id, failures);
123
  if (failures >= 3) {
124
  removeBackgroundGeneration(id);
125
  assistantSnapshots.delete(id);
126
  failureCounts.delete(id);
127
+ log("failures", id, failures);
128
  await invalidate(UrlDependency.ConversationList);
129
  }
130
  } finally {
 
141
 
142
  pollers.set(entry.id, () => clearInterval(intervalId));
143
  void pollOnce(entry.id);
144
+ log("start", entry.id);
145
  };
146
 
147
  $effect(() => {