File size: 7,269 Bytes
634e30a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { createPubSub } from "create-pubsub";
import throttle from "throttleit";
import { addLogEntry } from "./logEntries";
import { defaultSettings, SETTINGS_STORAGE_KEY } from "./settings";
import type {
  ImageSearchResults,
  SearchResults,
  SearchState,
  TextGenerationState,
  TextSearchResults,
} from "./types";

/**
 * Creates a PubSub instance that persists data to localStorage
 * @param localStorageKey - The key to use for localStorage storage
 * @param defaultValue - The default value if no localStorage value exists
 * @returns A PubSub instance with localStorage persistence
 */
function createLocalStoragePubSub<T>(localStorageKey: string, defaultValue: T) {
  const localStorageValue = localStorage.getItem(localStorageKey);
  const localStoragePubSub = createPubSub(
    localStorageValue ? (JSON.parse(localStorageValue) as T) : defaultValue,
  );

  const [, onValueChange] = localStoragePubSub;

  onValueChange((value) =>
    localStorage.setItem(localStorageKey, JSON.stringify(value)),
  );

  return localStoragePubSub;
}

/**
 * PubSub instance for managing query suggestions with localStorage persistence
 */
const querySuggestionsPubSub = createLocalStoragePubSub<string[]>(
  "querySuggestions",
  [],
);

/**
 * PubSub instance for managing the last search token hash with localStorage persistence
 */
const lastSearchTokenHashPubSub = createLocalStoragePubSub(
  "lastSearchTokenHash",
  "",
);

/**
 * Updates the last search token hash
 */
export const [updateLastSearchTokenHash, , getLastSearchTokenHash] =
  lastSearchTokenHashPubSub;

/**
 * Updates query suggestions
 */
export const [updateQuerySuggestions, , getQuerySuggestions] =
  querySuggestionsPubSub;

/**
 * PubSub instance for managing the current search query
 */
export const queryPubSub = createPubSub(
  new URLSearchParams(self.location.search).get("q") ?? "",
);

export const [, , getQuery] = queryPubSub;

/**
 * PubSub instance for managing the AI response content
 */
export const responsePubSub = createPubSub("");

/**
 * Throttled function to update the response content (12 updates per second)
 */
export const updateResponse = throttle(responsePubSub[0], 1000 / 12);
export const [, , getResponse] = responsePubSub;

/**
 * PubSub instance for managing the search promise
 */
export const [updateSearchPromise, , getSearchPromise] = createPubSub<
  Promise<SearchResults>
>(Promise.resolve({ textResults: [], imageResults: [] }));

/**
 * PubSub instance for managing text generation state
 */
export const textGenerationStatePubSub =
  createPubSub<TextGenerationState>("idle");

/**
 * Updates the text generation state
 */
export const [updateTextGenerationState, , getTextGenerationState] =
  textGenerationStatePubSub;

const [, listenToTextGenerationStateChanges] = textGenerationStatePubSub;

listenToTextGenerationStateChanges((textGenerationState) => {
  addLogEntry(`Text generation state changed to '${textGenerationState}'`);
});

/**
 * PubSub instance for managing model loading progress
 */
export const modelLoadingProgressPubSub = createPubSub(0);

/**
 * Updates the model loading progress
 */
export const [updateModelLoadingProgress] = modelLoadingProgressPubSub;

/**
 * PubSub instance for managing application settings with localStorage persistence
 */
export const settingsPubSub = createLocalStoragePubSub(
  SETTINGS_STORAGE_KEY,
  defaultSettings,
);

export const [, listenToSettingsChanges, getSettings] = settingsPubSub;

/**
 * PubSub instance for managing model size in megabytes
 */
export const modelSizeInMegabytesPubSub = createPubSub(0);

/**
 * Updates the model size in megabytes
 */
export const [updateModelSizeInMegabytes] = modelSizeInMegabytesPubSub;

/**
 * PubSub instance for managing text search state
 */
export const textSearchStatePubSub = createPubSub<SearchState>("idle");
/**
 * PubSub instance for managing image search state
 */
export const imageSearchStatePubSub = createPubSub<SearchState>("idle");

/**
 * Updates the text search state
 */
export const [updateTextSearchState] = textSearchStatePubSub;

const [, subscribeToTextSearchState] = textSearchStatePubSub;

subscribeToTextSearchState((textSearchState) => {
  addLogEntry(`Text search state changed to '${textSearchState}'`);
});

/**
 * Updates the image search state
 */
export const [updateImageSearchState] = imageSearchStatePubSub;

const [, subscribeToImageSearchState] = imageSearchStatePubSub;

subscribeToImageSearchState((imageSearchState) => {
  addLogEntry(`Image search state changed to '${imageSearchState}'`);
});

/**
 * PubSub instance for managing text search results
 */
export const textSearchResultsPubSub = createPubSub<TextSearchResults>([]);

/**
 * PubSub instance for managing LLM-generated text search results
 */
const llmTextSearchResultsPubSub = createPubSub<TextSearchResults>([]);

/**
 * PubSub instance for managing image search results
 */
export const imageSearchResultsPubSub = createPubSub<ImageSearchResults>([]);

/**
 * Updates text search results
 */
export const [updateTextSearchResults] = textSearchResultsPubSub;

/**
 * Updates LLM text search results
 */
export const [updateLlmTextSearchResults, , getLlmTextSearchResults] =
  llmTextSearchResultsPubSub;

/**
 * Updates image search results
 */
export const [updateImageSearchResults] = imageSearchResultsPubSub;

/**
 * PubSub instance for managing expanded menu accordions with localStorage persistence
 */
export const menuExpandedAccordionsPubSub = createLocalStoragePubSub<string[]>(
  "menuExpandedAccordions",
  [],
);

/**
 * PubSub instance for managing chat input content
 */
export const chatInputPubSub = createPubSub("");

/**
 * Updates chat input content
 */
export const [updateChatInput] = chatInputPubSub;

/**
 * PubSub instance for managing chat generation state
 */
export const chatGenerationStatePubSub = createPubSub({
  isGeneratingResponse: false,
  isGeneratingFollowUpQuestion: false,
});

/**
 * PubSub instance for managing follow-up questions
 */
export const followUpQuestionPubSub = createPubSub("");

/**
 * Updates follow-up question
 */
export const [updateFollowUpQuestion] = followUpQuestionPubSub;

/**
 * PubSub instance for managing conversation summary
 */
const conversationSummaryPubSub = createPubSub({
  conversationId: "",
  summary: "",
});

/**
 * Updates conversation summary
 */
export const [updateConversationSummary, , getConversationSummary] =
  conversationSummaryPubSub;

/**
 * PubSub instance for managing chat messages
 */
export const chatMessagesPubSub = createPubSub<
  Array<{ role: "user" | "assistant"; content: string }>
>([]);

/**
 * Updates chat messages
 */
export const [updateChatMessages] = chatMessagesPubSub;

/**
 * PubSub instance for managing history restoration state
 */
export const isRestoringFromHistoryPubSub = createPubSub(false);

/**
 * Updates history restoration state
 */
export const [updateIsRestoringFromHistory] = isRestoringFromHistoryPubSub;

/**
 * PubSub instance for managing follow-up question suppression
 */
export const suppressNextFollowUpPubSub = createPubSub(false);

/**
 * Updates follow-up question suppression state
 */
export const [updateSuppressNextFollowUp, , getSuppressNextFollowUp] =
  suppressNextFollowUpPubSub;