claude-remote-approver 0.4.1 → 0.5.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
@@ -169,6 +169,7 @@ Config file location: `~/.claude-remote-approver.json`
169
169
  "topic": "cra-a1b2c3d4e5f67890abcdef1234567890",
170
170
  "ntfyServer": "https://ntfy.sh",
171
171
  "timeout": 120,
172
+ "planTimeout": 300,
172
173
  "autoApprove": [],
173
174
  "autoDeny": []
174
175
  }
@@ -179,6 +180,7 @@ Config file location: `~/.claude-remote-approver.json`
179
180
  | `topic` | `string` | `""` | Your unique ntfy topic. Generated by `setup`. |
180
181
  | `ntfyServer` | `string` | `"https://ntfy.sh"` | The ntfy server URL. Change this if you self-host. |
181
182
  | `timeout` | `number` | `120` | Seconds to wait for a response before auto-denying. |
183
+ | `planTimeout` | `number` | `300` | Seconds to wait for ExitPlanMode (plan approval) responses. Plan reviews need more reading time. |
182
184
  | `autoApprove` | `string[]` | `[]` | Reserved for future use. |
183
185
  | `autoDeny` | `string[]` | `[]` | Reserved for future use. |
184
186
 
package/bin/cli.mjs CHANGED
@@ -87,8 +87,8 @@ export async function main(args, deps) {
87
87
  try {
88
88
  input = JSON.parse(deps.stdin);
89
89
  } catch {
90
- const deny = { hookSpecificOutput: { hookEventName: "PermissionRequest", decision: { behavior: "deny" } } };
91
- deps.stdout.write(JSON.stringify(deny) + "\n");
90
+ const ask = { hookSpecificOutput: { hookEventName: "PermissionRequest", decision: { behavior: "ask" } } };
91
+ deps.stdout.write(JSON.stringify(ask) + "\n");
92
92
  break;
93
93
  }
94
94
 
@@ -96,8 +96,8 @@ export async function main(args, deps) {
96
96
  try {
97
97
  result = await deps.processHook(input, deps);
98
98
  } catch {
99
- const deny = { hookSpecificOutput: { hookEventName: "PermissionRequest", decision: { behavior: "deny" } } };
100
- deps.stdout.write(JSON.stringify(deny) + "\n");
99
+ const ask = { hookSpecificOutput: { hookEventName: "PermissionRequest", decision: { behavior: "ask" } } };
100
+ deps.stdout.write(JSON.stringify(ask) + "\n");
101
101
  break;
102
102
  }
103
103
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-remote-approver",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
4
4
  "description": "Approve or deny Claude Code permission prompts remotely from your phone via ntfy.sh",
5
5
  "type": "module",
6
6
  "bin": {
package/src/config.mjs CHANGED
@@ -9,6 +9,7 @@ export const DEFAULT_CONFIG = {
9
9
  topic: "",
10
10
  ntfyServer: "https://ntfy.sh",
11
11
  timeout: 120,
12
+ planTimeout: 300,
12
13
  // autoApprove/autoDeny are reserved for future use and not yet implemented
13
14
  autoApprove: [],
14
15
  autoDeny: [],
@@ -21,7 +22,8 @@ export function loadConfig(configPath = CONFIG_PATH) {
21
22
  const config = { ...DEFAULT_CONFIG, ...fileConfig };
22
23
  if (typeof config.topic !== "string") config.topic = DEFAULT_CONFIG.topic;
23
24
  if (typeof config.ntfyServer !== "string") config.ntfyServer = DEFAULT_CONFIG.ntfyServer;
24
- if (typeof config.timeout !== "number" || config.timeout <= 0) config.timeout = DEFAULT_CONFIG.timeout;
25
+ if (!Number.isFinite(config.timeout) || config.timeout <= 0) config.timeout = DEFAULT_CONFIG.timeout;
26
+ if (!Number.isFinite(config.planTimeout) || config.planTimeout <= 0) config.planTimeout = DEFAULT_CONFIG.planTimeout;
25
27
  if (!Array.isArray(config.autoApprove)) config.autoApprove = DEFAULT_CONFIG.autoApprove;
26
28
  if (!Array.isArray(config.autoDeny)) config.autoDeny = DEFAULT_CONFIG.autoDeny;
27
29
  return config;
package/src/hook.mjs CHANGED
@@ -1,6 +1,11 @@
1
1
  // src/hook.mjs
2
2
 
3
3
  import crypto from "node:crypto";
4
+ import { DEFAULT_CONFIG } from "./config.mjs";
5
+
6
+ const ASK = Object.freeze({ hookSpecificOutput: Object.freeze({ hookEventName: "PermissionRequest", decision: Object.freeze({ behavior: "ask" }) }) });
7
+ const DENY = Object.freeze({ hookSpecificOutput: Object.freeze({ hookEventName: "PermissionRequest", decision: Object.freeze({ behavior: "deny" }) }) });
8
+ const MAX_RETRIES = 3;
4
9
 
5
10
  /**
6
11
  * Build ntfy action buttons for Approve / Deny.
@@ -30,6 +35,135 @@ export function buildActions(server, topic, requestId) {
30
35
  ];
31
36
  }
32
37
 
38
+ /**
39
+ * Send with retry, returning null on exhausted retries.
40
+ */
41
+ export async function sendWithRetry(sendFn, params) {
42
+ for (let i = 0; i < MAX_RETRIES; i++) {
43
+ try {
44
+ return await sendFn(params);
45
+ } catch (err) {
46
+ if (i === MAX_RETRIES - 1) {
47
+ console.error("sendNotification failed:", err);
48
+ return null;
49
+ }
50
+ }
51
+ }
52
+ return null;
53
+ }
54
+
55
+ /**
56
+ * Check if the input is an AskUserQuestion tool call with questions.
57
+ */
58
+ export function isAskUserQuestion(input) {
59
+ return (
60
+ input?.tool_name === "AskUserQuestion" &&
61
+ Array.isArray(input?.tool_input?.questions) &&
62
+ input.tool_input.questions.length > 0
63
+ );
64
+ }
65
+
66
+ /**
67
+ * Build ntfy action buttons for question options.
68
+ */
69
+ export function buildQuestionActions(server, topic, requestId, options) {
70
+ const url = `${server}/${topic}-response`;
71
+ return options.map((opt) => ({
72
+ action: "http",
73
+ label: opt.label,
74
+ url,
75
+ body: JSON.stringify({ requestId, answer: opt.label }),
76
+ method: "POST",
77
+ }));
78
+ }
79
+
80
+ /**
81
+ * Build a human-readable message for a question with options.
82
+ */
83
+ export function buildQuestionMessage(question, options, opts = {}) {
84
+ const { multiSelect, batchInfo } = opts;
85
+ let msg = question;
86
+ if (batchInfo) msg += ` ${batchInfo}`;
87
+ if (multiSelect) msg += "\n(multiple selections allowed)";
88
+ msg += "\n\n";
89
+ for (const opt of options) {
90
+ msg += `• ${opt.label}: ${opt.description}\n`;
91
+ }
92
+ return msg.trimEnd();
93
+ }
94
+
95
+ /**
96
+ * Process an AskUserQuestion hook request.
97
+ */
98
+ export async function processAskUserQuestion(input, deps) {
99
+ const config = deps.loadConfig();
100
+ if (!config.topic) return ASK;
101
+
102
+ const questions = input.tool_input.questions;
103
+ const answers = {};
104
+
105
+ for (const q of questions) {
106
+ const requestId = crypto.randomUUID();
107
+ const options = q.options;
108
+
109
+ const MAX_BUTTONS = 3;
110
+ const batches = [];
111
+ for (let j = 0; j < options.length; j += MAX_BUTTONS) {
112
+ batches.push(options.slice(j, j + MAX_BUTTONS));
113
+ }
114
+
115
+ for (let i = 0; i < batches.length; i++) {
116
+ const batch = batches[i];
117
+ const batchInfo = batches.length > 1 ? `(${i + 1}/${batches.length})` : undefined;
118
+ const actions = buildQuestionActions(config.ntfyServer, config.topic, requestId, batch);
119
+ const message = buildQuestionMessage(q.question, batch, { multiSelect: q.multiSelect, batchInfo });
120
+
121
+ const sent = await sendWithRetry(deps.sendNotification, {
122
+ server: config.ntfyServer,
123
+ topic: config.topic,
124
+ title: `Claude Code: ${q.header || "Question"}`,
125
+ message,
126
+ actions,
127
+ requestId,
128
+ });
129
+ if (!sent) return ASK;
130
+ }
131
+
132
+ // AskUserQuestion uses standard timeout (not planTimeout)
133
+ let response;
134
+ try {
135
+ response = await deps.waitForResponse({
136
+ server: config.ntfyServer,
137
+ topic: config.topic,
138
+ requestId,
139
+ timeout: config.timeout * 1000,
140
+ });
141
+ } catch (err) {
142
+ console.error("waitForResponse failed:", err);
143
+ return ASK;
144
+ }
145
+
146
+ if (response.answer) {
147
+ answers[q.question] = response.answer;
148
+ } else {
149
+ return ASK;
150
+ }
151
+ }
152
+
153
+ return {
154
+ hookSpecificOutput: {
155
+ hookEventName: "PermissionRequest",
156
+ decision: {
157
+ behavior: "allow",
158
+ updatedInput: {
159
+ questions: input.tool_input.questions,
160
+ answers,
161
+ },
162
+ },
163
+ },
164
+ };
165
+ }
166
+
33
167
  /**
34
168
  * Process a Claude Code hook request.
35
169
  *
@@ -45,38 +179,43 @@ export async function processHook(input, { loadConfig, sendNotification, waitFor
45
179
  const config = loadConfig();
46
180
 
47
181
  if (!config.topic) {
48
- return { hookSpecificOutput: { hookEventName: "PermissionRequest", decision: { behavior: "deny" } } };
182
+ return ASK;
183
+ }
184
+
185
+ if (isAskUserQuestion(input)) {
186
+ return processAskUserQuestion(input, { loadConfig, sendNotification, waitForResponse });
49
187
  }
50
188
 
51
189
  const requestId = crypto.randomUUID();
52
190
  const { title, message } = formatToolInfo(input);
53
191
  const actions = buildActions(config.ntfyServer, config.topic, requestId);
54
192
 
55
- try {
56
- await sendNotification({
57
- server: config.ntfyServer,
58
- topic: config.topic,
59
- title,
60
- message,
61
- actions,
62
- requestId,
63
- });
64
- } catch (err) {
65
- return { hookSpecificOutput: { hookEventName: "PermissionRequest", decision: { behavior: "deny" } } };
66
- }
193
+ const sent = await sendWithRetry(sendNotification, {
194
+ server: config.ntfyServer,
195
+ topic: config.topic,
196
+ title,
197
+ message,
198
+ actions,
199
+ requestId,
200
+ });
201
+ if (!sent) return ASK;
67
202
 
68
203
  let response;
69
204
  try {
205
+ const isPlanReview = input.tool_name === "ExitPlanMode";
206
+ const timeout = (isPlanReview ? (config.planTimeout ?? DEFAULT_CONFIG.planTimeout) : config.timeout) * 1000;
70
207
  response = await waitForResponse({
71
208
  server: config.ntfyServer,
72
209
  topic: config.topic,
73
210
  requestId,
74
- timeout: config.timeout * 1000,
211
+ timeout,
75
212
  });
76
213
  } catch (err) {
77
- return { hookSpecificOutput: { hookEventName: "PermissionRequest", decision: { behavior: "deny" } } };
214
+ console.error("waitForResponse failed:", err);
215
+ return ASK;
78
216
  }
79
217
 
80
- const behavior = response.approved ? "allow" : "deny";
81
- return { hookSpecificOutput: { hookEventName: "PermissionRequest", decision: { behavior } } };
218
+ if (response.timeout || response.error) return ASK;
219
+ if (response.approved === false) return DENY;
220
+ return { hookSpecificOutput: { hookEventName: "PermissionRequest", decision: { behavior: "allow" } } };
82
221
  }
package/src/ntfy.mjs CHANGED
@@ -27,7 +27,7 @@ export async function sendNotification({ server, topic, title, message, actions,
27
27
  * Subscribe to the response topic via SSE and wait for a matching requestId.
28
28
  *
29
29
  * @param {{ server: string, topic: string, requestId: string, timeout: number }} params
30
- * @returns {Promise<{ approved: boolean }>}
30
+ * @returns {Promise<{ approved: boolean } | { timeout: true } | { error: Error } | { answer: string }>}
31
31
  */
32
32
  export async function waitForResponse({ server, topic, requestId, timeout }) {
33
33
  const baseUrl = server.replace(/\/+$/, '');
@@ -70,6 +70,9 @@ export async function waitForResponse({ server, topic, requestId, timeout }) {
70
70
  clearTimeout(timer);
71
71
  controller.signal.removeEventListener('abort', onAbort);
72
72
  controller.abort();
73
+ if (typeof parsed.answer === 'string') {
74
+ return { answer: parsed.answer };
75
+ }
73
76
  return { approved: parsed.approved };
74
77
  }
75
78
  } catch {
@@ -82,13 +85,14 @@ export async function waitForResponse({ server, topic, requestId, timeout }) {
82
85
  }
83
86
 
84
87
  clearTimeout(timer);
85
- return { approved: false };
88
+ return { timeout: true };
86
89
  } catch (err) {
87
90
  if (timer !== undefined) clearTimeout(timer);
88
- if (err?.name !== "AbortError") {
89
- console.error("[claude-remote-approver] waitForResponse error:", err);
91
+ if (err?.name === "AbortError") {
92
+ return { timeout: true };
90
93
  }
91
- return { approved: false };
94
+ console.error("[claude-remote-approver] waitForResponse error:", err);
95
+ return { error: err };
92
96
  }
93
97
  }
94
98