Merge pull request #5946 from continuedev/dallin/broken-prompts
Prompts fixes
This commit is contained in:
commit
6d646ad3e4
10 changed files with 239 additions and 71 deletions
|
|
@ -7,6 +7,7 @@ import {
|
|||
ContextItem,
|
||||
ContextProviderDescription,
|
||||
ContextProviderExtras,
|
||||
FetchFunction,
|
||||
} from "../../index.js";
|
||||
import { fetchFavicon } from "../../util/fetchFavicon";
|
||||
|
||||
|
|
@ -22,42 +23,49 @@ class URLContextProvider extends BaseContextProvider {
|
|||
query: string,
|
||||
extras: ContextProviderExtras,
|
||||
): Promise<ContextItem[]> {
|
||||
try {
|
||||
const url = new URL(query);
|
||||
const icon = await fetchFavicon(url);
|
||||
const resp = await extras.fetch(url);
|
||||
const html = await resp.text();
|
||||
|
||||
const dom = new JSDOM(html);
|
||||
let reader = new Readability(dom.window.document);
|
||||
let article = reader.parse();
|
||||
const content = article?.content || "";
|
||||
const markdown = NodeHtmlMarkdown.translate(
|
||||
content,
|
||||
{},
|
||||
undefined,
|
||||
undefined,
|
||||
);
|
||||
|
||||
const title = article?.title || url.pathname;
|
||||
|
||||
return [
|
||||
{
|
||||
icon,
|
||||
description: url.toString(),
|
||||
content: markdown,
|
||||
name: title,
|
||||
uri: {
|
||||
type: "url",
|
||||
value: url.toString(),
|
||||
},
|
||||
},
|
||||
];
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
return [];
|
||||
}
|
||||
return await getUrlContextItems(query, extras.fetch);
|
||||
}
|
||||
}
|
||||
|
||||
export default URLContextProvider;
|
||||
|
||||
export async function getUrlContextItems(
|
||||
query: string,
|
||||
fetchFn: FetchFunction,
|
||||
): Promise<ContextItem[]> {
|
||||
try {
|
||||
const url = new URL(query);
|
||||
const icon = await fetchFavicon(url);
|
||||
const resp = await fetchFn(url);
|
||||
const html = await resp.text();
|
||||
|
||||
const dom = new JSDOM(html);
|
||||
let reader = new Readability(dom.window.document);
|
||||
let article = reader.parse();
|
||||
const content = article?.content || "";
|
||||
const markdown = NodeHtmlMarkdown.translate(
|
||||
content,
|
||||
{},
|
||||
undefined,
|
||||
undefined,
|
||||
);
|
||||
|
||||
const title = article?.title || url.pathname;
|
||||
|
||||
return [
|
||||
{
|
||||
icon,
|
||||
description: url.toString(),
|
||||
content: markdown,
|
||||
name: title,
|
||||
uri: {
|
||||
type: "url",
|
||||
value: url.toString(),
|
||||
},
|
||||
},
|
||||
];
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,13 +13,6 @@ export function extractName(preamble: { name?: string }, path: string): string {
|
|||
return preamble.name ?? getLastNPathParts(path, 1).split(".prompt")[0];
|
||||
}
|
||||
|
||||
export function extractUserInput(input: string, commandName: string): string {
|
||||
if (input.startsWith(`/${commandName}`)) {
|
||||
return input.slice(commandName.length + 1).trimStart();
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
async function renderPromptV1(
|
||||
prompt: string,
|
||||
context: ContinueSDK,
|
||||
|
|
@ -66,10 +59,9 @@ export function slashCommandFromPromptFileV1(
|
|||
prompt,
|
||||
promptFile: path,
|
||||
run: async function* (context) {
|
||||
const userInput = extractUserInput(context.input, name);
|
||||
const [_, renderedPrompt] = await renderPromptFileV2(prompt, {
|
||||
config: context.config,
|
||||
fullInput: userInput,
|
||||
fullInput: context.input,
|
||||
embeddingsProvider: context.config.modelsByRole.embed[0],
|
||||
reranker: context.config.modelsByRole.rerank[0],
|
||||
llm: context.llm,
|
||||
|
|
@ -85,18 +77,6 @@ export function slashCommandFromPromptFileV1(
|
|||
systemMessage,
|
||||
);
|
||||
|
||||
if (systemMessage) {
|
||||
const currentSystemMsg = messages.find((msg) => msg.role === "system");
|
||||
if (currentSystemMsg) {
|
||||
currentSystemMsg.content = systemMessage;
|
||||
} else {
|
||||
messages.unshift({
|
||||
role: "system",
|
||||
content: systemMessage,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for await (const chunk of context.llm.streamChat(
|
||||
messages,
|
||||
new AbortController().signal,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { ContextItem, ContextProviderExtras } from "../..";
|
||||
import { contextProviderClassFromName } from "../../context/providers";
|
||||
import URLContextProvider from "../../context/providers/URLContextProvider";
|
||||
import { getUrlContextItems } from "../../context/providers/URLContextProvider";
|
||||
import { resolveRelativePathInDir } from "../../util/ideUtils";
|
||||
import { getUriPathBasename } from "../../util/uri";
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ async function resolveAttachment(
|
|||
if (name.endsWith(".prompt")) {
|
||||
// Recurse
|
||||
const [items, _] = await renderPromptFileV2(
|
||||
await extras.ide.readFile(name),
|
||||
await extras.ide.readFile(resolvedFileUri),
|
||||
extras,
|
||||
);
|
||||
subItems.push(...items);
|
||||
|
|
@ -52,10 +52,7 @@ async function resolveAttachment(
|
|||
|
||||
// URLs
|
||||
if (name.startsWith("http")) {
|
||||
const items = await new URLContextProvider({}).getContextItems(
|
||||
name,
|
||||
extras,
|
||||
);
|
||||
const items = getUrlContextItems(name, extras.fetch);
|
||||
return items;
|
||||
}
|
||||
|
||||
|
|
@ -75,10 +72,13 @@ export async function renderPromptFileV2(
|
|||
});
|
||||
|
||||
const contextItems = (await Promise.all(contextItemsPromises)).flat();
|
||||
const renderedPrompt =
|
||||
contextItems.map((item) => item.content).join("\n\n") +
|
||||
"\n\n" +
|
||||
renderedBody;
|
||||
const renderedPrompt = [
|
||||
...contextItems.map((item) => item.content),
|
||||
renderedBody,
|
||||
extras.fullInput,
|
||||
]
|
||||
.join("\n\n")
|
||||
.trim();
|
||||
|
||||
return [contextItems, renderedPrompt];
|
||||
}
|
||||
|
|
|
|||
175
core/promptFiles/v2/renderPromptFile.vitest.ts
Normal file
175
core/promptFiles/v2/renderPromptFile.vitest.ts
Normal file
|
|
@ -0,0 +1,175 @@
|
|||
import { ContextProviderExtras, ILLM, RangeInFile } from "../..";
|
||||
import { testConfigHandler, testIde } from "../../test/fixtures";
|
||||
import {
|
||||
addToTestDir,
|
||||
setUpTestDir,
|
||||
tearDownTestDir,
|
||||
} from "../../test/testDir";
|
||||
import { renderPromptFileV2 } from "./renderPromptFile";
|
||||
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock(
|
||||
import("../../context/providers/URLContextProvider"),
|
||||
async (importOriginal) => {
|
||||
const actual = await importOriginal();
|
||||
return {
|
||||
...actual,
|
||||
getUrlContextItems: vi.fn().mockResolvedValue([
|
||||
{
|
||||
description: "https://example.com",
|
||||
content: "Example Content",
|
||||
name: "Example site",
|
||||
uri: {
|
||||
type: "url",
|
||||
value: "https://example.com",
|
||||
},
|
||||
},
|
||||
]),
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
describe("renderPromptFileV2", () => {
|
||||
let extras: ContextProviderExtras;
|
||||
const mockGetContextItems = vi.fn().mockResolvedValue([
|
||||
{
|
||||
name: "custom",
|
||||
content: "Custom provider content",
|
||||
description: "Custom provider",
|
||||
},
|
||||
]);
|
||||
beforeEach(async () => {
|
||||
const { config } = await testConfigHandler.loadConfig();
|
||||
|
||||
// Setup mock extras
|
||||
extras = {
|
||||
config: {
|
||||
...config!,
|
||||
contextProviders: [
|
||||
{
|
||||
description: {
|
||||
type: "normal",
|
||||
title: "CustomProvider",
|
||||
displayTitle: "Custom Provider",
|
||||
description: "A custom context provider",
|
||||
},
|
||||
loadSubmenuItems: vi.fn().mockResolvedValue([]),
|
||||
getContextItems: mockGetContextItems,
|
||||
},
|
||||
],
|
||||
},
|
||||
fullInput: "User input",
|
||||
embeddingsProvider: null,
|
||||
reranker: null,
|
||||
llm: {} as ILLM,
|
||||
ide: testIde,
|
||||
selectedCode: [] as RangeInFile[],
|
||||
fetch: vi.fn(),
|
||||
};
|
||||
|
||||
// Clear all mocks
|
||||
vi.clearAllMocks();
|
||||
setUpTestDir();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
tearDownTestDir();
|
||||
});
|
||||
|
||||
it("should render a prompt file with no attachments", async () => {
|
||||
const rawContent = "Simple prompt with no attachments";
|
||||
const [contextItems, renderedPrompt] = await renderPromptFileV2(
|
||||
rawContent,
|
||||
extras,
|
||||
);
|
||||
|
||||
expect(contextItems).toHaveLength(0);
|
||||
expect(renderedPrompt).toBe(
|
||||
"Simple prompt with no attachments\n\nUser input",
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle URL attachments", async () => {
|
||||
const rawContent = "Content with URL @https://example.com";
|
||||
|
||||
const [contextItems, renderedPrompt] = await renderPromptFileV2(
|
||||
rawContent,
|
||||
extras,
|
||||
);
|
||||
|
||||
expect(contextItems).toHaveLength(1);
|
||||
expect(renderedPrompt).toContain("Example Content");
|
||||
});
|
||||
|
||||
it("should resolve file attachments", async () => {
|
||||
const rawContent = "Content with file attachment @test.txt";
|
||||
|
||||
addToTestDir([["test.txt", "File content"]]);
|
||||
|
||||
const [contextItems, renderedPrompt] = await renderPromptFileV2(
|
||||
rawContent,
|
||||
extras,
|
||||
);
|
||||
|
||||
expect(contextItems).toHaveLength(1);
|
||||
expect(renderedPrompt).toContain("```test.txt\nFile content\n```");
|
||||
expect(renderedPrompt).toContain("Content with file attachment @test.txt");
|
||||
});
|
||||
|
||||
it("should handle nested prompt files", async () => {
|
||||
const rawContent = "Main prompt with nested @nested.prompt";
|
||||
addToTestDir([
|
||||
["nested.prompt", "Nested prompt content with @CustomProvider"],
|
||||
]);
|
||||
|
||||
const [contextItems, renderedPrompt] = await renderPromptFileV2(
|
||||
rawContent,
|
||||
extras,
|
||||
);
|
||||
|
||||
expect(renderedPrompt).toContain("Custom provider content");
|
||||
});
|
||||
|
||||
it("should handle custom context providers", async () => {
|
||||
const rawContent = "Content with custom provider @CustomProvider";
|
||||
|
||||
const [contextItems, renderedPrompt] = await renderPromptFileV2(
|
||||
rawContent,
|
||||
extras,
|
||||
);
|
||||
|
||||
expect(contextItems).toHaveLength(1);
|
||||
expect(renderedPrompt).toContain("Custom provider content");
|
||||
expect(mockGetContextItems).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should handle multiple attachments", async () => {
|
||||
const rawContent = "Content with @file1.txt and @file2.txt";
|
||||
addToTestDir([
|
||||
["file1.txt", "Content 1"],
|
||||
["file2.txt", "Content 2"],
|
||||
]);
|
||||
|
||||
const [contextItems, renderedPrompt] = await renderPromptFileV2(
|
||||
rawContent,
|
||||
extras,
|
||||
);
|
||||
|
||||
expect(contextItems).toHaveLength(2);
|
||||
expect(renderedPrompt).toContain("```file1.txt\nContent 1\n```");
|
||||
expect(renderedPrompt).toContain("```file2.txt\nContent 2\n```");
|
||||
});
|
||||
|
||||
it("should handle failed attachment resolutions gracefully", async () => {
|
||||
const rawContent = "Content with @nonexistent";
|
||||
|
||||
const [contextItems, renderedPrompt] = await renderPromptFileV2(
|
||||
rawContent,
|
||||
extras,
|
||||
);
|
||||
|
||||
expect(contextItems).toHaveLength(0);
|
||||
expect(renderedPrompt).toBe("Content with @nonexistent\n\nUser input");
|
||||
});
|
||||
});
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
import { defineConfig } from "vitest/config";
|
||||
|
||||
export default defineConfig({
|
||||
test: {},
|
||||
test: {
|
||||
include: ["**/*.vitest.ts"],
|
||||
},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -168,7 +168,7 @@ export function ExpandableToolbarPreview(props: ExpandableToolbarPreviewProps) {
|
|||
}}
|
||||
>
|
||||
<div
|
||||
className="flex items-center gap-1 text-[11px] hover:underline"
|
||||
className={`flex items-center gap-1 text-[11px] ${!!props.onTitleClick ? "hover:underline" : ""}`}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
if (props.onTitleClick) {
|
||||
|
|
|
|||
|
|
@ -89,6 +89,7 @@ export const PromptBlock = Node.create<PromptBlockOptions>({
|
|||
};
|
||||
|
||||
return chain()
|
||||
.clearPrompt()
|
||||
.insertContentAt(0, {
|
||||
type: this.name,
|
||||
attrs: {
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ export const PromptBlockPreview = ({
|
|||
inputId={inputId}
|
||||
itemId={item.id.itemId}
|
||||
onDelete={handleDelete}
|
||||
onTitleClick={handleTitleClick}
|
||||
onTitleClick={!item.content ? undefined : handleTitleClick}
|
||||
>
|
||||
{!item.content ? null : (
|
||||
<div
|
||||
|
|
|
|||
|
|
@ -198,6 +198,8 @@ function processSlashCommand(
|
|||
const lastTextIndex = findLastIndex(parts, (part) => part.type === "text");
|
||||
const lastTextPart = parts[lastTextIndex] as TextMessagePart;
|
||||
|
||||
const originalPrompt = command.prompt ? " " + command.prompt : "";
|
||||
|
||||
let input: string;
|
||||
|
||||
/**
|
||||
|
|
@ -211,10 +213,10 @@ function processSlashCommand(
|
|||
role: "user",
|
||||
content: lastTextPart.text,
|
||||
}).trimStart();
|
||||
lastTextPart.text = `/${command.name} ${lastTextPart.text}`;
|
||||
lastTextPart.text = `/${command.name}${originalPrompt} ${lastTextPart.text}`;
|
||||
} else {
|
||||
input = "";
|
||||
parts.push({ type: "text", text: `/${command.name}` });
|
||||
parts.push({ type: "text", text: `/${command.name}${originalPrompt}` });
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue