feat: Allow attaching images from clipboard when running prompts#1339
Open
TheephopWS wants to merge 2 commits intosimonw:mainfrom
Open
feat: Allow attaching images from clipboard when running prompts#1339TheephopWS wants to merge 2 commits intosimonw:mainfrom
TheephopWS wants to merge 2 commits intosimonw:mainfrom
Conversation
…mpts. I implemented support for attaching clipboard contents (images or text) when running one-shot prompts or interactive chat sessions. Key changes include: - Added new `llm/clipboard.py` module with cross-platform clipboard access for Windows, macOS, and Linux, supporting both image and text retrieval. - Added `-C`/`--clipboard` flag to `llm prompt` command to attach clipboard contents. Images are added as attachments, text is prepended to the prompt as fallback. - Added `!clipboard` command in interactive `llm chat` mode to attach clipboard content mid-conversation. - Added tests for clipboard functionality. - Added documentation for the new clipboard attachment feature in `docs/usage.md`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR: Allow attaching images from clipboard when running prompts
Summary
Add feature to attach clipboard contents (images or text) when running one-shot prompts or interactive chat sessions, enabling users to quickly share screenshots or copied images, with automatic fallback to text if no image is present.
Changes
New clipboard module (
llm/clipboard.py)get_clipboard_image()- Retrieves image data from clipboard as PNG bytesget_clipboard_text()- Retrieves text from clipboardresolve_clipboard()- Returns anAttachmentfor images orstrfor textClipboardErrorexception for empty/inaccessible clipboardNew
-C/--clipboardflag forllm promptcommand (llm/cli.py)--clipboard/-Cflag to attach clipboard contents-aattachmentsNew
!clipboardcommand forllm chat(llm/cli.py)!clipboardinteractive command in chat modeImport updates (
llm/cli.py)ClipboardErrorandresolve_clipboardfrom clipboard moduleDocumentation
Updated
docs/usage.md(usage-clipboard)=explaining clipboard attachment featureRegenerated
docs/help.md--clipboardoption in help outputTests
Added tests in
tests/test_clipboard.py:TestResolveClipboard::test_resolve_clipboard_with_image- Image returns AttachmentTestResolveClipboard::test_resolve_clipboard_with_text- Text fallback returns stringTestResolveClipboard::test_resolve_clipboard_empty- Empty clipboard raises errorTestResolveClipboard::test_resolve_clipboard_image_priority- Image takes priority over textTestClipboardCLI::test_prompt_with_clipboard_image- CLI with image in clipboardTestClipboardCLI::test_prompt_with_clipboard_text- CLI with text in clipboardTestClipboardCLI::test_prompt_with_clipboard_empty- CLI with empty clipboardTestClipboardCLI::test_prompt_clipboard_with_other_attachments- Combining -a and --clipboardTestClipboardCLI::test_prompt_short_flag- Testing -C short flagTestClipboardTextFallback::test_text_prepended_to_prompt- Text prepended correctlyTestClipboardTextFallback::test_text_only_no_user_prompt- Text-only prompt worksTesting Results
Test 1: Attach image from clipboard
Test 2: Attach text from clipboard (fallback)
Test 3: Short flag works
$ llm "what is this?" -C -m gpt-4oTest 4: Combine with file attachment
$ llm "compare these images" -a reference.jpg --clipboard -m gpt-4o Comparing the two images...Test 5: Empty clipboard error
Test 6: Interactive chat with !clipboard
Automated Test Results
Files Changed
Suggested Changelog Entry
-C/--clipboardflag for the prompt command allows attaching clipboard contents (images or text) directly to prompts. Usellm "describe this" --clipboardto attach a copied image, or use it with text in the clipboard to prepend copied text to your prompt. In chat mode, use the!clipboardcommand to attach clipboard content mid-conversation.PR Plan Completion Check
supports #1338