@gtheys/rifle-batteries 0.0.0-stage → 0.2.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/README.md CHANGED
@@ -1,3 +1,116 @@
1
- # Temporary Holding Version
1
+ # @gtheys/rifle-batteries
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Rifle distro batteries — the seven small single-purpose pi extensions of the
4
+ pi-my-rifle-ext distro (bootstrap, ask-user-question, prompt-snippets,
5
+ tool-pills, desktop-notify, sudo, herdr-title) consolidated into one
6
+ installable package, so a single manifest entry loads them all. Modules are
7
+ absorbed one per commit from their standalone packages; this README and the
8
+ manifest grow with every absorb.
9
+
10
+ ## Extensions
11
+
12
+ | Extension | Description | Link |
13
+ | --------- | ----------- | ---- |
14
+ | bootstrap | Startup bootstrap — symlinks `agents/AGENTS.md` to `~/.pi/agent/AGENTS.md`; `/doctor` command + `bun run doctor` CLI validate the whole rifle installation | [./bootstrap/](./bootstrap/) |
15
+ | ask-user-question | `ask_user_question` tool — pauses execution for interactive text / single-select / multi-select questions with an Other free-text escape hatch | [./ask-user-question/](./ask-user-question/) |
16
+ | prompt-snippets | One-shot prompt snippets — alt+s / `/snippets` toggle menu, bodies prepended/appended to the next message, widget shows active set | [./prompt-snippets/](./prompt-snippets/) |
17
+ | tool-pills | Colored pill badges + collapsed output for ls/read/find/grep/bash, Nerd Font file icons (`ls` renders as a tree; `PI_TOOL_PILLS_ICONS=off` disables), find/grep accelerated by the [@ff-labs/fff-node](https://www.npmjs.com/package/@ff-labs/fff-node) frecency index of the session cwd (falls back to the built-in SDK tools when unavailable), and Shiki-powered syntax-highlighted diffs for write/edit; its fff database lives at `~/.pi/agent/pi-rifle-batteries/tool-pills/fff/` (a legacy `~/.pi/agent/pi-tool-pills/fff/` is migrated there on first load and never destroyed) | [./tool-pills/](./tool-pills/) |
18
+ | desktop-notify | `/notify` command — desktop notifications (notify-send) when pi finishes work after an idle period, mirrored into herdr inside a herdr session, plus ntfy push; also pages on blocking prompts | [./desktop-notify/](./desktop-notify/) |
19
+ | sudo | `sudo_run` tool — root command execution behind an Allow/Deny confirmation + masked password prompt (password piped to `sudo -S`, never persisted); auto-denies after 60s of inactivity, and fires a best-effort attention alert (critical desktop notification + herdr `request` sound) while the prompt waits | [./sudo/](./sudo/) |
20
+ | herdr-title | Renames the current herdr pane to `Pi - <cwd>` on session start, so panes are identifiable in the workspace; no-op outside herdr | [./herdr-title/](./herdr-title/) |
21
+
22
+ <!-- AIDEV-NOTE: PMR-8YDK — seed table: rows were pre-seeded pointing at the
23
+ future in-package module dirs and became real as each absorb landed (one per
24
+ commit; desktop-notify landed in 5.2, tool-pills completed the set in 8.1 —
25
+ all seven rows are real now). Per-extension Configuration sections arrive
26
+ with their absorbs, not before; tool-pills has no config.json (its knobs are
27
+ PI_TOOL_PILLS_ICONS and the migrated fff DB dir, documented in its row). -->
28
+
29
+ ## Configuration
30
+
31
+ ### ask-user-question
32
+
33
+ Global config lives at `~/.pi/agent/pi-rifle-batteries/ask-user-question/config.json` (a legacy `~/.pi/agent/pi-ask-user-question/config.json` is migrated there on first load and never destroyed). A project override at `<cwd>/.pi/pi-rifle-batteries/ask-user-question/config.json` replaces the global `styleRules` list per project. Each `styleRules` entry is appended verbatim as a prompt guideline for `ask_user_question`, so your question-style preferences are in the model's context every time it asks a question (config changes need a session restart).
34
+
35
+ | Option | Type | Default | Description |
36
+ |--------|------|---------|-------------|
37
+ | `styleRules` | string[] | `[]` | Question-style rules, appended in order after the tool's 7 base guidelines |
38
+
39
+ ```json
40
+ {
41
+ "$schema": "config.schema.json",
42
+ "styleRules": []
43
+ }
44
+ ```
45
+
46
+ ### desktop-notify
47
+
48
+ Global config lives at `~/.pi/agent/pi-rifle-batteries/desktop-notify/config.json` (a legacy `~/.pi/agent/pi-desktop-notify/config.json` is migrated there on first load and never destroyed). A project override at `<cwd>/.pi/pi-rifle-batteries/desktop-notify/config.json` wins per-section. Create the file (and the `ntfy` section) to enable ntfy pushes — the channel is off while the section is absent.
49
+
50
+ | Option | Type | Default | Description |
51
+ |--------|------|---------|-------------|
52
+ | `desktop.enabled` | boolean | `true` | Send `notify-send` desktop notifications |
53
+ | `herdr.enabled` | boolean | `true` | Mirror notifications into herdr when `HERDR_ENV=1` |
54
+ | `herdr.sound` | string | `done` | herdr notification sound name |
55
+ | `ntfy` | object | — | ntfy section; presence enables the channel (unless `enabled: false`) |
56
+ | `ntfy.serverUrl` | string | — | **Required.** Base URL, e.g. `https://ntfy.homelab.local` |
57
+ | `ntfy.topic` | string | — | **Required.** Topic to publish to (secret topics work too) |
58
+ | `ntfy.token` | string | — | Bearer token for access control; `PI_NTFY_TOKEN` env wins over this |
59
+ | `ntfy.priority` | integer | `3` | ntfy priority 1–5 (5 = max priority, persistent) |
60
+ | `ntfy.tags` | string[] | `[]` | Emoji tags, e.g. `["robot", "white_check_mark"]` |
61
+ | `ntfy.clickUrl` | string | — | URL opened when the notification is tapped |
62
+ | `ntfy.timeoutMs` | integer | `5000` | HTTP timeout for the publish request |
63
+ | `notifyOnPrompt` | boolean | `true` | Also notify when Pi shows a blocking user prompt |
64
+
65
+ ```json
66
+ {
67
+ "$schema": "https://raw.githubusercontent.com/gtheys/pi-my-rifle-ext/main/packages/rifle-batteries/desktop-notify/config.schema.json",
68
+ "desktop": {
69
+ "enabled": true
70
+ },
71
+ "herdr": {
72
+ "enabled": true,
73
+ "sound": "done"
74
+ },
75
+ "ntfy": {
76
+ "enabled": true,
77
+ "serverUrl": "https://ntfy.homelab.local",
78
+ "topic": "pi-notifications",
79
+ "priority": 3,
80
+ "tags": ["robot"],
81
+ "timeoutMs": 5000
82
+ },
83
+ "notifyOnPrompt": true
84
+ }
85
+ ```
86
+
87
+ Minimal homelab setup:
88
+
89
+ ```json
90
+ {
91
+ "ntfy": {
92
+ "serverUrl": "https://ntfy.homelab.local",
93
+ "topic": "pi-notifications"
94
+ }
95
+ }
96
+ ```
97
+
98
+ If your ntfy topic requires access control, prefer the `PI_NTFY_TOKEN` environment variable over putting the token in the config file.
99
+
100
+ All channels are best-effort: an unreachable ntfy server or missing `notify-send` never interrupts a Pi session. A present-but-malformed config file is reported as an error at session start instead of being silently ignored.
101
+
102
+ ## Library subpath (`./notify`)
103
+
104
+ The package also exports a node-only `@gtheys/rifle-batteries/notify`
105
+ subpath: `dispatchNotification(config, summary, body)` plus the re-exported
106
+ `loadConfig` and config types. It reuses the desktop-notify module's channel
107
+ dispatch (desktop / herdr / ntfy, best-effort per channel) without loading
108
+ the extension, so another extension can page through the **same user channel
109
+ toggles**. It is currently consumed by [`pi-flow`](../pi-flow/README.md),
110
+ which dynamically imports `@gtheys/rifle-batteries/notify` to send a desktop
111
+ notification on a `waiting_human` / `halted` flow transition (PMR-ZV9Y; the
112
+ subpath moved here from the retired `@gtheys/pi-desktop-notify` package in
113
+ PMR-8YDK 5.2). The subpath keeps the node-only bar: it never imports the pi
114
+ extension runtime directly — only node built-ins plus `./config.ts` (which
115
+ reaches `@earendil-works/pi-coding-agent` for `getAgentDir()` transitively;
116
+ accepted, because the only consumer runs inside a Pi session already).
@@ -0,0 +1,13 @@
1
+ {
2
+ "type": "object",
3
+ "properties": {
4
+ "styleRules": {
5
+ "type": "array",
6
+ "items": {
7
+ "type": "string",
8
+ "description": "One question-style rule in plain English, appended verbatim as an ask_user_question prompt guideline"
9
+ },
10
+ "description": "Question-style rules shown to the model whenever it composes an ask_user_question question"
11
+ }
12
+ }
13
+ }
@@ -0,0 +1,155 @@
1
+ // AIDEV-NOTE: TypeBox schema is the source of truth for config shape;
2
+ // config.schema.json (checked in) mirrors it. Boundary parsing lives in the
3
+ // shared loader (../shared/config-loader.ts): JSON.parse -> unknown ->
4
+ // Value.Check -> typed. A present-but-malformed config throws a clear error
5
+ // naming the file — never silent defaults (AGENTS.md rule). The THROW is the
6
+ // boundary contract: readConfigFile/loadConfig always throw; it is the
7
+ // extension factory in index.ts that catches the error and degrades to the
8
+ // base guidelines (PMR-0WJ7), so a config typo costs the style rules — not
9
+ // the ask_user_question tool itself. Reads are synchronous by design:
10
+ // loadConfig runs ONCE in the extension factory before pi.registerTool, and
11
+ // promptGuidelines must be final by the time registerTool returns (the
12
+ // factory stays sync so the existing test harness can call it without
13
+ // awaiting).
14
+ // PMR-8YDK 7.1 unified the config dirs: global path is
15
+ // getAgentDir()/pi-rifle-batteries/ask-user-question/config.json, migrated
16
+ // idempotently (never-destroy) from the legacy getAgentDir()/pi-ask-user-question/
17
+ // config.json; the project override <cwd>/CONFIG_DIR_NAME/pi-rifle-batteries/ask-user-question/config.json
18
+ // wins (migrated from the legacy <cwd>/CONFIG_DIR_NAME/pi-ask-user-question/
19
+ // the same way) and its styleRules REPLACE the global list when present
20
+ // (even as an empty array), so a project can opt out of the global rules
21
+ // entirely.
22
+ // Scaffold: the factory calls scaffoldGlobalConfig() once before loadConfig.
23
+ // Migration runs BEFORE the default write: a legacy global config is copied
24
+ // to the unified dir first, so the default scaffold never shadows the user's
25
+ // real config. A config still absent after migration is created with the
26
+ // default content (DEFAULT_ASK_USER_QUESTION_CONFIG, pretty-printed,
27
+ // trailing newline); an EXISTING config file — valid or malformed — is never
28
+ // overwritten, so the strict parse contract still applies to it
29
+ // byte-for-byte. The schema copy in the unified dir is a machine mirror, not
30
+ // user config: created when missing, refreshed from the bundled
31
+ // config.schema.json (read relative to this file — co-located since 7.1)
32
+ // whenever its content differs. Scaffold is best-effort convenience: any
33
+ // failure (permissions, odd fs state) is swallowed so the extension still
34
+ // loads; the parse contract stays strict regardless.
35
+ // A "$schema" key is tolerated: the schema has no additionalProperties
36
+ // guard, mirroring pi-desktop-notify.
37
+
38
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
39
+ import path from 'node:path'
40
+ import { getAgentDir } from '@earendil-works/pi-coding-agent'
41
+ import { type Static, Type } from 'typebox'
42
+ import { readJsonChecked, readLayered } from '../shared/config-loader.ts'
43
+ import { migratedPath } from '../shared/migrate.ts'
44
+
45
+ export const AskUserQuestionConfigSchema = Type.Object({
46
+ styleRules: Type.Optional(
47
+ Type.Array(
48
+ Type.String({
49
+ description:
50
+ 'One question-style rule in plain English, appended verbatim as an ask_user_question prompt guideline',
51
+ }),
52
+ {
53
+ description:
54
+ 'Question-style rules shown to the model whenever it composes an ask_user_question question',
55
+ },
56
+ ),
57
+ ),
58
+ })
59
+
60
+ export type AskUserQuestionConfig = Static<typeof AskUserQuestionConfigSchema>
61
+
62
+ /** Normalized config: loadConfig/mergeConfig always resolve styleRules to an array. */
63
+ export interface ResolvedAskUserQuestionConfig {
64
+ styleRules: string[]
65
+ }
66
+
67
+ /** Exact content scaffolded into a missing global config.json. */
68
+ export const DEFAULT_ASK_USER_QUESTION_CONFIG = {
69
+ $schema: 'config.schema.json',
70
+ styleRules: [] as string[],
71
+ }
72
+
73
+ /**
74
+ * Best-effort scaffold of the unified global config dir, called once by the
75
+ * extension factory before loadConfig. Migrates the legacy
76
+ * pi-ask-user-question/config.json FIRST (copy-once, never-destroy), then
77
+ * writes defaults only if the unified config is still absent; existing
78
+ * config files are never touched; failures are swallowed (see header
79
+ * AIDEV-NOTE).
80
+ */
81
+ export function scaffoldGlobalConfig(): void {
82
+ try {
83
+ const dir = path.join(
84
+ getAgentDir(),
85
+ 'pi-rifle-batteries',
86
+ 'ask-user-question',
87
+ )
88
+ const configFile = path.join(dir, 'config.json')
89
+ // AIDEV-NOTE: PMR-8YDK — migration BEFORE scaffold: a legacy
90
+ // pi-ask-user-question/config.json is copied to the unified dir first so
91
+ // the default scaffold never shadows the user's real config. Existing
92
+ // files are never overwritten; failures are swallowed (convenience only).
93
+ migratedPath(
94
+ path.join(getAgentDir(), 'pi-ask-user-question', 'config.json'),
95
+ configFile,
96
+ )
97
+ if (!existsSync(configFile)) {
98
+ mkdirSync(dir, { recursive: true })
99
+ writeFileSync(
100
+ configFile,
101
+ `${JSON.stringify(DEFAULT_ASK_USER_QUESTION_CONFIG, null, 2)}\n`,
102
+ 'utf8',
103
+ )
104
+ }
105
+ const bundledSchema = readFileSync(
106
+ new URL('./config.schema.json', import.meta.url),
107
+ 'utf8',
108
+ )
109
+ const schemaFile = path.join(dir, 'config.schema.json')
110
+ let schemaUpToDate = false
111
+ try {
112
+ schemaUpToDate = readFileSync(schemaFile, 'utf8') === bundledSchema
113
+ } catch {
114
+ schemaUpToDate = false
115
+ }
116
+ if (!schemaUpToDate) {
117
+ mkdirSync(dir, { recursive: true })
118
+ writeFileSync(schemaFile, bundledSchema, 'utf8')
119
+ }
120
+ } catch {
121
+ // AIDEV-NOTE: scaffold is convenience only — never block extension load
122
+ // on it. The strict parse contract lives in readConfigFile/loadConfig.
123
+ }
124
+ }
125
+
126
+ export function readConfigFile(file: string): AskUserQuestionConfig {
127
+ return readJsonChecked<AskUserQuestionConfig>(
128
+ file,
129
+ AskUserQuestionConfigSchema,
130
+ 'pi-ask-user-question',
131
+ )
132
+ }
133
+
134
+ export function mergeConfig(
135
+ base: AskUserQuestionConfig,
136
+ override: AskUserQuestionConfig,
137
+ ): ResolvedAskUserQuestionConfig {
138
+ let styleRules: string[] = []
139
+ if (override.styleRules !== undefined) {
140
+ styleRules = override.styleRules
141
+ } else if (base.styleRules !== undefined) {
142
+ styleRules = base.styleRules
143
+ }
144
+ return { styleRules }
145
+ }
146
+
147
+ export function loadConfig(cwd: string): ResolvedAskUserQuestionConfig {
148
+ const { global, project } = readLayered<AskUserQuestionConfig>(
149
+ cwd,
150
+ 'ask-user-question',
151
+ AskUserQuestionConfigSchema,
152
+ 'pi-ask-user-question',
153
+ )
154
+ return mergeConfig(global, project)
155
+ }