-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vscode): feed docs into copilot integration (RAG) (#2404)
- Loading branch information
Showing
8 changed files
with
209 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { readNxJson } from '@nx-console/shared-npm'; | ||
import { GeneratorCollectionInfo } from '@nx-console/shared-schema'; | ||
import { withTimeout } from '@nx-console/shared-utils'; | ||
import { getGenerators, getNxWorkspace } from '@nx-console/vscode-nx-workspace'; | ||
import { getOutputChannel } from '@nx-console/vscode-output-channels'; | ||
import type { NxJsonConfiguration, ProjectGraph } from 'nx/src/devkit-exports'; | ||
import { xhr } from 'request-light'; | ||
import { | ||
ChatRequestTurn, | ||
ChatResponseStream, | ||
ChatResponseTurn, | ||
MarkdownString, | ||
} from 'vscode'; | ||
import { chatResponseToString } from './prompts/history'; | ||
|
||
export async function getProjectGraph( | ||
stream: ChatResponseStream, | ||
): Promise<ProjectGraph | undefined> { | ||
let projectGraph: ProjectGraph | undefined; | ||
try { | ||
await withTimeout<void>(async () => { | ||
const workspace = await getNxWorkspace(); | ||
projectGraph = workspace?.projectGraph; | ||
}, 10000); | ||
} catch (e) { | ||
projectGraph = undefined; | ||
} | ||
if ( | ||
projectGraph === undefined || | ||
Object.keys(projectGraph.nodes).length === 0 | ||
) { | ||
const md = new MarkdownString(); | ||
md.supportThemeIcons = true; | ||
md.appendMarkdown( | ||
'$(warning) Unable to retrieve workspace information. Proceeding without workspace data. ', | ||
); | ||
stream.markdown(md); | ||
} | ||
return projectGraph; | ||
} | ||
|
||
export async function getGeneratorNamesAndDescriptions(): Promise< | ||
{ | ||
name: string; | ||
description: string; | ||
}[] | ||
> { | ||
let generators: GeneratorCollectionInfo[]; | ||
try { | ||
await withTimeout<void>(async () => { | ||
generators = await getGenerators(); | ||
}, 3000); | ||
} catch (e) { | ||
generators = []; | ||
} | ||
|
||
return generators.map((generator) => ({ | ||
name: generator.name, | ||
description: generator.data.description, | ||
})); | ||
} | ||
|
||
export async function tryReadNxJson( | ||
workspacePath: string, | ||
): Promise<NxJsonConfiguration | undefined> { | ||
try { | ||
return await readNxJson(workspacePath); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
} | ||
|
||
export type DocsPageSection = { | ||
heading: string; | ||
longer_heading: string; | ||
content: string; | ||
similarity: number; | ||
}; | ||
|
||
export async function getDocsContext( | ||
prompt: string, | ||
history: ReadonlyArray<ChatRequestTurn | ChatResponseTurn>, | ||
): Promise<DocsPageSection[]> { | ||
try { | ||
const messages = history.map((chatItem) => ({ | ||
role: chatItem instanceof ChatRequestTurn ? 'user' : 'assistant', | ||
content: | ||
chatItem instanceof ChatRequestTurn | ||
? chatItem.prompt | ||
: chatResponseToString(chatItem), | ||
})); | ||
messages.push({ | ||
role: 'user', | ||
content: prompt, | ||
}); | ||
|
||
const req = await xhr({ | ||
url: 'https://nx.dev/api/query-ai-embeddings', | ||
type: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
data: JSON.stringify({ | ||
messages, | ||
}), | ||
}); | ||
|
||
const response = JSON.parse(req.responseText); | ||
return response.context.pageSections; | ||
} catch (error) { | ||
getOutputChannel().appendLine( | ||
`Error fetching AI context: ${JSON.stringify(error)}`, | ||
); | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
PromptElement, | ||
UserMessage, | ||
BasePromptElementProps, | ||
} from '@vscode/prompt-tsx'; | ||
import { DocsPageSection } from '../context'; | ||
|
||
interface DocsPagesPromptProps extends BasePromptElementProps { | ||
docsPages: DocsPageSection[]; | ||
passPriority: true; | ||
} | ||
|
||
export class DocsPagesPrompt extends PromptElement<DocsPagesPromptProps> { | ||
override render() { | ||
const pages = this.props.docsPages.slice(0, 4); | ||
return ( | ||
<> | ||
{pages.length > 0 ? ( | ||
<UserMessage priority={30}> | ||
Below are some documentation sections that could be relevant to the | ||
user request. Read through them carefully. You don't have to use | ||
them to answer the user query, but they might help. Do not assume | ||
knowledge about nx, its configuration and options. Instead, base | ||
your replies on the provided metadata and these documentation | ||
sections. <br /> | ||
<br /> | ||
{pages | ||
.map( | ||
(page, index) => | ||
`- ${page.longer_heading ?? page.heading ?? index} <br/> ${ | ||
page.content | ||
}`, | ||
) | ||
.join('<br/>')} | ||
</UserMessage> | ||
) : ( | ||
<UserMessage></UserMessage> | ||
)} | ||
</> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.