deduplicate context item outputs

This commit is contained in:
Dallin Romney 2025-06-23 19:04:45 -07:00
parent acde0b18fb
commit 7f877f262b
3 changed files with 89 additions and 46 deletions

View file

@ -1,12 +1,13 @@
import { CustomCommand, SlashCommandWithSource } from "../..";
import { Prompt } from "@continuedev/config-yaml";
import { SlashCommandWithSource } from "../..";
export function convertPromptBlockToSlashCommand(
customCommand: CustomCommand,
prompt: Prompt,
): SlashCommandWithSource {
return {
name: customCommand.name,
description: customCommand.description ?? "",
prompt: customCommand.prompt,
name: prompt.name,
description: prompt.description ?? "",
prompt: prompt.prompt,
source: "yaml-prompt-block",
};
}

View file

@ -17,15 +17,31 @@ export async function getPromptV2ContextRequests(
visitedFiles.add(command.promptFile);
}
const contextItemAttrs: GetContextRequest[] = [];
const addItem = (item: GetContextRequest) => {
contextItemAttrs.push(item);
};
const contextRequests: GetContextRequest[] = [];
async function addContextRequestsRecursive(promptBody: string) {
// Files
for (const match of promptBody.matchAll(/@([^\s]+)/g)) {
const name = match[0];
const templateMatches = promptBody.matchAll(/@([^\s]+)/g);
for (const match of templateMatches) {
const name = match[1];
if (SUPPORTED_PROMPT_CONTEXT_PROVIDERS.includes(name)) {
contextRequests.push({
provider: name,
});
continue;
}
// URLs
if (name.startsWith("http")) {
contextRequests.push({
provider: "url",
query: name,
});
continue;
}
// Files
const resolvedFileUri = await resolveRelativePathInDir(
match[0],
ideMessenger.ide,
@ -40,25 +56,13 @@ export async function getPromptV2ContextRequests(
visitedFiles.add(resolvedFileUri);
await addContextRequestsRecursive(contents);
}
return addItem({
contextRequests.push({
provider: name,
query: resolvedFileUri,
});
}
// URLs
if (name.startsWith("http")) {
return addItem({
provider: "url",
query: name,
});
}
if (SUPPORTED_PROMPT_CONTEXT_PROVIDERS.includes(name)) {
return addItem({
provider: name,
});
}
}
}
await addContextRequestsRecursive(command.prompt);
return contextItemAttrs;
return contextRequests;
}

View file

@ -234,8 +234,8 @@ async function renderSlashCommandPrompt(
}
const nonTextParts = parts.filter((part) => part.type !== "text");
const slashedParts: MessagePart[] = [...nonTextParts];
const textParts = parts.filter((part) => part.type === "text");
const slashedParts: MessagePart[] = [...nonTextParts];
const userInput = stripImages(textParts).trimStart();
@ -311,10 +311,17 @@ async function renderSlashCommandPrompt(
renderedPrompt = [command.prompt, userInput].join("\n\n");
}
slashedParts.push({
type: "text",
text: renderedPrompt.trim(), // Includes user input
});
if (renderedPrompt) {
slashedParts.push({
type: "text",
text: renderedPrompt.trim(), // Includes user input
});
} else {
console.warn(
`Invalid/empty prompt + input from slash command ${command.name}`,
);
}
break;
case "built-in":
case "invokable-rule":
@ -323,10 +330,18 @@ async function renderSlashCommandPrompt(
console.warn(`Slash command ${command.name} is missing prompt`);
break;
}
slashedParts.push({
type: "text",
text: `${command.prompt}${userInput ? "\n\n" + userInput : ""}`,
});
const rendered =
`${command.prompt}${userInput ? "\n\n" + userInput : ""}`.trim();
if (rendered) {
slashedParts.push({
type: "text",
text: rendered,
});
} else {
console.warn(
`Invalid/empty prompt + input from slash command ${command.name}`,
);
}
default:
break;
}
@ -362,18 +377,21 @@ async function gatherContextItems({
}),
);
const withDefaults = [...contextRequests, ...defaultRequests];
const deduplicated = withDefaults.reduce<GetContextRequest[]>((acc, item) => {
if (
!acc.some((i) => i.provider === item.provider && i.query === item.query)
) {
acc.push(item);
}
return acc;
}, []);
const deduplicatedInputs = withDefaults.reduce<GetContextRequest[]>(
(acc, item) => {
if (
!acc.some((i) => i.provider === item.provider && i.query === item.query)
) {
acc.push(item);
}
return acc;
},
[],
);
let contextItems: ContextItemWithId[] = [];
// Process context item attributes
for (const item of deduplicated) {
for (const item of deduplicatedInputs) {
const result = await ideMessenger.request("context/getContextItems", {
name: item.provider,
query: item.query ?? "",
@ -388,7 +406,7 @@ async function gatherContextItems({
// cmd+enter to use codebase
if (
modifiers.useCodebase &&
!deduplicated.some((item) => item.provider === "codebase")
!deduplicatedInputs.some((item) => item.provider === "codebase")
) {
const result = await ideMessenger.request("context/getContextItems", {
name: "codebase",
@ -405,7 +423,7 @@ async function gatherContextItems({
// noContext modifier adds currently open file if it's not already present
if (
!modifiers.noContext &&
!deduplicated.some((item) => item.provider === "currentFile")
!deduplicatedInputs.some((item) => item.provider === "currentFile")
) {
const currentFileResponse = await ideMessenger.request(
"context/getContextItems",
@ -428,7 +446,27 @@ async function gatherContextItems({
}
}
return contextItems;
// Deduplicates based on either providerTitle + itemId or uri type + value
const deduplicatedOutputs = contextItems.reduce<ContextItemWithId[]>(
(acc, item) => {
if (
!acc.some(
(i) =>
(i.id.providerTitle === item.id.providerTitle &&
i.id.itemId === item.id.itemId) ||
(i.uri &&
item.uri &&
i.uri.type === item.uri.type &&
i.uri.value === item.uri.value),
)
) {
acc.push(item);
}
return acc;
},
[],
);
return deduplicatedOutputs;
}
function resvolePromptBlock(p: JSONContent): string | undefined {