@herbertgao/pi-bark 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Herbert Gao
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,44 @@
1
+ # @herbertgao/pi-bark
2
+
3
+ Bark notifications for user-facing Pi sessions. It sends a notification when Pi fully settles and when [`@juicesharp/rpiv-ask-user-question`](https://github.com/juicesharp/rpiv-mono) opens a questionnaire. Requests follow Bark's official [POST form API](https://github.com/Finb/Bark/blob/master/docs/en-us/tutorial.md#request-methods).
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pi install npm:@herbertgao/pi-bark
9
+ ```
10
+
11
+ The extension is also included in `@herbertgao/pi-extensions`.
12
+
13
+ ## Configure
14
+
15
+ Create `$PI_CODING_AGENT_DIR/bark.json` (normally `~/.pi/agent/bark.json`):
16
+
17
+ ```json
18
+ {
19
+ "endpoint": "https://api.day.app/your-device-key",
20
+ "machine": "MacBook Pro M1 Max",
21
+ "locale": "zh-CN",
22
+ "events": {
23
+ "agentSettled": true,
24
+ "askUserQuestion": true
25
+ },
26
+ "params": {
27
+ "group": "pi",
28
+ "icon": "https://raw.githubusercontent.com/HerbertGao/pi-extensions/master/packages/pi-bark/assets/pi-icon.png"
29
+ },
30
+ "timeoutMs": 4000
31
+ }
32
+ ```
33
+
34
+ `endpoint` is required. `locale` accepts `auto` (default), `zh-CN`, `zh-TW`, or `en`; unsupported languages fall back to English. `params` accepts additional Bark POST parameters such as `group`, `sound`, `icon`, and `level`; dynamic `title` and `body` values always take precedence. Set `"enabled": false` to disable all notifications without removing the package.
35
+
36
+ The notification body uses `šŸ’»` for the configured machine name and `šŸ“` for Pi's full working directory. Questionnaire text is intentionally not sent because it may contain sensitive context. Only sessions with a user-facing UI notify, so nested `pi-subagents` sessions do not create duplicate pushes.
37
+
38
+ The bundled [`assets/pi-icon.png`](assets/pi-icon.png) is a 512Ɨ512 rasterization of Pi's official [square badge](https://pi.dev/favicon.svg) from the [Pi Press Kit](https://pi.dev/press-kit), suitable for Bark's iOS 15+ `icon` parameter.
39
+
40
+ Keep `bark.json` private because the endpoint normally contains the Bark device key. Restart Pi or run `/reload` after changing it.
41
+
42
+ ## License
43
+
44
+ [MIT](LICENSE)
Binary file
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@herbertgao/pi-bark",
3
+ "version": "0.1.0",
4
+ "description": "Send Bark notifications when Pi finishes or needs user input.",
5
+ "keywords": [
6
+ "bark",
7
+ "notifications",
8
+ "pi-extension",
9
+ "pi-package"
10
+ ],
11
+ "homepage": "https://github.com/HerbertGao/pi-extensions/tree/master/packages/pi-bark#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/HerbertGao/pi-extensions/issues"
14
+ },
15
+ "license": "MIT",
16
+ "author": "Herbert Gao",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/HerbertGao/pi-extensions.git",
20
+ "directory": "packages/pi-bark"
21
+ },
22
+ "files": [
23
+ "assets",
24
+ "src",
25
+ "README.md",
26
+ "LICENSE"
27
+ ],
28
+ "type": "module",
29
+ "publishConfig": {
30
+ "access": "public",
31
+ "provenance": true
32
+ },
33
+ "scripts": {
34
+ "test": "node --test tests/**/*.test.ts",
35
+ "typecheck": "tsc --noEmit"
36
+ },
37
+ "devDependencies": {
38
+ "@types/node": "^24",
39
+ "typescript": "^5.9.3"
40
+ },
41
+ "peerDependencies": {
42
+ "@earendil-works/pi-coding-agent": "^0.84.0"
43
+ },
44
+ "engines": {
45
+ "node": ">=22.19.0"
46
+ },
47
+ "pi": {
48
+ "extensions": [
49
+ "./src/index.ts"
50
+ ]
51
+ },
52
+ "x-origin": "original"
53
+ }
package/src/index.ts ADDED
@@ -0,0 +1,178 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"
2
+ import { readFileSync } from "node:fs"
3
+ import { homedir, hostname } from "node:os"
4
+ import { join } from "node:path"
5
+
6
+ const ASK_USER_PROMPT_EVENT = "rpiv:ask-user:prompt"
7
+ const DEFAULT_TIMEOUT_MS = 4_000
8
+
9
+ export type BarkLocale = "en" | "zh-CN" | "zh-TW"
10
+ type NotificationEvent = "agentSettled" | "askUserQuestion"
11
+
12
+ const COPY: Record<BarkLocale, Record<NotificationEvent, string>> = {
13
+ en: {
14
+ agentSettled: "āœ… Pi finished",
15
+ askUserQuestion: "🟔 Pi needs your input",
16
+ },
17
+ "zh-CN": {
18
+ agentSettled: "āœ… Pi č·‘å®Œäŗ†",
19
+ askUserQuestion: "🟔 Pi ē­‰ä½ å›žē­”",
20
+ },
21
+ "zh-TW": {
22
+ agentSettled: "āœ… Pi 已完成",
23
+ askUserQuestion: "🟔 Pi ē­‰ä½ å›žč¦†",
24
+ },
25
+ }
26
+
27
+ export interface BarkConfig {
28
+ endpoint: string
29
+ machine: string
30
+ locale: BarkLocale
31
+ events: {
32
+ agentSettled: boolean
33
+ askUserQuestion: boolean
34
+ }
35
+ params: Record<string, string | number | boolean>
36
+ timeoutMs: number
37
+ }
38
+
39
+ export function getConfigPath(): string {
40
+ return join(
41
+ process.env.PI_CODING_AGENT_DIR ?? join(homedir(), ".pi", "agent"),
42
+ "bark.json",
43
+ )
44
+ }
45
+
46
+ export function resolveLocale(input: unknown): BarkLocale {
47
+ const requested =
48
+ typeof input === "string" && input.toLowerCase() !== "auto"
49
+ ? input
50
+ : Intl.DateTimeFormat().resolvedOptions().locale
51
+ const locale = requested.toLowerCase()
52
+ if (locale.includes("hant") || /^zh-(tw|hk|mo)/.test(locale)) return "zh-TW"
53
+ if (locale.startsWith("zh")) return "zh-CN"
54
+ return "en"
55
+ }
56
+
57
+ export function notificationTitle(
58
+ locale: BarkLocale,
59
+ event: NotificationEvent,
60
+ ): string {
61
+ return COPY[locale][event]
62
+ }
63
+
64
+ export function normalizeConfig(input: unknown): BarkConfig | undefined {
65
+ if (!input || typeof input !== "object") return undefined
66
+ const source = input as Record<string, unknown>
67
+ if (source.enabled === false || typeof source.endpoint !== "string")
68
+ return undefined
69
+
70
+ try {
71
+ const protocol = new URL(source.endpoint).protocol
72
+ if (protocol !== "http:" && protocol !== "https:") return undefined
73
+ } catch {
74
+ return undefined
75
+ }
76
+
77
+ const eventSource =
78
+ source.events && typeof source.events === "object"
79
+ ? (source.events as Record<string, unknown>)
80
+ : {}
81
+ const params =
82
+ source.params && typeof source.params === "object"
83
+ ? Object.fromEntries(
84
+ Object.entries(source.params).filter((entry) =>
85
+ ["string", "number", "boolean"].includes(typeof entry[1]),
86
+ ),
87
+ )
88
+ : {}
89
+ const timeoutMs =
90
+ typeof source.timeoutMs === "number" &&
91
+ Number.isFinite(source.timeoutMs) &&
92
+ source.timeoutMs > 0
93
+ ? source.timeoutMs
94
+ : DEFAULT_TIMEOUT_MS
95
+
96
+ return {
97
+ endpoint: source.endpoint,
98
+ machine:
99
+ typeof source.machine === "string" && source.machine.trim()
100
+ ? source.machine.trim()
101
+ : hostname(),
102
+ locale: resolveLocale(source.locale),
103
+ events: {
104
+ agentSettled: eventSource.agentSettled !== false,
105
+ askUserQuestion: eventSource.askUserQuestion !== false,
106
+ },
107
+ params,
108
+ timeoutMs,
109
+ }
110
+ }
111
+
112
+ export function loadConfig(path = getConfigPath()): BarkConfig | undefined {
113
+ try {
114
+ return normalizeConfig(JSON.parse(readFileSync(path, "utf8")))
115
+ } catch {
116
+ return undefined
117
+ }
118
+ }
119
+
120
+ export function formatBody(config: BarkConfig, cwd: string): string {
121
+ return `šŸ’» ${config.machine}\nšŸ“ ${cwd}`
122
+ }
123
+
124
+ export async function sendBark(
125
+ config: BarkConfig,
126
+ title: string,
127
+ body: string,
128
+ fetcher: typeof fetch = fetch,
129
+ ): Promise<void> {
130
+ const form = new URLSearchParams()
131
+ for (const [key, value] of Object.entries(config.params))
132
+ form.set(key, String(value))
133
+ form.set("title", title)
134
+ form.set("body", body)
135
+
136
+ const response = await fetcher(config.endpoint, {
137
+ method: "POST",
138
+ body: form,
139
+ signal: AbortSignal.timeout(config.timeoutMs),
140
+ })
141
+ if (!response.ok) throw new Error(`Bark returned HTTP ${response.status}`)
142
+ }
143
+
144
+ export default function piBark(pi: ExtensionAPI): void {
145
+ let config: BarkConfig | undefined
146
+ let cwd = process.cwd()
147
+ let userFacingSession = false
148
+
149
+ const notify = async (title: string): Promise<void> => {
150
+ if (!config || !userFacingSession) return
151
+ try {
152
+ await sendBark(config, title, formatBody(config, cwd))
153
+ } catch {
154
+ // Notifications are best-effort and must never interrupt Pi.
155
+ }
156
+ }
157
+
158
+ const unsubscribeAskUser = pi.events.on(ASK_USER_PROMPT_EVENT, () => {
159
+ if (config?.events.askUserQuestion)
160
+ void notify(notificationTitle(config.locale, "askUserQuestion"))
161
+ })
162
+
163
+ pi.on("session_start", (_event, ctx) => {
164
+ config = loadConfig()
165
+ cwd = ctx.cwd
166
+ userFacingSession = ctx.hasUI
167
+ })
168
+
169
+ pi.on("agent_settled", (_event, ctx) => {
170
+ cwd = ctx.cwd
171
+ if (config?.events.agentSettled)
172
+ void notify(notificationTitle(config.locale, "agentSettled"))
173
+ })
174
+
175
+ pi.on("session_shutdown", () => {
176
+ unsubscribeAskUser()
177
+ })
178
+ }