File size: 4,858 Bytes
4326cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808bcfb
4326cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808bcfb
 
 
 
 
 
 
4326cd6
 
 
 
 
 
 
 
808bcfb
4326cd6
 
 
 
808bcfb
 
 
 
 
 
 
 
 
4326cd6
 
808bcfb
 
 
4326cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808bcfb
4326cd6
 
 
 
 
 
 
 
 
808bcfb
4326cd6
 
 
 
 
 
808bcfb
4326cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808bcfb
4326cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<script lang="ts">
	import { browser, dev } from "$app/environment";
	import { invalidate } from "$app/navigation";

	import {
		type BackgroundGeneration,
		backgroundGenerationEntries,
		removeBackgroundGeneration,
	} from "$lib/stores/backgroundGenerations";
	import { handleResponse, useAPIClient } from "$lib/APIClient";
	import { UrlDependency } from "$lib/types/UrlDependency";
	import { MessageUpdateStatus, MessageUpdateType } from "$lib/types/MessageUpdate";
	import type { Message } from "$lib/types/Message";

	const POLL_INTERVAL_MS = 1000;
	const MAX_POLL_DURATION_MS = 3 * 60_000;

	const client = useAPIClient();
	const pollers = new Map<string, () => void>();
	const inflight = new Set<string>();
	const assistantSnapshots = new Map<string, string>();
	const failureCounts = new Map<string, number>();

	$effect.root(() => {
		if (!browser) {
			pollers.clear();
			return;
		}

		let destroyed = false;

		const log = (...args: unknown[]) => {
			if (dev) {
				console.log("background generation", ...args);
			}
		};

		const stopPoller = (id: string, reason?: string) => {
			const stop = pollers.get(id);
			if (!stop) return;

			stop();
			pollers.delete(id);
			inflight.delete(id);
			assistantSnapshots.delete(id);
			failureCounts.delete(id);
			log("stop", id, reason);
		};

		const pollOnce = async (id: string) => {
			if (destroyed || inflight.has(id)) return;

			const entry = backgroundGenerationEntries.find((candidate) => candidate.id === id);
			if (entry && Date.now() - entry.startedAt > MAX_POLL_DURATION_MS) {
				removeBackgroundGeneration(id);
				stopPoller(id, "timed out");
				log("timeout", id);
				await invalidate(UrlDependency.ConversationList);
				await invalidate(UrlDependency.Conversation);
				return;
			}

			inflight.add(id);
			log("poll", id);

			try {
				const response = await client.conversations({ id }).get();
				const conversation = handleResponse(response);
				const messages = conversation?.messages ?? [];
				const lastAssistant = [...messages]
					.reverse()
					.find((message: Message) => message.from === "assistant");

				const hasFinalAnswer =
					lastAssistant?.updates?.some((update) => update.type === MessageUpdateType.FinalAnswer) ??
					false;
				const hasError =
					lastAssistant?.updates?.some(
						(update) =>
							update.type === MessageUpdateType.Status &&
							update.status === MessageUpdateStatus.Error
					) ?? false;

				const snapshot = lastAssistant
					? JSON.stringify({
							id: lastAssistant.id,
							updatedAt: lastAssistant.updatedAt,
							contentLength: lastAssistant.content?.length ?? 0,
							updatesLength: lastAssistant.updates?.length ?? 0,
						})
					: "__none__";
				const previousSnapshot = assistantSnapshots.get(id);
				let shouldInvalidateConversation = false;

				if (lastAssistant) {
					assistantSnapshots.set(id, snapshot);
					if (snapshot !== previousSnapshot) {
						shouldInvalidateConversation = true;
					}
				} else if (assistantSnapshots.has(id)) {
					assistantSnapshots.delete(id);
					shouldInvalidateConversation = true;
				}

				if (lastAssistant && (hasFinalAnswer || hasError)) {
					removeBackgroundGeneration(id);
					assistantSnapshots.delete(id);
					failureCounts.delete(id);
					shouldInvalidateConversation = true;
					log("complete", id, hasFinalAnswer ? "final" : "error");
					await invalidate(UrlDependency.ConversationList);
				}

				if (shouldInvalidateConversation) {
					await invalidate(UrlDependency.Conversation);
				}

				failureCounts.delete(id);
			} catch (err) {
				console.error("Background generation poll failed", id, err);
				const failures = (failureCounts.get(id) ?? 0) + 1;
				failureCounts.set(id, failures);
				if (failures >= 3) {
					removeBackgroundGeneration(id);
					assistantSnapshots.delete(id);
					failureCounts.delete(id);
					log("failures", id, failures);
					await invalidate(UrlDependency.ConversationList);
				}
			} finally {
				inflight.delete(id);
			}
		};

		const startPoller = (entry: BackgroundGeneration) => {
			if (pollers.has(entry.id)) return;

			const intervalId = setInterval(() => {
				void pollOnce(entry.id);
			}, POLL_INTERVAL_MS);

			pollers.set(entry.id, () => clearInterval(intervalId));
			void pollOnce(entry.id);
			log("start", entry.id);
		};

		$effect(() => {
			const entries = backgroundGenerationEntries;

			if (destroyed) return;

			const activeIds = new Set(entries.map((entry) => entry.id));

			for (const id of pollers.keys()) {
				if (!activeIds.has(id)) {
					stopPoller(id);
				}
			}

			for (const entry of entries) {
				startPoller(entry);
			}
		});

		return () => {
			destroyed = true;
			for (const stop of pollers.values()) stop();
			pollers.clear();
			inflight.clear();
			assistantSnapshots.clear();
			failureCounts.clear();
		};
	});
</script>