-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhyde.ts
More file actions
575 lines (514 loc) · 15.7 KB
/
Copy pathhyde.ts
File metadata and controls
575 lines (514 loc) · 15.7 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
import { getLoggedInClient, getSupabaseContext } from "./supabaseContext";
import { Result } from "./types";
import normalizePageTitle from "roamjs-components/queries/normalizePageTitle";
import { render as renderToast } from "roamjs-components/components/Toast";
import findDiscourseNode from "./findDiscourseNode";
import { nextApiRoot } from "@repo/utils/execContext";
import { DiscourseNode } from "./getDiscourseNodes";
import { getNodesByType } from "@repo/database/lib/queries";
import getAllReferencesOnPage from "./getAllReferencesOnPage";
import { getGlobalSetting } from "~/components/settings/utils/accessors";
import {
GLOBAL_KEYS,
SUGGESTIVE_MODE_KEYS,
} from "~/components/settings/utils/settingKeys";
type ApiEmbeddingResponse = {
data: Array<{
embedding: number[];
}>;
};
type ApiSupabaseResultItem = {
roam_uid: string;
text_content: string;
similarity: number;
};
export type EmbeddingVectorType = number[];
export type CandidateNodeWithEmbedding = Result & {
type: string;
};
export type SuggestedNode = Result & {
type: string;
};
export type RelationDetails = {
relationLabel: string;
relatedNodeText: string;
relatedNodeFormat: string;
};
export type NodeSearchResult = {
object: { uid: string; text: string };
score: number;
};
type ResultItemMin = { uid?: string };
export type ExistingResultGroup = {
label: string;
results: Record<string, ResultItemMin>;
};
type HypotheticalNodeGenerator = (params: {
node: string;
relationType: RelationDetails;
}) => Promise<string>;
type EmbeddingFunc = (text: string) => Promise<EmbeddingVectorType>;
type SearchFunc = (params: {
queryEmbedding: EmbeddingVectorType;
indexData: Result[];
}) => Promise<NodeSearchResult[]>;
const API_CONFIG = {
LLM: {
URL: `${nextApiRoot()}/llm/openai/chat`,
MODEL: "gpt-4.1",
TIMEOUT_MS: 30_000,
MAX_TOKENS: 104,
TEMPERATURE: 0.9,
},
EMBEDDINGS_URL: `${nextApiRoot()}/embeddings/openai/small`,
} as const;
const handleApiError = async (
response: Response,
context: string,
): Promise<never> => {
const errorText = await response.text();
let errorData: unknown;
try {
errorData = JSON.parse(errorText);
} catch (e) {
errorData = { error: `Server responded with ${response.status}` };
}
console.error(
`${context} failed with status ${response.status}. Error:`,
errorData,
);
throw new Error(
`${context} failed with status ${response.status}. Response: ${errorText}`,
);
};
const generateHypotheticalNode: HypotheticalNodeGenerator = async ({
node,
relationType,
}) => {
const { relationLabel, relatedNodeText, relatedNodeFormat } = relationType;
const userPromptContent = `Given the source discourse node \`\`\`${node}\`\`\`,
and considering the relation \`\`\`${relationLabel}\`\`\`
which typically connects to a node of type \`\`\`${relatedNodeText}\`\`\`
(formatted like \`\`\`${relatedNodeFormat}\`\`\`),
generate a hypothetical related discourse node text that would plausibly fit this relationship.
Only return the text of the hypothetical node.`;
const requestBody = {
documents: [{ role: "user", content: userPromptContent }],
passphrase: "",
settings: {
model: API_CONFIG.LLM.MODEL,
maxTokens: API_CONFIG.LLM.MAX_TOKENS,
temperature: API_CONFIG.LLM.TEMPERATURE,
},
};
let response: Response | null = null;
try {
const signal = AbortSignal.timeout(API_CONFIG.LLM.TIMEOUT_MS);
response = await fetch(API_CONFIG.LLM.URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody),
signal,
});
if (!response.ok) {
await handleApiError(response, "Hypothetical node generation");
}
return await response.text();
} catch (error: unknown) {
if (
error instanceof Error &&
(error.name === "AbortError" || error.name === "TimeoutError")
) {
console.error("Hypothetical node generation timed out", error);
return `Error: Failed to generate hypothetical node. Request timed out.`;
}
console.error("Hypothetical node generation failed:", error);
return `Error: Failed to generate hypothetical node. ${
error instanceof Error ? error.message : String(error)
}`;
}
};
const createEmbedding: EmbeddingFunc = async (
text: string,
): Promise<EmbeddingVectorType> => {
if (!text.trim()) throw new Error("Input text for embedding is empty.");
try {
const response = await fetch(API_CONFIG.EMBEDDINGS_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ input: text }),
});
if (!response.ok) {
await handleApiError(response, "Embedding creation");
}
const data = (await response.json()) as ApiEmbeddingResponse;
if (!data?.data?.[0]?.embedding) {
throw new Error("Invalid API response format from embedding service.");
}
return data.data[0].embedding;
} catch (error: unknown) {
console.error("Error creating embedding:", error);
throw error;
}
};
const searchEmbeddings: SearchFunc = async ({
queryEmbedding,
indexData,
}): Promise<NodeSearchResult[]> => {
if (!indexData?.length) {
return [];
}
const supabaseClient = await getLoggedInClient();
if (!supabaseClient) return [];
const subsetRoamUids = indexData.map((node) => node.uid);
const { data, error } = await supabaseClient.rpc(
"match_embeddings_for_subset_nodes",
{
p_query_embedding: JSON.stringify(queryEmbedding),
p_subset_roam_uids: subsetRoamUids,
},
);
if (error) {
console.error("Embedding search failed:", error);
throw new Error("Embedding search failed");
}
const results = data;
if (!Array.isArray(results)) {
console.error("Embedding search response was not an array:", results);
throw new Error("Invalid API response format: Expected an array.");
}
const mappedResults = results.map((item: ApiSupabaseResultItem) => ({
object: { uid: item.roam_uid, text: item.text_content },
score: item.similarity,
}));
return mappedResults;
};
const searchAgainstCandidates = async ({
hypotheticalTexts,
indexData,
}: {
hypotheticalTexts: string[];
indexData: CandidateNodeWithEmbedding[];
}): Promise<NodeSearchResult[][]> => {
if (!hypotheticalTexts?.length || !indexData?.length) {
return [];
}
const results = await Promise.all(
hypotheticalTexts.map(async (hypoText) => {
try {
const queryEmbedding = await createEmbedding(hypoText);
return await searchEmbeddings({
queryEmbedding,
indexData,
});
} catch (error: unknown) {
let errorMessage = `Search failed for hypothetical text "${hypoText}".`;
let errorStack;
if (error instanceof Error) {
errorMessage += ` Message: ${error.message}`;
errorStack = error.stack;
} else {
errorMessage += ` Error: ${String(error)}`;
}
console.error(
`Search exception:`,
errorMessage,
errorStack && `Stack: ${errorStack}`,
);
return [];
}
}),
);
return results;
};
const combineScores = (
allSearchResults: NodeSearchResult[][],
): Map<string, number> => {
const maxScores = new Map<string, number>();
allSearchResults.forEach((resultSet) => {
resultSet.forEach((result) => {
const currentMax = maxScores.get(result.object.uid) ?? -Infinity;
if (result.score > currentMax) {
maxScores.set(result.object.uid, result.score);
}
});
});
return maxScores;
};
const rankNodes = ({
maxScores,
candidateNodes,
}: {
maxScores: Map<string, number>;
candidateNodes: CandidateNodeWithEmbedding[];
}): SuggestedNode[] => {
if (!candidateNodes?.length) {
return [];
}
const nodeMap = new Map<string, CandidateNodeWithEmbedding>(
candidateNodes.map((node) => [node.uid, node]),
);
const combinedResults: { node: SuggestedNode; score: number }[] = [];
maxScores.forEach((score, uid) => {
const fullNode = nodeMap.get(uid);
if (fullNode) {
combinedResults.push({ node: fullNode, score });
}
});
combinedResults.sort((a, b) => b.score - a.score);
return combinedResults.map((item) => item.node);
};
export const findSimilarNodesUsingHyde = async ({
candidateNodes,
currentNodeText,
relationDetails,
}: {
candidateNodes: CandidateNodeWithEmbedding[];
currentNodeText: string;
relationDetails: RelationDetails[];
}): Promise<SuggestedNode[]> => {
if (
!candidateNodes?.length ||
!currentNodeText?.trim() ||
!relationDetails?.length
) {
return [];
}
try {
const hypotheticalTexts = (
await Promise.all(
relationDetails.map((relationType) =>
generateHypotheticalNode({ node: currentNodeText, relationType }),
),
)
).filter((text) => !text.startsWith("Error:"));
if (!hypotheticalTexts.length) {
console.warn("No valid hypothetical nodes were generated. Exiting.");
return [];
}
const allSearchResults = await searchAgainstCandidates({
hypotheticalTexts,
indexData: candidateNodes,
});
const maxScores = combineScores(allSearchResults);
return rankNodes({ maxScores, candidateNodes });
} catch (error: unknown) {
let errorMessage = "Failed to find similar nodes.";
let errorStack;
if (error instanceof Error) {
errorMessage += ` Message: ${error.message}`;
errorStack = error.stack;
} else {
errorMessage += ` Error: ${String(error)}`;
}
console.error(
"Similar nodes search exception:",
errorMessage,
errorStack && `Stack: ${errorStack}`,
);
return [];
}
};
export const getAllPageByUidAsync = async (): Promise<[string, string][]> => {
return (await window.roamAlphaAPI.data.backend.q(
"[:find ?pageName ?pageUid :where [?e :node/title ?pageName] [?e :block/uid ?pageUid]]",
)) as [string, string][];
};
export const extractPagesFromChildBlock = async (
tag: string,
): Promise<{ uid: string; text: string }[]> => {
const results = (await window.roamAlphaAPI.data.backend.q(
`[:find ?uid ?title
:where [?b :node/title "${normalizePageTitle(tag)}"]
[?a :block/refs ?b]
[?p :block/children ?a]
[?p :block/refs ?rf]
[?rf :block/uid ?uid]
[?rf :node/title ?title]]]`,
)) as Array<[string, string]>;
return results.map(([uid, title]) => ({ uid, text: title }));
};
export const extractPagesFromParentBlock = async (
tag: string,
): Promise<{ uid: string; text: string }[]> => {
const results = (await window.roamAlphaAPI.data.backend.q(
`[:find ?uid ?title
:where [?b :node/title "${normalizePageTitle(tag)}"]
[?a :block/refs ?b]
[?p :block/parents ?a]
[?p :block/refs ?rf]
[?rf :block/uid ?uid]
[?rf :node/title ?title]]]`,
)) as Array<[string, string]>;
return results.map(([uid, title]) => ({ uid, text: title }));
};
export type PerformHydeSearchParams = {
useAllPagesForSuggestions: boolean;
selectedPages: string[];
discourseNode: false | DiscourseNode;
blockUid: string;
validTypes: string[];
existingResults: ExistingResultGroup[];
uniqueRelationTypeTriplets: RelationDetails[];
pageTitle: string;
};
export const performHydeSearch = async ({
useAllPagesForSuggestions,
selectedPages,
discourseNode,
blockUid,
validTypes,
existingResults,
uniqueRelationTypeTriplets,
pageTitle,
}: PerformHydeSearchParams): Promise<SuggestedNode[]> => {
if (!useAllPagesForSuggestions && selectedPages.length === 0) {
return [];
}
if (!discourseNode) {
return [];
}
const shouldGrabFromReferencedPages =
getGlobalSetting<boolean>([
GLOBAL_KEYS.suggestiveMode,
SUGGESTIVE_MODE_KEYS.includeCurrentPageRelations,
]) ?? true;
const shouldGrabParentChildContext =
getGlobalSetting<boolean>([
GLOBAL_KEYS.suggestiveMode,
SUGGESTIVE_MODE_KEYS.includeParentAndChildBlocks,
]) ?? true;
let candidateNodesForHyde: SuggestedNode[] = [];
const existingUids = new Set<string>(
existingResults
.flatMap((group) => Object.values(group.results).map((item) => item.uid))
.filter((uid): uid is string => !!uid),
);
if (useAllPagesForSuggestions) {
const context = await getSupabaseContext();
if (!context) return [];
const supabase = await getLoggedInClient();
const spaceId = context.spaceId;
if (!supabase) return [];
candidateNodesForHyde = (
await getNodesByType({
supabase,
spaceId,
fields: { concepts: ["source_local_id", "name"], content: [] },
ofTypes: validTypes,
pagination: { limit: 1000 },
})
)
.map((c) => {
const node = findDiscourseNode({
uid: c.source_local_id || "",
});
return {
uid: c.source_local_id || "",
text: c.name || "",
type: node ? node.type : "",
};
})
.filter((n) => n.uid && n.text && n.type);
} else {
const referenced: { uid: string; text: string }[] = [];
if (shouldGrabFromReferencedPages) {
referenced.push(...(await getAllReferencesOnPage(pageTitle)));
for (const p of selectedPages) {
referenced.push(...(await getAllReferencesOnPage(p)));
}
}
if (shouldGrabParentChildContext) {
referenced.push(...(await extractPagesFromChildBlock(pageTitle)));
referenced.push(...(await extractPagesFromParentBlock(pageTitle)));
for (const p of selectedPages) {
referenced.push(...(await extractPagesFromChildBlock(p)));
referenced.push(...(await extractPagesFromParentBlock(p)));
}
}
const uniqueReferenced = Array.from(
new Map(referenced.map((x) => [x.uid, x])).values(),
);
candidateNodesForHyde = uniqueReferenced
.map((n) => {
const node = findDiscourseNode({ uid: n.uid });
if (
!node ||
node.backedBy === "default" ||
!validTypes.includes(node.type) ||
existingUids.has(n.uid) ||
n.uid === blockUid
) {
return null;
}
return {
uid: n.uid,
text: n.text,
type: node.type,
} as SuggestedNode;
})
.filter((n): n is SuggestedNode => n !== null);
}
if (candidateNodesForHyde.length && uniqueRelationTypeTriplets.length) {
const found = await findSimilarNodesUsingHyde({
candidateNodes: candidateNodesForHyde,
currentNodeText: pageTitle,
relationDetails: uniqueRelationTypeTriplets,
});
return found;
}
return [];
};
export type VectorMatch = {
node: Result;
score: number;
};
export const findSimilarNodesVectorOnly = async ({
text,
threshold = 0.4,
limit = 15,
}: {
text: string;
threshold?: number;
limit?: number;
}): Promise<VectorMatch[]> => {
if (!text.trim()) {
return [];
}
try {
const supabase = await getLoggedInClient();
if (!supabase) return [];
const queryEmbedding = await createEmbedding(text);
const { data, error } = await supabase
.rpc("match_content_embeddings", {
query_embedding: JSON.stringify(queryEmbedding),
match_threshold: threshold,
})
.limit(limit);
if (error) {
console.error("Vector search failed:", error);
throw error;
}
if (!data || !Array.isArray(data)) return [];
const results: VectorMatch[] = data.map((item) => ({
node: {
uid: item.roam_uid,
text: item.text_content,
},
score: item.similarity,
}));
return results;
} catch (error) {
console.error("Error in vector-only similar nodes search:", error);
renderToast({
content: `Error in vector-only similar nodes search: ${
error instanceof Error ? error.message : String(error)
}`,
intent: "danger",
id: "vector-search-error",
});
return [];
}
};