From 7f877f262b85d62003b478c400b63f5ee79d7ca7 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 23 Jun 2025 19:04:45 -0700 Subject: [PATCH] deduplicate context item outputs --- .../commands/slash/promptBlockSlashCommand.ts | 11 +-- .../TipTapEditor/utils/renderPromptv2.ts | 44 +++++----- .../utils/resolveEditorContent.ts | 80 ++++++++++++++----- 3 files changed, 89 insertions(+), 46 deletions(-) diff --git a/core/commands/slash/promptBlockSlashCommand.ts b/core/commands/slash/promptBlockSlashCommand.ts index a71ab20a6..00530b8bd 100644 --- a/core/commands/slash/promptBlockSlashCommand.ts +++ b/core/commands/slash/promptBlockSlashCommand.ts @@ -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", }; } diff --git a/gui/src/components/mainInput/TipTapEditor/utils/renderPromptv2.ts b/gui/src/components/mainInput/TipTapEditor/utils/renderPromptv2.ts index 987814193..51a8e37b8 100644 --- a/gui/src/components/mainInput/TipTapEditor/utils/renderPromptv2.ts +++ b/gui/src/components/mainInput/TipTapEditor/utils/renderPromptv2.ts @@ -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; } diff --git a/gui/src/components/mainInput/TipTapEditor/utils/resolveEditorContent.ts b/gui/src/components/mainInput/TipTapEditor/utils/resolveEditorContent.ts index 37677e2cb..465558dee 100644 --- a/gui/src/components/mainInput/TipTapEditor/utils/resolveEditorContent.ts +++ b/gui/src/components/mainInput/TipTapEditor/utils/resolveEditorContent.ts @@ -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((acc, item) => { - if ( - !acc.some((i) => i.provider === item.provider && i.query === item.query) - ) { - acc.push(item); - } - return acc; - }, []); + const deduplicatedInputs = withDefaults.reduce( + (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( + (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 {