@juicesharp/rpiv-args 0.9.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.
Files changed (5) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +202 -0
  3. package/args.ts +194 -0
  4. package/index.ts +13 -0
  5. package/package.json +41 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 juicesharp
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,202 @@
1
+ # @juicesharp/rpiv-args
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@juicesharp/rpiv-args.svg)](https://www.npmjs.com/package/@juicesharp/rpiv-args)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ Pi extension that adds placeholder substitution to skill arguments. When you
7
+ invoke `/skill:<name> <args>`, rpiv-args substitutes placeholders inside the
8
+ skill body — `$1`, `$ARGUMENTS`, `$@`, `${@:2}`, `${@:2:3}` — before the
9
+ content reaches the LLM. Skills without placeholders emit a `<skill>` wrapper
10
+ byte-identical to Pi's built-in, so installing rpiv-args never changes
11
+ behavior for existing skills.
12
+
13
+ ## Install
14
+
15
+ ```
16
+ pi install npm:@juicesharp/rpiv-args
17
+ ```
18
+
19
+ Or run `/rpiv-setup` if you have `@juicesharp/rpiv-pi` installed.
20
+
21
+ ## Placeholders
22
+
23
+ | Placeholder | Replaced with | Example |
24
+ |---|---|---|
25
+ | `$1`, `$2`, … | Positional argument (1-indexed) | `/skill:foo a b c` → `$1` = `a`, `$2` = `b` |
26
+ | `$ARGUMENTS` | All arguments as a single string | `/skill:foo a b c` → `a b c` |
27
+ | `$@` | Same as `$ARGUMENTS` | `/skill:foo a b c` → `a b c` |
28
+ | `${@:N}` | Arguments from position N onward | `/skill:foo a b c` → `${@:2}` = `b c` |
29
+ | `${@:N:L}` | L arguments starting at position N | `/skill:foo a b c d` → `${@:2:2}` = `b c` |
30
+
31
+ **Indexing is 1-based** — `$1` is the first argument, `$2` is the second.
32
+ Out-of-range positions resolve to an empty string. For `${@:N[:L]}`, `N` is
33
+ clamped to `≥ 1` and out-of-range slices yield an empty string.
34
+
35
+ Multi-word values use shell-style quoting:
36
+
37
+ ```
38
+ /skill:deploy "staging server" --force
39
+ ```
40
+
41
+ → `$1` = `staging server`, `$2` = `--force`, `$ARGUMENTS` = `staging server --force`
42
+
43
+ ## How it works
44
+
45
+ rpiv-args intercepts the `input` event (fires before Pi's built-in skill
46
+ expansion). When a skill body contains at least one placeholder, the extension:
47
+
48
+ 1. Parses arguments using shell-style quoting
49
+ 2. Substitutes all placeholders in the body
50
+ 3. Wraps the result in a `<skill>` block byte-identical to Pi's native format
51
+ 4. Appends the raw arguments after the block — matches Pi's standard output so any tool that parses `<skill>` blocks continues to work unchanged
52
+
53
+ When no placeholders are found in the skill body, the output is byte-identical
54
+ to Pi's built-in expansion — zero behavioral change.
55
+
56
+ ## Writing skills with arguments
57
+
58
+ ### `$ARGUMENTS` vs `$1` — which to use
59
+
60
+ Use **`$ARGUMENTS`** (or `$@`) when the input is freeform text the LLM should
61
+ interpret naturally:
62
+
63
+ ```yaml
64
+ ---
65
+ name: fix-issue
66
+ description: Fix a GitHub issue by number or description
67
+ ---
68
+
69
+ Fix the following issue: $ARGUMENTS
70
+ ```
71
+
72
+ ```
73
+ /skill:fix-issue login page crashes on mobile
74
+ ```
75
+
76
+ → `Fix the following issue: login page crashes on mobile`
77
+
78
+ Use **`$1`, `$2`** only for skills with a fixed, structured invocation pattern:
79
+
80
+ ```yaml
81
+ ---
82
+ name: migrate-component
83
+ description: Migrate a component between frameworks
84
+ ---
85
+
86
+ Migrate the $1 component from $2 to $3.
87
+ Preserve all existing behavior and tests.
88
+ ```
89
+
90
+ ```
91
+ /skill:migrate-component SearchBar React Vue
92
+ ```
93
+
94
+ → `Migrate the SearchBar component from React to Vue.`
95
+
96
+ ### Why this matters
97
+
98
+ If a positional skill receives natural language input:
99
+
100
+ ```
101
+ /skill:migrate-component can you migrate the search bar please
102
+ ```
103
+
104
+ → `Migrate the can component from you to migrate.` — **broken**.
105
+
106
+ The LLM is good at interpreting `$ARGUMENTS` as a whole, but positional
107
+ placeholders blindly split on spaces. Use `$ARGUMENTS` unless your skill has
108
+ a strict arg structure.
109
+
110
+ ### `argument-hint` frontmatter
111
+
112
+ Add an `argument-hint` to document what the skill expects:
113
+
114
+ ```yaml
115
+ ---
116
+ name: fix-issue
117
+ description: Fix a GitHub issue
118
+ argument-hint: [issue-number-or-description]
119
+ ---
120
+ ```
121
+
122
+ ```yaml
123
+ ---
124
+ name: migrate-component
125
+ description: Migrate a component between frameworks
126
+ argument-hint: [component] [from] [to]
127
+ ---
128
+ ```
129
+
130
+ rpiv-args ignores this field — substitution is triggered by placeholders in the body, not the hint.
131
+
132
+ **Note**: Pi currently surfaces `argument-hint` in autocomplete for prompt
133
+ templates (`commands/*.md`) but **not** for skills (`/skill:<name>`). The
134
+ field is read by Pi but not displayed in the `/skill:` autocomplete UI at
135
+ present — treat it as documentation metadata until upstream Pi exposes it.
136
+
137
+ ### Full example
138
+
139
+ <details>
140
+ <summary>Deploy skill — SKILL.md, invocation, and the exact text the LLM sees</summary>
141
+
142
+ ```yaml
143
+ ---
144
+ name: deploy
145
+ description: Deploy a service to an environment
146
+ argument-hint: [service] [environment]
147
+ ---
148
+
149
+ Deploy service $1 to $2.
150
+
151
+ ## Steps
152
+ 1. Run the test suite for $1
153
+ 2. Build the Docker image
154
+ 3. Push to the $2 registry
155
+ 4. Verify the deployment
156
+ ```
157
+
158
+ ```
159
+ /skill:deploy api production
160
+ ```
161
+
162
+ → The LLM receives:
163
+
164
+ ```xml
165
+ <skill name="deploy" location="...">
166
+ Deploy service api to production.
167
+
168
+ ## Steps
169
+ 1. Run the test suite for api
170
+ 2. Build the Docker image
171
+ 3. Push to the production registry
172
+ 4. Verify the deployment
173
+ </skill>
174
+
175
+ api production
176
+ ```
177
+
178
+ Note: the raw arguments (`api production`) are also appended after the
179
+ `</skill>` block — this is Pi's standard behavior and is preserved for
180
+ backward compatibility.
181
+
182
+ </details>
183
+
184
+ ## Backward compatibility
185
+
186
+ - Skills **without** placeholders → output is byte-identical to Pi's built-in expansion
187
+ - Skills **with** placeholders → body gets substitution, raw args still appended after block
188
+ - The `argument-hint` frontmatter field is read but not enforced in v1
189
+
190
+ ## Limitations
191
+
192
+ | Limitation | Detail |
193
+ |---|---|
194
+ | **No type validation** | `$1` expecting a file path receives whatever the user types |
195
+ | **No flag parsing** | `--env=prod` is a single positional token, not a parsed flag |
196
+ | **Literal substitution** | Placeholders are replaced even inside code blocks and inline code |
197
+ | **`steer()`/`followUp()` paths** | `session.steer()` / `session.followUp()` bypass the `input` event (see `agent-session.js:861-887`); placeholders are **not** resolved on those paths. Use the primary prompt path for argument-substituted skills. |
198
+ | **No recursive substitution** | A `$ARGUMENTS` value containing `$1` is not re-expanded |
199
+
200
+ ## License
201
+
202
+ MIT
package/args.ts ADDED
@@ -0,0 +1,194 @@
1
+ /**
2
+ * rpiv-args — core logic.
3
+ *
4
+ * Intercepts `/skill:<name> <args>` at the input hook and emits a byte-exact
5
+ * Pi skill wrapper with opt-in $N/$ARGUMENTS/$@/${@:N[:L]} substitution on
6
+ * the body. Falls through (returns {action:"continue"}) when the text is not
7
+ * a skill command, the skill is unknown, or the body contains no tokens —
8
+ * keeping Pi's built-in behavior 100% intact for today's 17 rpiv-pi skills.
9
+ *
10
+ * Byte-exact wrapper requirement: parseSkillBlock regex at
11
+ * node_modules/@mariozechner/pi-coding-agent/dist/core/agent-session.js:40
12
+ * is the load-bearing contract. Do not reformat the template literal below.
13
+ */
14
+
15
+ import { readFileSync } from "node:fs";
16
+ import {
17
+ type ExtensionAPI,
18
+ type InputEvent,
19
+ type InputEventResult,
20
+ loadSkills,
21
+ parseFrontmatter,
22
+ type Skill,
23
+ stripFrontmatter,
24
+ } from "@mariozechner/pi-coding-agent";
25
+
26
+ // ---------------------------------------------------------------------------
27
+ // Tokens
28
+ // ---------------------------------------------------------------------------
29
+
30
+ /** Matches any placeholder Pi's substituteArgs would replace. Used as the
31
+ * opt-in gate: absent → pass through verbatim (D2). */
32
+ const TOKEN_REGEX = /\$(?:\d+|ARGUMENTS|@|\{@:\d+(?::\d+)?\})/;
33
+
34
+ /** Prefix Pi uses (`agent-session.js:829`). Single-space tokenisation (D7). */
35
+ const SKILL_PREFIX = "/skill:";
36
+
37
+ /** Re-entrancy guard (D8). */
38
+ const WRAPPED_PREFIX = "<skill ";
39
+
40
+ // ---------------------------------------------------------------------------
41
+ // Tokeniser — byte-equivalent to Pi's parseCommandArgs at
42
+ // node_modules/@mariozechner/pi-coding-agent/dist/core/prompt-templates.js:11-42
43
+ // ---------------------------------------------------------------------------
44
+
45
+ export function parseCommandArgs(argsString: string): string[] {
46
+ const args: string[] = [];
47
+ let current = "";
48
+ let inQuote: string | null = null;
49
+ for (let i = 0; i < argsString.length; i++) {
50
+ const char = argsString[i];
51
+ if (inQuote) {
52
+ if (char === inQuote) {
53
+ inQuote = null;
54
+ } else {
55
+ current += char;
56
+ }
57
+ } else if (char === '"' || char === "'") {
58
+ inQuote = char;
59
+ } else if (char === " " || char === "\t") {
60
+ if (current) {
61
+ args.push(current);
62
+ current = "";
63
+ }
64
+ } else {
65
+ current += char;
66
+ }
67
+ }
68
+ if (current) args.push(current);
69
+ return args;
70
+ }
71
+
72
+ // ---------------------------------------------------------------------------
73
+ // Substitutor — byte-equivalent to Pi's substituteArgs at
74
+ // node_modules/@mariozechner/pi-coding-agent/dist/core/prompt-templates.js:54-82
75
+ // Order matters: $N first, then ${@:N[:L]}, then $ARGUMENTS, then $@.
76
+ // ---------------------------------------------------------------------------
77
+
78
+ export function substituteArgs(content: string, args: string[]): string {
79
+ let result = content;
80
+ result = result.replace(/\$(\d+)/g, (_, num) => args[parseInt(num, 10) - 1] ?? "");
81
+ result = result.replace(/\$\{@:(\d+)(?::(\d+))?\}/g, (_, startStr, lengthStr) => {
82
+ let start = parseInt(startStr, 10) - 1;
83
+ if (start < 0) start = 0;
84
+ if (lengthStr) {
85
+ const length = parseInt(lengthStr, 10);
86
+ return args.slice(start, start + length).join(" ");
87
+ }
88
+ return args.slice(start).join(" ");
89
+ });
90
+ const allArgs = args.join(" ");
91
+ result = result.replace(/\$ARGUMENTS/g, allArgs);
92
+ result = result.replace(/\$@/g, allArgs);
93
+ return result;
94
+ }
95
+
96
+ // ---------------------------------------------------------------------------
97
+ // Skill-path index — populated once, refreshed on session_start(reason:reload)
98
+ // ---------------------------------------------------------------------------
99
+
100
+ interface SkillIndexEntry {
101
+ readonly name: string;
102
+ readonly filePath: string;
103
+ readonly baseDir: string;
104
+ }
105
+
106
+ let skillIndex: Map<string, SkillIndexEntry> | null = null;
107
+
108
+ export function invalidateSkillIndex(): void {
109
+ skillIndex = null;
110
+ }
111
+
112
+ /** Build the name→path index by asking Pi for its currently-loaded skills. */
113
+ function buildSkillIndex(): Map<string, SkillIndexEntry> {
114
+ const { skills } = loadSkills({ cwd: process.cwd() });
115
+ const index = new Map<string, SkillIndexEntry>();
116
+ for (const s of skills as Skill[]) {
117
+ index.set(s.name, { name: s.name, filePath: s.filePath, baseDir: s.baseDir });
118
+ }
119
+ return index;
120
+ }
121
+
122
+ function getSkillIndex(): Map<string, SkillIndexEntry> {
123
+ if (!skillIndex) skillIndex = buildSkillIndex();
124
+ return skillIndex;
125
+ }
126
+
127
+ // ---------------------------------------------------------------------------
128
+ // Wrapper emit — byte-exact against parseSkillBlock regex at
129
+ // node_modules/@mariozechner/pi-coding-agent/dist/core/agent-session.js:40
130
+ // and byte-equivalent to _expandSkillCommand's output at :840-841.
131
+ // ---------------------------------------------------------------------------
132
+
133
+ function buildSkillBlock(entry: SkillIndexEntry, body: string): string {
134
+ return `<skill name="${entry.name}" location="${entry.filePath}">\nReferences are relative to ${entry.baseDir}.\n\n${body}\n</skill>`;
135
+ }
136
+
137
+ function appendArgs(skillBlock: string, args: string): string {
138
+ return args ? `${skillBlock}\n\n${args}` : skillBlock;
139
+ }
140
+
141
+ // ---------------------------------------------------------------------------
142
+ // Input handler
143
+ // ---------------------------------------------------------------------------
144
+
145
+ export function handleInput(event: InputEvent): InputEventResult {
146
+ const text = event.text;
147
+
148
+ // D8 re-entrancy: already-wrapped text (from our own or any other
149
+ // extension's {action:"transform"}) passes through untouched.
150
+ if (text.startsWith(WRAPPED_PREFIX)) return { action: "continue" };
151
+
152
+ if (!text.startsWith(SKILL_PREFIX)) return { action: "continue" };
153
+
154
+ // D7 single-space tokenisation — byte-match Pi's indexOf(" ") at :831.
155
+ const spaceIndex = text.indexOf(" ");
156
+ const skillName = spaceIndex === -1 ? text.slice(SKILL_PREFIX.length) : text.slice(SKILL_PREFIX.length, spaceIndex);
157
+ const argsString = spaceIndex === -1 ? "" : text.slice(spaceIndex + 1).trim();
158
+
159
+ const entry = getSkillIndex().get(skillName);
160
+ if (!entry) return { action: "continue" }; // unknown skill — let Pi handle it
161
+
162
+ let content: string;
163
+ try {
164
+ content = readFileSync(entry.filePath, "utf-8");
165
+ } catch {
166
+ return { action: "continue" }; // let Pi emit its error via _expandSkillCommand
167
+ }
168
+
169
+ const { frontmatter } = parseFrontmatter<{ "argument-hint"?: string }>(content);
170
+ void frontmatter; // informational only in v1 — D3
171
+ const body = stripFrontmatter(content).trim();
172
+
173
+ // D2 opt-in gate: if body has no token, emit byte-identical to Pi's :841.
174
+ if (!TOKEN_REGEX.test(body)) {
175
+ return { action: "transform", text: appendArgs(buildSkillBlock(entry, body), argsString) };
176
+ }
177
+
178
+ const parsed = parseCommandArgs(argsString);
179
+ const substituted = substituteArgs(body, parsed);
180
+ return { action: "transform", text: appendArgs(buildSkillBlock(entry, substituted), argsString) };
181
+ }
182
+
183
+ // ---------------------------------------------------------------------------
184
+ // Registration
185
+ // ---------------------------------------------------------------------------
186
+
187
+ export function registerArgsHandler(pi: ExtensionAPI): void {
188
+ pi.on("input", (event) => handleInput(event));
189
+ pi.on("session_start", (event) => {
190
+ if (event.reason === "reload" || event.reason === "startup") {
191
+ invalidateSkillIndex();
192
+ }
193
+ });
194
+ }
package/index.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * rpiv-args — Pi extension entry point.
3
+ *
4
+ * Registers the `input` event handler + a `session_start` cache invalidator.
5
+ * All logic lives in args.ts.
6
+ */
7
+
8
+ import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
9
+ import { registerArgsHandler } from "./args.js";
10
+
11
+ export default function (pi: ExtensionAPI): void {
12
+ registerArgsHandler(pi);
13
+ }
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@juicesharp/rpiv-args",
3
+ "version": "0.9.0",
4
+ "description": "Pi extension: skill-argument resolver — pre-emptively wraps /skill:<name> <args> via the input hook and performs opt-in $N/$ARGUMENTS substitution before Pi's built-in expansion",
5
+ "keywords": [
6
+ "pi-package",
7
+ "pi-extension",
8
+ "rpiv",
9
+ "skills",
10
+ "arguments"
11
+ ],
12
+ "type": "module",
13
+ "license": "MIT",
14
+ "author": "juicesharp",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/juicesharp/rpiv-mono.git",
18
+ "directory": "packages/rpiv-args"
19
+ },
20
+ "homepage": "https://github.com/juicesharp/rpiv-mono/tree/main/packages/rpiv-args#readme",
21
+ "bugs": {
22
+ "url": "https://github.com/juicesharp/rpiv-mono/issues"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "files": [
28
+ "index.ts",
29
+ "args.ts",
30
+ "README.md",
31
+ "LICENSE"
32
+ ],
33
+ "pi": {
34
+ "extensions": [
35
+ "./index.ts"
36
+ ]
37
+ },
38
+ "peerDependencies": {
39
+ "@mariozechner/pi-coding-agent": "*"
40
+ }
41
+ }