An open API service indexing awesome lists of open source software.

https://github.com/fitchmultz/pi-copy-user-message

pi extension that adds /copy-user to copy the most recent user message to your clipboard
https://github.com/fitchmultz/pi-copy-user-message

clipboard npm-package pi pi-extension pi-package typescript

Last synced: 2 days ago
JSON representation

pi extension that adds /copy-user to copy the most recent user message to your clipboard

Awesome Lists containing this project

README

          

# pi copy-user extension

A tiny [pi](https://github.com/badlogic/pi-mono) extension that adds a slash command for copying the **most recent user message** to your clipboard.

## What it does

- Adds `/copy-user`
- Copies the text from the most recent user message in the current session
- Preserves line breaks between text blocks
- Uses the system clipboard when available and a terminal OSC 52 clipboard escape in interactive TTY sessions
- Avoids synchronous clipboard subprocesses in the `/copy-user` command path
- Does **not** fall back to an older message if the latest user message has no text

## Install

Install it from npm with pi:

```bash
pi install npm:pi-copy-user-message
```

Or install it directly from GitHub with pi:

```bash
pi install https://github.com/fitchmultz/pi-copy-user-message
```

Then reload pi from inside the app with:

```text
/reload
```

If you prefer to load it directly from a local checkout during development, you can point pi at the package entrypoint:

```bash
pi -e ./extensions/copy-user-message.ts
```

## Compatibility

- Tested with [pi](https://github.com/earendil-works/pi-mono) 0.76.0
- Minimum Node.js for local repo tooling: `>=22.19.0`
- `.nvmrc` pins Node 22.19.0 for local development; CI also verifies Node 22.x, 24.x, and 26.x

This package keeps `@earendil-works/pi-coding-agent` as a development dependency for local typechecking and tests and declares pi core packages as wildcard peers per current pi package guidance; it is intended to run inside pi's bundled extension runtime.

## Usage

Once loaded, run:

```text
/copy-user
```

The command will:

- copy the newest user message text to the clipboard
- show a warning if there are no user messages
- show a warning if the newest user message contains no text

## Behavior notes

- Text-only by design: image-only user messages are not copied.
- If a user message contains multiple text blocks, they are joined with newlines.
- OSC 52 is only emitted in interactive TTY sessions, so print/RPC output stays clean.
- Clipboard command fallbacks run asynchronously so the slash-command path does not block the TUI event loop while utilities resolve or time out.
- On Linux-family environments, the extension tries `termux-clipboard-set`, then `wl-copy`, then X11 tools (`xclip`, then `xsel`).
- If no supported clipboard transport is available, the command reports an error instead of claiming success.
- The command is intentionally strict about “most recent” so it never copies an older user message by mistake.

## Development

This repo includes a small regression test in:

- `tests/copy-user-message.test.ts`

The test covers:

- latest text message
- latest image-only message
- latest whitespace-only message
- no user message at all
- actual `/copy-user` command success, warning, and error paths
- OSC 52 emission
- Linux clipboard fallback ordering
- X11 `xsel` fallback after `xclip`
- macOS `pbcopy` and Windows `clip` transport selection
- failed or unsupported clipboard transport cases

## Files

- `extensions/copy-user-message.ts` — publishable extension implementation
- `.pi/extensions/copy-user-message.ts` — thin project-local wrapper for auto-discovery in this repo
- `tests/copy-user-message.test.ts` — regression test for message selection, `/copy-user` command behavior, and clipboard transport fallbacks