-
Notifications
You must be signed in to change notification settings - Fork 22
Description
Evolving and in support of #7 , #10
I have a consistent need to reuse parts of prompts in my codebase context.
this method utilizes the @ reference to auto-inject saved prompt snippets.
Feature: Saved Prompt Snippets (V1)
Goal: Allow users to save frequently used pieces of text (snippets) from the Prompt Prefix or Prompt Suffix inputs. When a user inserts a reference to these snippets (e.g., @snippet_name) into their Prompt Prefix or Suffix, this reference will be automatically expanded with the snippet's full content before the final prompt context is generated and sent to the LLM.
Core Functionality (V1):
- Save Snippets: Users can save the content of the "Prompt Prefix" or "Prompt Suffix" textareas as named snippets.
- View Snippets: Saved snippets are listed in a new dedicated tree view in the Prompt Tower sidebar.
- Insert Snippet References: Clicking a snippet in the tree view inserts its reference (e.g.,
@snippet_name) into the "Prompt Prefix" textarea in the webview. Users can also manually type these references. - Reference Expansion: During the "Create Context" or "Create & Copy to Clipboard" process, any
@snippet_namereferences found in the Prompt Prefix or Prompt Suffix text are replaced with the actual content of the corresponding saved snippet. This expansion happens before the prefix/suffix are combined with the selected files and other context elements.
1. Data Model
A SavedPromptSnippet will be defined as follows:
interface SavedPromptSnippet {
id: string; // Unique identifier (e.g., generated UUID)
name: string; // User-defined name. Must be unique within the workspace.
// This name is used for the @reference (e.g., "project_overview").
// Does not contain the '@' symbol itself.
content: string; // The actual text content of the snippet.
createdAt: string; // ISO timestamp of when the snippet was created.
}2. Storage
- Snippets will be stored in the VS Code
workspaceStateunder a key like'promptTower.savedPromptSnippets'. - The stored value will be an array of
SavedPromptSnippetobjects. - This makes snippets specific to the current workspace.
3. Webview UI & Interaction
-
Saving a Snippet:
- An icon button (e.g., 💾 or "+") will be placed next to the "Prompt Prefix" textarea. Tooltip: "Save Prefix content as Snippet".
- A similar icon button will be placed next to the "Prompt Suffix" textarea. Tooltip: "Save Suffix content as Snippet".
- Action on Click:
- The extension retrieves the current content of the respective textarea (prefix or suffix).
- The extension prompts the user for a unique snippet name using
vscode.window.showInputBox:- Title: "Save Snippet"
- Prompt: "Enter a unique name for this snippet (e.g.,
project_overview). It will be referenced as@project_overviewand its content will be injected when used." - Placeholder:
snippet_name
- Name Validation (performed by the extension):
- The name cannot be empty.
- The name must be unique among all currently saved snippets for the workspace. If a duplicate is entered, an error message (
vscode.window.showErrorMessage) will be shown, and the save operation will be aborted. - It is recommended that names consist of letters, numbers, and underscores (e.g.,
^[a-zA-Z0-9_]+$) for clarity.
- If the name is valid and unique, a new
SavedPromptSnippetobject is created and added to the array inworkspaceState. - A confirmation message is shown:
vscode.window.showInformationMessage(\Snippet '@${name}' saved. Its content will be injected when this reference is used.`);` - The content of the webview's prefix/suffix textarea from which the snippet was saved remains unchanged.
-
Receiving Snippet References (from Tree View Click):
- The webview will listen for a message from the extension (e.g.,
command: 'appendToPrefix', text: string). - Upon receiving this message, the webview's JavaScript will append the provided
text(which will be the@snippet_namereference) to the "Prompt Prefix" textarea.- The appended text should preferably start on a new line. If the existing prefix content is not empty and does not end with a newline, a newline character should be inserted before the reference. A newline character should also be appended after the inserted reference.
- Example:
prefixTextArea.value += (currentVal.endsWith('\n') || currentVal.length === 0 ? '' : '\n') + referenceText + '\n';
- After updating the prefix textarea, the webview must send an
updatePrefixmessage back to the extension with the new full content of the prefix textarea. This ensures theMultiRootTreeProvider's internal prefix state is updated and the context preview is invalidated (the preview itself will still show the@referenceuntil context is re-generated).
- The webview will listen for a message from the extension (e.g.,
4. Sidebar Tree View ("Prompt Snippets")
- Location: A new tree view will be added to the "Prompt Tower" section in the VS Code Activity Bar. It should be titled "Prompt Snippets" (or "Saved Snippets") and appear above the "GitHub Issues" view.
- Tree Items:
- Each
SavedPromptSnippetwill be represented as an item in this tree. - Label: The
snippet.name(e.g.,project_overview). - Tooltip: "Click to insert
@snippet.nameinto Prompt Prefix. This reference will be expanded with the snippet's content during context generation." - Icon: A default file/text icon, or a custom snippet icon.
- Collapsible State:
vscode.TreeItemCollapsibleState.None. - Primary Action (On Click):
- Clicking a tree item will trigger a VS Code command (e.g.,
promptTower.insertSnippetReference). - This command will receive the
snippet.nameas an argument. - The command's handler in
extension.tswill then send theappendToPrefixmessage to the webview with the reference string (e.g.,@project_overview).
- Clicking a tree item will trigger a VS Code command (e.g.,
- Each
- No Checkboxes or Context Menu: These are deferred for future enhancements.
5. Reference Format
- The standard format for referencing a saved snippet within the prompt text will be
@snippetName.- Example: If a snippet is saved with the name
my_system_prompt, it will be referenced as@my_system_prompt.
- Example: If a snippet is saved with the name
6. Context Generation (Reference Expansion)
- This is a critical component of V1.
- The
ContextGenerationService(or a dedicated helper function called by it) will be responsible for processing the raw prefix and suffix text provided from the webview before they are used to construct the final prompt. - Expansion Process:
- When "Create Context" or "Create & Copy to Clipboard" is triggered, retrieve the current text from the "Prompt Prefix" and "Prompt Suffix" inputs.
- Scan these texts for any occurrences of the
@snippetNamepattern (e.g., using a regular expression like/@[a-zA-Z0-9_]+/g). - For each valid
@snippetNamereference found:
a. Extract thesnippetName(the part after@).
b. Look up theSavedPromptSnippetin theworkspaceState(viaSnippetService) that has a matchingname.
c. If a matching snippet is found, replace the entire@snippetNamereference in the prefix/suffix string with thesnippet.content.
d. If no matching snippet is found: The reference@snippetNameshould be left as-is in the text. A warning can be logged to the Output channel (e.g., "Prompt Tower: Unknown snippet reference '@unknown_snippet' found in prefix/suffix."). This prevents errors and makes the user aware if they mistyped a reference. - Recursion: For V1, recursive expansion (where a snippet's content itself contains another
@reference) will not be supported to prevent complexity and potential infinite loops. Only one level of expansion will occur on the initial prefix/suffix text. - The resulting prefix and suffix strings (with all valid references expanded) are then used by
ContextGenerationServiceto build the final prompt.
- The "Context Preview" in the webview will be updated with the fully expanded context after the "Create Context" button is clicked. The textareas for Prefix and Suffix in the webview will continue to show the
@references.
7. Scope of V1 (Exclusions)
- No editing of existing snippets.
- No deleting of existing snippets.
- No renaming of existing snippets.
- No reordering or grouping of snippets in the tree view.
- No live/inline autocompletion or expansion of
@snippetNamereferences as the user types in the webview textareas. Expansion occurs only during the context generation step. - No support for recursive snippet expansion.
This V1 plan ensures that the core utility of injecting predefined content into prompts via simple @references is delivered, directly addressing the spirit of the user's need for "cheat sheets" or reusable blocks of text. The LLM will receive the expanded content.