Skip to content

Commit 6939552

Browse files
authored
feat(ai-help): index short_title (#10579)
1 parent 8c988be commit 6939552

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

Diff for: scripts/ai-help-macros.ts

+43-18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ interface IndexedDoc {
2828
id: number;
2929
mdn_url: string;
3030
title: string;
31+
title_short: string;
3132
token_count: number | null;
3233
hash: string;
3334
text_hash: string;
@@ -36,6 +37,7 @@ interface IndexedDoc {
3637
interface Doc {
3738
mdn_url: string;
3839
title: string;
40+
title_short: string;
3941
hash: string;
4042
html: string;
4143
markdown: string;
@@ -108,9 +110,15 @@ export async function updateEmbeddings(
108110
const updates: Doc[] = [];
109111
const formattingUpdates: Doc[] = [];
110112

111-
for await (const { mdn_url, title, hash, html, markdown, text } of builtDocs(
112-
directory
113-
)) {
113+
for await (const {
114+
mdn_url,
115+
title,
116+
title_short,
117+
hash,
118+
html,
119+
markdown,
120+
text,
121+
} of builtDocs(directory)) {
114122
seenUrls.add(mdn_url);
115123

116124
// Check for existing document in DB and compare checksums.
@@ -122,6 +130,7 @@ export async function updateEmbeddings(
122130
updates.push({
123131
mdn_url,
124132
title,
133+
title_short,
125134
hash,
126135
html,
127136
markdown,
@@ -132,6 +141,7 @@ export async function updateEmbeddings(
132141
formattingUpdates.push({
133142
mdn_url,
134143
title,
144+
title_short,
135145
hash,
136146
html,
137147
markdown,
@@ -154,6 +164,7 @@ export async function updateEmbeddings(
154164
for (const {
155165
mdn_url,
156166
title,
167+
title_short,
157168
hash,
158169
html,
159170
markdown,
@@ -173,27 +184,30 @@ export async function updateEmbeddings(
173184
INSERT INTO mdn_doc_macro(
174185
mdn_url,
175186
title,
187+
title_short,
176188
hash,
177189
html,
178190
markdown,
179191
token_count,
180192
embedding,
181193
text_hash
182194
)
183-
VALUES($1, $2, $3, $4, $5, $6, $7, $8) ON CONFLICT (mdn_url) DO
195+
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9) ON CONFLICT (mdn_url) DO
184196
UPDATE
185197
SET mdn_url = $1,
186198
title = $2,
187-
hash = $3,
188-
html = $4,
189-
markdown = $5,
190-
token_count = $6,
191-
embedding = $7,
192-
text_hash = $8
199+
title_short = $3,
200+
hash = $4,
201+
html = $5,
202+
markdown = $6,
203+
token_count = $7,
204+
embedding = $8,
205+
text_hash = $9
193206
`,
194207
values: [
195208
mdn_url,
196209
title,
210+
title_short,
197211
hash,
198212
html,
199213
markdown,
@@ -211,7 +225,14 @@ export async function updateEmbeddings(
211225
console.error(context);
212226
}
213227
}
214-
for (const { mdn_url, title, hash, html, markdown } of formattingUpdates) {
228+
for (const {
229+
mdn_url,
230+
title,
231+
title_short,
232+
hash,
233+
html,
234+
markdown,
235+
} of formattingUpdates) {
215236
try {
216237
console.log(
217238
`-> [${mdn_url}] Updating document without generating new embedding...`
@@ -221,16 +242,17 @@ export async function updateEmbeddings(
221242
const query = {
222243
name: "upsert-doc",
223244
text: `
224-
INSERT INTO mdn_doc_macro(mdn_url, title, hash, html, markdown)
225-
VALUES($1, $2, $3, $4, $5) ON CONFLICT (mdn_url) DO
245+
INSERT INTO mdn_doc_macro(mdn_url, title, title_short, hash, html, markdown)
246+
VALUES($1, $2, $3, $4, $5, $6) ON CONFLICT (mdn_url) DO
226247
UPDATE
227248
SET mdn_url = $1,
228249
title = $2,
229-
hash = $3,
230-
html = $4,
231-
markdown = $5
250+
title_short = $3,
251+
hash = $4,
252+
html = $5,
253+
markdown = $6
232254
`,
233-
values: [mdn_url, title, hash, html, markdown],
255+
values: [mdn_url, title, title_short, hash, html, markdown],
234256
rowMode: "array",
235257
};
236258

@@ -286,7 +308,9 @@ async function* builtDocs(directory: string) {
286308
for await (const metadataPath of builtPaths(directory)) {
287309
try {
288310
const raw = await readFile(metadataPath, "utf-8");
289-
const { title, mdn_url, hash } = JSON.parse(raw) as DocMetadata;
311+
const { title, short_title, mdn_url, hash } = JSON.parse(
312+
raw
313+
) as DocMetadata;
290314

291315
const plainPath = path.join(path.dirname(metadataPath), "plain.html");
292316
const plainHTML = await readFile(plainPath, "utf-8");
@@ -314,6 +338,7 @@ async function* builtDocs(directory: string) {
314338
yield {
315339
mdn_url,
316340
title,
341+
title_short: short_title || title,
317342
hash,
318343
html,
319344
markdown,

Diff for: scripts/ai-help.sql

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ create table
2929
id bigserial,
3030
hash text null,
3131
title text not null,
32+
title_short text not null,
3233
mdn_url text not null,
3334
html text null,
3435
markdown text null,

0 commit comments

Comments
 (0)