render prompt file v2

This commit is contained in:
Nate 2024-11-05 23:20:33 -08:00
parent 66c97159ae
commit bfb485b693
6 changed files with 69 additions and 14 deletions

View file

@ -24,7 +24,7 @@ class PromptFilesContextProvider extends BaseContextProvider {
): Promise<ContextItem[]> {
const rawContent = await extras.ide.readFile(query);
const preamble = parsePreamble(query, rawContent);
const rendered = await renderPromptFileV2(rawContent);
const rendered = await renderPromptFileV2(rawContent, extras);
return [
{
content: rendered,

View file

@ -5,15 +5,21 @@ export function extractName(preamble: { name?: string }, path: string): string {
return preamble.name ?? getBasename(path).split(".prompt")[0];
}
export function getPreambleAndBody(content: string): [string, string] {
let [preamble, body] = content.split("\n---\n");
if (body === undefined) {
// This means there is no preamble
body = preamble;
preamble = "";
}
return [preamble, body];
}
export function parsePreamble(
path: string,
content: string,
): { [key: string]: any; name: string; description: string } {
let [preambleRaw, prompt] = content.split("\n---\n");
if (prompt === undefined) {
// This means there is no preamble
preambleRaw = "";
}
const [preambleRaw, _] = getPreambleAndBody(content);
const preamble = YAML.parse(preambleRaw) ?? {};
const name = extractName(preamble, path);

View file

@ -1,3 +1,53 @@
export async function renderPromptFileV2(rawContent: string): Promise<string> {
return rawContent;
import { ContextProviderExtras } from "../..";
import { getPreambleAndBody } from "./parse";
async function resolveAttachment(
name: string,
extras: ContextProviderExtras,
): Promise<string | null> {
// Context providers
const contextProvider = extras.config.contextProviders?.find(
(provider) => provider.description.title === name,
);
if (contextProvider) {
const items = await contextProvider.getContextItems("", extras);
return items.map((item) => item.content).join("\n\n");
}
// Files
if (await extras.ide.fileExists(name)) {
const content = await extras.ide.readFile(name);
return `\`\`\`${name}\n${content}\n\`\`\``;
}
// URLs
if (name.startsWith("http")) {
const response = await extras.fetch(name);
if (response.status !== 200) {
return `Error fetching ${name}: HTTP ${response.status}`;
}
return response.text();
}
return null;
}
export async function renderPromptFileV2(
rawContent: string,
extras: ContextProviderExtras,
): Promise<string> {
const [preamble, body] = getPreambleAndBody(rawContent);
const attachmentPromises: Promise<string | null>[] = [];
const renderedBody = body.replace(/@([^\s]+)/g, (match, name) => {
attachmentPromises.push(resolveAttachment(name, extras));
return match;
});
const attachments = (await Promise.all(attachmentPromises)).filter(Boolean);
if (!attachments.length) {
return renderedBody;
}
return attachments.join("\n\n") + "\n\n" + renderedBody;
}

View file

@ -1,12 +1,12 @@
{
"name": "continue",
"version": "0.9.225",
"version": "0.9.226",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "continue",
"version": "0.9.225",
"version": "0.9.226",
"license": "Apache-2.0",
"dependencies": {
"@electron/rebuild": "^3.2.10",

View file

@ -49,7 +49,8 @@ class VsCodeIde implements IDE {
return Promise.resolve(this.ideUtils.path.sep);
}
async fileExists(filepath: string): Promise<boolean> {
return vscode.workspace.fs.stat(uriFromFilePath(filepath)).then(
const absPath = await this.ideUtils.resolveAbsFilepathInWorkspace(filepath);
return vscode.workspace.fs.stat(uriFromFilePath(absPath)).then(
() => true,
() => false,
);

View file

@ -136,9 +136,7 @@ export class VsCodeIdeUtils {
);
}
private async resolveAbsFilepathInWorkspace(
filepath: string,
): Promise<string> {
async resolveAbsFilepathInWorkspace(filepath: string): Promise<string> {
// If the filepath is already absolute, return it as is
if (this.path.isAbsolute(filepath)) {
return filepath;