Skip to content

Commit

Permalink
feat(nx-dev): add embeddings for community plugins (#19168)
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Sep 14, 2023
1 parent 799ae13 commit fb1c39f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
13 changes: 9 additions & 4 deletions nx-dev/util-ai/src/lib/chat-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ export function getListOfSources(
return false;
})
.map((section) => {
const url = new URL('https://nx.dev');
url.pathname = section.url_partial as string;
if (section.slug) {
url.hash = section.slug;
let url: URL;
if (section.url_partial?.startsWith('https://')) {
url = new URL(section.url_partial);
} else {
url = new URL('https://nx.dev');
url.pathname = section.url_partial as string;
if (section.slug) {
url.hash = section.slug;
}
}
return {
heading: section.heading,
Expand Down
36 changes: 31 additions & 5 deletions tools/documentation/create-embeddings/src/main.mts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import manifestsExtending from '../../../../docs/generated/manifests/extending-n
import manifestsNx from '../../../../docs/generated/manifests/nx.json' assert { type: 'json' };
import manifestsPackages from '../../../../docs/generated/manifests/packages.json' assert { type: 'json' };
import manifestsTags from '../../../../docs/generated/manifests/tags.json' assert { type: 'json' };
import communityPlugins from '../../../../community/approved-plugins.json' assert { type: 'json' };

let identityMap = {};

Expand Down Expand Up @@ -125,14 +126,16 @@ class MarkdownEmbeddingSource extends BaseEmbeddingSource {
constructor(
source: string,
public filePath: string,
public url_partial: string
public url_partial: string,
public fileContent?: string
) {
const path = filePath.replace(/^docs/, '').replace(/\.md?$/, '');
super(source, path, url_partial);
}

async load() {
const contents = await readFile(this.filePath, 'utf8');
const contents =
this.fileContent ?? (await readFile(this.filePath, 'utf8'));

const { checksum, sections } = processMdxForSearch(contents);

Expand Down Expand Up @@ -209,6 +212,14 @@ async function generateEmbeddings() {
entry.url_partial
);
}),
...createMarkdownForCommunityPlugins().map((content, index) => {
return new MarkdownEmbeddingSource(
'community-plugins',
'/community/approved-plugins.json#' + index,
content.url,
content.text
);
}),
];

console.log(`Discovered ${embeddingSources.length} pages`);
Expand Down Expand Up @@ -307,7 +318,10 @@ async function generateEmbeddings() {

const [responseData] = embeddingResponse.data;

const longer_heading = createLongerHeading(heading, url_partial);
const longer_heading =
source !== 'community-plugins'
? createLongerHeading(heading, url_partial)
: heading;

const { error: insertPageSectionError, data: pageSection } =
await supabaseClient
Expand Down Expand Up @@ -446,22 +460,34 @@ function createLongerHeading(
if (url_partial?.length) {
if (heading?.length && heading !== null && heading !== 'null') {
return `${heading}${` - ${
url_partial.split('/')?.[1]?.[0].toUpperCase() +
url_partial.split('/')?.[1]?.[0]?.toUpperCase() +
url_partial.split('/')?.[1]?.slice(1)
}`}`;
} else {
return url_partial
.split('#')[0]
.split('/')
.map((part) =>
part?.length ? part[0].toUpperCase() + part.slice(1) + ' - ' : ''
part?.length ? part[0]?.toUpperCase() + part.slice(1) + ' - ' : ''
)
.join('')
.slice(0, -3);
}
}
}

function createMarkdownForCommunityPlugins(): {
text: string;
url: string;
}[] {
return communityPlugins.map((plugin) => {
return {
text: `## ${plugin.name} plugin\n\nThere is a ${plugin.name} community plugin.\n\nHere is the description for it: ${plugin.description}\n\nHere is the link to it: [${plugin.url}](${plugin.url})\n\nHere is the list of all the plugins that exist for Nx: https://nx.dev/extending-nx/registry`,
url: plugin.url,
};
});
}

async function main() {
await generateEmbeddings();
}
Expand Down

1 comment on commit fb1c39f

@vercel
Copy link

@vercel vercel bot commented on fb1c39f Sep 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-five.vercel.app
nx-dev-nrwl.vercel.app

Please sign in to comment.