Skip to content

kaidez/save-selected-text

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Save Selected Text

A VS Code extension that turns any highlighted text into an AI-powered exchange with Claude — instantly, from a right-click. Select text anywhere in your editor, choose Claude: Save Selected Text from the context menu, and Claude reasons through it and responds in a new tab.


What This Is

Save Selected Text is a TypeScript VS Code extension that wires the Anthropic API directly into your editing workflow. It follows the ETL (Extract, Transform, Load) pattern — the same architecture used in enterprise integration platforms like Workato, Boomi, and MuleSoft:

Your extension is the plumbing. Claude is the brain.

  1. Extract — capture the selected text from the active editor via the VS Code Selection API
  2. Transform — send it through Claude's reasoning engine via the Anthropic SDK, which generates a response that didn't exist before
  3. Load — save the selected text to a timestamped .txt file and display Claude's response in a new Markdown tab

Claude doesn't look up answers or retrieve pre-written responses. It reasons through what you selected, applies deduction and contextual understanding, and generates something new. The result opens right next to your code.


The Write-On-Success Pattern

This extension implements a write-on-success pattern — a core principle in integration engineering. The selected text is only saved to disk after Claude responds successfully. If the API call fails, nothing gets written. No orphaned files, no half-finished state.

This mirrors how database transactions work in production systems — nothing gets committed until the full operation succeeds.


Features

  • Right-click context menu — highlight any text, right-click, and run Claude: Save Selected Text instantly
  • Write-on-success — files are only saved after a confirmed Claude response
  • Timestamped output files — each selection is saved as prompt-[timestamp].txt in your workspace's prompts/ folder
  • Model dropdown — choose between Claude Haiku, Sonnet, and Opus in VS Code settings
  • Progress indicator — a notification bar shows while Claude is reasoning through your selection
  • Structured output — responses open in a new Markdown tab showing your selected text and Claude's reply side by side
  • Secure API key storage — your Anthropic API key is stored in VS Code settings, never hardcoded

How Claude's Reasoning Works Here

Claude is not a search engine. It doesn't retrieve pre-written answers. When your extension sends selected text to the API, Claude draws on everything it was trained on to reason through the input and generate a novel response.

Every API call is stateless — Claude starts fresh each time with no memory of previous selections. This makes the pipeline predictable and repeatable, which is exactly what you want in an automated workflow.

Practically, this means Claude can:

  • Summarize complex text into plain English
  • Classify content into categories without a predefined list
  • Deduce intent from loosely written notes and respond appropriately
  • Generate structured output — analyses, plans, explanations — from unstructured input
  • Answer questions by applying contextual reasoning, not keyword matching

Tech Stack

  • TypeScript — strict mode, Node16 module resolution
  • VS Code Extension APIregisterCommand, activeTextEditor, withProgress, openTextDocument
  • Anthropic SDK (@anthropic-ai/sdk) — claude-sonnet-4-6 model (configurable)
  • Node.js fs module — file system writes with write-on-success pattern

Getting Started

1. Clone the repo

git clone https://github.com/kaidez/save-selected-text.git
cd save-selected-text

2. Install dependencies

npm install

3. Add your Anthropic API key

  • Open VS Code Settings (Cmd+,)
  • Search for Save Selected Text
  • Paste your Anthropic API key into the Api Key field

Don't have a key? Get one at console.anthropic.com.

4. Compile and run

npm run compile

Press Fn+F5 (Mac) to launch the Extension Development Host.

5. Test it

In the dev host window, open any file with text in it, highlight a sentence or paragraph, right-click, and select Claude: Save Selected Text.


Usage

  1. Open any file in VS Code
  2. Highlight any text with your mouse
  3. Right-click and select Claude: Save Selected Text
  4. A progress notification appears while Claude reasons through your selection
  5. A new Markdown tab opens with your selected text and Claude's response
  6. A timestamped .txt file is saved to your workspace's prompts/ folder

Sample Output

SELECTED TEXT:
The write-on-success pattern ensures that data is only persisted after
a confirmed successful response from the external system.

---

CLAUDE'S RESPONSE:
This is a core principle in integration engineering — analogous to
database transaction commits. By deferring the write operation until
after the external API confirms success, the system avoids orphaned
records and maintains data integrity even when downstream services
are unavailable or return errors.

Project Structure

save-selected-text/
├── .vscode/
│   ├── extensions.json
│   ├── launch.json
│   └── tasks.json
├── out/                        # compiled JS output
├── prompts/                    # saved selections appear here
├── src/
│   ├── extension.ts            # all extension logic
│   └── test/
│       ├── runTest.ts          # test runner entry point
│       └── suite/
│           ├── index.ts        # test suite loader
│           └── extension.test.ts  # 9 unit tests
├── .gitignore
├── package.json
├── tsconfig.json
└── README.md

Tests

This project has 9 unit tests covering:

  • Extension presence and command registration
  • Guard clause behavior when no text is selected
  • Timestamped filename generation and format validation
  • File content accuracy — saved text matches selected text exactly
  • Write-on-success pattern — file only created after successful API response
  • Write-on-failure pattern — file NOT created when API call fails
  • Claude API response parsing — text extraction and fallback handling
  • Model dropdown configuration and valid default
npm test

How This Relates to Enterprise Integration

The architecture of this extension mirrors patterns used in production automation platforms every day:

  • The right-click command trigger is equivalent to a user-initiated webhook or manual trigger in Workato
  • The Anthropic SDK call is equivalent to calling an external AI enrichment service in a Boomi process
  • The write-on-success file save is equivalent to a conditional commit in an enterprise data pipeline
  • The timestamped output file is equivalent to writing an enriched record to a destination system

Building this from scratch in TypeScript demonstrates an understanding of the architecture, not just the tooling.


Companion Project

This extension is one of two AI-powered VS Code tools in this portfolio. The companion project — claude-prompt-reader — uses an event-driven file watcher to automatically process prompts on save.

Together they demonstrate two fundamental automation trigger patterns:

Project Trigger Type Pattern
claude-prompt-reader File save event Event-driven, automatic
save-selected-text User right-click Command-triggered, intentional

Both follow the same ETL architecture. Both connect to the Anthropic API. Both have full unit test suites.


What I'd Add Next

  • Conversation history — maintain context across multiple selections so Claude can reason over a growing thread
  • Multiple model support — let users configure different models for different selection types
  • Webhook output — POST enriched responses to an external endpoint in addition to saving locally
  • Batch processing — select multiple text blocks and process them as a single enriched pipeline

Author

Kai Gittens kaidez.com

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors