@foxfirecodes/pi-stash 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # @foxfirecodes/pi-stash
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Add prompt stash/restore shortcut with configurable keybinding.
8
+ - Replace an existing stash when the stash shortcut is pressed with editor text present.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 foxfirecodes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # pi-stash
2
+
3
+ Pi package that adds a prompt stash shortcut for the interactive editor.
4
+
5
+ ## Behavior
6
+
7
+ Press the stash keybinding to push the current prompt aside and clear the editor. Press it again with an empty editor to restore the stashed prompt. If the editor has text while a prompt is already stashed, pressing the stash keybinding replaces the old stash with the current editor text.
8
+
9
+ You can also send another message while a prompt is stashed; pi-stash will restore the stashed prompt back into the editor immediately after that message is submitted.
10
+
11
+ This is useful when you have a longer prompt drafted but want to quickly ask or send something else first.
12
+
13
+ ## Keybinding
14
+
15
+ The default keybinding is `ctrl+s`.
16
+
17
+ You can customize it with the `--pi-stash-key` extension flag:
18
+
19
+ ```bash
20
+ pi --pi-stash-key ctrl+x
21
+ ```
22
+
23
+ Or with an environment variable:
24
+
25
+ ```bash
26
+ PI_STASH_KEY=ctrl+x pi
27
+ ```
28
+
29
+ The key string uses pi's key format, for example `ctrl+x`, `ctrl+shift+s`, or `alt+s`. If your terminal swallows `ctrl+s` for flow control, choose a different key or disable XON/XOFF flow control in your shell.
30
+
31
+ ## Install
32
+
33
+ ```bash
34
+ pi install npm:@foxfirecodes/pi-stash
35
+ ```
36
+
37
+ ## Development
38
+
39
+ From this checkout:
40
+
41
+ ```bash
42
+ pi install .
43
+ ```
44
+
45
+ Or try it for one run:
46
+
47
+ ```bash
48
+ pi -e /path/to/pi-extensions/packages/pi-stash
49
+ ```
50
+
51
+ To run automated tests:
52
+
53
+ ```bash
54
+ pnpm test
55
+ ```
@@ -0,0 +1,93 @@
1
+ const DEFAULT_STASH_KEY = "ctrl+s";
2
+ const KEYBIND_FLAG = "pi-stash-key";
3
+ const STATUS_ID = "pi-stash";
4
+
5
+ function configuredKeybind(pi) {
6
+ const flagValue = pi.getFlag(KEYBIND_FLAG);
7
+ return typeof flagValue === "string" ? flagValue : DEFAULT_STASH_KEY;
8
+ }
9
+
10
+ function formatStatus(ctx, keybind) {
11
+ const text = `stash:${keybind}`;
12
+ return ctx.ui.theme?.fg ? ctx.ui.theme.fg("accent", text) : text;
13
+ }
14
+
15
+ export default function piStash(pi) {
16
+ let stashedPrompt;
17
+ let restoreAfterNextMessage = false;
18
+ let keybind = DEFAULT_STASH_KEY;
19
+
20
+ function updateStatus(ctx) {
21
+ if (!ctx.hasUI) return;
22
+ ctx.ui.setStatus(
23
+ STATUS_ID,
24
+ stashedPrompt === undefined ? undefined : formatStatus(ctx, keybind),
25
+ );
26
+ }
27
+
28
+ function restorePrompt(ctx, { notify = true } = {}) {
29
+ if (!ctx.hasUI || stashedPrompt === undefined) return false;
30
+
31
+ const prompt = stashedPrompt;
32
+ stashedPrompt = undefined;
33
+ restoreAfterNextMessage = false;
34
+
35
+ ctx.ui.setEditorText(prompt);
36
+ updateStatus(ctx);
37
+
38
+ if (notify) {
39
+ ctx.ui.notify("Restored stashed prompt.", "info");
40
+ }
41
+
42
+ return true;
43
+ }
44
+
45
+ function stashOrRestorePrompt(ctx) {
46
+ if (!ctx.hasUI) return;
47
+
48
+ const currentPrompt = ctx.ui.getEditorText();
49
+ if (currentPrompt.trim()) {
50
+ stashedPrompt = currentPrompt;
51
+ restoreAfterNextMessage = true;
52
+
53
+ ctx.ui.setEditorText("");
54
+ updateStatus(ctx);
55
+ ctx.ui.notify(
56
+ `Prompt stashed. Press ${keybind} again or send the next message to restore it.`,
57
+ "info",
58
+ );
59
+ return;
60
+ }
61
+
62
+ if (restorePrompt(ctx)) return;
63
+
64
+ ctx.ui.notify("No prompt to stash.", "info");
65
+ }
66
+
67
+ pi.registerFlag(KEYBIND_FLAG, {
68
+ description: `Keybinding for pi-stash prompt stash/restore (default: ${DEFAULT_STASH_KEY})`,
69
+ type: "string",
70
+ });
71
+
72
+ pi.on("session_start", async (_event, ctx) => {
73
+ keybind = configuredKeybind(pi);
74
+ pi.registerShortcut(keybind, {
75
+ description: "Stash or restore the current prompt",
76
+ handler: stashOrRestorePrompt,
77
+ });
78
+ updateStatus(ctx);
79
+ });
80
+
81
+ pi.on("input", async (event, ctx) => {
82
+ if (
83
+ event.source !== "interactive" ||
84
+ !restoreAfterNextMessage ||
85
+ stashedPrompt === undefined
86
+ ) {
87
+ return { action: "continue" };
88
+ }
89
+
90
+ restorePrompt(ctx);
91
+ return { action: "continue" };
92
+ });
93
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@foxfirecodes/pi-stash",
3
+ "version": "0.1.0",
4
+ "description": "Pi extension that adds a shortcut to stash and restore the current prompt.",
5
+ "type": "module",
6
+ "keywords": [
7
+ "pi-package",
8
+ "pi-extension",
9
+ "prompt",
10
+ "shortcut",
11
+ "stash"
12
+ ],
13
+ "license": "MIT",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/foxfirecodes/pi-extensions.git",
17
+ "directory": "packages/pi-stash"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/foxfirecodes/pi-extensions/issues"
21
+ },
22
+ "homepage": "https://github.com/foxfirecodes/pi-extensions/tree/main/packages/pi-stash#readme",
23
+ "pi": {
24
+ "extensions": [
25
+ "./extensions/pi-stash.js"
26
+ ]
27
+ },
28
+ "scripts": {
29
+ "test": "node --test test/*.test.js"
30
+ }
31
+ }
@@ -0,0 +1,182 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+
4
+ import piStash from "../extensions/pi-stash.js";
5
+
6
+ function createPi({ flagValue } = {}) {
7
+ const handlers = new Map();
8
+ const shortcuts = [];
9
+ const flags = [];
10
+
11
+ return {
12
+ flags,
13
+ handlers,
14
+ shortcuts,
15
+ on(event, handler) {
16
+ const list = handlers.get(event) ?? [];
17
+ list.push(handler);
18
+ handlers.set(event, list);
19
+ },
20
+ registerFlag(name, options) {
21
+ flags.push({ name, options });
22
+ },
23
+ getFlag(name) {
24
+ assert.equal(name, "pi-stash-key");
25
+ return flagValue;
26
+ },
27
+ registerShortcut(shortcut, options) {
28
+ shortcuts.push({ shortcut, options });
29
+ },
30
+ };
31
+ }
32
+
33
+ function createContext(initialText = "") {
34
+ let editorText = initialText;
35
+ const notifications = [];
36
+ const statuses = new Map();
37
+
38
+ return {
39
+ ctx: {
40
+ hasUI: true,
41
+ ui: {
42
+ getEditorText() {
43
+ return editorText;
44
+ },
45
+ setEditorText(nextText) {
46
+ editorText = nextText;
47
+ },
48
+ notify(message, type) {
49
+ notifications.push({ message, type });
50
+ },
51
+ setStatus(id, text) {
52
+ statuses.set(id, text);
53
+ },
54
+ theme: {
55
+ fg(_color, text) {
56
+ return text;
57
+ },
58
+ },
59
+ },
60
+ },
61
+ notifications,
62
+ statuses,
63
+ get editorText() {
64
+ return editorText;
65
+ },
66
+ };
67
+ }
68
+
69
+ async function loadExtension({ flagValue } = {}) {
70
+ const pi = createPi({ flagValue });
71
+ piStash(pi);
72
+
73
+ assert.equal(pi.flags.length, 1);
74
+ assert.equal(pi.flags[0].name, "pi-stash-key");
75
+
76
+ const sessionStartHandlers = pi.handlers.get("session_start") ?? [];
77
+ assert.equal(sessionStartHandlers.length, 1);
78
+
79
+ const context = createContext();
80
+ await sessionStartHandlers[0]({}, context.ctx);
81
+
82
+ return { pi, context };
83
+ }
84
+
85
+ test("registers a default ctrl+s stash shortcut", async () => {
86
+ const { pi } = await loadExtension();
87
+
88
+ assert.equal(pi.shortcuts.length, 1);
89
+ assert.equal(pi.shortcuts[0].shortcut, "ctrl+s");
90
+ assert.equal(typeof pi.shortcuts[0].options.handler, "function");
91
+ });
92
+
93
+ test("uses the pi-stash-key flag when provided", async () => {
94
+ const { pi } = await loadExtension({ flagValue: "ctrl+x" });
95
+
96
+ assert.equal(pi.shortcuts.length, 1);
97
+ assert.equal(pi.shortcuts[0].shortcut, "ctrl+x");
98
+ });
99
+
100
+ test("stashes current editor text and restores it on the next shortcut press", async () => {
101
+ const { pi } = await loadExtension();
102
+ const context = createContext("draft prompt");
103
+ const handler = pi.shortcuts[0].options.handler;
104
+
105
+ await handler(context.ctx);
106
+
107
+ assert.equal(context.editorText, "");
108
+ assert.equal(context.statuses.get("pi-stash"), "stash:ctrl+s");
109
+
110
+ await handler(context.ctx);
111
+
112
+ assert.equal(context.editorText, "draft prompt");
113
+ assert.equal(context.statuses.get("pi-stash"), undefined);
114
+ });
115
+
116
+ test("overwrites an existing stash when the editor has text", async () => {
117
+ const { pi } = await loadExtension();
118
+ const context = createContext("first draft");
119
+ const handler = pi.shortcuts[0].options.handler;
120
+
121
+ await handler(context.ctx);
122
+ context.ctx.ui.setEditorText("second draft");
123
+
124
+ await handler(context.ctx);
125
+
126
+ assert.equal(context.editorText, "");
127
+ assert.equal(context.statuses.get("pi-stash"), "stash:ctrl+s");
128
+
129
+ await handler(context.ctx);
130
+
131
+ assert.equal(context.editorText, "second draft");
132
+ assert.equal(context.statuses.get("pi-stash"), undefined);
133
+ });
134
+
135
+ test("restores stashed prompt after the next interactive message is sent", async () => {
136
+ const { pi } = await loadExtension();
137
+ const context = createContext("draft prompt");
138
+ const shortcutHandler = pi.shortcuts[0].options.handler;
139
+ const inputHandler = pi.handlers.get("input")[0];
140
+
141
+ await shortcutHandler(context.ctx);
142
+ context.ctx.ui.setEditorText("quick question");
143
+
144
+ const result = await inputHandler(
145
+ { text: "quick question", images: [], source: "interactive" },
146
+ context.ctx,
147
+ );
148
+
149
+ assert.deepEqual(result, { action: "continue" });
150
+ assert.equal(context.editorText, "draft prompt");
151
+ assert.equal(context.statuses.get("pi-stash"), undefined);
152
+ });
153
+
154
+ test("does not restore for extension-injected messages", async () => {
155
+ const { pi } = await loadExtension();
156
+ const context = createContext("draft prompt");
157
+ const shortcutHandler = pi.shortcuts[0].options.handler;
158
+ const inputHandler = pi.handlers.get("input")[0];
159
+
160
+ await shortcutHandler(context.ctx);
161
+ context.ctx.ui.setEditorText("");
162
+
163
+ await inputHandler(
164
+ { text: "generated", images: [], source: "extension" },
165
+ context.ctx,
166
+ );
167
+
168
+ assert.equal(context.editorText, "");
169
+ assert.equal(context.statuses.get("pi-stash"), "stash:ctrl+s");
170
+ });
171
+
172
+ test("does not stash an empty prompt", async () => {
173
+ const { pi } = await loadExtension();
174
+ const context = createContext(" ");
175
+ const handler = pi.shortcuts[0].options.handler;
176
+
177
+ await handler(context.ctx);
178
+
179
+ assert.equal(context.editorText, " ");
180
+ assert.equal(context.statuses.get("pi-stash"), undefined);
181
+ assert.equal(context.notifications.at(-1).message, "No prompt to stash.");
182
+ });