Skip to content

Prompt Snippets #34

@backnotprop

Description

@backnotprop

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):

  1. Save Snippets: Users can save the content of the "Prompt Prefix" or "Prompt Suffix" textareas as named snippets.
  2. View Snippets: Saved snippets are listed in a new dedicated tree view in the Prompt Tower sidebar.
  3. 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.
  4. Reference Expansion: During the "Create Context" or "Create & Copy to Clipboard" process, any @snippet_name references 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 workspaceState under a key like 'promptTower.savedPromptSnippets'.
  • The stored value will be an array of SavedPromptSnippet objects.
  • 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:
      1. The extension retrieves the current content of the respective textarea (prefix or suffix).
      2. 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_overview and its content will be injected when used."
        • Placeholder: snippet_name
      3. 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.
      4. If the name is valid and unique, a new SavedPromptSnippet object is created and added to the array in workspaceState.
      5. A confirmation message is shown: vscode.window.showInformationMessage(\Snippet '@${name}' saved. Its content will be injected when this reference is used.`);`
      6. 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_name reference) 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 updatePrefix message back to the extension with the new full content of the prefix textarea. This ensures the MultiRootTreeProvider's internal prefix state is updated and the context preview is invalidated (the preview itself will still show the @reference until context is re-generated).

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 SavedPromptSnippet will be represented as an item in this tree.
    • Label: The snippet.name (e.g., project_overview).
    • Tooltip: "Click to insert @snippet.name into 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.name as an argument.
      • The command's handler in extension.ts will then send the appendToPrefix message to the webview with the reference string (e.g., @project_overview).
  • 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.

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:
    1. When "Create Context" or "Create & Copy to Clipboard" is triggered, retrieve the current text from the "Prompt Prefix" and "Prompt Suffix" inputs.
    2. Scan these texts for any occurrences of the @snippetName pattern (e.g., using a regular expression like /@[a-zA-Z0-9_]+/g).
    3. For each valid @snippetName reference found:
      a. Extract the snippetName (the part after @).
      b. Look up the SavedPromptSnippet in the workspaceState (via SnippetService) that has a matching name.
      c. If a matching snippet is found, replace the entire @snippetName reference in the prefix/suffix string with the snippet.content.
      d. If no matching snippet is found: The reference @snippetName should 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.
    4. 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.
    5. The resulting prefix and suffix strings (with all valid references expanded) are then used by ContextGenerationService to 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 @snippetName references 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions